diff --git a/micropython/usb/usb-device-hid/usb/device/hid.py b/micropython/usb/usb-device-hid/usb/device/hid.py index 9e4c70dde..ce7dcc7bc 100644 --- a/micropython/usb/usb-device-hid/usb/device/hid.py +++ b/micropython/usb/usb-device-hid/usb/device/hid.py @@ -34,6 +34,8 @@ _INTERFACE_SUBCLASS_NONE = const(0x00) _INTERFACE_SUBCLASS_BOOT = const(0x01) +# These values will only make sense when interface subclass +# is 0x01, which indicates boot protocol support. _INTERFACE_PROTOCOL_NONE = const(0x00) _INTERFACE_PROTOCOL_KEYBOARD = const(0x01) _INTERFACE_PROTOCOL_MOUSE = const(0x02) @@ -130,7 +132,9 @@ def desc_cfg(self, desc, itf_num, ep_num, strs): itf_num, 1, _INTERFACE_CLASS, - _INTERFACE_SUBCLASS_NONE, + _INTERFACE_SUBCLASS_NONE + if self.protocol == _INTERFACE_PROTOCOL_NONE + else _INTERFACE_SUBCLASS_BOOT, self.protocol, len(strs) if self.interface_str else 0, ) @@ -148,7 +152,12 @@ def desc_cfg(self, desc, itf_num, ep_num, strs): desc.endpoint(self._int_ep, "interrupt", 8, 8) self.idle_rate = 0 - self.protocol = 0 + + # This variable is reused to track boot protocol status. + # 0 for boot protocol, 1 for report protocol + # According to Device Class Definition for Human Interface Devices (HID) v1.11 + # Appendix F.5, the device comes up in non-boot mode by default. + self.protocol = 1 def num_eps(self): return 1 @@ -197,6 +206,8 @@ def on_interface_control_xfer(self, stage, request): if desc_type == _DESC_HID_TYPE: return self.get_hid_descriptor() if desc_type == _DESC_REPORT_TYPE: + # Reset to report protocol when report descriptor is requested + self.protocol = 1 return self.report_descriptor elif req_type == _REQ_TYPE_CLASS: # HID Spec p50: 7.2 Class-Specific Requests