Skip to content

Commit 0071cd5

Browse files
sago35deadprogram
authored andcommitted
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
1 parent 60f2a67 commit 0071cd5

File tree

2 files changed

+56
-34
lines changed

2 files changed

+56
-34
lines changed

src/machine/usb/descriptor/hidreport.go

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package descriptor
33
const (
44
hidUsagePage = 0x05
55
hidUsage = 0x09
6-
hidLogicalMinimum = 0x15
7-
hidLogicalMaximum = 0x25
8-
hidUsageMinimum = 0x19
9-
hidUsageMaximum = 0x29
10-
hidPhysicalMinimum = 0x35
11-
hidPhysicalMaximum = 0x46
6+
hidLogicalMinimum = 0x14
7+
hidLogicalMaximum = 0x24
8+
hidUsageMinimum = 0x18
9+
hidUsageMaximum = 0x28
10+
hidPhysicalMinimum = 0x34
11+
hidPhysicalMaximum = 0x44
1212
hidUnitExponent = 0x55
1313
hidUnit = 0x65
1414
hidCollection = 0xa1
@@ -18,6 +18,13 @@ const (
1818
hidReportID = 0x85
1919
)
2020

21+
const (
22+
hidSizeValue0 = 0x00
23+
hidSizeValue1 = 0x01
24+
hidSizeValue2 = 0x02
25+
hidSizeValue4 = 0x03
26+
)
27+
2128
var (
2229
HIDUsagePageGenericDesktop = []byte{hidUsagePage, 0x01}
2330
HIDUsagePageSimulationControls = []byte{hidUsagePage, 0x02}
@@ -129,51 +136,69 @@ func HIDReportID(id int) []byte {
129136
}
130137

131138
func HIDLogicalMinimum(min int) []byte {
132-
if min > 255 {
133-
return []byte{hidLogicalMinimum + 1, uint8(min), uint8(min >> 8)}
139+
switch {
140+
case min < -32767 || 65535 < min:
141+
return []byte{hidLogicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
142+
case min < -127 || 255 < min:
143+
return []byte{hidLogicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
144+
default:
145+
return []byte{hidLogicalMinimum + hidSizeValue1, byte(min)}
134146
}
135-
136-
return []byte{hidLogicalMinimum, byte(min)}
137147
}
138148

139149
func HIDLogicalMaximum(max int) []byte {
140-
if max > 255 {
141-
return []byte{hidLogicalMaximum + 1, uint8(max), uint8(max >> 8)}
150+
switch {
151+
case max < -32767 || 65535 < max:
152+
return []byte{hidLogicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
153+
case max < -127 || 255 < max:
154+
return []byte{hidLogicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
155+
default:
156+
return []byte{hidLogicalMaximum + hidSizeValue1, byte(max)}
142157
}
143-
144-
return []byte{hidLogicalMaximum, byte(max)}
145158
}
146159

147160
func HIDUsageMinimum(min int) []byte {
148-
if min > 255 {
149-
return []byte{hidUsageMinimum + 1, uint8(min), uint8(min >> 8)}
161+
switch {
162+
case min < -32767 || 65535 < min:
163+
return []byte{hidUsageMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
164+
case min < -127 || 255 < min:
165+
return []byte{hidUsageMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
166+
default:
167+
return []byte{hidUsageMinimum + hidSizeValue1, byte(min)}
150168
}
151-
152-
return []byte{hidUsageMinimum, byte(min)}
153169
}
154170

155171
func HIDUsageMaximum(max int) []byte {
156-
if max > 255 {
157-
return []byte{hidUsageMaximum + 1, uint8(max), uint8(max >> 8)}
172+
switch {
173+
case max < -32767 || 65535 < max:
174+
return []byte{hidUsageMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
175+
case max < -127 || 255 < max:
176+
return []byte{hidUsageMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
177+
default:
178+
return []byte{hidUsageMaximum + hidSizeValue1, byte(max)}
158179
}
159-
160-
return []byte{hidUsageMaximum, byte(max)}
161180
}
162181

163182
func HIDPhysicalMinimum(min int) []byte {
164-
if min > 255 {
165-
return []byte{hidPhysicalMinimum + 1, uint8(min), uint8(min >> 8)}
183+
switch {
184+
case min < -32767 || 65535 < min:
185+
return []byte{hidPhysicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
186+
case min < -127 || 255 < min:
187+
return []byte{hidPhysicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
188+
default:
189+
return []byte{hidPhysicalMinimum + hidSizeValue1, byte(min)}
166190
}
167-
168-
return []byte{hidPhysicalMinimum, byte(min)}
169191
}
170192

171193
func HIDPhysicalMaximum(max int) []byte {
172-
if max > 255 {
173-
return []byte{hidPhysicalMaximum + 1, uint8(max), uint8(max >> 8)}
194+
switch {
195+
case max < -32767 || 65535 < max:
196+
return []byte{hidPhysicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
197+
case max < -127 || 255 < max:
198+
return []byte{hidPhysicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
199+
default:
200+
return []byte{hidPhysicalMaximum + hidSizeValue1, byte(max)}
174201
}
175-
176-
return []byte{hidPhysicalMaximum, byte(max)}
177202
}
178203

179204
func HIDUnitExponent(exp int) []byte {

src/machine/usb/descriptor/joystick.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ var JoystickDefaultHIDReport = Append([][]byte{
8080
HIDLogicalMaximum(1),
8181
HIDReportSize(1),
8282
HIDReportCount(16),
83-
HIDInputDataVarAbs,
84-
HIDReportCount(1),
85-
HIDReportSize(3),
86-
HIDUnitExponent(-16),
83+
HIDUnitExponent(0),
8784
HIDUnit(0),
8885
HIDInputDataVarAbs,
8986

0 commit comments

Comments
 (0)