blob: e929a058835bbbdccbcd9303d9240230268f5424 [file] [log] [blame]
Christina Quast968b9742015-02-25 14:10:12 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/*----------------------------------------------------------------------------
31 * Headers
32 *----------------------------------------------------------------------------*/
33
34#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010035#include "simtrace.h"
Kévin Redona1012b12018-07-01 18:11:01 +020036#include "simtrace_usb.h"
Harald Welte2fb59962016-02-28 12:34:26 +010037#include "utils.h"
Christina Quast968b9742015-02-25 14:10:12 +010038
Christina Quastdb7b1ab2015-03-03 12:34:36 +010039#include <cciddriverdescriptors.h>
Harald Welte7ed6f3b2017-02-26 17:00:43 +010040#include <usb/common/dfu/usb_dfu.h>
41#include <usb/device/dfu/dfu.h>
Christina Quastdb7b1ab2015-03-03 12:34:36 +010042
Christina Quastf5549502015-02-24 14:27:08 +010043/*------------------------------------------------------------------------------
44 * USB String descriptors
45 *------------------------------------------------------------------------------*/
Harald Welte2363fa02017-03-05 10:16:25 +010046#include "usb_strings_generated.h"
Christina Quastf5549502015-02-24 14:27:08 +010047enum strDescNum {
Harald Welte3b646952017-05-14 23:13:52 +020048 MANUF_STR = 1,
49 PRODUCT_STRING,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010050 SNIFFER_CONF_STR,
51 CCID_CONF_STR,
52 PHONE_CONF_STR,
53 MITM_CONF_STR,
54 CARDEM_USIM1_INTF_STR,
55 CARDEM_USIM2_INTF_STR,
56 STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +010057};
58
Christina Quastf5549502015-02-24 14:27:08 +010059/*------------------------------------------------------------------------------
60 * USB Device descriptors
61 *------------------------------------------------------------------------------*/
62
Harald Welte2fb59962016-02-28 12:34:26 +010063#ifdef HAVE_SNIFFER
Christina Quastf5549502015-02-24 14:27:08 +010064typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
65
Harald Welte7dd3dfd2016-03-03 12:32:04 +010066 /** Standard configuration descriptor. */
67 USBConfigurationDescriptor configuration;
68 USBInterfaceDescriptor sniffer;
69 USBEndpointDescriptor sniffer_dataOut;
70 USBEndpointDescriptor sniffer_dataIn;
71 USBEndpointDescriptor sniffer_interruptIn;
Harald Welte7ed6f3b2017-02-26 17:00:43 +010072 DFURT_IF_DESCRIPTOR_STRUCT;
Christina Quast01bbdc32015-02-24 17:38:45 +010073} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +010074
Harald Welte7dd3dfd2016-03-03 12:32:04 +010075static const SIMTraceDriverConfigurationDescriptorSniffer
76 configurationDescriptorSniffer = {
77 /* Standard configuration descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +010078 .configuration = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010079 .bLength = sizeof(USBConfigurationDescriptor),
80 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
81 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
Harald Welte7ed6f3b2017-02-26 17:00:43 +010082 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010083 .bConfigurationValue = CFG_NUM_SNIFF,
84 .iConfiguration = SNIFFER_CONF_STR,
85 .bmAttributes = USBD_BMATTRIBUTES,
86 .bMaxPower = USBConfigurationDescriptor_POWER(100),
87 },
88 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +010089 .sniffer = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010090 .bLength = sizeof(USBInterfaceDescriptor),
91 .bDescriptorType = USBGenericDescriptor_INTERFACE,
92 .bInterfaceNumber = 0,
93 .bAlternateSetting = 0,
94 .bNumEndpoints = 3,
Kévin Redona1012b12018-07-01 18:11:01 +020095 .bInterfaceClass = USB_CLASS_PROPRIETARY,
96 .bInterfaceSubClass = SIMTRACE_SNIFFER_USB_SUBCLASS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010097 .bInterfaceProtocol = 0,
98 .iInterface = SNIFFER_CONF_STR,
99 },
100 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100101 .sniffer_dataOut = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100102 .bLength = sizeof(USBEndpointDescriptor),
103 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
104 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
105 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200106 SIMTRACE_USB_EP_CARD_DATAOUT),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100107 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200108 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100109 .bInterval = 0,
110 },
111 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100112 .sniffer_dataIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100113 .bLength = sizeof(USBEndpointDescriptor),
114 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
115 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
116 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200117 SIMTRACE_USB_EP_CARD_DATAIN),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100118 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200119 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100120 .bInterval = 0,
121 },
122 // Notification endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100123 .sniffer_interruptIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100124 .bLength = sizeof(USBEndpointDescriptor),
125 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
126 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
127 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200128 SIMTRACE_USB_EP_CARD_INT),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100129 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200130 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100131 .bInterval = 0x10,
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100132 },
133 DFURT_IF_DESCRIPTOR(1, 0),
Christina Quastf5549502015-02-24 14:27:08 +0100134};
Harald Welte2fb59962016-02-28 12:34:26 +0100135#endif /* HAVE_SNIFFER */
Christina Quastf5549502015-02-24 14:27:08 +0100136
Harald Welte2fb59962016-02-28 12:34:26 +0100137#ifdef HAVE_CCID
Harald Welteec4fe232015-11-07 18:41:25 +0100138static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100139 // Standard USB configuration descriptor
140 {
141 .bLength = sizeof(USBConfigurationDescriptor),
142 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
143 .wTotalLength = sizeof(CCIDDriverConfigurationDescriptors),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100144 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100145 .bConfigurationValue = CFG_NUM_CCID,
146 .iConfiguration = CCID_CONF_STR,
147 .bmAttributes = BOARD_USB_BMATTRIBUTES,
148 .bMaxPower = USBConfigurationDescriptor_POWER(100),
149 },
150 // CCID interface descriptor
151 // Table 4.3-1 Interface Descriptor
152 // Interface descriptor
153 {
154 .bLength = sizeof(USBInterfaceDescriptor),
155 .bDescriptorType = USBGenericDescriptor_INTERFACE,
156 .bInterfaceNumber = 0,
157 .bAlternateSetting = 0,
158 .bNumEndpoints = 3,
159 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
160 .bInterfaceSubClass = 0,
161 .bInterfaceProtocol = 0,
162 .iInterface = CCID_CONF_STR,
163 },
164 {
165 .bLength = sizeof(CCIDDescriptor),
166 .bDescriptorType = CCID_DECRIPTOR_TYPE,
167 .bcdCCID = CCID1_10, // CCID version
168 .bMaxSlotIndex = 0, // 1 slot
169 .bVoltageSupport = VOLTS_3_0,
170 .dwProtocols = (1 << PROTOCOL_TO),
171 .dwDefaultClock = 3580,
172 .dwMaximumClock = 3580,
173 .bNumClockSupported = 0,
174 .dwDataRate = 9600,
175 .dwMaxDataRate = 9600,
176 .bNumDataRatesSupported = 0,
177 .dwMaxIFSD = 0xfe,
178 .dwSynchProtocols = 0,
179 .dwMechanical = 0,
180 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
181 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
182 CCID_FEATURES_EXC_TPDU,
183 .dwMaxCCIDMessageLength = 271, /* For extended APDU
184 level the value shall
185 be between 261 + 10 */
186 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
187 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
188 .wLcdLayout = 0, // wLcdLayout: no LCD
189 .bPINSupport = 0, // bPINSupport: No PIN
190 .bMaxCCIDBusySlots = 1,
191 },
192 // Bulk-OUT endpoint descriptor
193 {
194 .bLength = sizeof(USBEndpointDescriptor),
195 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
196 .bEndpointAddress =
197 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
198 CCID_EPT_DATA_OUT),
199 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200200 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100201 .bInterval = 0x00,
202 },
203 // Bulk-IN endpoint descriptor
204 {
205 .bLength = sizeof(USBEndpointDescriptor),
206 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
207 .bEndpointAddress =
208 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
209 CCID_EPT_DATA_IN),
210 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200211 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100212 .bInterval = 0x00,
213 },
214 // Notification endpoint descriptor
215 {
216 .bLength = sizeof(USBEndpointDescriptor),
217 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
218 .bEndpointAddress =
219 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
220 CCID_EPT_NOTIFICATION),
221 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200222 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100223 .bInterval = 0x10,
224 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100225 DFURT_IF_DESCRIPTOR(1, 0),
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100226};
Harald Welte2fb59962016-02-28 12:34:26 +0100227#endif /* HAVE_CCID */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100228
Harald Welte2fb59962016-02-28 12:34:26 +0100229#ifdef HAVE_CARDEM
Christina Quast01bbdc32015-02-24 17:38:45 +0100230/* SIM card emulator */
231typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100232 /* Standard configuration descriptor. */
233 USBConfigurationDescriptor configuration;
234 USBInterfaceDescriptor phone;
235 USBEndpointDescriptor phone_dataOut;
236 USBEndpointDescriptor phone_dataIn;
237 USBEndpointDescriptor phone_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100238#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100239 USBInterfaceDescriptor usim2;
240 USBEndpointDescriptor usim2_dataOut;
241 USBEndpointDescriptor usim2_dataIn;
242 USBEndpointDescriptor usim2_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100243#endif
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100244 DFURT_IF_DESCRIPTOR_STRUCT;
Christina Quast01bbdc32015-02-24 17:38:45 +0100245} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
246
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100247static const SIMTraceDriverConfigurationDescriptorPhone
248 configurationDescriptorPhone = {
249 /* Standard configuration descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100250 .configuration = {
251 .bLength = sizeof(USBConfigurationDescriptor),
252 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
253 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Harald Welte57b3a252016-03-02 15:20:27 +0100254#ifdef CARDEMU_SECOND_UART
Harald Welte495a67d2017-03-06 09:55:37 +0100255 .bNumInterfaces = 2+DFURT_NUM_IF,
Harald Welte57b3a252016-03-02 15:20:27 +0100256#else
Harald Weltef2315412017-11-28 19:16:10 +0100257 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte57b3a252016-03-02 15:20:27 +0100258#endif
Harald Welte495a67d2017-03-06 09:55:37 +0100259 .bConfigurationValue = CFG_NUM_PHONE,
260 .iConfiguration = PHONE_CONF_STR,
261 .bmAttributes = USBD_BMATTRIBUTES,
262 .bMaxPower = USBConfigurationDescriptor_POWER(100)
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100263 },
264 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100265 .phone = {
266 .bLength = sizeof(USBInterfaceDescriptor),
267 .bDescriptorType = USBGenericDescriptor_INTERFACE,
268 .bInterfaceNumber = 0,
269 .bAlternateSetting = 0,
270 .bNumEndpoints = 3,
Kévin Redona1012b12018-07-01 18:11:01 +0200271 .bInterfaceClass = USB_CLASS_PROPRIETARY,
272 .bInterfaceSubClass = SIMTRACE_CARDEM_USB_SUBCLASS,
Harald Welte495a67d2017-03-06 09:55:37 +0100273 .bInterfaceProtocol = 0,
274 .iInterface = CARDEM_USIM1_INTF_STR,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100275 },
276 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100277 .phone_dataOut = {
278 .bLength = sizeof(USBEndpointDescriptor),
279 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
280 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
281 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200282 SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT),
Harald Welte495a67d2017-03-06 09:55:37 +0100283 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200284 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100285 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100286 },
287 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100288 .phone_dataIn = {
289 .bLength = sizeof(USBEndpointDescriptor),
290 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
291 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
292 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200293 SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN),
Harald Welte495a67d2017-03-06 09:55:37 +0100294 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200295 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100296 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100297 },
298 /* Notification endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100299 .phone_interruptIn = {
300 .bLength = sizeof(USBEndpointDescriptor),
301 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
302 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
303 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200304 SIMTRACE_CARDEM_USB_EP_USIM1_INT),
Harald Welte495a67d2017-03-06 09:55:37 +0100305 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200306 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100307 .bInterval = 0x10
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100308 },
Harald Welte57b3a252016-03-02 15:20:27 +0100309#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100310 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100311 .usim2 = {
312 .bLength = sizeof(USBInterfaceDescriptor),
313 .bDescriptorType = USBGenericDescriptor_INTERFACE,
314 .bInterfaceNumber = 1,
315 .bAlternateSetting = 0,
316 .bNumEndpoints = 3,
Kévin Redona1012b12018-07-01 18:11:01 +0200317 .bInterfaceClass = USB_CLASS_PROPRIETARY,
318 .bInterfaceSubClass = SIMTRACE_CARDEM_USB_SUBCLASS,
Harald Welte495a67d2017-03-06 09:55:37 +0100319 .bInterfaceProtocol = 0,
320 .iInterface = CARDEM_USIM2_INTF_STR,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100321 },
322 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100323 .usim2_dataOut = {
324 .bLength = sizeof(USBEndpointDescriptor),
325 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
326 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
327 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200328 SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT),
Harald Welte495a67d2017-03-06 09:55:37 +0100329 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200330 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100331 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100332 }
333 ,
334 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100335 .usim2_dataIn = {
336 .bLength = sizeof(USBEndpointDescriptor),
337 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
338 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
339 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200340 SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN),
Harald Welte495a67d2017-03-06 09:55:37 +0100341 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200342 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100343 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100344 },
345 /* Notification endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100346 .usim2_interruptIn = {
347 .bLength = sizeof(USBEndpointDescriptor),
348 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
349 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
350 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200351 SIMTRACE_CARDEM_USB_EP_USIM2_INT),
Harald Welte495a67d2017-03-06 09:55:37 +0100352 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200353 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100354 .bInterval = 0x10,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100355 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100356 DFURT_IF_DESCRIPTOR(2, 0),
357#else
358 DFURT_IF_DESCRIPTOR(1, 0),
Harald Welte57b3a252016-03-02 15:20:27 +0100359#endif
Christina Quast01bbdc32015-02-24 17:38:45 +0100360};
Harald Welte2fb59962016-02-28 12:34:26 +0100361#endif /* HAVE_CARDEM */
Christina Quast01bbdc32015-02-24 17:38:45 +0100362
Harald Welte2fb59962016-02-28 12:34:26 +0100363#ifdef HAVE_MITM
Christina Quastf5549502015-02-24 14:27:08 +0100364typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100365 /* Standard configuration descriptor. */
366 USBConfigurationDescriptor configuration;
367 USBInterfaceDescriptor simcard;
368 /// CCID descriptor
369 CCIDDescriptor ccid;
370 /// Bulk OUT endpoint descriptor
371 USBEndpointDescriptor simcard_dataOut;
372 /// Bulk IN endpoint descriptor
373 USBEndpointDescriptor simcard_dataIn;
374 /// Interrupt OUT endpoint descriptor
375 USBEndpointDescriptor simcard_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100376
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100377 USBInterfaceDescriptor phone;
378 USBEndpointDescriptor phone_dataOut;
379 USBEndpointDescriptor phone_dataIn;
380 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100381
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100382 DFURT_IF_DESCRIPTOR_STRUCT;
383
Christina Quast01bbdc32015-02-24 17:38:45 +0100384} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100385
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100386static const SIMTraceDriverConfigurationDescriptorMITM
387 configurationDescriptorMITM = {
388 /* Standard configuration descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100389 .configuration = {
390 .bLength = sizeof(USBConfigurationDescriptor),
391 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
392 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorMITM),
393 .bNumInterfaces = 2+DFURT_NUM_IF,
394 .bConfigurationValue = CFG_NUM_MITM,
395 .iConfiguration = MITM_CONF_STR,
396 .bmAttributes = USBD_BMATTRIBUTES,
397 .bMaxPower = USBConfigurationDescriptor_POWER(100),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100398 },
399 // CCID interface descriptor
400 // Table 4.3-1 Interface Descriptor
401 // Interface descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100402 .simcard = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100403 .bLength = sizeof(USBInterfaceDescriptor),
404 .bDescriptorType = USBGenericDescriptor_INTERFACE,
405 .bInterfaceNumber = 0,
406 .bAlternateSetting = 0,
407 .bNumEndpoints = 3,
408 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
409 .bInterfaceSubClass = 0,
410 .bInterfaceProtocol = 0,
411 .iInterface = CCID_CONF_STR,
412 },
Harald Welte495a67d2017-03-06 09:55:37 +0100413 .ccid = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100414 .bLength = sizeof(CCIDDescriptor),
415 .bDescriptorType = CCID_DECRIPTOR_TYPE,
416 .bcdCCID = CCID1_10, // CCID version
417 .bMaxSlotIndex = 0, // 1 slot
418 .bVoltageSupport = VOLTS_3_0,
419 .dwProtocols = (1 << PROTOCOL_TO),
420 .dwDefaultClock = 3580,
421 .dwMaximumClock = 3580,
422 .bNumClockSupported = 0,
423 .dwDataRate = 9600,
424 .dwMaxDataRate = 9600,
425 .bNumDataRatesSupported = 0,
426 .dwMaxIFSD = 0xfe,
427 .dwSynchProtocols = 0,
428 .dwMechanical = 0,
429 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
430 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
431 CCID_FEATURES_EXC_TPDU,
432 .dwMaxCCIDMessageLength = 271, /* For extended APDU
433 level the value shall
434 be between 261 + 10 */
435 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
436 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
437 .wLcdLayout = 0, // wLcdLayout: no LCD
438 .bPINSupport = 0, // bPINSupport: No PIN
439 .bMaxCCIDBusySlots = 1,
440 },
441 // Bulk-OUT endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100442 .simcard_dataOut = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100443 .bLength = sizeof(USBEndpointDescriptor),
444 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
445 .bEndpointAddress =
446 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
447 CCID_EPT_DATA_OUT),
448 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200449 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100450 .bInterval = 0x00,
451 },
452 // Bulk-IN endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100453 .simcard_dataIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100454 .bLength = sizeof(USBEndpointDescriptor),
455 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
456 .bEndpointAddress =
457 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
458 CCID_EPT_DATA_IN),
459 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200460 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100461 .bInterval = 0x00,
462 },
463 // Notification endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100464 .simcard_interruptIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100465 .bLength = sizeof(USBEndpointDescriptor),
466 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
467 .bEndpointAddress =
468 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
469 CCID_EPT_NOTIFICATION),
470 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200471 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100472 .bInterval = 0x10,
473 },
474
475 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100476 .phone = {
477 .bLength = sizeof(USBInterfaceDescriptor),
478 .bDescriptorType = USBGenericDescriptor_INTERFACE,
479 .bInterfaceNumber = 1,
480 .bAlternateSetting = 0,
481 .bNumEndpoints = 3,
482 .bInterfaceClass = 0xff,
Harald Weltef2315412017-11-28 19:16:10 +0100483 .bInterfaceSubClass = SIMTRACE_SUBCLASS_CARDEM,
Harald Welte495a67d2017-03-06 09:55:37 +0100484 .bInterfaceProtocol = 0,
485 .iInterface = PHONE_CONF_STR,
486 },
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100487 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100488 .phone_dataOut = {
489 .bLength = sizeof(USBEndpointDescriptor),
490 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
491 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
492 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200493 SIMTRACE_USB_EP_PHONE_DATAOUT),
Harald Welte495a67d2017-03-06 09:55:37 +0100494 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200495 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100496 .bInterval = 0, /* Must be 0 for full-speed bulk endpoints */
497 },
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100498 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100499 .phone_dataIn = {
500 .bLength = sizeof(USBEndpointDescriptor),
501 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
502 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
503 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200504 SIMTRACE_USB_EP_PHONE_DATAIN),
Harald Welte495a67d2017-03-06 09:55:37 +0100505 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200506 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100507 .bInterval = 0, /* Must be 0 for full-speed bulk endpoints */
508 },
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100509 /* Notification endpoint descriptor */
510 {
Harald Welte495a67d2017-03-06 09:55:37 +0100511 .bLength = sizeof(USBEndpointDescriptor),
512 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
513 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
514 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200515 SIMTRACE_USB_EP_PHONE_INT),
Harald Welte495a67d2017-03-06 09:55:37 +0100516 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200517 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100518 .bInterval = 0x10
519 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100520 DFURT_IF_DESCRIPTOR(2, 0),
Christina Quastf5549502015-02-24 14:27:08 +0100521};
Harald Welte2fb59962016-02-28 12:34:26 +0100522#endif /* HAVE_CARDEM */
523
524const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
525#ifdef HAVE_SNIFFER
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100526 &configurationDescriptorSniffer.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100527#endif
528#ifdef HAVE_CCID
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100529 &configurationDescriptorCCID.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100530#endif
531#ifdef HAVE_CARDEM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100532 &configurationDescriptorPhone.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100533#endif
534#ifdef HAVE_MITM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100535 &configurationDescriptorMITM.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100536#endif
537};
Christina Quastf5549502015-02-24 14:27:08 +0100538
Christina Quastf5549502015-02-24 14:27:08 +0100539/** Standard USB device descriptor for the CDC serial driver */
540const USBDeviceDescriptor deviceDescriptor = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100541 .bLength = sizeof(USBDeviceDescriptor),
542 .bDescriptorType = USBGenericDescriptor_DEVICE,
543 .bcdUSB = USBDeviceDescriptor_USB2_00,
Harald Welteeab7e452017-03-06 10:04:56 +0100544 .bDeviceClass = 0,
545 .bDeviceSubClass = 0,
546 .bDeviceProtocol = 0,
Kévin Redona1012b12018-07-01 18:11:01 +0200547 .bMaxPacketSize0 = 64,
Harald Weltec6e482d2017-03-05 16:24:29 +0100548 .idVendor = BOARD_USB_VENDOR_ID,
549 .idProduct = BOARD_USB_PRODUCT_ID,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100550 .bcdDevice = 2, /* Release number */
551 .iManufacturer = MANUF_STR,
552 .iProduct = PRODUCT_STRING,
553 .iSerialNumber = 0,
554 .bNumConfigurations = ARRAY_SIZE(configurationDescriptorsArr),
Christina Quast01bbdc32015-02-24 17:38:45 +0100555};
556
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200557/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100558static const USBDDriverDescriptors driverDescriptors = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100559 &deviceDescriptor,
560 (const USBConfigurationDescriptor **)&(configurationDescriptorsArr), /* first full-speed configuration descriptor */
561 0, /* No full-speed device qualifier descriptor */
562 0, /* No full-speed other speed configuration */
563 0, /* No high-speed device descriptor */
564 0, /* No high-speed configuration descriptor */
565 0, /* No high-speed device qualifier descriptor */
566 0, /* No high-speed other speed configuration descriptor */
Harald Welte2363fa02017-03-05 10:16:25 +0100567 usb_strings,
568 ARRAY_SIZE(usb_strings),/* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100569};
570
Christina Quast0ae03142015-02-25 18:43:46 +0100571/*----------------------------------------------------------------------------
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100572 * Functions
Christina Quast968b9742015-02-25 14:10:12 +0100573 *----------------------------------------------------------------------------*/
574
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100575void SIMtrace_USB_Initialize(void)
Christina Quast968b9742015-02-25 14:10:12 +0100576{
Kévin Redonf5869d42018-06-17 22:31:21 +0200577
578 /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */
579 const Pin usb_dp_pullup = PIN_USB_PULLUP;
580 PIO_Configure(&usb_dp_pullup, 1);
581 PIO_Set(&usb_dp_pullup);
582 mdelay(15);
583 PIO_Clear(&usb_dp_pullup);
584
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100585 // Get std USB driver
586 USBDDriver *pUsbd = USBD_GetDriver();
Christina Quast968b9742015-02-25 14:10:12 +0100587
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100588 // Initialize standard USB driver
589 USBDDriver_Initialize(pUsbd, &driverDescriptors, 0); // Multiple interface settings not supported
590 USBD_Init();
591 USBD_Connect();
Christina Quast968b9742015-02-25 14:10:12 +0100592
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100593 NVIC_EnableIRQ(UDP_IRQn);
Christina Quast968b9742015-02-25 14:10:12 +0100594}