xref: /linux/drivers/hid/hid-multitouch.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  HID driver for multitouch panels
4  *
5  *  Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
6  *  Copyright (c) 2010-2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
7  *  Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
8  *  Copyright (c) 2012-2013 Red Hat, Inc
9  *
10  *  This code is partly based on hid-egalax.c:
11  *
12  *  Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
13  *  Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
14  *  Copyright (c) 2010 Canonical, Ltd.
15  *
16  *  This code is partly based on hid-3m-pct.c:
17  *
18  *  Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
19  *  Copyright (c) 2010      Henrik Rydberg <rydberg@euromail.se>
20  *  Copyright (c) 2010      Canonical, Ltd.
21  */
22 
23 /*
24  */
25 
26 /*
27  * This driver is regularly tested thanks to the test suite in hid-tools[1].
28  * Please run these regression tests before patching this module so that
29  * your patch won't break existing known devices.
30  *
31  * [1] https://gitlab.freedesktop.org/libevdev/hid-tools
32  */
33 
34 #include <linux/bitmap.h>
35 #include <linux/bits.h>
36 #include <linux/device.h>
37 #include <linux/hid.h>
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/input/mt.h>
41 #include <linux/jiffies.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 
45 
46 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
47 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
48 MODULE_DESCRIPTION("HID multitouch panels");
49 MODULE_LICENSE("GPL");
50 
51 #include "hid-ids.h"
52 
53 #include "hid-haptic.h"
54 
55 /* quirks to control the device */
56 #define MT_QUIRK_NOT_SEEN_MEANS_UP	BIT(0)
57 #define MT_QUIRK_SLOT_IS_CONTACTID	BIT(1)
58 #define MT_QUIRK_CYPRESS		BIT(2)
59 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER	BIT(3)
60 #define MT_QUIRK_ALWAYS_VALID		BIT(4)
61 #define MT_QUIRK_VALID_IS_INRANGE	BIT(5)
62 #define MT_QUIRK_VALID_IS_CONFIDENCE	BIT(6)
63 #define MT_QUIRK_CONFIDENCE		BIT(7)
64 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE	BIT(8)
65 #define MT_QUIRK_NO_AREA		BIT(9)
66 #define MT_QUIRK_IGNORE_DUPLICATES	BIT(10)
67 #define MT_QUIRK_HOVERING		BIT(11)
68 #define MT_QUIRK_CONTACT_CNT_ACCURATE	BIT(12)
69 #define MT_QUIRK_FORCE_GET_FEATURE	BIT(13)
70 #define MT_QUIRK_FIX_CONST_CONTACT_ID	BIT(14)
71 #define MT_QUIRK_TOUCH_SIZE_SCALING	BIT(15)
72 #define MT_QUIRK_STICKY_FINGERS		BIT(16)
73 #define MT_QUIRK_ASUS_CUSTOM_UP		BIT(17)
74 #define MT_QUIRK_WIN8_PTP_BUTTONS	BIT(18)
75 #define MT_QUIRK_SEPARATE_APP_REPORT	BIT(19)
76 #define MT_QUIRK_FORCE_MULTI_INPUT	BIT(20)
77 #define MT_QUIRK_DISABLE_WAKEUP		BIT(21)
78 #define MT_QUIRK_ORIENTATION_INVERT	BIT(22)
79 #define MT_QUIRK_APPLE_TOUCHBAR		BIT(23)
80 #define MT_QUIRK_YOGABOOK9I		BIT(24)
81 #define MT_QUIRK_KEEP_LATENCY_ON_CLOSE	BIT(25)
82 #define MT_QUIRK_IGNORE_FEATURE_ID_MISMATCH	BIT(26)
83 
84 #define MT_INPUTMODE_TOUCHSCREEN	0x02
85 #define MT_INPUTMODE_TOUCHPAD		0x03
86 
87 #define MT_BUTTONTYPE_CLICKPAD		0
88 #define MT_BUTTONTYPE_PRESSUREPAD	1
89 
90 enum latency_mode {
91 	HID_LATENCY_NORMAL = 0,
92 	HID_LATENCY_HIGH = 1,
93 };
94 
95 enum report_mode {
96 	TOUCHPAD_REPORT_NONE = 0,
97 	TOUCHPAD_REPORT_BUTTONS = BIT(0),
98 	TOUCHPAD_REPORT_CONTACTS = BIT(1),
99 	TOUCHPAD_REPORT_ALL = TOUCHPAD_REPORT_BUTTONS | TOUCHPAD_REPORT_CONTACTS,
100 };
101 
102 #define MT_IO_FLAGS_RUNNING		0
103 
104 static const bool mtrue = true;		/* default for true */
105 static const bool mfalse;		/* default for false */
106 static const __s32 mzero;		/* default for 0 */
107 
108 #define DEFAULT_TRUE	((void *)&mtrue)
109 #define DEFAULT_FALSE	((void *)&mfalse)
110 #define DEFAULT_ZERO	((void *)&mzero)
111 
112 struct mt_usages {
113 	struct list_head list;
114 	__s32 *x, *y, *cx, *cy, *p, *w, *h, *a;
115 	__s32 *contactid;	/* the device ContactID assigned to this slot */
116 	bool *tip_state;	/* is the touch valid? */
117 	bool *inrange_state;	/* is the finger in proximity of the sensor? */
118 	bool *confidence_state;	/* is the touch made by a finger? */
119 };
120 
121 struct mt_application {
122 	struct list_head list;
123 	unsigned int application;
124 	unsigned int report_id;
125 	struct list_head mt_usages;	/* mt usages list */
126 
127 	__s32 quirks;
128 
129 	__s32 *scantime;		/* scantime reported */
130 	__s32 scantime_logical_max;	/* max value for raw scantime */
131 
132 	__s32 *raw_cc;			/* contact count in the report */
133 	int left_button_state;		/* left button state */
134 	unsigned int mt_flags;		/* flags to pass to input-mt */
135 
136 	unsigned long *pending_palm_slots;	/* slots where we reported palm
137 						 * and need to release */
138 
139 	__u8 num_received;	/* how many contacts we received */
140 	__u8 num_expected;	/* expected last contact index */
141 	__u8 buttons_count;	/* number of physical buttons per touchpad */
142 	__u8 touches_by_report;	/* how many touches are present in one report:
143 				 * 1 means we should use a serial protocol
144 				 * > 1 means hybrid (multitouch) protocol
145 				 */
146 
147 	unsigned long jiffies;	/* the frame's jiffies */
148 	int timestamp;		/* the timestamp to be sent */
149 	int prev_scantime;		/* scantime reported previously */
150 
151 	bool have_contact_count;
152 };
153 
154 struct mt_class {
155 	__s32 name;	/* MT_CLS */
156 	__s32 quirks;
157 	__s32 sn_move;	/* Signal/noise ratio for move events */
158 	__s32 sn_width;	/* Signal/noise ratio for width events */
159 	__s32 sn_height;	/* Signal/noise ratio for height events */
160 	__s32 sn_pressure;	/* Signal/noise ratio for pressure events */
161 	__u8 maxcontacts;
162 	bool is_indirect;	/* true for touchpads */
163 	bool export_all_inputs;	/* do not ignore mouse, keyboards, etc... */
164 };
165 
166 struct mt_report_data {
167 	struct list_head list;
168 	struct hid_report *report;
169 	struct mt_application *application;
170 	bool is_mt_collection;
171 };
172 
173 struct mt_device {
174 	struct mt_class mtclass;	/* our mt device class */
175 	struct timer_list release_timer;	/* to release sticky fingers */
176 	struct hid_haptic_device *haptic;	/* haptic related configuration */
177 	struct hid_device *hdev;	/* hid_device we're attached to */
178 	unsigned long mt_io_flags;	/* mt flags (MT_IO_FLAGS_RUNNING) */
179 	unsigned long *active_slots;	/* bitmap of slots with an active
180 					 * contact, sized for maxcontacts
181 					 */
182 	__u8 inputmode_value;	/* InputMode HID feature value */
183 	__u8 maxcontacts;
184 	bool is_buttonpad;	/* is this device a button pad? */
185 	bool is_pressurepad;	/* is this device a pressurepad? */
186 	bool is_haptic_touchpad;	/* is this device a haptic touchpad? */
187 	bool serial_maybe;	/* need to check for serial protocol */
188 
189 	struct list_head applications;
190 	struct list_head reports;
191 };
192 
193 static void mt_post_parse_default_settings(struct mt_device *td,
194 					   struct mt_application *app);
195 static void mt_post_parse(struct mt_device *td, struct mt_application *app);
196 
197 /* classes of device behavior */
198 #define MT_CLS_DEFAULT				0x0001
199 
200 #define MT_CLS_SERIAL				0x0002
201 #define MT_CLS_CONFIDENCE			0x0003
202 #define MT_CLS_CONFIDENCE_CONTACT_ID		0x0004
203 #define MT_CLS_CONFIDENCE_MINUS_ONE		0x0005
204 #define MT_CLS_DUAL_INRANGE_CONTACTID		0x0006
205 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER	0x0007
206 /* reserved					0x0008 */
207 #define MT_CLS_INRANGE_CONTACTNUMBER		0x0009
208 #define MT_CLS_NSMU				0x000a
209 /* reserved					0x0010 */
210 /* reserved					0x0011 */
211 #define MT_CLS_WIN_8				0x0012
212 #define MT_CLS_EXPORT_ALL_INPUTS		0x0013
213 /* reserved					0x0014 */
214 #define MT_CLS_WIN_8_FORCE_MULTI_INPUT		0x0015
215 #define MT_CLS_WIN_8_DISABLE_WAKEUP		0x0016
216 #define MT_CLS_WIN_8_NO_STICKY_FINGERS		0x0017
217 #define MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU	0x0018
218 #define MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE	0x0019
219 
220 /* vendor specific classes */
221 #define MT_CLS_3M				0x0101
222 /* reserved					0x0102 */
223 #define MT_CLS_EGALAX				0x0103
224 #define MT_CLS_EGALAX_SERIAL			0x0104
225 #define MT_CLS_TOPSEED				0x0105
226 #define MT_CLS_PANASONIC			0x0106
227 #define MT_CLS_FLATFROG				0x0107
228 #define MT_CLS_GENERALTOUCH_TWOFINGERS		0x0108
229 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS	0x0109
230 #define MT_CLS_LG				0x010a
231 #define MT_CLS_ASUS				0x010b
232 #define MT_CLS_VTL				0x0110
233 #define MT_CLS_GOOGLE				0x0111
234 #define MT_CLS_RAZER_BLADE_STEALTH		0x0112
235 #define MT_CLS_SMART_TECH			0x0113
236 #define MT_CLS_APPLE_TOUCHBAR			0x0114
237 #define MT_CLS_YOGABOOK9I			0x0115
238 #define MT_CLS_EGALAX_P80H84			0x0116
239 #define MT_CLS_ASUS_ROG_Z13_FOLIO		0x0117
240 #define MT_CLS_SIS				0x0457
241 
242 #define MT_DEFAULT_MAXCONTACT	10
243 #define MT_MAX_MAXCONTACT	250
244 
245 /*
246  * Resync device and local timestamps after that many microseconds without
247  * receiving data.
248  */
249 #define MAX_TIMESTAMP_INTERVAL	1000000
250 
251 #define MT_USB_DEVICE(v, p)	HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
252 #define MT_BT_DEVICE(v, p)	HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
253 
254 /*
255  * these device-dependent functions determine what slot corresponds
256  * to a valid contact that was just read.
257  */
258 
259 static int cypress_compute_slot(struct mt_application *application,
260 				struct mt_usages *slot)
261 {
262 	if (*slot->contactid != 0 || application->num_received == 0)
263 		return *slot->contactid;
264 	else
265 		return -1;
266 }
267 
268 static const struct mt_class mt_classes[] = {
269 	{ .name = MT_CLS_DEFAULT,
270 		.quirks = MT_QUIRK_ALWAYS_VALID |
271 			MT_QUIRK_CONTACT_CNT_ACCURATE },
272 	{ .name = MT_CLS_NSMU,
273 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
274 	{ .name = MT_CLS_SERIAL,
275 		.quirks = MT_QUIRK_ALWAYS_VALID},
276 	{ .name = MT_CLS_CONFIDENCE,
277 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
278 	{ .name = MT_CLS_CONFIDENCE_CONTACT_ID,
279 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
280 			MT_QUIRK_SLOT_IS_CONTACTID },
281 	{ .name = MT_CLS_CONFIDENCE_MINUS_ONE,
282 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
283 			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
284 	{ .name = MT_CLS_DUAL_INRANGE_CONTACTID,
285 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
286 			MT_QUIRK_SLOT_IS_CONTACTID,
287 		.maxcontacts = 2 },
288 	{ .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
289 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
290 			MT_QUIRK_SLOT_IS_CONTACTNUMBER,
291 		.maxcontacts = 2 },
292 	{ .name = MT_CLS_INRANGE_CONTACTNUMBER,
293 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
294 			MT_QUIRK_SLOT_IS_CONTACTNUMBER },
295 	{ .name = MT_CLS_WIN_8,
296 		.quirks = MT_QUIRK_ALWAYS_VALID |
297 			MT_QUIRK_IGNORE_DUPLICATES |
298 			MT_QUIRK_HOVERING |
299 			MT_QUIRK_CONTACT_CNT_ACCURATE |
300 			MT_QUIRK_STICKY_FINGERS |
301 			MT_QUIRK_WIN8_PTP_BUTTONS,
302 		.export_all_inputs = true },
303 	{ .name = MT_CLS_EXPORT_ALL_INPUTS,
304 		.quirks = MT_QUIRK_ALWAYS_VALID |
305 			MT_QUIRK_CONTACT_CNT_ACCURATE,
306 		.export_all_inputs = true },
307 	{ .name = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
308 		.quirks = MT_QUIRK_ALWAYS_VALID |
309 			MT_QUIRK_IGNORE_DUPLICATES |
310 			MT_QUIRK_HOVERING |
311 			MT_QUIRK_CONTACT_CNT_ACCURATE |
312 			MT_QUIRK_STICKY_FINGERS |
313 			MT_QUIRK_WIN8_PTP_BUTTONS |
314 			MT_QUIRK_FORCE_MULTI_INPUT,
315 		.export_all_inputs = true },
316 	{ .name = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
317 		.quirks = MT_QUIRK_IGNORE_DUPLICATES |
318 			MT_QUIRK_HOVERING |
319 			MT_QUIRK_CONTACT_CNT_ACCURATE |
320 			MT_QUIRK_STICKY_FINGERS |
321 			MT_QUIRK_WIN8_PTP_BUTTONS |
322 			MT_QUIRK_FORCE_MULTI_INPUT |
323 			MT_QUIRK_NOT_SEEN_MEANS_UP,
324 		.export_all_inputs = true },
325 	{ .name = MT_CLS_WIN_8_DISABLE_WAKEUP,
326 		.quirks = MT_QUIRK_ALWAYS_VALID |
327 			MT_QUIRK_IGNORE_DUPLICATES |
328 			MT_QUIRK_HOVERING |
329 			MT_QUIRK_CONTACT_CNT_ACCURATE |
330 			MT_QUIRK_STICKY_FINGERS |
331 			MT_QUIRK_WIN8_PTP_BUTTONS |
332 			MT_QUIRK_DISABLE_WAKEUP,
333 		.export_all_inputs = true },
334 	{ .name = MT_CLS_WIN_8_NO_STICKY_FINGERS,
335 		.quirks = MT_QUIRK_ALWAYS_VALID |
336 			MT_QUIRK_IGNORE_DUPLICATES |
337 			MT_QUIRK_HOVERING |
338 			MT_QUIRK_CONTACT_CNT_ACCURATE |
339 			MT_QUIRK_WIN8_PTP_BUTTONS,
340 		.export_all_inputs = true },
341 	{ .name = MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE,
342 		.quirks = MT_QUIRK_ALWAYS_VALID |
343 			MT_QUIRK_IGNORE_DUPLICATES |
344 			MT_QUIRK_HOVERING |
345 			MT_QUIRK_CONTACT_CNT_ACCURATE |
346 			MT_QUIRK_STICKY_FINGERS |
347 			MT_QUIRK_WIN8_PTP_BUTTONS |
348 			MT_QUIRK_KEEP_LATENCY_ON_CLOSE,
349 		.export_all_inputs = true },
350 
351 	/*
352 	 * vendor specific classes
353 	 */
354 	{ .name = MT_CLS_3M,
355 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
356 			MT_QUIRK_SLOT_IS_CONTACTID |
357 			MT_QUIRK_TOUCH_SIZE_SCALING,
358 		.sn_move = 2048,
359 		.sn_width = 128,
360 		.sn_height = 128,
361 		.maxcontacts = 60,
362 	},
363 	{ .name = MT_CLS_EGALAX,
364 		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
365 			MT_QUIRK_VALID_IS_INRANGE,
366 		.sn_move = 4096,
367 		.sn_pressure = 32,
368 	},
369 	{ .name = MT_CLS_EGALAX_SERIAL,
370 		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
371 			MT_QUIRK_ALWAYS_VALID,
372 		.sn_move = 4096,
373 		.sn_pressure = 32,
374 	},
375 	{ .name = MT_CLS_TOPSEED,
376 		.quirks = MT_QUIRK_ALWAYS_VALID,
377 		.is_indirect = true,
378 		.maxcontacts = 2,
379 	},
380 	{ .name = MT_CLS_PANASONIC,
381 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
382 		.maxcontacts = 4 },
383 	{ .name	= MT_CLS_GENERALTOUCH_TWOFINGERS,
384 		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
385 			MT_QUIRK_VALID_IS_INRANGE |
386 			MT_QUIRK_SLOT_IS_CONTACTID,
387 		.maxcontacts = 2
388 	},
389 	{ .name	= MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
390 		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
391 			MT_QUIRK_SLOT_IS_CONTACTID
392 	},
393 
394 	{ .name = MT_CLS_FLATFROG,
395 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
396 			MT_QUIRK_NO_AREA,
397 		.sn_move = 2048,
398 		.maxcontacts = 40,
399 	},
400 	{ .name = MT_CLS_LG,
401 		.quirks = MT_QUIRK_ALWAYS_VALID |
402 			MT_QUIRK_FIX_CONST_CONTACT_ID |
403 			MT_QUIRK_IGNORE_DUPLICATES |
404 			MT_QUIRK_HOVERING |
405 			MT_QUIRK_CONTACT_CNT_ACCURATE },
406 	{ .name = MT_CLS_ASUS,
407 		.quirks = MT_QUIRK_ALWAYS_VALID |
408 			MT_QUIRK_CONTACT_CNT_ACCURATE |
409 			MT_QUIRK_ASUS_CUSTOM_UP },
410 	{ .name = MT_CLS_ASUS_ROG_Z13_FOLIO,
411 		.quirks = MT_QUIRK_ALWAYS_VALID |
412 			MT_QUIRK_IGNORE_DUPLICATES |
413 			MT_QUIRK_HOVERING |
414 			MT_QUIRK_CONTACT_CNT_ACCURATE |
415 			MT_QUIRK_STICKY_FINGERS |
416 			MT_QUIRK_WIN8_PTP_BUTTONS |
417 			MT_QUIRK_CONFIDENCE |
418 			MT_QUIRK_IGNORE_FEATURE_ID_MISMATCH,
419 		.export_all_inputs = true },
420 	{ .name = MT_CLS_VTL,
421 		.quirks = MT_QUIRK_ALWAYS_VALID |
422 			MT_QUIRK_CONTACT_CNT_ACCURATE |
423 			MT_QUIRK_STICKY_FINGERS |
424 			MT_QUIRK_FORCE_GET_FEATURE,
425 	},
426 	{ .name = MT_CLS_GOOGLE,
427 		.quirks = MT_QUIRK_ALWAYS_VALID |
428 			MT_QUIRK_CONTACT_CNT_ACCURATE |
429 			MT_QUIRK_SLOT_IS_CONTACTID |
430 			MT_QUIRK_HOVERING
431 	},
432 	{ .name = MT_CLS_RAZER_BLADE_STEALTH,
433 		.quirks = MT_QUIRK_ALWAYS_VALID |
434 			MT_QUIRK_IGNORE_DUPLICATES |
435 			MT_QUIRK_HOVERING |
436 			MT_QUIRK_CONTACT_CNT_ACCURATE |
437 			MT_QUIRK_WIN8_PTP_BUTTONS,
438 	},
439 	{ .name = MT_CLS_SMART_TECH,
440 		.quirks = MT_QUIRK_ALWAYS_VALID |
441 			MT_QUIRK_IGNORE_DUPLICATES |
442 			MT_QUIRK_CONTACT_CNT_ACCURATE |
443 			MT_QUIRK_SEPARATE_APP_REPORT,
444 	},
445 	{ .name = MT_CLS_APPLE_TOUCHBAR,
446 		.quirks = MT_QUIRK_HOVERING |
447 			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE |
448 			MT_QUIRK_APPLE_TOUCHBAR,
449 		.maxcontacts = 11,
450 	},
451 	{ .name = MT_CLS_SIS,
452 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
453 			MT_QUIRK_ALWAYS_VALID |
454 			MT_QUIRK_CONTACT_CNT_ACCURATE,
455 	},
456 		{ .name = MT_CLS_YOGABOOK9I,
457 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
458 			MT_QUIRK_ALWAYS_VALID |
459 			MT_QUIRK_CONTACT_CNT_ACCURATE |
460 			MT_QUIRK_FORCE_MULTI_INPUT |
461 			MT_QUIRK_SEPARATE_APP_REPORT |
462 			MT_QUIRK_HOVERING |
463 			MT_QUIRK_YOGABOOK9I,
464 		.maxcontacts = 10,
465 		.export_all_inputs = true
466 	},
467 	{ .name = MT_CLS_EGALAX_P80H84,
468 		.quirks = MT_QUIRK_ALWAYS_VALID |
469 			MT_QUIRK_IGNORE_DUPLICATES |
470 			MT_QUIRK_CONTACT_CNT_ACCURATE,
471 	},
472 	{ }
473 };
474 
475 static ssize_t mt_show_quirks(struct device *dev,
476 			   struct device_attribute *attr,
477 			   char *buf)
478 {
479 	struct hid_device *hdev = to_hid_device(dev);
480 	struct mt_device *td = hid_get_drvdata(hdev);
481 
482 	return sprintf(buf, "%u\n", td->mtclass.quirks);
483 }
484 
485 static ssize_t mt_set_quirks(struct device *dev,
486 			  struct device_attribute *attr,
487 			  const char *buf, size_t count)
488 {
489 	struct hid_device *hdev = to_hid_device(dev);
490 	struct mt_device *td = hid_get_drvdata(hdev);
491 	struct mt_application *application;
492 
493 	unsigned long val;
494 
495 	if (kstrtoul(buf, 0, &val))
496 		return -EINVAL;
497 
498 	td->mtclass.quirks = val;
499 
500 	list_for_each_entry(application, &td->applications, list) {
501 		application->quirks = val;
502 		if (!application->have_contact_count)
503 			application->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
504 	}
505 
506 	return count;
507 }
508 
509 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
510 
511 static struct attribute *sysfs_attrs[] = {
512 	&dev_attr_quirks.attr,
513 	NULL
514 };
515 
516 static const struct attribute_group mt_attribute_group = {
517 	.attrs = sysfs_attrs
518 };
519 
520 static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
521 {
522 	struct mt_device *td = hid_get_drvdata(hdev);
523 	int ret;
524 	u32 size = hid_report_len(report);
525 	u8 *buf;
526 
527 	/*
528 	 * Do not fetch the feature report if the device has been explicitly
529 	 * marked as non-capable.
530 	 */
531 	if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
532 		return;
533 
534 	buf = hid_alloc_report_buf(report, GFP_KERNEL);
535 	if (!buf)
536 		return;
537 
538 	ret = hid_hw_raw_request(hdev, report->id, buf, size,
539 				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
540 	if (ret < 0) {
541 		dev_warn(&hdev->dev, "failed to fetch feature %d\n",
542 			 report->id);
543 	} else {
544 		/*
545 		 * The report ID in the request and the response should match.
546 		 * Some firmware (e.g. the ASUS ROG Z13 Folio
547 		 * touchpad) returns a mismatched ID on this specific fetch;
548 		 * tolerate it only for devices explicitly flagged as such.
549 		 */
550 		if (report->id != buf[0] &&
551 		    !(td->mtclass.quirks & MT_QUIRK_IGNORE_FEATURE_ID_MISMATCH)) {
552 			hid_err(hdev, "Returned feature report did not match the request\n");
553 			goto free;
554 		}
555 
556 		ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
557 					   size, size, 0);
558 		if (ret)
559 			dev_warn(&hdev->dev, "failed to report feature\n");
560 	}
561 
562 free:
563 	kfree(buf);
564 }
565 
566 static void mt_feature_mapping(struct hid_device *hdev,
567 		struct hid_field *field, struct hid_usage *usage)
568 {
569 	struct mt_device *td = hid_get_drvdata(hdev);
570 
571 	switch (usage->hid) {
572 	case HID_DG_CONTACTMAX:
573 		mt_get_feature(hdev, field->report);
574 
575 		td->maxcontacts = field->value[0];
576 		if (!td->maxcontacts &&
577 		    field->logical_maximum <= MT_MAX_MAXCONTACT)
578 			td->maxcontacts = field->logical_maximum;
579 		if (td->mtclass.maxcontacts)
580 			/* check if the maxcontacts is given by the class */
581 			td->maxcontacts = td->mtclass.maxcontacts;
582 
583 		break;
584 	case HID_DG_BUTTONTYPE:
585 		if (usage->usage_index >= field->report_count) {
586 			dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
587 			break;
588 		}
589 
590 		mt_get_feature(hdev, field->report);
591 		switch (field->value[usage->usage_index]) {
592 		case MT_BUTTONTYPE_CLICKPAD:
593 			td->is_buttonpad = true;
594 			break;
595 		case MT_BUTTONTYPE_PRESSUREPAD:
596 			td->is_pressurepad = true;
597 			break;
598 		}
599 
600 		break;
601 	case 0xff0000c5:
602 		/* Retrieve the Win8 blob once to enable some devices */
603 		if (usage->usage_index == 0)
604 			mt_get_feature(hdev, field->report);
605 		break;
606 	}
607 
608 	hid_haptic_feature_mapping(hdev, td->haptic, field, usage);
609 }
610 
611 static void set_abs(struct input_dev *input, unsigned int code,
612 		struct hid_field *field, int snratio)
613 {
614 	int fmin = field->logical_minimum;
615 	int fmax = field->logical_maximum;
616 	int fuzz = snratio ? (fmax - fmin) / snratio : 0;
617 	input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
618 	input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
619 }
620 
621 static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,
622 					   struct mt_application *application)
623 {
624 	struct mt_usages *usage;
625 
626 	usage = devm_kzalloc_obj(&hdev->dev, *usage);
627 	if (!usage)
628 		return NULL;
629 
630 	/* set some defaults so we do not need to check for null pointers */
631 	usage->x = DEFAULT_ZERO;
632 	usage->y = DEFAULT_ZERO;
633 	usage->cx = DEFAULT_ZERO;
634 	usage->cy = DEFAULT_ZERO;
635 	usage->p = DEFAULT_ZERO;
636 	usage->w = DEFAULT_ZERO;
637 	usage->h = DEFAULT_ZERO;
638 	usage->a = DEFAULT_ZERO;
639 	usage->contactid = DEFAULT_ZERO;
640 	usage->tip_state = DEFAULT_FALSE;
641 	usage->inrange_state = DEFAULT_FALSE;
642 	usage->confidence_state = DEFAULT_TRUE;
643 
644 	list_add_tail(&usage->list, &application->mt_usages);
645 
646 	return usage;
647 }
648 
649 static struct mt_application *mt_allocate_application(struct mt_device *td,
650 						      struct hid_report *report)
651 {
652 	unsigned int application = report->application;
653 	struct mt_application *mt_application;
654 
655 	mt_application = devm_kzalloc_obj(&td->hdev->dev, *mt_application);
656 	if (!mt_application)
657 		return NULL;
658 
659 	mt_application->application = application;
660 	INIT_LIST_HEAD(&mt_application->mt_usages);
661 
662 	if (application == HID_DG_TOUCHSCREEN)
663 		mt_application->mt_flags |= INPUT_MT_DIRECT;
664 
665 	/*
666 	 * Model touchscreens providing buttons as touchpads.
667 	 */
668 	if (application == HID_DG_TOUCHPAD) {
669 		mt_application->mt_flags |= INPUT_MT_POINTER;
670 		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
671 	}
672 
673 	mt_application->scantime = DEFAULT_ZERO;
674 	mt_application->raw_cc = DEFAULT_ZERO;
675 	mt_application->quirks = td->mtclass.quirks;
676 	mt_application->report_id = report->id;
677 
678 	list_add_tail(&mt_application->list, &td->applications);
679 
680 	return mt_application;
681 }
682 
683 static struct mt_application *mt_find_application(struct mt_device *td,
684 						  struct hid_report *report)
685 {
686 	unsigned int application = report->application;
687 	struct mt_application *tmp, *mt_application = NULL;
688 
689 	list_for_each_entry(tmp, &td->applications, list) {
690 		if (application == tmp->application) {
691 			if (!(td->mtclass.quirks & MT_QUIRK_SEPARATE_APP_REPORT) ||
692 			    tmp->report_id == report->id) {
693 				mt_application = tmp;
694 				break;
695 			}
696 		}
697 	}
698 
699 	if (!mt_application)
700 		mt_application = mt_allocate_application(td, report);
701 
702 	return mt_application;
703 }
704 
705 static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,
706 						      struct hid_report *report)
707 {
708 	struct mt_class *cls = &td->mtclass;
709 	struct mt_report_data *rdata;
710 	struct hid_field *field;
711 	int r, n;
712 
713 	rdata = devm_kzalloc_obj(&td->hdev->dev, *rdata);
714 	if (!rdata)
715 		return NULL;
716 
717 	rdata->report = report;
718 	rdata->application = mt_find_application(td, report);
719 
720 	if (!rdata->application) {
721 		devm_kfree(&td->hdev->dev, rdata);
722 		return NULL;
723 	}
724 
725 	for (r = 0; r < report->maxfield; r++) {
726 		field = report->field[r];
727 
728 		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
729 			continue;
730 
731 		if (field->logical == HID_DG_FINGER || td->hdev->group != HID_GROUP_MULTITOUCH_WIN_8) {
732 			for (n = 0; n < field->report_count; n++) {
733 				unsigned int hid = field->usage[n].hid;
734 
735 				if (hid == HID_DG_CONTACTID ||
736 				   (cls->quirks & MT_QUIRK_APPLE_TOUCHBAR &&
737 				   hid == HID_DG_TRANSDUCER_INDEX)) {
738 					rdata->is_mt_collection = true;
739 					break;
740 				}
741 			}
742 		}
743 	}
744 
745 	list_add_tail(&rdata->list, &td->reports);
746 
747 	return rdata;
748 }
749 
750 static struct mt_report_data *mt_find_report_data(struct mt_device *td,
751 						  struct hid_report *report)
752 {
753 	struct mt_report_data *tmp, *rdata = NULL;
754 
755 	list_for_each_entry(tmp, &td->reports, list) {
756 		if (report == tmp->report) {
757 			rdata = tmp;
758 			break;
759 		}
760 	}
761 
762 	if (!rdata)
763 		rdata = mt_allocate_report_data(td, report);
764 
765 	return rdata;
766 }
767 
768 static void mt_store_field(struct hid_device *hdev,
769 			   struct mt_application *application,
770 			   __s32 *value,
771 			   size_t offset)
772 {
773 	struct mt_usages *usage;
774 	__s32 **target;
775 
776 	if (list_empty(&application->mt_usages))
777 		usage = mt_allocate_usage(hdev, application);
778 	else
779 		usage = list_last_entry(&application->mt_usages,
780 					struct mt_usages,
781 					list);
782 
783 	if (!usage)
784 		return;
785 
786 	target = (__s32 **)((char *)usage + offset);
787 
788 	/* the value has already been filled, create a new slot */
789 	if (*target != DEFAULT_TRUE &&
790 	    *target != DEFAULT_FALSE &&
791 	    *target != DEFAULT_ZERO) {
792 		if (usage->contactid == DEFAULT_ZERO ||
793 		    usage->x == DEFAULT_ZERO ||
794 		    usage->y == DEFAULT_ZERO) {
795 			hid_dbg(hdev,
796 				"ignoring duplicate usage on incomplete");
797 			return;
798 		}
799 		usage = mt_allocate_usage(hdev, application);
800 		if (!usage)
801 			return;
802 
803 		target = (__s32 **)((char *)usage + offset);
804 	}
805 
806 	*target = value;
807 }
808 
809 #define MT_STORE_FIELD(__name)						\
810 	mt_store_field(hdev, app,					\
811 		       &field->value[usage->usage_index],		\
812 		       offsetof(struct mt_usages, __name))
813 
814 static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
815 		struct hid_field *field, struct hid_usage *usage,
816 		unsigned long **bit, int *max, struct mt_application *app)
817 {
818 	struct mt_device *td = hid_get_drvdata(hdev);
819 	struct mt_class *cls = &td->mtclass;
820 	int code;
821 	struct hid_usage *prev_usage = NULL;
822 
823 	/*
824 	 * Model touchscreens providing buttons as touchpads.
825 	 */
826 	if (field->application == HID_DG_TOUCHSCREEN &&
827 	    (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
828 		app->mt_flags |= INPUT_MT_POINTER;
829 		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
830 	}
831 
832 	/* count the buttons on touchpads */
833 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
834 		app->buttons_count++;
835 
836 	if (usage->usage_index)
837 		prev_usage = &field->usage[usage->usage_index - 1];
838 
839 	switch (usage->hid & HID_USAGE_PAGE) {
840 
841 	case HID_UP_GENDESK:
842 		switch (usage->hid) {
843 		case HID_GD_X:
844 			if (prev_usage && (prev_usage->hid == usage->hid)) {
845 				code = ABS_MT_TOOL_X;
846 				MT_STORE_FIELD(cx);
847 			} else {
848 				code = ABS_MT_POSITION_X;
849 				MT_STORE_FIELD(x);
850 			}
851 
852 			set_abs(hi->input, code, field, cls->sn_move);
853 
854 			/*
855 			 * A system multi-axis that exports X and Y has a high
856 			 * chance of being used directly on a surface
857 			 */
858 			if (field->application == HID_GD_SYSTEM_MULTIAXIS) {
859 				__set_bit(INPUT_PROP_DIRECT,
860 					  hi->input->propbit);
861 				input_set_abs_params(hi->input,
862 						     ABS_MT_TOOL_TYPE,
863 						     MT_TOOL_DIAL,
864 						     MT_TOOL_DIAL, 0, 0);
865 			}
866 
867 			return 1;
868 		case HID_GD_Y:
869 			if (prev_usage && (prev_usage->hid == usage->hid)) {
870 				code = ABS_MT_TOOL_Y;
871 				MT_STORE_FIELD(cy);
872 			} else {
873 				code = ABS_MT_POSITION_Y;
874 				MT_STORE_FIELD(y);
875 			}
876 
877 			set_abs(hi->input, code, field, cls->sn_move);
878 
879 			return 1;
880 		}
881 		return 0;
882 
883 	case HID_UP_DIGITIZER:
884 		switch (usage->hid) {
885 		case HID_DG_INRANGE:
886 			if (app->quirks & MT_QUIRK_HOVERING) {
887 				input_set_abs_params(hi->input,
888 					ABS_MT_DISTANCE, 0, 1, 0, 0);
889 			}
890 			MT_STORE_FIELD(inrange_state);
891 			return 1;
892 		case HID_DG_CONFIDENCE:
893 			if ((cls->name == MT_CLS_WIN_8 ||
894 			     cls->name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
895 			     cls->name == MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU ||
896 			     cls->name == MT_CLS_WIN_8_DISABLE_WAKEUP ||
897 			     cls->name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE) &&
898 				(field->application == HID_DG_TOUCHPAD ||
899 				 field->application == HID_DG_TOUCHSCREEN))
900 				app->quirks |= MT_QUIRK_CONFIDENCE;
901 
902 			if (app->quirks & MT_QUIRK_CONFIDENCE)
903 				input_set_abs_params(hi->input,
904 						     ABS_MT_TOOL_TYPE,
905 						     MT_TOOL_FINGER,
906 						     MT_TOOL_PALM, 0, 0);
907 
908 			MT_STORE_FIELD(confidence_state);
909 			return 1;
910 		case HID_DG_TOUCH:
911 			/*
912 			 * Legacy devices use TIPSWITCH and not TOUCH.
913 			 * One special case here is of the Apple Touch Bars.
914 			 * In these devices, the tip state is contained in
915 			 * fields with the HID_DG_TOUCH usage.
916 			 * Let's just ignore this field for other devices.
917 			 */
918 			if (!(cls->quirks & MT_QUIRK_APPLE_TOUCHBAR))
919 				return -1;
920 			fallthrough;
921 		case HID_DG_TIPSWITCH:
922 			if (field->application != HID_GD_SYSTEM_MULTIAXIS)
923 				input_set_capability(hi->input,
924 						     EV_KEY, BTN_TOUCH);
925 			MT_STORE_FIELD(tip_state);
926 			return 1;
927 		case HID_DG_TRANSDUCER_INDEX:
928 			/*
929 			 * Contact ID in case of Apple Touch Bars is contained
930 			 * in fields with HID_DG_TRANSDUCER_INDEX usage.
931 			 */
932 			if (!(cls->quirks & MT_QUIRK_APPLE_TOUCHBAR))
933 				return 0;
934 			fallthrough;
935 		case HID_DG_CONTACTID:
936 			MT_STORE_FIELD(contactid);
937 			app->touches_by_report++;
938 			return 1;
939 		case HID_DG_WIDTH:
940 			if (!(app->quirks & MT_QUIRK_NO_AREA))
941 				set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
942 					cls->sn_width);
943 			MT_STORE_FIELD(w);
944 			return 1;
945 		case HID_DG_HEIGHT:
946 			if (!(app->quirks & MT_QUIRK_NO_AREA)) {
947 				set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
948 					cls->sn_height);
949 
950 				/*
951 				 * Only set ABS_MT_ORIENTATION if it is not
952 				 * already set by the HID_DG_AZIMUTH usage.
953 				 */
954 				if (!test_bit(ABS_MT_ORIENTATION,
955 						hi->input->absbit))
956 					input_set_abs_params(hi->input,
957 						ABS_MT_ORIENTATION, 0, 1, 0, 0);
958 			}
959 			MT_STORE_FIELD(h);
960 			return 1;
961 		case HID_DG_TIPPRESSURE:
962 			set_abs(hi->input, ABS_MT_PRESSURE, field,
963 				cls->sn_pressure);
964 			td->is_haptic_touchpad =
965 				hid_haptic_check_pressure_unit(td->haptic,
966 							       hi, field);
967 			MT_STORE_FIELD(p);
968 			return 1;
969 		case HID_DG_SCANTIME:
970 			input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
971 			app->scantime = &field->value[usage->usage_index];
972 			app->scantime_logical_max = field->logical_maximum;
973 			return 1;
974 		case HID_DG_CONTACTCOUNT:
975 			app->have_contact_count = true;
976 			app->raw_cc = &field->value[usage->usage_index];
977 			return 1;
978 		case HID_DG_AZIMUTH:
979 			/*
980 			 * Azimuth has the range of [0, MAX) representing a full
981 			 * revolution. Set ABS_MT_ORIENTATION to a quarter of
982 			 * MAX according the definition of ABS_MT_ORIENTATION
983 			 */
984 			input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
985 				-field->logical_maximum / 4,
986 				field->logical_maximum / 4,
987 				cls->sn_move ?
988 				field->logical_maximum / cls->sn_move : 0, 0);
989 			MT_STORE_FIELD(a);
990 			return 1;
991 		case HID_DG_CONTACTMAX:
992 			/* contact max are global to the report */
993 			return -1;
994 		}
995 		/* let hid-input decide for the others */
996 		return 0;
997 
998 	case HID_UP_BUTTON:
999 		code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
1000 		/*
1001 		 * MS PTP spec says that external buttons left and right have
1002 		 * usages 2 and 3.
1003 		 */
1004 		if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
1005 		    field->application == HID_DG_TOUCHPAD &&
1006 		    (usage->hid & HID_USAGE) > 1)
1007 			code--;
1008 
1009 		if (field->application == HID_GD_SYSTEM_MULTIAXIS)
1010 			code = BTN_0  + ((usage->hid - 1) & HID_USAGE);
1011 
1012 		hid_map_usage(hi, usage, bit, max, EV_KEY, code);
1013 		if (!*bit)
1014 			return -1;
1015 		input_set_capability(hi->input, EV_KEY, code);
1016 		return 1;
1017 
1018 	case 0xff000000:
1019 		/* we do not want to map these: no input-oriented meaning */
1020 		return -1;
1021 	}
1022 
1023 	return 0;
1024 }
1025 
1026 static int mt_compute_slot(struct mt_device *td, struct mt_application *app,
1027 			   struct mt_usages *slot,
1028 			   struct input_dev *input)
1029 {
1030 	__s32 quirks = app->quirks;
1031 
1032 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
1033 		return *slot->contactid;
1034 
1035 	if (quirks & MT_QUIRK_CYPRESS)
1036 		return cypress_compute_slot(app, slot);
1037 
1038 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
1039 		return app->num_received;
1040 
1041 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
1042 		return *slot->contactid - 1;
1043 
1044 	return input_mt_get_slot_by_key(input, *slot->contactid);
1045 }
1046 
1047 static void mt_release_pending_palms(struct mt_device *td,
1048 				     struct mt_application *app,
1049 				     struct input_dev *input)
1050 {
1051 	int slotnum;
1052 	bool need_sync = false;
1053 
1054 	for_each_set_bit(slotnum, app->pending_palm_slots, td->maxcontacts) {
1055 		clear_bit(slotnum, app->pending_palm_slots);
1056 		clear_bit(slotnum, td->active_slots);
1057 
1058 		input_mt_slot(input, slotnum);
1059 		input_mt_report_slot_inactive(input);
1060 
1061 		need_sync = true;
1062 	}
1063 
1064 	if (need_sync) {
1065 		input_mt_sync_frame(input);
1066 		input_sync(input);
1067 	}
1068 }
1069 
1070 /*
1071  * this function is called when a whole packet has been received and processed,
1072  * so that it can decide what to send to the input layer.
1073  */
1074 static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
1075 			  struct input_dev *input)
1076 {
1077 	if (app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
1078 		input_event(input, EV_KEY, BTN_LEFT, app->left_button_state);
1079 
1080 	input_mt_sync_frame(input);
1081 	input_event(input, EV_MSC, MSC_TIMESTAMP, app->timestamp);
1082 	input_sync(input);
1083 
1084 	mt_release_pending_palms(td, app, input);
1085 
1086 	app->num_received = 0;
1087 	app->left_button_state = 0;
1088 	if (td->is_haptic_touchpad)
1089 		hid_haptic_pressure_reset(td->haptic);
1090 }
1091 
1092 static int mt_compute_timestamp(struct mt_application *app, __s32 value)
1093 {
1094 	long delta = value - app->prev_scantime;
1095 	unsigned long jdelta = jiffies_to_usecs(jiffies - app->jiffies);
1096 
1097 	app->jiffies = jiffies;
1098 
1099 	if (delta < 0)
1100 		delta += app->scantime_logical_max;
1101 
1102 	/* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
1103 	delta *= 100;
1104 
1105 	if (jdelta > MAX_TIMESTAMP_INTERVAL)
1106 		/* No data received for a while, resync the timestamp. */
1107 		return 0;
1108 	else
1109 		return app->timestamp + delta;
1110 }
1111 
1112 static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
1113 				struct hid_usage *usage, __s32 value)
1114 {
1115 	/* we will handle the hidinput part later, now remains hiddev */
1116 	if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
1117 		hid->hiddev_hid_event(hid, field, usage, value);
1118 
1119 	return 1;
1120 }
1121 
1122 static int mt_process_slot(struct mt_device *td, struct input_dev *input,
1123 			    struct mt_application *app,
1124 			    struct mt_usages *slot)
1125 {
1126 	struct input_mt *mt = input->mt;
1127 	struct hid_device *hdev = td->hdev;
1128 	__s32 quirks = app->quirks;
1129 	bool valid = true;
1130 	bool confidence_state = true;
1131 	bool inrange_state = false;
1132 	int active;
1133 	int slotnum;
1134 	int tool = MT_TOOL_FINGER;
1135 
1136 	if (!slot)
1137 		return -EINVAL;
1138 
1139 	if ((quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
1140 	    app->num_received >= app->num_expected)
1141 		return -EAGAIN;
1142 
1143 	if (!(quirks & MT_QUIRK_ALWAYS_VALID)) {
1144 		if (quirks & MT_QUIRK_VALID_IS_INRANGE)
1145 			valid = *slot->inrange_state;
1146 		if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1147 			valid = *slot->tip_state;
1148 		if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
1149 			valid = *slot->confidence_state;
1150 
1151 		if (!valid)
1152 			return 0;
1153 	}
1154 
1155 	slotnum = mt_compute_slot(td, app, slot, input);
1156 	if (slotnum < 0 || slotnum >= td->maxcontacts)
1157 		return 0;
1158 
1159 	if ((quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
1160 		struct input_mt_slot *i_slot = &mt->slots[slotnum];
1161 
1162 		if (input_mt_is_active(i_slot) &&
1163 		    input_mt_is_used(mt, i_slot))
1164 			return -EAGAIN;
1165 	}
1166 
1167 	if (quirks & MT_QUIRK_CONFIDENCE)
1168 		confidence_state = *slot->confidence_state;
1169 
1170 	if (quirks & MT_QUIRK_HOVERING)
1171 		inrange_state = *slot->inrange_state;
1172 
1173 	active = *slot->tip_state || inrange_state;
1174 
1175 	if (app->application == HID_GD_SYSTEM_MULTIAXIS)
1176 		tool = MT_TOOL_DIAL;
1177 	else if (unlikely(!confidence_state)) {
1178 		tool = MT_TOOL_PALM;
1179 		if (!active && mt &&
1180 		    input_mt_is_active(&mt->slots[slotnum])) {
1181 			/*
1182 			 * The non-confidence was reported for
1183 			 * previously valid contact that is also no
1184 			 * longer valid. We can't simply report
1185 			 * lift-off as userspace will not be aware
1186 			 * of non-confidence, so we need to split
1187 			 * it into 2 events: active MT_TOOL_PALM
1188 			 * and a separate liftoff.
1189 			 */
1190 			active = true;
1191 			set_bit(slotnum, app->pending_palm_slots);
1192 		}
1193 	}
1194 
1195 	input_mt_slot(input, slotnum);
1196 	input_mt_report_slot_state(input, tool, active);
1197 	if (active) {
1198 		/* this finger is in proximity of the sensor */
1199 		int wide = (*slot->w > *slot->h);
1200 		int major = max(*slot->w, *slot->h);
1201 		int minor = min(*slot->w, *slot->h);
1202 		int orientation = wide;
1203 		int max_azimuth;
1204 		int azimuth;
1205 		int x;
1206 		int y;
1207 		int cx;
1208 		int cy;
1209 
1210 		if (slot->a != DEFAULT_ZERO) {
1211 			/*
1212 			 * Azimuth is counter-clockwise and ranges from [0, MAX)
1213 			 * (a full revolution). Convert it to clockwise ranging
1214 			 * [-MAX/2, MAX/2].
1215 			 *
1216 			 * Note that ABS_MT_ORIENTATION require us to report
1217 			 * the limit of [-MAX/4, MAX/4], but the value can go
1218 			 * out of range to [-MAX/2, MAX/2] to report an upside
1219 			 * down ellipsis.
1220 			 */
1221 			azimuth = *slot->a;
1222 			max_azimuth = input_abs_get_max(input,
1223 							ABS_MT_ORIENTATION);
1224 			if (azimuth > max_azimuth * 2)
1225 				azimuth -= max_azimuth * 4;
1226 			orientation = -azimuth;
1227 			if (quirks & MT_QUIRK_ORIENTATION_INVERT)
1228 				orientation = -orientation;
1229 
1230 		}
1231 
1232 		if (quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
1233 			/*
1234 			 * divided by two to match visual scale of touch
1235 			 * for devices with this quirk
1236 			 */
1237 			major = major >> 1;
1238 			minor = minor >> 1;
1239 		}
1240 
1241 		if (td->is_haptic_touchpad)
1242 			hid_haptic_pressure_increase(td->haptic, *slot->p);
1243 
1244 		x = hdev->quirks & HID_QUIRK_X_INVERT ?
1245 			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->x :
1246 			*slot->x;
1247 		y = hdev->quirks & HID_QUIRK_Y_INVERT ?
1248 			input_abs_get_max(input, ABS_MT_POSITION_Y) - *slot->y :
1249 			*slot->y;
1250 		cx = hdev->quirks & HID_QUIRK_X_INVERT ?
1251 			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->cx :
1252 			*slot->cx;
1253 		cy = hdev->quirks & HID_QUIRK_Y_INVERT ?
1254 			input_abs_get_max(input, ABS_MT_POSITION_Y) - *slot->cy :
1255 			*slot->cy;
1256 
1257 		input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
1258 		input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
1259 		input_event(input, EV_ABS, ABS_MT_TOOL_X, cx);
1260 		input_event(input, EV_ABS, ABS_MT_TOOL_Y, cy);
1261 		input_event(input, EV_ABS, ABS_MT_DISTANCE, !*slot->tip_state);
1262 		input_event(input, EV_ABS, ABS_MT_ORIENTATION, orientation);
1263 		input_event(input, EV_ABS, ABS_MT_PRESSURE, *slot->p);
1264 		input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
1265 		input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
1266 
1267 		set_bit(slotnum, td->active_slots);
1268 	} else {
1269 		clear_bit(slotnum, td->active_slots);
1270 	}
1271 
1272 	return 0;
1273 }
1274 
1275 static void mt_process_mt_event(struct hid_device *hid,
1276 				struct mt_application *app,
1277 				struct hid_field *field,
1278 				struct hid_usage *usage,
1279 				__s32 value,
1280 				bool first_packet)
1281 {
1282 	__s32 quirks = app->quirks;
1283 	struct input_dev *input = field->hidinput->input;
1284 
1285 	if (!usage->type || !(hid->claimed & HID_CLAIMED_INPUT))
1286 		return;
1287 
1288 	if (quirks & MT_QUIRK_WIN8_PTP_BUTTONS) {
1289 
1290 		/*
1291 		 * For Win8 PTP touchpads we should only look at
1292 		 * non finger/touch events in the first_packet of a
1293 		 * (possible) multi-packet frame.
1294 		 */
1295 		if (!first_packet)
1296 			return;
1297 
1298 		/*
1299 		 * For Win8 PTP touchpads we map both the clickpad click
1300 		 * and any "external" left buttons to BTN_LEFT if a
1301 		 * device claims to have both we need to report 1 for
1302 		 * BTN_LEFT if either is pressed, so we or all values
1303 		 * together and report the result in mt_sync_frame().
1304 		 */
1305 		if (usage->type == EV_KEY && usage->code == BTN_LEFT) {
1306 			app->left_button_state |= value;
1307 			return;
1308 		}
1309 	}
1310 
1311 	input_event(input, usage->type, usage->code, value);
1312 }
1313 
1314 static void mt_touch_report(struct hid_device *hid,
1315 			    struct mt_report_data *rdata)
1316 {
1317 	struct mt_device *td = hid_get_drvdata(hid);
1318 	struct hid_report *report = rdata->report;
1319 	struct mt_application *app = rdata->application;
1320 	struct hid_field *field;
1321 	struct input_dev *input;
1322 	struct mt_usages *slot;
1323 	bool first_packet;
1324 	unsigned count;
1325 	int r, n;
1326 	int scantime = 0;
1327 	int contact_count = -1;
1328 
1329 	/* sticky fingers release in progress, abort */
1330 	if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1331 		return;
1332 
1333 	scantime = *app->scantime;
1334 	app->timestamp = mt_compute_timestamp(app, scantime);
1335 	if (app->raw_cc != DEFAULT_ZERO)
1336 		contact_count = *app->raw_cc;
1337 
1338 	/*
1339 	 * Includes multi-packet support where subsequent
1340 	 * packets are sent with zero contactcount.
1341 	 */
1342 	if (contact_count > 0)
1343 		app->num_expected = contact_count;
1344 	else if (app->num_received == 0 && app->prev_scantime != scantime) {
1345 		/*
1346 		 * New multi-report frame:
1347 		 *
1348 		 * For Win8 PTPs the first packet (td->num_received == 0) may
1349 		 * have a contactcount of 0 if there only is a button event.
1350 		 *
1351 		 * Some other devices use a sentinel frame with 0 to release all contacts
1352 		 */
1353 		app->num_expected = 0;
1354 	}
1355 	app->prev_scantime = scantime;
1356 
1357 	first_packet = app->num_received == 0;
1358 
1359 	input = report->field[0]->hidinput->input;
1360 
1361 	list_for_each_entry(slot, &app->mt_usages, list) {
1362 		if (!mt_process_slot(td, input, app, slot))
1363 			app->num_received++;
1364 	}
1365 
1366 	for (r = 0; r < report->maxfield; r++) {
1367 		field = report->field[r];
1368 		count = field->report_count;
1369 
1370 		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
1371 			continue;
1372 
1373 		for (n = 0; n < count; n++)
1374 			mt_process_mt_event(hid, app, field,
1375 					    &field->usage[n], field->value[n],
1376 					    first_packet);
1377 	}
1378 
1379 	if (app->num_received >= app->num_expected)
1380 		mt_sync_frame(td, app, input);
1381 
1382 	/*
1383 	 * Windows 8 specs says 2 things:
1384 	 * - once a contact has been reported, it has to be reported in each
1385 	 *   subsequent report
1386 	 * - the report rate when fingers are present has to be at least
1387 	 *   the refresh rate of the screen, 60 or 120 Hz
1388 	 *
1389 	 * I interprete this that the specification forces a report rate of
1390 	 * at least 60 Hz for a touchscreen to be certified.
1391 	 * Which means that if we do not get a report whithin 16 ms, either
1392 	 * something wrong happens, either the touchscreen forgets to send
1393 	 * a release. Taking a reasonable margin allows to remove issues
1394 	 * with USB communication or the load of the machine.
1395 	 *
1396 	 * Given that Win 8 devices are forced to send a release, this will
1397 	 * only affect laggish machines and the ones that have a firmware
1398 	 * defect.
1399 	 */
1400 	if (app->quirks & MT_QUIRK_STICKY_FINGERS) {
1401 		if (!bitmap_empty(td->active_slots, td->maxcontacts))
1402 			mod_timer(&td->release_timer,
1403 				  jiffies + msecs_to_jiffies(100));
1404 		else
1405 			timer_delete(&td->release_timer);
1406 	}
1407 
1408 	clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1409 }
1410 
1411 static int mt_touch_input_configured(struct hid_device *hdev,
1412 				     struct hid_input *hi,
1413 				     struct mt_application *app)
1414 {
1415 	struct mt_device *td = hid_get_drvdata(hdev);
1416 	struct mt_class *cls = &td->mtclass;
1417 	struct input_dev *input = hi->input;
1418 	int ret;
1419 
1420 	/*
1421 	 * HID_DG_CONTACTMAX field is not present on Apple Touch Bars,
1422 	 * but the maximum contact count is greater than the default.
1423 	 */
1424 	if (cls->quirks & MT_QUIRK_APPLE_TOUCHBAR && cls->maxcontacts)
1425 		td->maxcontacts = cls->maxcontacts;
1426 
1427 	if (!td->maxcontacts)
1428 		td->maxcontacts = MT_DEFAULT_MAXCONTACT;
1429 
1430 	mt_post_parse(td, app);
1431 	if (td->serial_maybe)
1432 		mt_post_parse_default_settings(td, app);
1433 
1434 	/*
1435 	 * The application for Apple Touch Bars is HID_DG_TOUCHPAD,
1436 	 * but these devices are direct.
1437 	 */
1438 	if (cls->quirks & MT_QUIRK_APPLE_TOUCHBAR)
1439 		app->mt_flags |= INPUT_MT_DIRECT;
1440 
1441 	if (cls->is_indirect)
1442 		app->mt_flags |= INPUT_MT_POINTER;
1443 
1444 	if (td->is_haptic_touchpad)
1445 		app->mt_flags |= INPUT_MT_TOTAL_FORCE;
1446 
1447 	if (app->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1448 		app->mt_flags |= INPUT_MT_DROP_UNUSED;
1449 
1450 	/* check for clickpads */
1451 	if ((app->mt_flags & INPUT_MT_POINTER) &&
1452 	    (app->buttons_count == 1))
1453 		td->is_buttonpad = true;
1454 
1455 	if (td->is_buttonpad)
1456 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1457 	if (td->is_pressurepad)
1458 		__set_bit(INPUT_PROP_PRESSUREPAD, input->propbit);
1459 
1460 	if (!td->active_slots) {
1461 		td->active_slots = devm_kcalloc(&td->hdev->dev,
1462 						BITS_TO_LONGS(td->maxcontacts),
1463 						sizeof(long),
1464 						GFP_KERNEL);
1465 		if (!td->active_slots)
1466 			return -ENOMEM;
1467 	}
1468 
1469 	app->pending_palm_slots = devm_kcalloc(&hi->input->dev,
1470 					       BITS_TO_LONGS(td->maxcontacts),
1471 					       sizeof(long),
1472 					       GFP_KERNEL);
1473 	if (!app->pending_palm_slots)
1474 		return -ENOMEM;
1475 
1476 	ret = input_mt_init_slots(input, td->maxcontacts, app->mt_flags);
1477 	if (ret)
1478 		return ret;
1479 
1480 	app->mt_flags = 0;
1481 	return 0;
1482 }
1483 
1484 #define mt_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, \
1485 						    max, EV_KEY, (c))
1486 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1487 		struct hid_field *field, struct hid_usage *usage,
1488 		unsigned long **bit, int *max)
1489 {
1490 	struct mt_device *td = hid_get_drvdata(hdev);
1491 	struct mt_application *application;
1492 	struct mt_report_data *rdata;
1493 	int ret;
1494 
1495 	rdata = mt_find_report_data(td, field->report);
1496 	if (!rdata) {
1497 		hid_err(hdev, "failed to allocate data for report\n");
1498 		return 0;
1499 	}
1500 
1501 	application = rdata->application;
1502 
1503 	/*
1504 	 * If mtclass.export_all_inputs is not set, only map fields from
1505 	 * TouchScreen or TouchPad collections. We need to ignore fields
1506 	 * that belong to other collections such as Mouse that might have
1507 	 * the same GenericDesktop usages.
1508 	 */
1509 	if (!td->mtclass.export_all_inputs &&
1510 	    field->application != HID_DG_TOUCHSCREEN &&
1511 	    field->application != HID_DG_PEN &&
1512 	    field->application != HID_DG_TOUCHPAD &&
1513 	    field->application != HID_GD_KEYBOARD &&
1514 	    field->application != HID_GD_SYSTEM_CONTROL &&
1515 	    field->application != HID_CP_CONSUMER_CONTROL &&
1516 	    field->application != HID_GD_WIRELESS_RADIO_CTLS &&
1517 	    field->application != HID_GD_SYSTEM_MULTIAXIS &&
1518 	    !(field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1519 	      application->quirks & MT_QUIRK_ASUS_CUSTOM_UP))
1520 		return -1;
1521 
1522 	/*
1523 	 * Some Asus keyboard+touchpad devices have the hotkeys defined in the
1524 	 * touchpad report descriptor. We need to treat these as an array to
1525 	 * map usages to input keys.
1526 	 */
1527 	if (field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1528 	    application->quirks & MT_QUIRK_ASUS_CUSTOM_UP &&
1529 	    (usage->hid & HID_USAGE_PAGE) == HID_UP_CUSTOM) {
1530 		set_bit(EV_REP, hi->input->evbit);
1531 		if (field->flags & HID_MAIN_ITEM_VARIABLE)
1532 			field->flags &= ~HID_MAIN_ITEM_VARIABLE;
1533 		switch (usage->hid & HID_USAGE) {
1534 		case 0x10: mt_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
1535 		case 0x20: mt_map_key_clear(KEY_BRIGHTNESSUP);		break;
1536 		case 0x35: mt_map_key_clear(KEY_DISPLAY_OFF);		break;
1537 		case 0x6b: mt_map_key_clear(KEY_F21);			break;
1538 		case 0x6c: mt_map_key_clear(KEY_SLEEP);			break;
1539 		default:
1540 			return -1;
1541 		}
1542 		return 1;
1543 	}
1544 
1545 	if (rdata->is_mt_collection)
1546 		return mt_touch_input_mapping(hdev, hi, field, usage, bit, max,
1547 					      application);
1548 
1549 	/*
1550 	 * some egalax touchscreens have "application == DG_TOUCHSCREEN"
1551 	 * for the stylus. Overwrite the hid_input application
1552 	 */
1553 	if (field->physical == HID_DG_STYLUS)
1554 		hi->application = HID_DG_STYLUS;
1555 
1556 	ret = hid_haptic_input_mapping(hdev, td->haptic, hi, field, usage, bit,
1557 				       max);
1558 	if (ret != 0)
1559 		return ret;
1560 
1561 	/* let hid-core decide for the others */
1562 	return 0;
1563 }
1564 
1565 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
1566 		struct hid_field *field, struct hid_usage *usage,
1567 		unsigned long **bit, int *max)
1568 {
1569 	struct mt_device *td = hid_get_drvdata(hdev);
1570 	struct mt_report_data *rdata;
1571 
1572 	rdata = mt_find_report_data(td, field->report);
1573 	if (rdata && rdata->is_mt_collection) {
1574 		/* We own these mappings, tell hid-input to ignore them */
1575 		return -1;
1576 	}
1577 
1578 	/* let hid-core decide for the others */
1579 	return 0;
1580 }
1581 
1582 static int mt_event(struct hid_device *hid, struct hid_field *field,
1583 				struct hid_usage *usage, __s32 value)
1584 {
1585 	struct mt_device *td = hid_get_drvdata(hid);
1586 	struct mt_report_data *rdata;
1587 
1588 	rdata = mt_find_report_data(td, field->report);
1589 	if (rdata && rdata->is_mt_collection)
1590 		return mt_touch_event(hid, field, usage, value);
1591 
1592 	return 0;
1593 }
1594 
1595 /*
1596  * Yoga Book 9 14IAH10 descriptor fixup.
1597  *
1598  * The device includes a HID_DG_TOUCHPAD application collection designed for
1599  * the Windows inbox HID driver's Win8 PTP touchpad mode.  On Linux we want
1600  * only the HID_DG_TOUCHSCREEN collections.  The touchpad collection (and the
1601  * HID_DG_BUTTONTYPE and Win8 compliance blob features it contains) must be
1602  * removed so hid-multitouch does not misclassify the touchscreen nodes as
1603  * indirect buttonpads.
1604  *
1605  * The firmware also resets if any USB control request is received while the
1606  * CDC-ACM interface is initialising (~1.18 s after enumeration).  Dropping
1607  * the Win8 blob and Contact Count Max feature reports prevents the
1608  * GET_REPORT calls that hid-multitouch issues at probe.
1609  */
1610 static void mt_yogabook9_fixup(struct hid_device *hdev, __u8 *rdesc,
1611 			       unsigned int *size)
1612 {
1613 	/* Usage Page (Digitizer), Usage (Touch Pad), Collection (Application) */
1614 	static const __u8 tp_app_hdr[] = { 0x05, 0x0d, 0x09, 0x05, 0xa1, 0x01 };
1615 	/* Vendor Usage Page 0xff00 (Win8 compliance blob header) */
1616 	static const __u8 win8_page[] = { 0x06, 0x00, 0xff };
1617 	/* Usage (Contact Count Max = 0x55) */
1618 	static const __u8 ccmax_usage[] = { 0x09, 0x55 };
1619 	unsigned int i;
1620 
1621 	/*
1622 	 * Step 1: find and remove the Touch Pad application collection.
1623 	 * Walk HID short items from the collection header to its matching
1624 	 * End Collection, then close the gap with memmove.
1625 	 */
1626 	for (i = 0; i + sizeof(tp_app_hdr) <= *size; i++) {
1627 		if (memcmp(rdesc + i, tp_app_hdr, sizeof(tp_app_hdr)) == 0) {
1628 			__u8 *start = rdesc + i;
1629 			__u8 *coll_end = NULL;
1630 			__u8 *p = start;
1631 			unsigned int drop;
1632 			int depth = 0;
1633 
1634 			while (p < rdesc + *size) {
1635 				__u8 b = *p;
1636 				int ds = b & 3;
1637 				int item_len;
1638 
1639 				if (b == 0xfe) { /* long item */
1640 					if (p + 2 >= rdesc + *size)
1641 						break;
1642 					item_len = p[1] + 3;
1643 				} else {
1644 					item_len = (ds == 3) ? 5 : ds + 1;
1645 				}
1646 				if (p + item_len > rdesc + *size)
1647 					break;
1648 
1649 				if ((b & 0xfc) == 0xa0)
1650 					depth++; /* Collection */
1651 				else if (b == 0xc0) {
1652 					depth--; /* End Collection */
1653 					if (depth == 0) {
1654 						coll_end = p;
1655 						break;
1656 					}
1657 				}
1658 				p += item_len;
1659 			}
1660 
1661 			if (!coll_end) {
1662 				hid_err(hdev,
1663 					"Yoga Book 9: Touch Pad End Collection not found\n");
1664 				break;
1665 			}
1666 
1667 			drop = coll_end - start + 1;
1668 			memmove(start, coll_end + 1, rdesc + *size - coll_end - 1);
1669 			*size -= drop;
1670 			hid_dbg(hdev,
1671 				"Yoga Book 9: dropped Touch Pad collection (%u bytes)\n",
1672 				drop);
1673 			break;
1674 		}
1675 	}
1676 
1677 	/*
1678 	 * Step 2: neutralize Win8 compliance blob feature reports remaining
1679 	 * in the touchscreen collections.  Change Usage Page 0xff00 to 0x0f00
1680 	 * so the case 0xff0000c5 branch in mt_feature_mapping() is not reached
1681 	 * and no GET_REPORT is issued.
1682 	 */
1683 	for (i = 0; i + sizeof(win8_page) <= *size; i++) {
1684 		if (memcmp(rdesc + i, win8_page, sizeof(win8_page)) == 0) {
1685 			rdesc[i + 2] = 0x0f; /* 0xff00 -> 0x0f00 */
1686 			hid_dbg(hdev,
1687 				"Yoga Book 9: neutralized Win8 blob at offset %u\n",
1688 				i);
1689 		}
1690 	}
1691 
1692 	/*
1693 	 * Step 3: neutralize Contact Count Max feature reports.  Change usage
1694 	 * 0x55 (HID_DG_CONTACTMAX) to 0x00 so mt_feature_mapping() does not
1695 	 * issue GET_REPORT.  The class maxcontacts field provides the value.
1696 	 */
1697 	for (i = 0; i + sizeof(ccmax_usage) <= *size; i++) {
1698 		if (memcmp(rdesc + i, ccmax_usage, sizeof(ccmax_usage)) == 0) {
1699 			rdesc[i + 1] = 0x00;
1700 			hid_dbg(hdev,
1701 				"Yoga Book 9: neutralized ContactMax at offset %u\n",
1702 				i);
1703 		}
1704 	}
1705 
1706 	/*
1707 	 * Step 4: neutralize Surface Switch (0x57) and Button Switch (0x58)
1708 	 * feature report usages in the Device Configuration collection.
1709 	 * mt_set_modes() issues HID_REQ_SET_REPORT for these on every
1710 	 * input-device open/close; those repeated control requests hit the
1711 	 * firmware's CDC-ACM init window and trigger resets.
1712 	 *
1713 	 * Input Mode (0x52) is intentionally left intact.  mt_set_modes()
1714 	 * sends it once at probe to set the device into touchscreen mode,
1715 	 * which flushes the firmware's contact buffer and clears a persistent
1716 	 * ghost contact (cid 2, fixed coordinates) that otherwise appears on
1717 	 * every enumeration.  By probe time cdc_acm has already satisfied the
1718 	 * CDC-ACM init watchdog (~130 ms), so the single SET_REPORT for Input
1719 	 * Mode arrives safely after the reset window has closed.
1720 	 */
1721 	for (i = 0; i + 2 <= *size; i++) {
1722 		if (rdesc[i] == 0x09 &&
1723 		    (rdesc[i + 1] == 0x57 ||
1724 		     rdesc[i + 1] == 0x58)) {
1725 			hid_dbg(hdev,
1726 				"Yoga Book 9: neutralized set-modes usage 0x%02x at offset %u\n",
1727 				rdesc[i + 1], i);
1728 			rdesc[i + 1] = 0x00;
1729 		}
1730 	}
1731 }
1732 
1733 static const __u8 *mt_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1734 			     unsigned int *size)
1735 {
1736 	if (hdev->vendor == I2C_VENDOR_ID_GOODIX &&
1737 	    (hdev->product == I2C_DEVICE_ID_GOODIX_01E8 ||
1738 	     hdev->product == I2C_DEVICE_ID_GOODIX_01E9)) {
1739 		if (*size < 608) {
1740 			dev_info(
1741 				&hdev->dev,
1742 				"GT7868Q fixup: report descriptor is only %u bytes, skipping\n",
1743 				*size);
1744 			return rdesc;
1745 		}
1746 
1747 		if (rdesc[607] == 0x15) {
1748 			rdesc[607] = 0x25;
1749 			dev_info(
1750 				&hdev->dev,
1751 				"GT7868Q report descriptor fixup is applied.\n");
1752 		} else {
1753 			dev_info(
1754 				&hdev->dev,
1755 				"The byte is not expected for fixing the report descriptor. \
1756 It's possible that the touchpad firmware is not suitable for applying the fix. \
1757 got: %x\n",
1758 				rdesc[607]);
1759 		}
1760 	}
1761 
1762 	if (hdev->vendor == USB_VENDOR_ID_LENOVO &&
1763 	    hdev->product == USB_DEVICE_ID_LENOVO_YOGABOOK9I)
1764 		mt_yogabook9_fixup(hdev, rdesc, size);
1765 
1766 	return rdesc;
1767 }
1768 
1769 static void mt_report(struct hid_device *hid, struct hid_report *report)
1770 {
1771 	struct mt_device *td = hid_get_drvdata(hid);
1772 	struct hid_field *field = report->field[0];
1773 	struct mt_report_data *rdata;
1774 
1775 	if (!(hid->claimed & HID_CLAIMED_INPUT))
1776 		return;
1777 
1778 	rdata = mt_find_report_data(td, report);
1779 	if (rdata && rdata->is_mt_collection)
1780 		return mt_touch_report(hid, rdata);
1781 
1782 	/* Lenovo Yoga Book 9i requires consuming and dropping certain bogus reports */
1783 	if (rdata && rdata->application &&
1784 		(rdata->application->quirks & MT_QUIRK_YOGABOOK9I)) {
1785 
1786 		bool all_zero_report = true;
1787 
1788 		for (int f = 0; f < report->maxfield && all_zero_report; f++) {
1789 			struct hid_field *fld = report->field[f];
1790 
1791 			for (int i = 0; i < fld->report_count; i++) {
1792 				unsigned int usage = fld->usage[i].hid;
1793 
1794 				if (usage == HID_DG_INRANGE ||
1795 					usage == HID_DG_TIPSWITCH ||
1796 					usage == HID_DG_BARRELSWITCH ||
1797 					usage == HID_DG_BARRELSWITCH2 ||
1798 					usage == HID_DG_CONTACTID ||
1799 					usage == HID_DG_TILT_X ||
1800 					usage == HID_DG_TILT_Y) {
1801 
1802 					if (fld->value[i] != 0) {
1803 						all_zero_report = false;
1804 						break;
1805 					}
1806 				}
1807 			}
1808 		}
1809 
1810 		if (all_zero_report)
1811 			return;
1812 	}
1813 
1814 	if (field && field->hidinput && field->hidinput->input)
1815 		input_sync(field->hidinput->input);
1816 }
1817 
1818 static bool mt_need_to_apply_feature(struct hid_device *hdev,
1819 				     struct hid_field *field,
1820 				     struct hid_usage *usage,
1821 				     enum latency_mode latency,
1822 				     enum report_mode report_mode,
1823 				     bool *inputmode_found)
1824 {
1825 	struct mt_device *td = hid_get_drvdata(hdev);
1826 	struct mt_class *cls = &td->mtclass;
1827 	struct hid_report *report = field->report;
1828 	unsigned int index = usage->usage_index;
1829 	char *buf;
1830 	u32 report_len;
1831 	int max;
1832 
1833 	switch (usage->hid) {
1834 	case HID_DG_INPUTMODE:
1835 		/*
1836 		 * Some elan panels wrongly declare 2 input mode features,
1837 		 * and silently ignore when we set the value in the second
1838 		 * field. Skip the second feature and hope for the best.
1839 		 */
1840 		if (*inputmode_found)
1841 			return false;
1842 
1843 		if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
1844 			report_len = hid_report_len(report);
1845 			buf = hid_alloc_report_buf(report, GFP_KERNEL);
1846 			if (!buf) {
1847 				hid_err(hdev,
1848 					"failed to allocate buffer for report\n");
1849 				return false;
1850 			}
1851 			hid_hw_raw_request(hdev, report->id, buf, report_len,
1852 					   HID_FEATURE_REPORT,
1853 					   HID_REQ_GET_REPORT);
1854 			kfree(buf);
1855 		}
1856 
1857 		field->value[index] = td->inputmode_value;
1858 		*inputmode_found = true;
1859 		return true;
1860 
1861 	case HID_DG_CONTACTMAX:
1862 		if (cls->maxcontacts) {
1863 			max = min_t(int, field->logical_maximum,
1864 				    cls->maxcontacts);
1865 			if (field->value[index] != max) {
1866 				field->value[index] = max;
1867 				return true;
1868 			}
1869 		}
1870 		break;
1871 
1872 	case HID_DG_LATENCYMODE:
1873 		field->value[index] = latency;
1874 		return true;
1875 
1876 	case HID_DG_SURFACESWITCH:
1877 		field->value[index] = !!(report_mode & TOUCHPAD_REPORT_CONTACTS);
1878 		return true;
1879 
1880 	case HID_DG_BUTTONSWITCH:
1881 		field->value[index] = !!(report_mode & TOUCHPAD_REPORT_BUTTONS);
1882 		return true;
1883 	}
1884 
1885 	return false; /* no need to update the report */
1886 }
1887 
1888 static void mt_set_modes(struct hid_device *hdev, enum latency_mode latency,
1889 			 enum report_mode report_mode)
1890 {
1891 	struct hid_report_enum *rep_enum;
1892 	struct hid_report *rep;
1893 	struct hid_usage *usage;
1894 	int i, j;
1895 	bool update_report;
1896 	bool inputmode_found = false;
1897 
1898 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1899 	list_for_each_entry(rep, &rep_enum->report_list, list) {
1900 		update_report = false;
1901 
1902 		for (i = 0; i < rep->maxfield; i++) {
1903 			/* Ignore if report count is out of bounds. */
1904 			if (rep->field[i]->report_count < 1)
1905 				continue;
1906 
1907 			for (j = 0; j < rep->field[i]->maxusage; j++) {
1908 				usage = &rep->field[i]->usage[j];
1909 
1910 				if (mt_need_to_apply_feature(hdev,
1911 							     rep->field[i],
1912 							     usage,
1913 							     latency,
1914 							     report_mode,
1915 							     &inputmode_found))
1916 					update_report = true;
1917 			}
1918 		}
1919 
1920 		if (update_report)
1921 			hid_hw_request(hdev, rep, HID_REQ_SET_REPORT);
1922 	}
1923 }
1924 
1925 static void mt_post_parse_default_settings(struct mt_device *td,
1926 					   struct mt_application *app)
1927 {
1928 	__s32 quirks = app->quirks;
1929 
1930 	/* unknown serial device needs special quirks */
1931 	if (list_is_singular(&app->mt_usages)) {
1932 		quirks |= MT_QUIRK_ALWAYS_VALID;
1933 		quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1934 		quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1935 		quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1936 		quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1937 	}
1938 
1939 	app->quirks = quirks;
1940 }
1941 
1942 static void mt_post_parse(struct mt_device *td, struct mt_application *app)
1943 {
1944 	if (!app->have_contact_count)
1945 		app->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1946 }
1947 
1948 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1949 {
1950 	struct mt_device *td = hid_get_drvdata(hdev);
1951 	const char *suffix = NULL;
1952 	struct mt_report_data *rdata;
1953 	struct mt_application *mt_application = NULL;
1954 	struct hid_report *report;
1955 	int ret;
1956 
1957 	if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 ||
1958 	    td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
1959 	    td->mtclass.name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE)) {
1960 		if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0)
1961 			td->is_haptic_touchpad = false;
1962 	} else {
1963 		td->is_haptic_touchpad = false;
1964 	}
1965 
1966 	list_for_each_entry(report, &hi->reports, hidinput_list) {
1967 		rdata = mt_find_report_data(td, report);
1968 		if (!rdata) {
1969 			hid_err(hdev, "failed to allocate data for report\n");
1970 			return -ENOMEM;
1971 		}
1972 
1973 		mt_application = rdata->application;
1974 
1975 		if (rdata->is_mt_collection) {
1976 			ret = mt_touch_input_configured(hdev, hi,
1977 							mt_application);
1978 			if (ret)
1979 				return ret;
1980 		}
1981 	}
1982 
1983 	switch (hi->application) {
1984 	case HID_GD_KEYBOARD:
1985 	case HID_GD_KEYPAD:
1986 	case HID_GD_MOUSE:
1987 	case HID_DG_TOUCHPAD:
1988 	case HID_GD_SYSTEM_CONTROL:
1989 	case HID_CP_CONSUMER_CONTROL:
1990 	case HID_GD_WIRELESS_RADIO_CTLS:
1991 	case HID_GD_SYSTEM_MULTIAXIS:
1992 	case HID_DG_PEN:
1993 		/* already handled by hid core */
1994 		break;
1995 	case HID_DG_TOUCHSCREEN:
1996 		/* we do not set suffix = "Touchscreen" */
1997 		hi->input->name = hdev->name;
1998 		break;
1999 	case HID_VD_ASUS_CUSTOM_MEDIA_KEYS:
2000 		suffix = "Custom Media Keys";
2001 		break;
2002 	case HID_DG_STYLUS:
2003 		/* force BTN_STYLUS to allow tablet matching in udev */
2004 		__set_bit(BTN_STYLUS, hi->input->keybit);
2005 		break;
2006 	default:
2007 		suffix = "UNKNOWN";
2008 		break;
2009 	}
2010 
2011 	/* Lenovo Yoga Book 9i requires custom naming to allow differentiation in udev */
2012 	if (hi->report && td->mtclass.quirks & MT_QUIRK_YOGABOOK9I) {
2013 		switch (hi->report->id) {
2014 		case 48:
2015 			suffix = "Touchscreen Top";
2016 			break;
2017 		case 56:
2018 			suffix = "Touchscreen Bottom";
2019 			break;
2020 		case 20:
2021 			suffix = "Stylus Top";
2022 			break;
2023 		case 40:
2024 			suffix = "Stylus Bottom";
2025 			break;
2026 		case 80:
2027 			suffix = "Emulated Touchpad";
2028 			break;
2029 		default:
2030 			suffix = "";
2031 			break;
2032 		}
2033 	}
2034 
2035 	if (suffix) {
2036 		hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
2037 						 "%s %s", hdev->name, suffix);
2038 		if (!hi->input->name)
2039 			return -ENOMEM;
2040 	}
2041 
2042 	return 0;
2043 }
2044 
2045 static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
2046 {
2047 	if (field->usage[0].hid != usage ||
2048 	    !(field->flags & HID_MAIN_ITEM_CONSTANT))
2049 		return;
2050 
2051 	field->flags &= ~HID_MAIN_ITEM_CONSTANT;
2052 	field->flags |= HID_MAIN_ITEM_VARIABLE;
2053 }
2054 
2055 static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
2056 {
2057 	struct hid_report *report;
2058 	int i;
2059 
2060 	list_for_each_entry(report,
2061 			    &hdev->report_enum[HID_INPUT_REPORT].report_list,
2062 			    list) {
2063 
2064 		if (!report->maxfield)
2065 			continue;
2066 
2067 		for (i = 0; i < report->maxfield; i++)
2068 			if (report->field[i]->maxusage >= 1)
2069 				mt_fix_const_field(report->field[i], usage);
2070 	}
2071 }
2072 
2073 static void mt_release_contacts(struct hid_device *hid)
2074 {
2075 	struct hid_input *hidinput;
2076 	struct mt_application *application;
2077 	struct mt_device *td = hid_get_drvdata(hid);
2078 
2079 	list_for_each_entry(hidinput, &hid->inputs, list) {
2080 		struct input_dev *input_dev = hidinput->input;
2081 		struct input_mt *mt = input_dev->mt;
2082 		int i;
2083 
2084 		if (mt) {
2085 			for (i = 0; i < mt->num_slots; i++) {
2086 				input_mt_slot(input_dev, i);
2087 				input_mt_report_slot_inactive(input_dev);
2088 				clear_bit(i, td->active_slots);
2089 			}
2090 			input_mt_sync_frame(input_dev);
2091 			input_sync(input_dev);
2092 		}
2093 	}
2094 
2095 	list_for_each_entry(application, &td->applications, list) {
2096 		application->num_received = 0;
2097 	}
2098 }
2099 
2100 static void mt_expired_timeout(struct timer_list *t)
2101 {
2102 	struct mt_device *td = timer_container_of(td, t, release_timer);
2103 	struct hid_device *hdev = td->hdev;
2104 
2105 	/*
2106 	 * An input report came in just before we release the sticky fingers,
2107 	 * it will take care of the sticky fingers.
2108 	 */
2109 	if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
2110 		return;
2111 	if (!bitmap_empty(td->active_slots, td->maxcontacts))
2112 		mt_release_contacts(hdev);
2113 	clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
2114 }
2115 
2116 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
2117 {
2118 	int ret, i;
2119 	struct mt_device *td;
2120 	const struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
2121 
2122 	for (i = 0; mt_classes[i].name ; i++) {
2123 		if (id->driver_data == mt_classes[i].name) {
2124 			mtclass = &(mt_classes[i]);
2125 			break;
2126 		}
2127 	}
2128 
2129 	td = devm_kzalloc_obj(&hdev->dev, struct mt_device);
2130 	if (!td) {
2131 		dev_err(&hdev->dev, "cannot allocate multitouch data\n");
2132 		return -ENOMEM;
2133 	}
2134 	td->haptic = devm_kzalloc_obj(&hdev->dev, *(td->haptic));
2135 	if (!td->haptic)
2136 		return -ENOMEM;
2137 
2138 	td->haptic->hdev = hdev;
2139 	td->hdev = hdev;
2140 	td->mtclass = *mtclass;
2141 	td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
2142 	hid_set_drvdata(hdev, td);
2143 
2144 	INIT_LIST_HEAD(&td->applications);
2145 	INIT_LIST_HEAD(&td->reports);
2146 
2147 	if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
2148 		td->serial_maybe = true;
2149 
2150 
2151 	/* Orientation is inverted if the X or Y axes are
2152 	 * flipped, but normalized if both are inverted.
2153 	 */
2154 	if (hdev->quirks & (HID_QUIRK_X_INVERT | HID_QUIRK_Y_INVERT) &&
2155 	    !((hdev->quirks & HID_QUIRK_X_INVERT)
2156 	      && (hdev->quirks & HID_QUIRK_Y_INVERT)))
2157 		td->mtclass.quirks = MT_QUIRK_ORIENTATION_INVERT;
2158 
2159 	/* This allows the driver to correctly support devices
2160 	 * that emit events over several HID messages.
2161 	 */
2162 	hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
2163 
2164 	/*
2165 	 * This allows the driver to handle different input sensors
2166 	 * that emits events through different applications on the same HID
2167 	 * device.
2168 	 */
2169 	hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
2170 
2171 	if (id->group != HID_GROUP_MULTITOUCH_WIN_8)
2172 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
2173 
2174 	if (mtclass->quirks & MT_QUIRK_FORCE_MULTI_INPUT) {
2175 		hdev->quirks &= ~HID_QUIRK_INPUT_PER_APP;
2176 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
2177 	}
2178 
2179 	timer_setup(&td->release_timer, mt_expired_timeout, 0);
2180 
2181 	ret = hid_parse(hdev);
2182 	if (ret != 0)
2183 		return ret;
2184 
2185 	if (mtclass->name == MT_CLS_APPLE_TOUCHBAR &&
2186 	    !hid_find_field(hdev, HID_INPUT_REPORT,
2187 			    HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX))
2188 		return -ENODEV;
2189 
2190 	if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
2191 		mt_fix_const_fields(hdev, HID_DG_CONTACTID);
2192 
2193 	if (hdev->vendor == USB_VENDOR_ID_SIS_TOUCH)
2194 		hdev->quirks |= HID_QUIRK_NOGET;
2195 
2196 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2197 	if (ret)
2198 		return ret;
2199 
2200 	ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
2201 	if (ret)
2202 		dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
2203 				hdev->name);
2204 
2205 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2206 
2207 	if (!td->is_haptic_touchpad)
2208 		devm_kfree(&hdev->dev, td->haptic);
2209 
2210 	return 0;
2211 }
2212 
2213 static int mt_suspend(struct hid_device *hdev, pm_message_t state)
2214 {
2215 	struct mt_device *td = hid_get_drvdata(hdev);
2216 
2217 	/* High latency is desirable for power savings during S3/S0ix */
2218 	if ((td->mtclass.quirks & MT_QUIRK_DISABLE_WAKEUP) ||
2219 	    !hid_hw_may_wakeup(hdev))
2220 		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
2221 	else
2222 		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_ALL);
2223 
2224 	return 0;
2225 }
2226 
2227 static int mt_reset_resume(struct hid_device *hdev)
2228 {
2229 	mt_release_contacts(hdev);
2230 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2231 	return 0;
2232 }
2233 
2234 static int mt_resume(struct hid_device *hdev)
2235 {
2236 	/* Some Elan legacy devices require SET_IDLE to be set on resume.
2237 	 * It should be safe to send it to other devices too.
2238 	 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
2239 
2240 	hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
2241 
2242 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2243 
2244 	return 0;
2245 }
2246 
2247 static void mt_remove(struct hid_device *hdev)
2248 {
2249 	struct mt_device *td = hid_get_drvdata(hdev);
2250 
2251 	timer_shutdown_sync(&td->release_timer);
2252 
2253 	sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
2254 	hid_hw_stop(hdev);
2255 }
2256 
2257 static void mt_on_hid_hw_open(struct hid_device *hdev)
2258 {
2259 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2260 }
2261 
2262 static void mt_on_hid_hw_close(struct hid_device *hdev)
2263 {
2264 	struct mt_device *td = hid_get_drvdata(hdev);
2265 
2266 	if (td->mtclass.quirks & MT_QUIRK_KEEP_LATENCY_ON_CLOSE)
2267 		mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_NONE);
2268 	else
2269 		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
2270 }
2271 
2272 /*
2273  * This list contains only:
2274  * - VID/PID of products not working with the default multitouch handling
2275  * - 2 generic rules.
2276  * So there is no point in adding here any device with MT_CLS_DEFAULT.
2277  */
2278 static const struct hid_device_id mt_devices[] = {
2279 
2280 	/* 3M panels */
2281 	{ .driver_data = MT_CLS_3M,
2282 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
2283 			USB_DEVICE_ID_3M1968) },
2284 	{ .driver_data = MT_CLS_3M,
2285 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
2286 			USB_DEVICE_ID_3M2256) },
2287 	{ .driver_data = MT_CLS_3M,
2288 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
2289 			USB_DEVICE_ID_3M3266) },
2290 
2291 	/* Anton devices */
2292 	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
2293 		MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
2294 			USB_DEVICE_ID_ANTON_TOUCH_PAD) },
2295 
2296 	/* Asus T101HA */
2297 	{ .driver_data = MT_CLS_WIN_8_DISABLE_WAKEUP,
2298 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2299 			   USB_VENDOR_ID_ASUSTEK,
2300 			   USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
2301 
2302 	/* Asus T304UA */
2303 	{ .driver_data = MT_CLS_ASUS,
2304 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2305 			USB_VENDOR_ID_ASUSTEK,
2306 			USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD) },
2307 
2308 	/* Atmel panels */
2309 	{ .driver_data = MT_CLS_SERIAL,
2310 		MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
2311 			USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
2312 
2313 	/* Baanto multitouch devices */
2314 	{ .driver_data = MT_CLS_NSMU,
2315 		MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
2316 			USB_DEVICE_ID_BAANTO_MT_190W2) },
2317 
2318 	/* Cando panels */
2319 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2320 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2321 			USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
2322 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2323 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2324 			USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
2325 
2326 	/* Chunghwa Telecom touch panels */
2327 	{  .driver_data = MT_CLS_NSMU,
2328 		MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
2329 			USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
2330 
2331 	/* CJTouch panels */
2332 	{ .driver_data = MT_CLS_NSMU,
2333 		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
2334 			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
2335 	{ .driver_data = MT_CLS_NSMU,
2336 		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
2337 			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
2338 
2339 	/* CVTouch panels */
2340 	{ .driver_data = MT_CLS_NSMU,
2341 		MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
2342 			USB_DEVICE_ID_CVTOUCH_SCREEN) },
2343 
2344 	/* eGalax devices (SAW) */
2345 	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
2346 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2347 			USB_DEVICE_ID_EGALAX_TOUCHCONTROLLER) },
2348 
2349 	/* eGalax devices (resistive) */
2350 	{ .driver_data = MT_CLS_EGALAX,
2351 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2352 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
2353 	{ .driver_data = MT_CLS_EGALAX,
2354 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2355 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
2356 
2357 	/* eGalax devices (capacitive) */
2358 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2359 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2360 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
2361 	{ .driver_data = MT_CLS_EGALAX,
2362 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2363 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
2364 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2365 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2366 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
2367 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2368 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2369 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
2370 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2371 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2372 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
2373 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2374 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2375 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
2376 	{ .driver_data = MT_CLS_EGALAX,
2377 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2378 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
2379 	{ .driver_data = MT_CLS_EGALAX,
2380 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2381 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
2382 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2383 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2384 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
2385 	{ .driver_data = MT_CLS_EGALAX,
2386 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
2387 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
2388 	{ .driver_data = MT_CLS_EGALAX,
2389 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
2390 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
2391 	{ .driver_data = MT_CLS_EGALAX,
2392 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2393 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
2394 	{ .driver_data = MT_CLS_EGALAX,
2395 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2396 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
2397 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2398 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2399 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
2400 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2401 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2402 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
2403 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2404 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2405 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
2406 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2407 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2408 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_C000) },
2409 	{ .driver_data = MT_CLS_EGALAX_P80H84,
2410 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2411 			USB_VENDOR_ID_DWAV,
2412 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_C002) },
2413 
2414 	/* Elan devices */
2415 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2416 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2417 			USB_VENDOR_ID_ELAN, 0x313a) },
2418 
2419 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2420 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2421 			USB_VENDOR_ID_ELAN, 0x3148) },
2422 
2423 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2424 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2425 			USB_VENDOR_ID_ELAN, 0x32ae) },
2426 
2427 	/* Elitegroup panel */
2428 	{ .driver_data = MT_CLS_SERIAL,
2429 		MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
2430 			USB_DEVICE_ID_ELITEGROUP_05D8) },
2431 
2432 	/* Flatfrog Panels */
2433 	{ .driver_data = MT_CLS_FLATFROG,
2434 		MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
2435 			USB_DEVICE_ID_MULTITOUCH_3200) },
2436 
2437 	/* FocalTech Panels */
2438 	{ .driver_data = MT_CLS_SERIAL,
2439 		MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
2440 			USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
2441 
2442 	/* GeneralTouch panel */
2443 	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
2444 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2445 			USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
2446 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2447 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2448 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
2449 	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
2450 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2451 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
2452 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2453 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2454 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
2455 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2456 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2457 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
2458 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2459 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2460 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
2461 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2462 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2463 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
2464 
2465 	/* Gametel game controller */
2466 	{ .driver_data = MT_CLS_NSMU,
2467 		MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
2468 			USB_DEVICE_ID_GAMETEL_MT_MODE) },
2469 
2470 	/* Goodix GT7868Q devices */
2471 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2472 	  HID_DEVICE(BUS_I2C, HID_GROUP_ANY, I2C_VENDOR_ID_GOODIX,
2473 		     I2C_DEVICE_ID_GOODIX_01E8) },
2474 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2475 	  HID_DEVICE(BUS_I2C, HID_GROUP_ANY, I2C_VENDOR_ID_GOODIX,
2476 		     I2C_DEVICE_ID_GOODIX_01E9) },
2477 
2478 	/* GoodTouch panels */
2479 	{ .driver_data = MT_CLS_NSMU,
2480 		MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
2481 			USB_DEVICE_ID_GOODTOUCH_000f) },
2482 
2483 	/* Hanvon panels */
2484 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2485 		MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
2486 			USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
2487 
2488 	/* HONOR GLO-GXXX panel */
2489 	{ .driver_data = MT_CLS_VTL,
2490 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2491 			0x347d, 0x7853) },
2492 
2493 	/* HONOR MagicBook Art 14 touchpad */
2494 	{ .driver_data = MT_CLS_VTL,
2495 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2496 			0x35cc, 0x0104) },
2497 
2498 	/* Ilitek dual touch panel */
2499 	{  .driver_data = MT_CLS_NSMU,
2500 		MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
2501 			USB_DEVICE_ID_ILITEK_MULTITOUCH) },
2502 
2503 	/* LG Melfas panel */
2504 	{ .driver_data = MT_CLS_LG,
2505 		HID_USB_DEVICE(USB_VENDOR_ID_LG,
2506 			USB_DEVICE_ID_LG_MELFAS_MT) },
2507 	{ .driver_data = MT_CLS_LG,
2508 		HID_DEVICE(BUS_I2C, HID_GROUP_GENERIC,
2509 			USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_7010) },
2510 
2511 	/* Lenovo X1 TAB Gen 1 */
2512 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2513 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2514 			   USB_VENDOR_ID_LENOVO,
2515 			   USB_DEVICE_ID_LENOVO_X1_TAB) },
2516 
2517 	/* Lenovo X1 TAB Gen 2 */
2518 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2519 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2520 			   USB_VENDOR_ID_LENOVO,
2521 			   USB_DEVICE_ID_LENOVO_X1_TAB2) },
2522 
2523 	/* Lenovo X1 TAB Gen 3 */
2524 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2525 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2526 			   USB_VENDOR_ID_LENOVO,
2527 			   USB_DEVICE_ID_LENOVO_X1_TAB3) },
2528 
2529 	/* Lenovo X12 TAB Gen 1 */
2530 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2531 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2532 			   USB_VENDOR_ID_LENOVO,
2533 			   USB_DEVICE_ID_LENOVO_X12_TAB) },
2534 
2535 	/* Lenovo X12 TAB Gen 2 */
2536 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2537 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2538 			   USB_VENDOR_ID_LENOVO,
2539 			   USB_DEVICE_ID_LENOVO_X12_TAB2) },
2540 
2541 	/* Lenovo Yoga Book 9i */
2542 	{ .driver_data = MT_CLS_YOGABOOK9I,
2543 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2544 			   USB_VENDOR_ID_LENOVO,
2545 			   USB_DEVICE_ID_LENOVO_YOGABOOK9I) },
2546 
2547 	/* Logitech devices */
2548 	{ .driver_data = MT_CLS_NSMU,
2549 		HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH_WIN_8,
2550 			USB_VENDOR_ID_LOGITECH,
2551 			USB_DEVICE_ID_LOGITECH_CASA_TOUCHPAD) },
2552 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2553 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2554 			USB_VENDOR_ID_LOGITECH,
2555 			USB_DEVICE_ID_LOGITECH_BOLT_RECEIVER) },
2556 
2557 	/* MosArt panels */
2558 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2559 		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2560 			USB_DEVICE_ID_ASUS_T91MT)},
2561 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2562 		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2563 			USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
2564 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2565 		MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
2566 			USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
2567 
2568 	/* Novatek Panel */
2569 	{ .driver_data = MT_CLS_NSMU,
2570 		MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
2571 			USB_DEVICE_ID_NOVATEK_PCT) },
2572 
2573 	/* Ntrig Panel */
2574 	{ .driver_data = MT_CLS_NSMU,
2575 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2576 			USB_VENDOR_ID_NTRIG, 0x1b05) },
2577 
2578 	/* Panasonic panels */
2579 	{ .driver_data = MT_CLS_PANASONIC,
2580 		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2581 			USB_DEVICE_ID_PANABOARD_UBT780) },
2582 	{ .driver_data = MT_CLS_PANASONIC,
2583 		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2584 			USB_DEVICE_ID_PANABOARD_UBT880) },
2585 
2586 	/* PixArt optical touch screen */
2587 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2588 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2589 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
2590 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2591 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2592 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
2593 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2594 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2595 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
2596 
2597 	/* PixCir-based panels */
2598 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2599 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2600 			USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
2601 
2602 	/* Quanta-based panels */
2603 	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
2604 		MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
2605 			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
2606 
2607 	/* Razer touchpads */
2608 	{ .driver_data = MT_CLS_RAZER_BLADE_STEALTH,
2609 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2610 			USB_VENDOR_ID_SYNAPTICS, 0x8323) },
2611 
2612 	/* Smart Tech panels */
2613 	{ .driver_data = MT_CLS_SMART_TECH,
2614 		MT_USB_DEVICE(0x0b8c, 0x0092)},
2615 
2616 	/* Stantum panels */
2617 	{ .driver_data = MT_CLS_CONFIDENCE,
2618 		MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
2619 			USB_DEVICE_ID_MTP_STM)},
2620 
2621 	/* Synaptics devices */
2622 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2623 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2624 			USB_VENDOR_ID_SYNAPTICS, 0xcd7e) },
2625 
2626 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2627 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2628 			USB_VENDOR_ID_SYNAPTICS, 0xcddc) },
2629 
2630 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2631 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2632 			USB_VENDOR_ID_SYNAPTICS, 0xce08) },
2633 
2634 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2635 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2636 			USB_VENDOR_ID_SYNAPTICS, 0xce09) },
2637 
2638 	/* TopSeed panels */
2639 	{ .driver_data = MT_CLS_TOPSEED,
2640 		MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
2641 			USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
2642 
2643 	/* Touch International panels */
2644 	{ .driver_data = MT_CLS_NSMU,
2645 		MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
2646 			USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
2647 
2648 	/* Unitec panels */
2649 	{ .driver_data = MT_CLS_NSMU,
2650 		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2651 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
2652 	{ .driver_data = MT_CLS_NSMU,
2653 		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2654 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
2655 
2656 	/* Uniwill touchpads */
2657 	{ .driver_data = MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE,
2658 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2659 			USB_VENDOR_ID_PIXART, 0x0255) },
2660 	{ .driver_data = MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE,
2661 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2662 			USB_VENDOR_ID_PIXART, 0x0274) },
2663 
2664 	/* VTL panels */
2665 	{ .driver_data = MT_CLS_VTL,
2666 		MT_USB_DEVICE(USB_VENDOR_ID_VTL,
2667 			USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
2668 
2669 	/* Winbond Electronics Corp. */
2670 	{ .driver_data = MT_CLS_WIN_8_NO_STICKY_FINGERS,
2671 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2672 			   USB_VENDOR_ID_WINBOND, USB_DEVICE_ID_TSTP_MTOUCH) },
2673 
2674 	/* Wistron panels */
2675 	{ .driver_data = MT_CLS_NSMU,
2676 		MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
2677 			USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
2678 
2679 	/* XAT */
2680 	{ .driver_data = MT_CLS_NSMU,
2681 		MT_USB_DEVICE(USB_VENDOR_ID_XAT,
2682 			USB_DEVICE_ID_XAT_CSR) },
2683 
2684 	/* Xiroku */
2685 	{ .driver_data = MT_CLS_NSMU,
2686 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2687 			USB_DEVICE_ID_XIROKU_SPX) },
2688 	{ .driver_data = MT_CLS_NSMU,
2689 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2690 			USB_DEVICE_ID_XIROKU_MPX) },
2691 	{ .driver_data = MT_CLS_NSMU,
2692 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2693 			USB_DEVICE_ID_XIROKU_CSR) },
2694 	{ .driver_data = MT_CLS_NSMU,
2695 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2696 			USB_DEVICE_ID_XIROKU_SPX1) },
2697 	{ .driver_data = MT_CLS_NSMU,
2698 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2699 			USB_DEVICE_ID_XIROKU_MPX1) },
2700 	{ .driver_data = MT_CLS_NSMU,
2701 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2702 			USB_DEVICE_ID_XIROKU_CSR1) },
2703 	{ .driver_data = MT_CLS_NSMU,
2704 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2705 			USB_DEVICE_ID_XIROKU_SPX2) },
2706 	{ .driver_data = MT_CLS_NSMU,
2707 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2708 			USB_DEVICE_ID_XIROKU_MPX2) },
2709 	{ .driver_data = MT_CLS_NSMU,
2710 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2711 			USB_DEVICE_ID_XIROKU_CSR2) },
2712 
2713 	/* Apple Touch Bar */
2714 	{ .driver_data = MT_CLS_APPLE_TOUCHBAR,
2715 		HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
2716 			USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
2717 
2718 	/* Google MT devices */
2719 	{ .driver_data = MT_CLS_GOOGLE,
2720 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_GOOGLE,
2721 			USB_DEVICE_ID_GOOGLE_TOUCH_ROSE) },
2722 	{ .driver_data = MT_CLS_GOOGLE,
2723 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, USB_VENDOR_ID_GOOGLE,
2724 			USB_DEVICE_ID_GOOGLE_WHISKERS) },
2725 
2726 	/* sis */
2727 	{ .driver_data = MT_CLS_SIS,
2728 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_SIS_TOUCH,
2729 			HID_ANY_ID) },
2730 
2731 	/* Hantick */
2732 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2733 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2734 			   I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288) },
2735 
2736 	/* Asus ROG Flow Z13 (2025) GZ302EA keyboard-cover touchpad */
2737 	{ .driver_data = MT_CLS_ASUS_ROG_Z13_FOLIO,
2738 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2739 			USB_VENDOR_ID_ASUSTEK,
2740 			USB_DEVICE_ID_ASUSTEK_ROG_Z13_FOLIO) },
2741 
2742 	/* Generic MT device */
2743 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
2744 
2745 	/* Generic Win 8 certified MT device */
2746 	{  .driver_data = MT_CLS_WIN_8,
2747 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2748 			HID_ANY_ID, HID_ANY_ID) },
2749 	{ }
2750 };
2751 MODULE_DEVICE_TABLE(hid, mt_devices);
2752 
2753 static const struct hid_usage_id mt_grabbed_usages[] = {
2754 	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
2755 	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
2756 };
2757 
2758 static struct hid_driver mt_driver = {
2759 	.name = "hid-multitouch",
2760 	.id_table = mt_devices,
2761 	.probe = mt_probe,
2762 	.remove = mt_remove,
2763 	.input_mapping = mt_input_mapping,
2764 	.input_mapped = mt_input_mapped,
2765 	.input_configured = mt_input_configured,
2766 	.feature_mapping = mt_feature_mapping,
2767 	.usage_table = mt_grabbed_usages,
2768 	.event = mt_event,
2769 	.report_fixup = mt_report_fixup,
2770 	.report = mt_report,
2771 	.suspend = pm_ptr(mt_suspend),
2772 	.reset_resume = pm_ptr(mt_reset_resume),
2773 	.resume = pm_ptr(mt_resume),
2774 	.on_hid_hw_open = mt_on_hid_hw_open,
2775 	.on_hid_hw_close = mt_on_hid_hw_close,
2776 };
2777 module_hid_driver(mt_driver);
2778