Skip to content

Commit 766a14b

Browse files
authored
Merge pull request #370 from adafruit/rework-config-descriptor-builder
Rework config descriptor builder
2 parents 7a926e8 + a9262b4 commit 766a14b

19 files changed

+221
-86
lines changed

.github/workflows/githubci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Setup Python
14-
uses: actions/setup-python@v4
14+
uses: actions/setup-python@v5
1515
with:
1616
python-version: '3.x'
1717

@@ -60,7 +60,7 @@ jobs:
6060

6161
steps:
6262
- name: Setup Python
63-
uses: actions/setup-python@v4
63+
uses: actions/setup-python@v5
6464
with:
6565
python-version: '3.x'
6666

examples/Vendor/i2c_tiny_usb_adapter/Adafruit_USBD_I2C.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,28 @@ Adafruit_USBD_I2C::Adafruit_USBD_I2C(TwoWire* wire) {
6464
setStringDescriptor("I2C Interface");
6565
}
6666

67-
uint16_t Adafruit_USBD_I2C::getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) {
68-
uint8_t desc[] = { TUD_VENDOR_DESCRIPTOR(itfnum, 0, 0x00, 0x80, 64) };
67+
uint16_t Adafruit_USBD_I2C::getInterfaceDescriptor(uint8_t itfnum_deprecated, uint8_t* buf, uint16_t bufsize) {
68+
uint8_t itfnum = 0;
69+
uint8_t ep_in = 0;
70+
uint8_t ep_out = 0;
71+
72+
// null buffer is used to get the length of descriptor only
73+
if (buf) {
74+
itfnum = TinyUSBDevice.allocInterface(1);
75+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
76+
ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
77+
}
78+
79+
uint8_t const desc[] = { TUD_VENDOR_DESCRIPTOR(itfnum, _strid, ep_out, ep_in, 64) };
6980
uint16_t const len = sizeof(desc);
81+
7082
if (buf) {
7183
if (bufsize < len) {
7284
return 0;
7385
}
7486
memcpy(buf, desc, len);
7587
}
88+
7689
return len;
7790
}
7891

examples/Vendor/i2c_tiny_usb_adapter/Adafruit_USBD_I2C.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@
8888
class Adafruit_USBD_I2C : public Adafruit_USBD_Interface {
8989
public:
9090
Adafruit_USBD_I2C(TwoWire* wire);
91-
uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize);
9291
bool begin(uint8_t* buffer, size_t bufsize);
9392

9493
bool handleControlTransfer(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request);
9594

95+
// from Adafruit_USBD_Interface
96+
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize);
97+
9698
private:
9799
TwoWire* _wire;
98100
uint8_t _state;

src/arduino/Adafruit_TinyUSB_API.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
// API Version, need to be updated when there is changes for
3232
// TinyUSB_API, USBD_CDC, USBD_Device, USBD_Interface,
33-
#define TINYUSB_API_VERSION 20000
33+
#define TINYUSB_API_VERSION 20400
3434

3535
//--------------------------------------------------------------------+
3636
// Core API

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,42 @@ Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) { _instance = INVALID_INSTANCE; }
4646

4747
#if CFG_TUD_ENABLED
4848

