Skip to content

Commit 2ad9752

Browse files
committed
machine/usb: refactoring descriptors into subpackage for modularity
Signed-off-by: deadprogram <[email protected]>
1 parent 8b9bee4 commit 2ad9752

File tree

15 files changed

+1190
-266
lines changed

15 files changed

+1190
-266
lines changed

src/machine/usb.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
package machine
44

55
import (
6-
"bytes"
7-
"encoding/binary"
8-
"errors"
96
"machine/usb"
7+
"machine/usb/descriptor"
8+
9+
"errors"
1010
)
1111

1212
type USBDevice struct {
@@ -29,7 +29,7 @@ type Serialer interface {
2929
RTS() bool
3030
}
3131

32-
var usbDescriptor = usb.DescriptorCDC
32+
var usbDescriptor = descriptor.CDC
3333

3434
var usbDescriptorConfig uint8 = usb.DescriptorConfigCDC
3535

@@ -72,7 +72,7 @@ func usbProduct() string {
7272
// binary.
7373
func strToUTF16LEDescriptor(in string, out []byte) {
7474
out[0] = byte(len(out))
75-
out[1] = usb.STRING_DESCRIPTOR_TYPE
75+
out[1] = descriptor.TypeString
7676
for i, rune := range in {
7777
out[(i<<1)+2] = byte(rune)
7878
out[(i<<1)+3] = 0
@@ -88,7 +88,7 @@ var (
8888
)
8989

9090
var (
91-
usbEndpointDescriptors [usb.NumberOfEndpoints]usb.DeviceDescriptor
91+
usbEndpointDescriptors [usb.NumberOfEndpoints]descriptor.Device
9292

9393
isEndpointHalt = false
9494
isRemoteWakeUpEnabled = false
@@ -134,27 +134,27 @@ var (
134134
// can be requested by the host.
135135
func sendDescriptor(setup usb.Setup) {
136136
switch setup.WValueH {
137-
case usb.CONFIGURATION_DESCRIPTOR_TYPE:
137+
case descriptor.TypeConfiguration:
138138
sendUSBPacket(0, usbDescriptor.Configuration, setup.WLength)
139139
return
140-
case usb.DEVICE_DESCRIPTOR_TYPE:
140+
case descriptor.TypeDevice:
141141
// composite descriptor
142142
switch {
143143
case (usbDescriptorConfig & usb.DescriptorConfigHID) > 0:
144-
usbDescriptor = usb.DescriptorCDCHID
144+
usbDescriptor = descriptor.CDCHID
145145
case (usbDescriptorConfig & usb.DescriptorConfigMIDI) > 0:
146-
usbDescriptor = usb.DescriptorCDCMIDI
146+
usbDescriptor = descriptor.CDCMIDI
147147
case (usbDescriptorConfig & usb.DescriptorConfigJoystick) > 0:
148-
usbDescriptor = usb.DescriptorCDCJoystick
148+
usbDescriptor = descriptor.CDCJoystick
149149
default:
150-
usbDescriptor = usb.DescriptorCDC
150+
usbDescriptor = descriptor.CDC
151151
}
152152

153153
usbDescriptor.Configure(usbVendorID(), usbProductID())
154154
sendUSBPacket(0, usbDescriptor.Device, setup.WLength)
155155
return
156156

157-
case usb.STRING_DESCRIPTOR_TYPE:
157+
case descriptor.TypeString:
158158
switch setup.WValueL {
159159
case 0:
160160
usb_trans_buffer[0] = 0x04
@@ -178,12 +178,12 @@ func sendDescriptor(setup usb.Setup) {
178178
SendZlp()
179179
}
180180
return
181-
case usb.HID_REPORT_TYPE:
181+
case descriptor.TypeHIDReport:
182182
if h, ok := usbDescriptor.HID[setup.WIndex]; ok {
183183
sendUSBPacket(0, h, setup.WLength)
184184
return
185185
}
186-
case usb.DEVICE_QUALIFIER:
186+
case descriptor.TypeDeviceQualifier:
187187
// skip
188188
default:
189189
}
@@ -302,11 +302,10 @@ func EnableMIDI(txHandler func(), rxHandler func([]byte), setupHandler func(usb.
302302

303303
// EnableJoystick enables HID. This function must be executed from the init().
304304
func EnableJoystick(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool, hidDesc []byte) {
305-
idx := bytes.Index(usb.DescriptorCDCJoystick.Configuration, []byte{
306-
0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22,
307-
})
308-
binary.LittleEndian.PutUint16(usb.DescriptorCDCJoystick.Configuration[idx+7:idx+9], uint16(len(hidDesc)))
309-
usb.DescriptorCDCJoystick.HID[2] = hidDesc
305+
class := descriptor.FindClassHIDType(descriptor.CDCJoystick.Configuration, descriptor.ClassHIDJoystick.Bytes())
306+
class.ClassLength(uint16(len(hidDesc)))
307+
descriptor.CDCJoystick.HID[2] = hidDesc
308+
310309
usbDescriptorConfig |= usb.DescriptorConfigJoystick
311310
endPoints[usb.HID_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointOut)
312311
usbRxHandler[usb.HID_ENDPOINT_OUT] = rxHandler

src/machine/usb/descriptor.go

Lines changed: 0 additions & 179 deletions
This file was deleted.

0 commit comments

Comments
 (0)