OSDN Git Service

usb: core: Add PM runtime calls to usb_hcd_platform_shutdown
[sagit-ice-cold/kernel_xiaomi_msm8998.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/string.h>
47
48
49 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
50 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
51 MODULE_DESCRIPTION("HID multitouch panels");
52 MODULE_LICENSE("GPL");
53
54 #include "hid-ids.h"
55
56 /* quirks to control the device */
57 #define MT_QUIRK_NOT_SEEN_MEANS_UP      (1 << 0)
58 #define MT_QUIRK_SLOT_IS_CONTACTID      (1 << 1)
59 #define MT_QUIRK_CYPRESS                (1 << 2)
60 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER  (1 << 3)
61 #define MT_QUIRK_ALWAYS_VALID           (1 << 4)
62 #define MT_QUIRK_VALID_IS_INRANGE       (1 << 5)
63 #define MT_QUIRK_VALID_IS_CONFIDENCE    (1 << 6)
64 #define MT_QUIRK_CONFIDENCE             (1 << 7)
65 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE    (1 << 8)
66 #define MT_QUIRK_NO_AREA                (1 << 9)
67 #define MT_QUIRK_IGNORE_DUPLICATES      (1 << 10)
68 #define MT_QUIRK_HOVERING               (1 << 11)
69 #define MT_QUIRK_CONTACT_CNT_ACCURATE   (1 << 12)
70 #define MT_QUIRK_FORCE_GET_FEATURE      (1 << 13)
71
72 #define MT_INPUTMODE_TOUCHSCREEN        0x02
73 #define MT_INPUTMODE_TOUCHPAD           0x03
74
75 #define MT_BUTTONTYPE_CLICKPAD          0
76
77 struct mt_slot {
78         __s32 x, y, cx, cy, p, w, h;
79         __s32 contactid;        /* the device ContactID assigned to this slot */
80         bool touch_state;       /* is the touch valid? */
81         bool inrange_state;     /* is the finger in proximity of the sensor? */
82         bool confidence_state;  /* is the touch made by a finger? */
83 };
84
85 struct mt_class {
86         __s32 name;     /* MT_CLS */
87         __s32 quirks;
88         __s32 sn_move;  /* Signal/noise ratio for move events */
89         __s32 sn_width; /* Signal/noise ratio for width events */
90         __s32 sn_height;        /* Signal/noise ratio for height events */
91         __s32 sn_pressure;      /* Signal/noise ratio for pressure events */
92         __u8 maxcontacts;
93         bool is_indirect;       /* true for touchpads */
94         bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
95 };
96
97 struct mt_fields {
98         unsigned usages[HID_MAX_FIELDS];
99         unsigned int length;
100 };
101
102 struct mt_device {
103         struct mt_slot curdata; /* placeholder of incoming data */
104         struct mt_class mtclass;        /* our mt device class */
105         struct mt_fields *fields;       /* temporary placeholder for storing the
106                                            multitouch fields */
107         int cc_index;   /* contact count field index in the report */
108         int cc_value_index;     /* contact count value index in the field */
109         unsigned last_slot_field;       /* the last field of a slot */
110         unsigned mt_report_id;  /* the report ID of the multitouch device */
111         __s16 inputmode;        /* InputMode HID feature, -1 if non-existent */
112         __s16 inputmode_index;  /* InputMode HID feature index in the report */
113         __s16 maxcontact_report_id;     /* Maximum Contact Number HID feature,
114                                    -1 if non-existent */
115         __u8 inputmode_value;  /* InputMode HID feature value */
116         __u8 num_received;      /* how many contacts we received */
117         __u8 num_expected;      /* expected last contact index */
118         __u8 maxcontacts;
119         __u8 touches_by_report; /* how many touches are present in one report:
120                                 * 1 means we should use a serial protocol
121                                 * > 1 means hybrid (multitouch) protocol */
122         __u8 buttons_count;     /* number of physical buttons per touchpad */
123         bool is_buttonpad;      /* is this device a button pad? */
124         bool serial_maybe;      /* need to check for serial protocol */
125         bool curvalid;          /* is the current contact valid? */
126         unsigned mt_flags;      /* flags to pass to input-mt */
127 };
128
129 static void mt_post_parse_default_settings(struct mt_device *td);
130 static void mt_post_parse(struct mt_device *td);
131
132 /* classes of device behavior */
133 #define MT_CLS_DEFAULT                          0x0001
134
135 #define MT_CLS_SERIAL                           0x0002
136 #define MT_CLS_CONFIDENCE                       0x0003
137 #define MT_CLS_CONFIDENCE_CONTACT_ID            0x0004
138 #define MT_CLS_CONFIDENCE_MINUS_ONE             0x0005
139 #define MT_CLS_DUAL_INRANGE_CONTACTID           0x0006
140 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER       0x0007
141 /* reserved                                     0x0008 */
142 #define MT_CLS_INRANGE_CONTACTNUMBER            0x0009
143 #define MT_CLS_NSMU                             0x000a
144 /* reserved                                     0x0010 */
145 /* reserved                                     0x0011 */
146 #define MT_CLS_WIN_8                            0x0012
147 #define MT_CLS_EXPORT_ALL_INPUTS                0x0013
148
149 /* vendor specific classes */
150 #define MT_CLS_3M                               0x0101
151 /* reserved                                     0x0102 */
152 #define MT_CLS_EGALAX                           0x0103
153 #define MT_CLS_EGALAX_SERIAL                    0x0104
154 #define MT_CLS_TOPSEED                          0x0105
155 #define MT_CLS_PANASONIC                        0x0106
156 #define MT_CLS_FLATFROG                         0x0107
157 #define MT_CLS_GENERALTOUCH_TWOFINGERS          0x0108
158 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS      0x0109
159 #define MT_CLS_VTL                              0x0110
160
161 #define MT_DEFAULT_MAXCONTACT   10
162 #define MT_MAX_MAXCONTACT       250
163
164 #define MT_USB_DEVICE(v, p)     HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
165 #define MT_BT_DEVICE(v, p)      HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
166
167 /*
168  * these device-dependent functions determine what slot corresponds
169  * to a valid contact that was just read.
170  */
171
172 static int cypress_compute_slot(struct mt_device *td)
173 {
174         if (td->curdata.contactid != 0 || td->num_received == 0)
175                 return td->curdata.contactid;
176         else
177                 return -1;
178 }
179
180 static struct mt_class mt_classes[] = {
181         { .name = MT_CLS_DEFAULT,
182                 .quirks = MT_QUIRK_ALWAYS_VALID |
183                         MT_QUIRK_CONTACT_CNT_ACCURATE },
184         { .name = MT_CLS_NSMU,
185                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
186         { .name = MT_CLS_SERIAL,
187                 .quirks = MT_QUIRK_ALWAYS_VALID},
188         { .name = MT_CLS_CONFIDENCE,
189                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
190         { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
191                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
192                         MT_QUIRK_SLOT_IS_CONTACTID },
193         { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
194                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
195                         MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
196         { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
197                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
198                         MT_QUIRK_SLOT_IS_CONTACTID,
199                 .maxcontacts = 2 },
200         { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
201                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
202                         MT_QUIRK_SLOT_IS_CONTACTNUMBER,
203                 .maxcontacts = 2 },
204         { .name = MT_CLS_INRANGE_CONTACTNUMBER,
205                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
206                         MT_QUIRK_SLOT_IS_CONTACTNUMBER },
207         { .name = MT_CLS_WIN_8,
208                 .quirks = MT_QUIRK_ALWAYS_VALID |
209                         MT_QUIRK_IGNORE_DUPLICATES |
210                         MT_QUIRK_HOVERING |
211                         MT_QUIRK_CONTACT_CNT_ACCURATE },
212         { .name = MT_CLS_EXPORT_ALL_INPUTS,
213                 .quirks = MT_QUIRK_ALWAYS_VALID |
214                         MT_QUIRK_CONTACT_CNT_ACCURATE,
215                 .export_all_inputs = true },
216
217         /*
218          * vendor specific classes
219          */
220         { .name = MT_CLS_3M,
221                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
222                         MT_QUIRK_SLOT_IS_CONTACTID,
223                 .sn_move = 2048,
224                 .sn_width = 128,
225                 .sn_height = 128,
226                 .maxcontacts = 60,
227         },
228         { .name = MT_CLS_EGALAX,
229                 .quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
230                         MT_QUIRK_VALID_IS_INRANGE,
231                 .sn_move = 4096,
232                 .sn_pressure = 32,
233         },
234         { .name = MT_CLS_EGALAX_SERIAL,
235                 .quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
236                         MT_QUIRK_ALWAYS_VALID,
237                 .sn_move = 4096,
238                 .sn_pressure = 32,
239         },
240         { .name = MT_CLS_TOPSEED,
241                 .quirks = MT_QUIRK_ALWAYS_VALID,
242                 .is_indirect = true,
243                 .maxcontacts = 2,
244         },
245         { .name = MT_CLS_PANASONIC,
246                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
247                 .maxcontacts = 4 },
248         { .name = MT_CLS_GENERALTOUCH_TWOFINGERS,
249                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
250                         MT_QUIRK_VALID_IS_INRANGE |
251                         MT_QUIRK_SLOT_IS_CONTACTID,
252                 .maxcontacts = 2
253         },
254         { .name = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
255                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
256                         MT_QUIRK_SLOT_IS_CONTACTID
257         },
258
259         { .name = MT_CLS_FLATFROG,
260                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
261                         MT_QUIRK_NO_AREA,
262                 .sn_move = 2048,
263                 .maxcontacts = 40,
264         },
265         { .name = MT_CLS_VTL,
266                 .quirks = MT_QUIRK_ALWAYS_VALID |
267                         MT_QUIRK_CONTACT_CNT_ACCURATE |
268                         MT_QUIRK_FORCE_GET_FEATURE,
269         },
270         { }
271 };
272
273 static ssize_t mt_show_quirks(struct device *dev,
274                            struct device_attribute *attr,
275                            char *buf)
276 {
277         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
278         struct mt_device *td = hid_get_drvdata(hdev);
279
280         return sprintf(buf, "%u\n", td->mtclass.quirks);
281 }
282
283 static ssize_t mt_set_quirks(struct device *dev,
284                           struct device_attribute *attr,
285                           const char *buf, size_t count)
286 {
287         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
288         struct mt_device *td = hid_get_drvdata(hdev);
289
290         unsigned long val;
291
292         if (kstrtoul(buf, 0, &val))
293                 return -EINVAL;
294
295         td->mtclass.quirks = val;
296
297         if (td->cc_index < 0)
298                 td->mtclass.quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
299
300         return count;
301 }
302
303 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
304
305 static struct attribute *sysfs_attrs[] = {
306         &dev_attr_quirks.attr,
307         NULL
308 };
309
310 static struct attribute_group mt_attribute_group = {
311         .attrs = sysfs_attrs
312 };
313
314 static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
315 {
316         struct mt_device *td = hid_get_drvdata(hdev);
317         int ret;
318         u32 size = hid_report_len(report);
319         u8 *buf;
320
321         /*
322          * Only fetch the feature report if initial reports are not already
323          * been retrieved. Currently this is only done for Windows 8 touch
324          * devices.
325          */
326         if (!(hdev->quirks & HID_QUIRK_NO_INIT_REPORTS))
327                 return;
328         if (td->mtclass.name != MT_CLS_WIN_8)
329                 return;
330
331         buf = hid_alloc_report_buf(report, GFP_KERNEL);
332         if (!buf)
333                 return;
334
335         ret = hid_hw_raw_request(hdev, report->id, buf, size,
336                                  HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
337         if (ret < 0) {
338                 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
339                          report->id);
340         } else {
341                 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
342                                            size, 0);
343                 if (ret)
344                         dev_warn(&hdev->dev, "failed to report feature\n");
345         }
346
347         kfree(buf);
348 }
349
350 static void mt_feature_mapping(struct hid_device *hdev,
351                 struct hid_field *field, struct hid_usage *usage)
352 {
353         struct mt_device *td = hid_get_drvdata(hdev);
354
355         switch (usage->hid) {
356         case HID_DG_INPUTMODE:
357                 /* Ignore if value index is out of bounds. */
358                 if (usage->usage_index >= field->report_count) {
359                         dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
360                         break;
361                 }
362
363                 if (td->inputmode < 0) {
364                         td->inputmode = field->report->id;
365                         td->inputmode_index = usage->usage_index;
366                 } else {
367                         /*
368                          * Some elan panels wrongly declare 2 input mode
369                          * features, and silently ignore when we set the
370                          * value in the second field. Skip the second feature
371                          * and hope for the best.
372                          */
373                         dev_info(&hdev->dev,
374                                  "Ignoring the extra HID_DG_INPUTMODE\n");
375                 }
376
377                 break;
378         case HID_DG_CONTACTMAX:
379                 mt_get_feature(hdev, field->report);
380
381                 td->maxcontact_report_id = field->report->id;
382                 td->maxcontacts = field->value[0];
383                 if (!td->maxcontacts &&
384                     field->logical_maximum <= MT_MAX_MAXCONTACT)
385                         td->maxcontacts = field->logical_maximum;
386                 if (td->mtclass.maxcontacts)
387                         /* check if the maxcontacts is given by the class */
388                         td->maxcontacts = td->mtclass.maxcontacts;
389
390                 break;
391         case HID_DG_BUTTONTYPE:
392                 if (usage->usage_index >= field->report_count) {
393                         dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
394                         break;
395                 }
396
397                 mt_get_feature(hdev, field->report);
398                 if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
399                         td->is_buttonpad = true;
400
401                 break;
402         case 0xff0000c5:
403                 /* Retrieve the Win8 blob once to enable some devices */
404                 if (usage->usage_index == 0)
405                         mt_get_feature(hdev, field->report);
406                 break;
407         }
408 }
409
410 static void set_abs(struct input_dev *input, unsigned int code,
411                 struct hid_field *field, int snratio)
412 {
413         int fmin = field->logical_minimum;
414         int fmax = field->logical_maximum;
415         int fuzz = snratio ? (fmax - fmin) / snratio : 0;
416         input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
417         input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
418 }
419
420 static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
421                 struct hid_input *hi)
422 {
423         struct mt_fields *f = td->fields;
424
425         if (f->length >= HID_MAX_FIELDS)
426                 return;
427
428         f->usages[f->length++] = usage->hid;
429 }
430
431 static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
432                 struct hid_field *field, struct hid_usage *usage,
433                 unsigned long **bit, int *max)
434 {
435         struct mt_device *td = hid_get_drvdata(hdev);
436         struct mt_class *cls = &td->mtclass;
437         int code;
438         struct hid_usage *prev_usage = NULL;
439
440         if (field->application == HID_DG_TOUCHSCREEN)
441                 td->mt_flags |= INPUT_MT_DIRECT;
442
443         /*
444          * Model touchscreens providing buttons as touchpads.
445          */
446         if (field->application == HID_DG_TOUCHPAD ||
447             (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
448                 td->mt_flags |= INPUT_MT_POINTER;
449                 td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
450         }
451
452         /* count the buttons on touchpads */
453         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
454                 td->buttons_count++;
455
456         if (usage->usage_index)
457                 prev_usage = &field->usage[usage->usage_index - 1];
458
459         switch (usage->hid & HID_USAGE_PAGE) {
460
461         case HID_UP_GENDESK:
462                 switch (usage->hid) {
463                 case HID_GD_X:
464                         if (prev_usage && (prev_usage->hid == usage->hid)) {
465                                 hid_map_usage(hi, usage, bit, max,
466                                         EV_ABS, ABS_MT_TOOL_X);
467                                 set_abs(hi->input, ABS_MT_TOOL_X, field,
468                                         cls->sn_move);
469                         } else {
470                                 hid_map_usage(hi, usage, bit, max,
471                                         EV_ABS, ABS_MT_POSITION_X);
472                                 set_abs(hi->input, ABS_MT_POSITION_X, field,
473                                         cls->sn_move);
474                         }
475
476                         mt_store_field(usage, td, hi);
477                         return 1;
478                 case HID_GD_Y:
479                         if (prev_usage && (prev_usage->hid == usage->hid)) {
480                                 hid_map_usage(hi, usage, bit, max,
481                                         EV_ABS, ABS_MT_TOOL_Y);
482                                 set_abs(hi->input, ABS_MT_TOOL_Y, field,
483                                         cls->sn_move);
484                         } else {
485                                 hid_map_usage(hi, usage, bit, max,
486                                         EV_ABS, ABS_MT_POSITION_Y);
487                                 set_abs(hi->input, ABS_MT_POSITION_Y, field,
488                                         cls->sn_move);
489                         }
490
491                         mt_store_field(usage, td, hi);
492                         return 1;
493                 }
494                 return 0;
495
496         case HID_UP_DIGITIZER:
497                 switch (usage->hid) {
498                 case HID_DG_INRANGE:
499                         if (cls->quirks & MT_QUIRK_HOVERING) {
500                                 hid_map_usage(hi, usage, bit, max,
501                                         EV_ABS, ABS_MT_DISTANCE);
502                                 input_set_abs_params(hi->input,
503                                         ABS_MT_DISTANCE, 0, 1, 0, 0);
504                         }
505                         mt_store_field(usage, td, hi);
506                         return 1;
507                 case HID_DG_CONFIDENCE:
508                         if (cls->name == MT_CLS_WIN_8 &&
509                                 field->application == HID_DG_TOUCHPAD)
510                                 cls->quirks |= MT_QUIRK_CONFIDENCE;
511                         mt_store_field(usage, td, hi);
512                         return 1;
513                 case HID_DG_TIPSWITCH:
514                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
515                         input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
516                         mt_store_field(usage, td, hi);
517                         return 1;
518                 case HID_DG_CONTACTID:
519                         mt_store_field(usage, td, hi);
520                         td->touches_by_report++;
521                         td->mt_report_id = field->report->id;
522                         return 1;
523                 case HID_DG_WIDTH:
524                         hid_map_usage(hi, usage, bit, max,
525                                         EV_ABS, ABS_MT_TOUCH_MAJOR);
526                         if (!(cls->quirks & MT_QUIRK_NO_AREA))
527                                 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
528                                         cls->sn_width);
529                         mt_store_field(usage, td, hi);
530                         return 1;
531                 case HID_DG_HEIGHT:
532                         hid_map_usage(hi, usage, bit, max,
533                                         EV_ABS, ABS_MT_TOUCH_MINOR);
534                         if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
535                                 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
536                                         cls->sn_height);
537                                 input_set_abs_params(hi->input,
538                                         ABS_MT_ORIENTATION, 0, 1, 0, 0);
539                         }
540                         mt_store_field(usage, td, hi);
541                         return 1;
542                 case HID_DG_TIPPRESSURE:
543                         hid_map_usage(hi, usage, bit, max,
544                                         EV_ABS, ABS_MT_PRESSURE);
545                         set_abs(hi->input, ABS_MT_PRESSURE, field,
546                                 cls->sn_pressure);
547                         mt_store_field(usage, td, hi);
548                         return 1;
549                 case HID_DG_CONTACTCOUNT:
550                         /* Ignore if indexes are out of bounds. */
551                         if (field->index >= field->report->maxfield ||
552                             usage->usage_index >= field->report_count)
553                                 return 1;
554                         td->cc_index = field->index;
555                         td->cc_value_index = usage->usage_index;
556                         return 1;
557                 case HID_DG_CONTACTMAX:
558                         /* we don't set td->last_slot_field as contactcount and
559                          * contact max are global to the report */
560                         return -1;
561                 case HID_DG_TOUCH:
562                         /* Legacy devices use TIPSWITCH and not TOUCH.
563                          * Let's just ignore this field. */
564                         return -1;
565                 }
566                 /* let hid-input decide for the others */
567                 return 0;
568
569         case HID_UP_BUTTON:
570                 code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
571                 hid_map_usage(hi, usage, bit, max, EV_KEY, code);
572                 input_set_capability(hi->input, EV_KEY, code);
573                 return 1;
574
575         case 0xff000000:
576                 /* we do not want to map these: no input-oriented meaning */
577                 return -1;
578         }
579
580         return 0;
581 }
582
583 static int mt_touch_input_mapped(struct hid_device *hdev, struct hid_input *hi,
584                 struct hid_field *field, struct hid_usage *usage,
585                 unsigned long **bit, int *max)
586 {
587         if (usage->type == EV_KEY || usage->type == EV_ABS)
588                 set_bit(usage->type, hi->input->evbit);
589
590         return -1;
591 }
592
593 static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
594 {
595         __s32 quirks = td->mtclass.quirks;
596
597         if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
598                 return td->curdata.contactid;
599
600         if (quirks & MT_QUIRK_CYPRESS)
601                 return cypress_compute_slot(td);
602
603         if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
604                 return td->num_received;
605
606         if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
607                 return td->curdata.contactid - 1;
608
609         return input_mt_get_slot_by_key(input, td->curdata.contactid);
610 }
611
612 /*
613  * this function is called when a whole contact has been processed,
614  * so that it can assign it to a slot and store the data there
615  */
616 static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
617 {
618         if ((td->mtclass.quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
619             td->num_received >= td->num_expected)
620                 return;
621
622         if (td->curvalid || (td->mtclass.quirks & MT_QUIRK_ALWAYS_VALID)) {
623                 int active;
624                 int slotnum = mt_compute_slot(td, input);
625                 struct mt_slot *s = &td->curdata;
626                 struct input_mt *mt = input->mt;
627
628                 if (slotnum < 0 || slotnum >= td->maxcontacts)
629                         return;
630
631                 if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
632                         struct input_mt_slot *slot = &mt->slots[slotnum];
633                         if (input_mt_is_active(slot) &&
634                             input_mt_is_used(mt, slot))
635                                 return;
636                 }
637
638                 if (!(td->mtclass.quirks & MT_QUIRK_CONFIDENCE))
639                         s->confidence_state = 1;
640                 active = (s->touch_state || s->inrange_state) &&
641                                                         s->confidence_state;
642
643                 input_mt_slot(input, slotnum);
644                 input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
645                 if (active) {
646                         /* this finger is in proximity of the sensor */
647                         int wide = (s->w > s->h);
648                         /* divided by two to match visual scale of touch */
649                         int major = max(s->w, s->h) >> 1;
650                         int minor = min(s->w, s->h) >> 1;
651
652                         input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
653                         input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
654                         input_event(input, EV_ABS, ABS_MT_TOOL_X, s->cx);
655                         input_event(input, EV_ABS, ABS_MT_TOOL_Y, s->cy);
656                         input_event(input, EV_ABS, ABS_MT_DISTANCE,
657                                 !s->touch_state);
658                         input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
659                         input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
660                         input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
661                         input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
662                 }
663         }
664
665         td->num_received++;
666 }
667
668 /*
669  * this function is called when a whole packet has been received and processed,
670  * so that it can decide what to send to the input layer.
671  */
672 static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
673 {
674         input_mt_sync_frame(input);
675         input_sync(input);
676         td->num_received = 0;
677 }
678
679 static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
680                                 struct hid_usage *usage, __s32 value)
681 {
682         /* we will handle the hidinput part later, now remains hiddev */
683         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
684                 hid->hiddev_hid_event(hid, field, usage, value);
685
686         return 1;
687 }
688
689 static void mt_process_mt_event(struct hid_device *hid, struct hid_field *field,
690                                 struct hid_usage *usage, __s32 value)
691 {
692         struct mt_device *td = hid_get_drvdata(hid);
693         __s32 quirks = td->mtclass.quirks;
694         struct input_dev *input = field->hidinput->input;
695
696         if (hid->claimed & HID_CLAIMED_INPUT) {
697                 switch (usage->hid) {
698                 case HID_DG_INRANGE:
699                         if (quirks & MT_QUIRK_VALID_IS_INRANGE)
700                                 td->curvalid = value;
701                         if (quirks & MT_QUIRK_HOVERING)
702                                 td->curdata.inrange_state = value;
703                         break;
704                 case HID_DG_TIPSWITCH:
705                         if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
706                                 td->curvalid = value;
707                         td->curdata.touch_state = value;
708                         break;
709                 case HID_DG_CONFIDENCE:
710                         if (quirks & MT_QUIRK_CONFIDENCE)
711                                 td->curdata.confidence_state = value;
712                         if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
713                                 td->curvalid = value;
714                         break;
715                 case HID_DG_CONTACTID:
716                         td->curdata.contactid = value;
717                         break;
718                 case HID_DG_TIPPRESSURE:
719                         td->curdata.p = value;
720                         break;
721                 case HID_GD_X:
722                         if (usage->code == ABS_MT_TOOL_X)
723                                 td->curdata.cx = value;
724                         else
725                                 td->curdata.x = value;
726                         break;
727                 case HID_GD_Y:
728                         if (usage->code == ABS_MT_TOOL_Y)
729                                 td->curdata.cy = value;
730                         else
731                                 td->curdata.y = value;
732                         break;
733                 case HID_DG_WIDTH:
734                         td->curdata.w = value;
735                         break;
736                 case HID_DG_HEIGHT:
737                         td->curdata.h = value;
738                         break;
739                 case HID_DG_CONTACTCOUNT:
740                         break;
741                 case HID_DG_TOUCH:
742                         /* do nothing */
743                         break;
744
745                 default:
746                         if (usage->type)
747                                 input_event(input, usage->type, usage->code,
748                                                 value);
749                         return;
750                 }
751
752                 if (usage->usage_index + 1 == field->report_count) {
753                         /* we only take into account the last report. */
754                         if (usage->hid == td->last_slot_field)
755                                 mt_complete_slot(td, field->hidinput->input);
756                 }
757
758         }
759 }
760
761 static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
762 {
763         struct mt_device *td = hid_get_drvdata(hid);
764         struct hid_field *field;
765         unsigned count;
766         int r, n;
767
768         /*
769          * Includes multi-packet support where subsequent
770          * packets are sent with zero contactcount.
771          */
772         if (td->cc_index >= 0) {
773                 struct hid_field *field = report->field[td->cc_index];
774                 int value = field->value[td->cc_value_index];
775                 if (value)
776                         td->num_expected = value;
777         }
778
779         for (r = 0; r < report->maxfield; r++) {
780                 field = report->field[r];
781                 count = field->report_count;
782
783                 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
784                         continue;
785
786                 for (n = 0; n < count; n++)
787                         mt_process_mt_event(hid, field, &field->usage[n],
788                                         field->value[n]);
789         }
790
791         if (td->num_received >= td->num_expected)
792                 mt_sync_frame(td, report->field[0]->hidinput->input);
793 }
794
795 static int mt_touch_input_configured(struct hid_device *hdev,
796                                         struct hid_input *hi)
797 {
798         struct mt_device *td = hid_get_drvdata(hdev);
799         struct mt_class *cls = &td->mtclass;
800         struct input_dev *input = hi->input;
801         int ret;
802
803         if (!td->maxcontacts)
804                 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
805
806         mt_post_parse(td);
807         if (td->serial_maybe)
808                 mt_post_parse_default_settings(td);
809
810         if (cls->is_indirect)
811                 td->mt_flags |= INPUT_MT_POINTER;
812
813         if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
814                 td->mt_flags |= INPUT_MT_DROP_UNUSED;
815
816         /* check for clickpads */
817         if ((td->mt_flags & INPUT_MT_POINTER) && (td->buttons_count == 1))
818                 td->is_buttonpad = true;
819
820         if (td->is_buttonpad)
821                 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
822
823         ret = input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
824         if (ret)
825                 return ret;
826
827         td->mt_flags = 0;
828         return 0;
829 }
830
831 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
832                 struct hid_field *field, struct hid_usage *usage,
833                 unsigned long **bit, int *max)
834 {
835         struct mt_device *td = hid_get_drvdata(hdev);
836
837         /*
838          * If mtclass.export_all_inputs is not set, only map fields from
839          * TouchScreen or TouchPad collections. We need to ignore fields
840          * that belong to other collections such as Mouse that might have
841          * the same GenericDesktop usages.
842          */
843         if (!td->mtclass.export_all_inputs &&
844             field->application != HID_DG_TOUCHSCREEN &&
845             field->application != HID_DG_PEN &&
846             field->application != HID_DG_TOUCHPAD)
847                 return -1;
848
849         /*
850          * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
851          * for the stylus.
852          * The check for mt_report_id ensures we don't process
853          * HID_DG_CONTACTCOUNT from the pen report as it is outside the physical
854          * collection, but within the report ID.
855          */
856         if (field->physical == HID_DG_STYLUS)
857                 return 0;
858         else if ((field->physical == 0) &&
859                  (field->report->id != td->mt_report_id) &&
860                  (td->mt_report_id != -1))
861                 return 0;
862
863         if (field->application == HID_DG_TOUCHSCREEN ||
864             field->application == HID_DG_TOUCHPAD)
865                 return mt_touch_input_mapping(hdev, hi, field, usage, bit, max);
866
867         /* let hid-core decide for the others */
868         return 0;
869 }
870
871 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
872                 struct hid_field *field, struct hid_usage *usage,
873                 unsigned long **bit, int *max)
874 {
875         /*
876          * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
877          * for the stylus.
878          */
879         if (field->physical == HID_DG_STYLUS)
880                 return 0;
881
882         if (field->application == HID_DG_TOUCHSCREEN ||
883             field->application == HID_DG_TOUCHPAD)
884                 return mt_touch_input_mapped(hdev, hi, field, usage, bit, max);
885
886         /* let hid-core decide for the others */
887         return 0;
888 }
889
890 static int mt_event(struct hid_device *hid, struct hid_field *field,
891                                 struct hid_usage *usage, __s32 value)
892 {
893         struct mt_device *td = hid_get_drvdata(hid);
894
895         if (field->report->id == td->mt_report_id)
896                 return mt_touch_event(hid, field, usage, value);
897
898         return 0;
899 }
900
901 static void mt_report(struct hid_device *hid, struct hid_report *report)
902 {
903         struct mt_device *td = hid_get_drvdata(hid);
904         struct hid_field *field = report->field[0];
905
906         if (!(hid->claimed & HID_CLAIMED_INPUT))
907                 return;
908
909         if (report->id == td->mt_report_id)
910                 return mt_touch_report(hid, report);
911
912         if (field && field->hidinput && field->hidinput->input)
913                 input_sync(field->hidinput->input);
914 }
915
916 static void mt_set_input_mode(struct hid_device *hdev)
917 {
918         struct mt_device *td = hid_get_drvdata(hdev);
919         struct hid_report *r;
920         struct hid_report_enum *re;
921         struct mt_class *cls = &td->mtclass;
922         char *buf;
923         u32 report_len;
924
925         if (td->inputmode < 0)
926                 return;
927
928         re = &(hdev->report_enum[HID_FEATURE_REPORT]);
929         r = re->report_id_hash[td->inputmode];
930         if (r) {
931                 if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
932                         report_len = hid_report_len(r);
933                         buf = hid_alloc_report_buf(r, GFP_KERNEL);
934                         if (!buf) {
935                                 hid_err(hdev, "failed to allocate buffer for report\n");
936                                 return;
937                         }
938                         hid_hw_raw_request(hdev, r->id, buf, report_len,
939                                            HID_FEATURE_REPORT,
940                                            HID_REQ_GET_REPORT);
941                         kfree(buf);
942                 }
943                 r->field[0]->value[td->inputmode_index] = td->inputmode_value;
944                 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
945         }
946 }
947
948 static void mt_set_maxcontacts(struct hid_device *hdev)
949 {
950         struct mt_device *td = hid_get_drvdata(hdev);
951         struct hid_report *r;
952         struct hid_report_enum *re;
953         int fieldmax, max;
954
955         if (td->maxcontact_report_id < 0)
956                 return;
957
958         if (!td->mtclass.maxcontacts)
959                 return;
960
961         re = &hdev->report_enum[HID_FEATURE_REPORT];
962         r = re->report_id_hash[td->maxcontact_report_id];
963         if (r) {
964                 max = td->mtclass.maxcontacts;
965                 fieldmax = r->field[0]->logical_maximum;
966                 max = min(fieldmax, max);
967                 if (r->field[0]->value[0] != max) {
968                         r->field[0]->value[0] = max;
969                         hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
970                 }
971         }
972 }
973
974 static void mt_post_parse_default_settings(struct mt_device *td)
975 {
976         __s32 quirks = td->mtclass.quirks;
977
978         /* unknown serial device needs special quirks */
979         if (td->touches_by_report == 1) {
980                 quirks |= MT_QUIRK_ALWAYS_VALID;
981                 quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
982                 quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
983                 quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
984                 quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
985         }
986
987         td->mtclass.quirks = quirks;
988 }
989
990 static void mt_post_parse(struct mt_device *td)
991 {
992         struct mt_fields *f = td->fields;
993         struct mt_class *cls = &td->mtclass;
994
995         if (td->touches_by_report > 0) {
996                 int field_count_per_touch = f->length / td->touches_by_report;
997                 td->last_slot_field = f->usages[field_count_per_touch - 1];
998         }
999
1000         if (td->cc_index < 0)
1001                 cls->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1002 }
1003
1004 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1005 {
1006         struct mt_device *td = hid_get_drvdata(hdev);
1007         char *name;
1008         const char *suffix = NULL;
1009         struct hid_field *field = hi->report->field[0];
1010         int ret;
1011
1012         if (hi->report->id == td->mt_report_id) {
1013                 ret = mt_touch_input_configured(hdev, hi);
1014                 if (ret)
1015                         return ret;
1016         }
1017
1018         /*
1019          * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1020          * for the stylus. Check this first, and then rely on the application
1021          * field.
1022          */
1023         if (hi->report->field[0]->physical == HID_DG_STYLUS) {
1024                 suffix = "Pen";
1025                 /* force BTN_STYLUS to allow tablet matching in udev */
1026                 __set_bit(BTN_STYLUS, hi->input->keybit);
1027         } else {
1028                 switch (field->application) {
1029                 case HID_GD_KEYBOARD:
1030                         suffix = "Keyboard";
1031                         break;
1032                 case HID_GD_KEYPAD:
1033                         suffix = "Keypad";
1034                         break;
1035                 case HID_GD_MOUSE:
1036                         suffix = "Mouse";
1037                         break;
1038                 case HID_DG_STYLUS:
1039                         suffix = "Pen";
1040                         /* force BTN_STYLUS to allow tablet matching in udev */
1041                         __set_bit(BTN_STYLUS, hi->input->keybit);
1042                         break;
1043                 case HID_DG_TOUCHSCREEN:
1044                         /* we do not set suffix = "Touchscreen" */
1045                         break;
1046                 case HID_DG_TOUCHPAD:
1047                         suffix = "Touchpad";
1048                         break;
1049                 case HID_GD_SYSTEM_CONTROL:
1050                         suffix = "System Control";
1051                         break;
1052                 case HID_CP_CONSUMER_CONTROL:
1053                         suffix = "Consumer Control";
1054                         break;
1055                 default:
1056                         suffix = "UNKNOWN";
1057                         break;
1058                 }
1059         }
1060
1061         if (suffix) {
1062                 name = devm_kzalloc(&hi->input->dev,
1063                                     strlen(hdev->name) + strlen(suffix) + 2,
1064                                     GFP_KERNEL);
1065                 if (name) {
1066                         sprintf(name, "%s %s", hdev->name, suffix);
1067                         hi->input->name = name;
1068                 }
1069         }
1070
1071         return 0;
1072 }
1073
1074 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1075 {
1076         int ret, i;
1077         struct mt_device *td;
1078         struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1079
1080         for (i = 0; mt_classes[i].name ; i++) {
1081                 if (id->driver_data == mt_classes[i].name) {
1082                         mtclass = &(mt_classes[i]);
1083                         break;
1084                 }
1085         }
1086
1087         /* This allows the driver to correctly support devices
1088          * that emit events over several HID messages.
1089          */
1090         hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1091
1092         /*
1093          * This allows the driver to handle different input sensors
1094          * that emits events through different reports on the same HID
1095          * device.
1096          */
1097         hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1098         hdev->quirks |= HID_QUIRK_NO_EMPTY_INPUT;
1099
1100         /*
1101          * Handle special quirks for Windows 8 certified devices.
1102          */
1103         if (id->group == HID_GROUP_MULTITOUCH_WIN_8)
1104                 /*
1105                  * Some multitouch screens do not like to be polled for input
1106                  * reports. Fortunately, the Win8 spec says that all touches
1107                  * should be sent during each report, making the initialization
1108                  * of input reports unnecessary.
1109                  *
1110                  * In addition some touchpads do not behave well if we read
1111                  * all feature reports from them. Instead we prevent
1112                  * initial report fetching and then selectively fetch each
1113                  * report we are interested in.
1114                  */
1115                 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1116
1117         td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1118         if (!td) {
1119                 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1120                 return -ENOMEM;
1121         }
1122         td->mtclass = *mtclass;
1123         td->inputmode = -1;
1124         td->maxcontact_report_id = -1;
1125         td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1126         td->cc_index = -1;
1127         td->mt_report_id = -1;
1128         hid_set_drvdata(hdev, td);
1129
1130         td->fields = devm_kzalloc(&hdev->dev, sizeof(struct mt_fields),
1131                                   GFP_KERNEL);
1132         if (!td->fields) {
1133                 dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
1134                 return -ENOMEM;
1135         }
1136
1137         if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1138                 td->serial_maybe = true;
1139
1140         ret = hid_parse(hdev);
1141         if (ret != 0)
1142                 return ret;
1143
1144         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1145         if (ret)
1146                 return ret;
1147
1148         ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
1149
1150         mt_set_maxcontacts(hdev);
1151         mt_set_input_mode(hdev);
1152
1153         /* release .fields memory as it is not used anymore */
1154         devm_kfree(&hdev->dev, td->fields);
1155         td->fields = NULL;
1156
1157         return 0;
1158 }
1159
1160 #ifdef CONFIG_PM
1161 static int mt_reset_resume(struct hid_device *hdev)
1162 {
1163         mt_set_maxcontacts(hdev);
1164         mt_set_input_mode(hdev);
1165         return 0;
1166 }
1167
1168 static int mt_resume(struct hid_device *hdev)
1169 {
1170         /* Some Elan legacy devices require SET_IDLE to be set on resume.
1171          * It should be safe to send it to other devices too.
1172          * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
1173
1174         hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
1175
1176         return 0;
1177 }
1178 #endif
1179
1180 static void mt_remove(struct hid_device *hdev)
1181 {
1182         sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
1183         hid_hw_stop(hdev);
1184 }
1185
1186 /*
1187  * This list contains only:
1188  * - VID/PID of products not working with the default multitouch handling
1189  * - 2 generic rules.
1190  * So there is no point in adding here any device with MT_CLS_DEFAULT.
1191  */
1192 static const struct hid_device_id mt_devices[] = {
1193
1194         /* 3M panels */
1195         { .driver_data = MT_CLS_3M,
1196                 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1197                         USB_DEVICE_ID_3M1968) },
1198         { .driver_data = MT_CLS_3M,
1199                 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1200                         USB_DEVICE_ID_3M2256) },
1201         { .driver_data = MT_CLS_3M,
1202                 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1203                         USB_DEVICE_ID_3M3266) },
1204
1205         /* Anton devices */
1206         { .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
1207                 MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
1208                         USB_DEVICE_ID_ANTON_TOUCH_PAD) },
1209
1210         /* Atmel panels */
1211         { .driver_data = MT_CLS_SERIAL,
1212                 MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
1213                         USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
1214
1215         /* Baanto multitouch devices */
1216         { .driver_data = MT_CLS_NSMU,
1217                 MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
1218                         USB_DEVICE_ID_BAANTO_MT_190W2) },
1219
1220         /* Cando panels */
1221         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1222                 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1223                         USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1224         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1225                 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1226                         USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
1227
1228         /* Chunghwa Telecom touch panels */
1229         {  .driver_data = MT_CLS_NSMU,
1230                 MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
1231                         USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
1232
1233         /* CJTouch panels */
1234         { .driver_data = MT_CLS_NSMU,
1235                 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1236                         USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
1237         { .driver_data = MT_CLS_NSMU,
1238                 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1239                         USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
1240
1241         /* CVTouch panels */
1242         { .driver_data = MT_CLS_NSMU,
1243                 MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
1244                         USB_DEVICE_ID_CVTOUCH_SCREEN) },
1245
1246         /* eGalax devices (resistive) */
1247         { .driver_data = MT_CLS_EGALAX,
1248                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1249                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
1250         { .driver_data = MT_CLS_EGALAX,
1251                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1252                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
1253
1254         /* eGalax devices (capacitive) */
1255         { .driver_data = MT_CLS_EGALAX_SERIAL,
1256                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1257                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
1258         { .driver_data = MT_CLS_EGALAX,
1259                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1260                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
1261         { .driver_data = MT_CLS_EGALAX_SERIAL,
1262                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1263                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
1264         { .driver_data = MT_CLS_EGALAX_SERIAL,
1265                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1266                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
1267         { .driver_data = MT_CLS_EGALAX_SERIAL,
1268                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1269                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
1270         { .driver_data = MT_CLS_EGALAX_SERIAL,
1271                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1272                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
1273         { .driver_data = MT_CLS_EGALAX,
1274                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1275                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
1276         { .driver_data = MT_CLS_EGALAX,
1277                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1278                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
1279         { .driver_data = MT_CLS_EGALAX_SERIAL,
1280                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1281                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
1282         { .driver_data = MT_CLS_EGALAX,
1283                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1284                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
1285         { .driver_data = MT_CLS_EGALAX,
1286                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1287                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
1288         { .driver_data = MT_CLS_EGALAX,
1289                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1290                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
1291         { .driver_data = MT_CLS_EGALAX,
1292                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1293                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
1294         { .driver_data = MT_CLS_EGALAX_SERIAL,
1295                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1296                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
1297         { .driver_data = MT_CLS_EGALAX_SERIAL,
1298                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1299                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
1300         { .driver_data = MT_CLS_EGALAX_SERIAL,
1301                 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1302                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
1303
1304         /* Elitegroup panel */
1305         { .driver_data = MT_CLS_SERIAL,
1306                 MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
1307                         USB_DEVICE_ID_ELITEGROUP_05D8) },
1308
1309         /* Flatfrog Panels */
1310         { .driver_data = MT_CLS_FLATFROG,
1311                 MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
1312                         USB_DEVICE_ID_MULTITOUCH_3200) },
1313
1314         /* FocalTech Panels */
1315         { .driver_data = MT_CLS_SERIAL,
1316                 MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
1317                         USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
1318
1319         /* GeneralTouch panel */
1320         { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1321                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1322                         USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
1323         { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1324                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1325                         USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
1326         { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1327                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1328                         USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
1329         { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1330                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1331                         USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
1332         { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1333                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1334                         USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
1335         { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1336                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1337                         USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
1338         { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1339                 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1340                         USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
1341
1342         /* Gametel game controller */
1343         { .driver_data = MT_CLS_NSMU,
1344                 MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
1345                         USB_DEVICE_ID_GAMETEL_MT_MODE) },
1346
1347         /* GoodTouch panels */
1348         { .driver_data = MT_CLS_NSMU,
1349                 MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
1350                         USB_DEVICE_ID_GOODTOUCH_000f) },
1351
1352         /* Hanvon panels */
1353         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1354                 MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
1355                         USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
1356
1357         /* Ilitek dual touch panel */
1358         {  .driver_data = MT_CLS_NSMU,
1359                 MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
1360                         USB_DEVICE_ID_ILITEK_MULTITOUCH) },
1361
1362         /* MosArt panels */
1363         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1364                 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1365                         USB_DEVICE_ID_ASUS_T91MT)},
1366         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1367                 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1368                         USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
1369         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1370                 MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
1371                         USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
1372
1373         /* Panasonic panels */
1374         { .driver_data = MT_CLS_PANASONIC,
1375                 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1376                         USB_DEVICE_ID_PANABOARD_UBT780) },
1377         { .driver_data = MT_CLS_PANASONIC,
1378                 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1379                         USB_DEVICE_ID_PANABOARD_UBT880) },
1380
1381         /* Novatek Panel */
1382         { .driver_data = MT_CLS_NSMU,
1383                 MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
1384                         USB_DEVICE_ID_NOVATEK_PCT) },
1385
1386         /* PixArt optical touch screen */
1387         { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1388                 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1389                         USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
1390         { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1391                 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1392                         USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
1393         { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1394                 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1395                         USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
1396
1397         /* PixCir-based panels */
1398         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1399                 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1400                         USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
1401
1402         /* Quanta-based panels */
1403         { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1404                 MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
1405                         USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
1406
1407         /* Stantum panels */
1408         { .driver_data = MT_CLS_CONFIDENCE,
1409                 MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
1410                         USB_DEVICE_ID_MTP_STM)},
1411
1412         /* TopSeed panels */
1413         { .driver_data = MT_CLS_TOPSEED,
1414                 MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
1415                         USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
1416
1417         /* Touch International panels */
1418         { .driver_data = MT_CLS_NSMU,
1419                 MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
1420                         USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
1421
1422         /* Unitec panels */
1423         { .driver_data = MT_CLS_NSMU,
1424                 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1425                         USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
1426         { .driver_data = MT_CLS_NSMU,
1427                 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1428                         USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
1429
1430         /* VTL panels */
1431         { .driver_data = MT_CLS_VTL,
1432                 MT_USB_DEVICE(USB_VENDOR_ID_VTL,
1433                         USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
1434
1435         /* Wistron panels */
1436         { .driver_data = MT_CLS_NSMU,
1437                 MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
1438                         USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
1439
1440         /* XAT */
1441         { .driver_data = MT_CLS_NSMU,
1442                 MT_USB_DEVICE(USB_VENDOR_ID_XAT,
1443                         USB_DEVICE_ID_XAT_CSR) },
1444
1445         /* Xiroku */
1446         { .driver_data = MT_CLS_NSMU,
1447                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1448                         USB_DEVICE_ID_XIROKU_SPX) },
1449         { .driver_data = MT_CLS_NSMU,
1450                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1451                         USB_DEVICE_ID_XIROKU_MPX) },
1452         { .driver_data = MT_CLS_NSMU,
1453                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1454                         USB_DEVICE_ID_XIROKU_CSR) },
1455         { .driver_data = MT_CLS_NSMU,
1456                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1457                         USB_DEVICE_ID_XIROKU_SPX1) },
1458         { .driver_data = MT_CLS_NSMU,
1459                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1460                         USB_DEVICE_ID_XIROKU_MPX1) },
1461         { .driver_data = MT_CLS_NSMU,
1462                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1463                         USB_DEVICE_ID_XIROKU_CSR1) },
1464         { .driver_data = MT_CLS_NSMU,
1465                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1466                         USB_DEVICE_ID_XIROKU_SPX2) },
1467         { .driver_data = MT_CLS_NSMU,
1468                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1469                         USB_DEVICE_ID_XIROKU_MPX2) },
1470         { .driver_data = MT_CLS_NSMU,
1471                 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1472                         USB_DEVICE_ID_XIROKU_CSR2) },
1473
1474         /* Generic MT device */
1475         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
1476
1477         /* Generic Win 8 certified MT device */
1478         {  .driver_data = MT_CLS_WIN_8,
1479                 HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
1480                         HID_ANY_ID, HID_ANY_ID) },
1481         { }
1482 };
1483 MODULE_DEVICE_TABLE(hid, mt_devices);
1484
1485 static const struct hid_usage_id mt_grabbed_usages[] = {
1486         { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1487         { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
1488 };
1489
1490 static struct hid_driver mt_driver = {
1491         .name = "hid-multitouch",
1492         .id_table = mt_devices,
1493         .probe = mt_probe,
1494         .remove = mt_remove,
1495         .input_mapping = mt_input_mapping,
1496         .input_mapped = mt_input_mapped,
1497         .input_configured = mt_input_configured,
1498         .feature_mapping = mt_feature_mapping,
1499         .usage_table = mt_grabbed_usages,
1500         .event = mt_event,
1501         .report = mt_report,
1502 #ifdef CONFIG_PM
1503         .reset_resume = mt_reset_resume,
1504         .resume = mt_resume,
1505 #endif
1506 };
1507 module_hid_driver(mt_driver);