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