2 * HID driver for N-Trig touchscreens
4 * Copyright (c) 2008-2010 Rafi Rubin
5 * Copyright (c) 2009-2010 Stephane Chatty
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)
16 #include <linux/device.h>
17 #include <linux/hid.h>
18 #include <linux/usb.h>
19 #include "usbhid/usbhid.h"
20 #include <linux/module.h>
21 #include <linux/slab.h>
25 #define NTRIG_DUPLICATE_USAGES 0x001
27 #define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
31 /* Incoming raw values for a single contact */
37 bool first_contact_touch;
46 * this driver is aimed at two firmware versions in circulation:
47 * - dual pen/finger single touch
48 * - finger multitouch, pen not working
51 static 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)
55 /* No special mappings needed for the pen and single touch */
59 switch (usage->hid & HID_USAGE_PAGE) {
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);
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);
79 case HID_UP_DIGITIZER:
81 /* we do not want to map these for now */
82 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
83 case HID_DG_INPUTMODE:
84 case HID_DG_DEVICEINDEX:
85 case HID_DG_CONTACTMAX:
88 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
90 hid_map_usage(hi, usage, bit, max,
91 EV_ABS, ABS_MT_TOUCH_MAJOR);
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,
103 /* we do not want to map these: no input-oriented meaning */
110 static 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)
114 /* No special mappings needed for the pen and single touch */
118 if (usage->type == EV_KEY || usage->type == EV_REL
119 || usage->type == EV_ABS)
120 clear_bit(usage->code, *bit);
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
131 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
132 struct hid_usage *usage, __s32 value)
134 struct input_dev *input = field->hidinput->input;
135 struct ntrig_data *nd = hid_get_drvdata(hid);
137 /* No special handling needed for the pen */
138 if (field->application == HID_DG_PEN)
141 if (hid->claimed & HID_CLAIMED_INPUT) {
142 switch (usage->hid) {
144 /* Tag indicating the start of a multitouch group */
146 nd->first_contact_touch = 0;
148 case HID_DG_TIPSWITCH:
149 nd->tipswitch = value;
150 /* Prevent emission of touch until validated */
152 case HID_DG_CONFIDENCE:
153 nd->confidence = value;
157 /* Clear the contact footer */
158 nd->mt_foot_count = 0;
163 case HID_DG_CONTACTID:
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
176 if (!nd->reading_mt) {
178 * TipSwitch indicates the presence of a
179 * finger in single touch mode.
181 input_report_key(input, BTN_TOUCH,
183 input_report_key(input, BTN_TOOL_DOUBLETAP,
185 input_event(input, EV_ABS, ABS_X, nd->x);
186 input_event(input, EV_ABS, ABS_Y, nd->y);
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
197 /* Shouldn't get more than 4 footer packets, so skip */
198 if (nd->mt_foot_count >= 4)
201 nd->mt_footer[nd->mt_foot_count++] = value;
203 /* if the footer isn't complete break */
204 if (nd->mt_foot_count != 4)
207 /* Pen activity signal, trigger end of touch. */
208 if (nd->mt_footer[2]) {
213 /* If the contact was invalid */
214 if (!(nd->confidence && nd->mt_footer[0])
221 /* emit a normal (X, Y) for the first point only */
224 * TipSwitch is superfluous in multitouch
225 * mode. The footer events tell us
226 * if there is a finger on the screen or
229 nd->first_contact_touch = nd->confidence;
230 input_event(input, EV_ABS, ABS_X, nd->x);
231 input_event(input, EV_ABS, ABS_Y, nd->y);
233 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
234 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
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);
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);
250 input_mt_sync(field->hidinput->input);
253 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
259 if (nd->first_contact_touch) {
261 case 0: /* for single touch devices */
263 input_report_key(input,
264 BTN_TOOL_DOUBLETAP, 1);
267 input_report_key(input,
268 BTN_TOOL_TRIPLETAP, 1);
272 input_report_key(input,
273 BTN_TOOL_QUADTAP, 1);
275 input_report_key(input, BTN_TOUCH, 1);
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);
283 input_report_key(input, BTN_TOUCH, 0);
288 /* fallback to the generic hidinput handling */
293 /* we have handled the hidinput part, now remains hiddev */
294 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
295 hid->hiddev_hid_event(hid, field, usage, value);
300 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
303 struct ntrig_data *nd;
304 struct hid_input *hidinput;
305 struct input_dev *input;
306 struct hid_report *report;
309 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
311 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
313 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
318 hid_set_drvdata(hdev, nd);
320 ret = hid_parse(hdev);
322 dev_err(&hdev->dev, "parse failed\n");
326 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
328 dev_err(&hdev->dev, "hw start failed\n");
333 list_for_each_entry(hidinput, &hdev->inputs, list) {
334 if (hidinput->report->maxfield < 1)
337 input = hidinput->input;
338 switch (hidinput->report->field[0]->application) {
340 input->name = "N-Trig Pen";
342 case HID_DG_TOUCHSCREEN:
343 /* These keys are redundant for fingers, clear them
344 * to prevent incorrect identification */
345 __clear_bit(BTN_TOOL_PEN, input->keybit);
346 __clear_bit(BTN_TOOL_FINGER, input->keybit);
347 __clear_bit(BTN_0, input->keybit);
349 * A little something special to enable
350 * two and three finger taps.
352 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
353 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
354 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
356 * The physical touchscreen (single touch)
357 * input has a value for physical, whereas
358 * the multitouch only has logical input
362 (hidinput->report->field[0]
364 "N-Trig Touchscreen" :
370 /* This is needed for devices with more recent firmware versions */
371 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
373 usbhid_submit_report(hdev, report, USB_DIR_OUT);
382 static void ntrig_remove(struct hid_device *hdev)
385 kfree(hid_get_drvdata(hdev));
388 static 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 },
393 MODULE_DEVICE_TABLE(hid, ntrig_devices);
395 static const struct hid_usage_id ntrig_grabbed_usages[] = {
396 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
397 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
400 static struct hid_driver ntrig_driver = {
402 .id_table = ntrig_devices,
403 .probe = ntrig_probe,
404 .remove = ntrig_remove,
405 .input_mapping = ntrig_input_mapping,
406 .input_mapped = ntrig_input_mapped,
407 .usage_table = ntrig_grabbed_usages,
408 .event = ntrig_event,
411 static int __init ntrig_init(void)
413 return hid_register_driver(&ntrig_driver);
416 static void __exit ntrig_exit(void)
418 hid_unregister_driver(&ntrig_driver);
421 module_init(ntrig_init);
422 module_exit(ntrig_exit);
423 MODULE_LICENSE("GPL");