2 * HID driver for multitouch panels
4 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
5 * Copyright (c) 2010-2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6 * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
7 * Copyright (c) 2012-2013 Red Hat, Inc
9 * This code is partly based on hid-egalax.c:
11 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
12 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
13 * Copyright (c) 2010 Canonical, Ltd.
15 * This code is partly based on hid-3m-pct.c:
17 * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
18 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
19 * Copyright (c) 2010 Canonical, Ltd.
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the Free
26 * Software Foundation; either version 2 of the License, or (at your option)
31 * This driver is regularly tested thanks to the tool hid-test[1].
32 * This tool relies on hid-replay[2] and a database of hid devices[3].
33 * Please run these regression tests before patching this module so that
34 * your patch won't break existing known devices.
36 * [1] https://github.com/bentiss/hid-test
37 * [2] https://github.com/bentiss/hid-replay
38 * [3] https://github.com/bentiss/hid-devices
41 #include <linux/device.h>
42 #include <linux/hid.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/input/mt.h>
46 #include <linux/jiffies.h>
47 #include <linux/string.h>
48 #include <linux/timer.h>
51 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
52 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
53 MODULE_DESCRIPTION("HID multitouch panels");
54 MODULE_LICENSE("GPL");
58 /* quirks to control the device */
59 #define MT_QUIRK_NOT_SEEN_MEANS_UP BIT(0)
60 #define MT_QUIRK_SLOT_IS_CONTACTID BIT(1)
61 #define MT_QUIRK_CYPRESS BIT(2)
62 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER BIT(3)
63 #define MT_QUIRK_ALWAYS_VALID BIT(4)
64 #define MT_QUIRK_VALID_IS_INRANGE BIT(5)
65 #define MT_QUIRK_VALID_IS_CONFIDENCE BIT(6)
66 #define MT_QUIRK_CONFIDENCE BIT(7)
67 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE BIT(8)
68 #define MT_QUIRK_NO_AREA BIT(9)
69 #define MT_QUIRK_IGNORE_DUPLICATES BIT(10)
70 #define MT_QUIRK_HOVERING BIT(11)
71 #define MT_QUIRK_CONTACT_CNT_ACCURATE BIT(12)
72 #define MT_QUIRK_FORCE_GET_FEATURE BIT(13)
73 #define MT_QUIRK_FIX_CONST_CONTACT_ID BIT(14)
74 #define MT_QUIRK_TOUCH_SIZE_SCALING BIT(15)
75 #define MT_QUIRK_STICKY_FINGERS BIT(16)
76 #define MT_QUIRK_ASUS_CUSTOM_UP BIT(17)
77 #define MT_QUIRK_WIN8_PTP_BUTTONS BIT(18)
79 #define MT_INPUTMODE_TOUCHSCREEN 0x02
80 #define MT_INPUTMODE_TOUCHPAD 0x03
82 #define MT_BUTTONTYPE_CLICKPAD 0
84 #define MT_IO_FLAGS_RUNNING 0
85 #define MT_IO_FLAGS_ACTIVE_SLOTS 1
86 #define MT_IO_FLAGS_PENDING_SLOTS 2
89 __s32 x, y, cx, cy, p, w, h, a;
90 __s32 contactid; /* the device ContactID assigned to this slot */
91 bool touch_state; /* is the touch valid? */
92 bool inrange_state; /* is the finger in proximity of the sensor? */
93 bool confidence_state; /* is the touch made by a finger? */
94 bool has_azimuth; /* the contact reports azimuth */
98 __s32 name; /* MT_CLS */
100 __s32 sn_move; /* Signal/noise ratio for move events */
101 __s32 sn_width; /* Signal/noise ratio for width events */
102 __s32 sn_height; /* Signal/noise ratio for height events */
103 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
105 bool is_indirect; /* true for touchpads */
106 bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
110 unsigned usages[HID_MAX_FIELDS];
115 struct mt_slot curdata; /* placeholder of incoming data */
116 struct mt_class mtclass; /* our mt device class */
117 struct timer_list release_timer; /* to release sticky fingers */
118 struct hid_device *hdev; /* hid_device we're attached to */
119 struct mt_fields *fields; /* temporary placeholder for storing the
121 unsigned long mt_io_flags; /* mt flags (MT_IO_FLAGS_*) */
122 int cc_index; /* contact count field index in the report */
123 int cc_value_index; /* contact count value index in the field */
124 int scantime_index; /* scantime field index in the report */
125 int scantime_val_index; /* scantime value index in the field */
126 int prev_scantime; /* scantime reported in the previous packet */
127 int left_button_state; /* left button state */
128 unsigned last_slot_field; /* the last field of a slot */
129 unsigned mt_report_id; /* the report ID of the multitouch device */
130 __s16 inputmode; /* InputMode HID feature, -1 if non-existent */
131 __s16 inputmode_index; /* InputMode HID feature index in the report */
132 __s16 maxcontact_report_id; /* Maximum Contact Number HID feature,
133 -1 if non-existent */
134 __u8 inputmode_value; /* InputMode HID feature value */
135 __u8 num_received; /* how many contacts we received */
136 __u8 num_expected; /* expected last contact index */
138 __u8 touches_by_report; /* how many touches are present in one report:
139 * 1 means we should use a serial protocol
140 * > 1 means hybrid (multitouch) protocol */
141 __u8 buttons_count; /* number of physical buttons per touchpad */
142 bool is_buttonpad; /* is this device a button pad? */
143 bool serial_maybe; /* need to check for serial protocol */
144 bool curvalid; /* is the current contact valid? */
145 unsigned mt_flags; /* flags to pass to input-mt */
146 __s32 dev_time; /* the scan time provided by the device */
147 unsigned long jiffies; /* the frame's jiffies */
148 int timestamp; /* the timestamp to be sent */
151 static void mt_post_parse_default_settings(struct mt_device *td);
152 static void mt_post_parse(struct mt_device *td);
154 /* classes of device behavior */
155 #define MT_CLS_DEFAULT 0x0001
157 #define MT_CLS_SERIAL 0x0002
158 #define MT_CLS_CONFIDENCE 0x0003
159 #define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
160 #define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
161 #define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
162 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
163 /* reserved 0x0008 */
164 #define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
165 #define MT_CLS_NSMU 0x000a
166 /* reserved 0x0010 */
167 /* reserved 0x0011 */
168 #define MT_CLS_WIN_8 0x0012
169 #define MT_CLS_EXPORT_ALL_INPUTS 0x0013
170 #define MT_CLS_WIN_8_DUAL 0x0014
172 /* vendor specific classes */
173 #define MT_CLS_3M 0x0101
174 /* reserved 0x0102 */
175 #define MT_CLS_EGALAX 0x0103
176 #define MT_CLS_EGALAX_SERIAL 0x0104
177 #define MT_CLS_TOPSEED 0x0105
178 #define MT_CLS_PANASONIC 0x0106
179 #define MT_CLS_FLATFROG 0x0107
180 #define MT_CLS_GENERALTOUCH_TWOFINGERS 0x0108
181 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS 0x0109
182 #define MT_CLS_LG 0x010a
183 #define MT_CLS_ASUS 0x010b
184 #define MT_CLS_VTL 0x0110
185 #define MT_CLS_GOOGLE 0x0111
186 #define MT_CLS_RAZER_BLADE_STEALTH 0x0112
188 #define MT_DEFAULT_MAXCONTACT 10
189 #define MT_MAX_MAXCONTACT 250
192 * Resync device and local timestamps after that many microseconds without
195 #define MAX_TIMESTAMP_INTERVAL 1000000
197 #define MT_USB_DEVICE(v, p) HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
198 #define MT_BT_DEVICE(v, p) HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
201 * these device-dependent functions determine what slot corresponds
202 * to a valid contact that was just read.
205 static int cypress_compute_slot(struct mt_device *td)
207 if (td->curdata.contactid != 0 || td->num_received == 0)
208 return td->curdata.contactid;
213 static struct mt_class mt_classes[] = {
214 { .name = MT_CLS_DEFAULT,
215 .quirks = MT_QUIRK_ALWAYS_VALID |
216 MT_QUIRK_CONTACT_CNT_ACCURATE },
217 { .name = MT_CLS_NSMU,
218 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
219 { .name = MT_CLS_SERIAL,
220 .quirks = MT_QUIRK_ALWAYS_VALID},
221 { .name = MT_CLS_CONFIDENCE,
222 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
223 { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
224 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
225 MT_QUIRK_SLOT_IS_CONTACTID },
226 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
227 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
228 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
229 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
230 .quirks = MT_QUIRK_VALID_IS_INRANGE |
231 MT_QUIRK_SLOT_IS_CONTACTID,
233 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
234 .quirks = MT_QUIRK_VALID_IS_INRANGE |
235 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
237 { .name = MT_CLS_INRANGE_CONTACTNUMBER,
238 .quirks = MT_QUIRK_VALID_IS_INRANGE |
239 MT_QUIRK_SLOT_IS_CONTACTNUMBER },
240 { .name = MT_CLS_WIN_8,
241 .quirks = MT_QUIRK_ALWAYS_VALID |
242 MT_QUIRK_IGNORE_DUPLICATES |
244 MT_QUIRK_CONTACT_CNT_ACCURATE |
245 MT_QUIRK_STICKY_FINGERS |
246 MT_QUIRK_WIN8_PTP_BUTTONS },
247 { .name = MT_CLS_EXPORT_ALL_INPUTS,
248 .quirks = MT_QUIRK_ALWAYS_VALID |
249 MT_QUIRK_CONTACT_CNT_ACCURATE,
250 .export_all_inputs = true },
251 { .name = MT_CLS_WIN_8_DUAL,
252 .quirks = MT_QUIRK_ALWAYS_VALID |
253 MT_QUIRK_IGNORE_DUPLICATES |
255 MT_QUIRK_CONTACT_CNT_ACCURATE |
256 MT_QUIRK_WIN8_PTP_BUTTONS,
257 .export_all_inputs = true },
260 * vendor specific classes
263 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
264 MT_QUIRK_SLOT_IS_CONTACTID |
265 MT_QUIRK_TOUCH_SIZE_SCALING,
271 { .name = MT_CLS_EGALAX,
272 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
273 MT_QUIRK_VALID_IS_INRANGE,
277 { .name = MT_CLS_EGALAX_SERIAL,
278 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
279 MT_QUIRK_ALWAYS_VALID,
283 { .name = MT_CLS_TOPSEED,
284 .quirks = MT_QUIRK_ALWAYS_VALID,
288 { .name = MT_CLS_PANASONIC,
289 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
291 { .name = MT_CLS_GENERALTOUCH_TWOFINGERS,
292 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
293 MT_QUIRK_VALID_IS_INRANGE |
294 MT_QUIRK_SLOT_IS_CONTACTID,
297 { .name = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
298 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
299 MT_QUIRK_SLOT_IS_CONTACTID
302 { .name = MT_CLS_FLATFROG,
303 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
309 .quirks = MT_QUIRK_ALWAYS_VALID |
310 MT_QUIRK_FIX_CONST_CONTACT_ID |
311 MT_QUIRK_IGNORE_DUPLICATES |
313 MT_QUIRK_CONTACT_CNT_ACCURATE },
314 { .name = MT_CLS_ASUS,
315 .quirks = MT_QUIRK_ALWAYS_VALID |
316 MT_QUIRK_CONTACT_CNT_ACCURATE |
317 MT_QUIRK_ASUS_CUSTOM_UP },
318 { .name = MT_CLS_VTL,
319 .quirks = MT_QUIRK_ALWAYS_VALID |
320 MT_QUIRK_CONTACT_CNT_ACCURATE |
321 MT_QUIRK_FORCE_GET_FEATURE,
323 { .name = MT_CLS_GOOGLE,
324 .quirks = MT_QUIRK_ALWAYS_VALID |
325 MT_QUIRK_CONTACT_CNT_ACCURATE |
326 MT_QUIRK_SLOT_IS_CONTACTID |
329 { .name = MT_CLS_RAZER_BLADE_STEALTH,
330 .quirks = MT_QUIRK_ALWAYS_VALID |
331 MT_QUIRK_IGNORE_DUPLICATES |
333 MT_QUIRK_CONTACT_CNT_ACCURATE |
334 MT_QUIRK_WIN8_PTP_BUTTONS,
339 static ssize_t mt_show_quirks(struct device *dev,
340 struct device_attribute *attr,
343 struct hid_device *hdev = to_hid_device(dev);
344 struct mt_device *td = hid_get_drvdata(hdev);
346 return sprintf(buf, "%u\n", td->mtclass.quirks);
349 static ssize_t mt_set_quirks(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
353 struct hid_device *hdev = to_hid_device(dev);
354 struct mt_device *td = hid_get_drvdata(hdev);
358 if (kstrtoul(buf, 0, &val))
361 td->mtclass.quirks = val;
363 if (td->cc_index < 0)
364 td->mtclass.quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
369 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
371 static struct attribute *sysfs_attrs[] = {
372 &dev_attr_quirks.attr,
376 static const struct attribute_group mt_attribute_group = {
380 static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
383 u32 size = hid_report_len(report);
387 * Do not fetch the feature report if the device has been explicitly
388 * marked as non-capable.
390 if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
393 buf = hid_alloc_report_buf(report, GFP_KERNEL);
397 ret = hid_hw_raw_request(hdev, report->id, buf, size,
398 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
400 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
403 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
406 dev_warn(&hdev->dev, "failed to report feature\n");
412 static void mt_feature_mapping(struct hid_device *hdev,
413 struct hid_field *field, struct hid_usage *usage)
415 struct mt_device *td = hid_get_drvdata(hdev);
417 switch (usage->hid) {
418 case HID_DG_INPUTMODE:
419 /* Ignore if value index is out of bounds. */
420 if (usage->usage_index >= field->report_count) {
421 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
425 if (td->inputmode < 0) {
426 td->inputmode = field->report->id;
427 td->inputmode_index = usage->usage_index;
430 * Some elan panels wrongly declare 2 input mode
431 * features, and silently ignore when we set the
432 * value in the second field. Skip the second feature
433 * and hope for the best.
436 "Ignoring the extra HID_DG_INPUTMODE\n");
440 case HID_DG_CONTACTMAX:
441 mt_get_feature(hdev, field->report);
443 td->maxcontact_report_id = field->report->id;
444 td->maxcontacts = field->value[0];
445 if (!td->maxcontacts &&
446 field->logical_maximum <= MT_MAX_MAXCONTACT)
447 td->maxcontacts = field->logical_maximum;
448 if (td->mtclass.maxcontacts)
449 /* check if the maxcontacts is given by the class */
450 td->maxcontacts = td->mtclass.maxcontacts;
453 case HID_DG_BUTTONTYPE:
454 if (usage->usage_index >= field->report_count) {
455 dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
459 mt_get_feature(hdev, field->report);
460 if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
461 td->is_buttonpad = true;
465 /* Retrieve the Win8 blob once to enable some devices */
466 if (usage->usage_index == 0)
467 mt_get_feature(hdev, field->report);
472 static void set_abs(struct input_dev *input, unsigned int code,
473 struct hid_field *field, int snratio)
475 int fmin = field->logical_minimum;
476 int fmax = field->logical_maximum;
477 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
478 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
479 input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
482 static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
483 struct hid_input *hi)
485 struct mt_fields *f = td->fields;
487 if (f->length >= HID_MAX_FIELDS)
490 f->usages[f->length++] = usage->hid;
493 static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
494 struct hid_field *field, struct hid_usage *usage,
495 unsigned long **bit, int *max)
497 struct mt_device *td = hid_get_drvdata(hdev);
498 struct mt_class *cls = &td->mtclass;
500 struct hid_usage *prev_usage = NULL;
502 if (field->application == HID_DG_TOUCHSCREEN)
503 td->mt_flags |= INPUT_MT_DIRECT;
506 * Model touchscreens providing buttons as touchpads.
508 if (field->application == HID_DG_TOUCHPAD ||
509 (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
510 td->mt_flags |= INPUT_MT_POINTER;
511 td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
514 /* count the buttons on touchpads */
515 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
518 if (usage->usage_index)
519 prev_usage = &field->usage[usage->usage_index - 1];
521 switch (usage->hid & HID_USAGE_PAGE) {
524 switch (usage->hid) {
526 if (prev_usage && (prev_usage->hid == usage->hid)) {
527 hid_map_usage(hi, usage, bit, max,
528 EV_ABS, ABS_MT_TOOL_X);
529 set_abs(hi->input, ABS_MT_TOOL_X, field,
532 hid_map_usage(hi, usage, bit, max,
533 EV_ABS, ABS_MT_POSITION_X);
534 set_abs(hi->input, ABS_MT_POSITION_X, field,
538 mt_store_field(usage, td, hi);
541 if (prev_usage && (prev_usage->hid == usage->hid)) {
542 hid_map_usage(hi, usage, bit, max,
543 EV_ABS, ABS_MT_TOOL_Y);
544 set_abs(hi->input, ABS_MT_TOOL_Y, field,
547 hid_map_usage(hi, usage, bit, max,
548 EV_ABS, ABS_MT_POSITION_Y);
549 set_abs(hi->input, ABS_MT_POSITION_Y, field,
553 mt_store_field(usage, td, hi);
558 case HID_UP_DIGITIZER:
559 switch (usage->hid) {
561 if (cls->quirks & MT_QUIRK_HOVERING) {
562 hid_map_usage(hi, usage, bit, max,
563 EV_ABS, ABS_MT_DISTANCE);
564 input_set_abs_params(hi->input,
565 ABS_MT_DISTANCE, 0, 1, 0, 0);
567 mt_store_field(usage, td, hi);
569 case HID_DG_CONFIDENCE:
570 if ((cls->name == MT_CLS_WIN_8 ||
571 cls->name == MT_CLS_WIN_8_DUAL) &&
572 field->application == HID_DG_TOUCHPAD)
573 cls->quirks |= MT_QUIRK_CONFIDENCE;
574 mt_store_field(usage, td, hi);
576 case HID_DG_TIPSWITCH:
577 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
578 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
579 mt_store_field(usage, td, hi);
581 case HID_DG_CONTACTID:
582 mt_store_field(usage, td, hi);
583 td->touches_by_report++;
584 td->mt_report_id = field->report->id;
587 hid_map_usage(hi, usage, bit, max,
588 EV_ABS, ABS_MT_TOUCH_MAJOR);
589 if (!(cls->quirks & MT_QUIRK_NO_AREA))
590 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
592 mt_store_field(usage, td, hi);
595 hid_map_usage(hi, usage, bit, max,
596 EV_ABS, ABS_MT_TOUCH_MINOR);
597 if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
598 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
602 * Only set ABS_MT_ORIENTATION if it is not
603 * already set by the HID_DG_AZIMUTH usage.
605 if (!test_bit(ABS_MT_ORIENTATION,
607 input_set_abs_params(hi->input,
608 ABS_MT_ORIENTATION, 0, 1, 0, 0);
610 mt_store_field(usage, td, hi);
612 case HID_DG_TIPPRESSURE:
613 hid_map_usage(hi, usage, bit, max,
614 EV_ABS, ABS_MT_PRESSURE);
615 set_abs(hi->input, ABS_MT_PRESSURE, field,
617 mt_store_field(usage, td, hi);
619 case HID_DG_SCANTIME:
620 hid_map_usage(hi, usage, bit, max,
621 EV_MSC, MSC_TIMESTAMP);
622 input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
623 mt_store_field(usage, td, hi);
624 /* Ignore if indexes are out of bounds. */
625 if (field->index >= field->report->maxfield ||
626 usage->usage_index >= field->report_count)
628 td->scantime_index = field->index;
629 td->scantime_val_index = usage->usage_index;
631 case HID_DG_CONTACTCOUNT:
632 /* Ignore if indexes are out of bounds. */
633 if (field->index >= field->report->maxfield ||
634 usage->usage_index >= field->report_count)
636 td->cc_index = field->index;
637 td->cc_value_index = usage->usage_index;
640 hid_map_usage(hi, usage, bit, max,
641 EV_ABS, ABS_MT_ORIENTATION);
643 * Azimuth has the range of [0, MAX) representing a full
644 * revolution. Set ABS_MT_ORIENTATION to a quarter of
645 * MAX according the definition of ABS_MT_ORIENTATION
647 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
648 -field->logical_maximum / 4,
649 field->logical_maximum / 4,
651 field->logical_maximum / cls->sn_move : 0, 0);
652 mt_store_field(usage, td, hi);
654 case HID_DG_CONTACTMAX:
655 /* we don't set td->last_slot_field as contactcount and
656 * contact max are global to the report */
659 /* Legacy devices use TIPSWITCH and not TOUCH.
660 * Let's just ignore this field. */
663 /* let hid-input decide for the others */
667 code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
669 * MS PTP spec says that external buttons left and right have
672 if ((cls->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
673 field->application == HID_DG_TOUCHPAD &&
674 (usage->hid & HID_USAGE) > 1)
676 hid_map_usage(hi, usage, bit, max, EV_KEY, code);
677 input_set_capability(hi->input, EV_KEY, code);
681 /* we do not want to map these: no input-oriented meaning */
688 static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
690 __s32 quirks = td->mtclass.quirks;
692 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
693 return td->curdata.contactid;
695 if (quirks & MT_QUIRK_CYPRESS)
696 return cypress_compute_slot(td);
698 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
699 return td->num_received;
701 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
702 return td->curdata.contactid - 1;
704 return input_mt_get_slot_by_key(input, td->curdata.contactid);
708 * this function is called when a whole contact has been processed,
709 * so that it can assign it to a slot and store the data there
711 static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
713 if ((td->mtclass.quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
714 td->num_received >= td->num_expected)
717 if (td->curvalid || (td->mtclass.quirks & MT_QUIRK_ALWAYS_VALID)) {
719 int slotnum = mt_compute_slot(td, input);
720 struct mt_slot *s = &td->curdata;
721 struct input_mt *mt = input->mt;
723 if (slotnum < 0 || slotnum >= td->maxcontacts)
726 if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
727 struct input_mt_slot *slot = &mt->slots[slotnum];
728 if (input_mt_is_active(slot) &&
729 input_mt_is_used(mt, slot))
733 if (!(td->mtclass.quirks & MT_QUIRK_CONFIDENCE))
734 s->confidence_state = true;
735 active = (s->touch_state || s->inrange_state) &&
738 input_mt_slot(input, slotnum);
739 input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
741 /* this finger is in proximity of the sensor */
742 int wide = (s->w > s->h);
743 int major = max(s->w, s->h);
744 int minor = min(s->w, s->h);
745 int orientation = wide;
751 * divided by two to match visual scale of touch
752 * for devices with this quirk
754 if (td->mtclass.quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
759 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
760 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
761 input_event(input, EV_ABS, ABS_MT_TOOL_X, s->cx);
762 input_event(input, EV_ABS, ABS_MT_TOOL_Y, s->cy);
763 input_event(input, EV_ABS, ABS_MT_DISTANCE,
765 input_event(input, EV_ABS, ABS_MT_ORIENTATION,
767 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
768 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
769 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
771 set_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
779 * this function is called when a whole packet has been received and processed,
780 * so that it can decide what to send to the input layer.
782 static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
784 if (td->mtclass.quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
785 input_event(input, EV_KEY, BTN_LEFT, td->left_button_state);
787 input_mt_sync_frame(input);
788 input_event(input, EV_MSC, MSC_TIMESTAMP, td->timestamp);
790 td->num_received = 0;
791 td->left_button_state = 0;
792 if (test_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags))
793 set_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
795 clear_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
796 clear_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
799 static int mt_compute_timestamp(struct mt_device *td, struct hid_field *field,
802 long delta = value - td->dev_time;
803 unsigned long jdelta = jiffies_to_usecs(jiffies - td->jiffies);
805 td->jiffies = jiffies;
806 td->dev_time = value;
809 delta += field->logical_maximum;
811 /* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
814 if (jdelta > MAX_TIMESTAMP_INTERVAL)
815 /* No data received for a while, resync the timestamp. */
818 return td->timestamp + delta;
821 static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
822 struct hid_usage *usage, __s32 value)
824 /* we will handle the hidinput part later, now remains hiddev */
825 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
826 hid->hiddev_hid_event(hid, field, usage, value);
831 static void mt_process_mt_event(struct hid_device *hid, struct hid_field *field,
832 struct hid_usage *usage, __s32 value,
835 struct mt_device *td = hid_get_drvdata(hid);
836 __s32 quirks = td->mtclass.quirks;
837 struct input_dev *input = field->hidinput->input;
839 if (hid->claimed & HID_CLAIMED_INPUT) {
840 switch (usage->hid) {
842 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
843 td->curvalid = value;
844 if (quirks & MT_QUIRK_HOVERING)
845 td->curdata.inrange_state = value;
847 case HID_DG_TIPSWITCH:
848 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
849 td->curvalid = value;
850 td->curdata.touch_state = value;
852 case HID_DG_CONFIDENCE:
853 if (quirks & MT_QUIRK_CONFIDENCE)
854 td->curdata.confidence_state = value;
855 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
856 td->curvalid = value;
858 case HID_DG_CONTACTID:
859 td->curdata.contactid = value;
861 case HID_DG_TIPPRESSURE:
862 td->curdata.p = value;
865 if (usage->code == ABS_MT_TOOL_X)
866 td->curdata.cx = value;
868 td->curdata.x = value;
871 if (usage->code == ABS_MT_TOOL_Y)
872 td->curdata.cy = value;
874 td->curdata.y = value;
877 td->curdata.w = value;
880 td->curdata.h = value;
882 case HID_DG_SCANTIME:
883 td->timestamp = mt_compute_timestamp(td, field, value);
885 case HID_DG_CONTACTCOUNT:
889 * Azimuth is counter-clockwise and ranges from [0, MAX)
890 * (a full revolution). Convert it to clockwise ranging
893 * Note that ABS_MT_ORIENTATION require us to report
894 * the limit of [-MAX/4, MAX/4], but the value can go
895 * out of range to [-MAX/2, MAX/2] to report an upside
898 if (value > field->logical_maximum / 2)
899 value -= field->logical_maximum;
900 td->curdata.a = -value;
901 td->curdata.has_azimuth = true;
909 * For Win8 PTP touchpads we should only look at
910 * non finger/touch events in the first_packet of
911 * a (possible) multi-packet frame.
913 if ((quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
918 * For Win8 PTP touchpads we map both the clickpad click
919 * and any "external" left buttons to BTN_LEFT if a
920 * device claims to have both we need to report 1 for
921 * BTN_LEFT if either is pressed, so we or all values
922 * together and report the result in mt_sync_frame().
924 if ((quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
925 usage->type == EV_KEY && usage->code == BTN_LEFT) {
926 td->left_button_state |= value;
931 input_event(input, usage->type, usage->code,
936 if (usage->usage_index + 1 == field->report_count) {
937 /* we only take into account the last report. */
938 if (usage->hid == td->last_slot_field)
939 mt_complete_slot(td, field->hidinput->input);
945 static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
947 struct mt_device *td = hid_get_drvdata(hid);
948 struct hid_field *field;
951 int r, n, scantime = 0;
953 /* sticky fingers release in progress, abort */
954 if (test_and_set_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
958 * Includes multi-packet support where subsequent
959 * packets are sent with zero contactcount.
961 if (td->scantime_index >= 0) {
962 field = report->field[td->scantime_index];
963 scantime = field->value[td->scantime_val_index];
965 if (td->cc_index >= 0) {
966 struct hid_field *field = report->field[td->cc_index];
967 int value = field->value[td->cc_value_index];
970 * For Win8 PTPs the first packet (td->num_received == 0) may
971 * have a contactcount of 0 if there only is a button event.
972 * We double check that this is not a continuation packet
973 * of a possible multi-packet frame be checking that the
974 * timestamp has changed.
976 if ((td->mtclass.quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
977 td->num_received == 0 && td->prev_scantime != scantime)
978 td->num_expected = value;
979 /* A non 0 contact count always indicates a first packet */
981 td->num_expected = value;
983 td->prev_scantime = scantime;
985 first_packet = td->num_received == 0;
986 for (r = 0; r < report->maxfield; r++) {
987 field = report->field[r];
988 count = field->report_count;
990 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
993 for (n = 0; n < count; n++)
994 mt_process_mt_event(hid, field, &field->usage[n],
995 field->value[n], first_packet);
998 if (td->num_received >= td->num_expected)
999 mt_sync_frame(td, report->field[0]->hidinput->input);
1002 * Windows 8 specs says 2 things:
1003 * - once a contact has been reported, it has to be reported in each
1005 * - the report rate when fingers are present has to be at least
1006 * the refresh rate of the screen, 60 or 120 Hz
1008 * I interprete this that the specification forces a report rate of
1009 * at least 60 Hz for a touchscreen to be certified.
1010 * Which means that if we do not get a report whithin 16 ms, either
1011 * something wrong happens, either the touchscreen forgets to send
1012 * a release. Taking a reasonable margin allows to remove issues
1013 * with USB communication or the load of the machine.
1015 * Given that Win 8 devices are forced to send a release, this will
1016 * only affect laggish machines and the ones that have a firmware
1019 if (td->mtclass.quirks & MT_QUIRK_STICKY_FINGERS) {
1020 if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1021 mod_timer(&td->release_timer,
1022 jiffies + msecs_to_jiffies(100));
1024 del_timer(&td->release_timer);
1027 clear_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1030 static int mt_touch_input_configured(struct hid_device *hdev,
1031 struct hid_input *hi)
1033 struct mt_device *td = hid_get_drvdata(hdev);
1034 struct mt_class *cls = &td->mtclass;
1035 struct input_dev *input = hi->input;
1038 if (!td->maxcontacts)
1039 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
1042 if (td->serial_maybe)
1043 mt_post_parse_default_settings(td);
1045 if (cls->is_indirect)
1046 td->mt_flags |= INPUT_MT_POINTER;
1048 if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1049 td->mt_flags |= INPUT_MT_DROP_UNUSED;
1051 /* check for clickpads */
1052 if ((td->mt_flags & INPUT_MT_POINTER) && (td->buttons_count == 1))
1053 td->is_buttonpad = true;
1055 if (td->is_buttonpad)
1056 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1058 ret = input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
1066 #define mt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
1068 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1069 struct hid_field *field, struct hid_usage *usage,
1070 unsigned long **bit, int *max)
1072 struct mt_device *td = hid_get_drvdata(hdev);
1075 * If mtclass.export_all_inputs is not set, only map fields from
1076 * TouchScreen or TouchPad collections. We need to ignore fields
1077 * that belong to other collections such as Mouse that might have
1078 * the same GenericDesktop usages.
1080 if (!td->mtclass.export_all_inputs &&
1081 field->application != HID_DG_TOUCHSCREEN &&
1082 field->application != HID_DG_PEN &&
1083 field->application != HID_DG_TOUCHPAD &&
1084 field->application != HID_GD_KEYBOARD &&
1085 field->application != HID_GD_SYSTEM_CONTROL &&
1086 field->application != HID_CP_CONSUMER_CONTROL &&
1087 field->application != HID_GD_WIRELESS_RADIO_CTLS &&
1088 !(field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1089 td->mtclass.quirks & MT_QUIRK_ASUS_CUSTOM_UP))
1093 * Some Asus keyboard+touchpad devices have the hotkeys defined in the
1094 * touchpad report descriptor. We need to treat these as an array to
1095 * map usages to input keys.
1097 if (field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1098 td->mtclass.quirks & MT_QUIRK_ASUS_CUSTOM_UP &&
1099 (usage->hid & HID_USAGE_PAGE) == HID_UP_CUSTOM) {
1100 set_bit(EV_REP, hi->input->evbit);
1101 if (field->flags & HID_MAIN_ITEM_VARIABLE)
1102 field->flags &= ~HID_MAIN_ITEM_VARIABLE;
1103 switch (usage->hid & HID_USAGE) {
1104 case 0x10: mt_map_key_clear(KEY_BRIGHTNESSDOWN); break;
1105 case 0x20: mt_map_key_clear(KEY_BRIGHTNESSUP); break;
1106 case 0x35: mt_map_key_clear(KEY_DISPLAY_OFF); break;
1107 case 0x6b: mt_map_key_clear(KEY_F21); break;
1108 case 0x6c: mt_map_key_clear(KEY_SLEEP); break;
1116 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1118 * The check for mt_report_id ensures we don't process
1119 * HID_DG_CONTACTCOUNT from the pen report as it is outside the physical
1120 * collection, but within the report ID.
1122 if (field->physical == HID_DG_STYLUS)
1124 else if ((field->physical == 0) &&
1125 (field->report->id != td->mt_report_id) &&
1126 (td->mt_report_id != -1))
1129 if (field->application == HID_DG_TOUCHSCREEN ||
1130 field->application == HID_DG_TOUCHPAD)
1131 return mt_touch_input_mapping(hdev, hi, field, usage, bit, max);
1133 /* let hid-core decide for the others */
1137 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
1138 struct hid_field *field, struct hid_usage *usage,
1139 unsigned long **bit, int *max)
1142 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1145 if (field->physical == HID_DG_STYLUS)
1148 if (field->application == HID_DG_TOUCHSCREEN ||
1149 field->application == HID_DG_TOUCHPAD) {
1150 /* We own these mappings, tell hid-input to ignore them */
1154 /* let hid-core decide for the others */
1158 static int mt_event(struct hid_device *hid, struct hid_field *field,
1159 struct hid_usage *usage, __s32 value)
1161 struct mt_device *td = hid_get_drvdata(hid);
1163 if (field->report->id == td->mt_report_id)
1164 return mt_touch_event(hid, field, usage, value);
1169 static void mt_report(struct hid_device *hid, struct hid_report *report)
1171 struct mt_device *td = hid_get_drvdata(hid);
1172 struct hid_field *field = report->field[0];
1174 if (!(hid->claimed & HID_CLAIMED_INPUT))
1177 if (report->id == td->mt_report_id)
1178 return mt_touch_report(hid, report);
1180 if (field && field->hidinput && field->hidinput->input)
1181 input_sync(field->hidinput->input);
1184 static void mt_set_input_mode(struct hid_device *hdev)
1186 struct mt_device *td = hid_get_drvdata(hdev);
1187 struct hid_report *r;
1188 struct hid_report_enum *re;
1189 struct mt_class *cls = &td->mtclass;
1193 if (td->inputmode < 0)
1196 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
1197 r = re->report_id_hash[td->inputmode];
1199 if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
1200 report_len = hid_report_len(r);
1201 buf = hid_alloc_report_buf(r, GFP_KERNEL);
1203 hid_err(hdev, "failed to allocate buffer for report\n");
1206 hid_hw_raw_request(hdev, r->id, buf, report_len,
1208 HID_REQ_GET_REPORT);
1211 r->field[0]->value[td->inputmode_index] = td->inputmode_value;
1212 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
1216 static void mt_set_maxcontacts(struct hid_device *hdev)
1218 struct mt_device *td = hid_get_drvdata(hdev);
1219 struct hid_report *r;
1220 struct hid_report_enum *re;
1223 if (td->maxcontact_report_id < 0)
1226 if (!td->mtclass.maxcontacts)
1229 re = &hdev->report_enum[HID_FEATURE_REPORT];
1230 r = re->report_id_hash[td->maxcontact_report_id];
1232 max = td->mtclass.maxcontacts;
1233 fieldmax = r->field[0]->logical_maximum;
1234 max = min(fieldmax, max);
1235 if (r->field[0]->value[0] != max) {
1236 r->field[0]->value[0] = max;
1237 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
1242 static void mt_post_parse_default_settings(struct mt_device *td)
1244 __s32 quirks = td->mtclass.quirks;
1246 /* unknown serial device needs special quirks */
1247 if (td->touches_by_report == 1) {
1248 quirks |= MT_QUIRK_ALWAYS_VALID;
1249 quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1250 quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1251 quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1252 quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1255 td->mtclass.quirks = quirks;
1258 static void mt_post_parse(struct mt_device *td)
1260 struct mt_fields *f = td->fields;
1261 struct mt_class *cls = &td->mtclass;
1263 if (td->touches_by_report > 0) {
1264 int field_count_per_touch = f->length / td->touches_by_report;
1265 td->last_slot_field = f->usages[field_count_per_touch - 1];
1268 if (td->cc_index < 0)
1269 cls->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1272 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1274 struct mt_device *td = hid_get_drvdata(hdev);
1276 const char *suffix = NULL;
1277 struct hid_field *field = hi->report->field[0];
1280 if (hi->report->id == td->mt_report_id) {
1281 ret = mt_touch_input_configured(hdev, hi);
1287 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1288 * for the stylus. Check this first, and then rely on the application
1291 if (hi->report->field[0]->physical == HID_DG_STYLUS) {
1293 /* force BTN_STYLUS to allow tablet matching in udev */
1294 __set_bit(BTN_STYLUS, hi->input->keybit);
1296 switch (field->application) {
1297 case HID_GD_KEYBOARD:
1298 suffix = "Keyboard";
1308 /* force BTN_STYLUS to allow tablet matching in udev */
1309 __set_bit(BTN_STYLUS, hi->input->keybit);
1311 case HID_DG_TOUCHSCREEN:
1312 /* we do not set suffix = "Touchscreen" */
1314 case HID_DG_TOUCHPAD:
1315 suffix = "Touchpad";
1317 case HID_GD_SYSTEM_CONTROL:
1318 suffix = "System Control";
1320 case HID_CP_CONSUMER_CONTROL:
1321 suffix = "Consumer Control";
1323 case HID_GD_WIRELESS_RADIO_CTLS:
1324 suffix = "Wireless Radio Control";
1326 case HID_VD_ASUS_CUSTOM_MEDIA_KEYS:
1327 suffix = "Custom Media Keys";
1336 name = devm_kzalloc(&hi->input->dev,
1337 strlen(hdev->name) + strlen(suffix) + 2,
1340 sprintf(name, "%s %s", hdev->name, suffix);
1341 hi->input->name = name;
1348 static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
1350 if (field->usage[0].hid != usage ||
1351 !(field->flags & HID_MAIN_ITEM_CONSTANT))
1354 field->flags &= ~HID_MAIN_ITEM_CONSTANT;
1355 field->flags |= HID_MAIN_ITEM_VARIABLE;
1358 static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
1360 struct hid_report *report;
1363 list_for_each_entry(report,
1364 &hdev->report_enum[HID_INPUT_REPORT].report_list,
1367 if (!report->maxfield)
1370 for (i = 0; i < report->maxfield; i++)
1371 if (report->field[i]->maxusage >= 1)
1372 mt_fix_const_field(report->field[i], usage);
1376 static void mt_release_contacts(struct hid_device *hid)
1378 struct hid_input *hidinput;
1379 struct mt_device *td = hid_get_drvdata(hid);
1381 list_for_each_entry(hidinput, &hid->inputs, list) {
1382 struct input_dev *input_dev = hidinput->input;
1383 struct input_mt *mt = input_dev->mt;
1387 for (i = 0; i < mt->num_slots; i++) {
1388 input_mt_slot(input_dev, i);
1389 input_mt_report_slot_state(input_dev,
1393 input_mt_sync_frame(input_dev);
1394 input_sync(input_dev);
1398 td->num_received = 0;
1401 static void mt_expired_timeout(struct timer_list *t)
1403 struct mt_device *td = from_timer(td, t, release_timer);
1404 struct hid_device *hdev = td->hdev;
1407 * An input report came in just before we release the sticky fingers,
1408 * it will take care of the sticky fingers.
1410 if (test_and_set_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1412 if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1413 mt_release_contacts(hdev);
1414 clear_bit(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1417 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1420 struct mt_device *td;
1421 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1423 for (i = 0; mt_classes[i].name ; i++) {
1424 if (id->driver_data == mt_classes[i].name) {
1425 mtclass = &(mt_classes[i]);
1430 td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1432 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1436 td->mtclass = *mtclass;
1438 td->maxcontact_report_id = -1;
1439 td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1441 td->scantime_index = -1;
1442 td->mt_report_id = -1;
1443 hid_set_drvdata(hdev, td);
1445 td->fields = devm_kzalloc(&hdev->dev, sizeof(struct mt_fields),
1448 dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
1452 if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1453 td->serial_maybe = true;
1455 /* This allows the driver to correctly support devices
1456 * that emit events over several HID messages.
1458 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1461 * This allows the driver to handle different input sensors
1462 * that emits events through different reports on the same HID
1465 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1467 timer_setup(&td->release_timer, mt_expired_timeout, 0);
1469 ret = hid_parse(hdev);
1473 if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
1474 mt_fix_const_fields(hdev, HID_DG_CONTACTID);
1476 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1480 ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
1482 dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
1485 mt_set_maxcontacts(hdev);
1486 mt_set_input_mode(hdev);
1488 /* release .fields memory as it is not used anymore */
1489 devm_kfree(&hdev->dev, td->fields);
1496 static int mt_reset_resume(struct hid_device *hdev)
1498 mt_release_contacts(hdev);
1499 mt_set_maxcontacts(hdev);
1500 mt_set_input_mode(hdev);
1504 static int mt_resume(struct hid_device *hdev)
1506 /* Some Elan legacy devices require SET_IDLE to be set on resume.
1507 * It should be safe to send it to other devices too.
1508 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
1510 hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
1516 static void mt_remove(struct hid_device *hdev)
1518 struct mt_device *td = hid_get_drvdata(hdev);
1520 del_timer_sync(&td->release_timer);
1522 sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
1527 * This list contains only:
1528 * - VID/PID of products not working with the default multitouch handling
1529 * - 2 generic rules.
1530 * So there is no point in adding here any device with MT_CLS_DEFAULT.
1532 static const struct hid_device_id mt_devices[] = {
1535 { .driver_data = MT_CLS_3M,
1536 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1537 USB_DEVICE_ID_3M1968) },
1538 { .driver_data = MT_CLS_3M,
1539 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1540 USB_DEVICE_ID_3M2256) },
1541 { .driver_data = MT_CLS_3M,
1542 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1543 USB_DEVICE_ID_3M3266) },
1546 { .driver_data = MT_CLS_WIN_8_DUAL,
1547 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1548 USB_VENDOR_ID_ALPS_JP,
1549 HID_DEVICE_ID_ALPS_U1_DUAL_PTP) },
1550 { .driver_data = MT_CLS_WIN_8_DUAL,
1551 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1552 USB_VENDOR_ID_ALPS_JP,
1553 HID_DEVICE_ID_ALPS_U1_DUAL_3BTN_PTP) },
1555 /* Lenovo X1 TAB Gen 2 */
1556 { .driver_data = MT_CLS_WIN_8_DUAL,
1557 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1558 USB_VENDOR_ID_LENOVO,
1559 USB_DEVICE_ID_LENOVO_X1_TAB) },
1562 { .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
1563 MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
1564 USB_DEVICE_ID_ANTON_TOUCH_PAD) },
1567 { .driver_data = MT_CLS_ASUS,
1568 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1569 USB_VENDOR_ID_ASUSTEK,
1570 USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD) },
1573 { .driver_data = MT_CLS_SERIAL,
1574 MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
1575 USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
1577 /* Baanto multitouch devices */
1578 { .driver_data = MT_CLS_NSMU,
1579 MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
1580 USB_DEVICE_ID_BAANTO_MT_190W2) },
1583 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1584 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1585 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1586 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1587 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1588 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
1590 /* Chunghwa Telecom touch panels */
1591 { .driver_data = MT_CLS_NSMU,
1592 MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
1593 USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
1595 /* CJTouch panels */
1596 { .driver_data = MT_CLS_NSMU,
1597 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1598 USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
1599 { .driver_data = MT_CLS_NSMU,
1600 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1601 USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
1603 /* CVTouch panels */
1604 { .driver_data = MT_CLS_NSMU,
1605 MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
1606 USB_DEVICE_ID_CVTOUCH_SCREEN) },
1608 /* eGalax devices (resistive) */
1609 { .driver_data = MT_CLS_EGALAX,
1610 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1611 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
1612 { .driver_data = MT_CLS_EGALAX,
1613 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1614 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
1616 /* eGalax devices (capacitive) */
1617 { .driver_data = MT_CLS_EGALAX_SERIAL,
1618 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1619 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
1620 { .driver_data = MT_CLS_EGALAX,
1621 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1622 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
1623 { .driver_data = MT_CLS_EGALAX_SERIAL,
1624 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1625 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
1626 { .driver_data = MT_CLS_EGALAX_SERIAL,
1627 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1628 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
1629 { .driver_data = MT_CLS_EGALAX_SERIAL,
1630 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1631 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
1632 { .driver_data = MT_CLS_EGALAX_SERIAL,
1633 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1634 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
1635 { .driver_data = MT_CLS_EGALAX,
1636 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1637 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
1638 { .driver_data = MT_CLS_EGALAX,
1639 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1640 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
1641 { .driver_data = MT_CLS_EGALAX_SERIAL,
1642 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1643 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
1644 { .driver_data = MT_CLS_EGALAX,
1645 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1646 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
1647 { .driver_data = MT_CLS_EGALAX,
1648 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1649 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
1650 { .driver_data = MT_CLS_EGALAX,
1651 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1652 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
1653 { .driver_data = MT_CLS_EGALAX,
1654 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1655 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
1656 { .driver_data = MT_CLS_EGALAX_SERIAL,
1657 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1658 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
1659 { .driver_data = MT_CLS_EGALAX_SERIAL,
1660 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1661 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
1662 { .driver_data = MT_CLS_EGALAX_SERIAL,
1663 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1664 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
1666 /* Elitegroup panel */
1667 { .driver_data = MT_CLS_SERIAL,
1668 MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
1669 USB_DEVICE_ID_ELITEGROUP_05D8) },
1671 /* Flatfrog Panels */
1672 { .driver_data = MT_CLS_FLATFROG,
1673 MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
1674 USB_DEVICE_ID_MULTITOUCH_3200) },
1676 /* FocalTech Panels */
1677 { .driver_data = MT_CLS_SERIAL,
1678 MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
1679 USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
1681 /* GeneralTouch panel */
1682 { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1683 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1684 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
1685 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1686 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1687 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
1688 { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1689 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1690 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
1691 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1692 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1693 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
1694 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1695 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1696 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
1697 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1698 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1699 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
1700 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1701 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1702 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
1704 /* Gametel game controller */
1705 { .driver_data = MT_CLS_NSMU,
1706 MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
1707 USB_DEVICE_ID_GAMETEL_MT_MODE) },
1709 /* GoodTouch panels */
1710 { .driver_data = MT_CLS_NSMU,
1711 MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
1712 USB_DEVICE_ID_GOODTOUCH_000f) },
1715 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1716 MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
1717 USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
1719 /* Ilitek dual touch panel */
1720 { .driver_data = MT_CLS_NSMU,
1721 MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
1722 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
1724 /* LG Melfas panel */
1725 { .driver_data = MT_CLS_LG,
1726 HID_USB_DEVICE(USB_VENDOR_ID_LG,
1727 USB_DEVICE_ID_LG_MELFAS_MT) },
1730 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1731 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1732 USB_DEVICE_ID_ASUS_T91MT)},
1733 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1734 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1735 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
1736 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1737 MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
1738 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
1741 { .driver_data = MT_CLS_NSMU,
1742 MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
1743 USB_DEVICE_ID_NOVATEK_PCT) },
1746 { .driver_data = MT_CLS_NSMU,
1747 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1748 USB_VENDOR_ID_NTRIG, 0x1b05) },
1750 /* Panasonic panels */
1751 { .driver_data = MT_CLS_PANASONIC,
1752 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1753 USB_DEVICE_ID_PANABOARD_UBT780) },
1754 { .driver_data = MT_CLS_PANASONIC,
1755 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1756 USB_DEVICE_ID_PANABOARD_UBT880) },
1758 /* PixArt optical touch screen */
1759 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1760 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1761 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
1762 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1763 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1764 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
1765 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1766 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1767 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
1769 /* PixCir-based panels */
1770 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1771 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1772 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
1774 /* Quanta-based panels */
1775 { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1776 MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
1777 USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
1779 /* Razer touchpads */
1780 { .driver_data = MT_CLS_RAZER_BLADE_STEALTH,
1781 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1782 USB_VENDOR_ID_SYNAPTICS, 0x8323) },
1784 /* Stantum panels */
1785 { .driver_data = MT_CLS_CONFIDENCE,
1786 MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
1787 USB_DEVICE_ID_MTP_STM)},
1789 /* TopSeed panels */
1790 { .driver_data = MT_CLS_TOPSEED,
1791 MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
1792 USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
1794 /* Touch International panels */
1795 { .driver_data = MT_CLS_NSMU,
1796 MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
1797 USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
1800 { .driver_data = MT_CLS_NSMU,
1801 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1802 USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
1803 { .driver_data = MT_CLS_NSMU,
1804 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1805 USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
1808 { .driver_data = MT_CLS_VTL,
1809 MT_USB_DEVICE(USB_VENDOR_ID_VTL,
1810 USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
1812 /* Wistron panels */
1813 { .driver_data = MT_CLS_NSMU,
1814 MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
1815 USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
1818 { .driver_data = MT_CLS_NSMU,
1819 MT_USB_DEVICE(USB_VENDOR_ID_XAT,
1820 USB_DEVICE_ID_XAT_CSR) },
1823 { .driver_data = MT_CLS_NSMU,
1824 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1825 USB_DEVICE_ID_XIROKU_SPX) },
1826 { .driver_data = MT_CLS_NSMU,
1827 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1828 USB_DEVICE_ID_XIROKU_MPX) },
1829 { .driver_data = MT_CLS_NSMU,
1830 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1831 USB_DEVICE_ID_XIROKU_CSR) },
1832 { .driver_data = MT_CLS_NSMU,
1833 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1834 USB_DEVICE_ID_XIROKU_SPX1) },
1835 { .driver_data = MT_CLS_NSMU,
1836 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1837 USB_DEVICE_ID_XIROKU_MPX1) },
1838 { .driver_data = MT_CLS_NSMU,
1839 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1840 USB_DEVICE_ID_XIROKU_CSR1) },
1841 { .driver_data = MT_CLS_NSMU,
1842 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1843 USB_DEVICE_ID_XIROKU_SPX2) },
1844 { .driver_data = MT_CLS_NSMU,
1845 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1846 USB_DEVICE_ID_XIROKU_MPX2) },
1847 { .driver_data = MT_CLS_NSMU,
1848 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1849 USB_DEVICE_ID_XIROKU_CSR2) },
1851 /* Google MT devices */
1852 { .driver_data = MT_CLS_GOOGLE,
1853 HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_GOOGLE,
1854 USB_DEVICE_ID_GOOGLE_TOUCH_ROSE) },
1856 /* Generic MT device */
1857 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
1859 /* Generic Win 8 certified MT device */
1860 { .driver_data = MT_CLS_WIN_8,
1861 HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
1862 HID_ANY_ID, HID_ANY_ID) },
1865 MODULE_DEVICE_TABLE(hid, mt_devices);
1867 static const struct hid_usage_id mt_grabbed_usages[] = {
1868 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1869 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
1872 static struct hid_driver mt_driver = {
1873 .name = "hid-multitouch",
1874 .id_table = mt_devices,
1876 .remove = mt_remove,
1877 .input_mapping = mt_input_mapping,
1878 .input_mapped = mt_input_mapped,
1879 .input_configured = mt_input_configured,
1880 .feature_mapping = mt_feature_mapping,
1881 .usage_table = mt_grabbed_usages,
1883 .report = mt_report,
1885 .reset_resume = mt_reset_resume,
1886 .resume = mt_resume,
1889 module_hid_driver(mt_driver);