Skip to content

feat(usb_device): X-Input beside a WebUSB vendor interface, recognized as an Xbox 360 controller - #893

Merged
finger563 merged 5 commits into
mainfrom
feat/usb-xinput-composite
Nov 7, 2026
Merged

finger563 merged 5 commits into
mainfrom
feat/usb-xinput-composite

Conversation

@finger563

@finger563 finger563 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

X-Input beside a WebUSB vendor interface, recognized as an Xbox 360 controller. Until now a PC only bound the X-Input function when it was the device's only function (the device then took the Xbox 360 controller's own identity and 0xFF device class); combined with anything else it kept the composite identity and nothing told Windows that one interface was an Xbox controller. A device that is a controller and has a configuration console on a WebUSB interface needs both.

  • include/ms_os_20.hpp: a dependency-free builder for the Microsoft OS 2.0 descriptor set, one function subset per interface Windows must bind a driver to without an INF. The WebUSB vendor interface gets WINUSB plus the DeviceInterfaceGUIDs registry property (as before); an X-Input interface gets the XUSB10 compatible ID, which Windows' own Xbox 360 driver matches (USB\MS_COMP_XUSB10, the same route the virtual-controller drivers take). Host-tested (test/ms_os_20_host_test.cpp: the WinUSB-only set is byte-for-byte the 0xB2-byte set TinyUSB's example ships and the device served until now, plus the two-function sets in both orders, truncation, and the empty set). The device code builds its set from the enabled functions instead of a fixed 0xB2-byte blob.
  • Composite identity rules, documented in the header, the component README and the example: X-Input alone keeps the retail identity; X-Input beside the WebUSB vendor function keeps the composite identity, and the application sets Config::vid to Microsoft's 0x045E (Linux's xpad binds the interface class under it) and Config::pid to a product ID Windows' Xbox driver INF does not list by hardware ID (the retail controllers' are), since a hardware-ID match takes the whole device and hides the vendor interface. X-Input beside other functions without the WebUSB one still gets no MS OS 2.0 descriptor, so Windows does not bind it.
  • xinput_example gains a sdkconfig.composite variant (XINPUT_EXAMPLE_COMPOSITE, with XINPUT_EXAMPLE_COMPOSITE_PID) that adds the WebUSB vendor interface; the default stays X-Input only.
  • usb_device_tests.yml: the component's two host tests now run in CI (the X-Input one never did).

First consumer: the esp-usb-ble-hid dongle, which will offer Xbox 360 emulation as a console setting beside its Switch Pro one while keeping the console interface in both modes.

Verified on hardware with the dongle: Windows binds its Xbox 360 driver through the compatible ID on this composite device and the controller works. Linux not yet checked. The X-Input-only path is unchanged (same descriptor bytes, same identity).

Test plan

  • ms_os_20_host_test and xinput_host_test pass
  • xinput_example builds for esp32s3, X-Input only (default) and composite (sdkconfig.composite)
  • CI
  • On hardware (the esp-usb-ble-hid dongle, feat: Xbox 360 (X-Input) as a second USB controller type, chosen from the console finger563/esp-usb-ble-hid#33, X-Input beside the WebUSB console interface with VID 045E and its own PID): Windows binds its Xbox 360 driver through the XUSB10 compatible ID and sees a working controller; the 206-byte MS OS 2.0 set read back from the device matches the builder's output (WinUSB + GUID for the vendor interface, XUSB10 for the X-Input one)
  • Linux xpad binds

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

finger563 and others added 2 commits November 6, 2026 16:36
…zed as an Xbox controller

A PC only bound the X-Input function when it was the device's only
function: the device then took the Xbox 360 controller's own identity and
0xFF device class. Combined with anything else it kept the composite
identity and nothing told Windows that one interface was an Xbox
controller, so a controller with a configuration console on a WebUSB
interface could not be both.

include/ms_os_20.hpp builds the Microsoft OS 2.0 descriptor set from a
list of functions, one subset per interface Windows must bind a driver to
without an INF: the WebUSB vendor interface gets WINUSB plus the
DeviceInterfaceGUIDs registry property, as before; an X-Input interface
gets the XUSB10 compatible ID, which Windows' own Xbox 360 driver matches
(USB\MS_COMP_XUSB10, the route the virtual-controller drivers take). The
device code builds its set from the enabled functions instead of a fixed
0xB2-byte blob; the WinUSB-only set is byte-for-byte what it served until
now, which the host test pins along with the two-function sets.

The application gives a composite controller Microsoft's vendor ID (Linux's
xpad binds the interface class under it) and a product ID Windows' Xbox
driver INF does not list by hardware ID, since that match takes the whole
device and hides the vendor interface; the header says so. X-Input alone
is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
…s in CI

