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