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