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.
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)
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"
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");
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)
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 */
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 */
64 bool curvalid; /* is the current contact valid? */
65 struct mt_slot *slots;
69 __s32 name; /* MT_CLS */
71 __s32 sn_move; /* Signal/noise ratio for move events */
72 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
76 /* classes of device behavior */
77 #define MT_CLS_DEFAULT 1
78 #define MT_CLS_DUAL_INRANGE_CONTACTID 2
79 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3
80 #define MT_CLS_CYPRESS 4
81 #define MT_CLS_EGALAX 5
82 #define MT_CLS_STANTUM 6
84 #define MT_DEFAULT_MAXCONTACT 10
87 * these device-dependent functions determine what slot corresponds
88 * to a valid contact that was just read.
91 static int cypress_compute_slot(struct mt_device *td)
93 if (td->curdata.contactid != 0 || td->num_received == 0)
94 return td->curdata.contactid;
99 static int find_slot_from_contactid(struct mt_device *td)
102 for (i = 0; i < td->maxcontacts; ++i) {
103 if (td->slots[i].contactid == td->curdata.contactid &&
104 td->slots[i].touch_state)
107 for (i = 0; i < td->maxcontacts; ++i) {
108 if (!td->slots[i].seen_in_this_frame &&
109 !td->slots[i].touch_state)
112 /* should not occurs. If this happens that means
113 * that the device sent more touches that it says
114 * in the report descriptor. It is ignored then. */
118 struct mt_class mt_classes[] = {
119 { .name = MT_CLS_DEFAULT,
120 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
121 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
122 .quirks = MT_QUIRK_VALID_IS_INRANGE |
123 MT_QUIRK_SLOT_IS_CONTACTID,
125 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
126 .quirks = MT_QUIRK_VALID_IS_INRANGE |
127 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
129 { .name = MT_CLS_CYPRESS,
130 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
134 { .name = MT_CLS_EGALAX,
135 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
136 MT_QUIRK_VALID_IS_INRANGE |
137 MT_QUIRK_EGALAX_XYZ_FIXUP,
142 { .name = MT_CLS_STANTUM,
143 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
148 static void mt_feature_mapping(struct hid_device *hdev,
149 struct hid_field *field, struct hid_usage *usage)
151 struct mt_device *td = hid_get_drvdata(hdev);
153 switch (usage->hid) {
154 case HID_DG_INPUTMODE:
155 td->inputmode = field->report->id;
157 case HID_DG_CONTACTMAX:
158 td->maxcontacts = field->value[0];
159 if (td->mtclass->maxcontacts)
160 /* check if the maxcontacts is given by the class */
161 td->maxcontacts = td->mtclass->maxcontacts;
167 static void set_abs(struct input_dev *input, unsigned int code,
168 struct hid_field *field, int snratio)
170 int fmin = field->logical_minimum;
171 int fmax = field->logical_maximum;
172 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
173 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
176 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
177 struct hid_field *field, struct hid_usage *usage,
178 unsigned long **bit, int *max)
180 struct mt_device *td = hid_get_drvdata(hdev);
181 struct mt_class *cls = td->mtclass;
182 __s32 quirks = cls->quirks;
184 switch (usage->hid & HID_USAGE_PAGE) {
187 switch (usage->hid) {
189 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
190 field->logical_maximum = 32760;
191 hid_map_usage(hi, usage, bit, max,
192 EV_ABS, ABS_MT_POSITION_X);
193 set_abs(hi->input, ABS_MT_POSITION_X, field,
195 /* touchscreen emulation */
196 set_abs(hi->input, ABS_X, field, cls->sn_move);
197 td->last_slot_field = usage->hid;
200 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
201 field->logical_maximum = 32760;
202 hid_map_usage(hi, usage, bit, max,
203 EV_ABS, ABS_MT_POSITION_Y);
204 set_abs(hi->input, ABS_MT_POSITION_Y, field,
206 /* touchscreen emulation */
207 set_abs(hi->input, ABS_Y, field, cls->sn_move);
208 td->last_slot_field = usage->hid;
213 case HID_UP_DIGITIZER:
214 switch (usage->hid) {
216 td->last_slot_field = usage->hid;
218 case HID_DG_CONFIDENCE:
219 td->last_slot_field = usage->hid;
221 case HID_DG_TIPSWITCH:
222 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
223 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
224 td->last_slot_field = usage->hid;
226 case HID_DG_CONTACTID:
227 input_mt_init_slots(hi->input, td->maxcontacts);
228 td->last_slot_field = usage->hid;
231 hid_map_usage(hi, usage, bit, max,
232 EV_ABS, ABS_MT_TOUCH_MAJOR);
233 td->last_slot_field = usage->hid;
236 hid_map_usage(hi, usage, bit, max,
237 EV_ABS, ABS_MT_TOUCH_MINOR);
238 field->logical_maximum = 1;
239 field->logical_minimum = 0;
240 set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
241 td->last_slot_field = usage->hid;
243 case HID_DG_TIPPRESSURE:
244 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
245 field->logical_minimum = 0;
246 hid_map_usage(hi, usage, bit, max,
247 EV_ABS, ABS_MT_PRESSURE);
248 set_abs(hi->input, ABS_MT_PRESSURE, field,
250 /* touchscreen emulation */
251 set_abs(hi->input, ABS_PRESSURE, field,
253 td->last_slot_field = usage->hid;
255 case HID_DG_CONTACTCOUNT:
256 td->last_field_index = field->report->maxfield - 1;
258 case HID_DG_CONTACTMAX:
259 /* we don't set td->last_slot_field as contactcount and
260 * contact max are global to the report */
263 /* let hid-input decide for the others */
267 /* we do not want to map these: no input-oriented meaning */
274 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
275 struct hid_field *field, struct hid_usage *usage,
276 unsigned long **bit, int *max)
278 if (usage->type == EV_KEY || usage->type == EV_ABS)
279 set_bit(usage->type, hi->input->evbit);
284 static int mt_compute_slot(struct mt_device *td)
286 __s32 quirks = td->mtclass->quirks;
288 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
289 return td->curdata.contactid;
291 if (quirks & MT_QUIRK_CYPRESS)
292 return cypress_compute_slot(td);
294 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
295 return td->num_received;
297 return find_slot_from_contactid(td);
301 * this function is called when a whole contact has been processed,
302 * so that it can assign it to a slot and store the data there
304 static void mt_complete_slot(struct mt_device *td)
306 td->curdata.seen_in_this_frame = true;
308 int slotnum = mt_compute_slot(td);
310 if (slotnum >= 0 && slotnum < td->maxcontacts)
311 td->slots[slotnum] = td->curdata;
318 * this function is called when a whole packet has been received and processed,
319 * so that it can decide what to send to the input layer.
321 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
325 for (i = 0; i < td->maxcontacts; ++i) {
326 struct mt_slot *s = &(td->slots[i]);
327 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
328 !s->seen_in_this_frame) {
329 s->touch_state = false;
332 input_mt_slot(input, i);
333 input_mt_report_slot_state(input, MT_TOOL_FINGER,
335 if (s->touch_state) {
336 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
337 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
338 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
339 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
340 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
342 s->seen_in_this_frame = false;
346 input_mt_report_pointer_emulation(input, true);
348 td->num_received = 0;
353 static int mt_event(struct hid_device *hid, struct hid_field *field,
354 struct hid_usage *usage, __s32 value)
356 struct mt_device *td = hid_get_drvdata(hid);
357 __s32 quirks = td->mtclass->quirks;
359 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
360 switch (usage->hid) {
362 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
363 td->curvalid = value;
365 case HID_DG_TIPSWITCH:
366 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
367 td->curvalid = value;
368 td->curdata.touch_state = value;
370 case HID_DG_CONFIDENCE:
371 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
372 td->curvalid = value;
374 case HID_DG_CONTACTID:
375 td->curdata.contactid = value;
377 case HID_DG_TIPPRESSURE:
378 td->curdata.p = value;
381 td->curdata.x = value;
384 td->curdata.y = value;
387 td->curdata.w = value;
390 td->curdata.h = value;
392 case HID_DG_CONTACTCOUNT:
394 * Includes multi-packet support where subsequent
395 * packets are sent with zero contactcount.
398 td->num_expected = value;
402 /* fallback to the generic hidinput handling */
406 if (usage->hid == td->last_slot_field) {
407 mt_complete_slot(td);
408 if (!td->last_field_index)
409 mt_emit_event(td, field->hidinput->input);
412 if (field->index == td->last_field_index
413 && td->num_received >= td->num_expected)
414 mt_emit_event(td, field->hidinput->input);
418 /* we have handled the hidinput part, now remains hiddev */
419 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
420 hid->hiddev_hid_event(hid, field, usage, value);
425 static void mt_set_input_mode(struct hid_device *hdev)
427 struct mt_device *td = hid_get_drvdata(hdev);
428 struct hid_report *r;
429 struct hid_report_enum *re;
431 if (td->inputmode < 0)
434 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
435 r = re->report_id_hash[td->inputmode];
437 r->field[0]->value[0] = 0x02;
438 usbhid_submit_report(hdev, r, USB_DIR_OUT);
442 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
445 struct mt_device *td;
446 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
448 for (i = 0; mt_classes[i].name ; i++) {
449 if (id->driver_data == mt_classes[i].name) {
450 mtclass = &(mt_classes[i]);
455 /* This allows the driver to correctly support devices
456 * that emit events over several HID messages.
458 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
460 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
462 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
465 td->mtclass = mtclass;
467 hid_set_drvdata(hdev, td);
469 ret = hid_parse(hdev);
473 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
477 if (!td->maxcontacts)
478 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
480 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
483 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
489 mt_set_input_mode(hdev);
499 static int mt_reset_resume(struct hid_device *hdev)
501 mt_set_input_mode(hdev);
506 static void mt_remove(struct hid_device *hdev)
508 struct mt_device *td = hid_get_drvdata(hdev);
512 hid_set_drvdata(hdev, NULL);
515 static const struct hid_device_id mt_devices[] = {
518 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
519 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
520 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
521 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
522 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
523 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
524 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
525 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
526 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
527 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
528 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
529 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
532 { .driver_data = MT_CLS_CYPRESS,
533 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
534 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
536 /* GeneralTouch panel */
537 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
538 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
539 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
542 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
543 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
544 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
546 /* PixCir-based panels */
547 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
548 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
549 USB_DEVICE_ID_HANVON_MULTITOUCH) },
550 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
551 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
552 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
554 /* Resistive eGalax devices */
555 { .driver_data = MT_CLS_EGALAX,
556 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
557 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
558 { .driver_data = MT_CLS_EGALAX,
559 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
560 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
562 /* Capacitive eGalax devices */
563 { .driver_data = MT_CLS_EGALAX,
564 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
565 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
566 { .driver_data = MT_CLS_EGALAX,
567 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
568 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
569 { .driver_data = MT_CLS_EGALAX,
570 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
571 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
574 { .driver_data = MT_CLS_STANTUM,
575 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
577 { .driver_data = MT_CLS_STANTUM,
578 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
579 USB_DEVICE_ID_MTP_STM)},
580 { .driver_data = MT_CLS_STANTUM,
581 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
582 USB_DEVICE_ID_MTP_SITRONIX)},
586 MODULE_DEVICE_TABLE(hid, mt_devices);
588 static const struct hid_usage_id mt_grabbed_usages[] = {
589 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
590 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
593 static struct hid_driver mt_driver = {
594 .name = "hid-multitouch",
595 .id_table = mt_devices,
598 .input_mapping = mt_input_mapping,
599 .input_mapped = mt_input_mapped,
600 .feature_mapping = mt_feature_mapping,
601 .usage_table = mt_grabbed_usages,
604 .reset_resume = mt_reset_resume,
608 static int __init mt_init(void)
610 return hid_register_driver(&mt_driver);
613 static void __exit mt_exit(void)
615 hid_unregister_driver(&mt_driver);
618 module_init(mt_init);
619 module_exit(mt_exit);