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