blob: 7cd4febd2c097574f22a4b87f7fcf65d75d97cb9 [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
Harald Weltebeb72932015-11-07 18:35:41 +010052 USBStringDescriptor_LENGTH(10),
Christina Quastf5549502015-02-24 14:27:08 +010053 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'),
Harald Weltebeb72932015-11-07 18:35:41 +010062 USBStringDescriptor_UNICODE(' '),
63 USBStringDescriptor_UNICODE('2'),
Christina Quastf5549502015-02-24 14:27:08 +010064};
65
66const unsigned char snifferConfigStringDescriptor[] = {
67
Harald Weltebeb72932015-11-07 18:35:41 +010068 USBStringDescriptor_LENGTH(16),
Christina Quastf5549502015-02-24 14:27:08 +010069 USBGenericDescriptor_STRING,
70 USBStringDescriptor_UNICODE('S'),
71 USBStringDescriptor_UNICODE('I'),
72 USBStringDescriptor_UNICODE('M'),
73 USBStringDescriptor_UNICODE('t'),
74 USBStringDescriptor_UNICODE('r'),
75 USBStringDescriptor_UNICODE('a'),
76 USBStringDescriptor_UNICODE('c'),
77 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +010078 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +010079 USBStringDescriptor_UNICODE('S'),
80 USBStringDescriptor_UNICODE('n'),
81 USBStringDescriptor_UNICODE('i'),
82 USBStringDescriptor_UNICODE('f'),
83 USBStringDescriptor_UNICODE('f'),
84 USBStringDescriptor_UNICODE('e'),
85 USBStringDescriptor_UNICODE('r'),
86};
87
88const unsigned char CCIDConfigStringDescriptor[] = {
89
Harald Weltebeb72932015-11-07 18:35:41 +010090 USBStringDescriptor_LENGTH(13),
Christina Quastf5549502015-02-24 14:27:08 +010091 USBGenericDescriptor_STRING,
92 USBStringDescriptor_UNICODE('S'),
93 USBStringDescriptor_UNICODE('I'),
94 USBStringDescriptor_UNICODE('M'),
95 USBStringDescriptor_UNICODE('t'),
96 USBStringDescriptor_UNICODE('r'),
97 USBStringDescriptor_UNICODE('a'),
98 USBStringDescriptor_UNICODE('c'),
99 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100100 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100101 USBStringDescriptor_UNICODE('C'),
102 USBStringDescriptor_UNICODE('C'),
103 USBStringDescriptor_UNICODE('I'),
104 USBStringDescriptor_UNICODE('D'),
105};
106
107const unsigned char phoneConfigStringDescriptor[] = {
108
Harald Weltebeb72932015-11-07 18:35:41 +0100109 USBStringDescriptor_LENGTH(14),
110 USBGenericDescriptor_STRING,
111 USBStringDescriptor_UNICODE('S'),
112 USBStringDescriptor_UNICODE('I'),
113 USBStringDescriptor_UNICODE('M'),
114 USBStringDescriptor_UNICODE('t'),
115 USBStringDescriptor_UNICODE('r'),
116 USBStringDescriptor_UNICODE('a'),
117 USBStringDescriptor_UNICODE('c'),
118 USBStringDescriptor_UNICODE('e'),
119 USBStringDescriptor_UNICODE(' '),
120 USBStringDescriptor_UNICODE('P'),
121 USBStringDescriptor_UNICODE('h'),
122 USBStringDescriptor_UNICODE('o'),
123 USBStringDescriptor_UNICODE('n'),
124 USBStringDescriptor_UNICODE('e'),
125};
126
127
128const unsigned char MITMConfigStringDescriptor[] = {
129
Christina Quastf5549502015-02-24 14:27:08 +0100130 USBStringDescriptor_LENGTH(13),
131 USBGenericDescriptor_STRING,
132 USBStringDescriptor_UNICODE('S'),
133 USBStringDescriptor_UNICODE('I'),
134 USBStringDescriptor_UNICODE('M'),
135 USBStringDescriptor_UNICODE('t'),
136 USBStringDescriptor_UNICODE('r'),
137 USBStringDescriptor_UNICODE('a'),
138 USBStringDescriptor_UNICODE('c'),
139 USBStringDescriptor_UNICODE('e'),
Harald Weltebeb72932015-11-07 18:35:41 +0100140 USBStringDescriptor_UNICODE(' '),
Christina Quastf5549502015-02-24 14:27:08 +0100141 USBStringDescriptor_UNICODE('M'),
142 USBStringDescriptor_UNICODE('I'),
143 USBStringDescriptor_UNICODE('T'),
144 USBStringDescriptor_UNICODE('M'),
145};
146
147enum strDescNum {
Christina Quast54d0c1f2015-02-25 16:04:25 +0100148 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 +0100149};
150
151/** List of string descriptors used by the device */
Harald Welteec4fe232015-11-07 18:41:25 +0100152static const unsigned char *stringDescriptors[] = {
Christina Quast6032e792015-02-27 15:22:25 +0100153 langDesc,
Christina Quastf5549502015-02-24 14:27:08 +0100154 productStringDescriptor,
155 snifferConfigStringDescriptor,
Christina Quast968b9742015-02-25 14:10:12 +0100156 CCIDConfigStringDescriptor,
Christina Quastf5549502015-02-24 14:27:08 +0100157 phoneConfigStringDescriptor,
158 MITMConfigStringDescriptor
159};
160
Christina Quastf5549502015-02-24 14:27:08 +0100161/*------------------------------------------------------------------------------
162 * USB Device descriptors
163 *------------------------------------------------------------------------------*/
164
165typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
166
167 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100168 USBConfigurationDescriptor configuration;
169 USBInterfaceDescriptor sniffer;
170 USBEndpointDescriptor sniffer_dataOut;
171 USBEndpointDescriptor sniffer_dataIn;
172 USBEndpointDescriptor sniffer_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100173
Christina Quast01bbdc32015-02-24 17:38:45 +0100174} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100175
Harald Welteec4fe232015-11-07 18:41:25 +0100176static const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffer = {
Christina Quastf5549502015-02-24 14:27:08 +0100177 /* Standard configuration descriptor */
178 {
179 sizeof(USBConfigurationDescriptor),
180 USBGenericDescriptor_CONFIGURATION,
181 sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
182 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100183 CFG_NUM_SNIFF, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100184 SNIFFER_CONF_STR, /* string descriptor for this configuration */
185 USBD_BMATTRIBUTES,
186 USBConfigurationDescriptor_POWER(100)
187 },
188 /* Communication class interface standard descriptor */
189 {
190 sizeof(USBInterfaceDescriptor),
191 USBGenericDescriptor_INTERFACE,
192 0, /* This is interface #0 */
193 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200194 3, /* Number of endpoints */
Christina Quastf5549502015-02-24 14:27:08 +0100195 0xff, /* Descriptor Class: Vendor specific */
196 0, /* No subclass */
197 0, /* No l */
198 SNIFFER_CONF_STR /* Third in string descriptor for this interface */
199 },
200 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100201 {
202 sizeof(USBEndpointDescriptor),
203 USBGenericDescriptor_ENDPOINT,
204 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200205 PHONE_DATAOUT),
Christina Quastf5549502015-02-24 14:27:08 +0100206 USBEndpointDescriptor_BULK,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200207 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quastf5549502015-02-24 14:27:08 +0100208 USBEndpointDescriptor_MAXBULKSIZE_FS),
209 0 /* Must be 0 for full-speed bulk endpoints */
210 },
211 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100212 {
213 sizeof(USBEndpointDescriptor),
214 USBGenericDescriptor_ENDPOINT,
215 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200216 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100217 USBEndpointDescriptor_BULK,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200218 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100219 USBEndpointDescriptor_MAXBULKSIZE_FS),
220 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100221 },
222 // Notification endpoint descriptor
223 {
224 sizeof(USBEndpointDescriptor),
225 USBGenericDescriptor_ENDPOINT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200226 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100227 USBEndpointDescriptor_INTERRUPT,
Christina Quast2b8a18b2015-04-12 09:31:36 +0200228 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100229 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
230 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100231 }
232};
233
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100234/*
235/// CCIDDriverConfiguration Descriptors
236/// List of descriptors that make up the configuration descriptors of a
237/// device using the CCID driver.
Christina Quast01bbdc32015-02-24 17:38:45 +0100238typedef struct {
239
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100240 /// Configuration descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100241 USBConfigurationDescriptor configuration;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100242 /// Interface descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100243 USBInterfaceDescriptor interface;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100244 /// CCID descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100245 CCIDDescriptor ccid;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100246 /// Bulk OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100247 USBEndpointDescriptor bulkOut;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100248 /// Bulk IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100249 USBEndpointDescriptor bulkIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100250 /// Interrupt OUT endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100251 USBEndpointDescriptor interruptIn;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100252} __attribute__ ((packed)) CCIDDriverConfigurationDescriptors;
253*/
Christina Quast01bbdc32015-02-24 17:38:45 +0100254
Harald Welteec4fe232015-11-07 18:41:25 +0100255static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100256
257 // Standard USB configuration descriptor
258 {
259 sizeof(USBConfigurationDescriptor),
260 USBGenericDescriptor_CONFIGURATION,
261 sizeof(CCIDDriverConfigurationDescriptors),
262 1, // One interface in this configuration
263 CFG_NUM_CCID, // This is configuration #1
264 CCID_CONF_STR, // associated string descriptor
265 BOARD_USB_BMATTRIBUTES,
266 USBConfigurationDescriptor_POWER(100)
267 },
268 // CCID interface descriptor
269 // Table 4.3-1 Interface Descriptor
270 // Interface descriptor
271 {
272 sizeof(USBInterfaceDescriptor),
273 USBGenericDescriptor_INTERFACE,
274 0, // Interface 0
275 0, // No alternate settings
276 3, // uses bulk-IN, bulk-OUT and interrupt IN
277 SMART_CARD_DEVICE_CLASS,
278 0, // Subclass code
279 0, // bulk transfers optional interrupt-IN
280 CCID_CONF_STR // associated string descriptor
281 },
282 {
283 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
284 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
285 CCID1_10, // bcdCCID: CCID version
286 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
287 VOLTS_5_0, // bVoltageSupport
288 (1 << PROTOCOL_TO), // dwProtocols
289 3580, // dwDefaultClock
290 3580, // dwMaxClock
291 0, // bNumClockSupported
292 9600, // dwDataRate : 9600 bauds
293 9600, // dwMaxDataRate : 9600 bauds
294 0, // bNumDataRatesSupported
295 0xfe, // dwMaxIFSD
296 0, // dwSynchProtocols
297 0, // dwMechanical
298 //0x00010042, // dwFeatures: Short APDU level exchanges
299 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
300 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
301 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
302 0xFF, // bClassGetResponse: Echoes the class of the APDU
303 0xFF, // bClassEnvelope: Echoes the class of the APDU
304 0, // wLcdLayout: no LCD
305 0, // bPINSupport: No PIN
306 1 // bMaxCCIDBusySlot
307 },
308 // Bulk-OUT endpoint descriptor
309 {
310 sizeof(USBEndpointDescriptor),
311 USBGenericDescriptor_ENDPOINT,
312 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
313 USBEndpointDescriptor_BULK,
314 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
315 USBEndpointDescriptor_MAXBULKSIZE_FS),
316 0x00 // Does not apply to Bulk endpoints
317 },
318 // Bulk-IN endpoint descriptor
319 {
320 sizeof(USBEndpointDescriptor),
321 USBGenericDescriptor_ENDPOINT,
322 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
323 USBEndpointDescriptor_BULK,
324 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
325 USBEndpointDescriptor_MAXBULKSIZE_FS),
326 0x00 // Does not apply to Bulk endpoints
327 },
328 // Notification endpoint descriptor
329 {
330 sizeof(USBEndpointDescriptor),
331 USBGenericDescriptor_ENDPOINT,
332 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
333 USBEndpointDescriptor_INTERRUPT,
334 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
335 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
336 0x10
337 },
338};
339
Christina Quast01bbdc32015-02-24 17:38:45 +0100340
341/* SIM card emulator */
342typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
343
344 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100345 USBConfigurationDescriptor configuration;
346 USBInterfaceDescriptor phone;
347 USBEndpointDescriptor phone_dataOut;
348 USBEndpointDescriptor phone_dataIn;
349 USBEndpointDescriptor phone_interruptIn;
Christina Quast01bbdc32015-02-24 17:38:45 +0100350} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
351
Harald Welteec4fe232015-11-07 18:41:25 +0100352static const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = {
Christina Quast01bbdc32015-02-24 17:38:45 +0100353 /* Standard configuration descriptor */
354 {
355 sizeof(USBConfigurationDescriptor),
356 USBGenericDescriptor_CONFIGURATION,
Christina Quast6d9dcfc2015-04-03 22:26:43 +0200357 sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Christina Quast01bbdc32015-02-24 17:38:45 +0100358 1, /* There is one interface in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100359 CFG_NUM_PHONE, /* configuration number */
Christina Quast01bbdc32015-02-24 17:38:45 +0100360 PHONE_CONF_STR, /* string descriptor for this configuration */
361 USBD_BMATTRIBUTES,
362 USBConfigurationDescriptor_POWER(100)
363 },
364 /* Communication class interface standard descriptor */
365 {
366 sizeof(USBInterfaceDescriptor),
367 USBGenericDescriptor_INTERFACE,
368 0, /* This is interface #0 */
369 0, /* This is alternate setting #0 for this interface */
Christina Quastb65b8812015-04-04 10:51:37 +0200370 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100371 0xff, /* Descriptor Class: Vendor specific */
372 0, /* No subclass */
373 0, /* No l */
374 PHONE_CONF_STR /* Third in string descriptor for this interface */
375 },
376 /* Bulk-OUT endpoint standard descriptor */
377 {
378 sizeof(USBEndpointDescriptor),
379 USBGenericDescriptor_ENDPOINT,
380 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200381 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100382 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200383 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100384 USBEndpointDescriptor_MAXBULKSIZE_FS),
385 0 /* Must be 0 for full-speed bulk endpoints */
386 },
387 /* Bulk-IN endpoint descriptor */
388 {
389 sizeof(USBEndpointDescriptor),
390 USBGenericDescriptor_ENDPOINT,
391 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200392 PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100393 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200394 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100395 USBEndpointDescriptor_MAXBULKSIZE_FS),
396 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100397 },
398 /* Notification endpoint descriptor */
399 {
400 sizeof(USBEndpointDescriptor),
401 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200402 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100403 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200404 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100405 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
406 0x10
Christina Quast01bbdc32015-02-24 17:38:45 +0100407 }
408};
409
Christina Quastf5549502015-02-24 14:27:08 +0100410
411typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
412
413 /** Standard configuration descriptor. */
Christina Quastda373fd2015-02-27 15:25:22 +0100414 USBConfigurationDescriptor configuration;
415 USBInterfaceDescriptor simcard;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200416 /// CCID descriptor
417 CCIDDescriptor ccid;
418 /// Bulk OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100419 USBEndpointDescriptor simcard_dataOut;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200420 /// Bulk IN endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100421 USBEndpointDescriptor simcard_dataIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200422 /// Interrupt OUT endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100423 USBEndpointDescriptor simcard_interruptIn;
Christina Quaste01fb9a2015-04-04 19:56:23 +0200424
Christina Quastda373fd2015-02-27 15:25:22 +0100425 USBInterfaceDescriptor phone;
426 USBEndpointDescriptor phone_dataOut;
427 USBEndpointDescriptor phone_dataIn;
428 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100429
Christina Quast01bbdc32015-02-24 17:38:45 +0100430} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100431
Harald Welteec4fe232015-11-07 18:41:25 +0100432static const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
Christina Quastf5549502015-02-24 14:27:08 +0100433 /* Standard configuration descriptor */
434 {
435 sizeof(USBConfigurationDescriptor),
436 USBGenericDescriptor_CONFIGURATION,
Christina Quast968b9742015-02-25 14:10:12 +0100437 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Christina Quastf5549502015-02-24 14:27:08 +0100438 2, /* There are two interfaces in this configuration */
Christina Quast10b2e5a2015-02-25 18:40:15 +0100439 CFG_NUM_MITM, /* configuration number */
Christina Quastf5549502015-02-24 14:27:08 +0100440 MITM_CONF_STR, /* string descriptor for this configuration */
441 USBD_BMATTRIBUTES,
442 USBConfigurationDescriptor_POWER(100)
443 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200444 // CCID interface descriptor
445 // Table 4.3-1 Interface Descriptor
446 // Interface descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100447 {
448 sizeof(USBInterfaceDescriptor),
449 USBGenericDescriptor_INTERFACE,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200450 0, // Interface 0
451 0, // No alternate settings
452 3, // uses bulk-IN, bulk-OUT and interrupt IN
453 SMART_CARD_DEVICE_CLASS,
454 0, // Subclass code
455 0, // bulk transfers optional interrupt-IN
456 CCID_CONF_STR // associated string descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100457 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200458 {
459 sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
460 CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
461 CCID1_10, // bcdCCID: CCID version
462 0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
463 VOLTS_5_0, // bVoltageSupport
464 (1 << PROTOCOL_TO), // dwProtocols
465 3580, // dwDefaultClock
466 3580, // dwMaxClock
467 0, // bNumClockSupported
468 9600, // dwDataRate : 9600 bauds
469 9600, // dwMaxDataRate : 9600 bauds
470 0, // bNumDataRatesSupported
471 0xfe, // dwMaxIFSD
472 0, // dwSynchProtocols
473 0, // dwMechanical
474 //0x00010042, // dwFeatures: Short APDU level exchanges
475 CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
476 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
477 0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
478 0xFF, // bClassGetResponse: Echoes the class of the APDU
479 0xFF, // bClassEnvelope: Echoes the class of the APDU
480 0, // wLcdLayout: no LCD
481 0, // bPINSupport: No PIN
482 1 // bMaxCCIDBusySlot
483 },
484 // Bulk-OUT endpoint descriptor
Christina Quastf5549502015-02-24 14:27:08 +0100485 {
486 sizeof(USBEndpointDescriptor),
487 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200488 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
Christina Quastf5549502015-02-24 14:27:08 +0100489 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200490 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
Christina Quastf5549502015-02-24 14:27:08 +0100491 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200492 0x00 // Does not apply to Bulk endpoints
Christina Quastf5549502015-02-24 14:27:08 +0100493 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200494 // Bulk-IN endpoint descriptor
Christina Quast01bbdc32015-02-24 17:38:45 +0100495 {
496 sizeof(USBEndpointDescriptor),
497 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200498 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
Christina Quast01bbdc32015-02-24 17:38:45 +0100499 USBEndpointDescriptor_BULK,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200500 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
Christina Quast01bbdc32015-02-24 17:38:45 +0100501 USBEndpointDescriptor_MAXBULKSIZE_FS),
Christina Quaste01fb9a2015-04-04 19:56:23 +0200502 0x00 // Does not apply to Bulk endpoints
Christina Quast968b9742015-02-25 14:10:12 +0100503 },
Christina Quaste01fb9a2015-04-04 19:56:23 +0200504 // Notification endpoint descriptor
Christina Quastda373fd2015-02-27 15:25:22 +0100505 {
506 sizeof(USBEndpointDescriptor),
507 USBGenericDescriptor_ENDPOINT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200508 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
Christina Quastda373fd2015-02-27 15:25:22 +0100509 USBEndpointDescriptor_INTERRUPT,
Christina Quaste01fb9a2015-04-04 19:56:23 +0200510 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
Christina Quastda373fd2015-02-27 15:25:22 +0100511 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
512 0x10
513 },
Christina Quast01bbdc32015-02-24 17:38:45 +0100514 /* Communication class interface standard descriptor */
515 {
516 sizeof(USBInterfaceDescriptor),
517 USBGenericDescriptor_INTERFACE,
518 1, /* This is interface #1 */
519 0, /* This is alternate setting #0 for this interface */
Christina Quast99d80ff2015-04-03 22:26:07 +0200520 3, /* Number of endpoints */
Christina Quast01bbdc32015-02-24 17:38:45 +0100521 0xff,
522 0,
Christina Quastbfd73542015-05-08 16:00:51 +0200523 0,
524 PHONE_CONF_STR, /* string descriptor for this interface */
Christina Quast01bbdc32015-02-24 17:38:45 +0100525 },
526 /* Bulk-OUT endpoint standard descriptor */
527 {
528 sizeof(USBEndpointDescriptor),
529 USBGenericDescriptor_ENDPOINT,
530 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
Christina Quast71234252015-04-03 11:35:59 +0200531 PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100532 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200533 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
Christina Quast01bbdc32015-02-24 17:38:45 +0100534 USBEndpointDescriptor_MAXBULKSIZE_FS),
535 0 /* Must be 0 for full-speed bulk endpoints */
536 },
537 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100538 {
539 sizeof(USBEndpointDescriptor),
540 USBGenericDescriptor_ENDPOINT,
541 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
Christina Quast71234252015-04-03 11:35:59 +0200542 PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100543 USBEndpointDescriptor_BULK,
Christina Quast71234252015-04-03 11:35:59 +0200544 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
Christina Quastf5549502015-02-24 14:27:08 +0100545 USBEndpointDescriptor_MAXBULKSIZE_FS),
546 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quastda373fd2015-02-27 15:25:22 +0100547 },
548 /* Notification endpoint descriptor */
549 {
550 sizeof(USBEndpointDescriptor),
551 USBGenericDescriptor_ENDPOINT,
Christina Quast71234252015-04-03 11:35:59 +0200552 USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
Christina Quastda373fd2015-02-27 15:25:22 +0100553 USBEndpointDescriptor_INTERRUPT,
Christina Quast71234252015-04-03 11:35:59 +0200554 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
Christina Quastda373fd2015-02-27 15:25:22 +0100555 USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
556 0x10
Christina Quastf5549502015-02-24 14:27:08 +0100557 }
558};
559
Christina Quastf5549502015-02-24 14:27:08 +0100560/** Standard USB device descriptor for the CDC serial driver */
561const USBDeviceDescriptor deviceDescriptor = {
562
563 sizeof(USBDeviceDescriptor),
564 USBGenericDescriptor_DEVICE,
565 USBDeviceDescriptor_USB2_00,
566 0xff,
567// CDCDeviceDescriptor_CLASS,
568 0xff,
569// CDCDeviceDescriptor_SUBCLASS,
570 0xff,
571// CDCDeviceDescriptor_PROTOCOL,
572 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
Christina Quast37350392015-05-08 16:47:26 +0200573 SIMTRACE_VENDOR_ID,
Christina Quastf5549502015-02-24 14:27:08 +0100574 SIMTRACE_PRODUCT_ID,
575 1, /* Release number */
576 0, /* No string descriptor for manufacturer */
577 PRODUCT_STRING, /* Index of product string descriptor */
578 0, /* No string descriptor for serial number */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100579 4 /* Device has 4 possible configurations */
Christina Quastf5549502015-02-24 14:27:08 +0100580};
581
Christina Quast968b9742015-02-25 14:10:12 +0100582const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
Christina Quast0becc0c2015-04-04 13:04:06 +0200583 &configurationDescriptorSniffer.configuration,
584 &configurationDescriptorCCID.configuration,
585 &configurationDescriptorPhone.configuration,
586 &configurationDescriptorMITM.configuration,
Christina Quast01bbdc32015-02-24 17:38:45 +0100587};
588
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200589/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100590static const USBDDriverDescriptors driverDescriptors = {
Christina Quastf5549502015-02-24 14:27:08 +0100591 &deviceDescriptor,
Christina Quastb06fc382015-04-06 23:30:21 +0200592 (const USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100593 0, /* No full-speed device qualifier descriptor */
594 0, /* No full-speed other speed configuration */
595 0, /* No high-speed device descriptor */
596 0, /* No high-speed configuration descriptor */
597 0, /* No high-speed device qualifier descriptor */
598 0, /* No high-speed other speed configuration descriptor */
599 stringDescriptors,
Christina Quast54d0c1f2015-02-25 16:04:25 +0100600 STRING_DESC_CNT /* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100601};
602
Christina Quast0ae03142015-02-25 18:43:46 +0100603
604/*----------------------------------------------------------------------------
Christina Quast968b9742015-02-25 14:10:12 +0100605 * Functions
606 *----------------------------------------------------------------------------*/
607
608/**
609 * \brief Configure 48MHz Clock for USB
610 */
611static void _ConfigureUsbClock(void)
612{
613 /* Enable PLLB for USB */
Christina Quast968b9742015-02-25 14:10:12 +0100614 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
615 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
616 | CKGR_PLLBR_PLLBCOUNT_Msk;
617 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
618 /* USB Clock uses PLLB */
619 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
620 | PMC_USB_USBS; /* PLLB */
621}
622
623
624void SIMtrace_USB_Initialize( void )
625{
626 _ConfigureUsbClock();
627 // Get std USB driver
628 USBDDriver *pUsbd = USBD_GetDriver();
629
630 TRACE_DEBUG(".");
631
632 // Initialize standard USB driver
633 USBDDriver_Initialize(pUsbd,
634 &driverDescriptors,
Christina Quast968b9742015-02-25 14:10:12 +0100635 0); // Multiple interface settings not supported
Christina Quast968b9742015-02-25 14:10:12 +0100636 USBD_Init();
Christina Quast968b9742015-02-25 14:10:12 +0100637 USBD_Connect();
638
639 NVIC_EnableIRQ( UDP_IRQn );
640}