4c697498fcf9f31bd5666f26c87427e70474b826
[pandora-kernel.git] / drivers / hid / hid-multitouch.c
1 /*
2  *  HID driver for multitouch panels
3  *
4  *  Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
5  *  Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6  *  Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
7  *
8  *  This code is partly based on hid-egalax.c:
9  *
10  *  Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
11  *  Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
12  *  Copyright (c) 2010 Canonical, Ltd.
13  *
14  *  This code is partly based on hid-3m-pct.c:
15  *
16  *  Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
17  *  Copyright (c) 2010      Henrik Rydberg <rydberg@euromail.se>
18  *  Copyright (c) 2010      Canonical, Ltd.
19  *
20  */
21
22 /*
23  * This program is free software; you can redistribute it and/or modify it
24  * under the terms of the GNU General Public License as published by the Free
25  * Software Foundation; either version 2 of the License, or (at your option)
26  * any later version.
27  */
28
29 #include <linux/device.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/usb.h>
34 #include <linux/input/mt.h>
35 #include "usbhid/usbhid.h"
36
37
38 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
39 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
40 MODULE_DESCRIPTION("HID multitouch panels");
41 MODULE_LICENSE("GPL");
42
43 #include "hid-ids.h"
44
45 /* quirks to control the device */
46 #define MT_QUIRK_NOT_SEEN_MEANS_UP      (1 << 0)
47 #define MT_QUIRK_SLOT_IS_CONTACTID      (1 << 1)
48 #define MT_QUIRK_CYPRESS                (1 << 2)
49 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER  (1 << 3)
50 #define MT_QUIRK_ALWAYS_VALID           (1 << 4)
51 #define MT_QUIRK_VALID_IS_INRANGE       (1 << 5)
52 #define MT_QUIRK_VALID_IS_CONFIDENCE    (1 << 6)
53 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE    (1 << 8)
54
55 struct mt_slot {
56         __s32 x, y, p, w, h;
57         __s32 contactid;        /* the device ContactID assigned to this slot */
58         bool touch_state;       /* is the touch valid? */
59         bool seen_in_this_frame;/* has this slot been updated */
60 };
61
62 struct mt_class {
63         __s32 name;     /* MT_CLS */
64         __s32 quirks;
65         __s32 sn_move;  /* Signal/noise ratio for move events */
66         __s32 sn_width; /* Signal/noise ratio for width events */
67         __s32 sn_height;        /* Signal/noise ratio for height events */
68         __s32 sn_pressure;      /* Signal/noise ratio for pressure events */
69         __u8 maxcontacts;
70         bool is_indirect;       /* true for touchpads */
71 };
72
73 struct mt_device {
74         struct mt_slot curdata; /* placeholder of incoming data */
75         struct mt_class mtclass;        /* our mt device class */
76         unsigned last_field_index;      /* last field index of the report */
77         unsigned last_slot_field;       /* the last field of a slot */
78         int last_mt_collection; /* last known mt-related collection */
79         __s8 inputmode;         /* InputMode HID feature, -1 if non-existent */
80         __u8 num_received;      /* how many contacts we received */
81         __u8 num_expected;      /* expected last contact index */
82         __u8 maxcontacts;
83         bool curvalid;          /* is the current contact valid? */
84         struct mt_slot *slots;
85 };
86
87 /* classes of device behavior */
88 #define MT_CLS_DEFAULT                          0x0001
89
90 #define MT_CLS_SERIAL                           0x0002
91 #define MT_CLS_CONFIDENCE                       0x0003
92 #define MT_CLS_CONFIDENCE_CONTACT_ID            0x0004
93 #define MT_CLS_CONFIDENCE_MINUS_ONE             0x0005
94 #define MT_CLS_DUAL_INRANGE_CONTACTID           0x0006
95 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER       0x0007
96 #define MT_CLS_DUAL_NSMU_CONTACTID              0x0008
97 #define MT_CLS_INRANGE_CONTACTNUMBER            0x0009
98
99 /* vendor specific classes */
100 #define MT_CLS_3M                               0x0101
101 #define MT_CLS_CYPRESS                          0x0102
102 #define MT_CLS_EGALAX                           0x0103
103 #define MT_CLS_EGALAX_SERIAL                    0x0104
104
105 #define MT_DEFAULT_MAXCONTACT   10
106
107 /*
108  * these device-dependent functions determine what slot corresponds
109  * to a valid contact that was just read.
110  */
111
112 static int cypress_compute_slot(struct mt_device *td)
113 {
114         if (td->curdata.contactid != 0 || td->num_received == 0)
115                 return td->curdata.contactid;
116         else
117                 return -1;
118 }
119
120 static int find_slot_from_contactid(struct mt_device *td)
121 {
122         int i;
123         for (i = 0; i < td->maxcontacts; ++i) {
124                 if (td->slots[i].contactid == td->curdata.contactid &&
125                         td->slots[i].touch_state)
126                         return i;
127         }
128         for (i = 0; i < td->maxcontacts; ++i) {
129                 if (!td->slots[i].seen_in_this_frame &&
130                         !td->slots[i].touch_state)
131                         return i;
132         }
133         /* should not occurs. If this happens that means
134          * that the device sent more touches that it says
135          * in the report descriptor. It is ignored then. */
136         return -1;
137 }
138
139 static struct mt_class mt_classes[] = {
140         { .name = MT_CLS_DEFAULT,
141                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
142         { .name = MT_CLS_SERIAL,
143                 .quirks = MT_QUIRK_ALWAYS_VALID},
144         { .name = MT_CLS_CONFIDENCE,
145                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
146         { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
147                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
148                         MT_QUIRK_SLOT_IS_CONTACTID },
149         { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
150                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
151                         MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
152         { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
153                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
154                         MT_QUIRK_SLOT_IS_CONTACTID,
155                 .maxcontacts = 2 },
156         { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
157                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
158                         MT_QUIRK_SLOT_IS_CONTACTNUMBER,
159                 .maxcontacts = 2 },
160         { .name = MT_CLS_DUAL_NSMU_CONTACTID,
161                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
162                         MT_QUIRK_SLOT_IS_CONTACTID,
163                 .maxcontacts = 2 },
164         { .name = MT_CLS_INRANGE_CONTACTNUMBER,
165                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
166                         MT_QUIRK_SLOT_IS_CONTACTNUMBER },
167
168         /*
169          * vendor specific classes
170          */
171         { .name = MT_CLS_3M,
172                 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
173                         MT_QUIRK_SLOT_IS_CONTACTID,
174                 .sn_move = 2048,
175                 .sn_width = 128,
176                 .sn_height = 128 },
177         { .name = MT_CLS_CYPRESS,
178                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
179                         MT_QUIRK_CYPRESS,
180                 .maxcontacts = 10 },
181         { .name = MT_CLS_EGALAX,
182                 .quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
183                         MT_QUIRK_VALID_IS_INRANGE,
184                 .sn_move = 4096,
185                 .sn_pressure = 32,
186         },
187         { .name = MT_CLS_EGALAX_SERIAL,
188                 .quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
189                         MT_QUIRK_ALWAYS_VALID,
190                 .sn_move = 4096,
191                 .sn_pressure = 32,
192         },
193
194         { }
195 };
196
197 static ssize_t mt_show_quirks(struct device *dev,
198                            struct device_attribute *attr,
199                            char *buf)
200 {
201         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
202         struct mt_device *td = hid_get_drvdata(hdev);
203
204         return sprintf(buf, "%u\n", td->mtclass.quirks);
205 }
206
207 static ssize_t mt_set_quirks(struct device *dev,
208                           struct device_attribute *attr,
209                           const char *buf, size_t count)
210 {
211         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
212         struct mt_device *td = hid_get_drvdata(hdev);
213
214         unsigned long val;
215
216         if (kstrtoul(buf, 0, &val))
217                 return -EINVAL;
218
219         td->mtclass.quirks = val;
220
221         return count;
222 }
223
224 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
225
226 static struct attribute *sysfs_attrs[] = {
227         &dev_attr_quirks.attr,
228         NULL
229 };
230
231 static struct attribute_group mt_attribute_group = {
232         .attrs = sysfs_attrs
233 };
234
235 static void mt_feature_mapping(struct hid_device *hdev,
236                 struct hid_field *field, struct hid_usage *usage)
237 {
238         struct mt_device *td = hid_get_drvdata(hdev);
239
240         switch (usage->hid) {
241         case HID_DG_INPUTMODE:
242                 td->inputmode = field->report->id;
243                 break;
244         case HID_DG_CONTACTMAX:
245                 td->maxcontacts = field->value[0];
246                 if (td->mtclass.maxcontacts)
247                         /* check if the maxcontacts is given by the class */
248                         td->maxcontacts = td->mtclass.maxcontacts;
249
250                 break;
251         }
252 }
253
254 static void set_abs(struct input_dev *input, unsigned int code,
255                 struct hid_field *field, int snratio)
256 {
257         int fmin = field->logical_minimum;
258         int fmax = field->logical_maximum;
259         int fuzz = snratio ? (fmax - fmin) / snratio : 0;
260         input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
261 }
262
263 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
264                 struct hid_field *field, struct hid_usage *usage,
265                 unsigned long **bit, int *max)
266 {
267         struct mt_device *td = hid_get_drvdata(hdev);
268         struct mt_class *cls = &td->mtclass;
269         int code;
270
271         /* Only map fields from TouchScreen or TouchPad collections.
272          * We need to ignore fields that belong to other collections
273          * such as Mouse that might have the same GenericDesktop usages. */
274         if (field->application == HID_DG_TOUCHSCREEN)
275                 set_bit(INPUT_PROP_DIRECT, hi->input->propbit);
276         else if (field->application != HID_DG_TOUCHPAD)
277                 return 0;
278
279         /* In case of an indirect device (touchpad), we need to add
280          * specific BTN_TOOL_* to be handled by the synaptics xorg
281          * driver.
282          * We also consider that touchscreens providing buttons are touchpads.
283          */
284         if (field->application == HID_DG_TOUCHPAD ||
285             (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON ||
286             cls->is_indirect) {
287                 set_bit(INPUT_PROP_POINTER, hi->input->propbit);
288                 set_bit(BTN_TOOL_FINGER, hi->input->keybit);
289                 set_bit(BTN_TOOL_DOUBLETAP, hi->input->keybit);
290                 set_bit(BTN_TOOL_TRIPLETAP, hi->input->keybit);
291                 set_bit(BTN_TOOL_QUADTAP, hi->input->keybit);
292         }
293
294         /* eGalax devices provide a Digitizer.Stylus input which overrides
295          * the correct Digitizers.Finger X/Y ranges.
296          * Let's just ignore this input. */
297         if (field->physical == HID_DG_STYLUS)
298                 return -1;
299
300         switch (usage->hid & HID_USAGE_PAGE) {
301
302         case HID_UP_GENDESK:
303                 switch (usage->hid) {
304                 case HID_GD_X:
305                         hid_map_usage(hi, usage, bit, max,
306                                         EV_ABS, ABS_MT_POSITION_X);
307                         set_abs(hi->input, ABS_MT_POSITION_X, field,
308                                 cls->sn_move);
309                         /* touchscreen emulation */
310                         set_abs(hi->input, ABS_X, field, cls->sn_move);
311                         if (td->last_mt_collection == usage->collection_index) {
312                                 td->last_slot_field = usage->hid;
313                                 td->last_field_index = field->index;
314                         }
315                         return 1;
316                 case HID_GD_Y:
317                         hid_map_usage(hi, usage, bit, max,
318                                         EV_ABS, ABS_MT_POSITION_Y);
319                         set_abs(hi->input, ABS_MT_POSITION_Y, field,
320                                 cls->sn_move);
321                         /* touchscreen emulation */
322                         set_abs(hi->input, ABS_Y, field, cls->sn_move);
323                         if (td->last_mt_collection == usage->collection_index) {
324                                 td->last_slot_field = usage->hid;
325                                 td->last_field_index = field->index;
326                         }
327                         return 1;
328                 }
329                 return 0;
330
331         case HID_UP_DIGITIZER:
332                 switch (usage->hid) {
333                 case HID_DG_INRANGE:
334                         if (td->last_mt_collection == usage->collection_index) {
335                                 td->last_slot_field = usage->hid;
336                                 td->last_field_index = field->index;
337                         }
338                         return 1;
339                 case HID_DG_CONFIDENCE:
340                         if (td->last_mt_collection == usage->collection_index) {
341                                 td->last_slot_field = usage->hid;
342                                 td->last_field_index = field->index;
343                         }
344                         return 1;
345                 case HID_DG_TIPSWITCH:
346                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
347                         input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
348                         if (td->last_mt_collection == usage->collection_index) {
349                                 td->last_slot_field = usage->hid;
350                                 td->last_field_index = field->index;
351                         }
352                         return 1;
353                 case HID_DG_CONTACTID:
354                         if (!td->maxcontacts)
355                                 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
356                         input_mt_init_slots(hi->input, td->maxcontacts);
357                         td->last_slot_field = usage->hid;
358                         td->last_field_index = field->index;
359                         td->last_mt_collection = usage->collection_index;
360                         return 1;
361                 case HID_DG_WIDTH:
362                         hid_map_usage(hi, usage, bit, max,
363                                         EV_ABS, ABS_MT_TOUCH_MAJOR);
364                         set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
365                                 cls->sn_width);
366                         if (td->last_mt_collection == usage->collection_index) {
367                                 td->last_slot_field = usage->hid;
368                                 td->last_field_index = field->index;
369                         }
370                         return 1;
371                 case HID_DG_HEIGHT:
372                         hid_map_usage(hi, usage, bit, max,
373                                         EV_ABS, ABS_MT_TOUCH_MINOR);
374                         set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
375                                 cls->sn_height);
376                         input_set_abs_params(hi->input,
377                                         ABS_MT_ORIENTATION, 0, 1, 0, 0);
378                         if (td->last_mt_collection == usage->collection_index) {
379                                 td->last_slot_field = usage->hid;
380                                 td->last_field_index = field->index;
381                         }
382                         return 1;
383                 case HID_DG_TIPPRESSURE:
384                         hid_map_usage(hi, usage, bit, max,
385                                         EV_ABS, ABS_MT_PRESSURE);
386                         set_abs(hi->input, ABS_MT_PRESSURE, field,
387                                 cls->sn_pressure);
388                         /* touchscreen emulation */
389                         set_abs(hi->input, ABS_PRESSURE, field,
390                                 cls->sn_pressure);
391                         if (td->last_mt_collection == usage->collection_index) {
392                                 td->last_slot_field = usage->hid;
393                                 td->last_field_index = field->index;
394                         }
395                         return 1;
396                 case HID_DG_CONTACTCOUNT:
397                         if (td->last_mt_collection == usage->collection_index)
398                                 td->last_field_index = field->index;
399                         return 1;
400                 case HID_DG_CONTACTMAX:
401                         /* we don't set td->last_slot_field as contactcount and
402                          * contact max are global to the report */
403                         if (td->last_mt_collection == usage->collection_index)
404                                 td->last_field_index = field->index;
405                         return -1;
406                 }
407                 case HID_DG_TOUCH:
408                         /* Legacy devices use TIPSWITCH and not TOUCH.
409                          * Let's just ignore this field. */
410                         return -1;
411                 /* let hid-input decide for the others */
412                 return 0;
413
414         case HID_UP_BUTTON:
415                 code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
416                 hid_map_usage(hi, usage, bit, max, EV_KEY, code);
417                 input_set_capability(hi->input, EV_KEY, code);
418                 return 1;
419
420         case 0xff000000:
421                 /* we do not want to map these: no input-oriented meaning */
422                 return -1;
423         }
424
425         return 0;
426 }
427
428 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
429                 struct hid_field *field, struct hid_usage *usage,
430                 unsigned long **bit, int *max)
431 {
432         if (usage->type == EV_KEY || usage->type == EV_ABS)
433                 set_bit(usage->type, hi->input->evbit);
434
435         return -1;
436 }
437
438 static int mt_compute_slot(struct mt_device *td)
439 {
440         __s32 quirks = td->mtclass.quirks;
441
442         if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
443                 return td->curdata.contactid;
444
445         if (quirks & MT_QUIRK_CYPRESS)
446                 return cypress_compute_slot(td);
447
448         if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
449                 return td->num_received;
450
451         if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
452                 return td->curdata.contactid - 1;
453
454         return find_slot_from_contactid(td);
455 }
456
457 /*
458  * this function is called when a whole contact has been processed,
459  * so that it can assign it to a slot and store the data there
460  */
461 static void mt_complete_slot(struct mt_device *td)
462 {
463         td->curdata.seen_in_this_frame = true;
464         if (td->curvalid) {
465                 int slotnum = mt_compute_slot(td);
466
467                 if (slotnum >= 0 && slotnum < td->maxcontacts)
468                         td->slots[slotnum] = td->curdata;
469         }
470         td->num_received++;
471 }
472
473
474 /*
475  * this function is called when a whole packet has been received and processed,
476  * so that it can decide what to send to the input layer.
477  */
478 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
479 {
480         int i;
481
482         for (i = 0; i < td->maxcontacts; ++i) {
483                 struct mt_slot *s = &(td->slots[i]);
484                 if ((td->mtclass.quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
485                         !s->seen_in_this_frame) {
486                         s->touch_state = false;
487                 }
488
489                 input_mt_slot(input, i);
490                 input_mt_report_slot_state(input, MT_TOOL_FINGER,
491                         s->touch_state);
492                 if (s->touch_state) {
493                         /* this finger is on the screen */
494                         int wide = (s->w > s->h);
495                         /* divided by two to match visual scale of touch */
496                         int major = max(s->w, s->h) >> 1;
497                         int minor = min(s->w, s->h) >> 1;
498
499                         input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
500                         input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
501                         input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
502                         input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
503                         input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
504                         input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
505                 }
506                 s->seen_in_this_frame = false;
507
508         }
509
510         input_mt_report_pointer_emulation(input, true);
511         input_sync(input);
512         td->num_received = 0;
513 }
514
515
516
517 static int mt_event(struct hid_device *hid, struct hid_field *field,
518                                 struct hid_usage *usage, __s32 value)
519 {
520         struct mt_device *td = hid_get_drvdata(hid);
521         __s32 quirks = td->mtclass.quirks;
522
523         if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
524                 switch (usage->hid) {
525                 case HID_DG_INRANGE:
526                         if (quirks & MT_QUIRK_ALWAYS_VALID)
527                                 td->curvalid = true;
528                         else if (quirks & MT_QUIRK_VALID_IS_INRANGE)
529                                 td->curvalid = value;
530                         break;
531                 case HID_DG_TIPSWITCH:
532                         if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
533                                 td->curvalid = value;
534                         td->curdata.touch_state = value;
535                         break;
536                 case HID_DG_CONFIDENCE:
537                         if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
538                                 td->curvalid = value;
539                         break;
540                 case HID_DG_CONTACTID:
541                         td->curdata.contactid = value;
542                         break;
543                 case HID_DG_TIPPRESSURE:
544                         td->curdata.p = value;
545                         break;
546                 case HID_GD_X:
547                         td->curdata.x = value;
548                         break;
549                 case HID_GD_Y:
550                         td->curdata.y = value;
551                         break;
552                 case HID_DG_WIDTH:
553                         td->curdata.w = value;
554                         break;
555                 case HID_DG_HEIGHT:
556                         td->curdata.h = value;
557                         break;
558                 case HID_DG_CONTACTCOUNT:
559                         /*
560                          * Includes multi-packet support where subsequent
561                          * packets are sent with zero contactcount.
562                          */
563                         if (value)
564                                 td->num_expected = value;
565                         break;
566                 case HID_DG_TOUCH:
567                         /* do nothing */
568                         break;
569
570                 default:
571                         /* fallback to the generic hidinput handling */
572                         return 0;
573                 }
574
575                 if (usage->hid == td->last_slot_field) {
576                         mt_complete_slot(td);
577                 }
578
579                 if (field->index == td->last_field_index
580                         && td->num_received >= td->num_expected)
581                         mt_emit_event(td, field->hidinput->input);
582
583         }
584
585         /* we have handled the hidinput part, now remains hiddev */
586         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
587                 hid->hiddev_hid_event(hid, field, usage, value);
588
589         return 1;
590 }
591
592 static void mt_set_input_mode(struct hid_device *hdev)
593 {
594         struct mt_device *td = hid_get_drvdata(hdev);
595         struct hid_report *r;
596         struct hid_report_enum *re;
597
598         if (td->inputmode < 0)
599                 return;
600
601         re = &(hdev->report_enum[HID_FEATURE_REPORT]);
602         r = re->report_id_hash[td->inputmode];
603         if (r) {
604                 r->field[0]->value[0] = 0x02;
605                 usbhid_submit_report(hdev, r, USB_DIR_OUT);
606         }
607 }
608
609 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
610 {
611         int ret, i;
612         struct mt_device *td;
613         struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
614
615         for (i = 0; mt_classes[i].name ; i++) {
616                 if (id->driver_data == mt_classes[i].name) {
617                         mtclass = &(mt_classes[i]);
618                         break;
619                 }
620         }
621
622         /* This allows the driver to correctly support devices
623          * that emit events over several HID messages.
624          */
625         hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
626
627         td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
628         if (!td) {
629                 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
630                 return -ENOMEM;
631         }
632         td->mtclass = *mtclass;
633         td->inputmode = -1;
634         td->last_mt_collection = -1;
635         hid_set_drvdata(hdev, td);
636
637         ret = hid_parse(hdev);
638         if (ret != 0)
639                 goto fail;
640
641         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
642         if (ret)
643                 goto fail;
644
645         td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
646                                 GFP_KERNEL);
647         if (!td->slots) {
648                 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
649                 hid_hw_stop(hdev);
650                 ret = -ENOMEM;
651                 goto fail;
652         }
653
654         ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
655
656         mt_set_input_mode(hdev);
657
658         return 0;
659
660 fail:
661         kfree(td);
662         return ret;
663 }
664
665 #ifdef CONFIG_PM
666 static int mt_reset_resume(struct hid_device *hdev)
667 {
668         mt_set_input_mode(hdev);
669         return 0;
670 }
671 #endif
672
673 static void mt_remove(struct hid_device *hdev)
674 {
675         struct mt_device *td = hid_get_drvdata(hdev);
676         sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
677         hid_hw_stop(hdev);
678         kfree(td->slots);
679         kfree(td);
680         hid_set_drvdata(hdev, NULL);
681 }
682
683 static const struct hid_device_id mt_devices[] = {
684
685         /* 3M panels */
686         { .driver_data = MT_CLS_3M,
687                 HID_USB_DEVICE(USB_VENDOR_ID_3M,
688                         USB_DEVICE_ID_3M1968) },
689         { .driver_data = MT_CLS_3M,
690                 HID_USB_DEVICE(USB_VENDOR_ID_3M,
691                         USB_DEVICE_ID_3M2256) },
692         { .driver_data = MT_CLS_3M,
693                 HID_USB_DEVICE(USB_VENDOR_ID_3M,
694                         USB_DEVICE_ID_3M3266) },
695
696         /* ActionStar panels */
697         { .driver_data = MT_CLS_DEFAULT,
698                 HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR,
699                         USB_DEVICE_ID_ACTIONSTAR_1011) },
700
701         /* Atmel panels */
702         { .driver_data = MT_CLS_SERIAL,
703                 HID_USB_DEVICE(USB_VENDOR_ID_ATMEL,
704                         USB_DEVICE_ID_ATMEL_MULTITOUCH) },
705
706         /* Cando panels */
707         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
708                 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
709                         USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
710         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
711                 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
712                         USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
713         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
714                 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
715                         USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
716         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
717                 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
718                         USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
719
720         /* Chunghwa Telecom touch panels */
721         {  .driver_data = MT_CLS_DEFAULT,
722                 HID_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
723                         USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
724
725         /* CVTouch panels */
726         { .driver_data = MT_CLS_DEFAULT,
727                 HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
728                         USB_DEVICE_ID_CVTOUCH_SCREEN) },
729
730         /* Cypress panel */
731         { .driver_data = MT_CLS_CYPRESS,
732                 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
733                         USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
734
735         /* eGalax devices (resistive) */
736         { .driver_data = MT_CLS_EGALAX,
737                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
738                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
739         { .driver_data = MT_CLS_EGALAX,
740                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
741                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
742
743         /* eGalax devices (capacitive) */
744         { .driver_data = MT_CLS_EGALAX,
745                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
746                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
747         { .driver_data = MT_CLS_EGALAX,
748                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
749                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
750         { .driver_data = MT_CLS_EGALAX,
751                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
752                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
753         { .driver_data = MT_CLS_EGALAX,
754                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
755                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
756         { .driver_data = MT_CLS_EGALAX,
757                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
758                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
759         { .driver_data = MT_CLS_EGALAX_SERIAL,
760                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
761                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
762
763         /* Elo TouchSystems IntelliTouch Plus panel */
764         { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
765                 HID_USB_DEVICE(USB_VENDOR_ID_ELO,
766                         USB_DEVICE_ID_ELO_TS2515) },
767
768         /* GeneralTouch panel */
769         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
770                 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
771                         USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
772
773         /* GoodTouch panels */
774         { .driver_data = MT_CLS_DEFAULT,
775                 HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
776                         USB_DEVICE_ID_GOODTOUCH_000f) },
777
778         /* Hanvon panels */
779         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
780                 HID_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
781                         USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
782
783         /* Ideacom panel */
784         { .driver_data = MT_CLS_SERIAL,
785                 HID_USB_DEVICE(USB_VENDOR_ID_IDEACOM,
786                         USB_DEVICE_ID_IDEACOM_IDC6650) },
787
788         /* Ilitek dual touch panel */
789         {  .driver_data = MT_CLS_DEFAULT,
790                 HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
791                         USB_DEVICE_ID_ILITEK_MULTITOUCH) },
792
793         /* IRTOUCH panels */
794         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
795                 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
796                         USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
797
798         /* LG Display panels */
799         { .driver_data = MT_CLS_DEFAULT,
800                 HID_USB_DEVICE(USB_VENDOR_ID_LG,
801                         USB_DEVICE_ID_LG_MULTITOUCH) },
802
803         /* Lumio panels */
804         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
805                 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
806                         USB_DEVICE_ID_CRYSTALTOUCH) },
807         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
808                 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
809                         USB_DEVICE_ID_CRYSTALTOUCH_DUAL) },
810
811         /* MosArt panels */
812         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
813                 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
814                         USB_DEVICE_ID_ASUS_T91MT)},
815         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
816                 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
817                         USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
818         { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
819                 HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
820                         USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
821
822         /* PenMount panels */
823         { .driver_data = MT_CLS_CONFIDENCE,
824                 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
825                         USB_DEVICE_ID_PENMOUNT_PCI) },
826
827         /* PixArt optical touch screen */
828         { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
829                 HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
830                         USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
831         { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
832                 HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
833                         USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
834         { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
835                 HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
836                         USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
837
838         /* PixCir-based panels */
839         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
840                 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
841                         USB_DEVICE_ID_HANVON_MULTITOUCH) },
842         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
843                 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
844                         USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
845
846         /* Quanta-based panels */
847         { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
848                 HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
849                         USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
850         { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
851                 HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
852                         USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
853         { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
854                 HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
855                         USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3008) },
856
857         /* Stantum panels */
858         { .driver_data = MT_CLS_CONFIDENCE,
859                 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
860                         USB_DEVICE_ID_MTP)},
861         { .driver_data = MT_CLS_CONFIDENCE,
862                 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
863                         USB_DEVICE_ID_MTP_STM)},
864         { .driver_data = MT_CLS_CONFIDENCE,
865                 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX,
866                         USB_DEVICE_ID_MTP_SITRONIX)},
867
868         /* Touch International panels */
869         { .driver_data = MT_CLS_DEFAULT,
870                 HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
871                         USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
872
873         /* Unitec panels */
874         { .driver_data = MT_CLS_DEFAULT,
875                 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
876                         USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
877         { .driver_data = MT_CLS_DEFAULT,
878                 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
879                         USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
880         /* XAT */
881         { .driver_data = MT_CLS_DEFAULT,
882                 HID_USB_DEVICE(USB_VENDOR_ID_XAT,
883                         USB_DEVICE_ID_XAT_CSR) },
884
885         /* Xiroku */
886         { .driver_data = MT_CLS_DEFAULT,
887                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
888                         USB_DEVICE_ID_XIROKU_SPX) },
889         { .driver_data = MT_CLS_DEFAULT,
890                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
891                         USB_DEVICE_ID_XIROKU_MPX) },
892         { .driver_data = MT_CLS_DEFAULT,
893                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
894                         USB_DEVICE_ID_XIROKU_CSR) },
895         { .driver_data = MT_CLS_DEFAULT,
896                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
897                         USB_DEVICE_ID_XIROKU_SPX1) },
898         { .driver_data = MT_CLS_DEFAULT,
899                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
900                         USB_DEVICE_ID_XIROKU_MPX1) },
901         { .driver_data = MT_CLS_DEFAULT,
902                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
903                         USB_DEVICE_ID_XIROKU_CSR1) },
904         { .driver_data = MT_CLS_DEFAULT,
905                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
906                         USB_DEVICE_ID_XIROKU_SPX2) },
907         { .driver_data = MT_CLS_DEFAULT,
908                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
909                         USB_DEVICE_ID_XIROKU_MPX2) },
910         { .driver_data = MT_CLS_DEFAULT,
911                 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
912                         USB_DEVICE_ID_XIROKU_CSR2) },
913
914         { }
915 };
916 MODULE_DEVICE_TABLE(hid, mt_devices);
917
918 static const struct hid_usage_id mt_grabbed_usages[] = {
919         { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
920         { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
921 };
922
923 static struct hid_driver mt_driver = {
924         .name = "hid-multitouch",
925         .id_table = mt_devices,
926         .probe = mt_probe,
927         .remove = mt_remove,
928         .input_mapping = mt_input_mapping,
929         .input_mapped = mt_input_mapped,
930         .feature_mapping = mt_feature_mapping,
931         .usage_table = mt_grabbed_usages,
932         .event = mt_event,
933 #ifdef CONFIG_PM
934         .reset_resume = mt_reset_resume,
935 #endif
936 };
937
938 static int __init mt_init(void)
939 {
940         return hid_register_driver(&mt_driver);
941 }
942
943 static void __exit mt_exit(void)
944 {
945         hid_unregister_driver(&mt_driver);
946 }
947
948 module_init(mt_init);
949 module_exit(mt_exit);