Skip to content

Commit e174b2d

Browse files
authored
Merge pull request #554 from adafruit/update-tinyusb-0.19.0
Update tinyusb to 0.19.0
2 parents b66f12b + d0a75e4 commit e174b2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+933
-466
lines changed

src/arduino/webusb/Adafruit_USBD_WebUSB.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void Adafruit_USBD_WebUSB::flush(void) { tud_vendor_flush(); }
245245
//--------------------------------------------------------------------+
246246
extern "C" {
247247

248-
uint8_t const *tud_descriptor_bos_cb(void) { return desc_bos; }
248+
TU_ATTR_WEAK uint8_t const *tud_descriptor_bos_cb(void) { return desc_bos; }
249249

250250
// Invoked when a control transfer occurred on an interface of this class
251251
// Driver response accordingly to the request and the transfer stage

src/class/audio/audio_device.c

Lines changed: 37 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,6 @@ tu_static CFG_TUD_MEM_SECTION struct {
192192
#endif
193193
} ctrl_buf;
194194

195-
// Active alternate setting of interfaces
196-
tu_static uint8_t alt_setting_1[CFG_TUD_AUDIO_FUNC_1_N_AS_INT];
197-
198-
#if CFG_TUD_AUDIO > 1 && CFG_TUD_AUDIO_FUNC_2_N_AS_INT > 0
199-
tu_static uint8_t alt_setting_2[CFG_TUD_AUDIO_FUNC_2_N_AS_INT];
200-
#endif
201-
202-
#if CFG_TUD_AUDIO > 2 && CFG_TUD_AUDIO_FUNC_3_N_AS_INT > 0
203-
tu_static uint8_t alt_setting_3[CFG_TUD_AUDIO_FUNC_3_N_AS_INT];
204-
#endif
205-
206195
// Aligned buffer for feedback EP
207196
#if CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
208197
tu_static CFG_TUD_MEM_SECTION struct {
@@ -234,13 +223,14 @@ typedef struct
234223
uint8_t ep_in; // TX audio data EP.
235224
uint16_t ep_in_sz; // Current size of TX EP
236225
uint8_t ep_in_as_intf_num;// Corresponding Standard AS Interface Descriptor (4.9.1) belonging to output terminal to which this EP belongs - 0 is invalid (this fits to UAC2 specification since AS interfaces can not have interface number equal to zero)
237-
#endif
226+
uint8_t ep_in_alt; // Current alternate setting of TX EP
227+
#endif
238228

239229
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
240230
uint8_t ep_out; // Incoming (into uC) audio data EP.
241231
uint16_t ep_out_sz; // Current size of RX EP
242232
uint8_t ep_out_as_intf_num;// Corresponding Standard AS Interface Descriptor (4.9.1) belonging to input terminal to which this EP belongs - 0 is invalid (this fits to UAC2 specification since AS interfaces can not have interface number equal to zero)
243-
233+
uint8_t ep_out_alt; // Current alternate setting of RX EP
244234
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
245235
uint8_t ep_fb;// Feedback EP.
246236
#endif
@@ -304,9 +294,6 @@ typedef struct
304294
uint8_t *ctrl_buf;
305295
uint8_t ctrl_buf_sz;
306296

307-
// Current active alternate settings
308-
uint8_t *alt_setting;// We need to save the current alternate setting this way, because it is possible that there are AS interfaces which do not have an EP!
309-
310297
// EP Transfer buffers and FIFOs
311298
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
312299
tu_fifo_t ep_out_ff;
@@ -475,8 +462,6 @@ static bool audiod_tx_xfer_isr(uint8_t rhport, audiod_function_t* audio, uint16_
475462
static bool audiod_get_interface(uint8_t rhport, tusb_control_request_t const *p_request);
476463
static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *p_request);
477464

478-
static bool audiod_get_AS_interface_index_global(uint8_t itf, uint8_t *func_id, uint8_t *idxItf, uint8_t const **pp_desc_int);
479-
static bool audiod_get_AS_interface_index(uint8_t itf, audiod_function_t *audio, uint8_t *idxItf, uint8_t const **pp_desc_int);
480465
static bool audiod_verify_entity_exists(uint8_t itf, uint8_t entityID, uint8_t *func_id);
481466
static bool audiod_verify_itf_exists(uint8_t itf, uint8_t *func_id);
482467
static bool audiod_verify_ep_exists(uint8_t ep, uint8_t *func_id);
@@ -529,11 +514,7 @@ tu_fifo_t *tud_audio_n_get_ep_out_ff(uint8_t func_id) {
529514
}
530515

531516
static bool audiod_rx_xfer_isr(uint8_t rhport, audiod_function_t* audio, uint16_t n_bytes_received) {
532-
uint8_t idxItf;
533-
uint8_t const *dummy2;
534-
535517
uint8_t idx_audio_fct = audiod_get_audio_fct_idx(audio);
536-
TU_VERIFY(audiod_get_AS_interface_index(audio->ep_out_as_intf_num, audio, &idxItf, &dummy2));
537518

538519
#if USE_LINEAR_BUFFER_RX
539520
// Data currently is in linear buffer, copy into EP OUT FIFO
@@ -553,7 +534,7 @@ static bool audiod_rx_xfer_isr(uint8_t rhport, audiod_function_t* audio, uint16_
553534
#endif
554535

555536
// Call a weak callback here - a possibility for user to get informed an audio packet was received and data gets now loaded into EP FIFO
556-
TU_VERIFY(tud_audio_rx_done_isr(rhport, n_bytes_received, idx_audio_fct, audio->ep_out, audio->alt_setting[idxItf]));
537+
TU_VERIFY(tud_audio_rx_done_isr(rhport, n_bytes_received, idx_audio_fct, audio->ep_out, audio->ep_out_alt));
557538

558539
return true;
559540
}
@@ -584,14 +565,10 @@ tu_fifo_t *tud_audio_n_get_ep_in_ff(uint8_t func_id) {
584565
}
585566

586567
static bool audiod_tx_xfer_isr(uint8_t rhport, audiod_function_t * audio, uint16_t n_bytes_sent) {
587-
uint8_t idxItf;
588-
uint8_t const *dummy2;
589-
590568
uint8_t idx_audio_fct = audiod_get_audio_fct_idx(audio);
591-
TU_VERIFY(audiod_get_AS_interface_index(audio->ep_in_as_intf_num, audio, &idxItf, &dummy2));
592569

593570
// Only send something if current alternate interface is not 0 as in this case nothing is to be sent due to UAC2 specifications
594-
if (audio->alt_setting[idxItf] == 0) { return false; }
571+
if (audio->ep_in_alt == 0) { return false; }
595572

596573
// Send everything in ISO EP FIFO
597574
uint16_t n_bytes_tx;
@@ -611,7 +588,7 @@ static bool audiod_tx_xfer_isr(uint8_t rhport, audiod_function_t * audio, uint16
611588
#endif
612589

613590
// Call a weak callback here - a possibility for user to get informed former TX was completed and data gets now loaded into EP in buffer
614-
TU_VERIFY(tud_audio_tx_done_isr(rhport, n_bytes_sent, idx_audio_fct, audio->ep_in, audio->alt_setting[idxItf]));
591+
TU_VERIFY(tud_audio_tx_done_isr(rhport, n_bytes_sent, idx_audio_fct, audio->ep_in, audio->ep_in_alt));
615592

616593
return true;
617594
}
@@ -704,25 +681,6 @@ void audiod_init(void) {
704681
#endif
705682
}
706683

707-
// Initialize active alternate interface buffers
708-
switch (i) {
709-
#if CFG_TUD_AUDIO_FUNC_1_N_AS_INT > 0
710-
case 0:
711-
audio->alt_setting = alt_setting_1;
712-
break;
713-
#endif
714-
#if CFG_TUD_AUDIO > 1 && CFG_TUD_AUDIO_FUNC_2_N_AS_INT > 0
715-
case 1:
716-
audio->alt_setting = alt_setting_2;
717-
break;
718-
#endif
719-
#if CFG_TUD_AUDIO > 2 && CFG_TUD_AUDIO_FUNC_3_N_AS_INT > 0
720-
case 2:
721-
audio->alt_setting = alt_setting_3;
722-
break;
723-
#endif
724-
}
725-
726684
// Initialize IN EP FIFO if required
727685
#if CFG_TUD_AUDIO_ENABLE_EP_IN
728686

@@ -1029,13 +987,25 @@ static bool audiod_get_interface(uint8_t rhport, tusb_control_request_t const *p
1029987
uint8_t const itf = tu_u16_low(p_request->wIndex);
1030988

1031989
// Find index of audio streaming interface
1032-
uint8_t func_id, idxItf;
1033-
uint8_t const *dummy;
990+
uint8_t func_id;
991+
TU_VERIFY(audiod_verify_itf_exists(itf, &func_id));
992+
993+
// Default to 0 if interface not yet activated
994+
uint8_t alt = 0;
995+
#if CFG_TUD_AUDIO_ENABLE_EP_IN
996+
if (_audiod_fct[func_id].ep_in_as_intf_num == itf) {
997+
alt = _audiod_fct[func_id].ep_in_alt;
998+
}
999+
#endif
1000+
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
1001+
if (_audiod_fct[func_id].ep_out_as_intf_num == itf) {
1002+
alt = _audiod_fct[func_id].ep_out_alt;
1003+
}
1004+
#endif
10341005

1035-
TU_VERIFY(audiod_get_AS_interface_index_global(itf, &func_id, &idxItf, &dummy));
1036-
TU_VERIFY(tud_control_xfer(rhport, p_request, &_audiod_fct[func_id].alt_setting[idxItf], 1));
1006+
TU_VERIFY(tud_control_xfer(rhport, p_request, &alt, 1));
10371007

1038-
TU_LOG2(" Get itf: %u - current alt: %u\r\n", itf, _audiod_fct[func_id].alt_setting[idxItf]);
1008+
TU_LOG2(" Get itf: %u - current alt: %u\r\n", itf, alt);
10391009

10401010
return true;
10411011
}
@@ -1060,16 +1030,16 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *p
10601030
TU_LOG2(" Set itf: %u - alt: %u\r\n", itf, alt);
10611031

10621032
// Find index of audio streaming interface and index of interface
1063-
uint8_t func_id, idxItf;
1064-
uint8_t const *p_desc;
1065-
TU_VERIFY(audiod_get_AS_interface_index_global(itf, &func_id, &idxItf, &p_desc));
1033+
uint8_t func_id;
1034+
TU_VERIFY(audiod_verify_itf_exists(itf, &func_id));
10661035

10671036
audiod_function_t *audio = &_audiod_fct[func_id];
10681037

10691038
// Look if there is an EP to be closed - for this driver, there are only 3 possible EPs which may be closed (only AS related EPs can be closed, AC EP (if present) is always open)
10701039
#if CFG_TUD_AUDIO_ENABLE_EP_IN
10711040
if (audio->ep_in_as_intf_num == itf) {
10721041
audio->ep_in_as_intf_num = 0;
1042+
audio->ep_in_alt = 0;
10731043
#ifndef TUP_DCD_EDPT_ISO_ALLOC
10741044
usbd_edpt_close(rhport, audio->ep_in);
10751045
#endif
@@ -1093,6 +1063,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *p
10931063
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
10941064
if (audio->ep_out_as_intf_num == itf) {
10951065
audio->ep_out_as_intf_num = 0;
1066+
audio->ep_out_alt = 0;
10961067
#ifndef TUP_DCD_EDPT_ISO_ALLOC
10971068
usbd_edpt_close(rhport, audio->ep_out);
10981069
#endif
@@ -1116,10 +1087,10 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *p
11161087
}
11171088
#endif// CFG_TUD_AUDIO_ENABLE_EP_OUT
11181089

1119-
// Save current alternative interface setting
1120-
audio->alt_setting[idxItf] = alt;
1121-
11221090
// Open new EP if necessary - EPs are only to be closed or opened for AS interfaces - Look for AS interface with correct alternate interface
1091+
uint8_t const *p_desc = tu_desc_next(audio->p_desc);
1092+
// Skip entire AC descriptor block
1093+
p_desc += ((audio_desc_cs_ac_interface_t const *) p_desc)->wTotalLength;
11231094
// Get pointer at end
11241095
uint8_t const *p_desc_end = audio->p_desc + audio->desc_length - TUD_AUDIO_DESC_IAD_LEN;
11251096

@@ -1154,6 +1125,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *p
11541125
// Save address
11551126
audio->ep_in = ep_addr;
11561127
audio->ep_in_as_intf_num = itf;
1128+
audio->ep_in_alt = alt;
11571129
audio->ep_in_sz = tu_edpt_packet_size(desc_ep);
11581130

11591131
// If flow control is enabled, parse for the corresponding parameters - doing this here means only AS interfaces with EPs get scanned for parameters
@@ -1176,6 +1148,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *p
11761148
// Save address
11771149
audio->ep_out = ep_addr;
11781150
audio->ep_out_as_intf_num = itf;
1151+
audio->ep_out_alt = alt;
11791152
audio->ep_out_sz = tu_edpt_packet_size(desc_ep);
11801153

11811154
// Prepare for incoming data
@@ -1474,8 +1447,7 @@ bool audiod_xfer_isr(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
14741447
#if CFG_TUD_AUDIO_ENABLE_EP_IN
14751448

14761449
// Data transmission of audio packet finished
1477-
if (audio->ep_in == ep_addr && audio->alt_setting != 0)
1478-
{
1450+
if (audio->ep_in == ep_addr) {
14791451
// USB 2.0, section 5.6.4, third paragraph, states "An isochronous endpoint must specify its required bus access period. However, an isochronous endpoint must be prepared to handle poll rates faster than the one specified."
14801452
// That paragraph goes on to say "An isochronous IN endpoint must return a zero-length packet whenever data is requested at a faster interval than the specified interval and data is not available."
14811453
// This can only be solved reliably if we load a ZLP after every IN transmission since we can not say if the host requests samples earlier than we declared! Once all samples are collected we overwrite the loaded ZLP.
@@ -1492,8 +1464,7 @@ bool audiod_xfer_isr(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
14921464

14931465
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
14941466
// New audio packet received
1495-
if (audio->ep_out == ep_addr)
1496-
{
1467+
if (audio->ep_out == ep_addr) {
14971468
audiod_rx_xfer_isr(rhport, audio, (uint16_t) xferred_bytes);
14981469
return true;
14991470
}
@@ -1694,53 +1665,6 @@ bool tud_audio_buffer_and_schedule_control_xfer(uint8_t rhport, tusb_control_req
16941665
return tud_control_xfer(rhport, p_request, (void *) _audiod_fct[func_id].ctrl_buf, len);
16951666
}
16961667

1697-
// This helper function finds for a given audio function and AS interface number the index of the attached driver structure, the index of the interface in the audio function
1698-
// (e.g. the std. AS interface with interface number 15 is the first AS interface for the given audio function and thus gets index zero), and
1699-
// finally a pointer to the std. AS interface, where the pointer always points to the first alternate setting i.e. alternate interface zero.
1700-
static bool audiod_get_AS_interface_index(uint8_t itf, audiod_function_t *audio, uint8_t *idxItf, uint8_t const **pp_desc_int) {
1701-
if (audio->p_desc) {
1702-
// Get pointer at end
1703-
uint8_t const *p_desc_end = audio->p_desc + audio->desc_length - TUD_AUDIO_DESC_IAD_LEN;
1704-
1705-
// Advance past AC descriptors
1706-
uint8_t const *p_desc = tu_desc_next(audio->p_desc);
1707-
p_desc += ((audio_desc_cs_ac_interface_t const *) p_desc)->wTotalLength;
1708-
1709-
uint8_t tmp = 0;
1710-
// Condition modified from p_desc < p_desc_end to prevent gcc>=12 strict-overflow warning
1711-
while (p_desc_end - p_desc > 0) {
1712-
// We assume the number of alternate settings is increasing thus we return the index of alternate setting zero!
1713-
if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE && ((tusb_desc_interface_t const *) p_desc)->bAlternateSetting == 0) {
1714-
if (((tusb_desc_interface_t const *) p_desc)->bInterfaceNumber == itf) {
1715-
*idxItf = tmp;
1716-
*pp_desc_int = p_desc;
1717-
return true;
1718-
}
1719-
// Increase index, bytes read, and pointer
1720-
tmp++;
1721-
}
1722-
p_desc = tu_desc_next(p_desc);
1723-
}
1724-
}
1725-
return false;
1726-
}
1727-
1728-
// This helper function finds for a given AS interface number the index of the attached driver structure, the index of the interface in the audio function
1729-
// (e.g. the std. AS interface with interface number 15 is the first AS interface for the given audio function and thus gets index zero), and
1730-
// finally a pointer to the std. AS interface, where the pointer always points to the first alternate setting i.e. alternate interface zero.
1731-
static bool audiod_get_AS_interface_index_global(uint8_t itf, uint8_t *func_id, uint8_t *idxItf, uint8_t const **pp_desc_int) {
1732-
// Loop over audio driver interfaces
1733-
uint8_t i;
1734-
for (i = 0; i < CFG_TUD_AUDIO; i++) {
1735-
if (audiod_get_AS_interface_index(itf, &_audiod_fct[i], idxItf, pp_desc_int)) {
1736-
*func_id = i;
1737-
return true;
1738-
}
1739-
}
1740-
1741-
return false;
1742-
}
1743-
17441668
// Verify an entity with the given ID exists and returns also the corresponding driver index
17451669
static bool audiod_verify_entity_exists(uint8_t itf, uint8_t entityID, uint8_t *func_id) {
17461670
uint8_t i;
@@ -1754,8 +1678,8 @@ static bool audiod_verify_entity_exists(uint8_t itf, uint8_t entityID, uint8_t *
17541678

17551679
// Condition modified from p_desc < p_desc_end to prevent gcc>=12 strict-overflow warning
17561680
while (p_desc_end - p_desc > 0) {
1757-
if (p_desc[3] == entityID)// Entity IDs are always at offset 3
1758-
{
1681+
// Entity IDs are always at offset 3
1682+
if (p_desc[3] == entityID) {
17591683
*func_id = i;
17601684
return true;
17611685
}
@@ -1775,7 +1699,7 @@ static bool audiod_verify_itf_exists(uint8_t itf, uint8_t *func_id) {
17751699
uint8_t const *p_desc_end = _audiod_fct[i].p_desc + _audiod_fct[i].desc_length - TUD_AUDIO_DESC_IAD_LEN;
17761700
// Condition modified from p_desc < p_desc_end to prevent gcc>=12 strict-overflow warning
17771701
while (p_desc_end - p_desc > 0) {
1778-
if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE && ((tusb_desc_interface_t const *) _audiod_fct[i].p_desc)->bInterfaceNumber == itf) {
1702+
if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE && ((tusb_desc_interface_t const *)p_desc)->bInterfaceNumber == itf) {
17791703
*func_id = i;
17801704
return true;
17811705
}

src/class/audio/audio_device.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@
5151
#endif
5252
#endif
5353

54-
// Number of Standard AS Interface Descriptors (4.9.1) defined per audio function - this is required to be able to remember the current alternate settings of these interfaces
55-
#ifndef CFG_TUD_AUDIO_FUNC_1_N_AS_INT
56-
#error You must tell the driver the number of Standard AS Interface Descriptors you have defined in the audio function descriptor!
57-
#endif
58-
#if CFG_TUD_AUDIO > 1
59-
#ifndef CFG_TUD_AUDIO_FUNC_2_N_AS_INT
60-
#error You must tell the driver the number of Standard AS Interface Descriptors you have defined in the audio function descriptor!
61-
#endif
62-
#endif
63-
#if CFG_TUD_AUDIO > 2
64-
#ifndef CFG_TUD_AUDIO_FUNC_3_N_AS_INT
65-
#error You must tell the driver the number of Standard AS Interface Descriptors you have defined in the audio function descriptor!
66-
#endif
67-
#endif
68-
6954
// Size of control buffer used to receive and send control messages via EP0 - has to be big enough to hold your biggest request structure e.g. range requests with multiple intervals defined or cluster descriptors
7055
#ifndef CFG_TUD_AUDIO_FUNC_1_CTRL_BUF_SZ
7156
#error You must define an audio class control request buffer size!

0 commit comments

Comments
 (0)