Skip to content

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

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
13hourstilliland:feat/usbd-libusb
Oct 12, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
13hourstilliland:feat/usbd-libusb

Conversation

@13hourstilliland

Copy link
Copy Markdown
Contributor

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: feat(libSceUsbd): declare the device and handle calls for the no-device path #3847
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

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

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

Thanks — the cancel lock is fixed in 21f764e: 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 (59dca59, previously 873100e).

On the updated head 59dca59 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.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 11, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 11, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

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

@adriadam10
adriadam10 merged commit 529f7bf into boykopovar:main Oct 12, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants