blob: 10b08d6ab37ce125cffae82990374f08b520c093 [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
27#define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
28 EV_KEY, (c))
29
Stephane Chatty57fd6372009-05-20 15:49:35 +020030struct ntrig_data {
Rafi Rubindbf2b172010-02-12 21:13:05 -050031 /* Incoming raw values for a single contact */
32 __u16 x, y, w, h;
33 __u16 id;
Rafi Rubin250d3772010-05-03 05:08:29 -040034
35 bool tipswitch;
36 bool confidence;
37 bool first_contact_touch;
Rafi Rubindbf2b172010-02-12 21:13:05 -050038
39 bool reading_mt;
Rafi Rubindbf2b172010-02-12 21:13:05 -050040
41 __u8 mt_footer[4];
42 __u8 mt_foot_count;
Stephane Chatty57fd6372009-05-20 15:49:35 +020043};
44
45/*
46 * this driver is aimed at two firmware versions in circulation:
47 * - dual pen/finger single touch
48 * - finger multitouch, pen not working
49 */
50
Rafi Rubin94011f92008-11-19 15:54:46 +010051static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
52 struct hid_field *field, struct hid_usage *usage,
53 unsigned long **bit, int *max)
54{
Rafi Rubindbf2b172010-02-12 21:13:05 -050055 /* No special mappings needed for the pen and single touch */
56 if (field->physical)
Rafi Rubin943ed462010-02-11 22:14:05 -050057 return 0;
58
Stephane Chatty57fd6372009-05-20 15:49:35 +020059 switch (usage->hid & HID_USAGE_PAGE) {
Stephane Chatty57fd6372009-05-20 15:49:35 +020060 case HID_UP_GENDESK:
61 switch (usage->hid) {
62 case HID_GD_X:
63 hid_map_usage(hi, usage, bit, max,
64 EV_ABS, ABS_MT_POSITION_X);
65 input_set_abs_params(hi->input, ABS_X,
66 field->logical_minimum,
67 field->logical_maximum, 0, 0);
68 return 1;
69 case HID_GD_Y:
70 hid_map_usage(hi, usage, bit, max,
71 EV_ABS, ABS_MT_POSITION_Y);
72 input_set_abs_params(hi->input, ABS_Y,
73 field->logical_minimum,
74 field->logical_maximum, 0, 0);
75 return 1;
76 }
77 return 0;
78
79 case HID_UP_DIGITIZER:
80 switch (usage->hid) {
81 /* we do not want to map these for now */
Rafi Rubindbf2b172010-02-12 21:13:05 -050082 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
Stephane Chatty57fd6372009-05-20 15:49:35 +020083 case HID_DG_INPUTMODE:
84 case HID_DG_DEVICEINDEX:
Stephane Chatty57fd6372009-05-20 15:49:35 +020085 case HID_DG_CONTACTMAX:
86 return -1;
87
Stephane Chatty57fd6372009-05-20 15:49:35 +020088 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
89 case HID_DG_WIDTH:
90 hid_map_usage(hi, usage, bit, max,
91 EV_ABS, ABS_MT_TOUCH_MAJOR);
92 return 1;
93 case HID_DG_HEIGHT:
94 hid_map_usage(hi, usage, bit, max,
95 EV_ABS, ABS_MT_TOUCH_MINOR);
96 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
97 0, 1, 0, 0);
98 return 1;
99 }
100 return 0;
101
102 case 0xff000000:
103 /* we do not want to map these: no input-oriented meaning */
104 return -1;
Rafi Rubin94011f92008-11-19 15:54:46 +0100105 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200106
Rafi Rubin94011f92008-11-19 15:54:46 +0100107 return 0;
108}
109
110static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
111 struct hid_field *field, struct hid_usage *usage,
112 unsigned long **bit, int *max)
113{
Rafi Rubindbf2b172010-02-12 21:13:05 -0500114 /* No special mappings needed for the pen and single touch */
115 if (field->physical)
Rafi Rubin943ed462010-02-11 22:14:05 -0500116 return 0;
Rafi Rubinb0549cf2010-02-11 22:14:06 -0500117
Rafi Rubin94011f92008-11-19 15:54:46 +0100118 if (usage->type == EV_KEY || usage->type == EV_REL
119 || usage->type == EV_ABS)
120 clear_bit(usage->code, *bit);
121
122 return 0;
123}
Stephane Chatty57fd6372009-05-20 15:49:35 +0200124
125/*
126 * this function is called upon all reports
127 * so that we can filter contact point information,
128 * decide whether we are in multi or single touch mode
129 * and call input_mt_sync after each point if necessary
130 */
131static int ntrig_event (struct hid_device *hid, struct hid_field *field,
132 struct hid_usage *usage, __s32 value)
133{
134 struct input_dev *input = field->hidinput->input;
135 struct ntrig_data *nd = hid_get_drvdata(hid);
136
Rafi Rubin943ed462010-02-11 22:14:05 -0500137 /* No special handling needed for the pen */
138 if (field->application == HID_DG_PEN)
139 return 0;
140
Stephane Chatty57fd6372009-05-20 15:49:35 +0200141 if (hid->claimed & HID_CLAIMED_INPUT) {
142 switch (usage->hid) {
Rafi Rubindbf2b172010-02-12 21:13:05 -0500143 case 0xff000001:
144 /* Tag indicating the start of a multitouch group */
145 nd->reading_mt = 1;
Rafi Rubin250d3772010-05-03 05:08:29 -0400146 nd->first_contact_touch = 0;
Rafi Rubindbf2b172010-02-12 21:13:05 -0500147 break;
Rafi Rubin28865392010-03-10 16:10:28 +0100148 case HID_DG_TIPSWITCH:
Rafi Rubin250d3772010-05-03 05:08:29 -0400149 nd->tipswitch = value;
Rafi Rubin28865392010-03-10 16:10:28 +0100150 /* Prevent emission of touch until validated */
151 return 1;
Rafi Rubindbf2b172010-02-12 21:13:05 -0500152 case HID_DG_CONFIDENCE:
153 nd->confidence = value;
154 break;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200155 case HID_GD_X:
156 nd->x = value;
Rafi Rubindbf2b172010-02-12 21:13:05 -0500157 /* Clear the contact footer */
158 nd->mt_foot_count = 0;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200159 break;
160 case HID_GD_Y:
161 nd->y = value;
162 break;
163 case HID_DG_CONTACTID:
164 nd->id = value;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200165 break;
166 case HID_DG_WIDTH:
167 nd->w = value;
168 break;
169 case HID_DG_HEIGHT:
170 nd->h = value;
171 /*
172 * when in single touch mode, this is the last
173 * report received in a finger event. We want
174 * to emit a normal (X, Y) position
175 */
Rafi Rubindbf2b172010-02-12 21:13:05 -0500176 if (!nd->reading_mt) {
Rafi Rubin250d3772010-05-03 05:08:29 -0400177 /*
178 * TipSwitch indicates the presence of a
179 * finger in single touch mode.
180 */
Rafi Rubin2170c5a2010-04-09 17:58:25 -0400181 input_report_key(input, BTN_TOUCH,
Rafi Rubin250d3772010-05-03 05:08:29 -0400182 nd->tipswitch);
183 input_report_key(input, BTN_TOOL_DOUBLETAP,
184 nd->tipswitch);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200185 input_event(input, EV_ABS, ABS_X, nd->x);
186 input_event(input, EV_ABS, ABS_Y, nd->y);
187 }
188 break;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200189 case 0xff000002:
190 /*
191 * we receive this when the device is in multitouch
192 * mode. The first of the three values tagged with
193 * this usage tells if the contact point is real
194 * or a placeholder
195 */
Rafi Rubindbf2b172010-02-12 21:13:05 -0500196
197 /* Shouldn't get more than 4 footer packets, so skip */
198 if (nd->mt_foot_count >= 4)
Stephane Chatty57fd6372009-05-20 15:49:35 +0200199 break;
Rafi Rubindbf2b172010-02-12 21:13:05 -0500200
201 nd->mt_footer[nd->mt_foot_count++] = value;
202
203 /* if the footer isn't complete break */
204 if (nd->mt_foot_count != 4)
205 break;
206
207 /* Pen activity signal, trigger end of touch. */
208 if (nd->mt_footer[2]) {
209 nd->confidence = 0;
210 break;
211 }
212
213 /* If the contact was invalid */
214 if (!(nd->confidence && nd->mt_footer[0])
215 || nd->w <= 250
216 || nd->h <= 190) {
217 nd->confidence = 0;
218 break;
219 }
220
Stephane Chatty57fd6372009-05-20 15:49:35 +0200221 /* emit a normal (X, Y) for the first point only */
222 if (nd->id == 0) {
Rafi Rubin250d3772010-05-03 05:08:29 -0400223 /*
224 * TipSwitch is superfluous in multitouch
225 * mode. The footer events tell us
226 * if there is a finger on the screen or
227 * not.
228 */
229 nd->first_contact_touch = nd->confidence;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200230 input_event(input, EV_ABS, ABS_X, nd->x);
231 input_event(input, EV_ABS, ABS_Y, nd->y);
232 }
233 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
234 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
235 if (nd->w > nd->h) {
236 input_event(input, EV_ABS,
237 ABS_MT_ORIENTATION, 1);
238 input_event(input, EV_ABS,
239 ABS_MT_TOUCH_MAJOR, nd->w);
240 input_event(input, EV_ABS,
241 ABS_MT_TOUCH_MINOR, nd->h);
242 } else {
243 input_event(input, EV_ABS,
244 ABS_MT_ORIENTATION, 0);
245 input_event(input, EV_ABS,
246 ABS_MT_TOUCH_MAJOR, nd->h);
247 input_event(input, EV_ABS,
248 ABS_MT_TOUCH_MINOR, nd->w);
249 }
250 input_mt_sync(field->hidinput->input);
Rafi Rubindbf2b172010-02-12 21:13:05 -0500251 break;
252
253 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
254 if (!nd->reading_mt)
255 break;
256
257 nd->reading_mt = 0;
258
Rafi Rubin250d3772010-05-03 05:08:29 -0400259 if (nd->first_contact_touch) {
Rafi Rubindbf2b172010-02-12 21:13:05 -0500260 switch (value) {
261 case 0: /* for single touch devices */
262 case 1:
263 input_report_key(input,
264 BTN_TOOL_DOUBLETAP, 1);
265 break;
266 case 2:
267 input_report_key(input,
268 BTN_TOOL_TRIPLETAP, 1);
269 break;
270 case 3:
271 default:
272 input_report_key(input,
273 BTN_TOOL_QUADTAP, 1);
274 }
275 input_report_key(input, BTN_TOUCH, 1);
276 } else {
277 input_report_key(input,
278 BTN_TOOL_DOUBLETAP, 0);
279 input_report_key(input,
280 BTN_TOOL_TRIPLETAP, 0);
281 input_report_key(input,
282 BTN_TOOL_QUADTAP, 0);
Rafi Rubin28865392010-03-10 16:10:28 +0100283 input_report_key(input, BTN_TOUCH, 0);
Rafi Rubindbf2b172010-02-12 21:13:05 -0500284 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200285 break;
286
287 default:
288 /* fallback to the generic hidinput handling */
289 return 0;
290 }
291 }
292
293 /* we have handled the hidinput part, now remains hiddev */
Rafi Rubin943ed462010-02-11 22:14:05 -0500294 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
295 hid->hiddev_hid_event(hid, field, usage, value);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200296
297 return 1;
298}
299
300static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
301{
302 int ret;
303 struct ntrig_data *nd;
Rafi Rubin943ed462010-02-11 22:14:05 -0500304 struct hid_input *hidinput;
305 struct input_dev *input;
Stephane Chatty65499812010-04-06 22:22:58 +0200306 struct hid_report *report;
Rafi Rubin943ed462010-02-11 22:14:05 -0500307
308 if (id->driver_data)
309 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200310
311 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
312 if (!nd) {
313 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
314 return -ENOMEM;
315 }
Rafi Rubindbf2b172010-02-12 21:13:05 -0500316
317 nd->reading_mt = 0;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200318 hid_set_drvdata(hdev, nd);
319
320 ret = hid_parse(hdev);
Rafi Rubin943ed462010-02-11 22:14:05 -0500321 if (ret) {
322 dev_err(&hdev->dev, "parse failed\n");
323 goto err_free;
324 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200325
Rafi Rubin943ed462010-02-11 22:14:05 -0500326 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
327 if (ret) {
328 dev_err(&hdev->dev, "hw start failed\n");
329 goto err_free;
330 }
Rafi Rubin837b4752009-06-23 14:09:26 -0400331
Rafi Rubin943ed462010-02-11 22:14:05 -0500332
333 list_for_each_entry(hidinput, &hdev->inputs, list) {
Rafi Rubin28865392010-03-10 16:10:28 +0100334 if (hidinput->report->maxfield < 1)
335 continue;
336
Rafi Rubin943ed462010-02-11 22:14:05 -0500337 input = hidinput->input;
338 switch (hidinput->report->field[0]->application) {
339 case HID_DG_PEN:
340 input->name = "N-Trig Pen";
341 break;
342 case HID_DG_TOUCHSCREEN:
Rafi Rubin28865392010-03-10 16:10:28 +0100343 /* These keys are redundant for fingers, clear them
344 * to prevent incorrect identification */
Rafi Rubindbf2b172010-02-12 21:13:05 -0500345 __clear_bit(BTN_TOOL_PEN, input->keybit);
Rafi Rubin28865392010-03-10 16:10:28 +0100346 __clear_bit(BTN_TOOL_FINGER, input->keybit);
347 __clear_bit(BTN_0, input->keybit);
Rafi Rubindbf2b172010-02-12 21:13:05 -0500348 /*
349 * A little something special to enable
350 * two and three finger taps.
351 */
352 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
353 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
354 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
Rafi Rubin943ed462010-02-11 22:14:05 -0500355 /*
356 * The physical touchscreen (single touch)
357 * input has a value for physical, whereas
358 * the multitouch only has logical input
359 * fields.
360 */
361 input->name =
362 (hidinput->report->field[0]
363 ->physical) ?
364 "N-Trig Touchscreen" :
365 "N-Trig MultiTouch";
366 break;
367 }
368 }
369
Jiri Kosinac0858552010-04-07 12:10:29 +0200370 /* This is needed for devices with more recent firmware versions */
371 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
Stephane Chatty65499812010-04-06 22:22:58 +0200372 if (report)
373 usbhid_submit_report(hdev, report, USB_DIR_OUT);
374
375
Rafi Rubin943ed462010-02-11 22:14:05 -0500376 return 0;
377err_free:
378 kfree(nd);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200379 return ret;
380}
381
382static void ntrig_remove(struct hid_device *hdev)
383{
384 hid_hw_stop(hdev);
385 kfree(hid_get_drvdata(hdev));
386}
387
Rafi Rubin94011f92008-11-19 15:54:46 +0100388static const struct hid_device_id ntrig_devices[] = {
389 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
390 .driver_data = NTRIG_DUPLICATE_USAGES },
391 { }
392};
393MODULE_DEVICE_TABLE(hid, ntrig_devices);
394
Stephane Chatty57fd6372009-05-20 15:49:35 +0200395static const struct hid_usage_id ntrig_grabbed_usages[] = {
396 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
Rafi Rubin943ed462010-02-11 22:14:05 -0500397 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200398};
399
Rafi Rubin94011f92008-11-19 15:54:46 +0100400static struct hid_driver ntrig_driver = {
401 .name = "ntrig",
402 .id_table = ntrig_devices,
Stephane Chatty57fd6372009-05-20 15:49:35 +0200403 .probe = ntrig_probe,
404 .remove = ntrig_remove,
Rafi Rubin94011f92008-11-19 15:54:46 +0100405 .input_mapping = ntrig_input_mapping,
406 .input_mapped = ntrig_input_mapped,
Stephane Chatty57fd6372009-05-20 15:49:35 +0200407 .usage_table = ntrig_grabbed_usages,
408 .event = ntrig_event,
Rafi Rubin94011f92008-11-19 15:54:46 +0100409};
410
Peter Huewea24f423b2009-07-02 19:08:38 +0200411static int __init ntrig_init(void)
Rafi Rubin94011f92008-11-19 15:54:46 +0100412{
413 return hid_register_driver(&ntrig_driver);
414}
415
Peter Huewea24f423b2009-07-02 19:08:38 +0200416static void __exit ntrig_exit(void)
Rafi Rubin94011f92008-11-19 15:54:46 +0100417{
418 hid_unregister_driver(&ntrig_driver);
419}
420
421module_init(ntrig_init);
422module_exit(ntrig_exit);
423MODULE_LICENSE("GPL");