Repository navigation
Fix MCA pipe resets after PS3 reopen (#128) and restructure USB stack - #135
Merged
Merged
Conversation
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>
Collaborator
Author
|
Update: HW tests on PS3 2500 were successful. Merging! |
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.
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, andCLEAR_FEATURE(ENDPOINT_HALT). TinyUSB 0.18 doesn't reset its side:SET_CONFIGURATIONis ignoredSET_INTERFACEis only acknowledgedWith 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.cimplements TinyUSB's weaktud_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:0x83Resets caused by
CLEAR_FEATUREwait 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/usbis now its own CMake target,sd2psx_usb(src/usb/CMakeLists.txt):usb_session.c: USB lifecycle and class-driver registration (previously insidemca_device.c)usb_reset_itf.c: Pico reset interface as its own drivermca_device.c: MCA class driver onlymca_protocol.c: no PS2 card manager, auth or variant knowledge; the PS1/PS2 backends attach anmca_card_tsrc/ps2/ps2_mca_page.c, sosrc/usbno longer depends on PS2 codemca_protocol_service()replaces the duplicated backend task codeI 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
tests/mca,tests/ps1andtests/mscpass. New tests cover each reopen request, the requests that must be ignored, theCLEAR_FEATURErace (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.tests/mca/reopen.py --rounds 20passes 360/360 checks: 0 of 40 replies lost afterSET_INTERFACEand 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 repeatedSET_CONFIGURATION, so that case is covered by host tests only.reopen.pyrun 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