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