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 test suite in hid-tools[1].
32 * Please run these regression tests before patching this module so that
33 * your patch won't break existing known devices.
34 *
35 * [1] https://gitlab.freedesktop.org/libevdev/hid-tools
36 */
37
38 #include <linux/device.h>
39 #include <linux/hid.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/input/mt.h>
43 #include <linux/jiffies.h>
44 #include <linux/string.h>
45 #include <linux/timer.h>
46
47
48 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
49 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
50 MODULE_DESCRIPTION("HID multitouch panels");
51 MODULE_LICENSE("GPL");
52
53 #include "hid-ids.h"
54
55 /* quirks to control the device */
56 #define MT_QUIRK_NOT_SEEN_MEANS_UP BIT(0)
57 #define MT_QUIRK_SLOT_IS_CONTACTID BIT(1)
58 #define MT_QUIRK_CYPRESS BIT(2)
59 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER BIT(3)
60 #define MT_QUIRK_ALWAYS_VALID BIT(4)
61 #define MT_QUIRK_VALID_IS_INRANGE BIT(5)
62 #define MT_QUIRK_VALID_IS_CONFIDENCE BIT(6)
63 #define MT_QUIRK_CONFIDENCE BIT(7)
64 #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE BIT(8)
65 #define MT_QUIRK_NO_AREA BIT(9)
66 #define MT_QUIRK_IGNORE_DUPLICATES BIT(10)
67 #define MT_QUIRK_HOVERING BIT(11)
68 #define MT_QUIRK_CONTACT_CNT_ACCURATE BIT(12)
69 #define MT_QUIRK_FORCE_GET_FEATURE BIT(13)
70 #define MT_QUIRK_FIX_CONST_CONTACT_ID BIT(14)
71 #define MT_QUIRK_TOUCH_SIZE_SCALING BIT(15)
72 #define MT_QUIRK_STICKY_FINGERS BIT(16)
73 #define MT_QUIRK_ASUS_CUSTOM_UP BIT(17)
74 #define MT_QUIRK_WIN8_PTP_BUTTONS BIT(18)
75 #define MT_QUIRK_SEPARATE_APP_REPORT BIT(19)
76
77 #define MT_INPUTMODE_TOUCHSCREEN 0x02
78 #define MT_INPUTMODE_TOUCHPAD 0x03
79
80 #define MT_BUTTONTYPE_CLICKPAD 0
81
82 enum latency_mode {
83 HID_LATENCY_NORMAL = 0,
84 HID_LATENCY_HIGH = 1,
85 };
86
87 #define MT_IO_FLAGS_RUNNING 0
88 #define MT_IO_FLAGS_ACTIVE_SLOTS 1
89 #define MT_IO_FLAGS_PENDING_SLOTS 2
90
91 static const bool mtrue = true; /* default for true */
92 static const bool mfalse; /* default for false */
93 static const __s32 mzero; /* default for 0 */
94
95 #define DEFAULT_TRUE ((void *)&mtrue)
96 #define DEFAULT_FALSE ((void *)&mfalse)
97 #define DEFAULT_ZERO ((void *)&mzero)
98
99 struct mt_usages {
100 struct list_head list;
101 __s32 *x, *y, *cx, *cy, *p, *w, *h, *a;
102 __s32 *contactid; /* the device ContactID assigned to this slot */
103 bool *tip_state; /* is the touch valid? */
104 bool *inrange_state; /* is the finger in proximity of the sensor? */
105 bool *confidence_state; /* is the touch made by a finger? */
106 };
107
108 struct mt_application {
109 struct list_head list;
110 unsigned int application;
111 unsigned int report_id;
112 struct list_head mt_usages; /* mt usages list */
113
114 __s32 quirks;
115
116 __s32 *scantime; /* scantime reported */
117 __s32 scantime_logical_max; /* max value for raw scantime */
118
119 __s32 *raw_cc; /* contact count in the report */
120 int left_button_state; /* left button state */
121 unsigned int mt_flags; /* flags to pass to input-mt */
122
123 unsigned long *pending_palm_slots; /* slots where we reported palm
124 * and need to release */
125
126 __u8 num_received; /* how many contacts we received */
127 __u8 num_expected; /* expected last contact index */
128 __u8 buttons_count; /* number of physical buttons per touchpad */
129 __u8 touches_by_report; /* how many touches are present in one report:
130 * 1 means we should use a serial protocol
131 * > 1 means hybrid (multitouch) protocol
132 */
133
134 __s32 dev_time; /* the scan time provided by the device */
135 unsigned long jiffies; /* the frame's jiffies */
136 int timestamp; /* the timestamp to be sent */
137 int prev_scantime; /* scantime reported previously */
138
139 bool have_contact_count;
140 };
141
142 struct mt_class {
143 __s32 name; /* MT_CLS */
144 __s32 quirks;
145 __s32 sn_move; /* Signal/noise ratio for move events */
146 __s32 sn_width; /* Signal/noise ratio for width events */
147 __s32 sn_height; /* Signal/noise ratio for height events */
148 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
149 __u8 maxcontacts;
150 bool is_indirect; /* true for touchpads */
151 bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
152 };
153
154 struct mt_report_data {
155 struct list_head list;
156 struct hid_report *report;
157 struct mt_application *application;
158 bool is_mt_collection;
159 };
160
161 struct mt_device {
162 struct mt_class mtclass; /* our mt device class */
163 struct timer_list release_timer; /* to release sticky fingers */
164 struct hid_device *hdev; /* hid_device we're attached to */
165 unsigned long mt_io_flags; /* mt flags (MT_IO_FLAGS_*) */
166 __u8 inputmode_value; /* InputMode HID feature value */
167 __u8 maxcontacts;
168 bool is_buttonpad; /* is this device a button pad? */
169 bool serial_maybe; /* need to check for serial protocol */
170
171 struct list_head applications;
172 struct list_head reports;
173 };
174
175 static void mt_post_parse_default_settings(struct mt_device *td,
176 struct mt_application *app);
177 static void mt_post_parse(struct mt_device *td, struct mt_application *app);
178
179 /* classes of device behavior */
180 #define MT_CLS_DEFAULT 0x0001
181
182 #define MT_CLS_SERIAL 0x0002
183 #define MT_CLS_CONFIDENCE 0x0003
184 #define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
185 #define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
186 #define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
187 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
188 /* reserved 0x0008 */
189 #define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
190 #define MT_CLS_NSMU 0x000a
191 /* reserved 0x0010 */
192 /* reserved 0x0011 */
193 #define MT_CLS_WIN_8 0x0012
194 #define MT_CLS_EXPORT_ALL_INPUTS 0x0013
195 #define MT_CLS_WIN_8_DUAL 0x0014
196
197 /* vendor specific classes */
198 #define MT_CLS_3M 0x0101
199 /* reserved 0x0102 */
200 #define MT_CLS_EGALAX 0x0103
201 #define MT_CLS_EGALAX_SERIAL 0x0104
202 #define MT_CLS_TOPSEED 0x0105
203 #define MT_CLS_PANASONIC 0x0106
204 #define MT_CLS_FLATFROG 0x0107
205 #define MT_CLS_GENERALTOUCH_TWOFINGERS 0x0108
206 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS 0x0109
207 #define MT_CLS_LG 0x010a
208 #define MT_CLS_ASUS 0x010b
209 #define MT_CLS_VTL 0x0110
210 #define MT_CLS_GOOGLE 0x0111
211 #define MT_CLS_RAZER_BLADE_STEALTH 0x0112
212 #define MT_CLS_SMART_TECH 0x0113
213
214 #define MT_DEFAULT_MAXCONTACT 10
215 #define MT_MAX_MAXCONTACT 250
216
217 /*
218 * Resync device and local timestamps after that many microseconds without
219 * receiving data.
220 */
221 #define MAX_TIMESTAMP_INTERVAL 1000000
222
223 #define MT_USB_DEVICE(v, p) HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
224 #define MT_BT_DEVICE(v, p) HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
225
226 /*
227 * these device-dependent functions determine what slot corresponds
228 * to a valid contact that was just read.
229 */
230
cypress_compute_slot(struct mt_application * application,struct mt_usages * slot)231 static int cypress_compute_slot(struct mt_application *application,
232 struct mt_usages *slot)
233 {
234 if (*slot->contactid != 0 || application->num_received == 0)
235 return *slot->contactid;
236 else
237 return -1;
238 }
239
240 static const struct mt_class mt_classes[] = {
241 { .name = MT_CLS_DEFAULT,
242 .quirks = MT_QUIRK_ALWAYS_VALID |
243 MT_QUIRK_CONTACT_CNT_ACCURATE },
244 { .name = MT_CLS_NSMU,
245 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
246 { .name = MT_CLS_SERIAL,
247 .quirks = MT_QUIRK_ALWAYS_VALID},
248 { .name = MT_CLS_CONFIDENCE,
249 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
250 { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
251 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
252 MT_QUIRK_SLOT_IS_CONTACTID },
253 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
254 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
255 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
256 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
257 .quirks = MT_QUIRK_VALID_IS_INRANGE |
258 MT_QUIRK_SLOT_IS_CONTACTID,
259 .maxcontacts = 2 },
260 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
261 .quirks = MT_QUIRK_VALID_IS_INRANGE |
262 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
263 .maxcontacts = 2 },
264 { .name = MT_CLS_INRANGE_CONTACTNUMBER,
265 .quirks = MT_QUIRK_VALID_IS_INRANGE |
266 MT_QUIRK_SLOT_IS_CONTACTNUMBER },
267 { .name = MT_CLS_WIN_8,
268 .quirks = MT_QUIRK_ALWAYS_VALID |
269 MT_QUIRK_IGNORE_DUPLICATES |
270 MT_QUIRK_HOVERING |
271 MT_QUIRK_CONTACT_CNT_ACCURATE |
272 MT_QUIRK_STICKY_FINGERS |
273 MT_QUIRK_WIN8_PTP_BUTTONS },
274 { .name = MT_CLS_EXPORT_ALL_INPUTS,
275 .quirks = MT_QUIRK_ALWAYS_VALID |
276 MT_QUIRK_CONTACT_CNT_ACCURATE,
277 .export_all_inputs = true },
278 { .name = MT_CLS_WIN_8_DUAL,
279 .quirks = MT_QUIRK_ALWAYS_VALID |
280 MT_QUIRK_IGNORE_DUPLICATES |
281 MT_QUIRK_HOVERING |
282 MT_QUIRK_CONTACT_CNT_ACCURATE |
283 MT_QUIRK_WIN8_PTP_BUTTONS,
284 .export_all_inputs = true },
285
286 /*
287 * vendor specific classes
288 */
289 { .name = MT_CLS_3M,
290 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
291 MT_QUIRK_SLOT_IS_CONTACTID |
292 MT_QUIRK_TOUCH_SIZE_SCALING,
293 .sn_move = 2048,
294 .sn_width = 128,
295 .sn_height = 128,
296 .maxcontacts = 60,
297 },
298 { .name = MT_CLS_EGALAX,
299 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
300 MT_QUIRK_VALID_IS_INRANGE,
301 .sn_move = 4096,
302 .sn_pressure = 32,
303 },
304 { .name = MT_CLS_EGALAX_SERIAL,
305 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
306 MT_QUIRK_ALWAYS_VALID,
307 .sn_move = 4096,
308 .sn_pressure = 32,
309 },
310 { .name = MT_CLS_TOPSEED,
311 .quirks = MT_QUIRK_ALWAYS_VALID,
312 .is_indirect = true,
313 .maxcontacts = 2,
314 },
315 { .name = MT_CLS_PANASONIC,
316 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
317 .maxcontacts = 4 },
318 { .name = MT_CLS_GENERALTOUCH_TWOFINGERS,
319 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
320 MT_QUIRK_VALID_IS_INRANGE |
321 MT_QUIRK_SLOT_IS_CONTACTID,
322 .maxcontacts = 2
323 },
324 { .name = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
325 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
326 MT_QUIRK_SLOT_IS_CONTACTID
327 },
328
329 { .name = MT_CLS_FLATFROG,
330 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
331 MT_QUIRK_NO_AREA,
332 .sn_move = 2048,
333 .maxcontacts = 40,
334 },
335 { .name = MT_CLS_LG,
336 .quirks = MT_QUIRK_ALWAYS_VALID |
337 MT_QUIRK_FIX_CONST_CONTACT_ID |
338 MT_QUIRK_IGNORE_DUPLICATES |
339 MT_QUIRK_HOVERING |
340 MT_QUIRK_CONTACT_CNT_ACCURATE },
341 { .name = MT_CLS_ASUS,
342 .quirks = MT_QUIRK_ALWAYS_VALID |
343 MT_QUIRK_CONTACT_CNT_ACCURATE |
344 MT_QUIRK_ASUS_CUSTOM_UP },
345 { .name = MT_CLS_VTL,
346 .quirks = MT_QUIRK_ALWAYS_VALID |
347 MT_QUIRK_CONTACT_CNT_ACCURATE |
348 MT_QUIRK_FORCE_GET_FEATURE,
349 },
350 { .name = MT_CLS_GOOGLE,
351 .quirks = MT_QUIRK_ALWAYS_VALID |
352 MT_QUIRK_CONTACT_CNT_ACCURATE |
353 MT_QUIRK_SLOT_IS_CONTACTID |
354 MT_QUIRK_HOVERING
355 },
356 { .name = MT_CLS_RAZER_BLADE_STEALTH,
357 .quirks = MT_QUIRK_ALWAYS_VALID |
358 MT_QUIRK_IGNORE_DUPLICATES |
359 MT_QUIRK_HOVERING |
360 MT_QUIRK_CONTACT_CNT_ACCURATE |
361 MT_QUIRK_WIN8_PTP_BUTTONS,
362 },
363 { .name = MT_CLS_SMART_TECH,
364 .quirks = MT_QUIRK_ALWAYS_VALID |
365 MT_QUIRK_IGNORE_DUPLICATES |
366 MT_QUIRK_CONTACT_CNT_ACCURATE |
367 MT_QUIRK_SEPARATE_APP_REPORT,
368 },
369 { }
370 };
371
mt_show_quirks(struct device * dev,struct device_attribute * attr,char * buf)372 static ssize_t mt_show_quirks(struct device *dev,
373 struct device_attribute *attr,
374 char *buf)
375 {
376 struct hid_device *hdev = to_hid_device(dev);
377 struct mt_device *td = hid_get_drvdata(hdev);
378
379 return sprintf(buf, "%u\n", td->mtclass.quirks);
380 }
381
mt_set_quirks(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)382 static ssize_t mt_set_quirks(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t count)
385 {
386 struct hid_device *hdev = to_hid_device(dev);
387 struct mt_device *td = hid_get_drvdata(hdev);
388 struct mt_application *application;
389
390 unsigned long val;
391
392 if (kstrtoul(buf, 0, &val))
393 return -EINVAL;
394
395 td->mtclass.quirks = val;
396
397 list_for_each_entry(application, &td->applications, list) {
398 application->quirks = val;
399 if (!application->have_contact_count)
400 application->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
401 }
402
403 return count;
404 }
405
406 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
407
408 static struct attribute *sysfs_attrs[] = {
409 &dev_attr_quirks.attr,
410 NULL
411 };
412
413 static const struct attribute_group mt_attribute_group = {
414 .attrs = sysfs_attrs
415 };
416
mt_get_feature(struct hid_device * hdev,struct hid_report * report)417 static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
418 {
419 int ret;
420 u32 size = hid_report_len(report);
421 u8 *buf;
422
423 /*
424 * Do not fetch the feature report if the device has been explicitly
425 * marked as non-capable.
426 */
427 if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
428 return;
429
430 buf = hid_alloc_report_buf(report, GFP_KERNEL);
431 if (!buf)
432 return;
433
434 ret = hid_hw_raw_request(hdev, report->id, buf, size,
435 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
436 if (ret < 0) {
437 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
438 report->id);
439 } else {
440 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
441 size, 0);
442 if (ret)
443 dev_warn(&hdev->dev, "failed to report feature\n");
444 }
445
446 kfree(buf);
447 }
448
mt_feature_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)449 static void mt_feature_mapping(struct hid_device *hdev,
450 struct hid_field *field, struct hid_usage *usage)
451 {
452 struct mt_device *td = hid_get_drvdata(hdev);
453
454 switch (usage->hid) {
455 case HID_DG_CONTACTMAX:
456 mt_get_feature(hdev, field->report);
457
458 td->maxcontacts = field->value[0];
459 if (!td->maxcontacts &&
460 field->logical_maximum <= MT_MAX_MAXCONTACT)
461 td->maxcontacts = field->logical_maximum;
462 if (td->mtclass.maxcontacts)
463 /* check if the maxcontacts is given by the class */
464 td->maxcontacts = td->mtclass.maxcontacts;
465
466 break;
467 case HID_DG_BUTTONTYPE:
468 if (usage->usage_index >= field->report_count) {
469 dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
470 break;
471 }
472
473 mt_get_feature(hdev, field->report);
474 if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
475 td->is_buttonpad = true;
476
477 break;
478 case 0xff0000c5:
479 /* Retrieve the Win8 blob once to enable some devices */
480 if (usage->usage_index == 0)
481 mt_get_feature(hdev, field->report);
482 break;
483 }
484 }
485
set_abs(struct input_dev * input,unsigned int code,struct hid_field * field,int snratio)486 static void set_abs(struct input_dev *input, unsigned int code,
487 struct hid_field *field, int snratio)
488 {
489 int fmin = field->logical_minimum;
490 int fmax = field->logical_maximum;
491 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
492 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
493 input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
494 }
495
mt_allocate_usage(struct hid_device * hdev,struct mt_application * application)496 static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,
497 struct mt_application *application)
498 {
499 struct mt_usages *usage;
500
501 usage = devm_kzalloc(&hdev->dev, sizeof(*usage), GFP_KERNEL);
502 if (!usage)
503 return NULL;
504
505 /* set some defaults so we do not need to check for null pointers */
506 usage->x = DEFAULT_ZERO;
507 usage->y = DEFAULT_ZERO;
508 usage->cx = DEFAULT_ZERO;
509 usage->cy = DEFAULT_ZERO;
510 usage->p = DEFAULT_ZERO;
511 usage->w = DEFAULT_ZERO;
512 usage->h = DEFAULT_ZERO;
513 usage->a = DEFAULT_ZERO;
514 usage->contactid = DEFAULT_ZERO;
515 usage->tip_state = DEFAULT_FALSE;
516 usage->inrange_state = DEFAULT_FALSE;
517 usage->confidence_state = DEFAULT_TRUE;
518
519 list_add_tail(&usage->list, &application->mt_usages);
520
521 return usage;
522 }
523
mt_allocate_application(struct mt_device * td,struct hid_report * report)524 static struct mt_application *mt_allocate_application(struct mt_device *td,
525 struct hid_report *report)
526 {
527 unsigned int application = report->application;
528 struct mt_application *mt_application;
529
530 mt_application = devm_kzalloc(&td->hdev->dev, sizeof(*mt_application),
531 GFP_KERNEL);
532 if (!mt_application)
533 return NULL;
534
535 mt_application->application = application;
536 INIT_LIST_HEAD(&mt_application->mt_usages);
537
538 if (application == HID_DG_TOUCHSCREEN)
539 mt_application->mt_flags |= INPUT_MT_DIRECT;
540
541 /*
542 * Model touchscreens providing buttons as touchpads.
543 */
544 if (application == HID_DG_TOUCHPAD) {
545 mt_application->mt_flags |= INPUT_MT_POINTER;
546 td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
547 }
548
549 mt_application->scantime = DEFAULT_ZERO;
550 mt_application->raw_cc = DEFAULT_ZERO;
551 mt_application->quirks = td->mtclass.quirks;
552 mt_application->report_id = report->id;
553
554 list_add_tail(&mt_application->list, &td->applications);
555
556 return mt_application;
557 }
558
mt_find_application(struct mt_device * td,struct hid_report * report)559 static struct mt_application *mt_find_application(struct mt_device *td,
560 struct hid_report *report)
561 {
562 unsigned int application = report->application;
563 struct mt_application *tmp, *mt_application = NULL;
564
565 list_for_each_entry(tmp, &td->applications, list) {
566 if (application == tmp->application) {
567 if (!(td->mtclass.quirks & MT_QUIRK_SEPARATE_APP_REPORT) ||
568 tmp->report_id == report->id) {
569 mt_application = tmp;
570 break;
571 }
572 }
573 }
574
575 if (!mt_application)
576 mt_application = mt_allocate_application(td, report);
577
578 return mt_application;
579 }
580
mt_allocate_report_data(struct mt_device * td,struct hid_report * report)581 static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,
582 struct hid_report *report)
583 {
584 struct mt_report_data *rdata;
585 struct hid_field *field;
586 int r, n;
587
588 rdata = devm_kzalloc(&td->hdev->dev, sizeof(*rdata), GFP_KERNEL);
589 if (!rdata)
590 return NULL;
591
592 rdata->report = report;
593 rdata->application = mt_find_application(td, report);
594
595 if (!rdata->application) {
596 devm_kfree(&td->hdev->dev, rdata);
597 return NULL;
598 }
599
600 for (r = 0; r < report->maxfield; r++) {
601 field = report->field[r];
602
603 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
604 continue;
605
606 if (field->logical == HID_DG_FINGER || td->hdev->group != HID_GROUP_MULTITOUCH_WIN_8) {
607 for (n = 0; n < field->report_count; n++) {
608 if (field->usage[n].hid == HID_DG_CONTACTID) {
609 rdata->is_mt_collection = true;
610 break;
611 }
612 }
613 }
614 }
615
616 list_add_tail(&rdata->list, &td->reports);
617
618 return rdata;
619 }
620
mt_find_report_data(struct mt_device * td,struct hid_report * report)621 static struct mt_report_data *mt_find_report_data(struct mt_device *td,
622 struct hid_report *report)
623 {
624 struct mt_report_data *tmp, *rdata = NULL;
625
626 list_for_each_entry(tmp, &td->reports, list) {
627 if (report == tmp->report) {
628 rdata = tmp;
629 break;
630 }
631 }
632
633 if (!rdata)
634 rdata = mt_allocate_report_data(td, report);
635
636 return rdata;
637 }
638
mt_store_field(struct hid_device * hdev,struct mt_application * application,__s32 * value,size_t offset)639 static void mt_store_field(struct hid_device *hdev,
640 struct mt_application *application,
641 __s32 *value,
642 size_t offset)
643 {
644 struct mt_usages *usage;
645 __s32 **target;
646
647 if (list_empty(&application->mt_usages))
648 usage = mt_allocate_usage(hdev, application);
649 else
650 usage = list_last_entry(&application->mt_usages,
651 struct mt_usages,
652 list);
653
654 if (!usage)
655 return;
656
657 target = (__s32 **)((char *)usage + offset);
658
659 /* the value has already been filled, create a new slot */
660 if (*target != DEFAULT_TRUE &&
661 *target != DEFAULT_FALSE &&
662 *target != DEFAULT_ZERO) {
663 if (usage->contactid == DEFAULT_ZERO ||
664 usage->x == DEFAULT_ZERO ||
665 usage->y == DEFAULT_ZERO) {
666 hid_dbg(hdev,
667 "ignoring duplicate usage on incomplete");
668 return;
669 }
670 usage = mt_allocate_usage(hdev, application);
671 if (!usage)
672 return;
673
674 target = (__s32 **)((char *)usage + offset);
675 }
676
677 *target = value;
678 }
679
680 #define MT_STORE_FIELD(__name) \
681 mt_store_field(hdev, app, \
682 &field->value[usage->usage_index], \
683 offsetof(struct mt_usages, __name))
684
mt_touch_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max,struct mt_application * app)685 static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
686 struct hid_field *field, struct hid_usage *usage,
687 unsigned long **bit, int *max, struct mt_application *app)
688 {
689 struct mt_device *td = hid_get_drvdata(hdev);
690 struct mt_class *cls = &td->mtclass;
691 int code;
692 struct hid_usage *prev_usage = NULL;
693
694 /*
695 * Model touchscreens providing buttons as touchpads.
696 */
697 if (field->application == HID_DG_TOUCHSCREEN &&
698 (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
699 app->mt_flags |= INPUT_MT_POINTER;
700 td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
701 }
702
703 /* count the buttons on touchpads */
704 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
705 app->buttons_count++;
706
707 if (usage->usage_index)
708 prev_usage = &field->usage[usage->usage_index - 1];
709
710 switch (usage->hid & HID_USAGE_PAGE) {
711
712 case HID_UP_GENDESK:
713 switch (usage->hid) {
714 case HID_GD_X:
715 if (prev_usage && (prev_usage->hid == usage->hid)) {
716 code = ABS_MT_TOOL_X;
717 MT_STORE_FIELD(cx);
718 } else {
719 code = ABS_MT_POSITION_X;
720 MT_STORE_FIELD(x);
721 }
722
723 set_abs(hi->input, code, field, cls->sn_move);
724
725 /*
726 * A system multi-axis that exports X and Y has a high
727 * chance of being used directly on a surface
728 */
729 if (field->application == HID_GD_SYSTEM_MULTIAXIS) {
730 __set_bit(INPUT_PROP_DIRECT,
731 hi->input->propbit);
732 input_set_abs_params(hi->input,
733 ABS_MT_TOOL_TYPE,
734 MT_TOOL_DIAL,
735 MT_TOOL_DIAL, 0, 0);
736 }
737
738 return 1;
739 case HID_GD_Y:
740 if (prev_usage && (prev_usage->hid == usage->hid)) {
741 code = ABS_MT_TOOL_Y;
742 MT_STORE_FIELD(cy);
743 } else {
744 code = ABS_MT_POSITION_Y;
745 MT_STORE_FIELD(y);
746 }
747
748 set_abs(hi->input, code, field, cls->sn_move);
749
750 return 1;
751 }
752 return 0;
753
754 case HID_UP_DIGITIZER:
755 switch (usage->hid) {
756 case HID_DG_INRANGE:
757 if (app->quirks & MT_QUIRK_HOVERING) {
758 input_set_abs_params(hi->input,
759 ABS_MT_DISTANCE, 0, 1, 0, 0);
760 }
761 MT_STORE_FIELD(inrange_state);
762 return 1;
763 case HID_DG_CONFIDENCE:
764 if ((cls->name == MT_CLS_WIN_8 ||
765 cls->name == MT_CLS_WIN_8_DUAL) &&
766 (field->application == HID_DG_TOUCHPAD ||
767 field->application == HID_DG_TOUCHSCREEN))
768 app->quirks |= MT_QUIRK_CONFIDENCE;
769
770 if (app->quirks & MT_QUIRK_CONFIDENCE)
771 input_set_abs_params(hi->input,
772 ABS_MT_TOOL_TYPE,
773 MT_TOOL_FINGER,
774 MT_TOOL_PALM, 0, 0);
775
776 MT_STORE_FIELD(confidence_state);
777 return 1;
778 case HID_DG_TIPSWITCH:
779 if (field->application != HID_GD_SYSTEM_MULTIAXIS)
780 input_set_capability(hi->input,
781 EV_KEY, BTN_TOUCH);
782 MT_STORE_FIELD(tip_state);
783 return 1;
784 case HID_DG_CONTACTID:
785 MT_STORE_FIELD(contactid);
786 app->touches_by_report++;
787 return 1;
788 case HID_DG_WIDTH:
789 if (!(app->quirks & MT_QUIRK_NO_AREA))
790 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
791 cls->sn_width);
792 MT_STORE_FIELD(w);
793 return 1;
794 case HID_DG_HEIGHT:
795 if (!(app->quirks & MT_QUIRK_NO_AREA)) {
796 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
797 cls->sn_height);
798
799 /*
800 * Only set ABS_MT_ORIENTATION if it is not
801 * already set by the HID_DG_AZIMUTH usage.
802 */
803 if (!test_bit(ABS_MT_ORIENTATION,
804 hi->input->absbit))
805 input_set_abs_params(hi->input,
806 ABS_MT_ORIENTATION, 0, 1, 0, 0);
807 }
808 MT_STORE_FIELD(h);
809 return 1;
810 case HID_DG_TIPPRESSURE:
811 set_abs(hi->input, ABS_MT_PRESSURE, field,
812 cls->sn_pressure);
813 MT_STORE_FIELD(p);
814 return 1;
815 case HID_DG_SCANTIME:
816 input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
817 app->scantime = &field->value[usage->usage_index];
818 app->scantime_logical_max = field->logical_maximum;
819 return 1;
820 case HID_DG_CONTACTCOUNT:
821 app->have_contact_count = true;
822 app->raw_cc = &field->value[usage->usage_index];
823 return 1;
824 case HID_DG_AZIMUTH:
825 /*
826 * Azimuth has the range of [0, MAX) representing a full
827 * revolution. Set ABS_MT_ORIENTATION to a quarter of
828 * MAX according the definition of ABS_MT_ORIENTATION
829 */
830 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
831 -field->logical_maximum / 4,
832 field->logical_maximum / 4,
833 cls->sn_move ?
834 field->logical_maximum / cls->sn_move : 0, 0);
835 MT_STORE_FIELD(a);
836 return 1;
837 case HID_DG_CONTACTMAX:
838 /* contact max are global to the report */
839 return -1;
840 case HID_DG_TOUCH:
841 /* Legacy devices use TIPSWITCH and not TOUCH.
842 * Let's just ignore this field. */
843 return -1;
844 }
845 /* let hid-input decide for the others */
846 return 0;
847
848 case HID_UP_BUTTON:
849 code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
850 /*
851 * MS PTP spec says that external buttons left and right have
852 * usages 2 and 3.
853 */
854 if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
855 field->application == HID_DG_TOUCHPAD &&
856 (usage->hid & HID_USAGE) > 1)
857 code--;
858
859 if (field->application == HID_GD_SYSTEM_MULTIAXIS)
860 code = BTN_0 + ((usage->hid - 1) & HID_USAGE);
861
862 hid_map_usage(hi, usage, bit, max, EV_KEY, code);
863 if (!*bit)
864 return -1;
865 input_set_capability(hi->input, EV_KEY, code);
866 return 1;
867
868 case 0xff000000:
869 /* we do not want to map these: no input-oriented meaning */
870 return -1;
871 }
872
873 return 0;
874 }
875
mt_compute_slot(struct mt_device * td,struct mt_application * app,struct mt_usages * slot,struct input_dev * input)876 static int mt_compute_slot(struct mt_device *td, struct mt_application *app,
877 struct mt_usages *slot,
878 struct input_dev *input)
879 {
880 __s32 quirks = app->quirks;
881
882 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
883 return *slot->contactid;
884
885 if (quirks & MT_QUIRK_CYPRESS)
886 return cypress_compute_slot(app, slot);
887
888 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
889 return app->num_received;
890
891 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
892 return *slot->contactid - 1;
893
894 return input_mt_get_slot_by_key(input, *slot->contactid);
895 }
896
mt_release_pending_palms(struct mt_device * td,struct mt_application * app,struct input_dev * input)897 static void mt_release_pending_palms(struct mt_device *td,
898 struct mt_application *app,
899 struct input_dev *input)
900 {
901 int slotnum;
902 bool need_sync = false;
903
904 for_each_set_bit(slotnum, app->pending_palm_slots, td->maxcontacts) {
905 clear_bit(slotnum, app->pending_palm_slots);
906
907 input_mt_slot(input, slotnum);
908 input_mt_report_slot_state(input, MT_TOOL_PALM, false);
909
910 need_sync = true;
911 }
912
913 if (need_sync) {
914 input_mt_sync_frame(input);
915 input_sync(input);
916 }
917 }
918
919 /*
920 * this function is called when a whole packet has been received and processed,
921 * so that it can decide what to send to the input layer.
922 */
mt_sync_frame(struct mt_device * td,struct mt_application * app,struct input_dev * input)923 static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
924 struct input_dev *input)
925 {
926 if (app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
927 input_event(input, EV_KEY, BTN_LEFT, app->left_button_state);
928
929 input_mt_sync_frame(input);
930 input_event(input, EV_MSC, MSC_TIMESTAMP, app->timestamp);
931 input_sync(input);
932
933 mt_release_pending_palms(td, app, input);
934
935 app->num_received = 0;
936 app->left_button_state = 0;
937
938 if (test_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags))
939 set_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
940 else
941 clear_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
942 clear_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
943 }
944
mt_compute_timestamp(struct mt_application * app,__s32 value)945 static int mt_compute_timestamp(struct mt_application *app, __s32 value)
946 {
947 long delta = value - app->prev_scantime;
948 unsigned long jdelta = jiffies_to_usecs(jiffies - app->jiffies);
949
950 app->jiffies = jiffies;
951
952 if (delta < 0)
953 delta += app->scantime_logical_max;
954
955 /* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
956 delta *= 100;
957
958 if (jdelta > MAX_TIMESTAMP_INTERVAL)
959 /* No data received for a while, resync the timestamp. */
960 return 0;
961 else
962 return app->timestamp + delta;
963 }
964
mt_touch_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,__s32 value)965 static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
966 struct hid_usage *usage, __s32 value)
967 {
968 /* we will handle the hidinput part later, now remains hiddev */
969 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
970 hid->hiddev_hid_event(hid, field, usage, value);
971
972 return 1;
973 }
974
mt_process_slot(struct mt_device * td,struct input_dev * input,struct mt_application * app,struct mt_usages * slot)975 static int mt_process_slot(struct mt_device *td, struct input_dev *input,
976 struct mt_application *app,
977 struct mt_usages *slot)
978 {
979 struct input_mt *mt = input->mt;
980 __s32 quirks = app->quirks;
981 bool valid = true;
982 bool confidence_state = true;
983 bool inrange_state = false;
984 int active;
985 int slotnum;
986 int tool = MT_TOOL_FINGER;
987
988 if (!slot)
989 return -EINVAL;
990
991 if ((quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
992 app->num_received >= app->num_expected)
993 return -EAGAIN;
994
995 if (!(quirks & MT_QUIRK_ALWAYS_VALID)) {
996 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
997 valid = *slot->inrange_state;
998 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
999 valid = *slot->tip_state;
1000 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
1001 valid = *slot->confidence_state;
1002
1003 if (!valid)
1004 return 0;
1005 }
1006
1007 slotnum = mt_compute_slot(td, app, slot, input);
1008 if (slotnum < 0 || slotnum >= td->maxcontacts)
1009 return 0;
1010
1011 if ((quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
1012 struct input_mt_slot *i_slot = &mt->slots[slotnum];
1013
1014 if (input_mt_is_active(i_slot) &&
1015 input_mt_is_used(mt, i_slot))
1016 return -EAGAIN;
1017 }
1018
1019 if (quirks & MT_QUIRK_CONFIDENCE)
1020 confidence_state = *slot->confidence_state;
1021
1022 if (quirks & MT_QUIRK_HOVERING)
1023 inrange_state = *slot->inrange_state;
1024
1025 active = *slot->tip_state || inrange_state;
1026
1027 if (app->application == HID_GD_SYSTEM_MULTIAXIS)
1028 tool = MT_TOOL_DIAL;
1029 else if (unlikely(!confidence_state)) {
1030 tool = MT_TOOL_PALM;
1031 if (!active && mt &&
1032 input_mt_is_active(&mt->slots[slotnum])) {
1033 /*
1034 * The non-confidence was reported for
1035 * previously valid contact that is also no
1036 * longer valid. We can't simply report
1037 * lift-off as userspace will not be aware
1038 * of non-confidence, so we need to split
1039 * it into 2 events: active MT_TOOL_PALM
1040 * and a separate liftoff.
1041 */
1042 active = true;
1043 set_bit(slotnum, app->pending_palm_slots);
1044 }
1045 }
1046
1047 input_mt_slot(input, slotnum);
1048 input_mt_report_slot_state(input, tool, active);
1049 if (active) {
1050 /* this finger is in proximity of the sensor */
1051 int wide = (*slot->w > *slot->h);
1052 int major = max(*slot->w, *slot->h);
1053 int minor = min(*slot->w, *slot->h);
1054 int orientation = wide;
1055 int max_azimuth;
1056 int azimuth;
1057
1058 if (slot->a != DEFAULT_ZERO) {
1059 /*
1060 * Azimuth is counter-clockwise and ranges from [0, MAX)
1061 * (a full revolution). Convert it to clockwise ranging
1062 * [-MAX/2, MAX/2].
1063 *
1064 * Note that ABS_MT_ORIENTATION require us to report
1065 * the limit of [-MAX/4, MAX/4], but the value can go
1066 * out of range to [-MAX/2, MAX/2] to report an upside
1067 * down ellipsis.
1068 */
1069 azimuth = *slot->a;
1070 max_azimuth = input_abs_get_max(input,
1071 ABS_MT_ORIENTATION);
1072 if (azimuth > max_azimuth * 2)
1073 azimuth -= max_azimuth * 4;
1074 orientation = -azimuth;
1075 }
1076
1077 if (quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
1078 /*
1079 * divided by two to match visual scale of touch
1080 * for devices with this quirk
1081 */
1082 major = major >> 1;
1083 minor = minor >> 1;
1084 }
1085
1086 input_event(input, EV_ABS, ABS_MT_POSITION_X, *slot->x);
1087 input_event(input, EV_ABS, ABS_MT_POSITION_Y, *slot->y);
1088 input_event(input, EV_ABS, ABS_MT_TOOL_X, *slot->cx);
1089 input_event(input, EV_ABS, ABS_MT_TOOL_Y, *slot->cy);
1090 input_event(input, EV_ABS, ABS_MT_DISTANCE, !*slot->tip_state);
1091 input_event(input, EV_ABS, ABS_MT_ORIENTATION, orientation);
1092 input_event(input, EV_ABS, ABS_MT_PRESSURE, *slot->p);
1093 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
1094 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
1095
1096 set_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
1097 }
1098
1099 return 0;
1100 }
1101
mt_process_mt_event(struct hid_device * hid,struct mt_application * app,struct hid_field * field,struct hid_usage * usage,__s32 value,bool first_packet)1102 static void mt_process_mt_event(struct hid_device *hid,
1103 struct mt_application *app,
1104 struct hid_field *field,
1105 struct hid_usage *usage,
1106 __s32 value,
1107 bool first_packet)
1108 {
1109 __s32 quirks = app->quirks;
1110 struct input_dev *input = field->hidinput->input;
1111
1112 if (!usage->type || !(hid->claimed & HID_CLAIMED_INPUT))
1113 return;
1114
1115 if (quirks & MT_QUIRK_WIN8_PTP_BUTTONS) {
1116
1117 /*
1118 * For Win8 PTP touchpads we should only look at
1119 * non finger/touch events in the first_packet of a
1120 * (possible) multi-packet frame.
1121 */
1122 if (!first_packet)
1123 return;
1124
1125 /*
1126 * For Win8 PTP touchpads we map both the clickpad click
1127 * and any "external" left buttons to BTN_LEFT if a
1128 * device claims to have both we need to report 1 for
1129 * BTN_LEFT if either is pressed, so we or all values
1130 * together and report the result in mt_sync_frame().
1131 */
1132 if (usage->type == EV_KEY && usage->code == BTN_LEFT) {
1133 app->left_button_state |= value;
1134 return;
1135 }
1136 }
1137
1138 input_event(input, usage->type, usage->code, value);
1139 }
1140
mt_touch_report(struct hid_device * hid,struct mt_report_data * rdata)1141 static void mt_touch_report(struct hid_device *hid,
1142 struct mt_report_data *rdata)
1143 {
1144 struct mt_device *td = hid_get_drvdata(hid);
1145 struct hid_report *report = rdata->report;
1146 struct mt_application *app = rdata->application;
1147 struct hid_field *field;
1148 struct input_dev *input;
1149 struct mt_usages *slot;
1150 bool first_packet;
1151 unsigned count;
1152 int r, n;
1153 int scantime = 0;
1154 int contact_count = -1;
1155
1156 /* sticky fingers release in progress, abort */
1157 if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1158 return;
1159
1160 scantime = *app->scantime;
1161 app->timestamp = mt_compute_timestamp(app, scantime);
1162 if (app->raw_cc != DEFAULT_ZERO)
1163 contact_count = *app->raw_cc;
1164
1165 /*
1166 * Includes multi-packet support where subsequent
1167 * packets are sent with zero contactcount.
1168 */
1169 if (contact_count >= 0) {
1170 /*
1171 * For Win8 PTPs the first packet (td->num_received == 0) may
1172 * have a contactcount of 0 if there only is a button event.
1173 * We double check that this is not a continuation packet
1174 * of a possible multi-packet frame be checking that the
1175 * timestamp has changed.
1176 */
1177 if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
1178 app->num_received == 0 &&
1179 app->prev_scantime != scantime)
1180 app->num_expected = contact_count;
1181 /* A non 0 contact count always indicates a first packet */
1182 else if (contact_count)
1183 app->num_expected = contact_count;
1184 }
1185 app->prev_scantime = scantime;
1186
1187 first_packet = app->num_received == 0;
1188
1189 input = report->field[0]->hidinput->input;
1190
1191 list_for_each_entry(slot, &app->mt_usages, list) {
1192 if (!mt_process_slot(td, input, app, slot))
1193 app->num_received++;
1194 }
1195
1196 for (r = 0; r < report->maxfield; r++) {
1197 field = report->field[r];
1198 count = field->report_count;
1199
1200 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
1201 continue;
1202
1203 for (n = 0; n < count; n++)
1204 mt_process_mt_event(hid, app, field,
1205 &field->usage[n], field->value[n],
1206 first_packet);
1207 }
1208
1209 if (app->num_received >= app->num_expected)
1210 mt_sync_frame(td, app, input);
1211
1212 /*
1213 * Windows 8 specs says 2 things:
1214 * - once a contact has been reported, it has to be reported in each
1215 * subsequent report
1216 * - the report rate when fingers are present has to be at least
1217 * the refresh rate of the screen, 60 or 120 Hz
1218 *
1219 * I interprete this that the specification forces a report rate of
1220 * at least 60 Hz for a touchscreen to be certified.
1221 * Which means that if we do not get a report whithin 16 ms, either
1222 * something wrong happens, either the touchscreen forgets to send
1223 * a release. Taking a reasonable margin allows to remove issues
1224 * with USB communication or the load of the machine.
1225 *
1226 * Given that Win 8 devices are forced to send a release, this will
1227 * only affect laggish machines and the ones that have a firmware
1228 * defect.
1229 */
1230 if (app->quirks & MT_QUIRK_STICKY_FINGERS) {
1231 if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1232 mod_timer(&td->release_timer,
1233 jiffies + msecs_to_jiffies(100));
1234 else
1235 del_timer(&td->release_timer);
1236 }
1237
1238 clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1239 }
1240
mt_touch_input_configured(struct hid_device * hdev,struct hid_input * hi,struct mt_application * app)1241 static int mt_touch_input_configured(struct hid_device *hdev,
1242 struct hid_input *hi,
1243 struct mt_application *app)
1244 {
1245 struct mt_device *td = hid_get_drvdata(hdev);
1246 struct mt_class *cls = &td->mtclass;
1247 struct input_dev *input = hi->input;
1248 int ret;
1249
1250 if (!td->maxcontacts)
1251 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
1252
1253 mt_post_parse(td, app);
1254 if (td->serial_maybe)
1255 mt_post_parse_default_settings(td, app);
1256
1257 if (cls->is_indirect)
1258 app->mt_flags |= INPUT_MT_POINTER;
1259
1260 if (app->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1261 app->mt_flags |= INPUT_MT_DROP_UNUSED;
1262
1263 /* check for clickpads */
1264 if ((app->mt_flags & INPUT_MT_POINTER) &&
1265 (app->buttons_count == 1))
1266 td->is_buttonpad = true;
1267
1268 if (td->is_buttonpad)
1269 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1270
1271 app->pending_palm_slots = devm_kcalloc(&hi->input->dev,
1272 BITS_TO_LONGS(td->maxcontacts),
1273 sizeof(long),
1274 GFP_KERNEL);
1275 if (!app->pending_palm_slots)
1276 return -ENOMEM;
1277
1278 ret = input_mt_init_slots(input, td->maxcontacts, app->mt_flags);
1279 if (ret)
1280 return ret;
1281
1282 app->mt_flags = 0;
1283 return 0;
1284 }
1285
1286 #define mt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
1287 max, EV_KEY, (c))
mt_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)1288 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1289 struct hid_field *field, struct hid_usage *usage,
1290 unsigned long **bit, int *max)
1291 {
1292 struct mt_device *td = hid_get_drvdata(hdev);
1293 struct mt_application *application;
1294 struct mt_report_data *rdata;
1295
1296 rdata = mt_find_report_data(td, field->report);
1297 if (!rdata) {
1298 hid_err(hdev, "failed to allocate data for report\n");
1299 return 0;
1300 }
1301
1302 application = rdata->application;
1303
1304 /*
1305 * If mtclass.export_all_inputs is not set, only map fields from
1306 * TouchScreen or TouchPad collections. We need to ignore fields
1307 * that belong to other collections such as Mouse that might have
1308 * the same GenericDesktop usages.
1309 */
1310 if (!td->mtclass.export_all_inputs &&
1311 field->application != HID_DG_TOUCHSCREEN &&
1312 field->application != HID_DG_PEN &&
1313 field->application != HID_DG_TOUCHPAD &&
1314 field->application != HID_GD_KEYBOARD &&
1315 field->application != HID_GD_SYSTEM_CONTROL &&
1316 field->application != HID_CP_CONSUMER_CONTROL &&
1317 field->application != HID_GD_WIRELESS_RADIO_CTLS &&
1318 field->application != HID_GD_SYSTEM_MULTIAXIS &&
1319 !(field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1320 application->quirks & MT_QUIRK_ASUS_CUSTOM_UP))
1321 return -1;
1322
1323 /*
1324 * Some Asus keyboard+touchpad devices have the hotkeys defined in the
1325 * touchpad report descriptor. We need to treat these as an array to
1326 * map usages to input keys.
1327 */
1328 if (field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1329 application->quirks & MT_QUIRK_ASUS_CUSTOM_UP &&
1330 (usage->hid & HID_USAGE_PAGE) == HID_UP_CUSTOM) {
1331 set_bit(EV_REP, hi->input->evbit);
1332 if (field->flags & HID_MAIN_ITEM_VARIABLE)
1333 field->flags &= ~HID_MAIN_ITEM_VARIABLE;
1334 switch (usage->hid & HID_USAGE) {
1335 case 0x10: mt_map_key_clear(KEY_BRIGHTNESSDOWN); break;
1336 case 0x20: mt_map_key_clear(KEY_BRIGHTNESSUP); break;
1337 case 0x35: mt_map_key_clear(KEY_DISPLAY_OFF); break;
1338 case 0x6b: mt_map_key_clear(KEY_F21); break;
1339 case 0x6c: mt_map_key_clear(KEY_SLEEP); break;
1340 default:
1341 return -1;
1342 }
1343 return 1;
1344 }
1345
1346 if (rdata->is_mt_collection)
1347 return mt_touch_input_mapping(hdev, hi, field, usage, bit, max,
1348 application);
1349
1350 /*
1351 * some egalax touchscreens have "application == DG_TOUCHSCREEN"
1352 * for the stylus. Overwrite the hid_input application
1353 */
1354 if (field->physical == HID_DG_STYLUS)
1355 hi->application = HID_DG_STYLUS;
1356
1357 /* let hid-core decide for the others */
1358 return 0;
1359 }
1360
mt_input_mapped(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)1361 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
1362 struct hid_field *field, struct hid_usage *usage,
1363 unsigned long **bit, int *max)
1364 {
1365 struct mt_device *td = hid_get_drvdata(hdev);
1366 struct mt_report_data *rdata;
1367
1368 rdata = mt_find_report_data(td, field->report);
1369 if (rdata && rdata->is_mt_collection) {
1370 /* We own these mappings, tell hid-input to ignore them */
1371 return -1;
1372 }
1373
1374 /* let hid-core decide for the others */
1375 return 0;
1376 }
1377
mt_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,__s32 value)1378 static int mt_event(struct hid_device *hid, struct hid_field *field,
1379 struct hid_usage *usage, __s32 value)
1380 {
1381 struct mt_device *td = hid_get_drvdata(hid);
1382 struct mt_report_data *rdata;
1383
1384 rdata = mt_find_report_data(td, field->report);
1385 if (rdata && rdata->is_mt_collection)
1386 return mt_touch_event(hid, field, usage, value);
1387
1388 return 0;
1389 }
1390
mt_report(struct hid_device * hid,struct hid_report * report)1391 static void mt_report(struct hid_device *hid, struct hid_report *report)
1392 {
1393 struct mt_device *td = hid_get_drvdata(hid);
1394 struct hid_field *field = report->field[0];
1395 struct mt_report_data *rdata;
1396
1397 if (!(hid->claimed & HID_CLAIMED_INPUT))
1398 return;
1399
1400 rdata = mt_find_report_data(td, report);
1401 if (rdata && rdata->is_mt_collection)
1402 return mt_touch_report(hid, rdata);
1403
1404 if (field && field->hidinput && field->hidinput->input)
1405 input_sync(field->hidinput->input);
1406 }
1407
mt_need_to_apply_feature(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,enum latency_mode latency,bool surface_switch,bool button_switch,bool * inputmode_found)1408 static bool mt_need_to_apply_feature(struct hid_device *hdev,
1409 struct hid_field *field,
1410 struct hid_usage *usage,
1411 enum latency_mode latency,
1412 bool surface_switch,
1413 bool button_switch,
1414 bool *inputmode_found)
1415 {
1416 struct mt_device *td = hid_get_drvdata(hdev);
1417 struct mt_class *cls = &td->mtclass;
1418 struct hid_report *report = field->report;
1419 unsigned int index = usage->usage_index;
1420 char *buf;
1421 u32 report_len;
1422 int max;
1423
1424 switch (usage->hid) {
1425 case HID_DG_INPUTMODE:
1426 /*
1427 * Some elan panels wrongly declare 2 input mode features,
1428 * and silently ignore when we set the value in the second
1429 * field. Skip the second feature and hope for the best.
1430 */
1431 if (*inputmode_found)
1432 return false;
1433
1434 if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
1435 report_len = hid_report_len(report);
1436 buf = hid_alloc_report_buf(report, GFP_KERNEL);
1437 if (!buf) {
1438 hid_err(hdev,
1439 "failed to allocate buffer for report\n");
1440 return false;
1441 }
1442 hid_hw_raw_request(hdev, report->id, buf, report_len,
1443 HID_FEATURE_REPORT,
1444 HID_REQ_GET_REPORT);
1445 kfree(buf);
1446 }
1447
1448 field->value[index] = td->inputmode_value;
1449 *inputmode_found = true;
1450 return true;
1451
1452 case HID_DG_CONTACTMAX:
1453 if (cls->maxcontacts) {
1454 max = min_t(int, field->logical_maximum,
1455 cls->maxcontacts);
1456 if (field->value[index] != max) {
1457 field->value[index] = max;
1458 return true;
1459 }
1460 }
1461 break;
1462
1463 case HID_DG_LATENCYMODE:
1464 field->value[index] = latency;
1465 return true;
1466
1467 case HID_DG_SURFACESWITCH:
1468 field->value[index] = surface_switch;
1469 return true;
1470
1471 case HID_DG_BUTTONSWITCH:
1472 field->value[index] = button_switch;
1473 return true;
1474 }
1475
1476 return false; /* no need to update the report */
1477 }
1478
mt_set_modes(struct hid_device * hdev,enum latency_mode latency,bool surface_switch,bool button_switch)1479 static void mt_set_modes(struct hid_device *hdev, enum latency_mode latency,
1480 bool surface_switch, bool button_switch)
1481 {
1482 struct hid_report_enum *rep_enum;
1483 struct hid_report *rep;
1484 struct hid_usage *usage;
1485 int i, j;
1486 bool update_report;
1487 bool inputmode_found = false;
1488
1489 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1490 list_for_each_entry(rep, &rep_enum->report_list, list) {
1491 update_report = false;
1492
1493 for (i = 0; i < rep->maxfield; i++) {
1494 /* Ignore if report count is out of bounds. */
1495 if (rep->field[i]->report_count < 1)
1496 continue;
1497
1498 for (j = 0; j < rep->field[i]->maxusage; j++) {
1499 usage = &rep->field[i]->usage[j];
1500
1501 if (mt_need_to_apply_feature(hdev,
1502 rep->field[i],
1503 usage,
1504 latency,
1505 surface_switch,
1506 button_switch,
1507 &inputmode_found))
1508 update_report = true;
1509 }
1510 }
1511
1512 if (update_report)
1513 hid_hw_request(hdev, rep, HID_REQ_SET_REPORT);
1514 }
1515 }
1516
mt_post_parse_default_settings(struct mt_device * td,struct mt_application * app)1517 static void mt_post_parse_default_settings(struct mt_device *td,
1518 struct mt_application *app)
1519 {
1520 __s32 quirks = app->quirks;
1521
1522 /* unknown serial device needs special quirks */
1523 if (list_is_singular(&app->mt_usages)) {
1524 quirks |= MT_QUIRK_ALWAYS_VALID;
1525 quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1526 quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1527 quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1528 quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1529 }
1530
1531 app->quirks = quirks;
1532 }
1533
mt_post_parse(struct mt_device * td,struct mt_application * app)1534 static void mt_post_parse(struct mt_device *td, struct mt_application *app)
1535 {
1536 if (!app->have_contact_count)
1537 app->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1538 }
1539
mt_input_configured(struct hid_device * hdev,struct hid_input * hi)1540 static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1541 {
1542 struct mt_device *td = hid_get_drvdata(hdev);
1543 const char *suffix = NULL;
1544 struct mt_report_data *rdata;
1545 struct mt_application *mt_application = NULL;
1546 struct hid_report *report;
1547 int ret;
1548
1549 list_for_each_entry(report, &hi->reports, hidinput_list) {
1550 rdata = mt_find_report_data(td, report);
1551 if (!rdata) {
1552 hid_err(hdev, "failed to allocate data for report\n");
1553 return -ENOMEM;
1554 }
1555
1556 mt_application = rdata->application;
1557
1558 if (rdata->is_mt_collection) {
1559 ret = mt_touch_input_configured(hdev, hi,
1560 mt_application);
1561 if (ret)
1562 return ret;
1563 }
1564 }
1565
1566 switch (hi->application) {
1567 case HID_GD_KEYBOARD:
1568 case HID_GD_KEYPAD:
1569 case HID_GD_MOUSE:
1570 case HID_DG_TOUCHPAD:
1571 case HID_GD_SYSTEM_CONTROL:
1572 case HID_CP_CONSUMER_CONTROL:
1573 case HID_GD_WIRELESS_RADIO_CTLS:
1574 case HID_GD_SYSTEM_MULTIAXIS:
1575 /* already handled by hid core */
1576 break;
1577 case HID_DG_TOUCHSCREEN:
1578 /* we do not set suffix = "Touchscreen" */
1579 hi->input->name = hdev->name;
1580 break;
1581 case HID_DG_STYLUS:
1582 /* force BTN_STYLUS to allow tablet matching in udev */
1583 __set_bit(BTN_STYLUS, hi->input->keybit);
1584 break;
1585 case HID_VD_ASUS_CUSTOM_MEDIA_KEYS:
1586 suffix = "Custom Media Keys";
1587 break;
1588 case HID_DG_PEN:
1589 suffix = "Stylus";
1590 break;
1591 default:
1592 suffix = "UNKNOWN";
1593 break;
1594 }
1595
1596 if (suffix)
1597 hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
1598 "%s %s", hdev->name, suffix);
1599
1600 return 0;
1601 }
1602
mt_fix_const_field(struct hid_field * field,unsigned int usage)1603 static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
1604 {
1605 if (field->usage[0].hid != usage ||
1606 !(field->flags & HID_MAIN_ITEM_CONSTANT))
1607 return;
1608
1609 field->flags &= ~HID_MAIN_ITEM_CONSTANT;
1610 field->flags |= HID_MAIN_ITEM_VARIABLE;
1611 }
1612
mt_fix_const_fields(struct hid_device * hdev,unsigned int usage)1613 static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
1614 {
1615 struct hid_report *report;
1616 int i;
1617
1618 list_for_each_entry(report,
1619 &hdev->report_enum[HID_INPUT_REPORT].report_list,
1620 list) {
1621
1622 if (!report->maxfield)
1623 continue;
1624
1625 for (i = 0; i < report->maxfield; i++)
1626 if (report->field[i]->maxusage >= 1)
1627 mt_fix_const_field(report->field[i], usage);
1628 }
1629 }
1630
mt_release_contacts(struct hid_device * hid)1631 static void mt_release_contacts(struct hid_device *hid)
1632 {
1633 struct hid_input *hidinput;
1634 struct mt_application *application;
1635 struct mt_device *td = hid_get_drvdata(hid);
1636
1637 list_for_each_entry(hidinput, &hid->inputs, list) {
1638 struct input_dev *input_dev = hidinput->input;
1639 struct input_mt *mt = input_dev->mt;
1640 int i;
1641
1642 if (mt) {
1643 for (i = 0; i < mt->num_slots; i++) {
1644 input_mt_slot(input_dev, i);
1645 input_mt_report_slot_state(input_dev,
1646 MT_TOOL_FINGER,
1647 false);
1648 }
1649 input_mt_sync_frame(input_dev);
1650 input_sync(input_dev);
1651 }
1652 }
1653
1654 list_for_each_entry(application, &td->applications, list) {
1655 application->num_received = 0;
1656 }
1657 }
1658
mt_expired_timeout(struct timer_list * t)1659 static void mt_expired_timeout(struct timer_list *t)
1660 {
1661 struct mt_device *td = from_timer(td, t, release_timer);
1662 struct hid_device *hdev = td->hdev;
1663
1664 /*
1665 * An input report came in just before we release the sticky fingers,
1666 * it will take care of the sticky fingers.
1667 */
1668 if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1669 return;
1670 if (test_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags))
1671 mt_release_contacts(hdev);
1672 clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1673 }
1674
mt_probe(struct hid_device * hdev,const struct hid_device_id * id)1675 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1676 {
1677 int ret, i;
1678 struct mt_device *td;
1679 const struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1680
1681 for (i = 0; mt_classes[i].name ; i++) {
1682 if (id->driver_data == mt_classes[i].name) {
1683 mtclass = &(mt_classes[i]);
1684 break;
1685 }
1686 }
1687
1688 td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1689 if (!td) {
1690 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1691 return -ENOMEM;
1692 }
1693 td->hdev = hdev;
1694 td->mtclass = *mtclass;
1695 td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1696 hid_set_drvdata(hdev, td);
1697
1698 INIT_LIST_HEAD(&td->applications);
1699 INIT_LIST_HEAD(&td->reports);
1700
1701 if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1702 td->serial_maybe = true;
1703
1704 /* This allows the driver to correctly support devices
1705 * that emit events over several HID messages.
1706 */
1707 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1708
1709 /*
1710 * This allows the driver to handle different input sensors
1711 * that emits events through different applications on the same HID
1712 * device.
1713 */
1714 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1715
1716 if (id->group != HID_GROUP_MULTITOUCH_WIN_8)
1717 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1718
1719 timer_setup(&td->release_timer, mt_expired_timeout, 0);
1720
1721 ret = hid_parse(hdev);
1722 if (ret != 0)
1723 return ret;
1724
1725 if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
1726 mt_fix_const_fields(hdev, HID_DG_CONTACTID);
1727
1728 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1729 if (ret)
1730 return ret;
1731
1732 ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
1733 if (ret)
1734 dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
1735 hdev->name);
1736
1737 mt_set_modes(hdev, HID_LATENCY_NORMAL, true, true);
1738
1739 return 0;
1740 }
1741
1742 #ifdef CONFIG_PM
mt_reset_resume(struct hid_device * hdev)1743 static int mt_reset_resume(struct hid_device *hdev)
1744 {
1745 mt_release_contacts(hdev);
1746 mt_set_modes(hdev, HID_LATENCY_NORMAL, true, true);
1747 return 0;
1748 }
1749
mt_resume(struct hid_device * hdev)1750 static int mt_resume(struct hid_device *hdev)
1751 {
1752 /* Some Elan legacy devices require SET_IDLE to be set on resume.
1753 * It should be safe to send it to other devices too.
1754 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
1755
1756 hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
1757
1758 return 0;
1759 }
1760 #endif
1761
mt_remove(struct hid_device * hdev)1762 static void mt_remove(struct hid_device *hdev)
1763 {
1764 struct mt_device *td = hid_get_drvdata(hdev);
1765
1766 del_timer_sync(&td->release_timer);
1767
1768 sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
1769 hid_hw_stop(hdev);
1770 }
1771
1772 /*
1773 * This list contains only:
1774 * - VID/PID of products not working with the default multitouch handling
1775 * - 2 generic rules.
1776 * So there is no point in adding here any device with MT_CLS_DEFAULT.
1777 */
1778 static const struct hid_device_id mt_devices[] = {
1779
1780 /* 3M panels */
1781 { .driver_data = MT_CLS_3M,
1782 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1783 USB_DEVICE_ID_3M1968) },
1784 { .driver_data = MT_CLS_3M,
1785 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1786 USB_DEVICE_ID_3M2256) },
1787 { .driver_data = MT_CLS_3M,
1788 MT_USB_DEVICE(USB_VENDOR_ID_3M,
1789 USB_DEVICE_ID_3M3266) },
1790
1791 /* Alps devices */
1792 { .driver_data = MT_CLS_WIN_8_DUAL,
1793 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1794 USB_VENDOR_ID_ALPS_JP,
1795 HID_DEVICE_ID_ALPS_U1_DUAL_PTP) },
1796 { .driver_data = MT_CLS_WIN_8_DUAL,
1797 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1798 USB_VENDOR_ID_ALPS_JP,
1799 HID_DEVICE_ID_ALPS_U1_DUAL_3BTN_PTP) },
1800 { .driver_data = MT_CLS_WIN_8_DUAL,
1801 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1802 USB_VENDOR_ID_ALPS_JP,
1803 HID_DEVICE_ID_ALPS_1222) },
1804
1805 /* Lenovo X1 TAB Gen 2 */
1806 { .driver_data = MT_CLS_WIN_8_DUAL,
1807 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1808 USB_VENDOR_ID_LENOVO,
1809 USB_DEVICE_ID_LENOVO_X1_TAB) },
1810
1811 /* Lenovo X1 TAB Gen 3 */
1812 { .driver_data = MT_CLS_WIN_8_DUAL,
1813 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1814 USB_VENDOR_ID_LENOVO,
1815 USB_DEVICE_ID_LENOVO_X1_TAB3) },
1816
1817 /* Anton devices */
1818 { .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
1819 MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
1820 USB_DEVICE_ID_ANTON_TOUCH_PAD) },
1821
1822 /* Asus T304UA */
1823 { .driver_data = MT_CLS_ASUS,
1824 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
1825 USB_VENDOR_ID_ASUSTEK,
1826 USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD) },
1827
1828 /* Atmel panels */
1829 { .driver_data = MT_CLS_SERIAL,
1830 MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
1831 USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
1832
1833 /* Baanto multitouch devices */
1834 { .driver_data = MT_CLS_NSMU,
1835 MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
1836 USB_DEVICE_ID_BAANTO_MT_190W2) },
1837
1838 /* Cando panels */
1839 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1840 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1841 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
1842 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
1843 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
1844 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
1845
1846 /* Chunghwa Telecom touch panels */
1847 { .driver_data = MT_CLS_NSMU,
1848 MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
1849 USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
1850
1851 /* Cirque devices */
1852 { .driver_data = MT_CLS_WIN_8_DUAL,
1853 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
1854 I2C_VENDOR_ID_CIRQUE,
1855 I2C_PRODUCT_ID_CIRQUE_121F) },
1856
1857 /* CJTouch panels */
1858 { .driver_data = MT_CLS_NSMU,
1859 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1860 USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
1861 { .driver_data = MT_CLS_NSMU,
1862 MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
1863 USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
1864
1865 /* CVTouch panels */
1866 { .driver_data = MT_CLS_NSMU,
1867 MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
1868 USB_DEVICE_ID_CVTOUCH_SCREEN) },
1869
1870 /* eGalax devices (resistive) */
1871 { .driver_data = MT_CLS_EGALAX,
1872 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1873 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
1874 { .driver_data = MT_CLS_EGALAX,
1875 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1876 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
1877
1878 /* eGalax devices (capacitive) */
1879 { .driver_data = MT_CLS_EGALAX_SERIAL,
1880 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1881 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
1882 { .driver_data = MT_CLS_EGALAX,
1883 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1884 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
1885 { .driver_data = MT_CLS_EGALAX_SERIAL,
1886 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1887 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
1888 { .driver_data = MT_CLS_EGALAX_SERIAL,
1889 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1890 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
1891 { .driver_data = MT_CLS_EGALAX_SERIAL,
1892 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1893 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
1894 { .driver_data = MT_CLS_EGALAX_SERIAL,
1895 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1896 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
1897 { .driver_data = MT_CLS_EGALAX,
1898 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1899 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
1900 { .driver_data = MT_CLS_EGALAX,
1901 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1902 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
1903 { .driver_data = MT_CLS_EGALAX_SERIAL,
1904 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1905 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
1906 { .driver_data = MT_CLS_EGALAX,
1907 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1908 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
1909 { .driver_data = MT_CLS_EGALAX,
1910 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
1911 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
1912 { .driver_data = MT_CLS_EGALAX,
1913 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1914 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
1915 { .driver_data = MT_CLS_EGALAX,
1916 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1917 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
1918 { .driver_data = MT_CLS_EGALAX_SERIAL,
1919 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1920 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
1921 { .driver_data = MT_CLS_EGALAX_SERIAL,
1922 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1923 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
1924 { .driver_data = MT_CLS_EGALAX_SERIAL,
1925 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1926 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
1927 { .driver_data = MT_CLS_EGALAX,
1928 MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
1929 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_C002) },
1930
1931 /* Elitegroup panel */
1932 { .driver_data = MT_CLS_SERIAL,
1933 MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
1934 USB_DEVICE_ID_ELITEGROUP_05D8) },
1935
1936 /* Flatfrog Panels */
1937 { .driver_data = MT_CLS_FLATFROG,
1938 MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
1939 USB_DEVICE_ID_MULTITOUCH_3200) },
1940
1941 /* FocalTech Panels */
1942 { .driver_data = MT_CLS_SERIAL,
1943 MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
1944 USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
1945
1946 /* GeneralTouch panel */
1947 { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1948 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1949 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
1950 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1951 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1952 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
1953 { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
1954 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1955 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
1956 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1957 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1958 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
1959 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1960 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1961 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
1962 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1963 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1964 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
1965 { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
1966 MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
1967 USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
1968
1969 /* Gametel game controller */
1970 { .driver_data = MT_CLS_NSMU,
1971 MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
1972 USB_DEVICE_ID_GAMETEL_MT_MODE) },
1973
1974 /* GoodTouch panels */
1975 { .driver_data = MT_CLS_NSMU,
1976 MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
1977 USB_DEVICE_ID_GOODTOUCH_000f) },
1978
1979 /* Hanvon panels */
1980 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1981 MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
1982 USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
1983
1984 /* Ilitek dual touch panel */
1985 { .driver_data = MT_CLS_NSMU,
1986 MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
1987 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
1988
1989 /* LG Melfas panel */
1990 { .driver_data = MT_CLS_LG,
1991 HID_USB_DEVICE(USB_VENDOR_ID_LG,
1992 USB_DEVICE_ID_LG_MELFAS_MT) },
1993 { .driver_data = MT_CLS_LG,
1994 HID_DEVICE(BUS_I2C, HID_GROUP_GENERIC,
1995 USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_7010) },
1996
1997 /* MosArt panels */
1998 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
1999 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2000 USB_DEVICE_ID_ASUS_T91MT)},
2001 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2002 MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2003 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
2004 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2005 MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
2006 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
2007
2008 /* Novatek Panel */
2009 { .driver_data = MT_CLS_NSMU,
2010 MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
2011 USB_DEVICE_ID_NOVATEK_PCT) },
2012
2013 /* Ntrig Panel */
2014 { .driver_data = MT_CLS_NSMU,
2015 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2016 USB_VENDOR_ID_NTRIG, 0x1b05) },
2017
2018 /* Panasonic panels */
2019 { .driver_data = MT_CLS_PANASONIC,
2020 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2021 USB_DEVICE_ID_PANABOARD_UBT780) },
2022 { .driver_data = MT_CLS_PANASONIC,
2023 MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2024 USB_DEVICE_ID_PANABOARD_UBT880) },
2025
2026 /* PixArt optical touch screen */
2027 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2028 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2029 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
2030 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2031 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2032 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
2033 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2034 MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2035 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
2036
2037 /* PixCir-based panels */
2038 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2039 MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2040 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
2041
2042 /* Quanta-based panels */
2043 { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
2044 MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
2045 USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
2046
2047 /* Razer touchpads */
2048 { .driver_data = MT_CLS_RAZER_BLADE_STEALTH,
2049 HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2050 USB_VENDOR_ID_SYNAPTICS, 0x8323) },
2051
2052 /* Smart Tech panels */
2053 { .driver_data = MT_CLS_SMART_TECH,
2054 MT_USB_DEVICE(0x0b8c, 0x0092)},
2055
2056 /* Stantum panels */
2057 { .driver_data = MT_CLS_CONFIDENCE,
2058 MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
2059 USB_DEVICE_ID_MTP_STM)},
2060
2061 /* TopSeed panels */
2062 { .driver_data = MT_CLS_TOPSEED,
2063 MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
2064 USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
2065
2066 /* Touch International panels */
2067 { .driver_data = MT_CLS_NSMU,
2068 MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
2069 USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
2070
2071 /* Unitec panels */
2072 { .driver_data = MT_CLS_NSMU,
2073 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2074 USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
2075 { .driver_data = MT_CLS_NSMU,
2076 MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2077 USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
2078
2079 /* VTL panels */
2080 { .driver_data = MT_CLS_VTL,
2081 MT_USB_DEVICE(USB_VENDOR_ID_VTL,
2082 USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
2083
2084 /* Wistron panels */
2085 { .driver_data = MT_CLS_NSMU,
2086 MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
2087 USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
2088
2089 /* XAT */
2090 { .driver_data = MT_CLS_NSMU,
2091 MT_USB_DEVICE(USB_VENDOR_ID_XAT,
2092 USB_DEVICE_ID_XAT_CSR) },
2093
2094 /* Xiroku */
2095 { .driver_data = MT_CLS_NSMU,
2096 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2097 USB_DEVICE_ID_XIROKU_SPX) },
2098 { .driver_data = MT_CLS_NSMU,
2099 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2100 USB_DEVICE_ID_XIROKU_MPX) },
2101 { .driver_data = MT_CLS_NSMU,
2102 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2103 USB_DEVICE_ID_XIROKU_CSR) },
2104 { .driver_data = MT_CLS_NSMU,
2105 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2106 USB_DEVICE_ID_XIROKU_SPX1) },
2107 { .driver_data = MT_CLS_NSMU,
2108 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2109 USB_DEVICE_ID_XIROKU_MPX1) },
2110 { .driver_data = MT_CLS_NSMU,
2111 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2112 USB_DEVICE_ID_XIROKU_CSR1) },
2113 { .driver_data = MT_CLS_NSMU,
2114 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2115 USB_DEVICE_ID_XIROKU_SPX2) },
2116 { .driver_data = MT_CLS_NSMU,
2117 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2118 USB_DEVICE_ID_XIROKU_MPX2) },
2119 { .driver_data = MT_CLS_NSMU,
2120 MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2121 USB_DEVICE_ID_XIROKU_CSR2) },
2122
2123 /* Google MT devices */
2124 { .driver_data = MT_CLS_GOOGLE,
2125 HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_GOOGLE,
2126 USB_DEVICE_ID_GOOGLE_TOUCH_ROSE) },
2127 { .driver_data = MT_CLS_GOOGLE,
2128 HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, USB_VENDOR_ID_GOOGLE,
2129 USB_DEVICE_ID_GOOGLE_WHISKERS) },
2130
2131 /* Generic MT device */
2132 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
2133
2134 /* Generic Win 8 certified MT device */
2135 { .driver_data = MT_CLS_WIN_8,
2136 HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2137 HID_ANY_ID, HID_ANY_ID) },
2138 { }
2139 };
2140 MODULE_DEVICE_TABLE(hid, mt_devices);
2141
2142 static const struct hid_usage_id mt_grabbed_usages[] = {
2143 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
2144 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
2145 };
2146
2147 static struct hid_driver mt_driver = {
2148 .name = "hid-multitouch",
2149 .id_table = mt_devices,
2150 .probe = mt_probe,
2151 .remove = mt_remove,
2152 .input_mapping = mt_input_mapping,
2153 .input_mapped = mt_input_mapped,
2154 .input_configured = mt_input_configured,
2155 .feature_mapping = mt_feature_mapping,
2156 .usage_table = mt_grabbed_usages,
2157 .event = mt_event,
2158 .report = mt_report,
2159 #ifdef CONFIG_PM
2160 .reset_resume = mt_reset_resume,
2161 .resume = mt_resume,
2162 #endif
2163 };
2164 module_hid_driver(mt_driver);
2165