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