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