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