Skip to content

Fix MCA pipe resets after PS3 reopen (#128) and restructure USB stack - #135

Merged
bbsan2k merged 2 commits into
developfrom
fix/mca-pipe-reset
Nov 20, 2026
Merged

bbsan2k merged 2 commits into
developfrom
fix/mca-pipe-reset

Conversation

@bbsan2k

@bbsan2k bbsan2k commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Refs #128 — card missing after leaving/re-entering the XMB memory card screen, followed by a ~5 s XMB freeze.

Cause

When the PS3 reopens the adapter it sends standard requests that, per USB 2.0, reset the endpoint data toggles to DATA0 on both sides: SET_CONFIGURATION (also with the current value), SET_INTERFACE, and CLEAR_FEATURE(ENDPOINT_HALT). TinyUSB 0.18 doesn't reset its side:

  • a repeated SET_CONFIGURATION is ignored
  • SET_INTERFACE is only acknowledged
  • a cleared halt marks the endpoint idle while its buffer stays armed

With the toggles out of step, the host silently drops the next reply. Whether that happens depends on the parity of the packets sent before the reopen, which explains why it happens only "sometimes". Sony's library waits 5,000,000 µs for adapter events, which matches the freeze. Re-arming a still-armed endpoint also reaches TinyUSB's panic("ep %02X was already available").

Fix (no TinyUSB changes)

mca_device.c implements TinyUSB's weak tud_event_hook_cb. At SETUP time, inside the USB interrupt, it drops the affected endpoints' buffers and resets their toggles before the status stage. The main loop then finishes the reset:

  • clears TinyUSB's busy flags
  • drops stale replies and partial frames
  • re-sends the current presence on 0x83

Resets caused by CLEAR_FEATURE wait until TinyUSB has processed the request. Without that, the main loop could re-arm OUT before TinyUSB marks it idle, which hit the panic above during hardware testing.

Restructure

src/usb is now its own CMake target, sd2psx_usb (src/usb/CMakeLists.txt):

  • usb_session.c: USB lifecycle and class-driver registration (previously inside mca_device.c)
  • usb_reset_itf.c: Pico reset interface as its own driver
  • mca_device.c: MCA class driver only
  • mca_protocol.c: no PS2 card manager, auth or variant knowledge; the PS1/PS2 backends attach an mca_card_t
  • PS2 page execution moved to src/ps2/ps2_mca_page.c, so src/usb no longer depends on PS2 code
  • mca_protocol_service() replaces the duplicated backend task code
  • core 1 no longer takes the bridge spinlock on every idle poll

I checked the link map: the strong overrides of TinyUSB's weak hooks (tud_event_hook_cb, usbd_app_driver_get_cb, descriptor callbacks) come from the library.

Testing

  • Host tests: tests/mca, tests/ps1 and tests/msc pass. New tests cover each reopen request, the requests that must be ignored, the CLEAR_FEATURE race (the stubs now model the hardware rule that an armed endpoint can't be armed again), and an abandoned reply end to end. The new tests fail when the fix is removed.
  • Builds: SD2PSX, PMC+, no-PSRAM and debug configurations, plus the devcontainer build.
  • Hardware (SD2PSX on macOS): tests/mca/reopen.py --rounds 20 passes 360/360 checks: 0 of 40 replies lost after SET_INTERFACE and after clear-halt, and 0 of 20 bad replies after an abandoned reply. A 60-round clear-halt stress test with status checks also passes. macOS doesn't send a repeated SET_CONFIGURATION, so that case is covered by host tests only.
  • Not yet done: a PS3/XMB test, and a reopen.py run against the old firmware to show that it fails there.

Known limitation

If a host abandons a reply without sending any of these requests and then sends a new request, the old reply stays queued on IN. Dropping it would require restoring the host's expected toggle inside the RP2040 port. This is documented in doc/mca-protocol-notes.md.

🤖 Generated with Claude Code

bbsan2k and others added 2 commits November 20, 2026 09:39
When the XMB leaves and re-enters the memory card screen, the PS3 reopens
the adapter. Host reopen requests (SET_CONFIGURATION, SET_INTERFACE,
CLEAR_FEATURE(ENDPOINT_HALT)) reset the USB data toggles to DATA0, but
TinyUSB 0.18 ignores a repeated SET_CONFIGURATION, only acknowledges
SET_INTERFACE and leaves armed buffers in place on a cleared halt. With
the toggles out of step the host drops the next reply, so the card goes
missing and the XMB waits for its 5 s timeout (#128). Re-arming a still
armed endpoint also reaches TinyUSB's "ep already available" panic.

mca_device.c now implements tud_event_hook_cb: at SETUP time, inside the
USB interrupt, it drops the affected endpoints' buffers and resets their
toggles. The task then finishes the reset (TinyUSB busy flags, protocol
drops stale replies/partial frames, presence is re-sent). Resets from
CLEAR_FEATURE wait until TinyUSB has processed the request, otherwise the
main loop could re-arm OUT before TinyUSB marks it idle. TinyUSB itself
is unchanged.

Restructure src/usb into a dedicated sd2psx_usb CMake target:
- usb_session.c: USB lifecycle and driver registration
- usb_reset_itf.c: Pico reset interface as its own class driver
- mca_device.c: MCA class driver only
- mca_protocol.c: no PS2 knowledge; backends attach an mca_card_t
- PS2 page execution moved to src/ps2/ps2_mca_page.c
- shared mca_protocol_service() replaces duplicated backend tasks
- core 1 no longer takes the bridge spinlock while idle-polling

Tests: host tests for every reopen request, ignored requests, the
CLEAR_FEATURE race (stubs now model hardware AVAIL), and an end-to-end
abandoned reply. tests/mca/reopen.py reproduces the issue on hardware;
it passes 360/360 checks on an SD2PSX (macOS does not send a repeated
SET_CONFIGURATION, so that case is covered by host tests only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bbsan2k

bbsan2k commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

Update: HW tests on PS3 2500 were successful. Merging!

@bbsan2k
bbsan2k merged commit bb1f945 into develop Nov 20, 2026
12 checks passed
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.

1 participant