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