feat(libSceUsbd): pass USB devices and transfers through to the host with libusb #3848

Merged
13hourstilliland merged 4 commits from upstream-pr/3848 into main 2026-10-12 20:51:42 +00:00
13hourstilliland commented 2026-10-11 17:55:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 13hourstilliland on 2026-10-11T17:55:53Z.


What

Replaces the no-device path with host libusb 1.0.30, built from a new 3rdparty/libusb-cmake submodule (pinned to bea6567).

  • Devices, handles and descriptors are libusb's own objects. The device, config, interface and endpoint descriptor layouts of FreeBSD 11.4 libusb match libusb's field for field; static_assert pins sizes and offsets.
  • Every pointer handed to a title is tracked; one this library did not return throws.
  • libusb errors map to 0x80240000 - code, with LIBUSB_ERROR_OTHER as 0x802400FF, as in shadPS4.
  • A submitted transfer is copied into a host transfer. On completion the status, actual length and iso packet results are copied back and the title's callback runs from sceUsbdHandleEventsTimeout. Transfer flags other than short-not-ok and add-zero-packet throw.
  • When the host has no USB access, initialization keeps the empty device list, so CI needs no USB.

This builds on LordLava's closed #2870, which used the same submodule and approach. Stacked on #3847; until that merges the diff shows both commits.

