Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 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 Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 36 | #include <cciddriverdescriptors.h> |
| 37 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 38 | /*------------------------------------------------------------------------------ |
| 39 | * USB String descriptors |
| 40 | *------------------------------------------------------------------------------*/ |
| 41 | |
Christina Quast | 6032e79 | 2015-02-27 15:22:25 +0100 | [diff] [blame] | 42 | |
| 43 | static const unsigned char langDesc[] = { |
| 44 | |
| 45 | USBStringDescriptor_LENGTH(1), |
| 46 | USBGenericDescriptor_STRING, |
| 47 | USBStringDescriptor_ENGLISH_US |
| 48 | }; |
| 49 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 50 | const 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 | |
| 64 | const 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 | |
| 85 | const 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 | |
| 103 | const 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 | |
| 123 | const 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 | |
| 141 | enum strDescNum { |
Christina Quast | 54d0c1f | 2015-02-25 16:04:25 +0100 | [diff] [blame] | 142 | PRODUCT_STRING = 1, SNIFFER_CONF_STR, CCID_CONF_STR, PHONE_CONF_STR, MITM_CONF_STR, STRING_DESC_CNT |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /** List of string descriptors used by the device */ |
| 146 | const 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 Quast | 6032e79 | 2015-02-27 15:22:25 +0100 | [diff] [blame] | 151 | langDesc, |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 152 | productStringDescriptor, |
| 153 | snifferConfigStringDescriptor, |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 154 | CCIDConfigStringDescriptor, |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 155 | phoneConfigStringDescriptor, |
| 156 | MITMConfigStringDescriptor |
| 157 | }; |
| 158 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 159 | /*------------------------------------------------------------------------------ |
| 160 | * USB Device descriptors |
| 161 | *------------------------------------------------------------------------------*/ |
| 162 | |
| 163 | typedef struct _SIMTraceDriverConfigurationDescriptorSniffer { |
| 164 | |
| 165 | /** Standard configuration descriptor. */ |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 166 | USBConfigurationDescriptor configuration; |
| 167 | USBInterfaceDescriptor sniffer; |
| 168 | USBEndpointDescriptor sniffer_dataOut; |
| 169 | USBEndpointDescriptor sniffer_dataIn; |
| 170 | USBEndpointDescriptor sniffer_interruptIn; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 171 | |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 172 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 173 | |
| 174 | const 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 Quast | 10b2e5a | 2015-02-25 18:40:15 +0100 | [diff] [blame] | 181 | CFG_NUM_SNIFF, /* configuration number */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 182 | 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 Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 192 | 3, /* This interface uses 3 endpoints */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 193 | 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 Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 199 | { |
| 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 Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 210 | { |
| 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 Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 219 | }, |
| 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 Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 229 | } |
| 230 | }; |
| 231 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 232 | /* |
| 233 | /// CCIDDriverConfiguration Descriptors |
| 234 | /// List of descriptors that make up the configuration descriptors of a |
| 235 | /// device using the CCID driver. |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 236 | typedef struct { |
| 237 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 238 | /// Configuration descriptor |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 239 | USBConfigurationDescriptor configuration; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 240 | /// Interface descriptor |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 241 | USBInterfaceDescriptor interface; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 242 | /// CCID descriptor |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 243 | CCIDDescriptor ccid; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 244 | /// Bulk OUT endpoint descriptor |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 245 | USBEndpointDescriptor bulkOut; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 246 | /// Bulk IN endpoint descriptor |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 247 | USBEndpointDescriptor bulkIn; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 248 | /// Interrupt OUT endpoint descriptor |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 249 | USBEndpointDescriptor interruptIn; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 250 | } __attribute__ ((packed)) CCIDDriverConfigurationDescriptors; |
| 251 | */ |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 252 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 253 | const 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 Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 338 | |
| 339 | /* SIM card emulator */ |
| 340 | typedef struct _SIMTraceDriverConfigurationDescriptorPhone { |
| 341 | |
| 342 | /** Standard configuration descriptor. */ |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 343 | USBConfigurationDescriptor configuration; |
| 344 | USBInterfaceDescriptor phone; |
| 345 | USBEndpointDescriptor phone_dataOut; |
| 346 | USBEndpointDescriptor phone_dataIn; |
| 347 | USBEndpointDescriptor phone_interruptIn; |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 348 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone; |
| 349 | |
| 350 | const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = { |
| 351 | /* Standard configuration descriptor */ |
| 352 | { |
| 353 | sizeof(USBConfigurationDescriptor), |
| 354 | USBGenericDescriptor_CONFIGURATION, |
| 355 | sizeof(SIMTraceDriverConfigurationDescriptorSniffer), |
| 356 | 1, /* There is one interface in this configuration */ |
Christina Quast | 10b2e5a | 2015-02-25 18:40:15 +0100 | [diff] [blame] | 357 | CFG_NUM_PHONE, /* configuration number */ |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 358 | 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 Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 368 | 3, /* This interface uses 3 endpoints */ |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 369 | 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, |
| 379 | DATAOUT), |
| 380 | USBEndpointDescriptor_BULK, |
| 381 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT), |
| 382 | 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, |
| 390 | DATAIN), |
| 391 | USBEndpointDescriptor_BULK, |
| 392 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN), |
| 393 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 394 | 0 /* Must be 0 for full-speed bulk endpoints */ |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 395 | }, |
| 396 | /* Notification endpoint descriptor */ |
| 397 | { |
| 398 | sizeof(USBEndpointDescriptor), |
| 399 | USBGenericDescriptor_ENDPOINT, |
| 400 | USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, INT ), |
| 401 | USBEndpointDescriptor_INTERRUPT, |
| 402 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(INT), |
| 403 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 404 | 0x10 |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 405 | } |
| 406 | }; |
| 407 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 408 | |
| 409 | typedef struct _SIMTraceDriverConfigurationDescriptorMITM { |
| 410 | |
| 411 | /** Standard configuration descriptor. */ |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 412 | 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 Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 421 | |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 422 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 423 | |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 424 | const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = { |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 425 | /* Standard configuration descriptor */ |
| 426 | { |
| 427 | sizeof(USBConfigurationDescriptor), |
| 428 | USBGenericDescriptor_CONFIGURATION, |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 429 | sizeof(SIMTraceDriverConfigurationDescriptorMITM), |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 430 | 2, /* There are two interfaces in this configuration */ |
Christina Quast | 10b2e5a | 2015-02-25 18:40:15 +0100 | [diff] [blame] | 431 | CFG_NUM_MITM, /* configuration number */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 432 | 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 Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 442 | 3, /* This interface uses 3 endpoints */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 443 | //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 Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 452 | { |
| 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 Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 463 | { |
| 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 Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 472 | }, |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 473 | /* 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 Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 483 | /* 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 Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 489 | 3, /* This interface uses 3 endpoints */ |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 490 | 0xff, |
| 491 | 0, |
| 492 | 0, |
| 493 | 0, /* FIXME: string descriptor for this interface */ |
| 494 | }, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 495 | #define DATAOUT_2 DATAOUT+3 |
| 496 | #define DATAIN_2 DATAIN+3 |
| 497 | #define INT_2 INT+3 |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 498 | /* Bulk-OUT endpoint standard descriptor */ |
| 499 | { |
| 500 | sizeof(USBEndpointDescriptor), |
| 501 | USBGenericDescriptor_ENDPOINT, |
| 502 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 503 | DATAOUT_2), |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 504 | USBEndpointDescriptor_BULK, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 505 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT_2), |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 506 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 507 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 508 | }, |
| 509 | /* Bulk-IN endpoint descriptor */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 510 | { |
| 511 | sizeof(USBEndpointDescriptor), |
| 512 | USBGenericDescriptor_ENDPOINT, |
| 513 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 514 | DATAIN_2), |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 515 | USBEndpointDescriptor_BULK, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 516 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN_2), |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 517 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 518 | 0 /* Must be 0 for full-speed bulk endpoints */ |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 519 | }, |
| 520 | /* Notification endpoint descriptor */ |
| 521 | { |
| 522 | sizeof(USBEndpointDescriptor), |
| 523 | USBGenericDescriptor_ENDPOINT, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 524 | USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, INT_2 ), |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 525 | USBEndpointDescriptor_INTERRUPT, |
Christina Quast | 0166c6d | 2015-03-05 15:44:05 +0100 | [diff] [blame] | 526 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(INT_2), |
Christina Quast | da373fd | 2015-02-27 15:25:22 +0100 | [diff] [blame] | 527 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 528 | 0x10 |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 529 | } |
| 530 | }; |
| 531 | |
| 532 | |
| 533 | /** Standard USB device descriptor for the CDC serial driver */ |
| 534 | const USBDeviceDescriptor deviceDescriptor = { |
| 535 | |
| 536 | sizeof(USBDeviceDescriptor), |
| 537 | USBGenericDescriptor_DEVICE, |
| 538 | USBDeviceDescriptor_USB2_00, |
| 539 | 0xff, |
| 540 | // CDCDeviceDescriptor_CLASS, |
| 541 | 0xff, |
| 542 | // CDCDeviceDescriptor_SUBCLASS, |
| 543 | 0xff, |
| 544 | // CDCDeviceDescriptor_PROTOCOL, |
| 545 | BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0), |
| 546 | ATMEL_VENDOR_ID, |
| 547 | SIMTRACE_PRODUCT_ID, |
| 548 | 1, /* Release number */ |
| 549 | 0, /* No string descriptor for manufacturer */ |
| 550 | PRODUCT_STRING, /* Index of product string descriptor */ |
| 551 | 0, /* No string descriptor for serial number */ |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 552 | 4 /* Device has 4 possible configurations */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 553 | }; |
| 554 | |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 555 | const USBConfigurationDescriptor *configurationDescriptorsArr[] = { |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 556 | &configurationDescriptorSniffer, |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 557 | &configurationDescriptorCCID, |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 558 | &configurationDescriptorPhone, |
| 559 | &configurationDescriptorMITM, |
| 560 | }; |
| 561 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 562 | USBConfigurationDescriptor *getConfigDesc(uint8_t idx) { |
| 563 | return configurationDescriptorsArr[idx]; |
| 564 | } |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 565 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 566 | /* AT91SAM3S does only support full speed, but not high speed USB */ |
| 567 | const USBDDriverDescriptors driverDescriptors = { |
| 568 | &deviceDescriptor, |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 569 | (USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 570 | 0, /* No full-speed device qualifier descriptor */ |
| 571 | 0, /* No full-speed other speed configuration */ |
| 572 | 0, /* No high-speed device descriptor */ |
| 573 | 0, /* No high-speed configuration descriptor */ |
| 574 | 0, /* No high-speed device qualifier descriptor */ |
| 575 | 0, /* No high-speed other speed configuration descriptor */ |
| 576 | stringDescriptors, |
Christina Quast | 54d0c1f | 2015-02-25 16:04:25 +0100 | [diff] [blame] | 577 | STRING_DESC_CNT /* cnt string descriptors in list */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 578 | }; |
| 579 | |
Christina Quast | 0ae0314 | 2015-02-25 18:43:46 +0100 | [diff] [blame] | 580 | |
| 581 | /*---------------------------------------------------------------------------- |
| 582 | * Callbacks |
| 583 | *----------------------------------------------------------------------------*/ |
| 584 | extern uint8_t conf_changed; |
| 585 | extern uint8_t simtrace_config; |
| 586 | |
| 587 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 588 | { |
| 589 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 590 | conf_changed =1; |
| 591 | simtrace_config = cfgnum; |
| 592 | } |
| 593 | |
| 594 | |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 595 | /*---------------------------------------------------------------------------- |
| 596 | * Functions |
| 597 | *----------------------------------------------------------------------------*/ |
| 598 | |
| 599 | /** |
| 600 | * \brief Configure 48MHz Clock for USB |
| 601 | */ |
| 602 | static void _ConfigureUsbClock(void) |
| 603 | { |
| 604 | /* Enable PLLB for USB */ |
| 605 | // FIXME: are these the dividers I actually need? |
| 606 | // FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways? |
| 607 | PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5) |
| 608 | | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/ |
| 609 | | CKGR_PLLBR_PLLBCOUNT_Msk; |
| 610 | while((PMC->PMC_SR & PMC_SR_LOCKB) == 0); |
| 611 | /* USB Clock uses PLLB */ |
| 612 | PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */ |
| 613 | | PMC_USB_USBS; /* PLLB */ |
| 614 | } |
| 615 | |
| 616 | |
| 617 | void SIMtrace_USB_Initialize( void ) |
| 618 | { |
| 619 | _ConfigureUsbClock(); |
| 620 | // Get std USB driver |
| 621 | USBDDriver *pUsbd = USBD_GetDriver(); |
| 622 | |
| 623 | TRACE_DEBUG("."); |
| 624 | |
| 625 | // Initialize standard USB driver |
| 626 | USBDDriver_Initialize(pUsbd, |
| 627 | &driverDescriptors, |
| 628 | // FIXME: 2 interface settings supported in MITM mode |
| 629 | 0); // Multiple interface settings not supported |
| 630 | |
| 631 | USBD_Init(); |
| 632 | |
| 633 | USBD_Connect(); |
| 634 | |
| 635 | NVIC_EnableIRQ( UDP_IRQn ); |
| 636 | } |