Contiki-NG
Loading...
Searching...
No Matches
usb_descriptors.c
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26#include "tusb.h"
27
28/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
29 * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
30 *
31 * Auto ProductID layout's Bitmap:
32 * [MSB] HID | MSC | CDC [LSB]
33 */
34#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
35
36/* Use Nordic's blessed VID:PID for the nRF52840 dongle CDC + DFU-trigger
37 * image (0x1915:0x520F). nrfutil v7+ keys its `nordicDfu` trait
38 * detection off this exact pair, so a vendor-specific DFU trigger
39 * interface only gets exercised by the host when the device announces
40 * itself with this PID. This matches what the OLD arch/platform/nrf52840
41 * port did via APP_USBD_PID in sdk_config.h. */
42#ifndef USB_PID
43#define USB_PID 0x520Fu
44#endif
45
46//--------------------------------------------------------------------+
47// Device Descriptors
48//--------------------------------------------------------------------+
49tusb_desc_device_t const desc_device =
50{
51 .bLength = sizeof(tusb_desc_device_t),
52 .bDescriptorType = TUSB_DESC_DEVICE,
53 .bcdUSB = 0x0200,
54
55 // Use Interface Association Descriptor (IAD) for CDC
56 // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
57 .bDeviceClass = TUSB_CLASS_MISC,
58 .bDeviceSubClass = MISC_SUBCLASS_COMMON,
59 .bDeviceProtocol = MISC_PROTOCOL_IAD,
60
61 .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
62
63 .idVendor = 0x1915,
64 .idProduct = USB_PID,
65 .bcdDevice = 0x0100,
66
67 .iManufacturer = 0x01,
68 .iProduct = 0x02,
69 .iSerialNumber = 0x03,
70
71 .bNumConfigurations = 0x01
72};
73
74// Invoked when received GET DEVICE DESCRIPTOR
75// Application return pointer to descriptor
76uint8_t const * tud_descriptor_device_cb(void)
77{
78 return (uint8_t const *) &desc_device;
79}
80
81//--------------------------------------------------------------------+
82// Configuration Descriptor
83//--------------------------------------------------------------------+
84
85enum
86{
87 ITF_NUM_CDC = 0,
88 ITF_NUM_CDC_DATA,
89#if CFG_TUD_DFU_RUNTIME
90 ITF_NUM_DFU_RT,
91#endif
92 ITF_NUM_NORDIC_DFU,
93 ITF_NUM_TOTAL
94};
95
96/* Length of the Nordic-vendor DFU trigger interface descriptor block:
97 * one 9-byte interface descriptor + one 9-byte Nordic functional descriptor. */
98#define NORDIC_DFU_TRIGGER_DESC_LEN (9 + 9)
99
100#if CFG_TUD_DFU_RUNTIME
101#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN \
102 + TUD_DFU_RT_DESC_LEN + NORDIC_DFU_TRIGGER_DESC_LEN)
103#else
104#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN \
105 + NORDIC_DFU_TRIGGER_DESC_LEN)
106#endif
107
108/* Raw bytes for the Nordic DFU trigger interface + functional descriptor.
109 * Mirrors what arch/cpu/nrf52840/lib/nrf52-sdk/.../app_usbd_nrf_dfu_trigger
110 * emits so nrfutil can recognise the interface. */
111#define NORDIC_DFU_TRIGGER_DESCRIPTOR(_itfnum, _stridx) \
112 /* Interface */ \
113 9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, 0xFF, 0x01, 0x01, _stridx, \
114 /* Functional (bmAttributes = bitCanDnload | bitWillDetach,
115 * wDetachTimeout = 1000, wTransferSize = 4096,
116 * bcdDFUVersion = 0x0101) */ \
117 9, 0x21, 0x09, 0xE8, 0x03, 0x00, 0x10, 0x01, 0x01
118
119#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
120 // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
121 // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ...
122 #define EPNUM_CDC_NOTIF 0x81
123 #define EPNUM_CDC_OUT 0x02
124 #define EPNUM_CDC_IN 0x82
125
126#elif CFG_TUSB_MCU == OPT_MCU_SAMG
127 // SAMG doesn't support a same endpoint number with different direction IN and OUT
128 // e.g EP1 OUT & EP1 IN cannot exist together
129 #define EPNUM_CDC_NOTIF 0x81
130 #define EPNUM_CDC_OUT 0x02
131 #define EPNUM_CDC_IN 0x83
132
133#elif CFG_TUSB_MCU == OPT_MCU_CXD56
134 // CXD56 doesn't support a same endpoint number with different direction IN and OUT
135 // e.g EP1 OUT & EP1 IN cannot exist together
136 // CXD56 USB driver has fixed endpoint type (bulk/interrupt/iso) and direction (IN/OUT) by its number
137 // 0 control (IN/OUT), 1 Bulk (IN), 2 Bulk (OUT), 3 In (IN), 4 Bulk (IN), 5 Bulk (OUT), 6 In (IN)
138 #define EPNUM_CDC_NOTIF 0x83
139 #define EPNUM_CDC_OUT 0x02
140 #define EPNUM_CDC_IN 0x81
141
142#else
143 #define EPNUM_CDC_NOTIF 0x81
144 #define EPNUM_CDC_OUT 0x02
145 #define EPNUM_CDC_IN 0x82
146
147#endif
148
149/* DFU runtime descriptor parameters: bitWillDetach so the device reboots
150 * to bootloader on its own when the host sends a DETACH request. The
151 * detach timeout and transfer size are conventional values; the actual
152 * download happens after the device has re-enumerated as the bootloader,
153 * so the runtime transfer size is not load-bearing. */
154#define DFU_RT_ATTR (DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_WILL_DETACH)
155#define DFU_RT_TIMEOUT 1000
156#define DFU_RT_XFER 4096
157
158uint8_t const desc_fs_configuration[] =
159{
160 // Config number, interface count, string index, total length, attribute, power in mA
161 TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
162
163 // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
164 TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
165#if CFG_TUD_DFU_RUNTIME
166 TUD_DFU_RT_DESCRIPTOR(ITF_NUM_DFU_RT, 5, DFU_RT_ATTR, DFU_RT_TIMEOUT, DFU_RT_XFER),
167#endif
168 NORDIC_DFU_TRIGGER_DESCRIPTOR(ITF_NUM_NORDIC_DFU, 6),
169};
170
171#if TUD_OPT_HIGH_SPEED
172uint8_t const desc_hs_configuration[] =
173{
174 // Config number, interface count, string index, total length, attribute, power in mA
175 TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
176
177 // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
178 TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
179#if CFG_TUD_DFU_RUNTIME
180 TUD_DFU_RT_DESCRIPTOR(ITF_NUM_DFU_RT, 5, DFU_RT_ATTR, DFU_RT_TIMEOUT, DFU_RT_XFER),
181#endif
182 NORDIC_DFU_TRIGGER_DESCRIPTOR(ITF_NUM_NORDIC_DFU, 6),
183};
184#endif
185
186
187// Invoked when received GET CONFIGURATION DESCRIPTOR
188// Application return pointer to descriptor
189// Descriptor contents must exist long enough for transfer to complete
190uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
191{
192 (void) index; // for multiple configurations
193
194#if TUD_OPT_HIGH_SPEED
195 // Although we are highspeed, host may be fullspeed.
196 return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration;
197#else
198 return desc_fs_configuration;
199#endif
200}
201
202//--------------------------------------------------------------------+
203// String Descriptors
204//--------------------------------------------------------------------+
205
206// array of pointer to string descriptors
207char const* string_desc_arr [] =
208{
209 (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
210 NULL, // 1: Manufacturer
211 NULL, // 2: Product
212 NULL, // 3: Serials, should use chip ID
213 NULL, // 4: CDC Interface
214#if CFG_TUD_DFU_RUNTIME
215 "Contiki-NG DFU", // 5: DFU runtime Interface
216#else
217 NULL, // 5: (placeholder, keep index alignment)
218#endif
219 "Contiki-NG Nordic DFU", // 6: Nordic DFU trigger Interface
221
222void usb_descriptor_set_manufacturer(char * manufacturer)
223{
224 string_desc_arr[1] = manufacturer;
226
227void usb_descriptor_set_product(char * product)
228{
229 string_desc_arr[2] = product;
231
232void usb_descriptor_set_serial(char * serial)
233{
234 string_desc_arr[3] = serial;
236
237void usb_descriptor_set_cdc_interface(char * cdc_interface)
238{
239 string_desc_arr[4] = cdc_interface;
240}
241
242static uint16_t _desc_str[32];
243
244// Invoked when received GET STRING DESCRIPTOR request
245// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
246uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
247{
248 (void) langid;
249
250 uint8_t chr_count;
251
252 if ( index == 0)
253 {
254 memcpy(&_desc_str[1], string_desc_arr[0], 2);
255 chr_count = 1;
256 }else
257 {
258 // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
259 // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
260
261 if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
262
263 const char* str = string_desc_arr[index];
264
265 // Cap at max char
266 chr_count = strlen(str);
267 if ( chr_count > 31 ) chr_count = 31;
268
269 // Convert ASCII string into UTF-16
270 for(uint8_t i=0; i<chr_count; i++)
271 {
272 _desc_str[1+i] = str[i];
273 }
274 }
275
276 // first byte is length (including header), second byte is string type
277 _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
278
279 return _desc_str;
280}
void usb_descriptor_set_serial(char *serial)
Set the serial.
void usb_descriptor_set_manufacturer(char *manufacturer)
Set the manufactorer.
void usb_descriptor_set_cdc_interface(char *cdc_interface)
Set the cdc interface.
void usb_descriptor_set_product(char *product)
Set the product.