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