blob: 0e9857064f4698ff15060b6482879fe02cd62a92 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * HID driver for multitouch panels
3 *
4 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
5 * Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6 * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
7 *
8 * This code is partly based on hid-egalax.c:
9 *
10 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
11 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
12 * Copyright (c) 2010 Canonical, Ltd.
13 *
14 * This code is partly based on hid-3m-pct.c:
15 *
16 * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
17 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
18 * Copyright (c) 2010 Canonical, Ltd.
19 *
20 */
21
22/*
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the Free
25 * Software Foundation; either version 2 of the License, or (at your option)
26 * any later version.
27 */
28
29#include <linux/device.h>
30#include <linux/hid.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/usb.h>
34#include <linux/input/mt.h>
35#include "usbhid/usbhid.h"
36
37
38MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
39MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
40MODULE_DESCRIPTION("HID multitouch panels");
41MODULE_LICENSE("GPL");
42
43#include "hid-ids.h"
44
45/* quirks to control the device */
46#define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
47#define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
48#define MT_QUIRK_CYPRESS (1 << 2)
49#define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
50#define MT_QUIRK_ALWAYS_VALID (1 << 4)
51#define MT_QUIRK_VALID_IS_INRANGE (1 << 5)
52#define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 6)
53#define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE (1 << 8)
54
55struct mt_slot {
56 __s32 x, y, p, w, h;
57 __s32 contactid; /* the device ContactID assigned to this slot */
58 bool touch_state; /* is the touch valid? */
59 bool seen_in_this_frame;/* has this slot been updated */
60};
61
62struct mt_class {
63 __s32 name; /* MT_CLS */
64 __s32 quirks;
65 __s32 sn_move; /* Signal/noise ratio for move events */
66 __s32 sn_width; /* Signal/noise ratio for width events */
67 __s32 sn_height; /* Signal/noise ratio for height events */
68 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
69 __u8 maxcontacts;
70 bool is_indirect; /* true for touchpads */
71};
72
73struct mt_fields {
74 unsigned usages[HID_MAX_FIELDS];
75 unsigned int length;
76};
77
78struct mt_device {
79 struct mt_slot curdata; /* placeholder of incoming data */
80 struct mt_class mtclass; /* our mt device class */
81 struct mt_fields *fields; /* temporary placeholder for storing the
82 multitouch fields */
83 unsigned last_field_index; /* last field index of the report */
84 unsigned last_slot_field; /* the last field of a slot */
85 __s16 inputmode; /* InputMode HID feature, -1 if non-existent */
86 __s16 maxcontact_report_id; /* Maximum Contact Number HID feature,
87 -1 if non-existent */
88 __u8 num_received; /* how many contacts we received */
89 __u8 num_expected; /* expected last contact index */
90 __u8 maxcontacts;
91 __u8 touches_by_report; /* how many touches are present in one report:
92 * 1 means we should use a serial protocol
93 * > 1 means hybrid (multitouch) protocol */
94 bool curvalid; /* is the current contact valid? */
95 struct mt_slot *slots;
96};
97
98/* classes of device behavior */
99#define MT_CLS_DEFAULT 0x0001
100
101#define MT_CLS_SERIAL 0x0002
102#define MT_CLS_CONFIDENCE 0x0003
103#define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
104#define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
105#define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
106#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
107#define MT_CLS_DUAL_NSMU_CONTACTID 0x0008
108#define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
109
110/* vendor specific classes */
111#define MT_CLS_3M 0x0101
112#define MT_CLS_CYPRESS 0x0102
113#define MT_CLS_EGALAX 0x0103
114#define MT_CLS_EGALAX_SERIAL 0x0104
115#define MT_CLS_TOPSEED 0x0105
116#define MT_CLS_PANASONIC 0x0106
117
118#define MT_DEFAULT_MAXCONTACT 10
119
120/*
121 * these device-dependent functions determine what slot corresponds
122 * to a valid contact that was just read.
123 */
124
125static int cypress_compute_slot(struct mt_device *td)
126{
127 if (td->curdata.contactid != 0 || td->num_received == 0)
128 return td->curdata.contactid;
129 else
130 return -1;
131}
132
133static int find_slot_from_contactid(struct mt_device *td)
134{
135 int i;
136 for (i = 0; i < td->maxcontacts; ++i) {
137 if (td->slots[i].contactid == td->curdata.contactid &&
138 td->slots[i].touch_state)
139 return i;
140 }
141 for (i = 0; i < td->maxcontacts; ++i) {
142 if (!td->slots[i].seen_in_this_frame &&
143 !td->slots[i].touch_state)
144 return i;
145 }
146 /* should not occurs. If this happens that means
147 * that the device sent more touches that it says
148 * in the report descriptor. It is ignored then. */
149 return -1;
150}
151
152static struct mt_class mt_classes[] = {
153 { .name = MT_CLS_DEFAULT,
154 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
155 { .name = MT_CLS_SERIAL,
156 .quirks = MT_QUIRK_ALWAYS_VALID},
157 { .name = MT_CLS_CONFIDENCE,
158 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
159 { .name = MT_CLS_CONFIDENCE_CONTACT_ID,
160 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
161 MT_QUIRK_SLOT_IS_CONTACTID },
162 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
163 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
164 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
165 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
166 .quirks = MT_QUIRK_VALID_IS_INRANGE |
167 MT_QUIRK_SLOT_IS_CONTACTID,
168 .maxcontacts = 2 },
169 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
170 .quirks = MT_QUIRK_VALID_IS_INRANGE |
171 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
172 .maxcontacts = 2 },
173 { .name = MT_CLS_DUAL_NSMU_CONTACTID,
174 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
175 MT_QUIRK_SLOT_IS_CONTACTID,
176 .maxcontacts = 2 },
177 { .name = MT_CLS_INRANGE_CONTACTNUMBER,
178 .quirks = MT_QUIRK_VALID_IS_INRANGE |
179 MT_QUIRK_SLOT_IS_CONTACTNUMBER },
180
181 /*
182 * vendor specific classes
183 */
184 { .name = MT_CLS_3M,
185 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
186 MT_QUIRK_SLOT_IS_CONTACTID,
187 .sn_move = 2048,
188 .sn_width = 128,
189 .sn_height = 128 },
190 { .name = MT_CLS_CYPRESS,
191 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
192 MT_QUIRK_CYPRESS,
193 .maxcontacts = 10 },
194 { .name = MT_CLS_EGALAX,
195 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
196 MT_QUIRK_VALID_IS_INRANGE,
197 .sn_move = 4096,
198 .sn_pressure = 32,
199 },
200 { .name = MT_CLS_EGALAX_SERIAL,
201 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
202 MT_QUIRK_ALWAYS_VALID,
203 .sn_move = 4096,
204 .sn_pressure = 32,
205 },
206 { .name = MT_CLS_TOPSEED,
207 .quirks = MT_QUIRK_ALWAYS_VALID,
208 .is_indirect = true,
209 .maxcontacts = 2,
210 },
211 { .name = MT_CLS_PANASONIC,
212 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
213 .maxcontacts = 4 },
214
215 { }
216};
217
218static ssize_t mt_show_quirks(struct device *dev,
219 struct device_attribute *attr,
220 char *buf)
221{
222 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
223 struct mt_device *td = hid_get_drvdata(hdev);
224
225 return sprintf(buf, "%u\n", td->mtclass.quirks);
226}
227
228static ssize_t mt_set_quirks(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t count)
231{
232 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
233 struct mt_device *td = hid_get_drvdata(hdev);
234
235 unsigned long val;
236
237 if (kstrtoul(buf, 0, &val))
238 return -EINVAL;
239
240 td->mtclass.quirks = val;
241
242 return count;
243}
244
245static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
246
247static struct attribute *sysfs_attrs[] = {
248 &dev_attr_quirks.attr,
249 NULL
250};
251
252static struct attribute_group mt_attribute_group = {
253 .attrs = sysfs_attrs
254};
255
256static void mt_feature_mapping(struct hid_device *hdev,
257 struct hid_field *field, struct hid_usage *usage)
258{
259 struct mt_device *td = hid_get_drvdata(hdev);
260
261 switch (usage->hid) {
262 case HID_DG_INPUTMODE:
263 td->inputmode = field->report->id;
264 break;
265 case HID_DG_CONTACTMAX:
266 td->maxcontact_report_id = field->report->id;
267 td->maxcontacts = field->value[0];
268 if (td->mtclass.maxcontacts)
269 /* check if the maxcontacts is given by the class */
270 td->maxcontacts = td->mtclass.maxcontacts;
271
272 break;
273 }
274}
275
276static void set_abs(struct input_dev *input, unsigned int code,
277 struct hid_field *field, int snratio)
278{
279 int fmin = field->logical_minimum;
280 int fmax = field->logical_maximum;
281 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
282 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
283}
284
285static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
286 struct hid_input *hi)
287{
288 struct mt_fields *f = td->fields;
289
290 if (f->length >= HID_MAX_FIELDS)
291 return;
292
293 f->usages[f->length++] = usage->hid;
294}
295
296static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
297 struct hid_field *field, struct hid_usage *usage,
298 unsigned long **bit, int *max)
299{
300 struct mt_device *td = hid_get_drvdata(hdev);
301 struct mt_class *cls = &td->mtclass;
302 int code;
303
304 /* Only map fields from TouchScreen or TouchPad collections.
305 * We need to ignore fields that belong to other collections
306 * such as Mouse that might have the same GenericDesktop usages. */
307 if (field->application == HID_DG_TOUCHSCREEN)
308 set_bit(INPUT_PROP_DIRECT, hi->input->propbit);
309 else if (field->application != HID_DG_TOUCHPAD)
310 return 0;
311
312 /* In case of an indirect device (touchpad), we need to add
313 * specific BTN_TOOL_* to be handled by the synaptics xorg
314 * driver.
315 * We also consider that touchscreens providing buttons are touchpads.
316 */
317 if (field->application == HID_DG_TOUCHPAD ||
318 (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON ||
319 cls->is_indirect) {
320 set_bit(INPUT_PROP_POINTER, hi->input->propbit);
321 set_bit(BTN_TOOL_FINGER, hi->input->keybit);
322 set_bit(BTN_TOOL_DOUBLETAP, hi->input->keybit);
323 set_bit(BTN_TOOL_TRIPLETAP, hi->input->keybit);
324 set_bit(BTN_TOOL_QUADTAP, hi->input->keybit);
325 }
326
327 /* eGalax devices provide a Digitizer.Stylus input which overrides
328 * the correct Digitizers.Finger X/Y ranges.
329 * Let's just ignore this input. */
330 if (field->physical == HID_DG_STYLUS)
331 return -1;
332
333 /* Only map fields from TouchScreen or TouchPad collections.
334 * We need to ignore fields that belong to other collections
335 * such as Mouse that might have the same GenericDesktop usages. */
336 if (field->application == HID_DG_TOUCHSCREEN)
337 set_bit(INPUT_PROP_DIRECT, hi->input->propbit);
338 else if (field->application == HID_DG_TOUCHPAD)
339 set_bit(INPUT_PROP_POINTER, hi->input->propbit);
340 else
341 return 0;
342
343 switch (usage->hid & HID_USAGE_PAGE) {
344
345 case HID_UP_GENDESK:
346 switch (usage->hid) {
347 case HID_GD_X:
348 hid_map_usage(hi, usage, bit, max,
349 EV_ABS, ABS_MT_POSITION_X);
350 set_abs(hi->input, ABS_MT_POSITION_X, field,
351 cls->sn_move);
352 /* touchscreen emulation */
353 set_abs(hi->input, ABS_X, field, cls->sn_move);
354 mt_store_field(usage, td, hi);
355 td->last_field_index = field->index;
356 return 1;
357 case HID_GD_Y:
358 hid_map_usage(hi, usage, bit, max,
359 EV_ABS, ABS_MT_POSITION_Y);
360 set_abs(hi->input, ABS_MT_POSITION_Y, field,
361 cls->sn_move);
362 /* touchscreen emulation */
363 set_abs(hi->input, ABS_Y, field, cls->sn_move);
364 mt_store_field(usage, td, hi);
365 td->last_field_index = field->index;
366 return 1;
367 }
368 return 0;
369
370 case HID_UP_DIGITIZER:
371 switch (usage->hid) {
372 case HID_DG_INRANGE:
373 mt_store_field(usage, td, hi);
374 td->last_field_index = field->index;
375 return 1;
376 case HID_DG_CONFIDENCE:
377 mt_store_field(usage, td, hi);
378 td->last_field_index = field->index;
379 return 1;
380 case HID_DG_TIPSWITCH:
381 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
382 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
383 mt_store_field(usage, td, hi);
384 td->last_field_index = field->index;
385 return 1;
386 case HID_DG_CONTACTID:
387 if (!td->maxcontacts)
388 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
389 input_mt_init_slots(hi->input, td->maxcontacts);
390 mt_store_field(usage, td, hi);
391 td->last_field_index = field->index;
392 td->touches_by_report++;
393 return 1;
394 case HID_DG_WIDTH:
395 hid_map_usage(hi, usage, bit, max,
396 EV_ABS, ABS_MT_TOUCH_MAJOR);
397 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
398 cls->sn_width);
399 mt_store_field(usage, td, hi);
400 td->last_field_index = field->index;
401 return 1;
402 case HID_DG_HEIGHT:
403 hid_map_usage(hi, usage, bit, max,
404 EV_ABS, ABS_MT_TOUCH_MINOR);
405 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
406 cls->sn_height);
407 input_set_abs_params(hi->input,
408 ABS_MT_ORIENTATION, 0, 1, 0, 0);
409 mt_store_field(usage, td, hi);
410 td->last_field_index = field->index;
411 return 1;
412 case HID_DG_TIPPRESSURE:
413 hid_map_usage(hi, usage, bit, max,
414 EV_ABS, ABS_MT_PRESSURE);
415 set_abs(hi->input, ABS_MT_PRESSURE, field,
416 cls->sn_pressure);
417 /* touchscreen emulation */
418 set_abs(hi->input, ABS_PRESSURE, field,
419 cls->sn_pressure);
420 mt_store_field(usage, td, hi);
421 td->last_field_index = field->index;
422 return 1;
423 case HID_DG_CONTACTCOUNT:
424 td->last_field_index = field->index;
425 return 1;
426 case HID_DG_CONTACTMAX:
427 /* we don't set td->last_slot_field as contactcount and
428 * contact max are global to the report */
429 td->last_field_index = field->index;
430 return -1;
431 }
432 case HID_DG_TOUCH:
433 /* Legacy devices use TIPSWITCH and not TOUCH.
434 * Let's just ignore this field. */
435 return -1;
436 /* let hid-input decide for the others */
437 return 0;
438
439 case HID_UP_BUTTON:
440 code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
441 hid_map_usage(hi, usage, bit, max, EV_KEY, code);
442 input_set_capability(hi->input, EV_KEY, code);
443 return 1;
444
445 case 0xff000000:
446 /* we do not want to map these: no input-oriented meaning */
447 return -1;
448 }
449
450 return 0;
451}
452
453static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
454 struct hid_field *field, struct hid_usage *usage,
455 unsigned long **bit, int *max)
456{
457 if (usage->type == EV_KEY || usage->type == EV_ABS)
458 set_bit(usage->type, hi->input->evbit);
459
460 return -1;
461}
462
463static int mt_compute_slot(struct mt_device *td)
464{
465 __s32 quirks = td->mtclass.quirks;
466
467 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
468 return td->curdata.contactid;
469
470 if (quirks & MT_QUIRK_CYPRESS)
471 return cypress_compute_slot(td);
472
473 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
474 return td->num_received;
475
476 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
477 return td->curdata.contactid - 1;
478
479 return find_slot_from_contactid(td);
480}
481
482/*
483 * this function is called when a whole contact has been processed,
484 * so that it can assign it to a slot and store the data there
485 */
486static void mt_complete_slot(struct mt_device *td)
487{
488 td->curdata.seen_in_this_frame = true;
489 if (td->curvalid) {
490 int slotnum = mt_compute_slot(td);
491
492 if (slotnum >= 0 && slotnum < td->maxcontacts)
493 td->slots[slotnum] = td->curdata;
494 }
495 td->num_received++;
496}
497
498
499/*
500 * this function is called when a whole packet has been received and processed,
501 * so that it can decide what to send to the input layer.
502 */
503static void mt_emit_event(struct mt_device *td, struct input_dev *input)
504{
505 int i;
506
507 for (i = 0; i < td->maxcontacts; ++i) {
508 struct mt_slot *s = &(td->slots[i]);
509 if ((td->mtclass.quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
510 !s->seen_in_this_frame) {
511 s->touch_state = false;
512 }
513
514 input_mt_slot(input, i);
515 input_mt_report_slot_state(input, MT_TOOL_FINGER,
516 s->touch_state);
517 if (s->touch_state) {
518 /* this finger is on the screen */
519 int wide = (s->w > s->h);
520 /* divided by two to match visual scale of touch */
521 int major = max(s->w, s->h) >> 1;
522 int minor = min(s->w, s->h) >> 1;
523
524 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
525 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
526 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
527 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
528 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
529 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
530 }
531 s->seen_in_this_frame = false;
532
533 }
534
535 input_mt_report_pointer_emulation(input, true);
536 input_sync(input);
537 td->num_received = 0;
538}
539
540
541
542static int mt_event(struct hid_device *hid, struct hid_field *field,
543 struct hid_usage *usage, __s32 value)
544{
545 struct mt_device *td = hid_get_drvdata(hid);
546 __s32 quirks = td->mtclass.quirks;
547
548 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
549 switch (usage->hid) {
550 case HID_DG_INRANGE:
551 if (quirks & MT_QUIRK_ALWAYS_VALID)
552 td->curvalid = true;
553 else if (quirks & MT_QUIRK_VALID_IS_INRANGE)
554 td->curvalid = value;
555 break;
556 case HID_DG_TIPSWITCH:
557 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
558 td->curvalid = value;
559 td->curdata.touch_state = value;
560 break;
561 case HID_DG_CONFIDENCE:
562 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
563 td->curvalid = value;
564 break;
565 case HID_DG_CONTACTID:
566 td->curdata.contactid = value;
567 break;
568 case HID_DG_TIPPRESSURE:
569 td->curdata.p = value;
570 break;
571 case HID_GD_X:
572 td->curdata.x = value;
573 break;
574 case HID_GD_Y:
575 td->curdata.y = value;
576 break;
577 case HID_DG_WIDTH:
578 td->curdata.w = value;
579 break;
580 case HID_DG_HEIGHT:
581 td->curdata.h = value;
582 break;
583 case HID_DG_CONTACTCOUNT:
584 /*
585 * Includes multi-packet support where subsequent
586 * packets are sent with zero contactcount.
587 */
588 if (value)
589 td->num_expected = value;
590 break;
591 case HID_DG_TOUCH:
592 /* do nothing */
593 break;
594
595 default:
596 /* fallback to the generic hidinput handling */
597 return 0;
598 }
599
600 if (usage->hid == td->last_slot_field)
601 mt_complete_slot(td);
602
603 if (field->index == td->last_field_index
604 && td->num_received >= td->num_expected)
605 mt_emit_event(td, field->hidinput->input);
606
607 }
608
609 /* we have handled the hidinput part, now remains hiddev */
610 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
611 hid->hiddev_hid_event(hid, field, usage, value);
612
613 return 1;
614}
615
616static void mt_set_input_mode(struct hid_device *hdev)
617{
618 struct mt_device *td = hid_get_drvdata(hdev);
619 struct hid_report *r;
620 struct hid_report_enum *re;
621
622 if (td->inputmode < 0)
623 return;
624
625 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
626 r = re->report_id_hash[td->inputmode];
627 if (r) {
628 r->field[0]->value[0] = 0x02;
629 usbhid_submit_report(hdev, r, USB_DIR_OUT);
630 }
631}
632
633static void mt_set_maxcontacts(struct hid_device *hdev)
634{
635 struct mt_device *td = hid_get_drvdata(hdev);
636 struct hid_report *r;
637 struct hid_report_enum *re;
638 int fieldmax, max;
639
640 if (td->maxcontact_report_id < 0)
641 return;
642
643 if (!td->mtclass.maxcontacts)
644 return;
645
646 re = &hdev->report_enum[HID_FEATURE_REPORT];
647 r = re->report_id_hash[td->maxcontact_report_id];
648 if (r) {
649 max = td->mtclass.maxcontacts;
650 fieldmax = r->field[0]->logical_maximum;
651 max = min(fieldmax, max);
652 if (r->field[0]->value[0] != max) {
653 r->field[0]->value[0] = max;
654 usbhid_submit_report(hdev, r, USB_DIR_OUT);
655 }
656 }
657}
658
659static void mt_post_parse(struct mt_device *td)
660{
661 struct mt_fields *f = td->fields;
662
663 if (td->touches_by_report > 0) {
664 int field_count_per_touch = f->length / td->touches_by_report;
665 td->last_slot_field = f->usages[field_count_per_touch - 1];
666 }
667}
668
669static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
670{
671 int ret, i;
672 struct mt_device *td;
673 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
674
675 if (id) {
676 for (i = 0; mt_classes[i].name ; i++) {
677 if (id->driver_data == mt_classes[i].name) {
678 mtclass = &(mt_classes[i]);
679 break;
680 }
681 }
682 }
683
684 /* This allows the driver to correctly support devices
685 * that emit events over several HID messages.
686 */
687 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
688 hdev->quirks &= ~HID_QUIRK_MULTITOUCH;
689
690 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
691 if (!td) {
692 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
693 return -ENOMEM;
694 }
695 td->mtclass = *mtclass;
696 td->inputmode = -1;
697 td->maxcontact_report_id = -1;
698 hid_set_drvdata(hdev, td);
699
700 td->fields = kzalloc(sizeof(struct mt_fields), GFP_KERNEL);
701 if (!td->fields) {
702 dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
703 ret = -ENOMEM;
704 goto fail;
705 }
706
707 ret = hid_parse(hdev);
708 if (ret != 0)
709 goto fail;
710
711 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
712 if (ret)
713 goto fail;
714
715 mt_post_parse(td);
716
717 if (!id && td->touches_by_report == 1) {
718 /* the device has been sent by hid-generic */
719 mtclass = &td->mtclass;
720 mtclass->quirks |= MT_QUIRK_ALWAYS_VALID;
721 mtclass->quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
722 mtclass->quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
723 mtclass->quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
724 }
725
726 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
727 GFP_KERNEL);
728 if (!td->slots) {
729 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
730 hid_hw_stop(hdev);
731 ret = -ENOMEM;
732 goto fail;
733 }
734
735 ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
736
737 mt_set_maxcontacts(hdev);
738 mt_set_input_mode(hdev);
739
740 kfree(td->fields);
741 td->fields = NULL;
742
743 return 0;
744
745fail:
746 kfree(td->fields);
747 kfree(td);
748 return ret;
749}
750
751#ifdef CONFIG_PM
752static int mt_reset_resume(struct hid_device *hdev)
753{
754 mt_set_maxcontacts(hdev);
755 mt_set_input_mode(hdev);
756 return 0;
757}
758#endif
759
760static void mt_remove(struct hid_device *hdev)
761{
762 struct mt_device *td = hid_get_drvdata(hdev);
763 sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
764 hid_hw_stop(hdev);
765 kfree(td->slots);
766 kfree(td);
767 hid_set_drvdata(hdev, NULL);
768}
769
770static const struct hid_device_id mt_devices[] = {
771
772 /* 3M panels */
773 { .driver_data = MT_CLS_3M,
774 HID_USB_DEVICE(USB_VENDOR_ID_3M,
775 USB_DEVICE_ID_3M1968) },
776 { .driver_data = MT_CLS_3M,
777 HID_USB_DEVICE(USB_VENDOR_ID_3M,
778 USB_DEVICE_ID_3M2256) },
779 { .driver_data = MT_CLS_3M,
780 HID_USB_DEVICE(USB_VENDOR_ID_3M,
781 USB_DEVICE_ID_3M3266) },
782
783 /* ActionStar panels */
784 { .driver_data = MT_CLS_DEFAULT,
785 HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR,
786 USB_DEVICE_ID_ACTIONSTAR_1011) },
787
788 /* Atmel panels */
789 { .driver_data = MT_CLS_SERIAL,
790 HID_USB_DEVICE(USB_VENDOR_ID_ATMEL,
791 USB_DEVICE_ID_ATMEL_MULTITOUCH) },
792 { .driver_data = MT_CLS_SERIAL,
793 HID_USB_DEVICE(USB_VENDOR_ID_ATMEL,
794 USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
795
796 /* Baanto multitouch devices */
797 { .driver_data = MT_CLS_DEFAULT,
798 HID_USB_DEVICE(USB_VENDOR_ID_BAANTO,
799 USB_DEVICE_ID_BAANTO_MT_190W2) },
800 /* Cando panels */
801 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
802 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
803 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
804 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
805 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
806 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
807 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
808 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
809 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
810 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
811 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
812 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
813
814 /* Chunghwa Telecom touch panels */
815 { .driver_data = MT_CLS_DEFAULT,
816 HID_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
817 USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
818
819 /* CVTouch panels */
820 { .driver_data = MT_CLS_DEFAULT,
821 HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
822 USB_DEVICE_ID_CVTOUCH_SCREEN) },
823
824 /* Cypress panel */
825 { .driver_data = MT_CLS_CYPRESS,
826 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
827 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
828
829 /* eGalax devices (resistive) */
830 { .driver_data = MT_CLS_EGALAX,
831 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
832 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
833 { .driver_data = MT_CLS_EGALAX,
834 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
835 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
836
837 /* eGalax devices (capacitive) */
838 { .driver_data = MT_CLS_EGALAX,
839 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
840 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
841 { .driver_data = MT_CLS_EGALAX_SERIAL,
842 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
843 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
844 { .driver_data = MT_CLS_EGALAX_SERIAL,
845 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
846 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
847 { .driver_data = MT_CLS_EGALAX_SERIAL,
848 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
849 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
850 { .driver_data = MT_CLS_EGALAX_SERIAL,
851 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
852 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
853 { .driver_data = MT_CLS_EGALAX,
854 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
855 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
856 { .driver_data = MT_CLS_EGALAX_SERIAL,
857 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
858 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
859 { .driver_data = MT_CLS_EGALAX,
860 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
861 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
862 { .driver_data = MT_CLS_EGALAX_SERIAL,
863 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
864 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
865 { .driver_data = MT_CLS_EGALAX,
866 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
867 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
868 { .driver_data = MT_CLS_EGALAX,
869 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
870 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
871 { .driver_data = MT_CLS_EGALAX_SERIAL,
872 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
873 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
874 { .driver_data = MT_CLS_EGALAX_SERIAL,
875 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
876 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
877
878 /* Elo TouchSystems IntelliTouch Plus panel */
879 { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
880 HID_USB_DEVICE(USB_VENDOR_ID_ELO,
881 USB_DEVICE_ID_ELO_TS2515) },
882
883 /* GeneralTouch panel */
884 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
885 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
886 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
887
888 /* Gametel game controller */
889 { .driver_data = MT_CLS_DEFAULT,
890 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_FRUCTEL,
891 USB_DEVICE_ID_GAMETEL_MT_MODE) },
892
893 /* GoodTouch panels */
894 { .driver_data = MT_CLS_DEFAULT,
895 HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
896 USB_DEVICE_ID_GOODTOUCH_000f) },
897
898 /* Hanvon panels */
899 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
900 HID_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
901 USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
902
903 /* Ideacom panel */
904 { .driver_data = MT_CLS_SERIAL,
905 HID_USB_DEVICE(USB_VENDOR_ID_IDEACOM,
906 USB_DEVICE_ID_IDEACOM_IDC6650) },
907 { .driver_data = MT_CLS_SERIAL,
908 HID_USB_DEVICE(USB_VENDOR_ID_IDEACOM,
909 USB_DEVICE_ID_IDEACOM_IDC6651) },
910
911 /* Ilitek dual touch panel */
912 { .driver_data = MT_CLS_DEFAULT,
913 HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
914 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
915
916 /* IRTOUCH panels */
917 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
918 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
919 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
920
921 /* LG Display panels */
922 { .driver_data = MT_CLS_DEFAULT,
923 HID_USB_DEVICE(USB_VENDOR_ID_LG,
924 USB_DEVICE_ID_LG_MULTITOUCH) },
925
926 /* Lumio panels */
927 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
928 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
929 USB_DEVICE_ID_CRYSTALTOUCH) },
930 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
931 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
932 USB_DEVICE_ID_CRYSTALTOUCH_DUAL) },
933
934 /* MosArt panels */
935 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
936 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
937 USB_DEVICE_ID_ASUS_T91MT)},
938 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
939 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
940 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
941 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
942 HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
943 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
944
945 /* Panasonic panels */
946 { .driver_data = MT_CLS_PANASONIC,
947 HID_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
948 USB_DEVICE_ID_PANABOARD_UBT780) },
949 { .driver_data = MT_CLS_PANASONIC,
950 HID_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
951 USB_DEVICE_ID_PANABOARD_UBT880) },
952
953 /* Novatek Panel */
954 { .driver_data = MT_CLS_DEFAULT,
955 HID_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
956 USB_DEVICE_ID_NOVATEK_PCT) },
957
958 /* PenMount panels */
959 { .driver_data = MT_CLS_CONFIDENCE,
960 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
961 USB_DEVICE_ID_PENMOUNT_PCI) },
962
963 /* PixArt optical touch screen */
964 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
965 HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
966 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
967 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
968 HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
969 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
970 { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
971 HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
972 USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
973
974 /* PixCir-based panels */
975 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
976 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
977 USB_DEVICE_ID_HANVON_MULTITOUCH) },
978 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
979 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
980 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
981
982 /* Quanta-based panels */
983 { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
984 HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
985 USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
986 { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
987 HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
988 USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
989 { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
990 HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
991 USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3008) },
992
993 /* Stantum panels */
994 { .driver_data = MT_CLS_CONFIDENCE,
995 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
996 USB_DEVICE_ID_MTP)},
997 { .driver_data = MT_CLS_CONFIDENCE,
998 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
999 USB_DEVICE_ID_MTP_STM)},
1000 { .driver_data = MT_CLS_CONFIDENCE,
1001 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX,
1002 USB_DEVICE_ID_MTP_SITRONIX)},
1003
1004 /* TopSeed panels */
1005 { .driver_data = MT_CLS_TOPSEED,
1006 HID_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
1007 USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
1008
1009 /* Touch International panels */
1010 { .driver_data = MT_CLS_DEFAULT,
1011 HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
1012 USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
1013
1014 /* Unitec panels */
1015 { .driver_data = MT_CLS_DEFAULT,
1016 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1017 USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
1018 { .driver_data = MT_CLS_DEFAULT,
1019 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
1020 USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
1021 /* XAT */
1022 { .driver_data = MT_CLS_DEFAULT,
1023 HID_USB_DEVICE(USB_VENDOR_ID_XAT,
1024 USB_DEVICE_ID_XAT_CSR) },
1025
1026 /* Xiroku */
1027 { .driver_data = MT_CLS_DEFAULT,
1028 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1029 USB_DEVICE_ID_XIROKU_SPX) },
1030 { .driver_data = MT_CLS_DEFAULT,
1031 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1032 USB_DEVICE_ID_XIROKU_MPX) },
1033 { .driver_data = MT_CLS_DEFAULT,
1034 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1035 USB_DEVICE_ID_XIROKU_CSR) },
1036 { .driver_data = MT_CLS_DEFAULT,
1037 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1038 USB_DEVICE_ID_XIROKU_SPX1) },
1039 { .driver_data = MT_CLS_DEFAULT,
1040 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1041 USB_DEVICE_ID_XIROKU_MPX1) },
1042 { .driver_data = MT_CLS_DEFAULT,
1043 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1044 USB_DEVICE_ID_XIROKU_CSR1) },
1045 { .driver_data = MT_CLS_DEFAULT,
1046 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1047 USB_DEVICE_ID_XIROKU_SPX2) },
1048 { .driver_data = MT_CLS_DEFAULT,
1049 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1050 USB_DEVICE_ID_XIROKU_MPX2) },
1051 { .driver_data = MT_CLS_DEFAULT,
1052 HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
1053 USB_DEVICE_ID_XIROKU_CSR2) },
1054
1055 { }
1056};
1057MODULE_DEVICE_TABLE(hid, mt_devices);
1058
1059static const struct hid_usage_id mt_grabbed_usages[] = {
1060 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1061 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
1062};
1063
1064static struct hid_driver mt_driver = {
1065 .name = "hid-multitouch",
1066 .id_table = mt_devices,
1067 .probe = mt_probe,
1068 .remove = mt_remove,
1069 .input_mapping = mt_input_mapping,
1070 .input_mapped = mt_input_mapped,
1071 .feature_mapping = mt_feature_mapping,
1072 .usage_table = mt_grabbed_usages,
1073 .event = mt_event,
1074#ifdef CONFIG_PM
1075 .reset_resume = mt_reset_resume,
1076#endif
1077};
1078
1079static int __init mt_init(void)
1080{
1081 return hid_register_driver(&mt_driver);
1082}
1083
1084static void __exit mt_exit(void)
1085{
1086 hid_unregister_driver(&mt_driver);
1087}
1088
1089module_init(mt_init);
1090module_exit(mt_exit);