2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * HID spec: https://www.usb.org/sites/default/files/documents/hid1_11.pdf
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/sysctl.h>
44 #include <dev/evdev/input.h>
45 #include <dev/evdev/evdev.h>
47 #include <dev/hid/hid.h>
48 #include <dev/hid/hidbus.h>
49 #include <dev/hid/hidmap.h>
50 #include <dev/hid/hidquirk.h>
51 #include <dev/hid/hidrdesc.h>
53 static const uint8_t hms_boot_desc[] = { HID_MOUSE_BOOTPROTO_DESCR() };
69 static hidmap_cb_t hms_final_cb;
70 #ifdef IICHID_SAMPLING
71 static hid_intr_t hms_intr;
74 #define HMS_MAP_BUT_RG(usage_from, usage_to, code) \
75 { HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) }
76 #define HMS_MAP_BUT_MS(usage, code) \
77 { HIDMAP_KEY(HUP_MICROSOFT, usage, code) }
78 #define HMS_MAP_ABS(usage, code) \
79 { HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) }
80 #define HMS_MAP_REL(usage, code) \
81 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) }
82 #define HMS_MAP_REL_REV(usage, code) \
83 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true }
84 #define HMS_MAP_REL_CN(usage, code) \
85 { HIDMAP_REL(HUP_CONSUMER, usage, code) }
86 #define HMS_FINAL_CB(cb) \
87 { HIDMAP_FINAL_CB(&cb) }
89 static const struct hidmap_item hms_map[] = {
90 [HMS_REL_X] = HMS_MAP_REL(HUG_X, REL_X),
91 [HMS_REL_Y] = HMS_MAP_REL(HUG_Y, REL_Y),
92 [HMS_REL_Z] = HMS_MAP_REL(HUG_Z, REL_Z),
93 [HMS_ABS_X] = HMS_MAP_ABS(HUG_X, ABS_X),
94 [HMS_ABS_Y] = HMS_MAP_ABS(HUG_Y, ABS_Y),
95 [HMS_ABS_Z] = HMS_MAP_ABS(HUG_Z, ABS_Z),
96 [HMS_HWHEEL] = HMS_MAP_REL_CN(HUC_AC_PAN, REL_HWHEEL),
97 [HMS_BTN] = HMS_MAP_BUT_RG(1, 16, BTN_MOUSE),
98 [HMS_BTN_MS1] = HMS_MAP_BUT_MS(1, BTN_RIGHT),
99 [HMS_BTN_MS2] = HMS_MAP_BUT_MS(2, BTN_MIDDLE),
100 [HMS_FINAL_CB] = HMS_FINAL_CB(hms_final_cb),
103 static const struct hidmap_item hms_map_wheel[] = {
104 HMS_MAP_REL(HUG_WHEEL, REL_WHEEL),
106 static const struct hidmap_item hms_map_wheel_rev[] = {
107 HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL),
110 /* A match on these entries will load hms */
111 static const struct hid_device_id hms_devs[] = {
112 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_POINTER) },
113 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) },
118 HIDMAP_CAPS(caps, hms_map);
119 #ifdef IICHID_SAMPLING
120 bool iichid_sampling;
122 hid_size_t last_irsize;
125 uint32_t drift_thresh;
129 #ifdef IICHID_SAMPLING
131 hms_intr(void *context, void *buf, hid_size_t len)
133 struct hidmap *hm = context;
134 struct hms_softc *sc = device_get_softc(hm->dev);
140 * Many I2C "compatibility" mouse devices found on touchpads continue
141 * to return last report data in sampling mode even after touch has
142 * been ended. That results in cursor drift. Filter out such a
143 * reports through comparing with previous one.
145 if (len == sc->last_irsize && memcmp(buf, sc->last_ir, len) == 0) {
147 if (sc->drift_thresh != 0 && sc->drift_cnt >= sc->drift_thresh)
151 sc->last_irsize = len;
152 bcopy(buf, sc->last_ir, len);
155 hidmap_intr(context, buf, len);
160 hms_final_cb(HIDMAP_CB_ARGS)
162 struct hms_softc *sc = HIDMAP_CB_GET_SOFTC();
163 struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
165 if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
166 if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
167 hidmap_test_cap(sc->caps, HMS_ABS_Y))
168 evdev_support_prop(evdev, INPUT_PROP_DIRECT);
170 evdev_support_prop(evdev, INPUT_PROP_POINTER);
171 #ifdef IICHID_SAMPLING
172 /* Overload interrupt handler to skip identical reports */
173 if (sc->iichid_sampling)
174 hidbus_set_intr(sc->hm.dev, hms_intr, &sc->hm);
178 /* Do not execute callback at interrupt handler and detach */
183 hms_identify(driver_t *driver, device_t parent)
185 const struct hid_device_info *hw = hid_get_device_info(parent);
191 * If device claimed boot protocol support but do not have report
192 * descriptor, load one defined in "Appendix B.2" of HID1_11.pdf
194 error = hid_get_report_descr(parent, &d_ptr, &d_len);
195 if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) ||
196 (error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) &&
197 hid_is_mouse(d_ptr, d_len)))
198 (void)hid_set_report_descr(parent, hms_boot_desc,
199 sizeof(hms_boot_desc));
203 hms_probe(device_t dev)
205 struct hms_softc *sc = device_get_softc(dev);
208 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs);
212 hidmap_set_dev(&sc->hm, dev);
214 /* Check if report descriptor belongs to mouse */
215 error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps);
219 /* There should be at least one X or Y axis */
220 if (!hidmap_test_cap(sc->caps, HMS_REL_X) &&
221 !hidmap_test_cap(sc->caps, HMS_REL_X) &&
222 !hidmap_test_cap(sc->caps, HMS_ABS_X) &&
223 !hidmap_test_cap(sc->caps, HMS_ABS_Y))
226 if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
227 hidmap_test_cap(sc->caps, HMS_ABS_Y))
228 hidbus_set_desc(dev, "Tablet");
230 hidbus_set_desc(dev, "Mouse");
232 return (BUS_PROBE_GENERIC);
236 hms_attach(device_t dev)
238 struct hms_softc *sc = device_get_softc(dev);
239 const struct hid_device_info *hw = hid_get_device_info(dev);
240 struct hidmap_hid_item *hi;
241 HIDMAP_CAPS(cap_wheel, hms_map_wheel);
244 bool set_report_proto;
245 int error, nbuttons = 0;
248 * Set the report (non-boot) protocol if report descriptor has not been
249 * overloaded with boot protocol report descriptor.
251 * Mice without boot protocol support may choose not to implement
252 * Set_Protocol at all; Ignore any error.
254 error = hid_get_report_descr(dev, &d_ptr, &d_len);
255 set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) &&
256 memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0);
257 (void)hid_set_protocol(dev, set_report_proto ? 1 : 0);
259 if (hid_test_quirk(hw, HQ_MS_REVZ))
260 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel);
262 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel);
264 #ifdef IICHID_SAMPLING
265 if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) &&
266 hidmap_test_cap(sc->caps, HMS_REL_X) &&
267 hidmap_test_cap(sc->caps, HMS_REL_Y)) {
268 sc->iichid_sampling = true;
269 sc->isize = hid_report_size_max(d_ptr, d_len, hid_input, NULL);
270 sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO);
271 sc->drift_thresh = 2;
272 SYSCTL_ADD_U32(device_get_sysctl_ctx(dev),
273 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
274 "drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0,
275 "drift detection threshold");
279 error = hidmap_attach(&sc->hm);
283 /* Count number of input usages of variable type mapped to buttons */
284 for (hi = sc->hm.hid_items;
285 hi < sc->hm.hid_items + sc->hm.nhid_items;
287 if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
290 /* announce information about the mouse */
291 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
293 (hidmap_test_cap(sc->caps, HMS_REL_X) ||
294 hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
295 (hidmap_test_cap(sc->caps, HMS_REL_Y) ||
296 hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
297 (hidmap_test_cap(sc->caps, HMS_REL_Z) ||
298 hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
299 hidmap_test_cap(cap_wheel, 0) ? "W" : "",
300 hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
301 sc->hm.hid_items[0].id);
307 hms_detach(device_t dev)
309 struct hms_softc *sc = device_get_softc(dev);
312 error = hidmap_detach(&sc->hm);
313 #ifdef IICHID_SAMPLING
315 free(sc->last_ir, M_DEVBUF);
320 static devclass_t hms_devclass;
321 static device_method_t hms_methods[] = {
322 DEVMETHOD(device_identify, hms_identify),
323 DEVMETHOD(device_probe, hms_probe),
324 DEVMETHOD(device_attach, hms_attach),
325 DEVMETHOD(device_detach, hms_detach),
330 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
331 DRIVER_MODULE(hms, hidbus, hms_driver, hms_devclass, NULL, 0);
332 MODULE_DEPEND(hms, hid, 1, 1, 1);
333 MODULE_DEPEND(hms, hidbus, 1, 1, 1);
334 MODULE_DEPEND(hms, hidmap, 1, 1, 1);
335 MODULE_DEPEND(hms, evdev, 1, 1, 1);
336 MODULE_VERSION(hms, 1);
337 HID_PNP_INFO(hms_devs);