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