-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.go
182 lines (148 loc) · 4.67 KB
/
usb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package wallera
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/f-secure-foundry/tamago/soc/imx6/usb"
wallerausb "github.com/wallera-computer/wallera/usb"
)
const (
hidRequestSetIdle = 10
hidRequestTypeGetDescriptor = 0x21
descriptorTypeGetReport = 0x22
)
type HIDHandler interface {
Tx(buf []byte, lastErr error) (res []byte, err error)
Rx(buf []byte, lastErr error) (res []byte, err error)
}
// hidDescriptor represents a HID standard descriptor.
// Device Class Definition for Human Interface Devices (HID) Version 1.11, pg 22.
type hidDescriptor struct {
Length uint8
Type uint8
bcdHID uint16
CountryCode uint8
NumDescriptors uint8
ReportDescriptorType uint8
DescriptorLength uint16
}
// setDefaults sets some standard properties for hidDescriptor.
func (d *hidDescriptor) setDefaults() {
d.Length = 0x09
d.Type = 0x21
d.bcdHID = 0x101
}
// bytes converts the descriptor structure to byte array format.
func (d *hidDescriptor) bytes() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, d)
return buf.Bytes()
}
// configureDevice configures device to use hidSetup Setup function, and adds an HID InterfaceDescriptor to conf
// along with the needed Endpoints.
func configureDevice(device *usb.Device, conf *usb.ConfigurationDescriptor, handler HIDHandler) error {
device.Setup = hidSetup(device)
id, err := addInterface(device, conf)
if err != nil {
return fmt.Errorf("cannot add U2F USB Interface, %w", err)
}
endpoints := addEndpoints(id)
endpoints.in.Function = handler.Tx
endpoints.out.Function = handler.Rx
addHIDClassDescriptor(id)
// device qualifier
device.Qualifier = &usb.DeviceQualifierDescriptor{}
device.Qualifier.SetDefaults()
device.Qualifier.NumConfigurations = uint8(len(device.Configurations))
return nil
}
// addInterface adds a Interface Descriptor with 2 endpoints, with HID interface class.
func addInterface(device *usb.Device, conf *usb.ConfigurationDescriptor) (*usb.InterfaceDescriptor, error) {
id := &usb.InterfaceDescriptor{}
id.SetDefaults()
id.NumEndpoints = 2
id.InterfaceClass = 0x03
id.InterfaceSubClass = 0x0
id.InterfaceProtocol = 0x0
var err error
id.Interface, err = device.AddString("fidati interface descriptor")
if err != nil {
return nil, err
}
conf.AddInterface(id)
return id, nil
}
// endpoints is a convenience struct, holds input and output endpoints.
type endpoints struct {
in *usb.EndpointDescriptor
out *usb.EndpointDescriptor
}
// addEndpoints adds an input and output endpoint to conf, returns a endpoints instance to let
// the caller determine their behavior.
func addEndpoints(conf *usb.InterfaceDescriptor) endpoints {
var e endpoints
e.in = &usb.EndpointDescriptor{}
e.in.SetDefaults()
e.in.Attributes = 0x03
e.in.EndpointAddress = 0x81
e.in.MaxPacketSize = 512
e.in.Interval = 1
e.out = &usb.EndpointDescriptor{}
e.out.SetDefaults()
e.out.Attributes = 0x03
e.out.EndpointAddress = 0x01
e.out.MaxPacketSize = 512
e.out.Interval = 1
conf.Endpoints = append(conf.Endpoints, e.out, e.in)
return e
}
// addHIDClassDescriptor adds a HID class descriptor to conf.
// The report descriptor length is len(LedgerNanoXReport).
func addHIDClassDescriptor(conf *usb.InterfaceDescriptor) {
hid := hidDescriptor{}
hid.setDefaults()
hid.CountryCode = 0x0
hid.NumDescriptors = 0x01
hid.ReportDescriptorType = 0x22
hid.DescriptorLength = uint16(len(wallerausb.LedgerNanoXReport))
conf.ClassDescriptors = append(conf.ClassDescriptors, hid.bytes())
}
// hidSetup returns a custom setup function for device.
func hidSetup(device *usb.Device) usb.SetupFunction {
return func(setup *usb.SetupData) (in []byte, ack, done bool, err error) {
bDescriptorType := setup.Value & 0xff
if setup.Request == usb.SET_FEATURE {
// stall here
err = errors.New("should stall")
done = true
return
}
if int(setup.RequestType) & ^0x80 == hidRequestTypeGetDescriptor {
if setup.Request == hidRequestSetIdle {
ack = true
done = true
return
}
}
if setup.Request == usb.GET_DESCRIPTOR {
if bDescriptorType == descriptorTypeGetReport {
in = wallerausb.LedgerNanoXReport
done = true
return
}
}
return
}
}
// DefaultConfiguration returns a usb.ConfigurationDescriptor ready to be used for ConfigureUSB.
func DefaultConfiguration() usb.ConfigurationDescriptor {
cd := usb.ConfigurationDescriptor{}
cd.SetDefaults()
cd.Attributes = 160
return cd
}
// ConfigureUSB configures device and config to be used as a HID handler.
func ConfigureUSB(config *usb.ConfigurationDescriptor, device *usb.Device, handler HIDHandler) error {
return configureDevice(device, config, handler)
}