Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hid-multitouch.c
Go to the documentation of this file.
1 /*
2  * HID driver for multitouch panels
3  *
4  * Copyright (c) 2010-2012 Stephane Chatty <[email protected]>
5  * Copyright (c) 2010-2012 Benjamin Tissoires <[email protected]>
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 <[email protected]>
11  * Copyright (c) 2010 Henrik Rydberg <[email protected]>
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 <[email protected]>
17  * Copyright (c) 2010 Henrik Rydberg <[email protected]>
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 
38 MODULE_AUTHOR("Stephane Chatty <[email protected]>");
39 MODULE_AUTHOR("Benjamin Tissoires <[email protected]>");
40 MODULE_DESCRIPTION("HID multitouch panels");
41 MODULE_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 #define MT_QUIRK_NO_AREA (1 << 9)
55 
56 struct mt_slot {
57  __s32 x, y, p, w, h;
58  __s32 contactid; /* the device ContactID assigned to this slot */
59  bool touch_state; /* is the touch valid? */
60 };
61 
62 struct mt_class {
63  __s32 name; /* MT_CLS */
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 */
70  bool is_indirect; /* true for touchpads */
71 };
72 
73 struct mt_fields {
75  unsigned int length;
76 };
77 
78 struct 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  __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
86  __s8 inputmode_index; /* InputMode HID feature index in the report */
87  __s8 maxcontact_report_id; /* Maximum Contact Number HID feature,
88  -1 if non-existent */
89  __u8 num_received; /* how many contacts we received */
90  __u8 num_expected; /* expected last contact index */
92  __u8 touches_by_report; /* how many touches are present in one report:
93  * 1 means we should use a serial protocol
94  * > 1 means hybrid (multitouch) protocol */
95  bool serial_maybe; /* need to check for serial protocol */
96  bool curvalid; /* is the current contact valid? */
97  unsigned mt_flags; /* flags to pass to input-mt */
98 };
99 
100 /* classes of device behavior */
101 #define MT_CLS_DEFAULT 0x0001
102 
103 #define MT_CLS_SERIAL 0x0002
104 #define MT_CLS_CONFIDENCE 0x0003
105 #define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
106 #define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
107 #define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
108 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
109 #define MT_CLS_DUAL_NSMU_CONTACTID 0x0008
110 #define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
111 
112 /* vendor specific classes */
113 #define MT_CLS_3M 0x0101
114 #define MT_CLS_CYPRESS 0x0102
115 #define MT_CLS_EGALAX 0x0103
116 #define MT_CLS_EGALAX_SERIAL 0x0104
117 #define MT_CLS_TOPSEED 0x0105
118 #define MT_CLS_PANASONIC 0x0106
119 #define MT_CLS_FLATFROG 0x0107
120 #define MT_CLS_GENERALTOUCH_TWOFINGERS 0x0108
121 #define MT_CLS_GENERALTOUCH_PWT_TENFINGERS 0x0109
122 
123 #define MT_DEFAULT_MAXCONTACT 10
124 
125 #define MT_USB_DEVICE(v, p) HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
126 #define MT_BT_DEVICE(v, p) HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
127 
128 /*
129  * these device-dependent functions determine what slot corresponds
130  * to a valid contact that was just read.
131  */
132 
133 static int cypress_compute_slot(struct mt_device *td)
134 {
135  if (td->curdata.contactid != 0 || td->num_received == 0)
136  return td->curdata.contactid;
137  else
138  return -1;
139 }
140 
141 static struct mt_class mt_classes[] = {
142  { .name = MT_CLS_DEFAULT,
143  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
144  { .name = MT_CLS_SERIAL,
145  .quirks = MT_QUIRK_ALWAYS_VALID},
146  { .name = MT_CLS_CONFIDENCE,
147  .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
149  .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
151  { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
152  .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
155  .quirks = MT_QUIRK_VALID_IS_INRANGE |
157  .maxcontacts = 2 },
159  .quirks = MT_QUIRK_VALID_IS_INRANGE |
161  .maxcontacts = 2 },
162  { .name = MT_CLS_DUAL_NSMU_CONTACTID,
163  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
165  .maxcontacts = 2 },
167  .quirks = MT_QUIRK_VALID_IS_INRANGE |
169 
170  /*
171  * vendor specific classes
172  */
173  { .name = MT_CLS_3M,
174  .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
176  .sn_move = 2048,
177  .sn_width = 128,
178  .sn_height = 128,
179  .maxcontacts = 60,
180  },
181  { .name = MT_CLS_CYPRESS,
182  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
184  .maxcontacts = 10 },
185  { .name = MT_CLS_EGALAX,
186  .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
188  .sn_move = 4096,
189  .sn_pressure = 32,
190  },
191  { .name = MT_CLS_EGALAX_SERIAL,
192  .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
194  .sn_move = 4096,
195  .sn_pressure = 32,
196  },
197  { .name = MT_CLS_TOPSEED,
198  .quirks = MT_QUIRK_ALWAYS_VALID,
199  .is_indirect = true,
200  .maxcontacts = 2,
201  },
202  { .name = MT_CLS_PANASONIC,
203  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
204  .maxcontacts = 4 },
206  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
209  .maxcontacts = 2
210  },
212  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
214  },
215 
216  { .name = MT_CLS_FLATFROG,
217  .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
219  .sn_move = 2048,
220  .maxcontacts = 40,
221  },
222  { }
223 };
224 
225 static ssize_t mt_show_quirks(struct device *dev,
226  struct device_attribute *attr,
227  char *buf)
228 {
229  struct hid_device *hdev = container_of(dev, struct hid_device, dev);
230  struct mt_device *td = hid_get_drvdata(hdev);
231 
232  return sprintf(buf, "%u\n", td->mtclass.quirks);
233 }
234 
235 static ssize_t mt_set_quirks(struct device *dev,
236  struct device_attribute *attr,
237  const char *buf, size_t count)
238 {
239  struct hid_device *hdev = container_of(dev, struct hid_device, dev);
240  struct mt_device *td = hid_get_drvdata(hdev);
241 
242  unsigned long val;
243 
244  if (kstrtoul(buf, 0, &val))
245  return -EINVAL;
246 
247  td->mtclass.quirks = val;
248 
249  return count;
250 }
251 
252 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
253 
254 static struct attribute *sysfs_attrs[] = {
255  &dev_attr_quirks.attr,
256  NULL
257 };
258 
259 static struct attribute_group mt_attribute_group = {
260  .attrs = sysfs_attrs
261 };
262 
263 static void mt_feature_mapping(struct hid_device *hdev,
264  struct hid_field *field, struct hid_usage *usage)
265 {
266  struct mt_device *td = hid_get_drvdata(hdev);
267  int i;
268 
269  switch (usage->hid) {
270  case HID_DG_INPUTMODE:
271  td->inputmode = field->report->id;
272  td->inputmode_index = 0; /* has to be updated below */
273 
274  for (i=0; i < field->maxusage; i++) {
275  if (field->usage[i].hid == usage->hid) {
276  td->inputmode_index = i;
277  break;
278  }
279  }
280 
281  break;
282  case HID_DG_CONTACTMAX:
283  td->maxcontact_report_id = field->report->id;
284  td->maxcontacts = field->value[0];
285  if (td->mtclass.maxcontacts)
286  /* check if the maxcontacts is given by the class */
287  td->maxcontacts = td->mtclass.maxcontacts;
288 
289  break;
290  }
291 }
292 
293 static void set_abs(struct input_dev *input, unsigned int code,
294  struct hid_field *field, int snratio)
295 {
296  int fmin = field->logical_minimum;
297  int fmax = field->logical_maximum;
298  int fuzz = snratio ? (fmax - fmin) / snratio : 0;
299  input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
300 }
301 
302 static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
303  struct hid_input *hi)
304 {
305  struct mt_fields *f = td->fields;
306 
307  if (f->length >= HID_MAX_FIELDS)
308  return;
309 
310  f->usages[f->length++] = usage->hid;
311 }
312 
313 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
314  struct hid_field *field, struct hid_usage *usage,
315  unsigned long **bit, int *max)
316 {
317  struct mt_device *td = hid_get_drvdata(hdev);
318  struct mt_class *cls = &td->mtclass;
319  int code;
320 
321  /* Only map fields from TouchScreen or TouchPad collections.
322  * We need to ignore fields that belong to other collections
323  * such as Mouse that might have the same GenericDesktop usages. */
324  if (field->application == HID_DG_TOUCHSCREEN)
325  td->mt_flags |= INPUT_MT_DIRECT;
326  else if (field->application != HID_DG_TOUCHPAD)
327  return 0;
328 
329  /*
330  * Model touchscreens providing buttons as touchpads.
331  */
332  if (field->application == HID_DG_TOUCHPAD ||
333  (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
334  td->mt_flags |= INPUT_MT_POINTER;
335 
336  /* eGalax devices provide a Digitizer.Stylus input which overrides
337  * the correct Digitizers.Finger X/Y ranges.
338  * Let's just ignore this input. */
339  if (field->physical == HID_DG_STYLUS)
340  return -1;
341 
342  switch (usage->hid & HID_USAGE_PAGE) {
343 
344  case HID_UP_GENDESK:
345  switch (usage->hid) {
346  case HID_GD_X:
347  hid_map_usage(hi, usage, bit, max,
349  set_abs(hi->input, ABS_MT_POSITION_X, field,
350  cls->sn_move);
351  mt_store_field(usage, td, hi);
352  td->last_field_index = field->index;
353  return 1;
354  case HID_GD_Y:
355  hid_map_usage(hi, usage, bit, max,
357  set_abs(hi->input, ABS_MT_POSITION_Y, field,
358  cls->sn_move);
359  mt_store_field(usage, td, hi);
360  td->last_field_index = field->index;
361  return 1;
362  }
363  return 0;
364 
365  case HID_UP_DIGITIZER:
366  switch (usage->hid) {
367  case HID_DG_INRANGE:
368  mt_store_field(usage, td, hi);
369  td->last_field_index = field->index;
370  return 1;
371  case HID_DG_CONFIDENCE:
372  mt_store_field(usage, td, hi);
373  td->last_field_index = field->index;
374  return 1;
375  case HID_DG_TIPSWITCH:
376  hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
377  input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
378  mt_store_field(usage, td, hi);
379  td->last_field_index = field->index;
380  return 1;
381  case HID_DG_CONTACTID:
382  mt_store_field(usage, td, hi);
383  td->last_field_index = field->index;
384  td->touches_by_report++;
385  return 1;
386  case HID_DG_WIDTH:
387  hid_map_usage(hi, usage, bit, max,
389  if (!(cls->quirks & MT_QUIRK_NO_AREA))
390  set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
391  cls->sn_width);
392  mt_store_field(usage, td, hi);
393  td->last_field_index = field->index;
394  return 1;
395  case HID_DG_HEIGHT:
396  hid_map_usage(hi, usage, bit, max,
398  if (!(cls->quirks & MT_QUIRK_NO_AREA)) {
399  set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
400  cls->sn_height);
401  input_set_abs_params(hi->input,
402  ABS_MT_ORIENTATION, 0, 1, 0, 0);
403  }
404  mt_store_field(usage, td, hi);
405  td->last_field_index = field->index;
406  return 1;
407  case HID_DG_TIPPRESSURE:
408  hid_map_usage(hi, usage, bit, max,
410  set_abs(hi->input, ABS_MT_PRESSURE, field,
411  cls->sn_pressure);
412  mt_store_field(usage, td, hi);
413  td->last_field_index = field->index;
414  return 1;
415  case HID_DG_CONTACTCOUNT:
416  td->last_field_index = field->index;
417  return 1;
418  case HID_DG_CONTACTMAX:
419  /* we don't set td->last_slot_field as contactcount and
420  * contact max are global to the report */
421  td->last_field_index = field->index;
422  return -1;
423  case HID_DG_TOUCH:
424  /* Legacy devices use TIPSWITCH and not TOUCH.
425  * Let's just ignore this field. */
426  return -1;
427  }
428  /* let hid-input decide for the others */
429  return 0;
430 
431  case HID_UP_BUTTON:
432  code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
433  hid_map_usage(hi, usage, bit, max, EV_KEY, code);
434  input_set_capability(hi->input, EV_KEY, code);
435  return 1;
436 
437  case 0xff000000:
438  /* we do not want to map these: no input-oriented meaning */
439  return -1;
440  }
441 
442  return 0;
443 }
444 
445 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
446  struct hid_field *field, struct hid_usage *usage,
447  unsigned long **bit, int *max)
448 {
449  if (usage->type == EV_KEY || usage->type == EV_ABS)
450  set_bit(usage->type, hi->input->evbit);
451 
452  return -1;
453 }
454 
455 static int mt_compute_slot(struct mt_device *td, struct input_dev *input)
456 {
457  __s32 quirks = td->mtclass.quirks;
458 
459  if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
460  return td->curdata.contactid;
461 
462  if (quirks & MT_QUIRK_CYPRESS)
463  return cypress_compute_slot(td);
464 
465  if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
466  return td->num_received;
467 
469  return td->curdata.contactid - 1;
470 
471  return input_mt_get_slot_by_key(input, td->curdata.contactid);
472 }
473 
474 /*
475  * this function is called when a whole contact has been processed,
476  * so that it can assign it to a slot and store the data there
477  */
478 static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
479 {
480  if (td->curvalid) {
481  int slotnum = mt_compute_slot(td, input);
482  struct mt_slot *s = &td->curdata;
483 
484  if (slotnum < 0 || slotnum >= td->maxcontacts)
485  return;
486 
487  input_mt_slot(input, slotnum);
489  s->touch_state);
490  if (s->touch_state) {
491  /* this finger is on the screen */
492  int wide = (s->w > s->h);
493  /* divided by two to match visual scale of touch */
494  int major = max(s->w, s->h) >> 1;
495  int minor = min(s->w, s->h) >> 1;
496 
497  input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
498  input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
499  input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
500  input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
501  input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
502  input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
503  }
504  }
505 
506  td->num_received++;
507 }
508 
509 /*
510  * this function is called when a whole packet has been received and processed,
511  * so that it can decide what to send to the input layer.
512  */
513 static void mt_sync_frame(struct mt_device *td, struct input_dev *input)
514 {
515  input_mt_sync_frame(input);
516  input_sync(input);
517  td->num_received = 0;
518 }
519 
520 static int mt_event(struct hid_device *hid, struct hid_field *field,
521  struct hid_usage *usage, __s32 value)
522 {
523  struct mt_device *td = hid_get_drvdata(hid);
524  __s32 quirks = td->mtclass.quirks;
525 
526  if (hid->claimed & HID_CLAIMED_INPUT) {
527  switch (usage->hid) {
528  case HID_DG_INRANGE:
529  if (quirks & MT_QUIRK_ALWAYS_VALID)
530  td->curvalid = true;
531  else if (quirks & MT_QUIRK_VALID_IS_INRANGE)
532  td->curvalid = value;
533  break;
534  case HID_DG_TIPSWITCH:
535  if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
536  td->curvalid = value;
537  td->curdata.touch_state = value;
538  break;
539  case HID_DG_CONFIDENCE:
540  if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
541  td->curvalid = value;
542  break;
543  case HID_DG_CONTACTID:
544  td->curdata.contactid = value;
545  break;
546  case HID_DG_TIPPRESSURE:
547  td->curdata.p = value;
548  break;
549  case HID_GD_X:
550  td->curdata.x = value;
551  break;
552  case HID_GD_Y:
553  td->curdata.y = value;
554  break;
555  case HID_DG_WIDTH:
556  td->curdata.w = value;
557  break;
558  case HID_DG_HEIGHT:
559  td->curdata.h = value;
560  break;
561  case HID_DG_CONTACTCOUNT:
562  /*
563  * Includes multi-packet support where subsequent
564  * packets are sent with zero contactcount.
565  */
566  if (value)
567  td->num_expected = value;
568  break;
569  case HID_DG_TOUCH:
570  /* do nothing */
571  break;
572 
573  default:
574  /* fallback to the generic hidinput handling */
575  return 0;
576  }
577 
578  if (usage->hid == td->last_slot_field)
579  mt_complete_slot(td, field->hidinput->input);
580 
581  if (field->index == td->last_field_index
582  && td->num_received >= td->num_expected)
583  mt_sync_frame(td, field->hidinput->input);
584 
585  }
586 
587  /* we have handled the hidinput part, now remains hiddev */
588  if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
589  hid->hiddev_hid_event(hid, field, usage, value);
590 
591  return 1;
592 }
593 
594 static void mt_set_input_mode(struct hid_device *hdev)
595 {
596  struct mt_device *td = hid_get_drvdata(hdev);
597  struct hid_report *r;
598  struct hid_report_enum *re;
599 
600  if (td->inputmode < 0)
601  return;
602 
603  re = &(hdev->report_enum[HID_FEATURE_REPORT]);
604  r = re->report_id_hash[td->inputmode];
605  if (r) {
606  r->field[0]->value[td->inputmode_index] = 0x02;
608  }
609 }
610 
611 static void mt_set_maxcontacts(struct hid_device *hdev)
612 {
613  struct mt_device *td = hid_get_drvdata(hdev);
614  struct hid_report *r;
615  struct hid_report_enum *re;
616  int fieldmax, max;
617 
618  if (td->maxcontact_report_id < 0)
619  return;
620 
621  if (!td->mtclass.maxcontacts)
622  return;
623 
624  re = &hdev->report_enum[HID_FEATURE_REPORT];
626  if (r) {
627  max = td->mtclass.maxcontacts;
628  fieldmax = r->field[0]->logical_maximum;
629  max = min(fieldmax, max);
630  if (r->field[0]->value[0] != max) {
631  r->field[0]->value[0] = max;
633  }
634  }
635 }
636 
637 static void mt_post_parse_default_settings(struct mt_device *td)
638 {
639  __s32 quirks = td->mtclass.quirks;
640 
641  /* unknown serial device needs special quirks */
642  if (td->touches_by_report == 1) {
643  quirks |= MT_QUIRK_ALWAYS_VALID;
644  quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
645  quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
646  quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
647  }
648 
649  td->mtclass.quirks = quirks;
650 }
651 
652 static void mt_post_parse(struct mt_device *td)
653 {
654  struct mt_fields *f = td->fields;
655 
656  if (td->touches_by_report > 0) {
657  int field_count_per_touch = f->length / td->touches_by_report;
658  td->last_slot_field = f->usages[field_count_per_touch - 1];
659  }
660 }
661 
662 static void mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
663 
664 {
665  struct mt_device *td = hid_get_drvdata(hdev);
666  struct mt_class *cls = &td->mtclass;
667  struct input_dev *input = hi->input;
668 
669  /* Only initialize slots for MT input devices */
670  if (!test_bit(ABS_MT_POSITION_X, input->absbit))
671  return;
672 
673  if (!td->maxcontacts)
675 
676  mt_post_parse(td);
677  if (td->serial_maybe)
678  mt_post_parse_default_settings(td);
679 
680  if (cls->is_indirect)
681  td->mt_flags |= INPUT_MT_POINTER;
682 
683  if (cls->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
685 
686  input_mt_init_slots(input, td->maxcontacts, td->mt_flags);
687 
688  td->mt_flags = 0;
689 }
690 
691 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
692 {
693  int ret, i;
694  struct mt_device *td;
695  struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
696 
697  for (i = 0; mt_classes[i].name ; i++) {
698  if (id->driver_data == mt_classes[i].name) {
699  mtclass = &(mt_classes[i]);
700  break;
701  }
702  }
703 
704  /* This allows the driver to correctly support devices
705  * that emit events over several HID messages.
706  */
708 
709  td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
710  if (!td) {
711  dev_err(&hdev->dev, "cannot allocate multitouch data\n");
712  return -ENOMEM;
713  }
714  td->mtclass = *mtclass;
715  td->inputmode = -1;
716  td->maxcontact_report_id = -1;
717  hid_set_drvdata(hdev, td);
718 
719  td->fields = kzalloc(sizeof(struct mt_fields), GFP_KERNEL);
720  if (!td->fields) {
721  dev_err(&hdev->dev, "cannot allocate multitouch fields data\n");
722  ret = -ENOMEM;
723  goto fail;
724  }
725 
726  if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
727  td->serial_maybe = true;
728 
729  ret = hid_parse(hdev);
730  if (ret != 0)
731  goto fail;
732 
733  ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
734  if (ret)
735  goto fail;
736 
737  ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
738 
739  mt_set_maxcontacts(hdev);
740  mt_set_input_mode(hdev);
741 
742  kfree(td->fields);
743  td->fields = NULL;
744 
745  return 0;
746 
747 fail:
748  kfree(td->fields);
749  kfree(td);
750  return ret;
751 }
752 
753 #ifdef CONFIG_PM
754 static int mt_reset_resume(struct hid_device *hdev)
755 {
756  mt_set_maxcontacts(hdev);
757  mt_set_input_mode(hdev);
758  return 0;
759 }
760 
761 static int mt_resume(struct hid_device *hdev)
762 {
763  struct usb_interface *intf;
764  struct usb_host_interface *interface;
765  struct usb_device *dev;
766 
767  if (hdev->bus != BUS_USB)
768  return 0;
769 
770  intf = to_usb_interface(hdev->dev.parent);
771  interface = intf->cur_altsetting;
772  dev = hid_to_usb_dev(hdev);
773 
774  /* Some Elan legacy devices require SET_IDLE to be set on resume.
775  * It should be safe to send it to other devices too.
776  * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
777 
778  usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
781  0, interface->desc.bInterfaceNumber,
782  NULL, 0, USB_CTRL_SET_TIMEOUT);
783 
784  return 0;
785 }
786 #endif
787 
788 static void mt_remove(struct hid_device *hdev)
789 {
790  struct mt_device *td = hid_get_drvdata(hdev);
791  sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
792  hid_hw_stop(hdev);
793  kfree(td);
794  hid_set_drvdata(hdev, NULL);
795 }
796 
797 static const struct hid_device_id mt_devices[] = {
798 
799  /* 3M panels */
800  { .driver_data = MT_CLS_3M,
803  { .driver_data = MT_CLS_3M,
806  { .driver_data = MT_CLS_3M,
809 
810  /* ActionStar panels */
811  { .driver_data = MT_CLS_DEFAULT,
814 
815  /* Atmel panels */
816  { .driver_data = MT_CLS_SERIAL,
819  { .driver_data = MT_CLS_SERIAL,
822 
823  /* Baanto multitouch devices */
824  { .driver_data = MT_CLS_DEFAULT,
827  /* Cando panels */
828  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
831  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
834  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
837  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
840 
841  /* Chunghwa Telecom touch panels */
842  { .driver_data = MT_CLS_DEFAULT,
845 
846  /* CVTouch panels */
847  { .driver_data = MT_CLS_DEFAULT,
850 
851  /* Cypress panel */
852  { .driver_data = MT_CLS_CYPRESS,
855 
856  /* eGalax devices (resistive) */
857  { .driver_data = MT_CLS_EGALAX,
860  { .driver_data = MT_CLS_EGALAX,
863 
864  /* eGalax devices (capacitive) */
865  { .driver_data = MT_CLS_EGALAX,
868  { .driver_data = MT_CLS_EGALAX_SERIAL,
871  { .driver_data = MT_CLS_EGALAX_SERIAL,
874  { .driver_data = MT_CLS_EGALAX_SERIAL,
877  { .driver_data = MT_CLS_EGALAX_SERIAL,
880  { .driver_data = MT_CLS_EGALAX,
883  { .driver_data = MT_CLS_EGALAX_SERIAL,
886  { .driver_data = MT_CLS_EGALAX,
889  { .driver_data = MT_CLS_EGALAX_SERIAL,
892  { .driver_data = MT_CLS_EGALAX,
895  { .driver_data = MT_CLS_EGALAX,
898  { .driver_data = MT_CLS_EGALAX_SERIAL,
901  { .driver_data = MT_CLS_EGALAX_SERIAL,
904  { .driver_data = MT_CLS_EGALAX_SERIAL,
907  { .driver_data = MT_CLS_EGALAX,
910  { .driver_data = MT_CLS_EGALAX,
913  { .driver_data = MT_CLS_EGALAX,
916 
917  /* Elo TouchSystems IntelliTouch Plus panel */
918  { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
921 
922  /* Flatfrog Panels */
923  { .driver_data = MT_CLS_FLATFROG,
926 
927  /* GeneralTouch panel */
928  { .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
931  { .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
934 
935  /* Gametel game controller */
936  { .driver_data = MT_CLS_DEFAULT,
939 
940  /* GoodTouch panels */
941  { .driver_data = MT_CLS_DEFAULT,
944 
945  /* Hanvon panels */
946  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
949 
950  /* Ideacom panel */
951  { .driver_data = MT_CLS_SERIAL,
954  { .driver_data = MT_CLS_SERIAL,
957 
958  /* Ilitek dual touch panel */
959  { .driver_data = MT_CLS_DEFAULT,
962 
963  /* IRTOUCH panels */
964  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
967 
968  /* LG Display panels */
969  { .driver_data = MT_CLS_DEFAULT,
972 
973  /* Lumio panels */
974  { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
977  { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
980 
981  /* MosArt panels */
982  { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
985  { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
988  { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
991 
992  /* Panasonic panels */
993  { .driver_data = MT_CLS_PANASONIC,
996  { .driver_data = MT_CLS_PANASONIC,
999 
1000  /* Novatek Panel */
1001  { .driver_data = MT_CLS_DEFAULT,
1004 
1005  /* PenMount panels */
1006  { .driver_data = MT_CLS_CONFIDENCE,
1009 
1010  /* PixArt optical touch screen */
1011  { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1014  { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1017  { .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
1020 
1021  /* PixCir-based panels */
1022  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1025  { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
1028 
1029  /* Quanta-based panels */
1030  { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1033  { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1036  { .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
1039 
1040  /* Stantum panels */
1041  { .driver_data = MT_CLS_CONFIDENCE,
1044  { .driver_data = MT_CLS_CONFIDENCE,
1047  { .driver_data = MT_CLS_CONFIDENCE,
1050 
1051  /* TopSeed panels */
1052  { .driver_data = MT_CLS_TOPSEED,
1055 
1056  /* Touch International panels */
1057  { .driver_data = MT_CLS_DEFAULT,
1060 
1061  /* Unitec panels */
1062  { .driver_data = MT_CLS_DEFAULT,
1065  { .driver_data = MT_CLS_DEFAULT,
1068  /* XAT */
1069  { .driver_data = MT_CLS_DEFAULT,
1072 
1073  /* Xiroku */
1074  { .driver_data = MT_CLS_DEFAULT,
1077  { .driver_data = MT_CLS_DEFAULT,
1080  { .driver_data = MT_CLS_DEFAULT,
1083  { .driver_data = MT_CLS_DEFAULT,
1086  { .driver_data = MT_CLS_DEFAULT,
1089  { .driver_data = MT_CLS_DEFAULT,
1092  { .driver_data = MT_CLS_DEFAULT,
1095  { .driver_data = MT_CLS_DEFAULT,
1098  { .driver_data = MT_CLS_DEFAULT,
1101 
1102  /* Zytronic panels */
1103  { .driver_data = MT_CLS_SERIAL,
1106 
1107  /* Generic MT device */
1109  { }
1110 };
1111 MODULE_DEVICE_TABLE(hid, mt_devices);
1112 
1113 static const struct hid_usage_id mt_grabbed_usages[] = {
1114  { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1115  { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
1116 };
1117 
1118 static struct hid_driver mt_driver = {
1119  .name = "hid-multitouch",
1120  .id_table = mt_devices,
1121  .probe = mt_probe,
1122  .remove = mt_remove,
1123  .input_mapping = mt_input_mapping,
1124  .input_mapped = mt_input_mapped,
1125  .input_configured = mt_input_configured,
1126  .feature_mapping = mt_feature_mapping,
1127  .usage_table = mt_grabbed_usages,
1128  .event = mt_event,
1129 #ifdef CONFIG_PM
1130  .reset_resume = mt_reset_resume,
1131  .resume = mt_resume,
1132 #endif
1133 };
1134 
1135 static int __init mt_init(void)
1136 {
1137  return hid_register_driver(&mt_driver);
1138 }
1139 
1140 static void __exit mt_exit(void)
1141 {
1142  hid_unregister_driver(&mt_driver);
1143 }
1144 
1145 module_init(mt_init);
1146 module_exit(mt_exit);