blob: 686f6bf7b9644f044fc6196f175c2e72a1c0413b [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"
35
Christina Quastdb7b1ab2015-03-03 12:34:36 +010036#include <cciddriverdescriptors.h>
37
Christina Quastf5549502015-02-24 14:27:08 +010038/*------------------------------------------------------------------------------
39 * USB String descriptors
40 *------------------------------------------------------------------------------*/
41
Christina Quast6032e792015-02-27 15:22:25 +010042
43static const unsigned char langDesc[] = {
44
45 USBStringDescriptor_LENGTH(1),
46 USBGenericDescriptor_STRING,
47 USBStringDescriptor_ENGLISH_US
48};
49
Harald Welte6d44c1f2015-11-07 19:01:30 +010050const unsigned char manufStringDescriptor[] = {
51
52 USBStringDescriptor_LENGTH(24),
53 USBGenericDescriptor_STRING,
54 USBStringDescriptor_UNICODE('s'),
55 USBStringDescriptor_UNICODE('y'),
56 USBStringDescriptor_UNICODE('s'),
57 USBStringDescriptor_UNICODE('m'),
58 USBStringDescriptor_UNICODE('o'),
59 USBStringDescriptor_UNICODE('c'),
60 USBStringDescriptor_UNICODE('o'),
61 USBStringDescriptor_UNICODE('m'),
62 USBStringDescriptor_UNICODE(' '),
63 USBStringDescriptor_UNICODE('-'),
64 USBStringDescriptor_UNICODE(' '),
65 USBStringDescriptor_UNICODE('s'),
66 USBStringDescriptor_UNICODE('.'),
67 USBStringDescriptor_UNICODE('f'),
68 USBStringDescriptor_UNICODE('.'),
69 USBStringDescriptor_UNICODE('m'),
70 USBStringDescriptor_UNICODE('.'),
71 USBStringDescriptor_UNICODE('c'),
72 USBStringDescriptor_UNICODE('.'),
73 USBStringDescriptor_UNICODE(' '),
74 USBStringDescriptor_UNICODE('G'),
75 USBStringDescriptor_UNICODE('m'),
76 USBStringDescriptor_UNICODE('b'),
77 USBStringDescriptor_UNICODE('H'),
78};
79
Christina Quastf5549502015-02-24 14:27:08 +010080const unsigned char productStringDescriptor[] = {
81
Harald Weltebeb72932015-11-07 18:35:41 +010082 USBStringDescriptor_LENGTH(10),
Christina Quastf5549502015-02-24 14:27:08 +010083 USBGenericDescriptor_STRING,
84 USBStringDescriptor_UNICODE('S'),
85 USBStringDescriptor_UNICODE('I'),
86 USBStringDescriptor_UNICODE('M'),
87 USBStringDescriptor_UNICODE('t'),
88 USBStringDescriptor_UNICODE('r'),
89 USBStringDescriptor_UNICODE('a'),
90 USBStringDescriptor_UNICODE('c'),
91 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +010092 USBStringDescriptor_UNICODE(' '),
93 USBStringDescriptor_UNICODE('2'),
Christina Quastf5549502015-02-24 14:27:08 +010094};
95
96const unsigned char snifferConfigStringDescriptor[] = {
97
Harald Weltebeb72932015-11-07 18:35:41 +010098 USBStringDescriptor_LENGTH(16),
Christina Quastf5549502015-02-24 14:27:08 +010099 USBGenericDescriptor_STRING,
100 USBStringDescriptor_UNICODE('S'),
101 USBStringDescriptor_UNICODE('I'),
102 USBStringDescriptor_UNICODE('M'),
103 USBStringDescriptor_UNICODE('t'),
104 USBStringDescriptor_UNICODE('r'),
105 USBStringDescriptor_UNICODE('a'),
106 USBStringDescriptor_UNICODE('c'),
107 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100108 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100109 USBStringDescriptor_UNICODE('S'),
110 USBStringDescriptor_UNICODE('n'),
111 USBStringDescriptor_UNICODE('i'),
112 USBStringDescriptor_UNICODE('f'),
113 USBStringDescriptor_UNICODE('f'),
114 USBStringDescriptor_UNICODE('e'),
115 USBStringDescriptor_UNICODE('r'),
116};
117
118const unsigned char CCIDConfigStringDescriptor[] = {
119
Harald Weltebeb72932015-11-07 18:35:41 +0100120 USBStringDescriptor_LENGTH(13),
Christina Quastf5549502015-02-24 14:27:08 +0100121 USBGenericDescriptor_STRING,
122 USBStringDescriptor_UNICODE('S'),
123 USBStringDescriptor_UNICODE('I'),
124 USBStringDescriptor_UNICODE('M'),
125 USBStringDescriptor_UNICODE('t'),
126 USBStringDescriptor_UNICODE('r'),
127 USBStringDescriptor_UNICODE('a'),
128 USBStringDescriptor_UNICODE('c'),
129 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100130 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100131 USBStringDescriptor_UNICODE('C'),
132 USBStringDescriptor_UNICODE('C'),
133 USBStringDescriptor_UNICODE('I'),
134 USBStringDescriptor_UNICODE('D'),
135};
136
137const unsigned char phoneConfigStringDescriptor[] = {
138
Harald Weltebeb72932015-11-07 18:35:41 +0100139 USBStringDescriptor_LENGTH(14),
140 USBGenericDescriptor_STRING,
141 USBStringDescriptor_UNICODE('S'),
142 USBStringDescriptor_UNICODE('I'),
143 USBStringDescriptor_UNICODE('M'),
144 USBStringDescriptor_UNICODE('t'),
145 USBStringDescriptor_UNICODE('r'),
146 USBStringDescriptor_UNICODE('a'),
147 USBStringDescriptor_UNICODE('c'),
148 USBStringDescriptor_UNICODE('e'),
149 USBStringDescriptor_UNICODE(' '),
150 USBStringDescriptor_UNICODE('P'),
151 USBStringDescriptor_UNICODE('h'),
152 USBStringDescriptor_UNICODE('o'),
153 USBStringDescriptor_UNICODE('n'),
154 USBStringDescriptor_UNICODE('e'),
155};
156
157
158const unsigned char MITMConfigStringDescriptor[] = {
159
Christina Quastf5549502015-02-24 14:27:08 +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'),
Harald Weltebeb72932015-11-07 18:35:41 +0100170 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100171 USBStringDescriptor_UNICODE('M'),
172 USBStringDescriptor_UNICODE('I'),
173 USBStringDescriptor_UNICODE('T'),
174 USBStringDescriptor_UNICODE('M'),
175};
176
177enum strDescNum {
Harald Welte6d44c1f2015-11-07 19:01:30 +0100178 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 +0100179};
180
181/** List of string descriptors used by the device */
Harald Welteec4fe232015-11-07 18:41:25 +0100182static const unsigned char *stringDescriptors[] = {
Christina Quast6032e792015-02-27 15:22:25 +0100183 langDesc,
Harald Welte6d44c1f2015-11-07 19:01:30 +0100184 [PRODUCT_STRING] = productStringDescriptor,
185 [MANUF_STR] = manufStringDescriptor,
186 [SNIFFER_CONF_STR] = snifferConfigStringDescriptor,
187 [CCID_CONF_STR] = CCIDConfigStringDescriptor,
188 [PHONE_CONF_STR] = phoneConfigStringDescriptor,
189 [MITM_CONF_STR] = MITMConfigStringDescriptor
Christina Quastf5549502015-02-24 14:27:08 +0100190};
191
Christina Quastf5549502015-02-24 14:27:08 +0100192/*------------------------------------------------------------------------------
193 * USB Device descriptors
194 *------------------------------------------------------------------------------*/
195
196typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
197
198 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100199 USBConfigurationDescriptor configuration;
200 USBInterfaceDescriptor sniffer;
201 USBEndpointDescriptor sniffer_dataOut;
202 USBEndpointDescriptor sniffer_dataIn;
203 USBEndpointDescriptor sniffer_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100204
Christina Quast01bbdc32015-02-24 17:38:45 +0100205} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100206
Harald Welteec4fe232015-11-07 18:41:25 +0100207static const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffer = {
Christina Quastf5549502015-02-24 14:27:08 +0100208 /* Standard configuration descriptor */
209 {
210 sizeof(USBConfigurationDescriptor),
211 USBGenericDescriptor_CONFIGURATION,
212 sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
213 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100214 CFG_NUM_SNIFF, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100215 SNIFFER_CONF_STR, /* string descriptor for this configuration */
216 USBD_BMATTRIBUTES,
217 USBConfigurationDescriptor_POWER(100)
218 },
219 /* Communication class interface standard descriptor */
220 {
221 sizeof(USBInterfaceDescriptor),
222 USBGenericDescriptor_INTERFACE,
223 0, /* This is interface #0 */
224 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200225 3, /* Number of endpoints */
Christina Quastf5549502015-02-24 14:27:08 +0100226 0xff, /* Descriptor Class: Vendor specific */
227 0, /* No subclass */
228 0, /* No l */
229 SNIFFER_CONF_STR /* Third in string descriptor for this interface */
230 },
231 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100232 {
233 sizeof(USBEndpointDescriptor),
234 USBGenericDescriptor_ENDPOINT,
235 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200236 PHONE_DATAOUT),
Christina Quastf5549502015-02-24 14:27:08 +0100237 USBEndpointDescriptor_BULK,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200238 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quastf5549502015-02-24 14:27:08 +0100239 USBEndpointDescriptor_MAXBULKSIZE_FS),
240 0 /* Must be 0 for full-speed bulk endpoints */
241 },
242 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100243 {
244 sizeof(USBEndpointDescriptor),
245 USBGenericDescriptor_ENDPOINT,
246 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200247 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100248 USBEndpointDescriptor_BULK,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200249 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100250 USBEndpointDescriptor_MAXBULKSIZE_FS),
251 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100252 },
253 // Notification endpoint descriptor
254 {
255 sizeof(USBEndpointDescriptor),
256 USBGenericDescriptor_ENDPOINT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200257 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100258 USBEndpointDescriptor_INTERRUPT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200259 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100260 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
261 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100262 }
263};
264
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100265/*
266/// CCIDDriverConfiguration Descriptors
267/// List of descriptors that make up the configuration descriptors of a
268/// device using the CCID driver.
Christina Quast01bbdc32015-02-24 17:38:45 +0100269typedef struct {
270
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100271 /// Configuration descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100272 USBConfigurationDescriptor configuration;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100273 /// Interface descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100274 USBInterfaceDescriptor interface;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100275 /// CCID descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100276 CCIDDescriptor ccid;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100277 /// Bulk OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100278 USBEndpointDescriptor bulkOut;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100279 /// Bulk IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100280 USBEndpointDescriptor bulkIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100281 /// Interrupt OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100282 USBEndpointDescriptor interruptIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100283} __attribute__ ((packed)) CCIDDriverConfigurationDescriptors;
284*/
Christina Quast01bbdc32015-02-24 17:38:45 +0100285
Harald Welteec4fe232015-11-07 18:41:25 +0100286static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100287
288 // Standard USB configuration descriptor
289 {
290 sizeof(USBConfigurationDescriptor),
291 USBGenericDescriptor_CONFIGURATION,
292 sizeof(CCIDDriverConfigurationDescriptors),
293 1, // One interface in this configuration
294 CFG_NUM_CCID, // This is configuration #1
295 CCID_CONF_STR, // associated string descriptor
296 BOARD_USB_BMATTRIBUTES,
297 USBConfigurationDescriptor_POWER(100)
298 },
299 // CCID interface descriptor
300 // Table 4.3-1 Interface Descriptor
301 // Interface descriptor
302 {
303 sizeof(USBInterfaceDescriptor),
304 USBGenericDescriptor_INTERFACE,
305 0, // Interface 0
306 0, // No alternate settings
307 3, // uses bulk-IN, bulk-OUT and interrupt IN
308 SMART_CARD_DEVICE_CLASS,
309 0, // Subclass code
310 0, // bulk transfers optional interrupt-IN
311 CCID_CONF_STR // associated string descriptor
312 },
313 {
314 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
315 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
316 CCID1_10, // bcdCCID: CCID version
317 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
Harald Welte8a5b5802015-11-07 18:52:52 +0100318 VOLTS_3_0, // bVoltageSupport
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100319 (1 << PROTOCOL_TO), // dwProtocols
320 3580, // dwDefaultClock
321 3580, // dwMaxClock
322 0, // bNumClockSupported
323 9600, // dwDataRate : 9600 bauds
324 9600, // dwMaxDataRate : 9600 bauds
325 0, // bNumDataRatesSupported
326 0xfe, // dwMaxIFSD
327 0, // dwSynchProtocols
328 0, // dwMechanical
329 //0x00010042, // dwFeatures: Short APDU level exchanges
330 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
331 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
332 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
333 0xFF, // bClassGetResponse: Echoes the class of the APDU
334 0xFF, // bClassEnvelope: Echoes the class of the APDU
335 0, // wLcdLayout: no LCD
336 0, // bPINSupport: No PIN
337 1 // bMaxCCIDBusySlot
338 },
339 // Bulk-OUT endpoint descriptor
340 {
341 sizeof(USBEndpointDescriptor),
342 USBGenericDescriptor_ENDPOINT,
343 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
344 USBEndpointDescriptor_BULK,
345 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
346 USBEndpointDescriptor_MAXBULKSIZE_FS),
347 0x00 // Does not apply to Bulk endpoints
348 },
349 // Bulk-IN endpoint descriptor
350 {
351 sizeof(USBEndpointDescriptor),
352 USBGenericDescriptor_ENDPOINT,
353 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
354 USBEndpointDescriptor_BULK,
355 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
356 USBEndpointDescriptor_MAXBULKSIZE_FS),
357 0x00 // Does not apply to Bulk endpoints
358 },
359 // Notification endpoint descriptor
360 {
361 sizeof(USBEndpointDescriptor),
362 USBGenericDescriptor_ENDPOINT,
363 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
364 USBEndpointDescriptor_INTERRUPT,
365 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
366 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
367 0x10
368 },
369};
370
Christina Quast01bbdc32015-02-24 17:38:45 +0100371
372/* SIM card emulator */
373typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
374
375 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100376 USBConfigurationDescriptor configuration;
377 USBInterfaceDescriptor phone;
378 USBEndpointDescriptor phone_dataOut;
379 USBEndpointDescriptor phone_dataIn;
380 USBEndpointDescriptor phone_interruptIn;
Christina Quast01bbdc32015-02-24 17:38:45 +0100381} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
382
Harald Welteec4fe232015-11-07 18:41:25 +0100383static const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = {
Christina Quast01bbdc32015-02-24 17:38:45 +0100384 /* Standard configuration descriptor */
385 {
386 sizeof(USBConfigurationDescriptor),
387 USBGenericDescriptor_CONFIGURATION,
Christina Quast6d9dcfc2015-04-03 22:26:43 +0200388 sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Christina Quast01bbdc32015-02-24 17:38:45 +0100389 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100390 CFG_NUM_PHONE, /* configuration number */
Christina Quast01bbdc32015-02-24 17:38:45 +0100391 PHONE_CONF_STR, /* string descriptor for this configuration */
392 USBD_BMATTRIBUTES,
393 USBConfigurationDescriptor_POWER(100)
394 },
395 /* Communication class interface standard descriptor */
396 {
397 sizeof(USBInterfaceDescriptor),
398 USBGenericDescriptor_INTERFACE,
399 0, /* This is interface #0 */
400 0, /* This is alternate setting #0 for this interface */
Christina Quastb65b8812015-04-04 10:51:37 +0200401 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100402 0xff, /* Descriptor Class: Vendor specific */
403 0, /* No subclass */
404 0, /* No l */
405 PHONE_CONF_STR /* Third in string descriptor for this interface */
406 },
407 /* Bulk-OUT endpoint standard descriptor */
408 {
409 sizeof(USBEndpointDescriptor),
410 USBGenericDescriptor_ENDPOINT,
411 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200412 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100413 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200414 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100415 USBEndpointDescriptor_MAXBULKSIZE_FS),
416 0 /* Must be 0 for full-speed bulk endpoints */
417 },
418 /* Bulk-IN endpoint descriptor */
419 {
420 sizeof(USBEndpointDescriptor),
421 USBGenericDescriptor_ENDPOINT,
422 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200423 PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100424 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200425 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100426 USBEndpointDescriptor_MAXBULKSIZE_FS),
427 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100428 },
429 /* Notification endpoint descriptor */
430 {
431 sizeof(USBEndpointDescriptor),
432 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200433 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100434 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200435 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100436 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
437 0x10
Christina Quast01bbdc32015-02-24 17:38:45 +0100438 }
439};
440
Christina Quastf5549502015-02-24 14:27:08 +0100441
442typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
443
444 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100445 USBConfigurationDescriptor configuration;
446 USBInterfaceDescriptor simcard;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200447 /// CCID descriptor
448 CCIDDescriptor ccid;
449 /// Bulk OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100450 USBEndpointDescriptor simcard_dataOut;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200451 /// Bulk IN endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100452 USBEndpointDescriptor simcard_dataIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200453 /// Interrupt OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100454 USBEndpointDescriptor simcard_interruptIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200455
Christina Quastda373fd2015-02-27 15:25:22 +0100456 USBInterfaceDescriptor phone;
457 USBEndpointDescriptor phone_dataOut;
458 USBEndpointDescriptor phone_dataIn;
459 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100460
Christina Quast01bbdc32015-02-24 17:38:45 +0100461} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100462
Harald Welteec4fe232015-11-07 18:41:25 +0100463static const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
Christina Quastf5549502015-02-24 14:27:08 +0100464 /* Standard configuration descriptor */
465 {
466 sizeof(USBConfigurationDescriptor),
467 USBGenericDescriptor_CONFIGURATION,
Christina Quast968b9742015-02-25 14:10:12 +0100468 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Christina Quastf5549502015-02-24 14:27:08 +0100469 2, /* There are two interfaces in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100470 CFG_NUM_MITM, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100471 MITM_CONF_STR, /* string descriptor for this configuration */
472 USBD_BMATTRIBUTES,
473 USBConfigurationDescriptor_POWER(100)
474 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200475 // CCID interface descriptor
476 // Table 4.3-1 Interface Descriptor
477 // Interface descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100478 {
479 sizeof(USBInterfaceDescriptor),
480 USBGenericDescriptor_INTERFACE,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200481 0, // Interface 0
482 0, // No alternate settings
483 3, // uses bulk-IN, bulk-OUT and interrupt IN
484 SMART_CARD_DEVICE_CLASS,
485 0, // Subclass code
486 0, // bulk transfers optional interrupt-IN
487 CCID_CONF_STR // associated string descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100488 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200489 {
490 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
491 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
492 CCID1_10, // bcdCCID: CCID version
493 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
Harald Welte8a5b5802015-11-07 18:52:52 +0100494 VOLTS_3_0, // bVoltageSupport
Christina Quaste01fb9a2015-04-04 19:56:23 +0200495 (1 << PROTOCOL_TO), // dwProtocols
496 3580, // dwDefaultClock
497 3580, // dwMaxClock
498 0, // bNumClockSupported
499 9600, // dwDataRate : 9600 bauds
500 9600, // dwMaxDataRate : 9600 bauds
501 0, // bNumDataRatesSupported
502 0xfe, // dwMaxIFSD
503 0, // dwSynchProtocols
504 0, // dwMechanical
505 //0x00010042, // dwFeatures: Short APDU level exchanges
506 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
507 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
508 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
509 0xFF, // bClassGetResponse: Echoes the class of the APDU
510 0xFF, // bClassEnvelope: Echoes the class of the APDU
511 0, // wLcdLayout: no LCD
512 0, // bPINSupport: No PIN
513 1 // bMaxCCIDBusySlot
514 },
515 // Bulk-OUT endpoint descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100516 {
517 sizeof(USBEndpointDescriptor),
518 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200519 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
Christina Quastf5549502015-02-24 14:27:08 +0100520 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200521 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
Christina Quastf5549502015-02-24 14:27:08 +0100522 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200523 0x00 // Does not apply to Bulk endpoints
Christina Quastf5549502015-02-24 14:27:08 +0100524 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200525 // Bulk-IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100526 {
527 sizeof(USBEndpointDescriptor),
528 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200529 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
Christina Quast01bbdc32015-02-24 17:38:45 +0100530 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200531 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100532 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200533 0x00 // Does not apply to Bulk endpoints
Christina Quast968b9742015-02-25 14:10:12 +0100534 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200535 // Notification endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100536 {
537 sizeof(USBEndpointDescriptor),
538 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200539 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
Christina Quastda373fd2015-02-27 15:25:22 +0100540 USBEndpointDescriptor_INTERRUPT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200541 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
Christina Quastda373fd2015-02-27 15:25:22 +0100542 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
543 0x10
544 },
Christina Quast01bbdc32015-02-24 17:38:45 +0100545 /* Communication class interface standard descriptor */
546 {
547 sizeof(USBInterfaceDescriptor),
548 USBGenericDescriptor_INTERFACE,
549 1, /* This is interface #1 */
550 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200551 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100552 0xff,
553 0,
Christina Quastbfd73542015-05-08 16:00:51 +0200554 0,
555 PHONE_CONF_STR, /* string descriptor for this interface */
Christina Quast01bbdc32015-02-24 17:38:45 +0100556 },
557 /* Bulk-OUT endpoint standard descriptor */
558 {
559 sizeof(USBEndpointDescriptor),
560 USBGenericDescriptor_ENDPOINT,
561 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200562 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100563 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200564 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100565 USBEndpointDescriptor_MAXBULKSIZE_FS),
566 0 /* Must be 0 for full-speed bulk endpoints */
567 },
568 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100569 {
570 sizeof(USBEndpointDescriptor),
571 USBGenericDescriptor_ENDPOINT,
572 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200573 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100574 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200575 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100576 USBEndpointDescriptor_MAXBULKSIZE_FS),
577 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100578 },
579 /* Notification endpoint descriptor */
580 {
581 sizeof(USBEndpointDescriptor),
582 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200583 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100584 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200585 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100586 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
587 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100588 }
589};
590
Christina Quastf5549502015-02-24 14:27:08 +0100591/** Standard USB device descriptor for the CDC serial driver */
592const USBDeviceDescriptor deviceDescriptor = {
593
594 sizeof(USBDeviceDescriptor),
595 USBGenericDescriptor_DEVICE,
596 USBDeviceDescriptor_USB2_00,
597 0xff,
598// CDCDeviceDescriptor_CLASS,
599 0xff,
600// CDCDeviceDescriptor_SUBCLASS,
601 0xff,
602// CDCDeviceDescriptor_PROTOCOL,
603 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
Christina Quast37350392015-05-08 16:47:26 +0200604 SIMTRACE_VENDOR_ID,
Christina Quastf5549502015-02-24 14:27:08 +0100605 SIMTRACE_PRODUCT_ID,
606 1, /* Release number */
Harald Welte6d44c1f2015-11-07 19:01:30 +0100607 MANUF_STR, /* Indesx of manufacturer string descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100608 PRODUCT_STRING, /* Index of product string descriptor */
609 0, /* No string descriptor for serial number */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100610 4 /* Device has 4 possible configurations */
Christina Quastf5549502015-02-24 14:27:08 +0100611};
612
Christina Quast968b9742015-02-25 14:10:12 +0100613const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
Christina Quast0becc0c2015-04-04 13:04:06 +0200614 &configurationDescriptorSniffer.configuration,
615 &configurationDescriptorCCID.configuration,
616 &configurationDescriptorPhone.configuration,
617 &configurationDescriptorMITM.configuration,
Christina Quast01bbdc32015-02-24 17:38:45 +0100618};
619
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200620/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100621static const USBDDriverDescriptors driverDescriptors = {
Christina Quastf5549502015-02-24 14:27:08 +0100622 &deviceDescriptor,
Christina Quastb06fc382015-04-06 23:30:21 +0200623 (const USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100624 0, /* No full-speed device qualifier descriptor */
625 0, /* No full-speed other speed configuration */
626 0, /* No high-speed device descriptor */
627 0, /* No high-speed configuration descriptor */
628 0, /* No high-speed device qualifier descriptor */
629 0, /* No high-speed other speed configuration descriptor */
630 stringDescriptors,
Christina Quast54d0c1f2015-02-25 16:04:25 +0100631 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100632};
633
Christina Quast0ae03142015-02-25 18:43:46 +0100634
635/*----------------------------------------------------------------------------
Christina Quast968b9742015-02-25 14:10:12 +0100636 * Functions
637 *----------------------------------------------------------------------------*/
638
639/**
640 * \brief Configure 48MHz Clock for USB
641 */
642static void _ConfigureUsbClock(void)
643{
644 /* Enable PLLB for USB */
Christina Quast968b9742015-02-25 14:10:12 +0100645 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
646 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
647 | CKGR_PLLBR_PLLBCOUNT_Msk;
648 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
649 /* USB Clock uses PLLB */
650 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
651 | PMC_USB_USBS; /* PLLB */
652}
653
654
655void SIMtrace_USB_Initialize( void )
656{
657 _ConfigureUsbClock();
658 // Get std USB driver
659 USBDDriver *pUsbd = USBD_GetDriver();
660
661 TRACE_DEBUG(".");
662
663 // Initialize standard USB driver
664 USBDDriver_Initialize(pUsbd,
665 &driverDescriptors,
Christina Quast968b9742015-02-25 14:10:12 +0100666 0); // Multiple interface settings not supported
Christina Quast968b9742015-02-25 14:10:12 +0100667 USBD_Init();
Christina Quast968b9742015-02-25 14:10:12 +0100668 USBD_Connect();
669
670 NVIC_EnableIRQ( UDP_IRQn );
671}