xref: /linux/drivers/hid/hid-multitouch.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
1 /*
2  *  HID driver for multitouch panels
3  *
4  *  Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr>
5  *  Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6  *  Copyright (c) 2010-2011 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 
38 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
39 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
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 
55 struct 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 
62 struct 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 };
71 
72 struct mt_device {
73 	struct mt_slot curdata;	/* placeholder of incoming data */
74 	struct mt_class mtclass;	/* our mt device class */
75 	unsigned last_field_index;	/* last field index of the report */
76 	unsigned last_slot_field;	/* the last field of a slot */
77 	int last_mt_collection;	/* last known mt-related collection */
78 	__s8 inputmode;		/* InputMode HID feature, -1 if non-existent */
79 	__u8 num_received;	/* how many contacts we received */
80 	__u8 num_expected;	/* expected last contact index */
81 	__u8 maxcontacts;
82 	bool curvalid;		/* is the current contact valid? */
83 	struct mt_slot *slots;
84 };
85 
86 /* classes of device behavior */
87 #define MT_CLS_DEFAULT				0x0001
88 
89 #define MT_CLS_SERIAL				0x0002
90 #define MT_CLS_CONFIDENCE			0x0003
91 #define MT_CLS_CONFIDENCE_CONTACT_ID		0x0004
92 #define MT_CLS_CONFIDENCE_MINUS_ONE		0x0005
93 #define MT_CLS_DUAL_INRANGE_CONTACTID		0x0006
94 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER	0x0007
95 #define MT_CLS_DUAL_NSMU_CONTACTID		0x0008
96 #define MT_CLS_INRANGE_CONTACTNUMBER		0x0009
97 
98 /* vendor specific classes */
99 #define MT_CLS_3M				0x0101
100 #define MT_CLS_CYPRESS				0x0102
101 #define MT_CLS_EGALAX				0x0103
102 #define MT_CLS_EGALAX_SERIAL			0x0104
103 
104 #define MT_DEFAULT_MAXCONTACT	10
105 
106 /*
107  * these device-dependent functions determine what slot corresponds
108  * to a valid contact that was just read.
109  */
110 
111 static int cypress_compute_slot(struct mt_device *td)
112 {
113 	if (td->curdata.contactid != 0 || td->num_received == 0)
114 		return td->curdata.contactid;
115 	else
116 		return -1;
117 }
118 
119 static int find_slot_from_contactid(struct mt_device *td)
120 {
121 	int i;
122 	for (i = 0; i < td->maxcontacts; ++i) {
123 		if (td->slots[i].contactid == td->curdata.contactid &&
124 			td->slots[i].touch_state)
125 			return i;
126 	}
127 	for (i = 0; i < td->maxcontacts; ++i) {
128 		if (!td->slots[i].seen_in_this_frame &&
129 			!td->slots[i].touch_state)
130 			return i;
131 	}
132 	/* should not occurs. If this happens that means
133 	 * that the device sent more touches that it says
134 	 * in the report descriptor. It is ignored then. */
135 	return -1;
136 }
137 
138 static struct mt_class mt_classes[] = {
139 	{ .name = MT_CLS_DEFAULT,
140 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
141 	{ .name = MT_CLS_SERIAL,
142 		.quirks = MT_QUIRK_ALWAYS_VALID},
143 	{ .name = MT_CLS_CONFIDENCE,
144 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
145 	{ .name = MT_CLS_CONFIDENCE_CONTACT_ID,
146 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
147 			MT_QUIRK_SLOT_IS_CONTACTID },
148 	{ .name = MT_CLS_CONFIDENCE_MINUS_ONE,
149 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
150 			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
151 	{ .name = MT_CLS_DUAL_INRANGE_CONTACTID,
152 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
153 			MT_QUIRK_SLOT_IS_CONTACTID,
154 		.maxcontacts = 2 },
155 	{ .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
156 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
157 			MT_QUIRK_SLOT_IS_CONTACTNUMBER,
158 		.maxcontacts = 2 },
159 	{ .name = MT_CLS_DUAL_NSMU_CONTACTID,
160 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
161 			MT_QUIRK_SLOT_IS_CONTACTID,
162 		.maxcontacts = 2 },
163 	{ .name = MT_CLS_INRANGE_CONTACTNUMBER,
164 		.quirks = MT_QUIRK_VALID_IS_INRANGE |
165 			MT_QUIRK_SLOT_IS_CONTACTNUMBER },
166 
167 	/*
168 	 * vendor specific classes
169 	 */
170 	{ .name = MT_CLS_3M,
171 		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
172 			MT_QUIRK_SLOT_IS_CONTACTID,
173 		.sn_move = 2048,
174 		.sn_width = 128,
175 		.sn_height = 128 },
176 	{ .name = MT_CLS_CYPRESS,
177 		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
178 			MT_QUIRK_CYPRESS,
179 		.maxcontacts = 10 },
180 	{ .name = MT_CLS_EGALAX,
181 		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
182 			MT_QUIRK_VALID_IS_INRANGE,
183 		.sn_move = 4096,
184 		.sn_pressure = 32,
185 	},
186 	{ .name = MT_CLS_EGALAX_SERIAL,
187 		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
188 			MT_QUIRK_ALWAYS_VALID,
189 		.sn_move = 4096,
190 		.sn_pressure = 32,
191 	},
192 
193 	{ }
194 };
195 
196 static ssize_t mt_show_quirks(struct device *dev,
197 			   struct device_attribute *attr,
198 			   char *buf)
199 {
200 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
201 	struct mt_device *td = hid_get_drvdata(hdev);
202 
203 	return sprintf(buf, "%u\n", td->mtclass.quirks);
204 }
205 
206 static ssize_t mt_set_quirks(struct device *dev,
207 			  struct device_attribute *attr,
208 			  const char *buf, size_t count)
209 {
210 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
211 	struct mt_device *td = hid_get_drvdata(hdev);
212 
213 	unsigned long val;
214 
215 	if (kstrtoul(buf, 0, &val))
216 		return -EINVAL;
217 
218 	td->mtclass.quirks = val;
219 
220 	return count;
221 }
222 
223 static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
224 
225 static struct attribute *sysfs_attrs[] = {
226 	&dev_attr_quirks.attr,
227 	NULL
228 };
229 
230 static struct attribute_group mt_attribute_group = {
231 	.attrs = sysfs_attrs
232 };
233 
234 static void mt_feature_mapping(struct hid_device *hdev,
235 		struct hid_field *field, struct hid_usage *usage)
236 {
237 	struct mt_device *td = hid_get_drvdata(hdev);
238 
239 	switch (usage->hid) {
240 	case HID_DG_INPUTMODE:
241 		td->inputmode = field->report->id;
242 		break;
243 	case HID_DG_CONTACTMAX:
244 		td->maxcontacts = field->value[0];
245 		if (td->mtclass.maxcontacts)
246 			/* check if the maxcontacts is given by the class */
247 			td->maxcontacts = td->mtclass.maxcontacts;
248 
249 		break;
250 	}
251 }
252 
253 static void set_abs(struct input_dev *input, unsigned int code,
254 		struct hid_field *field, int snratio)
255 {
256 	int fmin = field->logical_minimum;
257 	int fmax = field->logical_maximum;
258 	int fuzz = snratio ? (fmax - fmin) / snratio : 0;
259 	input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
260 }
261 
262 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
263 		struct hid_field *field, struct hid_usage *usage,
264 		unsigned long **bit, int *max)
265 {
266 	struct mt_device *td = hid_get_drvdata(hdev);
267 	struct mt_class *cls = &td->mtclass;
268 
269 	/* Only map fields from TouchScreen or TouchPad collections.
270          * We need to ignore fields that belong to other collections
271          * such as Mouse that might have the same GenericDesktop usages. */
272 	if (field->application == HID_DG_TOUCHSCREEN)
273 		set_bit(INPUT_PROP_DIRECT, hi->input->propbit);
274 	else if (field->application == HID_DG_TOUCHPAD)
275 		set_bit(INPUT_PROP_POINTER, hi->input->propbit);
276 	else
277 		return 0;
278 
279 	/* eGalax devices provide a Digitizer.Stylus input which overrides
280 	 * the correct Digitizers.Finger X/Y ranges.
281 	 * Let's just ignore this input. */
282 	if (field->physical == HID_DG_STYLUS)
283 		return -1;
284 
285 	switch (usage->hid & HID_USAGE_PAGE) {
286 
287 	case HID_UP_GENDESK:
288 		switch (usage->hid) {
289 		case HID_GD_X:
290 			hid_map_usage(hi, usage, bit, max,
291 					EV_ABS, ABS_MT_POSITION_X);
292 			set_abs(hi->input, ABS_MT_POSITION_X, field,
293 				cls->sn_move);
294 			/* touchscreen emulation */
295 			set_abs(hi->input, ABS_X, field, cls->sn_move);
296 			if (td->last_mt_collection == usage->collection_index) {
297 				td->last_slot_field = usage->hid;
298 				td->last_field_index = field->index;
299 			}
300 			return 1;
301 		case HID_GD_Y:
302 			hid_map_usage(hi, usage, bit, max,
303 					EV_ABS, ABS_MT_POSITION_Y);
304 			set_abs(hi->input, ABS_MT_POSITION_Y, field,
305 				cls->sn_move);
306 			/* touchscreen emulation */
307 			set_abs(hi->input, ABS_Y, field, cls->sn_move);
308 			if (td->last_mt_collection == usage->collection_index) {
309 				td->last_slot_field = usage->hid;
310 				td->last_field_index = field->index;
311 			}
312 			return 1;
313 		}
314 		return 0;
315 
316 	case HID_UP_DIGITIZER:
317 		switch (usage->hid) {
318 		case HID_DG_INRANGE:
319 			if (td->last_mt_collection == usage->collection_index) {
320 				td->last_slot_field = usage->hid;
321 				td->last_field_index = field->index;
322 			}
323 			return 1;
324 		case HID_DG_CONFIDENCE:
325 			if (td->last_mt_collection == usage->collection_index) {
326 				td->last_slot_field = usage->hid;
327 				td->last_field_index = field->index;
328 			}
329 			return 1;
330 		case HID_DG_TIPSWITCH:
331 			hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
332 			input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
333 			if (td->last_mt_collection == usage->collection_index) {
334 				td->last_slot_field = usage->hid;
335 				td->last_field_index = field->index;
336 			}
337 			return 1;
338 		case HID_DG_CONTACTID:
339 			if (!td->maxcontacts)
340 				td->maxcontacts = MT_DEFAULT_MAXCONTACT;
341 			input_mt_init_slots(hi->input, td->maxcontacts);
342 			td->last_slot_field = usage->hid;
343 			td->last_field_index = field->index;
344 			td->last_mt_collection = usage->collection_index;
345 			return 1;
346 		case HID_DG_WIDTH:
347 			hid_map_usage(hi, usage, bit, max,
348 					EV_ABS, ABS_MT_TOUCH_MAJOR);
349 			set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
350 				cls->sn_width);
351 			if (td->last_mt_collection == usage->collection_index) {
352 				td->last_slot_field = usage->hid;
353 				td->last_field_index = field->index;
354 			}
355 			return 1;
356 		case HID_DG_HEIGHT:
357 			hid_map_usage(hi, usage, bit, max,
358 					EV_ABS, ABS_MT_TOUCH_MINOR);
359 			set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
360 				cls->sn_height);
361 			input_set_abs_params(hi->input,
362 					ABS_MT_ORIENTATION, 0, 1, 0, 0);
363 			if (td->last_mt_collection == usage->collection_index) {
364 				td->last_slot_field = usage->hid;
365 				td->last_field_index = field->index;
366 			}
367 			return 1;
368 		case HID_DG_TIPPRESSURE:
369 			hid_map_usage(hi, usage, bit, max,
370 					EV_ABS, ABS_MT_PRESSURE);
371 			set_abs(hi->input, ABS_MT_PRESSURE, field,
372 				cls->sn_pressure);
373 			/* touchscreen emulation */
374 			set_abs(hi->input, ABS_PRESSURE, field,
375 				cls->sn_pressure);
376 			if (td->last_mt_collection == usage->collection_index) {
377 				td->last_slot_field = usage->hid;
378 				td->last_field_index = field->index;
379 			}
380 			return 1;
381 		case HID_DG_CONTACTCOUNT:
382 			if (td->last_mt_collection == usage->collection_index)
383 				td->last_field_index = field->index;
384 			return 1;
385 		case HID_DG_CONTACTMAX:
386 			/* we don't set td->last_slot_field as contactcount and
387 			 * contact max are global to the report */
388 			if (td->last_mt_collection == usage->collection_index)
389 				td->last_field_index = field->index;
390 			return -1;
391 		}
392 		/* let hid-input decide for the others */
393 		return 0;
394 
395 	case 0xff000000:
396 		/* we do not want to map these: no input-oriented meaning */
397 		return -1;
398 	}
399 
400 	return 0;
401 }
402 
403 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
404 		struct hid_field *field, struct hid_usage *usage,
405 		unsigned long **bit, int *max)
406 {
407 	if (usage->type == EV_KEY || usage->type == EV_ABS)
408 		set_bit(usage->type, hi->input->evbit);
409 
410 	return -1;
411 }
412 
413 static int mt_compute_slot(struct mt_device *td)
414 {
415 	__s32 quirks = td->mtclass.quirks;
416 
417 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
418 		return td->curdata.contactid;
419 
420 	if (quirks & MT_QUIRK_CYPRESS)
421 		return cypress_compute_slot(td);
422 
423 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
424 		return td->num_received;
425 
426 	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
427 		return td->curdata.contactid - 1;
428 
429 	return find_slot_from_contactid(td);
430 }
431 
432 /*
433  * this function is called when a whole contact has been processed,
434  * so that it can assign it to a slot and store the data there
435  */
436 static void mt_complete_slot(struct mt_device *td)
437 {
438 	td->curdata.seen_in_this_frame = true;
439 	if (td->curvalid) {
440 		int slotnum = mt_compute_slot(td);
441 
442 		if (slotnum >= 0 && slotnum < td->maxcontacts)
443 			td->slots[slotnum] = td->curdata;
444 	}
445 	td->num_received++;
446 }
447 
448 
449 /*
450  * this function is called when a whole packet has been received and processed,
451  * so that it can decide what to send to the input layer.
452  */
453 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
454 {
455 	int i;
456 
457 	for (i = 0; i < td->maxcontacts; ++i) {
458 		struct mt_slot *s = &(td->slots[i]);
459 		if ((td->mtclass.quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
460 			!s->seen_in_this_frame) {
461 			s->touch_state = false;
462 		}
463 
464 		input_mt_slot(input, i);
465 		input_mt_report_slot_state(input, MT_TOOL_FINGER,
466 			s->touch_state);
467 		if (s->touch_state) {
468 			/* this finger is on the screen */
469 			int wide = (s->w > s->h);
470 			/* divided by two to match visual scale of touch */
471 			int major = max(s->w, s->h) >> 1;
472 			int minor = min(s->w, s->h) >> 1;
473 
474 			input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
475 			input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
476 			input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
477 			input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
478 			input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
479 			input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
480 		}
481 		s->seen_in_this_frame = false;
482 
483 	}
484 
485 	input_mt_report_pointer_emulation(input, true);
486 	input_sync(input);
487 	td->num_received = 0;
488 }
489 
490 
491 
492 static int mt_event(struct hid_device *hid, struct hid_field *field,
493 				struct hid_usage *usage, __s32 value)
494 {
495 	struct mt_device *td = hid_get_drvdata(hid);
496 	__s32 quirks = td->mtclass.quirks;
497 
498 	if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
499 		switch (usage->hid) {
500 		case HID_DG_INRANGE:
501 			if (quirks & MT_QUIRK_ALWAYS_VALID)
502 				td->curvalid = true;
503 			else if (quirks & MT_QUIRK_VALID_IS_INRANGE)
504 				td->curvalid = value;
505 			break;
506 		case HID_DG_TIPSWITCH:
507 			if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
508 				td->curvalid = value;
509 			td->curdata.touch_state = value;
510 			break;
511 		case HID_DG_CONFIDENCE:
512 			if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
513 				td->curvalid = value;
514 			break;
515 		case HID_DG_CONTACTID:
516 			td->curdata.contactid = value;
517 			break;
518 		case HID_DG_TIPPRESSURE:
519 			td->curdata.p = value;
520 			break;
521 		case HID_GD_X:
522 			td->curdata.x = value;
523 			break;
524 		case HID_GD_Y:
525 			td->curdata.y = value;
526 			break;
527 		case HID_DG_WIDTH:
528 			td->curdata.w = value;
529 			break;
530 		case HID_DG_HEIGHT:
531 			td->curdata.h = value;
532 			break;
533 		case HID_DG_CONTACTCOUNT:
534 			/*
535 			 * Includes multi-packet support where subsequent
536 			 * packets are sent with zero contactcount.
537 			 */
538 			if (value)
539 				td->num_expected = value;
540 			break;
541 
542 		default:
543 			/* fallback to the generic hidinput handling */
544 			return 0;
545 		}
546 
547 		if (usage->hid == td->last_slot_field) {
548 			mt_complete_slot(td);
549 		}
550 
551 		if (field->index == td->last_field_index
552 			&& td->num_received >= td->num_expected)
553 			mt_emit_event(td, field->hidinput->input);
554 
555 	}
556 
557 	/* we have handled the hidinput part, now remains hiddev */
558 	if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
559 		hid->hiddev_hid_event(hid, field, usage, value);
560 
561 	return 1;
562 }
563 
564 static void mt_set_input_mode(struct hid_device *hdev)
565 {
566 	struct mt_device *td = hid_get_drvdata(hdev);
567 	struct hid_report *r;
568 	struct hid_report_enum *re;
569 
570 	if (td->inputmode < 0)
571 		return;
572 
573 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
574 	r = re->report_id_hash[td->inputmode];
575 	if (r) {
576 		r->field[0]->value[0] = 0x02;
577 		usbhid_submit_report(hdev, r, USB_DIR_OUT);
578 	}
579 }
580 
581 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
582 {
583 	int ret, i;
584 	struct mt_device *td;
585 	struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
586 
587 	for (i = 0; mt_classes[i].name ; i++) {
588 		if (id->driver_data == mt_classes[i].name) {
589 			mtclass = &(mt_classes[i]);
590 			break;
591 		}
592 	}
593 
594 	/* This allows the driver to correctly support devices
595 	 * that emit events over several HID messages.
596 	 */
597 	hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
598 
599 	td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
600 	if (!td) {
601 		dev_err(&hdev->dev, "cannot allocate multitouch data\n");
602 		return -ENOMEM;
603 	}
604 	td->mtclass = *mtclass;
605 	td->inputmode = -1;
606 	td->last_mt_collection = -1;
607 	hid_set_drvdata(hdev, td);
608 
609 	ret = hid_parse(hdev);
610 	if (ret != 0)
611 		goto fail;
612 
613 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
614 	if (ret)
615 		goto fail;
616 
617 	td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
618 				GFP_KERNEL);
619 	if (!td->slots) {
620 		dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
621 		hid_hw_stop(hdev);
622 		ret = -ENOMEM;
623 		goto fail;
624 	}
625 
626 	ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
627 
628 	mt_set_input_mode(hdev);
629 
630 	return 0;
631 
632 fail:
633 	kfree(td);
634 	return ret;
635 }
636 
637 #ifdef CONFIG_PM
638 static int mt_reset_resume(struct hid_device *hdev)
639 {
640 	mt_set_input_mode(hdev);
641 	return 0;
642 }
643 #endif
644 
645 static void mt_remove(struct hid_device *hdev)
646 {
647 	struct mt_device *td = hid_get_drvdata(hdev);
648 	sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
649 	hid_hw_stop(hdev);
650 	kfree(td->slots);
651 	kfree(td);
652 	hid_set_drvdata(hdev, NULL);
653 }
654 
655 static const struct hid_device_id mt_devices[] = {
656 
657 	/* 3M panels */
658 	{ .driver_data = MT_CLS_3M,
659 		HID_USB_DEVICE(USB_VENDOR_ID_3M,
660 			USB_DEVICE_ID_3M1968) },
661 	{ .driver_data = MT_CLS_3M,
662 		HID_USB_DEVICE(USB_VENDOR_ID_3M,
663 			USB_DEVICE_ID_3M2256) },
664 	{ .driver_data = MT_CLS_3M,
665 		HID_USB_DEVICE(USB_VENDOR_ID_3M,
666 			USB_DEVICE_ID_3M3266) },
667 
668 	/* ActionStar panels */
669 	{ .driver_data = MT_CLS_DEFAULT,
670 		HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR,
671 			USB_DEVICE_ID_ACTIONSTAR_1011) },
672 
673 	/* Atmel panels */
674 	{ .driver_data = MT_CLS_SERIAL,
675 		HID_USB_DEVICE(USB_VENDOR_ID_ATMEL,
676 			USB_DEVICE_ID_ATMEL_MULTITOUCH) },
677 
678 	/* Cando panels */
679 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
680 		HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
681 			USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
682 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
683 		HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
684 			USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
685 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
686 		HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
687 			USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
688 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
689 		HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
690 			USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
691 
692 	/* Chunghwa Telecom touch panels */
693 	{  .driver_data = MT_CLS_DEFAULT,
694 		HID_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
695 			USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
696 
697 	/* CVTouch panels */
698 	{ .driver_data = MT_CLS_DEFAULT,
699 		HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
700 			USB_DEVICE_ID_CVTOUCH_SCREEN) },
701 
702 	/* Cypress panel */
703 	{ .driver_data = MT_CLS_CYPRESS,
704 		HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
705 			USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
706 
707 	/* eGalax devices (resistive) */
708 	{ .driver_data = MT_CLS_EGALAX,
709 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
710 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
711 	{ .driver_data = MT_CLS_EGALAX,
712 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
713 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
714 
715 	/* eGalax devices (capacitive) */
716 	{ .driver_data = MT_CLS_EGALAX,
717 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
718 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
719 	{ .driver_data = MT_CLS_EGALAX,
720 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
721 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
722 	{ .driver_data = MT_CLS_EGALAX,
723 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
724 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
725 	{ .driver_data = MT_CLS_EGALAX,
726 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
727 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
728 	{ .driver_data = MT_CLS_EGALAX,
729 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
730 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
731 	{ .driver_data = MT_CLS_EGALAX_SERIAL,
732 		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
733 			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
734 
735 	/* Elo TouchSystems IntelliTouch Plus panel */
736 	{ .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
737 		HID_USB_DEVICE(USB_VENDOR_ID_ELO,
738 			USB_DEVICE_ID_ELO_TS2515) },
739 
740 	/* GeneralTouch panel */
741 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
742 		HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
743 			USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
744 
745 	/* GoodTouch panels */
746 	{ .driver_data = MT_CLS_DEFAULT,
747 		HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
748 			USB_DEVICE_ID_GOODTOUCH_000f) },
749 
750 	/* Hanvon panels */
751 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
752 		HID_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
753 			USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
754 
755 	/* Ideacom panel */
756 	{ .driver_data = MT_CLS_SERIAL,
757 		HID_USB_DEVICE(USB_VENDOR_ID_IDEACOM,
758 			USB_DEVICE_ID_IDEACOM_IDC6650) },
759 
760 	/* Ilitek dual touch panel */
761 	{  .driver_data = MT_CLS_DEFAULT,
762 		HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
763 			USB_DEVICE_ID_ILITEK_MULTITOUCH) },
764 
765 	/* IRTOUCH panels */
766 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
767 		HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
768 			USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
769 
770 	/* LG Display panels */
771 	{ .driver_data = MT_CLS_DEFAULT,
772 		HID_USB_DEVICE(USB_VENDOR_ID_LG,
773 			USB_DEVICE_ID_LG_MULTITOUCH) },
774 
775 	/* Lumio panels */
776 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
777 		HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
778 			USB_DEVICE_ID_CRYSTALTOUCH) },
779 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
780 		HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
781 			USB_DEVICE_ID_CRYSTALTOUCH_DUAL) },
782 
783 	/* MosArt panels */
784 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
785 		HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
786 			USB_DEVICE_ID_ASUS_T91MT)},
787 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
788 		HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
789 			USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
790 	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
791 		HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
792 			USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
793 
794 	/* PenMount panels */
795 	{ .driver_data = MT_CLS_CONFIDENCE,
796 		HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
797 			USB_DEVICE_ID_PENMOUNT_PCI) },
798 
799 	/* PixArt optical touch screen */
800 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
801 		HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
802 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
803 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
804 		HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
805 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
806 	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
807 		HID_USB_DEVICE(USB_VENDOR_ID_PIXART,
808 			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
809 
810 	/* PixCir-based panels */
811 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
812 		HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
813 			USB_DEVICE_ID_HANVON_MULTITOUCH) },
814 	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
815 		HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
816 			USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
817 
818 	/* Quanta-based panels */
819 	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
820 		HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
821 			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
822 	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
823 		HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
824 			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
825 	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
826 		HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
827 			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3008) },
828 
829 	/* Stantum panels */
830 	{ .driver_data = MT_CLS_CONFIDENCE,
831 		HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
832 			USB_DEVICE_ID_MTP)},
833 	{ .driver_data = MT_CLS_CONFIDENCE,
834 		HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
835 			USB_DEVICE_ID_MTP_STM)},
836 	{ .driver_data = MT_CLS_CONFIDENCE,
837 		HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX,
838 			USB_DEVICE_ID_MTP_SITRONIX)},
839 
840 	/* Touch International panels */
841 	{ .driver_data = MT_CLS_DEFAULT,
842 		HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
843 			USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
844 
845 	/* Unitec panels */
846 	{ .driver_data = MT_CLS_DEFAULT,
847 		HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
848 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
849 	{ .driver_data = MT_CLS_DEFAULT,
850 		HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
851 			USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
852 	/* XAT */
853 	{ .driver_data = MT_CLS_DEFAULT,
854 		HID_USB_DEVICE(USB_VENDOR_ID_XAT,
855 			USB_DEVICE_ID_XAT_CSR) },
856 
857 	/* Xiroku */
858 	{ .driver_data = MT_CLS_DEFAULT,
859 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
860 			USB_DEVICE_ID_XIROKU_SPX) },
861 	{ .driver_data = MT_CLS_DEFAULT,
862 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
863 			USB_DEVICE_ID_XIROKU_MPX) },
864 	{ .driver_data = MT_CLS_DEFAULT,
865 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
866 			USB_DEVICE_ID_XIROKU_CSR) },
867 	{ .driver_data = MT_CLS_DEFAULT,
868 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
869 			USB_DEVICE_ID_XIROKU_SPX1) },
870 	{ .driver_data = MT_CLS_DEFAULT,
871 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
872 			USB_DEVICE_ID_XIROKU_MPX1) },
873 	{ .driver_data = MT_CLS_DEFAULT,
874 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
875 			USB_DEVICE_ID_XIROKU_CSR1) },
876 	{ .driver_data = MT_CLS_DEFAULT,
877 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
878 			USB_DEVICE_ID_XIROKU_SPX2) },
879 	{ .driver_data = MT_CLS_DEFAULT,
880 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
881 			USB_DEVICE_ID_XIROKU_MPX2) },
882 	{ .driver_data = MT_CLS_DEFAULT,
883 		HID_USB_DEVICE(USB_VENDOR_ID_XIROKU,
884 			USB_DEVICE_ID_XIROKU_CSR2) },
885 
886 	{ }
887 };
888 MODULE_DEVICE_TABLE(hid, mt_devices);
889 
890 static const struct hid_usage_id mt_grabbed_usages[] = {
891 	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
892 	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
893 };
894 
895 static struct hid_driver mt_driver = {
896 	.name = "hid-multitouch",
897 	.id_table = mt_devices,
898 	.probe = mt_probe,
899 	.remove = mt_remove,
900 	.input_mapping = mt_input_mapping,
901 	.input_mapped = mt_input_mapped,
902 	.feature_mapping = mt_feature_mapping,
903 	.usage_table = mt_grabbed_usages,
904 	.event = mt_event,
905 #ifdef CONFIG_PM
906 	.reset_resume = mt_reset_resume,
907 #endif
908 };
909 
910 static int __init mt_init(void)
911 {
912 	return hid_register_driver(&mt_driver);
913 }
914 
915 static void __exit mt_exit(void)
916 {
917 	hid_unregister_driver(&mt_driver);
918 }
919 
920 module_init(mt_init);
921 module_exit(mt_exit);
922