Skip to content

Commit 1d5c5ca

Browse files
committed
machine/usb/descriptor: further refactor HID report creation
Signed-off-by: deadprogram <[email protected]>
1 parent 9093570 commit 1d5c5ca

File tree

3 files changed

+174
-113
lines changed

3 files changed

+174
-113
lines changed

src/machine/usb/descriptor/hid.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ var CDCHID = Descriptor{
156156
HIDUsageMinimum(0),
157157
HIDUsageMaximum(255),
158158
HIDInputDataAryAbs,
159-
HIDEndCollection,
159+
HIDCollectionEnd,
160160

161161
HIDUsagePageGenericDesktop,
162162
HIDUsageDesktopMouse,
@@ -186,22 +186,20 @@ var CDCHID = Descriptor{
186186
HIDReportSize(8),
187187
HIDReportCount(3),
188188
HIDInputDataVarRel,
189-
HIDEndCollection,
190-
HIDEndCollection,
189+
HIDCollectionEnd,
190+
HIDCollectionEnd,
191191

192192
HIDUsagePageConsumer,
193193
HIDUsageConsumerControl,
194194
HIDCollectionApplication,
195195
HIDReportID(3),
196196
HIDLogicalMinimum(0),
197-
[]byte{
198-
0x26, 0xFF, 0x1F, // Logical Maximum (8191)
199-
0x19, 0x00, // Usage Minimum (Unassigned)
200-
0x2A, 0xFF, 0x1F, // Usage Maximum (0x1FFF)
201-
},
197+
HIDLogicalMaximum(8191),
198+
HIDUsageMinimum(0),
199+
HIDUsageMaximum(0x1FFF),
202200
HIDReportSize(16),
203201
HIDReportCount(1),
204202
HIDInputDataAryAbs,
205-
HIDEndCollection}),
203+
HIDCollectionEnd}),
206204
},
207205
}
Lines changed: 155 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,201 @@
11
package descriptor
22

