OSDN Git Service

hid-multitouch: Filter collections by application usage.
[android-x86/kernel.git] / drivers / hid / hid-multitouch.c
1 /*
2  *  HID driver for multitouch panels
3  *
4  *  Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr>
5  *  Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6  *  Copyright (c) 2010-2011 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  */
15
16 /*
17  * This program is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the Free
19  * Software Foundation; either version 2 of the License, or (at your option)
20  * any later version.
21  */
22
23 #include <linux/device.h>
24 #include <linux/hid.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/usb.h>
28 #include <linux/input/mt.h>
29 #include "usbhid/usbhid.h"
30
31
32 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
33 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
34 MODULE_DESCRIPTION("HID multitouch panels");
35 MODULE_LICENSE("GPL");
36
37 #include "hid-ids.h"
38
39 /* quirks to control the device */
40 #define MT_QUIRK_NOT_SEEN_MEANS_UP      (1 << 0)
41 #define MT_QUIRK_SLOT_IS_CONTACTID      (1 << 1)
42 #define MT_QUIRK_CYPRESS                (1 << 2)
43 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER  (1 << 3)
44 #define MT_QUIRK_VALID_IS_INRANGE       (1 << 4)
45 #define MT_QUIRK_VALID_IS_CONFIDENCE    (1 << 5)
46 #define MT_QUIRK_EGALAX_XYZ_FIXUP       (1 << 6)
47
48 struct mt_slot {
49         __s32 x, y, p, w, h;
50         __s32 contactid;        /* the device ContactID assigned to this slot */
51         bool touch_state;       /* is the touch valid? */
52         bool seen_in_this_frame;/* has this slot been updated */
53 };
54
55 struct mt_device {
56         struct mt_slot curdata; /* placeholder of incoming data */
57         struct mt_class *mtclass;       /* our mt device class */
58         unsigned last_field_index;      /* last field index of the report */
59         unsigned last_slot_field;       /* the last field of a slot */
60         __s8 inputmode;         /* InputMode HID feature, -1 if non-existent */
61         __u8 num_received;      /* how many contacts we received */
62         __u8 num_expected;      /* expected last contact index */
63         bool curvalid;          /* is the current contact valid? */
64         struct mt_slot slots[0];        /* first slot */
65 };
66
67 struct mt_class {
68         __s32 name;     /* MT_CLS */
69         __s32 quirks;
70         __s32 sn_move;  /* Signal/noise ratio for move events */
71         __s32 sn_pressure;      /* Signal/noise ratio for pressure events */
72         __u8 maxcontacts;
73 };
74
75 /* classes of device behavior */
76 #define MT_CLS_DEFAULT                          1
77 #define MT_CLS_DUAL_INRANGE_CONTACTID           2
78 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER       3
79 #define MT_CLS_CYPRESS                          4
80 #define MT_CLS_EGALAX                           5
81
82 /*
83  * these device-dependent functions determine what slot corresponds
84  * to a valid contact that was just read.
85  */
86
87 static int cypress_compute_slot(struct mt_device *td)
88 {
89         if (td->curdata.contactid != 0 || td->num_received == 0)
90                 return td->curdata.contactid;
91         else
92                 return -1;
93 }
94
95 static int find_slot_from_contactid(struct mt_device *td)
96 {
97         int i;
98         for (i = 0; i < td->mtclass->maxcontacts; ++i) {
99                 if (td->slots[i].contactid == td->curdata.contactid &&
100                         td->slots[i].touch_state)
101                         return i;
102         }
103         for (i = 0; i < td->mtclass->maxcontacts; ++i) {
104                 if (!td->slots[i].seen_in_this_frame &&
105                         !td->slots[i].touch_state)
106                         return i;
107         }
108         /* should not occurs. If this happens that means
109          * that the device sent more touches that it says
110          * in the report descriptor. It is ignored then. */
111         return -1;
112 }
113
114 struct mt_class mt_classes[] = {
115         { .name = MT_CLS_DEFAULT,
116                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
117                 .maxcontacts = 10 },
118         { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
119                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
120                         MT_QUIRK_SLOT_IS_CONTACTID,
121                 .maxcontacts = 2 },
122         { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
123                 .quirks = MT_QUIRK_VALID_IS_INRANGE |
124                         MT_QUIRK_SLOT_IS_CONTACTNUMBER,
125                 .maxcontacts = 2 },
126         { .name = MT_CLS_CYPRESS,
127                 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
128                         MT_QUIRK_CYPRESS,
129                 .maxcontacts = 10 },
130
131         { .name = MT_CLS_EGALAX,
132                 .quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
133                         MT_QUIRK_VALID_IS_INRANGE |
134                         MT_QUIRK_EGALAX_XYZ_FIXUP,
135                 .maxcontacts = 2,
136                 .sn_move = 4096,
137                 .sn_pressure = 32,
138         },
139         { }
140 };
141
142 static void mt_feature_mapping(struct hid_device *hdev,
143                 struct hid_field *field, struct hid_usage *usage)
144 {
145         if (usage->hid == HID_DG_INPUTMODE) {
146                 struct mt_device *td = hid_get_drvdata(hdev);
147                 td->inputmode = field->report->id;
148         }
149 }
150
151 static void set_abs(struct input_dev *input, unsigned int code,
152                 struct hid_field *field, int snratio)
153 {
154         int fmin = field->logical_minimum;
155         int fmax = field->logical_maximum;
156         int fuzz = snratio ? (fmax - fmin) / snratio : 0;
157         input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
158 }
159
160 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
161                 struct hid_field *field, struct hid_usage *usage,
162                 unsigned long **bit, int *max)
163 {
164         struct mt_device *td = hid_get_drvdata(hdev);
165         struct mt_class *cls = td->mtclass;
166         __s32 quirks = cls->quirks;
167
168         /* Only map fields from TouchScreen or TouchPad collections.
169          * We need to ignore fields that belong to other collections
170          * such as Mouse that might have the same GenericDesktop usages. */
171         if (field->application == HID_DG_TOUCHSCREEN)
172                 set_bit(INPUT_PROP_DIRECT, hi->input->propbit);
173         else if (field->application == HID_DG_TOUCHPAD)
174                 set_bit(INPUT_PROP_POINTER, hi->input->propbit);
175         else
176                 return 0;
177
178         switch (usage->hid & HID_USAGE_PAGE) {
179
180         case HID_UP_GENDESK:
181                 switch (usage->hid) {
182                 case HID_GD_X:
183                         if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
184                                 field->logical_maximum = 32760;
185                         hid_map_usage(hi, usage, bit, max,
186                                         EV_ABS, ABS_MT_POSITION_X);
187                         set_abs(hi->input, ABS_MT_POSITION_X, field,
188                                 cls->sn_move);
189                         /* touchscreen emulation */
190                         set_abs(hi->input, ABS_X, field, cls->sn_move);
191                         td->last_slot_field = usage->hid;
192                         return 1;
193                 case HID_GD_Y:
194                         if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
195                                 field->logical_maximum = 32760;
196                         hid_map_usage(hi, usage, bit, max,
197                                         EV_ABS, ABS_MT_POSITION_Y);
198                         set_abs(hi->input, ABS_MT_POSITION_Y, field,
199                                 cls->sn_move);
200                         /* touchscreen emulation */
201                         set_abs(hi->input, ABS_Y, field, cls->sn_move);
202                         td->last_slot_field = usage->hid;
203                         return 1;
204                 }
205                 return 0;
206
207         case HID_UP_DIGITIZER:
208                 switch (usage->hid) {
209                 case HID_DG_INRANGE:
210                         td->last_slot_field = usage->hid;
211                         return 1;
212                 case HID_DG_CONFIDENCE:
213                         td->last_slot_field = usage->hid;
214                         return 1;
215                 case HID_DG_TIPSWITCH:
216                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
217                         input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
218                         td->last_slot_field = usage->hid;
219                         return 1;
220                 case HID_DG_CONTACTID:
221                         input_mt_init_slots(hi->input,
222                                         td->mtclass->maxcontacts);
223                         td->last_slot_field = usage->hid;
224                         return 1;
225                 case HID_DG_WIDTH:
226                         hid_map_usage(hi, usage, bit, max,
227                                         EV_ABS, ABS_MT_TOUCH_MAJOR);
228                         td->last_slot_field = usage->hid;
229                         return 1;
230                 case HID_DG_HEIGHT:
231                         hid_map_usage(hi, usage, bit, max,
232                                         EV_ABS, ABS_MT_TOUCH_MINOR);
233                         field->logical_maximum = 1;
234                         field->logical_minimum = 0;
235                         set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
236                         td->last_slot_field = usage->hid;
237                         return 1;
238                 case HID_DG_TIPPRESSURE:
239                         if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
240                                 field->logical_minimum = 0;
241                         hid_map_usage(hi, usage, bit, max,
242                                         EV_ABS, ABS_MT_PRESSURE);
243                         set_abs(hi->input, ABS_MT_PRESSURE, field,
244                                 cls->sn_pressure);
245                         /* touchscreen emulation */
246                         set_abs(hi->input, ABS_PRESSURE, field,
247                                 cls->sn_pressure);
248                         td->last_slot_field = usage->hid;
249                         return 1;
250                 case HID_DG_CONTACTCOUNT:
251                         td->last_field_index = field->report->maxfield - 1;
252                         return 1;
253                 case HID_DG_CONTACTMAX:
254                         /* we don't set td->last_slot_field as contactcount and
255                          * contact max are global to the report */
256                         return -1;
257                 }
258                 /* let hid-input decide for the others */
259                 return 0;
260
261         case 0xff000000:
262                 /* we do not want to map these: no input-oriented meaning */
263                 return -1;
264         }
265
266         return 0;
267 }
268
269 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
270                 struct hid_field *field, struct hid_usage *usage,
271                 unsigned long **bit, int *max)
272 {
273         if (usage->type == EV_KEY || usage->type == EV_ABS)
274                 set_bit(usage->type, hi->input->evbit);
275
276         return -1;
277 }
278
279 static int mt_compute_slot(struct mt_device *td)
280 {
281         __s32 quirks = td->mtclass->quirks;
282
283         if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
284                 return td->curdata.contactid;
285
286         if (quirks & MT_QUIRK_CYPRESS)
287                 return cypress_compute_slot(td);
288
289         if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
290                 return td->num_received;
291
292         return find_slot_from_contactid(td);
293 }
294
295 /*
296  * this function is called when a whole contact has been processed,
297  * so that it can assign it to a slot and store the data there
298  */
299 static void mt_complete_slot(struct mt_device *td)
300 {
301         td->curdata.seen_in_this_frame = true;
302         if (td->curvalid) {
303                 int slotnum = mt_compute_slot(td);
304
305                 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
306                         td->slots[slotnum] = td->curdata;
307         }
308         td->num_received++;
309 }
310
311
312 /*
313  * this function is called when a whole packet has been received and processed,
314  * so that it can decide what to send to the input layer.
315  */
316 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
317 {
318         int i;
319
320         for (i = 0; i < td->mtclass->maxcontacts; ++i) {
321                 struct mt_slot *s = &(td->slots[i]);
322                 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
323                         !s->seen_in_this_frame) {
324                         s->touch_state = false;
325                 }
326
327                 input_mt_slot(input, i);
328                 input_mt_report_slot_state(input, MT_TOOL_FINGER,
329                         s->touch_state);
330                 if (s->touch_state) {
331                         input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
332                         input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
333                         input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
334                         input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
335                         input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
336                 }
337                 s->seen_in_this_frame = false;
338
339         }
340
341         input_mt_report_pointer_emulation(input, true);
342         input_sync(input);
343         td->num_received = 0;
344 }
345
346
347
348 static int mt_event(struct hid_device *hid, struct hid_field *field,
349                                 struct hid_usage *usage, __s32 value)
350 {
351         struct mt_device *td = hid_get_drvdata(hid);
352         __s32 quirks = td->mtclass->quirks;
353
354         if (hid->claimed & HID_CLAIMED_INPUT) {
355                 switch (usage->hid) {
356                 case HID_DG_INRANGE:
357                         if (quirks & MT_QUIRK_VALID_IS_INRANGE)
358                                 td->curvalid = value;
359                         break;
360                 case HID_DG_TIPSWITCH:
361                         if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
362                                 td->curvalid = value;
363                         td->curdata.touch_state = value;
364                         break;
365                 case HID_DG_CONFIDENCE:
366                         if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
367                                 td->curvalid = value;
368                         break;
369                 case HID_DG_CONTACTID:
370                         td->curdata.contactid = value;
371                         break;
372                 case HID_DG_TIPPRESSURE:
373                         td->curdata.p = value;
374                         break;
375                 case HID_GD_X:
376                         td->curdata.x = value;
377                         break;
378                 case HID_GD_Y:
379                         td->curdata.y = value;
380                         break;
381                 case HID_DG_WIDTH:
382                         td->curdata.w = value;
383                         break;
384                 case HID_DG_HEIGHT:
385                         td->curdata.h = value;
386                         break;
387                 case HID_DG_CONTACTCOUNT:
388                         /*
389                          * Includes multi-packet support where subsequent
390                          * packets are sent with zero contactcount.
391                          */
392                         if (value)
393                                 td->num_expected = value;
394                         break;
395
396                 default:
397                         /* fallback to the generic hidinput handling */
398                         return 0;
399                 }
400
401                 if (usage->hid == td->last_slot_field) {
402                         mt_complete_slot(td);
403                         if (!td->last_field_index)
404                                 mt_emit_event(td, field->hidinput->input);
405                 }
406
407                 if (field->index == td->last_field_index
408                         && td->num_received >= td->num_expected)
409                         mt_emit_event(td, field->hidinput->input);
410
411         }
412
413         /* we have handled the hidinput part, now remains hiddev */
414         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
415                 hid->hiddev_hid_event(hid, field, usage, value);
416
417         return 1;
418 }
419
420 static void mt_set_input_mode(struct hid_device *hdev)
421 {
422         struct mt_device *td = hid_get_drvdata(hdev);
423         struct hid_report *r;
424         struct hid_report_enum *re;
425
426         if (td->inputmode < 0)
427                 return;
428
429         re = &(hdev->report_enum[HID_FEATURE_REPORT]);
430         r = re->report_id_hash[td->inputmode];
431         if (r) {
432                 r->field[0]->value[0] = 0x02;
433                 usbhid_submit_report(hdev, r, USB_DIR_OUT);
434         }
435 }
436
437 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
438 {
439         int ret, i;
440         struct mt_device *td;
441         struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
442
443         for (i = 0; mt_classes[i].name ; i++) {
444                 if (id->driver_data == mt_classes[i].name) {
445                         mtclass = &(mt_classes[i]);
446                         break;
447                 }
448         }
449
450         /* This allows the driver to correctly support devices
451          * that emit events over several HID messages.
452          */
453         hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
454
455         td = kzalloc(sizeof(struct mt_device) +
456                                 mtclass->maxcontacts * sizeof(struct mt_slot),
457                                 GFP_KERNEL);
458         if (!td) {
459                 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
460                 return -ENOMEM;
461         }
462         td->mtclass = mtclass;
463         td->inputmode = -1;
464         hid_set_drvdata(hdev, td);
465
466         ret = hid_parse(hdev);
467         if (ret != 0)
468                 goto fail;
469
470         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
471         if (ret)
472                 goto fail;
473
474         mt_set_input_mode(hdev);
475
476         return 0;
477
478 fail:
479         kfree(td);
480         return ret;
481 }
482
483 #ifdef CONFIG_PM
484 static int mt_reset_resume(struct hid_device *hdev)
485 {
486         mt_set_input_mode(hdev);
487         return 0;
488 }
489 #endif
490
491 static void mt_remove(struct hid_device *hdev)
492 {
493         struct mt_device *td = hid_get_drvdata(hdev);
494         hid_hw_stop(hdev);
495         kfree(td);
496         hid_set_drvdata(hdev, NULL);
497 }
498
499 static const struct hid_device_id mt_devices[] = {
500
501         /* Cypress panel */
502         { .driver_data = MT_CLS_CYPRESS,
503                 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
504                         USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
505
506         /* GeneralTouch panel */
507         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
508                 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
509                         USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
510
511         /* IRTOUCH panels */
512         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
513                 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
514                         USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
515
516         /* PixCir-based panels */
517         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
518                 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
519                         USB_DEVICE_ID_HANVON_MULTITOUCH) },
520         { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
521                 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
522                         USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
523
524         /* Resistive eGalax devices */
525         {  .driver_data = MT_CLS_EGALAX,
526                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
527                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
528         {  .driver_data = MT_CLS_EGALAX,
529                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
530                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
531
532         /* Capacitive eGalax devices */
533         {  .driver_data = MT_CLS_EGALAX,
534                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
535                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
536         {  .driver_data = MT_CLS_EGALAX,
537                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
538                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
539         {  .driver_data = MT_CLS_EGALAX,
540                 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
541                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
542
543         { }
544 };
545 MODULE_DEVICE_TABLE(hid, mt_devices);
546
547 static const struct hid_usage_id mt_grabbed_usages[] = {
548         { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
549         { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
550 };
551
552 static struct hid_driver mt_driver = {
553         .name = "hid-multitouch",
554         .id_table = mt_devices,
555         .probe = mt_probe,
556         .remove = mt_remove,
557         .input_mapping = mt_input_mapping,
558         .input_mapped = mt_input_mapped,
559         .feature_mapping = mt_feature_mapping,
560         .usage_table = mt_grabbed_usages,
561         .event = mt_event,
562 #ifdef CONFIG_PM
563         .reset_resume = mt_reset_resume,
564 #endif
565 };
566
567 static int __init mt_init(void)
568 {
569         return hid_register_driver(&mt_driver);
570 }
571
572 static void __exit mt_exit(void)
573 {
574         hid_unregister_driver(&mt_driver);
575 }
576
577 module_init(mt_init);
578 module_exit(mt_exit);