xref: /linux/drivers/hid/hid-multitouch.c (revision 02d6eeedbc36d4b309d5518778071a749ef79c4e)
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 (*size < 608) {
1507 			dev_info(
1508 				&hdev->dev,
1509 				"GT7868Q fixup: report descriptor is only %u bytes, skipping\n",
1510 				*size);
1511 			return rdesc;
1512 		}
1513 
1514 		if (rdesc[607] == 0x15) {
1515 			rdesc[607] = 0x25;
1516 			dev_info(
1517 				&hdev->dev,
1518 				"GT7868Q report descriptor fixup is applied.\n");
1519 		} else {
1520 			dev_info(
1521 				&hdev->dev,
1522 				"The byte is not expected for fixing the report descriptor. \
1523 It's possible that the touchpad firmware is not suitable for applying the fix. \
1524 got: %x\n",
1525 				rdesc[607]);
1526 		}
1527 	}
1528 
1529 	return rdesc;
1530 }
1531 
mt_report(struct hid_device * hid,struct hid_report * report)1532 static void mt_report(struct hid_device *hid, struct hid_report *report)
1533 {
1534 	struct mt_device *td = hid_get_drvdata(hid);
1535 	struct hid_field *field = report->field[0];
1536 	struct mt_report_data *rdata;
1537 
1538 	if (!(hid->claimed & HID_CLAIMED_INPUT))
1539 		return;
1540 
1541 	rdata = mt_find_report_data(td, report);
1542 	if (rdata && rdata->is_mt_collection)
1543 		return mt_touch_report(hid, rdata);
1544 
1545 	if (field && field->hidinput && field->hidinput->input)
1546 		input_sync(field->hidinput->input);
1547 }
1548 
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)1549 static bool mt_need_to_apply_feature(struct hid_device *hdev,
1550 				     struct hid_field *field,
1551 				     struct hid_usage *usage,
1552 				     enum latency_mode latency,
1553 				     enum report_mode report_mode,
1554 				     bool *inputmode_found)
1555 {
1556 	struct mt_device *td = hid_get_drvdata(hdev);
1557 	struct mt_class *cls = &td->mtclass;
1558 	struct hid_report *report = field->report;
1559 	unsigned int index = usage->usage_index;
1560 	char *buf;
1561 	u32 report_len;
1562 	int max;
1563 
1564 	switch (usage->hid) {
1565 	case HID_DG_INPUTMODE:
1566 		/*
1567 		 * Some elan panels wrongly declare 2 input mode features,
1568 		 * and silently ignore when we set the value in the second
1569 		 * field. Skip the second feature and hope for the best.
1570 		 */
1571 		if (*inputmode_found)
1572 			return false;
1573 
1574 		if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
1575 			report_len = hid_report_len(report);
1576 			buf = hid_alloc_report_buf(report, GFP_KERNEL);
1577 			if (!buf) {
1578 				hid_err(hdev,
1579 					"failed to allocate buffer for report\n");
1580 				return false;
1581 			}
1582 			hid_hw_raw_request(hdev, report->id, buf, report_len,
1583 					   HID_FEATURE_REPORT,
1584 					   HID_REQ_GET_REPORT);
1585 			kfree(buf);
1586 		}
1587 
1588 		field->value[index] = td->inputmode_value;
1589 		*inputmode_found = true;
1590 		return true;
1591 
1592 	case HID_DG_CONTACTMAX:
1593 		if (cls->maxcontacts) {
1594 			max = min_t(int, field->logical_maximum,
1595 				    cls->maxcontacts);
1596 			if (field->value[index] != max) {
1597 				field->value[index] = max;
1598 				return true;
1599 			}
1600 		}
1601 		break;
1602 
1603 	case HID_DG_LATENCYMODE:
1604 		field->value[index] = latency;
1605 		return true;
1606 
1607 	case HID_DG_SURFACESWITCH:
1608 		field->value[index] = !!(report_mode & TOUCHPAD_REPORT_CONTACTS);
1609 		return true;
1610 
1611 	case HID_DG_BUTTONSWITCH:
1612 		field->value[index] = !!(report_mode & TOUCHPAD_REPORT_BUTTONS);
1613 		return true;
1614 	}
1615 
1616 	return false; /* no need to update the report */
1617 }
1618 
mt_set_modes(struct hid_device * hdev,enum latency_mode latency,enum report_mode report_mode)1619 static void mt_set_modes(struct hid_device *hdev, enum latency_mode latency,
1620 			 enum report_mode report_mode)
1621 {
1622 	struct hid_report_enum *rep_enum;
1623 	struct hid_report *rep;
1624 	struct hid_usage *usage;
1625 	int i, j;
1626 	bool update_report;
1627 	bool inputmode_found = false;
1628 
1629 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1630 	list_for_each_entry(rep, &rep_enum->report_list, list) {
1631 		update_report = false;
1632 
1633 		for (i = 0; i < rep->maxfield; i++) {
1634 			/* Ignore if report count is out of bounds. */
1635 			if (rep->field[i]->report_count < 1)
1636 				continue;
1637 
1638 			for (j = 0; j < rep->field[i]->maxusage; j++) {
1639 				usage = &rep->field[i]->usage[j];
1640 
1641 				if (mt_need_to_apply_feature(hdev,
1642 							     rep->field[i],
1643 							     usage,
1644 							     latency,
1645 							     report_mode,
1646 							     &inputmode_found))
1647 					update_report = true;
1648 			}
1649 		}
1650 
1651 		if (update_report)
1652 			hid_hw_request(hdev, rep, HID_REQ_SET_REPORT);
1653 	}
1654 }
1655 
mt_post_parse_default_settings(struct mt_device * td,struct mt_application * app)1656 static void mt_post_parse_default_settings(struct mt_device *td,
1657 					   struct mt_application *app)
1658 {
1659 	__s32 quirks = app->quirks;
1660 
1661 	/* unknown serial device needs special quirks */
1662 	if (list_is_singular(&app->mt_usages)) {
1663 		quirks |= MT_QUIRK_ALWAYS_VALID;
1664 		quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1665 		quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1666 		quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1667 		quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1668 	}
1669 
1670 	app->quirks = quirks;
1671 }
1672 
mt_post_parse(struct mt_device * td,struct mt_application * app)1673 static void mt_post_parse(struct mt_device *td, struct mt_application *app)
1674 {
1675 	if (!app->have_contact_count)
1676 		app->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1677 }
1678 
mt_input_configured(struct hid_device * hdev,struct hid_input * hi)1679 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1680 {
1681 	struct mt_device *td = hid_get_drvdata(hdev);
1682 	const char *suffix = NULL;
1683 	struct mt_report_data *rdata;
1684 	struct mt_application *mt_application = NULL;
1685 	struct hid_report *report;
1686 	int ret;
1687 
1688 	list_for_each_entry(report, &hi->reports, hidinput_list) {
1689 		rdata = mt_find_report_data(td, report);
1690 		if (!rdata) {
1691 			hid_err(hdev, "failed to allocate data for report\n");
1692 			return -ENOMEM;
1693 		}
1694 
1695 		mt_application = rdata->application;
1696 
1697 		if (rdata->is_mt_collection) {
1698 			ret = mt_touch_input_configured(hdev, hi,
1699 							mt_application);
1700 			if (ret)
1701 				return ret;
1702 		}
1703 	}
1704 
1705 	switch (hi->application) {
1706 	case HID_GD_KEYBOARD:
1707 	case HID_GD_KEYPAD:
1708 	case HID_GD_MOUSE:
1709 	case HID_DG_TOUCHPAD:
1710 	case HID_GD_SYSTEM_CONTROL:
1711 	case HID_CP_CONSUMER_CONTROL:
1712 	case HID_GD_WIRELESS_RADIO_CTLS:
1713 	case HID_GD_SYSTEM_MULTIAXIS:
1714 		/* already handled by hid core */
1715 		break;
1716 	case HID_DG_TOUCHSCREEN:
1717 		/* we do not set suffix = "Touchscreen" */
1718 		hi->input->name = hdev->name;
1719 		break;
1720 	case HID_VD_ASUS_CUSTOM_MEDIA_KEYS:
1721 		suffix = "Custom Media Keys";
1722 		break;
1723 	case HID_DG_STYLUS:
1724 		/* force BTN_STYLUS to allow tablet matching in udev */
1725 		__set_bit(BTN_STYLUS, hi->input->keybit);
1726 		break;
1727 	default:
1728 		suffix = "UNKNOWN";
1729 		break;
1730 	}
1731 
1732 	if (suffix) {
1733 		hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
1734 						 "%s %s", hdev->name, suffix);
1735 		if (!hi->input->name)
1736 			return -ENOMEM;
1737 	}
1738 
1739 	return 0;
1740 }
1741 
mt_fix_const_field(struct hid_field * field,unsigned int usage)1742 static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
1743 {
1744 	if (field->usage[0].hid != usage ||
1745 	    !(field->flags & HID_MAIN_ITEM_CONSTANT))
1746 		return;
1747 
1748 	field->flags &= ~HID_MAIN_ITEM_CONSTANT;
1749 	field->flags |= HID_MAIN_ITEM_VARIABLE;
1750 }
1751 
mt_fix_const_fields(struct hid_device * hdev,unsigned int usage)1752 static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
1753 {
1754 	struct hid_report *report;
1755 	int i;
1756 
1757 	list_for_each_entry(report,
1758 			    &hdev->report_enum[HID_INPUT_REPORT].report_list,
1759 			    list) {
1760 
1761 		if (!report->maxfield)
1762 			continue;
1763 
1764 		for (i = 0; i < report->maxfield; i++)
1765 			if (report->field[i]->maxusage >= 1)
1766 				mt_fix_const_field(report->field[i], usage);
1767 	}
1768 }
1769 
mt_release_contacts(struct hid_device * hid)1770 static void mt_release_contacts(struct hid_device *hid)
1771 {
1772 	struct hid_input *hidinput;
1773 	struct mt_application *application;
1774 	struct mt_device *td = hid_get_drvdata(hid);
1775 
1776 	list_for_each_entry(hidinput, &hid->inputs, list) {
1777 		struct input_dev *input_dev = hidinput->input;
1778 		struct input_mt *mt = input_dev->mt;
1779 		int i;
1780 
1781 		if (mt) {
1782 			for (i = 0; i < mt->num_slots; i++) {
1783 				input_mt_slot(input_dev, i);
1784 				input_mt_report_slot_inactive(input_dev);
1785 			}
1786 			input_mt_sync_frame(input_dev);
1787 			input_sync(input_dev);
1788 		}
1789 	}
1790 
1791 	list_for_each_entry(application, &td->applications, list) {
1792 		application->num_received = 0;
1793 	}
1794 }
1795 
mt_expired_timeout(struct timer_list * t)1796 static void mt_expired_timeout(struct timer_list *t)
1797 {
1798 	struct mt_device *td = timer_container_of(td, t, release_timer);
1799 	struct hid_device *hdev = td->hdev;
1800 
1801 	/*
1802 	 * An input report came in just before we release the sticky fingers,
1803 	 * it will take care of the sticky fingers.
1804 	 */
1805 	if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1806 		return;
1807 	if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1808 		mt_release_contacts(hdev);
1809 	clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1810 }
1811 
mt_probe(struct hid_device * hdev,const struct hid_device_id * id)1812 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1813 {
1814 	int ret, i;
1815 	struct mt_device *td;
1816 	const struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1817 
1818 	for (i = 0; mt_classes[i].name ; i++) {
1819 		if (id->driver_data == mt_classes[i].name) {
1820 			mtclass = &(mt_classes[i]);
1821 			break;
1822 		}
1823 	}
1824 
1825 	td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1826 	if (!td) {
1827 		dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1828 		return -ENOMEM;
1829 	}
1830 	td->hdev = hdev;
1831 	td->mtclass = *mtclass;
1832 	td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1833 	hid_set_drvdata(hdev, td);
1834 
1835 	INIT_LIST_HEAD(&td->applications);
1836 	INIT_LIST_HEAD(&td->reports);
1837 
1838 	if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1839 		td->serial_maybe = true;
1840 
1841 
1842 	/* Orientation is inverted if the X or Y axes are
1843 	 * flipped, but normalized if both are inverted.
1844 	 */
1845 	if (hdev->quirks & (HID_QUIRK_X_INVERT | HID_QUIRK_Y_INVERT) &&
1846 	    !((hdev->quirks & HID_QUIRK_X_INVERT)
1847 	      && (hdev->quirks & HID_QUIRK_Y_INVERT)))
1848 		td->mtclass.quirks = MT_QUIRK_ORIENTATION_INVERT;
1849 
1850 	/* This allows the driver to correctly support devices
1851 	 * that emit events over several HID messages.
1852 	 */
1853 	hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1854 
1855 	/*
1856 	 * This allows the driver to handle different input sensors
1857 	 * that emits events through different applications on the same HID
1858 	 * device.
1859 	 */
1860 	hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1861 
1862 	if (id->group != HID_GROUP_MULTITOUCH_WIN_8)
1863 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1864 
1865 	if (mtclass->quirks & MT_QUIRK_FORCE_MULTI_INPUT) {
1866 		hdev->quirks &= ~HID_QUIRK_INPUT_PER_APP;
1867 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1868 	}
1869 
1870 	timer_setup(&td->release_timer, mt_expired_timeout, 0);
1871 
1872 	ret = hid_parse(hdev);
1873 	if (ret != 0)
1874 		return ret;
1875 
1876 	if (mtclass->name == MT_CLS_APPLE_TOUCHBAR &&
1877 	    !hid_find_field(hdev, HID_INPUT_REPORT,
1878 			    HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX))
1879 		return -ENODEV;
1880 
1881 	if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
1882 		mt_fix_const_fields(hdev, HID_DG_CONTACTID);
1883 
1884 	if (hdev->vendor == USB_VENDOR_ID_SIS_TOUCH)
1885 		hdev->quirks |= HID_QUIRK_NOGET;
1886 
1887 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1888 	if (ret)
1889 		return ret;
1890 
1891 	ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
1892 	if (ret)
1893 		dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
1894 				hdev->name);
1895 
1896 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
1897 
1898 	return 0;
1899 }
1900 
mt_suspend(struct hid_device * hdev,pm_message_t state)1901 static int mt_suspend(struct hid_device *hdev, pm_message_t state)
1902 {
1903 	struct mt_device *td = hid_get_drvdata(hdev);
1904 
1905 	/* High latency is desirable for power savings during S3/S0ix */
1906 	if ((td->mtclass.quirks & MT_QUIRK_DISABLE_WAKEUP) ||
1907 	    !hid_hw_may_wakeup(hdev))
1908 		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
1909 	else
1910 		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_ALL);
1911 
1912 	return 0;
1913 }
1914 
mt_reset_resume(struct hid_device * hdev)1915 static int mt_reset_resume(struct hid_device *hdev)
1916 {
1917 	mt_release_contacts(hdev);
1918 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
1919 	return 0;
1920 }
1921 
mt_resume(struct hid_device * hdev)1922 static int mt_resume(struct hid_device *hdev)
1923 {
1924 	/* Some Elan legacy devices require SET_IDLE to be set on resume.
1925 	 * It should be safe to send it to other devices too.
1926 	 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
1927 
1928 	hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
1929 
1930 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
1931 
1932 	return 0;
1933 }
1934 
mt_remove(struct hid_device * hdev)1935 static void mt_remove(struct hid_device *hdev)
1936 {
1937 	struct mt_device *td = hid_get_drvdata(hdev);
1938 
1939 	timer_delete_sync(&td->release_timer);
1940 
1941 	sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
1942 	hid_hw_stop(hdev);
1943 }
1944 
mt_on_hid_hw_open(struct hid_device * hdev)1945 static void mt_on_hid_hw_open(struct hid_device *hdev)
1946 {
1947 	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
1948 }
1949 
mt_on_hid_hw_close(struct hid_device * hdev)1950 static void mt_on_hid_hw_close(struct hid_device *hdev)
1951 {
1952 	mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
1953 }
1954 
1955 /*
1956  * This list contains only:
1957  * - VID/PID of products not working with the default multitouch handling
1958  * - 2 generic rules.
1959  * So there is no point in adding here any device with MT_CLS_DEFAULT.
1960  */
1961 static const struct hid_device_id mt_devices[] = {
1962 
1963 	/* 3M panels */
1964 	{ .driver_data = MT_CLS_3M,
1965 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
1966 			USB_DEVICE_ID_3M1968) },
1967 	{ .driver_data = MT_CLS_3M,
1968 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
1969 			USB_DEVICE_ID_3M2256) },
1970 	{ .driver_data = MT_CLS_3M,
1971 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
1972 			USB_DEVICE_ID_3M3266) },
1973 
1974 	/* Anton devices */
1975 	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
1976 		MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
1977 			USB_DEVICE_ID_ANTON_TOUCH_PAD) },
1978 
1979 	/* Asus T101HA */
1980 	{ .driver_data = MT_CLS_WIN_8_DISABLE_WAKEUP,
1981 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1982 			   USB_VENDOR_ID_ASUSTEK,
1983 			   USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
1984 
1985 	/* Asus T304UA */
1986 	{ .driver_data = MT_CLS_ASUS,
1987 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1988 			USB_VENDOR_ID_ASUSTEK,
1989 			USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD) },
1990 
1991 	/* Atmel panels */
1992 	{ .driver_data = MT_CLS_SERIAL,
1993 		MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
1994 			USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
1995 
1996 	/* Baanto multitouch devices */
1997 	{ .driver_data = MT_CLS_NSMU,
1998 		MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
1999 			USB_DEVICE_ID_BAANTO_MT_190W2) },
2000 
2001 	/* Cando panels */
2002 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2003 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2004 			USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
2005 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2006 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2007 			USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
2008 
2009 	/* Chunghwa Telecom touch panels */
2010 	{  .driver_data = MT_CLS_NSMU,
2011 		MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
2012 			USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
2013 
2014 	/* CJTouch panels */
2015 	{ .driver_data = MT_CLS_NSMU,
2016 		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
2017 			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
2018 	{ .driver_data = MT_CLS_NSMU,
2019 		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
2020 			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
2021 
2022 	/* CVTouch panels */
2023 	{ .driver_data = MT_CLS_NSMU,
2024 		MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
2025 			USB_DEVICE_ID_CVTOUCH_SCREEN) },
2026 
2027 	/* eGalax devices (SAW) */
2028 	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
2029 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2030 			USB_DEVICE_ID_EGALAX_TOUCHCONTROLLER) },
2031 
2032 	/* eGalax devices (resistive) */
2033 	{ .driver_data = MT_CLS_EGALAX,
2034 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2035 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
2036 	{ .driver_data = MT_CLS_EGALAX,
2037 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2038 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
2039 
2040 	/* eGalax devices (capacitive) */
2041 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2042 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2043 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
2044 	{ .driver_data = MT_CLS_EGALAX,
2045 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2046 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
2047 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2048 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2049 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
2050 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2051 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2052 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
2053 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2054 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2055 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
2056 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2057 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2058 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
2059 	{ .driver_data = MT_CLS_EGALAX,
2060 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2061 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
2062 	{ .driver_data = MT_CLS_EGALAX,
2063 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2064 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
2065 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2066 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2067 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
2068 	{ .driver_data = MT_CLS_EGALAX,
2069 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
2070 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
2071 	{ .driver_data = MT_CLS_EGALAX,
2072 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
2073 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
2074 	{ .driver_data = MT_CLS_EGALAX,
2075 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2076 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
2077 	{ .driver_data = MT_CLS_EGALAX,
2078 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2079 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
2080 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2081 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2082 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
2083 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2084 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2085 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
2086 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2087 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2088 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
2089 	{ .driver_data = MT_CLS_EGALAX,
2090 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2091 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_C002) },
2092 
2093 	/* Elan devices */
2094 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2095 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2096 			USB_VENDOR_ID_ELAN, 0x313a) },
2097 
2098 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2099 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2100 			USB_VENDOR_ID_ELAN, 0x3148) },
2101 
2102 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2103 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2104 			USB_VENDOR_ID_ELAN, 0x32ae) },
2105 
2106 	/* Elitegroup panel */
2107 	{ .driver_data = MT_CLS_SERIAL,
2108 		MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
2109 			USB_DEVICE_ID_ELITEGROUP_05D8) },
2110 
2111 	/* Flatfrog Panels */
2112 	{ .driver_data = MT_CLS_FLATFROG,
2113 		MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
2114 			USB_DEVICE_ID_MULTITOUCH_3200) },
2115 
2116 	/* FocalTech Panels */
2117 	{ .driver_data = MT_CLS_SERIAL,
2118 		MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
2119 			USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
2120 
2121 	/* GeneralTouch panel */
2122 	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
2123 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2124 			USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
2125 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2126 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2127 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
2128 	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
2129 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2130 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
2131 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2132 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2133 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
2134 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2135 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2136 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
2137 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2138 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2139 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
2140 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2141 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2142 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
2143 
2144 	/* Gametel game controller */
2145 	{ .driver_data = MT_CLS_NSMU,
2146 		MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
2147 			USB_DEVICE_ID_GAMETEL_MT_MODE) },
2148 
2149 	/* Goodix GT7868Q devices */
2150 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2151 	  HID_DEVICE(BUS_I2C, HID_GROUP_ANY, I2C_VENDOR_ID_GOODIX,
2152 		     I2C_DEVICE_ID_GOODIX_01E8) },
2153 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2154 	  HID_DEVICE(BUS_I2C, HID_GROUP_ANY, I2C_VENDOR_ID_GOODIX,
2155 		     I2C_DEVICE_ID_GOODIX_01E9) },
2156 
2157 	/* GoodTouch panels */
2158 	{ .driver_data = MT_CLS_NSMU,
2159 		MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
2160 			USB_DEVICE_ID_GOODTOUCH_000f) },
2161 
2162 	/* Hanvon panels */
2163 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2164 		MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
2165 			USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
2166 
2167 	/* HONOR GLO-GXXX panel */
2168 	{ .driver_data = MT_CLS_VTL,
2169 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2170 			0x347d, 0x7853) },
2171 
2172 	/* HONOR MagicBook Art 14 touchpad */
2173 	{ .driver_data = MT_CLS_VTL,
2174 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2175 			0x35cc, 0x0104) },
2176 
2177 	/* Ilitek dual touch panel */
2178 	{  .driver_data = MT_CLS_NSMU,
2179 		MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
2180 			USB_DEVICE_ID_ILITEK_MULTITOUCH) },
2181 
2182 	/* LG Melfas panel */
2183 	{ .driver_data = MT_CLS_LG,
2184 		HID_USB_DEVICE(USB_VENDOR_ID_LG,
2185 			USB_DEVICE_ID_LG_MELFAS_MT) },
2186 	{ .driver_data = MT_CLS_LG,
2187 		HID_DEVICE(BUS_I2C, HID_GROUP_GENERIC,
2188 			USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_7010) },
2189 
2190 	/* Lenovo X1 TAB Gen 1 */
2191 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2192 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2193 			   USB_VENDOR_ID_LENOVO,
2194 			   USB_DEVICE_ID_LENOVO_X1_TAB) },
2195 
2196 	/* Lenovo X1 TAB Gen 2 */
2197 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2198 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2199 			   USB_VENDOR_ID_LENOVO,
2200 			   USB_DEVICE_ID_LENOVO_X1_TAB2) },
2201 
2202 	/* Lenovo X1 TAB Gen 3 */
2203 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2204 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2205 			   USB_VENDOR_ID_LENOVO,
2206 			   USB_DEVICE_ID_LENOVO_X1_TAB3) },
2207 
2208 	/* Lenovo X12 TAB Gen 1 */
2209 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2210 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2211 			   USB_VENDOR_ID_LENOVO,
2212 			   USB_DEVICE_ID_LENOVO_X12_TAB) },
2213 
2214 	/* Lenovo X12 TAB Gen 2 */
2215 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2216 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2217 			   USB_VENDOR_ID_LENOVO,
2218 			   USB_DEVICE_ID_LENOVO_X12_TAB2) },
2219 
2220 	/* Logitech devices */
2221 	{ .driver_data = MT_CLS_NSMU,
2222 		HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH_WIN_8,
2223 			USB_VENDOR_ID_LOGITECH,
2224 			USB_DEVICE_ID_LOGITECH_CASA_TOUCHPAD) },
2225 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2226 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2227 			USB_VENDOR_ID_LOGITECH,
2228 			USB_DEVICE_ID_LOGITECH_BOLT_RECEIVER) },
2229 
2230 	/* MosArt panels */
2231 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2232 		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2233 			USB_DEVICE_ID_ASUS_T91MT)},
2234 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2235 		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2236 			USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
2237 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2238 		MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
2239 			USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
2240 
2241 	/* Novatek Panel */
2242 	{ .driver_data = MT_CLS_NSMU,
2243 		MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
2244 			USB_DEVICE_ID_NOVATEK_PCT) },
2245 
2246 	/* Ntrig Panel */
2247 	{ .driver_data = MT_CLS_NSMU,
2248 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2249 			USB_VENDOR_ID_NTRIG, 0x1b05) },
2250 
2251 	/* Panasonic panels */
2252 	{ .driver_data = MT_CLS_PANASONIC,
2253 		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2254 			USB_DEVICE_ID_PANABOARD_UBT780) },
2255 	{ .driver_data = MT_CLS_PANASONIC,
2256 		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2257 			USB_DEVICE_ID_PANABOARD_UBT880) },
2258 
2259 	/* PixArt optical touch screen */
2260 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2261 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2262 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
2263 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2264 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2265 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
2266 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2267 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2268 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
2269 
2270 	/* PixCir-based panels */
2271 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2272 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2273 			USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
2274 
2275 	/* Quanta-based panels */
2276 	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
2277 		MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
2278 			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
2279 
2280 	/* Razer touchpads */
2281 	{ .driver_data = MT_CLS_RAZER_BLADE_STEALTH,
2282 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2283 			USB_VENDOR_ID_SYNAPTICS, 0x8323) },
2284 
2285 	/* Smart Tech panels */
2286 	{ .driver_data = MT_CLS_SMART_TECH,
2287 		MT_USB_DEVICE(0x0b8c, 0x0092)},
2288 
2289 	/* Stantum panels */
2290 	{ .driver_data = MT_CLS_CONFIDENCE,
2291 		MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
2292 			USB_DEVICE_ID_MTP_STM)},
2293 
2294 	/* Synaptics devices */
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, 0xcd7e) },
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, 0xcddc) },
2302 
2303 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2304 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2305 			USB_VENDOR_ID_SYNAPTICS, 0xce08) },
2306 
2307 	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2308 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2309 			USB_VENDOR_ID_SYNAPTICS, 0xce09) },
2310 
2311 	/* TopSeed panels */
2312 	{ .driver_data = MT_CLS_TOPSEED,
2313 		MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
2314 			USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
2315 
2316 	/* Touch International panels */
2317 	{ .driver_data = MT_CLS_NSMU,
2318 		MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
2319 			USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
2320 
2321 	/* Unitec panels */
2322 	{ .driver_data = MT_CLS_NSMU,
2323 		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2324 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
2325 	{ .driver_data = MT_CLS_NSMU,
2326 		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2327 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
2328 
2329 	/* VTL panels */
2330 	{ .driver_data = MT_CLS_VTL,
2331 		MT_USB_DEVICE(USB_VENDOR_ID_VTL,
2332 			USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
2333 
2334 	/* Winbond Electronics Corp. */
2335 	{ .driver_data = MT_CLS_WIN_8_NO_STICKY_FINGERS,
2336 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2337 			   USB_VENDOR_ID_WINBOND, USB_DEVICE_ID_TSTP_MTOUCH) },
2338 
2339 	/* Wistron panels */
2340 	{ .driver_data = MT_CLS_NSMU,
2341 		MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
2342 			USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
2343 
2344 	/* XAT */
2345 	{ .driver_data = MT_CLS_NSMU,
2346 		MT_USB_DEVICE(USB_VENDOR_ID_XAT,
2347 			USB_DEVICE_ID_XAT_CSR) },
2348 
2349 	/* Xiroku */
2350 	{ .driver_data = MT_CLS_NSMU,
2351 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2352 			USB_DEVICE_ID_XIROKU_SPX) },
2353 	{ .driver_data = MT_CLS_NSMU,
2354 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2355 			USB_DEVICE_ID_XIROKU_MPX) },
2356 	{ .driver_data = MT_CLS_NSMU,
2357 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2358 			USB_DEVICE_ID_XIROKU_CSR) },
2359 	{ .driver_data = MT_CLS_NSMU,
2360 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2361 			USB_DEVICE_ID_XIROKU_SPX1) },
2362 	{ .driver_data = MT_CLS_NSMU,
2363 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2364 			USB_DEVICE_ID_XIROKU_MPX1) },
2365 	{ .driver_data = MT_CLS_NSMU,
2366 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2367 			USB_DEVICE_ID_XIROKU_CSR1) },
2368 	{ .driver_data = MT_CLS_NSMU,
2369 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2370 			USB_DEVICE_ID_XIROKU_SPX2) },
2371 	{ .driver_data = MT_CLS_NSMU,
2372 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2373 			USB_DEVICE_ID_XIROKU_MPX2) },
2374 	{ .driver_data = MT_CLS_NSMU,
2375 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2376 			USB_DEVICE_ID_XIROKU_CSR2) },
2377 
2378 	/* Apple Touch Bar */
2379 	{ .driver_data = MT_CLS_APPLE_TOUCHBAR,
2380 		HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
2381 			USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
2382 
2383 	/* Google MT devices */
2384 	{ .driver_data = MT_CLS_GOOGLE,
2385 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_GOOGLE,
2386 			USB_DEVICE_ID_GOOGLE_TOUCH_ROSE) },
2387 	{ .driver_data = MT_CLS_GOOGLE,
2388 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, USB_VENDOR_ID_GOOGLE,
2389 			USB_DEVICE_ID_GOOGLE_WHISKERS) },
2390 
2391 	/* sis */
2392 	{ .driver_data = MT_CLS_SIS,
2393 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_SIS_TOUCH,
2394 			HID_ANY_ID) },
2395 
2396 	/* Hantick */
2397 	{ .driver_data = MT_CLS_NSMU,
2398 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2399 			   I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288) },
2400 
2401 	/* Generic MT device */
2402 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
2403 
2404 	/* Generic Win 8 certified MT device */
2405 	{  .driver_data = MT_CLS_WIN_8,
2406 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2407 			HID_ANY_ID, HID_ANY_ID) },
2408 	{ }
2409 };
2410 MODULE_DEVICE_TABLE(hid, mt_devices);
2411 
2412 static const struct hid_usage_id mt_grabbed_usages[] = {
2413 	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
2414 	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
2415 };
2416 
2417 static struct hid_driver mt_driver = {
2418 	.name = "hid-multitouch",
2419 	.id_table = mt_devices,
2420 	.probe = mt_probe,
2421 	.remove = mt_remove,
2422 	.input_mapping = mt_input_mapping,
2423 	.input_mapped = mt_input_mapped,
2424 	.input_configured = mt_input_configured,
2425 	.feature_mapping = mt_feature_mapping,
2426 	.usage_table = mt_grabbed_usages,
2427 	.event = mt_event,
2428 	.report_fixup = mt_report_fixup,
2429 	.report = mt_report,
2430 	.suspend = pm_ptr(mt_suspend),
2431 	.reset_resume = pm_ptr(mt_reset_resume),
2432 	.resume = pm_ptr(mt_resume),
2433 	.on_hid_hw_open = mt_on_hid_hw_open,
2434 	.on_hid_hw_close = mt_on_hid_hw_close,
2435 };
2436 module_hid_driver(mt_driver);
2437