feat(libSceUsbd): pass the device, packet size, alternate setting and halt calls through to libusb #9338

Merged
MaxLikesCode merged 1 commit from upstream-pr/9338 into main 2026-10-25 00:59:57 +00:00
MaxLikesCode commented 2026-10-21 18:48:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T18:48:26Z.


What

Implements eight more sceUsbd calls over the host libusb, with the argument checks and results of FreeBSD 11.4 libusb10.c, as #9190 does for the configuration and descriptor queries. Their signatures were already in the file, from the console type database in orbital; shadPS4 maps each of these calls one-to-one onto libusb.

Call Result
sceUsbdGetDevice the device of the handle; null for a null handle
sceUsbdGetDeviceSpeed libusb's speed, 0 to 4; a host SuperSpeed+ device reports SuperSpeed, the fastest speed FreeBSD 11.4 has; 0 for a null device
sceUsbdGetMaxPacketSize wMaxPacketSize of the first endpoint with that address in any interface and alternate setting of the active configuration, 0x80240005 if there is none, 0x80240004 for a null device
sceUsbdGetMaxIsoPacketSize the same, and above full speed (size & 0x7FF) * min(1 + ((size >> 11) & 3), 3)
sceUsbdOpenDeviceWithVidPid a handle to the first listed device with that vendor and product, or null
sceUsbdSetInterfaceAltSetting 0x80240002 for an interface outside 0 to 31, 0x80240005 for an interface the handle has not claimed, 0x802400FF for any other failure; the alternate setting is taken as 8 bits
sceUsbdClearHalt 0, or 0x802400FF for an endpoint the host does not find
sceUsbdDetachKernelDriver 0, or 0x802400FF for any failure; on Windows libusb cannot detach drivers, so it always returns 0x802400FF there

The two packet sizes are computed as FreeBSD computes them, not through the host's libusb_get_max_packet_size and libusb_get_max_iso_packet_size, which return LIBUSB_ERROR_OTHER for an unreadable configuration and scale SuperSpeed endpoints by their companion descriptor.

To make the alternate setting check FreeBSD's, the library now records which interfaces each handle has claimed (sceUsbdClaimInterface, sceUsbdReleaseInterface, sceUsbdClose, sceUsbdExit). A device also stays known while a handle to it is open, so the device returned by sceUsbdGetDevice for a handle from sceUsbdOpenDeviceWithVidPid can be used (and referenced) until that handle is closed.

These throw:

  • sceUsbdSetInterfaceAltSetting while the device has transfers in flight, which FreeBSD cancels first;
  • sceUsbdClearHalt for host errors other than a missing endpoint, because FreeBSD ignores the result of the clear itself, so they have no known result; and for a null handle, which FreeBSD dereferences;
  • sceUsbdGetMaxIsoPacketSize for a null device, which FreeBSD dereferences;
  • like the other calls, any handle or device this library did not return.

