-
Notifications
You must be signed in to change notification settings - Fork 10
/
ioctl.go
274 lines (231 loc) · 7.31 KB
/
ioctl.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package evdev
import (
"errors"
"fmt"
"strings"
"syscall"
"unsafe"
)
const (
ioctlDirNone = 0x0
ioctlDirWrite = 0x1
ioctlDirRead = 0x2
)
func trimNull(s string) string {
return strings.Trim(s, "\x00")
}
func ioctlMakeCode(dir, typ, nr int, size uintptr) uint32 {
var code uint32
if dir > ioctlDirWrite|ioctlDirRead {
panic(fmt.Errorf("invalid ioctl dir value: %d", dir))
}
if size > 1<<14 {
panic(fmt.Errorf("invalid ioctl size value: %d", size))
}
code |= uint32(dir) << 30
code |= uint32(size) << 16
code |= uint32(typ) << 8
code |= uint32(nr)
return code
}
func doIoctl(fd uintptr, code uint32, ptr unsafe.Pointer) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(code), uintptr(ptr))
if errno != 0 {
return errors.New(errno.Error())
}
return nil
}
func ioctlEVIOCGVERSION(fd uintptr) (int32, error) {
version := int32(0)
code := ioctlMakeCode(ioctlDirRead, 'E', 0x01, unsafe.Sizeof(version))
err := doIoctl(fd, code, unsafe.Pointer(&version))
return version, err
}
func ioctlEVIOCGID(fd uintptr) (InputID, error) {
id := InputID{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x02, unsafe.Sizeof(id))
err := doIoctl(fd, code, unsafe.Pointer(&id))
return id, err
}
func ioctlEVIOCGREP(fd uintptr) ([2]uint32, error) {
rep := [2]uint32{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x03, unsafe.Sizeof(rep))
err := doIoctl(fd, code, unsafe.Pointer(&rep))
return rep, err
}
func ioctlEVIOCSREP(fd uintptr, rep [2]uint32) error {
code := ioctlMakeCode(ioctlDirWrite, 'E', 0x03, unsafe.Sizeof(rep))
return doIoctl(fd, code, unsafe.Pointer(&rep))
}
func ioctlEVIOCGKEYCODE(fd uintptr) (InputKeymapEntry, error) {
entry := InputKeymapEntry{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x04, unsafe.Sizeof(entry))
err := doIoctl(fd, code, unsafe.Pointer(&entry))
return entry, err
}
func ioctlEVIOCSKEYCODE(fd uintptr, entry InputKeymapEntry) error {
code := ioctlMakeCode(ioctlDirWrite, 'E', 0x04, unsafe.Sizeof(entry))
return doIoctl(fd, code, unsafe.Pointer(&entry))
}
func ioctlEVIOCGNAME(fd uintptr) (string, error) {
str := [256]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x06, unsafe.Sizeof(str))
err := doIoctl(fd, code, unsafe.Pointer(&str))
return trimNull(string(str[:])), err
}
func ioctlEVIOCGPHYS(fd uintptr) (string, error) {
str := [256]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x07, unsafe.Sizeof(str))
err := doIoctl(fd, code, unsafe.Pointer(&str))
return trimNull(string(str[:])), err
}
func ioctlEVIOCGUNIQ(fd uintptr) (string, error) {
str := [256]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x08, unsafe.Sizeof(str))
err := doIoctl(fd, code, unsafe.Pointer(&str))
return trimNull(string(str[:])), err
}
func ioctlEVIOCGPROP(fd uintptr) ([]byte, error) {
bits := [256]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x09, unsafe.Sizeof(bits))
err := doIoctl(fd, code, unsafe.Pointer(&bits))
return bits[:], err
}
func ioctlEVIOCGKEY(fd uintptr) ([]byte, error) {
bits := [KEY_MAX]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x18, unsafe.Sizeof(bits))
err := doIoctl(fd, code, unsafe.Pointer(&bits))
return bits[:], err
}
func ioctlEVIOCGLED(fd uintptr) ([]byte, error) {
bits := [LED_MAX]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x19, unsafe.Sizeof(bits))
err := doIoctl(fd, code, unsafe.Pointer(&bits))
return bits[:], err
}
func ioctlEVIOCGSND(fd uintptr) ([]byte, error) {
bits := [SND_MAX]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x1a, unsafe.Sizeof(bits))
err := doIoctl(fd, code, unsafe.Pointer(&bits))
return bits[:], err
}
func ioctlEVIOCGSW(fd uintptr) ([]byte, error) {
bits := [SW_MAX]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x1b, unsafe.Sizeof(bits))
err := doIoctl(fd, code, unsafe.Pointer(&bits))
return bits[:], err
}
func ioctlEVIOCGBIT(fd uintptr, evtype int) ([]byte, error) {
var cnt int
switch evtype {
case 0:
// special case, indicating the list of all feature types supported should be returned,
// rather than the list of particular features for that type
cnt = EV_CNT
case EV_KEY:
cnt = KEY_CNT
case EV_REL:
cnt = REL_CNT
case EV_ABS:
cnt = ABS_CNT
case EV_MSC:
cnt = MSC_CNT
case EV_SW:
cnt = SW_CNT
case EV_LED:
cnt = LED_CNT
case EV_SND:
cnt = SND_CNT
case EV_REP:
cnt = REP_CNT
case EV_FF:
cnt = FF_CNT
default: // EV_PWR, EV_FF_STATUS ??
cnt = KEY_MAX
}
bytesNumber := (cnt + 7) / 8
bits := [KEY_MAX]byte{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x20+evtype, unsafe.Sizeof(bits))
err := doIoctl(fd, code, unsafe.Pointer(&bits))
return bits[:bytesNumber], err
}
func ioctlEVIOCGABS(fd uintptr, abs int) (AbsInfo, error) {
info := AbsInfo{}
code := ioctlMakeCode(ioctlDirRead, 'E', 0x40+abs, unsafe.Sizeof(info))
err := doIoctl(fd, code, unsafe.Pointer(&info))
return info, err
}
func ioctlEVIOCSABS(fd uintptr, abs int, info AbsInfo) error {
code := ioctlMakeCode(ioctlDirWrite, 'E', 0xc0+abs, unsafe.Sizeof(info))
return doIoctl(fd, code, unsafe.Pointer(&info))
}
func ioctlEVIOCGRAB(fd uintptr, p int32) error {
code := ioctlMakeCode(ioctlDirWrite, 'E', 0x90, unsafe.Sizeof(p))
if p != 0 {
return doIoctl(fd, code, unsafe.Pointer(&p))
}
return doIoctl(fd, code, nil)
}
func ioctlEVIOCREVOKE(fd uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'E', 0x91, unsafe.Sizeof(p))
return doIoctl(fd, code, nil)
}
func ioctlUISETEVBIT(fd uintptr, ev uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 100, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(ev))
}
func ioctlUISETKEYBIT(fd uintptr, key uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 101, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(key))
}
func ioctlUISETRELBIT(fd uintptr, rel uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 102, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(rel))
}
func ioctlUISETABSBIT(fd uintptr, abs uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 103, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(abs))
}
func ioctlUISETMSCBIT(fd uintptr, msc uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 104, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(msc))
}
func ioctlUISETLEDBIT(fd uintptr, led uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 105, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(led))
}
func ioctlUISETSNDBIT(fd uintptr, snd uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 106, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(snd))
}
func ioctlUISETFFBIT(fd uintptr, fe uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 107, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(fe))
}
func ioctlUISETSWBIT(fd uintptr, sw uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 109, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(sw))
}
func ioctlUISETPROPBIT(fd uintptr, prop uintptr) error {
var p int32
code := ioctlMakeCode(ioctlDirWrite, 'U', 110, unsafe.Sizeof(p))
return doIoctl(fd, code, unsafe.Pointer(prop))
}
func ioctlUIDEVCREATE(fd uintptr) error {
code := ioctlMakeCode(ioctlDirNone, 'U', 1, 0)
return doIoctl(fd, code, nil)
}
func ioctlUIDEVDESTROY(fd uintptr) error {
code := ioctlMakeCode(ioctlDirNone, 'U', 2, 0)
return doIoctl(fd, code, nil)
}