2 * @file usbd_descriptor.c
4 * @brief USB device descriptor configuration
8 * Copyright (C) 2023 Geehy Semiconductor
10 * You may not use this file except in compliance with the
11 * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
13 * The program is only for reference, which is distributed in the hope
14 * that it will be useful and instructional for customers to develop
15 * their software. Unless required by applicable law or agreed to in
16 * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
17 * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
19 * and limitations under the License.
22 /* Includes ***************************************************************/
23 #include "usbd_msc_descriptor.h"
25 /* Private includes *******************************************************/
31 /* Private macro **********************************************************/
32 #define USBD_GEEHY_VID 0x314B
33 #define USBD_FS_PID 0x5720
34 #define USBD_LANGID_STR 0x0409
35 #define USBD_MANUFACTURER_STR "Geehy"
36 #define USBD_PRODUCT_HS_STR "Betaflight FC Mass Storage (HS Mode)"
37 #define USBD_PRODUCT_FS_STR "Betaflight FC Mass Storage (FS Mode)"
38 #define USBD_CONFIGURATION_HS_STR "MSC Config"
39 #define USBD_CONFIGURATION_FS_STR "MSC Config"
40 #define USBD_INTERFACE_HS_STR "MSC Interface"
41 #define USBD_INTERFACE_FS_STR "MSC Interface"
43 /* Private function prototypes ********************************************/
44 static USBD_DESC_INFO_T
USBD_MSC_DeviceDescHandler(uint8_t usbSpeed
);
45 static USBD_DESC_INFO_T
USBD_MSC_ConfigDescHandler(uint8_t usbSpeed
);
46 static USBD_DESC_INFO_T
USBD_MSC_ConfigStrDescHandler(uint8_t usbSpeed
);
47 static USBD_DESC_INFO_T
USBD_MSC_InterfaceStrDescHandler(uint8_t usbSpeed
);
48 static USBD_DESC_INFO_T
USBD_MSC_LangIdStrDescHandler(uint8_t usbSpeed
);
49 static USBD_DESC_INFO_T
USBD_MSC_ManufacturerStrDescHandler(uint8_t usbSpeed
);
50 static USBD_DESC_INFO_T
USBD_MSC_ProductStrDescHandler(uint8_t usbSpeed
);
51 static USBD_DESC_INFO_T
USBD_MSC_SerialStrDescHandler(uint8_t usbSpeed
);
53 static USBD_DESC_INFO_T
USBD_MSC_BosDescHandler(uint8_t usbSpeed
);
55 static USBD_DESC_INFO_T
USBD_OtherSpeedConfigDescHandler(uint8_t usbSpeed
);
56 static USBD_DESC_INFO_T
USBD_DevQualifierDescHandler(uint8_t usbSpeed
);
58 static void IntToUnicode (uint32_t value
, uint8_t *pbuf
, uint8_t len
);
59 static void Get_SerialNum(void);
61 /* Private typedef ********************************************************/
63 /* USB device descripotr handler */
64 USBD_DESC_T USBD_DESC_MSC
=
67 USBD_MSC_DeviceDescHandler
,
68 USBD_MSC_ConfigDescHandler
,
69 USBD_MSC_ConfigStrDescHandler
,
70 USBD_MSC_InterfaceStrDescHandler
,
71 USBD_MSC_LangIdStrDescHandler
,
72 USBD_MSC_ManufacturerStrDescHandler
,
73 USBD_MSC_ProductStrDescHandler
,
74 USBD_MSC_SerialStrDescHandler
,
76 USBD_MSC_BosDescHandler
,
79 USBD_OtherSpeedConfigDescHandler
,
80 USBD_DevQualifierDescHandler
,
83 /* Private variables ******************************************************/
86 * @brief Device descriptor
88 static uint8_t USBD_DeviceDesc
[USBD_DEVICE_DESCRIPTOR_SIZE
] =
96 0x01, /*<! For resume test of USBCV3.0. Only support LPM USB device */
103 /* bDeviceSubClass */
105 /* bDeviceProtocol */
108 USBD_EP0_PACKET_MAX_SIZE
,
110 USBD_GEEHY_VID
& 0xFF, USBD_GEEHY_VID
>> 8,
112 USBD_FS_PID
& 0xFF, USBD_FS_PID
>> 8,
113 /* bcdDevice = 2.00 */
115 /* Index of string descriptor describing manufacturer */
117 /* Index of string descriptor describing product */
118 USBD_DESC_STR_PRODUCT
,
119 /* Index of string descriptor describing the device serial number */
120 USBD_DESC_STR_SERIAL
,
121 /* bNumConfigurations */
122 USBD_SUP_CONFIGURATION_MAX_NUM
,
126 * @brief Configuration descriptor
128 static uint8_t USBD_ConfigDesc
[USBD_CONFIG_DESCRIPTOR_SIZE
] =
132 /* bDescriptorType */
133 USBD_DESC_CONFIGURATION
,
135 USBD_CONFIG_DESCRIPTOR_SIZE
& 0xFF,
136 USBD_CONFIG_DESCRIPTOR_SIZE
>> 8,
140 /* bConfigurationValue */
145 #if USBD_SUP_SELF_PWR
153 /* Mass Storage interface */
156 /* bDescriptorType */
158 /* bInterfaceNumber */
160 /* bAlternateSetting */
164 /* bInterfaceClass */
165 USBD_MSC_ITF_CLASS_ID
,
166 /* bInterfaceSubClass */
167 USBD_MSC_ITF_SUB_CLASS
,
168 /* bInterfaceProtocol */
169 USBD_MSC_ITF_PROTOCOL
,
173 /* Mass Storage Endpoints */
176 /* bDescriptorType: Endpoint */
178 /* bEndpointAddress */
182 /* wMaxPacketSize: */
183 USBD_MSC_FS_MP_SIZE
& 0xFF,
184 USBD_MSC_FS_MP_SIZE
>> 8,
190 /* bDescriptorType: Endpoint */
192 /* bEndpointAddress */
193 USBD_MSC_OUT_EP_ADDR
,
196 /* wMaxPacketSize: */
197 USBD_MSC_FS_MP_SIZE
& 0xFF,
198 USBD_MSC_FS_MP_SIZE
>> 8,
204 * @brief Other speed configuration descriptor
206 static uint8_t USBD_OtherSpeedCfgDesc
[USBD_CONFIG_DESCRIPTOR_SIZE
] =
210 /* bDescriptorType */
211 USBD_DESC_OTHER_SPEED
,
213 USBD_CONFIG_DESCRIPTOR_SIZE
& 0xFF,
214 USBD_CONFIG_DESCRIPTOR_SIZE
>> 8,
218 /* bConfigurationValue */
223 #if USBD_SUP_SELF_PWR
231 /* Mass Storage interface */
234 /* bDescriptorType */
236 /* bInterfaceNumber */
238 /* bAlternateSetting */
242 /* bInterfaceClass */
243 USBD_MSC_ITF_CLASS_ID
,
244 /* bInterfaceSubClass */
245 USBD_MSC_ITF_SUB_CLASS
,
246 /* bInterfaceProtocol */
247 USBD_MSC_ITF_PROTOCOL
,
251 /* Mass Storage Endpoints */
254 /* bDescriptorType: Endpoint */
256 /* bEndpointAddress */
260 /* wMaxPacketSize: */
261 USBD_MSC_FS_MP_SIZE
& 0xFF,
262 USBD_MSC_FS_MP_SIZE
>> 8,
268 /* bDescriptorType: Endpoint */
270 /* bEndpointAddress */
271 USBD_MSC_OUT_EP_ADDR
,
274 /* wMaxPacketSize: */
275 USBD_MSC_FS_MP_SIZE
& 0xFF,
276 USBD_MSC_FS_MP_SIZE
>> 8,
283 * @brief BOS descriptor
285 static uint8_t USBD_BosDesc
[USBD_BOS_DESCRIPTOR_SIZE
] =
289 /* bDescriptorType */
296 /* Device Capability */
299 /* bDescriptorType */
300 USBD_DEVICE_CAPABILITY_TYPE
,
301 /* bDevCapabilityType */
302 USBD_20_EXTENSION_TYPE
,
304 0x02, 0x00, 0x00, 0x00,
309 * @brief Serial string descriptor
311 static uint8_t USBD_SerialStrDesc
[USBD_SERIAL_STRING_SIZE
] =
314 USBD_SERIAL_STRING_SIZE
,
315 /* bDescriptorType */
320 * @brief Language ID string descriptor
322 static uint8_t USBD_LandIDStrDesc
[USBD_LANGID_STRING_SIZE
] =
325 USBD_LANGID_STRING_SIZE
,
326 /* bDescriptorType */
328 USBD_LANGID_STR
& 0xFF, USBD_LANGID_STR
>> 8
332 * @brief Device qualifier descriptor
334 static uint8_t USBD_DevQualifierDesc
[USBD_DEVICE_QUALIFIER_DESCRIPTOR_SIZE
] =
337 USBD_DEVICE_QUALIFIER_DESCRIPTOR_SIZE
,
338 /* bDescriptorType */
339 USBD_DESC_DEVICE_QUALIFIER
,
345 USBD_MSC_FS_MP_SIZE
, /* In FS device*/
350 /* Private functions ******************************************************/
353 * @brief USB device convert ascii string descriptor to unicode format
355 * @param desc : descriptor string
357 * @retval usb descriptor information
359 static USBD_DESC_INFO_T
USBD_DESC_Ascii2Unicode(uint8_t* desc
)
361 USBD_DESC_INFO_T descInfo
;
363 uint8_t str
[USBD_SUP_STR_DESC_MAX_NUM
];
365 uint8_t* unicode
= str
;
367 __IO
uint8_t index
= 0;
371 descInfo
.desc
= NULL
;
377 length
= (strlen((char*)buffer
) * 2) + 2;
378 /* Get unicode descriptor */
379 unicode
[index
] = length
;
382 unicode
[index
] = USBD_DESC_STRING
;
385 while (*buffer
!= '\0')
387 unicode
[index
] = *buffer
;
391 unicode
[index
] = 0x00;
396 descInfo
.desc
= unicode
;
397 descInfo
.size
= length
;
403 * @brief USB device FS device descriptor
405 * @param usbSpeed : usb speed
407 * @retval usb descriptor information
409 static USBD_DESC_INFO_T
USBD_MSC_DeviceDescHandler(uint8_t usbSpeed
)
411 USBD_DESC_INFO_T descInfo
;
415 descInfo
.desc
= USBD_DeviceDesc
;
416 descInfo
.size
= sizeof(USBD_DeviceDesc
);
422 * @brief USB device FS configuration descriptor
424 * @param usbSpeed : usb speed
426 * @retval usb descriptor information
428 static USBD_DESC_INFO_T
USBD_MSC_ConfigDescHandler(uint8_t usbSpeed
)
430 USBD_DESC_INFO_T descInfo
;
434 descInfo
.desc
= USBD_ConfigDesc
;
435 descInfo
.size
= sizeof(USBD_ConfigDesc
);
442 * @brief USB device FS BOS descriptor
444 * @param usbSpeed : usb speed
446 * @retval usb descriptor information
448 static USBD_DESC_INFO_T
USBD_MSC_BosDescHandler(uint8_t usbSpeed
)
450 USBD_DESC_INFO_T descInfo
;
454 descInfo
.desc
= USBD_BosDesc
;
455 descInfo
.size
= sizeof(USBD_BosDesc
);
462 * @brief USB device FS configuration string descriptor
464 * @param usbSpeed : usb speed
466 * @retval usb descriptor information
468 static USBD_DESC_INFO_T
USBD_MSC_ConfigStrDescHandler(uint8_t usbSpeed
)
470 USBD_DESC_INFO_T descInfo
;
474 if (usbSpeed
== USBD_SPEED_HS
)
476 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_CONFIGURATION_HS_STR
);
480 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_CONFIGURATION_FS_STR
);
487 * @brief USB device FS interface string descriptor
489 * @param usbSpeed : usb speed
491 * @retval usb descriptor information
493 static USBD_DESC_INFO_T
USBD_MSC_InterfaceStrDescHandler(uint8_t usbSpeed
)
495 USBD_DESC_INFO_T descInfo
;
497 if (usbSpeed
== USBD_SPEED_HS
)
499 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_INTERFACE_HS_STR
);
503 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_INTERFACE_FS_STR
);
510 * @brief USB device FS LANG ID string descriptor
512 * @param usbSpeed : usb speed
514 * @retval usb descriptor information
516 static USBD_DESC_INFO_T
USBD_MSC_LangIdStrDescHandler(uint8_t usbSpeed
)
518 USBD_DESC_INFO_T descInfo
;
522 descInfo
.desc
= USBD_LandIDStrDesc
;
523 descInfo
.size
= sizeof(USBD_LandIDStrDesc
);
529 * @brief USB device FS manufacturer string descriptor
531 * @param usbSpeed : usb speed
533 * @retval usb descriptor information
535 static USBD_DESC_INFO_T
USBD_MSC_ManufacturerStrDescHandler(uint8_t usbSpeed
)
537 USBD_DESC_INFO_T descInfo
;
541 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_MANUFACTURER_STR
);
547 * @brief USB device FS product string descriptor
549 * @param usbSpeed : usb speed
551 * @retval usb descriptor information
553 static USBD_DESC_INFO_T
USBD_MSC_ProductStrDescHandler(uint8_t usbSpeed
)
555 USBD_DESC_INFO_T descInfo
;
557 if (usbSpeed
== USBD_SPEED_HS
)
559 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_PRODUCT_HS_STR
);
563 descInfo
= USBD_DESC_Ascii2Unicode((uint8_t*)USBD_PRODUCT_FS_STR
);
570 * @brief USB device FS serial string descriptor
572 * @param usbSpeed : usb speed
574 * @retval usb descriptor information
576 static USBD_DESC_INFO_T
USBD_MSC_SerialStrDescHandler(uint8_t usbSpeed
)
578 USBD_DESC_INFO_T descInfo
;
582 /* Update the serial number string descriptor with the data from the unique ID*/
585 descInfo
.desc
= USBD_SerialStrDesc
;
586 descInfo
.size
= sizeof(USBD_SerialStrDesc
);
592 * @brief USB device other speed configuration descriptor
594 * @param usbSpeed : usb speed
596 * @retval usb descriptor information
598 static USBD_DESC_INFO_T
USBD_OtherSpeedConfigDescHandler(uint8_t usbSpeed
)
600 USBD_DESC_INFO_T descInfo
;
604 /* Use FS configuration */
605 descInfo
.desc
= USBD_OtherSpeedCfgDesc
;
606 descInfo
.size
= sizeof(USBD_OtherSpeedCfgDesc
);
612 * @brief USB device device qualifier descriptor
614 * @param usbSpeed : usb speed
616 * @retval usb descriptor information
618 static USBD_DESC_INFO_T
USBD_DevQualifierDescHandler(uint8_t usbSpeed
)
620 USBD_DESC_INFO_T descInfo
;
624 descInfo
.desc
= USBD_DevQualifierDesc
;
625 descInfo
.size
= sizeof(USBD_DevQualifierDesc
);
631 * @brief Create the serial number string descriptor
635 static void Get_SerialNum(void)
637 uint32_t deviceserial0
, deviceserial1
, deviceserial2
;
639 deviceserial0
= U_ID_0
;
640 deviceserial1
= U_ID_1
;
641 deviceserial2
= U_ID_2
;
643 deviceserial0
+= deviceserial2
;
645 if (deviceserial0
!= 0)
647 IntToUnicode (deviceserial0
, &USBD_SerialStrDesc
[2] ,8);
648 IntToUnicode (deviceserial1
, &USBD_SerialStrDesc
[18] ,4);
653 * @brief Convert Hex 32Bits value into char
654 * @param value: value to convert
655 * @param pbuf: pointer to the buffer
656 * @param len: buffer length
659 static void IntToUnicode (uint32_t value
, uint8_t *pbuf
, uint8_t len
)
663 for ( idx
= 0; idx
< len
; idx
++)
665 if ( ((value
>> 28)) < 0xA )
667 pbuf
[ 2* idx
] = (value
>> 28) + '0';
671 pbuf
[2* idx
] = (value
>> 28) + 'A' - 10;
676 pbuf
[ 2* idx
+ 1] = 0;