xinput_example gains a sdkconfig.composite variant (XINPUT_EXAMPLE_COMPOSITE
with XINPUT_EXAMPLE_COMPOSITE_PID) that adds a WebUSB vendor interface
beside the controller; the default stays X-Input only. The component README
and the example's explain how a PC recognizes the controller in each
configuration. usb_device_tests.yml runs the component's two host tests
(the X-Input one never ran in CI).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
Copilot AI balanced review requested due to automatic review settings November 6, 2026 22:36
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

Adds support for serving a Microsoft OS 2.0 descriptor set that can bind both WinUSB (for a WebUSB vendor interface) and the Xbox 360 X-Input driver (via XUSB10) on composite devices, plus CI coverage for the component’s host-buildable tests.

Changes:

  • Introduces a dependency-free MS OS 2.0 descriptor-set builder and a host unit test validating structure/bytes.
  • Updates UsbDevice to build the MS OS 2.0 descriptor dynamically based on enabled interfaces (vendor + optional X-Input).
  • Adds an xinput_example composite sdkconfig/Kconfig path and a GitHub Actions workflow to run host tests in CI.
File Description
components/​usb_device/​include/​ms_os_20.hpp New MS OS 2.0 descriptor-set builder for per-interface driver binding without INF
components/​usb_device/​src/​usb_device.cpp Replaces fixed MS OS 2.0 blob with builder output (WinUSB + optional XUSB10)
components/​usb_device/​test/​ms_os_20_host_test.cpp New host unit test for descriptor-set builder correctness and edge cases
.github/​workflows/​usb_device_tests.yml New CI workflow running usb_device host tests
components/​usb_device/​xinput_example/​main/​Kconfig.projbuild Adds composite configuration options (enable vendor interface + custom PID)
components/​usb_device/​xinput_example/​sdkconfig.composite New sdkconfig defaults enabling composite variant + TinyUSB vendor count
components/​usb_device/​xinput_example/​main/​xinput_example.cpp Example logic for composite VID/PID + vendor function + updated log output
components/​usb_device/​xinput_example/​README.md Documents composite build variant and host driver-binding rationale
components/​usb_device/​include/​usb_device.hpp Expands documentation on identity/binding rules across configurations
components/​usb_device/​README.md Updates component docs describing X-Input binding alone vs composite with WebUSB

Review effort: Lite

finger563 and others added 2 commits November 6, 2026 20:51
…includes

Review: the set's size was narrowed to the BOS capability's uint16_t
without a check (a few hundred bytes today; fail initialization with a
clear error if it ever exceeds the field), the builder was called through
the implicit vector -> span conversion (now an explicit span), and the host
test includes <span> / <vector> itself instead of through ms_os_20.hpp.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
…usy means

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563
finger563 requested a balanced review from Copilot November 7, 2026 05:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +107 to +117
// set header
u16(static_cast<uint16_t>(kSetHeaderLen));
u16(kSetHeaderDescriptor);
u32(kWindowsVersion);
u16(static_cast<uint16_t>(total));
// configuration subset header: configuration 0, reserved, subset length
u16(static_cast<uint16_t>(kConfigurationSubsetHeaderLen));
u16(kSubsetHeaderConfiguration);
out.push_back(0x00);
out.push_back(0x00);
u16(static_cast<uint16_t>(total - kSetHeaderLen));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