3-
var (
4-
HIDUsagePageGenericDesktop = []byte{0x05, 0x01}
5-
HIDUsagePageSimulationControls = []byte{0x05, 0x02}
6-
HIDUsagePageVRControls = []byte{0x05, 0x03}
7-
HIDUsagePageSportControls = []byte{0x05, 0x04}
8-
HIDUsagePageGameControls = []byte{0x05, 0x05}
9-
HIDUsagePageGenericControls = []byte{0x05, 0x06}
10-
HIDUsagePageKeyboard = []byte{0x05, 0x07}
11-
HIDUsagePageLED = []byte{0x05, 0x08}
12-
HIDUsagePageButton = []byte{0x05, 0x09}
13-
HIDUsagePageOrdinal = []byte{0x05, 0x0A}
14-
HIDUsagePageTelephony = []byte{0x05, 0x0B}
15-
HIDUsagePageConsumer = []byte{0x05, 0x0C}
16-
HIDUsagePageDigitizers = []byte{0x05, 0x0D}
17-
HIDUsagePageHaptics = []byte{0x05, 0x0E}
18-
HIDUsagePagePhysicalInput = []byte{0x05, 0x0F}
19-
HIDUsagePageUnicode = []byte{0x05, 0x10}
20-
HIDUsagePageSoC = []byte{0x05, 0x11}
21-
HIDUsagePageEyeHeadTrackers = []byte{0x05, 0x12}
22-
HIDUsagePageAuxDisplay = []byte{0x05, 0x14}
23-
HIDUsagePageSensors = []byte{0x05, 0x20}
24-
HIDUsagePageMedicalInstrument = []byte{0x05, 0x40}
25-
HIDUsagePageBrailleDisplay = []byte{0x05, 0x41}
26-
HIDUsagePageLighting = []byte{0x05, 0x59}
27-
HIDUsagePageMonitor = []byte{0x05, 0x80}
28-
HIDUsagePageMonitorEnum = []byte{0x05, 0x81}
29-
HIDUsagePageVESA = []byte{0x05, 0x82}
30-
HIDUsagePagePower = []byte{0x05, 0x84}
31-
HIDUsagePageBatterySystem = []byte{0x05, 0x85}
32-
HIDUsagePageBarcodeScanner = []byte{0x05, 0x8C}
33-
HIDUsagePageScales = []byte{0x05, 0x8D}
34-
HIDUsagePageMagneticStripe = []byte{0x05, 0x8E}
35-
HIDUsagePageCameraControl = []byte{0x05, 0x90}
36-
HIDUsagePageArcade = []byte{0x05, 0x91}
37-
HIDUsagePageGaming = []byte{0x05, 0x92}
3+
import (
4+
"encoding/binary"
5+
)
6+
7+
const (
8+
hidUsagePage = 0x05
9+
hidUsage = 0x09
10+
hidLogicalMinimum = 0x15
11+
hidLogicalMaximum = 0x25
12+
hidUsageMinimum = 0x19
13+
hidUsageMaximum = 0x29
14+
hidPhysicalMinimum = 0x35
15+
hidPhysicalMaximum = 0x46
16+
hidUnitExponent = 0x55
17+
hidUnit = 0x65
18+
hidCollection = 0xa1
19+
hidInput = 0x81
20+
hidReportSize = 0x75
21+
hidReportCount = 0x95
22+
hidReportID = 0x85
3823
)
3924

4025
var (
41-
HIDUsageDesktopPointer = []byte{0x09, 0x01}
42-
HIDUsageDesktopMouse = []byte{0x09, 0x02}
43-
HIDUsageDesktopJoystick = []byte{0x09, 0x04}
44-
HIDUsageDesktopGamepad = []byte{0x09, 0x05}
45-
HIDUsageDesktopKeyboard = []byte{0x09, 0x06}
46-
HIDUsageDesktopKeypad = []byte{0x09, 0x07}
47-
HIDUsageDesktopMultiaxis = []byte{0x09, 0x08}
48-
HIDUsageDesktopTablet = []byte{0x09, 0x09}
49-
HIDUsageDesktopWaterCooling = []byte{0x09, 0x0A}
50-
HIDUsageDesktopChassis = []byte{0x09, 0x0B}
51-
HIDUsageDesktopWireless = []byte{0x09, 0x0C}
52-
HIDUsageDesktopPortable = []byte{0x09, 0x0D}
53-
HIDUsageDesktopSystemMultiaxis = []byte{0x09, 0x0E}
54-
HIDUsageDesktopSpatial = []byte{0x09, 0x0F}
55-
HIDUsageDesktopAssistive = []byte{0x09, 0x10}
56-
HIDUsageDesktopDock = []byte{0x09, 0x11}
57-
HIDUsageDesktopDockable = []byte{0x09, 0x12}
58-
HIDUsageDesktopCallState = []byte{0x09, 0x13}
59-
HIDUsageDesktopX = []byte{0x09, 0x30}
60-
HIDUsageDesktopY = []byte{0x09, 0x31}
61-
HIDUsageDesktopZ = []byte{0x09, 0x32}
62-
HIDUsageDesktopRx = []byte{0x09, 0x33}
63-
HIDUsageDesktopRy = []byte{0x09, 0x34}
64-
HIDUsageDesktopRz = []byte{0x09, 0x35}
65-
HIDUsageDesktopSlider = []byte{0x09, 0x36}
66-
HIDUsageDesktopDial = []byte{0x09, 0x37}
67-
HIDUsageDesktopWheel = []byte{0x09, 0x38}
68-
HIDUsageDesktopHatSwitch = []byte{0x09, 0x39}
69-
HIDUsageDesktopCountedBuffer = []byte{0x09, 0x3A}
26+
HIDUsagePageGenericDesktop = []byte{hidUsagePage, 0x01}
27+
HIDUsagePageSimulationControls = []byte{hidUsagePage, 0x02}
28+
HIDUsagePageVRControls = []byte{hidUsagePage, 0x03}
29+
HIDUsagePageSportControls = []byte{hidUsagePage, 0x04}
30+
HIDUsagePageGameControls = []byte{hidUsagePage, 0x05}
31+
HIDUsagePageGenericControls = []byte{hidUsagePage, 0x06}
32+
HIDUsagePageKeyboard = []byte{hidUsagePage, 0x07}
33+
HIDUsagePageLED = []byte{hidUsagePage, 0x08}
34+
HIDUsagePageButton = []byte{hidUsagePage, 0x09}
35+
HIDUsagePageOrdinal = []byte{hidUsagePage, 0x0A}
36+
HIDUsagePageTelephony = []byte{hidUsagePage, 0x0B}
37+
HIDUsagePageConsumer = []byte{hidUsagePage, 0x0C}
38+
HIDUsagePageDigitizers = []byte{hidUsagePage, 0x0D}
39+
HIDUsagePageHaptics = []byte{hidUsagePage, 0x0E}
40+
HIDUsagePagePhysicalInput = []byte{hidUsagePage, 0x0F}
41+
HIDUsagePageUnicode = []byte{hidUsagePage, 0x10}
42+
HIDUsagePageSoC = []byte{hidUsagePage, 0x11}
43+
HIDUsagePageEyeHeadTrackers = []byte{hidUsagePage, 0x12}
44+
HIDUsagePageAuxDisplay = []byte{hidUsagePage, 0x14}
45+
HIDUsagePageSensors = []byte{hidUsagePage, 0x20}
46+
HIDUsagePageMedicalInstrument = []byte{hidUsagePage, 0x40}
47+
HIDUsagePageBrailleDisplay = []byte{hidUsagePage, 0x41}
48+
HIDUsagePageLighting = []byte{hidUsagePage, 0x59}
49+
HIDUsagePageMonitor = []byte{hidUsagePage, 0x80}
50+
HIDUsagePageMonitorEnum = []byte{hidUsagePage, 0x81}
51+
HIDUsagePageVESA = []byte{hidUsagePage, 0x82}
52+
HIDUsagePagePower = []byte{hidUsagePage, 0x84}
53+
HIDUsagePageBatterySystem = []byte{hidUsagePage, 0x85}
54+
HIDUsagePageBarcodeScanner = []byte{hidUsagePage, 0x8C}
55+
HIDUsagePageScales = []byte{hidUsagePage, 0x8D}
56+
HIDUsagePageMagneticStripe = []byte{hidUsagePage, 0x8E}
57+
HIDUsagePageCameraControl = []byte{hidUsagePage, 0x90}
58+
HIDUsagePageArcade = []byte{hidUsagePage, 0x91}
59+
HIDUsagePageGaming = []byte{hidUsagePage, 0x92}
7060
)
7161

7262
var (
73-
HIDUsageConsumerControl = []byte{0x09, 0x01}
74-
HIDUsageConsumerNumericKeypad = []byte{0x09, 0x02}
75-
HIDUsageConsumerProgrammableButtons = []byte{0x09, 0x03}
76-
HIDUsageConsumerMicrophone = []byte{0x09, 0x04}
77-
HIDUsageConsumerHeadphone = []byte{0x09, 0x05}
78-
HIDUsageConsumerGraphicEqualizer = []byte{0x09, 0x06}
63+
HIDUsageDesktopPointer = []byte{hidUsage, 0x01}
64+
HIDUsageDesktopMouse = []byte{hidUsage, 0x02}
65+
HIDUsageDesktopJoystick = []byte{hidUsage, 0x04}
66+
HIDUsageDesktopGamepad = []byte{hidUsage, 0x05}
67+
HIDUsageDesktopKeyboard = []byte{hidUsage, 0x06}
68+
HIDUsageDesktopKeypad = []byte{hidUsage, 0x07}
69+
HIDUsageDesktopMultiaxis = []byte{hidUsage, 0x08}
70+
HIDUsageDesktopTablet = []byte{hidUsage, 0x09}
71+
HIDUsageDesktopWaterCooling = []byte{hidUsage, 0x0A}
72+
HIDUsageDesktopChassis = []byte{hidUsage, 0x0B}
73+
HIDUsageDesktopWireless = []byte{hidUsage, 0x0C}
74+
HIDUsageDesktopPortable = []byte{hidUsage, 0x0D}
75+
HIDUsageDesktopSystemMultiaxis = []byte{hidUsage, 0x0E}
76+
HIDUsageDesktopSpatial = []byte{hidUsage, 0x0F}
77+
HIDUsageDesktopAssistive = []byte{hidUsage, 0x10}
78+
HIDUsageDesktopDock = []byte{hidUsage, 0x11}
79+
HIDUsageDesktopDockable = []byte{hidUsage, 0x12}
80+
HIDUsageDesktopCallState = []byte{hidUsage, 0x13}
81+
HIDUsageDesktopX = []byte{hidUsage, 0x30}
82+
HIDUsageDesktopY = []byte{hidUsage, 0x31}
83+
HIDUsageDesktopZ = []byte{hidUsage, 0x32}
84+
HIDUsageDesktopRx = []byte{hidUsage, 0x33}
85+
HIDUsageDesktopRy = []byte{hidUsage, 0x34}
86+
HIDUsageDesktopRz = []byte{hidUsage, 0x35}
87+
HIDUsageDesktopSlider = []byte{hidUsage, 0x36}
88+
HIDUsageDesktopDial = []byte{hidUsage, 0x37}
89+
HIDUsageDesktopWheel = []byte{hidUsage, 0x38}
90+
HIDUsageDesktopHatSwitch = []byte{hidUsage, 0x39}
91+
HIDUsageDesktopCountedBuffer = []byte{hidUsage, 0x3A}
7992
)
8093

8194
var (
82-
HIDCollectionPhysical = []byte{0xa1, 0x00}
83-
HIDCollectionApplication = []byte{0xa1, 0x01}
95+
HIDUsageConsumerControl = []byte{hidUsage, 0x01}
96+
HIDUsageConsumerNumericKeypad = []byte{hidUsage, 0x02}
97+
HIDUsageConsumerProgrammableButtons = []byte{hidUsage, 0x03}
98+
HIDUsageConsumerMicrophone = []byte{hidUsage, 0x04}
99+
HIDUsageConsumerHeadphone = []byte{hidUsage, 0x05}
100+
HIDUsageConsumerGraphicEqualizer = []byte{hidUsage, 0x06}
84101
)
85102

86103
var (
87-
HIDEndCollection = []byte{0xc0}
104+
HIDCollectionPhysical = []byte{hidCollection, 0x00}
105+
HIDCollectionApplication = []byte{hidCollection, 0x01}
106+
HIDCollectionEnd = []byte{0xc0}
88107
)
89108

90109
var (
91110
// Input (Data,Ary,Abs), Key arrays (6 bytes)
92-
HIDInputDataAryAbs = []byte{0x81, 0x00}
111+
HIDInputDataAryAbs = []byte{hidInput, 0x00}
93112

94113
// Input (Data, Variable, Absolute), Modifier byte
95-
HIDInputDataVarAbs = []byte{0x81, 0x02}
114+
HIDInputDataVarAbs = []byte{hidInput, 0x02}
96115

97116
// Input (Const,Var,Abs), Modifier byte
98-
HIDInputConstVarAbs = []byte{0x81, 0x03}
117+
HIDInputConstVarAbs = []byte{hidInput, 0x03}
99118

100119
// Input (Data, Variable, Relative), 2 position bytes (X & Y)
101-
HIDInputDataVarRel = []byte{0x81, 0x06}
120+
HIDInputDataVarRel = []byte{hidInput, 0x06}
102121
)
103122

104123
func HIDReportSize(size int) []byte {
105-
return []byte{0x75, byte(size)}
124+
return []byte{hidReportSize, byte(size)}
106125
}
107126

108127
func HIDReportCount(count int) []byte {
109-
return []byte{0x95, byte(count)}
128+
return []byte{hidReportCount, byte(count)}
110129
}
111130

112131
func HIDReportID(id int) []byte {
113-
return []byte{0x85, byte(id)}
132+
return []byte{hidReportID, byte(id)}
114133
}
115134

116135
func HIDLogicalMinimum(min int) []byte {
117-
return []byte{0x15, byte(min)}
136+
if min < -255 {
137+
result := []byte{hidLogicalMinimum, 0x0, 0x0}
138+
binary.LittleEndian.PutUint16(result[1:3], uint16(min))
139+
return result
140+
}
141+
142+
return []byte{hidLogicalMinimum, byte(min)}
118143
}
119144

120145
func HIDLogicalMaximum(max int) []byte {
121-
return []byte{0x25, byte(max)}
146+
if max > 255 {
147+
result := []byte{hidLogicalMaximum, 0x0, 0x0}
148+
binary.LittleEndian.PutUint16(result[1:3], uint16(max))
149+
return result
150+
}
151+
152+
return []byte{hidLogicalMaximum, byte(max)}
122153
}
123154

124155
func HIDUsageMinimum(min int) []byte {
125-
return []byte{0x19, byte(min)}
156+
if min < -255 {
157+
result := []byte{hidUsageMinimum, 0x0, 0x0}
158+
binary.LittleEndian.PutUint16(result[1:3], uint16(min))
159+
return result
160+
}
161+
162+
return []byte{hidUsageMinimum, byte(min)}
126163
}
127164

128165
func HIDUsageMaximum(max int) []byte {
129-
return []byte{0x29, byte(max)}
166+
if max > 255 {
167+
result := []byte{hidUsageMaximum, 0x0, 0x0}
168+
binary.LittleEndian.PutUint16(result[1:3], uint16(max))
169+
return result
170+
}
171+
172+
return []byte{hidUsageMaximum, byte(max)}
173+
}
174+
175+
func HIDPhysicalMinimum(min int) []byte {
176+
if min < -255 {
177+
result := []byte{hidPhysicalMinimum, 0x0, 0x0}
178+
binary.LittleEndian.PutUint16(result[1:3], uint16(min))
179+
return result
180+
}
181+
182+
return []byte{hidPhysicalMinimum, byte(min)}
183+
}
184+
185+
func HIDPhysicalMaximum(max int) []byte {
186+
if max > 255 {
187+
result := []byte{hidPhysicalMaximum, 0x0, 0x0}
188+
binary.LittleEndian.PutUint16(result[1:3], uint16(max))
189+
return result
190+
}
191+
192+
return []byte{hidPhysicalMaximum, byte(max)}
193+
}
194+
195+
func HIDUnitExponent(exp int) []byte {
196+
return []byte{hidUnitExponent, byte(exp)}
197+
}
198+
199+
func HIDUnit(unit int) []byte {
200+
return []byte{hidUnit, byte(unit)}
130201
}

src/machine/usb/descriptor/joystick.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,40 +83,32 @@ var JoystickDefaultHIDReport = Append([][]byte{
8383
HIDInputDataVarAbs,
8484
HIDReportCount(1),
8585
HIDReportSize(3),
86-
[]byte{
87-
0x55, 0x00, // Unit Exponent (-16)
88-
0x65, 0x00, // Unit (0x00)
89-
},
86+
HIDUnitExponent(-16),
87+
HIDUnit(0),
9088
HIDInputDataVarAbs,
9189

9290
HIDUsagePageGenericDesktop,
9391
HIDUsageDesktopHatSwitch,
9492
HIDLogicalMinimum(0),
9593
HIDLogicalMaximum(7),
96-
[]byte{
97-
0x35, 0x00, // PHYSICAL_MINIMUM (0)
98-
0x46, 0x3b, 0x01, // PHYSICAL_MAXIMUM(315)
99-
0x65, 0x14, // UNIT (Eng Rot:Angular Pos)
100-
},
94+
HIDPhysicalMinimum(0),
95+
HIDPhysicalMaximum(315),
96+
HIDUnit(0x14),
10197
HIDReportCount(1),
10298
HIDReportSize(4),
10399
HIDInputDataVarAbs,
104100
HIDUsageDesktopHatSwitch,
105101
HIDLogicalMinimum(0),
106102
HIDLogicalMaximum(7),
107-
[]byte{
108-
0x35, 0x00, // PHYSICAL_MINIMUM (0)
109-
0x46, 0x3b, 0x01, // PHYSICAL_MAXIMUM(315)
110-
0x65, 0x14, // UNIT (Eng Rot:Angular Pos)
111-
},
103+
HIDPhysicalMinimum(0),
104+
HIDPhysicalMaximum(315),
105+
HIDUnit(0x14),
112106
HIDReportCount(1),
113107
HIDReportSize(4),
114108
HIDInputDataVarAbs,
115109
HIDUsageDesktopPointer,
116-
[]byte{
117-
0x16, 0x01, 0x80, // LOGICAL_MINIMUM (-32767)
118-
0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767)
119-
},
110+
HIDLogicalMinimum(-32767),
111+
HIDLogicalMaximum(32767),
120112
HIDReportCount(6),
121113
HIDReportSize(16),
122114
HIDCollectionPhysical,
@@ -127,8 +119,8 @@ var JoystickDefaultHIDReport = Append([][]byte{
127119
HIDUsageDesktopRy,
128120
HIDUsageDesktopRz,
129121
HIDInputDataVarAbs,
130-
HIDEndCollection,
131-
HIDEndCollection,
122+
HIDCollectionEnd,
123+
HIDCollectionEnd,
132124
})
133125

134126
// CDCJoystick requires that you append the JoystickDescriptor

0 commit comments

Comments
 (0)