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