Skip to content

GameInput: size PlayStation HID read buffer from HIDP_CAPS - #2546

Open
ramon-victor wants to merge 1 commit into
xcl-dev:mainfrom
ramon-victor:fix/playstation-hid-read-buffer
Open

ramon-victor wants to merge 1 commit into
xcl-dev:mainfrom
ramon-victor:fix/playstation-hid-read-buffer

Conversation

@ramon-victor

Copy link
Copy Markdown
Contributor

Fixes #2465.

Problem

crates/xcl/src/hle/playstation.rs matches DualShock 4 controllers over Bluetooth (vid&0002054c, service 00001124-0000-1000-8000-00805f9b34fb), opens their HID handle, and reads reports into a hardcoded 128-byte buffer (let mut buf = [0u8; 128];).

On Windows, hidclass.sys rejects every ReadFile call whose buffer is smaller than HIDP_CAPS.InputReportByteLength with ERROR_INVALID_USER_BUFFER (1784). A DualShock 4 over Bluetooth advertises InputReportByteLength = 547 (input report 0x19), so ReadFile fails immediately on the first call, causing reader() to close the handle, sleep 1 s, and reconnect in a loop without reading input.

Fix

  • Query HIDP_CAPS.InputReportByteLength on the opened HID handle via HidD_GetPreparsedData, HidP_GetCaps, and HidD_FreePreparsedData (hid.dll, raw-dylib), always freeing preparsed data before returning.
  • Allocate the ReadFile buffer dynamically to read_buffer_len(input_report_byte_length(h)), clamping nonzero cap lengths to at least 64 bytes and falling back to 1024 bytes (>= 547) if HIDP_CAPS cannot be queried or reports zero.
  • Guard the unreadable-report log (buf[0]) against zero-length reads so it does not log a stale report ID.
  • Leave known() and parse() untouched so this change stays independent of Bluetooth DualSense report-parsing work (GameInput: read a DualSense and a DualShock 4's full reports over Bluetooth #2081).
  • Add unit tests in playstation::read_buffer_tests covering HidpCaps ABI size/offset against Windows SDK hidpi.h, buffer sizing across USB/Bluetooth/fallback lengths, and invalid-handle handling through hid.dll.

Tested

  • Targeted unit tests (cargo test --lib -p xcl playstation): 4 passed, 0 failed (hidp_caps_layout_and_read_buffer_sizing, input_report_byte_length_handles_invalid_handle, plus the 2 existing PlayStation tests).
  • Full workspace (.\scripts\test.ps1, NVIDIA GeForce RTX 3070, Windows 11): 1425 passed, 0 failed, 46 ignored.
  • Release build (cargo build --release -p xcl) and 60 s retail smoke run (see below).

Not tested

  • Live input delivery on a physical DualShock 4 controller connected over Bluetooth (no DualShock 4 hardware is attached on this test machine; HIDP_CAPS layout and HidD_GetPreparsedData/HidP_GetCaps/HidD_FreePreparsedData signatures were verified against Windows SDK hidpi.h/hidsdi.h, and hid.dll raw-dylib invocation was verified on INVALID_HANDLE_VALUE).

Regression run

Main (f087b99):

xcl-smoke v1
commit: f087b998d0d51a9f11bbfa0761967a63e778edd9
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14467 MB

This PR (20b18f7):

xcl-smoke v1
commit: 20b18f742afb2f2cfa0670d132cc78329757780d
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1622 (PresentX 1622)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14003 MB

AI use

  • Antigravity (Gemini 4 Neon): implementation, ABI verification against Windows SDK headers, tests, and PR drafting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 20b18f7 (on its base e1d84b7).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::recomp::lower_dxil::tests::mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 1413 passed, 0 failed, 44 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, dispatch_chain_native_wave64_builds_whole, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: hidp_caps_layout_and_read_buffer_sizing, input_report_byte_length_handles_invalid_handle.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 1424 passed, 0 failed, 46 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main e1d84b7 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests, const_and_null_mask_operands_agree_with_hlsl_on_the_gpu and mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@CalvyPZ CalvyPZ added the off-goal Does not bear on the current goal (world loading); waits until it is reached label Nov 25, 2026
@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of hidp_caps_layout_and_read_buffer_sizing and input_report_byte_length_handles_invalid_handle: 2 passed. No physical pad on this lane; these verify the HID parsing/sizing path, not device I/O.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@throny

throny commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #2525 solves the same problem (for #2465) in a different way: #1607, group D120. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

off-goal Does not bear on the current goal (world loading); waits until it is reached

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GameInput: DualShock 4 over Bluetooth fails immediately on ReadFile (128-byte buffer < 547-byte HIDP_CAPS.InputReportByteLength)

6 participants