feat(libs): implement libSceUsbd over libusb #2870

Closed
LordLava wants to merge 2 commits from upstream-pr/2870 into main
LordLava commented 2026-10-10 17:12:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LordLava on 2026-10-10T17:12:53Z.


What

Implements libSceUsbd, the console's libusb 1.0 wrapper, replacing the 25 throwing stubs and 5 partial functions with a real host backend. All 61 exports the library is known to have are implemented and none calls NotImplemented_nid_no_patch; tools/progress.py reports done 61, todo 0 (was 5/30).

libSceUsbd is a thin wrapper over libusb on the console: its exports, argument lists and error codes are libusb's, with the context and the transfer's own callback hidden behind a global context and a per-process registry. The guest-visible structures are layout identical to libusb's, which static_assert on sizeof and offsetof pins at compile time, so device, handle, descriptor and transfer pointers cross the boundary without a copy. libusb error codes are translated to 0x80240000 - code, with LIBUSB_ERROR_OTHER at 0x802400FF; the same static assertions pin every constant.

Two details worth flagging for review:

  • Names and NIDs. The export set is the PS4 library's 61 exports, which agree with the PS5 NID list. Each name hashes to the NID titles import, which I checked by reimplementing the NID algorithm and matching all 61 against the reference table. No PS5-native header for this library is public, so the argument lists are the PS4 ones and only NID-level PS5 equality is verified; this is recorded in technical debt.
  • Transfer callbacks. A guest callback cannot be handed to libusb directly, because libusb invokes its own callback and overwrites the field it shares with the guest's user data. The sceUsbdFill*Transfer functions park the guest callback in a registry, restore the user data pointer before invoking it, and drop the entry when the transfer completes or is freed.

The new third-party dependency is libusb-cmake, the CMake build project libusb officially links to, as a submodule pinned at tag v1.0.30-0. libusb has no CMake build of its own. It is built statically with position-independent code so it links into the shared .prx without adding a runtime file to libs/.

Tested

Linux x86-64, GCC 16.2.1, cmake 4.4.4, against libusb 1.0.30 built from the pinned submodule.

  • ctest -R guest_usbd equivalent (GuestUsbd) passes: initialization, device enumeration and descriptor reads against the host's real devices, the empty-list and null-argument paths, event handling with a zero and a 50 ms timeout, transfer allocation and filling, the callback round-trip, and the isochronous packet buffer bounds.
  • python3 tools/check_conventions.py --base main --head HEAD reports All conventions checks passed.
  • The library and the test both link and run through the real CMake targets, with the submodule initialized by git submodule update --init 3rdparty/libusb-cmake.
  • guest_usbd is registered as a test by the library's CMakeLists.txt, so CI covers it.

Not tested, and worth saying plainly:

  • No full build and no title run. The other 3rdparty submodules are not checked out in my environment, so I could not run cmake --build build --target libs or start a game. CI runs the full build.
  • No PS5 hardware. No console was available, so nothing here is confirmed against firmware. The unverified assumptions are recorded in docs/dev/TechnicalDebt.md under the libSceUsbd entry, including the PS4-sourced prototypes, the Windows timeval conversion, and the three exports where libusb has no equivalent and behaviour had to be chosen (sceUsbdCheckConnected, sceUsbdClaimInterface, sceUsbdOpenDeviceWithVidPid).
  • No USB device was exercised. The host used for testing has no /dev/bus/usb access, so device access paths (open, claim, transfer submission) return real libusb errors but never complete successfully in the test.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4813018209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2870). Originally posted by [LordLava](https://github.com/LordLava) on 2026-10-10T17:12:53Z. --- ### What Implements `libSceUsbd`, the console's libusb 1.0 wrapper, replacing the 25 throwing stubs and 5 partial functions with a real host backend. All 61 exports the library is known to have are implemented and none calls `NotImplemented_nid_no_patch`; `tools/progress.py` reports `done 61, todo 0` (was 5/30). `libSceUsbd` is a thin wrapper over libusb on the console: its exports, argument lists and error codes are libusb's, with the context and the transfer's own callback hidden behind a global context and a per-process registry. The guest-visible structures are layout identical to libusb's, which `static_assert` on `sizeof` and `offsetof` pins at compile time, so device, handle, descriptor and transfer pointers cross the boundary without a copy. libusb error codes are translated to `0x80240000 - code`, with `LIBUSB_ERROR_OTHER` at `0x802400FF`; the same static assertions pin every constant. Two details worth flagging for review: - **Names and NIDs.** The export set is the PS4 library's 61 exports, which agree with the PS5 NID list. Each name hashes to the NID titles import, which I checked by reimplementing the NID algorithm and matching all 61 against the reference table. No PS5-native header for this library is public, so the argument lists are the PS4 ones and only NID-level PS5 equality is verified; this is recorded in technical debt. - **Transfer callbacks.** A guest callback cannot be handed to libusb directly, because libusb invokes its own callback and overwrites the field it shares with the guest's user data. The `sceUsbdFill*Transfer` functions park the guest callback in a registry, restore the user data pointer before invoking it, and drop the entry when the transfer completes or is freed. The new third-party dependency is `libusb-cmake`, the CMake build project libusb officially links to, as a submodule pinned at tag `v1.0.30-0`. libusb has no CMake build of its own. It is built statically with position-independent code so it links into the shared `.prx` without adding a runtime file to `libs/`. ### Tested Linux x86-64, GCC 16.2.1, cmake 4.4.4, against libusb 1.0.30 built from the pinned submodule. - `ctest -R guest_usbd` equivalent (`GuestUsbd`) passes: initialization, device enumeration and descriptor reads against the host's real devices, the empty-list and null-argument paths, event handling with a zero and a 50 ms timeout, transfer allocation and filling, the callback round-trip, and the isochronous packet buffer bounds. - `python3 tools/check_conventions.py --base main --head HEAD` reports **All conventions checks passed.** - The library and the test both link and run through the real CMake targets, with the submodule initialized by `git submodule update --init 3rdparty/libusb-cmake`. - `guest_usbd` is registered as a test by the library's `CMakeLists.txt`, so CI covers it. Not tested, and worth saying plainly: - **No full build and no title run.** The other `3rdparty` submodules are not checked out in my environment, so I could not run `cmake --build build --target libs` or start a game. CI runs the full build. - **No PS5 hardware.** No console was available, so nothing here is confirmed against firmware. The unverified assumptions are recorded in `docs/dev/TechnicalDebt.md` under the `libSceUsbd` entry, including the PS4-sourced prototypes, the Windows `timeval` conversion, and the three exports where libusb has no equivalent and behaviour had to be chosen (`sceUsbdCheckConnected`, `sceUsbdClaimInterface`, `sceUsbdOpenDeviceWithVidPid`). - **No USB device was exercised.** The host used for testing has no `/dev/bus/usb` access, so device *access* paths (open, claim, transfer submission) return real libusb errors but never complete successfully in the test. ### 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: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-10 17:13:03 +00:00 (Migrated from github.com)

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


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100102818 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2870#issuecomment-6100102818). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T17:13:03Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

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!2870
No description provided.