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