usbd_desc.c
Go to the documentation of this file.
1 /**
2  * @file usbd_desc.c
3  * @brief USB descriptors
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Pro.
10  *
11  * This software is provided under a commercial license. You may
12  * use this software under the conditions stated in the license
13  * terms. This source code cannot be redistributed.
14  *
15  * @author Oryx Embedded SARL (www.oryx-embedded.com)
16  * @version 1.9.0
17  **/
18 
19 //Dependencies
20 #include "usbd_def.h"
21 #include "usbd_ctlreq.h"
22 #include "usbd_desc.h"
23 #include "os_port.h"
24 #include "cpu_endian.h"
25 #include "debug.h"
26 
27 //Vendor identifier
28 #define USBD_VENDOR_ID 0x0483
29 //Product identifier
30 #define USBD_PRODUCT_ID 0x0123
31 //Device revision
32 #define USBD_DEVICE_REV 0x0100
33 
34 //String descriptors
35 #define USBD_MANUFACTURER_STR "STMicroelectronics"
36 #define USBD_PRODUCT_STR "STM32 RNDIS Demo"
37 #define USBD_SERIAL_NUMBER_STR "00000000123C"
38 #define USBD_CONFIGURATION_STR "RNDIS Configuration"
39 #define USBD_INTERFACE_STR "RNDIS Interface"
40 
41 //Global variables
42 static uint8_t usbdStrDescriptor[USBD_MAX_STR_DESC_SIZ];
43 
44 
45 /**
46  * @brief USB descriptors
47  **/
48 
49 USBD_DescriptorsTypeDef usbdRndisDescriptors = {
57 };
58 
59 
60 /**
61  * @brief USB device descriptor
62  **/
63 
65 {
66  sizeof(UsbDeviceDescriptor), //bLength
67  USB_DESC_TYPE_DEVICE, //bDescriptorType
68  HTOLE16(0x0200), //bcdUsb (2.00)
69  USB_DEVICE_CLASS_CDC, //bDeviceClass
70  USB_DEVICE_SUBCLASS_CDC, //bDeviceSubClass
71  USB_DEVICE_PROTOCOL_CDC, //bDeviceProtocol
72  USB_EP0_MAX_PACKET_SIZE, //bMaxPacketSize0
73  HTOLE16(USBD_VENDOR_ID), //idVendor
74  HTOLE16(USBD_PRODUCT_ID), //idProduct
75  HTOLE16(USBD_DEVICE_REV), //bcdDevice
76  1, //iManufacturer
77  2, //iProduct
78  3, //iSerialNumber
79  1 //bNumConfigurations
80 };
81 
82 
83 /**
84  * @brief USB configuration descriptors
85  **/
86 
88 {
89  //Standard configuration descriptor
90  {
91  sizeof(UsbConfigDescriptor), //bLength
92  USB_DESC_TYPE_CONFIGURATION, //bDescriptorType
93  HTOLE16(sizeof(usbdConfigDescriptors)), //wTotalLength
94  2, //bNumInterfaces
95  1, //bConfigurationValue
96  0, //iConfiguration
97  USB_SELF_POWERED | USB_NO_REMOTE_WAKEUP, //bmAttributes
98  0 //bMaxPower
99  },
100  //Communication class interface Descriptor
101  {
102  sizeof(UsbInterfaceDescriptor), //bLength
103  USB_DESC_TYPE_INTERFACE, //bDescriptorType
104  0, //bInterfaceNumber
105  0, //bAlternateSetting
106  1, //bNumEndpoints
107 #if 0
108  //Linux or windows 7
109  CDC_INTERFACE_CLASS_COMMUNICATION, //bInterfaceClass
110  CDC_INTERFACE_SUBCLASS_ACM, //bInterfaceSubClass
111  CDC_INTERFACE_PROTOCOL_VENDOR_SPECIFIC, //bInterfaceProtocol
112 #else
113  //Windows 10
114  CDC_INTERFACE_CLASS_RNDIS, //bInterfaceClass
115  CDC_INTERFACE_SUBCLASS_RNDIS, //bInterfaceSubClass
116  CDC_INTERFACE_PROTOCOL_RNDIS, //bInterfaceProtocol
117 #endif
118  0 //iInterface
119  },
120  //CDC header functional descriptor
121  {
122  sizeof(CdcHeaderDescriptor), //bFunctionLength
123  CDC_CS_INTERFACE, //bDescriptorType
124  CDC_HEADER_DESC_SUBTYPE, //bDescriptorSubtype
125  HTOLE16(0x0110), //bcdCdc (1.10)
126  },
127  //CDC call management functional descriptor
128  {
129  sizeof(CdcCallManagementDescriptor), //bFunctionLength
130  CDC_CS_INTERFACE, //bDescriptorType
131  CDC_CALL_MANAGEMENT_DESC_SUBTYPE, //bDescriptorSubtype
132  0x00, //bmCapabilities
133  1 //bDataInterface
134  },
135  //CDC abstract control management functional descriptor
136  {
137  sizeof(CdcAcmDescriptor), //bFunctionLength
138  CDC_CS_INTERFACE, //bDescriptorType
139  CDC_ACM_DESC_SUBTYPE, //bDescriptorSubtype
140  0x00 //bmCapabilities
141  },
142  //CDC union functional descriptor
143  {
144  sizeof(CdcUnionDescriptor), //bFunctionLength
145  CDC_CS_INTERFACE, //bDescriptorType
146  CDC_UNION_DESC_SUBTYPE, //bDescriptorSubtype
147  0, //bControlInterface (communication interface)
148  1 //bSubordinateInterface0 (data interface)
149  },
150  //Notification endpoint descriptor
151  {
152  sizeof(UsbEndpointDescriptor), //bLength
153  USB_DESC_TYPE_ENDPOINT, //bDescriptorType
154  USB_DIR_IN | USB_EP1, //bEndpointAddress
155  USB_EP_TYPE_INTERRUPT, //bmAttributes
156  HTOLE16(USB_EP1_MAX_PACKET_SIZE), //wMaxPacketSize
157  1 //bInterval (1ms)
158  },
159  //Data class interface descriptor
160  {
161  sizeof(UsbInterfaceDescriptor), //bLength
162  USB_DESC_TYPE_INTERFACE, //bDescriptorType
163  1, //bInterfaceNumber
164  0, //bAlternateSetting
165  2, //bNumEndpoints
166  CDC_INTERFACE_CLASS_DATA, //bInterfaceClass
167  CDC_INTERFACE_SUBCLASS_DATA, //bInterfaceSubClass
168  CDC_INTERFACE_PROTOCOL_DATA, //bInterfaceProtocol
169  0 //iInterface
170  },
171  //Data IN endpoint descriptor
172  {
173  sizeof(UsbEndpointDescriptor), //bLength
174  USB_DESC_TYPE_ENDPOINT, //bDescriptorType
175  USB_DIR_IN | USB_EP2, //bEndpointAddress
176  USB_EP_TYPE_BULK, //bmAttributes
177  HTOLE16(USB_EP2_MAX_PACKET_SIZE), //wMaxPacketSize
178  0 //bInterval
179  },
180  //Data OUT endpoint descriptor
181  {
182  sizeof(UsbEndpointDescriptor), //bLength
183  USB_DESC_TYPE_ENDPOINT, //bDescriptorType
184  USB_DIR_OUT | USB_EP3, //bEndpointAddress
185  USB_EP_TYPE_BULK, //bmAttributes
186  HTOLE16(USB_EP3_MAX_PACKET_SIZE), //wMaxPacketSize
187  0 //bInterval
188  }
189 };
190 
191 
192 /**
193  * @brief Retrieve device descriptor
194  * @param[in] speed Current speed
195  * @param[out] length Length of the device descriptor, in bytes
196  * @return Pointer to the device descriptor
197  **/
198 
199 uint8_t *usbdGetDeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
200 {
201  //Length of the string descriptor
202  *length = sizeof(usbdDeviceDescriptor);
203  //Return a pointer to the string descriptor
204  return (uint8_t *) &usbdDeviceDescriptor;
205 }
206 
207 
208 /**
209  * @brief Retrieve the languages supported by the device
210  * @param[in] speed Current speed
211  * @param[out] length Length of the string descriptor, in bytes
212  * @return Pointer to the string descriptor
213  **/
214 
215 uint8_t *usbdGetLangIdStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
216 {
217  UsbStringDescriptor *desc;
218 
219  //Point to the buffer where to format the string descriptor
220  desc = (UsbStringDescriptor *) usbdStrDescriptor;
221 
222  //Format string descriptor
223  desc->bLength = 4;
224  desc->bDescriptorType = USB_DESC_TYPE_STRING;
225  desc->bString[0] = 0x0409;
226 
227  //Length of the string descriptor
228  *length = desc->bLength;
229  //Return a pointer to the string descriptor
230  return (uint8_t *) desc;
231 }
232 
233 
234 /**
235  * @brief Retrieve the manufacturer string descriptor
236  * @param[in] speed Current speed
237  * @param[out] length Length of the string descriptor, in bytes
238  * @return Pointer to the string descriptor
239  **/
240 
241 uint8_t *usbdGetManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
242 {
243  //Convert the string to unicode
244  USBD_GetString((uint8_t *) USBD_MANUFACTURER_STR, usbdStrDescriptor, length);
245  //Return a pointer to the string descriptor
246  return usbdStrDescriptor;
247 }
248 
249 
250 /**
251  * @brief Retrieve the product string descriptor
252  * @param[in] speed Current speed
253  * @param[out] length Length of the string descriptor, in bytes
254  * @return Pointer to the string descriptor
255  **/
256 
257 uint8_t *usbdGetProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
258 {
259  //Convert the string to unicode
260  USBD_GetString((uint8_t *) USBD_PRODUCT_STR, usbdStrDescriptor, length);
261  //Return a pointer to the string descriptor
262  return usbdStrDescriptor;
263 }
264 
265 
266 /**
267  * @brief Retrieve the serial number string descriptor
268  * @param[in] speed Current speed
269  * @param[out] length Length of the string descriptor, in bytes
270  * @return Pointer to the string descriptor
271  **/
272 
273 uint8_t *usbdGetSerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
274 {
275  //Convert the string to unicode
276  USBD_GetString((uint8_t *) USBD_SERIAL_NUMBER_STR, usbdStrDescriptor, length);
277  //Return a pointer to the string descriptor
278  return usbdStrDescriptor;
279 }
280 
281 
282 /**
283  * @brief Retrieve the configuration string descriptor
284  * @param[in] speed Current speed
285  * @param[out] length Length of the string descriptor, in bytes
286  * @return Pointer to the string descriptor
287  **/
288 
289 uint8_t *usbdGetConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
290 {
291  //Convert the string to unicode
292  USBD_GetString((uint8_t *) USBD_CONFIGURATION_STR, usbdStrDescriptor, length);
293  //Return a pointer to the string descriptor
294  return usbdStrDescriptor;
295 }
296 
297 
298 /**
299  * @brief Retrieve the interface string descriptor
300  * @param[in] speed Current speed
301  * @param[out] length Length of the string descriptor, in bytes
302  * @return Pointer to the string descriptor
303  **/
304 
305 uint8_t *usbdGetInterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
306 {
307  //Convert the string to unicode
308  USBD_GetString((uint8_t *) USBD_INTERFACE_STR, usbdStrDescriptor, length);
309  //Return a pointer to the string descriptor
310  return usbdStrDescriptor;
311 }
#define USB_DESC_TYPE_STRING
Definition: usbd_desc.h:45
#define USB_DESC_TYPE_ENDPOINT
Definition: usbd_desc.h:47
#define USB_DEVICE_CLASS_CDC
Definition: usbd_desc.h:64
uint8_t * usbdGetLangIdStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve the languages supported by the device.
Definition: usbd_desc.c:215
__start_packed struct @164 CdcUnionDescriptor
CDC union functional descriptor.
const UsbConfigDescriptors usbdConfigDescriptors
USB configuration descriptors.
Definition: usbd_desc.c:87
__start_packed struct @163 CdcAcmDescriptor
CDC abstract control management functional descriptor.
#define USB_SELF_POWERED
Definition: usbd_desc.h:51
uint8_t * usbdGetInterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve the interface string descriptor.
Definition: usbd_desc.c:305
#define USB_NO_REMOTE_WAKEUP
Definition: usbd_desc.h:54
__start_packed struct @157 UsbConfigDescriptor
Configuration descriptor.
#define CDC_UNION_DESC_SUBTYPE
Definition: usbd_desc.h:95
#define CDC_HEADER_DESC_SUBTYPE
Definition: usbd_desc.h:92
__start_packed struct @160 UsbStringDescriptor
String descriptor.
Debugging facilities.
#define USB_EP1_MAX_PACKET_SIZE
Definition: usbd_desc.h:38
#define USBD_MANUFACTURER_STR
Definition: usbd_desc.c:35
#define USB_EP_TYPE_BULK
Definition: usbd_desc.h:60
__start_packed struct @159 UsbEndpointDescriptor
Endpoint descriptor.
#define USB_EP2
Definition: usbd_desc.h:28
USBD_DescriptorsTypeDef usbdRndisDescriptors
USB descriptors.
Definition: usbd_desc.c:49
#define USB_EP3
Definition: usbd_desc.h:29
#define USB_DEVICE_SUBCLASS_CDC
Definition: usbd_desc.h:66
#define USB_DIR_IN
Definition: usbd_desc.h:34
#define USB_DIR_OUT
Definition: usbd_desc.h:33
#define USB_EP1
Definition: usbd_desc.h:27
#define USBD_CONFIGURATION_STR
Definition: usbd_desc.c:38
const UsbDeviceDescriptor usbdDeviceDescriptor
USB device descriptor.
Definition: usbd_desc.c:64
#define CDC_CS_INTERFACE
Definition: usbd_desc.h:88
uint8_t * usbdGetConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve the configuration string descriptor.
Definition: usbd_desc.c:289
#define CDC_INTERFACE_PROTOCOL_DATA
Definition: usbd_desc.h:85
#define USB_DESC_TYPE_CONFIGURATION
Definition: usbd_desc.h:44
#define CDC_CALL_MANAGEMENT_DESC_SUBTYPE
Definition: usbd_desc.h:93
#define USB_EP_TYPE_INTERRUPT
Definition: usbd_desc.h:61
#define USB_DESC_TYPE_DEVICE
Definition: usbd_desc.h:43
#define USB_EP3_MAX_PACKET_SIZE
Definition: usbd_desc.h:40
#define CDC_INTERFACE_CLASS_RNDIS
Definition: usbd_desc.h:72
__start_packed struct @162 CdcCallManagementDescriptor
CDC call management functional descriptor.
#define CDC_ACM_DESC_SUBTYPE
Definition: usbd_desc.h:94
__start_packed struct @165 UsbConfigDescriptors
Configuration descriptors.
RTOS abstraction layer.
#define USBD_PRODUCT_STR
Definition: usbd_desc.c:36
#define CDC_INTERFACE_CLASS_COMMUNICATION
Definition: usbd_desc.h:71
#define CDC_INTERFACE_PROTOCOL_VENDOR_SPECIFIC
Definition: usbd_desc.h:78
#define CDC_INTERFACE_CLASS_DATA
Definition: usbd_desc.h:81
__start_packed struct @156 UsbDeviceDescriptor
Device descriptor.
Byte order conversion.
#define USBD_VENDOR_ID
Definition: usbd_desc.c:28
#define CDC_INTERFACE_SUBCLASS_ACM
Definition: usbd_desc.h:74
__start_packed struct @161 CdcHeaderDescriptor
CDC header functional descriptor.
uint8_t * usbdGetManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve the manufacturer string descriptor.
Definition: usbd_desc.c:241
#define CDC_INTERFACE_PROTOCOL_RNDIS
Definition: usbd_desc.h:77
#define USB_DEVICE_PROTOCOL_CDC
Definition: usbd_desc.h:68
uint8_t * usbdGetDeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve device descriptor.
Definition: usbd_desc.c:199
__start_packed struct @158 UsbInterfaceDescriptor
Interface descriptor.
uint8_t * usbdGetSerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve the serial number string descriptor.
Definition: usbd_desc.c:273
#define USBD_INTERFACE_STR
Definition: usbd_desc.c:39
#define USBD_SERIAL_NUMBER_STR
Definition: usbd_desc.c:37
#define USB_DESC_TYPE_INTERFACE
Definition: usbd_desc.h:46
#define USBD_PRODUCT_ID
Definition: usbd_desc.c:30
#define CDC_INTERFACE_SUBCLASS_DATA
Definition: usbd_desc.h:83
#define USB_EP2_MAX_PACKET_SIZE
Definition: usbd_desc.h:39
#define CDC_INTERFACE_SUBCLASS_RNDIS
Definition: usbd_desc.h:75
uint8_t length
Definition: dtls_misc.h:140
#define USBD_DEVICE_REV
Definition: usbd_desc.c:32
#define HTOLE16(value)
Definition: cpu_endian.h:400
uint8_t * usbdGetProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Retrieve the product string descriptor.
Definition: usbd_desc.c:257
#define USB_EP0_MAX_PACKET_SIZE
Definition: usbd_desc.h:37
USB descriptors.