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 __u8 first_contact_confidence;
44 * this driver is aimed at two firmware versions in circulation:
45 * - dual pen/finger single touch
46 * - finger multitouch, pen not working
49 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
50 struct hid_field *field, struct hid_usage *usage,
51 unsigned long **bit, int *max)
53 /* No special mappings needed for the pen and single touch */
57 switch (usage->hid & HID_USAGE_PAGE) {
61 hid_map_usage(hi, usage, bit, max,
62 EV_ABS, ABS_MT_POSITION_X);
63 input_set_abs_params(hi->input, ABS_X,
64 field->logical_minimum,
65 field->logical_maximum, 0, 0);
68 hid_map_usage(hi, usage, bit, max,
69 EV_ABS, ABS_MT_POSITION_Y);
70 input_set_abs_params(hi->input, ABS_Y,
71 field->logical_minimum,
72 field->logical_maximum, 0, 0);
77 case HID_UP_DIGITIZER:
79 /* we do not want to map these for now */
80 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
81 case HID_DG_INPUTMODE:
82 case HID_DG_DEVICEINDEX:
83 case HID_DG_CONTACTMAX:
86 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
88 hid_map_usage(hi, usage, bit, max,
89 EV_ABS, ABS_MT_TOUCH_MAJOR);
92 hid_map_usage(hi, usage, bit, max,
93 EV_ABS, ABS_MT_TOUCH_MINOR);
94 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
101 /* we do not want to map these: no input-oriented meaning */
108 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
109 struct hid_field *field, struct hid_usage *usage,
110 unsigned long **bit, int *max)
112 /* No special mappings needed for the pen and single touch */
116 if (usage->type == EV_KEY || usage->type == EV_REL
117 || usage->type == EV_ABS)
118 clear_bit(usage->code, *bit);
124 * this function is called upon all reports
125 * so that we can filter contact point information,
126 * decide whether we are in multi or single touch mode
127 * and call input_mt_sync after each point if necessary
129 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
130 struct hid_usage *usage, __s32 value)
132 struct input_dev *input = field->hidinput->input;
133 struct ntrig_data *nd = hid_get_drvdata(hid);
135 /* No special handling needed for the pen */
136 if (field->application == HID_DG_PEN)
139 if (hid->claimed & HID_CLAIMED_INPUT) {
140 switch (usage->hid) {
142 /* Tag indicating the start of a multitouch group */
144 nd->first_contact_confidence = 0;
146 case HID_DG_TIPSWITCH:
147 /* Prevent emission of touch until validated */
149 case HID_DG_CONFIDENCE:
150 nd->confidence = value;
154 /* Clear the contact footer */
155 nd->mt_foot_count = 0;
160 case HID_DG_CONTACTID:
169 * when in single touch mode, this is the last
170 * report received in a finger event. We want
171 * to emit a normal (X, Y) position
173 if (!nd->reading_mt) {
174 input_report_key(input, BTN_TOOL_DOUBLETAP,
175 (nd->confidence != 0));
176 input_report_key(input, BTN_TOUCH,
177 (nd->confidence != 0));
178 input_event(input, EV_ABS, ABS_X, nd->x);
179 input_event(input, EV_ABS, ABS_Y, nd->y);
184 * we receive this when the device is in multitouch
185 * mode. The first of the three values tagged with
186 * this usage tells if the contact point is real
190 /* Shouldn't get more than 4 footer packets, so skip */
191 if (nd->mt_foot_count >= 4)
194 nd->mt_footer[nd->mt_foot_count++] = value;
196 /* if the footer isn't complete break */
197 if (nd->mt_foot_count != 4)
200 /* Pen activity signal, trigger end of touch. */
201 if (nd->mt_footer[2]) {
206 /* If the contact was invalid */
207 if (!(nd->confidence && nd->mt_footer[0])
214 /* emit a normal (X, Y) for the first point only */
216 nd->first_contact_confidence = nd->confidence;
217 input_event(input, EV_ABS, ABS_X, nd->x);
218 input_event(input, EV_ABS, ABS_Y, nd->y);
220 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
221 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
223 input_event(input, EV_ABS,
224 ABS_MT_ORIENTATION, 1);
225 input_event(input, EV_ABS,
226 ABS_MT_TOUCH_MAJOR, nd->w);
227 input_event(input, EV_ABS,
228 ABS_MT_TOUCH_MINOR, nd->h);
230 input_event(input, EV_ABS,
231 ABS_MT_ORIENTATION, 0);
232 input_event(input, EV_ABS,
233 ABS_MT_TOUCH_MAJOR, nd->h);
234 input_event(input, EV_ABS,
235 ABS_MT_TOUCH_MINOR, nd->w);
237 input_mt_sync(field->hidinput->input);
240 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
246 if (nd->first_contact_confidence) {
248 case 0: /* for single touch devices */
250 input_report_key(input,
251 BTN_TOOL_DOUBLETAP, 1);
254 input_report_key(input,
255 BTN_TOOL_TRIPLETAP, 1);
259 input_report_key(input,
260 BTN_TOOL_QUADTAP, 1);
262 input_report_key(input, BTN_TOUCH, 1);
264 input_report_key(input,
265 BTN_TOOL_DOUBLETAP, 0);
266 input_report_key(input,
267 BTN_TOOL_TRIPLETAP, 0);
268 input_report_key(input,
269 BTN_TOOL_QUADTAP, 0);
270 input_report_key(input, BTN_TOUCH, 0);
275 /* fallback to the generic hidinput handling */
280 /* we have handled the hidinput part, now remains hiddev */
281 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
282 hid->hiddev_hid_event(hid, field, usage, value);
287 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
290 struct ntrig_data *nd;
291 struct hid_input *hidinput;
292 struct input_dev *input;
293 struct hid_report *report;
296 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
298 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
300 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
305 hid_set_drvdata(hdev, nd);
307 ret = hid_parse(hdev);
309 dev_err(&hdev->dev, "parse failed\n");
313 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
315 dev_err(&hdev->dev, "hw start failed\n");
320 list_for_each_entry(hidinput, &hdev->inputs, list) {
321 if (hidinput->report->maxfield < 1)
324 input = hidinput->input;
325 switch (hidinput->report->field[0]->application) {
327 input->name = "N-Trig Pen";
329 case HID_DG_TOUCHSCREEN:
330 /* These keys are redundant for fingers, clear them
331 * to prevent incorrect identification */
332 __clear_bit(BTN_TOOL_PEN, input->keybit);
333 __clear_bit(BTN_TOOL_FINGER, input->keybit);
334 __clear_bit(BTN_0, input->keybit);
336 * A little something special to enable
337 * two and three finger taps.
339 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
340 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
341 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
343 * The physical touchscreen (single touch)
344 * input has a value for physical, whereas
345 * the multitouch only has logical input
349 (hidinput->report->field[0]
351 "N-Trig Touchscreen" :
357 /* This is needed for devices with more recent firmware versions */
358 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
360 usbhid_submit_report(hdev, report, USB_DIR_OUT);
369 static void ntrig_remove(struct hid_device *hdev)
372 kfree(hid_get_drvdata(hdev));
375 static const struct hid_device_id ntrig_devices[] = {
376 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
377 .driver_data = NTRIG_DUPLICATE_USAGES },
380 MODULE_DEVICE_TABLE(hid, ntrig_devices);
382 static const struct hid_usage_id ntrig_grabbed_usages[] = {
383 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
384 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
387 static struct hid_driver ntrig_driver = {
389 .id_table = ntrig_devices,
390 .probe = ntrig_probe,
391 .remove = ntrig_remove,
392 .input_mapping = ntrig_input_mapping,
393 .input_mapped = ntrig_input_mapped,
394 .usage_table = ntrig_grabbed_usages,
395 .event = ntrig_event,
398 static int __init ntrig_init(void)
400 return hid_register_driver(&ntrig_driver);
403 static void __exit ntrig_exit(void)
405 hid_unregister_driver(&ntrig_driver);
408 module_init(ntrig_init);
409 module_exit(ntrig_exit);
410 MODULE_LICENSE("GPL");