blob: dffffa763bd76138a6cbece2ee8e424fcf2c3df8 [file] [log] [blame]
Rafi Rubin94011f92008-11-19 15:54:46 +01001/*
Stephane Chatty57fd6372009-05-20 15:49:35 +02002 * HID driver for N-Trig touchscreens
Rafi Rubin94011f92008-11-19 15:54:46 +01003 *
Stephane Chatty65499812010-04-06 22:22:58 +02004 * Copyright (c) 2008-2010 Rafi Rubin
5 * Copyright (c) 2009-2010 Stephane Chatty
Rafi Rubin94011f92008-11-19 15:54:46 +01006 *
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>
Stephane Chatty65499812010-04-06 22:22:58 +020018#include <linux/usb.h>
19#include "usbhid/usbhid.h"
Rafi Rubin94011f92008-11-19 15:54:46 +010020#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Rafi Rubin94011f92008-11-19 15:54:46 +010022
23#include "hid-ids.h"
24
25#define NTRIG_DUPLICATE_USAGES 0x001
26
Rafi Rubin369db2a62010-05-04 14:20:15 -040027static unsigned int min_width;
Rafi Rubinab3f4982010-05-04 14:20:16 -040028module_param(min_width, uint, 0644);
29MODULE_PARM_DESC(min_width, "Minimum touch contact width to accept.");
30
Rafi Rubin369db2a62010-05-04 14:20:15 -040031static unsigned int min_height;
Rafi Rubinab3f4982010-05-04 14:20:16 -040032module_param(min_height, uint, 0644);
33MODULE_PARM_DESC(min_height, "Minimum touch contact height to accept.");
34
Rafi Rubin369db2a62010-05-04 14:20:15 -040035static unsigned int activate_slack = 1;
Rafi Rubinab3f4982010-05-04 14:20:16 -040036module_param(activate_slack, uint, 0644);
37MODULE_PARM_DESC(activate_slack, "Number of touch frames to ignore at "
38 "the start of touch input.");
39
Rafi Rubin369db2a62010-05-04 14:20:15 -040040static unsigned int deactivate_slack = 4;
Rafi Rubinab3f4982010-05-04 14:20:16 -040041module_param(deactivate_slack, uint, 0644);
42MODULE_PARM_DESC(deactivate_slack, "Number of empty frames to ignore before "
43 "deactivating touch.");
44
Rafi Rubin369db2a62010-05-04 14:20:15 -040045static unsigned int activation_width = 64;
Rafi Rubinab3f4982010-05-04 14:20:16 -040046module_param(activation_width, uint, 0644);
47MODULE_PARM_DESC(activation_width, "Width threshold to immediately start "
48 "processing touch events.");
49
Rafi Rubin369db2a62010-05-04 14:20:15 -040050static unsigned int activation_height = 32;
Rafi Rubinab3f4982010-05-04 14:20:16 -040051module_param(activation_height, uint, 0644);
52MODULE_PARM_DESC(activation_height, "Height threshold to immediately start "
53 "processing touch events.");
Rafi Rubin369db2a62010-05-04 14:20:15 -040054
Stephane Chatty57fd6372009-05-20 15:49:35 +020055struct ntrig_data {
Rafi Rubindbf2b172010-02-12 21:13:05 -050056 /* Incoming raw values for a single contact */
57 __u16 x, y, w, h;
58 __u16 id;
Rafi Rubin250d3772010-05-03 05:08:29 -040059
60 bool tipswitch;
61 bool confidence;
62 bool first_contact_touch;
Rafi Rubindbf2b172010-02-12 21:13:05 -050063
64 bool reading_mt;
Rafi Rubindbf2b172010-02-12 21:13:05 -050065
66 __u8 mt_footer[4];
67 __u8 mt_foot_count;
Rafi Rubin369db2a62010-05-04 14:20:15 -040068
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;
Stephane Chatty57fd6372009-05-20 15:49:35 +020090};
91
Rafi Rubineab32f52010-05-04 14:20:17 -040092
Rafi Rubin02778732010-09-08 11:46:14 +020093/*
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
Joe Perches4291ee32010-12-09 19:29:03 -0800133 hid_info(hdev, "Firmware version: %s (%02x%02x %02x%02x)\n",
Rafi Rubin02778732010-09-08 11:46:14 +0200134 buf, data[2], data[3], data[4], data[5]);
135 }
136
137err_free:
138 kfree(data);
139}
140
Rafi Rubineab32f52010-05-04 14:20:17 -0400141static ssize_t show_phys_width(struct device *dev,
142 struct device_attribute *attr,
143 char *buf)
144{
145 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
146 struct ntrig_data *nd = hid_get_drvdata(hdev);
147
148 return sprintf(buf, "%d\n", nd->sensor_physical_width);
149}
150
151static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL);
152
153static ssize_t show_phys_height(struct device *dev,
154 struct device_attribute *attr,
155 char *buf)
156{
157 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
158 struct ntrig_data *nd = hid_get_drvdata(hdev);
159
160 return sprintf(buf, "%d\n", nd->sensor_physical_height);
161}
162
163static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL);
164
165static ssize_t show_log_width(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
168{
169 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
170 struct ntrig_data *nd = hid_get_drvdata(hdev);
171
172 return sprintf(buf, "%d\n", nd->sensor_logical_width);
173}
174
175static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL);
176
177static ssize_t show_log_height(struct device *dev,
178 struct device_attribute *attr,
179 char *buf)
180{
181 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
182 struct ntrig_data *nd = hid_get_drvdata(hdev);
183
184 return sprintf(buf, "%d\n", nd->sensor_logical_height);
185}
186
187static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL);
188
189static ssize_t show_min_width(struct device *dev,
190 struct device_attribute *attr,
191 char *buf)
192{
193 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
194 struct ntrig_data *nd = hid_get_drvdata(hdev);
195
196 return sprintf(buf, "%d\n", nd->min_width *
197 nd->sensor_physical_width /
198 nd->sensor_logical_width);
199}
200
201static ssize_t set_min_width(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf, size_t count)
204{
205 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
206 struct ntrig_data *nd = hid_get_drvdata(hdev);
207
208 unsigned long val;
209
210 if (strict_strtoul(buf, 0, &val))
211 return -EINVAL;
212
213 if (val > nd->sensor_physical_width)
214 return -EINVAL;
215
216 nd->min_width = val * nd->sensor_logical_width /
217 nd->sensor_physical_width;
218
219 return count;
220}
221
222static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width);
223
224static ssize_t show_min_height(struct device *dev,
225 struct device_attribute *attr,
226 char *buf)
227{
228 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
229 struct ntrig_data *nd = hid_get_drvdata(hdev);
230
231 return sprintf(buf, "%d\n", nd->min_height *
232 nd->sensor_physical_height /
233 nd->sensor_logical_height);
234}
235
236static ssize_t set_min_height(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t count)
239{
240 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
241 struct ntrig_data *nd = hid_get_drvdata(hdev);
242
243 unsigned long val;
244
245 if (strict_strtoul(buf, 0, &val))
246 return -EINVAL;
247
248 if (val > nd->sensor_physical_height)
249 return -EINVAL;
250
251 nd->min_height = val * nd->sensor_logical_height /
252 nd->sensor_physical_height;
253
254 return count;
255}
256
257static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height,
258 set_min_height);
259
260static ssize_t show_activate_slack(struct device *dev,
261 struct device_attribute *attr,
262 char *buf)
263{
264 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
265 struct ntrig_data *nd = hid_get_drvdata(hdev);
266
267 return sprintf(buf, "%d\n", nd->activate_slack);
268}
269
270static ssize_t set_activate_slack(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t count)
273{
274 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
275 struct ntrig_data *nd = hid_get_drvdata(hdev);
276
277 unsigned long val;
278
279 if (strict_strtoul(buf, 0, &val))
280 return -EINVAL;
281
282 if (val > 0x7f)
283 return -EINVAL;
284
285 nd->activate_slack = val;
286
287 return count;
288}
289
290static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack,
291 set_activate_slack);
292
293static ssize_t show_activation_width(struct device *dev,
294 struct device_attribute *attr,
295 char *buf)
296{
297 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
298 struct ntrig_data *nd = hid_get_drvdata(hdev);
299
300 return sprintf(buf, "%d\n", nd->activation_width *
301 nd->sensor_physical_width /
302 nd->sensor_logical_width);
303}
304
305static ssize_t set_activation_width(struct device *dev,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
308{
309 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
310 struct ntrig_data *nd = hid_get_drvdata(hdev);
311
312 unsigned long val;
313
314 if (strict_strtoul(buf, 0, &val))
315 return -EINVAL;
316
317 if (val > nd->sensor_physical_width)
318 return -EINVAL;
319
320 nd->activation_width = val * nd->sensor_logical_width /
321 nd->sensor_physical_width;
322
323 return count;
324}
325
326static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width,
327 set_activation_width);
328
329static ssize_t show_activation_height(struct device *dev,
330 struct device_attribute *attr,
331 char *buf)
332{
333 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
334 struct ntrig_data *nd = hid_get_drvdata(hdev);
335
336 return sprintf(buf, "%d\n", nd->activation_height *
337 nd->sensor_physical_height /
338 nd->sensor_logical_height);
339}
340
341static ssize_t set_activation_height(struct device *dev,
342 struct device_attribute *attr,
343 const char *buf, size_t count)
344{
345 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
346 struct ntrig_data *nd = hid_get_drvdata(hdev);
347
348 unsigned long val;
349
350 if (strict_strtoul(buf, 0, &val))
351 return -EINVAL;
352
353 if (val > nd->sensor_physical_height)
354 return -EINVAL;
355
356 nd->activation_height = val * nd->sensor_logical_height /
357 nd->sensor_physical_height;
358
359 return count;
360}
361
362static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO,
363 show_activation_height, set_activation_height);
364
365static ssize_t show_deactivate_slack(struct device *dev,
366 struct device_attribute *attr,
367 char *buf)
368{
369 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
370 struct ntrig_data *nd = hid_get_drvdata(hdev);
371
372 return sprintf(buf, "%d\n", -nd->deactivate_slack);
373}
374
375static ssize_t set_deactivate_slack(struct device *dev,
376 struct device_attribute *attr,
377 const char *buf, size_t count)
378{
379 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
380 struct ntrig_data *nd = hid_get_drvdata(hdev);
381
382 unsigned long val;
383
384 if (strict_strtoul(buf, 0, &val))
385 return -EINVAL;
386
387 /*
388 * No more than 8 terminal frames have been observed so far
389 * and higher slack is highly likely to leave the single
390 * touch emulation stuck down.
391 */
392 if (val > 7)
393 return -EINVAL;
394
395 nd->deactivate_slack = -val;
396
397 return count;
398}
399
400static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack,
401 set_deactivate_slack);
402
403static struct attribute *sysfs_attrs[] = {
404 &dev_attr_sensor_physical_width.attr,
405 &dev_attr_sensor_physical_height.attr,
406 &dev_attr_sensor_logical_width.attr,
407 &dev_attr_sensor_logical_height.attr,
408 &dev_attr_min_height.attr,
409 &dev_attr_min_width.attr,
410 &dev_attr_activate_slack.attr,
411 &dev_attr_activation_width.attr,
412 &dev_attr_activation_height.attr,
413 &dev_attr_deactivate_slack.attr,
414 NULL
415};
416
417static struct attribute_group ntrig_attribute_group = {
418 .attrs = sysfs_attrs
419};
420
Stephane Chatty57fd6372009-05-20 15:49:35 +0200421/*
422 * this driver is aimed at two firmware versions in circulation:
423 * - dual pen/finger single touch
424 * - finger multitouch, pen not working
425 */
426
Rafi Rubin94011f92008-11-19 15:54:46 +0100427static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
Rafi Rubina52dc342010-08-26 00:54:55 -0400428 struct hid_field *field, struct hid_usage *usage,
429 unsigned long **bit, int *max)
Rafi Rubin94011f92008-11-19 15:54:46 +0100430{
Rafi Rubin369db2a62010-05-04 14:20:15 -0400431 struct ntrig_data *nd = hid_get_drvdata(hdev);
432
Rafi Rubindbf2b172010-02-12 21:13:05 -0500433 /* No special mappings needed for the pen and single touch */
434 if (field->physical)
Rafi Rubin943ed462010-02-11 22:14:05 -0500435 return 0;
436
Stephane Chatty57fd6372009-05-20 15:49:35 +0200437 switch (usage->hid & HID_USAGE_PAGE) {
Stephane Chatty57fd6372009-05-20 15:49:35 +0200438 case HID_UP_GENDESK:
439 switch (usage->hid) {
440 case HID_GD_X:
441 hid_map_usage(hi, usage, bit, max,
442 EV_ABS, ABS_MT_POSITION_X);
443 input_set_abs_params(hi->input, ABS_X,
444 field->logical_minimum,
445 field->logical_maximum, 0, 0);
Rafi Rubin369db2a62010-05-04 14:20:15 -0400446
447 if (!nd->sensor_logical_width) {
448 nd->sensor_logical_width =
449 field->logical_maximum -
450 field->logical_minimum;
451 nd->sensor_physical_width =
452 field->physical_maximum -
453 field->physical_minimum;
454 nd->activation_width = activation_width *
455 nd->sensor_logical_width /
456 nd->sensor_physical_width;
457 nd->min_width = min_width *
458 nd->sensor_logical_width /
459 nd->sensor_physical_width;
460 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200461 return 1;
462 case HID_GD_Y:
463 hid_map_usage(hi, usage, bit, max,
464 EV_ABS, ABS_MT_POSITION_Y);
465 input_set_abs_params(hi->input, ABS_Y,
466 field->logical_minimum,
467 field->logical_maximum, 0, 0);
Rafi Rubin369db2a62010-05-04 14:20:15 -0400468
469 if (!nd->sensor_logical_height) {
470 nd->sensor_logical_height =
471 field->logical_maximum -
472 field->logical_minimum;
473 nd->sensor_physical_height =
474 field->physical_maximum -
475 field->physical_minimum;
476 nd->activation_height = activation_height *
477 nd->sensor_logical_height /
478 nd->sensor_physical_height;
479 nd->min_height = min_height *
480 nd->sensor_logical_height /
481 nd->sensor_physical_height;
482 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200483 return 1;
484 }
485 return 0;
486
487 case HID_UP_DIGITIZER:
488 switch (usage->hid) {
489 /* we do not want to map these for now */
Rafi Rubindbf2b172010-02-12 21:13:05 -0500490 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
Stephane Chatty57fd6372009-05-20 15:49:35 +0200491 case HID_DG_INPUTMODE:
492 case HID_DG_DEVICEINDEX:
Stephane Chatty57fd6372009-05-20 15:49:35 +0200493 case HID_DG_CONTACTMAX:
494 return -1;
495
Stephane Chatty57fd6372009-05-20 15:49:35 +0200496 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
497 case HID_DG_WIDTH:
498 hid_map_usage(hi, usage, bit, max,
Rafi Rubina52dc342010-08-26 00:54:55 -0400499 EV_ABS, ABS_MT_TOUCH_MAJOR);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200500 return 1;
501 case HID_DG_HEIGHT:
502 hid_map_usage(hi, usage, bit, max,
Rafi Rubina52dc342010-08-26 00:54:55 -0400503 EV_ABS, ABS_MT_TOUCH_MINOR);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200504 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
Rafi Rubina52dc342010-08-26 00:54:55 -0400505 0, 1, 0, 0);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200506 return 1;
507 }
508 return 0;
509
510 case 0xff000000:
511 /* we do not want to map these: no input-oriented meaning */
512 return -1;
Rafi Rubin94011f92008-11-19 15:54:46 +0100513 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200514
Rafi Rubin94011f92008-11-19 15:54:46 +0100515 return 0;
516}
517
518static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
Rafi Rubina52dc342010-08-26 00:54:55 -0400519 struct hid_field *field, struct hid_usage *usage,
520 unsigned long **bit, int *max)
Rafi Rubin94011f92008-11-19 15:54:46 +0100521{
Rafi Rubindbf2b172010-02-12 21:13:05 -0500522 /* No special mappings needed for the pen and single touch */
523 if (field->physical)
Rafi Rubin943ed462010-02-11 22:14:05 -0500524 return 0;
Rafi Rubinb0549cf2010-02-11 22:14:06 -0500525
Rafi Rubin94011f92008-11-19 15:54:46 +0100526 if (usage->type == EV_KEY || usage->type == EV_REL
527 || usage->type == EV_ABS)
528 clear_bit(usage->code, *bit);
529
530 return 0;
531}
Stephane Chatty57fd6372009-05-20 15:49:35 +0200532
533/*
534 * this function is called upon all reports
535 * so that we can filter contact point information,
536 * decide whether we are in multi or single touch mode
537 * and call input_mt_sync after each point if necessary
538 */
539static int ntrig_event (struct hid_device *hid, struct hid_field *field,
Rafi Rubina52dc342010-08-26 00:54:55 -0400540 struct hid_usage *usage, __s32 value)
Stephane Chatty57fd6372009-05-20 15:49:35 +0200541{
Stephane Chatty57fd6372009-05-20 15:49:35 +0200542 struct ntrig_data *nd = hid_get_drvdata(hid);
Rafi Rubinf41a52d2011-03-08 00:24:29 -0500543 struct input_dev *input;
544
545 /* Skip processing if not a claimed input */
546 if (!(hid->claimed & HID_CLAIMED_INPUT))
547 goto not_claimed_input;
548
549 /* This function is being called before the structures are fully
550 * initialized */
551 if(!(field->hidinput && field->hidinput->input))
552 return -EINVAL;
553
554 input = field->hidinput->input;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200555
Rafi Rubin943ed462010-02-11 22:14:05 -0500556 /* No special handling needed for the pen */
557 if (field->application == HID_DG_PEN)
558 return 0;
559
Rafi Rubinff404622011-03-07 21:13:28 -0500560 switch (usage->hid) {
561 case 0xff000001:
562 /* Tag indicating the start of a multitouch group */
563 nd->reading_mt = 1;
564 nd->first_contact_touch = 0;
565 break;
566 case HID_DG_TIPSWITCH:
567 nd->tipswitch = value;
568 /* Prevent emission of touch until validated */
569 return 1;
570 case HID_DG_CONFIDENCE:
571 nd->confidence = value;
572 break;
573 case HID_GD_X:
574 nd->x = value;
575 /* Clear the contact footer */
576 nd->mt_foot_count = 0;
577 break;
578 case HID_GD_Y:
579 nd->y = value;
580 break;
581 case HID_DG_CONTACTID:
582 nd->id = value;
583 break;
584 case HID_DG_WIDTH:
585 nd->w = value;
586 break;
587 case HID_DG_HEIGHT:
588 nd->h = value;
589 /*
590 * when in single touch mode, this is the last
591 * report received in a finger event. We want
592 * to emit a normal (X, Y) position
593 */
594 if (!nd->reading_mt) {
Stephane Chatty57fd6372009-05-20 15:49:35 +0200595 /*
Rafi Rubinff404622011-03-07 21:13:28 -0500596 * TipSwitch indicates the presence of a
597 * finger in single touch mode.
Stephane Chatty57fd6372009-05-20 15:49:35 +0200598 */
Rafi Rubinff404622011-03-07 21:13:28 -0500599 input_report_key(input, BTN_TOUCH,
600 nd->tipswitch);
601 input_report_key(input, BTN_TOOL_DOUBLETAP,
602 nd->tipswitch);
603 input_event(input, EV_ABS, ABS_X, nd->x);
604 input_event(input, EV_ABS, ABS_Y, nd->y);
605 }
606 break;
607 case 0xff000002:
608 /*
609 * we receive this when the device is in multitouch
610 * mode. The first of the three values tagged with
611 * this usage tells if the contact point is real
612 * or a placeholder
613 */
614
615 /* Shouldn't get more than 4 footer packets, so skip */
616 if (nd->mt_foot_count >= 4)
Stephane Chatty57fd6372009-05-20 15:49:35 +0200617 break;
Rafi Rubinff404622011-03-07 21:13:28 -0500618
619 nd->mt_footer[nd->mt_foot_count++] = value;
620
621 /* if the footer isn't complete break */
622 if (nd->mt_foot_count != 4)
623 break;
624
625 /* Pen activity signal. */
626 if (nd->mt_footer[2]) {
Stephane Chatty57fd6372009-05-20 15:49:35 +0200627 /*
Rafi Rubinff404622011-03-07 21:13:28 -0500628 * When the pen deactivates touch, we see a
629 * bogus frame with ContactCount > 0.
630 * We can
631 * save a bit of work by ensuring act_state < 0
632 * even if deactivation slack is turned off.
Stephane Chatty57fd6372009-05-20 15:49:35 +0200633 */
Rafi Rubinff404622011-03-07 21:13:28 -0500634 nd->act_state = deactivate_slack - 1;
635 nd->confidence = 0;
636 break;
637 }
Rafi Rubindbf2b172010-02-12 21:13:05 -0500638
Rafi Rubinff404622011-03-07 21:13:28 -0500639 /*
640 * The first footer value indicates the presence of a
641 * finger.
642 */
643 if (nd->mt_footer[0]) {
644 /*
645 * We do not want to process contacts under
646 * the size threshold, but do not want to
647 * ignore them for activation state
648 */
649 if (nd->w < nd->min_width ||
650 nd->h < nd->min_height)
Rafi Rubindbf2b172010-02-12 21:13:05 -0500651 nd->confidence = 0;
Rafi Rubinff404622011-03-07 21:13:28 -0500652 } else
Rafi Rubindbf2b172010-02-12 21:13:05 -0500653 break;
654
Rafi Rubinff404622011-03-07 21:13:28 -0500655 if (nd->act_state > 0) {
Rafi Rubin369db2a62010-05-04 14:20:15 -0400656 /*
Rafi Rubinff404622011-03-07 21:13:28 -0500657 * Contact meets the activation size threshold
Rafi Rubin369db2a62010-05-04 14:20:15 -0400658 */
Rafi Rubinff404622011-03-07 21:13:28 -0500659 if (nd->w >= nd->activation_width &&
660 nd->h >= nd->activation_height) {
661 if (nd->id)
Rafi Rubin369db2a62010-05-04 14:20:15 -0400662 /*
Rafi Rubinff404622011-03-07 21:13:28 -0500663 * first contact, activate now
Rafi Rubin369db2a62010-05-04 14:20:15 -0400664 */
665 nd->act_state = 0;
Rafi Rubinff404622011-03-07 21:13:28 -0500666 else {
Rafi Rubin369db2a62010-05-04 14:20:15 -0400667 /*
Rafi Rubinff404622011-03-07 21:13:28 -0500668 * avoid corrupting this frame
669 * but ensure next frame will
670 * be active
Rafi Rubin369db2a62010-05-04 14:20:15 -0400671 */
Rafi Rubinff404622011-03-07 21:13:28 -0500672 nd->act_state = 1;
Rafi Rubin369db2a62010-05-04 14:20:15 -0400673 break;
674 }
Rafi Rubinff404622011-03-07 21:13:28 -0500675 } else
Rafi Rubin369db2a62010-05-04 14:20:15 -0400676 /*
Rafi Rubinff404622011-03-07 21:13:28 -0500677 * Defer adjusting the activation state
678 * until the end of the frame.
Rafi Rubin369db2a62010-05-04 14:20:15 -0400679 */
Rafi Rubinff404622011-03-07 21:13:28 -0500680 break;
681 }
682
683 /* Discarding this contact */
684 if (!nd->confidence)
Stephane Chatty57fd6372009-05-20 15:49:35 +0200685 break;
686
Rafi Rubinff404622011-03-07 21:13:28 -0500687 /* emit a normal (X, Y) for the first point only */
688 if (nd->id == 0) {
689 /*
690 * TipSwitch is superfluous in multitouch
691 * mode. The footer events tell us
692 * if there is a finger on the screen or
693 * not.
694 */
695 nd->first_contact_touch = nd->confidence;
696 input_event(input, EV_ABS, ABS_X, nd->x);
697 input_event(input, EV_ABS, ABS_Y, nd->y);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200698 }
Rafi Rubinff404622011-03-07 21:13:28 -0500699
700 /* Emit MT events */
701 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
702 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
703
704 /*
705 * Translate from height and width to size
706 * and orientation.
707 */
708 if (nd->w > nd->h) {
709 input_event(input, EV_ABS,
710 ABS_MT_ORIENTATION, 1);
711 input_event(input, EV_ABS,
712 ABS_MT_TOUCH_MAJOR, nd->w);
713 input_event(input, EV_ABS,
714 ABS_MT_TOUCH_MINOR, nd->h);
715 } else {
716 input_event(input, EV_ABS,
717 ABS_MT_ORIENTATION, 0);
718 input_event(input, EV_ABS,
719 ABS_MT_TOUCH_MAJOR, nd->h);
720 input_event(input, EV_ABS,
721 ABS_MT_TOUCH_MINOR, nd->w);
722 }
723 input_mt_sync(field->hidinput->input);
724 break;
725
726 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
727 if (!nd->reading_mt) /* Just to be sure */
728 break;
729
730 nd->reading_mt = 0;
731
732
733 /*
734 * Activation state machine logic:
735 *
736 * Fundamental states:
737 * state > 0: Inactive
738 * state <= 0: Active
739 * state < -deactivate_slack:
740 * Pen termination of touch
741 *
742 * Specific values of interest
743 * state == activate_slack
744 * no valid input since the last reset
745 *
746 * state == 0
747 * general operational state
748 *
749 * state == -deactivate_slack
750 * read sufficient empty frames to accept
751 * the end of input and reset
752 */
753
754 if (nd->act_state > 0) { /* Currently inactive */
755 if (value)
756 /*
757 * Consider each live contact as
758 * evidence of intentional activity.
759 */
760 nd->act_state = (nd->act_state > value)
761 ? nd->act_state - value
762 : 0;
763 else
764 /*
765 * Empty frame before we hit the
766 * activity threshold, reset.
767 */
768 nd->act_state = nd->activate_slack;
769
770 /*
771 * Entered this block inactive and no
772 * coordinates sent this frame, so hold off
773 * on button state.
774 */
775 break;
776 } else { /* Currently active */
777 if (value && nd->act_state >=
778 nd->deactivate_slack)
779 /*
780 * Live point: clear accumulated
781 * deactivation count.
782 */
783 nd->act_state = 0;
784 else if (nd->act_state <= nd->deactivate_slack)
785 /*
786 * We've consumed the deactivation
787 * slack, time to deactivate and reset.
788 */
789 nd->act_state =
790 nd->activate_slack;
791 else { /* Move towards deactivation */
792 nd->act_state--;
793 break;
794 }
795 }
796
797 if (nd->first_contact_touch && nd->act_state <= 0) {
798 /*
799 * Check to see if we're ready to start
800 * emitting touch events.
801 *
802 * Note: activation slack will decrease over
803 * the course of the frame, and it will be
804 * inconsistent from the start to the end of
805 * the frame. However if the frame starts
806 * with slack, first_contact_touch will still
807 * be 0 and we will not get to this point.
808 */
809 input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
810 input_report_key(input, BTN_TOUCH, 1);
811 } else {
812 input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
813 input_report_key(input, BTN_TOUCH, 0);
814 }
815 break;
816
817 default:
818 /* fall-back to the generic hidinput handling */
819 return 0;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200820 }
821
Rafi Rubinf41a52d2011-03-08 00:24:29 -0500822not_claimed_input:
823
Stephane Chatty57fd6372009-05-20 15:49:35 +0200824 /* we have handled the hidinput part, now remains hiddev */
Rafi Rubin943ed462010-02-11 22:14:05 -0500825 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
826 hid->hiddev_hid_event(hid, field, usage, value);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200827
828 return 1;
829}
830
831static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
832{
833 int ret;
834 struct ntrig_data *nd;
Rafi Rubin943ed462010-02-11 22:14:05 -0500835 struct hid_input *hidinput;
836 struct input_dev *input;
Stephane Chatty65499812010-04-06 22:22:58 +0200837 struct hid_report *report;
Rafi Rubin943ed462010-02-11 22:14:05 -0500838
839 if (id->driver_data)
Rafi Rubin6638ded2011-03-09 23:33:51 -0500840 hdev->quirks |= HID_QUIRK_MULTI_INPUT
841 | HID_QUIRK_NO_INIT_REPORTS;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200842
843 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
844 if (!nd) {
Joe Perches4291ee32010-12-09 19:29:03 -0800845 hid_err(hdev, "cannot allocate N-Trig data\n");
Stephane Chatty57fd6372009-05-20 15:49:35 +0200846 return -ENOMEM;
847 }
Rafi Rubindbf2b172010-02-12 21:13:05 -0500848
849 nd->reading_mt = 0;
Rafi Rubin369db2a62010-05-04 14:20:15 -0400850 nd->min_width = 0;
851 nd->min_height = 0;
852 nd->activate_slack = activate_slack;
853 nd->act_state = activate_slack;
854 nd->deactivate_slack = -deactivate_slack;
855 nd->sensor_logical_width = 0;
856 nd->sensor_logical_height = 0;
857 nd->sensor_physical_width = 0;
858 nd->sensor_physical_height = 0;
859
Stephane Chatty57fd6372009-05-20 15:49:35 +0200860 hid_set_drvdata(hdev, nd);
861
862 ret = hid_parse(hdev);
Rafi Rubin943ed462010-02-11 22:14:05 -0500863 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800864 hid_err(hdev, "parse failed\n");
Rafi Rubin943ed462010-02-11 22:14:05 -0500865 goto err_free;
866 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200867
Rafi Rubin943ed462010-02-11 22:14:05 -0500868 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
869 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800870 hid_err(hdev, "hw start failed\n");
Rafi Rubin943ed462010-02-11 22:14:05 -0500871 goto err_free;
872 }
Rafi Rubin837b4752009-06-23 14:09:26 -0400873
Rafi Rubin943ed462010-02-11 22:14:05 -0500874
875 list_for_each_entry(hidinput, &hdev->inputs, list) {
Rafi Rubin28865392010-03-10 16:10:28 +0100876 if (hidinput->report->maxfield < 1)
877 continue;
878
Rafi Rubin943ed462010-02-11 22:14:05 -0500879 input = hidinput->input;
880 switch (hidinput->report->field[0]->application) {
881 case HID_DG_PEN:
882 input->name = "N-Trig Pen";
883 break;
884 case HID_DG_TOUCHSCREEN:
Rafi Rubin28865392010-03-10 16:10:28 +0100885 /* These keys are redundant for fingers, clear them
886 * to prevent incorrect identification */
Rafi Rubindbf2b172010-02-12 21:13:05 -0500887 __clear_bit(BTN_TOOL_PEN, input->keybit);
Rafi Rubin28865392010-03-10 16:10:28 +0100888 __clear_bit(BTN_TOOL_FINGER, input->keybit);
889 __clear_bit(BTN_0, input->keybit);
Rafi Rubindbf2b172010-02-12 21:13:05 -0500890 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
Rafi Rubin943ed462010-02-11 22:14:05 -0500891 /*
892 * The physical touchscreen (single touch)
893 * input has a value for physical, whereas
894 * the multitouch only has logical input
895 * fields.
896 */
897 input->name =
898 (hidinput->report->field[0]
899 ->physical) ?
900 "N-Trig Touchscreen" :
901 "N-Trig MultiTouch";
902 break;
903 }
904 }
905
Jiri Kosinac0858552010-04-07 12:10:29 +0200906 /* This is needed for devices with more recent firmware versions */
907 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
Stephane Chatty65499812010-04-06 22:22:58 +0200908 if (report)
909 usbhid_submit_report(hdev, report, USB_DIR_OUT);
910
Rafi Rubin02778732010-09-08 11:46:14 +0200911 ntrig_report_version(hdev);
912
Rafi Rubineab32f52010-05-04 14:20:17 -0400913 ret = sysfs_create_group(&hdev->dev.kobj,
914 &ntrig_attribute_group);
Stephane Chatty65499812010-04-06 22:22:58 +0200915
Rafi Rubin943ed462010-02-11 22:14:05 -0500916 return 0;
917err_free:
918 kfree(nd);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200919 return ret;
920}
921
922static void ntrig_remove(struct hid_device *hdev)
923{
Rafi Rubineab32f52010-05-04 14:20:17 -0400924 sysfs_remove_group(&hdev->dev.kobj,
Rafi Rubina52dc342010-08-26 00:54:55 -0400925 &ntrig_attribute_group);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200926 hid_hw_stop(hdev);
927 kfree(hid_get_drvdata(hdev));
928}
929
Rafi Rubin94011f92008-11-19 15:54:46 +0100930static const struct hid_device_id ntrig_devices[] = {
931 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
932 .driver_data = NTRIG_DUPLICATE_USAGES },
micki6e328192010-06-19 11:37:29 +0300933 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1),
934 .driver_data = NTRIG_DUPLICATE_USAGES },
935 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2),
936 .driver_data = NTRIG_DUPLICATE_USAGES },
937 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3),
938 .driver_data = NTRIG_DUPLICATE_USAGES },
939 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4),
940 .driver_data = NTRIG_DUPLICATE_USAGES },
941 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5),
942 .driver_data = NTRIG_DUPLICATE_USAGES },
943 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6),
944 .driver_data = NTRIG_DUPLICATE_USAGES },
945 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7),
946 .driver_data = NTRIG_DUPLICATE_USAGES },
947 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8),
948 .driver_data = NTRIG_DUPLICATE_USAGES },
949 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9),
950 .driver_data = NTRIG_DUPLICATE_USAGES },
951 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10),
952 .driver_data = NTRIG_DUPLICATE_USAGES },
953 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11),
954 .driver_data = NTRIG_DUPLICATE_USAGES },
955 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12),
956 .driver_data = NTRIG_DUPLICATE_USAGES },
957 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13),
958 .driver_data = NTRIG_DUPLICATE_USAGES },
959 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14),
960 .driver_data = NTRIG_DUPLICATE_USAGES },
961 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15),
962 .driver_data = NTRIG_DUPLICATE_USAGES },
963 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16),
964 .driver_data = NTRIG_DUPLICATE_USAGES },
965 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17),
966 .driver_data = NTRIG_DUPLICATE_USAGES },
967 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18),
968 .driver_data = NTRIG_DUPLICATE_USAGES },
Rafi Rubin94011f92008-11-19 15:54:46 +0100969 { }
970};
971MODULE_DEVICE_TABLE(hid, ntrig_devices);
972
Stephane Chatty57fd6372009-05-20 15:49:35 +0200973static const struct hid_usage_id ntrig_grabbed_usages[] = {
974 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
Rafi Rubin943ed462010-02-11 22:14:05 -0500975 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200976};
977
Rafi Rubin94011f92008-11-19 15:54:46 +0100978static struct hid_driver ntrig_driver = {
979 .name = "ntrig",
980 .id_table = ntrig_devices,
Stephane Chatty57fd6372009-05-20 15:49:35 +0200981 .probe = ntrig_probe,
982 .remove = ntrig_remove,
Rafi Rubin94011f92008-11-19 15:54:46 +0100983 .input_mapping = ntrig_input_mapping,
984 .input_mapped = ntrig_input_mapped,
Stephane Chatty57fd6372009-05-20 15:49:35 +0200985 .usage_table = ntrig_grabbed_usages,
986 .event = ntrig_event,
Rafi Rubin94011f92008-11-19 15:54:46 +0100987};
988
Peter Huewea24f4232009-07-02 19:08:38 +0200989static int __init ntrig_init(void)
Rafi Rubin94011f92008-11-19 15:54:46 +0100990{
991 return hid_register_driver(&ntrig_driver);
992}
993
Peter Huewea24f4232009-07-02 19:08:38 +0200994static void __exit ntrig_exit(void)
Rafi Rubin94011f92008-11-19 15:54:46 +0100995{
996 hid_unregister_driver(&ntrig_driver);
997}
998
999module_init(ntrig_init);
1000module_exit(ntrig_exit);
1001MODULE_LICENSE("GPL");