descriptor_set() now refuses a set longer than kMaxSetLength (0xFFFF, the format's 16-bit length fields) and returns an empty vector; the per-subset lengths are at most 160 bytes by construction. The device treats an empty set as an initialization error with the computed size in the log. Host test added: an over-long list is refused, one just under the limit builds with consistent lengths.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

Comment on lines +1332 to +1333
impl_->ms_os_20_desc =
espp::ms_os_20::descriptor_set(std::span<const espp::ms_os_20::Function>{ms_functions});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Built from data()/size() now. (The range constructor is C++20, and espp builds as C++23 throughout, but this form needs neither.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

Comment on lines +89 to +91
std::vector<uint8_t> out;
const size_t total = descriptor_set_length(functions);
out.reserve(total);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added: descriptor_set() asserts that out.size() == descriptor_set_length(functions) before returning.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

Review: the builder narrowed the set and subset lengths to uint16_t without
a check. descriptor_set() now returns an empty vector when the set would
exceed kMaxSetLength (0xFFFF; thousands of functions, a caller error), and
asserts that the bytes it emitted match descriptor_set_length(). The device
treats an empty set as an initialization error. The span is built from
data()/size(). Host test: an over-long list is refused, one just under the
limit builds with consistent lengths.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
finger563 added a commit to finger563/esp-usb-ble-hid that referenced this pull request Nov 7, 2026
… the console

A "USB controller" setting (device-config key 0x0A, NVS usb_ctl; Switch
Pro by default) picks what the dongle is to the USB host at the next boot:
the Switch Pro Controller as before, or a wired Xbox 360 controller for
Windows, Linux, Steam and Android (native X-Input: analog triggers, the
Guide button). The dongle stays a composite device in both modes, so the
console keeps working.

usb.cpp builds either the HID (Switch Pro) function or espp's X-Input
function beside the vendor console. Xbox mode presents Microsoft's vendor
ID with the dongle's own product ID (CONFIG_DONGLE_USB_XBOX_PID, 0x0E60):
the retail identity would make Windows' Xbox driver take the whole device
and hide the console; with its own product ID Windows splits the composite
device and binds the X-Input interface through the XUSB10 compatible ID
espp advertises (esp-cpp/espp#893), and Linux's xpad binds the interface
class under the Microsoft vendor ID. Rumble from the host is ignored.

main.cpp maps the settings-adjusted GamepadInputs to an X-Input state and
pushes it on change; the Switch-only L+R registration window never opens
in Xbox mode. INFO gains the running mode as an optional trailer byte, so
the console's Device tab shows it and whether a change awaits a reboot.
The console lists both identities as permitted dongles (device filter,
auto-connect, plug-in), adds the select to Settings with a reboot note,
and the README documents the mode and its platform limits.

Depends on the espp release that carries esp-cpp/espp#893; the
espp/usb_device floor is bumped to it before merge.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563
finger563 merged commit 351825c into main Nov 7, 2026
184 checks passed
@finger563
finger563 deleted the feat/usb-xinput-composite branch November 7, 2026 06:49
finger563 added a commit to finger563/esp-usb-ble-hid that referenced this pull request Nov 8, 2026
Every espp floor moves to 1.4.2, the release that carries esp-cpp/espp#893,
and the lock resolves all 32 espp components to it. No local override is
needed any more. Verified on the T-Dongle-S3 (built from the registry, OTA):
the MS OS 2.0 set read back from the device carries WINUSB + XUSB10, the
neutral X-Input report queued at mount is taken when the host reads it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
finger563 added a commit to finger563/esp-usb-ble-hid that referenced this pull request Nov 8, 2026
… the console (#33)

* feat: Xbox 360 (X-Input) as a second USB controller type, chosen from the console

A "USB controller" setting (device-config key 0x0A, NVS usb_ctl; Switch
Pro by default) picks what the dongle is to the USB host at the next boot:
the Switch Pro Controller as before, or a wired Xbox 360 controller for
Windows, Linux, Steam and Android (native X-Input: analog triggers, the
Guide button). The dongle stays a composite device in both modes, so the
console keeps working.

usb.cpp builds either the HID (Switch Pro) function or espp's X-Input
function beside the vendor console. Xbox mode presents Microsoft's vendor
ID with the dongle's own product ID (CONFIG_DONGLE_USB_XBOX_PID, 0x0E60):
the retail identity would make Windows' Xbox driver take the whole device
and hide the console; with its own product ID Windows splits the composite
device and binds the X-Input interface through the XUSB10 compatible ID
espp advertises (esp-cpp/espp#893), and Linux's xpad binds the interface
class under the Microsoft vendor ID. Rumble from the host is ignored.

main.cpp maps the settings-adjusted GamepadInputs to an X-Input state and
pushes it on change; the Switch-only L+R registration window never opens
in Xbox mode. INFO gains the running mode as an optional trailer byte, so
the console's Device tab shows it and whether a change awaits a reboot.
The console lists both identities as permitted dongles (device filter,
auto-connect, plug-in), adds the select to Settings with a reboot note,
and the README documents the mode and its platform limits.

Depends on the espp release that carries esp-cpp/espp#893; the
espp/usb_device floor is bumped to it before merge.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* web: the ready message names both USB identities

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* docs: reboot before an OTA when a USB controller change is waiting

Seen on hardware: with the Xbox 360 setting saved but not yet applied, the
freshly flashed image booted straight into Xbox mode, and the espp-ota
command-line tool, waiting for the identity it had flashed, reported the
dongle missing and left the image unconfirmed (mark-valid fixed it). Say so
next to the setting; the console handles either identity.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* fix(usb): start_usb() parameter named the same in declaration and definition

cppcheck (CI's static analysis): funcArgNamesDifferent. The definition says
`which` because usb.cpp keeps the running mode in a file-scope `personality`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* feat(gui): the screen's USB-side icon follows the controller type

Xbox 360 mode shows the Xbox icon where the Switch icon was (the design's
xbox image was already compiled in); Switch Pro mode is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* feat(gui): the big icons show their link state

Each side of the screen is a big icon (the controller on the BLE side, the
host on the USB side) with a small link badge under it. The design
recolored the controller icon solid white while the host icon kept its own
colors, so the Xbox logo looked "on" (green) on the USB side from boot and
never on the BLE side. Both big icons are now a white silhouette while their
link is down and show their own colors once it is up; the badges toggle as
before.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* web: X-Input has no handshake; say whether the host reads the reports

In Xbox 360 mode the Device tab showed "waiting for host handshake", the
Pro Controller wording, whenever the host was not reading the X-Input
interface (on a Mac, which has no X-Input driver, the first report stays
queued and the firmware's ready flag is false). Say what it is: the host is
or is not reading X-Input reports, with the report count, and name the
usual reason. README says the same next to the setting.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* fix(usb): X-Input state is kept and retried until the host takes it

Review: a report the endpoint could not take was dropped, so a release, or
the neutral state after a BLE disconnect, could leave the host with
pressed buttons until the next input change. usb_push_xinput() now only
records the latest state; the sender task submits it and retries every
tick until the endpoint accepts it, and after a (re)mount the state is sent
again so the new host starts from the truth.

Xbox mode without the vendor interface (not the default build): X-Input
alone with the retail identity, the CDC log console left out in that mode;
without the vendor interface there is no MS OS 2.0 descriptor to carry the
XUSB10 compatible ID, and a composite device with neither binds nothing on
Windows.

usb_hid_ready() in Xbox mode reports a stall only after a report has been
in flight for 250 ms (a working host polls every 4 ms), not the gap between
a submit and the host's next poll.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* web: reconnect to a dongle that came back under its other USB identity

Review: after a controller-type change the reboot re-enumerates the dongle
as the other identity (Switch Pro <-> Xbox 360) and the recovery only
looked for the one it lost. The reconnect attempts now look for the
dongle's serial under every dongle identity, an arrival under the other
identity with the same serial is opened at once, and when the dongle does
not show up while a controller-type change was pending the log says that
the new identity may need to be authorized through Connect dongle. The
X-Input USB line says "ready, no report sent yet" before the first report.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* fix(usb): X-Input sender never spins; a remount keeps the resend pending

Review round 3:
- a pending X-Input state no longer bypasses the sender's 8 ms wait: a push
  wakes it once (kick), after that a report the endpoint cannot take is
  retried on the tick, so a host that never reads cannot make the
  priority-10 task spin
- the mount callback increments the state generation instead of rewinding
  the sent one, so an acknowledgment of a report submitted before the
  remount cannot clear the resend
- web: a dongle arriving under its other identity during a recovery wakes
  the reconnect supervisor (with the identity it lost) instead of being
  opened behind its back, so its cancellation and discard semantics hold

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

* build: espp >= 1.4.2 (composite X-Input: the XUSB10 compatible ID)

Every espp floor moves to 1.4.2, the release that carries esp-cpp/espp#893,
and the lock resolves all 32 espp components to it. No local override is
needed any more. Verified on the T-Dongle-S3 (built from the registry, OTA):
the MS OS 2.0 set read back from the device carries WINUSB + XUSB10, the
neutral X-Input report queued at mount is taken when the host reads it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants