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" |
Harald Welte | 1605564 | 2016-03-03 11:02:45 +0100 | [diff] [blame] | 35 | #include "simtrace.h" |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 36 | #include "utils.h" |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 37 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 38 | #include <cciddriverdescriptors.h> |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 39 | #include <usb/common/dfu/usb_dfu.h> |
| 40 | #include <usb/device/dfu/dfu.h> |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 41 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 42 | /*------------------------------------------------------------------------------ |
| 43 | * USB String descriptors |
| 44 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2363fa0 | 2017-03-05 10:16:25 +0100 | [diff] [blame] | 45 | #include "usb_strings_generated.h" |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 46 | enum strDescNum { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 47 | PRODUCT_STRING = 1, |
| 48 | MANUF_STR, |
| 49 | SNIFFER_CONF_STR, |
| 50 | CCID_CONF_STR, |
| 51 | PHONE_CONF_STR, |
| 52 | MITM_CONF_STR, |
| 53 | CARDEM_USIM1_INTF_STR, |
| 54 | CARDEM_USIM2_INTF_STR, |
| 55 | STRING_DESC_CNT |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 58 | /*------------------------------------------------------------------------------ |
| 59 | * USB Device descriptors |
| 60 | *------------------------------------------------------------------------------*/ |
| 61 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 62 | #ifdef HAVE_SNIFFER |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 63 | typedef struct _SIMTraceDriverConfigurationDescriptorSniffer { |
| 64 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 65 | /** Standard configuration descriptor. */ |
| 66 | USBConfigurationDescriptor configuration; |
| 67 | USBInterfaceDescriptor sniffer; |
| 68 | USBEndpointDescriptor sniffer_dataOut; |
| 69 | USBEndpointDescriptor sniffer_dataIn; |
| 70 | USBEndpointDescriptor sniffer_interruptIn; |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 71 | DFURT_IF_DESCRIPTOR_STRUCT; |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 72 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 73 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 74 | static const SIMTraceDriverConfigurationDescriptorSniffer |
| 75 | configurationDescriptorSniffer = { |
| 76 | /* Standard configuration descriptor */ |
| 77 | { |
| 78 | .bLength = sizeof(USBConfigurationDescriptor), |
| 79 | .bDescriptorType = USBGenericDescriptor_CONFIGURATION, |
| 80 | .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorSniffer), |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 81 | .bNumInterfaces = 1+DFURT_NUM_IF, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 82 | .bConfigurationValue = CFG_NUM_SNIFF, |
| 83 | .iConfiguration = SNIFFER_CONF_STR, |
| 84 | .bmAttributes = USBD_BMATTRIBUTES, |
| 85 | .bMaxPower = USBConfigurationDescriptor_POWER(100), |
| 86 | }, |
| 87 | /* Communication class interface standard descriptor */ |
| 88 | { |
| 89 | .bLength = sizeof(USBInterfaceDescriptor), |
| 90 | .bDescriptorType = USBGenericDescriptor_INTERFACE, |
| 91 | .bInterfaceNumber = 0, |
| 92 | .bAlternateSetting = 0, |
| 93 | .bNumEndpoints = 3, |
| 94 | .bInterfaceClass = 0xff, |
| 95 | .bInterfaceSubClass = 0, |
| 96 | .bInterfaceProtocol = 0, |
| 97 | .iInterface = SNIFFER_CONF_STR, |
| 98 | }, |
| 99 | /* Bulk-OUT endpoint standard descriptor */ |
| 100 | { |
| 101 | .bLength = sizeof(USBEndpointDescriptor), |
| 102 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 103 | .bEndpointAddress = USBEndpointDescriptor_ADDRESS( |
| 104 | USBEndpointDescriptor_OUT, |
| 105 | PHONE_DATAOUT), |
| 106 | .bmAttributes = USBEndpointDescriptor_BULK, |
| 107 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 108 | PHONE_DATAOUT), |
| 109 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 110 | .bInterval = 0, |
| 111 | }, |
| 112 | /* Bulk-IN endpoint descriptor */ |
| 113 | { |
| 114 | .bLength = sizeof(USBEndpointDescriptor), |
| 115 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 116 | .bEndpointAddress = USBEndpointDescriptor_ADDRESS( |
| 117 | USBEndpointDescriptor_IN, |
| 118 | PHONE_DATAIN), |
| 119 | .bmAttributes = USBEndpointDescriptor_BULK, |
| 120 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 121 | PHONE_DATAIN), |
| 122 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 123 | .bInterval = 0, |
| 124 | }, |
| 125 | // Notification endpoint descriptor |
| 126 | { |
| 127 | .bLength = sizeof(USBEndpointDescriptor), |
| 128 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 129 | .bEndpointAddress = USBEndpointDescriptor_ADDRESS( |
| 130 | USBEndpointDescriptor_IN, |
| 131 | PHONE_INT), |
| 132 | .bmAttributes = USBEndpointDescriptor_INTERRUPT, |
| 133 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 134 | PHONE_INT), |
| 135 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 136 | .bInterval = 0x10, |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 137 | }, |
| 138 | DFURT_IF_DESCRIPTOR(1, 0), |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 139 | }; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 140 | #endif /* HAVE_SNIFFER */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 141 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 142 | #ifdef HAVE_CCID |
Harald Welte | ec4fe23 | 2015-11-07 18:41:25 +0100 | [diff] [blame] | 143 | static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 144 | // Standard USB configuration descriptor |
| 145 | { |
| 146 | .bLength = sizeof(USBConfigurationDescriptor), |
| 147 | .bDescriptorType = USBGenericDescriptor_CONFIGURATION, |
| 148 | .wTotalLength = sizeof(CCIDDriverConfigurationDescriptors), |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 149 | .bNumInterfaces = 1+DFURT_NUM_IF, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 150 | .bConfigurationValue = CFG_NUM_CCID, |
| 151 | .iConfiguration = CCID_CONF_STR, |
| 152 | .bmAttributes = BOARD_USB_BMATTRIBUTES, |
| 153 | .bMaxPower = USBConfigurationDescriptor_POWER(100), |
| 154 | }, |
| 155 | // CCID interface descriptor |
| 156 | // Table 4.3-1 Interface Descriptor |
| 157 | // Interface descriptor |
| 158 | { |
| 159 | .bLength = sizeof(USBInterfaceDescriptor), |
| 160 | .bDescriptorType = USBGenericDescriptor_INTERFACE, |
| 161 | .bInterfaceNumber = 0, |
| 162 | .bAlternateSetting = 0, |
| 163 | .bNumEndpoints = 3, |
| 164 | .bInterfaceClass = SMART_CARD_DEVICE_CLASS, |
| 165 | .bInterfaceSubClass = 0, |
| 166 | .bInterfaceProtocol = 0, |
| 167 | .iInterface = CCID_CONF_STR, |
| 168 | }, |
| 169 | { |
| 170 | .bLength = sizeof(CCIDDescriptor), |
| 171 | .bDescriptorType = CCID_DECRIPTOR_TYPE, |
| 172 | .bcdCCID = CCID1_10, // CCID version |
| 173 | .bMaxSlotIndex = 0, // 1 slot |
| 174 | .bVoltageSupport = VOLTS_3_0, |
| 175 | .dwProtocols = (1 << PROTOCOL_TO), |
| 176 | .dwDefaultClock = 3580, |
| 177 | .dwMaximumClock = 3580, |
| 178 | .bNumClockSupported = 0, |
| 179 | .dwDataRate = 9600, |
| 180 | .dwMaxDataRate = 9600, |
| 181 | .bNumDataRatesSupported = 0, |
| 182 | .dwMaxIFSD = 0xfe, |
| 183 | .dwSynchProtocols = 0, |
| 184 | .dwMechanical = 0, |
| 185 | .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD | |
| 186 | CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | |
| 187 | CCID_FEATURES_EXC_TPDU, |
| 188 | .dwMaxCCIDMessageLength = 271, /* For extended APDU |
| 189 | level the value shall |
| 190 | be between 261 + 10 */ |
| 191 | .bClassGetResponse = 0xFF, // Echoes the class of the APDU |
| 192 | .bClassEnvelope = 0xFF, // Echoes the class of the APDU |
| 193 | .wLcdLayout = 0, // wLcdLayout: no LCD |
| 194 | .bPINSupport = 0, // bPINSupport: No PIN |
| 195 | .bMaxCCIDBusySlots = 1, |
| 196 | }, |
| 197 | // Bulk-OUT endpoint descriptor |
| 198 | { |
| 199 | .bLength = sizeof(USBEndpointDescriptor), |
| 200 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 201 | .bEndpointAddress = |
| 202 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
| 203 | CCID_EPT_DATA_OUT), |
| 204 | .bmAttributes = USBEndpointDescriptor_BULK, |
| 205 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 206 | CCID_EPT_DATA_OUT), |
| 207 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 208 | .bInterval = 0x00, |
| 209 | }, |
| 210 | // Bulk-IN endpoint descriptor |
| 211 | { |
| 212 | .bLength = sizeof(USBEndpointDescriptor), |
| 213 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 214 | .bEndpointAddress = |
| 215 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 216 | CCID_EPT_DATA_IN), |
| 217 | .bmAttributes = USBEndpointDescriptor_BULK, |
| 218 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 219 | CCID_EPT_DATA_IN), |
| 220 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 221 | .bInterval = 0x00, |
| 222 | }, |
| 223 | // Notification endpoint descriptor |
| 224 | { |
| 225 | .bLength = sizeof(USBEndpointDescriptor), |
| 226 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 227 | .bEndpointAddress = |
| 228 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 229 | CCID_EPT_NOTIFICATION), |
| 230 | .bmAttributes = USBEndpointDescriptor_INTERRUPT, |
| 231 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 232 | CCID_EPT_NOTIFICATION), |
| 233 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 234 | .bInterval = 0x10, |
| 235 | }, |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 236 | DFURT_IF_DESCRIPTOR(1, 0), |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 237 | }; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 238 | #endif /* HAVE_CCID */ |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 239 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 240 | #ifdef HAVE_CARDEM |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 241 | /* SIM card emulator */ |
| 242 | typedef struct _SIMTraceDriverConfigurationDescriptorPhone { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 243 | /* Standard configuration descriptor. */ |
| 244 | USBConfigurationDescriptor configuration; |
| 245 | USBInterfaceDescriptor phone; |
| 246 | USBEndpointDescriptor phone_dataOut; |
| 247 | USBEndpointDescriptor phone_dataIn; |
| 248 | USBEndpointDescriptor phone_interruptIn; |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 249 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 250 | USBInterfaceDescriptor usim2; |
| 251 | USBEndpointDescriptor usim2_dataOut; |
| 252 | USBEndpointDescriptor usim2_dataIn; |
| 253 | USBEndpointDescriptor usim2_interruptIn; |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 254 | #endif |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 255 | DFURT_IF_DESCRIPTOR_STRUCT; |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 256 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone; |
| 257 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 258 | static const SIMTraceDriverConfigurationDescriptorPhone |
| 259 | configurationDescriptorPhone = { |
| 260 | /* Standard configuration descriptor */ |
| 261 | { |
| 262 | sizeof(USBConfigurationDescriptor), |
| 263 | USBGenericDescriptor_CONFIGURATION, |
| 264 | sizeof(SIMTraceDriverConfigurationDescriptorPhone), |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 265 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 266 | 2+DFURT_NUM_IF, |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 267 | #else |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 268 | 1+DFURT_NUM_IF, /* There is one interface in this configuration */ |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 269 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 270 | CFG_NUM_PHONE, /* configuration number */ |
| 271 | PHONE_CONF_STR, /* string descriptor for this configuration */ |
| 272 | USBD_BMATTRIBUTES, |
| 273 | USBConfigurationDescriptor_POWER(100) |
| 274 | }, |
| 275 | /* Communication class interface standard descriptor */ |
| 276 | { |
| 277 | sizeof(USBInterfaceDescriptor), |
| 278 | USBGenericDescriptor_INTERFACE, |
| 279 | 0, /* This is interface #0 */ |
| 280 | 0, /* This is alternate setting #0 for this interface */ |
| 281 | 3, /* Number of endpoints */ |
| 282 | 0xff, /* Descriptor Class: Vendor specific */ |
| 283 | 0, /* No subclass */ |
| 284 | 0, /* No l */ |
| 285 | CARDEM_USIM1_INTF_STR |
| 286 | }, |
| 287 | /* Bulk-OUT endpoint standard descriptor */ |
| 288 | { |
| 289 | sizeof(USBEndpointDescriptor), |
| 290 | USBGenericDescriptor_ENDPOINT, |
| 291 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
| 292 | PHONE_DATAOUT), |
| 293 | USBEndpointDescriptor_BULK, |
| 294 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT), |
| 295 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 296 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 297 | }, |
| 298 | /* Bulk-IN endpoint descriptor */ |
| 299 | { |
| 300 | sizeof(USBEndpointDescriptor), |
| 301 | USBGenericDescriptor_ENDPOINT, |
| 302 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 303 | PHONE_DATAIN), |
| 304 | USBEndpointDescriptor_BULK, |
| 305 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN), |
| 306 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 307 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 308 | }, |
| 309 | /* Notification endpoint descriptor */ |
| 310 | { |
| 311 | sizeof(USBEndpointDescriptor), |
| 312 | USBGenericDescriptor_ENDPOINT, |
| 313 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 314 | PHONE_INT), |
| 315 | USBEndpointDescriptor_INTERRUPT, |
| 316 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT), |
| 317 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 318 | 0x10 |
| 319 | }, |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 320 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 321 | /* Communication class interface standard descriptor */ |
| 322 | { |
| 323 | sizeof(USBInterfaceDescriptor), |
| 324 | USBGenericDescriptor_INTERFACE, |
Harald Welte | 6dcacf3 | 2016-03-19 14:01:31 +0100 | [diff] [blame] | 325 | 1, /* This is interface #1 */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 326 | 0, /* This is alternate setting #0 for this interface */ |
| 327 | 3, /* Number of endpoints */ |
| 328 | 0xff, /* Descriptor Class: Vendor specific */ |
| 329 | 0, /* No subclass */ |
| 330 | 0, /* No l */ |
| 331 | CARDEM_USIM2_INTF_STR |
| 332 | }, |
| 333 | /* Bulk-OUT endpoint standard descriptor */ |
| 334 | { |
| 335 | sizeof(USBEndpointDescriptor), |
| 336 | USBGenericDescriptor_ENDPOINT, |
| 337 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
| 338 | CARDEM_USIM2_DATAOUT), |
| 339 | USBEndpointDescriptor_BULK, |
| 340 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_DATAOUT), |
| 341 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 342 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 343 | } |
| 344 | , |
| 345 | /* Bulk-IN endpoint descriptor */ |
| 346 | { |
| 347 | sizeof(USBEndpointDescriptor), |
| 348 | USBGenericDescriptor_ENDPOINT, |
| 349 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 350 | CARDEM_USIM2_DATAIN), |
| 351 | USBEndpointDescriptor_BULK, |
| 352 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_DATAIN), |
| 353 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 354 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 355 | }, |
| 356 | /* Notification endpoint descriptor */ |
| 357 | { |
| 358 | sizeof(USBEndpointDescriptor), |
| 359 | USBGenericDescriptor_ENDPOINT, |
| 360 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 361 | CARDEM_USIM2_INT), |
| 362 | USBEndpointDescriptor_INTERRUPT, |
| 363 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CARDEM_USIM2_INT), |
| 364 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 365 | 0x10 |
| 366 | }, |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 367 | DFURT_IF_DESCRIPTOR(2, 0), |
| 368 | #else |
| 369 | DFURT_IF_DESCRIPTOR(1, 0), |
Harald Welte | 57b3a25 | 2016-03-02 15:20:27 +0100 | [diff] [blame] | 370 | #endif |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 371 | }; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 372 | #endif /* HAVE_CARDEM */ |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 373 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 374 | #ifdef HAVE_MITM |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 375 | typedef struct _SIMTraceDriverConfigurationDescriptorMITM { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 376 | /* Standard configuration descriptor. */ |
| 377 | USBConfigurationDescriptor configuration; |
| 378 | USBInterfaceDescriptor simcard; |
| 379 | /// CCID descriptor |
| 380 | CCIDDescriptor ccid; |
| 381 | /// Bulk OUT endpoint descriptor |
| 382 | USBEndpointDescriptor simcard_dataOut; |
| 383 | /// Bulk IN endpoint descriptor |
| 384 | USBEndpointDescriptor simcard_dataIn; |
| 385 | /// Interrupt OUT endpoint descriptor |
| 386 | USBEndpointDescriptor simcard_interruptIn; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 387 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 388 | USBInterfaceDescriptor phone; |
| 389 | USBEndpointDescriptor phone_dataOut; |
| 390 | USBEndpointDescriptor phone_dataIn; |
| 391 | USBEndpointDescriptor phone_interruptIn; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 392 | |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 393 | DFURT_IF_DESCRIPTOR_STRUCT; |
| 394 | |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 395 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 396 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 397 | static const SIMTraceDriverConfigurationDescriptorMITM |
| 398 | configurationDescriptorMITM = { |
| 399 | /* Standard configuration descriptor */ |
| 400 | { |
| 401 | sizeof(USBConfigurationDescriptor), |
| 402 | USBGenericDescriptor_CONFIGURATION, |
| 403 | sizeof(SIMTraceDriverConfigurationDescriptorMITM), |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 404 | 2+DFURT_NUM_IF, /* There are two interfaces in this configuration */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 405 | CFG_NUM_MITM, /* configuration number */ |
| 406 | MITM_CONF_STR, /* string descriptor for this configuration */ |
| 407 | USBD_BMATTRIBUTES, |
| 408 | USBConfigurationDescriptor_POWER(100) |
| 409 | }, |
| 410 | // CCID interface descriptor |
| 411 | // Table 4.3-1 Interface Descriptor |
| 412 | // Interface descriptor |
| 413 | { |
| 414 | .bLength = sizeof(USBInterfaceDescriptor), |
| 415 | .bDescriptorType = USBGenericDescriptor_INTERFACE, |
| 416 | .bInterfaceNumber = 0, |
| 417 | .bAlternateSetting = 0, |
| 418 | .bNumEndpoints = 3, |
| 419 | .bInterfaceClass = SMART_CARD_DEVICE_CLASS, |
| 420 | .bInterfaceSubClass = 0, |
| 421 | .bInterfaceProtocol = 0, |
| 422 | .iInterface = CCID_CONF_STR, |
| 423 | }, |
| 424 | { |
| 425 | .bLength = sizeof(CCIDDescriptor), |
| 426 | .bDescriptorType = CCID_DECRIPTOR_TYPE, |
| 427 | .bcdCCID = CCID1_10, // CCID version |
| 428 | .bMaxSlotIndex = 0, // 1 slot |
| 429 | .bVoltageSupport = VOLTS_3_0, |
| 430 | .dwProtocols = (1 << PROTOCOL_TO), |
| 431 | .dwDefaultClock = 3580, |
| 432 | .dwMaximumClock = 3580, |
| 433 | .bNumClockSupported = 0, |
| 434 | .dwDataRate = 9600, |
| 435 | .dwMaxDataRate = 9600, |
| 436 | .bNumDataRatesSupported = 0, |
| 437 | .dwMaxIFSD = 0xfe, |
| 438 | .dwSynchProtocols = 0, |
| 439 | .dwMechanical = 0, |
| 440 | .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD | |
| 441 | CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | |
| 442 | CCID_FEATURES_EXC_TPDU, |
| 443 | .dwMaxCCIDMessageLength = 271, /* For extended APDU |
| 444 | level the value shall |
| 445 | be between 261 + 10 */ |
| 446 | .bClassGetResponse = 0xFF, // Echoes the class of the APDU |
| 447 | .bClassEnvelope = 0xFF, // Echoes the class of the APDU |
| 448 | .wLcdLayout = 0, // wLcdLayout: no LCD |
| 449 | .bPINSupport = 0, // bPINSupport: No PIN |
| 450 | .bMaxCCIDBusySlots = 1, |
| 451 | }, |
| 452 | // Bulk-OUT endpoint descriptor |
| 453 | { |
| 454 | .bLength = sizeof(USBEndpointDescriptor), |
| 455 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 456 | .bEndpointAddress = |
| 457 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
| 458 | CCID_EPT_DATA_OUT), |
| 459 | .bmAttributes = USBEndpointDescriptor_BULK, |
| 460 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 461 | CCID_EPT_DATA_OUT), |
| 462 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 463 | .bInterval = 0x00, |
| 464 | }, |
| 465 | // Bulk-IN endpoint descriptor |
| 466 | { |
| 467 | .bLength = sizeof(USBEndpointDescriptor), |
| 468 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 469 | .bEndpointAddress = |
| 470 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 471 | CCID_EPT_DATA_IN), |
| 472 | .bmAttributes = USBEndpointDescriptor_BULK, |
| 473 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 474 | CCID_EPT_DATA_IN), |
| 475 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 476 | .bInterval = 0x00, |
| 477 | }, |
| 478 | // Notification endpoint descriptor |
| 479 | { |
| 480 | .bLength = sizeof(USBEndpointDescriptor), |
| 481 | .bDescriptorType = USBGenericDescriptor_ENDPOINT, |
| 482 | .bEndpointAddress = |
| 483 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 484 | CCID_EPT_NOTIFICATION), |
| 485 | .bmAttributes = USBEndpointDescriptor_INTERRUPT, |
| 486 | .wMaxPacketSize = MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE( |
| 487 | CCID_EPT_NOTIFICATION), |
| 488 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
| 489 | .bInterval = 0x10, |
| 490 | }, |
| 491 | |
| 492 | /* Communication class interface standard descriptor */ |
| 493 | { |
| 494 | sizeof(USBInterfaceDescriptor), |
| 495 | USBGenericDescriptor_INTERFACE, |
| 496 | 1, /* This is interface #1 */ |
| 497 | 0, /* This is alternate setting #0 for this interface */ |
| 498 | 3, /* Number of endpoints */ |
| 499 | 0xff, |
| 500 | 0, |
| 501 | 0, |
| 502 | PHONE_CONF_STR, /* string descriptor for this interface */ |
| 503 | } |
| 504 | , |
| 505 | /* Bulk-OUT endpoint standard descriptor */ |
| 506 | { |
| 507 | sizeof(USBEndpointDescriptor), |
| 508 | USBGenericDescriptor_ENDPOINT, |
| 509 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
| 510 | PHONE_DATAOUT), |
| 511 | USBEndpointDescriptor_BULK, |
| 512 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT), |
| 513 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 514 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 515 | } |
| 516 | , |
| 517 | /* Bulk-IN endpoint descriptor */ |
| 518 | { |
| 519 | sizeof(USBEndpointDescriptor), |
| 520 | USBGenericDescriptor_ENDPOINT, |
| 521 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 522 | PHONE_DATAIN), |
| 523 | USBEndpointDescriptor_BULK, |
| 524 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN), |
| 525 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 526 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 527 | } |
| 528 | , |
| 529 | /* Notification endpoint descriptor */ |
| 530 | { |
| 531 | sizeof(USBEndpointDescriptor), |
| 532 | USBGenericDescriptor_ENDPOINT, |
| 533 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, PHONE_INT), |
| 534 | USBEndpointDescriptor_INTERRUPT, |
| 535 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT), |
| 536 | USBEndpointDescriptor_MAXINTERRUPTSIZE_FS), |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 537 | 0x10}, |
| 538 | DFURT_IF_DESCRIPTOR(2, 0), |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 539 | }; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 540 | #endif /* HAVE_CARDEM */ |
| 541 | |
| 542 | const USBConfigurationDescriptor *configurationDescriptorsArr[] = { |
| 543 | #ifdef HAVE_SNIFFER |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 544 | &configurationDescriptorSniffer.configuration, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 545 | #endif |
| 546 | #ifdef HAVE_CCID |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 547 | &configurationDescriptorCCID.configuration, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 548 | #endif |
| 549 | #ifdef HAVE_CARDEM |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 550 | &configurationDescriptorPhone.configuration, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 551 | #endif |
| 552 | #ifdef HAVE_MITM |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 553 | &configurationDescriptorMITM.configuration, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 554 | #endif |
| 555 | }; |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 556 | |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 557 | /** Standard USB device descriptor for the CDC serial driver */ |
| 558 | const USBDeviceDescriptor deviceDescriptor = { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 559 | .bLength = sizeof(USBDeviceDescriptor), |
| 560 | .bDescriptorType = USBGenericDescriptor_DEVICE, |
| 561 | .bcdUSB = USBDeviceDescriptor_USB2_00, |
| 562 | .bDeviceClass = 0xff, |
| 563 | .bDeviceSubClass = 0xff, |
| 564 | .bDeviceProtocol = 0xff, |
| 565 | .bMaxPacketSize0 = BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0), |
Harald Welte | c6e482d | 2017-03-05 16:24:29 +0100 | [diff] [blame^] | 566 | .idVendor = BOARD_USB_VENDOR_ID, |
| 567 | .idProduct = BOARD_USB_PRODUCT_ID, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 568 | .bcdDevice = 2, /* Release number */ |
| 569 | .iManufacturer = MANUF_STR, |
| 570 | .iProduct = PRODUCT_STRING, |
| 571 | .iSerialNumber = 0, |
| 572 | .bNumConfigurations = ARRAY_SIZE(configurationDescriptorsArr), |
Christina Quast | 01bbdc3 | 2015-02-24 17:38:45 +0100 | [diff] [blame] | 573 | }; |
| 574 | |
Christina Quast | bd5b8bd | 2015-05-14 17:25:41 +0200 | [diff] [blame] | 575 | /* AT91SAM3S only supports full speed, but not high speed USB */ |
Harald Welte | ec4fe23 | 2015-11-07 18:41:25 +0100 | [diff] [blame] | 576 | static const USBDDriverDescriptors driverDescriptors = { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 577 | &deviceDescriptor, |
| 578 | (const USBConfigurationDescriptor **)&(configurationDescriptorsArr), /* first full-speed configuration descriptor */ |
| 579 | 0, /* No full-speed device qualifier descriptor */ |
| 580 | 0, /* No full-speed other speed configuration */ |
| 581 | 0, /* No high-speed device descriptor */ |
| 582 | 0, /* No high-speed configuration descriptor */ |
| 583 | 0, /* No high-speed device qualifier descriptor */ |
| 584 | 0, /* No high-speed other speed configuration descriptor */ |
Harald Welte | 2363fa0 | 2017-03-05 10:16:25 +0100 | [diff] [blame] | 585 | usb_strings, |
| 586 | ARRAY_SIZE(usb_strings),/* cnt string descriptors in list */ |
Christina Quast | f554950 | 2015-02-24 14:27:08 +0100 | [diff] [blame] | 587 | }; |
| 588 | |
Christina Quast | 0ae0314 | 2015-02-25 18:43:46 +0100 | [diff] [blame] | 589 | /*---------------------------------------------------------------------------- |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 590 | * Functions |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 591 | *----------------------------------------------------------------------------*/ |
| 592 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 593 | void SIMtrace_USB_Initialize(void) |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 594 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 595 | // Get std USB driver |
| 596 | USBDDriver *pUsbd = USBD_GetDriver(); |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 597 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 598 | TRACE_DEBUG("."); |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 599 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 600 | // Initialize standard USB driver |
| 601 | USBDDriver_Initialize(pUsbd, &driverDescriptors, 0); // Multiple interface settings not supported |
| 602 | USBD_Init(); |
| 603 | USBD_Connect(); |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 604 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 605 | NVIC_EnableIRQ(UDP_IRQn); |
Christina Quast | 968b974 | 2015-02-25 14:10:12 +0100 | [diff] [blame] | 606 | } |