xref: /openbmc/linux/drivers/hid/hid-multitouch.c (revision 2c363576)
1 /*
2  *  HID driver for multitouch panels
3  *
4  *  Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
5  *  Copyright (c) 2010-2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6  *  Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
7  *  Copyright (c) 2012-2013 Red Hat, Inc
8  *
9  *  This code is partly based on hid-egalax.c:
10  *
11  *  Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
12  *  Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
13  *  Copyright (c) 2010 Canonical, Ltd.
14  *
15  *  This code is partly based on hid-3m-pct.c:
16  *
17  *  Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
18  *  Copyright (c) 2010      Henrik Rydberg <rydberg@euromail.se>
19  *  Copyright (c) 2010      Canonical, Ltd.
20  *
21  */
22 
23 /*
24  * This program is free software; you can redistribute it and/or modify it
25  * under the terms of the GNU General Public License as published by the Free
26  * Software Foundation; either version 2 of the License, or (at your option)
27  * any later version.
28  */
29 
30 /*
31  * This driver is regularly tested thanks to the tool hid-test[1].
32  * This tool relies on hid-replay[2] and a database of hid devices[3].
33  * Please run these regression tests before patching this module so that
34  * your patch won't break existing known devices.
35  *
36  * [1] https://github.com/bentiss/hid-test
37  * [2] https://github.com/bentiss/hid-replay
38  * [3] https://github.com/bentiss/hid-devices
39  */
40 
41 #include <linux/device.h>
42 #include <linux/hid.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/input/mt.h>
46 #include <linux/jiffies.h>
47 #include <linux/string.h>
48 #include <linux/timer.h>
49 
50 
51 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
52 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
53 MODULE_DESCRIPTION("HID multitouch panels");
54 MODULE_LICENSE("GPL");
55 
56 #include "hid-ids.h"
57 
58 /* quirks to control the device */
59 #define MT_QUIRK_NOT_SEEN_MEANS_UP	BIT(0)
60 #define MT_QUIRK_SLOT_IS_CONTACTID	BIT(1)
61 #define MT_QUIRK_CYPRESS		BIT(2)
62 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER	BIT(3)
63 #define MT_QUIRK_ALWAYS_VALID		BIT(4)
64 #define MT_QUIRK_VALID_IS_INRANGE	BIT(5)
65 #define MT_QUIRK_VALID_IS_CONFIDENCE	BIT(6)
66 #define MT_QUIRK_CONFIDENCE		BIT(7)
67 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE	BIT(8)
68 #define MT_QUIRK_NO_AREA		BIT(9)
69 #define MT_QUIRK_IGNORE_DUPLICATES	BIT(10)
70 #define MT_QUIRK_HOVERING		BIT(11)
71 #define MT_QUIRK_CONTACT_CNT_ACCURATE	BIT(12)
72 #define MT_QUIRK_FORCE_GET_FEATURE	BIT(13)
73 #define MT_QUIRK_FIX_CONST_CONTACT_ID	BIT(14)
74 #define MT_QUIRK_TOUCH_SIZE_SCALING	BIT(15)
75 #define MT_QUIRK_STICKY_FINGERS		BIT(16)
76 #define MT_QUIRK_ASUS_CUSTOM_UP		BIT(17)
77 #define MT_QUIRK_WIN8_PTP_BUTTONS	BIT(18)
78 
79 #define MT_INPUTMODE_TOUCHSCREEN	0x02
80 #define MT_INPUTMODE_TOUCHPAD		0x03
81 
82 #define MT_BUTTONTYPE_CLICKPAD		0
83 
84 #define MT_IO_FLAGS_RUNNING		0
85 #define MT_IO_FLAGS_ACTIVE_SLOTS	1
86 #define MT_IO_FLAGS_PENDING_SLOTS	2
87 
88 struct mt_slot {
89 	__s32 x, y, cx, cy, p, w, h, a;
90 	__s32 contactid;	/* the device ContactID assigned to this slot */
91 	bool touch_state;	/* is the touch valid? */
92 	bool inrange_state;	/* is the finger in proximity of the sensor? */
93 	bool confidence_state;  /* is the touch made by a finger? */
94 	bool has_azimuth;       /* the contact reports azimuth */
95 };
96 
97 struct mt_class {
98 	__s32 name;	/* MT_CLS */
99 	__s32 quirks;
100 	__s32 sn_move;	/* Signal/noise ratio for move events */
101 	__s32 sn_width;	/* Signal/noise ratio for width events */
102 	__s32 sn_height;	/* Signal/noise ratio for height events */
103 	__s32 sn_pressure;	/* Signal/noise ratio for pressure events */
104 	__u8 maxcontacts;
105 	bool is_indirect;	/* true for touchpads */
106 	bool export_all_inputs;	/* do not ignore mouse, keyboards, etc... */
107 };
108 
109 struct mt_fields {
110 	unsigned usages[HID_MAX_FIELDS];
111 	unsigned int length;
112 };
113 
114 struct mt_device {
115 	struct mt_slot curdata;	/* placeholder of incoming data */
116 	struct mt_class mtclass;	/* our mt device class */
117 	struct timer_list release_timer;	/* to release sticky fingers */
118 	struct hid_device *hdev;	/* hid_device we're attached to */
119 	struct mt_fields *fields;	/* temporary placeholder for storing the
120 					   multitouch fields */
121 	unsigned long mt_io_flags;	/* mt flags (MT_IO_FLAGS_*) */
122 	int cc_index;	/* contact count field index in the report */
123 	int cc_value_index;	/* contact count value index in the field */
124 	int scantime_index;	/* scantime field index in the report */
125 	int scantime_val_index;	/* scantime value index in the field */
126 	int prev_scantime;	/* scantime reported in the previous packet */
127 	int left_button_state;	/* left button state */
128 	unsigned last_slot_field;	/* the last field of a slot */
129 	unsigned mt_report_id;	/* the report ID of the multitouch device */
130 	__s16 inputmode;	/* InputMode HID feature, -1 if non-existent */
131 	__s16 inputmode_index;	/* InputMode HID feature index in the report */
132 	__s16 maxcontact_report_id;	/* Maximum Contact Number HID feature,
133 				   -1 if non-existent */
134 	__u8 inputmode_value;  /* InputMode HID feature value */
135 	__u8 num_received;	/* how many contacts we received */
136 	__u8 num_expected;	/* expected last contact index */
137 	__u8 maxcontacts;
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 	__u8 buttons_count;	/* number of physical buttons per touchpad */
142 	bool is_buttonpad;	/* is this device a button pad? */
143 	bool serial_maybe;	/* need to check for serial protocol */
144 	bool curvalid;		/* is the current contact valid? */
145 	unsigned mt_flags;	/* flags to pass to input-mt */
146 	__s32 dev_time;		/* the scan time provided by the device */
147 	unsigned long jiffies;	/* the frame's jiffies */
148 	int timestamp;		/* the timestamp to be sent */
149 };
150 
151 static void mt_post_parse_default_settings(struct mt_device *td);
152 static void mt_post_parse(struct mt_device *td);
153 
154 /* classes of device behavior */
155 #define MT_CLS_DEFAULT				0x0001
156 
157 #define MT_CLS_SERIAL				0x0002
158 #define MT_CLS_CONFIDENCE			0x0003
159 #define MT_CLS_CONFIDENCE_CONTACT_ID		0x0004
160 #define MT_CLS_CONFIDENCE_MINUS_ONE		0x0005
161 #define MT_CLS_DUAL_INRANGE_CONTACTID		0x0006
162 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER	0x0007
163 /* reserved					0x0008 */
164 #define MT_CLS_INRANGE_CONTACTNUMBER		0x0009
165 #define MT_CLS_NSMU				0x000a
166 /* reserved					0x0010 */
167 /* reserved					0x0011 */
168 #define MT_CLS_WIN_8				0x0012
169 #define MT_CLS_EXPORT_ALL_INPUTS		0x0013
170 #define MT_CLS_WIN_8_DUAL			0x0014
171 
172 /* vendor specific classes */
173 #define MT_CLS_3M				0x0101
174 /* reserved					0x0102 */
175 #define MT_CLS_EGALAX				0x0103
176 #define MT_CLS_EGALAX_SERIAL			0x0104
177 #define MT_CLS_TOPSEED				0x0105
178 #define MT_CLS_PANASONIC			0x0106
179 #define MT_CLS_FLATFROG				0x0107
180 #define MT_CLS_GENERALTOUCH_TWOFINGERS		0x0108
181 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS	0x0109
182 #define MT_CLS_LG				0x010a
183 #define MT_CLS_ASUS				0x010b
184 #define MT_CLS_VTL				0x0110
185 #define MT_CLS_GOOGLE				0x0111
186 #define MT_CLS_RAZER_BLADE_STEALTH		0x0112
187 
188 #define MT_DEFAULT_MAXCONTACT	10
189 #define MT_MAX_MAXCONTACT	250
190 
191 /*
192  * Resync device and local timestamps after that many microseconds without
193  * receiving data.
194  */
195 #define MAX_TIMESTAMP_INTERVAL	1000000
196 
197 #define MT_USB_DEVICE(v, p)	HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
198 #define MT_BT_DEVICE(v, p)	HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
199 
200 /*
201  * these device-dependent functions determine what slot corresponds
202  * to a valid contact that was just read.
203  */
204 
205 static int cypress_compute_slot(struct mt_device *td)
206 {
207 	if (td->curdata.contactid != 0 || td->num_received == 0)
208 		return td->curdata.contactid;
209 	else
210 		return -1;
211 }
212 
213 static struct mt_class mt_classes[] = {
214 	{ .name = MT_CLS_DEFAULT,
215 		.quirks = MT_QUIRK_ALWAYS_VALID |
216 			MT_QUIRK_CONTACT_CNT_ACCURATE },
217 	{ .name = MT_CLS_NSMU,
218 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
219 	{ .name = MT_CLS_SERIAL,
220 		.quirks = MT_QUIRK_ALWAYS_VALID},
221 	{ .name = MT_CLS_CONFIDENCE,
222 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
223 	{ .name = MT_CLS_CONFIDENCE_CONTACT_ID,
224 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
225 			MT_QUIRK_SLOT_IS_CONTACTID },
226 	{ .name = MT_CLS_CONFIDENCE_MINUS_ONE,
227 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
228 			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
229 	{ .name = MT_CLS_DUAL_INRANGE_CONTACTID,
230 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
231 			MT_QUIRK_SLOT_IS_CONTACTID,
232 		.maxcontacts = 2 },
233 	{ .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
234 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
235 			MT_QUIRK_SLOT_IS_CONTACTNUMBER,
236 		.maxcontacts = 2 },
237 	{ .name = MT_CLS_INRANGE_CONTACTNUMBER,
238 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
239 			MT_QUIRK_SLOT_IS_CONTACTNUMBER },
240 	{ .name = MT_CLS_WIN_8,
241 		.quirks = MT_QUIRK_ALWAYS_VALID |
242 			MT_QUIRK_IGNORE_DUPLICATES |
243 			MT_QUIRK_HOVERING |
244 			MT_QUIRK_CONTACT_CNT_ACCURATE |
245 			MT_QUIRK_STICKY_FINGERS |
246 			MT_QUIRK_WIN8_PTP_BUTTONS },
247 	{ .name = MT_CLS_EXPORT_ALL_INPUTS,
248 		.quirks = MT_QUIRK_ALWAYS_VALID |
249 			MT_QUIRK_CONTACT_CNT_ACCURATE,
250 		.export_all_inputs = true },
251 	{ .name = MT_CLS_WIN_8_DUAL,
252 		.quirks = MT_QUIRK_ALWAYS_VALID |
253 			MT_QUIRK_IGNORE_DUPLICATES |
254 			MT_QUIRK_HOVERING |
255 			MT_QUIRK_CONTACT_CNT_ACCURATE |
256 			MT_QUIRK_WIN8_PTP_BUTTONS,
257 		.export_all_inputs = true },
258 
259 	/*
260 	 * vendor specific classes
261 	 */
262 	{ .name = MT_CLS_3M,
263 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
264 			MT_QUIRK_SLOT_IS_CONTACTID |
265 			MT_QUIRK_TOUCH_SIZE_SCALING,
266 		.sn_move = 2048,
267 		.sn_width = 128,
268 		.sn_height = 128,
269 		.maxcontacts = 60,
270 	},
271 	{ .name = MT_CLS_EGALAX,
272 		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
273 			MT_QUIRK_VALID_IS_INRANGE,
274 		.sn_move = 4096,
275 		.sn_pressure = 32,
276 	},
277 	{ .name = MT_CLS_EGALAX_SERIAL,
278 		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
279 			MT_QUIRK_ALWAYS_VALID,
280 		.sn_move = 4096,
281 		.sn_pressure = 32,
282 	},
283 	{ .name = MT_CLS_TOPSEED,
284 		.quirks = MT_QUIRK_ALWAYS_VALID,
285 		.is_indirect = true,
286 		.maxcontacts = 2,
287 	},
288 	{ .name = MT_CLS_PANASONIC,
289 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
290 		.maxcontacts = 4 },
291 	{ .name	= MT_CLS_GENERALTOUCH_TWOFINGERS,
292 		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
293 			MT_QUIRK_VALID_IS_INRANGE |
294 			MT_QUIRK_SLOT_IS_CONTACTID,
295 		.maxcontacts = 2
296 	},
297 	{ .name	= MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
298 		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
299 			MT_QUIRK_SLOT_IS_CONTACTID
300 	},
301 
302 	{ .name = MT_CLS_FLATFROG,
303 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
304 			MT_QUIRK_NO_AREA,
305 		.sn_move = 2048,
306 		.maxcontacts = 40,
307 	},
308 	{ .name = MT_CLS_LG,
309 		.quirks = MT_QUIRK_ALWAYS_VALID |
310 			MT_QUIRK_FIX_CONST_CONTACT_ID |
311 			MT_QUIRK_IGNORE_DUPLICATES |
312 			MT_QUIRK_HOVERING |
313 			MT_QUIRK_CONTACT_CNT_ACCURATE },
314 	{ .name = MT_CLS_ASUS,
315 		.quirks = MT_QUIRK_ALWAYS_VALID |
316 			MT_QUIRK_CONTACT_CNT_ACCURATE |
317 			MT_QUIRK_ASUS_CUSTOM_UP },
318 	{ .name = MT_CLS_VTL,
319 		.quirks = MT_QUIRK_ALWAYS_VALID |
320 			MT_QUIRK_CONTACT_CNT_ACCURATE |
321 			MT_QUIRK_FORCE_GET_FEATURE,
322 	},
323 	{ .name = MT_CLS_GOOGLE,
324 		.quirks = MT_QUIRK_ALWAYS_VALID |
325 			MT_QUIRK_CONTACT_CNT_ACCURATE |
326 			MT_QUIRK_SLOT_IS_CONTACTID |
327 			MT_QUIRK_HOVERING
328 	},
329 	{ .name = MT_CLS_RAZER_BLADE_STEALTH,
330 		.quirks = MT_QUIRK_ALWAYS_VALID |
331 			MT_QUIRK_IGNORE_DUPLICATES |
332 			MT_QUIRK_HOVERING |
333 			MT_QUIRK_CONTACT_CNT_ACCURATE |
334 			MT_QUIRK_WIN8_PTP_BUTTONS,
335 	},
336 	{ }
337 };
338 
339 static ssize_t mt_show_quirks(struct device *dev,
340 			   struct device_attribute *attr,
341 			   char *buf)
342 {
343 	struct hid_device *hdev = to_hid_device(dev);
344 	struct mt_device *td = hid_get_drvdata(hdev);
345 
346 	return sprintf(buf, "%u\n", td->mtclass.quirks);
347 }
348 
349 static ssize_t mt_set_quirks(struct device *dev,
350 			  struct device_attribute *attr,
351 			  const char *buf, size_t count)
352 {
353 	struct hid_device *hdev = to_hid_device(dev);
354 	struct mt_device *td = hid_get_drvdata(hdev);
355 
356 	unsigned long val;
357 
358 	if (kstrtoul(buf, 0, &val))
359 		return -EINVAL;
360 
361 	td->mtclass.quirks = val;
362 
363 	if (td->cc_index < 0)
364 		td->mtclass.quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
365 
366 	return count;
367 }
368 
369 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
370 
371 static struct attribute *sysfs_attrs[] = {
372 	&dev_attr_quirks.attr,
373 	NULL
374 };
375 
376 static const struct attribute_group mt_attribute_group = {
377 	.attrs = sysfs_attrs
378 };
379 
380 static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
381 {
382 	int ret;
383 	u32 size = hid_report_len(report);
384 	u8 *buf;
385 
386 	/*
387 	 * Do not fetch the feature report if the device has been explicitly
388 	 * marked as non-capable.
389 	 */
390 	if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
391 		return;
392 
393 	buf = hid_alloc_report_buf(report, GFP_KERNEL);
394 	if (!buf)
395 		return;
396 
397 	ret = hid_hw_raw_request(hdev, report->id, buf, size,
398 				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
399 	if (ret < 0) {
400 		dev_warn(&hdev->dev, "failed to fetch feature %d\n",
401 			 report->id);
402 	} else {
403 		ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
404 					   size, 0);
405 		if (ret)
406 			dev_warn(&hdev->dev, "failed to report feature\n");
407 	}
408 
409 	kfree(buf);
410 }
411 
412 static void mt_feature_mapping(struct hid_device *hdev,
413 		struct hid_field *field, struct hid_usage *usage)
414 {
415 	struct mt_device *td = hid_get_drvdata(hdev);
416 
417 	switch (usage->hid) {
418 	case HID_DG_INPUTMODE:
419 		/* Ignore if value index is out of bounds. */
420 		if (usage->usage_index >= field->report_count) {
421 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
422 			break;
423 		}
424 
425 		if (td->inputmode < 0) {
426 			td->inputmode = field->report->id;
427 			td->inputmode_index = usage->usage_index;
428 		} else {
429 			/*
430 			 * Some elan panels wrongly declare 2 input mode
431 			 * features, and silently ignore when we set the
432 			 * value in the second field. Skip the second feature
433 			 * and hope for the best.
434 			 */
435 			dev_info(&hdev->dev,
436 				 "Ignoring the extra HID_DG_INPUTMODE\n");
437 		}
438 
439 		break;
440 	case HID_DG_CONTACTMAX:
441 		mt_get_feature(hdev, field->report);
442 
443 		td->maxcontact_report_id = field->report->id;
444 		td->maxcontacts = field->value[0];
445 		if (!td->maxcontacts &&
446 		    field->logical_maximum <= MT_MAX_MAXCONTACT)
447 			td->maxcontacts = field->logical_maximum;
448 		if (td->mtclass.maxcontacts)
449 			/* check if the maxcontacts is given by the class */
450 			td->maxcontacts = td->mtclass.maxcontacts;
451 
452 		break;
453 	case HID_DG_BUTTONTYPE:
454 		if (usage->usage_index >= field->report_count) {
455 			dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
456 			break;
457 		}
458 
459 		mt_get_feature(hdev, field->report);
460 		if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
461 			td->is_buttonpad = true;
462 
463 		break;
464 	case 0xff0000c5:
465 		/* Retrieve the Win8 blob once to enable some devices */
466 		if (usage->usage_index == 0)
467 			mt_get_feature(hdev, field->report);
468 		break;
469 	}
470 }
471 
472 static void set_abs(struct input_dev *input, unsigned int code,
473 		struct hid_field *field, int snratio)
474 {
475 	int fmin = field->logical_minimum;
476 	int fmax = field->logical_maximum;
477 	int fuzz = snratio ? (fmax - fmin) / snratio : 0;
478 	input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
479 	input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
480 }
481 
482 static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
483 		struct hid_input *hi)
484 {
485 	struct mt_fields *f = td->fields;
486 
487 	if (f->length >= HID_MAX_FIELDS)
488 		return;
489 
490 	f->usages[f->length++] = usage->hid;
491 }
492 
493 static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
494 		struct hid_field *field, struct hid_usage *usage,
495 		unsigned long **bit, int *max)
496 {
497 	struct mt_device *td = hid_get_drvdata(hdev);
498 	struct mt_class *cls = &td->mtclass;
499 	int code;
500 	struct hid_usage *prev_usage = NULL;
501 
502 	if (field->application == HID_DG_TOUCHSCREEN)
503 		td->mt_flags |= INPUT_MT_DIRECT;
504 
505 	/*
506 	 * Model touchscreens providing buttons as touchpads.
507 	 */
508 	if (field->application == HID_DG_TOUCHPAD ||
509 	    (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
510 		td->mt_flags |= INPUT_MT_POINTER;
511 		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
512 	}
513 
514 	/* count the buttons on touchpads */
515 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
516 		td->buttons_count++;
517 
518 	if (usage->usage_index)
519 		prev_usage = &field->usage[usage->usage_index - 1];
520 
521 	switch (usage->hid & HID_USAGE_PAGE) {
522 
523 	case HID_UP_GENDESK:
524 		switch (usage->hid) {
525 		case HID_GD_X:
526 			if (prev_usage && (prev_usage->hid == usage->hid)) {
527 				hid_map_usage(hi, usage, bit, max,
528 					EV_ABS, ABS_MT_TOOL_X);
529 				set_abs(hi->input, ABS_MT_TOOL_X, field,
530 					cls->sn_move);
531 			} else {
532 				hid_map_usage(hi, usage, bit, max,
533 					EV_ABS, ABS_MT_POSITION_X);
534 				set_abs(hi->input, ABS_MT_POSITION_X, field,
535 					cls->sn_move);
536 			}
537 
538 			mt_store_field(usage, td, hi);
539 			return 1;
540 		case HID_GD_Y:
541 			if (prev_usage && (prev_usage->hid == usage->hid)) {
542 				hid_map_usage(hi, usage, bit, max,
543 					EV_ABS, ABS_MT_TOOL_Y);
544 				set_abs(hi->input, ABS_MT_TOOL_Y, field,
545 					cls->sn_move);
546 			} else {
547 				hid_map_usage(hi, usage, bit, max,
548 					EV_ABS, ABS_MT_POSITION_Y);
549 				set_abs(hi->input, ABS_MT_POSITION_Y, field,
550 					cls->sn_move);
551 			}
552 
553 			mt_store_field(usage, td, hi);
554 			return 1;
555 		}
556 		return 0;
557 
558 	case HID_UP_DIGITIZER:
559 		switch (usage->hid) {
560 		case HID_DG_INRANGE:
561 			if (cls->quirks & MT_QUIRK_HOVERING) {
562 				hid_map_usage(hi, usage, bit, max,
563 					EV_ABS, ABS_MT_DISTANCE);
564 				input_set_abs_params(hi->input,
565 					ABS_MT_DISTANCE, 0, 1, 0, 0);
566 			}
567 			mt_store_field(usage, td, hi);
568 			return 1;
569 		case HID_DG_CONFIDENCE:
570 			if ((cls->name == MT_CLS_WIN_8 ||
571 				cls->name == MT_CLS_WIN_8_DUAL) &&
572 				field->application == HID_DG_TOUCHPAD)
573 				cls->quirks |= MT_QUIRK_CONFIDENCE;
574 			mt_store_field(usage, td, hi);
575 			return 1;
576 		case HID_DG_TIPSWITCH:
577 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
578 			input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
579 			mt_store_field(usage, td, hi);
580 			return 1;
581 		case HID_DG_CONTACTID:
582 			mt_store_field(usage, td, hi);
583 			td->touches_by_report++;
584 			td->mt_report_id = field->report->id;
585 			return 1;
586 		case HID_DG_WIDTH:
587 			hid_map_usage(hi, usage, bit, max,
588 					EV_ABS, ABS_MT_TOUCH_MAJOR);
589 			if (!(cls->quirks & MT_QUIRK_NO_AREA))
590 				set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
591 					cls->sn_width);
592 			mt_store_field(usage, td, hi);
593 			return 1;
594 		case HID_DG_HEIGHT:
595 			hid_map_usage(hi, usage, bit, max,
596 					EV_ABS, ABS_MT_TOUCH_MINOR);
597 			if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
598 				set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
599 					cls->sn_height);
600 
601 				/*
602 				 * Only set ABS_MT_ORIENTATION if it is not
603 				 * already set by the HID_DG_AZIMUTH usage.
604 				 */
605 				if (!test_bit(ABS_MT_ORIENTATION,
606 						hi->input->absbit))
607 					input_set_abs_params(hi->input,
608 						ABS_MT_ORIENTATION, 0, 1, 0, 0);
609 			}
610 			mt_store_field(usage, td, hi);
611 			return 1;
612 		case HID_DG_TIPPRESSURE:
613 			hid_map_usage(hi, usage, bit, max,
614 					EV_ABS, ABS_MT_PRESSURE);
615 			set_abs(hi->input, ABS_MT_PRESSURE, field,
616 				cls->sn_pressure);
617 			mt_store_field(usage, td, hi);
618 			return 1;
619 		case HID_DG_SCANTIME:
620 			hid_map_usage(hi, usage, bit, max,
621 				EV_MSC, MSC_TIMESTAMP);
622 			input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
623 			mt_store_field(usage, td, hi);
624 			/* Ignore if indexes are out of bounds. */
625 			if (field->index >= field->report->maxfield ||
626 			    usage->usage_index >= field->report_count)
627 				return 1;
628 			td->scantime_index = field->index;
629 			td->scantime_val_index = usage->usage_index;
630 			return 1;
631 		case HID_DG_CONTACTCOUNT:
632 			/* Ignore if indexes are out of bounds. */
633 			if (field->index >= field->report->maxfield ||
634 			    usage->usage_index >= field->report_count)
635 				return 1;
636 			td->cc_index = field->index;
637 			td->cc_value_index = usage->usage_index;
638 			return 1;
639 		case HID_DG_AZIMUTH:
640 			hid_map_usage(hi, usage, bit, max,
641 				EV_ABS, ABS_MT_ORIENTATION);
642 			/*
643 			 * Azimuth has the range of [0, MAX) representing a full
644 			 * revolution. Set ABS_MT_ORIENTATION to a quarter of
645 			 * MAX according the definition of ABS_MT_ORIENTATION
646 			 */
647 			input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
648 				-field->logical_maximum / 4,
649 				field->logical_maximum / 4,
650 				cls->sn_move ?
651 				field->logical_maximum / cls->sn_move : 0, 0);
652 			mt_store_field(usage, td, hi);
653 			return 1;
654 		case HID_DG_CONTACTMAX:
655 			/* we don't set td->last_slot_field as contactcount and
656 			 * contact max are global to the report */
657 			return -1;
658 		case HID_DG_TOUCH:
659 			/* Legacy devices use TIPSWITCH and not TOUCH.
660 			 * Let's just ignore this field. */
661 			return -1;
662 		}
663 		/* let hid-input decide for the others */
664 		return 0;
665 
666 	case HID_UP_BUTTON:
667 		code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
668 		/*
669 		 * MS PTP spec says that external buttons left and right have
670 		 * usages 2 and 3.
671 		 */
672 		if ((cls->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
673 		    field->application == HID_DG_TOUCHPAD &&
674 		    (usage->hid & HID_USAGE) > 1)
675 			code--;
676 		hid_map_usage(hi, usage, bit, max, EV_KEY, code);
677 		input_set_capability(hi->input, EV_KEY, code);
678 		return 1;
679 
680 	case 0xff000000:
681 		/* we do not want to map these: no input-oriented meaning */
682 		return -1;
683 	}
684 
685 	return 0;
686 }
687 
688 static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
689 {
690 	__s32 quirks = td->mtclass.quirks;
691 
692 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
693 		return td->curdata.contactid;
694 
695 	if (quirks & MT_QUIRK_CYPRESS)
696 		return cypress_compute_slot(td);
697 
698 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
699 		return td->num_received;
700 
701 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
702 		return td->curdata.contactid - 1;
703 
704 	return input_mt_get_slot_by_key(input, td->curdata.contactid);
705 }
706 
707 /*
708  * this function is called when a whole contact has been processed,
709  * so that it can assign it to a slot and store the data there
710  */
711 static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
712 {
713 	if ((td->mtclass.quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
714 	    td->num_received >= td->num_expected)
715 		return;
716 
717 	if (td->curvalid || (td->mtclass.quirks & MT_QUIRK_ALWAYS_VALID)) {
718 		int active;
719 		int slotnum = mt_compute_slot(td, input);
720 		struct mt_slot *s = &td->curdata;
721 		struct input_mt *mt = input->mt;
722 
723 		if (slotnum < 0 || slotnum >= td->maxcontacts)
724 			return;
725 
726 		if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
727 			struct input_mt_slot *slot = &mt->slots[slotnum];
728 			if (input_mt_is_active(slot) &&
729 			    input_mt_is_used(mt, slot))
730 				return;
731 		}
732 
733 		if (!(td->mtclass.quirks & MT_QUIRK_CONFIDENCE))
734 			s->confidence_state = true;
735 		active = (s->touch_state || s->inrange_state) &&
736 							s->confidence_state;
737 
738 		input_mt_slot(input, slotnum);
739 		input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
740 		if (active) {
741 			/* this finger is in proximity of the sensor */
742 			int wide = (s->w > s->h);
743 			int major = max(s->w, s->h);
744 			int minor = min(s->w, s->h);
745 			int orientation = wide;
746 
747 			if (s->has_azimuth)
748 				orientation = s->a;
749 
750 			/*
751 			 * divided by two to match visual scale of touch
752 			 * for devices with this quirk
753 			 */
754 			if (td->mtclass.quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
755 				major = major >> 1;
756 				minor = minor >> 1;
757 			}
758 
759 			input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
760 			input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
761 			input_event(input, EV_ABS, ABS_MT_TOOL_X, s->cx);
762 			input_event(input, EV_ABS, ABS_MT_TOOL_Y, s->cy);
763 			input_event(input, EV_ABS, ABS_MT_DISTANCE,
764 				!s->touch_state);
765 			input_event(input, EV_ABS, ABS_MT_ORIENTATION,
766 				orientation);
767 			input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
768 			input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
769 			input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
770 
771 			set_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
772 		}
773 	}
774 
775 	td->num_received++;
776 }
777 
778 /*
779  * this function is called when a whole packet has been received and processed,
780  * so that it can decide what to send to the input layer.
781  */
782 static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
783 {
784 	if (td->mtclass.quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
785 		input_event(input, EV_KEY, BTN_LEFT, td->left_button_state);
786 
787 	input_mt_sync_frame(input);
788 	input_event(input, EV_MSC, MSC_TIMESTAMP, td->timestamp);
789 	input_sync(input);
790 	td->num_received = 0;
791 	td->left_button_state = 0;
792 	if (test_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags))
793 		set_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
794 	else
795 		clear_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
796 	clear_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
797 }
798 
799 static int mt_compute_timestamp(struct mt_device *td, struct hid_field *field,
800 		__s32 value)
801 {
802 	long delta = value - td->dev_time;
803 	unsigned long jdelta = jiffies_to_usecs(jiffies - td->jiffies);
804 
805 	td->jiffies = jiffies;
806 	td->dev_time = value;
807 
808 	if (delta < 0)
809 		delta += field->logical_maximum;
810 
811 	/* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
812 	delta *= 100;
813 
814 	if (jdelta > MAX_TIMESTAMP_INTERVAL)
815 		/* No data received for a while, resync the timestamp. */
816 		return 0;
817 	else
818 		return td->timestamp + delta;
819 }
820 
821 static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
822 				struct hid_usage *usage, __s32 value)
823 {
824 	/* we will handle the hidinput part later, now remains hiddev */
825 	if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
826 		hid->hiddev_hid_event(hid, field, usage, value);
827 
828 	return 1;
829 }
830 
831 static void mt_process_mt_event(struct hid_device *hid, struct hid_field *field,
832 				struct hid_usage *usage, __s32 value,
833 				bool first_packet)
834 {
835 	struct mt_device *td = hid_get_drvdata(hid);
836 	__s32 quirks = td->mtclass.quirks;
837 	struct input_dev *input = field->hidinput->input;
838 
839 	if (hid->claimed & HID_CLAIMED_INPUT) {
840 		switch (usage->hid) {
841 		case HID_DG_INRANGE:
842 			if (quirks & MT_QUIRK_VALID_IS_INRANGE)
843 				td->curvalid = value;
844 			if (quirks & MT_QUIRK_HOVERING)
845 				td->curdata.inrange_state = value;
846 			break;
847 		case HID_DG_TIPSWITCH:
848 			if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
849 				td->curvalid = value;
850 			td->curdata.touch_state = value;
851 			break;
852 		case HID_DG_CONFIDENCE:
853 			if (quirks & MT_QUIRK_CONFIDENCE)
854 				td->curdata.confidence_state = value;
855 			if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
856 				td->curvalid = value;
857 			break;
858 		case HID_DG_CONTACTID:
859 			td->curdata.contactid = value;
860 			break;
861 		case HID_DG_TIPPRESSURE:
862 			td->curdata.p = value;
863 			break;
864 		case HID_GD_X:
865 			if (usage->code == ABS_MT_TOOL_X)
866 				td->curdata.cx = value;
867 			else
868 				td->curdata.x = value;
869 			break;
870 		case HID_GD_Y:
871 			if (usage->code == ABS_MT_TOOL_Y)
872 				td->curdata.cy = value;
873 			else
874 				td->curdata.y = value;
875 			break;
876 		case HID_DG_WIDTH:
877 			td->curdata.w = value;
878 			break;
879 		case HID_DG_HEIGHT:
880 			td->curdata.h = value;
881 			break;
882 		case HID_DG_SCANTIME:
883 			td->timestamp = mt_compute_timestamp(td, field, value);
884 			break;
885 		case HID_DG_CONTACTCOUNT:
886 			break;
887 		case HID_DG_AZIMUTH:
888 			/*
889 			 * Azimuth is counter-clockwise and ranges from [0, MAX)
890 			 * (a full revolution). Convert it to clockwise ranging
891 			 * [-MAX/2, MAX/2].
892 			 *
893 			 * Note that ABS_MT_ORIENTATION require us to report
894 			 * the limit of [-MAX/4, MAX/4], but the value can go
895 			 * out of range to [-MAX/2, MAX/2] to report an upside
896 			 * down ellipsis.
897 			 */
898 			if (value > field->logical_maximum / 2)
899 				value -= field->logical_maximum;
900 			td->curdata.a = -value;
901 			td->curdata.has_azimuth = true;
902 			break;
903 		case HID_DG_TOUCH:
904 			/* do nothing */
905 			break;
906 
907 		default:
908 			/*
909 			 * For Win8 PTP touchpads we should only look at
910 			 * non finger/touch events in the first_packet of
911 			 * a (possible) multi-packet frame.
912 			 */
913 			if ((quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
914 			    !first_packet)
915 				return;
916 
917 			/*
918 			 * For Win8 PTP touchpads we map both the clickpad click
919 			 * and any "external" left buttons to BTN_LEFT if a
920 			 * device claims to have both we need to report 1 for
921 			 * BTN_LEFT if either is pressed, so we or all values
922 			 * together and report the result in mt_sync_frame().
923 			 */
924 			if ((quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
925 			    usage->type == EV_KEY && usage->code == BTN_LEFT) {
926 				td->left_button_state |= value;
927 				return;
928 			}
929 
930 			if (usage->type)
931 				input_event(input, usage->type, usage->code,
932 						value);
933 			return;
934 		}
935 
936 		if (usage->usage_index + 1 == field->report_count) {
937 			/* we only take into account the last report. */
938 			if (usage->hid == td->last_slot_field)
939 				mt_complete_slot(td, field->hidinput->input);
940 		}
941 
942 	}
943 }
944 
945 static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
946 {
947 	struct mt_device *td = hid_get_drvdata(hid);
948 	struct hid_field *field;
949 	bool first_packet;
950 	unsigned count;
951 	int r, n, scantime = 0;
952 
953 	/* sticky fingers release in progress, abort */
954 	if (test_and_set_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
955 		return;
956 
957 	/*
958 	 * Includes multi-packet support where subsequent
959 	 * packets are sent with zero contactcount.
960 	 */
961 	if (td->scantime_index >= 0) {
962 		field = report->field[td->scantime_index];
963 		scantime = field->value[td->scantime_val_index];
964 	}
965 	if (td->cc_index >= 0) {
966 		struct hid_field *field = report->field[td->cc_index];
967 		int value = field->value[td->cc_value_index];
968 
969 		/*
970 		 * For Win8 PTPs the first packet (td->num_received == 0) may
971 		 * have a contactcount of 0 if there only is a button event.
972 		 * We double check that this is not a continuation packet
973 		 * of a possible multi-packet frame be checking that the
974 		 * timestamp has changed.
975 		 */
976 		if ((td->mtclass.quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
977 		    td->num_received == 0 && td->prev_scantime != scantime)
978 			td->num_expected = value;
979 		/* A non 0 contact count always indicates a first packet */
980 		else if (value)
981 			td->num_expected = value;
982 	}
983 	td->prev_scantime = scantime;
984 
985 	first_packet = td->num_received == 0;
986 	for (r = 0; r < report->maxfield; r++) {
987 		field = report->field[r];
988 		count = field->report_count;
989 
990 		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
991 			continue;
992 
993 		for (n = 0; n < count; n++)
994 			mt_process_mt_event(hid, field, &field->usage[n],
995 					    field->value[n], first_packet);
996 	}
997 
998 	if (td->num_received >= td->num_expected)
999 		mt_sync_frame(td, report->field[0]->hidinput->input);
1000 
1001 	/*
1002 	 * Windows 8 specs says 2 things:
1003 	 * - once a contact has been reported, it has to be reported in each
1004 	 *   subsequent report
1005 	 * - the report rate when fingers are present has to be at least
1006 	 *   the refresh rate of the screen, 60 or 120 Hz
1007 	 *
1008 	 * I interprete this that the specification forces a report rate of
1009 	 * at least 60 Hz for a touchscreen to be certified.
1010 	 * Which means that if we do not get a report whithin 16 ms, either
1011 	 * something wrong happens, either the touchscreen forgets to send
1012 	 * a release. Taking a reasonable margin allows to remove issues
1013 	 * with USB communication or the load of the machine.
1014 	 *
1015 	 * Given that Win 8 devices are forced to send a release, this will
1016 	 * only affect laggish machines and the ones that have a firmware
1017 	 * defect.
1018 	 */
1019 	if (td->mtclass.quirks & MT_QUIRK_STICKY_FINGERS) {
1020 		if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1021 			mod_timer(&td->release_timer,
1022 				  jiffies + msecs_to_jiffies(100));
1023 		else
1024 			del_timer(&td->release_timer);
1025 	}
1026 
1027 	clear_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1028 }
1029 
1030 static int mt_touch_input_configured(struct hid_device *hdev,
1031 					struct hid_input *hi)
1032 {
1033 	struct mt_device *td = hid_get_drvdata(hdev);
1034 	struct mt_class *cls = &td->mtclass;
1035 	struct input_dev *input = hi->input;
1036 	int ret;
1037 
1038 	if (!td->maxcontacts)
1039 		td->maxcontacts = MT_DEFAULT_MAXCONTACT;
1040 
1041 	mt_post_parse(td);
1042 	if (td->serial_maybe)
1043 		mt_post_parse_default_settings(td);
1044 
1045 	if (cls->is_indirect)
1046 		td->mt_flags |= INPUT_MT_POINTER;
1047 
1048 	if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1049 		td->mt_flags |= INPUT_MT_DROP_UNUSED;
1050 
1051 	/* check for clickpads */
1052 	if ((td->mt_flags & INPUT_MT_POINTER) && (td->buttons_count == 1))
1053 		td->is_buttonpad = true;
1054 
1055 	if (td->is_buttonpad)
1056 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1057 
1058 	ret = input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
1059 	if (ret)
1060 		return ret;
1061 
1062 	td->mt_flags = 0;
1063 	return 0;
1064 }
1065 
1066 #define mt_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, \
1067 						    max, EV_KEY, (c))
1068 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1069 		struct hid_field *field, struct hid_usage *usage,
1070 		unsigned long **bit, int *max)
1071 {
1072 	struct mt_device *td = hid_get_drvdata(hdev);
1073 
1074 	/*
1075 	 * If mtclass.export_all_inputs is not set, only map fields from
1076 	 * TouchScreen or TouchPad collections. We need to ignore fields
1077 	 * that belong to other collections such as Mouse that might have
1078 	 * the same GenericDesktop usages.
1079 	 */
1080 	if (!td->mtclass.export_all_inputs &&
1081 	    field->application != HID_DG_TOUCHSCREEN &&
1082 	    field->application != HID_DG_PEN &&
1083 	    field->application != HID_DG_TOUCHPAD &&
1084 	    field->application != HID_GD_KEYBOARD &&
1085 	    field->application != HID_GD_SYSTEM_CONTROL &&
1086 	    field->application != HID_CP_CONSUMER_CONTROL &&
1087 	    field->application != HID_GD_WIRELESS_RADIO_CTLS &&
1088 	    !(field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1089 	      td->mtclass.quirks & MT_QUIRK_ASUS_CUSTOM_UP))
1090 		return -1;
1091 
1092 	/*
1093 	 * Some Asus keyboard+touchpad devices have the hotkeys defined in the
1094 	 * touchpad report descriptor. We need to treat these as an array to
1095 	 * map usages to input keys.
1096 	 */
1097 	if (field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1098 	    td->mtclass.quirks & MT_QUIRK_ASUS_CUSTOM_UP &&
1099 	    (usage->hid & HID_USAGE_PAGE) == HID_UP_CUSTOM) {
1100 		set_bit(EV_REP, hi->input->evbit);
1101 		if (field->flags & HID_MAIN_ITEM_VARIABLE)
1102 			field->flags &= ~HID_MAIN_ITEM_VARIABLE;
1103 		switch (usage->hid & HID_USAGE) {
1104 		case 0x10: mt_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
1105 		case 0x20: mt_map_key_clear(KEY_BRIGHTNESSUP);		break;
1106 		case 0x35: mt_map_key_clear(KEY_DISPLAY_OFF);		break;
1107 		case 0x6b: mt_map_key_clear(KEY_F21);			break;
1108 		case 0x6c: mt_map_key_clear(KEY_SLEEP);			break;
1109 		default:
1110 			return -1;
1111 		}
1112 		return 1;
1113 	}
1114 
1115 	/*
1116 	 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1117 	 * for the stylus.
1118 	 * The check for mt_report_id ensures we don't process
1119 	 * HID_DG_CONTACTCOUNT from the pen report as it is outside the physical
1120 	 * collection, but within the report ID.
1121 	 */
1122 	if (field->physical == HID_DG_STYLUS)
1123 		return 0;
1124 	else if ((field->physical == 0) &&
1125 		 (field->report->id != td->mt_report_id) &&
1126 		 (td->mt_report_id != -1))
1127 		return 0;
1128 
1129 	if (field->application == HID_DG_TOUCHSCREEN ||
1130 	    field->application == HID_DG_TOUCHPAD)
1131 		return mt_touch_input_mapping(hdev, hi, field, usage, bit, max);
1132 
1133 	/* let hid-core decide for the others */
1134 	return 0;
1135 }
1136 
1137 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
1138 		struct hid_field *field, struct hid_usage *usage,
1139 		unsigned long **bit, int *max)
1140 {
1141 	/*
1142 	 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1143 	 * for the stylus.
1144 	 */
1145 	if (field->physical == HID_DG_STYLUS)
1146 		return 0;
1147 
1148 	if (field->application == HID_DG_TOUCHSCREEN ||
1149 	    field->application == HID_DG_TOUCHPAD) {
1150 		/* We own these mappings, tell hid-input to ignore them */
1151 		return -1;
1152 	}
1153 
1154 	/* let hid-core decide for the others */
1155 	return 0;
1156 }
1157 
1158 static int mt_event(struct hid_device *hid, struct hid_field *field,
1159 				struct hid_usage *usage, __s32 value)
1160 {
1161 	struct mt_device *td = hid_get_drvdata(hid);
1162 
1163 	if (field->report->id == td->mt_report_id)
1164 		return mt_touch_event(hid, field, usage, value);
1165 
1166 	return 0;
1167 }
1168 
1169 static void mt_report(struct hid_device *hid, struct hid_report *report)
1170 {
1171 	struct mt_device *td = hid_get_drvdata(hid);
1172 	struct hid_field *field = report->field[0];
1173 
1174 	if (!(hid->claimed & HID_CLAIMED_INPUT))
1175 		return;
1176 
1177 	if (report->id == td->mt_report_id)
1178 		return mt_touch_report(hid, report);
1179 
1180 	if (field && field->hidinput && field->hidinput->input)
1181 		input_sync(field->hidinput->input);
1182 }
1183 
1184 static void mt_set_input_mode(struct hid_device *hdev)
1185 {
1186 	struct mt_device *td = hid_get_drvdata(hdev);
1187 	struct hid_report *r;
1188 	struct hid_report_enum *re;
1189 	struct mt_class *cls = &td->mtclass;
1190 	char *buf;
1191 	u32 report_len;
1192 
1193 	if (td->inputmode < 0)
1194 		return;
1195 
1196 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
1197 	r = re->report_id_hash[td->inputmode];
1198 	if (r) {
1199 		if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
1200 			report_len = hid_report_len(r);
1201 			buf = hid_alloc_report_buf(r, GFP_KERNEL);
1202 			if (!buf) {
1203 				hid_err(hdev, "failed to allocate buffer for report\n");
1204 				return;
1205 			}
1206 			hid_hw_raw_request(hdev, r->id, buf, report_len,
1207 					   HID_FEATURE_REPORT,
1208 					   HID_REQ_GET_REPORT);
1209 			kfree(buf);
1210 		}
1211 		r->field[0]->value[td->inputmode_index] = td->inputmode_value;
1212 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
1213 	}
1214 }
1215 
1216 static void mt_set_maxcontacts(struct hid_device *hdev)
1217 {
1218 	struct mt_device *td = hid_get_drvdata(hdev);
1219 	struct hid_report *r;
1220 	struct hid_report_enum *re;
1221 	int fieldmax, max;
1222 
1223 	if (td->maxcontact_report_id < 0)
1224 		return;
1225 
1226 	if (!td->mtclass.maxcontacts)
1227 		return;
1228 
1229 	re = &hdev->report_enum[HID_FEATURE_REPORT];
1230 	r = re->report_id_hash[td->maxcontact_report_id];
1231 	if (r) {
1232 		max = td->mtclass.maxcontacts;
1233 		fieldmax = r->field[0]->logical_maximum;
1234 		max = min(fieldmax, max);
1235 		if (r->field[0]->value[0] != max) {
1236 			r->field[0]->value[0] = max;
1237 			hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
1238 		}
1239 	}
1240 }
1241 
1242 static void mt_post_parse_default_settings(struct mt_device *td)
1243 {
1244 	__s32 quirks = td->mtclass.quirks;
1245 
1246 	/* unknown serial device needs special quirks */
1247 	if (td->touches_by_report == 1) {
1248 		quirks |= MT_QUIRK_ALWAYS_VALID;
1249 		quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1250 		quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1251 		quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1252 		quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1253 	}
1254 
1255 	td->mtclass.quirks = quirks;
1256 }
1257 
1258 static void mt_post_parse(struct mt_device *td)
1259 {
1260 	struct mt_fields *f = td->fields;
1261 	struct mt_class *cls = &td->mtclass;
1262 
1263 	if (td->touches_by_report > 0) {
1264 		int field_count_per_touch = f->length / td->touches_by_report;
1265 		td->last_slot_field = f->usages[field_count_per_touch - 1];
1266 	}
1267 
1268 	if (td->cc_index < 0)
1269 		cls->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1270 }
1271 
1272 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1273 {
1274 	struct mt_device *td = hid_get_drvdata(hdev);
1275 	char *name;
1276 	const char *suffix = NULL;
1277 	struct hid_field *field = hi->report->field[0];
1278 	int ret;
1279 
1280 	if (hi->report->id == td->mt_report_id) {
1281 		ret = mt_touch_input_configured(hdev, hi);
1282 		if (ret)
1283 			return ret;
1284 	}
1285 
1286 	/*
1287 	 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1288 	 * for the stylus. Check this first, and then rely on the application
1289 	 * field.
1290 	 */
1291 	if (hi->report->field[0]->physical == HID_DG_STYLUS) {
1292 		suffix = "Pen";
1293 		/* force BTN_STYLUS to allow tablet matching in udev */
1294 		__set_bit(BTN_STYLUS, hi->input->keybit);
1295 	} else {
1296 		switch (field->application) {
1297 		case HID_GD_KEYBOARD:
1298 			suffix = "Keyboard";
1299 			break;
1300 		case HID_GD_KEYPAD:
1301 			suffix = "Keypad";
1302 			break;
1303 		case HID_GD_MOUSE:
1304 			suffix = "Mouse";
1305 			break;
1306 		case HID_DG_STYLUS:
1307 			suffix = "Pen";
1308 			/* force BTN_STYLUS to allow tablet matching in udev */
1309 			__set_bit(BTN_STYLUS, hi->input->keybit);
1310 			break;
1311 		case HID_DG_TOUCHSCREEN:
1312 			/* we do not set suffix = "Touchscreen" */
1313 			break;
1314 		case HID_DG_TOUCHPAD:
1315 			suffix = "Touchpad";
1316 			break;
1317 		case HID_GD_SYSTEM_CONTROL:
1318 			suffix = "System Control";
1319 			break;
1320 		case HID_CP_CONSUMER_CONTROL:
1321 			suffix = "Consumer Control";
1322 			break;
1323 		case HID_GD_WIRELESS_RADIO_CTLS:
1324 			suffix = "Wireless Radio Control";
1325 			break;
1326 		case HID_VD_ASUS_CUSTOM_MEDIA_KEYS:
1327 			suffix = "Custom Media Keys";
1328 			break;
1329 		default:
1330 			suffix = "UNKNOWN";
1331 			break;
1332 		}
1333 	}
1334 
1335 	if (suffix) {
1336 		name = devm_kzalloc(&hi->input->dev,
1337 				    strlen(hdev->name) + strlen(suffix) + 2,
1338 				    GFP_KERNEL);
1339 		if (name) {
1340 			sprintf(name, "%s %s", hdev->name, suffix);
1341 			hi->input->name = name;
1342 		}
1343 	}
1344 
1345 	return 0;
1346 }
1347 
1348 static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
1349 {
1350 	if (field->usage[0].hid != usage ||
1351 	    !(field->flags & HID_MAIN_ITEM_CONSTANT))
1352 		return;
1353 
1354 	field->flags &= ~HID_MAIN_ITEM_CONSTANT;
1355 	field->flags |= HID_MAIN_ITEM_VARIABLE;
1356 }
1357 
1358 static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
1359 {
1360 	struct hid_report *report;
1361 	int i;
1362 
1363 	list_for_each_entry(report,
1364 			    &hdev->report_enum[HID_INPUT_REPORT].report_list,
1365 			    list) {
1366 
1367 		if (!report->maxfield)
1368 			continue;
1369 
1370 		for (i = 0; i < report->maxfield; i++)
1371 			if (report->field[i]->maxusage >= 1)
1372 				mt_fix_const_field(report->field[i], usage);
1373 	}
1374 }
1375 
1376 static void mt_release_contacts(struct hid_device *hid)
1377 {
1378 	struct hid_input *hidinput;
1379 	struct mt_device *td = hid_get_drvdata(hid);
1380 
1381 	list_for_each_entry(hidinput, &hid->inputs, list) {
1382 		struct input_dev *input_dev = hidinput->input;
1383 		struct input_mt *mt = input_dev->mt;
1384 		int i;
1385 
1386 		if (mt) {
1387 			for (i = 0; i < mt->num_slots; i++) {
1388 				input_mt_slot(input_dev, i);
1389 				input_mt_report_slot_state(input_dev,
1390 							   MT_TOOL_FINGER,
1391 							   false);
1392 			}
1393 			input_mt_sync_frame(input_dev);
1394 			input_sync(input_dev);
1395 		}
1396 	}
1397 
1398 	td->num_received = 0;
1399 }
1400 
1401 static void mt_expired_timeout(struct timer_list *t)
1402 {
1403 	struct mt_device *td = from_timer(td, t, release_timer);
1404 	struct hid_device *hdev = td->hdev;
1405 
1406 	/*
1407 	 * An input report came in just before we release the sticky fingers,
1408 	 * it will take care of the sticky fingers.
1409 	 */
1410 	if (test_and_set_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1411 		return;
1412 	if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1413 		mt_release_contacts(hdev);
1414 	clear_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1415 }
1416 
1417 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1418 {
1419 	int ret, i;
1420 	struct mt_device *td;
1421 	struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1422 
1423 	for (i = 0; mt_classes[i].name ; i++) {
1424 		if (id->driver_data == mt_classes[i].name) {
1425 			mtclass = &(mt_classes[i]);
1426 			break;
1427 		}
1428 	}
1429 
1430 	td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1431 	if (!td) {
1432 		dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1433 		return -ENOMEM;
1434 	}
1435 	td->hdev = hdev;
1436 	td->mtclass = *mtclass;
1437 	td->inputmode = -1;
1438 	td->maxcontact_report_id = -1;
1439 	td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1440 	td->cc_index = -1;
1441 	td->scantime_index = -1;
1442 	td->mt_report_id = -1;
1443 	hid_set_drvdata(hdev, td);
1444 
1445 	td->fields = devm_kzalloc(&hdev->dev, sizeof(struct mt_fields),
1446 				  GFP_KERNEL);
1447 	if (!td->fields) {
1448 		dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
1449 		return -ENOMEM;
1450 	}
1451 
1452 	if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1453 		td->serial_maybe = true;
1454 
1455 	/* This allows the driver to correctly support devices
1456 	 * that emit events over several HID messages.
1457 	 */
1458 	hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1459 
1460 	/*
1461 	 * This allows the driver to handle different input sensors
1462 	 * that emits events through different reports on the same HID
1463 	 * device.
1464 	 */
1465 	hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1466 
1467 	timer_setup(&td->release_timer, mt_expired_timeout, 0);
1468 
1469 	ret = hid_parse(hdev);
1470 	if (ret != 0)
1471 		return ret;
1472 
1473 	if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
1474 		mt_fix_const_fields(hdev, HID_DG_CONTACTID);
1475 
1476 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1477 	if (ret)
1478 		return ret;
1479 
1480 	ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
1481 	if (ret)
1482 		dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
1483 				hdev->name);
1484 
1485 	mt_set_maxcontacts(hdev);
1486 	mt_set_input_mode(hdev);
1487 
1488 	/* release .fields memory as it is not used anymore */
1489 	devm_kfree(&hdev->dev, td->fields);
1490 	td->fields = NULL;
1491 
1492 	return 0;
1493 }
1494 
1495 #ifdef CONFIG_PM
1496 static int mt_reset_resume(struct hid_device *hdev)
1497 {
1498 	mt_release_contacts(hdev);
1499 	mt_set_maxcontacts(hdev);
1500 	mt_set_input_mode(hdev);
1501 	return 0;
1502 }
1503 
1504 static int mt_resume(struct hid_device *hdev)
1505 {
1506 	/* Some Elan legacy devices require SET_IDLE to be set on resume.
1507 	 * It should be safe to send it to other devices too.
1508 	 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
1509 
1510 	hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
1511 
1512 	return 0;
1513 }
1514 #endif
1515 
1516 static void mt_remove(struct hid_device *hdev)
1517 {
1518 	struct mt_device *td = hid_get_drvdata(hdev);
1519 
1520 	del_timer_sync(&td->release_timer);
1521 
1522 	sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
1523 	hid_hw_stop(hdev);
1524 }
1525 
1526 /*
1527  * This list contains only:
1528  * - VID/PID of products not working with the default multitouch handling
1529  * - 2 generic rules.
1530  * So there is no point in adding here any device with MT_CLS_DEFAULT.
1531  */
1532 static const struct hid_device_id mt_devices[] = {
1533 
1534 	/* 3M panels */
1535 	{ .driver_data = MT_CLS_3M,
1536 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
1537 			USB_DEVICE_ID_3M1968) },
1538 	{ .driver_data = MT_CLS_3M,
1539 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
1540 			USB_DEVICE_ID_3M2256) },
1541 	{ .driver_data = MT_CLS_3M,
1542 		MT_USB_DEVICE(USB_VENDOR_ID_3M,
1543 			USB_DEVICE_ID_3M3266) },
1544 
1545 	/* Alps devices */
1546 	{ .driver_data = MT_CLS_WIN_8_DUAL,
1547 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1548 			USB_VENDOR_ID_ALPS_JP,
1549 			HID_DEVICE_ID_ALPS_U1_DUAL_PTP) },
1550 	{ .driver_data = MT_CLS_WIN_8_DUAL,
1551 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1552 			USB_VENDOR_ID_ALPS_JP,
1553 			HID_DEVICE_ID_ALPS_U1_DUAL_3BTN_PTP) },
1554 
1555 	/* Lenovo X1 TAB Gen 2 */
1556 	{ .driver_data = MT_CLS_WIN_8_DUAL,
1557 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1558 			   USB_VENDOR_ID_LENOVO,
1559 			   USB_DEVICE_ID_LENOVO_X1_TAB) },
1560 
1561 	/* Anton devices */
1562 	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
1563 		MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
1564 			USB_DEVICE_ID_ANTON_TOUCH_PAD) },
1565 
1566 	/* Asus T304UA */
1567 	{ .driver_data = MT_CLS_ASUS,
1568 		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1569 			USB_VENDOR_ID_ASUSTEK,
1570 			USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD) },
1571 
1572 	/* Atmel panels */
1573 	{ .driver_data = MT_CLS_SERIAL,
1574 		MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
1575 			USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
1576 
1577 	/* Baanto multitouch devices */
1578 	{ .driver_data = MT_CLS_NSMU,
1579 		MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
1580 			USB_DEVICE_ID_BAANTO_MT_190W2) },
1581 
1582 	/* Cando panels */
1583 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1584 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1585 			USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1586 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1587 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1588 			USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
1589 
1590 	/* Chunghwa Telecom touch panels */
1591 	{  .driver_data = MT_CLS_NSMU,
1592 		MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
1593 			USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
1594 
1595 	/* CJTouch panels */
1596 	{ .driver_data = MT_CLS_NSMU,
1597 		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1598 			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
1599 	{ .driver_data = MT_CLS_NSMU,
1600 		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1601 			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
1602 
1603 	/* CVTouch panels */
1604 	{ .driver_data = MT_CLS_NSMU,
1605 		MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
1606 			USB_DEVICE_ID_CVTOUCH_SCREEN) },
1607 
1608 	/* eGalax devices (resistive) */
1609 	{ .driver_data = MT_CLS_EGALAX,
1610 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1611 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
1612 	{ .driver_data = MT_CLS_EGALAX,
1613 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1614 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
1615 
1616 	/* eGalax devices (capacitive) */
1617 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1618 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1619 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
1620 	{ .driver_data = MT_CLS_EGALAX,
1621 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1622 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
1623 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1624 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1625 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
1626 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1627 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1628 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
1629 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1630 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1631 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
1632 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1633 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1634 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
1635 	{ .driver_data = MT_CLS_EGALAX,
1636 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1637 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
1638 	{ .driver_data = MT_CLS_EGALAX,
1639 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1640 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
1641 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1642 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1643 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
1644 	{ .driver_data = MT_CLS_EGALAX,
1645 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1646 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
1647 	{ .driver_data = MT_CLS_EGALAX,
1648 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1649 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
1650 	{ .driver_data = MT_CLS_EGALAX,
1651 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1652 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
1653 	{ .driver_data = MT_CLS_EGALAX,
1654 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1655 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
1656 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1657 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1658 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
1659 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1660 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1661 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
1662 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
1663 		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1664 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
1665 
1666 	/* Elitegroup panel */
1667 	{ .driver_data = MT_CLS_SERIAL,
1668 		MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
1669 			USB_DEVICE_ID_ELITEGROUP_05D8) },
1670 
1671 	/* Flatfrog Panels */
1672 	{ .driver_data = MT_CLS_FLATFROG,
1673 		MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
1674 			USB_DEVICE_ID_MULTITOUCH_3200) },
1675 
1676 	/* FocalTech Panels */
1677 	{ .driver_data = MT_CLS_SERIAL,
1678 		MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
1679 			USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
1680 
1681 	/* GeneralTouch panel */
1682 	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1683 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1684 			USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
1685 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1686 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1687 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
1688 	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1689 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1690 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
1691 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1692 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1693 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
1694 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1695 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1696 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
1697 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1698 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1699 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
1700 	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1701 		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1702 			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
1703 
1704 	/* Gametel game controller */
1705 	{ .driver_data = MT_CLS_NSMU,
1706 		MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
1707 			USB_DEVICE_ID_GAMETEL_MT_MODE) },
1708 
1709 	/* GoodTouch panels */
1710 	{ .driver_data = MT_CLS_NSMU,
1711 		MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
1712 			USB_DEVICE_ID_GOODTOUCH_000f) },
1713 
1714 	/* Hanvon panels */
1715 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1716 		MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
1717 			USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
1718 
1719 	/* Ilitek dual touch panel */
1720 	{  .driver_data = MT_CLS_NSMU,
1721 		MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
1722 			USB_DEVICE_ID_ILITEK_MULTITOUCH) },
1723 
1724 	/* LG Melfas panel */
1725 	{ .driver_data = MT_CLS_LG,
1726 		HID_USB_DEVICE(USB_VENDOR_ID_LG,
1727 			USB_DEVICE_ID_LG_MELFAS_MT) },
1728 
1729 	/* MosArt panels */
1730 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1731 		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1732 			USB_DEVICE_ID_ASUS_T91MT)},
1733 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1734 		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1735 			USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
1736 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1737 		MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
1738 			USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
1739 
1740 	/* Novatek Panel */
1741 	{ .driver_data = MT_CLS_NSMU,
1742 		MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
1743 			USB_DEVICE_ID_NOVATEK_PCT) },
1744 
1745 	/* Ntrig Panel */
1746 	{ .driver_data = MT_CLS_NSMU,
1747 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1748 			USB_VENDOR_ID_NTRIG, 0x1b05) },
1749 
1750 	/* Panasonic panels */
1751 	{ .driver_data = MT_CLS_PANASONIC,
1752 		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1753 			USB_DEVICE_ID_PANABOARD_UBT780) },
1754 	{ .driver_data = MT_CLS_PANASONIC,
1755 		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1756 			USB_DEVICE_ID_PANABOARD_UBT880) },
1757 
1758 	/* PixArt optical touch screen */
1759 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1760 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1761 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
1762 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1763 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1764 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
1765 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1766 		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1767 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
1768 
1769 	/* PixCir-based panels */
1770 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1771 		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1772 			USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
1773 
1774 	/* Quanta-based panels */
1775 	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1776 		MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
1777 			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
1778 
1779 	/* Razer touchpads */
1780 	{ .driver_data = MT_CLS_RAZER_BLADE_STEALTH,
1781 		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1782 			USB_VENDOR_ID_SYNAPTICS, 0x8323) },
1783 
1784 	/* Stantum panels */
1785 	{ .driver_data = MT_CLS_CONFIDENCE,
1786 		MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
1787 			USB_DEVICE_ID_MTP_STM)},
1788 
1789 	/* TopSeed panels */
1790 	{ .driver_data = MT_CLS_TOPSEED,
1791 		MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
1792 			USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
1793 
1794 	/* Touch International panels */
1795 	{ .driver_data = MT_CLS_NSMU,
1796 		MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
1797 			USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
1798 
1799 	/* Unitec panels */
1800 	{ .driver_data = MT_CLS_NSMU,
1801 		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1802 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
1803 	{ .driver_data = MT_CLS_NSMU,
1804 		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1805 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
1806 
1807 	/* VTL panels */
1808 	{ .driver_data = MT_CLS_VTL,
1809 		MT_USB_DEVICE(USB_VENDOR_ID_VTL,
1810 			USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
1811 
1812 	/* Wistron panels */
1813 	{ .driver_data = MT_CLS_NSMU,
1814 		MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
1815 			USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
1816 
1817 	/* XAT */
1818 	{ .driver_data = MT_CLS_NSMU,
1819 		MT_USB_DEVICE(USB_VENDOR_ID_XAT,
1820 			USB_DEVICE_ID_XAT_CSR) },
1821 
1822 	/* Xiroku */
1823 	{ .driver_data = MT_CLS_NSMU,
1824 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1825 			USB_DEVICE_ID_XIROKU_SPX) },
1826 	{ .driver_data = MT_CLS_NSMU,
1827 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1828 			USB_DEVICE_ID_XIROKU_MPX) },
1829 	{ .driver_data = MT_CLS_NSMU,
1830 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1831 			USB_DEVICE_ID_XIROKU_CSR) },
1832 	{ .driver_data = MT_CLS_NSMU,
1833 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1834 			USB_DEVICE_ID_XIROKU_SPX1) },
1835 	{ .driver_data = MT_CLS_NSMU,
1836 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1837 			USB_DEVICE_ID_XIROKU_MPX1) },
1838 	{ .driver_data = MT_CLS_NSMU,
1839 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1840 			USB_DEVICE_ID_XIROKU_CSR1) },
1841 	{ .driver_data = MT_CLS_NSMU,
1842 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1843 			USB_DEVICE_ID_XIROKU_SPX2) },
1844 	{ .driver_data = MT_CLS_NSMU,
1845 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1846 			USB_DEVICE_ID_XIROKU_MPX2) },
1847 	{ .driver_data = MT_CLS_NSMU,
1848 		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1849 			USB_DEVICE_ID_XIROKU_CSR2) },
1850 
1851 	/* Google MT devices */
1852 	{ .driver_data = MT_CLS_GOOGLE,
1853 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_GOOGLE,
1854 			USB_DEVICE_ID_GOOGLE_TOUCH_ROSE) },
1855 
1856 	/* Generic MT device */
1857 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
1858 
1859 	/* Generic Win 8 certified MT device */
1860 	{  .driver_data = MT_CLS_WIN_8,
1861 		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
1862 			HID_ANY_ID, HID_ANY_ID) },
1863 	{ }
1864 };
1865 MODULE_DEVICE_TABLE(hid, mt_devices);
1866 
1867 static const struct hid_usage_id mt_grabbed_usages[] = {
1868 	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1869 	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
1870 };
1871 
1872 static struct hid_driver mt_driver = {
1873 	.name = "hid-multitouch",
1874 	.id_table = mt_devices,
1875 	.probe = mt_probe,
1876 	.remove = mt_remove,
1877 	.input_mapping = mt_input_mapping,
1878 	.input_mapped = mt_input_mapped,
1879 	.input_configured = mt_input_configured,
1880 	.feature_mapping = mt_feature_mapping,
1881 	.usage_table = mt_grabbed_usages,
1882 	.event = mt_event,
1883 	.report = mt_report,
1884 #ifdef CONFIG_PM
1885 	.reset_resume = mt_reset_resume,
1886 	.resume = mt_resume,
1887 #endif
1888 };
1889 module_hid_driver(mt_driver);
1890