Skip to content

usb-device-hid: Keep interface protocol separate from protocol mode. - #1177

Open
Duhan07 wants to merge 1 commit into
micropython:masterfrom
Duhan07:usb-device-hid-interface-protocol
Open

Duhan07 wants to merge 1 commit into
micropython:masterfrom
Duhan07:usb-device-hid-interface-protocol

Conversation

@Duhan07

@Duhan07 Duhan07 commented Nov 19, 2026 •

Copy link
Copy Markdown

Summary

MouseInterface was enumerated with bInterfaceProtocol 1 (keyboard) instead of 2 (mouse), because HIDInterface.protocol held both the descriptor value and the runtime protocol mode, and Device.config() builds the descriptor in two passes. This stores the descriptor value in a separate interface_protocol attribute. The constructor argument is unchanged.

Fixes #1176.

Testing

ESP32-S3 (ESP32_GENERIC_S3, 2 MB PSRAM), MicroPython v1.29.0, macOS host:

  • Before: interface "MicroPython Mouse" = class 3 / subclass 1 / protocol 1.
  • After: class 3 / subclass 1 / protocol 2. Keyboard still 3 / 1 / 1.
  • Checked with ioreg and by building the descriptor on the board.
  • Keyboard + mouse composite demo still types and moves the pointer.
  • Not tested: a real BIOS in boot protocol mode, other ports.

Generative AI

I used generative AI tools when creating this PR, but I checked the code and I am responsible for the code and the description above.

HIDInterface.protocol held both the bInterfaceProtocol descriptor value
and the runtime boot/report protocol mode. desc_cfg() resets it to 1 at
the end, and Device.config() calls desc_cfg() twice (once to measure the
length, once to write), so every boot interface was described as a
keyboard. MouseInterface was announced as protocol 1 instead of 2.

Store the descriptor value in a separate interface_protocol attribute.

Fixes issue micropython#1176.

Signed-off-by: Duhan Günsel <gnsl.duhan.07@gmail.com>
@Duhan07

Duhan07 commented Nov 19, 2026

Copy link
Copy Markdown
Author

Tested on real firmware, with a test build that logs every HID request:

  • Gigabyte B550M K (AMI-based UEFI, BIOS F-B): never sends SET_PROTOCOL, reads the report descriptors and stays in report mode. Keyboard and mouse work in setup with or without this fix.
  • EDK2/OVMF (QEMU 11.1.3, edk2-stable202408, device passed through):
    • without this fix, EDK2's keyboard driver also binds to the mouse interface: it sends SET_PROTOCOL(boot) and keyboard LED reports to it, then drops its 3-byte reports;
    • with this fix, only the keyboard gets SET_PROTOCOL(boot), and the mouse interface is left alone.

@Zosili Zosili left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested the patch against the descriptor path with a stub Descriptor that records the bytes
desc_cfg() writes, and it fixes the reported enumeration problem. Driving desc_cfg() twice like
Device.config() does, on master every interface ends up with bInterfaceProtocol = 1 (the reset
at the end of desc_cfg() overwrites the constructor value):

HIDInterface (no protocol)  09 04 03 01 03 01 01 00 00   subclass=1 protocol=1
HIDInterface(protocol=1)    09 04 03 01 03 01 01 00 00   subclass=1 protocol=1
HIDInterface(protocol=2)    09 04 03 01 03 01 01 00 00   subclass=1 protocol=1   <- mouse reports keyboard

With this branch:

HIDInterface (no protocol)  09 04 03 01 03 00 00 00 00   subclass=0 protocol=0
HIDInterface(protocol=1)    09 04 03 01 03 01 01 00 00   subclass=1 protocol=1
HIDInterface(protocol=2)    09 04 03 01 03 01 02 00 00   subclass=1 protocol=2

So MouseInterface now enumerates as a mouse, and self.protocol stays in report mode (1) as HID
1.11 F.5 wants. A side effect worth mentioning: GET_PROTOCOL before desc_cfg() has run now answers
1 (report) instead of the descriptor value, which is the correct answer for a device that has not
switched to boot mode.

One question about the version: metadata(version="0.2.1") is a patch bump, but the meaning of the
public protocol attribute changes — it used to be the descriptor value and is now the runtime mode,
so anything reading interface.protocol to find out what kind of HID device this is will get a
different answer. Would 0.3.0 be the more accurate bump?

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.

usb-device-hid: MouseInterface reports bInterfaceProtocol 1 (keyboard) instead of 2

2 participants