xref: /openbmc/linux/drivers/hid/hid-ntrig.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  *  HID driver for N-Trig touchscreens
3  *
4  *  Copyright (c) 2008-2010 Rafi Rubin
5  *  Copyright (c) 2009-2010 Stephane Chatty
6  *
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  */
15 
16 #include <linux/device.h>
17 #include <linux/hid.h>
18 #include <linux/usb.h>
19 #include "usbhid/usbhid.h"
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 
23 #include "hid-ids.h"
24 
25 #define NTRIG_DUPLICATE_USAGES	0x001
26 
27 static unsigned int min_width;
28 module_param(min_width, uint, 0644);
29 MODULE_PARM_DESC(min_width, "Minimum touch contact width to accept.");
30 
31 static unsigned int min_height;
32 module_param(min_height, uint, 0644);
33 MODULE_PARM_DESC(min_height, "Minimum touch contact height to accept.");
34 
35 static unsigned int activate_slack = 1;
36 module_param(activate_slack, uint, 0644);
37 MODULE_PARM_DESC(activate_slack, "Number of touch frames to ignore at "
38 		 "the start of touch input.");
39 
40 static unsigned int deactivate_slack = 4;
41 module_param(deactivate_slack, uint, 0644);
42 MODULE_PARM_DESC(deactivate_slack, "Number of empty frames to ignore before "
43 		 "deactivating touch.");
44 
45 static unsigned int activation_width = 64;
46 module_param(activation_width, uint, 0644);
47 MODULE_PARM_DESC(activation_width, "Width threshold to immediately start "
48 		 "processing touch events.");
49 
50 static unsigned int activation_height = 32;
51 module_param(activation_height, uint, 0644);
52 MODULE_PARM_DESC(activation_height, "Height threshold to immediately start "
53 		 "processing touch events.");
54 
55 struct ntrig_data {
56 	/* Incoming raw values for a single contact */
57 	__u16 x, y, w, h;
58 	__u16 id;
59 
60 	bool tipswitch;
61 	bool confidence;
62 	bool first_contact_touch;
63 
64 	bool reading_mt;
65 
66 	__u8 mt_footer[4];
67 	__u8 mt_foot_count;
68 
69 	/* The current activation state. */
70 	__s8 act_state;
71 
72 	/* Empty frames to ignore before recognizing the end of activity */
73 	__s8 deactivate_slack;
74 
75 	/* Frames to ignore before acknowledging the start of activity */
76 	__s8 activate_slack;
77 
78 	/* Minimum size contact to accept */
79 	__u16 min_width;
80 	__u16 min_height;
81 
82 	/* Threshold to override activation slack */
83 	__u16 activation_width;
84 	__u16 activation_height;
85 
86 	__u16 sensor_logical_width;
87 	__u16 sensor_logical_height;
88 	__u16 sensor_physical_width;
89 	__u16 sensor_physical_height;
90 };
91 
92 
93 /*
94  * This function converts the 4 byte raw firmware code into
95  * a string containing 5 comma separated numbers.
96  */
97 static int ntrig_version_string(unsigned char *raw, char *buf)
98 {
99 	__u8 a =  (raw[1] & 0x0e) >> 1;
100 	__u8 b =  (raw[0] & 0x3c) >> 2;
101 	__u8 c = ((raw[0] & 0x03) << 3) | ((raw[3] & 0xe0) >> 5);
102 	__u8 d = ((raw[3] & 0x07) << 3) | ((raw[2] & 0xe0) >> 5);
103 	__u8 e =   raw[2] & 0x07;
104 
105 	/*
106 	 * As yet unmapped bits:
107 	 * 0b11000000 0b11110001 0b00011000 0b00011000
108 	 */
109 
110 	return sprintf(buf, "%u.%u.%u.%u.%u", a, b, c, d, e);
111 }
112 
113 static void ntrig_report_version(struct hid_device *hdev)
114 {
115 	int ret;
116 	char buf[20];
117 	struct usb_device *usb_dev = hid_to_usb_dev(hdev);
118 	unsigned char *data = kmalloc(8, GFP_KERNEL);
119 
120 	if (!data)
121 		goto err_free;
122 
123 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
124 			      USB_REQ_CLEAR_FEATURE,
125 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE |
126 			      USB_DIR_IN,
127 			      0x30c, 1, data, 8,
128 			      USB_CTRL_SET_TIMEOUT);
129 
130 	if (ret == 8) {
131 		ret = ntrig_version_string(&data[2], buf);
132 
133 		dev_info(&hdev->dev,
134 			 "Firmware version: %s (%02x%02x %02x%02x)\n",
135 			 buf, data[2], data[3], data[4], data[5]);
136 	}
137 
138 err_free:
139 	kfree(data);
140 }
141 
142 static ssize_t show_phys_width(struct device *dev,
143 			       struct device_attribute *attr,
144 			       char *buf)
145 {
146 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
147 	struct ntrig_data *nd = hid_get_drvdata(hdev);
148 
149 	return sprintf(buf, "%d\n", nd->sensor_physical_width);
150 }
151 
152 static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL);
153 
154 static ssize_t show_phys_height(struct device *dev,
155 				struct device_attribute *attr,
156 				char *buf)
157 {
158 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
159 	struct ntrig_data *nd = hid_get_drvdata(hdev);
160 
161 	return sprintf(buf, "%d\n", nd->sensor_physical_height);
162 }
163 
164 static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL);
165 
166 static ssize_t show_log_width(struct device *dev,
167 			      struct device_attribute *attr,
168 			      char *buf)
169 {
170 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
171 	struct ntrig_data *nd = hid_get_drvdata(hdev);
172 
173 	return sprintf(buf, "%d\n", nd->sensor_logical_width);
174 }
175 
176 static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL);
177 
178 static ssize_t show_log_height(struct device *dev,
179 			       struct device_attribute *attr,
180 			       char *buf)
181 {
182 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
183 	struct ntrig_data *nd = hid_get_drvdata(hdev);
184 
185 	return sprintf(buf, "%d\n", nd->sensor_logical_height);
186 }
187 
188 static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL);
189 
190 static ssize_t show_min_width(struct device *dev,
191 			      struct device_attribute *attr,
192 			      char *buf)
193 {
194 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
195 	struct ntrig_data *nd = hid_get_drvdata(hdev);
196 
197 	return sprintf(buf, "%d\n", nd->min_width *
198 				    nd->sensor_physical_width /
199 				    nd->sensor_logical_width);
200 }
201 
202 static ssize_t set_min_width(struct device *dev,
203 			     struct device_attribute *attr,
204 			     const char *buf, size_t count)
205 {
206 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
207 	struct ntrig_data *nd = hid_get_drvdata(hdev);
208 
209 	unsigned long val;
210 
211 	if (strict_strtoul(buf, 0, &val))
212 		return -EINVAL;
213 
214 	if (val > nd->sensor_physical_width)
215 		return -EINVAL;
216 
217 	nd->min_width = val * nd->sensor_logical_width /
218 			      nd->sensor_physical_width;
219 
220 	return count;
221 }
222 
223 static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width);
224 
225 static ssize_t show_min_height(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 ntrig_data *nd = hid_get_drvdata(hdev);
231 
232 	return sprintf(buf, "%d\n", nd->min_height *
233 				    nd->sensor_physical_height /
234 				    nd->sensor_logical_height);
235 }
236 
237 static ssize_t set_min_height(struct device *dev,
238 			      struct device_attribute *attr,
239 			      const char *buf, size_t count)
240 {
241 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
242 	struct ntrig_data *nd = hid_get_drvdata(hdev);
243 
244 	unsigned long val;
245 
246 	if (strict_strtoul(buf, 0, &val))
247 		return -EINVAL;
248 
249 	if (val > nd->sensor_physical_height)
250 		return -EINVAL;
251 
252 	nd->min_height = val * nd->sensor_logical_height /
253 			       nd->sensor_physical_height;
254 
255 	return count;
256 }
257 
258 static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height,
259 		   set_min_height);
260 
261 static ssize_t show_activate_slack(struct device *dev,
262 				   struct device_attribute *attr,
263 				   char *buf)
264 {
265 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
266 	struct ntrig_data *nd = hid_get_drvdata(hdev);
267 
268 	return sprintf(buf, "%d\n", nd->activate_slack);
269 }
270 
271 static ssize_t set_activate_slack(struct device *dev,
272 				  struct device_attribute *attr,
273 				  const char *buf, size_t count)
274 {
275 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
276 	struct ntrig_data *nd = hid_get_drvdata(hdev);
277 
278 	unsigned long val;
279 
280 	if (strict_strtoul(buf, 0, &val))
281 		return -EINVAL;
282 
283 	if (val > 0x7f)
284 		return -EINVAL;
285 
286 	nd->activate_slack = val;
287 
288 	return count;
289 }
290 
291 static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack,
292 		   set_activate_slack);
293 
294 static ssize_t show_activation_width(struct device *dev,
295 				     struct device_attribute *attr,
296 				     char *buf)
297 {
298 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
299 	struct ntrig_data *nd = hid_get_drvdata(hdev);
300 
301 	return sprintf(buf, "%d\n", nd->activation_width *
302 				    nd->sensor_physical_width /
303 				    nd->sensor_logical_width);
304 }
305 
306 static ssize_t set_activation_width(struct device *dev,
307 				    struct device_attribute *attr,
308 				    const char *buf, size_t count)
309 {
310 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
311 	struct ntrig_data *nd = hid_get_drvdata(hdev);
312 
313 	unsigned long val;
314 
315 	if (strict_strtoul(buf, 0, &val))
316 		return -EINVAL;
317 
318 	if (val > nd->sensor_physical_width)
319 		return -EINVAL;
320 
321 	nd->activation_width = val * nd->sensor_logical_width /
322 				     nd->sensor_physical_width;
323 
324 	return count;
325 }
326 
327 static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width,
328 		   set_activation_width);
329 
330 static ssize_t show_activation_height(struct device *dev,
331 				      struct device_attribute *attr,
332 				      char *buf)
333 {
334 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
335 	struct ntrig_data *nd = hid_get_drvdata(hdev);
336 
337 	return sprintf(buf, "%d\n", nd->activation_height *
338 				    nd->sensor_physical_height /
339 				    nd->sensor_logical_height);
340 }
341 
342 static ssize_t set_activation_height(struct device *dev,
343 				     struct device_attribute *attr,
344 				     const char *buf, size_t count)
345 {
346 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
347 	struct ntrig_data *nd = hid_get_drvdata(hdev);
348 
349 	unsigned long val;
350 
351 	if (strict_strtoul(buf, 0, &val))
352 		return -EINVAL;
353 
354 	if (val > nd->sensor_physical_height)
355 		return -EINVAL;
356 
357 	nd->activation_height = val * nd->sensor_logical_height /
358 				      nd->sensor_physical_height;
359 
360 	return count;
361 }
362 
363 static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO,
364 		   show_activation_height, set_activation_height);
365 
366 static ssize_t show_deactivate_slack(struct device *dev,
367 				     struct device_attribute *attr,
368 				     char *buf)
369 {
370 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
371 	struct ntrig_data *nd = hid_get_drvdata(hdev);
372 
373 	return sprintf(buf, "%d\n", -nd->deactivate_slack);
374 }
375 
376 static ssize_t set_deactivate_slack(struct device *dev,
377 				    struct device_attribute *attr,
378 				    const char *buf, size_t count)
379 {
380 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
381 	struct ntrig_data *nd = hid_get_drvdata(hdev);
382 
383 	unsigned long val;
384 
385 	if (strict_strtoul(buf, 0, &val))
386 		return -EINVAL;
387 
388 	/*
389 	 * No more than 8 terminal frames have been observed so far
390 	 * and higher slack is highly likely to leave the single
391 	 * touch emulation stuck down.
392 	 */
393 	if (val > 7)
394 		return -EINVAL;
395 
396 	nd->deactivate_slack = -val;
397 
398 	return count;
399 }
400 
401 static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack,
402 		   set_deactivate_slack);
403 
404 static struct attribute *sysfs_attrs[] = {
405 	&dev_attr_sensor_physical_width.attr,
406 	&dev_attr_sensor_physical_height.attr,
407 	&dev_attr_sensor_logical_width.attr,
408 	&dev_attr_sensor_logical_height.attr,
409 	&dev_attr_min_height.attr,
410 	&dev_attr_min_width.attr,
411 	&dev_attr_activate_slack.attr,
412 	&dev_attr_activation_width.attr,
413 	&dev_attr_activation_height.attr,
414 	&dev_attr_deactivate_slack.attr,
415 	NULL
416 };
417 
418 static struct attribute_group ntrig_attribute_group = {
419 	.attrs = sysfs_attrs
420 };
421 
422 /*
423  * this driver is aimed at two firmware versions in circulation:
424  *  - dual pen/finger single touch
425  *  - finger multitouch, pen not working
426  */
427 
428 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
429 			       struct hid_field *field, struct hid_usage *usage,
430 			       unsigned long **bit, int *max)
431 {
432 	struct ntrig_data *nd = hid_get_drvdata(hdev);
433 
434 	/* No special mappings needed for the pen and single touch */
435 	if (field->physical)
436 		return 0;
437 
438 	switch (usage->hid & HID_USAGE_PAGE) {
439 	case HID_UP_GENDESK:
440 		switch (usage->hid) {
441 		case HID_GD_X:
442 			hid_map_usage(hi, usage, bit, max,
443 					EV_ABS, ABS_MT_POSITION_X);
444 			input_set_abs_params(hi->input, ABS_X,
445 					field->logical_minimum,
446 					field->logical_maximum, 0, 0);
447 
448 			if (!nd->sensor_logical_width) {
449 				nd->sensor_logical_width =
450 					field->logical_maximum -
451 					field->logical_minimum;
452 				nd->sensor_physical_width =
453 					field->physical_maximum -
454 					field->physical_minimum;
455 				nd->activation_width = activation_width *
456 					nd->sensor_logical_width /
457 					nd->sensor_physical_width;
458 				nd->min_width = min_width *
459 					nd->sensor_logical_width /
460 					nd->sensor_physical_width;
461 			}
462 			return 1;
463 		case HID_GD_Y:
464 			hid_map_usage(hi, usage, bit, max,
465 					EV_ABS, ABS_MT_POSITION_Y);
466 			input_set_abs_params(hi->input, ABS_Y,
467 					field->logical_minimum,
468 					field->logical_maximum, 0, 0);
469 
470 			if (!nd->sensor_logical_height) {
471 				nd->sensor_logical_height =
472 					field->logical_maximum -
473 					field->logical_minimum;
474 				nd->sensor_physical_height =
475 					field->physical_maximum -
476 					field->physical_minimum;
477 				nd->activation_height = activation_height *
478 					nd->sensor_logical_height /
479 					nd->sensor_physical_height;
480 				nd->min_height = min_height *
481 					nd->sensor_logical_height /
482 					nd->sensor_physical_height;
483 			}
484 			return 1;
485 		}
486 		return 0;
487 
488 	case HID_UP_DIGITIZER:
489 		switch (usage->hid) {
490 		/* we do not want to map these for now */
491 		case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
492 		case HID_DG_INPUTMODE:
493 		case HID_DG_DEVICEINDEX:
494 		case HID_DG_CONTACTMAX:
495 			return -1;
496 
497 		/* width/height mapped on TouchMajor/TouchMinor/Orientation */
498 		case HID_DG_WIDTH:
499 			hid_map_usage(hi, usage, bit, max,
500 				      EV_ABS, ABS_MT_TOUCH_MAJOR);
501 			return 1;
502 		case HID_DG_HEIGHT:
503 			hid_map_usage(hi, usage, bit, max,
504 				      EV_ABS, ABS_MT_TOUCH_MINOR);
505 			input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
506 					     0, 1, 0, 0);
507 			return 1;
508 		}
509 		return 0;
510 
511 	case 0xff000000:
512 		/* we do not want to map these: no input-oriented meaning */
513 		return -1;
514 	}
515 
516 	return 0;
517 }
518 
519 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
520 			      struct hid_field *field, struct hid_usage *usage,
521 			      unsigned long **bit, int *max)
522 {
523 	/* No special mappings needed for the pen and single touch */
524 	if (field->physical)
525 		return 0;
526 
527 	if (usage->type == EV_KEY || usage->type == EV_REL
528 			|| usage->type == EV_ABS)
529 		clear_bit(usage->code, *bit);
530 
531 	return 0;
532 }
533 
534 /*
535  * this function is called upon all reports
536  * so that we can filter contact point information,
537  * decide whether we are in multi or single touch mode
538  * and call input_mt_sync after each point if necessary
539  */
540 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
541 			struct hid_usage *usage, __s32 value)
542 {
543 	struct input_dev *input = field->hidinput->input;
544 	struct ntrig_data *nd = hid_get_drvdata(hid);
545 
546 	/* No special handling needed for the pen */
547 	if (field->application == HID_DG_PEN)
548 		return 0;
549 
550         if (hid->claimed & HID_CLAIMED_INPUT) {
551 		switch (usage->hid) {
552 		case 0xff000001:
553 			/* Tag indicating the start of a multitouch group */
554 			nd->reading_mt = 1;
555 			nd->first_contact_touch = 0;
556 			break;
557 		case HID_DG_TIPSWITCH:
558 			nd->tipswitch = value;
559 			/* Prevent emission of touch until validated */
560 			return 1;
561 		case HID_DG_CONFIDENCE:
562 			nd->confidence = value;
563 			break;
564 		case HID_GD_X:
565 			nd->x = value;
566 			/* Clear the contact footer */
567 			nd->mt_foot_count = 0;
568 			break;
569 		case HID_GD_Y:
570 			nd->y = value;
571 			break;
572 		case HID_DG_CONTACTID:
573 			nd->id = value;
574 			break;
575 		case HID_DG_WIDTH:
576 			nd->w = value;
577 			break;
578 		case HID_DG_HEIGHT:
579 			nd->h = value;
580 			/*
581 			 * when in single touch mode, this is the last
582 			 * report received in a finger event. We want
583 			 * to emit a normal (X, Y) position
584 			 */
585 			if (!nd->reading_mt) {
586 				/*
587 				 * TipSwitch indicates the presence of a
588 				 * finger in single touch mode.
589 				 */
590 				input_report_key(input, BTN_TOUCH,
591 						 nd->tipswitch);
592 				input_report_key(input, BTN_TOOL_DOUBLETAP,
593 						 nd->tipswitch);
594 				input_event(input, EV_ABS, ABS_X, nd->x);
595 				input_event(input, EV_ABS, ABS_Y, nd->y);
596 			}
597 			break;
598 		case 0xff000002:
599 			/*
600 			 * we receive this when the device is in multitouch
601 			 * mode. The first of the three values tagged with
602 			 * this usage tells if the contact point is real
603 			 * or a placeholder
604 			 */
605 
606 			/* Shouldn't get more than 4 footer packets, so skip */
607 			if (nd->mt_foot_count >= 4)
608 				break;
609 
610 			nd->mt_footer[nd->mt_foot_count++] = value;
611 
612 			/* if the footer isn't complete break */
613 			if (nd->mt_foot_count != 4)
614 				break;
615 
616 			/* Pen activity signal. */
617 			if (nd->mt_footer[2]) {
618 				/*
619 				 * When the pen deactivates touch, we see a
620 				 * bogus frame with ContactCount > 0.
621 				 * We can
622 				 * save a bit of work by ensuring act_state < 0
623 				 * even if deactivation slack is turned off.
624 				 */
625 				nd->act_state = deactivate_slack - 1;
626 				nd->confidence = 0;
627 				break;
628 			}
629 
630 			/*
631 			 * The first footer value indicates the presence of a
632 			 * finger.
633 			 */
634 			if (nd->mt_footer[0]) {
635 				/*
636 				 * We do not want to process contacts under
637 				 * the size threshold, but do not want to
638 				 * ignore them for activation state
639 				 */
640 				if (nd->w < nd->min_width ||
641 				    nd->h < nd->min_height)
642 					nd->confidence = 0;
643 			} else
644 				break;
645 
646 			if (nd->act_state > 0) {
647 				/*
648 				 * Contact meets the activation size threshold
649 				 */
650 				if (nd->w >= nd->activation_width &&
651 				    nd->h >= nd->activation_height) {
652 					if (nd->id)
653 						/*
654 						 * first contact, activate now
655 						 */
656 						nd->act_state = 0;
657 					else {
658 						/*
659 						 * avoid corrupting this frame
660 						 * but ensure next frame will
661 						 * be active
662 						 */
663 						nd->act_state = 1;
664 						break;
665 					}
666 				} else
667 					/*
668 					 * Defer adjusting the activation state
669 					 * until the end of the frame.
670 					 */
671 					break;
672 			}
673 
674 			/* Discarding this contact */
675 			if (!nd->confidence)
676 				break;
677 
678 			/* emit a normal (X, Y) for the first point only */
679 			if (nd->id == 0) {
680 				/*
681 				 * TipSwitch is superfluous in multitouch
682 				 * mode.  The footer events tell us
683 				 * if there is a finger on the screen or
684 				 * not.
685 				 */
686 				nd->first_contact_touch = nd->confidence;
687 				input_event(input, EV_ABS, ABS_X, nd->x);
688 				input_event(input, EV_ABS, ABS_Y, nd->y);
689 			}
690 
691 			/* Emit MT events */
692 			input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
693 			input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
694 
695 			/*
696 			 * Translate from height and width to size
697 			 * and orientation.
698 			 */
699 			if (nd->w > nd->h) {
700 				input_event(input, EV_ABS,
701 						ABS_MT_ORIENTATION, 1);
702 				input_event(input, EV_ABS,
703 						ABS_MT_TOUCH_MAJOR, nd->w);
704 				input_event(input, EV_ABS,
705 						ABS_MT_TOUCH_MINOR, nd->h);
706 			} else {
707 				input_event(input, EV_ABS,
708 						ABS_MT_ORIENTATION, 0);
709 				input_event(input, EV_ABS,
710 						ABS_MT_TOUCH_MAJOR, nd->h);
711 				input_event(input, EV_ABS,
712 						ABS_MT_TOUCH_MINOR, nd->w);
713 			}
714 			input_mt_sync(field->hidinput->input);
715 			break;
716 
717 		case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
718 			if (!nd->reading_mt) /* Just to be sure */
719 				break;
720 
721 			nd->reading_mt = 0;
722 
723 
724 			/*
725 			 * Activation state machine logic:
726 			 *
727 			 * Fundamental states:
728 			 *	state >  0: Inactive
729 			 *	state <= 0: Active
730 			 *	state <  -deactivate_slack:
731 			 *		 Pen termination of touch
732 			 *
733 			 * Specific values of interest
734 			 *	state == activate_slack
735 			 *		 no valid input since the last reset
736 			 *
737 			 *	state == 0
738 			 *		 general operational state
739 			 *
740 			 *	state == -deactivate_slack
741 			 *		 read sufficient empty frames to accept
742 			 *		 the end of input and reset
743 			 */
744 
745 			if (nd->act_state > 0) { /* Currently inactive */
746 				if (value)
747 					/*
748 					 * Consider each live contact as
749 					 * evidence of intentional activity.
750 					 */
751 					nd->act_state = (nd->act_state > value)
752 							? nd->act_state - value
753 							: 0;
754 				else
755 					/*
756 					 * Empty frame before we hit the
757 					 * activity threshold, reset.
758 					 */
759 					nd->act_state = nd->activate_slack;
760 
761 				/*
762 				 * Entered this block inactive and no
763 				 * coordinates sent this frame, so hold off
764 				 * on button state.
765 				 */
766 				break;
767 			} else { /* Currently active */
768 				if (value && nd->act_state >=
769 					     nd->deactivate_slack)
770 					/*
771 					 * Live point: clear accumulated
772 					 * deactivation count.
773 					 */
774 					nd->act_state = 0;
775 				else if (nd->act_state <= nd->deactivate_slack)
776 					/*
777 					 * We've consumed the deactivation
778 					 * slack, time to deactivate and reset.
779 					 */
780 					nd->act_state =
781 						nd->activate_slack;
782 				else { /* Move towards deactivation */
783 					nd->act_state--;
784 					break;
785 				}
786 			}
787 
788 			if (nd->first_contact_touch && nd->act_state <= 0) {
789 				/*
790 				 * Check to see if we're ready to start
791 				 * emitting touch events.
792 				 *
793 				 * Note: activation slack will decrease over
794 				 * the course of the frame, and it will be
795 				 * inconsistent from the start to the end of
796 				 * the frame.  However if the frame starts
797 				 * with slack, first_contact_touch will still
798 				 * be 0 and we will not get to this point.
799 				 */
800 				input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
801 				input_report_key(input, BTN_TOUCH, 1);
802 			} else {
803 				input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
804 				input_report_key(input, BTN_TOUCH, 0);
805 			}
806 			break;
807 
808 		default:
809 			/* fall-back to the generic hidinput handling */
810 			return 0;
811 		}
812 	}
813 
814 	/* we have handled the hidinput part, now remains hiddev */
815 	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
816 		hid->hiddev_hid_event(hid, field, usage, value);
817 
818 	return 1;
819 }
820 
821 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
822 {
823 	int ret;
824 	struct ntrig_data *nd;
825 	struct hid_input *hidinput;
826 	struct input_dev *input;
827 	struct hid_report *report;
828 
829 	if (id->driver_data)
830 		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
831 
832 	nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
833 	if (!nd) {
834 		dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
835 		return -ENOMEM;
836 	}
837 
838 	nd->reading_mt = 0;
839 	nd->min_width = 0;
840 	nd->min_height = 0;
841 	nd->activate_slack = activate_slack;
842 	nd->act_state = activate_slack;
843 	nd->deactivate_slack = -deactivate_slack;
844 	nd->sensor_logical_width = 0;
845 	nd->sensor_logical_height = 0;
846 	nd->sensor_physical_width = 0;
847 	nd->sensor_physical_height = 0;
848 
849 	hid_set_drvdata(hdev, nd);
850 
851 	ret = hid_parse(hdev);
852 	if (ret) {
853 		dev_err(&hdev->dev, "parse failed\n");
854 		goto err_free;
855 	}
856 
857 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
858 	if (ret) {
859 		dev_err(&hdev->dev, "hw start failed\n");
860 		goto err_free;
861 	}
862 
863 
864 	list_for_each_entry(hidinput, &hdev->inputs, list) {
865 		if (hidinput->report->maxfield < 1)
866 			continue;
867 
868 		input = hidinput->input;
869 		switch (hidinput->report->field[0]->application) {
870 		case HID_DG_PEN:
871 			input->name = "N-Trig Pen";
872 			break;
873 		case HID_DG_TOUCHSCREEN:
874 			/* These keys are redundant for fingers, clear them
875 			 * to prevent incorrect identification */
876 			__clear_bit(BTN_TOOL_PEN, input->keybit);
877 			__clear_bit(BTN_TOOL_FINGER, input->keybit);
878 			__clear_bit(BTN_0, input->keybit);
879 			__set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
880 			/*
881 			 * The physical touchscreen (single touch)
882 			 * input has a value for physical, whereas
883 			 * the multitouch only has logical input
884 			 * fields.
885 			 */
886 			input->name =
887 				(hidinput->report->field[0]
888 				 ->physical) ?
889 				"N-Trig Touchscreen" :
890 				"N-Trig MultiTouch";
891 			break;
892 		}
893 	}
894 
895 	/* This is needed for devices with more recent firmware versions */
896 	report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
897 	if (report)
898 		usbhid_submit_report(hdev, report, USB_DIR_OUT);
899 
900 	ntrig_report_version(hdev);
901 
902 	ret = sysfs_create_group(&hdev->dev.kobj,
903 			&ntrig_attribute_group);
904 
905 	return 0;
906 err_free:
907 	kfree(nd);
908 	return ret;
909 }
910 
911 static void ntrig_remove(struct hid_device *hdev)
912 {
913 	sysfs_remove_group(&hdev->dev.kobj,
914 			   &ntrig_attribute_group);
915 	hid_hw_stop(hdev);
916 	kfree(hid_get_drvdata(hdev));
917 }
918 
919 static const struct hid_device_id ntrig_devices[] = {
920 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
921 		.driver_data = NTRIG_DUPLICATE_USAGES },
922 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1),
923 		.driver_data = NTRIG_DUPLICATE_USAGES },
924 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2),
925 		.driver_data = NTRIG_DUPLICATE_USAGES },
926 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3),
927 		.driver_data = NTRIG_DUPLICATE_USAGES },
928 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4),
929 		.driver_data = NTRIG_DUPLICATE_USAGES },
930 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5),
931 		.driver_data = NTRIG_DUPLICATE_USAGES },
932 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6),
933 		.driver_data = NTRIG_DUPLICATE_USAGES },
934 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7),
935 		.driver_data = NTRIG_DUPLICATE_USAGES },
936 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8),
937 		.driver_data = NTRIG_DUPLICATE_USAGES },
938 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9),
939 		.driver_data = NTRIG_DUPLICATE_USAGES },
940 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10),
941 		.driver_data = NTRIG_DUPLICATE_USAGES },
942 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11),
943 		.driver_data = NTRIG_DUPLICATE_USAGES },
944 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12),
945 		.driver_data = NTRIG_DUPLICATE_USAGES },
946 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13),
947 		.driver_data = NTRIG_DUPLICATE_USAGES },
948 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14),
949 		.driver_data = NTRIG_DUPLICATE_USAGES },
950 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15),
951 		.driver_data = NTRIG_DUPLICATE_USAGES },
952 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16),
953 		.driver_data = NTRIG_DUPLICATE_USAGES },
954 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17),
955 		.driver_data = NTRIG_DUPLICATE_USAGES },
956 	{ HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18),
957 		.driver_data = NTRIG_DUPLICATE_USAGES },
958 	{ }
959 };
960 MODULE_DEVICE_TABLE(hid, ntrig_devices);
961 
962 static const struct hid_usage_id ntrig_grabbed_usages[] = {
963 	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
964 	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
965 };
966 
967 static struct hid_driver ntrig_driver = {
968 	.name = "ntrig",
969 	.id_table = ntrig_devices,
970 	.probe = ntrig_probe,
971 	.remove = ntrig_remove,
972 	.input_mapping = ntrig_input_mapping,
973 	.input_mapped = ntrig_input_mapped,
974 	.usage_table = ntrig_grabbed_usages,
975 	.event = ntrig_event,
976 };
977 
978 static int __init ntrig_init(void)
979 {
980 	return hid_register_driver(&ntrig_driver);
981 }
982 
983 static void __exit ntrig_exit(void)
984 {
985 	hid_unregister_driver(&ntrig_driver);
986 }
987 
988 module_init(ntrig_init);
989 module_exit(ntrig_exit);
990 MODULE_LICENSE("GPL");
991