49-
#define EPOUT 0x00
50-
#define EPIN 0x80
51-
52-
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
49+
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum_deprecated,
50+
uint8_t *buf,
5351
uint16_t bufsize) {
52+
(void)itfnum_deprecated;
53+
5454
// CDC is mostly always existed for DFU
55-
// usb core will automatically update endpoint number
56-
uint8_t desc[] = {TUD_CDC_DESCRIPTOR(itfnum, 0, EPIN, 8, EPOUT, EPIN, 64)};
55+
uint8_t itfnum = 0;
56+
uint8_t ep_notif = 0;
57+
uint8_t ep_in = 0;
58+
uint8_t ep_out = 0;
59+
60+
if (buf) {
61+
itfnum = TinyUSBDevice.allocInterface(2);
62+
ep_notif = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
63+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
64+
ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
65+
}
66+
67+
#if TINYUSB_API_VERSION < 20400
68+
// backward compatible for core that include pre-2.4.0 TinyUSB
69+
uint8_t _strid = 0;
70+
#endif
71+
72+
uint8_t const desc[] = {
73+
TUD_CDC_DESCRIPTOR(itfnum, _strid, ep_notif, 8, ep_out, ep_in, 64)};
74+
5775
uint16_t const len = sizeof(desc);
5876

59-
if (bufsize < len) {
60-
return 0;
77+
// null buffer is used to get the length of descriptor only
78+
if (buf) {
79+
if (bufsize < len) {
80+
return 0;
81+
}
82+
memcpy(buf, desc, len);
6183
}
6284

63-
memcpy(buf, desc, len);
6485
return len;
6586
}
6687

@@ -268,9 +289,10 @@ void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) {
268289
// Device stack is not enabled (probably in host mode)
269290
#warning "TinyUSB Host selected. No output to Serial will occur!"
270291

271-
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
292+
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum_deprecated,
293+
uint8_t *buf,
272294
uint16_t bufsize) {
273-
(void)itfnum;
295+
(void)itfnum_deprecated;
274296
(void)buf;
275297
(void)bufsize;
276298

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
4545

4646
static uint8_t getInstanceCount(void) { return _instance_count; }
4747

48-
// from Adafruit_USBD_Interface
49-
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
50-
uint16_t bufsize);
51-
5248
void setPins(uint8_t pin_rx, uint8_t pin_tx) {
5349
(void)pin_rx;
5450
(void)pin_tx;
@@ -83,6 +79,10 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
8379
using Print::write; // pull in write(str) from Print
8480
operator bool();
8581

82+
// from Adafruit_USBD_Interface
83+
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated,
84+
uint8_t *buf, uint16_t bufsize);
85+
8686
private:
8787
enum { INVALID_INSTANCE = 0xffu };
8888
static uint8_t _instance_count;

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,15 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
235235
uint8_t *desc = _desc_cfg + _desc_cfg_len;
236236
uint16_t const len = itf.getInterfaceDescriptor(
237237
_itf_count, desc, _desc_cfg_maxlen - _desc_cfg_len);
238-
uint8_t *desc_end = desc + len;
239-
240-
const char *desc_str = itf.getStringDescriptor();
241238

242239
if (!len) {
243240
return false;
244241
}
245242

243+
#if 0
244+
uint8_t *desc_end = desc + len;
245+
const char *desc_str = itf.getStringDescriptor();
246+
246247
// Parse interface descriptor to update
247248
// - IAD: interface number
248249
// - Interface: number & string descriptor
@@ -321,6 +322,7 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
321322

322323
desc += tu_desc_len(desc);
323324
}
325+
#endif
324326

325327
_desc_cfg_len += len;
326328

src/arduino/Adafruit_USBD_Device.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,26 @@ class Adafruit_USBD_Device {
7171

7272
//------------- Configuration descriptor -------------//
7373

74-
// Add an new interface
74+
// Add a new interface
7575
bool addInterface(Adafruit_USBD_Interface &itf);
7676

7777
// Clear/Reset configuration descriptor
7878
void clearConfiguration(void);
7979

80-
// Provide user buffer for configuration descriptor, needed if total length >
81-
// 256
80+
// Provide user buffer for configuration descriptor, if total length > 256
8281
void setConfigurationBuffer(uint8_t *buf, uint32_t buflen);
8382

83+
// Allocate a new interface number
84+
uint8_t allocInterface(uint8_t count = 1) {
85+
uint8_t ret = _itf_count;
86+
_itf_count += count;
87+
return ret;
88+
}
89+
90+
uint8_t allocEndpoint(uint8_t in) {
91+
return in ? (0x80 | _epin_count++) : _epout_count++;
92+
}
93+
8494
//------------- String descriptor -------------//
8595
void setLanguageDescriptor(uint16_t language_id);
8696
void setManufacturerDescriptor(const char *s);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Ha Thach (tinyusb.org) for Adafruit Industries
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+
#include "tusb_option.h"
26+
27+
#if CFG_TUD_ENABLED
28+
29+
#include "Adafruit_USBD_Device.h"
30+
31+
void Adafruit_USBD_Interface::setStringDescriptor(const char *str) {
32+
_strid = TinyUSBDevice.addStringDescriptor(str);
33+
}
34+
35+
#endif

src/arduino/Adafruit_USBD_Interface.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@
3030

3131
class Adafruit_USBD_Interface {
3232
protected:
33-
const char *_desc_str;
33+
uint8_t _strid;
3434

3535
public:
36-
Adafruit_USBD_Interface(void) { _desc_str = NULL; }
36+
Adafruit_USBD_Interface(void) { _strid = 0; }
3737

3838
// Get Interface Descriptor
3939
// Fill the descriptor (if buf is not NULL) and return its length
40-
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
41-
uint16_t bufsize) = 0;
40+
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated,
41+
uint8_t *buf, uint16_t bufsize) = 0;
42+
// Get Interface Descriptor Length
43+
uint16_t getInterfaceDescriptorLen() {
44+
return getInterfaceDescriptor(0, NULL, 0);
45+
}
4246

