FreeBSD/Linux Kernel Cross Reference
sys/dev/hid/hms.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_BTN_MS1,
65 HMS_BTN_MS2,
66 HMS_FINAL_CB,
67 };
68
69 static hidmap_cb_t hms_final_cb;
70 #ifdef IICHID_SAMPLING
71 static hid_intr_t hms_intr;
72 #endif
73
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) }
88
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),
101 };
102
103 static const struct hidmap_item hms_map_wheel[] = {
104 HMS_MAP_REL(HUG_WHEEL, REL_WHEEL),
105 };
106 static const struct hidmap_item hms_map_wheel_rev[] = {
107 HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL),
108 };
109
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_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_X) &&
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_DEFAULT);
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 #ifdef IICHID_SAMPLING
264 if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) &&
265 hidmap_test_cap(sc->caps, HMS_REL_X) &&
266 hidmap_test_cap(sc->caps, HMS_REL_Y)) {
267 sc->iichid_sampling = true;
268 sc->isize = hid_report_size_max(d_ptr, d_len, hid_input, NULL);
269 sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO);
270 sc->drift_thresh = 2;
271 SYSCTL_ADD_U32(device_get_sysctl_ctx(dev),
272 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
273 "drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0,
274 "drift detection threshhold");
275 }
276 #endif
277
278 error = hidmap_attach(&sc->hm);
279 if (error)
280 return (error);
281
282 /* Count number of input usages of variable type mapped to buttons */
283 for (hi = sc->hm.hid_items;
284 hi < sc->hm.hid_items + sc->hm.nhid_items;
285 hi++)
286 if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
287 nbuttons++;
288
289 /* announce information about the mouse */
290 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
291 nbuttons,
292 (hidmap_test_cap(sc->caps, HMS_REL_X) ||
293 hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
294 (hidmap_test_cap(sc->caps, HMS_REL_Y) ||
295 hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
296 (hidmap_test_cap(sc->caps, HMS_REL_Z) ||
297 hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
298 hidmap_test_cap(cap_wheel, 0) ? "W" : "",
299 hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
300 sc->hm.hid_items[0].id);
301
302 return (0);
303 }
304
305 static int
306 hms_detach(device_t dev)
307 {
308 struct hms_softc *sc = device_get_softc(dev);
309 int error;
310
311 error = hidmap_detach(&sc->hm);
312 #ifdef IICHID_SAMPLING
313 if (error == 0)
314 free(sc->last_ir, M_DEVBUF);
315 #endif
316 return (error);
317 }
318
319 static devclass_t hms_devclass;
320 static device_method_t hms_methods[] = {
321 DEVMETHOD(device_identify, hms_identify),
322 DEVMETHOD(device_probe, hms_probe),
323 DEVMETHOD(device_attach, hms_attach),
324 DEVMETHOD(device_detach, hms_detach),
325
326 DEVMETHOD_END
327 };
328
329 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
330 DRIVER_MODULE(hms, hidbus, hms_driver, hms_devclass, NULL, 0);
331 MODULE_DEPEND(hms, hid, 1, 1, 1);
332 MODULE_DEPEND(hms, hidbus, 1, 1, 1);
333 MODULE_DEPEND(hms, hidmap, 1, 1, 1);
334 MODULE_DEPEND(hms, evdev, 1, 1, 1);
335 MODULE_VERSION(hms, 1);
336 HID_PNP_INFO(hms_devs);
Cache object: e26fce72aa8c999ecfaf3686a6627fda
|