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