Skip to content

Commit fe2f7c8

Browse files
committed
pkg/tinyusb: support second device configuration descriptor
An alternative device configuration descriptor is required if multiple protocols, e.g. CDC ECM and RNDIS, should be used with same device interface. This commit is a prerequisite for the support of tinyusb netdev driver.
1 parent b9449b0 commit fe2f7c8

File tree

2 files changed

+122
-20
lines changed

2 files changed

+122
-20
lines changed

pkg/tinyusb/contrib/include/tinyusb_descriptors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ enum {
125125
(CONFIG_TUSBD_VENDOR_NUMOF * TUD_VENDOR_DESC_LEN))
126126
#endif /* !defined(TUSBD_DESC_TOTAL_LEN) */
127127

128+
#define TUSBD_DESC_ALT_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \
129+
(CONFIG_TUSBD_CDC_NUMOF * TUD_CDC_DESC_LEN) + \
130+
(CONFIG_TUSBD_HID_NUMOF * TUD_HID_INOUT_DESC_LEN) + \
131+
(CONFIG_TUSBD_MSC_NUMOF * TUD_MSC_DESC_LEN) + \
132+
(CONFIG_TUSBD_VENDOR_NUMOF * TUD_VENDOR_DESC_LEN))
133+
128134
#endif /* !defined(CONFIG_TUSBD_USE_CUSTOM_DESC) */
129135

130136
#ifdef __cplusplus

pkg/tinyusb/contrib/tinyusb_descriptors.c

Lines changed: 116 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
of device class interfaces. Custom descriptors have to be implemented.
5353
#endif
5454

55+
#define _TUD_CONFIG_DESC_NUMOF 1
56+
57+
enum {
58+
_TUD_CONFIG_DESC_ID = 0,
59+
#if _TUD_CONFIG_DESC_NUMOF == 2
60+
_TUD_CONFIG_DESC_ALT_ID = 1,
61+
#endif
62+
};
63+
5564
/*
5665
* --------------------------------------------------------------------+
5766
* Device Descriptors
@@ -84,7 +93,7 @@ tusb_desc_device_t const tusb_desc_device = {
8493
.iProduct = TUSBD_STR_IDX_PRODUCT,
8594
.iSerialNumber = TUSBD_STR_IDX_SERIAL,
8695

87-
.bNumConfigurations = 0x01
96+
.bNumConfigurations = _TUD_CONFIG_DESC_NUMOF
8897
};
8998

9099
/*
@@ -200,10 +209,10 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
200209
#define _tusb_speed_fs 0
201210
#define _tusb_speed_hs 1
202211

203-
#define _TUD_CONFIG_DESC(id) \
212+
#define _TUD_CONFIG_DESC(id, len) \
204213
/* Config number, interface count, string index, total length, attribute,
205214
* power in mA */ \
206-
TUD_CONFIG_DESCRIPTOR(id, TUSBD_ITF_NUMOF, 0, TUSBD_DESC_TOTAL_LEN, \
215+
TUD_CONFIG_DESCRIPTOR(id + 1, TUSBD_ITF_NUMOF, 0, len, \
207216
DESC_DEV_ATTR, CONFIG_USB_MAX_POWER)
208217

209218
#define _TUD_CDC_DESC(speed, n) \
@@ -242,7 +251,31 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
242251
/* FS configuration */
243252
__attribute__((weak))
244253
uint8_t const tusb_desc_fs_config[] = {
245-
_TUD_CONFIG_DESC(1),
254+
_TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ID, TUSBD_DESC_TOTAL_LEN),
255+
#if CONFIG_TUSBD_CDC_NUMOF > 0
256+
_TUD_CDC_DESC(_tusb_speed_fs, 0),
257+
#endif
258+
#if CONFIG_TUSBD_CDC_NUMOF > 1
259+
_TUD_CDC_DESC(_tusb_speed_fs, 1),
260+
#endif
261+
#if CONFIG_TUSBD_HID_NUMOF > 0
262+
_TUD_HID_INOUT_DESC(_tusb_speed_fs, 0),
263+
#endif
264+
#if CONFIG_TUSBD_HID_NUMOF > 1
265+
_TUD_HID_INOUT_DESC(_tusb_speed_fs, 1),
266+
#endif
267+
#if CONFIG_TUSBD_MSC_NUMOF
268+
_TUD_MSC_DESC(_tusb_speed_fs),
269+
#endif
270+
#if CONFIG_TUSBD_VENDOR_NUMOF
271+
_TUD_VENDOR_DESC(_tusb_speed_fs),
272+
#endif
273+
};
274+
275+
#if _TUD_CONFIG_DESC_NUMOF == 2
276+
__attribute__((weak))
277+
uint8_t const tusb_desc_fs_config_alt[] = {
278+
_TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ALT_ID, TUSBD_DESC_ALT_TOTAL_LEN),
246279
#if CONFIG_TUSBD_CDC_NUMOF > 0
247280
_TUD_CDC_DESC(_tusb_speed_fs, 0),
248281
#endif
@@ -262,6 +295,7 @@ uint8_t const tusb_desc_fs_config[] = {
262295
_TUD_VENDOR_DESC(_tusb_speed_fs),
263296
#endif
264297
};
298+
#endif /* _TUD_CONFIG_DESC_NUMOF == 2 */
265299

