forked from boombuler/hid
-
Notifications
You must be signed in to change notification settings - Fork 23
/
hid_linux.go
214 lines (180 loc) · 4.62 KB
/
hid_linux.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package hid
// #include <linux/hidraw.h>
import "C"
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"sync"
"unsafe"
)
var (
ioctlHIDIOCGRDESCSIZE = ioR('H', 0x01, C.sizeof_int)
ioctlHIDIOCGRDESC = ioR('H', 0x02, C.sizeof_struct_hidraw_report_descriptor)
ioctlHIDIOCGRAWINFO = ioR('H', 0x03, C.sizeof_struct_hidraw_devinfo)
)
func ioctlHIDIOCGRAWNAME(size int) uintptr {
return ioR('H', 0x04, uintptr(size))
}
func ioctlHIDIOCGRAWPHYS(size int) uintptr {
return ioR('H', 0x05, uintptr(size))
}
func ioctlHIDIOCSFEATURE(size int) uintptr {
return ioRW('H', 0x06, uintptr(size))
}
func ioctlHIDIOCGFEATURE(size int) uintptr {
return ioRW('H', 0x07, uintptr(size))
}
type linuxDevice struct {
f *os.File
info *DeviceInfo
readSetup sync.Once
readErr error
readCh chan []byte
}
func Devices() ([]*DeviceInfo, error) {
sys, err := os.Open("/sys/class/hidraw")
if err != nil {
return nil, err
}
names, err := sys.Readdirnames(0)
sys.Close()
if err != nil {
return nil, err
}
var res []*DeviceInfo
for _, dir := range names {
path := filepath.Join("/dev", filepath.Base(dir))
info, err := getDeviceInfo(path)
if os.IsPermission(err) {
continue
} else if err != nil {
return nil, err
}
res = append(res, info)
}
return res, nil
}
func getDeviceInfo(path string) (*DeviceInfo, error) {
d := &DeviceInfo{
Path: path,
}
dev, err := os.OpenFile(path, os.O_RDWR, 0)
if err != nil {
return nil, err
}
defer dev.Close()
fd := uintptr(dev.Fd())
var descSize C.int
if err := ioctl(fd, ioctlHIDIOCGRDESCSIZE, uintptr(unsafe.Pointer(&descSize))); err != nil {
return nil, err
}
rawDescriptor := C.struct_hidraw_report_descriptor{
size: C.__u32(descSize),
}
if err := ioctl(fd, ioctlHIDIOCGRDESC, uintptr(unsafe.Pointer(&rawDescriptor))); err != nil {
return nil, err
}
d.parseReport(C.GoBytes(unsafe.Pointer(&rawDescriptor.value), descSize))
var rawInfo C.struct_hidraw_devinfo
if err := ioctl(fd, ioctlHIDIOCGRAWINFO, uintptr(unsafe.Pointer(&rawInfo))); err != nil {
return nil, err
}
d.VendorID = uint16(rawInfo.vendor)
d.ProductID = uint16(rawInfo.product)
rawName := make([]byte, 256)
if err := ioctl(fd, ioctlHIDIOCGRAWNAME(len(rawName)), uintptr(unsafe.Pointer(&rawName[0]))); err != nil {
return nil, err
}
d.Product = string(rawName[:bytes.IndexByte(rawName, 0)])
if p, err := filepath.EvalSymlinks(filepath.Join("/sys/class/hidraw", filepath.Base(path), "device")); err == nil {
if rawManufacturer, err := ioutil.ReadFile(filepath.Join(p, "/../../manufacturer")); err == nil {
d.Manufacturer = string(bytes.TrimRight(rawManufacturer, "\n"))
}
}
return d, nil
}
// very basic report parser that will pull out the usage page, usage, and the
// sizes of the first input and output reports
func (d *DeviceInfo) parseReport(b []byte) {
var reportSize uint16
for len(b) > 0 {
// read item size, type, and tag
size := int(b[0] & 0x03)
if size == 3 {
size = 4
}
typ := (b[0] >> 2) & 0x03
tag := (b[0] >> 4) & 0x0f
b = b[1:]
if len(b) < size {
return
}
// read item value
var v uint64
for i := 0; i < size; i++ {
v += uint64(b[i]) << (8 * uint(i))
}
b = b[size:]
switch {
case typ == 0 && tag == 8 && d.InputReportLength == 0 && reportSize > 0: // input report type
d.InputReportLength = reportSize
reportSize = 0
case typ == 0 && tag == 9 && d.OutputReportLength == 0 && reportSize > 0: // output report type
d.OutputReportLength = reportSize
reportSize = 0
case typ == 1 && tag == 0: // usage page
d.UsagePage = uint16(v)
case typ == 1 && tag == 9: // report count
reportSize = uint16(v)
case typ == 2 && tag == 0 && d.Usage == 0: // usage
d.Usage = uint16(v)
}
if d.UsagePage > 0 && d.Usage > 0 && d.InputReportLength > 0 && d.OutputReportLength > 0 {
return
}
}
}
func ByPath(path string) (*DeviceInfo, error) {
return getDeviceInfo(path)
}
func (d *DeviceInfo) Open() (Device, error) {
f, err := os.OpenFile(d.Path, os.O_RDWR, 0)
if err != nil {
return nil, err
}
return &linuxDevice{f: f, info: d}, nil
}
func (d *linuxDevice) Close() {
d.f.Close()
}
func (d *linuxDevice) Write(data []byte) error {
_, err := d.f.Write(data)
return err
}
func (d *linuxDevice) ReadCh() <-chan []byte {
d.readSetup.Do(func() {
d.readCh = make(chan []byte, 30)
go d.readThread()
})
return d.readCh
}
func (d *linuxDevice) ReadError() error {
return d.readErr
}
func (d *linuxDevice) readThread() {
defer close(d.readCh)
for {
buf := make([]byte, d.info.InputReportLength)
n, err := d.f.Read(buf)
if err != nil {
d.readErr = err
return
}
select {
case d.readCh <- buf[:n]:
default:
}
}
}