2 * HID driver for N-Trig touchscreens
4 * Copyright (c) 2008-2010 Rafi Rubin
5 * Copyright (c) 2009-2010 Stephane Chatty
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)
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>
25 #define NTRIG_DUPLICATE_USAGES 0x001
27 static unsigned int min_width;
28 static unsigned int min_height;
29 static unsigned int activate_slack = 1;
30 static unsigned int deactivate_slack = 4;
31 static unsigned int activation_width = 64;
32 static unsigned int activation_height = 32;
35 /* Incoming raw values for a single contact */
41 bool first_contact_touch;
48 /* The current activation state. */
51 /* Empty frames to ignore before recognizing the end of activity */
52 __s8 deactivate_slack;
54 /* Frames to ignore before acknowledging the start of activity */
57 /* Minimum size contact to accept */
61 /* Threshold to override activation slack */
62 __u16 activation_width;
63 __u16 activation_height;
65 __u16 sensor_logical_width;
66 __u16 sensor_logical_height;
67 __u16 sensor_physical_width;
68 __u16 sensor_physical_height;
72 static ssize_t show_phys_width(struct device *dev,
73 struct device_attribute *attr,
76 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
77 struct ntrig_data *nd = hid_get_drvdata(hdev);
79 return sprintf(buf, "%d\n", nd->sensor_physical_width);
82 static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL);
84 static ssize_t show_phys_height(struct device *dev,
85 struct device_attribute *attr,
88 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
89 struct ntrig_data *nd = hid_get_drvdata(hdev);
91 return sprintf(buf, "%d\n", nd->sensor_physical_height);
94 static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL);
96 static ssize_t show_log_width(struct device *dev,
97 struct device_attribute *attr,
100 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
101 struct ntrig_data *nd = hid_get_drvdata(hdev);
103 return sprintf(buf, "%d\n", nd->sensor_logical_width);
106 static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL);
108 static ssize_t show_log_height(struct device *dev,
109 struct device_attribute *attr,
112 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
113 struct ntrig_data *nd = hid_get_drvdata(hdev);
115 return sprintf(buf, "%d\n", nd->sensor_logical_height);
118 static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL);
120 static ssize_t show_min_width(struct device *dev,
121 struct device_attribute *attr,
124 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
125 struct ntrig_data *nd = hid_get_drvdata(hdev);
127 return sprintf(buf, "%d\n", nd->min_width *
128 nd->sensor_physical_width /
129 nd->sensor_logical_width);
132 static ssize_t set_min_width(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf, size_t count)
136 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
137 struct ntrig_data *nd = hid_get_drvdata(hdev);
141 if (strict_strtoul(buf, 0, &val))
144 if (val > nd->sensor_physical_width)
147 nd->min_width = val * nd->sensor_logical_width /
148 nd->sensor_physical_width;
153 static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width);
155 static ssize_t show_min_height(struct device *dev,
156 struct device_attribute *attr,
159 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
160 struct ntrig_data *nd = hid_get_drvdata(hdev);
162 return sprintf(buf, "%d\n", nd->min_height *
163 nd->sensor_physical_height /
164 nd->sensor_logical_height);
167 static ssize_t set_min_height(struct device *dev,
168 struct device_attribute *attr,
169 const char *buf, size_t count)
171 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
172 struct ntrig_data *nd = hid_get_drvdata(hdev);
176 if (strict_strtoul(buf, 0, &val))
179 if (val > nd->sensor_physical_height)
182 nd->min_height = val * nd->sensor_logical_height /
183 nd->sensor_physical_height;
188 static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height,
191 static ssize_t show_activate_slack(struct device *dev,
192 struct device_attribute *attr,
195 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
196 struct ntrig_data *nd = hid_get_drvdata(hdev);
198 return sprintf(buf, "%d\n", nd->activate_slack);
201 static ssize_t set_activate_slack(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf, size_t count)
205 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
206 struct ntrig_data *nd = hid_get_drvdata(hdev);
210 if (strict_strtoul(buf, 0, &val))
216 nd->activate_slack = val;
221 static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack,
224 static ssize_t show_activation_width(struct device *dev,
225 struct device_attribute *attr,
228 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
229 struct ntrig_data *nd = hid_get_drvdata(hdev);
231 return sprintf(buf, "%d\n", nd->activation_width *
232 nd->sensor_physical_width /
233 nd->sensor_logical_width);
236 static ssize_t set_activation_width(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t count)
240 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
241 struct ntrig_data *nd = hid_get_drvdata(hdev);
245 if (strict_strtoul(buf, 0, &val))
248 if (val > nd->sensor_physical_width)
251 nd->activation_width = val * nd->sensor_logical_width /
252 nd->sensor_physical_width;
257 static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width,
258 set_activation_width);
260 static ssize_t show_activation_height(struct device *dev,
261 struct device_attribute *attr,
264 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
265 struct ntrig_data *nd = hid_get_drvdata(hdev);
267 return sprintf(buf, "%d\n", nd->activation_height *
268 nd->sensor_physical_height /
269 nd->sensor_logical_height);
272 static ssize_t set_activation_height(struct device *dev,
273 struct device_attribute *attr,
274 const char *buf, size_t count)
276 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
277 struct ntrig_data *nd = hid_get_drvdata(hdev);
281 if (strict_strtoul(buf, 0, &val))
284 if (val > nd->sensor_physical_height)
287 nd->activation_height = val * nd->sensor_logical_height /
288 nd->sensor_physical_height;
293 static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO,
294 show_activation_height, set_activation_height);
296 static ssize_t show_deactivate_slack(struct device *dev,
297 struct device_attribute *attr,
300 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
301 struct ntrig_data *nd = hid_get_drvdata(hdev);
303 return sprintf(buf, "%d\n", -nd->deactivate_slack);
306 static ssize_t set_deactivate_slack(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t count)
310 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
311 struct ntrig_data *nd = hid_get_drvdata(hdev);
315 if (strict_strtoul(buf, 0, &val))
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.
326 nd->deactivate_slack = -val;
331 static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack,
332 set_deactivate_slack);
334 static 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,
348 static struct attribute_group ntrig_attribute_group = {
353 * this driver is aimed at two firmware versions in circulation:
354 * - dual pen/finger single touch
355 * - finger multitouch, pen not working
358 static 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)
362 struct ntrig_data *nd = hid_get_drvdata(hdev);
364 /* No special mappings needed for the pen and single touch */
368 switch (usage->hid & HID_USAGE_PAGE) {
370 switch (usage->hid) {
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);
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;
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);
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;
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:
427 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
429 hid_map_usage(hi, usage, bit, max,
430 EV_ABS, ABS_MT_TOUCH_MAJOR);
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,
442 /* we do not want to map these: no input-oriented meaning */
449 static 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)
453 /* No special mappings needed for the pen and single touch */
457 if (usage->type == EV_KEY || usage->type == EV_REL
458 || usage->type == EV_ABS)
459 clear_bit(usage->code, *bit);
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
470 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
471 struct hid_usage *usage, __s32 value)
473 struct input_dev *input = field->hidinput->input;
474 struct ntrig_data *nd = hid_get_drvdata(hid);
476 /* No special handling needed for the pen */
477 if (field->application == HID_DG_PEN)
480 if (hid->claimed & HID_CLAIMED_INPUT) {
481 switch (usage->hid) {
483 /* Tag indicating the start of a multitouch group */
485 nd->first_contact_touch = 0;
487 case HID_DG_TIPSWITCH:
488 nd->tipswitch = value;
489 /* Prevent emission of touch until validated */
491 case HID_DG_CONFIDENCE:
492 nd->confidence = value;
496 /* Clear the contact footer */
497 nd->mt_foot_count = 0;
502 case HID_DG_CONTACTID:
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
515 if (!nd->reading_mt) {
517 * TipSwitch indicates the presence of a
518 * finger in single touch mode.
520 input_report_key(input, BTN_TOUCH,
522 input_report_key(input, BTN_TOOL_DOUBLETAP,
524 input_event(input, EV_ABS, ABS_X, nd->x);
525 input_event(input, EV_ABS, ABS_Y, nd->y);
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
536 /* Shouldn't get more than 4 footer packets, so skip */
537 if (nd->mt_foot_count >= 4)
540 nd->mt_footer[nd->mt_foot_count++] = value;
542 /* if the footer isn't complete break */
543 if (nd->mt_foot_count != 4)
546 /* Pen activity signal. */
547 if (nd->mt_footer[2]) {
549 * When the pen deactivates touch, we see a
550 * bogus frame with ContactCount > 0.
552 * save a bit of work by ensuring act_state < 0
553 * even if deactivation slack is turned off.
555 nd->act_state = deactivate_slack - 1;
561 * The first footer value indicates the presence of a
564 if (nd->mt_footer[0]) {
566 * We do not want to process contacts under
567 * the size threshold, but do not want to
568 * ignore them for activation state
570 if (nd->w < nd->min_width ||
571 nd->h < nd->min_height)
576 if (nd->act_state > 0) {
578 * Contact meets the activation size threshold
580 if (nd->w >= nd->activation_width &&
581 nd->h >= nd->activation_height) {
584 * first contact, activate now
589 * avoid corrupting this frame
590 * but ensure next frame will
598 * Defer adjusting the activation state
599 * until the end of the frame.
604 /* Discarding this contact */
608 /* emit a normal (X, Y) for the first point only */
611 * TipSwitch is superfluous in multitouch
612 * mode. The footer events tell us
613 * if there is a finger on the screen or
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);
622 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
623 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
626 * Translate from height and width to size
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);
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);
644 input_mt_sync(field->hidinput->input);
647 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
648 if (!nd->reading_mt) /* Just to be sure */
655 * Activation state machine logic:
657 * Fundamental states:
658 * state > 0: Inactive
660 * state < -deactivate_slack:
661 * Pen termination of touch
663 * Specific values of interest
664 * state == activate_slack
665 * no valid input since the last reset
668 * general operational state
670 * state == -deactivate_slack
671 * read sufficient empty frames to accept
672 * the end of input and reset
675 if (nd->act_state > 0) { /* Currently inactive */
678 * Consider each live contact as
679 * evidence of intentional activity.
681 nd->act_state = (nd->act_state > value)
682 ? nd->act_state - value
686 * Empty frame before we hit the
687 * activity threshold, reset.
689 nd->act_state = nd->activate_slack;
692 * Entered this block inactive and no
693 * coordinates sent this frame, so hold off
697 } else { /* Currently active */
698 if (value && nd->act_state >=
699 nd->deactivate_slack)
701 * Live point: clear accumulated
702 * deactivation count.
705 else if (nd->act_state <= nd->deactivate_slack)
707 * We've consumed the deactivation
708 * slack, time to deactivate and reset.
712 else { /* Move towards deactivation */
718 if (nd->first_contact_touch && nd->act_state <= 0) {
720 * Check to see if we're ready to start
721 * emitting touch events.
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.
730 input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
731 input_report_key(input, BTN_TOUCH, 1);
733 input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
734 input_report_key(input, BTN_TOUCH, 0);
739 /* fall-back to the generic hidinput handling */
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);
751 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
754 struct ntrig_data *nd;
755 struct hid_input *hidinput;
756 struct input_dev *input;
757 struct hid_report *report;
760 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
762 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
764 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
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;
779 hid_set_drvdata(hdev, nd);
781 ret = hid_parse(hdev);
783 dev_err(&hdev->dev, "parse failed\n");
787 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
789 dev_err(&hdev->dev, "hw start failed\n");
794 list_for_each_entry(hidinput, &hdev->inputs, list) {
795 if (hidinput->report->maxfield < 1)
798 input = hidinput->input;
799 switch (hidinput->report->field[0]->application) {
801 input->name = "N-Trig Pen";
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);
811 * The physical touchscreen (single touch)
812 * input has a value for physical, whereas
813 * the multitouch only has logical input
817 (hidinput->report->field[0]
819 "N-Trig Touchscreen" :
825 /* This is needed for devices with more recent firmware versions */
826 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
828 usbhid_submit_report(hdev, report, USB_DIR_OUT);
830 ret = sysfs_create_group(&hdev->dev.kobj,
831 &ntrig_attribute_group);
839 static void ntrig_remove(struct hid_device *hdev)
841 sysfs_remove_group(&hdev->dev.kobj,
842 &ntrig_attribute_group);
844 kfree(hid_get_drvdata(hdev));
847 static 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 },
852 MODULE_DEVICE_TABLE(hid, ntrig_devices);
854 static 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 }
859 static struct hid_driver ntrig_driver = {
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,
870 static int __init ntrig_init(void)
872 return hid_register_driver(&ntrig_driver);
875 static void __exit ntrig_exit(void)
877 hid_unregister_driver(&ntrig_driver);
880 module_init(ntrig_init);
881 module_exit(ntrig_exit);
882 MODULE_LICENSE("GPL");