2 * HID driver for multitouch panels
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
8 * This code is partly based on hid-egalax.c:
10 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
11 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
12 * Copyright (c) 2010 Canonical, Ltd.
14 * This code is partly based on hid-3m-pct.c:
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.
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)
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"
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");
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_VALID_IS_INRANGE (1 << 4)
51 #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
52 #define MT_QUIRK_EGALAX_XYZ_FIXUP (1 << 6)
56 __s32 contactid; /* the device ContactID assigned to this slot */
57 bool touch_state; /* is the touch valid? */
58 bool seen_in_this_frame;/* has this slot been updated */
62 struct mt_slot curdata; /* placeholder of incoming data */
63 struct mt_class *mtclass; /* our mt device class */
64 unsigned last_field_index; /* last field index of the report */
65 unsigned last_slot_field; /* the last field of a slot */
66 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
67 __u8 num_received; /* how many contacts we received */
68 __u8 num_expected; /* expected last contact index */
70 bool curvalid; /* is the current contact valid? */
71 struct mt_slot *slots;
75 __s32 name; /* MT_CLS */
77 __s32 sn_move; /* Signal/noise ratio for move events */
78 __s32 sn_width; /* Signal/noise ratio for width events */
79 __s32 sn_height; /* Signal/noise ratio for height events */
80 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
84 /* classes of device behavior */
85 #define MT_CLS_DEFAULT 1
86 #define MT_CLS_DUAL_INRANGE_CONTACTID 2
87 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3
88 #define MT_CLS_CYPRESS 4
89 #define MT_CLS_EGALAX 5
90 #define MT_CLS_STANTUM 6
92 #define MT_CLS_CONFIDENCE 8
94 #define MT_DEFAULT_MAXCONTACT 10
97 * these device-dependent functions determine what slot corresponds
98 * to a valid contact that was just read.
101 static int cypress_compute_slot(struct mt_device *td)
103 if (td->curdata.contactid != 0 || td->num_received == 0)
104 return td->curdata.contactid;
109 static int find_slot_from_contactid(struct mt_device *td)
112 for (i = 0; i < td->maxcontacts; ++i) {
113 if (td->slots[i].contactid == td->curdata.contactid &&
114 td->slots[i].touch_state)
117 for (i = 0; i < td->maxcontacts; ++i) {
118 if (!td->slots[i].seen_in_this_frame &&
119 !td->slots[i].touch_state)
122 /* should not occurs. If this happens that means
123 * that the device sent more touches that it says
124 * in the report descriptor. It is ignored then. */
128 struct mt_class mt_classes[] = {
129 { .name = MT_CLS_DEFAULT,
130 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
131 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
132 .quirks = MT_QUIRK_VALID_IS_INRANGE |
133 MT_QUIRK_SLOT_IS_CONTACTID,
135 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
136 .quirks = MT_QUIRK_VALID_IS_INRANGE |
137 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
139 { .name = MT_CLS_CYPRESS,
140 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
144 { .name = MT_CLS_EGALAX,
145 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
146 MT_QUIRK_VALID_IS_INRANGE |
147 MT_QUIRK_EGALAX_XYZ_FIXUP,
152 { .name = MT_CLS_STANTUM,
153 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
155 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
156 MT_QUIRK_SLOT_IS_CONTACTID,
160 { .name = MT_CLS_CONFIDENCE,
161 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
166 static void mt_feature_mapping(struct hid_device *hdev,
167 struct hid_field *field, struct hid_usage *usage)
169 struct mt_device *td = hid_get_drvdata(hdev);
171 switch (usage->hid) {
172 case HID_DG_INPUTMODE:
173 td->inputmode = field->report->id;
175 case HID_DG_CONTACTMAX:
176 td->maxcontacts = field->value[0];
177 if (td->mtclass->maxcontacts)
178 /* check if the maxcontacts is given by the class */
179 td->maxcontacts = td->mtclass->maxcontacts;
185 static void set_abs(struct input_dev *input, unsigned int code,
186 struct hid_field *field, int snratio)
188 int fmin = field->logical_minimum;
189 int fmax = field->logical_maximum;
190 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
191 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
194 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
195 struct hid_field *field, struct hid_usage *usage,
196 unsigned long **bit, int *max)
198 struct mt_device *td = hid_get_drvdata(hdev);
199 struct mt_class *cls = td->mtclass;
200 __s32 quirks = cls->quirks;
202 switch (usage->hid & HID_USAGE_PAGE) {
205 switch (usage->hid) {
207 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
208 field->logical_maximum = 32760;
209 hid_map_usage(hi, usage, bit, max,
210 EV_ABS, ABS_MT_POSITION_X);
211 set_abs(hi->input, ABS_MT_POSITION_X, field,
213 /* touchscreen emulation */
214 set_abs(hi->input, ABS_X, field, cls->sn_move);
215 td->last_slot_field = usage->hid;
216 td->last_field_index = field->index;
219 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
220 field->logical_maximum = 32760;
221 hid_map_usage(hi, usage, bit, max,
222 EV_ABS, ABS_MT_POSITION_Y);
223 set_abs(hi->input, ABS_MT_POSITION_Y, field,
225 /* touchscreen emulation */
226 set_abs(hi->input, ABS_Y, field, cls->sn_move);
227 td->last_slot_field = usage->hid;
228 td->last_field_index = field->index;
233 case HID_UP_DIGITIZER:
234 switch (usage->hid) {
236 td->last_slot_field = usage->hid;
237 td->last_field_index = field->index;
239 case HID_DG_CONFIDENCE:
240 td->last_slot_field = usage->hid;
241 td->last_field_index = field->index;
243 case HID_DG_TIPSWITCH:
244 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
245 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
246 td->last_slot_field = usage->hid;
247 td->last_field_index = field->index;
249 case HID_DG_CONTACTID:
250 input_mt_init_slots(hi->input, td->maxcontacts);
251 td->last_slot_field = usage->hid;
252 td->last_field_index = field->index;
255 hid_map_usage(hi, usage, bit, max,
256 EV_ABS, ABS_MT_TOUCH_MAJOR);
257 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
259 td->last_slot_field = usage->hid;
260 td->last_field_index = field->index;
263 hid_map_usage(hi, usage, bit, max,
264 EV_ABS, ABS_MT_TOUCH_MINOR);
265 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
267 input_set_abs_params(hi->input,
268 ABS_MT_ORIENTATION, 0, 1, 0, 0);
269 td->last_slot_field = usage->hid;
270 td->last_field_index = field->index;
272 case HID_DG_TIPPRESSURE:
273 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
274 field->logical_minimum = 0;
275 hid_map_usage(hi, usage, bit, max,
276 EV_ABS, ABS_MT_PRESSURE);
277 set_abs(hi->input, ABS_MT_PRESSURE, field,
279 /* touchscreen emulation */
280 set_abs(hi->input, ABS_PRESSURE, field,
282 td->last_slot_field = usage->hid;
283 td->last_field_index = field->index;
285 case HID_DG_CONTACTCOUNT:
286 td->last_field_index = field->index;
288 case HID_DG_CONTACTMAX:
289 /* we don't set td->last_slot_field as contactcount and
290 * contact max are global to the report */
291 td->last_field_index = field->index;
294 /* let hid-input decide for the others */
298 /* we do not want to map these: no input-oriented meaning */
305 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
306 struct hid_field *field, struct hid_usage *usage,
307 unsigned long **bit, int *max)
309 if (usage->type == EV_KEY || usage->type == EV_ABS)
310 set_bit(usage->type, hi->input->evbit);
315 static int mt_compute_slot(struct mt_device *td)
317 __s32 quirks = td->mtclass->quirks;
319 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
320 return td->curdata.contactid;
322 if (quirks & MT_QUIRK_CYPRESS)
323 return cypress_compute_slot(td);
325 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
326 return td->num_received;
328 return find_slot_from_contactid(td);
332 * this function is called when a whole contact has been processed,
333 * so that it can assign it to a slot and store the data there
335 static void mt_complete_slot(struct mt_device *td)
337 td->curdata.seen_in_this_frame = true;
339 int slotnum = mt_compute_slot(td);
341 if (slotnum >= 0 && slotnum < td->maxcontacts)
342 td->slots[slotnum] = td->curdata;
349 * this function is called when a whole packet has been received and processed,
350 * so that it can decide what to send to the input layer.
352 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
356 for (i = 0; i < td->maxcontacts; ++i) {
357 struct mt_slot *s = &(td->slots[i]);
358 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
359 !s->seen_in_this_frame) {
360 s->touch_state = false;
363 input_mt_slot(input, i);
364 input_mt_report_slot_state(input, MT_TOOL_FINGER,
366 if (s->touch_state) {
367 /* this finger is on the screen */
368 int wide = (s->w > s->h);
369 /* divided by two to match visual scale of touch */
370 int major = max(s->w, s->h) >> 1;
371 int minor = min(s->w, s->h) >> 1;
373 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
374 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
375 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
376 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
377 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
378 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
380 s->seen_in_this_frame = false;
384 input_mt_report_pointer_emulation(input, true);
386 td->num_received = 0;
391 static int mt_event(struct hid_device *hid, struct hid_field *field,
392 struct hid_usage *usage, __s32 value)
394 struct mt_device *td = hid_get_drvdata(hid);
395 __s32 quirks = td->mtclass->quirks;
397 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
398 switch (usage->hid) {
400 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
401 td->curvalid = value;
403 case HID_DG_TIPSWITCH:
404 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
405 td->curvalid = value;
406 td->curdata.touch_state = value;
408 case HID_DG_CONFIDENCE:
409 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
410 td->curvalid = value;
412 case HID_DG_CONTACTID:
413 td->curdata.contactid = value;
415 case HID_DG_TIPPRESSURE:
416 td->curdata.p = value;
419 td->curdata.x = value;
422 td->curdata.y = value;
425 td->curdata.w = value;
428 td->curdata.h = value;
430 case HID_DG_CONTACTCOUNT:
432 * Includes multi-packet support where subsequent
433 * packets are sent with zero contactcount.
436 td->num_expected = value;
440 /* fallback to the generic hidinput handling */
444 if (usage->hid == td->last_slot_field) {
445 mt_complete_slot(td);
448 if (field->index == td->last_field_index
449 && td->num_received >= td->num_expected)
450 mt_emit_event(td, field->hidinput->input);
454 /* we have handled the hidinput part, now remains hiddev */
455 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
456 hid->hiddev_hid_event(hid, field, usage, value);
461 static void mt_set_input_mode(struct hid_device *hdev)
463 struct mt_device *td = hid_get_drvdata(hdev);
464 struct hid_report *r;
465 struct hid_report_enum *re;
467 if (td->inputmode < 0)
470 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
471 r = re->report_id_hash[td->inputmode];
473 r->field[0]->value[0] = 0x02;
474 usbhid_submit_report(hdev, r, USB_DIR_OUT);
478 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
481 struct mt_device *td;
482 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
484 for (i = 0; mt_classes[i].name ; i++) {
485 if (id->driver_data == mt_classes[i].name) {
486 mtclass = &(mt_classes[i]);
491 /* This allows the driver to correctly support devices
492 * that emit events over several HID messages.
494 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
496 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
498 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
501 td->mtclass = mtclass;
503 hid_set_drvdata(hdev, td);
505 ret = hid_parse(hdev);
509 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
513 if (!td->maxcontacts)
514 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
516 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
519 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
525 mt_set_input_mode(hdev);
535 static int mt_reset_resume(struct hid_device *hdev)
537 mt_set_input_mode(hdev);
542 static void mt_remove(struct hid_device *hdev)
544 struct mt_device *td = hid_get_drvdata(hdev);
548 hid_set_drvdata(hdev, NULL);
551 static const struct hid_device_id mt_devices[] = {
554 { .driver_data = MT_CLS_3M,
555 HID_USB_DEVICE(USB_VENDOR_ID_3M,
556 USB_DEVICE_ID_3M1968) },
557 { .driver_data = MT_CLS_3M,
558 HID_USB_DEVICE(USB_VENDOR_ID_3M,
559 USB_DEVICE_ID_3M2256) },
562 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
563 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
564 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
565 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
566 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
567 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
568 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
569 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
570 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
571 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
572 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
573 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
576 { .driver_data = MT_CLS_CYPRESS,
577 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
578 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
580 /* GeneralTouch panel */
581 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
582 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
583 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
586 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
587 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
588 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
590 /* PenMount panels */
591 { .driver_data = MT_CLS_CONFIDENCE,
592 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
593 USB_DEVICE_ID_PENMOUNT_PCI) },
595 /* PixCir-based panels */
596 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
597 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
598 USB_DEVICE_ID_HANVON_MULTITOUCH) },
599 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
600 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
601 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
603 /* Resistive eGalax devices */
604 { .driver_data = MT_CLS_EGALAX,
605 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
606 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
607 { .driver_data = MT_CLS_EGALAX,
608 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
609 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
611 /* Capacitive eGalax devices */
612 { .driver_data = MT_CLS_EGALAX,
613 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
614 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
615 { .driver_data = MT_CLS_EGALAX,
616 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
617 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
618 { .driver_data = MT_CLS_EGALAX,
619 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
620 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
623 { .driver_data = MT_CLS_STANTUM,
624 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
626 { .driver_data = MT_CLS_STANTUM,
627 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
628 USB_DEVICE_ID_MTP_STM)},
629 { .driver_data = MT_CLS_STANTUM,
630 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
631 USB_DEVICE_ID_MTP_SITRONIX)},
635 MODULE_DEVICE_TABLE(hid, mt_devices);
637 static const struct hid_usage_id mt_grabbed_usages[] = {
638 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
639 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
642 static struct hid_driver mt_driver = {
643 .name = "hid-multitouch",
644 .id_table = mt_devices,
647 .input_mapping = mt_input_mapping,
648 .input_mapped = mt_input_mapped,
649 .feature_mapping = mt_feature_mapping,
650 .usage_table = mt_grabbed_usages,
653 .reset_resume = mt_reset_resume,
657 static int __init mt_init(void)
659 return hid_register_driver(&mt_driver);
662 static void __exit mt_exit(void)
664 hid_unregister_driver(&mt_driver);
667 module_init(mt_init);
668 module_exit(mt_exit);