blob: f6174c3a5acd14cf44150647096a67e03a991ce4 [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 Welte2fb59962016-02-28 12:34:26 +010035#include "utils.h"
Christina Quast968b9742015-02-25 14:10:12 +010036
Christina Quastdb7b1ab2015-03-03 12:34:36 +010037#include <cciddriverdescriptors.h>
38
Christina Quastf5549502015-02-24 14:27:08 +010039/*------------------------------------------------------------------------------
40 * USB String descriptors
41 *------------------------------------------------------------------------------*/
42
Christina Quast6032e792015-02-27 15:22:25 +010043
44static const unsigned char langDesc[] = {
45
46 USBStringDescriptor_LENGTH(1),
47 USBGenericDescriptor_STRING,
48 USBStringDescriptor_ENGLISH_US
49};
50
Harald Welte6d44c1f2015-11-07 19:01:30 +010051const unsigned char manufStringDescriptor[] = {
52
53 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'),
79};
80
Christina Quastf5549502015-02-24 14:27:08 +010081const unsigned char productStringDescriptor[] = {
82
Harald Weltebeb72932015-11-07 18:35:41 +010083 USBStringDescriptor_LENGTH(10),
Christina Quastf5549502015-02-24 14:27:08 +010084 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'),
Harald Weltebeb72932015-11-07 18:35:41 +010093 USBStringDescriptor_UNICODE(' '),
94 USBStringDescriptor_UNICODE('2'),
Christina Quastf5549502015-02-24 14:27:08 +010095};
96
97const unsigned char snifferConfigStringDescriptor[] = {
98
Harald Weltebeb72932015-11-07 18:35:41 +010099 USBStringDescriptor_LENGTH(16),
Christina Quastf5549502015-02-24 14:27:08 +0100100 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'),
Harald Weltebeb72932015-11-07 18:35:41 +0100109 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100110 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'),
117};
118
119const unsigned char CCIDConfigStringDescriptor[] = {
120
Harald Weltebeb72932015-11-07 18:35:41 +0100121 USBStringDescriptor_LENGTH(13),
Christina Quastf5549502015-02-24 14:27:08 +0100122 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'),
Harald Weltebeb72932015-11-07 18:35:41 +0100131 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100132 USBStringDescriptor_UNICODE('C'),
133 USBStringDescriptor_UNICODE('C'),
134 USBStringDescriptor_UNICODE('I'),
135 USBStringDescriptor_UNICODE('D'),
136};
137
138const unsigned char phoneConfigStringDescriptor[] = {
139
Harald Weltebeb72932015-11-07 18:35:41 +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'),
156};
157
158
159const unsigned char MITMConfigStringDescriptor[] = {
160
Christina Quastf5549502015-02-24 14:27:08 +0100161 USBStringDescriptor_LENGTH(13),
162 USBGenericDescriptor_STRING,
163 USBStringDescriptor_UNICODE('S'),
164 USBStringDescriptor_UNICODE('I'),
165 USBStringDescriptor_UNICODE('M'),
166 USBStringDescriptor_UNICODE('t'),
167 USBStringDescriptor_UNICODE('r'),
168 USBStringDescriptor_UNICODE('a'),
169 USBStringDescriptor_UNICODE('c'),
170 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100171 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100172 USBStringDescriptor_UNICODE('M'),
173 USBStringDescriptor_UNICODE('I'),
174 USBStringDescriptor_UNICODE('T'),
175 USBStringDescriptor_UNICODE('M'),
176};
177
178enum strDescNum {
Harald Welte6d44c1f2015-11-07 19:01:30 +0100179 PRODUCT_STRING = 1, MANUF_STR, SNIFFER_CONF_STR, CCID_CONF_STR, PHONE_CONF_STR, MITM_CONF_STR, STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +0100180};
181
182/** List of string descriptors used by the device */
Harald Welteec4fe232015-11-07 18:41:25 +0100183static const unsigned char *stringDescriptors[] = {
Christina Quast6032e792015-02-27 15:22:25 +0100184 langDesc,
Harald Welte6d44c1f2015-11-07 19:01:30 +0100185 [PRODUCT_STRING] = productStringDescriptor,
186 [MANUF_STR] = manufStringDescriptor,
187 [SNIFFER_CONF_STR] = snifferConfigStringDescriptor,
188 [CCID_CONF_STR] = CCIDConfigStringDescriptor,
189 [PHONE_CONF_STR] = phoneConfigStringDescriptor,
190 [MITM_CONF_STR] = MITMConfigStringDescriptor
Christina Quastf5549502015-02-24 14:27:08 +0100191};
192
Christina Quastf5549502015-02-24 14:27:08 +0100193/*------------------------------------------------------------------------------
194 * USB Device descriptors
195 *------------------------------------------------------------------------------*/
196
Harald Welte2fb59962016-02-28 12:34:26 +0100197#ifdef HAVE_SNIFFER
Christina Quastf5549502015-02-24 14:27:08 +0100198typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
199
200 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100201 USBConfigurationDescriptor configuration;
202 USBInterfaceDescriptor sniffer;
203 USBEndpointDescriptor sniffer_dataOut;
204 USBEndpointDescriptor sniffer_dataIn;
205 USBEndpointDescriptor sniffer_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100206
Christina Quast01bbdc32015-02-24 17:38:45 +0100207} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100208
Harald Welteec4fe232015-11-07 18:41:25 +0100209static const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffer = {
Christina Quastf5549502015-02-24 14:27:08 +0100210 /* Standard configuration descriptor */
211 {
212 sizeof(USBConfigurationDescriptor),
213 USBGenericDescriptor_CONFIGURATION,
214 sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
215 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100216 CFG_NUM_SNIFF, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100217 SNIFFER_CONF_STR, /* string descriptor for this configuration */
218 USBD_BMATTRIBUTES,
219 USBConfigurationDescriptor_POWER(100)
220 },
221 /* Communication class interface standard descriptor */
222 {
223 sizeof(USBInterfaceDescriptor),
224 USBGenericDescriptor_INTERFACE,
225 0, /* This is interface #0 */
226 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200227 3, /* Number of endpoints */
Christina Quastf5549502015-02-24 14:27:08 +0100228 0xff, /* Descriptor Class: Vendor specific */
229 0, /* No subclass */
230 0, /* No l */
231 SNIFFER_CONF_STR /* Third in string descriptor for this interface */
232 },
233 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100234 {
235 sizeof(USBEndpointDescriptor),
236 USBGenericDescriptor_ENDPOINT,
237 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200238 PHONE_DATAOUT),
Christina Quastf5549502015-02-24 14:27:08 +0100239 USBEndpointDescriptor_BULK,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200240 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quastf5549502015-02-24 14:27:08 +0100241 USBEndpointDescriptor_MAXBULKSIZE_FS),
242 0 /* Must be 0 for full-speed bulk endpoints */
243 },
244 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100245 {
246 sizeof(USBEndpointDescriptor),
247 USBGenericDescriptor_ENDPOINT,
248 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200249 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100250 USBEndpointDescriptor_BULK,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200251 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100252 USBEndpointDescriptor_MAXBULKSIZE_FS),
253 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100254 },
255 // Notification endpoint descriptor
256 {
257 sizeof(USBEndpointDescriptor),
258 USBGenericDescriptor_ENDPOINT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200259 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100260 USBEndpointDescriptor_INTERRUPT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200261 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100262 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
263 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100264 }
265};
Harald Welte2fb59962016-02-28 12:34:26 +0100266#endif /* HAVE_SNIFFER */
Christina Quastf5549502015-02-24 14:27:08 +0100267
Harald Welte2fb59962016-02-28 12:34:26 +0100268
269#ifdef HAVE_CCID
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100270/*
271/// CCIDDriverConfiguration Descriptors
272/// List of descriptors that make up the configuration descriptors of a
273/// device using the CCID driver.
Christina Quast01bbdc32015-02-24 17:38:45 +0100274typedef struct {
275
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100276 /// Configuration descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100277 USBConfigurationDescriptor configuration;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100278 /// Interface descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100279 USBInterfaceDescriptor interface;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100280 /// CCID descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100281 CCIDDescriptor ccid;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100282 /// Bulk OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100283 USBEndpointDescriptor bulkOut;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100284 /// Bulk IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100285 USBEndpointDescriptor bulkIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100286 /// Interrupt OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100287 USBEndpointDescriptor interruptIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100288} __attribute__ ((packed)) CCIDDriverConfigurationDescriptors;
289*/
Christina Quast01bbdc32015-02-24 17:38:45 +0100290
Harald Welteec4fe232015-11-07 18:41:25 +0100291static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100292
293 // Standard USB configuration descriptor
294 {
295 sizeof(USBConfigurationDescriptor),
296 USBGenericDescriptor_CONFIGURATION,
297 sizeof(CCIDDriverConfigurationDescriptors),
298 1, // One interface in this configuration
299 CFG_NUM_CCID, // This is configuration #1
300 CCID_CONF_STR, // associated string descriptor
301 BOARD_USB_BMATTRIBUTES,
302 USBConfigurationDescriptor_POWER(100)
303 },
304 // CCID interface descriptor
305 // Table 4.3-1 Interface Descriptor
306 // Interface descriptor
307 {
308 sizeof(USBInterfaceDescriptor),
309 USBGenericDescriptor_INTERFACE,
310 0, // Interface 0
311 0, // No alternate settings
312 3, // uses bulk-IN, bulk-OUT and interrupt IN
313 SMART_CARD_DEVICE_CLASS,
314 0, // Subclass code
315 0, // bulk transfers optional interrupt-IN
316 CCID_CONF_STR // associated string descriptor
317 },
318 {
319 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
320 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
321 CCID1_10, // bcdCCID: CCID version
322 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
Harald Welte8a5b5802015-11-07 18:52:52 +0100323 VOLTS_3_0, // bVoltageSupport
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100324 (1 << PROTOCOL_TO), // dwProtocols
325 3580, // dwDefaultClock
326 3580, // dwMaxClock
327 0, // bNumClockSupported
328 9600, // dwDataRate : 9600 bauds
329 9600, // dwMaxDataRate : 9600 bauds
330 0, // bNumDataRatesSupported
331 0xfe, // dwMaxIFSD
332 0, // dwSynchProtocols
333 0, // dwMechanical
334 //0x00010042, // dwFeatures: Short APDU level exchanges
335 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
336 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
337 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
338 0xFF, // bClassGetResponse: Echoes the class of the APDU
339 0xFF, // bClassEnvelope: Echoes the class of the APDU
340 0, // wLcdLayout: no LCD
341 0, // bPINSupport: No PIN
342 1 // bMaxCCIDBusySlot
343 },
344 // Bulk-OUT endpoint descriptor
345 {
346 sizeof(USBEndpointDescriptor),
347 USBGenericDescriptor_ENDPOINT,
348 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
349 USBEndpointDescriptor_BULK,
350 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
351 USBEndpointDescriptor_MAXBULKSIZE_FS),
352 0x00 // Does not apply to Bulk endpoints
353 },
354 // Bulk-IN endpoint descriptor
355 {
356 sizeof(USBEndpointDescriptor),
357 USBGenericDescriptor_ENDPOINT,
358 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
359 USBEndpointDescriptor_BULK,
360 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
361 USBEndpointDescriptor_MAXBULKSIZE_FS),
362 0x00 // Does not apply to Bulk endpoints
363 },
364 // Notification endpoint descriptor
365 {
366 sizeof(USBEndpointDescriptor),
367 USBGenericDescriptor_ENDPOINT,
368 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
369 USBEndpointDescriptor_INTERRUPT,
370 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
371 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
372 0x10
373 },
374};
Harald Welte2fb59962016-02-28 12:34:26 +0100375#endif /* HAVE_CCID */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100376
Christina Quast01bbdc32015-02-24 17:38:45 +0100377
Harald Welte2fb59962016-02-28 12:34:26 +0100378#ifdef HAVE_CARDEM
Christina Quast01bbdc32015-02-24 17:38:45 +0100379/* SIM card emulator */
380typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
381
382 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100383 USBConfigurationDescriptor configuration;
384 USBInterfaceDescriptor phone;
385 USBEndpointDescriptor phone_dataOut;
386 USBEndpointDescriptor phone_dataIn;
387 USBEndpointDescriptor phone_interruptIn;
Christina Quast01bbdc32015-02-24 17:38:45 +0100388} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
389
Harald Welteec4fe232015-11-07 18:41:25 +0100390static const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = {
Christina Quast01bbdc32015-02-24 17:38:45 +0100391 /* Standard configuration descriptor */
392 {
393 sizeof(USBConfigurationDescriptor),
394 USBGenericDescriptor_CONFIGURATION,
Christina Quast6d9dcfc2015-04-03 22:26:43 +0200395 sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Christina Quast01bbdc32015-02-24 17:38:45 +0100396 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100397 CFG_NUM_PHONE, /* configuration number */
Christina Quast01bbdc32015-02-24 17:38:45 +0100398 PHONE_CONF_STR, /* string descriptor for this configuration */
399 USBD_BMATTRIBUTES,
400 USBConfigurationDescriptor_POWER(100)
401 },
402 /* Communication class interface standard descriptor */
403 {
404 sizeof(USBInterfaceDescriptor),
405 USBGenericDescriptor_INTERFACE,
406 0, /* This is interface #0 */
407 0, /* This is alternate setting #0 for this interface */
Christina Quastb65b8812015-04-04 10:51:37 +0200408 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100409 0xff, /* Descriptor Class: Vendor specific */
410 0, /* No subclass */
411 0, /* No l */
412 PHONE_CONF_STR /* Third in string descriptor for this interface */
413 },
414 /* Bulk-OUT endpoint standard descriptor */
415 {
416 sizeof(USBEndpointDescriptor),
417 USBGenericDescriptor_ENDPOINT,
418 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200419 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100420 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200421 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100422 USBEndpointDescriptor_MAXBULKSIZE_FS),
423 0 /* Must be 0 for full-speed bulk endpoints */
424 },
425 /* Bulk-IN endpoint descriptor */
426 {
427 sizeof(USBEndpointDescriptor),
428 USBGenericDescriptor_ENDPOINT,
429 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200430 PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100431 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200432 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100433 USBEndpointDescriptor_MAXBULKSIZE_FS),
434 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100435 },
436 /* Notification endpoint descriptor */
437 {
438 sizeof(USBEndpointDescriptor),
439 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200440 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100441 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200442 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100443 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
444 0x10
Christina Quast01bbdc32015-02-24 17:38:45 +0100445 }
446};
Harald Welte2fb59962016-02-28 12:34:26 +0100447#endif /* HAVE_CARDEM */
Christina Quast01bbdc32015-02-24 17:38:45 +0100448
Harald Welte2fb59962016-02-28 12:34:26 +0100449#ifdef HAVE_MITM
Christina Quastf5549502015-02-24 14:27:08 +0100450typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
451
452 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100453 USBConfigurationDescriptor configuration;
454 USBInterfaceDescriptor simcard;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200455 /// CCID descriptor
456 CCIDDescriptor ccid;
457 /// Bulk OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100458 USBEndpointDescriptor simcard_dataOut;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200459 /// Bulk IN endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100460 USBEndpointDescriptor simcard_dataIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200461 /// Interrupt OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100462 USBEndpointDescriptor simcard_interruptIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200463
Christina Quastda373fd2015-02-27 15:25:22 +0100464 USBInterfaceDescriptor phone;
465 USBEndpointDescriptor phone_dataOut;
466 USBEndpointDescriptor phone_dataIn;
467 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100468
Christina Quast01bbdc32015-02-24 17:38:45 +0100469} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100470
Harald Welteec4fe232015-11-07 18:41:25 +0100471static const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
Christina Quastf5549502015-02-24 14:27:08 +0100472 /* Standard configuration descriptor */
473 {
474 sizeof(USBConfigurationDescriptor),
475 USBGenericDescriptor_CONFIGURATION,
Christina Quast968b9742015-02-25 14:10:12 +0100476 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Christina Quastf5549502015-02-24 14:27:08 +0100477 2, /* There are two interfaces in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100478 CFG_NUM_MITM, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100479 MITM_CONF_STR, /* string descriptor for this configuration */
480 USBD_BMATTRIBUTES,
481 USBConfigurationDescriptor_POWER(100)
482 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200483 // CCID interface descriptor
484 // Table 4.3-1 Interface Descriptor
485 // Interface descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100486 {
487 sizeof(USBInterfaceDescriptor),
488 USBGenericDescriptor_INTERFACE,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200489 0, // Interface 0
490 0, // No alternate settings
491 3, // uses bulk-IN, bulk-OUT and interrupt IN
492 SMART_CARD_DEVICE_CLASS,
493 0, // Subclass code
494 0, // bulk transfers optional interrupt-IN
495 CCID_CONF_STR // associated string descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100496 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200497 {
498 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
499 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
500 CCID1_10, // bcdCCID: CCID version
501 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
Harald Welte8a5b5802015-11-07 18:52:52 +0100502 VOLTS_3_0, // bVoltageSupport
Christina Quaste01fb9a2015-04-04 19:56:23 +0200503 (1 << PROTOCOL_TO), // dwProtocols
504 3580, // dwDefaultClock
505 3580, // dwMaxClock
506 0, // bNumClockSupported
507 9600, // dwDataRate : 9600 bauds
508 9600, // dwMaxDataRate : 9600 bauds
509 0, // bNumDataRatesSupported
510 0xfe, // dwMaxIFSD
511 0, // dwSynchProtocols
512 0, // dwMechanical
513 //0x00010042, // dwFeatures: Short APDU level exchanges
514 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
515 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
516 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
517 0xFF, // bClassGetResponse: Echoes the class of the APDU
518 0xFF, // bClassEnvelope: Echoes the class of the APDU
519 0, // wLcdLayout: no LCD
520 0, // bPINSupport: No PIN
521 1 // bMaxCCIDBusySlot
522 },
523 // Bulk-OUT endpoint descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100524 {
525 sizeof(USBEndpointDescriptor),
526 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200527 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
Christina Quastf5549502015-02-24 14:27:08 +0100528 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200529 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
Christina Quastf5549502015-02-24 14:27:08 +0100530 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200531 0x00 // Does not apply to Bulk endpoints
Christina Quastf5549502015-02-24 14:27:08 +0100532 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200533 // Bulk-IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100534 {
535 sizeof(USBEndpointDescriptor),
536 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200537 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
Christina Quast01bbdc32015-02-24 17:38:45 +0100538 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200539 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100540 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200541 0x00 // Does not apply to Bulk endpoints
Christina Quast968b9742015-02-25 14:10:12 +0100542 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200543 // Notification endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100544 {
545 sizeof(USBEndpointDescriptor),
546 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200547 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
Christina Quastda373fd2015-02-27 15:25:22 +0100548 USBEndpointDescriptor_INTERRUPT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200549 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
Christina Quastda373fd2015-02-27 15:25:22 +0100550 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
551 0x10
552 },
Christina Quast01bbdc32015-02-24 17:38:45 +0100553 /* Communication class interface standard descriptor */
554 {
555 sizeof(USBInterfaceDescriptor),
556 USBGenericDescriptor_INTERFACE,
557 1, /* This is interface #1 */
558 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200559 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100560 0xff,
561 0,
Christina Quastbfd73542015-05-08 16:00:51 +0200562 0,
563 PHONE_CONF_STR, /* string descriptor for this interface */
Christina Quast01bbdc32015-02-24 17:38:45 +0100564 },
565 /* Bulk-OUT endpoint standard descriptor */
566 {
567 sizeof(USBEndpointDescriptor),
568 USBGenericDescriptor_ENDPOINT,
569 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200570 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100571 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200572 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100573 USBEndpointDescriptor_MAXBULKSIZE_FS),
574 0 /* Must be 0 for full-speed bulk endpoints */
575 },
576 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100577 {
578 sizeof(USBEndpointDescriptor),
579 USBGenericDescriptor_ENDPOINT,
580 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200581 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100582 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200583 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100584 USBEndpointDescriptor_MAXBULKSIZE_FS),
585 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100586 },
587 /* Notification endpoint descriptor */
588 {
589 sizeof(USBEndpointDescriptor),
590 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200591 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100592 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200593 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100594 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
595 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100596 }
597};
Harald Welte2fb59962016-02-28 12:34:26 +0100598#endif /* HAVE_CARDEM */
599
600const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
601#ifdef HAVE_SNIFFER
602 &configurationDescriptorSniffer.configuration,
603#endif
604#ifdef HAVE_CCID
605 &configurationDescriptorCCID.configuration,
606#endif
607#ifdef HAVE_CARDEM
608 &configurationDescriptorPhone.configuration,
609#endif
610#ifdef HAVE_MITM
611 &configurationDescriptorMITM.configuration,
612#endif
613};
Christina Quastf5549502015-02-24 14:27:08 +0100614
Christina Quastf5549502015-02-24 14:27:08 +0100615/** Standard USB device descriptor for the CDC serial driver */
616const USBDeviceDescriptor deviceDescriptor = {
617
618 sizeof(USBDeviceDescriptor),
619 USBGenericDescriptor_DEVICE,
620 USBDeviceDescriptor_USB2_00,
621 0xff,
622// CDCDeviceDescriptor_CLASS,
623 0xff,
624// CDCDeviceDescriptor_SUBCLASS,
625 0xff,
626// CDCDeviceDescriptor_PROTOCOL,
627 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
Christina Quast37350392015-05-08 16:47:26 +0200628 SIMTRACE_VENDOR_ID,
Christina Quastf5549502015-02-24 14:27:08 +0100629 SIMTRACE_PRODUCT_ID,
630 1, /* Release number */
Harald Welte6d44c1f2015-11-07 19:01:30 +0100631 MANUF_STR, /* Indesx of manufacturer string descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100632 PRODUCT_STRING, /* Index of product string descriptor */
633 0, /* No string descriptor for serial number */
Harald Welte2fb59962016-02-28 12:34:26 +0100634 ARRAY_SIZE(configurationDescriptorsArr) /* Device has N possible configs */
Christina Quast01bbdc32015-02-24 17:38:45 +0100635};
636
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200637/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100638static const USBDDriverDescriptors driverDescriptors = {
Christina Quastf5549502015-02-24 14:27:08 +0100639 &deviceDescriptor,
Christina Quastb06fc382015-04-06 23:30:21 +0200640 (const USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100641 0, /* No full-speed device qualifier descriptor */
642 0, /* No full-speed other speed configuration */
643 0, /* No high-speed device descriptor */
644 0, /* No high-speed configuration descriptor */
645 0, /* No high-speed device qualifier descriptor */
646 0, /* No high-speed other speed configuration descriptor */
647 stringDescriptors,
Christina Quast54d0c1f2015-02-25 16:04:25 +0100648 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100649};
650
Christina Quast0ae03142015-02-25 18:43:46 +0100651
652/*----------------------------------------------------------------------------
Christina Quast968b9742015-02-25 14:10:12 +0100653 * Functions
654 *----------------------------------------------------------------------------*/
655
656/**
657 * \brief Configure 48MHz Clock for USB
658 */
659static void _ConfigureUsbClock(void)
660{
661 /* Enable PLLB for USB */
Christina Quast968b9742015-02-25 14:10:12 +0100662 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
663 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
664 | CKGR_PLLBR_PLLBCOUNT_Msk;
665 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
666 /* USB Clock uses PLLB */
667 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
668 | PMC_USB_USBS; /* PLLB */
669}
670
671
672void SIMtrace_USB_Initialize( void )
673{
674 _ConfigureUsbClock();
675 // Get std USB driver
676 USBDDriver *pUsbd = USBD_GetDriver();
677
678 TRACE_DEBUG(".");
679
680 // Initialize standard USB driver
681 USBDDriver_Initialize(pUsbd,
682 &driverDescriptors,
Christina Quast968b9742015-02-25 14:10:12 +0100683 0); // Multiple interface settings not supported
Christina Quast968b9742015-02-25 14:10:12 +0100684 USBD_Init();
Christina Quast968b9742015-02-25 14:10:12 +0100685 USBD_Connect();
686
687 NVIC_EnableIRQ( UDP_IRQn );
688}