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