xref: /freebsd/sys/dev/hid/hms.c (revision aa339f1d5df9e38f36a34eb522355c4eebcae6c4)
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 #endif
126 };
127 
128 #ifdef IICHID_SAMPLING
129 static void
130 hms_intr(void *context, void *buf, hid_size_t len)
131 {
132 	struct hidmap *hm = context;
133 	struct hms_softc *sc = device_get_softc(hm->dev);
134 
135 	if (len > sc->isize)
136 		len = sc->isize;
137 
138 	/*
139 	 * Many I2C "compatibility" mouse devices found on touchpads continue
140 	 * to return last report data in sampling mode even after touch has
141 	 * been ended.  That results in cursor drift.  Filter out such a
142 	 * reports through comparing with previous one.
143 	 */
144 	if (len == sc->last_irsize && memcmp(buf, sc->last_ir, len) == 0) {
145 		sc->drift_cnt++;
146 		if (sc->drift_thresh != 0 && sc->drift_cnt >= sc->drift_thresh)
147 			return;
148 	} else {
149 		sc->drift_cnt = 0;
150 		sc->last_irsize = len;
151 		bcopy(buf, sc->last_ir, len);
152 	}
153 
154 	hidmap_intr(context, buf, len);
155 }
156 #endif
157 
158 static int
159 hms_final_cb(HIDMAP_CB_ARGS)
160 {
161 	struct hms_softc *sc = HIDMAP_CB_GET_SOFTC();
162 	struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
163 
164 	if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
165 		if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
166 		    hidmap_test_cap(sc->caps, HMS_ABS_Y))
167 			evdev_support_prop(evdev, INPUT_PROP_DIRECT);
168 		else
169 			evdev_support_prop(evdev, INPUT_PROP_POINTER);
170 #ifdef IICHID_SAMPLING
171 		/* Overload interrupt handler to skip identical reports */
172 		if (sc->iichid_sampling)
173 			hidbus_set_intr(sc->hm.dev, hms_intr, &sc->hm);
174 #endif
175 	}
176 
177 	/* Do not execute callback at interrupt handler and detach */
178 	return (ENOSYS);
179 }
180 
181 static void
182 hms_identify(driver_t *driver, device_t parent)
183 {
184 	const struct hid_device_info *hw = hid_get_device_info(parent);
185 	void *d_ptr;
186 	hid_size_t d_len;
187 	int error;
188 
189 	/*
190 	 * If device claimed boot protocol support but do not have report
191 	 * descriptor, load one defined in "Appendix B.2" of HID1_11.pdf
192 	 */
193 	error = hid_get_report_descr(parent, &d_ptr, &d_len);
194 	if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) ||
195 	    (error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) &&
196 	     hid_is_mouse(d_ptr, d_len)))
197 		(void)hid_set_report_descr(parent, hms_boot_desc,
198 		    sizeof(hms_boot_desc));
199 }
200 
201 static int
202 hms_probe(device_t dev)
203 {
204 	struct hms_softc *sc = device_get_softc(dev);
205 	int error;
206 
207 	error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs);
208 	if (error != 0)
209 		return (error);
210 
211 	hidmap_set_dev(&sc->hm, dev);
212 
213 	/* Check if report descriptor belongs to mouse */
214 	error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps);
215 	if (error != 0)
216 		return (error);
217 
218 	/* There should be at least one X or Y axis */
219 	if (!hidmap_test_cap(sc->caps, HMS_REL_X) &&
220 	    !hidmap_test_cap(sc->caps, HMS_REL_Y) &&
221 	    !hidmap_test_cap(sc->caps, HMS_ABS_X) &&
222 	    !hidmap_test_cap(sc->caps, HMS_ABS_Y))
223 		return (ENXIO);
224 
225 	if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
226 	    hidmap_test_cap(sc->caps, HMS_ABS_Y))
227 		hidbus_set_desc(dev, "Tablet");
228 	else
229 		hidbus_set_desc(dev, "Mouse");
230 
231 	return (BUS_PROBE_GENERIC);
232 }
233 
234 static int
235 hms_attach(device_t dev)
236 {
237 	struct hms_softc *sc = device_get_softc(dev);
238 	const struct hid_device_info *hw = hid_get_device_info(dev);
239 	struct hidmap_hid_item *hi;
240 	HIDMAP_CAPS(cap_wheel, hms_map_wheel);
241 	void *d_ptr;
242 	hid_size_t d_len;
243 	bool set_report_proto;
244 	int error, nbuttons = 0;
245 
246 	/*
247 	 * Set the report (non-boot) protocol if report descriptor has not been
248 	 * overloaded with boot protocol report descriptor.
249 	 *
250 	 * Mice without boot protocol support may choose not to implement
251 	 * Set_Protocol at all; Ignore any error.
252 	 */
253 	error = hid_get_report_descr(dev, &d_ptr, &d_len);
254 	set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) &&
255 	    memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0);
256 	(void)hid_set_protocol(dev, set_report_proto ? 1 : 0);
257 
258 	if (hid_test_quirk(hw, HQ_MS_REVZ))
259 		HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel);
260 	else
261 		HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel);
262 
263 	if (hid_test_quirk(hw, HQ_MS_VENDOR_BTN))
264 		HIDMAP_ADD_MAP(&sc->hm, hms_map_kensington_slimblade, NULL);
265 
266 #ifdef IICHID_SAMPLING
267 	if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) &&
268 	    hidmap_test_cap(sc->caps, HMS_REL_X) &&
269 	    hidmap_test_cap(sc->caps, HMS_REL_Y)) {
270 		sc->iichid_sampling = true;
271 		sc->isize = hid_report_size_max(d_ptr, d_len, hid_input, NULL);
272 		sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO);
273 		sc->drift_thresh = 2;
274 		SYSCTL_ADD_U32(device_get_sysctl_ctx(dev),
275 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
276 		    "drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0,
277 		    "drift detection threshold");
278 	}
279 #endif
280 
281 	error = hidmap_attach(&sc->hm);
282 	if (error)
283 		return (error);
284 
285 	/* Count number of input usages of variable type mapped to buttons */
286 	for (hi = sc->hm.hid_items;
287 	     hi < sc->hm.hid_items + sc->hm.nhid_items;
288 	     hi++)
289 		if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
290 			nbuttons++;
291 
292 	/* announce information about the mouse */
293 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
294 	    nbuttons,
295 	    (hidmap_test_cap(sc->caps, HMS_REL_X) ||
296 	     hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
297 	    (hidmap_test_cap(sc->caps, HMS_REL_Y) ||
298 	     hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
299 	    (hidmap_test_cap(sc->caps, HMS_REL_Z) ||
300 	     hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
301 	    hidmap_test_cap(cap_wheel, 0) ? "W" : "",
302 	    hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
303 	    sc->hm.hid_items[0].id);
304 
305 	return (0);
306 }
307 
308 static int
309 hms_detach(device_t dev)
310 {
311 	struct hms_softc *sc = device_get_softc(dev);
312 	int error;
313 
314 	error = hidmap_detach(&sc->hm);
315 #ifdef IICHID_SAMPLING
316 	if (error == 0)
317 		free(sc->last_ir, M_DEVBUF);
318 #endif
319 	return (error);
320 }
321 
322 static device_method_t hms_methods[] = {
323 	DEVMETHOD(device_identify,	hms_identify),
324 	DEVMETHOD(device_probe,		hms_probe),
325 	DEVMETHOD(device_attach,	hms_attach),
326 	DEVMETHOD(device_detach,	hms_detach),
327 
328 	DEVMETHOD_END
329 };
330 
331 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
332 DRIVER_MODULE(hms, hidbus, hms_driver, NULL, NULL);
333 MODULE_DEPEND(hms, hid, 1, 1, 1);
334 MODULE_DEPEND(hms, hidbus, 1, 1, 1);
335 MODULE_DEPEND(hms, hidmap, 1, 1, 1);
336 MODULE_DEPEND(hms, evdev, 1, 1, 1);
337 MODULE_VERSION(hms, 1);
338 HID_PNP_INFO(hms_devs);
339