Tested

  • Windows (WinLibs MinGW GCC 15.2, the CI toolchain), Xbox 360 controller plugged in: guest_usbd device with APS5_USBD_DEVICE=3537:100f lists the device, reads its device and config descriptors and product string, does a synchronous control request, claims and releases all three interfaces, and completes an asynchronous control transfer through the event loop. Each result matches the controller's own descriptor.
  • Mutation check: dropping the actual-length copy-back makes the device run fail.
  • Linux (no USB access): the no-device guest_usbd run passes.
  • check_conventions.py passes. Isochronous transfers and PS5 behaviour are unverified, as recorded in TechnicalDebt. Not tested with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #3847
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821113543 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848). Originally posted by [13hourstilliland](https://github.com/13hourstilliland) on 2026-10-11T17:55:53Z. --- ### What Replaces the no-device path with host [libusb](https://github.com/libusb/libusb) 1.0.30, built from a new `3rdparty/libusb-cmake` submodule (pinned to bea6567). - Devices, handles and descriptors are libusb's own objects. The device, config, interface and endpoint descriptor layouts of [FreeBSD 11.4 libusb](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libusb/libusb.h) match libusb's field for field; `static_assert` pins sizes and offsets. - Every pointer handed to a title is tracked; one this library did not return throws. - libusb errors map to `0x80240000 - code`, with `LIBUSB_ERROR_OTHER` as `0x802400FF`, as in [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/usbd/usbd.cpp). - A submitted transfer is copied into a host transfer. On completion the status, actual length and iso packet results are copied back and the title's callback runs from `sceUsbdHandleEventsTimeout`. Transfer flags other than short-not-ok and add-zero-packet throw. - When the host has no USB access, initialization keeps the empty device list, so CI needs no USB. This builds on LordLava's closed #2870, which used the same submodule and approach. Stacked on #3847; until that merges the diff shows both commits. ### Tested - Windows (WinLibs MinGW GCC 15.2, the CI toolchain), Xbox 360 controller plugged in: `guest_usbd device` with `APS5_USBD_DEVICE=3537:100f` lists the device, reads its device and config descriptors and product string, does a synchronous control request, claims and releases all three interfaces, and completes an asynchronous control transfer through the event loop. Each result matches the controller's own descriptor. - Mutation check: dropping the actual-length copy-back makes the device run fail. - Linux (no USB access): the no-device `guest_usbd` run passes. - `check_conventions.py` passes. Isochronous transfers and PS5 behaviour are unverified, as recorded in TechnicalDebt. Not tested with a title. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #3847 - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-11 17:56:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T17:56:12Z.


Progress report (84659e1 → 42cf829)

📈 System libraries: 87.19% (+0.04%, +10 implemented functions)

✅ 10 implemented
Library Name
libSceUsbd sceUsbdBulkTransfer
libSceUsbd sceUsbdControlTransferGetData
libSceUsbd sceUsbdControlTransferGetSetup
libSceUsbd sceUsbdFillBulkTransfer
libSceUsbd sceUsbdFillControlSetup
libSceUsbd sceUsbdFillControlTransfer
libSceUsbd sceUsbdFillIsoTransfer
libSceUsbd sceUsbdGetIsoPacketBuffer
libSceUsbd sceUsbdInterruptTransfer
libSceUsbd sceUsbdSetIsoPacketLengths
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6111958883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848#issuecomment-6111958883). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T17:56:12Z. --- <!-- progress-report --> ### Progress report (84659e1 → 42cf829) 📈 **System libraries**: 87.19% (+0.04%, +10 implemented functions) <details> <summary>✅ 10 implemented</summary> | Library | Name | | - | - | | libSceUsbd | `sceUsbdBulkTransfer` | | libSceUsbd | `sceUsbdControlTransferGetData` | | libSceUsbd | `sceUsbdControlTransferGetSetup` | | libSceUsbd | `sceUsbdFillBulkTransfer` | | libSceUsbd | `sceUsbdFillControlSetup` | | libSceUsbd | `sceUsbdFillControlTransfer` | | libSceUsbd | `sceUsbdFillIsoTransfer` | | libSceUsbd | `sceUsbdGetIsoPacketBuffer` | | libSceUsbd | `sceUsbdInterruptTransfer` | | libSceUsbd | `sceUsbdSetIsoPacketLengths` | </details>
adriadam10 commented 2026-10-11 18:17:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T18:17:38Z.

Review: CHANGES_REQUESTED


Thanks for the rework. Built the PR merged onto main and ran guest_usbd: it passes, and it fails without the change.

One problem to fix:

  • core/libs/prx/libSceUsbd/Export.cpp:254-262 (sceUsbdCancelTransfer): the mutex is released before libusb_cancel_transfer(host). If another thread runs sceUsbdHandleEventsTimeout and CompleteTransfer finishes (libusb_free_transfer(host)) in between, host is a dangling pointer. Keep state.mutex locked across the libusb_cancel_transfer call (it does not invoke the callback synchronously, so CompleteTransfer taking the same mutex cannot deadlock), or otherwise make the lookup and the cancel atomic. A test with one thread cancelling while another pumps events would pin it.

This is stacked on #3847, so it also has to wait for that one to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5484388099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848#pullrequestreview-5484388099). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T18:17:38Z. **Review:** CHANGES_REQUESTED --- Thanks for the rework. Built the PR merged onto `main` and ran `guest_usbd`: it passes, and it fails without the change. One problem to fix: - `core/libs/prx/libSceUsbd/Export.cpp:254-262` (`sceUsbdCancelTransfer`): the mutex is released before `libusb_cancel_transfer(host)`. If another thread runs `sceUsbdHandleEventsTimeout` and `CompleteTransfer` finishes (`libusb_free_transfer(host)`) in between, `host` is a dangling pointer. Keep `state.mutex` locked across the `libusb_cancel_transfer` call (it does not invoke the callback synchronously, so `CompleteTransfer` taking the same mutex cannot deadlock), or otherwise make the lookup and the cancel atomic. A test with one thread cancelling while another pumps events would pin it. This is stacked on #3847, so it also has to wait for that one to merge.
13hourstilliland commented 2026-10-11 19:05:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 13hourstilliland on 2026-10-11T19:05:33Z.


Fix pushed

  • sceUsbdCancelTransfer now keeps state.mutex held across libusb_cancel_transfer, as suggested: the lookup and the cancel are one critical section.
  • The race test is in the same commit: 300 rounds of submit, cancel from a second thread, and event pumping in the main thread, accepting completed/timed-out/cancelled and failing otherwise. With the fix the run passes 4/4 on the device.
  • One honest limitation: I rebuilt the unfixed version and ran the race test against it 8 times on hardware; it never crashed. The unlock-then-cancel window closes within a few instructions on Windows, so the test exercises the path but does not reliably reproduce the failure. The held lock itself is the guarantee here.
  • Branch rebalanced too: the fix and this test now live on feat/usbd-libusb (741f43cc); the helpers PR stays helpers-only. Both are 0 commits behind main.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6112599947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848#issuecomment-6112599947). Originally posted by [13hourstilliland](https://github.com/13hourstilliland) on 2026-10-11T19:05:33Z. --- ### Fix pushed - `sceUsbdCancelTransfer` now keeps `state.mutex` held across `libusb_cancel_transfer`, as suggested: the lookup and the cancel are one critical section. - The race test is in the same commit: 300 rounds of submit, cancel from a second thread, and event pumping in the main thread, accepting completed/timed-out/cancelled and failing otherwise. With the fix the run passes 4/4 on the device. - One honest limitation: I rebuilt the unfixed version and ran the race test against it 8 times on hardware; it never crashed. The unlock-then-cancel window closes within a few instructions on Windows, so the test exercises the path but does not reliably reproduce the failure. The held lock itself is the guarantee here. - Branch rebalanced too: the fix and this test now live on `feat/usbd-libusb` (`741f43cc`); the helpers PR stays helpers-only. Both are 0 commits behind `main`.
13hourstilliland commented 2026-10-11 22:12:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 13hourstilliland on 2026-10-11T22:12:02Z.


Thanks — the cancel lock is fixed in 21f764e69: sceUsbdCancelTransfer now holds state.mutex across libusb_cancel_transfer, and the lookup and the cancel are one critical section. CompleteTransfer taking the same mutex off the event thread cannot deadlock, because the cancel never invokes the callback synchronously.

While running the device path on Windows with the Xbox 360 controller (VID 3537, PID 100F) I found a second crash in the test, present before this PR and without it: guest_usbd_tests device dies with an access violation at its endpoint scan. Root cause was a use-after-free — the test read the config descriptor for the interrupt-endpoint scan after sceUsbdFreeConfigDescriptor had already freed it, so config->interface and the array behind it came back as freed garbage whose inner pointers were null or stale (alternate[4] was the first hardened read to trip). Fixed by refetching config descriptor 0 over the handle before the scan and skipping null altsetting/endpoint arrays in it (59dca5975, previously 873100e97).

On the updated head 59dca5975 with the controller live: the full device run including the cancel-vs-events race completes 3 for 3, exit 0; guest_usbd ctest passes on Linux and Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114427213 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848#issuecomment-6114427213). Originally posted by [13hourstilliland](https://github.com/13hourstilliland) on 2026-10-11T22:12:02Z. --- Thanks — the cancel lock is fixed in 21f764e69: `sceUsbdCancelTransfer` now holds `state.mutex` across `libusb_cancel_transfer`, and the lookup and the cancel are one critical section. `CompleteTransfer` taking the same mutex off the event thread cannot deadlock, because the cancel never invokes the callback synchronously. While running the device path on Windows with the Xbox 360 controller (VID 3537, PID 100F) I found a second crash in the test, present before this PR and without it: `guest_usbd_tests device` dies with an access violation at its endpoint scan. Root cause was a use-after-free — the test read the config descriptor for the interrupt-endpoint scan after `sceUsbdFreeConfigDescriptor` had already freed it, so `config->interface` and the array behind it came back as freed garbage whose inner pointers were null or stale (`alternate[4]` was the first hardened read to trip). Fixed by refetching config descriptor 0 over the handle before the scan and skipping null `altsetting`/`endpoint` arrays in it (59dca5975, previously 873100e97). On the updated head 59dca5975 with the controller live: the full device run including the cancel-vs-events race completes 3 for 3, exit 0; `guest_usbd` ctest passes on Linux and Windows.
adriadam10 commented 2026-10-12 20:41:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-12T20:41:31Z.

Review: APPROVED


Rebased onto current main; the only conflict was docs/dev/TechnicalDebt.md, whose libSceUsbd entry now lives in docs/dev/debt/libSceUsbd.md, so I moved the PR's updated line there. No logic changed.

Checked on Linux with the PR merged onto main: full build, guest_usbd passes and fails without the change (no-device path only, no USB access here), whole ctest green. The cancel-under-lock fix from the earlier review is in. Device, isochronous and PS5 behaviour were not run here and stay as recorded in TechnicalDebt; merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5495747068 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848#pullrequestreview-5495747068). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-12T20:41:31Z. **Review:** APPROVED --- Rebased onto current `main`; the only conflict was `docs/dev/TechnicalDebt.md`, whose libSceUsbd entry now lives in `docs/dev/debt/libSceUsbd.md`, so I moved the PR's updated line there. No logic changed. Checked on Linux with the PR merged onto `main`: full build, `guest_usbd` passes and fails without the change (no-device path only, no USB access here), whole ctest green. The cancel-under-lock fix from the earlier review is in. Device, isochronous and PS5 behaviour were not run here and stay as recorded in TechnicalDebt; merging once CI is green.
github-actions[bot] commented 2026-10-12 20:47:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T20:47:14Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1835 .gitmodules L33
#4740 .gitmodules L33
#4749 .gitmodules L33
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132335727 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3848#issuecomment-6132335727). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T20:47:14Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1835 | `.gitmodules` [L33](https://github.com/boykopovar/AnyPS5/blob/c8debc778879d9ec234121906152440d7d735cea/.gitmodules#L33) | | | | #4740 | `.gitmodules` [L33](https://github.com/boykopovar/AnyPS5/blob/c8debc778879d9ec234121906152440d7d735cea/.gitmodules#L33) | | | | #4749 | `.gitmodules` [L33](https://github.com/boykopovar/AnyPS5/blob/c8debc778879d9ec234121906152440d7d735cea/.gitmodules#L33) | | |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!3848
No description provided.