blob: d99cce0c4c339ab791294774e62aae54a46c1e34 [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>
39
Christina Quastf5549502015-02-24 14:27:08 +010040/*------------------------------------------------------------------------------
41 * USB String descriptors
42 *------------------------------------------------------------------------------*/
43
Christina Quast6032e792015-02-27 15:22:25 +010044static const unsigned char langDesc[] = {
45
Harald Welte7dd3dfd2016-03-03 12:32:04 +010046 USBStringDescriptor_LENGTH(1),
47 USBGenericDescriptor_STRING,
48 USBStringDescriptor_ENGLISH_US
Christina Quast6032e792015-02-27 15:22:25 +010049};
50
Harald Welte6d44c1f2015-11-07 19:01:30 +010051const unsigned char manufStringDescriptor[] = {
52
Harald Welte7dd3dfd2016-03-03 12:32:04 +010053 USBStringDescriptor_LENGTH(24),
54 USBGenericDescriptor_STRING,
55 USBStringDescriptor_UNICODE('s'),
56 USBStringDescriptor_UNICODE('y'),
57 USBStringDescriptor_UNICODE('s'),
58 USBStringDescriptor_UNICODE('m'),
59 USBStringDescriptor_UNICODE('o'),
60 USBStringDescriptor_UNICODE('c'),
61 USBStringDescriptor_UNICODE('o'),
62 USBStringDescriptor_UNICODE('m'),
63 USBStringDescriptor_UNICODE(' '),
64 USBStringDescriptor_UNICODE('-'),
65 USBStringDescriptor_UNICODE(' '),
66 USBStringDescriptor_UNICODE('s'),
67 USBStringDescriptor_UNICODE('.'),
68 USBStringDescriptor_UNICODE('f'),
69 USBStringDescriptor_UNICODE('.'),
70 USBStringDescriptor_UNICODE('m'),
71 USBStringDescriptor_UNICODE('.'),
72 USBStringDescriptor_UNICODE('c'),
73 USBStringDescriptor_UNICODE('.'),
74 USBStringDescriptor_UNICODE(' '),
75 USBStringDescriptor_UNICODE('G'),
76 USBStringDescriptor_UNICODE('m'),
77 USBStringDescriptor_UNICODE('b'),
78 USBStringDescriptor_UNICODE('H'),
Harald Welte6d44c1f2015-11-07 19:01:30 +010079};
80
Christina Quastf5549502015-02-24 14:27:08 +010081const unsigned char productStringDescriptor[] = {
82
Harald Welte7dd3dfd2016-03-03 12:32:04 +010083 USBStringDescriptor_LENGTH(10),
84 USBGenericDescriptor_STRING,
85 USBStringDescriptor_UNICODE('S'),
86 USBStringDescriptor_UNICODE('I'),
87 USBStringDescriptor_UNICODE('M'),
88 USBStringDescriptor_UNICODE('t'),
89 USBStringDescriptor_UNICODE('r'),
90 USBStringDescriptor_UNICODE('a'),
91 USBStringDescriptor_UNICODE('c'),
92 USBStringDescriptor_UNICODE('e'),
93 USBStringDescriptor_UNICODE(' '),
94 USBStringDescriptor_UNICODE('2'),
Christina Quastf5549502015-02-24 14:27:08 +010095};
96
97const unsigned char snifferConfigStringDescriptor[] = {
98
Harald Welte7dd3dfd2016-03-03 12:32:04 +010099 USBStringDescriptor_LENGTH(16),
100 USBGenericDescriptor_STRING,
101 USBStringDescriptor_UNICODE('S'),
102 USBStringDescriptor_UNICODE('I'),
103 USBStringDescriptor_UNICODE('M'),
104 USBStringDescriptor_UNICODE('t'),
105 USBStringDescriptor_UNICODE('r'),
106 USBStringDescriptor_UNICODE('a'),
107 USBStringDescriptor_UNICODE('c'),
108 USBStringDescriptor_UNICODE('e'),
109 USBStringDescriptor_UNICODE(' '),
110 USBStringDescriptor_UNICODE('S'),
111 USBStringDescriptor_UNICODE('n'),
112 USBStringDescriptor_UNICODE('i'),
113 USBStringDescriptor_UNICODE('f'),
114 USBStringDescriptor_UNICODE('f'),
115 USBStringDescriptor_UNICODE('e'),
116 USBStringDescriptor_UNICODE('r'),
Christina Quastf5549502015-02-24 14:27:08 +0100117};
118
119const unsigned char CCIDConfigStringDescriptor[] = {
120
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100121 USBStringDescriptor_LENGTH(13),
122 USBGenericDescriptor_STRING,
123 USBStringDescriptor_UNICODE('S'),
124 USBStringDescriptor_UNICODE('I'),
125 USBStringDescriptor_UNICODE('M'),
126 USBStringDescriptor_UNICODE('t'),
127 USBStringDescriptor_UNICODE('r'),
128 USBStringDescriptor_UNICODE('a'),
129 USBStringDescriptor_UNICODE('c'),
130 USBStringDescriptor_UNICODE('e'),
131 USBStringDescriptor_UNICODE(' '),
132 USBStringDescriptor_UNICODE('C'),
133 USBStringDescriptor_UNICODE('C'),
134 USBStringDescriptor_UNICODE('I'),
135 USBStringDescriptor_UNICODE('D'),
Christina Quastf5549502015-02-24 14:27:08 +0100136};
137
138const unsigned char phoneConfigStringDescriptor[] = {
139
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100140 USBStringDescriptor_LENGTH(14),
141 USBGenericDescriptor_STRING,
142 USBStringDescriptor_UNICODE('S'),
143 USBStringDescriptor_UNICODE('I'),
144 USBStringDescriptor_UNICODE('M'),
145 USBStringDescriptor_UNICODE('t'),
146 USBStringDescriptor_UNICODE('r'),
147 USBStringDescriptor_UNICODE('a'),
148 USBStringDescriptor_UNICODE('c'),
149 USBStringDescriptor_UNICODE('e'),
150 USBStringDescriptor_UNICODE(' '),
151 USBStringDescriptor_UNICODE('P'),
152 USBStringDescriptor_UNICODE('h'),
153 USBStringDescriptor_UNICODE('o'),
154 USBStringDescriptor_UNICODE('n'),
155 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100156};
157
Harald Weltebeb72932015-11-07 18:35:41 +0100158const unsigned char MITMConfigStringDescriptor[] = {
159
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100160 USBStringDescriptor_LENGTH(13),
161 USBGenericDescriptor_STRING,
162 USBStringDescriptor_UNICODE('S'),
163 USBStringDescriptor_UNICODE('I'),
164 USBStringDescriptor_UNICODE('M'),
165 USBStringDescriptor_UNICODE('t'),
166 USBStringDescriptor_UNICODE('r'),
167 USBStringDescriptor_UNICODE('a'),
168 USBStringDescriptor_UNICODE('c'),
169 USBStringDescriptor_UNICODE('e'),
170 USBStringDescriptor_UNICODE(' '),
171 USBStringDescriptor_UNICODE('M'),
172 USBStringDescriptor_UNICODE('I'),
173 USBStringDescriptor_UNICODE('T'),
174 USBStringDescriptor_UNICODE('M'),
Christina Quastf5549502015-02-24 14:27:08 +0100175};
176
Harald Welte57b3a252016-03-02 15:20:27 +0100177const unsigned char cardem_usim1_intf_str[] = {
178
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100179 USBStringDescriptor_LENGTH(18),
180 USBGenericDescriptor_STRING,
181 USBStringDescriptor_UNICODE('C'),
182 USBStringDescriptor_UNICODE('a'),
183 USBStringDescriptor_UNICODE('r'),
184 USBStringDescriptor_UNICODE('d'),
185 USBStringDescriptor_UNICODE('E'),
186 USBStringDescriptor_UNICODE('m'),
187 USBStringDescriptor_UNICODE('u'),
188 USBStringDescriptor_UNICODE('l'),
189 USBStringDescriptor_UNICODE('a'),
190 USBStringDescriptor_UNICODE('t'),
191 USBStringDescriptor_UNICODE('o'),
192 USBStringDescriptor_UNICODE('r'),
193 USBStringDescriptor_UNICODE(' '),
194 USBStringDescriptor_UNICODE('U'),
195 USBStringDescriptor_UNICODE('S'),
196 USBStringDescriptor_UNICODE('I'),
197 USBStringDescriptor_UNICODE('M'),
198 USBStringDescriptor_UNICODE('1'),
Harald Welte57b3a252016-03-02 15:20:27 +0100199};
200
201const unsigned char cardem_usim2_intf_str[] = {
202
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100203 USBStringDescriptor_LENGTH(18),
204 USBGenericDescriptor_STRING,
205 USBStringDescriptor_UNICODE('C'),
206 USBStringDescriptor_UNICODE('a'),
207 USBStringDescriptor_UNICODE('r'),
208 USBStringDescriptor_UNICODE('d'),
209 USBStringDescriptor_UNICODE('E'),
210 USBStringDescriptor_UNICODE('m'),
211 USBStringDescriptor_UNICODE('u'),
212 USBStringDescriptor_UNICODE('l'),
213 USBStringDescriptor_UNICODE('a'),
214 USBStringDescriptor_UNICODE('t'),
215 USBStringDescriptor_UNICODE('o'),
216 USBStringDescriptor_UNICODE('r'),
217 USBStringDescriptor_UNICODE(' '),
218 USBStringDescriptor_UNICODE('U'),
219 USBStringDescriptor_UNICODE('S'),
220 USBStringDescriptor_UNICODE('I'),
221 USBStringDescriptor_UNICODE('M'),
222 USBStringDescriptor_UNICODE('2'),
Harald Welte57b3a252016-03-02 15:20:27 +0100223};
224
Christina Quastf5549502015-02-24 14:27:08 +0100225enum strDescNum {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100226 PRODUCT_STRING = 1,
227 MANUF_STR,
228 SNIFFER_CONF_STR,
229 CCID_CONF_STR,
230 PHONE_CONF_STR,
231 MITM_CONF_STR,
232 CARDEM_USIM1_INTF_STR,
233 CARDEM_USIM2_INTF_STR,
234 STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +0100235};
236
237/** List of string descriptors used by the device */
Harald Welteec4fe232015-11-07 18:41:25 +0100238static const unsigned char *stringDescriptors[] = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100239 langDesc,
240 [PRODUCT_STRING] = productStringDescriptor,
241 [MANUF_STR] = manufStringDescriptor,
242 [SNIFFER_CONF_STR] = snifferConfigStringDescriptor,
243 [CCID_CONF_STR] = CCIDConfigStringDescriptor,
244 [PHONE_CONF_STR] = phoneConfigStringDescriptor,
245 [MITM_CONF_STR] = MITMConfigStringDescriptor,
246 [CARDEM_USIM1_INTF_STR] = cardem_usim1_intf_str,
247 [CARDEM_USIM2_INTF_STR] = cardem_usim2_intf_str,
Christina Quastf5549502015-02-24 14:27:08 +0100248};
249
Christina Quastf5549502015-02-24 14:27:08 +0100250/*------------------------------------------------------------------------------
251 * USB Device descriptors
252 *------------------------------------------------------------------------------*/
253
Harald Welte2fb59962016-02-28 12:34:26 +0100254#ifdef HAVE_SNIFFER
Christina Quastf5549502015-02-24 14:27:08 +0100255typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
256
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100257 /** Standard configuration descriptor. */
258 USBConfigurationDescriptor configuration;
259 USBInterfaceDescriptor sniffer;
260 USBEndpointDescriptor sniffer_dataOut;
261 USBEndpointDescriptor sniffer_dataIn;
262 USBEndpointDescriptor sniffer_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100263
Christina Quast01bbdc32015-02-24 17:38:45 +0100264} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100265
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100266static const SIMTraceDriverConfigurationDescriptorSniffer
267 configurationDescriptorSniffer = {
268 /* Standard configuration descriptor */
269 {
270 .bLength = sizeof(USBConfigurationDescriptor),
271 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
272 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
273 .bNumInterfaces = 1,
274 .bConfigurationValue = CFG_NUM_SNIFF,
275 .iConfiguration = SNIFFER_CONF_STR,
276 .bmAttributes = USBD_BMATTRIBUTES,
277 .bMaxPower = USBConfigurationDescriptor_POWER(100),
278 },
279 /* Communication class interface standard descriptor */
280 {
281 .bLength = sizeof(USBInterfaceDescriptor),
282 .bDescriptorType = USBGenericDescriptor_INTERFACE,
283 .bInterfaceNumber = 0,
284 .bAlternateSetting = 0,
285 .bNumEndpoints = 3,
286 .bInterfaceClass = 0xff,
287 .bInterfaceSubClass = 0,
288 .bInterfaceProtocol = 0,
289 .iInterface = SNIFFER_CONF_STR,
290 },
291 /* Bulk-OUT endpoint standard descriptor */
292 {
293 .bLength = sizeof(USBEndpointDescriptor),
294 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
295 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
296 USBEndpointDescriptor_OUT,
297 PHONE_DATAOUT),
298 .bmAttributes = USBEndpointDescriptor_BULK,
299 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
300 PHONE_DATAOUT),
301 USBEndpointDescriptor_MAXBULKSIZE_FS),
302 .bInterval = 0,
303 },
304 /* Bulk-IN endpoint descriptor */
305 {
306 .bLength = sizeof(USBEndpointDescriptor),
307 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
308 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
309 USBEndpointDescriptor_IN,
310 PHONE_DATAIN),
311 .bmAttributes = USBEndpointDescriptor_BULK,
312 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
313 PHONE_DATAIN),
314 USBEndpointDescriptor_MAXBULKSIZE_FS),
315 .bInterval = 0,
316 },
317 // Notification endpoint descriptor
318 {
319 .bLength = sizeof(USBEndpointDescriptor),
320 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
321 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
322 USBEndpointDescriptor_IN,
323 PHONE_INT),
324 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
325 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
326 PHONE_INT),
327 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
328 .bInterval = 0x10,
329 }
Christina Quastf5549502015-02-24 14:27:08 +0100330};
Harald Welte2fb59962016-02-28 12:34:26 +0100331#endif /* HAVE_SNIFFER */
Christina Quastf5549502015-02-24 14:27:08 +0100332
Harald Welte2fb59962016-02-28 12:34:26 +0100333#ifdef HAVE_CCID
Harald Welteec4fe232015-11-07 18:41:25 +0100334static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100335 // Standard USB configuration descriptor
336 {
337 .bLength = sizeof(USBConfigurationDescriptor),
338 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
339 .wTotalLength = sizeof(CCIDDriverConfigurationDescriptors),
340 .bNumInterfaces = 1,
341 .bConfigurationValue = CFG_NUM_CCID,
342 .iConfiguration = CCID_CONF_STR,
343 .bmAttributes = BOARD_USB_BMATTRIBUTES,
344 .bMaxPower = USBConfigurationDescriptor_POWER(100),
345 },
346 // CCID interface descriptor
347 // Table 4.3-1 Interface Descriptor
348 // Interface descriptor
349 {
350 .bLength = sizeof(USBInterfaceDescriptor),
351 .bDescriptorType = USBGenericDescriptor_INTERFACE,
352 .bInterfaceNumber = 0,
353 .bAlternateSetting = 0,
354 .bNumEndpoints = 3,
355 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
356 .bInterfaceSubClass = 0,
357 .bInterfaceProtocol = 0,
358 .iInterface = CCID_CONF_STR,
359 },
360 {
361 .bLength = sizeof(CCIDDescriptor),
362 .bDescriptorType = CCID_DECRIPTOR_TYPE,
363 .bcdCCID = CCID1_10, // CCID version
364 .bMaxSlotIndex = 0, // 1 slot
365 .bVoltageSupport = VOLTS_3_0,
366 .dwProtocols = (1 << PROTOCOL_TO),
367 .dwDefaultClock = 3580,
368 .dwMaximumClock = 3580,
369 .bNumClockSupported = 0,
370 .dwDataRate = 9600,
371 .dwMaxDataRate = 9600,
372 .bNumDataRatesSupported = 0,
373 .dwMaxIFSD = 0xfe,
374 .dwSynchProtocols = 0,
375 .dwMechanical = 0,
376 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
377 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
378 CCID_FEATURES_EXC_TPDU,
379 .dwMaxCCIDMessageLength = 271, /* For extended APDU
380 level the value shall
381 be between 261 + 10 */
382 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
383 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
384 .wLcdLayout = 0, // wLcdLayout: no LCD
385 .bPINSupport = 0, // bPINSupport: No PIN
386 .bMaxCCIDBusySlots = 1,
387 },
388 // Bulk-OUT endpoint descriptor
389 {
390 .bLength = sizeof(USBEndpointDescriptor),
391 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
392 .bEndpointAddress =
393 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
394 CCID_EPT_DATA_OUT),
395 .bmAttributes = USBEndpointDescriptor_BULK,
396 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
397 CCID_EPT_DATA_OUT),
398 USBEndpointDescriptor_MAXBULKSIZE_FS),
399 .bInterval = 0x00,
400 },
401 // Bulk-IN endpoint descriptor
402 {
403 .bLength = sizeof(USBEndpointDescriptor),
404 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
405 .bEndpointAddress =
406 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
407 CCID_EPT_DATA_IN),
408 .bmAttributes = USBEndpointDescriptor_BULK,
409 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
410 CCID_EPT_DATA_IN),
411 USBEndpointDescriptor_MAXBULKSIZE_FS),
412 .bInterval = 0x00,
413 },
414 // Notification endpoint descriptor
415 {
416 .bLength = sizeof(USBEndpointDescriptor),
417 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
418 .bEndpointAddress =
419 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
420 CCID_EPT_NOTIFICATION),
421 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
422 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
423 CCID_EPT_NOTIFICATION),
424 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
425 .bInterval = 0x10,
426 },
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100427};
Harald Welte2fb59962016-02-28 12:34:26 +0100428#endif /* HAVE_CCID */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100429
Harald Welte2fb59962016-02-28 12:34:26 +0100430#ifdef HAVE_CARDEM
Christina Quast01bbdc32015-02-24 17:38:45 +0100431/* SIM card emulator */
432typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100433 /* Standard configuration descriptor. */
434 USBConfigurationDescriptor configuration;
435 USBInterfaceDescriptor phone;
436 USBEndpointDescriptor phone_dataOut;
437 USBEndpointDescriptor phone_dataIn;
438 USBEndpointDescriptor phone_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100439#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100440 USBInterfaceDescriptor usim2;
441 USBEndpointDescriptor usim2_dataOut;
442 USBEndpointDescriptor usim2_dataIn;
443 USBEndpointDescriptor usim2_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100444#endif
Christina Quast01bbdc32015-02-24 17:38:45 +0100445} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
446
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100447static const SIMTraceDriverConfigurationDescriptorPhone
448 configurationDescriptorPhone = {
449 /* Standard configuration descriptor */
450 {
451 sizeof(USBConfigurationDescriptor),
452 USBGenericDescriptor_CONFIGURATION,
453 sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Harald Welte57b3a252016-03-02 15:20:27 +0100454#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100455 2,
Harald Welte57b3a252016-03-02 15:20:27 +0100456#else
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100457 1, /* There is one interface in this configuration */
Harald Welte57b3a252016-03-02 15:20:27 +0100458#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100459 CFG_NUM_PHONE, /* configuration number */
460 PHONE_CONF_STR, /* string descriptor for this configuration */
461 USBD_BMATTRIBUTES,
462 USBConfigurationDescriptor_POWER(100)
463 },
464 /* Communication class interface standard descriptor */
465 {
466 sizeof(USBInterfaceDescriptor),
467 USBGenericDescriptor_INTERFACE,
468 0, /* This is interface #0 */
469 0, /* This is alternate setting #0 for this interface */
470 3, /* Number of endpoints */
471 0xff, /* Descriptor Class: Vendor specific */
472 0, /* No subclass */
473 0, /* No l */
474 CARDEM_USIM1_INTF_STR
475 },
476 /* Bulk-OUT endpoint standard descriptor */
477 {
478 sizeof(USBEndpointDescriptor),
479 USBGenericDescriptor_ENDPOINT,
480 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
481 PHONE_DATAOUT),
482 USBEndpointDescriptor_BULK,
483 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
484 USBEndpointDescriptor_MAXBULKSIZE_FS),
485 0 /* Must be 0 for full-speed bulk endpoints */
486 },
487 /* Bulk-IN endpoint descriptor */
488 {
489 sizeof(USBEndpointDescriptor),
490 USBGenericDescriptor_ENDPOINT,
491 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
492 PHONE_DATAIN),
493 USBEndpointDescriptor_BULK,
494 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
495 USBEndpointDescriptor_MAXBULKSIZE_FS),
496 0 /* Must be 0 for full-speed bulk endpoints */
497 },
498 /* Notification endpoint descriptor */
499 {
500 sizeof(USBEndpointDescriptor),
501 USBGenericDescriptor_ENDPOINT,
502 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
503 PHONE_INT),
504 USBEndpointDescriptor_INTERRUPT,
505 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
506 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
507 0x10
508 },
Harald Welte57b3a252016-03-02 15:20:27 +0100509#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100510 /* Communication class interface standard descriptor */
511 {
512 sizeof(USBInterfaceDescriptor),
513 USBGenericDescriptor_INTERFACE,
514 1, /* This is interface #0 */
515 0, /* This is alternate setting #0 for this interface */
516 3, /* Number of endpoints */
517 0xff, /* Descriptor Class: Vendor specific */
518 0, /* No subclass */
519 0, /* No l */
520 CARDEM_USIM2_INTF_STR
521 },
522 /* Bulk-OUT endpoint standard descriptor */
523 {
524 sizeof(USBEndpointDescriptor),
525 USBGenericDescriptor_ENDPOINT,
526 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
527 CARDEM_USIM2_DATAOUT),
528 USBEndpointDescriptor_BULK,
529 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_DATAOUT),
530 USBEndpointDescriptor_MAXBULKSIZE_FS),
531 0 /* Must be 0 for full-speed bulk endpoints */
532 }
533 ,
534 /* Bulk-IN endpoint descriptor */
535 {
536 sizeof(USBEndpointDescriptor),
537 USBGenericDescriptor_ENDPOINT,
538 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
539 CARDEM_USIM2_DATAIN),
540 USBEndpointDescriptor_BULK,
541 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_DATAIN),
542 USBEndpointDescriptor_MAXBULKSIZE_FS),
543 0 /* Must be 0 for full-speed bulk endpoints */
544 },
545 /* Notification endpoint descriptor */
546 {
547 sizeof(USBEndpointDescriptor),
548 USBGenericDescriptor_ENDPOINT,
549 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
550 CARDEM_USIM2_INT),
551 USBEndpointDescriptor_INTERRUPT,
552 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_INT),
553 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
554 0x10
555 },
Harald Welte57b3a252016-03-02 15:20:27 +0100556#endif
Christina Quast01bbdc32015-02-24 17:38:45 +0100557};
Harald Welte2fb59962016-02-28 12:34:26 +0100558#endif /* HAVE_CARDEM */
Christina Quast01bbdc32015-02-24 17:38:45 +0100559
Harald Welte2fb59962016-02-28 12:34:26 +0100560#ifdef HAVE_MITM
Christina Quastf5549502015-02-24 14:27:08 +0100561typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100562 /* Standard configuration descriptor. */
563 USBConfigurationDescriptor configuration;
564 USBInterfaceDescriptor simcard;
565 /// CCID descriptor
566 CCIDDescriptor ccid;
567 /// Bulk OUT endpoint descriptor
568 USBEndpointDescriptor simcard_dataOut;
569 /// Bulk IN endpoint descriptor
570 USBEndpointDescriptor simcard_dataIn;
571 /// Interrupt OUT endpoint descriptor
572 USBEndpointDescriptor simcard_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100573
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100574 USBInterfaceDescriptor phone;
575 USBEndpointDescriptor phone_dataOut;
576 USBEndpointDescriptor phone_dataIn;
577 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100578
Christina Quast01bbdc32015-02-24 17:38:45 +0100579} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100580
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100581static const SIMTraceDriverConfigurationDescriptorMITM
582 configurationDescriptorMITM = {
583 /* Standard configuration descriptor */
584 {
585 sizeof(USBConfigurationDescriptor),
586 USBGenericDescriptor_CONFIGURATION,
587 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
588 2, /* There are two interfaces in this configuration */
589 CFG_NUM_MITM, /* configuration number */
590 MITM_CONF_STR, /* string descriptor for this configuration */
591 USBD_BMATTRIBUTES,
592 USBConfigurationDescriptor_POWER(100)
593 },
594 // CCID interface descriptor
595 // Table 4.3-1 Interface Descriptor
596 // Interface descriptor
597 {
598 .bLength = sizeof(USBInterfaceDescriptor),
599 .bDescriptorType = USBGenericDescriptor_INTERFACE,
600 .bInterfaceNumber = 0,
601 .bAlternateSetting = 0,
602 .bNumEndpoints = 3,
603 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
604 .bInterfaceSubClass = 0,
605 .bInterfaceProtocol = 0,
606 .iInterface = CCID_CONF_STR,
607 },
608 {
609 .bLength = sizeof(CCIDDescriptor),
610 .bDescriptorType = CCID_DECRIPTOR_TYPE,
611 .bcdCCID = CCID1_10, // CCID version
612 .bMaxSlotIndex = 0, // 1 slot
613 .bVoltageSupport = VOLTS_3_0,
614 .dwProtocols = (1 << PROTOCOL_TO),
615 .dwDefaultClock = 3580,
616 .dwMaximumClock = 3580,
617 .bNumClockSupported = 0,
618 .dwDataRate = 9600,
619 .dwMaxDataRate = 9600,
620 .bNumDataRatesSupported = 0,
621 .dwMaxIFSD = 0xfe,
622 .dwSynchProtocols = 0,
623 .dwMechanical = 0,
624 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
625 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
626 CCID_FEATURES_EXC_TPDU,
627 .dwMaxCCIDMessageLength = 271, /* For extended APDU
628 level the value shall
629 be between 261 + 10 */
630 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
631 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
632 .wLcdLayout = 0, // wLcdLayout: no LCD
633 .bPINSupport = 0, // bPINSupport: No PIN
634 .bMaxCCIDBusySlots = 1,
635 },
636 // Bulk-OUT endpoint descriptor
637 {
638 .bLength = sizeof(USBEndpointDescriptor),
639 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
640 .bEndpointAddress =
641 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
642 CCID_EPT_DATA_OUT),
643 .bmAttributes = USBEndpointDescriptor_BULK,
644 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
645 CCID_EPT_DATA_OUT),
646 USBEndpointDescriptor_MAXBULKSIZE_FS),
647 .bInterval = 0x00,
648 },
649 // Bulk-IN endpoint descriptor
650 {
651 .bLength = sizeof(USBEndpointDescriptor),
652 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
653 .bEndpointAddress =
654 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
655 CCID_EPT_DATA_IN),
656 .bmAttributes = USBEndpointDescriptor_BULK,
657 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
658 CCID_EPT_DATA_IN),
659 USBEndpointDescriptor_MAXBULKSIZE_FS),
660 .bInterval = 0x00,
661 },
662 // Notification endpoint descriptor
663 {
664 .bLength = sizeof(USBEndpointDescriptor),
665 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
666 .bEndpointAddress =
667 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
668 CCID_EPT_NOTIFICATION),
669 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
670 .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(
671 CCID_EPT_NOTIFICATION),
672 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
673 .bInterval = 0x10,
674 },
675
676 /* Communication class interface standard descriptor */
677 {
678 sizeof(USBInterfaceDescriptor),
679 USBGenericDescriptor_INTERFACE,
680 1, /* This is interface #1 */
681 0, /* This is alternate setting #0 for this interface */
682 3, /* Number of endpoints */
683 0xff,
684 0,
685 0,
686 PHONE_CONF_STR, /* string descriptor for this interface */
687 }
688 ,
689 /* Bulk-OUT endpoint standard descriptor */
690 {
691 sizeof(USBEndpointDescriptor),
692 USBGenericDescriptor_ENDPOINT,
693 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
694 PHONE_DATAOUT),
695 USBEndpointDescriptor_BULK,
696 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
697 USBEndpointDescriptor_MAXBULKSIZE_FS),
698 0 /* Must be 0 for full-speed bulk endpoints */
699 }
700 ,
701 /* Bulk-IN endpoint descriptor */
702 {
703 sizeof(USBEndpointDescriptor),
704 USBGenericDescriptor_ENDPOINT,
705 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
706 PHONE_DATAIN),
707 USBEndpointDescriptor_BULK,
708 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
709 USBEndpointDescriptor_MAXBULKSIZE_FS),
710 0 /* Must be 0 for full-speed bulk endpoints */
711 }
712 ,
713 /* Notification endpoint descriptor */
714 {
715 sizeof(USBEndpointDescriptor),
716 USBGenericDescriptor_ENDPOINT,
717 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, PHONE_INT),
718 USBEndpointDescriptor_INTERRUPT,
719 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
720 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
721 0x10}
Christina Quastf5549502015-02-24 14:27:08 +0100722};
Harald Welte2fb59962016-02-28 12:34:26 +0100723#endif /* HAVE_CARDEM */
724
725const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
726#ifdef HAVE_SNIFFER
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100727 &configurationDescriptorSniffer.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100728#endif
729#ifdef HAVE_CCID
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100730 &configurationDescriptorCCID.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100731#endif
732#ifdef HAVE_CARDEM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100733 &configurationDescriptorPhone.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100734#endif
735#ifdef HAVE_MITM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100736 &configurationDescriptorMITM.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100737#endif
738};
Christina Quastf5549502015-02-24 14:27:08 +0100739
Christina Quastf5549502015-02-24 14:27:08 +0100740/** Standard USB device descriptor for the CDC serial driver */
741const USBDeviceDescriptor deviceDescriptor = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100742 .bLength = sizeof(USBDeviceDescriptor),
743 .bDescriptorType = USBGenericDescriptor_DEVICE,
744 .bcdUSB = USBDeviceDescriptor_USB2_00,
745 .bDeviceClass = 0xff,
746 .bDeviceSubClass = 0xff,
747 .bDeviceProtocol = 0xff,
748 .bMaxPacketSize0 = BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
749 .idVendor = SIMTRACE_VENDOR_ID,
750 .idProduct = SIMTRACE_PRODUCT_ID,
751 .bcdDevice = 2, /* Release number */
752 .iManufacturer = MANUF_STR,
753 .iProduct = PRODUCT_STRING,
754 .iSerialNumber = 0,
755 .bNumConfigurations = ARRAY_SIZE(configurationDescriptorsArr),
Christina Quast01bbdc32015-02-24 17:38:45 +0100756};
757
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200758/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100759static const USBDDriverDescriptors driverDescriptors = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100760 &deviceDescriptor,
761 (const USBConfigurationDescriptor **)&(configurationDescriptorsArr), /* first full-speed configuration descriptor */
762 0, /* No full-speed device qualifier descriptor */
763 0, /* No full-speed other speed configuration */
764 0, /* No high-speed device descriptor */
765 0, /* No high-speed configuration descriptor */
766 0, /* No high-speed device qualifier descriptor */
767 0, /* No high-speed other speed configuration descriptor */
768 stringDescriptors,
769 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100770};
771
Christina Quast0ae03142015-02-25 18:43:46 +0100772/*----------------------------------------------------------------------------
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100773 * Functions
Christina Quast968b9742015-02-25 14:10:12 +0100774 *----------------------------------------------------------------------------*/
775
776/**
777 * \brief Configure 48MHz Clock for USB
778 */
779static void _ConfigureUsbClock(void)
780{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100781 /* Enable PLLB for USB */
782 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
783 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd */
784 | CKGR_PLLBR_PLLBCOUNT_Msk;
785 while ((PMC->PMC_SR & PMC_SR_LOCKB) == 0) ;
786
787 /* USB Clock uses PLLB */
788 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
789 | PMC_USB_USBS; /* PLLB */
Christina Quast968b9742015-02-25 14:10:12 +0100790}
791
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100792void SIMtrace_USB_Initialize(void)
Christina Quast968b9742015-02-25 14:10:12 +0100793{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100794 _ConfigureUsbClock();
795 // Get std USB driver
796 USBDDriver *pUsbd = USBD_GetDriver();
Christina Quast968b9742015-02-25 14:10:12 +0100797
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100798 TRACE_DEBUG(".");
Christina Quast968b9742015-02-25 14:10:12 +0100799
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100800 // Initialize standard USB driver
801 USBDDriver_Initialize(pUsbd, &driverDescriptors, 0); // Multiple interface settings not supported
802 USBD_Init();
803 USBD_Connect();
Christina Quast968b9742015-02-25 14:10:12 +0100804
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100805 NVIC_EnableIRQ(UDP_IRQn);
Christina Quast968b9742015-02-25 14:10:12 +0100806}