feat(libSceUsbd): handle events with FreeBSD libusb's event lock and handler thread #9339
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9339"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T18:48:34Z.
What
Implements the nine remaining
sceUsbdplaceholders, the event calls, with the locking model of FreeBSD 11.4libusb10_io.cover the host libusb:sceUsbdLockEvents,sceUsbdTryLockEvents,sceUsbdUnlockEvents,sceUsbdLockEventWaiters,sceUsbdUnlockEventWaiters,sceUsbdWaitForEvent,sceUsbdEventHandlerActive,sceUsbdHandleEventsandsceUsbdHandleEventsLocked. Their signatures were already in the file, from orbital's console type database.FreeBSD's model, which upstream libusb (the host's) does not share:
sceUsbdEventHandlingOkreports whether the caller is the handler, andsceUsbdEventHandlerActivewhether any thread is.sceUsbdTryLockEventsreturns 1 when the lock is busy, held by the caller (FreeBSD'spthread_mutex_trylockgivesEBUSYthere) or when another thread handles events; otherwise it takes the lock and the handler role and returns 0.sceUsbdUnlockEventsgives up the handler role, wakes all waiters and releases the lock.sceUsbdWaitForEventwaits on that wake-up with the lock held, returns 1 on timeout and 0 otherwise, and takes the handler role if no thread has it.sceUsbdHandleEventsLockedhandles events if the caller is the handler, releasing the lock while the host libusb handles them and runs the transfer callbacks. Otherwise it waits and returns0x80240007(timeout) when the wait times out.libusb10_handle_events_subdoes withpthread_cond_broadcast(L223-L224); a waiter insceUsbdWaitForEventorsceUsbdHandleEventsLockedthen returns 0.sceUsbdHandleEventslocks, handles events without a timeout and unlocks.Changes to existing calls:
sceUsbdEventHandlingOkreturned 1 for every thread. It now reports the handler role, as FreeBSD'slibusb_event_handling_okdoes, and the existing check inguest_usbdexpects 0 from a thread without the lock.sceUsbdHandleEventsTimeoutnow takes the same lock and handler path, so a second thread handling events waits for the first instead of entering the host libusb at the same time. Its argument checks are unchanged, and a timeout of more thanINT_MAXseconds now throws.These throw, as FreeBSD's result there is undefined or unknown:
sceUsbdHandleEventsLockedwithout holding it;sceUsbdWaitForEventandsceUsbdHandleEventsLocked.With no timeout, FreeBSD's
pollreturns on any event of its descriptors, including the wake-up a submit from another thread writes. Here event handling without a timeout returns once a transfer completes, in one-second host slices. Callers loop on these calls, but the difference is recorded.docs/dev/debt/libSceUsbd.mddrops "sceUsbdEventHandlingOkreturns 1" and the empty unknown section, and adds one entry for the event calls. The library's 2214-character first entry is split into three entries of under 1000 characters with the same text, except that transfer callbacks now run "from event handling" rather than only fromsceUsbdHandleEventsTimeout.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, full Release build with
BUILD_TESTING=ON, this branch on #9190 and #9338, merged ontomainb00235dda.guest_usbdgainsEventLocks, which runs without a device:sceUsbdHandleEventsLockedwhen a pass of the handler ends, gets0x80240007when its timeout ends during a longer pass, and returns after the handler keeps the event lock past its timeout. The handler releases the lock until that thread has returned, and only then joins it;It passes 3 of 3, and 56 of 56 with 8 copies at once (rechecked at bda456a644). With
Export.cppfrom the device calls PR and the new tests, it fails at the first changed answer (a thread without the event lock may handle events). With the earlier ordering, where the handler joined that thread while holding the event lock, the held-lock case hangs (3 of 3 runs stopped at 20 s).guest_usbd_tests devicewithAPS5_USBD_DEVICE=1b1c:0c1a(CORSAIR Lighting Node CORE) completes an asynchronous control transfer throughsceUsbdHandleEvents, and another undersceUsbdLockEventsthroughsceUsbdHandleEventsLocked, besides the existing transfer and cancel-race checks throughsceUsbdHandleEventsTimeout. It passes 11 of 11 runs.python tools/select_tests.py ctest build --output-on-failure --timeout 180 --parallel 8: 1523 of the 1526 selected tests pass. The 3 failures areagc_driver_sample_order,agc_driver_sample_order_grid_referenceandagc_driver_sample_order_recipe_depth_locations, which fail the same way onmainon this RTX 4090 and which #9056 fixes.agc_driver_image_array_window_past_layer_limitis not built: it does not compile with MinGW onmain(#8310).python tools/check_conventions.py --base origin/mainpasses.python tools/pr_overlap.py --branch feat/usbd-event-callsreports only #9190, which this builds on. #9116 adds test lines toGuestUsbd.cpptoo; the two merge cleanly.guest_usbd_tests devicefrom #9339's branch, which contains #9338, on Windows 11 Pro 26200 (comment): an MSI Mystic Light HID controller (1462:7D75, full speed) and a Wooting 80HE keyboard (31E3:1402, high speed) pass 3 of 3 each, andsceUsbdDetachKernelDriverreturns0x802400FFon both. On the Wooting the iso packet size check takes the multiplier path above full speed.guest_usbd_testson Linux (WSL2, Ubuntu 24.04, GCC 13.3, Release) from this head merged ontomaindf2569d2c5(comment): it exits with 0, and withExport.cppreset tomainit aborts (SIGABRT).64ae578ddaafter #10706; the review fix is now 6c208252d7 (was bda456a644).guest_usbdpasses 3 of 3, and 56 of 56 with 8 copies at once, and fails 3 of 3 withExport.cppfrom the device calls PR. The device run withAPS5_USBD_DEVICE=1b1c:0c1apasses 2 of 2.public_headersand the conventions check pass.main750cf6cf48(Windows 11 Pro 26200, Ryzen 9 7950X3D, WinLibs GCC 15.2.0 r7, Release; comment):guest_usbd20 of 20;guest_usbd_tests device5 of 5 on1462:7D75and 5 of 5 on31E3:1402; a Logitech LIGHTSPEED receiver046D:C54Dpasses the newsceUsbdHandleEventsandsceUsbdHandleEventsLockedtransfers and stops at the existing cancel-race check, as it does withmain'sExport.cppandGuestUsbd.cpp;guest_usb_storage,guest_system_service_usb_storageandguest_usb_storage_dialog5 of 5 each. They also found that waiters woke only at the handler's unlock, where FreeBSD wakes them after each pass.9f3799cfaa, rebased ontomaind5ef780bb4(the two earlier commits are nowdbb27b9ef1andc0d8f0a670): the handler wakes waiters after each pass, andguest_usbd's follower case is split into the three cases above. With the new test andExport.cppwithout the change,guest_usbdfails 3 of 3 ata thread that is not the handler was not woken when a pass of event handling ended(the follower returns only at its 5 s timeout). With the change it passes 10 of 10, and 56 of 56 with 8 copies at once.guest_usbd_tests devicewithAPS5_USBD_DEVICE=1b1c:0c1apasses 3 of 3. The full suite passes 2827 of 2828; onlyguest_wide_formattingfails, the same way as onmainon this machine (#10981, German code page, fixed by #10987).python tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by simnJS on 2026-10-21T19:11:48Z.
Device run of #9339's head
be52eb439(on #9338b77a11079and #919066f467e40). Windows 11 Pro 26200, Ryzen 9 7950X3D, WinLibs GCC 15.2.0 r7, Release.ctest -R guest_usbdpasses 3 of 3.APS5_USBD_DEVICEguest_usbd_tests devicesceUsbdDetachKernelDriver1462:7D750x802400FF31E3:14020x802400FFBoth runs print
USBD device tests passedand the product stringsMYSTIC LIGHTandWooting 80HE.sceUsbdDetachKernelDriverreturns the other error on both devices, which the test accepts.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T19:53:23Z.
Thanks @simnJS. The Wooting is the first high-speed device on these tests, so its run is also the first where
sceUsbdGetMaxIsoPacketSizetakes the multiplier path above full speed. I added both devices to the Tested sections here and on #9338.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-22T04:41:20Z.
Linux data point for
be52eb4396(WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release,BUILD_TESTING=ON), this head merged ontomaindf2569d2c5(clean merge). I builtguest_usbd_testsand ran the executable directly (exit code, not throughctest):main, withcore/libs/prx/libSceUsbd/Export.cppreset tomain): it aborts (SIGABRT, exit code 134).Note: #9338 changes the same test file (
guest_usbd); this run covers this head only.Not tested: macOS, Windows, the rest of the test suite.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-22T10:02:57Z.
Thanks @xXPBSXx. These are the first Linux runs of the USBD tests, and the first to check on Linux that they fail without the change. I added them to the Tested sections here and on #9338.
AI-assisted: yes (Claude Code)