@@ -211,7 +211,14 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
211211 desc_cb .jack_num = 0 ;
212212
213213 // There can be just a MIDI or an Audio + MIDI interface
214- // If there is Audio Control Interface + Audio Header descriptor, skip it
214+ // - If there is Audio Control Interface + Audio Header descriptor, then skip it.
215+ // - If there is an Audio Control Interface + Audio Streaming Interface, then ignore the Audio Streaming Interface.
216+ // Future:
217+ // Note that if this driver is used with an USB Audio Streaming host driver,
218+ // then call that driver first. If the MIDI interface comes before the
219+ // audio streaming interface, then the audio driver will have to call this
220+ // driver after parsing the audio control interface and then resume parsing
221+ // the streaming audio interface.
215222 if (AUDIO_SUBCLASS_CONTROL == desc_itf -> bInterfaceSubClass ) {
216223 TU_VERIFY (max_len > 2 * sizeof (tusb_desc_interface_t ) + sizeof (audio_desc_cs_ac_interface_t ));
217224
@@ -222,8 +229,18 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
222229
223230 p_desc = tu_desc_next (p_desc );
224231 desc_itf = (const tusb_desc_interface_t * )p_desc ;
225- TU_VERIFY (TUSB_CLASS_AUDIO == desc_itf -> bInterfaceClass );
226232 p_midi -> itf_count = 1 ;
233+ // skip non-interface and non-midi streaming descriptors
234+ while (tu_desc_in_bounds (p_desc , p_end ) &&
235+ (desc_itf -> bDescriptorType != TUSB_DESC_INTERFACE || (desc_itf -> bInterfaceClass == TUSB_CLASS_AUDIO && desc_itf -> bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING ))) {
236+ if (desc_itf -> bDescriptorType == TUSB_DESC_INTERFACE && desc_itf -> bAlternateSetting == 0 ) {
237+ p_midi -> itf_count ++ ;
238+ }
239+ p_desc = tu_desc_next (p_desc );
240+ desc_itf = (tusb_desc_interface_t const * )p_desc ;
241+ }
242+ TU_VERIFY (p_desc < p_end ); // TODO: If MIDI interface comes after Audio Streaming, then max_len did not include the MIDI interface descriptor
243+ TU_VERIFY (TUSB_CLASS_AUDIO == desc_itf -> bInterfaceClass );
227244 }
228245 TU_VERIFY (AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf -> bInterfaceSubClass );
229246
@@ -236,7 +253,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
236253 p_desc = tu_desc_next (p_desc ); // next to CS Header
237254
238255 bool found_new_interface = false;
239- while ((p_desc < p_end ) && ( tu_desc_next ( p_desc ) <= p_end ) && !found_new_interface ) {
256+ while (tu_desc_in_bounds (p_desc , p_end ) && !found_new_interface ) {
240257 switch (tu_desc_type (p_desc )) {
241258 case TUSB_DESC_INTERFACE :
242259 found_new_interface = true;
0 commit comments