Skip to content

Commit 069e4f0

Browse files
sago35deadprogram
authored andcommitted
machine/usb: allow USB Endpoint settings to be changed externally
1 parent d1eb642 commit 069e4f0

File tree

5 files changed

+130
-46
lines changed

5 files changed

+130
-46
lines changed

src/machine/usb.go

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -263,47 +263,52 @@ func EnableCDC(txHandler func(), rxHandler func([]byte), setupHandler func(usb.S
263263
usbDescriptor = descriptor.CDC
264264
}
265265
// Initialization of endpoints is required even for non-CDC
266-
endPoints[usb.CDC_ENDPOINT_ACM] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn)
267-
endPoints[usb.CDC_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut)
268-
endPoints[usb.CDC_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn)
269-
usbRxHandler[usb.CDC_ENDPOINT_OUT] = rxHandler
270-
usbTxHandler[usb.CDC_ENDPOINT_IN] = txHandler
271-
usbSetupHandler[usb.CDC_ACM_INTERFACE] = setupHandler // 0x02 (Communications and CDC Control)
272-
usbSetupHandler[usb.CDC_DATA_INTERFACE] = nil // 0x0A (CDC-Data)
266+
ConfigureUSBEndpoint(usbDescriptor,
267+
[]usb.EndpointConfig{
268+
{
269+
No: usb.CDC_ENDPOINT_ACM,
270+
IsIn: true,
271+
Type: usb.ENDPOINT_TYPE_INTERRUPT,
272+
},
273+
{
274+
No: usb.CDC_ENDPOINT_OUT,
275+
IsIn: false,
276+
Type: usb.ENDPOINT_TYPE_BULK,
277+
RxHandler: rxHandler,
278+
},
279+
{
280+
No: usb.CDC_ENDPOINT_IN,
281+
IsIn: true,
282+
Type: usb.ENDPOINT_TYPE_BULK,
283+
TxHandler: txHandler,
284+
},
285+
},
286+
[]usb.SetupConfig{
287+
{
288+
No: usb.CDC_ACM_INTERFACE,
289+
Handler: setupHandler,
290+
},
291+
})
273292
}
274293

