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