blob: 1d37914ddb9ad01b91d03d831df9457cff4f6aa8 [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"
Harald Welte2fb59962016-02-28 12:34:26 +010036#include "utils.h"
Christina Quast968b9742015-02-25 14:10:12 +010037
Christina Quastdb7b1ab2015-03-03 12:34:36 +010038#include <cciddriverdescriptors.h>
Harald Welte7ed6f3b2017-02-26 17:00:43 +010039#include <usb/common/dfu/usb_dfu.h>
40#include <usb/device/dfu/dfu.h>
Christina Quastdb7b1ab2015-03-03 12:34:36 +010041
Christina Quastf5549502015-02-24 14:27:08 +010042/*------------------------------------------------------------------------------
43 * USB String descriptors
44 *------------------------------------------------------------------------------*/
45
Christina Quast6032e792015-02-27 15:22:25 +010046static const unsigned char langDesc[] = {
47
Harald Welte7dd3dfd2016-03-03 12:32:04 +010048 USBStringDescriptor_LENGTH(1),
49 USBGenericDescriptor_STRING,
50 USBStringDescriptor_ENGLISH_US
Christina Quast6032e792015-02-27 15:22:25 +010051};
52
Harald Welte6d44c1f2015-11-07 19:01:30 +010053const unsigned char manufStringDescriptor[] = {
54
Harald Welte7dd3dfd2016-03-03 12:32:04 +010055 USBStringDescriptor_LENGTH(24),
56 USBGenericDescriptor_STRING,
57 USBStringDescriptor_UNICODE('s'),
58 USBStringDescriptor_UNICODE('y'),
59 USBStringDescriptor_UNICODE('s'),
60 USBStringDescriptor_UNICODE('m'),
61 USBStringDescriptor_UNICODE('o'),
62 USBStringDescriptor_UNICODE('c'),
63 USBStringDescriptor_UNICODE('o'),
64 USBStringDescriptor_UNICODE('m'),
65 USBStringDescriptor_UNICODE(' '),
66 USBStringDescriptor_UNICODE('-'),
67 USBStringDescriptor_UNICODE(' '),
68 USBStringDescriptor_UNICODE('s'),
69 USBStringDescriptor_UNICODE('.'),
70 USBStringDescriptor_UNICODE('f'),
71 USBStringDescriptor_UNICODE('.'),
72 USBStringDescriptor_UNICODE('m'),
73 USBStringDescriptor_UNICODE('.'),
74 USBStringDescriptor_UNICODE('c'),
75 USBStringDescriptor_UNICODE('.'),
76 USBStringDescriptor_UNICODE(' '),
77 USBStringDescriptor_UNICODE('G'),
78 USBStringDescriptor_UNICODE('m'),
79 USBStringDescriptor_UNICODE('b'),
80 USBStringDescriptor_UNICODE('H'),
Harald Welte6d44c1f2015-11-07 19:01:30 +010081};
82
Christina Quastf5549502015-02-24 14:27:08 +010083const unsigned char productStringDescriptor[] = {
84
Harald Welte7dd3dfd2016-03-03 12:32:04 +010085 USBStringDescriptor_LENGTH(10),
86 USBGenericDescriptor_STRING,
87 USBStringDescriptor_UNICODE('S'),
88 USBStringDescriptor_UNICODE('I'),
89 USBStringDescriptor_UNICODE('M'),
90 USBStringDescriptor_UNICODE('t'),
91 USBStringDescriptor_UNICODE('r'),
92 USBStringDescriptor_UNICODE('a'),
93 USBStringDescriptor_UNICODE('c'),
94 USBStringDescriptor_UNICODE('e'),
95 USBStringDescriptor_UNICODE(' '),
96 USBStringDescriptor_UNICODE('2'),
Christina Quastf5549502015-02-24 14:27:08 +010097};
98
99const unsigned char snifferConfigStringDescriptor[] = {
100
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100101 USBStringDescriptor_LENGTH(16),
102 USBGenericDescriptor_STRING,
103 USBStringDescriptor_UNICODE('S'),
104 USBStringDescriptor_UNICODE('I'),
105 USBStringDescriptor_UNICODE('M'),
106 USBStringDescriptor_UNICODE('t'),
107 USBStringDescriptor_UNICODE('r'),
108 USBStringDescriptor_UNICODE('a'),
109 USBStringDescriptor_UNICODE('c'),
110 USBStringDescriptor_UNICODE('e'),
111 USBStringDescriptor_UNICODE(' '),
112 USBStringDescriptor_UNICODE('S'),
113 USBStringDescriptor_UNICODE('n'),
114 USBStringDescriptor_UNICODE('i'),
115 USBStringDescriptor_UNICODE('f'),
116 USBStringDescriptor_UNICODE('f'),
117 USBStringDescriptor_UNICODE('e'),
118 USBStringDescriptor_UNICODE('r'),
Christina Quastf5549502015-02-24 14:27:08 +0100119};
120
121const unsigned char CCIDConfigStringDescriptor[] = {
122
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100123 USBStringDescriptor_LENGTH(13),
124 USBGenericDescriptor_STRING,
125 USBStringDescriptor_UNICODE('S'),
126 USBStringDescriptor_UNICODE('I'),
127 USBStringDescriptor_UNICODE('M'),
128 USBStringDescriptor_UNICODE('t'),
129 USBStringDescriptor_UNICODE('r'),
130 USBStringDescriptor_UNICODE('a'),
131 USBStringDescriptor_UNICODE('c'),
132 USBStringDescriptor_UNICODE('e'),
133 USBStringDescriptor_UNICODE(' '),
134 USBStringDescriptor_UNICODE('C'),
135 USBStringDescriptor_UNICODE('C'),
136 USBStringDescriptor_UNICODE('I'),
137 USBStringDescriptor_UNICODE('D'),
Christina Quastf5549502015-02-24 14:27:08 +0100138};
139
140const unsigned char phoneConfigStringDescriptor[] = {
141
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100142 USBStringDescriptor_LENGTH(14),
143 USBGenericDescriptor_STRING,
144 USBStringDescriptor_UNICODE('S'),
145 USBStringDescriptor_UNICODE('I'),
146 USBStringDescriptor_UNICODE('M'),
147 USBStringDescriptor_UNICODE('t'),
148 USBStringDescriptor_UNICODE('r'),
149 USBStringDescriptor_UNICODE('a'),
150 USBStringDescriptor_UNICODE('c'),
151 USBStringDescriptor_UNICODE('e'),
152 USBStringDescriptor_UNICODE(' '),
153 USBStringDescriptor_UNICODE('P'),
154 USBStringDescriptor_UNICODE('h'),
155 USBStringDescriptor_UNICODE('o'),
156 USBStringDescriptor_UNICODE('n'),
157 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100158};
159
Harald Weltebeb72932015-11-07 18:35:41 +0100160const unsigned char MITMConfigStringDescriptor[] = {
161
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100162 USBStringDescriptor_LENGTH(13),
163 USBGenericDescriptor_STRING,
164 USBStringDescriptor_UNICODE('S'),
165 USBStringDescriptor_UNICODE('I'),
166 USBStringDescriptor_UNICODE('M'),
167 USBStringDescriptor_UNICODE('t'),
168 USBStringDescriptor_UNICODE('r'),
169 USBStringDescriptor_UNICODE('a'),
170 USBStringDescriptor_UNICODE('c'),
171 USBStringDescriptor_UNICODE('e'),
172 USBStringDescriptor_UNICODE(' '),
173 USBStringDescriptor_UNICODE('M'),
174 USBStringDescriptor_UNICODE('I'),
175 USBStringDescriptor_UNICODE('T'),
176 USBStringDescriptor_UNICODE('M'),
Christina Quastf5549502015-02-24 14:27:08 +0100177};
178
Harald Welte57b3a252016-03-02 15:20:27 +0100179const unsigned char cardem_usim1_intf_str[] = {
180
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100181 USBStringDescriptor_LENGTH(18),
182 USBGenericDescriptor_STRING,
183 USBStringDescriptor_UNICODE('C'),
184 USBStringDescriptor_UNICODE('a'),
185 USBStringDescriptor_UNICODE('r'),
186 USBStringDescriptor_UNICODE('d'),
187 USBStringDescriptor_UNICODE('E'),
188 USBStringDescriptor_UNICODE('m'),
189 USBStringDescriptor_UNICODE('u'),
190 USBStringDescriptor_UNICODE('l'),
191 USBStringDescriptor_UNICODE('a'),
192 USBStringDescriptor_UNICODE('t'),
193 USBStringDescriptor_UNICODE('o'),
194 USBStringDescriptor_UNICODE('r'),
195 USBStringDescriptor_UNICODE(' '),
196 USBStringDescriptor_UNICODE('U'),
197 USBStringDescriptor_UNICODE('S'),
198 USBStringDescriptor_UNICODE('I'),
199 USBStringDescriptor_UNICODE('M'),
200 USBStringDescriptor_UNICODE('1'),
Harald Welte57b3a252016-03-02 15:20:27 +0100201};
202
203const unsigned char cardem_usim2_intf_str[] = {
204
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100205 USBStringDescriptor_LENGTH(18),
206 USBGenericDescriptor_STRING,
207 USBStringDescriptor_UNICODE('C'),
208 USBStringDescriptor_UNICODE('a'),
209 USBStringDescriptor_UNICODE('r'),
210 USBStringDescriptor_UNICODE('d'),
211 USBStringDescriptor_UNICODE('E'),
212 USBStringDescriptor_UNICODE('m'),
213 USBStringDescriptor_UNICODE('u'),
214 USBStringDescriptor_UNICODE('l'),
215 USBStringDescriptor_UNICODE('a'),
216 USBStringDescriptor_UNICODE('t'),
217 USBStringDescriptor_UNICODE('o'),
218 USBStringDescriptor_UNICODE('r'),
219 USBStringDescriptor_UNICODE(' '),
220 USBStringDescriptor_UNICODE('U'),
221 USBStringDescriptor_UNICODE('S'),
222 USBStringDescriptor_UNICODE('I'),
223 USBStringDescriptor_UNICODE('M'),
224 USBStringDescriptor_UNICODE('2'),
Harald Welte57b3a252016-03-02 15:20:27 +0100225};
226
Christina Quastf5549502015-02-24 14:27:08 +0100227enum strDescNum {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100228 PRODUCT_STRING = 1,
229 MANUF_STR,
230 SNIFFER_CONF_STR,
231 CCID_CONF_STR,
232 PHONE_CONF_STR,
233 MITM_CONF_STR,
234 CARDEM_USIM1_INTF_STR,
235 CARDEM_USIM2_INTF_STR,
236 STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +0100237};
238
239/** List of string descriptors used by the device */
Harald Welteec4fe232015-11-07 18:41:25 +0100240static const unsigned char *stringDescriptors[] = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100241 langDesc,
242 [PRODUCT_STRING] = productStringDescriptor,
243 [MANUF_STR] = manufStringDescriptor,
244 [SNIFFER_CONF_STR] = snifferConfigStringDescriptor,
245 [CCID_CONF_STR] = CCIDConfigStringDescriptor,
246 [PHONE_CONF_STR] = phoneConfigStringDescriptor,
247 [MITM_CONF_STR] = MITMConfigStringDescriptor,
248 [CARDEM_USIM1_INTF_STR] = cardem_usim1_intf_str,
249 [CARDEM_USIM2_INTF_STR] = cardem_usim2_intf_str,
Christina Quastf5549502015-02-24 14:27:08 +0100250};
251
Christina Quastf5549502015-02-24 14:27:08 +0100252/*------------------------------------------------------------------------------
253 * USB Device descriptors
254 *------------------------------------------------------------------------------*/
255
Harald Welte2fb59962016-02-28 12:34:26 +0100256#ifdef HAVE_SNIFFER
Christina Quastf5549502015-02-24 14:27:08 +0100257typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
258
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100259 /** Standard configuration descriptor. */
260 USBConfigurationDescriptor configuration;
261 USBInterfaceDescriptor sniffer;
262 USBEndpointDescriptor sniffer_dataOut;
263 USBEndpointDescriptor sniffer_dataIn;
264 USBEndpointDescriptor sniffer_interruptIn;
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100265 DFURT_IF_DESCRIPTOR_STRUCT;
Christina Quast01bbdc32015-02-24 17:38:45 +0100266} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100267
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100268static const SIMTraceDriverConfigurationDescriptorSniffer
269 configurationDescriptorSniffer = {
270 /* Standard configuration descriptor */
271 {
272 .bLength = sizeof(USBConfigurationDescriptor),
273 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
274 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100275 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100276 .bConfigurationValue = CFG_NUM_SNIFF,
277 .iConfiguration = SNIFFER_CONF_STR,
278 .bmAttributes = USBD_BMATTRIBUTES,
279 .bMaxPower = USBConfigurationDescriptor_POWER(100),
280 },
281 /* Communication class interface standard descriptor */
282 {
283 .bLength = sizeof(USBInterfaceDescriptor),
284 .bDescriptorType = USBGenericDescriptor_INTERFACE,
285 .bInterfaceNumber = 0,
286 .bAlternateSetting = 0,
287 .bNumEndpoints = 3,
288 .bInterfaceClass = 0xff,
289 .bInterfaceSubClass = 0,
290 .bInterfaceProtocol = 0,
291 .iInterface = SNIFFER_CONF_STR,
292 },
293 /* Bulk-OUT endpoint standard descriptor */
294 {
295 .bLength = sizeof(USBEndpointDescriptor),
296 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
297 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
298 USBEndpointDescriptor_OUT,
299 PHONE_DATAOUT),
300 .bmAttributes = USBEndpointDescriptor_BULK,
301 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
302 PHONE_DATAOUT),
303 USBEndpointDescriptor_MAXBULKSIZE_FS),
304 .bInterval = 0,
305 },
306 /* Bulk-IN endpoint descriptor */
307 {
308 .bLength = sizeof(USBEndpointDescriptor),
309 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
310 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
311 USBEndpointDescriptor_IN,
312 PHONE_DATAIN),
313 .bmAttributes = USBEndpointDescriptor_BULK,
314 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
315 PHONE_DATAIN),
316 USBEndpointDescriptor_MAXBULKSIZE_FS),
317 .bInterval = 0,
318 },
319 // Notification endpoint descriptor
320 {
321 .bLength = sizeof(USBEndpointDescriptor),
322 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
323 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
324 USBEndpointDescriptor_IN,
325 PHONE_INT),
326 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
327 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
328 PHONE_INT),
329 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
330 .bInterval = 0x10,
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100331 },
332 DFURT_IF_DESCRIPTOR(1, 0),
Christina Quastf5549502015-02-24 14:27:08 +0100333};
Harald Welte2fb59962016-02-28 12:34:26 +0100334#endif /* HAVE_SNIFFER */
Christina Quastf5549502015-02-24 14:27:08 +0100335
Harald Welte2fb59962016-02-28 12:34:26 +0100336#ifdef HAVE_CCID
Harald Welteec4fe232015-11-07 18:41:25 +0100337static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100338 // Standard USB configuration descriptor
339 {
340 .bLength = sizeof(USBConfigurationDescriptor),
341 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
342 .wTotalLength = sizeof(CCIDDriverConfigurationDescriptors),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100343 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100344 .bConfigurationValue = CFG_NUM_CCID,
345 .iConfiguration = CCID_CONF_STR,
346 .bmAttributes = BOARD_USB_BMATTRIBUTES,
347 .bMaxPower = USBConfigurationDescriptor_POWER(100),
348 },
349 // CCID interface descriptor
350 // Table 4.3-1 Interface Descriptor
351 // Interface descriptor
352 {
353 .bLength = sizeof(USBInterfaceDescriptor),
354 .bDescriptorType = USBGenericDescriptor_INTERFACE,
355 .bInterfaceNumber = 0,
356 .bAlternateSetting = 0,
357 .bNumEndpoints = 3,
358 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
359 .bInterfaceSubClass = 0,
360 .bInterfaceProtocol = 0,
361 .iInterface = CCID_CONF_STR,
362 },
363 {
364 .bLength = sizeof(CCIDDescriptor),
365 .bDescriptorType = CCID_DECRIPTOR_TYPE,
366 .bcdCCID = CCID1_10, // CCID version
367 .bMaxSlotIndex = 0, // 1 slot
368 .bVoltageSupport = VOLTS_3_0,
369 .dwProtocols = (1 << PROTOCOL_TO),
370 .dwDefaultClock = 3580,
371 .dwMaximumClock = 3580,
372 .bNumClockSupported = 0,
373 .dwDataRate = 9600,
374 .dwMaxDataRate = 9600,
375 .bNumDataRatesSupported = 0,
376 .dwMaxIFSD = 0xfe,
377 .dwSynchProtocols = 0,
378 .dwMechanical = 0,
379 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
380 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
381 CCID_FEATURES_EXC_TPDU,
382 .dwMaxCCIDMessageLength = 271, /* For extended APDU
383 level the value shall
384 be between 261 + 10 */
385 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
386 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
387 .wLcdLayout = 0, // wLcdLayout: no LCD
388 .bPINSupport = 0, // bPINSupport: No PIN
389 .bMaxCCIDBusySlots = 1,
390 },
391 // Bulk-OUT endpoint descriptor
392 {
393 .bLength = sizeof(USBEndpointDescriptor),
394 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
395 .bEndpointAddress =
396 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
397 CCID_EPT_DATA_OUT),
398 .bmAttributes = USBEndpointDescriptor_BULK,
399 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
400 CCID_EPT_DATA_OUT),
401 USBEndpointDescriptor_MAXBULKSIZE_FS),
402 .bInterval = 0x00,
403 },
404 // Bulk-IN endpoint descriptor
405 {
406 .bLength = sizeof(USBEndpointDescriptor),
407 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
408 .bEndpointAddress =
409 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
410 CCID_EPT_DATA_IN),
411 .bmAttributes = USBEndpointDescriptor_BULK,
412 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
413 CCID_EPT_DATA_IN),
414 USBEndpointDescriptor_MAXBULKSIZE_FS),
415 .bInterval = 0x00,
416 },
417 // Notification endpoint descriptor
418 {
419 .bLength = sizeof(USBEndpointDescriptor),
420 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
421 .bEndpointAddress =
422 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
423 CCID_EPT_NOTIFICATION),
424 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
425 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
426 CCID_EPT_NOTIFICATION),
427 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
428 .bInterval = 0x10,
429 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100430 DFURT_IF_DESCRIPTOR(1, 0),
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100431};
Harald Welte2fb59962016-02-28 12:34:26 +0100432#endif /* HAVE_CCID */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100433
Harald Welte2fb59962016-02-28 12:34:26 +0100434#ifdef HAVE_CARDEM
Christina Quast01bbdc32015-02-24 17:38:45 +0100435/* SIM card emulator */
436typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100437 /* Standard configuration descriptor. */
438 USBConfigurationDescriptor configuration;
439 USBInterfaceDescriptor phone;
440 USBEndpointDescriptor phone_dataOut;
441 USBEndpointDescriptor phone_dataIn;
442 USBEndpointDescriptor phone_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100443#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100444 USBInterfaceDescriptor usim2;
445 USBEndpointDescriptor usim2_dataOut;
446 USBEndpointDescriptor usim2_dataIn;
447 USBEndpointDescriptor usim2_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100448#endif
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100449 DFURT_IF_DESCRIPTOR_STRUCT;
Christina Quast01bbdc32015-02-24 17:38:45 +0100450} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
451
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100452static const SIMTraceDriverConfigurationDescriptorPhone
453 configurationDescriptorPhone = {
454 /* Standard configuration descriptor */
455 {
456 sizeof(USBConfigurationDescriptor),
457 USBGenericDescriptor_CONFIGURATION,
458 sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Harald Welte57b3a252016-03-02 15:20:27 +0100459#ifdef CARDEMU_SECOND_UART
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100460 2+DFURT_NUM_IF,
Harald Welte57b3a252016-03-02 15:20:27 +0100461#else
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100462 1+DFURT_NUM_IF, /* There is one interface in this configuration */
Harald Welte57b3a252016-03-02 15:20:27 +0100463#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100464 CFG_NUM_PHONE, /* configuration number */
465 PHONE_CONF_STR, /* string descriptor for this configuration */
466 USBD_BMATTRIBUTES,
467 USBConfigurationDescriptor_POWER(100)
468 },
469 /* Communication class interface standard descriptor */
470 {
471 sizeof(USBInterfaceDescriptor),
472 USBGenericDescriptor_INTERFACE,
473 0, /* This is interface #0 */
474 0, /* This is alternate setting #0 for this interface */
475 3, /* Number of endpoints */
476 0xff, /* Descriptor Class: Vendor specific */
477 0, /* No subclass */
478 0, /* No l */
479 CARDEM_USIM1_INTF_STR
480 },
481 /* Bulk-OUT endpoint standard descriptor */
482 {
483 sizeof(USBEndpointDescriptor),
484 USBGenericDescriptor_ENDPOINT,
485 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
486 PHONE_DATAOUT),
487 USBEndpointDescriptor_BULK,
488 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
489 USBEndpointDescriptor_MAXBULKSIZE_FS),
490 0 /* Must be 0 for full-speed bulk endpoints */
491 },
492 /* Bulk-IN endpoint descriptor */
493 {
494 sizeof(USBEndpointDescriptor),
495 USBGenericDescriptor_ENDPOINT,
496 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
497 PHONE_DATAIN),
498 USBEndpointDescriptor_BULK,
499 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
500 USBEndpointDescriptor_MAXBULKSIZE_FS),
501 0 /* Must be 0 for full-speed bulk endpoints */
502 },
503 /* Notification endpoint descriptor */
504 {
505 sizeof(USBEndpointDescriptor),
506 USBGenericDescriptor_ENDPOINT,
507 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
508 PHONE_INT),
509 USBEndpointDescriptor_INTERRUPT,
510 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
511 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
512 0x10
513 },
Harald Welte57b3a252016-03-02 15:20:27 +0100514#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100515 /* Communication class interface standard descriptor */
516 {
517 sizeof(USBInterfaceDescriptor),
518 USBGenericDescriptor_INTERFACE,
Harald Welte6dcacf32016-03-19 14:01:31 +0100519 1, /* This is interface #1 */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100520 0, /* This is alternate setting #0 for this interface */
521 3, /* Number of endpoints */
522 0xff, /* Descriptor Class: Vendor specific */
523 0, /* No subclass */
524 0, /* No l */
525 CARDEM_USIM2_INTF_STR
526 },
527 /* Bulk-OUT endpoint standard descriptor */
528 {
529 sizeof(USBEndpointDescriptor),
530 USBGenericDescriptor_ENDPOINT,
531 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
532 CARDEM_USIM2_DATAOUT),
533 USBEndpointDescriptor_BULK,
534 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_DATAOUT),
535 USBEndpointDescriptor_MAXBULKSIZE_FS),
536 0 /* Must be 0 for full-speed bulk endpoints */
537 }
538 ,
539 /* Bulk-IN endpoint descriptor */
540 {
541 sizeof(USBEndpointDescriptor),
542 USBGenericDescriptor_ENDPOINT,
543 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
544 CARDEM_USIM2_DATAIN),
545 USBEndpointDescriptor_BULK,
546 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_DATAIN),
547 USBEndpointDescriptor_MAXBULKSIZE_FS),
548 0 /* Must be 0 for full-speed bulk endpoints */
549 },
550 /* Notification endpoint descriptor */
551 {
552 sizeof(USBEndpointDescriptor),
553 USBGenericDescriptor_ENDPOINT,
554 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
555 CARDEM_USIM2_INT),
556 USBEndpointDescriptor_INTERRUPT,
557 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_INT),
558 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
559 0x10
560 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100561 DFURT_IF_DESCRIPTOR(2, 0),
562#else
563 DFURT_IF_DESCRIPTOR(1, 0),
Harald Welte57b3a252016-03-02 15:20:27 +0100564#endif
Christina Quast01bbdc32015-02-24 17:38:45 +0100565};
Harald Welte2fb59962016-02-28 12:34:26 +0100566#endif /* HAVE_CARDEM */
Christina Quast01bbdc32015-02-24 17:38:45 +0100567
Harald Welte2fb59962016-02-28 12:34:26 +0100568#ifdef HAVE_MITM
Christina Quastf5549502015-02-24 14:27:08 +0100569typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100570 /* Standard configuration descriptor. */
571 USBConfigurationDescriptor configuration;
572 USBInterfaceDescriptor simcard;
573 /// CCID descriptor
574 CCIDDescriptor ccid;
575 /// Bulk OUT endpoint descriptor
576 USBEndpointDescriptor simcard_dataOut;
577 /// Bulk IN endpoint descriptor
578 USBEndpointDescriptor simcard_dataIn;
579 /// Interrupt OUT endpoint descriptor
580 USBEndpointDescriptor simcard_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100581
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100582 USBInterfaceDescriptor phone;
583 USBEndpointDescriptor phone_dataOut;
584 USBEndpointDescriptor phone_dataIn;
585 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100586
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100587 DFURT_IF_DESCRIPTOR_STRUCT;
588
Christina Quast01bbdc32015-02-24 17:38:45 +0100589} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100590
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100591static const SIMTraceDriverConfigurationDescriptorMITM
592 configurationDescriptorMITM = {
593 /* Standard configuration descriptor */
594 {
595 sizeof(USBConfigurationDescriptor),
596 USBGenericDescriptor_CONFIGURATION,
597 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100598 2+DFURT_NUM_IF, /* There are two interfaces in this configuration */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100599 CFG_NUM_MITM, /* configuration number */
600 MITM_CONF_STR, /* string descriptor for this configuration */
601 USBD_BMATTRIBUTES,
602 USBConfigurationDescriptor_POWER(100)
603 },
604 // CCID interface descriptor
605 // Table 4.3-1 Interface Descriptor
606 // Interface descriptor
607 {
608 .bLength = sizeof(USBInterfaceDescriptor),
609 .bDescriptorType = USBGenericDescriptor_INTERFACE,
610 .bInterfaceNumber = 0,
611 .bAlternateSetting = 0,
612 .bNumEndpoints = 3,
613 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
614 .bInterfaceSubClass = 0,
615 .bInterfaceProtocol = 0,
616 .iInterface = CCID_CONF_STR,
617 },
618 {
619 .bLength = sizeof(CCIDDescriptor),
620 .bDescriptorType = CCID_DECRIPTOR_TYPE,
621 .bcdCCID = CCID1_10, // CCID version
622 .bMaxSlotIndex = 0, // 1 slot
623 .bVoltageSupport = VOLTS_3_0,
624 .dwProtocols = (1 << PROTOCOL_TO),
625 .dwDefaultClock = 3580,
626 .dwMaximumClock = 3580,
627 .bNumClockSupported = 0,
628 .dwDataRate = 9600,
629 .dwMaxDataRate = 9600,
630 .bNumDataRatesSupported = 0,
631 .dwMaxIFSD = 0xfe,
632 .dwSynchProtocols = 0,
633 .dwMechanical = 0,
634 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
635 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
636 CCID_FEATURES_EXC_TPDU,
637 .dwMaxCCIDMessageLength = 271, /* For extended APDU
638 level the value shall
639 be between 261 + 10 */
640 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
641 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
642 .wLcdLayout = 0, // wLcdLayout: no LCD
643 .bPINSupport = 0, // bPINSupport: No PIN
644 .bMaxCCIDBusySlots = 1,
645 },
646 // Bulk-OUT endpoint descriptor
647 {
648 .bLength = sizeof(USBEndpointDescriptor),
649 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
650 .bEndpointAddress =
651 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
652 CCID_EPT_DATA_OUT),
653 .bmAttributes = USBEndpointDescriptor_BULK,
654 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
655 CCID_EPT_DATA_OUT),
656 USBEndpointDescriptor_MAXBULKSIZE_FS),
657 .bInterval = 0x00,
658 },
659 // Bulk-IN endpoint descriptor
660 {
661 .bLength = sizeof(USBEndpointDescriptor),
662 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
663 .bEndpointAddress =
664 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
665 CCID_EPT_DATA_IN),
666 .bmAttributes = USBEndpointDescriptor_BULK,
667 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
668 CCID_EPT_DATA_IN),
669 USBEndpointDescriptor_MAXBULKSIZE_FS),
670 .bInterval = 0x00,
671 },
672 // Notification endpoint descriptor
673 {
674 .bLength = sizeof(USBEndpointDescriptor),
675 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
676 .bEndpointAddress =
677 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
678 CCID_EPT_NOTIFICATION),
679 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
680 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
681 CCID_EPT_NOTIFICATION),
682 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
683 .bInterval = 0x10,
684 },
685
686 /* Communication class interface standard descriptor */
687 {
688 sizeof(USBInterfaceDescriptor),
689 USBGenericDescriptor_INTERFACE,
690 1, /* This is interface #1 */
691 0, /* This is alternate setting #0 for this interface */
692 3, /* Number of endpoints */
693 0xff,
694 0,
695 0,
696 PHONE_CONF_STR, /* string descriptor for this interface */
697 }
698 ,
699 /* Bulk-OUT endpoint standard descriptor */
700 {
701 sizeof(USBEndpointDescriptor),
702 USBGenericDescriptor_ENDPOINT,
703 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
704 PHONE_DATAOUT),
705 USBEndpointDescriptor_BULK,
706 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
707 USBEndpointDescriptor_MAXBULKSIZE_FS),
708 0 /* Must be 0 for full-speed bulk endpoints */
709 }
710 ,
711 /* Bulk-IN endpoint descriptor */
712 {
713 sizeof(USBEndpointDescriptor),
714 USBGenericDescriptor_ENDPOINT,
715 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
716 PHONE_DATAIN),
717 USBEndpointDescriptor_BULK,
718 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
719 USBEndpointDescriptor_MAXBULKSIZE_FS),
720 0 /* Must be 0 for full-speed bulk endpoints */
721 }
722 ,
723 /* Notification endpoint descriptor */
724 {
725 sizeof(USBEndpointDescriptor),
726 USBGenericDescriptor_ENDPOINT,
727 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, PHONE_INT),
728 USBEndpointDescriptor_INTERRUPT,
729 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
730 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100731 0x10},
732 DFURT_IF_DESCRIPTOR(2, 0),
Christina Quastf5549502015-02-24 14:27:08 +0100733};
Harald Welte2fb59962016-02-28 12:34:26 +0100734#endif /* HAVE_CARDEM */
735
736const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
737#ifdef HAVE_SNIFFER
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100738 &configurationDescriptorSniffer.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100739#endif
740#ifdef HAVE_CCID
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100741 &configurationDescriptorCCID.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100742#endif
743#ifdef HAVE_CARDEM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100744 &configurationDescriptorPhone.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100745#endif
746#ifdef HAVE_MITM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100747 &configurationDescriptorMITM.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100748#endif
749};
Christina Quastf5549502015-02-24 14:27:08 +0100750
Christina Quastf5549502015-02-24 14:27:08 +0100751/** Standard USB device descriptor for the CDC serial driver */
752const USBDeviceDescriptor deviceDescriptor = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100753 .bLength = sizeof(USBDeviceDescriptor),
754 .bDescriptorType = USBGenericDescriptor_DEVICE,
755 .bcdUSB = USBDeviceDescriptor_USB2_00,
756 .bDeviceClass = 0xff,
757 .bDeviceSubClass = 0xff,
758 .bDeviceProtocol = 0xff,
759 .bMaxPacketSize0 = BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
760 .idVendor = SIMTRACE_VENDOR_ID,
761 .idProduct = SIMTRACE_PRODUCT_ID,
762 .bcdDevice = 2, /* Release number */
763 .iManufacturer = MANUF_STR,
764 .iProduct = PRODUCT_STRING,
765 .iSerialNumber = 0,
766 .bNumConfigurations = ARRAY_SIZE(configurationDescriptorsArr),
Christina Quast01bbdc32015-02-24 17:38:45 +0100767};
768
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200769/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100770static const USBDDriverDescriptors driverDescriptors = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100771 &deviceDescriptor,
772 (const USBConfigurationDescriptor **)&(configurationDescriptorsArr), /* first full-speed configuration descriptor */
773 0, /* No full-speed device qualifier descriptor */
774 0, /* No full-speed other speed configuration */
775 0, /* No high-speed device descriptor */
776 0, /* No high-speed configuration descriptor */
777 0, /* No high-speed device qualifier descriptor */
778 0, /* No high-speed other speed configuration descriptor */
779 stringDescriptors,
780 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100781};
782
Harald Welte84ad9852017-01-12 18:06:57 +0100783#if (BOARD_MAINOSC == 12000000)
784#define PLLB_CFG (CKGR_PLLBR_DIVB(2)|CKGR_PLLBR_MULB(8-1)|CKGR_PLLBR_PLLBCOUNT_Msk)
785#elif (BOARD_MAINOSC == 18432000)
786#define PLLB_CFG (CKGR_PLLBR_DIVB(5)|CKGR_PLLBR_MULB(13-1)|CKGR_PLLBR_PLLBCOUNT_Msk)
787#else
788#error "Please configure PLLB for your MAINOSC freq"
789#endif
790
Christina Quast0ae03142015-02-25 18:43:46 +0100791/*----------------------------------------------------------------------------
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100792 * Functions
Christina Quast968b9742015-02-25 14:10:12 +0100793 *----------------------------------------------------------------------------*/
794
795/**
796 * \brief Configure 48MHz Clock for USB
797 */
798static void _ConfigureUsbClock(void)
799{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100800 /* Enable PLLB for USB */
Harald Welte84ad9852017-01-12 18:06:57 +0100801 PMC->CKGR_PLLBR = PLLB_CFG;
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100802 while ((PMC->PMC_SR & PMC_SR_LOCKB) == 0) ;
803
804 /* USB Clock uses PLLB */
805 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
806 | PMC_USB_USBS; /* PLLB */
Christina Quast968b9742015-02-25 14:10:12 +0100807}
808
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100809void SIMtrace_USB_Initialize(void)
Christina Quast968b9742015-02-25 14:10:12 +0100810{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100811 _ConfigureUsbClock();
812 // Get std USB driver
813 USBDDriver *pUsbd = USBD_GetDriver();
Christina Quast968b9742015-02-25 14:10:12 +0100814
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100815 TRACE_DEBUG(".");
Christina Quast968b9742015-02-25 14:10:12 +0100816
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100817 // Initialize standard USB driver
818 USBDDriver_Initialize(pUsbd, &driverDescriptors, 0); // Multiple interface settings not supported
819 USBD_Init();
820 USBD_Connect();
Christina Quast968b9742015-02-25 14:10:12 +0100821
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100822 NVIC_EnableIRQ(UDP_IRQn);
Christina Quast968b9742015-02-25 14:10:12 +0100823}