blob: f91c80c5c37a6f23d357c6f62bea65959194d8df [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;
414 USBEndpointDescriptor simcard_dataOut;
415 USBEndpointDescriptor simcard_dataIn;
416 USBEndpointDescriptor simcard_interruptIn;
417 USBInterfaceDescriptor phone;
418 USBEndpointDescriptor phone_dataOut;
419 USBEndpointDescriptor phone_dataIn;
420 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100421
Christina Quast01bbdc32015-02-24 17:38:45 +0100422} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100423
Christina Quast968b9742015-02-25 14:10:12 +0100424const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
Christina Quastf5549502015-02-24 14:27:08 +0100425 /* Standard configuration descriptor */
426 {
427 sizeof(USBConfigurationDescriptor),
428 USBGenericDescriptor_CONFIGURATION,
Christina Quast968b9742015-02-25 14:10:12 +0100429 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Christina Quastf5549502015-02-24 14:27:08 +0100430 2, /* There are two interfaces in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100431 CFG_NUM_MITM, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100432 MITM_CONF_STR, /* string descriptor for this configuration */
433 USBD_BMATTRIBUTES,
434 USBConfigurationDescriptor_POWER(100)
435 },
436 /* Communication class interface standard descriptor */
437 {
438 sizeof(USBInterfaceDescriptor),
439 USBGenericDescriptor_INTERFACE,
440 0, /* This is interface #0 */
441 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200442 3, /* Number of endpoints */
Christina Quastf5549502015-02-24 14:27:08 +0100443 //CDCCommunicationInterfaceDescriptor_CLASS,
444 0xff,
445// CDCCommunicationInterfaceDescriptor_ABSTRACTCONTROLMODEL,
446 0,
447// CDCCommunicationInterfaceDescriptor_NOPROTOCOL,
448 0,
449 MITM_CONF_STR /* string descriptor for this interface */
450 },
451 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100452 {
453 sizeof(USBEndpointDescriptor),
454 USBGenericDescriptor_ENDPOINT,
455 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
456 DATAOUT),
457 USBEndpointDescriptor_BULK,
458 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
459 USBEndpointDescriptor_MAXBULKSIZE_FS),
460 0 /* Must be 0 for full-speed bulk endpoints */
461 },
462 /* Bulk-IN endpoint descriptor */
Christina Quast01bbdc32015-02-24 17:38:45 +0100463 {
464 sizeof(USBEndpointDescriptor),
465 USBGenericDescriptor_ENDPOINT,
466 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
467 DATAIN),
468 USBEndpointDescriptor_BULK,
469 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
470 USBEndpointDescriptor_MAXBULKSIZE_FS),
471 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quast968b9742015-02-25 14:10:12 +0100472 },
Christina Quastda373fd2015-02-27 15:25:22 +0100473 /* Notification endpoint descriptor */
474 {
475 sizeof(USBEndpointDescriptor),
476 USBGenericDescriptor_ENDPOINT,
477 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, INT ),
478 USBEndpointDescriptor_INTERRUPT,
479 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(INT),
480 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
481 0x10
482 },
Christina Quast01bbdc32015-02-24 17:38:45 +0100483 /* Communication class interface standard descriptor */
484 {
485 sizeof(USBInterfaceDescriptor),
486 USBGenericDescriptor_INTERFACE,
487 1, /* This is interface #1 */
488 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200489 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100490 0xff,
491 0,
492 0,
493 0, /* FIXME: string descriptor for this interface */
494 },
495 /* Bulk-OUT endpoint standard descriptor */
496 {
497 sizeof(USBEndpointDescriptor),
498 USBGenericDescriptor_ENDPOINT,
499 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200500 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100501 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200502 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100503 USBEndpointDescriptor_MAXBULKSIZE_FS),
504 0 /* Must be 0 for full-speed bulk endpoints */
505 },
506 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100507 {
508 sizeof(USBEndpointDescriptor),
509 USBGenericDescriptor_ENDPOINT,
510 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200511 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100512 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200513 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100514 USBEndpointDescriptor_MAXBULKSIZE_FS),
515 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100516 },
517 /* Notification endpoint descriptor */
518 {
519 sizeof(USBEndpointDescriptor),
520 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200521 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100522 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200523 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100524 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
525 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100526 }
527};
528
529
530/** Standard USB device descriptor for the CDC serial driver */
531const USBDeviceDescriptor deviceDescriptor = {
532
533 sizeof(USBDeviceDescriptor),
534 USBGenericDescriptor_DEVICE,
535 USBDeviceDescriptor_USB2_00,
536 0xff,
537// CDCDeviceDescriptor_CLASS,
538 0xff,
539// CDCDeviceDescriptor_SUBCLASS,
540 0xff,
541// CDCDeviceDescriptor_PROTOCOL,
542 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
543 ATMEL_VENDOR_ID,
544 SIMTRACE_PRODUCT_ID,
545 1, /* Release number */
546 0, /* No string descriptor for manufacturer */
547 PRODUCT_STRING, /* Index of product string descriptor */
548 0, /* No string descriptor for serial number */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100549 4 /* Device has 4 possible configurations */
Christina Quastf5549502015-02-24 14:27:08 +0100550};
551
Christina Quast968b9742015-02-25 14:10:12 +0100552const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
Christina Quast0becc0c2015-04-04 13:04:06 +0200553 &configurationDescriptorSniffer.configuration,
554 &configurationDescriptorCCID.configuration,
555 &configurationDescriptorPhone.configuration,
556 &configurationDescriptorMITM.configuration,
Christina Quast01bbdc32015-02-24 17:38:45 +0100557};
558
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100559USBConfigurationDescriptor *getConfigDesc(uint8_t idx) {
560 return configurationDescriptorsArr[idx];
561}
Christina Quast01bbdc32015-02-24 17:38:45 +0100562
Christina Quastf5549502015-02-24 14:27:08 +0100563/* AT91SAM3S does only support full speed, but not high speed USB */
564const USBDDriverDescriptors driverDescriptors = {
565 &deviceDescriptor,
Christina Quast01bbdc32015-02-24 17:38:45 +0100566 (USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100567 0, /* No full-speed device qualifier descriptor */
568 0, /* No full-speed other speed configuration */
569 0, /* No high-speed device descriptor */
570 0, /* No high-speed configuration descriptor */
571 0, /* No high-speed device qualifier descriptor */
572 0, /* No high-speed other speed configuration descriptor */
573 stringDescriptors,
Christina Quast54d0c1f2015-02-25 16:04:25 +0100574 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100575};
576
Christina Quast0ae03142015-02-25 18:43:46 +0100577
578/*----------------------------------------------------------------------------
579 * Callbacks
580 *----------------------------------------------------------------------------*/
581extern uint8_t conf_changed;
582extern uint8_t simtrace_config;
583
584void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
585{
586 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
587 conf_changed =1;
588 simtrace_config = cfgnum;
589}
590
591
Christina Quast968b9742015-02-25 14:10:12 +0100592/*----------------------------------------------------------------------------
593 * Functions
594 *----------------------------------------------------------------------------*/
595
596/**
597 * \brief Configure 48MHz Clock for USB
598 */
599static void _ConfigureUsbClock(void)
600{
601 /* Enable PLLB for USB */
602// FIXME: are these the dividers I actually need?
603// FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways?
604 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
605 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
606 | CKGR_PLLBR_PLLBCOUNT_Msk;
607 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
608 /* USB Clock uses PLLB */
609 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
610 | PMC_USB_USBS; /* PLLB */
611}
612
613
614void SIMtrace_USB_Initialize( void )
615{
616 _ConfigureUsbClock();
617 // Get std USB driver
618 USBDDriver *pUsbd = USBD_GetDriver();
619
620 TRACE_DEBUG(".");
621
622 // Initialize standard USB driver
623 USBDDriver_Initialize(pUsbd,
624 &driverDescriptors,
625// FIXME: 2 interface settings supported in MITM mode
626 0); // Multiple interface settings not supported
627
628 USBD_Init();
629
630 USBD_Connect();
631
632 NVIC_EnableIRQ( UDP_IRQn );
633}