xref: /freebsd/sys/dev/hid/hms.c (revision 535af610)
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 __FBSDID("$FreeBSD$");
30 
31 /*
32  * HID spec: https://www.usb.org/sites/default/files/documents/hid1_11.pdf
33  */
34 
35 #include "opt_hid.h"
36 
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 
44 #include <dev/evdev/input.h>
45 #include <dev/evdev/evdev.h>
46 
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>
52 
53 static const uint8_t hms_boot_desc[] = { HID_MOUSE_BOOTPROTO_DESCR() };
54 
55 enum {
56 	HMS_REL_X,
57 	HMS_REL_Y,
58 	HMS_REL_Z,
59 	HMS_ABS_X,
60 	HMS_ABS_Y,
61 	HMS_ABS_Z,
62 	HMS_HWHEEL,
63 	HMS_BTN,
64 	HMS_FINAL_CB,
65 };
66 
67 static hidmap_cb_t	hms_final_cb;
68 #ifdef IICHID_SAMPLING
69 static hid_intr_t	hms_intr;
70 #endif
71 
72 #define HMS_MAP_BUT_RG(usage_from, usage_to, code)	\
73 	{ HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) }
74 #define HMS_MAP_BUT_MS(usage, code)	\
75 	{ HIDMAP_KEY(HUP_MICROSOFT, usage, code) }
76 #define HMS_MAP_ABS(usage, code)	\
77 	{ HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) }
78 #define HMS_MAP_REL(usage, code)	\
79 	{ HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) }
80 #define HMS_MAP_REL_REV(usage, code)	\
81 	{ HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true }
82 #define HMS_MAP_REL_CN(usage, code)	\
83 	{ HIDMAP_REL(HUP_CONSUMER, usage, code) }
84 #define	HMS_FINAL_CB(cb)		\
85 	{ HIDMAP_FINAL_CB(&cb) }
86 
87 static const struct hidmap_item hms_map[] = {
88 	[HMS_REL_X]	= HMS_MAP_REL(HUG_X,		REL_X),
89 	[HMS_REL_Y]	= HMS_MAP_REL(HUG_Y,		REL_Y),
90 	[HMS_REL_Z]	= HMS_MAP_REL(HUG_Z,		REL_Z),
91 	[HMS_ABS_X]	= HMS_MAP_ABS(HUG_X,		ABS_X),
92 	[HMS_ABS_Y]	= HMS_MAP_ABS(HUG_Y,		ABS_Y),
93 	[HMS_ABS_Z]	= HMS_MAP_ABS(HUG_Z,		ABS_Z),
94 	[HMS_HWHEEL]	= HMS_MAP_REL_CN(HUC_AC_PAN,	REL_HWHEEL),
95 	[HMS_BTN]	= HMS_MAP_BUT_RG(1, 16,		BTN_MOUSE),
96 	[HMS_FINAL_CB]	= HMS_FINAL_CB(hms_final_cb),
97 };
98 
99 static const struct hidmap_item hms_map_wheel[] = {
100 	HMS_MAP_REL(HUG_WHEEL,		REL_WHEEL),
101 };
102 static const struct hidmap_item hms_map_wheel_rev[] = {
103 	HMS_MAP_REL_REV(HUG_WHEEL,	REL_WHEEL),
104 };
105 
106 static const struct hidmap_item hms_map_kensington_slimblade[] = {
107 	HMS_MAP_BUT_MS(1,	BTN_RIGHT),
108 	HMS_MAP_BUT_MS(2,	BTN_MIDDLE),
109 };
110 
111 /* A match on these entries will load hms */
112 static const struct hid_device_id hms_devs[] = {
113 	{ HID_TLC(HUP_GENERIC_DESKTOP, HUG_POINTER) },
114 	{ HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) },
115 };
116 
117 struct hms_softc {
118 	struct hidmap		hm;
119 	HIDMAP_CAPS(caps, hms_map);
120 #ifdef IICHID_SAMPLING
121 	bool			iichid_sampling;
122 	void			*last_ir;
123 	hid_size_t		last_irsize;
124 	hid_size_t		isize;
125 	uint32_t		drift_cnt;
126 	uint32_t		drift_thresh;
127 #endif
128 };
129 
130 #ifdef IICHID_SAMPLING
131 static void
132 hms_intr(void *context, void *buf, hid_size_t len)
133 {
134 	struct hidmap *hm = context;
135 	struct hms_softc *sc = device_get_softc(hm->dev);
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 	if (len == sc->last_irsize && memcmp(buf, sc->last_ir, len) == 0) {
147 		sc->drift_cnt++;
148 		if (sc->drift_thresh != 0 && sc->drift_cnt >= sc->drift_thresh)
149 			return;
150 	} else {
151 		sc->drift_cnt = 0;
152 		sc->last_irsize = len;
153 		bcopy(buf, sc->last_ir, len);
154 	}
155 
156 	hidmap_intr(context, buf, len);
157 }
158 #endif
159 
160 static int
161 hms_final_cb(HIDMAP_CB_ARGS)
162 {
163 	struct hms_softc *sc = HIDMAP_CB_GET_SOFTC();
164 	struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
165 
166 	if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
167 		if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
168 		    hidmap_test_cap(sc->caps, HMS_ABS_Y))
169 			evdev_support_prop(evdev, INPUT_PROP_DIRECT);
170 		else
171 			evdev_support_prop(evdev, INPUT_PROP_POINTER);
172 #ifdef IICHID_SAMPLING
173 		/* Overload interrupt handler to skip identical reports */
174 		if (sc->iichid_sampling)
175 			hidbus_set_intr(sc->hm.dev, hms_intr, &sc->hm);
176 #endif
177 	}
178 
179 	/* Do not execute callback at interrupt handler and detach */
180 	return (ENOSYS);
181 }
182 
183 static void
184 hms_identify(driver_t *driver, device_t parent)
185 {
186 	const struct hid_device_info *hw = hid_get_device_info(parent);
187 	void *d_ptr;
188 	hid_size_t d_len;
189 	int error;
190 
191 	/*
192 	 * If device claimed boot protocol support but do not have report
193 	 * descriptor, load one defined in "Appendix B.2" of HID1_11.pdf
194 	 */
195 	error = hid_get_report_descr(parent, &d_ptr, &d_len);
196 	if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) ||
197 	    (error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) &&
198 	     hid_is_mouse(d_ptr, d_len)))
199 		(void)hid_set_report_descr(parent, hms_boot_desc,
200 		    sizeof(hms_boot_desc));
201 }
202 
203 static int
204 hms_probe(device_t dev)
205 {
206 	struct hms_softc *sc = device_get_softc(dev);
207 	int error;
208 
209 	error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs);
210 	if (error != 0)
211 		return (error);
212 
213 	hidmap_set_dev(&sc->hm, dev);
214 
215 	/* Check if report descriptor belongs to mouse */
216 	error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps);
217 	if (error != 0)
218 		return (error);
219 
220 	/* There should be at least one X or Y axis */
221 	if (!hidmap_test_cap(sc->caps, HMS_REL_X) &&
222 	    !hidmap_test_cap(sc->caps, HMS_REL_Y) &&
223 	    !hidmap_test_cap(sc->caps, HMS_ABS_X) &&
224 	    !hidmap_test_cap(sc->caps, HMS_ABS_Y))
225 		return (ENXIO);
226 
227 	if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
228 	    hidmap_test_cap(sc->caps, HMS_ABS_Y))
229 		hidbus_set_desc(dev, "Tablet");
230 	else
231 		hidbus_set_desc(dev, "Mouse");
232 
233 	return (BUS_PROBE_GENERIC);
234 }
235 
236 static int
237 hms_attach(device_t dev)
238 {
239 	struct hms_softc *sc = device_get_softc(dev);
240 	const struct hid_device_info *hw = hid_get_device_info(dev);
241 	struct hidmap_hid_item *hi;
242 	HIDMAP_CAPS(cap_wheel, hms_map_wheel);
243 	void *d_ptr;
244 	hid_size_t d_len;
245 	bool set_report_proto;
246 	int error, nbuttons = 0;
247 
248 	/*
249 	 * Set the report (non-boot) protocol if report descriptor has not been
250 	 * overloaded with boot protocol report descriptor.
251 	 *
252 	 * Mice without boot protocol support may choose not to implement
253 	 * Set_Protocol at all; Ignore any error.
254 	 */
255 	error = hid_get_report_descr(dev, &d_ptr, &d_len);
256 	set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) &&
257 	    memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0);
258 	(void)hid_set_protocol(dev, set_report_proto ? 1 : 0);
259 
260 	if (hid_test_quirk(hw, HQ_MS_REVZ))
261 		HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel);
262 	else
263 		HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel);
264 
265 	if (hid_test_quirk(hw, HQ_MS_VENDOR_BTN))
266 		HIDMAP_ADD_MAP(&sc->hm, hms_map_kensington_slimblade, NULL);
267 
268 #ifdef IICHID_SAMPLING
269 	if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) &&
270 	    hidmap_test_cap(sc->caps, HMS_REL_X) &&
271 	    hidmap_test_cap(sc->caps, HMS_REL_Y)) {
272 		sc->iichid_sampling = true;
273 		sc->isize = hid_report_size_max(d_ptr, d_len, hid_input, NULL);
274 		sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO);
275 		sc->drift_thresh = 2;
276 		SYSCTL_ADD_U32(device_get_sysctl_ctx(dev),
277 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
278 		    "drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0,
279 		    "drift detection threshold");
280 	}
281 #endif
282 
283 	error = hidmap_attach(&sc->hm);
284 	if (error)
285 		return (error);
286 
287 	/* Count number of input usages of variable type mapped to buttons */
288 	for (hi = sc->hm.hid_items;
289 	     hi < sc->hm.hid_items + sc->hm.nhid_items;
290 	     hi++)
291 		if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
292 			nbuttons++;
293 
294 	/* announce information about the mouse */
295 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
296 	    nbuttons,
297 	    (hidmap_test_cap(sc->caps, HMS_REL_X) ||
298 	     hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
299 	    (hidmap_test_cap(sc->caps, HMS_REL_Y) ||
300 	     hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
301 	    (hidmap_test_cap(sc->caps, HMS_REL_Z) ||
302 	     hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
303 	    hidmap_test_cap(cap_wheel, 0) ? "W" : "",
304 	    hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
305 	    sc->hm.hid_items[0].id);
306 
307 	return (0);
308 }
309 
310 static int
311 hms_detach(device_t dev)
312 {
313 	struct hms_softc *sc = device_get_softc(dev);
314 	int error;
315 
316 	error = hidmap_detach(&sc->hm);
317 #ifdef IICHID_SAMPLING
318 	if (error == 0)
319 		free(sc->last_ir, M_DEVBUF);
320 #endif
321 	return (error);
322 }
323 
324 static device_method_t hms_methods[] = {
325 	DEVMETHOD(device_identify,	hms_identify),
326 	DEVMETHOD(device_probe,		hms_probe),
327 	DEVMETHOD(device_attach,	hms_attach),
328 	DEVMETHOD(device_detach,	hms_detach),
329 
330 	DEVMETHOD_END
331 };
332 
333 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
334 DRIVER_MODULE(hms, hidbus, hms_driver, NULL, NULL);
335 MODULE_DEPEND(hms, hid, 1, 1, 1);
336 MODULE_DEPEND(hms, hidbus, 1, 1, 1);
337 MODULE_DEPEND(hms, hidmap, 1, 1, 1);
338 MODULE_DEPEND(hms, evdev, 1, 1, 1);
339 MODULE_VERSION(hms, 1);
340 HID_PNP_INFO(hms_devs);
341