Skip to content

fix(hcd_rp2040): reset shared EPX state on disconnect - #3878

Open
mringwal wants to merge 1 commit into
hathach:masterfrom
bluekitchen:rp2040-host-unplug-replug
Open

mringwal wants to merge 1 commit into
hathach:masterfrom
bluekitchen:rp2040-host-unplug-replug

Conversation

@mringwal

@mringwal mringwal commented Sep 3, 2026

Copy link
Copy Markdown

This fixes repeated USB-device unplug/re-plug failures on the RP2040 host controller, which could panic with "buf_ctrl ... already available".

AI (Codex) analysis traced the failure to stale state in the shared EPX DPRAM registers:

  • Clear EPX DPRAM state when its owning device is closed, including address-0 enumeration transfers.
  • Clear epx_buf_ctrl before rearming EPX on RP2040-E4, preventing a stale AVAILABLE bit from a prior single-buffered transfer.

The changes adapt safeguards from commits 93ff3da and 9ac343a that seem to have gone lost during later RP2040 HCD refactoring.

I have verified the fix on an original Raspberry Pi RP2040 board with repeated unplug/re-plug testing; the panic no longer occurs.

Tested with a Bluetooth HCI USB to UART bridge which uses Control, Interrupt and Bulk transfers.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a4cf02c590

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +480 to +483
if (epx->dev_addr == dev_addr && epx->max_packet_size > 0) {
usbh_dpram->epx_buf_ctrl = 0;
usbh_dpram->epx_ctrl = 0;
rp2usb_reset_transfer(epx);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Resume pending EPX work after resetting the active endpoint

When an address-0 device behind a hub is unplugged while its EPX transfer is active and another attached device already has a non-interrupt transfer pending, this reset leaves epx->state as EPSTATE_IDLE without starting the survivor. The RP2040 SOF path in hcd_rp2040_irq() switches to next_ep only while the current EPX is active, and the RP2350 path requires a stopped-on-NAK event that cannot arrive after these registers are cleared, so the pending transfer can remain stuck indefinitely unless unrelated work later re-arms EPX. After removing the closing device's endpoints, explicitly hand EPX to a surviving pending endpoint and normalize the polling/switch state.

Useful? React with 👍 / 👎.

Comment on lines +480 to +483
if (epx->dev_addr == dev_addr && epx->max_packet_size > 0) {
usbh_dpram->epx_buf_ctrl = 0;
usbh_dpram->epx_ctrl = 0;
rp2usb_reset_transfer(epx);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Stop the SIE before discarding an active EPX transfer

When an address-0 device behind a hub disappears during an active SETUP transaction, zeroing epx_ctrl and epx_buf_ctrl does not cancel that transaction because hcd_setup_send() arms it solely through the SIE SEND_SETUP/START_TRANS bits. The root bus remains connected in this topology, so after rp2usb_reset_transfer() frees the software endpoint, the old SIE operation can still produce a TRANS_COMPLETE or STALL interrupt; both handlers then report completion using the current epx, potentially completing a reused address-0 endpoint or another device's transfer. Stop the active SIE transaction and clear its latched completion state before resetting or reusing EPX.

Useful? React with 👍 / 👎.

@github-actions

github-actions Bot commented Oct 3, 2026

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

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

Code size

Baseline: unavailable - the PR snapshots record no single base commit

0 builds on 0 boards compared, 0 changed

Not compared:

  • FAILED scope PR snapshot: no usable scope manifest

no comparable pairs
Full report

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant