USB host: tolerate more keyboards and retry the probe before NOT USB MIDI

uh_parse_midi refused real descriptor shapes: an OUT-only MIDI interface
ahead of the one with the IN endpoint, an interrupt IN with bInterval 0,
and bulk sizes that are not a power of two. uh_connect gave up after one
probe, so any slow answer after the bus reset also read as NOT USB MIDI.

Now: the parser keeps looking past an OUT-only interface, takes interval 0
as every frame and any bulk size of 4..64; the probe is tried three times
with a bus reset and a longer recovery each, and waits 20 ms after
SET_ADDRESS. tests/usb_host_desc_test.c covers the new shapes (they fail on
the old parser) and runs in voice-user-check. Layout repinned (keep).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
This commit is contained in:
Claude committed 2026-11-27 23:29:56 +00:00
1 parent 6dc63ba325
commit 81582f8fcc
6 files changed
+2305 -2261

No files matched your search

+2
View File
@@ -78,6 +78,8 @@ jobs:
build/song-check/key-trk | tee build/song-check/key-trk.log
cc -O1 -w -Ibuild/gen -Ifirmware/src tests/west_test.c -lm -o build/song-check/west
build/song-check/west | tee build/song-check/west.log
cc -O1 -w -fsanitize=address,bounds tests/usb_host_desc_test.c -o build/song-check/usbhost
build/song-check/usbhost | tee build/song-check/usbhost.log
for test in audio perform upreset; do
cc -O1 -w -Wno-implicit-function-declaration -Ibuild/gen -Ifirmware/src tests/${test}_test.c -lm -o build/song-check/$test
build/song-check/$test | tee build/song-check/$test.log
+15 -7
View File
@@ -172,7 +172,7 @@ UH_SEG static int uh_probe(uint8_t depth, uh_midi_t *m)
cls = d8[4];
if (uh_ctl(0, 0x00, 5, addr, 0, 0, 0, mps) < 0) /* SET_ADDRESS */
return 0;
uh_wait(10);
uh_wait(20); /* (V10.8.69) 20 ms: some keyboards take more than 10 */
r = uh_ctl(addr, 0x80, 6, 0x0200, 0, 9, uh_cfg, mps);
if (r < 9)
return 0;
@@ -348,12 +348,20 @@ UH_SEG static void uh_connect(void) /* a device on th
uh.tried = 1;
return;
}
sie_wr(S_POWER, 0x08u); /* bus reset */
uh_wait(30);
sie_wr(S_POWER, 0);
uh_wait(30);
uh.next_addr = UH_FIRST_ADDR;
if (uh_probe(0, &m)) {
int ok = 0;
for (uint32_t k = 0; ; k++) { /* (V10.8.69) three tries, a bus reset and a longer recovery
* each: a keyboard slow to answer after its reset is not
* "NOT USB MIDI" on the first unanswered request */
sie_wr(S_POWER, 0x08u); /* bus reset */
uh_wait(30);
sie_wr(S_POWER, 0);
uh_wait(30 + 70 * k);
uh.next_addr = UH_FIRST_ADDR;
ok = uh_probe(0, &m);
if (ok || k == 2u)
break;
}
if (ok) {
uh_start_ep(&m);
uh.msg = uh.aud ? UH_MSG_AUDIO : UH_MSG_MIDI;
uh.tried = 0;
+9 -7
View File
@@ -35,7 +35,10 @@ static int uh_parse_midi(const uint8_t *d, uint32_t len, uh_midi_t *m)
if (d[i + 1] == 2u && l >= 9u) {
cfg = d[i + 5];
} else if (d[i + 1] == 4u && l >= 9u) {
if (m->found) break; /* never combine interfaces or alternate settings */
if (m->found && m->in_ep) break; /* never combine interfaces or alternate settings */
if (m->found) /* (V10.8.69) an OUT-only MIDI interface before the one with
* the keyboard's IN: forgotten, keep looking */
*m = (uh_midi_t){0};
m->cfg = cfg;
m->iface = d[i + 2];
m->alt = d[i + 3];
@@ -43,21 +46,20 @@ static int uh_parse_midi(const uint8_t *d, uint32_t len, uh_midi_t *m)
} else if (d[i + 1] == 5u && l >= 7u && in_midi) {
uint8_t ea = d[i + 2], type = d[i + 3] & 3u;
uint16_t mps = (uint16_t)(d[i + 4] | d[i + 5] << 8);
/* Full-speed USB-MIDI 1.0 events are four bytes. The buffers hold at most 64 bytes. */
if ((ea & 15u) && !(ea & 0x70u) && mps >= 4u && mps <= 64u &&
((type == 2u && (mps == 8u || mps == 16u || mps == 32u || mps == 64u)) ||
(type == 3u && d[i + 6]))) {
/* Full-speed USB-MIDI 1.0 events are four bytes. The buffers hold at most 64 bytes. (V10.8.69: any bulk
* size of whole events, an interrupt one with bInterval 0 polled every frame) */
if ((ea & 15u) && !(ea & 0x70u) && mps >= 4u && mps <= 64u && (type == 2u || type == 3u)) {
if ((ea & 0x80u) && !m->in_ep) {
m->found = 1;
m->in_ep = ea & 0x0Fu;
m->in_type = type;
m->in_interval = d[i + 6];
m->in_interval = d[i + 6] ? d[i + 6] : 1u;
m->in_mps = mps;
} else if (!(ea & 0x80u) && !m->out_ep) {
m->found = 1;
m->out_ep = ea & 0x0Fu;
m->out_type = type;
m->out_interval = d[i + 6];
m->out_interval = d[i + 6] ? d[i + 6] : 1u;
m->out_mps = mps & ~3u; /* send whole event packets only */
}
}
+32
View File
@@ -85,6 +85,38 @@ int main(void)
check("44.1 and 48 kHz: 44.1 kept (no conversion)", uh_parse_uac(UM2, sizeof UM2, &a) && !a.r48);
check("a keyboard is no audio input", !uh_parse_uac(KBD, sizeof KBD, &a));
}
{ /* (V10.8.69) shapes real keyboards use that the parser used to refuse ("NOT USB MIDI") */
static const uint8_t OUTFIRST[] = {9, 2, 0, 0, 2, 1, 0, 0x80, 50,
9, 4, 0, 0, 1, 1, 3, 0, 0,
7, 5, 0x01, 2, 64, 0, 0,
9, 4, 1, 0, 1, 1, 3, 0, 0,
7, 5, 0x81, 2, 64, 0, 0};
static const uint8_t INT0[] = {9, 2, 0, 0, 1, 1, 0, 0x80, 50,
9, 4, 0, 0, 1, 1, 3, 0, 0,
7, 5, 0x81, 3, 8, 0, 0};
static const uint8_t BULK48[] = {9, 2, 0, 0, 1, 1, 0, 0x80, 50,
9, 4, 0, 0, 1, 1, 3, 0, 0,
7, 5, 0x82, 2, 48, 0, 0};
/* MPK mini mk3-like: audio control, MIDI streaming with jacks, 9-byte endpoints and their class ones */
static const uint8_t MPK[] = {9, 2, 0, 0, 2, 1, 0, 0x80, 50,
9, 4, 0, 0, 0, 1, 1, 0, 0,
9, 0x24, 1, 0, 1, 9, 0, 1, 1,
9, 4, 1, 0, 2, 1, 3, 0, 0,
7, 0x24, 1, 0, 1, 0x41, 0,
6, 0x24, 2, 1, 1, 0,
9, 5, 0x01, 2, 64, 0, 0, 0, 0,
5, 0x25, 1, 1, 1,
9, 5, 0x81, 2, 64, 0, 0, 0, 0,
5, 0x25, 1, 1, 3};
uh_midi_t m;
check("an OUT-only MIDI interface first: the next one's IN taken", uh_parse_midi(OUTFIRST, sizeof OUTFIRST, &m) &&
m.iface == 1 && m.in_ep == 1 && !m.out_ep);
check("interrupt IN with bInterval 0: taken, polled every frame", uh_parse_midi(INT0, sizeof INT0, &m) &&
m.in_ep == 1 && m.in_type == 3 && m.in_interval == 1);
check("bulk IN of 48 bytes: taken", uh_parse_midi(BULK48, sizeof BULK48, &m) && m.in_ep == 2 && m.in_mps == 48);
check("MPK mini mk3-like: iface 1, IN 1, OUT 1", uh_parse_midi(MPK, sizeof MPK, &m) && m.iface == 1 &&
m.in_ep == 1 && m.out_ep == 1 && m.in_mps == 64);
}
printf("usbhost: %s\n", bad ? "FAILED" : "all ok");
return bad != 0;
}
+2240 -2240
View File
File diff suppressed because it is too large. Load diff
+7 -7
View File
@@ -569,7 +569,7 @@ formant_note_on 150 text *
formant_render 1918 text * fmt_glot fmt_ratio fmt_sung formant_word_render fres_coef mac2_64 mul64 mul64_us umul64 vowel_at
formant_word_render 2468 text fmt_glot fmt_ratio fmt_sung fres_coef mac2_64 mul64 mul64_sh15 mul64_us umul64
fres_coef 196 text mul64
ft_of 14 text
ft_of 14 text *
fugue_tick 370 text fg_next fg_play fg_release
fvb_boot 6 text
fvb_commit 78 text st_crc32 st_prog
@@ -835,7 +835,7 @@ kc_w 22 text
kern 78 text glyph_at
key_black 28 text
key_next 336 text drum_oct_note dxo_on midi_local_held mo_key mo_kon
key_off 278 text drum_oct_note dxo_off input_off midi_local_held mo_koff rec_release trk_note_off
key_off 278 text * drum_oct_note dxo_off input_off midi_local_held mo_koff rec_release trk_note_off
key_place 46 text key_black
keys_mode 32 text ckeys_on grid_on name_on skeys_on
keys_pg 56 text cur_page
@@ -1393,10 +1393,10 @@ sc_track 374 text input_on midi_local_held mo_kon rec_note rec_on scn_notes
scale_mask 32 text
scale_snap 52 text scale_mask
scale_steps 112 text scale_mask scale_snap
scene_drum_tick 272 text div_samples trk_note_off trk_note_on
scene_drum_tick 272 text * div_samples trk_note_off trk_note_on
scene_follow 156 text sc_track scene_follow_off
scene_follow_off 82 text input_off mo_koff
scene_key 322 text input_off kb_map midi_local_held mo_koff sc_track
scene_follow_off 82 text * input_off mo_koff
scene_key 322 text * input_off kb_map midi_local_held mo_koff sc_track
scn_notes 280 text chord_build_mode
scope_at 238 text cv_rect scope_lod scope_seg
scope_lod 110 text scope_seg
@@ -1669,7 +1669,7 @@ uac_stream 50 text memset
uac_tap 172 text
uac_tap48 226 text uac_collect uac_rs_run
uac_tap8 436 text
uh_connect 116 bt sie_rd sie_wr uh_probe uh_start_ep uh_wait
uh_connect 128 bt sie_rd sie_wr uh_probe uh_start_ep uh_wait
uh_csr0 88 bt fm1_wdt_feed sie_rd sie_wr
uh_ctl 272 bt sie_rd sie_wr uh_csr0
uh_ctl_out 170 bt sie_wr uh_csr0
@@ -1677,7 +1677,7 @@ uh_host_up 168 bt sie_wr uh_wait
uh_hub_port_probe 230 bt uh_ctl uh_probe uh_wait
uh_hub_probe 282 bt uh_ctl uh_hub_port_probe uh_hub_status uh_wait
uh_hub_status 100 text uh_ctl
uh_parse_midi 358 text
uh_parse_midi 394 text
uh_parse_uac 642 text
uh_probe 536 bt uh_ctl uh_ctl_out uh_hub_probe uh_parse_midi uh_parse_uac uh_wait
uh_session_end 30 bt