Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hid / hid-ntrig.c
blobb5d26f03fe6bb79326899850df6a43a975ef8eb8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * HID driver for N-Trig touchscreens
5 * Copyright (c) 2008-2010 Rafi Rubin
6 * Copyright (c) 2009-2010 Stephane Chatty
7 */
9 /*
12 #include <linux/device.h>
13 #include <linux/hid.h>
14 #include <linux/usb.h>
15 #include "usbhid/usbhid.h"
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #include "hid-ids.h"
21 #define NTRIG_DUPLICATE_USAGES 0x001
23 static unsigned int min_width;
24 module_param(min_width, uint, 0644);
25 MODULE_PARM_DESC(min_width, "Minimum touch contact width to accept.");
27 static unsigned int min_height;
28 module_param(min_height, uint, 0644);
29 MODULE_PARM_DESC(min_height, "Minimum touch contact height to accept.");
31 static unsigned int activate_slack = 1;
32 module_param(activate_slack, uint, 0644);
33 MODULE_PARM_DESC(activate_slack, "Number of touch frames to ignore at "
34 "the start of touch input.");
36 static unsigned int deactivate_slack = 4;
37 module_param(deactivate_slack, uint, 0644);
38 MODULE_PARM_DESC(deactivate_slack, "Number of empty frames to ignore before "
39 "deactivating touch.");
41 static unsigned int activation_width = 64;
42 module_param(activation_width, uint, 0644);
43 MODULE_PARM_DESC(activation_width, "Width threshold to immediately start "
44 "processing touch events.");
46 static unsigned int activation_height = 32;
47 module_param(activation_height, uint, 0644);
48 MODULE_PARM_DESC(activation_height, "Height threshold to immediately start "
49 "processing touch events.");
51 struct ntrig_data {
52 /* Incoming raw values for a single contact */
53 __u16 x, y, w, h;
54 __u16 id;
56 bool tipswitch;
57 bool confidence;
58 bool first_contact_touch;
60 bool reading_mt;
62 __u8 mt_footer[4];
63 __u8 mt_foot_count;
65 /* The current activation state. */
66 __s8 act_state;
68 /* Empty frames to ignore before recognizing the end of activity */
69 __s8 deactivate_slack;
71 /* Frames to ignore before acknowledging the start of activity */
72 __s8 activate_slack;
74 /* Minimum size contact to accept */
75 __u16 min_width;
76 __u16 min_height;
78 /* Threshold to override activation slack */
79 __u16 activation_width;
80 __u16 activation_height;
82 __u16 sensor_logical_width;
83 __u16 sensor_logical_height;
84 __u16 sensor_physical_width;
85 __u16 sensor_physical_height;
90 * This function converts the 4 byte raw firmware code into
91 * a string containing 5 comma separated numbers.
93 static int ntrig_version_string(unsigned char *raw, char *buf)
95 __u8 a = (raw[1] & 0x0e) >> 1;
96 __u8 b = (raw[0] & 0x3c) >> 2;
97 __u8 c = ((raw[0] & 0x03) << 3) | ((raw[3] & 0xe0) >> 5);
98 __u8 d = ((raw[3] & 0x07) << 3) | ((raw[2] & 0xe0) >> 5);
99 __u8 e = raw[2] & 0x07;
102 * As yet unmapped bits:
103 * 0b11000000 0b11110001 0b00011000 0b00011000
106 return sprintf(buf, "%u.%u.%u.%u.%u", a, b, c, d, e);
109 static inline int ntrig_get_mode(struct hid_device *hdev)
111 struct hid_report *report = hdev->report_enum[HID_FEATURE_REPORT].
112 report_id_hash[0x0d];
114 if (!report || report->maxfield < 1 ||
115 report->field[0]->report_count < 1)
116 return -EINVAL;
118 hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
119 hid_hw_wait(hdev);
120 return (int)report->field[0]->value[0];
123 static inline void ntrig_set_mode(struct hid_device *hdev, const int mode)
125 struct hid_report *report;
126 __u8 mode_commands[4] = { 0xe, 0xf, 0x1b, 0x10 };
128 if (mode < 0 || mode > 3)
129 return;
131 report = hdev->report_enum[HID_FEATURE_REPORT].
132 report_id_hash[mode_commands[mode]];
134 if (!report)
135 return;
137 hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
140 static void ntrig_report_version(struct hid_device *hdev)
142 int ret;
143 char buf[20];
144 struct usb_device *usb_dev = hid_to_usb_dev(hdev);
145 unsigned char *data = kmalloc(8, GFP_KERNEL);
147 if (!data)
148 goto err_free;
150 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
151 USB_REQ_CLEAR_FEATURE,
152 USB_TYPE_CLASS | USB_RECIP_INTERFACE |
153 USB_DIR_IN,
154 0x30c, 1, data, 8,
155 USB_CTRL_SET_TIMEOUT);
157 if (ret == 8) {
158 ret = ntrig_version_string(&data[2], buf);
160 hid_info(hdev, "Firmware version: %s (%02x%02x %02x%02x)\n",
161 buf, data[2], data[3], data[4], data[5]);
164 err_free:
165 kfree(data);
168 static ssize_t show_phys_width(struct device *dev,
169 struct device_attribute *attr,
170 char *buf)
172 struct hid_device *hdev = to_hid_device(dev);
173 struct ntrig_data *nd = hid_get_drvdata(hdev);
175 return sprintf(buf, "%d\n", nd->sensor_physical_width);
178 static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL);
180 static ssize_t show_phys_height(struct device *dev,
181 struct device_attribute *attr,
182 char *buf)
184 struct hid_device *hdev = to_hid_device(dev);
185 struct ntrig_data *nd = hid_get_drvdata(hdev);
187 return sprintf(buf, "%d\n", nd->sensor_physical_height);
190 static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL);
192 static ssize_t show_log_width(struct device *dev,
193 struct device_attribute *attr,
194 char *buf)
196 struct hid_device *hdev = to_hid_device(dev);
197 struct ntrig_data *nd = hid_get_drvdata(hdev);
199 return sprintf(buf, "%d\n", nd->sensor_logical_width);
202 static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL);
204 static ssize_t show_log_height(struct device *dev,
205 struct device_attribute *attr,
206 char *buf)
208 struct hid_device *hdev = to_hid_device(dev);
209 struct ntrig_data *nd = hid_get_drvdata(hdev);
211 return sprintf(buf, "%d\n", nd->sensor_logical_height);
214 static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL);
216 static ssize_t show_min_width(struct device *dev,
217 struct device_attribute *attr,
218 char *buf)
220 struct hid_device *hdev = to_hid_device(dev);
221 struct ntrig_data *nd = hid_get_drvdata(hdev);
223 return sprintf(buf, "%d\n", nd->min_width *
224 nd->sensor_physical_width /
225 nd->sensor_logical_width);
228 static ssize_t set_min_width(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t count)
232 struct hid_device *hdev = to_hid_device(dev);
233 struct ntrig_data *nd = hid_get_drvdata(hdev);
235 unsigned long val;
237 if (kstrtoul(buf, 0, &val))
238 return -EINVAL;
240 if (val > nd->sensor_physical_width)
241 return -EINVAL;
243 nd->min_width = val * nd->sensor_logical_width /
244 nd->sensor_physical_width;
246 return count;
249 static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width);
251 static ssize_t show_min_height(struct device *dev,
252 struct device_attribute *attr,
253 char *buf)
255 struct hid_device *hdev = to_hid_device(dev);
256 struct ntrig_data *nd = hid_get_drvdata(hdev);
258 return sprintf(buf, "%d\n", nd->min_height *
259 nd->sensor_physical_height /
260 nd->sensor_logical_height);
263 static ssize_t set_min_height(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf, size_t count)
267 struct hid_device *hdev = to_hid_device(dev);
268 struct ntrig_data *nd = hid_get_drvdata(hdev);
270 unsigned long val;
272 if (kstrtoul(buf, 0, &val))
273 return -EINVAL;
275 if (val > nd->sensor_physical_height)
276 return -EINVAL;
278 nd->min_height = val * nd->sensor_logical_height /
279 nd->sensor_physical_height;
281 return count;
284 static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height,
285 set_min_height);
287 static ssize_t show_activate_slack(struct device *dev,
288 struct device_attribute *attr,
289 char *buf)
291 struct hid_device *hdev = to_hid_device(dev);
292 struct ntrig_data *nd = hid_get_drvdata(hdev);
294 return sprintf(buf, "%d\n", nd->activate_slack);
297 static ssize_t set_activate_slack(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf, size_t count)
301 struct hid_device *hdev = to_hid_device(dev);
302 struct ntrig_data *nd = hid_get_drvdata(hdev);
304 unsigned long val;
306 if (kstrtoul(buf, 0, &val))
307 return -EINVAL;
309 if (val > 0x7f)
310 return -EINVAL;
312 nd->activate_slack = val;
314 return count;
317 static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack,
318 set_activate_slack);
320 static ssize_t show_activation_width(struct device *dev,
321 struct device_attribute *attr,
322 char *buf)
324 struct hid_device *hdev = to_hid_device(dev);
325 struct ntrig_data *nd = hid_get_drvdata(hdev);
327 return sprintf(buf, "%d\n", nd->activation_width *
328 nd->sensor_physical_width /
329 nd->sensor_logical_width);
332 static ssize_t set_activation_width(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf, size_t count)
336 struct hid_device *hdev = to_hid_device(dev);
337 struct ntrig_data *nd = hid_get_drvdata(hdev);
339 unsigned long val;
341 if (kstrtoul(buf, 0, &val))
342 return -EINVAL;
344 if (val > nd->sensor_physical_width)
345 return -EINVAL;
347 nd->activation_width = val * nd->sensor_logical_width /
348 nd->sensor_physical_width;
350 return count;
353 static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width,
354 set_activation_width);
356 static ssize_t show_activation_height(struct device *dev,
357 struct device_attribute *attr,
358 char *buf)
360 struct hid_device *hdev = to_hid_device(dev);
361 struct ntrig_data *nd = hid_get_drvdata(hdev);
363 return sprintf(buf, "%d\n", nd->activation_height *
364 nd->sensor_physical_height /
365 nd->sensor_logical_height);
368 static ssize_t set_activation_height(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf, size_t count)
372 struct hid_device *hdev = to_hid_device(dev);
373 struct ntrig_data *nd = hid_get_drvdata(hdev);
375 unsigned long val;
377 if (kstrtoul(buf, 0, &val))
378 return -EINVAL;
380 if (val > nd->sensor_physical_height)
381 return -EINVAL;
383 nd->activation_height = val * nd->sensor_logical_height /
384 nd->sensor_physical_height;
386 return count;
389 static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO,
390 show_activation_height, set_activation_height);
392 static ssize_t show_deactivate_slack(struct device *dev,
393 struct device_attribute *attr,
394 char *buf)
396 struct hid_device *hdev = to_hid_device(dev);
397 struct ntrig_data *nd = hid_get_drvdata(hdev);
399 return sprintf(buf, "%d\n", -nd->deactivate_slack);
402 static ssize_t set_deactivate_slack(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
406 struct hid_device *hdev = to_hid_device(dev);
407 struct ntrig_data *nd = hid_get_drvdata(hdev);
409 unsigned long val;
411 if (kstrtoul(buf, 0, &val))
412 return -EINVAL;
415 * No more than 8 terminal frames have been observed so far
416 * and higher slack is highly likely to leave the single
417 * touch emulation stuck down.
419 if (val > 7)
420 return -EINVAL;
422 nd->deactivate_slack = -val;
424 return count;
427 static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack,
428 set_deactivate_slack);
430 static struct attribute *sysfs_attrs[] = {
431 &dev_attr_sensor_physical_width.attr,
432 &dev_attr_sensor_physical_height.attr,
433 &dev_attr_sensor_logical_width.attr,
434 &dev_attr_sensor_logical_height.attr,
435 &dev_attr_min_height.attr,
436 &dev_attr_min_width.attr,
437 &dev_attr_activate_slack.attr,
438 &dev_attr_activation_width.attr,
439 &dev_attr_activation_height.attr,
440 &dev_attr_deactivate_slack.attr,
441 NULL
444 static const struct attribute_group ntrig_attribute_group = {
445 .attrs = sysfs_attrs
449 * this driver is aimed at two firmware versions in circulation:
450 * - dual pen/finger single touch
451 * - finger multitouch, pen not working
454 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
455 struct hid_field *field, struct hid_usage *usage,
456 unsigned long **bit, int *max)
458 struct ntrig_data *nd = hid_get_drvdata(hdev);
460 /* No special mappings needed for the pen and single touch */
461 if (field->physical)
462 return 0;
464 switch (usage->hid & HID_USAGE_PAGE) {
465 case HID_UP_GENDESK:
466 switch (usage->hid) {
467 case HID_GD_X:
468 hid_map_usage(hi, usage, bit, max,
469 EV_ABS, ABS_MT_POSITION_X);
470 input_set_abs_params(hi->input, ABS_X,
471 field->logical_minimum,
472 field->logical_maximum, 0, 0);
474 if (!nd->sensor_logical_width) {
475 nd->sensor_logical_width =
476 field->logical_maximum -
477 field->logical_minimum;
478 nd->sensor_physical_width =
479 field->physical_maximum -
480 field->physical_minimum;
481 nd->activation_width = activation_width *
482 nd->sensor_logical_width /
483 nd->sensor_physical_width;
484 nd->min_width = min_width *
485 nd->sensor_logical_width /
486 nd->sensor_physical_width;
488 return 1;
489 case HID_GD_Y:
490 hid_map_usage(hi, usage, bit, max,
491 EV_ABS, ABS_MT_POSITION_Y);
492 input_set_abs_params(hi->input, ABS_Y,
493 field->logical_minimum,
494 field->logical_maximum, 0, 0);
496 if (!nd->sensor_logical_height) {
497 nd->sensor_logical_height =
498 field->logical_maximum -
499 field->logical_minimum;
500 nd->sensor_physical_height =
501 field->physical_maximum -
502 field->physical_minimum;
503 nd->activation_height = activation_height *
504 nd->sensor_logical_height /
505 nd->sensor_physical_height;
506 nd->min_height = min_height *
507 nd->sensor_logical_height /
508 nd->sensor_physical_height;
510 return 1;
512 return 0;
514 case HID_UP_DIGITIZER:
515 switch (usage->hid) {
516 /* we do not want to map these for now */
517 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
518 case HID_DG_INPUTMODE:
519 case HID_DG_DEVICEINDEX:
520 case HID_DG_CONTACTMAX:
521 return -1;
523 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
524 case HID_DG_WIDTH:
525 hid_map_usage(hi, usage, bit, max,
526 EV_ABS, ABS_MT_TOUCH_MAJOR);
527 return 1;
528 case HID_DG_HEIGHT:
529 hid_map_usage(hi, usage, bit, max,
530 EV_ABS, ABS_MT_TOUCH_MINOR);
531 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
532 0, 1, 0, 0);
533 return 1;
535 return 0;
537 case 0xff000000:
538 /* we do not want to map these: no input-oriented meaning */
539 return -1;
542 return 0;
545 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
546 struct hid_field *field, struct hid_usage *usage,
547 unsigned long **bit, int *max)
549 /* No special mappings needed for the pen and single touch */
550 if (field->physical)
551 return 0;
553 if (usage->type == EV_KEY || usage->type == EV_REL
554 || usage->type == EV_ABS)
555 clear_bit(usage->code, *bit);
557 return 0;
561 * this function is called upon all reports
562 * so that we can filter contact point information,
563 * decide whether we are in multi or single touch mode
564 * and call input_mt_sync after each point if necessary
566 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
567 struct hid_usage *usage, __s32 value)
569 struct ntrig_data *nd = hid_get_drvdata(hid);
570 struct input_dev *input;
572 /* Skip processing if not a claimed input */
573 if (!(hid->claimed & HID_CLAIMED_INPUT))
574 goto not_claimed_input;
576 /* This function is being called before the structures are fully
577 * initialized */
578 if(!(field->hidinput && field->hidinput->input))
579 return -EINVAL;
581 input = field->hidinput->input;
583 /* No special handling needed for the pen */
584 if (field->application == HID_DG_PEN)
585 return 0;
587 switch (usage->hid) {
588 case 0xff000001:
589 /* Tag indicating the start of a multitouch group */
590 nd->reading_mt = true;
591 nd->first_contact_touch = false;
592 break;
593 case HID_DG_TIPSWITCH:
594 nd->tipswitch = value;
595 /* Prevent emission of touch until validated */
596 return 1;
597 case HID_DG_CONFIDENCE:
598 nd->confidence = value;
599 break;
600 case HID_GD_X:
601 nd->x = value;
602 /* Clear the contact footer */
603 nd->mt_foot_count = 0;
604 break;
605 case HID_GD_Y:
606 nd->y = value;
607 break;
608 case HID_DG_CONTACTID:
609 nd->id = value;
610 break;
611 case HID_DG_WIDTH:
612 nd->w = value;
613 break;
614 case HID_DG_HEIGHT:
615 nd->h = value;
617 * when in single touch mode, this is the last
618 * report received in a finger event. We want
619 * to emit a normal (X, Y) position
621 if (!nd->reading_mt) {
623 * TipSwitch indicates the presence of a
624 * finger in single touch mode.
626 input_report_key(input, BTN_TOUCH,
627 nd->tipswitch);
628 input_report_key(input, BTN_TOOL_DOUBLETAP,
629 nd->tipswitch);
630 input_event(input, EV_ABS, ABS_X, nd->x);
631 input_event(input, EV_ABS, ABS_Y, nd->y);
633 break;
634 case 0xff000002:
636 * we receive this when the device is in multitouch
637 * mode. The first of the three values tagged with
638 * this usage tells if the contact point is real
639 * or a placeholder
642 /* Shouldn't get more than 4 footer packets, so skip */
643 if (nd->mt_foot_count >= 4)
644 break;
646 nd->mt_footer[nd->mt_foot_count++] = value;
648 /* if the footer isn't complete break */
649 if (nd->mt_foot_count != 4)
650 break;
652 /* Pen activity signal. */
653 if (nd->mt_footer[2]) {
655 * When the pen deactivates touch, we see a
656 * bogus frame with ContactCount > 0.
657 * We can
658 * save a bit of work by ensuring act_state < 0
659 * even if deactivation slack is turned off.
661 nd->act_state = deactivate_slack - 1;
662 nd->confidence = false;
663 break;
667 * The first footer value indicates the presence of a
668 * finger.
670 if (nd->mt_footer[0]) {
672 * We do not want to process contacts under
673 * the size threshold, but do not want to
674 * ignore them for activation state
676 if (nd->w < nd->min_width ||
677 nd->h < nd->min_height)
678 nd->confidence = false;
679 } else
680 break;
682 if (nd->act_state > 0) {
684 * Contact meets the activation size threshold
686 if (nd->w >= nd->activation_width &&
687 nd->h >= nd->activation_height) {
688 if (nd->id)
690 * first contact, activate now
692 nd->act_state = 0;
693 else {
695 * avoid corrupting this frame
696 * but ensure next frame will
697 * be active
699 nd->act_state = 1;
700 break;
702 } else
704 * Defer adjusting the activation state
705 * until the end of the frame.
707 break;
710 /* Discarding this contact */
711 if (!nd->confidence)
712 break;
714 /* emit a normal (X, Y) for the first point only */
715 if (nd->id == 0) {
717 * TipSwitch is superfluous in multitouch
718 * mode. The footer events tell us
719 * if there is a finger on the screen or
720 * not.
722 nd->first_contact_touch = nd->confidence;
723 input_event(input, EV_ABS, ABS_X, nd->x);
724 input_event(input, EV_ABS, ABS_Y, nd->y);
727 /* Emit MT events */
728 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
729 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
732 * Translate from height and width to size
733 * and orientation.
735 if (nd->w > nd->h) {
736 input_event(input, EV_ABS,
737 ABS_MT_ORIENTATION, 1);
738 input_event(input, EV_ABS,
739 ABS_MT_TOUCH_MAJOR, nd->w);
740 input_event(input, EV_ABS,
741 ABS_MT_TOUCH_MINOR, nd->h);
742 } else {
743 input_event(input, EV_ABS,
744 ABS_MT_ORIENTATION, 0);
745 input_event(input, EV_ABS,
746 ABS_MT_TOUCH_MAJOR, nd->h);
747 input_event(input, EV_ABS,
748 ABS_MT_TOUCH_MINOR, nd->w);
750 input_mt_sync(field->hidinput->input);
751 break;
753 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
754 if (!nd->reading_mt) /* Just to be sure */
755 break;
757 nd->reading_mt = false;
761 * Activation state machine logic:
763 * Fundamental states:
764 * state > 0: Inactive
765 * state <= 0: Active
766 * state < -deactivate_slack:
767 * Pen termination of touch
769 * Specific values of interest
770 * state == activate_slack
771 * no valid input since the last reset
773 * state == 0
774 * general operational state
776 * state == -deactivate_slack
777 * read sufficient empty frames to accept
778 * the end of input and reset
781 if (nd->act_state > 0) { /* Currently inactive */
782 if (value)
784 * Consider each live contact as
785 * evidence of intentional activity.
787 nd->act_state = (nd->act_state > value)
788 ? nd->act_state - value
789 : 0;
790 else
792 * Empty frame before we hit the
793 * activity threshold, reset.
795 nd->act_state = nd->activate_slack;
798 * Entered this block inactive and no
799 * coordinates sent this frame, so hold off
800 * on button state.
802 break;
803 } else { /* Currently active */
804 if (value && nd->act_state >=
805 nd->deactivate_slack)
807 * Live point: clear accumulated
808 * deactivation count.
810 nd->act_state = 0;
811 else if (nd->act_state <= nd->deactivate_slack)
813 * We've consumed the deactivation
814 * slack, time to deactivate and reset.
816 nd->act_state =
817 nd->activate_slack;
818 else { /* Move towards deactivation */
819 nd->act_state--;
820 break;
824 if (nd->first_contact_touch && nd->act_state <= 0) {
826 * Check to see if we're ready to start
827 * emitting touch events.
829 * Note: activation slack will decrease over
830 * the course of the frame, and it will be
831 * inconsistent from the start to the end of
832 * the frame. However if the frame starts
833 * with slack, first_contact_touch will still
834 * be 0 and we will not get to this point.
836 input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
837 input_report_key(input, BTN_TOUCH, 1);
838 } else {
839 input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
840 input_report_key(input, BTN_TOUCH, 0);
842 break;
844 default:
845 /* fall-back to the generic hidinput handling */
846 return 0;
849 not_claimed_input:
851 /* we have handled the hidinput part, now remains hiddev */
852 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
853 hid->hiddev_hid_event(hid, field, usage, value);
855 return 1;
858 static int ntrig_input_configured(struct hid_device *hid,
859 struct hid_input *hidinput)
862 struct input_dev *input = hidinput->input;
864 if (hidinput->report->maxfield < 1)
865 return 0;
867 switch (hidinput->report->field[0]->application) {
868 case HID_DG_PEN:
869 input->name = "N-Trig Pen";
870 break;
871 case HID_DG_TOUCHSCREEN:
872 /* These keys are redundant for fingers, clear them
873 * to prevent incorrect identification */
874 __clear_bit(BTN_TOOL_PEN, input->keybit);
875 __clear_bit(BTN_TOOL_FINGER, input->keybit);
876 __clear_bit(BTN_0, input->keybit);
877 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
879 * The physical touchscreen (single touch)
880 * input has a value for physical, whereas
881 * the multitouch only has logical input
882 * fields.
884 input->name = (hidinput->report->field[0]->physical) ?
885 "N-Trig Touchscreen" :
886 "N-Trig MultiTouch";
887 break;
890 return 0;
893 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
895 int ret;
896 struct ntrig_data *nd;
897 struct hid_report *report;
899 if (id->driver_data)
900 hdev->quirks |= HID_QUIRK_MULTI_INPUT
901 | HID_QUIRK_NO_INIT_REPORTS;
903 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
904 if (!nd) {
905 hid_err(hdev, "cannot allocate N-Trig data\n");
906 return -ENOMEM;
909 nd->reading_mt = false;
910 nd->min_width = 0;
911 nd->min_height = 0;
912 nd->activate_slack = activate_slack;
913 nd->act_state = activate_slack;
914 nd->deactivate_slack = -deactivate_slack;
915 nd->sensor_logical_width = 1;
916 nd->sensor_logical_height = 1;
917 nd->sensor_physical_width = 1;
918 nd->sensor_physical_height = 1;
920 hid_set_drvdata(hdev, nd);
922 ret = hid_parse(hdev);
923 if (ret) {
924 hid_err(hdev, "parse failed\n");
925 goto err_free;
928 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
929 if (ret) {
930 hid_err(hdev, "hw start failed\n");
931 goto err_free;
934 /* This is needed for devices with more recent firmware versions */
935 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
936 if (report) {
937 /* Let the device settle to ensure the wakeup message gets
938 * through */
939 hid_hw_wait(hdev);
940 hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
943 * Sanity check: if the current mode is invalid reset it to
944 * something reasonable.
946 if (ntrig_get_mode(hdev) >= 4)
947 ntrig_set_mode(hdev, 3);
950 ntrig_report_version(hdev);
952 ret = sysfs_create_group(&hdev->dev.kobj,
953 &ntrig_attribute_group);
954 if (ret)
955 hid_err(hdev, "cannot create sysfs group\n");
957 return 0;
958 err_free:
959 kfree(nd);
960 return ret;
963 static void ntrig_remove(struct hid_device *hdev)
965 sysfs_remove_group(&hdev->dev.kobj,
966 &ntrig_attribute_group);
967 hid_hw_stop(hdev);
968 kfree(hid_get_drvdata(hdev));
971 static const struct hid_device_id ntrig_devices[] = {
972 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
973 .driver_data = NTRIG_DUPLICATE_USAGES },
974 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1),
975 .driver_data = NTRIG_DUPLICATE_USAGES },
976 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2),
977 .driver_data = NTRIG_DUPLICATE_USAGES },
978 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3),
979 .driver_data = NTRIG_DUPLICATE_USAGES },
980 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4),
981 .driver_data = NTRIG_DUPLICATE_USAGES },
982 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5),
983 .driver_data = NTRIG_DUPLICATE_USAGES },
984 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6),
985 .driver_data = NTRIG_DUPLICATE_USAGES },
986 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7),
987 .driver_data = NTRIG_DUPLICATE_USAGES },
988 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8),
989 .driver_data = NTRIG_DUPLICATE_USAGES },
990 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9),
991 .driver_data = NTRIG_DUPLICATE_USAGES },
992 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10),
993 .driver_data = NTRIG_DUPLICATE_USAGES },
994 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11),
995 .driver_data = NTRIG_DUPLICATE_USAGES },
996 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12),
997 .driver_data = NTRIG_DUPLICATE_USAGES },
998 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13),
999 .driver_data = NTRIG_DUPLICATE_USAGES },
1000 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14),
1001 .driver_data = NTRIG_DUPLICATE_USAGES },
1002 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15),
1003 .driver_data = NTRIG_DUPLICATE_USAGES },
1004 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16),
1005 .driver_data = NTRIG_DUPLICATE_USAGES },
1006 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17),
1007 .driver_data = NTRIG_DUPLICATE_USAGES },
1008 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18),
1009 .driver_data = NTRIG_DUPLICATE_USAGES },
1012 MODULE_DEVICE_TABLE(hid, ntrig_devices);
1014 static const struct hid_usage_id ntrig_grabbed_usages[] = {
1015 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1016 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
1019 static struct hid_driver ntrig_driver = {
1020 .name = "ntrig",
1021 .id_table = ntrig_devices,
1022 .probe = ntrig_probe,
1023 .remove = ntrig_remove,
1024 .input_mapping = ntrig_input_mapping,
1025 .input_mapped = ntrig_input_mapped,
1026 .input_configured = ntrig_input_configured,
1027 .usage_table = ntrig_grabbed_usages,
1028 .event = ntrig_event,
1030 module_hid_driver(ntrig_driver);
1032 MODULE_LICENSE("GPL");