-
Notifications
You must be signed in to change notification settings - Fork 6
/
hms.c
346 lines (304 loc) · 10.4 KB
/
hms.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2020 Vladimir Kondratyev <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* HID spec: https://www.usb.org/sites/default/files/documents/hid1_11.pdf
*/
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <dev/evdev/input.h>
#include <dev/evdev/evdev.h>
#include "hid.h"
#include "hidbus.h"
#include "hidquirk.h"
#include "hidmap.h"
static const uint8_t hms_boot_desc[] = {
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application)
0x09, 0x01, // Usage (Pointer)
0xA1, 0x00, // Collection (Physical)
0x95, 0x03, // Report Count (3)
0x75, 0x01, // Report Size (1)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (0x01)
0x29, 0x03, // Usage Maximum (0x03)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x81, 0x02, // Input (Data,Var,Abs)
0x95, 0x01, // Report Count (1)
0x75, 0x05, // Report Size (5)
0x81, 0x03, // Input (Const)
0x75, 0x08, // Report Size (8)
0x95, 0x02, // Report Count (2)
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x81, 0x06, // Input (Data,Var,Rel)
0xC0, // End Collection
0xC0, // End Collection
};
enum {
HMS_REL_X,
HMS_REL_Y,
HMS_REL_Z,
HMS_ABS_X,
HMS_ABS_Y,
HMS_ABS_Z,
HMS_HWHEEL,
HMS_BTN,
HMS_BTN_MS1,
HMS_BTN_MS2,
HMS_FINAL_CB,
};
static hidmap_cb_t hms_final_cb;
static hid_intr_t hms_intr;
#define HMS_MAP_BUT_RG(usage_from, usage_to, code) \
{ HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) }
#define HMS_MAP_BUT_MS(usage, code) \
{ HIDMAP_KEY(HUP_MICROSOFT, usage, code) }
#define HMS_MAP_ABS(usage, code) \
{ HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) }
#define HMS_MAP_REL(usage, code) \
{ HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) }
#define HMS_MAP_REL_REV(usage, code) \
{ HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true }
#define HMS_MAP_REL_CN(usage, code) \
{ HIDMAP_REL(HUP_CONSUMER, usage, code) }
#define HMS_FINAL_CB(cb) \
{ HIDMAP_FINAL_CB(&cb) }
static const struct hidmap_item hms_map[] = {
[HMS_REL_X] = HMS_MAP_REL(HUG_X, REL_X),
[HMS_REL_Y] = HMS_MAP_REL(HUG_Y, REL_Y),
[HMS_REL_Z] = HMS_MAP_REL(HUG_Z, REL_Z),
[HMS_ABS_X] = HMS_MAP_ABS(HUG_X, ABS_X),
[HMS_ABS_Y] = HMS_MAP_ABS(HUG_Y, ABS_Y),
[HMS_ABS_Z] = HMS_MAP_ABS(HUG_Z, ABS_Z),
[HMS_HWHEEL] = HMS_MAP_REL_CN(HUC_AC_PAN, REL_HWHEEL),
[HMS_BTN] = HMS_MAP_BUT_RG(1, 16, BTN_MOUSE),
[HMS_BTN_MS1] = HMS_MAP_BUT_MS(1, BTN_RIGHT),
[HMS_BTN_MS2] = HMS_MAP_BUT_MS(2, BTN_MIDDLE),
[HMS_FINAL_CB] = HMS_FINAL_CB(hms_final_cb),
};
static const struct hidmap_item hms_map_wheel[] = {
HMS_MAP_REL(HUG_WHEEL, REL_WHEEL),
};
static const struct hidmap_item hms_map_wheel_rev[] = {
HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL),
};
/* A match on these entries will load hms */
static const struct hid_device_id hms_devs[] = {
{ HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) },
};
struct hms_softc {
struct hidmap hm;
HIDMAP_CAPS(caps, hms_map);
bool iichid_sampling;
void *last_ir;
hid_size_t last_irsize;
hid_size_t isize;
uint32_t drift_cnt;
uint32_t drift_thresh;
};
static void
hms_intr(void *context, void *buf, hid_size_t len)
{
struct hidmap *hm = context;
struct hms_softc *sc = device_get_softc(hm->dev);
if (len > sc->isize)
len = sc->isize;
/*
* Many I2C "compatibility" mouse devices found on touchpads continue
* to return last report data in sampling mode even after touch has
* been ended. That results in cursor drift. Filter out such a
* reports through comparing with previous one.
*/
if (len == sc->last_irsize && memcmp(buf, sc->last_ir, len) == 0) {
sc->drift_cnt++;
if (sc->drift_thresh != 0 && sc->drift_cnt >= sc->drift_thresh)
return;
} else {
sc->drift_cnt = 0;
sc->last_irsize = len;
bcopy(buf, sc->last_ir, len);
}
hidmap_intr(context, buf, len);
}
static int
hms_final_cb(HIDMAP_CB_ARGS)
{
struct hms_softc *sc = HIDMAP_CB_GET_SOFTC();
struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
hidmap_test_cap(sc->caps, HMS_ABS_Y))
evdev_support_prop(evdev, INPUT_PROP_DIRECT);
else
evdev_support_prop(evdev, INPUT_PROP_POINTER);
/* Overload interrupt handler to skip identical reports */
if (sc->iichid_sampling)
hidbus_set_intr(sc->hm.dev, hms_intr, &sc->hm);
}
/* Do not execute callback at interrupt handler and detach */
return (ENOSYS);
}
static void
hms_identify(driver_t *driver, device_t parent)
{
const struct hid_device_info *hw = hid_get_device_info(parent);
void *d_ptr;
hid_size_t d_len;
int error;
/*
* If device claimed boot protocol support but do not have report
* descriptor, load one defined in "Appendix B.2" of HID1_11.pdf
*/
error = hid_get_report_descr(parent, &d_ptr, &d_len);
if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) ||
(error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) &&
hid_is_mouse(d_ptr, d_len)))
(void)hid_set_report_descr(parent, hms_boot_desc,
sizeof(hms_boot_desc));
}
static int
hms_probe(device_t dev)
{
struct hms_softc *sc = device_get_softc(dev);
int error;
error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs);
if (error != 0)
return (error);
hidmap_set_dev(&sc->hm, dev);
/* Check if report descriptor belongs to mouse */
error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps);
if (error != 0)
return (error);
/* There should be at least one X or Y axis */
if (!hidmap_test_cap(sc->caps, HMS_REL_X) &&
!hidmap_test_cap(sc->caps, HMS_REL_X) &&
!hidmap_test_cap(sc->caps, HMS_ABS_X) &&
!hidmap_test_cap(sc->caps, HMS_ABS_Y))
return (ENXIO);
if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
hidmap_test_cap(sc->caps, HMS_ABS_Y))
hidbus_set_desc(dev, "Tablet");
else
hidbus_set_desc(dev, "Mouse");
return (BUS_PROBE_DEFAULT);
}
static int
hms_attach(device_t dev)
{
struct hms_softc *sc = device_get_softc(dev);
const struct hid_device_info *hw = hid_get_device_info(dev);
struct hidmap_hid_item *hi;
HIDMAP_CAPS(cap_wheel, hms_map_wheel);
void *d_ptr;
hid_size_t d_len;
bool set_report_proto;
int error, nbuttons = 0;
/*
* Set the report (non-boot) protocol if report descriptor has not been
* overloaded with boot protocol report descriptor.
*
* Mice without boot protocol support may choose not to implement
* Set_Protocol at all; Ignore any error.
*/
error = hid_get_report_descr(dev, &d_ptr, &d_len);
set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) &&
memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0);
(void)hid_set_protocol(dev, set_report_proto ? 1 : 0);
if (hid_test_quirk(hw, HQ_MS_REVZ))
HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel);
else
HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel);
if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) &&
hidmap_test_cap(sc->caps, HMS_REL_X) &&
hidmap_test_cap(sc->caps, HMS_REL_Y)) {
sc->iichid_sampling = true;
sc->isize = hid_report_size(d_ptr, d_len, hid_input, NULL);
sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO);
sc->drift_thresh = 2;
SYSCTL_ADD_U32(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
"drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0,
"drift detection threshhold");
}
error = hidmap_attach(&sc->hm);
if (error)
return (error);
/* Count number of input usages of variable type mapped to buttons */
for (hi = sc->hm.hid_items;
hi < sc->hm.hid_items + sc->hm.nhid_items;
hi++)
if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
nbuttons++;
/* announce information about the mouse */
device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
nbuttons,
(hidmap_test_cap(sc->caps, HMS_REL_X) ||
hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
(hidmap_test_cap(sc->caps, HMS_REL_Y) ||
hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
(hidmap_test_cap(sc->caps, HMS_REL_Z) ||
hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
hidmap_test_cap(cap_wheel, 0) ? "W" : "",
hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
sc->hm.hid_items[0].id);
return (0);
}
static int
hms_detach(device_t dev)
{
struct hms_softc *sc = device_get_softc(dev);
int error;
error = hidmap_detach(&sc->hm);
if (error == 0)
free(sc->last_ir, M_DEVBUF);
return (error);
}
static devclass_t hms_devclass;
static device_method_t hms_methods[] = {
DEVMETHOD(device_identify, hms_identify),
DEVMETHOD(device_probe, hms_probe),
DEVMETHOD(device_attach, hms_attach),
DEVMETHOD(device_detach, hms_detach),
DEVMETHOD_END
};
DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
DRIVER_MODULE(hms, hidbus, hms_driver, hms_devclass, NULL, 0);
MODULE_DEPEND(hms, hid, 1, 1, 1);
MODULE_DEPEND(hms, hidmap, 1, 1, 1);
MODULE_DEPEND(hms, evdev, 1, 1, 1);
MODULE_VERSION(hms, 1);
HID_PNP_INFO(hms_devs);