275-
// EnableHID enables HID. This function must be executed from the init().
276-
func EnableHID(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool) {
277-
usbDescriptor = descriptor.CDCHID
278-
endPoints[usb.HID_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn)
279-
usbTxHandler[usb.HID_ENDPOINT_IN] = txHandler
280-
usbSetupHandler[usb.HID_INTERFACE] = setupHandler // 0x03 (HID - Human Interface Device)
281-
}
282-
283-
// EnableMIDI enables MIDI. This function must be executed from the init().
284-
func EnableMIDI(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool) {
285-
usbDescriptor = descriptor.CDCMIDI
286-
endPoints[usb.MIDI_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut)
287-
endPoints[usb.MIDI_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn)
288-
usbRxHandler[usb.MIDI_ENDPOINT_OUT] = rxHandler
289-
usbTxHandler[usb.MIDI_ENDPOINT_IN] = txHandler
290-
}
294+
func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointConfig, setup []usb.SetupConfig) {
295+
usbDescriptor = desc
291296

292-
// EnableJoystick enables HID. This function must be executed from the init().
293-
func EnableJoystick(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool, hidDesc []byte) {
294-
class, err := descriptor.FindClassHIDType(descriptor.CDCJoystick.Configuration, descriptor.ClassHIDJoystick.Bytes())
295-
if err != nil {
296-
// TODO: some way to notify about error
297-
return
297+
for _, ep := range epSettings {
298+
if ep.IsIn {
299+
endPoints[ep.No] = uint32(ep.Type | usb.EndpointIn)
300+
if ep.TxHandler != nil {
301+
usbTxHandler[ep.No] = ep.TxHandler
302+
}
303+
} else {
304+
endPoints[ep.No] = uint32(ep.Type | usb.EndpointOut)
305+
if ep.RxHandler != nil {
306+
usbRxHandler[ep.No] = ep.RxHandler
307+
}
308+
}
298309
}
299310

300-
class.ClassLength(uint16(len(hidDesc)))
301-
descriptor.CDCJoystick.HID[2] = hidDesc
302-
303-
usbDescriptor = descriptor.CDCJoystick
304-
endPoints[usb.HID_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointOut)
305-
usbRxHandler[usb.HID_ENDPOINT_OUT] = rxHandler
306-
endPoints[usb.HID_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn)
307-
usbTxHandler[usb.HID_ENDPOINT_IN] = txHandler
308-
usbSetupHandler[usb.HID_INTERFACE] = setupHandler // 0x03 (HID - Human Interface Device)
311+
for _, s := range setup {
312+
usbSetupHandler[s.No] = s.Handler
313+
}
309314
}

src/machine/usb/adc/midi/midi.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package midi
33
import (
44
"machine"
55
"machine/usb"
6+
"machine/usb/descriptor"
67
)
78

89
const (
@@ -40,7 +41,23 @@ func newMidi() *midi {
4041
m := &midi{
4142
buf: NewRingBuffer(),
4243
}
43-
machine.EnableMIDI(m.Handler, m.RxHandler, nil)
44+
machine.ConfigureUSBEndpoint(descriptor.CDCMIDI,
45+
[]usb.EndpointConfig{
46+
{
47+
No: usb.MIDI_ENDPOINT_OUT,
48+
IsIn: false,
49+
Type: usb.ENDPOINT_TYPE_BULK,
50+
RxHandler: m.RxHandler,
51+
},
52+
{
53+
No: usb.MIDI_ENDPOINT_IN,
54+
IsIn: true,
55+
Type: usb.ENDPOINT_TYPE_BULK,
56+
TxHandler: m.Handler,
57+
},
58+
},
59+
[]usb.SetupConfig{},
60+
)
4461
return m
4562
}
4663

src/machine/usb/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package usb
2+
3+
type EndpointConfig struct {
4+
No uint8
5+
IsIn bool
6+
TxHandler func()
7+
RxHandler func([]byte)
8+
Type uint8
9+
}
10+
11+
type SetupConfig struct {
12+
No uint8
13+
Handler func(Setup) bool
14+
}

src/machine/usb/hid/hid.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"machine"
66
"machine/usb"
7+
"machine/usb/descriptor"
78
)
89

910
// from usb-hid.go
@@ -32,7 +33,21 @@ var size int
3233
// calls machine.EnableHID for USB configuration
3334
func SetHandler(d hidDevicer) {
3435
if size == 0 {
35-
machine.EnableHID(handler, nil, setupHandler)
36+
machine.ConfigureUSBEndpoint(descriptor.CDCHID,
37+
[]usb.EndpointConfig{
38+
{
39+
No: usb.HID_ENDPOINT_IN,
40+
IsIn: true,
41+
Type: usb.ENDPOINT_TYPE_INTERRUPT,
42+
TxHandler: handler,
43+
},
44+
},
45+
[]usb.SetupConfig{
46+
{
47+
No: usb.HID_INTERFACE,
48+
Handler: setupHandler,
49+
},
50+
})
3651
}
3752

3853
devices[size] = d

src/machine/usb/hid/joystick/joystick.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package joystick
33
import (
44
"machine"
55
"machine/usb"
6+
"machine/usb/descriptor"
67
"machine/usb/hid"
78
)
89

@@ -32,18 +33,50 @@ func UseSettings(def Definitions, rxHandlerFunc func(b []byte), setupFunc func(s
3233
if setupFunc == nil {
3334
setupFunc = hid.DefaultSetupHandler
3435
}
35-
machine.EnableJoystick(js.handler, rxHandlerFunc, setupFunc, hidDesc)
36+
if rxHandlerFunc == nil {
37+
rxHandlerFunc = js.rxHandlerFunc
38+
}
39+
if len(hidDesc) == 0 {
40+
hidDesc = descriptor.JoystickDefaultHIDReport
41+
}
42+
class, err := descriptor.FindClassHIDType(descriptor.CDCJoystick.Configuration, descriptor.ClassHIDJoystick.Bytes())
43+
if err != nil {
44+
// TODO: some way to notify about error
45+
return nil
46+
}
47+
48+
class.ClassLength(uint16(len(hidDesc)))
49+
descriptor.CDCJoystick.HID[2] = hidDesc
50+
51+
machine.ConfigureUSBEndpoint(descriptor.CDCJoystick,
52+
[]usb.EndpointConfig{
53+
{
54+
No: usb.HID_ENDPOINT_OUT,
55+
IsIn: false,
56+
Type: usb.ENDPOINT_TYPE_INTERRUPT,
57+
RxHandler: rxHandlerFunc,
58+
},
59+
{
60+
No: usb.HID_ENDPOINT_IN,
61+
IsIn: true,
62+
Type: usb.ENDPOINT_TYPE_INTERRUPT,
63+
TxHandler: js.handler,
64+
},
65+
},
66+
[]usb.SetupConfig{
67+
{
68+
No: usb.HID_INTERFACE,
69+
Handler: setupFunc,
70+
},
71+
},
72+
)
3673
Joystick = js
3774
return js
3875
}
3976

4077
func newDefaultJoystick() *joystick {
4178
def := DefaultDefinitions()
42-
js := &joystick{
43-
State: def.NewState(),
44-
buf: hid.NewRingBuffer(),
45-
}
46-
machine.EnableJoystick(js.handler, js.rxHandler, hid.DefaultSetupHandler, def.Descriptor())
79+
js := UseSettings(def, nil, nil, nil)
4780
return js
4881
}
4982

0 commit comments

Comments
 (0)