HID: ntrig: add sensitivity and responsiveness support
[deliverable/linux.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
RR
27static unsigned int min_width;
28static unsigned int min_height;
29static unsigned int activate_slack = 1;
30static unsigned int deactivate_slack = 4;
31static unsigned int activation_width = 64;
32static unsigned int activation_height = 32;
33
57fd637a 34struct ntrig_data {
dbf2b17d
RR
35 /* Incoming raw values for a single contact */
36 __u16 x, y, w, h;
37 __u16 id;
250d3775
RR
38
39 bool tipswitch;
40 bool confidence;
41 bool first_contact_touch;
dbf2b17d
RR
42
43 bool reading_mt;
dbf2b17d
RR
44
45 __u8 mt_footer[4];
46 __u8 mt_foot_count;
369db2a6
RR
47
48 /* The current activation state. */
49 __s8 act_state;
50
51 /* Empty frames to ignore before recognizing the end of activity */
52 __s8 deactivate_slack;
53
54 /* Frames to ignore before acknowledging the start of activity */
55 __s8 activate_slack;
56
57 /* Minimum size contact to accept */
58 __u16 min_width;
59 __u16 min_height;
60
61 /* Threshold to override activation slack */
62 __u16 activation_width;
63 __u16 activation_height;
64
65 __u16 sensor_logical_width;
66 __u16 sensor_logical_height;
67 __u16 sensor_physical_width;
68 __u16 sensor_physical_height;
57fd637a
SC
69};
70
71/*
72 * this driver is aimed at two firmware versions in circulation:
73 * - dual pen/finger single touch
74 * - finger multitouch, pen not working
75 */
76
94011f93
RR
77static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
78 struct hid_field *field, struct hid_usage *usage,
79 unsigned long **bit, int *max)
80{
369db2a6
RR
81 struct ntrig_data *nd = hid_get_drvdata(hdev);
82
dbf2b17d
RR
83 /* No special mappings needed for the pen and single touch */
84 if (field->physical)
943ed464
RR
85 return 0;
86
57fd637a 87 switch (usage->hid & HID_USAGE_PAGE) {
57fd637a
SC
88 case HID_UP_GENDESK:
89 switch (usage->hid) {
90 case HID_GD_X:
91 hid_map_usage(hi, usage, bit, max,
92 EV_ABS, ABS_MT_POSITION_X);
93 input_set_abs_params(hi->input, ABS_X,
94 field->logical_minimum,
95 field->logical_maximum, 0, 0);
369db2a6
RR
96
97 if (!nd->sensor_logical_width) {
98 nd->sensor_logical_width =
99 field->logical_maximum -
100 field->logical_minimum;
101 nd->sensor_physical_width =
102 field->physical_maximum -
103 field->physical_minimum;
104 nd->activation_width = activation_width *
105 nd->sensor_logical_width /
106 nd->sensor_physical_width;
107 nd->min_width = min_width *
108 nd->sensor_logical_width /
109 nd->sensor_physical_width;
110 }
57fd637a
SC
111 return 1;
112 case HID_GD_Y:
113 hid_map_usage(hi, usage, bit, max,
114 EV_ABS, ABS_MT_POSITION_Y);
115 input_set_abs_params(hi->input, ABS_Y,
116 field->logical_minimum,
117 field->logical_maximum, 0, 0);
369db2a6
RR
118
119 if (!nd->sensor_logical_height) {
120 nd->sensor_logical_height =
121 field->logical_maximum -
122 field->logical_minimum;
123 nd->sensor_physical_height =
124 field->physical_maximum -
125 field->physical_minimum;
126 nd->activation_height = activation_height *
127 nd->sensor_logical_height /
128 nd->sensor_physical_height;
129 nd->min_height = min_height *
130 nd->sensor_logical_height /
131 nd->sensor_physical_height;
132 }
57fd637a
SC
133 return 1;
134 }
135 return 0;
136
137 case HID_UP_DIGITIZER:
138 switch (usage->hid) {
139 /* we do not want to map these for now */
dbf2b17d 140 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
57fd637a
SC
141 case HID_DG_INPUTMODE:
142 case HID_DG_DEVICEINDEX:
57fd637a
SC
143 case HID_DG_CONTACTMAX:
144 return -1;
145
57fd637a
SC
146 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
147 case HID_DG_WIDTH:
148 hid_map_usage(hi, usage, bit, max,
149 EV_ABS, ABS_MT_TOUCH_MAJOR);
150 return 1;
151 case HID_DG_HEIGHT:
152 hid_map_usage(hi, usage, bit, max,
153 EV_ABS, ABS_MT_TOUCH_MINOR);
154 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
155 0, 1, 0, 0);
156 return 1;
157 }
158 return 0;
159
160 case 0xff000000:
161 /* we do not want to map these: no input-oriented meaning */
162 return -1;
94011f93 163 }
57fd637a 164
94011f93
RR
165 return 0;
166}
167
168static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
169 struct hid_field *field, struct hid_usage *usage,
170 unsigned long **bit, int *max)
171{
dbf2b17d
RR
172 /* No special mappings needed for the pen and single touch */
173 if (field->physical)
943ed464 174 return 0;
b0549cf1 175
94011f93
RR
176 if (usage->type == EV_KEY || usage->type == EV_REL
177 || usage->type == EV_ABS)
178 clear_bit(usage->code, *bit);
179
180 return 0;
181}
57fd637a
SC
182
183/*
184 * this function is called upon all reports
185 * so that we can filter contact point information,
186 * decide whether we are in multi or single touch mode
187 * and call input_mt_sync after each point if necessary
188 */
189static int ntrig_event (struct hid_device *hid, struct hid_field *field,
190 struct hid_usage *usage, __s32 value)
191{
192 struct input_dev *input = field->hidinput->input;
193 struct ntrig_data *nd = hid_get_drvdata(hid);
194
943ed464
RR
195 /* No special handling needed for the pen */
196 if (field->application == HID_DG_PEN)
197 return 0;
198
57fd637a
SC
199 if (hid->claimed & HID_CLAIMED_INPUT) {
200 switch (usage->hid) {
dbf2b17d
RR
201 case 0xff000001:
202 /* Tag indicating the start of a multitouch group */
203 nd->reading_mt = 1;
250d3775 204 nd->first_contact_touch = 0;
dbf2b17d 205 break;
2886539d 206 case HID_DG_TIPSWITCH:
250d3775 207 nd->tipswitch = value;
2886539d
RR
208 /* Prevent emission of touch until validated */
209 return 1;
dbf2b17d
RR
210 case HID_DG_CONFIDENCE:
211 nd->confidence = value;
212 break;
57fd637a
SC
213 case HID_GD_X:
214 nd->x = value;
dbf2b17d
RR
215 /* Clear the contact footer */
216 nd->mt_foot_count = 0;
57fd637a
SC
217 break;
218 case HID_GD_Y:
219 nd->y = value;
220 break;
221 case HID_DG_CONTACTID:
222 nd->id = value;
57fd637a
SC
223 break;
224 case HID_DG_WIDTH:
225 nd->w = value;
226 break;
227 case HID_DG_HEIGHT:
228 nd->h = value;
229 /*
230 * when in single touch mode, this is the last
231 * report received in a finger event. We want
232 * to emit a normal (X, Y) position
233 */
dbf2b17d 234 if (!nd->reading_mt) {
250d3775
RR
235 /*
236 * TipSwitch indicates the presence of a
237 * finger in single touch mode.
238 */
2170c5a8 239 input_report_key(input, BTN_TOUCH,
250d3775
RR
240 nd->tipswitch);
241 input_report_key(input, BTN_TOOL_DOUBLETAP,
242 nd->tipswitch);
57fd637a
SC
243 input_event(input, EV_ABS, ABS_X, nd->x);
244 input_event(input, EV_ABS, ABS_Y, nd->y);
57fd637a
SC
245 }
246 break;
247 case 0xff000002:
248 /*
249 * we receive this when the device is in multitouch
250 * mode. The first of the three values tagged with
251 * this usage tells if the contact point is real
252 * or a placeholder
253 */
dbf2b17d
RR
254
255 /* Shouldn't get more than 4 footer packets, so skip */
256 if (nd->mt_foot_count >= 4)
257 break;
258
259 nd->mt_footer[nd->mt_foot_count++] = value;
260
261 /* if the footer isn't complete break */
262 if (nd->mt_foot_count != 4)
263 break;
264
369db2a6 265 /* Pen activity signal. */
dbf2b17d 266 if (nd->mt_footer[2]) {
369db2a6
RR
267 /*
268 * When the pen deactivates touch, we see a
269 * bogus frame with ContactCount > 0.
270 * We can
271 * save a bit of work by ensuring act_state < 0
272 * even if deactivation slack is turned off.
273 */
274 nd->act_state = deactivate_slack - 1;
dbf2b17d 275 nd->confidence = 0;
57fd637a 276 break;
dbf2b17d
RR
277 }
278
369db2a6
RR
279 /*
280 * The first footer value indicates the presence of a
281 * finger.
282 */
283 if (nd->mt_footer[0]) {
284 /*
285 * We do not want to process contacts under
286 * the size threshold, but do not want to
287 * ignore them for activation state
288 */
289 if (nd->w < nd->min_width ||
290 nd->h < nd->min_height)
291 nd->confidence = 0;
292 } else
dbf2b17d 293 break;
369db2a6
RR
294
295 if (nd->act_state > 0) {
296 /*
297 * Contact meets the activation size threshold
298 */
299 if (nd->w >= nd->activation_width &&
300 nd->h >= nd->activation_height) {
301 if (nd->id)
302 /*
303 * first contact, activate now
304 */
305 nd->act_state = 0;
306 else {
307 /*
308 * avoid corrupting this frame
309 * but ensure next frame will
310 * be active
311 */
312 nd->act_state = 1;
313 break;
314 }
315 } else
316 /*
317 * Defer adjusting the activation state
318 * until the end of the frame.
319 */
320 break;
dbf2b17d
RR
321 }
322
369db2a6
RR
323 /* Discarding this contact */
324 if (!nd->confidence)
325 break;
326
57fd637a
SC
327 /* emit a normal (X, Y) for the first point only */
328 if (nd->id == 0) {
250d3775
RR
329 /*
330 * TipSwitch is superfluous in multitouch
331 * mode. The footer events tell us
332 * if there is a finger on the screen or
333 * not.
334 */
335 nd->first_contact_touch = nd->confidence;
57fd637a
SC
336 input_event(input, EV_ABS, ABS_X, nd->x);
337 input_event(input, EV_ABS, ABS_Y, nd->y);
338 }
369db2a6
RR
339
340 /* Emit MT events */
57fd637a
SC
341 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
342 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
369db2a6
RR
343
344 /*
345 * Translate from height and width to size
346 * and orientation.
347 */
57fd637a
SC
348 if (nd->w > nd->h) {
349 input_event(input, EV_ABS,
350 ABS_MT_ORIENTATION, 1);
351 input_event(input, EV_ABS,
352 ABS_MT_TOUCH_MAJOR, nd->w);
353 input_event(input, EV_ABS,
354 ABS_MT_TOUCH_MINOR, nd->h);
355 } else {
356 input_event(input, EV_ABS,
357 ABS_MT_ORIENTATION, 0);
358 input_event(input, EV_ABS,
359 ABS_MT_TOUCH_MAJOR, nd->h);
360 input_event(input, EV_ABS,
361 ABS_MT_TOUCH_MINOR, nd->w);
362 }
363 input_mt_sync(field->hidinput->input);
dbf2b17d
RR
364 break;
365
366 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
369db2a6 367 if (!nd->reading_mt) /* Just to be sure */
dbf2b17d
RR
368 break;
369
370 nd->reading_mt = 0;
371
369db2a6
RR
372
373 /*
374 * Activation state machine logic:
375 *
376 * Fundamental states:
377 * state > 0: Inactive
378 * state <= 0: Active
379 * state < -deactivate_slack:
380 * Pen termination of touch
381 *
382 * Specific values of interest
383 * state == activate_slack
384 * no valid input since the last reset
385 *
386 * state == 0
387 * general operational state
388 *
389 * state == -deactivate_slack
390 * read sufficient empty frames to accept
391 * the end of input and reset
392 */
393
394 if (nd->act_state > 0) { /* Currently inactive */
395 if (value)
396 /*
397 * Consider each live contact as
398 * evidence of intentional activity.
399 */
400 nd->act_state = (nd->act_state > value)
401 ? nd->act_state - value
402 : 0;
403 else
404 /*
405 * Empty frame before we hit the
406 * activity threshold, reset.
407 */
408 nd->act_state = nd->activate_slack;
409
410 /*
411 * Entered this block inactive and no
412 * coordinates sent this frame, so hold off
413 * on button state.
414 */
415 break;
416 } else { /* Currently active */
417 if (value && nd->act_state >=
418 nd->deactivate_slack)
419 /*
420 * Live point: clear accumulated
421 * deactivation count.
422 */
423 nd->act_state = 0;
424 else if (nd->act_state <= nd->deactivate_slack)
425 /*
426 * We've consumed the deactivation
427 * slack, time to deactivate and reset.
428 */
429 nd->act_state =
430 nd->activate_slack;
431 else { /* Move towards deactivation */
432 nd->act_state--;
433 break;
434 }
435 }
436
437 if (nd->first_contact_touch && nd->act_state <= 0) {
438 /*
439 * Check to see if we're ready to start
440 * emitting touch events.
441 *
442 * Note: activation slack will decrease over
443 * the course of the frame, and it will be
444 * inconsistent from the start to the end of
445 * the frame. However if the frame starts
446 * with slack, first_contact_touch will still
447 * be 0 and we will not get to this point.
448 */
ed7e2ca2 449 input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
dbf2b17d
RR
450 input_report_key(input, BTN_TOUCH, 1);
451 } else {
ed7e2ca2 452 input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
2886539d 453 input_report_key(input, BTN_TOUCH, 0);
dbf2b17d 454 }
57fd637a
SC
455 break;
456
457 default:
369db2a6 458 /* fall-back to the generic hidinput handling */
57fd637a
SC
459 return 0;
460 }
461 }
462
463 /* we have handled the hidinput part, now remains hiddev */
943ed464
RR
464 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
465 hid->hiddev_hid_event(hid, field, usage, value);
57fd637a
SC
466
467 return 1;
468}
469
470static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
471{
472 int ret;
473 struct ntrig_data *nd;
943ed464
RR
474 struct hid_input *hidinput;
475 struct input_dev *input;
6549981b 476 struct hid_report *report;
943ed464
RR
477
478 if (id->driver_data)
479 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
57fd637a
SC
480
481 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
482 if (!nd) {
483 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
484 return -ENOMEM;
485 }
dbf2b17d
RR
486
487 nd->reading_mt = 0;
369db2a6
RR
488 nd->min_width = 0;
489 nd->min_height = 0;
490 nd->activate_slack = activate_slack;
491 nd->act_state = activate_slack;
492 nd->deactivate_slack = -deactivate_slack;
493 nd->sensor_logical_width = 0;
494 nd->sensor_logical_height = 0;
495 nd->sensor_physical_width = 0;
496 nd->sensor_physical_height = 0;
497
57fd637a
SC
498 hid_set_drvdata(hdev, nd);
499
500 ret = hid_parse(hdev);
943ed464
RR
501 if (ret) {
502 dev_err(&hdev->dev, "parse failed\n");
503 goto err_free;
504 }
505
506 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
507 if (ret) {
508 dev_err(&hdev->dev, "hw start failed\n");
509 goto err_free;
510 }
57fd637a 511
837b4753 512
943ed464 513 list_for_each_entry(hidinput, &hdev->inputs, list) {
2886539d
RR
514 if (hidinput->report->maxfield < 1)
515 continue;
516
943ed464
RR
517 input = hidinput->input;
518 switch (hidinput->report->field[0]->application) {
519 case HID_DG_PEN:
520 input->name = "N-Trig Pen";
521 break;
522 case HID_DG_TOUCHSCREEN:
2886539d
RR
523 /* These keys are redundant for fingers, clear them
524 * to prevent incorrect identification */
dbf2b17d 525 __clear_bit(BTN_TOOL_PEN, input->keybit);
2886539d
RR
526 __clear_bit(BTN_TOOL_FINGER, input->keybit);
527 __clear_bit(BTN_0, input->keybit);
dbf2b17d 528 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
943ed464
RR
529 /*
530 * The physical touchscreen (single touch)
531 * input has a value for physical, whereas
532 * the multitouch only has logical input
533 * fields.
534 */
535 input->name =
536 (hidinput->report->field[0]
537 ->physical) ?
538 "N-Trig Touchscreen" :
539 "N-Trig MultiTouch";
540 break;
541 }
542 }
543
c0858552
JK
544 /* This is needed for devices with more recent firmware versions */
545 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
6549981b
SC
546 if (report)
547 usbhid_submit_report(hdev, report, USB_DIR_OUT);
548
549
943ed464
RR
550 return 0;
551err_free:
552 kfree(nd);
57fd637a
SC
553 return ret;
554}
555
556static void ntrig_remove(struct hid_device *hdev)
557{
558 hid_hw_stop(hdev);
559 kfree(hid_get_drvdata(hdev));
560}
561
94011f93
RR
562static const struct hid_device_id ntrig_devices[] = {
563 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
564 .driver_data = NTRIG_DUPLICATE_USAGES },
565 { }
566};
567MODULE_DEVICE_TABLE(hid, ntrig_devices);
568
57fd637a
SC
569static const struct hid_usage_id ntrig_grabbed_usages[] = {
570 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
943ed464 571 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
57fd637a
SC
572};
573
94011f93
RR
574static struct hid_driver ntrig_driver = {
575 .name = "ntrig",
576 .id_table = ntrig_devices,
57fd637a
SC
577 .probe = ntrig_probe,
578 .remove = ntrig_remove,
94011f93
RR
579 .input_mapping = ntrig_input_mapping,
580 .input_mapped = ntrig_input_mapped,
57fd637a
SC
581 .usage_table = ntrig_grabbed_usages,
582 .event = ntrig_event,
94011f93
RR
583};
584
a24f423b 585static int __init ntrig_init(void)
94011f93
RR
586{
587 return hid_register_driver(&ntrig_driver);
588}
589
a24f423b 590static void __exit ntrig_exit(void)
94011f93
RR
591{
592 hid_unregister_driver(&ntrig_driver);
593}
594
595module_init(ntrig_init);
596module_exit(ntrig_exit);
597MODULE_LICENSE("GPL");
This page took 0.145695 seconds and 5 git commands to generate.