FreeBSD/Linux Kernel Cross Reference
sys/dev/hid/hmt.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014-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 * MS Windows 7/8/10 compatible HID Multi-touch Device driver.
33 * https://msdn.microsoft.com/en-us/library/windows/hardware/jj151569(v=vs.85).aspx
34 * http://download.microsoft.com/download/7/d/d/7dd44bb7-2a7a-4505-ac1c-7227d3d96d5b/hid-over-i2c-protocol-spec-v1-0.docx
35 * https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
36 */
37
38 #include "opt_hid.h"
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49
50 #include <dev/evdev/evdev.h>
51 #include <dev/evdev/input.h>
52
53 #define HID_DEBUG_VAR hmt_debug
54 #include <dev/hid/hid.h>
55 #include <dev/hid/hidbus.h>
56 #include <dev/hid/hidquirk.h>
57
58 #include <dev/hid/hconf.h>
59
60 static SYSCTL_NODE(_hw_hid, OID_AUTO, hmt, CTLFLAG_RW, 0,
61 "MSWindows 7/8/10 compatible HID Multi-touch Device");
62 #ifdef HID_DEBUG
63 static int hmt_debug = 0;
64 SYSCTL_INT(_hw_hid_hmt, OID_AUTO, debug, CTLFLAG_RWTUN,
65 &hmt_debug, 1, "Debug level");
66 #endif
67 static bool hmt_timestamps = 0;
68 SYSCTL_BOOL(_hw_hid_hmt, OID_AUTO, timestamps, CTLFLAG_RDTUN,
69 &hmt_timestamps, 1, "Enable hardware timestamp reporting");
70
71 #define HMT_BTN_MAX 8 /* Number of buttons supported */
72
73 enum hmt_type {
74 HMT_TYPE_UNKNOWN = 0, /* HID report descriptor is not probed */
75 HMT_TYPE_UNSUPPORTED, /* Repdescr does not belong to MT device */
76 HMT_TYPE_TOUCHPAD,
77 HMT_TYPE_TOUCHSCREEN,
78 };
79
80 enum {
81 HMT_TIP_SWITCH = ABS_MT_INDEX(ABS_MT_TOOL_TYPE),
82 HMT_WIDTH = ABS_MT_INDEX(ABS_MT_TOUCH_MAJOR),
83 HMT_HEIGHT = ABS_MT_INDEX(ABS_MT_TOUCH_MINOR),
84 HMT_ORIENTATION = ABS_MT_INDEX(ABS_MT_ORIENTATION),
85 HMT_X = ABS_MT_INDEX(ABS_MT_POSITION_X),
86 HMT_Y = ABS_MT_INDEX(ABS_MT_POSITION_Y),
87 HMT_CONTACTID = ABS_MT_INDEX(ABS_MT_TRACKING_ID),
88 HMT_PRESSURE = ABS_MT_INDEX(ABS_MT_PRESSURE),
89 HMT_IN_RANGE = ABS_MT_INDEX(ABS_MT_DISTANCE),
90 HMT_CONFIDENCE = ABS_MT_INDEX(ABS_MT_BLOB_ID),
91 HMT_TOOL_X = ABS_MT_INDEX(ABS_MT_TOOL_X),
92 HMT_TOOL_Y = ABS_MT_INDEX(ABS_MT_TOOL_Y),
93 };
94
95 #define HMT_N_USAGES MT_CNT
96 #define HMT_NO_USAGE -1
97
98 struct hmt_hid_map_item {
99 char name[5];
100 int32_t usage; /* HID usage */
101 bool reported; /* Item value is passed to evdev */
102 bool required; /* Required for MT Digitizers */
103 };
104
105 static const struct hmt_hid_map_item hmt_hid_map[HMT_N_USAGES] = {
106 [HMT_TIP_SWITCH] = {
107 .name = "TIP",
108 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH),
109 .reported = false,
110 .required = true,
111 },
112 [HMT_WIDTH] = {
113 .name = "WDTH",
114 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_WIDTH),
115 .reported = true,
116 .required = false,
117 },
118 [HMT_HEIGHT] = {
119 .name = "HGHT",
120 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_HEIGHT),
121 .reported = true,
122 .required = false,
123 },
124 [HMT_ORIENTATION] = {
125 .name = "ORIE",
126 .usage = HMT_NO_USAGE,
127 .reported = true,
128 .required = false,
129 },
130 [HMT_X] = {
131 .name = "X",
132 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
133 .reported = true,
134 .required = true,
135 },
136 [HMT_Y] = {
137 .name = "Y",
138 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
139 .reported = true,
140 .required = true,
141 },
142 [HMT_CONTACTID] = {
143 .name = "C_ID",
144 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID),
145 .reported = true,
146 .required = true,
147 },
148 [HMT_PRESSURE] = {
149 .name = "PRES",
150 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_PRESSURE),
151 .reported = true,
152 .required = false,
153 },
154 [HMT_IN_RANGE] = {
155 .name = "RANG",
156 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE),
157 .reported = true,
158 .required = false,
159 },
160 [HMT_CONFIDENCE] = {
161 .name = "CONF",
162 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIDENCE),
163 .reported = false,
164 .required = false,
165 },
166 [HMT_TOOL_X] = { /* Shares HID usage with POS_X */
167 .name = "TL_X",
168 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
169 .reported = true,
170 .required = false,
171 },
172 [HMT_TOOL_Y] = { /* Shares HID usage with POS_Y */
173 .name = "TL_Y",
174 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
175 .reported = true,
176 .required = false,
177 },
178 };
179
180 struct hmt_softc {
181 device_t dev;
182 enum hmt_type type;
183
184 int32_t cont_count_max;
185 struct hid_absinfo ai[HMT_N_USAGES];
186 struct hid_location locs[MAX_MT_SLOTS][HMT_N_USAGES];
187 struct hid_location cont_count_loc;
188 struct hid_location btn_loc[HMT_BTN_MAX];
189 struct hid_location int_btn_loc;
190 struct hid_location scan_time_loc;
191 int32_t scan_time_max;
192 int32_t scan_time;
193 int32_t timestamp;
194 bool touch;
195 bool prev_touch;
196
197 struct evdev_dev *evdev;
198
199 union evdev_mt_slot slot_data;
200 uint8_t caps[howmany(HMT_N_USAGES, 8)];
201 uint8_t buttons[howmany(HMT_BTN_MAX, 8)];
202 uint32_t nconts_per_report;
203 uint32_t nconts_todo;
204 uint8_t report_id;
205 uint32_t max_button;
206 bool has_int_button;
207 bool has_cont_count;
208 bool has_scan_time;
209 bool is_clickpad;
210 bool do_timestamps;
211 #ifdef IICHID_SAMPLING
212 bool iichid_sampling;
213 #endif
214
215 struct hid_location cont_max_loc;
216 uint32_t cont_max_rlen;
217 uint8_t cont_max_rid;
218 struct hid_location btn_type_loc;
219 uint32_t btn_type_rlen;
220 uint8_t btn_type_rid;
221 uint32_t thqa_cert_rlen;
222 uint8_t thqa_cert_rid;
223 };
224
225 #define HMT_FOREACH_USAGE(caps, usage) \
226 for ((usage) = 0; (usage) < HMT_N_USAGES; ++(usage)) \
227 if (isset((caps), (usage)))
228
229 static enum hmt_type hmt_hid_parse(struct hmt_softc *, const void *,
230 hid_size_t, uint32_t, uint8_t);
231 static int hmt_set_input_mode(struct hmt_softc *, enum hconf_input_mode);
232
233 static hid_intr_t hmt_intr;
234
235 static device_probe_t hmt_probe;
236 static device_attach_t hmt_attach;
237 static device_detach_t hmt_detach;
238
239 static evdev_open_t hmt_ev_open;
240 static evdev_close_t hmt_ev_close;
241
242 static const struct evdev_methods hmt_evdev_methods = {
243 .ev_open = &hmt_ev_open,
244 .ev_close = &hmt_ev_close,
245 };
246
247 static const struct hid_device_id hmt_devs[] = {
248 { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN) },
249 { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHPAD) },
250 };
251
252 static int
253 hmt_ev_close(struct evdev_dev *evdev)
254 {
255 return (hidbus_intr_stop(evdev_get_softc(evdev)));
256 }
257
258 static int
259 hmt_ev_open(struct evdev_dev *evdev)
260 {
261 return (hidbus_intr_start(evdev_get_softc(evdev)));
262 }
263
264 static int
265 hmt_probe(device_t dev)
266 {
267 struct hmt_softc *sc = device_get_softc(dev);
268 void *d_ptr;
269 hid_size_t d_len;
270 int err;
271
272 err = HIDBUS_LOOKUP_DRIVER_INFO(dev, hmt_devs);
273 if (err != 0)
274 return (err);
275
276 err = hid_get_report_descr(dev, &d_ptr, &d_len);
277 if (err != 0) {
278 device_printf(dev, "could not retrieve report descriptor from "
279 "device: %d\n", err);
280 return (ENXIO);
281 }
282
283 /* Check if report descriptor belongs to a HID multitouch device */
284 if (sc->type == HMT_TYPE_UNKNOWN)
285 sc->type = hmt_hid_parse(sc, d_ptr, d_len,
286 hidbus_get_usage(dev), hidbus_get_index(dev));
287 if (sc->type == HMT_TYPE_UNSUPPORTED)
288 return (ENXIO);
289
290 hidbus_set_desc(dev,
291 sc->type == HMT_TYPE_TOUCHPAD ? "TouchPad" : "TouchScreen");
292
293 return (BUS_PROBE_DEFAULT);
294 }
295
296 static int
297 hmt_attach(device_t dev)
298 {
299 struct hmt_softc *sc = device_get_softc(dev);
300 const struct hid_device_info *hw = hid_get_device_info(dev);
301 void *d_ptr;
302 uint8_t *fbuf = NULL;
303 hid_size_t d_len, fsize, rsize;
304 uint32_t cont_count_max;
305 int nbuttons, btn;
306 size_t i;
307 int err;
308
309 err = hid_get_report_descr(dev, &d_ptr, &d_len);
310 if (err != 0) {
311 device_printf(dev, "could not retrieve report descriptor from "
312 "device: %d\n", err);
313 return (ENXIO);
314 }
315
316 sc->dev = dev;
317
318 fsize = hid_report_size_max(d_ptr, d_len, hid_feature, NULL);
319 if (fsize != 0)
320 fbuf = malloc(fsize, M_TEMP, M_WAITOK | M_ZERO);
321
322 /* Fetch and parse "Contact count maximum" feature report */
323 if (sc->cont_max_rlen > 1) {
324 err = hid_get_report(dev, fbuf, sc->cont_max_rlen, &rsize,
325 HID_FEATURE_REPORT, sc->cont_max_rid);
326 if (err == 0 && (rsize - 1) * 8 >=
327 sc->cont_max_loc.pos + sc->cont_max_loc.size) {
328 cont_count_max = hid_get_udata(fbuf + 1,
329 sc->cont_max_rlen - 1, &sc->cont_max_loc);
330 /*
331 * Feature report is a primary source of
332 * 'Contact Count Maximum'
333 */
334 if (cont_count_max > 0)
335 sc->cont_count_max = cont_count_max;
336 } else
337 DPRINTF("hid_get_report error=%d\n", err);
338 }
339 if (sc->cont_count_max == 0)
340 sc->cont_count_max = sc->type == HMT_TYPE_TOUCHSCREEN ? 10 : 5;
341
342 /* Fetch and parse "Button type" feature report */
343 if (sc->btn_type_rlen > 1 && sc->btn_type_rid != sc->cont_max_rid) {
344 bzero(fbuf, fsize);
345 err = hid_get_report(dev, fbuf, sc->btn_type_rlen, &rsize,
346 HID_FEATURE_REPORT, sc->btn_type_rid);
347 if (err != 0)
348 DPRINTF("hid_get_report error=%d\n", err);
349 }
350 if (sc->btn_type_rlen > 1 && err == 0 && (rsize - 1) * 8 >=
351 sc->btn_type_loc.pos + sc->btn_type_loc.size)
352 sc->is_clickpad = hid_get_udata(fbuf + 1, sc->btn_type_rlen - 1,
353 &sc->btn_type_loc) == 0;
354 else
355 sc->is_clickpad = sc->max_button == 0 && sc->has_int_button;
356
357 /* Fetch THQA certificate to enable some devices like WaveShare */
358 if (sc->thqa_cert_rlen > 1 && sc->thqa_cert_rid != sc->cont_max_rid)
359 (void)hid_get_report(dev, fbuf, sc->thqa_cert_rlen, NULL,
360 HID_FEATURE_REPORT, sc->thqa_cert_rid);
361
362 free(fbuf, M_TEMP);
363
364 /* Switch touchpad in to absolute multitouch mode */
365 if (sc->type == HMT_TYPE_TOUCHPAD) {
366 err = hmt_set_input_mode(sc, HCONF_INPUT_MODE_MT_TOUCHPAD);
367 if (err != 0)
368 DPRINTF("Failed to set input mode: %d\n", err);
369 }
370
371 /* Cap contact count maximum to MAX_MT_SLOTS */
372 if (sc->cont_count_max > MAX_MT_SLOTS) {
373 DPRINTF("Hardware reported %d contacts while only %d is "
374 "supported\n", sc->cont_count_max, MAX_MT_SLOTS);
375 sc->cont_count_max = MAX_MT_SLOTS;
376 }
377
378 if (sc->has_scan_time &&
379 (hid_test_quirk(hw, HQ_MT_TIMESTAMP) || hmt_timestamps))
380 sc->do_timestamps = true;
381 #ifdef IICHID_SAMPLING
382 if (hid_test_quirk(hw, HQ_IICHID_SAMPLING))
383 sc->iichid_sampling = true;
384 #endif
385
386 hidbus_set_intr(dev, hmt_intr, sc);
387
388 sc->evdev = evdev_alloc();
389 evdev_set_name(sc->evdev, device_get_desc(dev));
390 evdev_set_phys(sc->evdev, device_get_nameunit(dev));
391 evdev_set_id(sc->evdev, hw->idBus, hw->idVendor, hw->idProduct,
392 hw->idVersion);
393 evdev_set_serial(sc->evdev, hw->serial);
394 evdev_set_methods(sc->evdev, dev, &hmt_evdev_methods);
395 evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT);
396 evdev_set_flag(sc->evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */
397 switch (sc->type) {
398 case HMT_TYPE_TOUCHSCREEN:
399 evdev_support_prop(sc->evdev, INPUT_PROP_DIRECT);
400 break;
401 case HMT_TYPE_TOUCHPAD:
402 evdev_support_prop(sc->evdev, INPUT_PROP_POINTER);
403 if (sc->is_clickpad)
404 evdev_support_prop(sc->evdev, INPUT_PROP_BUTTONPAD);
405 break;
406 default:
407 KASSERT(0, ("hmt_attach: unsupported touch device type"));
408 }
409 evdev_support_event(sc->evdev, EV_SYN);
410 evdev_support_event(sc->evdev, EV_ABS);
411 if (sc->do_timestamps) {
412 evdev_support_event(sc->evdev, EV_MSC);
413 evdev_support_msc(sc->evdev, MSC_TIMESTAMP);
414 }
415 #ifdef IICHID_SAMPLING
416 if (sc->iichid_sampling)
417 evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_AUTOREL);
418 #endif
419 nbuttons = 0;
420 if (sc->max_button != 0 || sc->has_int_button) {
421 evdev_support_event(sc->evdev, EV_KEY);
422 if (sc->has_int_button)
423 evdev_support_key(sc->evdev, BTN_LEFT);
424 for (btn = 0; btn < sc->max_button; ++btn) {
425 if (isset(sc->buttons, btn)) {
426 evdev_support_key(sc->evdev, BTN_MOUSE + btn);
427 nbuttons++;
428 }
429 }
430 }
431 evdev_support_abs(sc->evdev,
432 ABS_MT_SLOT, 0, sc->cont_count_max - 1, 0, 0, 0);
433 HMT_FOREACH_USAGE(sc->caps, i) {
434 if (hmt_hid_map[i].reported)
435 evdev_support_abs(sc->evdev, ABS_MT_FIRST + i,
436 sc->ai[i].min, sc->ai[i].max, 0, 0, sc->ai[i].res);
437 }
438
439 err = evdev_register(sc->evdev);
440 if (err) {
441 hmt_detach(dev);
442 return (ENXIO);
443 }
444
445 /* Announce information about the touch device */
446 device_printf(sc->dev, "%s %s with %d external button%s%s\n",
447 sc->cont_count_max > 1 ? "Multitouch" : "Singletouch",
448 sc->type == HMT_TYPE_TOUCHSCREEN ? "touchscreen" : "touchpad",
449 nbuttons, nbuttons != 1 ? "s" : "",
450 sc->is_clickpad ? ", click-pad" : "");
451 device_printf(sc->dev,
452 "%d contact%s with [%s%s%s%s%s] properties. Report range [%d:%d] - [%d:%d]\n",
453 (int)sc->cont_count_max, sc->cont_count_max != 1 ? "s" : "",
454 isset(sc->caps, HMT_IN_RANGE) ? "R" : "",
455 isset(sc->caps, HMT_CONFIDENCE) ? "C" : "",
456 isset(sc->caps, HMT_WIDTH) ? "W" : "",
457 isset(sc->caps, HMT_HEIGHT) ? "H" : "",
458 isset(sc->caps, HMT_PRESSURE) ? "P" : "",
459 (int)sc->ai[HMT_X].min, (int)sc->ai[HMT_Y].min,
460 (int)sc->ai[HMT_X].max, (int)sc->ai[HMT_Y].max);
461
462 return (0);
463 }
464
465 static int
466 hmt_detach(device_t dev)
467 {
468 struct hmt_softc *sc = device_get_softc(dev);
469
470 evdev_free(sc->evdev);
471
472 return (0);
473 }
474
475 static void
476 hmt_intr(void *context, void *buf, hid_size_t len)
477 {
478 struct hmt_softc *sc = context;
479 size_t usage;
480 union evdev_mt_slot *slot_data;
481 uint32_t cont, btn;
482 uint32_t cont_count;
483 uint32_t width;
484 uint32_t height;
485 uint32_t int_btn = 0;
486 uint32_t left_btn = 0;
487 int slot;
488 uint32_t scan_time;
489 int32_t delta;
490 uint8_t id;
491
492 #ifdef IICHID_SAMPLING
493 /*
494 * Special packet of zero length is generated by iichid driver running
495 * in polling mode at the start of inactivity period to workaround
496 * "stuck touch" problem caused by miss of finger release events.
497 * This snippet is to be removed after GPIO interrupt support is added.
498 */
499 if (sc->iichid_sampling && len == 0) {
500 sc->prev_touch = false;
501 sc->timestamp = 0;
502 /* EVDEV_FLAG_MT_AUTOREL releases all touches for us */
503 evdev_sync(sc->evdev);
504 return;
505 }
506 #endif
507
508 /* Ignore irrelevant reports */
509 id = sc->report_id != 0 ? *(uint8_t *)buf : 0;
510 if (sc->report_id != id) {
511 DPRINTF("Skip report with unexpected ID: %hhu\n", id);
512 return;
513 }
514
515 /* Strip leading "report ID" byte */
516 if (sc->report_id != 0) {
517 len--;
518 buf = (uint8_t *)buf + 1;
519 }
520
521 /*
522 * "In Serial mode, each packet contains information that describes a
523 * single physical contact point. Multiple contacts are streamed
524 * serially. In this mode, devices report all contact information in a
525 * series of packets. The device sends a separate packet for each
526 * concurrent contact."
527 *
528 * "In Parallel mode, devices report all contact information in a
529 * single packet. Each physical contact is represented by a logical
530 * collection that is embedded in the top-level collection."
531 *
532 * Since additional contacts that were not present will still be in the
533 * report with contactid=0 but contactids are zero-based, find
534 * contactcount first.
535 */
536 if (sc->has_cont_count)
537 cont_count = hid_get_udata(buf, len, &sc->cont_count_loc);
538 else
539 cont_count = 1;
540 /*
541 * "In Hybrid mode, the number of contacts that can be reported in one
542 * report is less than the maximum number of contacts that the device
543 * supports. For example, a device that supports a maximum of
544 * 4 concurrent physical contacts, can set up its top-level collection
545 * to deliver a maximum of two contacts in one report. If four contact
546 * points are present, the device can break these up into two serial
547 * reports that deliver two contacts each.
548 *
549 * "When a device delivers data in this manner, the Contact Count usage
550 * value in the first report should reflect the total number of
551 * contacts that are being delivered in the hybrid reports. The other
552 * serial reports should have a contact count of zero (0)."
553 */
554 if (cont_count != 0)
555 sc->nconts_todo = cont_count;
556
557 #ifdef HID_DEBUG
558 DPRINTFN(6, "cont_count:%2u", (unsigned)cont_count);
559 if (hmt_debug >= 6) {
560 HMT_FOREACH_USAGE(sc->caps, usage) {
561 if (hmt_hid_map[usage].usage != HMT_NO_USAGE)
562 printf(" %-4s", hmt_hid_map[usage].name);
563 }
564 printf("\n");
565 }
566 #endif
567
568 /* Find the number of contacts reported in current report */
569 cont_count = MIN(sc->nconts_todo, sc->nconts_per_report);
570
571 /* Use protocol Type B for reporting events */
572 for (cont = 0; cont < cont_count; cont++) {
573 slot_data = &sc->slot_data;
574 bzero(slot_data, sizeof(sc->slot_data));
575 HMT_FOREACH_USAGE(sc->caps, usage) {
576 if (sc->locs[cont][usage].size > 0)
577 slot_data->val[usage] = hid_get_udata(
578 buf, len, &sc->locs[cont][usage]);
579 }
580
581 slot = evdev_mt_id_to_slot(sc->evdev, slot_data->id);
582
583 #ifdef HID_DEBUG
584 DPRINTFN(6, "cont%01x: data = ", cont);
585 if (hmt_debug >= 6) {
586 HMT_FOREACH_USAGE(sc->caps, usage) {
587 if (hmt_hid_map[usage].usage != HMT_NO_USAGE)
588 printf("%04x ", slot_data->val[usage]);
589 }
590 printf("slot = %d\n", slot);
591 }
592 #endif
593
594 if (slot == -1) {
595 DPRINTF("Slot overflow for contact_id %u\n",
596 (unsigned)slot_data->id);
597 continue;
598 }
599
600 if (slot_data->val[HMT_TIP_SWITCH] != 0 &&
601 !(isset(sc->caps, HMT_CONFIDENCE) &&
602 slot_data->val[HMT_CONFIDENCE] == 0)) {
603 /* This finger is in proximity of the sensor */
604 sc->touch = true;
605 slot_data->dist = !slot_data->val[HMT_IN_RANGE];
606 /* Divided by two to match visual scale of touch */
607 width = slot_data->val[HMT_WIDTH] >> 1;
608 height = slot_data->val[HMT_HEIGHT] >> 1;
609 slot_data->ori = width > height;
610 slot_data->maj = MAX(width, height);
611 slot_data->min = MIN(width, height);
612 } else
613 slot_data = NULL;
614
615 evdev_mt_push_slot(sc->evdev, slot, slot_data);
616 }
617
618 sc->nconts_todo -= cont_count;
619 if (sc->do_timestamps && sc->nconts_todo == 0) {
620 /* HUD_SCAN_TIME is measured in 100us, convert to us. */
621 scan_time = hid_get_udata(buf, len, &sc->scan_time_loc);
622 if (sc->prev_touch) {
623 delta = scan_time - sc->scan_time;
624 if (delta < 0)
625 delta += sc->scan_time_max;
626 } else
627 delta = 0;
628 sc->scan_time = scan_time;
629 sc->timestamp += delta * 100;
630 evdev_push_msc(sc->evdev, MSC_TIMESTAMP, sc->timestamp);
631 sc->prev_touch = sc->touch;
632 sc->touch = false;
633 if (!sc->prev_touch)
634 sc->timestamp = 0;
635 }
636 if (sc->nconts_todo == 0) {
637 /* Report both the click and external left btns as BTN_LEFT */
638 if (sc->has_int_button)
639 int_btn = hid_get_data(buf, len, &sc->int_btn_loc);
640 if (isset(sc->buttons, 0))
641 left_btn = hid_get_data(buf, len, &sc->btn_loc[0]);
642 if (sc->has_int_button || isset(sc->buttons, 0))
643 evdev_push_key(sc->evdev, BTN_LEFT,
644 (int_btn != 0) | (left_btn != 0));
645 for (btn = 1; btn < sc->max_button; ++btn) {
646 if (isset(sc->buttons, btn))
647 evdev_push_key(sc->evdev, BTN_MOUSE + btn,
648 hid_get_data(buf,
649 len,
650 &sc->btn_loc[btn]) != 0);
651 }
652 evdev_sync(sc->evdev);
653 }
654 }
655
656 static enum hmt_type
657 hmt_hid_parse(struct hmt_softc *sc, const void *d_ptr, hid_size_t d_len,
658 uint32_t tlc_usage, uint8_t tlc_index)
659 {
660 struct hid_absinfo ai;
661 struct hid_item hi;
662 struct hid_data *hd;
663 uint32_t flags;
664 size_t i;
665 size_t cont = 0;
666 enum hmt_type type;
667 uint32_t left_btn, btn;
668 int32_t cont_count_max = 0;
669 uint8_t report_id = 0;
670 bool finger_coll = false;
671 bool cont_count_found = false;
672 bool scan_time_found = false;
673 bool has_int_button = false;
674
675 #define HMT_HI_ABSOLUTE(hi) ((hi).nusages != 0 && \
676 ((hi).flags & (HIO_VARIABLE | HIO_RELATIVE)) == HIO_VARIABLE)
677 #define HUMS_THQA_CERT 0xC5
678
679 /*
680 * Get left button usage taking in account MS Precision Touchpad specs.
681 * For Windows PTP report descriptor assigns buttons in following way:
682 * Button 1 - Indicates Button State for touchpad button integrated
683 * with digitizer.
684 * Button 2 - Indicates Button State for external button for primary
685 * (default left) clicking.
686 * Button 3 - Indicates Button State for external button for secondary
687 * (default right) clicking.
688 * If a device only supports external buttons, it must still use
689 * Button 2 and Button 3 to reference the external buttons.
690 */
691 switch (tlc_usage) {
692 case HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN):
693 type = HMT_TYPE_TOUCHSCREEN;
694 left_btn = 1;
695 break;
696 case HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHPAD):
697 type = HMT_TYPE_TOUCHPAD;
698 left_btn = 2;
699 break;
700 default:
701 return (HMT_TYPE_UNSUPPORTED);
702 }
703
704 /* Parse features for mandatory maximum contact count usage */
705 if (!hidbus_locate(d_ptr, d_len,
706 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX), hid_feature,
707 tlc_index, 0, &sc->cont_max_loc, &flags, &sc->cont_max_rid, &ai) ||
708 (flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE)
709 return (HMT_TYPE_UNSUPPORTED);
710
711 cont_count_max = ai.max;
712
713 /* Parse features for button type usage */
714 if (hidbus_locate(d_ptr, d_len,
715 HID_USAGE2(HUP_DIGITIZERS, HUD_BUTTON_TYPE), hid_feature,
716 tlc_index, 0, &sc->btn_type_loc, &flags, &sc->btn_type_rid, NULL)
717 && (flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE)
718 sc->btn_type_rid = 0;
719
720 /* Parse features for THQA certificate report ID */
721 hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_MICROSOFT, HUMS_THQA_CERT),
722 hid_feature, tlc_index, 0, NULL, NULL, &sc->thqa_cert_rid, NULL);
723
724 /* Parse input for other parameters */
725 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
726 HIDBUS_FOREACH_ITEM(hd, &hi, tlc_index) {
727 switch (hi.kind) {
728 case hid_collection:
729 if (hi.collevel == 2 &&
730 hi.usage == HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER))
731 finger_coll = true;
732 break;
733 case hid_endcollection:
734 if (hi.collevel == 1 && finger_coll) {
735 finger_coll = false;
736 cont++;
737 }
738 break;
739 case hid_input:
740 /*
741 * Ensure that all usages belong to the same report
742 */
743 if (HMT_HI_ABSOLUTE(hi) &&
744 (report_id == 0 || report_id == hi.report_ID))
745 report_id = hi.report_ID;
746 else
747 break;
748
749 if (hi.collevel == 1 && left_btn == 2 &&
750 hi.usage == HID_USAGE2(HUP_BUTTON, 1)) {
751 has_int_button = true;
752 sc->int_btn_loc = hi.loc;
753 break;
754 }
755 if (hi.collevel == 1 &&
756 hi.usage >= HID_USAGE2(HUP_BUTTON, left_btn) &&
757 hi.usage <= HID_USAGE2(HUP_BUTTON, HMT_BTN_MAX)) {
758 btn = (hi.usage & 0xFFFF) - left_btn;
759 setbit(sc->buttons, btn);
760 sc->btn_loc[btn] = hi.loc;
761 if (btn >= sc->max_button)
762 sc->max_button = btn + 1;
763 break;
764 }
765 if (hi.collevel == 1 && hi.usage ==
766 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTCOUNT)) {
767 cont_count_found = true;
768 sc->cont_count_loc = hi.loc;
769 break;
770 }
771 if (hi.collevel == 1 && hi.usage ==
772 HID_USAGE2(HUP_DIGITIZERS, HUD_SCAN_TIME)) {
773 scan_time_found = true;
774 sc->scan_time_loc = hi.loc;
775 sc->scan_time_max = hi.logical_maximum;
776 break;
777 }
778
779 if (!finger_coll || hi.collevel != 2)
780 break;
781 if (cont >= MAX_MT_SLOTS) {
782 DPRINTF("Finger %zu ignored\n", cont);
783 break;
784 }
785
786 for (i = 0; i < HMT_N_USAGES; i++) {
787 if (hi.usage == hmt_hid_map[i].usage) {
788 /*
789 * HUG_X usage is an array mapped to
790 * both ABS_MT_POSITION and ABS_MT_TOOL
791 * events. So don`t stop search if we
792 * already have HUG_X mapping done.
793 */
794 if (sc->locs[cont][i].size)
795 continue;
796 sc->locs[cont][i] = hi.loc;
797 /*
798 * Hid parser returns valid logical and
799 * physical sizes for first finger only
800 * at least on ElanTS 0x04f3:0x0012.
801 */
802 if (cont > 0)
803 break;
804 setbit(sc->caps, i);
805 sc->ai[i] = (struct hid_absinfo) {
806 .max = hi.logical_maximum,
807 .min = hi.logical_minimum,
808 .res = hid_item_resolution(&hi),
809 };
810 break;
811 }
812 }
813 break;
814 default:
815 break;
816 }
817 }
818 hid_end_parse(hd);
819
820 /* Check for required HID Usages */
821 if ((!cont_count_found && cont != 1) || cont == 0)
822 return (HMT_TYPE_UNSUPPORTED);
823 for (i = 0; i < HMT_N_USAGES; i++) {
824 if (hmt_hid_map[i].required && isclr(sc->caps, i))
825 return (HMT_TYPE_UNSUPPORTED);
826 }
827
828 /* Touchpads must have at least one button */
829 if (type == HMT_TYPE_TOUCHPAD && !sc->max_button && !has_int_button)
830 return (HMT_TYPE_UNSUPPORTED);
831
832 /*
833 * According to specifications 'Contact Count Maximum' should be read
834 * from Feature Report rather than from HID descriptor. Set sane
835 * default value now to handle the case of 'Get Report' request failure
836 */
837 if (cont_count_max < 1)
838 cont_count_max = cont;
839
840 /* Report touch orientation if both width and height are supported */
841 if (isset(sc->caps, HMT_WIDTH) && isset(sc->caps, HMT_HEIGHT)) {
842 setbit(sc->caps, HMT_ORIENTATION);
843 sc->ai[HMT_ORIENTATION].max = 1;
844 }
845
846 sc->cont_max_rlen = hid_report_size(d_ptr, d_len, hid_feature,
847 sc->cont_max_rid);
848 if (sc->btn_type_rid > 0)
849 sc->btn_type_rlen = hid_report_size(d_ptr, d_len,
850 hid_feature, sc->btn_type_rid);
851 if (sc->thqa_cert_rid > 0)
852 sc->thqa_cert_rlen = hid_report_size(d_ptr, d_len,
853 hid_feature, sc->thqa_cert_rid);
854
855 sc->report_id = report_id;
856 sc->cont_count_max = cont_count_max;
857 sc->nconts_per_report = cont;
858 sc->has_int_button = has_int_button;
859 sc->has_cont_count = cont_count_found;
860 sc->has_scan_time = scan_time_found;
861
862 return (type);
863 }
864
865 static int
866 hmt_set_input_mode(struct hmt_softc *sc, enum hconf_input_mode mode)
867 {
868 devclass_t hconf_devclass;
869 device_t hconf;
870 int err;
871
872 bus_topo_assert();
873
874 /* Find touchpad's configuration TLC */
875 hconf = hidbus_find_child(device_get_parent(sc->dev),
876 HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIG));
877 if (hconf == NULL)
878 return (ENXIO);
879
880 /* Ensure that hconf driver is attached to configuration TLC */
881 if (device_is_alive(hconf) == 0)
882 device_probe_and_attach(hconf);
883 if (device_is_attached(hconf) == 0)
884 return (ENXIO);
885 hconf_devclass = devclass_find("hconf");
886 if (device_get_devclass(hconf) != hconf_devclass)
887 return (ENXIO);
888
889 /* hconf_set_input_mode can drop the topo lock while sleeping */
890 device_busy(hconf);
891 err = hconf_set_input_mode(hconf, mode);
892 device_unbusy(hconf);
893
894 return (err);
895 }
896
897 static device_method_t hmt_methods[] = {
898 DEVMETHOD(device_probe, hmt_probe),
899 DEVMETHOD(device_attach, hmt_attach),
900 DEVMETHOD(device_detach, hmt_detach),
901
902 DEVMETHOD_END
903 };
904
905 static driver_t hmt_driver = {
906 .name = "hmt",
907 .methods = hmt_methods,
908 .size = sizeof(struct hmt_softc),
909 };
910
911 DRIVER_MODULE(hmt, hidbus, hmt_driver, NULL, NULL);
912 MODULE_DEPEND(hmt, hidbus, 1, 1, 1);
913 MODULE_DEPEND(hmt, hid, 1, 1, 1);
914 MODULE_DEPEND(hmt, hconf, 1, 1, 1);
915 MODULE_DEPEND(hmt, evdev, 1, 1, 1);
916 MODULE_VERSION(hmt, 1);
917 HID_PNP_INFO(hmt_devs);
Cache object: 948b2b759bdb87f14f49384711da5d2f
|