-
Notifications
You must be signed in to change notification settings - Fork 2
/
usb_device_descriptor.c
627 lines (584 loc) · 26.1 KB
/
usb_device_descriptor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 - 2019 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_string_descriptor.h"
#include "usb_device_class.h"
#include "usb_device_cdc_acm.h"
#include "usb_device_descriptor.h"
#if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
usb_device_endpoint_struct_t g_UsbMtpEndpoints[USB_MTP_ENDPOINT_COUNT] =
{
{
USB_MTP_BULK_IN_ENDPOINT | (USB_IN << 7U), USB_ENDPOINT_BULK, HS_MTP_BULK_IN_PACKET_SIZE, 0U,
},
{
USB_MTP_BULK_OUT_ENDPOINT | (USB_OUT << 7U), USB_ENDPOINT_BULK, HS_MTP_BULK_OUT_PACKET_SIZE, 0U,
},
{
USB_MTP_INTR_IN_ENDPOINT | (USB_IN << 7U), USB_ENDPOINT_INTERRUPT, HS_MTP_INTR_IN_PACKET_SIZE, 0U,
}
};
usb_device_interface_struct_t g_UsbDeviceMtpInterface[] =
{
{
0,
{
USB_MTP_ENDPOINT_COUNT, g_UsbMtpEndpoints,
},
NULL
}
};
usb_device_interfaces_struct_t g_UsbDeviceMtpInterfaces[USB_MTP_INTERFACE_COUNT] = {{
USB_MTP_DEVICE_CLASS,
USB_MTP_DEVICE_SUBCLASS,
USB_MTP_DEVICE_PROTOCOL,
USB_MTP_INTERFACE_INDEX,
g_UsbDeviceMtpInterface,
sizeof(g_UsbDeviceMtpInterface) / sizeof(usb_device_interface_struct_t),
}};
usb_device_interface_list_t g_UsbDeviceMtpInterfaceList[USB_DEVICE_CONFIGURATION_COUNT] = {
{
USB_MTP_INTERFACE_COUNT,
g_UsbDeviceMtpInterfaces,
},
};
/* Define class information for MTP */
usb_device_class_struct_t g_MtpClass = {
g_UsbDeviceMtpInterfaceList,
kUSB_DeviceClassTypeMtp,
USB_DEVICE_CONFIGURATION_COUNT,
};
#endif // #if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
/* cdc virtual com information */
/* Define endpoint for communication class */
usb_device_endpoint_struct_t g_cdcVcomCicEndpoints[USB_CDC_VCOM_CIC_ENDPOINT_COUNT] = {
{
USB_CDC_VCOM_CIC_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
USB_ENDPOINT_INTERRUPT,
FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
},
};
/* Define endpoint for data class */
usb_device_endpoint_struct_t g_cdcVcomDicEndpoints[USB_CDC_VCOM_DIC_ENDPOINT_COUNT] = {
{
USB_CDC_VCOM_DIC_BULK_IN_ENDPOINT | (USB_IN << 7U),
USB_ENDPOINT_BULK,
FS_CDC_VCOM_BULK_IN_PACKET_SIZE,
0U,
},
{
USB_CDC_VCOM_DIC_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
USB_ENDPOINT_BULK,
FS_CDC_VCOM_BULK_OUT_PACKET_SIZE,
0U,
},
};
/* Define interface for communication class */
usb_device_interface_struct_t g_cdcVcomCicInterface[] = {{0,
{
USB_CDC_VCOM_CIC_ENDPOINT_COUNT,
g_cdcVcomCicEndpoints,
},
NULL}};
/* Define interface for data class */
usb_device_interface_struct_t g_cdcVcomDicInterface[] = {{0,
{
USB_CDC_VCOM_DIC_ENDPOINT_COUNT,
g_cdcVcomDicEndpoints,
},
NULL}};
/* Define interfaces for virtual com */
usb_device_interfaces_struct_t g_cdcVcomInterfaces[USB_CDC_VCOM_INTERFACE_COUNT] = {
{USB_CDC_VCOM_CIC_CLASS, USB_CDC_VCOM_CIC_SUBCLASS, USB_CDC_VCOM_CIC_PROTOCOL, USB_CDC_VCOM_CIC_INTERFACE_INDEX,
g_cdcVcomCicInterface, sizeof(g_cdcVcomCicInterface) / sizeof(usb_device_interface_struct_t)},
{USB_CDC_VCOM_DIC_CLASS, USB_CDC_VCOM_DIC_SUBCLASS, USB_CDC_VCOM_DIC_PROTOCOL, USB_CDC_VCOM_DIC_INTERFACE_INDEX,
g_cdcVcomDicInterface, sizeof(g_cdcVcomDicInterface) / sizeof(usb_device_interface_struct_t)},
};
/* Define configurations for virtual com */
usb_device_interface_list_t g_UsbDeviceCdcVcomInterfaceList[USB_DEVICE_CONFIGURATION_COUNT] = {
{
USB_CDC_VCOM_INTERFACE_COUNT,
g_cdcVcomInterfaces,
},
};
/* Define class information for virtual com */
usb_device_class_struct_t g_UsbDeviceCdcVcomConfig = {
g_UsbDeviceCdcVcomInterfaceList,
kUSB_DeviceClassTypeCdc,
USB_DEVICE_CONFIGURATION_COUNT,
};
/* Define device descriptor */
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceDescriptor[] = {
/* Size of this descriptor in bytes */
USB_DESCRIPTOR_LENGTH_DEVICE,
/* DEVICE Descriptor Type */
USB_DESCRIPTOR_TYPE_DEVICE,
/* USB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). */
USB_SHORT_GET_LOW(USB_DEVICE_SPECIFIC_BCD_VERSION),
USB_SHORT_GET_HIGH(USB_DEVICE_SPECIFIC_BCD_VERSION),
/* Class code (assigned by the USB-IF). */
USB_DEVICE_CLASS,
/* Subclass code (assigned by the USB-IF). */
USB_DEVICE_SUBCLASS,
/* Protocol code (assigned by the USB-IF). */
USB_DEVICE_PROTOCOL,
/* Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid) */
USB_CONTROL_MAX_PACKET_SIZE,
/* Vendor ID (assigned by the USB-IF) */
USB_SHORT_GET_LOW(USB_DEVICE_VENDOR_ID),
USB_SHORT_GET_HIGH(USB_DEVICE_VENDOR_ID),
/* Product ID (assigned by the manufacturer) */
USB_SHORT_GET_LOW(USB_DEVICE_PRODUCT_ID),
USB_SHORT_GET_HIGH(USB_DEVICE_PRODUCT_ID),
/* Device release number in binary-coded decimal */
USB_SHORT_GET_LOW(USB_DEVICE_DEMO_BCD_VERSION),
USB_SHORT_GET_HIGH(USB_DEVICE_DEMO_BCD_VERSION),
/* Index of string descriptor describing manufacturer */
USB_STRING_MANUFACTURER,
/* Index of string descriptor describing product */
USB_STRING_PRODUCT,
/* Index of string descriptor describing the device's serial number */
USB_STRING_SERIAL_NUMBER,
/* Number of possible configurations */
USB_DEVICE_CONFIGURATION_COUNT,
};
/* Define configuration descriptor */
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceConfigurationDescriptor[] = {
/* Configuration 1 Descriptor */
USB_DESCRIPTOR_LENGTH_CONFIGURE,
USB_DESCRIPTOR_TYPE_CONFIGURE,
/* Total length of data returned for this configuration. */
USB_SHORT_GET_LOW(USB_DECRIPTOR_CONFIGURATION_LENGTH),
USB_SHORT_GET_HIGH(USB_DECRIPTOR_CONFIGURATION_LENGTH),
USB_INTERFACE_COUNT,
/* Value to use as an argument to the SetConfiguration() request to select this configuration */
USB_COMPOSITE_CONFIGURE_INDEX,
/* Index of string descriptor describing this configuration */
USB_STRING_DEF_CONFIGURATION,
/* Configuration characteristics D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4...0: Reserved
(reset to zero) */
(USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK) |
(USB_DEVICE_CONFIG_SELF_POWER << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT) |
(USB_DEVICE_CONFIG_REMOTE_WAKEUP << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT),
/* Maximum power consumption of the USB * device from the bus in this specific * configuration when the device is
fully * operational. Expressed in 2 mA units * (i.e., 50 = 100 mA). */
USB_DEVICE_MAX_POWER,
#if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
/***** MTP Device Class *****/
/* Interface Descriptors */
USB_DESCRIPTOR_LENGTH_INTERFACE,
USB_DESCRIPTOR_TYPE_INTERFACE,
USB_MTP_INTERFACE_INDEX,
0x00,
USB_MTP_ENDPOINT_COUNT,
USB_MTP_DEVICE_CLASS,
USB_MTP_DEVICE_SUBCLASS,
USB_MTP_DEVICE_PROTOCOL,
USB_STRING_MTP_INTERFACE,
/* Endpoint Descriptors */
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_MTP_BULK_IN_ENDPOINT | (USB_IN << 7U),
USB_ENDPOINT_BULK,
USB_SHORT_GET_LOW(HS_MTP_BULK_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(HS_MTP_BULK_IN_PACKET_SIZE),
0x00,
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_MTP_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
USB_ENDPOINT_BULK,
USB_SHORT_GET_LOW(HS_MTP_BULK_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(HS_MTP_BULK_IN_PACKET_SIZE),
0x00,
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_MTP_INTR_IN_ENDPOINT | (USB_IN << 7U),
USB_ENDPOINT_INTERRUPT,
USB_SHORT_GET_LOW(HS_MTP_INTR_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(HS_MTP_INTR_IN_PACKET_SIZE),
FS_MTP_INTR_IN_INTERVAL,
#endif // #if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
/* Interface Association Descriptor (IAD) */
/* Size of this descriptor in bytes */
USB_IAD_DESC_SIZE,
/* INTERFACE_ASSOCIATION Descriptor Type */
USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION,
/* The first interface number associated with this function */
USB_CDC_VCOM_CIC_INTERFACE_INDEX,
/* The number of contiguous interfaces associated with this function */
0x02,
/* The function belongs to the Communication Device/Interface Class */
USB_CDC_VCOM_CIC_CLASS,
USB_CDC_VCOM_CIC_SUBCLASS,
/* The function uses the No class specific protocol required Protocol */
USB_CDC_VCOM_CIC_PROTOCOL,
/* The Function string descriptor index */
USB_STRING_CDC_ACM_CLASS,
/***** CDC ACM Devic Class *****/
/* Interface Descriptor */
USB_DESCRIPTOR_LENGTH_INTERFACE,
USB_DESCRIPTOR_TYPE_INTERFACE,
USB_CDC_VCOM_CIC_INTERFACE_INDEX,
0x00,
USB_CDC_VCOM_CIC_ENDPOINT_COUNT,
USB_CDC_VCOM_CIC_CLASS,
USB_CDC_VCOM_CIC_SUBCLASS,
USB_CDC_VCOM_CIC_PROTOCOL,
USB_STRING_CDC_ACM_CIC,
/* CDC Class-Specific descriptor */
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
USB_CDC_HEADER_FUNC_DESC,
0x10,
0x01, /* USB Class Definitions for Communications the Communication specification version 1.10 */
USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
USB_CDC_CALL_MANAGEMENT_FUNC_DESC,
0x01, /*Bit 0: Whether device handle call management itself 1, Bit 1: Whether device can send/receive call
management information over a Data Class Interface 0 */
0x01, /* Indicates multiplexed commands are handled via data interface */
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT,
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE,
USB_CDC_ABSTRACT_CONTROL_FUNC_DESC,
0x06, /* Bit 0: Whether device supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and
Get_Comm_Feature 0, Bit 1: Whether device supports the request combination of Set_Line_Coding,
Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State 1, Bit ... */
/* Union Communication Descriptor */
USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC,
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE,
USB_CDC_UNION_FUNC_DESC,
USB_CDC_VCOM_CIC_INTERFACE_INDEX,
USB_CDC_VCOM_DIC_INTERFACE_INDEX,
/*Notification Endpoint descriptor */
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_CDC_VCOM_CIC_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
USB_ENDPOINT_INTERRUPT,
USB_SHORT_GET_LOW(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE),
FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
/* Data Interface Descriptor */
USB_DESCRIPTOR_LENGTH_INTERFACE,
USB_DESCRIPTOR_TYPE_INTERFACE,
USB_CDC_VCOM_DIC_INTERFACE_INDEX,
0x00,
USB_CDC_VCOM_DIC_ENDPOINT_COUNT,
USB_CDC_VCOM_DIC_CLASS,
USB_CDC_VCOM_DIC_SUBCLASS,
USB_CDC_VCOM_DIC_PROTOCOL,
USB_STRING_CDC_ACM_DIC,
/* Endpoint Descriptors */
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_CDC_VCOM_DIC_BULK_IN_ENDPOINT | (USB_IN << 7U),
USB_ENDPOINT_BULK,
USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_IN_PACKET_SIZE),
0x00, /* The polling interval value is every 0 Frames */
/*Bulk OUT Endpoint descriptor */
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_CDC_VCOM_DIC_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
USB_ENDPOINT_BULK,
USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE),
USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE),
0x00, /* The polling interval value is every 0 Frames */
};
#if (defined(USB_DEVICE_CONFIG_CV_TEST) && (USB_DEVICE_CONFIG_CV_TEST > 0U))
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceQualifierDescriptor[] = {
USB_DESCRIPTOR_LENGTH_DEVICE_QUALITIER, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_DEVICE_QUALITIER, /* DEVICE Descriptor Type */
USB_SHORT_GET_LOW(USB_DEVICE_SPECIFIC_BCD_VERSION),
USB_SHORT_GET_HIGH(USB_DEVICE_SPECIFIC_BCD_VERSION), /* USB Specification Release Number in
Binary-Coded Decimal (i.e., 2.10 is 210H). */
USB_DEVICE_CLASS, /* Class code (assigned by the USB-IF). */
USB_DEVICE_SUBCLASS, /* Subclass code (assigned by the USB-IF). */
USB_DEVICE_PROTOCOL, /* Protocol code (assigned by the USB-IF). */
USB_CONTROL_MAX_PACKET_SIZE, /* Maximum packet size for endpoint zero
(only 8, 16, 32, or 64 are valid) */
0x00U, /* Number of Other-speed Configurations */
0x00U, /* Reserved for future use, must be zero */
};
#endif
/*!
* @brief USB device get device descriptor function.
*
* This function gets the device descriptor of the USB device.
*
* @param handle The USB device handle.
* @param deviceDescriptor The pointer to the device descriptor structure.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceGetDeviceDescriptor(usb_device_handle handle,
usb_device_get_device_descriptor_struct_t *deviceDescriptor)
{
deviceDescriptor->buffer = g_UsbDeviceDescriptor;
deviceDescriptor->length = USB_DESCRIPTOR_LENGTH_DEVICE;
return kStatus_USB_Success;
}
#if (defined(USB_DEVICE_CONFIG_CV_TEST) && (USB_DEVICE_CONFIG_CV_TEST > 0U))
/* Get device qualifier descriptor request */
usb_status_t USB_DeviceGetDeviceQualifierDescriptor(
usb_device_handle handle, usb_device_get_device_qualifier_descriptor_struct_t *deviceQualifierDescriptor)
{
deviceQualifierDescriptor->buffer = g_UsbDeviceQualifierDescriptor;
deviceQualifierDescriptor->length = USB_DESCRIPTOR_LENGTH_DEVICE_QUALITIER;
return kStatus_USB_Success;
}
#endif
/*!
* @brief USB device get configuration descriptor function.
*
* This function gets the configuration descriptor of the USB device.
*
* @param handle The USB device handle.
* @param configurationDescriptor The pointer to the configuration descriptor structure.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceGetConfigurationDescriptor(
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configurationDescriptor)
{
if (USB_COMPOSITE_CONFIGURE_INDEX > configurationDescriptor->configuration)
{
configurationDescriptor->buffer = g_UsbDeviceConfigurationDescriptor;
configurationDescriptor->length = USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL;
return kStatus_USB_Success;
}
return kStatus_USB_InvalidRequest;
}
/*!
* @brief USB device get string descriptor function.
*
* This function gets the string descriptor of the USB device.
*
* @param handle The USB device handle.
* @param stringDescriptor Pointer to the string descriptor structure.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceGetStringDescriptor(usb_device_handle handle,
usb_device_get_string_descriptor_struct_t *stringDescriptor)
{
const uint16_t length = USB_GetStringDescriptor(USB_StringDescriptorBuffer, stringDescriptor->stringIndex);
if (length > 0) {
stringDescriptor->buffer = USB_StringDescriptorBuffer;
stringDescriptor->length = length;
return kStatus_USB_Success;
}
return kStatus_USB_Error;
}
/*!
* @brief USB device set speed function.
*
* This function sets the speed of the USB device.
*
* Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
* current speed.
* As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
* KHCI.
* When the EHCI is enabled, the application needs to call this function to update device by using current speed.
* The updated information includes endpoint max packet size, endpoint interval, etc.
*
* @param handle The USB device handle.
* @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed)
{
usb_descriptor_union_t *ptr1;
usb_descriptor_union_t *ptr2;
ptr1 = (usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[0]);
ptr2 = (usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL - 1]);
while (ptr1 < ptr2)
{
if (ptr1->common.bDescriptorType == USB_DESCRIPTOR_TYPE_ENDPOINT)
{
if (USB_SPEED_HIGH == speed)
{
if ((USB_CDC_VCOM_CIC_INTERRUPT_IN_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN))
{
ptr1->endpoint.bInterval = HS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
ptr1->endpoint.wMaxPacketSize);
}
else if ((USB_CDC_VCOM_DIC_BULK_IN_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
else if ((USB_CDC_VCOM_DIC_BULK_OUT_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
#if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
else if ((USB_MTP_BULK_IN_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_MTP_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
else if ((USB_MTP_BULK_OUT_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_MTP_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
#endif
else
{
}
}
else
{
if ((USB_CDC_VCOM_CIC_INTERRUPT_IN_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN))
{
ptr1->endpoint.bInterval = FS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
ptr1->endpoint.wMaxPacketSize);
}
else if ((USB_CDC_VCOM_DIC_BULK_IN_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
else if ((USB_CDC_VCOM_DIC_BULK_OUT_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
#if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
else if ((USB_MTP_BULK_IN_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_MTP_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
else if ((USB_MTP_BULK_OUT_ENDPOINT ==
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)) &&
((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT))
{
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_MTP_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
}
#endif
else
{
}
}
}
ptr1 = (usb_descriptor_union_t *)((uint8_t *)ptr1 + ptr1->common.bLength);
}
for (int i = 0; i < USB_CDC_VCOM_CIC_ENDPOINT_COUNT; i++)
{
if (USB_SPEED_HIGH == speed)
{
g_cdcVcomCicEndpoints[i].maxPacketSize = HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE;
g_cdcVcomCicEndpoints[i].interval = HS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
}
else
{
g_cdcVcomCicEndpoints[i].maxPacketSize = FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE;
g_cdcVcomCicEndpoints[i].interval = FS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
}
}
for (int i = 0; i < USB_CDC_VCOM_DIC_ENDPOINT_COUNT; i++)
{
if (USB_SPEED_HIGH == speed)
{
if (g_cdcVcomDicEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
{
g_cdcVcomDicEndpoints[i].maxPacketSize = HS_CDC_VCOM_BULK_IN_PACKET_SIZE;
}
else
{
g_cdcVcomDicEndpoints[i].maxPacketSize = HS_CDC_VCOM_BULK_OUT_PACKET_SIZE;
}
}
else
{
if (g_cdcVcomDicEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
{
g_cdcVcomDicEndpoints[i].maxPacketSize = FS_CDC_VCOM_BULK_IN_PACKET_SIZE;
}
else
{
g_cdcVcomDicEndpoints[i].maxPacketSize = FS_CDC_VCOM_BULK_OUT_PACKET_SIZE;
}
}
}
#if defined (USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
for (int i = 0; i < USB_MTP_ENDPOINT_COUNT; i++)
{
if (USB_SPEED_HIGH == speed)
{
if (g_UsbMtpEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
{
g_UsbMtpEndpoints[i].maxPacketSize = HS_MTP_BULK_IN_PACKET_SIZE;
}
else
{
g_UsbMtpEndpoints[i].maxPacketSize = HS_MTP_BULK_OUT_PACKET_SIZE;
}
}
else
{
if (g_UsbMtpEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
{
g_UsbMtpEndpoints[i].maxPacketSize = FS_MTP_BULK_IN_PACKET_SIZE;
}
else
{
g_UsbMtpEndpoints[i].maxPacketSize = FS_MTP_BULK_OUT_PACKET_SIZE;
}
}
}
#endif
return kStatus_USB_Success;
}
uint16_t USB_DeviceSetSerialNumberString(const char *serialNumberAscii)
{
return USB_SetDescriptorString(serialNumberAscii, USB_STRING_SERIAL_NUMBER);
}
uint16_t USB_DeviceSetBcdVersion(usb_device_handle *handle, const uint16_t version)
{
// Indexes from struct g_UsbDeviceDescriptor
const uint16_t bcdDeviceLowIndex = 12;
const uint16_t bcdDeviceHighIndex = 13;
usb_device_get_device_descriptor_struct_t device_descriptor;
usb_status_t status = USB_DeviceGetDeviceDescriptor(&handle, &device_descriptor);
device_descriptor.buffer[bcdDeviceLowIndex] = USB_SHORT_GET_LOW(version);
device_descriptor.buffer[bcdDeviceHighIndex] = USB_SHORT_GET_HIGH(version);
return status;
}