]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/hid/hid-multitouch.c
HID: multitouch: Support PTP Stick and Touchpad device
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / hid-multitouch.c
1 /*
2 * HID driver for multitouch panels
3 *
4 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
5 * Copyright (c) 2010-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
8 *
9 * This code is partly based on hid-egalax.c:
10 *
11 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
12 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
13 * Copyright (c) 2010 Canonical, Ltd.
14 *
15 * This code is partly based on hid-3m-pct.c:
16 *
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.
20 *
21 */
22
23 /*
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)
27 * any later version.
28 */
29
30 /*
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.
35 *
36 * [1] https://github.com/bentiss/hid-test
37 * [2] https://github.com/bentiss/hid-replay
38 * [3] https://github.com/bentiss/hid-devices
39 */
40
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/string.h>
47
48
49 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
50 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
51 MODULE_DESCRIPTION("HID multitouch panels");
52 MODULE_LICENSE("GPL");
53
54 #include "hid-ids.h"
55
56 /* quirks to control the device */
57 #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
58 #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
59 #define MT_QUIRK_CYPRESS (1 << 2)
60 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
61 #define MT_QUIRK_ALWAYS_VALID (1 << 4)
62 #define MT_QUIRK_VALID_IS_INRANGE (1 << 5)
63 #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 6)
64 #define MT_QUIRK_CONFIDENCE (1 << 7)
65 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE (1 << 8)
66 #define MT_QUIRK_NO_AREA (1 << 9)
67 #define MT_QUIRK_IGNORE_DUPLICATES (1 << 10)
68 #define MT_QUIRK_HOVERING (1 << 11)
69 #define MT_QUIRK_CONTACT_CNT_ACCURATE (1 << 12)
70 #define MT_QUIRK_FORCE_GET_FEATURE (1 << 13)
71 #define MT_QUIRK_FIX_CONST_CONTACT_ID (1 << 14)
72 #define MT_QUIRK_TOUCH_SIZE_SCALING (1 << 15)
73
74 #define MT_INPUTMODE_TOUCHSCREEN 0x02
75 #define MT_INPUTMODE_TOUCHPAD 0x03
76
77 #define MT_BUTTONTYPE_CLICKPAD 0
78
79 struct mt_slot {
80 __s32 x, y, cx, cy, p, w, h;
81 __s32 contactid; /* the device ContactID assigned to this slot */
82 bool touch_state; /* is the touch valid? */
83 bool inrange_state; /* is the finger in proximity of the sensor? */
84 bool confidence_state; /* is the touch made by a finger? */
85 };
86
87 struct mt_class {
88 __s32 name; /* MT_CLS */
89 __s32 quirks;
90 __s32 sn_move; /* Signal/noise ratio for move events */
91 __s32 sn_width; /* Signal/noise ratio for width events */
92 __s32 sn_height; /* Signal/noise ratio for height events */
93 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
94 __u8 maxcontacts;
95 bool is_indirect; /* true for touchpads */
96 bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
97 };
98
99 struct mt_fields {
100 unsigned usages[HID_MAX_FIELDS];
101 unsigned int length;
102 };
103
104 struct mt_device {
105 struct mt_slot curdata; /* placeholder of incoming data */
106 struct mt_class mtclass; /* our mt device class */
107 struct mt_fields *fields; /* temporary placeholder for storing the
108 multitouch fields */
109 int cc_index; /* contact count field index in the report */
110 int cc_value_index; /* contact count value index in the field */
111 unsigned last_slot_field; /* the last field of a slot */
112 unsigned mt_report_id; /* the report ID of the multitouch device */
113 unsigned long initial_quirks; /* initial quirks state */
114 __s16 inputmode; /* InputMode HID feature, -1 if non-existent */
115 __s16 inputmode_index; /* InputMode HID feature index in the report */
116 __s16 maxcontact_report_id; /* Maximum Contact Number HID feature,
117 -1 if non-existent */
118 __u8 inputmode_value; /* InputMode HID feature value */
119 __u8 num_received; /* how many contacts we received */
120 __u8 num_expected; /* expected last contact index */
121 __u8 maxcontacts;
122 __u8 touches_by_report; /* how many touches are present in one report:
123 * 1 means we should use a serial protocol
124 * > 1 means hybrid (multitouch) protocol */
125 __u8 buttons_count; /* number of physical buttons per touchpad */
126 bool is_buttonpad; /* is this device a button pad? */
127 bool serial_maybe; /* need to check for serial protocol */
128 bool curvalid; /* is the current contact valid? */
129 unsigned mt_flags; /* flags to pass to input-mt */
130 };
131
132 static void mt_post_parse_default_settings(struct mt_device *td);
133 static void mt_post_parse(struct mt_device *td);
134
135 /* classes of device behavior */
136 #define MT_CLS_DEFAULT 0x0001
137
138 #define MT_CLS_SERIAL 0x0002
139 #define MT_CLS_CONFIDENCE 0x0003
140 #define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
141 #define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
142 #define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
143 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
144 /* reserved 0x0008 */
145 #define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
146 #define MT_CLS_NSMU 0x000a
147 /* reserved 0x0010 */
148 /* reserved 0x0011 */
149 #define MT_CLS_WIN_8 0x0012
150 #define MT_CLS_EXPORT_ALL_INPUTS 0x0013
151 #define MT_CLS_WIN_8_DUAL 0x0014
152
153 /* vendor specific classes */
154 #define MT_CLS_3M 0x0101
155 /* reserved 0x0102 */
156 #define MT_CLS_EGALAX 0x0103
157 #define MT_CLS_EGALAX_SERIAL 0x0104
158 #define MT_CLS_TOPSEED 0x0105
159 #define MT_CLS_PANASONIC 0x0106
160 #define MT_CLS_FLATFROG 0x0107
161 #define MT_CLS_GENERALTOUCH_TWOFINGERS 0x0108
162 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS 0x0109
163 #define MT_CLS_LG 0x010a
164 #define MT_CLS_VTL 0x0110
165
166 #define MT_DEFAULT_MAXCONTACT 10
167 #define MT_MAX_MAXCONTACT 250
168
169 #define MT_USB_DEVICE(v, p) HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
170 #define MT_BT_DEVICE(v, p) HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
171
172 /*
173 * these device-dependent functions determine what slot corresponds
174 * to a valid contact that was just read.
175 */
176
177 static int cypress_compute_slot(struct mt_device *td)
178 {
179 if (td->curdata.contactid != 0 || td->num_received == 0)
180 return td->curdata.contactid;
181 else
182 return -1;
183 }
184
185 static struct mt_class mt_classes[] = {
186 { .name = MT_CLS_DEFAULT,
187 .quirks = MT_QUIRK_ALWAYS_VALID |
188 MT_QUIRK_CONTACT_CNT_ACCURATE },
189 { .name = MT_CLS_NSMU,
190 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
191 { .name = MT_CLS_SERIAL,
192 .quirks = MT_QUIRK_ALWAYS_VALID},
193 { .name = MT_CLS_CONFIDENCE,
194 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
195 { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
196 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
197 MT_QUIRK_SLOT_IS_CONTACTID },
198 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
199 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
200 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
201 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
202 .quirks = MT_QUIRK_VALID_IS_INRANGE |
203 MT_QUIRK_SLOT_IS_CONTACTID,
204 .maxcontacts = 2 },
205 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
206 .quirks = MT_QUIRK_VALID_IS_INRANGE |
207 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
208 .maxcontacts = 2 },
209 { .name = MT_CLS_INRANGE_CONTACTNUMBER,
210 .quirks = MT_QUIRK_VALID_IS_INRANGE |
211 MT_QUIRK_SLOT_IS_CONTACTNUMBER },
212 { .name = MT_CLS_WIN_8,
213 .quirks = MT_QUIRK_ALWAYS_VALID |
214 MT_QUIRK_IGNORE_DUPLICATES |
215 MT_QUIRK_HOVERING |
216 MT_QUIRK_CONTACT_CNT_ACCURATE },
217 { .name = MT_CLS_EXPORT_ALL_INPUTS,
218 .quirks = MT_QUIRK_ALWAYS_VALID |
219 MT_QUIRK_CONTACT_CNT_ACCURATE,
220 .export_all_inputs = true },
221 { .name = MT_CLS_WIN_8_DUAL,
222 .quirks = MT_QUIRK_ALWAYS_VALID |
223 MT_QUIRK_IGNORE_DUPLICATES |
224 MT_QUIRK_HOVERING |
225 MT_QUIRK_CONTACT_CNT_ACCURATE,
226 .export_all_inputs = true },
227
228 /*
229 * vendor specific classes
230 */
231 { .name = MT_CLS_3M,
232 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
233 MT_QUIRK_SLOT_IS_CONTACTID |
234 MT_QUIRK_TOUCH_SIZE_SCALING,
235 .sn_move = 2048,
236 .sn_width = 128,
237 .sn_height = 128,
238 .maxcontacts = 60,
239 },
240 { .name = MT_CLS_EGALAX,
241 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
242 MT_QUIRK_VALID_IS_INRANGE,
243 .sn_move = 4096,
244 .sn_pressure = 32,
245 },
246 { .name = MT_CLS_EGALAX_SERIAL,
247 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
248 MT_QUIRK_ALWAYS_VALID,
249 .sn_move = 4096,
250 .sn_pressure = 32,
251 },
252 { .name = MT_CLS_TOPSEED,
253 .quirks = MT_QUIRK_ALWAYS_VALID,
254 .is_indirect = true,
255 .maxcontacts = 2,
256 },
257 { .name = MT_CLS_PANASONIC,
258 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
259 .maxcontacts = 4 },
260 { .name = MT_CLS_GENERALTOUCH_TWOFINGERS,
261 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
262 MT_QUIRK_VALID_IS_INRANGE |
263 MT_QUIRK_SLOT_IS_CONTACTID,
264 .maxcontacts = 2
265 },
266 { .name = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
267 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
268 MT_QUIRK_SLOT_IS_CONTACTID
269 },
270
271 { .name = MT_CLS_FLATFROG,
272 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
273 MT_QUIRK_NO_AREA,
274 .sn_move = 2048,
275 .maxcontacts = 40,
276 },
277 { .name = MT_CLS_LG,
278 .quirks = MT_QUIRK_ALWAYS_VALID |
279 MT_QUIRK_FIX_CONST_CONTACT_ID |
280 MT_QUIRK_IGNORE_DUPLICATES |
281 MT_QUIRK_HOVERING |
282 MT_QUIRK_CONTACT_CNT_ACCURATE },
283 { .name = MT_CLS_VTL,
284 .quirks = MT_QUIRK_ALWAYS_VALID |
285 MT_QUIRK_CONTACT_CNT_ACCURATE |
286 MT_QUIRK_FORCE_GET_FEATURE,
287 },
288 { }
289 };
290
291 static ssize_t mt_show_quirks(struct device *dev,
292 struct device_attribute *attr,
293 char *buf)
294 {
295 struct hid_device *hdev = to_hid_device(dev);
296 struct mt_device *td = hid_get_drvdata(hdev);
297
298 return sprintf(buf, "%u\n", td->mtclass.quirks);
299 }
300
301 static ssize_t mt_set_quirks(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304 {
305 struct hid_device *hdev = to_hid_device(dev);
306 struct mt_device *td = hid_get_drvdata(hdev);
307
308 unsigned long val;
309
310 if (kstrtoul(buf, 0, &val))
311 return -EINVAL;
312
313 td->mtclass.quirks = val;
314
315 if (td->cc_index < 0)
316 td->mtclass.quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
317
318 return count;
319 }
320
321 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
322
323 static struct attribute *sysfs_attrs[] = {
324 &dev_attr_quirks.attr,
325 NULL
326 };
327
328 static struct attribute_group mt_attribute_group = {
329 .attrs = sysfs_attrs
330 };
331
332 static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
333 {
334 struct mt_device *td = hid_get_drvdata(hdev);
335 int ret, size = hid_report_len(report);
336 u8 *buf;
337
338 /*
339 * Do not fetch the feature report if the device has been explicitly
340 * marked as non-capable.
341 */
342 if (td->initial_quirks & HID_QUIRK_NO_INIT_REPORTS)
343 return;
344
345 buf = hid_alloc_report_buf(report, GFP_KERNEL);
346 if (!buf)
347 return;
348
349 ret = hid_hw_raw_request(hdev, report->id, buf, size,
350 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
351 if (ret < 0) {
352 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
353 report->id);
354 } else {
355 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
356 size, 0);
357 if (ret)
358 dev_warn(&hdev->dev, "failed to report feature\n");
359 }
360
361 kfree(buf);
362 }
363
364 static void mt_feature_mapping(struct hid_device *hdev,
365 struct hid_field *field, struct hid_usage *usage)
366 {
367 struct mt_device *td = hid_get_drvdata(hdev);
368
369 switch (usage->hid) {
370 case HID_DG_INPUTMODE:
371 /* Ignore if value index is out of bounds. */
372 if (usage->usage_index >= field->report_count) {
373 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
374 break;
375 }
376
377 if (td->inputmode < 0) {
378 td->inputmode = field->report->id;
379 td->inputmode_index = usage->usage_index;
380 } else {
381 /*
382 * Some elan panels wrongly declare 2 input mode
383 * features, and silently ignore when we set the
384 * value in the second field. Skip the second feature
385 * and hope for the best.
386 */
387 dev_info(&hdev->dev,
388 "Ignoring the extra HID_DG_INPUTMODE\n");
389 }
390
391 break;
392 case HID_DG_CONTACTMAX:
393 mt_get_feature(hdev, field->report);
394
395 td->maxcontact_report_id = field->report->id;
396 td->maxcontacts = field->value[0];
397 if (!td->maxcontacts &&
398 field->logical_maximum <= MT_MAX_MAXCONTACT)
399 td->maxcontacts = field->logical_maximum;
400 if (td->mtclass.maxcontacts)
401 /* check if the maxcontacts is given by the class */
402 td->maxcontacts = td->mtclass.maxcontacts;
403
404 break;
405 case HID_DG_BUTTONTYPE:
406 if (usage->usage_index >= field->report_count) {
407 dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
408 break;
409 }
410
411 mt_get_feature(hdev, field->report);
412 if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
413 td->is_buttonpad = true;
414
415 break;
416 case 0xff0000c5:
417 /* Retrieve the Win8 blob once to enable some devices */
418 if (usage->usage_index == 0)
419 mt_get_feature(hdev, field->report);
420 break;
421 }
422 }
423
424 static void set_abs(struct input_dev *input, unsigned int code,
425 struct hid_field *field, int snratio)
426 {
427 int fmin = field->logical_minimum;
428 int fmax = field->logical_maximum;
429 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
430 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
431 input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
432 }
433
434 static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
435 struct hid_input *hi)
436 {
437 struct mt_fields *f = td->fields;
438
439 if (f->length >= HID_MAX_FIELDS)
440 return;
441
442 f->usages[f->length++] = usage->hid;
443 }
444
445 static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
446 struct hid_field *field, struct hid_usage *usage,
447 unsigned long **bit, int *max)
448 {
449 struct mt_device *td = hid_get_drvdata(hdev);
450 struct mt_class *cls = &td->mtclass;
451 int code;
452 struct hid_usage *prev_usage = NULL;
453
454 if (field->application == HID_DG_TOUCHSCREEN)
455 td->mt_flags |= INPUT_MT_DIRECT;
456
457 /*
458 * Model touchscreens providing buttons as touchpads.
459 */
460 if (field->application == HID_DG_TOUCHPAD ||
461 (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
462 td->mt_flags |= INPUT_MT_POINTER;
463 td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
464 }
465
466 /* count the buttons on touchpads */
467 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
468 td->buttons_count++;
469
470 if (usage->usage_index)
471 prev_usage = &field->usage[usage->usage_index - 1];
472
473 switch (usage->hid & HID_USAGE_PAGE) {
474
475 case HID_UP_GENDESK:
476 switch (usage->hid) {
477 case HID_GD_X:
478 if (prev_usage && (prev_usage->hid == usage->hid)) {
479 hid_map_usage(hi, usage, bit, max,
480 EV_ABS, ABS_MT_TOOL_X);
481 set_abs(hi->input, ABS_MT_TOOL_X, field,
482 cls->sn_move);
483 } else {
484 hid_map_usage(hi, usage, bit, max,
485 EV_ABS, ABS_MT_POSITION_X);
486 set_abs(hi->input, ABS_MT_POSITION_X, field,
487 cls->sn_move);
488 }
489
490 mt_store_field(usage, td, hi);
491 return 1;
492 case HID_GD_Y:
493 if (prev_usage && (prev_usage->hid == usage->hid)) {
494 hid_map_usage(hi, usage, bit, max,
495 EV_ABS, ABS_MT_TOOL_Y);
496 set_abs(hi->input, ABS_MT_TOOL_Y, field,
497 cls->sn_move);
498 } else {
499 hid_map_usage(hi, usage, bit, max,
500 EV_ABS, ABS_MT_POSITION_Y);
501 set_abs(hi->input, ABS_MT_POSITION_Y, field,
502 cls->sn_move);
503 }
504
505 mt_store_field(usage, td, hi);
506 return 1;
507 }
508 return 0;
509
510 case HID_UP_DIGITIZER:
511 switch (usage->hid) {
512 case HID_DG_INRANGE:
513 if (cls->quirks & MT_QUIRK_HOVERING) {
514 hid_map_usage(hi, usage, bit, max,
515 EV_ABS, ABS_MT_DISTANCE);
516 input_set_abs_params(hi->input,
517 ABS_MT_DISTANCE, 0, 1, 0, 0);
518 }
519 mt_store_field(usage, td, hi);
520 return 1;
521 case HID_DG_CONFIDENCE:
522 if ((cls->name == MT_CLS_WIN_8 ||
523 cls->name == MT_CLS_WIN_8_DUAL) &&
524 field->application == HID_DG_TOUCHPAD)
525 cls->quirks |= MT_QUIRK_CONFIDENCE;
526 mt_store_field(usage, td, hi);
527 return 1;
528 case HID_DG_TIPSWITCH:
529 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
530 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
531 mt_store_field(usage, td, hi);
532 return 1;
533 case HID_DG_CONTACTID:
534 mt_store_field(usage, td, hi);
535 td->touches_by_report++;
536 td->mt_report_id = field->report->id;
537 return 1;
538 case HID_DG_WIDTH:
539 hid_map_usage(hi, usage, bit, max,
540 EV_ABS, ABS_MT_TOUCH_MAJOR);
541 if (!(cls->quirks & MT_QUIRK_NO_AREA))
542 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
543 cls->sn_width);
544 mt_store_field(usage, td, hi);
545 return 1;
546 case HID_DG_HEIGHT:
547 hid_map_usage(hi, usage, bit, max,
548 EV_ABS, ABS_MT_TOUCH_MINOR);
549 if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
550 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
551 cls->sn_height);
552 input_set_abs_params(hi->input,
553 ABS_MT_ORIENTATION, 0, 1, 0, 0);
554 }
555 mt_store_field(usage, td, hi);
556 return 1;
557 case HID_DG_TIPPRESSURE:
558 hid_map_usage(hi, usage, bit, max,
559 EV_ABS, ABS_MT_PRESSURE);
560 set_abs(hi->input, ABS_MT_PRESSURE, field,
561 cls->sn_pressure);
562 mt_store_field(usage, td, hi);
563 return 1;
564 case HID_DG_CONTACTCOUNT:
565 /* Ignore if indexes are out of bounds. */
566 if (field->index >= field->report->maxfield ||
567 usage->usage_index >= field->report_count)
568 return 1;
569 td->cc_index = field->index;
570 td->cc_value_index = usage->usage_index;
571 return 1;
572 case HID_DG_CONTACTMAX:
573 /* we don't set td->last_slot_field as contactcount and
574 * contact max are global to the report */
575 return -1;
576 case HID_DG_TOUCH:
577 /* Legacy devices use TIPSWITCH and not TOUCH.
578 * Let's just ignore this field. */
579 return -1;
580 }
581 /* let hid-input decide for the others */
582 return 0;
583
584 case HID_UP_BUTTON:
585 code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
586 /*
587 * MS PTP spec says that external buttons left and right have
588 * usages 2 and 3.
589 */
590 if ((cls->name == MT_CLS_WIN_8 ||
591 cls->name == MT_CLS_WIN_8_DUAL) &&
592 field->application == HID_DG_TOUCHPAD &&
593 (usage->hid & HID_USAGE) > 1)
594 code--;
595 hid_map_usage(hi, usage, bit, max, EV_KEY, code);
596 input_set_capability(hi->input, EV_KEY, code);
597 return 1;
598
599 case 0xff000000:
600 /* we do not want to map these: no input-oriented meaning */
601 return -1;
602 }
603
604 return 0;
605 }
606
607 static int mt_touch_input_mapped(struct hid_device *hdev, struct hid_input *hi,
608 struct hid_field *field, struct hid_usage *usage,
609 unsigned long **bit, int *max)
610 {
611 if (usage->type == EV_KEY || usage->type == EV_ABS)
612 set_bit(usage->type, hi->input->evbit);
613
614 return -1;
615 }
616
617 static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
618 {
619 __s32 quirks = td->mtclass.quirks;
620
621 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
622 return td->curdata.contactid;
623
624 if (quirks & MT_QUIRK_CYPRESS)
625 return cypress_compute_slot(td);
626
627 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
628 return td->num_received;
629
630 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
631 return td->curdata.contactid - 1;
632
633 return input_mt_get_slot_by_key(input, td->curdata.contactid);
634 }
635
636 /*
637 * this function is called when a whole contact has been processed,
638 * so that it can assign it to a slot and store the data there
639 */
640 static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
641 {
642 if ((td->mtclass.quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
643 td->num_received >= td->num_expected)
644 return;
645
646 if (td->curvalid || (td->mtclass.quirks & MT_QUIRK_ALWAYS_VALID)) {
647 int active;
648 int slotnum = mt_compute_slot(td, input);
649 struct mt_slot *s = &td->curdata;
650 struct input_mt *mt = input->mt;
651
652 if (slotnum < 0 || slotnum >= td->maxcontacts)
653 return;
654
655 if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
656 struct input_mt_slot *slot = &mt->slots[slotnum];
657 if (input_mt_is_active(slot) &&
658 input_mt_is_used(mt, slot))
659 return;
660 }
661
662 if (!(td->mtclass.quirks & MT_QUIRK_CONFIDENCE))
663 s->confidence_state = 1;
664 active = (s->touch_state || s->inrange_state) &&
665 s->confidence_state;
666
667 input_mt_slot(input, slotnum);
668 input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
669 if (active) {
670 /* this finger is in proximity of the sensor */
671 int wide = (s->w > s->h);
672 int major = max(s->w, s->h);
673 int minor = min(s->w, s->h);
674
675 /*
676 * divided by two to match visual scale of touch
677 * for devices with this quirk
678 */
679 if (td->mtclass.quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
680 major = major >> 1;
681 minor = minor >> 1;
682 }
683
684 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
685 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
686 input_event(input, EV_ABS, ABS_MT_TOOL_X, s->cx);
687 input_event(input, EV_ABS, ABS_MT_TOOL_Y, s->cy);
688 input_event(input, EV_ABS, ABS_MT_DISTANCE,
689 !s->touch_state);
690 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
691 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
692 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
693 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
694 }
695 }
696
697 td->num_received++;
698 }
699
700 /*
701 * this function is called when a whole packet has been received and processed,
702 * so that it can decide what to send to the input layer.
703 */
704 static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
705 {
706 input_mt_sync_frame(input);
707 input_sync(input);
708 td->num_received = 0;
709 }
710
711 static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
712 struct hid_usage *usage, __s32 value)
713 {
714 /* we will handle the hidinput part later, now remains hiddev */
715 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
716 hid->hiddev_hid_event(hid, field, usage, value);
717
718 return 1;
719 }
720
721 static void mt_process_mt_event(struct hid_device *hid, struct hid_field *field,
722 struct hid_usage *usage, __s32 value)
723 {
724 struct mt_device *td = hid_get_drvdata(hid);
725 __s32 quirks = td->mtclass.quirks;
726 struct input_dev *input = field->hidinput->input;
727
728 if (hid->claimed & HID_CLAIMED_INPUT) {
729 switch (usage->hid) {
730 case HID_DG_INRANGE:
731 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
732 td->curvalid = value;
733 if (quirks & MT_QUIRK_HOVERING)
734 td->curdata.inrange_state = value;
735 break;
736 case HID_DG_TIPSWITCH:
737 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
738 td->curvalid = value;
739 td->curdata.touch_state = value;
740 break;
741 case HID_DG_CONFIDENCE:
742 if (quirks & MT_QUIRK_CONFIDENCE)
743 td->curdata.confidence_state = value;
744 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
745 td->curvalid = value;
746 break;
747 case HID_DG_CONTACTID:
748 td->curdata.contactid = value;
749 break;
750 case HID_DG_TIPPRESSURE:
751 td->curdata.p = value;
752 break;
753 case HID_GD_X:
754 if (usage->code == ABS_MT_TOOL_X)
755 td->curdata.cx = value;
756 else
757 td->curdata.x = value;
758 break;
759 case HID_GD_Y:
760 if (usage->code == ABS_MT_TOOL_Y)
761 td->curdata.cy = value;
762 else
763 td->curdata.y = value;
764 break;
765 case HID_DG_WIDTH:
766 td->curdata.w = value;
767 break;
768 case HID_DG_HEIGHT:
769 td->curdata.h = value;
770 break;
771 case HID_DG_CONTACTCOUNT:
772 break;
773 case HID_DG_TOUCH:
774 /* do nothing */
775 break;
776
777 default:
778 if (usage->type)
779 input_event(input, usage->type, usage->code,
780 value);
781 return;
782 }
783
784 if (usage->usage_index + 1 == field->report_count) {
785 /* we only take into account the last report. */
786 if (usage->hid == td->last_slot_field)
787 mt_complete_slot(td, field->hidinput->input);
788 }
789
790 }
791 }
792
793 static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
794 {
795 struct mt_device *td = hid_get_drvdata(hid);
796 struct hid_field *field;
797 unsigned count;
798 int r, n;
799
800 /*
801 * Includes multi-packet support where subsequent
802 * packets are sent with zero contactcount.
803 */
804 if (td->cc_index >= 0) {
805 struct hid_field *field = report->field[td->cc_index];
806 int value = field->value[td->cc_value_index];
807 if (value)
808 td->num_expected = value;
809 }
810
811 for (r = 0; r < report->maxfield; r++) {
812 field = report->field[r];
813 count = field->report_count;
814
815 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
816 continue;
817
818 for (n = 0; n < count; n++)
819 mt_process_mt_event(hid, field, &field->usage[n],
820 field->value[n]);
821 }
822
823 if (td->num_received >= td->num_expected)
824 mt_sync_frame(td, report->field[0]->hidinput->input);
825 }
826
827 static int mt_touch_input_configured(struct hid_device *hdev,
828 struct hid_input *hi)
829 {
830 struct mt_device *td = hid_get_drvdata(hdev);
831 struct mt_class *cls = &td->mtclass;
832 struct input_dev *input = hi->input;
833 int ret;
834
835 if (!td->maxcontacts)
836 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
837
838 mt_post_parse(td);
839 if (td->serial_maybe)
840 mt_post_parse_default_settings(td);
841
842 if (cls->is_indirect)
843 td->mt_flags |= INPUT_MT_POINTER;
844
845 if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
846 td->mt_flags |= INPUT_MT_DROP_UNUSED;
847
848 /* check for clickpads */
849 if ((td->mt_flags & INPUT_MT_POINTER) && (td->buttons_count == 1))
850 td->is_buttonpad = true;
851
852 if (td->is_buttonpad)
853 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
854
855 ret = input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
856 if (ret)
857 return ret;
858
859 td->mt_flags = 0;
860 return 0;
861 }
862
863 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
864 struct hid_field *field, struct hid_usage *usage,
865 unsigned long **bit, int *max)
866 {
867 struct mt_device *td = hid_get_drvdata(hdev);
868
869 /*
870 * If mtclass.export_all_inputs is not set, only map fields from
871 * TouchScreen or TouchPad collections. We need to ignore fields
872 * that belong to other collections such as Mouse that might have
873 * the same GenericDesktop usages.
874 */
875 if (!td->mtclass.export_all_inputs &&
876 field->application != HID_DG_TOUCHSCREEN &&
877 field->application != HID_DG_PEN &&
878 field->application != HID_DG_TOUCHPAD &&
879 field->application != HID_GD_KEYBOARD &&
880 field->application != HID_CP_CONSUMER_CONTROL)
881 return -1;
882
883 /*
884 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
885 * for the stylus.
886 * The check for mt_report_id ensures we don't process
887 * HID_DG_CONTACTCOUNT from the pen report as it is outside the physical
888 * collection, but within the report ID.
889 */
890 if (field->physical == HID_DG_STYLUS)
891 return 0;
892 else if ((field->physical == 0) &&
893 (field->report->id != td->mt_report_id) &&
894 (td->mt_report_id != -1))
895 return 0;
896
897 if (field->application == HID_DG_TOUCHSCREEN ||
898 field->application == HID_DG_TOUCHPAD)
899 return mt_touch_input_mapping(hdev, hi, field, usage, bit, max);
900
901 /* let hid-core decide for the others */
902 return 0;
903 }
904
905 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
906 struct hid_field *field, struct hid_usage *usage,
907 unsigned long **bit, int *max)
908 {
909 /*
910 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
911 * for the stylus.
912 */
913 if (field->physical == HID_DG_STYLUS)
914 return 0;
915
916 if (field->application == HID_DG_TOUCHSCREEN ||
917 field->application == HID_DG_TOUCHPAD)
918 return mt_touch_input_mapped(hdev, hi, field, usage, bit, max);
919
920 /* let hid-core decide for the others */
921 return 0;
922 }
923
924 static int mt_event(struct hid_device *hid, struct hid_field *field,
925 struct hid_usage *usage, __s32 value)
926 {
927 struct mt_device *td = hid_get_drvdata(hid);
928
929 if (field->report->id == td->mt_report_id)
930 return mt_touch_event(hid, field, usage, value);
931
932 return 0;
933 }
934
935 static void mt_report(struct hid_device *hid, struct hid_report *report)
936 {
937 struct mt_device *td = hid_get_drvdata(hid);
938 struct hid_field *field = report->field[0];
939
940 if (!(hid->claimed & HID_CLAIMED_INPUT))
941 return;
942
943 if (report->id == td->mt_report_id)
944 return mt_touch_report(hid, report);
945
946 if (field && field->hidinput && field->hidinput->input)
947 input_sync(field->hidinput->input);
948 }
949
950 static void mt_set_input_mode(struct hid_device *hdev)
951 {
952 struct mt_device *td = hid_get_drvdata(hdev);
953 struct hid_report *r;
954 struct hid_report_enum *re;
955 struct mt_class *cls = &td->mtclass;
956 char *buf;
957 int report_len;
958
959 if (td->inputmode < 0)
960 return;
961
962 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
963 r = re->report_id_hash[td->inputmode];
964 if (r) {
965 if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
966 report_len = hid_report_len(r);
967 buf = hid_alloc_report_buf(r, GFP_KERNEL);
968 if (!buf) {
969 hid_err(hdev, "failed to allocate buffer for report\n");
970 return;
971 }
972 hid_hw_raw_request(hdev, r->id, buf, report_len,
973 HID_FEATURE_REPORT,
974 HID_REQ_GET_REPORT);
975 kfree(buf);
976 }
977 r->field[0]->value[td->inputmode_index] = td->inputmode_value;
978 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
979 }
980 }
981
982 static void mt_set_maxcontacts(struct hid_device *hdev)
983 {
984 struct mt_device *td = hid_get_drvdata(hdev);
985 struct hid_report *r;
986 struct hid_report_enum *re;
987 int fieldmax, max;
988
989 if (td->maxcontact_report_id < 0)
990 return;
991
992 if (!td->mtclass.maxcontacts)
993 return;
994
995 re = &hdev->report_enum[HID_FEATURE_REPORT];
996 r = re->report_id_hash[td->maxcontact_report_id];
997 if (r) {
998 max = td->mtclass.maxcontacts;
999 fieldmax = r->field[0]->logical_maximum;
1000 max = min(fieldmax, max);
1001 if (r->field[0]->value[0] != max) {
1002 r->field[0]->value[0] = max;
1003 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
1004 }
1005 }
1006 }
1007
1008 static void mt_post_parse_default_settings(struct mt_device *td)
1009 {
1010 __s32 quirks = td->mtclass.quirks;
1011
1012 /* unknown serial device needs special quirks */
1013 if (td->touches_by_report == 1) {
1014 quirks |= MT_QUIRK_ALWAYS_VALID;
1015 quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1016 quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1017 quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1018 quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1019 }
1020
1021 td->mtclass.quirks = quirks;
1022 }
1023
1024 static void mt_post_parse(struct mt_device *td)
1025 {
1026 struct mt_fields *f = td->fields;
1027 struct mt_class *cls = &td->mtclass;
1028
1029 if (td->touches_by_report > 0) {
1030 int field_count_per_touch = f->length / td->touches_by_report;
1031 td->last_slot_field = f->usages[field_count_per_touch - 1];
1032 }
1033
1034 if (td->cc_index < 0)
1035 cls->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1036 }
1037
1038 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1039 {
1040 struct mt_device *td = hid_get_drvdata(hdev);
1041 char *name;
1042 const char *suffix = NULL;
1043 struct hid_field *field = hi->report->field[0];
1044 int ret;
1045
1046 if (hi->report->id == td->mt_report_id) {
1047 ret = mt_touch_input_configured(hdev, hi);
1048 if (ret)
1049 return ret;
1050 }
1051
1052 /*
1053 * some egalax touchscreens have "application == HID_DG_TOUCHSCREEN"
1054 * for the stylus. Check this first, and then rely on the application
1055 * field.
1056 */
1057 if (hi->report->field[0]->physical == HID_DG_STYLUS) {
1058 suffix = "Pen";
1059 /* force BTN_STYLUS to allow tablet matching in udev */
1060 __set_bit(BTN_STYLUS, hi->input->keybit);
1061 } else {
1062 switch (field->application) {
1063 case HID_GD_KEYBOARD:
1064 suffix = "Keyboard";
1065 break;
1066 case HID_GD_KEYPAD:
1067 suffix = "Keypad";
1068 break;
1069 case HID_GD_MOUSE:
1070 suffix = "Mouse";
1071 break;
1072 case HID_DG_STYLUS:
1073 suffix = "Pen";
1074 /* force BTN_STYLUS to allow tablet matching in udev */
1075 __set_bit(BTN_STYLUS, hi->input->keybit);
1076 break;
1077 case HID_DG_TOUCHSCREEN:
1078 /* we do not set suffix = "Touchscreen" */
1079 break;
1080 case HID_DG_TOUCHPAD:
1081 suffix = "Touchpad";
1082 break;
1083 case HID_GD_SYSTEM_CONTROL:
1084 suffix = "System Control";
1085 break;
1086 case HID_CP_CONSUMER_CONTROL:
1087 suffix = "Consumer Control";
1088 break;
1089 default:
1090 suffix = "UNKNOWN";
1091 break;
1092 }
1093 }
1094
1095 if (suffix) {
1096 name = devm_kzalloc(&hi->input->dev,
1097 strlen(hdev->name) + strlen(suffix) + 2,
1098 GFP_KERNEL);
1099 if (name) {
1100 sprintf(name, "%s %s", hdev->name, suffix);
1101 hi->input->name = name;
1102 }
1103 }
1104
1105 return 0;
1106 }
1107
1108 static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
1109 {
1110 if (field->usage[0].hid != usage ||
1111 !(field->flags & HID_MAIN_ITEM_CONSTANT))
1112 return;
1113
1114 field->flags &= ~HID_MAIN_ITEM_CONSTANT;
1115 field->flags |= HID_MAIN_ITEM_VARIABLE;
1116 }
1117
1118 static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
1119 {
1120 struct hid_report *report;
1121 int i;
1122
1123 list_for_each_entry(report,
1124 &hdev->report_enum[HID_INPUT_REPORT].report_list,
1125 list) {
1126
1127 if (!report->maxfield)
1128 continue;
1129
1130 for (i = 0; i < report->maxfield; i++)
1131 if (report->field[i]->maxusage >= 1)
1132 mt_fix_const_field(report->field[i], usage);
1133 }
1134 }
1135
1136 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1137 {
1138 int ret, i;
1139 struct mt_device *td;
1140 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1141
1142 for (i = 0; mt_classes[i].name ; i++) {
1143 if (id->driver_data == mt_classes[i].name) {
1144 mtclass = &(mt_classes[i]);
1145 break;
1146 }
1147 }
1148
1149 td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1150 if (!td) {
1151 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1152 return -ENOMEM;
1153 }
1154 td->mtclass = *mtclass;
1155 td->inputmode = -1;
1156 td->maxcontact_report_id = -1;
1157 td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1158 td->cc_index = -1;
1159 td->mt_report_id = -1;
1160 hid_set_drvdata(hdev, td);
1161
1162 td->fields = devm_kzalloc(&hdev->dev, sizeof(struct mt_fields),
1163 GFP_KERNEL);
1164 if (!td->fields) {
1165 dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
1166 return -ENOMEM;
1167 }
1168
1169 if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1170 td->serial_maybe = true;
1171
1172 /*
1173 * Store the initial quirk state
1174 */
1175 td->initial_quirks = hdev->quirks;
1176
1177 /* This allows the driver to correctly support devices
1178 * that emit events over several HID messages.
1179 */
1180 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1181
1182 /*
1183 * This allows the driver to handle different input sensors
1184 * that emits events through different reports on the same HID
1185 * device.
1186 */
1187 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1188 hdev->quirks |= HID_QUIRK_NO_EMPTY_INPUT;
1189
1190 /*
1191 * Some multitouch screens do not like to be polled for input
1192 * reports. Fortunately, the Win8 spec says that all touches
1193 * should be sent during each report, making the initialization
1194 * of input reports unnecessary. For Win7 devices, well, let's hope
1195 * they will still be happy (this is only be a problem if a touch
1196 * was already there while probing the device).
1197 *
1198 * In addition some touchpads do not behave well if we read
1199 * all feature reports from them. Instead we prevent
1200 * initial report fetching and then selectively fetch each
1201 * report we are interested in.
1202 */
1203 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1204
1205 ret = hid_parse(hdev);
1206 if (ret != 0)
1207 return ret;
1208
1209 if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
1210 mt_fix_const_fields(hdev, HID_DG_CONTACTID);
1211
1212 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1213 if (ret)
1214 return ret;
1215
1216 ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
1217 if (ret)
1218 dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
1219 hdev->name);
1220
1221 mt_set_maxcontacts(hdev);
1222 mt_set_input_mode(hdev);
1223
1224 /* release .fields memory as it is not used anymore */
1225 devm_kfree(&hdev->dev, td->fields);
1226 td->fields = NULL;
1227
1228 return 0;
1229 }
1230
1231 #ifdef CONFIG_PM
1232 static void mt_release_contacts(struct hid_device *hid)
1233 {
1234 struct hid_input *hidinput;
1235
1236 list_for_each_entry(hidinput, &hid->inputs, list) {
1237 struct input_dev *input_dev = hidinput->input;
1238 struct input_mt *mt = input_dev->mt;
1239 int i;
1240
1241 if (mt) {
1242 for (i = 0; i < mt->num_slots; i++) {
1243 input_mt_slot(input_dev, i);
1244 input_mt_report_slot_state(input_dev,
1245 MT_TOOL_FINGER,
1246 false);
1247 }
1248 input_mt_sync_frame(input_dev);
1249 input_sync(input_dev);
1250 }
1251 }
1252 }
1253
1254 static int mt_reset_resume(struct hid_device *hdev)
1255 {
1256 mt_release_contacts(hdev);
1257 mt_set_maxcontacts(hdev);
1258 mt_set_input_mode(hdev);
1259 return 0;
1260 }
1261
1262 static int mt_resume(struct hid_device *hdev)
1263 {
1264 /* Some Elan legacy devices require SET_IDLE to be set on resume.
1265 * It should be safe to send it to other devices too.
1266 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
1267
1268 hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
1269
1270 return 0;
1271 }
1272 #endif
1273
1274 static void mt_remove(struct hid_device *hdev)
1275 {
1276 struct mt_device *td = hid_get_drvdata(hdev);
1277
1278 sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
1279 hid_hw_stop(hdev);
1280 hdev->quirks = td->initial_quirks;
1281 }
1282
1283 /*
1284 * This list contains only:
1285 * - VID/PID of products not working with the default multitouch handling
1286 * - 2 generic rules.
1287 * So there is no point in adding here any device with MT_CLS_DEFAULT.
1288 */
1289 static const struct hid_device_id mt_devices[] = {
1290
1291 /* 3M panels */
1292 { .driver_data = MT_CLS_3M,
1293 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1294 USB_DEVICE_ID_3M1968) },
1295 { .driver_data = MT_CLS_3M,
1296 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1297 USB_DEVICE_ID_3M2256) },
1298 { .driver_data = MT_CLS_3M,
1299 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1300 USB_DEVICE_ID_3M3266) },
1301
1302 /* Alps devices */
1303 { .driver_data = MT_CLS_WIN_8_DUAL,
1304 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1305 USB_VENDOR_ID_ALPS_JP,
1306 HID_DEVICE_ID_ALPS_U1_DUAL_PTP) },
1307 { .driver_data = MT_CLS_WIN_8_DUAL,
1308 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1309 USB_VENDOR_ID_ALPS_JP,
1310 HID_DEVICE_ID_ALPS_U1_DUAL_3BTN_PTP) },
1311
1312 /* Anton devices */
1313 { .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
1314 MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
1315 USB_DEVICE_ID_ANTON_TOUCH_PAD) },
1316
1317 /* Atmel panels */
1318 { .driver_data = MT_CLS_SERIAL,
1319 MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
1320 USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
1321
1322 /* Baanto multitouch devices */
1323 { .driver_data = MT_CLS_NSMU,
1324 MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
1325 USB_DEVICE_ID_BAANTO_MT_190W2) },
1326
1327 /* Cando panels */
1328 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1329 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1330 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1331 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1332 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1333 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
1334
1335 /* Chunghwa Telecom touch panels */
1336 { .driver_data = MT_CLS_NSMU,
1337 MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
1338 USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
1339
1340 /* CJTouch panels */
1341 { .driver_data = MT_CLS_NSMU,
1342 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1343 USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
1344 { .driver_data = MT_CLS_NSMU,
1345 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1346 USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
1347
1348 /* CVTouch panels */
1349 { .driver_data = MT_CLS_NSMU,
1350 MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
1351 USB_DEVICE_ID_CVTOUCH_SCREEN) },
1352
1353 /* eGalax devices (resistive) */
1354 { .driver_data = MT_CLS_EGALAX,
1355 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1356 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
1357 { .driver_data = MT_CLS_EGALAX,
1358 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1359 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
1360
1361 /* eGalax devices (capacitive) */
1362 { .driver_data = MT_CLS_EGALAX_SERIAL,
1363 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1364 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
1365 { .driver_data = MT_CLS_EGALAX,
1366 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1367 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
1368 { .driver_data = MT_CLS_EGALAX_SERIAL,
1369 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1370 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
1371 { .driver_data = MT_CLS_EGALAX_SERIAL,
1372 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1373 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
1374 { .driver_data = MT_CLS_EGALAX_SERIAL,
1375 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1376 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
1377 { .driver_data = MT_CLS_EGALAX_SERIAL,
1378 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1379 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
1380 { .driver_data = MT_CLS_EGALAX,
1381 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1382 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
1383 { .driver_data = MT_CLS_EGALAX,
1384 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1385 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
1386 { .driver_data = MT_CLS_EGALAX_SERIAL,
1387 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1388 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
1389 { .driver_data = MT_CLS_EGALAX,
1390 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1391 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
1392 { .driver_data = MT_CLS_EGALAX,
1393 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1394 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
1395 { .driver_data = MT_CLS_EGALAX,
1396 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1397 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
1398 { .driver_data = MT_CLS_EGALAX,
1399 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1400 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
1401 { .driver_data = MT_CLS_EGALAX_SERIAL,
1402 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1403 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
1404 { .driver_data = MT_CLS_EGALAX_SERIAL,
1405 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1406 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
1407 { .driver_data = MT_CLS_EGALAX_SERIAL,
1408 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1409 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
1410
1411 /* Elitegroup panel */
1412 { .driver_data = MT_CLS_SERIAL,
1413 MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
1414 USB_DEVICE_ID_ELITEGROUP_05D8) },
1415
1416 /* Flatfrog Panels */
1417 { .driver_data = MT_CLS_FLATFROG,
1418 MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
1419 USB_DEVICE_ID_MULTITOUCH_3200) },
1420
1421 /* FocalTech Panels */
1422 { .driver_data = MT_CLS_SERIAL,
1423 MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
1424 USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
1425
1426 /* GeneralTouch panel */
1427 { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1428 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1429 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
1430 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1431 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1432 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
1433 { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1434 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1435 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
1436 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1437 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1438 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
1439 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1440 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1441 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
1442 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1443 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1444 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
1445 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1446 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1447 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
1448
1449 /* Gametel game controller */
1450 { .driver_data = MT_CLS_NSMU,
1451 MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
1452 USB_DEVICE_ID_GAMETEL_MT_MODE) },
1453
1454 /* GoodTouch panels */
1455 { .driver_data = MT_CLS_NSMU,
1456 MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
1457 USB_DEVICE_ID_GOODTOUCH_000f) },
1458
1459 /* Hanvon panels */
1460 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1461 MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
1462 USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
1463
1464 /* Ilitek dual touch panel */
1465 { .driver_data = MT_CLS_NSMU,
1466 MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
1467 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
1468
1469 /* LG Melfas panel */
1470 { .driver_data = MT_CLS_LG,
1471 HID_USB_DEVICE(USB_VENDOR_ID_LG,
1472 USB_DEVICE_ID_LG_MELFAS_MT) },
1473
1474 /* MosArt panels */
1475 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1476 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1477 USB_DEVICE_ID_ASUS_T91MT)},
1478 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1479 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
1480 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
1481 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1482 MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
1483 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
1484
1485 /* Panasonic panels */
1486 { .driver_data = MT_CLS_PANASONIC,
1487 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1488 USB_DEVICE_ID_PANABOARD_UBT780) },
1489 { .driver_data = MT_CLS_PANASONIC,
1490 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
1491 USB_DEVICE_ID_PANABOARD_UBT880) },
1492
1493 /* Novatek Panel */
1494 { .driver_data = MT_CLS_NSMU,
1495 MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
1496 USB_DEVICE_ID_NOVATEK_PCT) },
1497
1498 /* Ntrig Panel */
1499 { .driver_data = MT_CLS_NSMU,
1500 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1501 USB_VENDOR_ID_NTRIG, 0x1b05) },
1502
1503 /* PixArt optical touch screen */
1504 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1505 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1506 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
1507 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1508 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1509 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
1510 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1511 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
1512 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
1513
1514 /* PixCir-based panels */
1515 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1516 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1517 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
1518
1519 /* Quanta-based panels */
1520 { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1521 MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
1522 USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
1523
1524 /* Stantum panels */
1525 { .driver_data = MT_CLS_CONFIDENCE,
1526 MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
1527 USB_DEVICE_ID_MTP_STM)},
1528
1529 /* TopSeed panels */
1530 { .driver_data = MT_CLS_TOPSEED,
1531 MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
1532 USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
1533
1534 /* Touch International panels */
1535 { .driver_data = MT_CLS_NSMU,
1536 MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
1537 USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
1538
1539 /* Unitec panels */
1540 { .driver_data = MT_CLS_NSMU,
1541 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1542 USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
1543 { .driver_data = MT_CLS_NSMU,
1544 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1545 USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
1546
1547 /* VTL panels */
1548 { .driver_data = MT_CLS_VTL,
1549 MT_USB_DEVICE(USB_VENDOR_ID_VTL,
1550 USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
1551
1552 /* Wistron panels */
1553 { .driver_data = MT_CLS_NSMU,
1554 MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
1555 USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
1556
1557 /* XAT */
1558 { .driver_data = MT_CLS_NSMU,
1559 MT_USB_DEVICE(USB_VENDOR_ID_XAT,
1560 USB_DEVICE_ID_XAT_CSR) },
1561
1562 /* Xiroku */
1563 { .driver_data = MT_CLS_NSMU,
1564 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1565 USB_DEVICE_ID_XIROKU_SPX) },
1566 { .driver_data = MT_CLS_NSMU,
1567 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1568 USB_DEVICE_ID_XIROKU_MPX) },
1569 { .driver_data = MT_CLS_NSMU,
1570 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1571 USB_DEVICE_ID_XIROKU_CSR) },
1572 { .driver_data = MT_CLS_NSMU,
1573 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1574 USB_DEVICE_ID_XIROKU_SPX1) },
1575 { .driver_data = MT_CLS_NSMU,
1576 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1577 USB_DEVICE_ID_XIROKU_MPX1) },
1578 { .driver_data = MT_CLS_NSMU,
1579 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1580 USB_DEVICE_ID_XIROKU_CSR1) },
1581 { .driver_data = MT_CLS_NSMU,
1582 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1583 USB_DEVICE_ID_XIROKU_SPX2) },
1584 { .driver_data = MT_CLS_NSMU,
1585 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1586 USB_DEVICE_ID_XIROKU_MPX2) },
1587 { .driver_data = MT_CLS_NSMU,
1588 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1589 USB_DEVICE_ID_XIROKU_CSR2) },
1590
1591 /* Generic MT device */
1592 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
1593
1594 /* Generic Win 8 certified MT device */
1595 { .driver_data = MT_CLS_WIN_8,
1596 HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
1597 HID_ANY_ID, HID_ANY_ID) },
1598 { }
1599 };
1600 MODULE_DEVICE_TABLE(hid, mt_devices);
1601
1602 static const struct hid_usage_id mt_grabbed_usages[] = {
1603 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1604 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
1605 };
1606
1607 static struct hid_driver mt_driver = {
1608 .name = "hid-multitouch",
1609 .id_table = mt_devices,
1610 .probe = mt_probe,
1611 .remove = mt_remove,
1612 .input_mapping = mt_input_mapping,
1613 .input_mapped = mt_input_mapped,
1614 .input_configured = mt_input_configured,
1615 .feature_mapping = mt_feature_mapping,
1616 .usage_table = mt_grabbed_usages,
1617 .event = mt_event,
1618 .report = mt_report,
1619 #ifdef CONFIG_PM
1620 .reset_resume = mt_reset_resume,
1621 .resume = mt_resume,
1622 #endif
1623 };
1624 module_hid_driver(mt_driver);