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