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