From ad32d2651161d9f51b3b0d259f2872e15fc23d07 Mon Sep 17 00:00:00 2001 From: sago35 Date: Sat, 1 Jul 2023 18:58:23 +0900 Subject: [PATCH] machine/usb/hid,joystick: fix hidreport (3) (#3802) * machine/usb/hid,joystick: fix hidreport (3) and handling of logical, usage, and physical minimum/maximum values --- src/machine/usb/descriptor/hidreport.go | 85 ++++++++++++++++--------- src/machine/usb/descriptor/joystick.go | 5 +- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/machine/usb/descriptor/hidreport.go b/src/machine/usb/descriptor/hidreport.go index 914279a5..0d90d47c 100644 --- a/src/machine/usb/descriptor/hidreport.go +++ b/src/machine/usb/descriptor/hidreport.go @@ -3,12 +3,12 @@ package descriptor const ( hidUsagePage = 0x05 hidUsage = 0x09 - hidLogicalMinimum = 0x15 - hidLogicalMaximum = 0x25 - hidUsageMinimum = 0x19 - hidUsageMaximum = 0x29 - hidPhysicalMinimum = 0x35 - hidPhysicalMaximum = 0x46 + hidLogicalMinimum = 0x14 + hidLogicalMaximum = 0x24 + hidUsageMinimum = 0x18 + hidUsageMaximum = 0x28 + hidPhysicalMinimum = 0x34 + hidPhysicalMaximum = 0x44 hidUnitExponent = 0x55 hidUnit = 0x65 hidCollection = 0xa1 @@ -18,6 +18,13 @@ const ( hidReportID = 0x85 ) +const ( + hidSizeValue0 = 0x00 + hidSizeValue1 = 0x01 + hidSizeValue2 = 0x02 + hidSizeValue4 = 0x03 +) + var ( HIDUsagePageGenericDesktop = []byte{hidUsagePage, 0x01} HIDUsagePageSimulationControls = []byte{hidUsagePage, 0x02} @@ -129,51 +136,69 @@ func HIDReportID(id int) []byte { } func HIDLogicalMinimum(min int) []byte { - if min > 255 { - return []byte{hidLogicalMinimum + 1, uint8(min), uint8(min >> 8)} + switch { + case min < -32767 || 65535 < min: + return []byte{hidLogicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)} + case min < -127 || 255 < min: + return []byte{hidLogicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)} + default: + return []byte{hidLogicalMinimum + hidSizeValue1, byte(min)} } - - return []byte{hidLogicalMinimum, byte(min)} } func HIDLogicalMaximum(max int) []byte { - if max > 255 { - return []byte{hidLogicalMaximum + 1, uint8(max), uint8(max >> 8)} + switch { + case max < -32767 || 65535 < max: + return []byte{hidLogicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)} + case max < -127 || 255 < max: + return []byte{hidLogicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)} + default: + return []byte{hidLogicalMaximum + hidSizeValue1, byte(max)} } - - return []byte{hidLogicalMaximum, byte(max)} } func HIDUsageMinimum(min int) []byte { - if min > 255 { - return []byte{hidUsageMinimum + 1, uint8(min), uint8(min >> 8)} + switch { + case min < -32767 || 65535 < min: + return []byte{hidUsageMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)} + case min < -127 || 255 < min: + return []byte{hidUsageMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)} + default: + return []byte{hidUsageMinimum + hidSizeValue1, byte(min)} } - - return []byte{hidUsageMinimum, byte(min)} } func HIDUsageMaximum(max int) []byte { - if max > 255 { - return []byte{hidUsageMaximum + 1, uint8(max), uint8(max >> 8)} + switch { + case max < -32767 || 65535 < max: + return []byte{hidUsageMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)} + case max < -127 || 255 < max: + return []byte{hidUsageMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)} + default: + return []byte{hidUsageMaximum + hidSizeValue1, byte(max)} } - - return []byte{hidUsageMaximum, byte(max)} } func HIDPhysicalMinimum(min int) []byte { - if min > 255 { - return []byte{hidPhysicalMinimum + 1, uint8(min), uint8(min >> 8)} + switch { + case min < -32767 || 65535 < min: + return []byte{hidPhysicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)} + case min < -127 || 255 < min: + return []byte{hidPhysicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)} + default: + return []byte{hidPhysicalMinimum + hidSizeValue1, byte(min)} } - - return []byte{hidPhysicalMinimum, byte(min)} } func HIDPhysicalMaximum(max int) []byte { - if max > 255 { - return []byte{hidPhysicalMaximum + 1, uint8(max), uint8(max >> 8)} + switch { + case max < -32767 || 65535 < max: + return []byte{hidPhysicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)} + case max < -127 || 255 < max: + return []byte{hidPhysicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)} + default: + return []byte{hidPhysicalMaximum + hidSizeValue1, byte(max)} } - - return []byte{hidPhysicalMaximum, byte(max)} } func HIDUnitExponent(exp int) []byte { diff --git a/src/machine/usb/descriptor/joystick.go b/src/machine/usb/descriptor/joystick.go index ebf2f7c3..03bde259 100644 --- a/src/machine/usb/descriptor/joystick.go +++ b/src/machine/usb/descriptor/joystick.go @@ -80,10 +80,7 @@ var JoystickDefaultHIDReport = Append([][]byte{ HIDLogicalMaximum(1), HIDReportSize(1), HIDReportCount(16), - HIDInputDataVarAbs, - HIDReportCount(1), - HIDReportSize(3), - HIDUnitExponent(-16), + HIDUnitExponent(0), HIDUnit(0), HIDInputDataVarAbs,