43-
void setStringDescriptor(const char *str) { _desc_str = str; }
44-
const char *getStringDescriptor(void) { return _desc_str; }
47+
void setStringDescriptor(const char *str);
4548
};
4649

4750
#endif

src/arduino/hid/Adafruit_USBD_HID.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828

2929
#include "Adafruit_USBD_HID.h"
3030

31-
#define EPOUT 0x00
32-
#define EPIN 0x80
33-
3431
uint8_t const _ascii2keycode[128][2] = {HID_ASCII_TO_KEYCODE};
3532
static Adafruit_USBD_HID *_hid_instances[CFG_TUD_HID] = {0};
3633

@@ -94,7 +91,7 @@ Adafruit_USBD_HID::Adafruit_USBD_HID(uint8_t const *desc_report, uint16_t len,
9491
_instance = _instance_count++;
9592
_hid_instances[_instance] = this;
9693

97-
uint16_t const desc_len = getInterfaceDescriptor(0, NULL, 0);
94+
uint16_t const desc_len = getInterfaceDescriptorLen();
9895
tinyusb_enable_interface(USB_INTERFACE_HID, desc_len, hid_load_descriptor);
9996
#endif
10097
}
@@ -132,11 +129,11 @@ uint16_t Adafruit_USBD_HID::makeItfDesc(uint8_t itfnum, uint8_t *buf,
132129
return 0;
133130
}
134131

135-
uint8_t const desc_inout[] = {
136-
TUD_HID_INOUT_DESCRIPTOR(itfnum, 0, _protocol, _desc_report_len, ep_in,
137-
ep_out, CFG_TUD_HID_EP_BUFSIZE, _interval_ms)};
132+
uint8_t const desc_inout[] = {TUD_HID_INOUT_DESCRIPTOR(
133+
itfnum, _strid, _protocol, _desc_report_len, ep_in, ep_out,
134+
CFG_TUD_HID_EP_BUFSIZE, _interval_ms)};
138135
uint8_t const desc_in_only[] = {
139-
TUD_HID_DESCRIPTOR(itfnum, 0, _protocol, _desc_report_len, ep_in,
136+
TUD_HID_DESCRIPTOR(itfnum, _strid, _protocol, _desc_report_len, ep_in,
140137
CFG_TUD_HID_EP_BUFSIZE, _interval_ms)};
141138

142139
uint8_t const *desc;
@@ -155,17 +152,32 @@ uint16_t Adafruit_USBD_HID::makeItfDesc(uint8_t itfnum, uint8_t *buf,
155152
if (bufsize < len) {
156153
return 0;
157154
}
158-
159155
memcpy(buf, desc, len);
160156
}
161157

162158
return len;
163159
}
164160

165-
uint16_t Adafruit_USBD_HID::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
161+
uint16_t Adafruit_USBD_HID::getInterfaceDescriptor(uint8_t itfnum_deprecated,
162+
uint8_t *buf,
166163
uint16_t bufsize) {
167-
// usb core will automatically update endpoint number
168-
return makeItfDesc(itfnum, buf, bufsize, EPIN, EPOUT);
164+
(void)itfnum_deprecated;
165+
166+
uint8_t itfnum = 0;
167+
uint8_t ep_in = 0;
168+
uint8_t ep_out = 0;
169+
170+
// null buffer is used to get the length of descriptor only
171+
if (buf) {
172+
itfnum = TinyUSBDevice.allocInterface(1);
173+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
174+
175+
if (_out_endpoint) {
176+
TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
177+
}
178+
}
179+
180+
return makeItfDesc(itfnum, buf, bufsize, ep_in, ep_out);
169181
}
170182

171183
bool Adafruit_USBD_HID::begin(void) {

src/arduino/hid/Adafruit_USBD_HID.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ class Adafruit_USBD_HID : public Adafruit_USBD_Interface {
8080
bool mouseButtonRelease(uint8_t report_id);
8181

8282
// from Adafruit_USBD_Interface
83-
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
84-
uint16_t bufsize);
83+
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated,
84+
uint8_t *buf, uint16_t bufsize);
8585

8686
// internal use only
8787
uint16_t makeItfDesc(uint8_t itfnum, uint8_t *buf, uint16_t bufsize,

0 commit comments

Comments
 (0)