blob: 460f3160260b30106bee80ed43a9117b042c6a75 [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
Christina Quastf5549502015-02-24 14:27:08 +010050const unsigned char productStringDescriptor[] = {
51
52 USBStringDescriptor_LENGTH(8),
53 USBGenericDescriptor_STRING,
54 USBStringDescriptor_UNICODE('S'),
55 USBStringDescriptor_UNICODE('I'),
56 USBStringDescriptor_UNICODE('M'),
57 USBStringDescriptor_UNICODE('t'),
58 USBStringDescriptor_UNICODE('r'),
59 USBStringDescriptor_UNICODE('a'),
60 USBStringDescriptor_UNICODE('c'),
61 USBStringDescriptor_UNICODE('e'),
62};
63
64const unsigned char snifferConfigStringDescriptor[] = {
65
66 USBStringDescriptor_LENGTH(15),
67 USBGenericDescriptor_STRING,
68 USBStringDescriptor_UNICODE('S'),
69 USBStringDescriptor_UNICODE('I'),
70 USBStringDescriptor_UNICODE('M'),
71 USBStringDescriptor_UNICODE('t'),
72 USBStringDescriptor_UNICODE('r'),
73 USBStringDescriptor_UNICODE('a'),
74 USBStringDescriptor_UNICODE('c'),
75 USBStringDescriptor_UNICODE('e'),
76 USBStringDescriptor_UNICODE('S'),
77 USBStringDescriptor_UNICODE('n'),
78 USBStringDescriptor_UNICODE('i'),
79 USBStringDescriptor_UNICODE('f'),
80 USBStringDescriptor_UNICODE('f'),
81 USBStringDescriptor_UNICODE('e'),
82 USBStringDescriptor_UNICODE('r'),
83};
84
85const unsigned char CCIDConfigStringDescriptor[] = {
86
87 USBStringDescriptor_LENGTH(12),
88 USBGenericDescriptor_STRING,
89 USBStringDescriptor_UNICODE('S'),
90 USBStringDescriptor_UNICODE('I'),
91 USBStringDescriptor_UNICODE('M'),
92 USBStringDescriptor_UNICODE('t'),
93 USBStringDescriptor_UNICODE('r'),
94 USBStringDescriptor_UNICODE('a'),
95 USBStringDescriptor_UNICODE('c'),
96 USBStringDescriptor_UNICODE('e'),
97 USBStringDescriptor_UNICODE('C'),
98 USBStringDescriptor_UNICODE('C'),
99 USBStringDescriptor_UNICODE('I'),
100 USBStringDescriptor_UNICODE('D'),
101};
102
103const unsigned char phoneConfigStringDescriptor[] = {
104
105 USBStringDescriptor_LENGTH(13),
106 USBGenericDescriptor_STRING,
107 USBStringDescriptor_UNICODE('S'),
108 USBStringDescriptor_UNICODE('I'),
109 USBStringDescriptor_UNICODE('M'),
110 USBStringDescriptor_UNICODE('t'),
111 USBStringDescriptor_UNICODE('r'),
112 USBStringDescriptor_UNICODE('a'),
113 USBStringDescriptor_UNICODE('c'),
114 USBStringDescriptor_UNICODE('e'),
115 USBStringDescriptor_UNICODE('P'),
116 USBStringDescriptor_UNICODE('h'),
117 USBStringDescriptor_UNICODE('o'),
118 USBStringDescriptor_UNICODE('n'),
119 USBStringDescriptor_UNICODE('e'),
120};
121
122
123const unsigned char MITMConfigStringDescriptor[] = {
124
125 USBStringDescriptor_LENGTH(12),
126 USBGenericDescriptor_STRING,
127 USBStringDescriptor_UNICODE('S'),
128 USBStringDescriptor_UNICODE('I'),
129 USBStringDescriptor_UNICODE('M'),
130 USBStringDescriptor_UNICODE('t'),
131 USBStringDescriptor_UNICODE('r'),
132 USBStringDescriptor_UNICODE('a'),
133 USBStringDescriptor_UNICODE('c'),
134 USBStringDescriptor_UNICODE('e'),
135 USBStringDescriptor_UNICODE('M'),
136 USBStringDescriptor_UNICODE('I'),
137 USBStringDescriptor_UNICODE('T'),
138 USBStringDescriptor_UNICODE('M'),
139};
140
141enum strDescNum {
Christina Quast54d0c1f2015-02-25 16:04:25 +0100142 PRODUCT_STRING = 1, SNIFFER_CONF_STR, CCID_CONF_STR, PHONE_CONF_STR, MITM_CONF_STR, STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +0100143};
144
145/** List of string descriptors used by the device */
146const unsigned char *stringDescriptors[] = {
147/* FIXME: Is it true that I can't use the string desc #0,
148 * because 0 also stands for "no string desc"?
149 * on the other hand, dmesg output:
150 * "string descriptor 0 malformed (err = -61), defaulting to 0x0409" */
Christina Quast6032e792015-02-27 15:22:25 +0100151 langDesc,
Christina Quastf5549502015-02-24 14:27:08 +0100152 productStringDescriptor,
153 snifferConfigStringDescriptor,
Christina Quast968b9742015-02-25 14:10:12 +0100154 CCIDConfigStringDescriptor,
Christina Quastf5549502015-02-24 14:27:08 +0100155 phoneConfigStringDescriptor,
156 MITMConfigStringDescriptor
157};
158
Christina Quastf5549502015-02-24 14:27:08 +0100159/*------------------------------------------------------------------------------
160 * USB Device descriptors
161 *------------------------------------------------------------------------------*/
162
163typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
164
165 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100166 USBConfigurationDescriptor configuration;
167 USBInterfaceDescriptor sniffer;
168 USBEndpointDescriptor sniffer_dataOut;
169 USBEndpointDescriptor sniffer_dataIn;
170 USBEndpointDescriptor sniffer_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100171
Christina Quast01bbdc32015-02-24 17:38:45 +0100172} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100173
174const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffer = {
175 /* Standard configuration descriptor */
176 {
177 sizeof(USBConfigurationDescriptor),
178 USBGenericDescriptor_CONFIGURATION,
179 sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
180 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100181 CFG_NUM_SNIFF, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100182 SNIFFER_CONF_STR, /* string descriptor for this configuration */
183 USBD_BMATTRIBUTES,
184 USBConfigurationDescriptor_POWER(100)
185 },
186 /* Communication class interface standard descriptor */
187 {
188 sizeof(USBInterfaceDescriptor),
189 USBGenericDescriptor_INTERFACE,
190 0, /* This is interface #0 */
191 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200192 3, /* Number of endpoints */
Christina Quastf5549502015-02-24 14:27:08 +0100193 0xff, /* Descriptor Class: Vendor specific */
194 0, /* No subclass */
195 0, /* No l */
196 SNIFFER_CONF_STR /* Third in string descriptor for this interface */
197 },
198 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100199 {
200 sizeof(USBEndpointDescriptor),
201 USBGenericDescriptor_ENDPOINT,
202 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
203 DATAOUT),
204 USBEndpointDescriptor_BULK,
205 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
206 USBEndpointDescriptor_MAXBULKSIZE_FS),
207 0 /* Must be 0 for full-speed bulk endpoints */
208 },
209 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100210 {
211 sizeof(USBEndpointDescriptor),
212 USBGenericDescriptor_ENDPOINT,
213 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
214 DATAIN),
215 USBEndpointDescriptor_BULK,
216 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
217 USBEndpointDescriptor_MAXBULKSIZE_FS),
218 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100219 },
220 // Notification endpoint descriptor
221 {
222 sizeof(USBEndpointDescriptor),
223 USBGenericDescriptor_ENDPOINT,
224 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, INT ),
225 USBEndpointDescriptor_INTERRUPT,
226 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(INT),
227 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
228 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100229 }
230};
231
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100232/*
233/// CCIDDriverConfiguration Descriptors
234/// List of descriptors that make up the configuration descriptors of a
235/// device using the CCID driver.
Christina Quast01bbdc32015-02-24 17:38:45 +0100236typedef struct {
237
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100238 /// Configuration descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100239 USBConfigurationDescriptor configuration;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100240 /// Interface descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100241 USBInterfaceDescriptor interface;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100242 /// CCID descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100243 CCIDDescriptor ccid;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100244 /// Bulk OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100245 USBEndpointDescriptor bulkOut;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100246 /// Bulk IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100247 USBEndpointDescriptor bulkIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100248 /// Interrupt OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100249 USBEndpointDescriptor interruptIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100250} __attribute__ ((packed)) CCIDDriverConfigurationDescriptors;
251*/
Christina Quast01bbdc32015-02-24 17:38:45 +0100252
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100253const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
254
255 // Standard USB configuration descriptor
256 {
257 sizeof(USBConfigurationDescriptor),
258 USBGenericDescriptor_CONFIGURATION,
259 sizeof(CCIDDriverConfigurationDescriptors),
260 1, // One interface in this configuration
261 CFG_NUM_CCID, // This is configuration #1
262 CCID_CONF_STR, // associated string descriptor
263 BOARD_USB_BMATTRIBUTES,
264 USBConfigurationDescriptor_POWER(100)
265 },
266 // CCID interface descriptor
267 // Table 4.3-1 Interface Descriptor
268 // Interface descriptor
269 {
270 sizeof(USBInterfaceDescriptor),
271 USBGenericDescriptor_INTERFACE,
272 0, // Interface 0
273 0, // No alternate settings
274 3, // uses bulk-IN, bulk-OUT and interrupt IN
275 SMART_CARD_DEVICE_CLASS,
276 0, // Subclass code
277 0, // bulk transfers optional interrupt-IN
278 CCID_CONF_STR // associated string descriptor
279 },
280 {
281 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
282 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
283 CCID1_10, // bcdCCID: CCID version
284 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
285 VOLTS_5_0, // bVoltageSupport
286 (1 << PROTOCOL_TO), // dwProtocols
287 3580, // dwDefaultClock
288 3580, // dwMaxClock
289 0, // bNumClockSupported
290 9600, // dwDataRate : 9600 bauds
291 9600, // dwMaxDataRate : 9600 bauds
292 0, // bNumDataRatesSupported
293 0xfe, // dwMaxIFSD
294 0, // dwSynchProtocols
295 0, // dwMechanical
296 //0x00010042, // dwFeatures: Short APDU level exchanges
297 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
298 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
299 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
300 0xFF, // bClassGetResponse: Echoes the class of the APDU
301 0xFF, // bClassEnvelope: Echoes the class of the APDU
302 0, // wLcdLayout: no LCD
303 0, // bPINSupport: No PIN
304 1 // bMaxCCIDBusySlot
305 },
306 // Bulk-OUT endpoint descriptor
307 {
308 sizeof(USBEndpointDescriptor),
309 USBGenericDescriptor_ENDPOINT,
310 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
311 USBEndpointDescriptor_BULK,
312 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
313 USBEndpointDescriptor_MAXBULKSIZE_FS),
314 0x00 // Does not apply to Bulk endpoints
315 },
316 // Bulk-IN endpoint descriptor
317 {
318 sizeof(USBEndpointDescriptor),
319 USBGenericDescriptor_ENDPOINT,
320 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
321 USBEndpointDescriptor_BULK,
322 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
323 USBEndpointDescriptor_MAXBULKSIZE_FS),
324 0x00 // Does not apply to Bulk endpoints
325 },
326 // Notification endpoint descriptor
327 {
328 sizeof(USBEndpointDescriptor),
329 USBGenericDescriptor_ENDPOINT,
330 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
331 USBEndpointDescriptor_INTERRUPT,
332 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
333 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
334 0x10
335 },
336};
337
Christina Quast01bbdc32015-02-24 17:38:45 +0100338
339/* SIM card emulator */
340typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
341
342 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100343 USBConfigurationDescriptor configuration;
344 USBInterfaceDescriptor phone;
345 USBEndpointDescriptor phone_dataOut;
346 USBEndpointDescriptor phone_dataIn;
347 USBEndpointDescriptor phone_interruptIn;
Christina Quast01bbdc32015-02-24 17:38:45 +0100348} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
349
350const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = {
351 /* Standard configuration descriptor */
352 {
353 sizeof(USBConfigurationDescriptor),
354 USBGenericDescriptor_CONFIGURATION,
Christina Quast6d9dcfc2015-04-03 22:26:43 +0200355 sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Christina Quast01bbdc32015-02-24 17:38:45 +0100356 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100357 CFG_NUM_PHONE, /* configuration number */
Christina Quast01bbdc32015-02-24 17:38:45 +0100358 PHONE_CONF_STR, /* string descriptor for this configuration */
359 USBD_BMATTRIBUTES,
360 USBConfigurationDescriptor_POWER(100)
361 },
362 /* Communication class interface standard descriptor */
363 {
364 sizeof(USBInterfaceDescriptor),
365 USBGenericDescriptor_INTERFACE,
366 0, /* This is interface #0 */
367 0, /* This is alternate setting #0 for this interface */
Christina Quastb65b8812015-04-04 10:51:37 +0200368 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100369 0xff, /* Descriptor Class: Vendor specific */
370 0, /* No subclass */
371 0, /* No l */
372 PHONE_CONF_STR /* Third in string descriptor for this interface */
373 },
374 /* Bulk-OUT endpoint standard descriptor */
375 {
376 sizeof(USBEndpointDescriptor),
377 USBGenericDescriptor_ENDPOINT,
378 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200379 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100380 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200381 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100382 USBEndpointDescriptor_MAXBULKSIZE_FS),
383 0 /* Must be 0 for full-speed bulk endpoints */
384 },
385 /* Bulk-IN endpoint descriptor */
386 {
387 sizeof(USBEndpointDescriptor),
388 USBGenericDescriptor_ENDPOINT,
389 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200390 PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100391 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200392 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100393 USBEndpointDescriptor_MAXBULKSIZE_FS),
394 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100395 },
396 /* Notification endpoint descriptor */
397 {
398 sizeof(USBEndpointDescriptor),
399 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200400 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100401 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200402 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100403 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
404 0x10
Christina Quast01bbdc32015-02-24 17:38:45 +0100405 }
406};
407
Christina Quastf5549502015-02-24 14:27:08 +0100408
409typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
410
411 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100412 USBConfigurationDescriptor configuration;
413 USBInterfaceDescriptor simcard;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200414 /// CCID descriptor
415 CCIDDescriptor ccid;
416 /// Bulk OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100417 USBEndpointDescriptor simcard_dataOut;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200418 /// Bulk IN endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100419 USBEndpointDescriptor simcard_dataIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200420 /// Interrupt OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100421 USBEndpointDescriptor simcard_interruptIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200422
Christina Quastda373fd2015-02-27 15:25:22 +0100423 USBInterfaceDescriptor phone;
424 USBEndpointDescriptor phone_dataOut;
425 USBEndpointDescriptor phone_dataIn;
426 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100427
Christina Quast01bbdc32015-02-24 17:38:45 +0100428} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100429
Christina Quast968b9742015-02-25 14:10:12 +0100430const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
Christina Quastf5549502015-02-24 14:27:08 +0100431 /* Standard configuration descriptor */
432 {
433 sizeof(USBConfigurationDescriptor),
434 USBGenericDescriptor_CONFIGURATION,
Christina Quast968b9742015-02-25 14:10:12 +0100435 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Christina Quastf5549502015-02-24 14:27:08 +0100436 2, /* There are two interfaces in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100437 CFG_NUM_MITM, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100438 MITM_CONF_STR, /* string descriptor for this configuration */
439 USBD_BMATTRIBUTES,
440 USBConfigurationDescriptor_POWER(100)
441 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200442 // CCID interface descriptor
443 // Table 4.3-1 Interface Descriptor
444 // Interface descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100445 {
446 sizeof(USBInterfaceDescriptor),
447 USBGenericDescriptor_INTERFACE,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200448 0, // Interface 0
449 0, // No alternate settings
450 3, // uses bulk-IN, bulk-OUT and interrupt IN
451 SMART_CARD_DEVICE_CLASS,
452 0, // Subclass code
453 0, // bulk transfers optional interrupt-IN
454 CCID_CONF_STR // associated string descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100455 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200456 {
457 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
458 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
459 CCID1_10, // bcdCCID: CCID version
460 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
461 VOLTS_5_0, // bVoltageSupport
462 (1 << PROTOCOL_TO), // dwProtocols
463 3580, // dwDefaultClock
464 3580, // dwMaxClock
465 0, // bNumClockSupported
466 9600, // dwDataRate : 9600 bauds
467 9600, // dwMaxDataRate : 9600 bauds
468 0, // bNumDataRatesSupported
469 0xfe, // dwMaxIFSD
470 0, // dwSynchProtocols
471 0, // dwMechanical
472 //0x00010042, // dwFeatures: Short APDU level exchanges
473 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
474 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
475 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
476 0xFF, // bClassGetResponse: Echoes the class of the APDU
477 0xFF, // bClassEnvelope: Echoes the class of the APDU
478 0, // wLcdLayout: no LCD
479 0, // bPINSupport: No PIN
480 1 // bMaxCCIDBusySlot
481 },
482 // Bulk-OUT endpoint descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100483 {
484 sizeof(USBEndpointDescriptor),
485 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200486 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
Christina Quastf5549502015-02-24 14:27:08 +0100487 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200488 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
Christina Quastf5549502015-02-24 14:27:08 +0100489 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200490 0x00 // Does not apply to Bulk endpoints
Christina Quastf5549502015-02-24 14:27:08 +0100491 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200492 // Bulk-IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100493 {
494 sizeof(USBEndpointDescriptor),
495 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200496 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
Christina Quast01bbdc32015-02-24 17:38:45 +0100497 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200498 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100499 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200500 0x00 // Does not apply to Bulk endpoints
Christina Quast968b9742015-02-25 14:10:12 +0100501 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200502 // Notification endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100503 {
504 sizeof(USBEndpointDescriptor),
505 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200506 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
Christina Quastda373fd2015-02-27 15:25:22 +0100507 USBEndpointDescriptor_INTERRUPT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200508 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
Christina Quastda373fd2015-02-27 15:25:22 +0100509 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
510 0x10
511 },
Christina Quast01bbdc32015-02-24 17:38:45 +0100512 /* Communication class interface standard descriptor */
513 {
514 sizeof(USBInterfaceDescriptor),
515 USBGenericDescriptor_INTERFACE,
516 1, /* This is interface #1 */
517 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200518 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100519 0xff,
520 0,
521 0,
522 0, /* FIXME: string descriptor for this interface */
523 },
524 /* Bulk-OUT endpoint standard descriptor */
525 {
526 sizeof(USBEndpointDescriptor),
527 USBGenericDescriptor_ENDPOINT,
528 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200529 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100530 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200531 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100532 USBEndpointDescriptor_MAXBULKSIZE_FS),
533 0 /* Must be 0 for full-speed bulk endpoints */
534 },
535 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100536 {
537 sizeof(USBEndpointDescriptor),
538 USBGenericDescriptor_ENDPOINT,
539 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200540 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100541 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200542 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100543 USBEndpointDescriptor_MAXBULKSIZE_FS),
544 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100545 },
546 /* Notification endpoint descriptor */
547 {
548 sizeof(USBEndpointDescriptor),
549 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200550 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100551 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200552 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100553 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
554 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100555 }
556};
557
Christina Quastf5549502015-02-24 14:27:08 +0100558/** Standard USB device descriptor for the CDC serial driver */
559const USBDeviceDescriptor deviceDescriptor = {
560
561 sizeof(USBDeviceDescriptor),
562 USBGenericDescriptor_DEVICE,
563 USBDeviceDescriptor_USB2_00,
564 0xff,
565// CDCDeviceDescriptor_CLASS,
566 0xff,
567// CDCDeviceDescriptor_SUBCLASS,
568 0xff,
569// CDCDeviceDescriptor_PROTOCOL,
570 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
571 ATMEL_VENDOR_ID,
572 SIMTRACE_PRODUCT_ID,
573 1, /* Release number */
574 0, /* No string descriptor for manufacturer */
575 PRODUCT_STRING, /* Index of product string descriptor */
576 0, /* No string descriptor for serial number */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100577 4 /* Device has 4 possible configurations */
Christina Quastf5549502015-02-24 14:27:08 +0100578};
579
Christina Quast968b9742015-02-25 14:10:12 +0100580const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
Christina Quast0becc0c2015-04-04 13:04:06 +0200581 &configurationDescriptorSniffer.configuration,
582 &configurationDescriptorCCID.configuration,
583 &configurationDescriptorPhone.configuration,
584 &configurationDescriptorMITM.configuration,
Christina Quast01bbdc32015-02-24 17:38:45 +0100585};
586
Christina Quastf5549502015-02-24 14:27:08 +0100587/* AT91SAM3S does only support full speed, but not high speed USB */
588const USBDDriverDescriptors driverDescriptors = {
589 &deviceDescriptor,
Christina Quastb06fc382015-04-06 23:30:21 +0200590 (const USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100591 0, /* No full-speed device qualifier descriptor */
592 0, /* No full-speed other speed configuration */
593 0, /* No high-speed device descriptor */
594 0, /* No high-speed configuration descriptor */
595 0, /* No high-speed device qualifier descriptor */
596 0, /* No high-speed other speed configuration descriptor */
597 stringDescriptors,
Christina Quast54d0c1f2015-02-25 16:04:25 +0100598 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100599};
600
Christina Quast0ae03142015-02-25 18:43:46 +0100601
602/*----------------------------------------------------------------------------
603 * Callbacks
604 *----------------------------------------------------------------------------*/
605extern uint8_t conf_changed;
606extern uint8_t simtrace_config;
607
608void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
609{
610 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
611 conf_changed =1;
612 simtrace_config = cfgnum;
613}
614
615
Christina Quast968b9742015-02-25 14:10:12 +0100616/*----------------------------------------------------------------------------
617 * Functions
618 *----------------------------------------------------------------------------*/
619
620/**
621 * \brief Configure 48MHz Clock for USB
622 */
623static void _ConfigureUsbClock(void)
624{
625 /* Enable PLLB for USB */
626// FIXME: are these the dividers I actually need?
627// FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways?
628 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
629 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
630 | CKGR_PLLBR_PLLBCOUNT_Msk;
631 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
632 /* USB Clock uses PLLB */
633 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
634 | PMC_USB_USBS; /* PLLB */
635}
636
637
638void SIMtrace_USB_Initialize( void )
639{
640 _ConfigureUsbClock();
641 // Get std USB driver
642 USBDDriver *pUsbd = USBD_GetDriver();
643
644 TRACE_DEBUG(".");
645
646 // Initialize standard USB driver
647 USBDDriver_Initialize(pUsbd,
648 &driverDescriptors,
649// FIXME: 2 interface settings supported in MITM mode
650 0); // Multiple interface settings not supported
651
652 USBD_Init();
653
654 USBD_Connect();
655
656 NVIC_EnableIRQ( UDP_IRQn );
657}