docs/dev/debt/libSceUsbd.md moves the eight calls from the unknown list to two entries that say what follows FreeBSD and what the host cannot do. The event calls (sceUsbdHandleEvents, the event locks, sceUsbdWaitForEvent, sceUsbdEventHandlerActive) are left for a separate change.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, full Release build with BUILD_TESTING=ON, on main b4d10586e with #9190; this branch, which sits on #9190's commit, was rechecked merged onto main b00235dda (guest_usbd 3 of 3, device run 2 of 2, conventions).

  • guest_usbd gains checks for null and unknown handles and devices for all eight calls, and for sceUsbdOpenDeviceWithVidPid with no matching device, before and after sceUsbdExit. It passes 3 of 3. With Export.cpp from #9190 and the new tests, it fails at the first placeholder (sceUsbdDetachKernelDriver not implemented).
  • guest_usbd_tests device with APS5_USBD_DEVICE=1b1c:0c1a (CORSAIR Lighting Node CORE, full speed, HID) passes 12 of 12 runs and checks:
    • sceUsbdGetDevice of an open handle is the device it was opened from, and the speed is 1 to 4 (2 here);
    • both packet sizes of the interrupt IN endpoint against its endpoint descriptor, and 0x80240005 for an absent endpoint;
    • the alternate setting: 0x80240005 before the claim and after the release, 0x80240002 for interfaces 32 and -1, and 0 for setting 0 of the claimed interface;
    • sceUsbdClearHalt returns 0 on the interrupt endpoint and 0x802400FF for an absent one;
    • sceUsbdDetachKernelDriver returns 0x802400FF (Windows);
    • after the handle is closed and the device unreferenced, sceUsbdOpenDeviceWithVidPid opens it again, its device has the same vendor and product and the same packet size, and that device is unknown again once the handle is closed.
  • On an ASUS Aura controller (0b05:19af), the device run stops at #9190's existing claiming an interface failed, before the new checks: Windows does not give libusb its non-HID interfaces.
  • python tools/select_tests.py ctest build --output-on-failure --timeout 180 --parallel 8: 1523 of the 1526 selected tests pass. The 3 failures are agc_driver_sample_order, agc_driver_sample_order_grid_reference and agc_driver_sample_order_recipe_depth_locations, which fail the same way on main on this RTX 4090 and which #9056 fixes. agc_driver_image_array_window_past_layer_limit is not built: it does not compile with MinGW on main (#8310).
  • python tools/check_conventions.py --base origin/main passes. python tools/pr_overlap.py --branch feat/usbd-device-calls reports only #9190, which this builds on. #9116 adds test lines to GuestUsbd.cpp too; the two merge cleanly.
  • @simnJS ran guest_usbd_tests device from #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, and sceUsbdDetachKernelDriver returns 0x802400FF on both. On the Wooting the iso packet size check takes the multiplier path above full speed.
  • @xXPBSXx built guest_usbd_tests on Linux (WSL2, Ubuntu 24.04, GCC 13.3, Release) from this head merged onto main df2569d2c5 (comment): it exits with 0, and with Export.cpp reset to main it aborts (SIGABRT).
  • Rebased onto main 38d463a6cf after #10706 moved the declarations into libSceUsbd/include/Api.hpp, which already declares the eight calls with these signatures. At 64ae578dda, guest_usbd passes 3 of 3 and fails 3 of 3 with Export.cpp from main, and the device run with APS5_USBD_DEVICE=1b1c:0c1a passes 2 of 2. public_headers and the conventions check pass.
  • Not tested: a console, a title, bare-metal Linux or macOS.

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 under Silent stubs in their library's technical debt file
  • 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: none. #9190, which this builds on, is merged, so the diff of this PR is its one commit. @simnJS confirmed on #9190 that nobody else is working on these calls.
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes. Written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4956675803 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9338). Originally posted by [MaxLikesCode](https://github.com/MaxLikesCode) on 2026-10-21T18:48:26Z. --- ### What Implements eight more `sceUsbd` calls over the host libusb, with the argument checks and results of FreeBSD 11.4 [`libusb10.c`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libusb/libusb10.c), as #9190 does for the configuration and descriptor queries. Their signatures were already in the file, from the console type database in orbital; shadPS4 maps each of these calls one-to-one onto libusb. | Call | Result | |---|---| | `sceUsbdGetDevice` | the device of the handle; null for a null handle | | `sceUsbdGetDeviceSpeed` | libusb's speed, 0 to 4; a host SuperSpeed+ device reports SuperSpeed, the fastest speed FreeBSD 11.4 has; 0 for a null device | | `sceUsbdGetMaxPacketSize` | `wMaxPacketSize` of the first endpoint with that address in any interface and alternate setting of the active configuration, `0x80240005` if there is none, `0x80240004` for a null device | | `sceUsbdGetMaxIsoPacketSize` | the same, and above full speed `(size & 0x7FF) * min(1 + ((size >> 11) & 3), 3)` | | `sceUsbdOpenDeviceWithVidPid` | a handle to the first listed device with that vendor and product, or null | | `sceUsbdSetInterfaceAltSetting` | `0x80240002` for an interface outside 0 to 31, `0x80240005` for an interface the handle has not claimed, `0x802400FF` for any other failure; the alternate setting is taken as 8 bits | | `sceUsbdClearHalt` | 0, or `0x802400FF` for an endpoint the host does not find | | `sceUsbdDetachKernelDriver` | 0, or `0x802400FF` for any failure; on Windows libusb cannot detach drivers, so it always returns `0x802400FF` there | The two packet sizes are computed as FreeBSD computes them, not through the host's `libusb_get_max_packet_size` and `libusb_get_max_iso_packet_size`, which return `LIBUSB_ERROR_OTHER` for an unreadable configuration and scale SuperSpeed endpoints by their companion descriptor. To make the alternate setting check FreeBSD's, the library now records which interfaces each handle has claimed (`sceUsbdClaimInterface`, `sceUsbdReleaseInterface`, `sceUsbdClose`, `sceUsbdExit`). A device also stays known while a handle to it is open, so the device returned by `sceUsbdGetDevice` for a handle from `sceUsbdOpenDeviceWithVidPid` can be used (and referenced) until that handle is closed. These throw: - `sceUsbdSetInterfaceAltSetting` while the device has transfers in flight, which FreeBSD cancels first; - `sceUsbdClearHalt` for host errors other than a missing endpoint, because FreeBSD ignores the result of the clear itself, so they have no known result; and for a null handle, which FreeBSD dereferences; - `sceUsbdGetMaxIsoPacketSize` for a null device, which FreeBSD dereferences; - like the other calls, any handle or device this library did not return. `docs/dev/debt/libSceUsbd.md` moves the eight calls from the unknown list to two entries that say what follows FreeBSD and what the host cannot do. The event calls (`sceUsbdHandleEvents`, the event locks, `sceUsbdWaitForEvent`, `sceUsbdEventHandlerActive`) are left for a separate change. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, full Release build with `BUILD_TESTING=ON`, on `main` b4d10586e with #9190; this branch, which sits on #9190's commit, was rechecked merged onto `main` b00235dda (`guest_usbd` 3 of 3, device run 2 of 2, conventions). - `guest_usbd` gains checks for null and unknown handles and devices for all eight calls, and for `sceUsbdOpenDeviceWithVidPid` with no matching device, before and after `sceUsbdExit`. It passes 3 of 3. With `Export.cpp` from #9190 and the new tests, it fails at the first placeholder (`sceUsbdDetachKernelDriver not implemented`). - `guest_usbd_tests device` with `APS5_USBD_DEVICE=1b1c:0c1a` (CORSAIR Lighting Node CORE, full speed, HID) passes 12 of 12 runs and checks: - `sceUsbdGetDevice` of an open handle is the device it was opened from, and the speed is 1 to 4 (2 here); - both packet sizes of the interrupt IN endpoint against its endpoint descriptor, and `0x80240005` for an absent endpoint; - the alternate setting: `0x80240005` before the claim and after the release, `0x80240002` for interfaces 32 and -1, and 0 for setting 0 of the claimed interface; - `sceUsbdClearHalt` returns 0 on the interrupt endpoint and `0x802400FF` for an absent one; - `sceUsbdDetachKernelDriver` returns `0x802400FF` (Windows); - after the handle is closed and the device unreferenced, `sceUsbdOpenDeviceWithVidPid` opens it again, its device has the same vendor and product and the same packet size, and that device is unknown again once the handle is closed. - On an ASUS Aura controller (`0b05:19af`), the device run stops at #9190's existing `claiming an interface failed`, before the new checks: Windows does not give libusb its non-HID interfaces. - `python tools/select_tests.py ctest build --output-on-failure --timeout 180 --parallel 8`: 1523 of the 1526 selected tests pass. The 3 failures are `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference` and `agc_driver_sample_order_recipe_depth_locations`, which fail the same way on `main` on this RTX 4090 and which #9056 fixes. `agc_driver_image_array_window_past_layer_limit` is not built: it does not compile with MinGW on `main` (#8310). - `python tools/check_conventions.py --base origin/main` passes. `python tools/pr_overlap.py --branch feat/usbd-device-calls` reports only #9190, which this builds on. #9116 adds test lines to `GuestUsbd.cpp` too; the two merge cleanly. - @simnJS ran `guest_usbd_tests device` from #9339's branch, which contains #9338, on Windows 11 Pro 26200 ([comment](https://github.com/boykopovar/AnyPS5/pull/9339#issuecomment-6324333906)): an MSI Mystic Light HID controller (`1462:7D75`, full speed) and a Wooting 80HE keyboard (`31E3:1402`, high speed) pass 3 of 3 each, and `sceUsbdDetachKernelDriver` returns `0x802400FF` on both. On the Wooting the iso packet size check takes the multiplier path above full speed. - @xXPBSXx built `guest_usbd_tests` on Linux (WSL2, Ubuntu 24.04, GCC 13.3, Release) from this head merged onto `main` df2569d2c5 ([comment](https://github.com/boykopovar/AnyPS5/pull/9338#issuecomment-6332877474)): it exits with 0, and with `Export.cpp` reset to `main` it aborts (SIGABRT). - Rebased onto `main` 38d463a6cf after #10706 moved the declarations into `libSceUsbd/include/Api.hpp`, which already declares the eight calls with these signatures. At 64ae578dda, `guest_usbd` passes 3 of 3 and fails 3 of 3 with `Export.cpp` from `main`, and the device run with `APS5_USBD_DEVICE=1b1c:0c1a` passes 2 of 2. `public_headers` and the conventions check pass. - Not tested: a console, a title, bare-metal Linux or macOS. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: none. #9190, which this builds on, is merged, so the diff of this PR is its one commit. @simnJS confirmed on #9190 that nobody else is working on these calls. - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes. Written with Claude Code (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-22 04:41:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-22T04:41:26Z.


Linux data point for b77a110791 (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release, BUILD_TESTING=ON), this head merged onto main df2569d2c5 (clean merge). I built guest_usbd_tests and ran the executable directly (exit code, not through ctest):

  • With the PR: exit code 0.
  • Without it (the PR's test sources on top of main, with core/libs/prx/libSceUsbd/Export.cpp reset to main): it aborts (SIGABRT, exit code 134).
    Note: #9339 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332877474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9338#issuecomment-6332877474). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-22T04:41:26Z. --- Linux data point for `b77a110791` (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release, `BUILD_TESTING=ON`), this head merged onto `main` `df2569d2c5` (clean merge). I built `guest_usbd_tests` and ran the executable directly (exit code, not through `ctest`): - With the PR: exit code 0. - Without it (the PR's test sources on top of `main`, with `core/libs/prx/libSceUsbd/Export.cpp` reset to `main`): it aborts (SIGABRT, exit code 134). Note: #9339 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).
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!9338
No description provided.