1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 /*
30 * HID spec: https://www.usb.org/sites/default/files/documents/hid1_11.pdf
31 */
32
33 #include "opt_hid.h"
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/sysctl.h>
41
42 #include <dev/evdev/input.h>
43 #include <dev/evdev/evdev.h>
44
45 #include <dev/hid/hid.h>
46 #include <dev/hid/hidbus.h>
47 #include <dev/hid/hidmap.h>
48 #include <dev/hid/hidquirk.h>
49 #include <dev/hid/hidrdesc.h>
50
51 static const uint8_t hms_boot_desc[] = { HID_MOUSE_BOOTPROTO_DESCR() };
52
53 enum {
54 HMS_REL_X,
55 HMS_REL_Y,
56 HMS_REL_Z,
57 HMS_ABS_X,
58 HMS_ABS_Y,
59 HMS_ABS_Z,
60 HMS_HWHEEL,
61 HMS_BTN,
62 HMS_FINAL_CB,
63 };
64
65 static hidmap_cb_t hms_final_cb;
66 #ifdef IICHID_SAMPLING
67 static hid_intr_t hms_intr;
68 #endif
69
70 #define HMS_MAP_BUT_RG(usage_from, usage_to, code) \
71 { HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) }
72 #define HMS_MAP_BUT_MS(usage, code) \
73 { HIDMAP_KEY(HUP_MICROSOFT, usage, code) }
74 #define HMS_MAP_ABS(usage, code) \
75 { HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) }
76 #define HMS_MAP_REL(usage, code) \
77 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) }
78 #define HMS_MAP_REL_REV(usage, code) \
79 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true }
80 #define HMS_MAP_REL_CN(usage, code) \
81 { HIDMAP_REL(HUP_CONSUMER, usage, code) }
82 #define HMS_FINAL_CB(cb) \
83 { HIDMAP_FINAL_CB(&cb) }
84
85 static const struct hidmap_item hms_map[] = {
86 [HMS_REL_X] = HMS_MAP_REL(HUG_X, REL_X),
87 [HMS_REL_Y] = HMS_MAP_REL(HUG_Y, REL_Y),
88 [HMS_REL_Z] = HMS_MAP_REL(HUG_Z, REL_Z),
89 [HMS_ABS_X] = HMS_MAP_ABS(HUG_X, ABS_X),
90 [HMS_ABS_Y] = HMS_MAP_ABS(HUG_Y, ABS_Y),
91 [HMS_ABS_Z] = HMS_MAP_ABS(HUG_Z, ABS_Z),
92 [HMS_HWHEEL] = HMS_MAP_REL_CN(HUC_AC_PAN, REL_HWHEEL),
93 [HMS_BTN] = HMS_MAP_BUT_RG(1, 16, BTN_MOUSE),
94 [HMS_FINAL_CB] = HMS_FINAL_CB(hms_final_cb),
95 };
96
97 static const struct hidmap_item hms_map_wheel[] = {
98 HMS_MAP_REL(HUG_WHEEL, REL_WHEEL),
99 };
100 static const struct hidmap_item hms_map_wheel_rev[] = {
101 HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL),
102 };
103
104 static const struct hidmap_item hms_map_kensington_slimblade[] = {
105 HMS_MAP_BUT_MS(1, BTN_RIGHT),
106 HMS_MAP_BUT_MS(2, BTN_MIDDLE),
107 };
108
109 /* A match on these entries will load hms */
110 static const struct hid_device_id hms_devs[] = {
111 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_POINTER) },
112 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) },
113 };
114
115 struct hms_softc {
116 struct hidmap hm;
117 HIDMAP_CAPS(caps, hms_map);
118 #ifdef IICHID_SAMPLING
119 bool iichid_sampling;
120 void *last_ir;
121 hid_size_t last_irsize;
122 hid_size_t isize;
123 uint32_t drift_cnt;
124 uint32_t drift_thresh;
125 struct hid_location wheel_loc;
126 #endif
127 };
128
129 #ifdef IICHID_SAMPLING
130 static void
hms_intr(void * context,void * buf,hid_size_t len)131 hms_intr(void *context, void *buf, hid_size_t len)
132 {
133 struct hidmap *hm = context;
134 struct hms_softc *sc = device_get_softc(hm->dev);
135 int32_t wheel;
136
137 if (len > sc->isize)
138 len = sc->isize;
139
140 /*
141 * Many I2C "compatibility" mouse devices found on touchpads continue
142 * to return last report data in sampling mode even after touch has
143 * been ended. That results in cursor drift. Filter out such a
144 * reports through comparing with previous one.
145 *
146 * Except this results in dropping consecutive mouse wheel events,
147 * because differently from cursor movement they always move by the
148 * same amount. So, don't do it when there's mouse wheel movement.
149 */
150 if (sc->wheel_loc.size != 0)
151 wheel = hid_get_data(buf, len, &sc->wheel_loc);
152 else
153 wheel = 0;
154
155 if (len == sc->last_irsize && memcmp(buf, sc->last_ir, len) == 0 &&
156 wheel == 0) {
157 sc->drift_cnt++;
158 if (sc->drift_thresh != 0 && sc->drift_cnt >= sc->drift_thresh)
159 return;
160 } else {
161 sc->drift_cnt = 0;
162 sc->last_irsize = len;
163 bcopy(buf, sc->last_ir, len);
164 }
165
166 hidmap_intr(context, buf, len);
167 }
168 #endif
169
170 static int
hms_final_cb(HIDMAP_CB_ARGS)171 hms_final_cb(HIDMAP_CB_ARGS)
172 {
173 struct hms_softc *sc = HIDMAP_CB_GET_SOFTC();
174 struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
175
176 if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
177 if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
178 hidmap_test_cap(sc->caps, HMS_ABS_Y))
179 evdev_support_prop(evdev, INPUT_PROP_DIRECT);
180 else
181 evdev_support_prop(evdev, INPUT_PROP_POINTER);
182 #ifdef IICHID_SAMPLING
183 /* Overload interrupt handler to skip identical reports */
184 if (sc->iichid_sampling)
185 hidbus_set_intr(sc->hm.dev, hms_intr, &sc->hm);
186 #endif
187 }
188
189 /* Do not execute callback at interrupt handler and detach */
190 return (ENOSYS);
191 }
192
193 static void
hms_identify(driver_t * driver,device_t parent)194 hms_identify(driver_t *driver, device_t parent)
195 {
196 const struct hid_device_info *hw = hid_get_device_info(parent);
197 void *d_ptr;
198 hid_size_t d_len;
199 int error;
200
201 /*
202 * If device claimed boot protocol support but do not have report
203 * descriptor, load one defined in "Appendix B.2" of HID1_11.pdf
204 */
205 error = hid_get_report_descr(parent, &d_ptr, &d_len);
206 if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) ||
207 (error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) &&
208 hid_is_mouse(d_ptr, d_len)))
209 (void)hid_set_report_descr(parent, hms_boot_desc,
210 sizeof(hms_boot_desc));
211 }
212
213 static int
hms_probe(device_t dev)214 hms_probe(device_t dev)
215 {
216 struct hms_softc *sc = device_get_softc(dev);
217 int error;
218
219 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs);
220 if (error != 0)
221 return (error);
222
223 hidmap_set_dev(&sc->hm, dev);
224
225 /* Check if report descriptor belongs to mouse */
226 error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps);
227 if (error != 0)
228 return (error);
229
230 /* There should be at least one X or Y axis */
231 if (!hidmap_test_cap(sc->caps, HMS_REL_X) &&
232 !hidmap_test_cap(sc->caps, HMS_REL_Y) &&
233 !hidmap_test_cap(sc->caps, HMS_ABS_X) &&
234 !hidmap_test_cap(sc->caps, HMS_ABS_Y))
235 return (ENXIO);
236
237 if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
238 hidmap_test_cap(sc->caps, HMS_ABS_Y))
239 hidbus_set_desc(dev, "Tablet");
240 else
241 hidbus_set_desc(dev, "Mouse");
242
243 return (BUS_PROBE_GENERIC);
244 }
245
246 static int
hms_attach(device_t dev)247 hms_attach(device_t dev)
248 {
249 struct hms_softc *sc = device_get_softc(dev);
250 const struct hid_device_info *hw = hid_get_device_info(dev);
251 struct hidmap_hid_item *hi;
252 HIDMAP_CAPS(cap_wheel, hms_map_wheel);
253 void *d_ptr;
254 hid_size_t d_len;
255 bool set_report_proto;
256 int error, nbuttons = 0;
257
258 /*
259 * Set the report (non-boot) protocol if report descriptor has not been
260 * overloaded with boot protocol report descriptor.
261 *
262 * Mice without boot protocol support may choose not to implement
263 * Set_Protocol at all; Ignore any error.
264 */
265 error = hid_get_report_descr(dev, &d_ptr, &d_len);
266 set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) &&
267 memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0);
268 (void)hid_set_protocol(dev, set_report_proto ? 1 : 0);
269
270 if (hid_test_quirk(hw, HQ_MS_REVZ))
271 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel);
272 else
273 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel);
274
275 if (hid_test_quirk(hw, HQ_MS_VENDOR_BTN))
276 HIDMAP_ADD_MAP(&sc->hm, hms_map_kensington_slimblade, NULL);
277
278 #ifdef IICHID_SAMPLING
279 if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) &&
280 hidmap_test_cap(sc->caps, HMS_REL_X) &&
281 hidmap_test_cap(sc->caps, HMS_REL_Y)) {
282 sc->iichid_sampling = true;
283 sc->isize = hid_report_size_max(d_ptr, d_len, hid_input, NULL);
284 sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO);
285 sc->drift_thresh = 2;
286 SYSCTL_ADD_U32(device_get_sysctl_ctx(dev),
287 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
288 "drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0,
289 "drift detection threshold");
290 }
291 #endif
292
293 error = hidmap_attach(&sc->hm);
294 if (error)
295 return (error);
296
297 /* Count number of input usages of variable type mapped to buttons */
298 for (hi = sc->hm.hid_items;
299 hi < sc->hm.hid_items + sc->hm.nhid_items;
300 hi++) {
301 if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
302 nbuttons++;
303 #ifdef IICHID_SAMPLING
304 /*
305 * Make note of which part of the report descriptor is the wheel.
306 */
307 if (hi->type == HIDMAP_TYPE_VARIABLE &&
308 hi->evtype == EV_REL && hi->code == REL_WHEEL) {
309 sc->wheel_loc = hi->loc;
310 /*
311 * Account for the leading Report ID byte
312 * if it is a multi-report device.
313 */
314 if (hi->id != 0)
315 sc->wheel_loc.pos += 8;
316 }
317 #endif
318 }
319
320 /* announce information about the mouse */
321 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
322 nbuttons,
323 (hidmap_test_cap(sc->caps, HMS_REL_X) ||
324 hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
325 (hidmap_test_cap(sc->caps, HMS_REL_Y) ||
326 hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
327 (hidmap_test_cap(sc->caps, HMS_REL_Z) ||
328 hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
329 hidmap_test_cap(cap_wheel, 0) ? "W" : "",
330 hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
331 sc->hm.hid_items[0].id);
332
333 return (0);
334 }
335
336 static int
hms_detach(device_t dev)337 hms_detach(device_t dev)
338 {
339 struct hms_softc *sc = device_get_softc(dev);
340 int error;
341
342 error = hidmap_detach(&sc->hm);
343 #ifdef IICHID_SAMPLING
344 if (error == 0)
345 free(sc->last_ir, M_DEVBUF);
346 #endif
347 return (error);
348 }
349
350 static device_method_t hms_methods[] = {
351 DEVMETHOD(device_identify, hms_identify),
352 DEVMETHOD(device_probe, hms_probe),
353 DEVMETHOD(device_attach, hms_attach),
354 DEVMETHOD(device_detach, hms_detach),
355
356 DEVMETHOD_END
357 };
358
359 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
360 DRIVER_MODULE(hms, hidbus, hms_driver, NULL, NULL);
361 MODULE_DEPEND(hms, hid, 1, 1, 1);
362 MODULE_DEPEND(hms, hidbus, 1, 1, 1);
363 MODULE_DEPEND(hms, hidmap, 1, 1, 1);
364 MODULE_DEPEND(hms, evdev, 1, 1, 1);
365 MODULE_VERSION(hms, 1);
366 HID_PNP_INFO(hms_devs);
367