blob: e75646e5b5862c2890bc61cd4a2626ea24c83f8e [file] [log] [blame]
Christina Quast968b9742015-02-25 14:10:12 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
Kévin Redone0265462019-08-06 15:41:31 +02005 * Copyright (c) 2018-2019, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Christina Quast968b9742015-02-25 14:10:12 +01006 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the disclaimer below.
14 *
15 * Atmel's name may not be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
21 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * ----------------------------------------------------------------------------
29 */
30
31/*----------------------------------------------------------------------------
32 * Headers
33 *----------------------------------------------------------------------------*/
34
35#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010036#include "simtrace.h"
Kévin Redona1012b12018-07-01 18:11:01 +020037#include "simtrace_usb.h"
Harald Welte2fb59962016-02-28 12:34:26 +010038#include "utils.h"
Kévin Redonff3d8492018-08-06 17:57:20 +020039#include "USBD_HAL.h"
Christina Quast968b9742015-02-25 14:10:12 +010040
Christina Quastdb7b1ab2015-03-03 12:34:36 +010041#include <cciddriverdescriptors.h>
Harald Welte7ed6f3b2017-02-26 17:00:43 +010042#include <usb/common/dfu/usb_dfu.h>
43#include <usb/device/dfu/dfu.h>
Christina Quastdb7b1ab2015-03-03 12:34:36 +010044
Christina Quastf5549502015-02-24 14:27:08 +010045/*------------------------------------------------------------------------------
46 * USB String descriptors
47 *------------------------------------------------------------------------------*/
Harald Welte2363fa02017-03-05 10:16:25 +010048#include "usb_strings_generated.h"
Kévin Redone0265462019-08-06 15:41:31 +020049
50// the index of the strings (must match the order in usb_strings.txt)
Christina Quastf5549502015-02-24 14:27:08 +010051enum strDescNum {
Kévin Redone0265462019-08-06 15:41:31 +020052 // static strings from usb_strings
Harald Welte3b646952017-05-14 23:13:52 +020053 MANUF_STR = 1,
54 PRODUCT_STRING,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010055 SNIFFER_CONF_STR,
56 CCID_CONF_STR,
57 PHONE_CONF_STR,
58 MITM_CONF_STR,
59 CARDEM_USIM1_INTF_STR,
60 CARDEM_USIM2_INTF_STR,
Kévin Redone0265462019-08-06 15:41:31 +020061 CARDEM_USIM3_INTF_STR,
62 CARDEM_USIM4_INTF_STR,
63 // runtime strings
64 SERIAL_STR,
Kévin Redonede87e02019-08-13 17:03:11 +020065 VERSION_CONF_STR,
Kévin Redone0265462019-08-06 15:41:31 +020066 VERSION_STR,
67 // count
Harald Welte7dd3dfd2016-03-03 12:32:04 +010068 STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +010069};
70
Kévin Redone0265462019-08-06 15:41:31 +020071/** array of static (from usb_strings) and runtime (serial, version) USB strings
72 */
Kévin Redonede87e02019-08-13 17:03:11 +020073static const unsigned char *usb_strings_extended[ARRAY_SIZE(usb_strings) + 3];
Kévin Redone0265462019-08-06 15:41:31 +020074
75/* USB string for the serial (using 128-bit device ID) */
76static unsigned char usb_string_serial[] = {
77 USBStringDescriptor_LENGTH(32),
78 USBGenericDescriptor_STRING,
79 USBStringDescriptor_UNICODE('0'),
80 USBStringDescriptor_UNICODE('0'),
81 USBStringDescriptor_UNICODE('1'),
82 USBStringDescriptor_UNICODE('1'),
83 USBStringDescriptor_UNICODE('2'),
84 USBStringDescriptor_UNICODE('2'),
85 USBStringDescriptor_UNICODE('3'),
86 USBStringDescriptor_UNICODE('3'),
87 USBStringDescriptor_UNICODE('4'),
88 USBStringDescriptor_UNICODE('4'),
89 USBStringDescriptor_UNICODE('5'),
90 USBStringDescriptor_UNICODE('5'),
91 USBStringDescriptor_UNICODE('6'),
92 USBStringDescriptor_UNICODE('6'),
93 USBStringDescriptor_UNICODE('7'),
94 USBStringDescriptor_UNICODE('7'),
95 USBStringDescriptor_UNICODE('8'),
96 USBStringDescriptor_UNICODE('8'),
97 USBStringDescriptor_UNICODE('9'),
98 USBStringDescriptor_UNICODE('9'),
99 USBStringDescriptor_UNICODE('a'),
100 USBStringDescriptor_UNICODE('a'),
101 USBStringDescriptor_UNICODE('b'),
102 USBStringDescriptor_UNICODE('b'),
103 USBStringDescriptor_UNICODE('c'),
104 USBStringDescriptor_UNICODE('c'),
105 USBStringDescriptor_UNICODE('d'),
106 USBStringDescriptor_UNICODE('d'),
107 USBStringDescriptor_UNICODE('e'),
108 USBStringDescriptor_UNICODE('e'),
109 USBStringDescriptor_UNICODE('f'),
110 USBStringDescriptor_UNICODE('f'),
111};
112
113/* USB string for the version */
Kévin Redonede87e02019-08-13 17:03:11 +0200114static const unsigned char usb_string_version_conf[] = {
115 USBStringDescriptor_LENGTH(16),
116 USBGenericDescriptor_STRING,
117 USBStringDescriptor_UNICODE('f'),
118 USBStringDescriptor_UNICODE('i'),
119 USBStringDescriptor_UNICODE('r'),
120 USBStringDescriptor_UNICODE('m'),
121 USBStringDescriptor_UNICODE('w'),
122 USBStringDescriptor_UNICODE('a'),
123 USBStringDescriptor_UNICODE('r'),
124 USBStringDescriptor_UNICODE('e'),
125 USBStringDescriptor_UNICODE(' '),
126 USBStringDescriptor_UNICODE('v'),
127 USBStringDescriptor_UNICODE('e'),
128 USBStringDescriptor_UNICODE('r'),
129 USBStringDescriptor_UNICODE('s'),
130 USBStringDescriptor_UNICODE('i'),
131 USBStringDescriptor_UNICODE('o'),
132 USBStringDescriptor_UNICODE('n'),
133};
Kévin Redone0265462019-08-06 15:41:31 +0200134static const char git_version[] = GIT_VERSION;
135static unsigned char usb_string_version[2 + ARRAY_SIZE(git_version) * 2 - 2];
136
Christina Quastf5549502015-02-24 14:27:08 +0100137/*------------------------------------------------------------------------------
138 * USB Device descriptors
139 *------------------------------------------------------------------------------*/
140
Harald Welte2fb59962016-02-28 12:34:26 +0100141#ifdef HAVE_SNIFFER
Christina Quastf5549502015-02-24 14:27:08 +0100142typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
143
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100144 /** Standard configuration descriptor. */
145 USBConfigurationDescriptor configuration;
146 USBInterfaceDescriptor sniffer;
147 USBEndpointDescriptor sniffer_dataOut;
148 USBEndpointDescriptor sniffer_dataIn;
149 USBEndpointDescriptor sniffer_interruptIn;
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100150 DFURT_IF_DESCRIPTOR_STRUCT;
Christina Quast01bbdc32015-02-24 17:38:45 +0100151} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100152
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100153static const SIMTraceDriverConfigurationDescriptorSniffer
154 configurationDescriptorSniffer = {
155 /* Standard configuration descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100156 .configuration = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100157 .bLength = sizeof(USBConfigurationDescriptor),
158 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
159 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100160 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100161 .bConfigurationValue = CFG_NUM_SNIFF,
162 .iConfiguration = SNIFFER_CONF_STR,
163 .bmAttributes = USBD_BMATTRIBUTES,
164 .bMaxPower = USBConfigurationDescriptor_POWER(100),
165 },
166 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100167 .sniffer = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100168 .bLength = sizeof(USBInterfaceDescriptor),
169 .bDescriptorType = USBGenericDescriptor_INTERFACE,
170 .bInterfaceNumber = 0,
171 .bAlternateSetting = 0,
172 .bNumEndpoints = 3,
Kévin Redona1012b12018-07-01 18:11:01 +0200173 .bInterfaceClass = USB_CLASS_PROPRIETARY,
174 .bInterfaceSubClass = SIMTRACE_SNIFFER_USB_SUBCLASS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100175 .bInterfaceProtocol = 0,
176 .iInterface = SNIFFER_CONF_STR,
177 },
178 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100179 .sniffer_dataOut = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100180 .bLength = sizeof(USBEndpointDescriptor),
181 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
182 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
183 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200184 SIMTRACE_USB_EP_CARD_DATAOUT),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100185 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200186 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100187 .bInterval = 0,
188 },
189 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100190 .sniffer_dataIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100191 .bLength = sizeof(USBEndpointDescriptor),
192 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
193 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
194 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200195 SIMTRACE_USB_EP_CARD_DATAIN),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100196 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200197 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100198 .bInterval = 0,
199 },
200 // Notification endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100201 .sniffer_interruptIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100202 .bLength = sizeof(USBEndpointDescriptor),
203 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
204 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
205 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200206 SIMTRACE_USB_EP_CARD_INT),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100207 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200208 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100209 .bInterval = 0x10,
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100210 },
211 DFURT_IF_DESCRIPTOR(1, 0),
Christina Quastf5549502015-02-24 14:27:08 +0100212};
Harald Welte2fb59962016-02-28 12:34:26 +0100213#endif /* HAVE_SNIFFER */
Christina Quastf5549502015-02-24 14:27:08 +0100214
Harald Welte2fb59962016-02-28 12:34:26 +0100215#ifdef HAVE_CCID
Harald Welteec4fe232015-11-07 18:41:25 +0100216static const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100217 // Standard USB configuration descriptor
218 {
219 .bLength = sizeof(USBConfigurationDescriptor),
220 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
221 .wTotalLength = sizeof(CCIDDriverConfigurationDescriptors),
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100222 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100223 .bConfigurationValue = CFG_NUM_CCID,
224 .iConfiguration = CCID_CONF_STR,
225 .bmAttributes = BOARD_USB_BMATTRIBUTES,
226 .bMaxPower = USBConfigurationDescriptor_POWER(100),
227 },
228 // CCID interface descriptor
229 // Table 4.3-1 Interface Descriptor
230 // Interface descriptor
231 {
232 .bLength = sizeof(USBInterfaceDescriptor),
233 .bDescriptorType = USBGenericDescriptor_INTERFACE,
234 .bInterfaceNumber = 0,
235 .bAlternateSetting = 0,
236 .bNumEndpoints = 3,
237 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
238 .bInterfaceSubClass = 0,
239 .bInterfaceProtocol = 0,
240 .iInterface = CCID_CONF_STR,
241 },
242 {
243 .bLength = sizeof(CCIDDescriptor),
244 .bDescriptorType = CCID_DECRIPTOR_TYPE,
245 .bcdCCID = CCID1_10, // CCID version
246 .bMaxSlotIndex = 0, // 1 slot
247 .bVoltageSupport = VOLTS_3_0,
248 .dwProtocols = (1 << PROTOCOL_TO),
249 .dwDefaultClock = 3580,
250 .dwMaximumClock = 3580,
251 .bNumClockSupported = 0,
252 .dwDataRate = 9600,
253 .dwMaxDataRate = 9600,
254 .bNumDataRatesSupported = 0,
255 .dwMaxIFSD = 0xfe,
256 .dwSynchProtocols = 0,
257 .dwMechanical = 0,
258 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
259 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
260 CCID_FEATURES_EXC_TPDU,
261 .dwMaxCCIDMessageLength = 271, /* For extended APDU
262 level the value shall
263 be between 261 + 10 */
264 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
265 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
266 .wLcdLayout = 0, // wLcdLayout: no LCD
267 .bPINSupport = 0, // bPINSupport: No PIN
268 .bMaxCCIDBusySlots = 1,
269 },
270 // Bulk-OUT endpoint descriptor
271 {
272 .bLength = sizeof(USBEndpointDescriptor),
273 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
274 .bEndpointAddress =
275 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
276 CCID_EPT_DATA_OUT),
277 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200278 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100279 .bInterval = 0x00,
280 },
281 // Bulk-IN endpoint descriptor
282 {
283 .bLength = sizeof(USBEndpointDescriptor),
284 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
285 .bEndpointAddress =
286 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
287 CCID_EPT_DATA_IN),
288 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200289 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100290 .bInterval = 0x00,
291 },
292 // Notification endpoint descriptor
293 {
294 .bLength = sizeof(USBEndpointDescriptor),
295 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
296 .bEndpointAddress =
297 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
298 CCID_EPT_NOTIFICATION),
299 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200300 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100301 .bInterval = 0x10,
302 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100303 DFURT_IF_DESCRIPTOR(1, 0),
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100304};
Harald Welte2fb59962016-02-28 12:34:26 +0100305#endif /* HAVE_CCID */
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100306
Harald Welte2fb59962016-02-28 12:34:26 +0100307#ifdef HAVE_CARDEM
Christina Quast01bbdc32015-02-24 17:38:45 +0100308/* SIM card emulator */
309typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100310 /* Standard configuration descriptor. */
311 USBConfigurationDescriptor configuration;
312 USBInterfaceDescriptor phone;
313 USBEndpointDescriptor phone_dataOut;
314 USBEndpointDescriptor phone_dataIn;
315 USBEndpointDescriptor phone_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100316#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100317 USBInterfaceDescriptor usim2;
318 USBEndpointDescriptor usim2_dataOut;
319 USBEndpointDescriptor usim2_dataIn;
320 USBEndpointDescriptor usim2_interruptIn;
Harald Welte57b3a252016-03-02 15:20:27 +0100321#endif
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100322 DFURT_IF_DESCRIPTOR_STRUCT;
Christina Quast01bbdc32015-02-24 17:38:45 +0100323} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
324
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100325static const SIMTraceDriverConfigurationDescriptorPhone
326 configurationDescriptorPhone = {
327 /* Standard configuration descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100328 .configuration = {
329 .bLength = sizeof(USBConfigurationDescriptor),
330 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
331 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorPhone),
Harald Welte57b3a252016-03-02 15:20:27 +0100332#ifdef CARDEMU_SECOND_UART
Harald Welte495a67d2017-03-06 09:55:37 +0100333 .bNumInterfaces = 2+DFURT_NUM_IF,
Harald Welte57b3a252016-03-02 15:20:27 +0100334#else
Harald Weltef2315412017-11-28 19:16:10 +0100335 .bNumInterfaces = 1+DFURT_NUM_IF,
Harald Welte57b3a252016-03-02 15:20:27 +0100336#endif
Harald Welte495a67d2017-03-06 09:55:37 +0100337 .bConfigurationValue = CFG_NUM_PHONE,
338 .iConfiguration = PHONE_CONF_STR,
339 .bmAttributes = USBD_BMATTRIBUTES,
340 .bMaxPower = USBConfigurationDescriptor_POWER(100)
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100341 },
342 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100343 .phone = {
344 .bLength = sizeof(USBInterfaceDescriptor),
345 .bDescriptorType = USBGenericDescriptor_INTERFACE,
346 .bInterfaceNumber = 0,
347 .bAlternateSetting = 0,
348 .bNumEndpoints = 3,
Kévin Redona1012b12018-07-01 18:11:01 +0200349 .bInterfaceClass = USB_CLASS_PROPRIETARY,
350 .bInterfaceSubClass = SIMTRACE_CARDEM_USB_SUBCLASS,
Harald Welte495a67d2017-03-06 09:55:37 +0100351 .bInterfaceProtocol = 0,
352 .iInterface = CARDEM_USIM1_INTF_STR,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100353 },
354 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100355 .phone_dataOut = {
356 .bLength = sizeof(USBEndpointDescriptor),
357 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
358 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
359 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200360 SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT),
Harald Welte495a67d2017-03-06 09:55:37 +0100361 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200362 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100363 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100364 },
365 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100366 .phone_dataIn = {
367 .bLength = sizeof(USBEndpointDescriptor),
368 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
369 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
370 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200371 SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN),
Harald Welte495a67d2017-03-06 09:55:37 +0100372 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200373 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100374 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100375 },
376 /* Notification endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100377 .phone_interruptIn = {
378 .bLength = sizeof(USBEndpointDescriptor),
379 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
380 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
381 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200382 SIMTRACE_CARDEM_USB_EP_USIM1_INT),
Harald Welte495a67d2017-03-06 09:55:37 +0100383 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200384 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100385 .bInterval = 0x10
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100386 },
Harald Welte57b3a252016-03-02 15:20:27 +0100387#ifdef CARDEMU_SECOND_UART
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100388 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100389 .usim2 = {
390 .bLength = sizeof(USBInterfaceDescriptor),
391 .bDescriptorType = USBGenericDescriptor_INTERFACE,
392 .bInterfaceNumber = 1,
393 .bAlternateSetting = 0,
394 .bNumEndpoints = 3,
Kévin Redona1012b12018-07-01 18:11:01 +0200395 .bInterfaceClass = USB_CLASS_PROPRIETARY,
396 .bInterfaceSubClass = SIMTRACE_CARDEM_USB_SUBCLASS,
Harald Welte495a67d2017-03-06 09:55:37 +0100397 .bInterfaceProtocol = 0,
398 .iInterface = CARDEM_USIM2_INTF_STR,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100399 },
400 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100401 .usim2_dataOut = {
402 .bLength = sizeof(USBEndpointDescriptor),
403 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
404 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
405 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200406 SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT),
Harald Welte495a67d2017-03-06 09:55:37 +0100407 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200408 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100409 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100410 }
411 ,
412 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100413 .usim2_dataIn = {
414 .bLength = sizeof(USBEndpointDescriptor),
415 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
416 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
417 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200418 SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN),
Harald Welte495a67d2017-03-06 09:55:37 +0100419 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200420 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100421 .bInterval = 0 /* Must be 0 for full-speed bulk endpoints */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100422 },
423 /* Notification endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100424 .usim2_interruptIn = {
425 .bLength = sizeof(USBEndpointDescriptor),
426 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
427 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
428 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200429 SIMTRACE_CARDEM_USB_EP_USIM2_INT),
Harald Welte495a67d2017-03-06 09:55:37 +0100430 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200431 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100432 .bInterval = 0x10,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100433 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100434 DFURT_IF_DESCRIPTOR(2, 0),
435#else
436 DFURT_IF_DESCRIPTOR(1, 0),
Harald Welte57b3a252016-03-02 15:20:27 +0100437#endif
Christina Quast01bbdc32015-02-24 17:38:45 +0100438};
Harald Welte2fb59962016-02-28 12:34:26 +0100439#endif /* HAVE_CARDEM */
Christina Quast01bbdc32015-02-24 17:38:45 +0100440
Harald Welte2fb59962016-02-28 12:34:26 +0100441#ifdef HAVE_MITM
Christina Quastf5549502015-02-24 14:27:08 +0100442typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100443 /* Standard configuration descriptor. */
444 USBConfigurationDescriptor configuration;
445 USBInterfaceDescriptor simcard;
446 /// CCID descriptor
447 CCIDDescriptor ccid;
448 /// Bulk OUT endpoint descriptor
449 USBEndpointDescriptor simcard_dataOut;
450 /// Bulk IN endpoint descriptor
451 USBEndpointDescriptor simcard_dataIn;
452 /// Interrupt OUT endpoint descriptor
453 USBEndpointDescriptor simcard_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100454
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100455 USBInterfaceDescriptor phone;
456 USBEndpointDescriptor phone_dataOut;
457 USBEndpointDescriptor phone_dataIn;
458 USBEndpointDescriptor phone_interruptIn;
Christina Quastf5549502015-02-24 14:27:08 +0100459
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100460 DFURT_IF_DESCRIPTOR_STRUCT;
461
Christina Quast01bbdc32015-02-24 17:38:45 +0100462} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100463
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100464static const SIMTraceDriverConfigurationDescriptorMITM
465 configurationDescriptorMITM = {
466 /* Standard configuration descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100467 .configuration = {
468 .bLength = sizeof(USBConfigurationDescriptor),
469 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
470 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorMITM),
471 .bNumInterfaces = 2+DFURT_NUM_IF,
472 .bConfigurationValue = CFG_NUM_MITM,
473 .iConfiguration = MITM_CONF_STR,
474 .bmAttributes = USBD_BMATTRIBUTES,
475 .bMaxPower = USBConfigurationDescriptor_POWER(100),
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100476 },
477 // CCID interface descriptor
478 // Table 4.3-1 Interface Descriptor
479 // Interface descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100480 .simcard = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100481 .bLength = sizeof(USBInterfaceDescriptor),
482 .bDescriptorType = USBGenericDescriptor_INTERFACE,
483 .bInterfaceNumber = 0,
484 .bAlternateSetting = 0,
485 .bNumEndpoints = 3,
486 .bInterfaceClass = SMART_CARD_DEVICE_CLASS,
487 .bInterfaceSubClass = 0,
488 .bInterfaceProtocol = 0,
489 .iInterface = CCID_CONF_STR,
490 },
Harald Welte495a67d2017-03-06 09:55:37 +0100491 .ccid = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100492 .bLength = sizeof(CCIDDescriptor),
493 .bDescriptorType = CCID_DECRIPTOR_TYPE,
494 .bcdCCID = CCID1_10, // CCID version
495 .bMaxSlotIndex = 0, // 1 slot
496 .bVoltageSupport = VOLTS_3_0,
497 .dwProtocols = (1 << PROTOCOL_TO),
498 .dwDefaultClock = 3580,
499 .dwMaximumClock = 3580,
500 .bNumClockSupported = 0,
501 .dwDataRate = 9600,
502 .dwMaxDataRate = 9600,
503 .bNumDataRatesSupported = 0,
504 .dwMaxIFSD = 0xfe,
505 .dwSynchProtocols = 0,
506 .dwMechanical = 0,
507 .dwFeatures = CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
508 CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO |
509 CCID_FEATURES_EXC_TPDU,
510 .dwMaxCCIDMessageLength = 271, /* For extended APDU
511 level the value shall
512 be between 261 + 10 */
513 .bClassGetResponse = 0xFF, // Echoes the class of the APDU
514 .bClassEnvelope = 0xFF, // Echoes the class of the APDU
515 .wLcdLayout = 0, // wLcdLayout: no LCD
516 .bPINSupport = 0, // bPINSupport: No PIN
517 .bMaxCCIDBusySlots = 1,
518 },
519 // Bulk-OUT endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100520 .simcard_dataOut = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100521 .bLength = sizeof(USBEndpointDescriptor),
522 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
523 .bEndpointAddress =
524 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
525 CCID_EPT_DATA_OUT),
526 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200527 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100528 .bInterval = 0x00,
529 },
530 // Bulk-IN endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100531 .simcard_dataIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100532 .bLength = sizeof(USBEndpointDescriptor),
533 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
534 .bEndpointAddress =
535 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
536 CCID_EPT_DATA_IN),
537 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200538 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100539 .bInterval = 0x00,
540 },
541 // Notification endpoint descriptor
Harald Welte495a67d2017-03-06 09:55:37 +0100542 .simcard_interruptIn = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100543 .bLength = sizeof(USBEndpointDescriptor),
544 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
545 .bEndpointAddress =
546 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
547 CCID_EPT_NOTIFICATION),
548 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200549 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100550 .bInterval = 0x10,
551 },
552
553 /* Communication class interface standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100554 .phone = {
555 .bLength = sizeof(USBInterfaceDescriptor),
556 .bDescriptorType = USBGenericDescriptor_INTERFACE,
557 .bInterfaceNumber = 1,
558 .bAlternateSetting = 0,
559 .bNumEndpoints = 3,
560 .bInterfaceClass = 0xff,
Harald Weltef2315412017-11-28 19:16:10 +0100561 .bInterfaceSubClass = SIMTRACE_SUBCLASS_CARDEM,
Harald Welte495a67d2017-03-06 09:55:37 +0100562 .bInterfaceProtocol = 0,
563 .iInterface = PHONE_CONF_STR,
564 },
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100565 /* Bulk-OUT endpoint standard descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100566 .phone_dataOut = {
567 .bLength = sizeof(USBEndpointDescriptor),
568 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
569 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
570 USBEndpointDescriptor_OUT,
Kévin Redona1012b12018-07-01 18:11:01 +0200571 SIMTRACE_USB_EP_PHONE_DATAOUT),
Harald Welte495a67d2017-03-06 09:55:37 +0100572 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200573 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100574 .bInterval = 0, /* Must be 0 for full-speed bulk endpoints */
575 },
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100576 /* Bulk-IN endpoint descriptor */
Harald Welte495a67d2017-03-06 09:55:37 +0100577 .phone_dataIn = {
578 .bLength = sizeof(USBEndpointDescriptor),
579 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
580 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
581 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200582 SIMTRACE_USB_EP_PHONE_DATAIN),
Harald Welte495a67d2017-03-06 09:55:37 +0100583 .bmAttributes = USBEndpointDescriptor_BULK,
Kévin Redona1012b12018-07-01 18:11:01 +0200584 .wMaxPacketSize = USBEndpointDescriptor_MAXBULKSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100585 .bInterval = 0, /* Must be 0 for full-speed bulk endpoints */
586 },
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100587 /* Notification endpoint descriptor */
588 {
Harald Welte495a67d2017-03-06 09:55:37 +0100589 .bLength = sizeof(USBEndpointDescriptor),
590 .bDescriptorType = USBGenericDescriptor_ENDPOINT,
591 .bEndpointAddress = USBEndpointDescriptor_ADDRESS(
592 USBEndpointDescriptor_IN,
Kévin Redona1012b12018-07-01 18:11:01 +0200593 SIMTRACE_USB_EP_PHONE_INT),
Harald Welte495a67d2017-03-06 09:55:37 +0100594 .bmAttributes = USBEndpointDescriptor_INTERRUPT,
Kévin Redona1012b12018-07-01 18:11:01 +0200595 .wMaxPacketSize = USBEndpointDescriptor_MAXINTERRUPTSIZE_FS,
Harald Welte495a67d2017-03-06 09:55:37 +0100596 .bInterval = 0x10
597 },
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100598 DFURT_IF_DESCRIPTOR(2, 0),
Christina Quastf5549502015-02-24 14:27:08 +0100599};
Harald Welte2fb59962016-02-28 12:34:26 +0100600#endif /* HAVE_CARDEM */
601
Kévin Redone0265462019-08-06 15:41:31 +0200602/* USB descriptor just to show the version */
603typedef struct _SIMTraceDriverConfigurationDescriptorVersion {
604 /** Standard configuration descriptor. */
605 USBConfigurationDescriptor configuration;
Kévin Redonede87e02019-08-13 17:03:11 +0200606 USBInterfaceDescriptor version;
Kévin Redone0265462019-08-06 15:41:31 +0200607} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorVersion;
608
609static const SIMTraceDriverConfigurationDescriptorVersion
610 configurationDescriptorVersion = {
Kévin Redonede87e02019-08-13 17:03:11 +0200611 /* Standard configuration descriptor for the interface descriptor*/
Kévin Redone0265462019-08-06 15:41:31 +0200612 .configuration = {
613 .bLength = sizeof(USBConfigurationDescriptor),
614 .bDescriptorType = USBGenericDescriptor_CONFIGURATION,
615 .wTotalLength = sizeof(SIMTraceDriverConfigurationDescriptorVersion),
Kévin Redonede87e02019-08-13 17:03:11 +0200616 .bNumInterfaces = 1,
Kévin Redone0265462019-08-06 15:41:31 +0200617 .bConfigurationValue = CFG_NUM_VERSION,
Kévin Redonede87e02019-08-13 17:03:11 +0200618 .iConfiguration = VERSION_CONF_STR,
Kévin Redone0265462019-08-06 15:41:31 +0200619 .bmAttributes = USBD_BMATTRIBUTES,
620 .bMaxPower = USBConfigurationDescriptor_POWER(100),
621 },
Kévin Redonede87e02019-08-13 17:03:11 +0200622 /* Interface standard descriptor just holding the version information */
623 .version = {
624 .bLength = sizeof(USBInterfaceDescriptor),
625 .bDescriptorType = USBGenericDescriptor_INTERFACE,
626 .bInterfaceNumber = 0,
627 .bAlternateSetting = 0,
628 .bNumEndpoints = 0,
629 .bInterfaceClass = USB_CLASS_PROPRIETARY,
630 .bInterfaceSubClass = 0xff,
631 .bInterfaceProtocol = 0,
632 .iInterface = VERSION_STR,
633 },
Kévin Redone0265462019-08-06 15:41:31 +0200634};
635
Harald Welte2fb59962016-02-28 12:34:26 +0100636const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
637#ifdef HAVE_SNIFFER
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100638 &configurationDescriptorSniffer.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100639#endif
640#ifdef HAVE_CCID
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100641 &configurationDescriptorCCID.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100642#endif
643#ifdef HAVE_CARDEM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100644 &configurationDescriptorPhone.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100645#endif
646#ifdef HAVE_MITM
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100647 &configurationDescriptorMITM.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100648#endif
Kévin Redone0265462019-08-06 15:41:31 +0200649 &configurationDescriptorVersion.configuration,
Harald Welte2fb59962016-02-28 12:34:26 +0100650};
Christina Quastf5549502015-02-24 14:27:08 +0100651
Christina Quastf5549502015-02-24 14:27:08 +0100652/** Standard USB device descriptor for the CDC serial driver */
653const USBDeviceDescriptor deviceDescriptor = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100654 .bLength = sizeof(USBDeviceDescriptor),
655 .bDescriptorType = USBGenericDescriptor_DEVICE,
656 .bcdUSB = USBDeviceDescriptor_USB2_00,
Harald Welteeab7e452017-03-06 10:04:56 +0100657 .bDeviceClass = 0,
658 .bDeviceSubClass = 0,
659 .bDeviceProtocol = 0,
Kévin Redona1012b12018-07-01 18:11:01 +0200660 .bMaxPacketSize0 = 64,
Harald Weltec6e482d2017-03-05 16:24:29 +0100661 .idVendor = BOARD_USB_VENDOR_ID,
662 .idProduct = BOARD_USB_PRODUCT_ID,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100663 .bcdDevice = 2, /* Release number */
664 .iManufacturer = MANUF_STR,
665 .iProduct = PRODUCT_STRING,
Kévin Redone0265462019-08-06 15:41:31 +0200666 .iSerialNumber = SERIAL_STR,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100667 .bNumConfigurations = ARRAY_SIZE(configurationDescriptorsArr),
Christina Quast01bbdc32015-02-24 17:38:45 +0100668};
669
Christina Quastbd5b8bd2015-05-14 17:25:41 +0200670/* AT91SAM3S only supports full speed, but not high speed USB */
Harald Welteec4fe232015-11-07 18:41:25 +0100671static const USBDDriverDescriptors driverDescriptors = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100672 &deviceDescriptor,
673 (const USBConfigurationDescriptor **)&(configurationDescriptorsArr), /* first full-speed configuration descriptor */
674 0, /* No full-speed device qualifier descriptor */
675 0, /* No full-speed other speed configuration */
676 0, /* No high-speed device descriptor */
677 0, /* No high-speed configuration descriptor */
678 0, /* No high-speed device qualifier descriptor */
679 0, /* No high-speed other speed configuration descriptor */
Kévin Redone0265462019-08-06 15:41:31 +0200680 usb_strings_extended,
681 ARRAY_SIZE(usb_strings_extended),/* cnt string descriptors in list */
Christina Quastf5549502015-02-24 14:27:08 +0100682};
683
Christina Quast0ae03142015-02-25 18:43:46 +0100684/*----------------------------------------------------------------------------
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100685 * Functions
Christina Quast968b9742015-02-25 14:10:12 +0100686 *----------------------------------------------------------------------------*/
687
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100688void SIMtrace_USB_Initialize(void)
Christina Quast968b9742015-02-25 14:10:12 +0100689{
Harald Welte4b487b82019-08-08 10:18:35 +0200690 unsigned int i;
Kévin Redonf5869d42018-06-17 22:31:21 +0200691 /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */
Kévin Redonff3d8492018-08-06 17:57:20 +0200692#ifdef PIN_USB_PULLUP
Kévin Redonf5869d42018-06-17 22:31:21 +0200693 const Pin usb_dp_pullup = PIN_USB_PULLUP;
694 PIO_Configure(&usb_dp_pullup, 1);
695 PIO_Set(&usb_dp_pullup);
Kévin Redonff3d8492018-08-06 17:57:20 +0200696#endif
697 USBD_HAL_Suspend();
698 mdelay(20);
699#ifdef PIN_USB_PULLUP
Kévin Redonf5869d42018-06-17 22:31:21 +0200700 PIO_Clear(&usb_dp_pullup);
Kévin Redonff3d8492018-08-06 17:57:20 +0200701#endif
702 USBD_HAL_Activate();
Kévin Redonf5869d42018-06-17 22:31:21 +0200703
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100704 // Get std USB driver
705 USBDDriver *pUsbd = USBD_GetDriver();
Christina Quast968b9742015-02-25 14:10:12 +0100706
Kévin Redone0265462019-08-06 15:41:31 +0200707 // put device ID into USB serial number description
708 unsigned int device_id[4];
709 EEFC_ReadUniqueID(device_id);
710 char device_id_string[32 + 1];
711 snprintf(device_id_string, ARRAY_SIZE(device_id_string), "%08x%08x%08x%08x",
712 device_id[0], device_id[1], device_id[2], device_id[3]);
Harald Welte4b487b82019-08-08 10:18:35 +0200713 for (i = 0; i < ARRAY_SIZE(device_id_string) - 1; i++) {
Kévin Redone0265462019-08-06 15:41:31 +0200714 usb_string_serial[2 + 2 * i] = device_id_string[i];
715 }
716
717 // put version into USB string
718 usb_string_version[0] = USBStringDescriptor_LENGTH(ARRAY_SIZE(git_version) - 1);
719 usb_string_version[1] = USBGenericDescriptor_STRING;
Harald Welte4b487b82019-08-08 10:18:35 +0200720 for (i = 0; i < ARRAY_SIZE(git_version) - 1; i++) {
Kévin Redone0265462019-08-06 15:41:31 +0200721 usb_string_version[2 + i * 2 + 0] = git_version[i];
722 usb_string_version[2 + i * 2 + 1] = 0;
723 }
724
725 // fill extended USB strings
Harald Welte4b487b82019-08-08 10:18:35 +0200726 for (i = 0; i < ARRAY_SIZE(usb_strings) && i < ARRAY_SIZE(usb_strings_extended); i++) {
Kévin Redone0265462019-08-06 15:41:31 +0200727 usb_strings_extended[i] = usb_strings[i];
728 }
729 usb_strings_extended[SERIAL_STR] = usb_string_serial;
Kévin Redonede87e02019-08-13 17:03:11 +0200730 usb_strings_extended[VERSION_CONF_STR] = usb_string_version_conf;
Kévin Redone0265462019-08-06 15:41:31 +0200731 usb_strings_extended[VERSION_STR] = usb_string_version;
732
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100733 // Initialize standard USB driver
734 USBDDriver_Initialize(pUsbd, &driverDescriptors, 0); // Multiple interface settings not supported
735 USBD_Init();
736 USBD_Connect();
Christina Quast968b9742015-02-25 14:10:12 +0100737
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100738 NVIC_EnableIRQ(UDP_IRQn);
Christina Quast968b9742015-02-25 14:10:12 +0100739}