266300
#if TUD_OPT_HIGH_SPEED
267301
/* Per USB specs: high speed capable device must report device_qualifier
@@ -270,7 +304,31 @@ uint8_t const tusb_desc_fs_config[] = {
270304
/* HS configuration */
271305
__attribute__((weak))
272306
uint8_t const tusb_desc_hs_config[] = {
273-
_TUD_CONFIG_DESC(1),
307+
_TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ID, TUSBD_DESC_TOTAL_LEN),
308+
#if CONFIG_TUSBD_CDC_NUMOF > 0
309+
_TUD_CDC_DESC(_tusb_speed_hs, 0),
310+
#endif
311+
#if CONFIG_TUSBD_CDC_NUMOF > 1
312+
_TUD_CDC_DESC(_tusb_speed_hs, 1),
313+
#endif
314+
#if CONFIG_TUSBD_HID_NUMOF > 0
315+
_TUD_HID_INOUT_DESC(_tusb_speed_hs, 0),
316+
#endif
317+
#if CONFIG_TUSBD_HID_NUMOF > 1
318+
_TUD_HID_INOUT_DESC(_tusb_speed_hs, 1),
319+
#endif
320+
#if CONFIG_TUSBD_MSC_NUMOF
321+
_TUD_MSC_DESC(_tusb_speed_hs),
322+
#endif
323+
#if CONFIG_TUSBD_VENDOR_NUMOF
324+
_TUD_VENDOR_DESC(_tusb_speed_hs),
325+
#endif
326+
};
327+
328+
#if _TUD_CONFIG_DESC_NUMOF == 2
329+
__attribute__((weak))
330+
uint8_t const tusb_desc_hs_config_alt[] = {
331+
_TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ALT_ID, TUSBD_DESC_ALT_TOTAL_LEN),
274332
#if CONFIG_TUSBD_CDC_NUMOF > 0
275333
_TUD_CDC_DESC(_tusb_speed_hs, 0),
276334
#endif
@@ -290,6 +348,7 @@ uint8_t const tusb_desc_hs_config[] = {
290348
_TUD_VENDOR_DESC(_tusb_speed_hs),
291349
#endif
292350
};
351+
#endif /* _TUD_CONFIG_DESC_NUMOF == 2 */
293352

294353
/* other speed configuration */
295354
uint8_t tusb_desc_other_speed_config[TUSBD_DESC_TOTAL_LEN];
@@ -316,7 +375,7 @@ tusb_desc_device_qualifier_t const tusb_desc_device_qualifier = {
316375
#endif
317376

318377
.bMaxPacketSize0 = CONFIG_TUSBD_EP0_SIZE,
319-
.bNumConfigurations = 0x01,
378+
.bNumConfigurations = _TUD_CONFIG_DESC_NUMOF,
320379
.bReserved = 0x00
321380
};
322381

@@ -344,16 +403,35 @@ uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index)
344403
{
345404
DEBUG("[tinyusb] %s: %u\n", __func__, index);
346405

347-
/* If the link speed is HS, return the FS config, and vice versa.
348-
* Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */
349-
memcpy(tusb_desc_other_speed_config,
350-
(tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config
351-
: tusb_desc_hs_config,
352-
TUSBD_DESC_TOTAL_LEN);
406+
assert(index < _TUD_CONFIG_DESC_NUMOF);
407+
408+
if (index == _TUD_CONFIG_DESC_ID) {
409+
/* If the link speed is HS, return the FS config, and vice versa.
410+
* Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */
411+
memcpy(tusb_desc_other_speed_config,
412+
(tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config
413+
: tusb_desc_hs_config,
414+
TUSBD_DESC_TOTAL_LEN);
353415

354-
tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
416+
tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
355417

356-
return tusb_desc_other_speed_config;
418+
return tusb_desc_other_speed_config;
419+
}
420+
#if _TUD_CONFIG_DESC_NUMOF == 2
421+
else if (index == _TUD_CONFIG_DESC_ALT_ID) {
422+
/* If the link speed is HS, return the FS config, and vice versa.
423+
* Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */
424+
memcpy(tusb_desc_other_speed_config,
425+
(tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config_alt
426+
: tusb_desc_hs_config_alt,
427+
TUSBD_DESC_ALT_TOTAL_LEN);
428+
429+
tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
430+
431+
return tusb_desc_other_speed_config;
432+
}
433+
#endif
434+
return NULL;
357435
}
358436

359437
#endif /* TUD_OPT_HIGH_SPEED */
@@ -368,13 +446,31 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
368446
{
369447
DEBUG("[tinyusb] %s: %u\n", __func__, index);
370448

449+
assert(index < _TUD_CONFIG_DESC_NUMOF);
450+
451+
if (index == _TUD_CONFIG_DESC_ID) {
371452
#if TUD_OPT_HIGH_SPEED
372-
/* Although we are HS, host may be FS. */
373-
return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config
374-
: tusb_desc_fs_config;
375-
#else
376-
return (uint8_t const *)tusb_desc_fs_config;
377-
#endif
453+
/* Although we are HS, host may be FS. */
454+
return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config
455+
: tusb_desc_fs_config;
456+
#else /* TUD_OPT_HIGH_SPEED */
457+
return (uint8_t const *)tusb_desc_fs_config;
458+
#endif /* TUD_OPT_HIGH_SPEED */
459+
}
460+
461+
#if _TUD_CONFIG_DESC_NUMOF == 2
462+
else if (index == _TUD_CONFIG_DESC_ALT_ID) {
463+
#if TUD_OPT_HIGH_SPEED
464+
/* Although we are HS, host may be FS. */
465+
return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config_alt
466+
: tusb_desc_fs_config_alt;
467+
#else /* TUD_OPT_HIGH_SPEED */
468+
return (uint8_t const *)tusb_desc_fs_config_alt;
469+
#endif /* TUD_OPT_HIGH_SPEED */
470+
}
471+
#endif /* _TUD_CONFIG_DESC_NUMOF == 2 */
472+
473+
return NULL;
378474
}
379475

380476
/*

0 commit comments

Comments
 (0)