Skip to content

Commit 0ef86e1

Browse files
Unruddeadprogram
authored andcommitted
Add support for HID Keyboard LEDs
1 parent 72270f9 commit 0ef86e1

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/machine/usb/descriptor/hid.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var interfaceHID = [interfaceTypeLen]byte{
2626
TypeInterface,
2727
0x02, // InterfaceNumber
2828
0x00, // AlternateSetting
29-
0x01, // NumEndpoints
29+
0x02, // NumEndpoints
3030
0x03, // InterfaceClass
3131
0x00, // InterfaceSubClass
3232
0x00, // InterfaceProtocol
@@ -103,7 +103,7 @@ var classHID = [ClassHIDTypeLen]byte{
103103
0x00, // CountryCode
104104
0x01, // NumDescriptors
105105
0x22, // ClassType
106-
0x7E, // ClassLength L
106+
0x90, // ClassLength L
107107
0x00, // ClassLength H
108108
}
109109

@@ -128,6 +128,7 @@ var CDCHID = Descriptor{
128128
InterfaceHID.Bytes(),
129129
ClassHID.Bytes(),
130130
EndpointEP4IN.Bytes(),
131+
EndpointEP5OUT.Bytes(),
131132
}),
132133
HID: map[uint16][]byte{
133134
2: Append([][]byte{
@@ -147,6 +148,15 @@ var CDCHID = Descriptor{
147148
HIDReportCount(1),
148149
HIDReportSize(8),
149150
HIDInputConstVarAbs,
151+
HIDReportCount(3),
152+
HIDReportSize(1),
153+
HIDUsagePageLED,
154+
HIDUsageMinimum(1),
155+
HIDUsageMaximum(3),
156+
HIDOutputDataVarAbs,
157+
HIDReportCount(5),
158+
HIDReportSize(1),
159+
HIDOutputConstVarAbs,
150160
HIDReportCount(6),
151161
HIDReportSize(8),
152162
HIDLogicalMinimum(0),

src/machine/usb/descriptor/hidreport.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
hidUnit = 0x65
1414
hidCollection = 0xa1
1515
hidInput = 0x81
16+
hidOutput = 0x91
1617
hidReportSize = 0x75
1718
hidReportCount = 0x95
1819
hidReportID = 0x85
@@ -121,6 +122,12 @@ var (
121122

122123
// Input (Data, Variable, Relative), 2 position bytes (X & Y)
123124
HIDInputDataVarRel = []byte{hidInput, 0x06}
125+
126+
// Output (Data, Variable, Absolute), Modifier byte
127+
HIDOutputDataVarAbs = []byte{hidOutput, 0x02}
128+
129+
// Output (Const, Variable, Absolute), Modifier byte
130+
HIDOutputConstVarAbs = []byte{hidOutput, 0x03}
124131
)
125132

126133
func HIDReportSize(size int) []byte {

src/machine/usb/hid/hid.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func SetHandler(d hidDevicer) {
3636
if size == 0 {
3737
machine.ConfigureUSBEndpoint(descriptor.CDCHID,
3838
[]usb.EndpointConfig{
39+
{
40+
Index: usb.HID_ENDPOINT_OUT,
41+
IsIn: false,
42+
Type: usb.ENDPOINT_TYPE_INTERRUPT,
43+
RxHandler: rxHandler,
44+
},
3945
{
4046
Index: usb.HID_ENDPOINT_IN,
4147
IsIn: true,

src/machine/usb/hid/keyboard/keyboard.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func (kb *keyboard) TxHandler() bool {
9292
}
9393

9494
func (kb *keyboard) RxHandler(b []byte) bool {
95+
if len(b) >= 2 && b[0] == 2 /* ReportID */ {
96+
kb.led = b[1]
97+
}
9598
return false
9699
}
97100

@@ -106,6 +109,18 @@ func (kb *keyboard) tx(b []byte) {
106109
}
107110
}
108111

112+
func (kb *keyboard) NumLockLed() bool {
113+
return kb.led&1 != 0
114+
}
115+
116+
func (kb *keyboard) CapsLockLed() bool {
117+
return kb.led&2 != 0
118+
}
119+
120+
func (kb *keyboard) ScrollLockLed() bool {
121+
return kb.led&4 != 0
122+
}
123+
109124
func (kb *keyboard) ready() bool {
110125
return true
111126
}

0 commit comments

Comments
 (0)