xref: /freebsd/sys/dev/usb/input/wmt.c (revision 4f52dfbb)
1 /*-
2  * Copyright (c) 2014-2017 Vladimir Kondratyev <wulf@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * MS Windows 7/8/10 compatible USB HID Multi-touch Device driver.
32  * https://msdn.microsoft.com/en-us/library/windows/hardware/jj151569(v=vs.85).aspx
33  * https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
34  */
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/stddef.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 
48 #include "usbdevs.h"
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbhid.h>
53 
54 #include <dev/usb/quirk/usb_quirk.h>
55 
56 #include <dev/evdev/evdev.h>
57 #include <dev/evdev/input.h>
58 
59 #define	USB_DEBUG_VAR wmt_debug
60 #include <dev/usb/usb_debug.h>
61 
62 #ifdef USB_DEBUG
63 static int wmt_debug = 0;
64 
65 static SYSCTL_NODE(_hw_usb, OID_AUTO, wmt, CTLFLAG_RW, 0,
66     "USB MSWindows 7/8/10 compatible Multi-touch Device");
67 SYSCTL_INT(_hw_usb_wmt, OID_AUTO, debug, CTLFLAG_RWTUN,
68     &wmt_debug, 1, "Debug level");
69 #endif
70 
71 #define	WMT_BSIZE	1024	/* bytes, buffer size */
72 
73 enum {
74 	WMT_INTR_DT,
75 	WMT_N_TRANSFER,
76 };
77 
78 enum {
79 	WMT_TIP_SWITCH,
80 #define	WMT_SLOT	WMT_TIP_SWITCH
81 	WMT_WIDTH,
82 #define	WMT_MAJOR	WMT_WIDTH
83 	WMT_HEIGHT,
84 #define WMT_MINOR	WMT_HEIGHT
85 	WMT_ORIENTATION,
86 	WMT_X,
87 	WMT_Y,
88 	WMT_CONTACTID,
89 	WMT_PRESSURE,
90 	WMT_IN_RANGE,
91 	WMT_CONFIDENCE,
92 	WMT_TOOL_X,
93 	WMT_TOOL_Y,
94 	WMT_N_USAGES,
95 };
96 
97 #define	WMT_NO_CODE	(ABS_MAX + 10)
98 #define	WMT_NO_USAGE	-1
99 
100 struct wmt_hid_map_item {
101 	char		name[5];
102 	int32_t 	usage;		/* HID usage */
103 	uint32_t	code;		/* Evdev event code */
104 	bool		required;	/* Required for MT Digitizers */
105 };
106 
107 static const struct wmt_hid_map_item wmt_hid_map[WMT_N_USAGES] = {
108 
109 	[WMT_TIP_SWITCH] = {	/* WMT_SLOT */
110 		.name = "TIP",
111 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH),
112 		.code = ABS_MT_SLOT,
113 		.required = true,
114 	},
115 	[WMT_WIDTH] = {		/* WMT_MAJOR */
116 		.name = "WDTH",
117 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_WIDTH),
118 		.code = ABS_MT_TOUCH_MAJOR,
119 		.required = false,
120 	},
121 	[WMT_HEIGHT] = {	/* WMT_MINOR */
122 		.name = "HGHT",
123 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_HEIGHT),
124 		.code = ABS_MT_TOUCH_MINOR,
125 		.required = false,
126 	},
127 	[WMT_ORIENTATION] = {
128 		.name = "ORIE",
129 		.usage = WMT_NO_USAGE,
130 		.code = ABS_MT_ORIENTATION,
131 		.required = false,
132 	},
133 	[WMT_X] = {
134 		.name = "X",
135 		.usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
136 		.code = ABS_MT_POSITION_X,
137 		.required = true,
138 	},
139 	[WMT_Y] = {
140 		.name = "Y",
141 		.usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
142 		.code = ABS_MT_POSITION_Y,
143 		.required = true,
144 	},
145 	[WMT_CONTACTID] = {
146 		.name = "C_ID",
147 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID),
148 		.code = ABS_MT_TRACKING_ID,
149 		.required = true,
150 	},
151 	[WMT_PRESSURE] = {
152 		.name = "PRES",
153 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_PRESSURE),
154 		.code = ABS_MT_PRESSURE,
155 		.required = false,
156 	},
157 	[WMT_IN_RANGE] = {
158 		.name = "RANG",
159 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE),
160 		.code = ABS_MT_DISTANCE,
161 		.required = false,
162 	},
163 	[WMT_CONFIDENCE] = {
164 		.name = "CONF",
165 		.usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIDENCE),
166 		.code = WMT_NO_CODE,
167 		.required = false,
168 	},
169 	[WMT_TOOL_X] = {	/* Shares HID usage with WMT_X */
170 		.name = "TL_X",
171 		.usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
172 		.code = ABS_MT_TOOL_X,
173 		.required = false,
174 	},
175 	[WMT_TOOL_Y] = {	/* Shares HID usage with WMT_Y */
176 		.name = "TL_Y",
177 		.usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
178 		.code = ABS_MT_TOOL_Y,
179 		.required = false,
180 	},
181 };
182 
183 struct wmt_absinfo {
184 	int32_t			min;
185 	int32_t			max;
186 	int32_t			res;
187 };
188 
189 struct wmt_softc
190 {
191 	device_t		dev;
192 	struct mtx		mtx;
193 	struct wmt_absinfo	ai[WMT_N_USAGES];
194 	struct hid_location	locs[MAX_MT_SLOTS][WMT_N_USAGES];
195 	struct hid_location	nconts_loc;
196 
197 	struct usb_xfer		*xfer[WMT_N_TRANSFER];
198 	struct evdev_dev	*evdev;
199 
200 	uint32_t		slot_data[WMT_N_USAGES];
201 	uint32_t		caps;
202 	uint32_t		isize;
203 	uint32_t		nconts_max;
204 	uint8_t			report_id;
205 
206 	uint8_t			buf[WMT_BSIZE] __aligned(4);
207 };
208 
209 #define	USAGE_SUPPORTED(caps, usage)	((caps) & (1 << (usage)))
210 #define	WMT_FOREACH_USAGE(caps, usage)			\
211 	for ((usage) = 0; (usage) < WMT_N_USAGES; ++(usage))	\
212 		if (USAGE_SUPPORTED((caps), (usage)))
213 
214 static bool wmt_hid_parse(struct wmt_softc *, const void *, uint16_t);
215 
216 static usb_callback_t	wmt_intr_callback;
217 
218 static device_probe_t	wmt_probe;
219 static device_attach_t	wmt_attach;
220 static device_detach_t	wmt_detach;
221 
222 static evdev_open_t	wmt_ev_open;
223 static evdev_close_t	wmt_ev_close;
224 
225 static const struct evdev_methods wmt_evdev_methods = {
226 	.ev_open = &wmt_ev_open,
227 	.ev_close = &wmt_ev_close,
228 };
229 
230 static const struct usb_config wmt_config[WMT_N_TRANSFER] = {
231 
232 	[WMT_INTR_DT] = {
233 		.type = UE_INTERRUPT,
234 		.endpoint = UE_ADDR_ANY,
235 		.direction = UE_DIR_IN,
236 		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
237 		.bufsize = WMT_BSIZE,
238 		.callback = &wmt_intr_callback,
239 	},
240 };
241 
242 static int
243 wmt_probe(device_t dev)
244 {
245 	struct usb_attach_arg *uaa = device_get_ivars(dev);
246 	void *d_ptr;
247 	uint16_t d_len;
248 	int err;
249 
250 	if (uaa->usb_mode != USB_MODE_HOST)
251 		return (ENXIO);
252 
253 	if (uaa->info.bInterfaceClass != UICLASS_HID)
254 		return (ENXIO);
255 
256 	if (usb_test_quirk(uaa, UQ_WMT_IGNORE))
257 		return (ENXIO);
258 
259 	err = usbd_req_get_hid_desc(uaa->device, NULL,
260 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
261 	if (err)
262 		return (ENXIO);
263 
264 	if (wmt_hid_parse(NULL, d_ptr, d_len))
265 		err = BUS_PROBE_DEFAULT;
266 	else
267 		err = ENXIO;
268 
269 	free(d_ptr, M_TEMP);
270 	return (err);
271 }
272 
273 static int
274 wmt_attach(device_t dev)
275 {
276 	struct usb_attach_arg *uaa = device_get_ivars(dev);
277 	struct wmt_softc *sc = device_get_softc(dev);
278 	void *d_ptr;
279 	uint16_t d_len;
280 	size_t i;
281 	int err;
282 
283 	device_set_usb_desc(dev);
284 	sc->dev = dev;
285 
286 	/* Get HID descriptor */
287 	err = usbd_req_get_hid_desc(uaa->device, NULL,
288 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
289 	if (err) {
290 		DPRINTF("usbd_req_get_hid_desc error=%s\n", usbd_errstr(err));
291 		return (ENXIO);
292 	}
293 
294 	mtx_init(&sc->mtx, "wmt lock", NULL, MTX_DEF);
295 
296 	/* Get HID report length */
297 	sc->isize = hid_report_size(d_ptr, d_len, hid_input, NULL);
298 	if (sc->isize <= 0 || sc->isize > WMT_BSIZE) {
299 		DPRINTF("Input size invalid or too large: %d\n", sc->isize);
300 		goto detach;
301 	}
302 
303 	err = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
304 	    sc->xfer, wmt_config, WMT_N_TRANSFER, sc, &sc->mtx);
305 	if (err) {
306 		DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(err));
307 		goto detach;
308 	}
309 
310 	if (!wmt_hid_parse(sc, d_ptr, d_len))
311 		goto detach;
312 
313 	sc->evdev = evdev_alloc();
314 	evdev_set_name(sc->evdev, device_get_desc(dev));
315 	evdev_set_phys(sc->evdev, device_get_nameunit(dev));
316 	evdev_set_id(sc->evdev, BUS_USB, uaa->info.idVendor,
317 	    uaa->info.idProduct, 0);
318 	evdev_set_serial(sc->evdev, usb_get_serial(uaa->device));
319 	evdev_set_methods(sc->evdev, sc, &wmt_evdev_methods);
320 	evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT);
321 	evdev_support_prop(sc->evdev, INPUT_PROP_DIRECT);
322 	evdev_support_event(sc->evdev, EV_SYN);
323 	evdev_support_event(sc->evdev, EV_ABS);
324 	WMT_FOREACH_USAGE(sc->caps, i) {
325 		if (wmt_hid_map[i].code != WMT_NO_CODE)
326 			evdev_support_abs(sc->evdev, wmt_hid_map[i].code, 0,
327 			    sc->ai[i].min, sc->ai[i].max, 0, 0, sc->ai[i].res);
328 	}
329 
330 	err = evdev_register_mtx(sc->evdev, &sc->mtx);
331 	if (err)
332 		goto detach;
333 
334 	return (0);
335 
336 detach:
337 	free(d_ptr, M_TEMP);
338 	wmt_detach(dev);
339 	return (ENXIO);
340 }
341 
342 static int
343 wmt_detach(device_t dev)
344 {
345 	struct wmt_softc *sc = device_get_softc(dev);
346 
347 	evdev_free(sc->evdev);
348 	usbd_transfer_unsetup(sc->xfer, WMT_N_TRANSFER);
349 	mtx_destroy(&sc->mtx);
350 	return (0);
351 }
352 
353 static void
354 wmt_process_report(struct wmt_softc *sc, uint8_t *buf, int len)
355 {
356 	size_t usage;
357 	uint32_t *slot_data = sc->slot_data;
358 	uint32_t cont;
359 	uint32_t nconts;
360 	uint32_t width;
361 	uint32_t height;
362 	int32_t slot;
363 
364 	nconts = hid_get_data_unsigned(buf, len, &sc->nconts_loc);
365 
366 #ifdef USB_DEBUG
367 	DPRINTFN(6, "nconts = %u   ", (unsigned)nconts);
368 	if (wmt_debug >= 6) {
369 		WMT_FOREACH_USAGE(sc->caps, usage) {
370 			if (wmt_hid_map[usage].usage != WMT_NO_USAGE)
371 				printf(" %-4s", wmt_hid_map[usage].name);
372 		}
373 		printf("\n");
374 	}
375 #endif
376 
377 	if (nconts > sc->nconts_max) {
378 		DPRINTF("Contact count overflow %u\n", (unsigned)nconts);
379 		nconts = sc->nconts_max;
380 	}
381 
382 	/* Use protocol Type B for reporting events */
383 	for (cont = 0; cont < nconts; cont++) {
384 
385 		bzero(slot_data, sizeof(sc->slot_data));
386 		WMT_FOREACH_USAGE(sc->caps, usage) {
387 			if (sc->locs[cont][usage].size > 0)
388 				slot_data[usage] = hid_get_data_unsigned(
389 				    buf, len, &sc->locs[cont][usage]);
390 		}
391 
392 		slot = evdev_get_mt_slot_by_tracking_id(sc->evdev,
393 		    slot_data[WMT_CONTACTID]);
394 
395 #ifdef USB_DEBUG
396 		DPRINTFN(6, "cont%01x: data = ", cont);
397 		if (wmt_debug >= 6) {
398 			WMT_FOREACH_USAGE(sc->caps, usage) {
399 				if (wmt_hid_map[usage].usage != WMT_NO_USAGE)
400 					printf("%04x ", slot_data[usage]);
401 			}
402 			printf("slot = %d\n", (int)slot);
403 		}
404 #endif
405 
406 		if (slot == -1) {
407 			DPRINTF("Slot overflow for contact_id %u\n",
408 			    (unsigned)slot_data[WMT_CONTACTID]);
409 			continue;
410 		}
411 
412 		if (slot_data[WMT_TIP_SWITCH] != 0 &&
413 		    !(USAGE_SUPPORTED(sc->caps, WMT_CONFIDENCE) &&
414 		      slot_data[WMT_CONFIDENCE] == 0)) {
415 			/* This finger is in proximity of the sensor */
416 			slot_data[WMT_SLOT] = slot;
417 			slot_data[WMT_IN_RANGE] = !slot_data[WMT_IN_RANGE];
418 			/* Divided by two to match visual scale of touch */
419 			width = slot_data[WMT_WIDTH] >> 1;
420 			height = slot_data[WMT_HEIGHT] >> 1;
421 			slot_data[WMT_ORIENTATION] = width > height;
422 			slot_data[WMT_MAJOR] = MAX(width, height);
423 			slot_data[WMT_MINOR] = MIN(width, height);
424 
425 			WMT_FOREACH_USAGE(sc->caps, usage)
426 				if (wmt_hid_map[usage].code != WMT_NO_CODE)
427 					evdev_push_abs(sc->evdev,
428 					    wmt_hid_map[usage].code,
429 					    slot_data[usage]);
430 		} else {
431 			evdev_push_abs(sc->evdev, ABS_MT_SLOT, slot);
432 			evdev_push_abs(sc->evdev, ABS_MT_TRACKING_ID, -1);
433 		}
434 	}
435 	evdev_sync(sc->evdev);
436 }
437 
438 static void
439 wmt_intr_callback(struct usb_xfer *xfer, usb_error_t error)
440 {
441 	struct wmt_softc *sc = usbd_xfer_softc(xfer);
442 	struct usb_page_cache *pc;
443 	uint8_t *buf = sc->buf;
444 	int len;
445 
446 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
447 
448 	switch (USB_GET_STATE(xfer)) {
449 	case USB_ST_TRANSFERRED:
450 		pc = usbd_xfer_get_frame(xfer, 0);
451 
452 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
453 
454 		if (len >= (int)sc->isize || (len > 0 && sc->report_id != 0)) {
455 			/* Limit report length to the maximum */
456 			if (len > (int)sc->isize)
457 				len = sc->isize;
458 
459 			usbd_copy_out(pc, 0, buf, len);
460 
461 			/* Ignore irrelevant reports */
462 			if (sc->report_id && *buf != sc->report_id)
463 				goto tr_ignore;
464 
465 			/* Make sure we don't process old data */
466 			if (len < sc->isize)
467 				bzero(buf + len, sc->isize - len);
468 
469 			/* Strip leading "report ID" byte */
470 			if (sc->report_id) {
471 				len--;
472 				buf++;
473 			}
474 
475 			wmt_process_report(sc, buf, len);
476 		} else {
477 tr_ignore:
478 			DPRINTF("Ignored transfer, %d bytes\n", len);
479 		}
480 
481 	case USB_ST_SETUP:
482 tr_setup:
483 		usbd_xfer_set_frame_len(xfer, 0, sc->isize);
484 		usbd_transfer_submit(xfer);
485 		break;
486 	default:
487 		if (error != USB_ERR_CANCELLED) {
488 			/* Try clear stall first */
489 			usbd_xfer_set_stall(xfer);
490 			goto tr_setup;
491 		}
492 		break;
493 	}
494 }
495 
496 static void
497 wmt_ev_close(struct evdev_dev *evdev, void *ev_softc)
498 {
499 	struct wmt_softc *sc = (struct wmt_softc *)ev_softc;
500 
501 	mtx_assert(&sc->mtx, MA_OWNED);
502 	usbd_transfer_stop(sc->xfer[WMT_INTR_DT]);
503 }
504 
505 static int
506 wmt_ev_open(struct evdev_dev *evdev, void *ev_softc)
507 {
508 	struct wmt_softc *sc = (struct wmt_softc *)ev_softc;
509 
510 	mtx_assert(&sc->mtx, MA_OWNED);
511 	usbd_transfer_start(sc->xfer[WMT_INTR_DT]);
512 
513 	return (0);
514 }
515 
516 static bool
517 wmt_hid_parse(struct wmt_softc *sc, const void *d_ptr, uint16_t d_len)
518 {
519 	struct hid_item hi;
520 	struct hid_data *hd;
521 	size_t i;
522 	size_t cont = 0;
523 	uint32_t caps = 0;
524 	int32_t cont_count_max = 0;
525 	uint8_t report_id = 0;
526 	bool touch_coll = false;
527 	bool finger_coll = false;
528 	bool cont_count_found = false;
529 	bool scan_time_found = false;
530 
531 #define WMT_HI_ABSOLUTE(hi)	\
532 	(((hi).flags & (HIO_CONST|HIO_VARIABLE|HIO_RELATIVE)) == HIO_VARIABLE)
533 
534 	/* Parse features for maximum contact count */
535 	hd = hid_start_parse(d_ptr, d_len, 1 << hid_feature);
536 	while (hid_get_item(hd, &hi)) {
537 		switch (hi.kind) {
538 		case hid_collection:
539 			if (hi.collevel == 1 && hi.usage ==
540 			    HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN))
541 				touch_coll = true;
542 			break;
543 		case hid_endcollection:
544 			if (hi.collevel == 0 && touch_coll)
545 				touch_coll = false;
546 			break;
547 		case hid_feature:
548 			if (hi.collevel == 1 && touch_coll &&
549 			    WMT_HI_ABSOLUTE(hi) && hi.usage ==
550 			      HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX))
551 				cont_count_max = hi.logical_maximum;
552 			break;
553 		default:
554 			break;
555 		}
556 	}
557 	hid_end_parse(hd);
558 
559 	/* Maximum contact count is required usage */
560 	if (cont_count_max < 1)
561 		return (false);
562 
563 	touch_coll = false;
564 
565 	/* Parse input for other parameters */
566 	hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
567 	while (hid_get_item(hd, &hi)) {
568 		switch (hi.kind) {
569 		case hid_collection:
570 			if (hi.collevel == 1 && hi.usage ==
571 			    HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN))
572 				touch_coll = true;
573 			else if (touch_coll && hi.collevel == 2 &&
574 			    (report_id == 0 || report_id == hi.report_ID) &&
575 			    hi.usage == HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER))
576 				finger_coll = true;
577 			break;
578 		case hid_endcollection:
579 			if (hi.collevel == 1 && finger_coll) {
580 				finger_coll = false;
581 				cont++;
582 			} else if (hi.collevel == 0 && touch_coll)
583 				touch_coll = false;
584 			break;
585 		case hid_input:
586 			/*
587 			 * Ensure that all usages are located within the same
588 			 * report and proper collection.
589 			 */
590 			if (WMT_HI_ABSOLUTE(hi) && touch_coll &&
591 			    (report_id == 0 || report_id == hi.report_ID))
592 				report_id = hi.report_ID;
593 			else
594 				break;
595 
596 			if (hi.collevel == 1 && hi.usage ==
597 			    HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTCOUNT)) {
598 				cont_count_found = true;
599 				if (sc != NULL)
600 					sc->nconts_loc = hi.loc;
601 				break;
602 			}
603 			/* Scan time is required but clobbered by evdev */
604 			if (hi.collevel == 1 && hi.usage ==
605 			    HID_USAGE2(HUP_DIGITIZERS, HUD_SCAN_TIME)) {
606 				scan_time_found = true;
607 				break;
608 			}
609 
610 			if (!finger_coll || hi.collevel != 2)
611 				break;
612 			if (sc == NULL && cont > 0)
613 				break;
614 			if (cont >= MAX_MT_SLOTS) {
615 				DPRINTF("Finger %zu ignored\n", cont);
616 				break;
617 			}
618 
619 			for (i = 0; i < WMT_N_USAGES; i++) {
620 				if (hi.usage == wmt_hid_map[i].usage) {
621 					if (sc == NULL) {
622 						if (USAGE_SUPPORTED(caps, i))
623 							continue;
624 						caps |= 1 << i;
625 						break;
626 					}
627 					/*
628 					 * HUG_X usage is an array mapped to
629 					 * both ABS_MT_POSITION and ABS_MT_TOOL
630 					 * events. So don`t stop search if we
631 					 * already have HUG_X mapping done.
632 					 */
633 					if (sc->locs[cont][i].size)
634 						continue;
635 					sc->locs[cont][i] = hi.loc;
636 					/*
637 					 * Hid parser returns valid logical and
638 					 * physical sizes for first finger only
639 					 * at least on ElanTS 0x04f3:0x0012.
640 					 */
641 					if (cont > 0)
642 						break;
643 					caps |= 1 << i;
644 					sc->ai[i] = (struct wmt_absinfo) {
645 					    .max = hi.logical_maximum,
646 					    .min = hi.logical_minimum,
647 					    .res = hid_item_resolution(&hi),
648 					};
649 					break;
650 				}
651 			}
652 			break;
653 		default:
654 			break;
655 		}
656 	}
657 	hid_end_parse(hd);
658 
659 	/* Check for required HID Usages */
660 	if (!cont_count_found || !scan_time_found || cont == 0)
661 		return (false);
662 	for (i = 0; i < WMT_N_USAGES; i++) {
663 		if (wmt_hid_map[i].required && !USAGE_SUPPORTED(caps, i))
664 			return (false);
665 	}
666 
667 	/* Stop probing here */
668 	if (sc == NULL)
669 		return (true);
670 
671 	/* Cap contact count maximum to MAX_MT_SLOTS */
672 	if (cont_count_max > MAX_MT_SLOTS) {
673 		DPRINTF("Hardware reported %d contacts while only %d is "
674 		    "supported\n", (int)cont_count_max, MAX_MT_SLOTS);
675 		cont_count_max = MAX_MT_SLOTS;
676 	}
677 
678 	/* Set number of MT protocol type B slots */
679 	sc->ai[WMT_SLOT] = (struct wmt_absinfo) {
680 		.min = 0,
681 		.max = cont_count_max - 1,
682 		.res = 0,
683 	};
684 
685 	/* Report touch orientation if both width and height are supported */
686 	if (USAGE_SUPPORTED(caps, WMT_WIDTH) &&
687 	    USAGE_SUPPORTED(caps, WMT_HEIGHT)) {
688 		caps |= (1 << WMT_ORIENTATION);
689 		sc->ai[WMT_ORIENTATION].max = 1;
690 	}
691 
692 	sc->report_id = report_id;
693 	sc->caps = caps;
694 	sc->nconts_max = cont;
695 
696 	/* Announce information about the touch device */
697 	device_printf(sc->dev,
698 	    "%d contacts and [%s%s%s%s%s]. Report range [%d:%d] - [%d:%d]\n",
699 	    (int)cont_count_max,
700 	    USAGE_SUPPORTED(sc->caps, WMT_IN_RANGE) ? "R" : "",
701 	    USAGE_SUPPORTED(sc->caps, WMT_CONFIDENCE) ? "C" : "",
702 	    USAGE_SUPPORTED(sc->caps, WMT_WIDTH) ? "W" : "",
703 	    USAGE_SUPPORTED(sc->caps, WMT_HEIGHT) ? "H" : "",
704 	    USAGE_SUPPORTED(sc->caps, WMT_PRESSURE) ? "P" : "",
705 	    (int)sc->ai[WMT_X].min, (int)sc->ai[WMT_Y].min,
706 	    (int)sc->ai[WMT_X].max, (int)sc->ai[WMT_Y].max);
707 	return (true);
708 }
709 
710 static devclass_t wmt_devclass;
711 
712 static device_method_t wmt_methods[] = {
713 	DEVMETHOD(device_probe, wmt_probe),
714 	DEVMETHOD(device_attach, wmt_attach),
715 	DEVMETHOD(device_detach, wmt_detach),
716 
717 	DEVMETHOD_END
718 };
719 
720 static driver_t wmt_driver = {
721 	.name = "wmt",
722 	.methods = wmt_methods,
723 	.size = sizeof(struct wmt_softc),
724 };
725 
726 DRIVER_MODULE(wmt, uhub, wmt_driver, wmt_devclass, NULL, 0);
727 MODULE_DEPEND(wmt, usb, 1, 1, 1);
728 MODULE_DEPEND(wmt, evdev, 1, 1, 1);
729 MODULE_VERSION(wmt, 1);
730