fix(rp2040): clear stale EPX buf_ctrl AVAIL bit before arming a transfer - #3826
Open
tendrils wants to merge 1 commit into
Open
fix(rp2040): clear stale EPX buf_ctrl AVAIL bit before arming a transfer#3826tendrils wants to merge 1 commit into
tendrils wants to merge 1 commit into
Conversation
EPX is shared by every non-interrupt endpoint of every connected device. The hardware does not reliably clear USB_BUF_CTRL_AVAIL in epx_buf_ctrl once a transaction completes, so when the next EPX user re-arms immediately -- with no intervening scheduler tick -- bufctrl_write32() sees the leftover AVAILABLE bit from the finished transaction and panics with 'buf_ctrl already available', despite nothing actually being in flight. Reproduced with a USB-MIDI host: the class driver's xfer-complete callback synchronously starts the next transfer on every zero-byte completion, which makes the re-arm immediate enough to hit this consistently. hcd_edpt_xfer() has already established, under the same critical section, that no endpoint currently owns EPX -- software's epx->state is the authority on that rather than the hardware bit -- so clearing any leftover bit before arming is safe at this point.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a host-mode RP2040 USB issue where the EPX shared buffer-control register may retain a stale USB_BUF_CTRL_AVAIL bit after a transaction completes, causing bufctrl_write32() to panic when the next transfer is armed immediately.
Changes:
- Clear
usbh_dpram->epx_buf_ctrlbefore arming a new EPX transfer inhcd_edpt_xfer(). - Add an in-code rationale explaining why the clear is safe given EPX ownership is determined by software state under the same critical section.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
hcd_edpt_xfer()clears any staleUSB_BUF_CTRL_AVAILbit inepx_buf_ctrlbefore arming a new transfer on EPX.Why
EPX is shared by every non-interrupt endpoint of every connected device. The hardware does not reliably clear
USB_BUF_CTRL_AVAILinepx_buf_ctrlonce a transaction completes. When the next EPX user re-arms immediately — with no intervening scheduler tick —bufctrl_write32()sees the leftover AVAILABLE bit from the finished transaction and panics:despite nothing actually being in flight.
Reproducing
Seen consistently with a USB-MIDI host on RP2040. The MIDI class driver's xfer-complete callback synchronously starts the next transfer on every zero-byte completion, which makes the re-arm immediate enough to hit this reliably. It presents as a hard panic partway through a MIDI session rather than at enumeration, which made it slow to attribute.
Why this is safe here
At this point
hcd_edpt_xfer()has already established, under the same critical section, that no endpoint currently owns EPX — the preceding check testsepx->state, and software's own state is the authority on EPX ownership rather than the hardware bit. So a set AVAILABLE bit at this point can only be a leftover, and clearing it cannot discard a live transaction.Notes
Draft: this is running in a downstream project against real hardware, but I have not exercised it beyond the RP2040 host path (MIDI class, full speed). Happy to adjust the placement or the comment if you would rather handle it inside
rp2usb_xfer_start()/bufctrl_write32()instead — the fix is deliberately narrow and placed where the ownership check already is, but I do not have a strong view on where it belongs.