Skip to content

class/midi: skip requeue in midih_xfer_cb on transfer failure - #3838

Open
sanjay-nagesh wants to merge 1 commit into
hathach:masterfrom
sanjay-nagesh:fix/midi-host-xfer-fail-panic
Open

class/midi: skip requeue in midih_xfer_cb on transfer failure#3838
sanjay-nagesh wants to merge 1 commit into
hathach:masterfrom
sanjay-nagesh:fix/midi-host-xfer-fail-panic

Conversation

@sanjay-nagesh

@sanjay-nagesh sanjay-nagesh commented Aug 20, 2026

Copy link
Copy Markdown

PR for #3805 - RP2xxx MIDI Host panics on device unplug

midih_xfer_cb() was just throwing away the transfer result and always re-queueing the next RX transfer regardless of what happened. So, when you unplug a MIDI device mid-transfer, the pending In transfer comes back as XFER_RESULT_FAILED, but the driver didn't noticed that and tried to re-arm the endpoint regardless. On RP2040, that collides with the host's controller abort/cleanup on the same buffer-control register and it panics with buf_ctrl already available

So, I fixed it by making t so that it bails out early if result != XFER_RESULT_SUCCESS. cdc_host.c already handles this in (in the same way) cdch_xfer_cb so this just brings the MIDI in line with other patterns in the host stack.

When a MIDI device is unplugged mid-transfer, the pending IN transfer
on the interrupt/bulk endpoint completes with XFER_RESULT_FAILED, but
midih_xfer_cb ignored the result and unconditionally re-queued a new
transfer on the same endpoint buffer. On rp2040, this re-queue raced
with the host controller's own abort handling for the same buffer
control register, hitting the "buf_ctrl already available" panic.

Bail out immediately when result != XFER_RESULT_SUCCESS, matching the
existing pattern in cdc_host.c's xfer callback.
@sanjay-nagesh
sanjay-nagesh marked this pull request as ready for review August 23, 2026 03:32
Copilot AI lite review requested due to automatic review settings August 23, 2026 03:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is minimal, consistent with existing host-driver patterns, and directly addresses the reported unplug panic by preventing endpoint re-queue on failed transfers.

Pull request overview

Fixes a MIDI Host robustness issue where midih_xfer_cb() would re-arm the RX endpoint even when the underlying transfer failed (e.g., device unplug mid-transfer), aligning MIDI Host behavior with existing host-class patterns (notably CDC) and avoiding RP2xxx controller-side abort/rearm collisions.

Changes:

  • Stop processing and re-queuing transfers in midih_xfer_cb() when result != XFER_RESULT_SUCCESS.
  • Remove the previous (void) result; discard to ensure failure is handled immediately.
File summaries
File Description
src/class/midi/midi_host.c Adds an early-return guard on failed transfers in the MIDI host transfer callback to prevent re-arming endpoints after unplug/failure.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

Copy link
Copy Markdown

Hardware-in-the-loop (HIL) Test Report

No HIL run for this push (no affected boards, or hardware testing did not run).

@github-actions

Copy link
Copy Markdown

Size Difference Report

Because TinyUSB code size varies by port and configuration, the metrics below represent the averaged totals across all example builds.

Note: If there is no change, only one value is shown.

Changes >1% in size

file .text .rodata .data .bss size % diff
hcd_samd.c 2168 ➙ 2220 (+52) 0 0 324 2492 ➙ 2544 (+52) +2.1%
midi_host.c 1338 ➙ 1344 (+6) 7 7 3449 ➙ 3538 (+89) 4791 ➙ 4885 (+94) +2.0%
TOTAL 3506 ➙ 3564 (+58) 7 7 3773 ➙ 3862 (+89) 7283 ➙ 7429 (+146) +2.0%
Changes <1% in size
file .text .rodata .data .bss size % diff
cdc_host.c 6475 ➙ 6479 (+4) 487 15 926 ➙ 946 (+20) 7607 ➙ 7639 (+32) +0.4%
hid_host.c 1244 ➙ 1247 (+3) 0 0 1287 ➙ 1288 (+1) 2531 ➙ 2534 (+3) +0.1%
hub.c 1380 ➙ 1385 (+5) 8 8 30 1414 ➙ 1419 (+5) +0.4%
midi2_host.c 1798 ➙ 1802 (+4) 0 0 5876 ➙ 5921 (+45) 7674 ➙ 7723 (+49) +0.6%
msc_host.c 1620 ➙ 1625 (+5) 0 0 395 2015 ➙ 2020 (+5) +0.2%
usbh.c 4971 ➙ 4979 (+8) 57 81 1174 ➙ 1173 (-1) 6244 ➙ 6256 (+12) +0.2%
TOTAL 17488 ➙ 17517 (+29) 552 104 9688 ➙ 9753 (+65) 27485 ➙ 27591 (+106) +0.4%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2885 0 1252 1621 4501 +0.0%
cdc_device.c 1235 16 1092 722 1955 +0.0%
dcd_ch32_usbfs.c 1708 0 0 1364 3072 +0.0%
dcd_ch32_usbhs.c 1922 0 0 481 2404 +0.0%
dcd_ci_fs.c 1955 0 0 1290 3245 +0.0%
dcd_ci_hs.c 1941 0 0 779 2721 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4230 19 0 265 4514 +0.0%
dcd_eptri.c 2281 0 0 259 2540 +0.0%
dcd_ft9xx.c 3284 0 0 172 3456 +0.0%
dcd_lpc17_40.c 1842 0 0 792 2238 +0.0%
dcd_lpc_ip3511.c 1580 0 0 264 1800 +0.0%
dcd_mm32f327x_otg.c 1474 0 0 1290 2764 +0.0%
dcd_msp430x5xx.c 1801 0 0 176 1977 +0.0%
dcd_musb.c 2667 0 0 179 2846 +0.0%
dcd_nrf5x.c 2974 0 0 292 3266 +0.0%
dcd_nuc120.c 1096 0 0 78 1174 +0.0%
dcd_nuc121.c 1170 0 0 101 1271 +0.0%
dcd_nuc505.c 0 0 1533 157 1690 +0.0%
dcd_rp2040.c 1004 0 764 653 2420 +0.0%
dcd_rusb2.c 3346 0 0 156 3502 +0.0%
dcd_samd.c 1071 0 0 266 1337 +0.0%
dcd_samg.c 1326 0 0 72 1398 +0.0%
dcd_stm32_fsdev.c 2568 0 0 291 2859 +0.0%
dfu_device.c 776 28 712 134 910 +0.0%
dfu_rt_device.c 157 0 134 0 157 +0.0%
dwc2_common.c 603 22 0 0 615 +0.0%
ecm_rndis_device.c 1067 0 1 2759 3826 +0.0%
ehci.c 2763 0 0 6274 7783 +0.0%
fsdev_common.c 182 0 0 0 182 +0.0%
hcd_ch32_usbfs.c 2491 0 0 502 2993 +0.0%
hcd_ci_fs.c 2466 0 0 469 2936 +0.0%
hcd_ci_hs.c 186 0 0 0 186 +0.0%
hcd_dwc2.c 5071 25 1 545 5642 +0.0%
hcd_musb.c 3071 0 0 157 3228 +0.0%
hcd_pio_usb.c 262 0 240 0 502 +0.0%
hcd_rp2040.c 1996 17 4 321 2338 +0.0%
hcd_rusb2.c 2951 0 0 245 3196 +0.0%
hcd_stm32_fsdev.c 3248 0 1 420 3670 +0.0%
hid_device.c 1121 44 997 119 1240 +0.0%
midi2_device.c 3608 34 1600 563 4183 +0.0%
midi_device.c 1148 0 1007 614 1759 +0.0%
msc_device.c 2522 108 2293 802 3325 +0.0%
mtp_device.c 1781 22 771 589 2378 +0.0%
ncm_device.c 1791 28 833 4393 6197 +0.0%
ohci.c 2157 0 0 2503 4660 +0.0%
printer_device.c 827 0 706 555 1380 +0.0%
rp2040_usb.c 386 35 632 11 1065 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb.c 449 0 389 3 451 +0.0%
tusb_fifo.c 855 0 486 0 850 +0.0%
typec_stm32.c 1230 8 2 19 1255 +0.0%
usbc.c 500 2 20 166 688 +0.0%
usbd.c 3708 57 92 354 4126 +0.0%
usbtmc_device.c 2283 24 69 311 2625 +0.0%
vendor_device.c 1132 0 538 1561 2685 +0.0%
video_device.c 4477 5 1245 480 4949 +0.0%
TOTAL 105852 494 17430 36733 144317 +0.0%

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

Top 10 targets by memory change (%) (out of 2467 targets) View Project Dashboard →

target .text .rodata .data .bss total % diff
frdm_kl25z/midi_rx 18,584 → 18,592 (+8) 18,744 → 18,752 (+8) +0.0%
stm32c542nucleo/midi_rx 19,592 → 19,600 (+8) 20,536 → 20,544 (+8) +0.0%
stm32c071nucleo/midi_rx 21,736 → 21,744 (+8) 22,540 → 22,548 (+8) +0.0%
stm32g0b1nucleo/midi_rx 23,776 → 23,784 (+8) 24,580 → 24,588 (+8) +0.0%
frdm_rw612/midi_rx 24,472 → 24,480 (+8) 26,612 → 26,620 (+8) +0.0%
raspberry_pi_pico/midi_rx 25,732 → 25,740 (+8) 26,688 → 26,696 (+8) +0.0%
b_u585i_iot2a/midi_rx 32,792 → 32,800 (+8) 34,112 → 34,120 (+8) +0.0%
lpcxpresso1769/midi_rx 16,732 → 16,736 (+4) 17,252 → 17,256 (+4) +0.0%
lpcxpresso18s37/midi_rx 18,748 → 18,752 (+4) 19,342 → 19,346 (+4) +0.0%
portenta_c33/midi_rx 19,260 → 19,264 (+4) 19,424 → 19,428 (+4) +0.0%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants