2 * HID driver for N-Trig touchscreens
4 * Copyright (c) 2008 Rafi Rubin
5 * Copyright (c) 2009 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/module.h>
19 #include <linux/slab.h>
23 #define NTRIG_DUPLICATE_USAGES 0x001
25 #define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
29 /* Incoming raw values for a single contact */
35 __u8 first_contact_confidence;
42 * this driver is aimed at two firmware versions in circulation:
43 * - dual pen/finger single touch
44 * - finger multitouch, pen not working
47 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
48 struct hid_field *field, struct hid_usage *usage,
49 unsigned long **bit, int *max)
51 /* No special mappings needed for the pen and single touch */
55 switch (usage->hid & HID_USAGE_PAGE) {
59 hid_map_usage(hi, usage, bit, max,
60 EV_ABS, ABS_MT_POSITION_X);
61 input_set_abs_params(hi->input, ABS_X,
62 field->logical_minimum,
63 field->logical_maximum, 0, 0);
66 hid_map_usage(hi, usage, bit, max,
67 EV_ABS, ABS_MT_POSITION_Y);
68 input_set_abs_params(hi->input, ABS_Y,
69 field->logical_minimum,
70 field->logical_maximum, 0, 0);
75 case HID_UP_DIGITIZER:
77 /* we do not want to map these for now */
78 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
79 case HID_DG_INPUTMODE:
80 case HID_DG_DEVICEINDEX:
81 case HID_DG_CONTACTMAX:
84 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
86 hid_map_usage(hi, usage, bit, max,
87 EV_ABS, ABS_MT_TOUCH_MAJOR);
90 hid_map_usage(hi, usage, bit, max,
91 EV_ABS, ABS_MT_TOUCH_MINOR);
92 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
99 /* we do not want to map these: no input-oriented meaning */
106 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
107 struct hid_field *field, struct hid_usage *usage,
108 unsigned long **bit, int *max)
110 /* No special mappings needed for the pen and single touch */
114 if (usage->type == EV_KEY || usage->type == EV_REL
115 || usage->type == EV_ABS)
116 clear_bit(usage->code, *bit);
122 * this function is called upon all reports
123 * so that we can filter contact point information,
124 * decide whether we are in multi or single touch mode
125 * and call input_mt_sync after each point if necessary
127 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
128 struct hid_usage *usage, __s32 value)
130 struct input_dev *input = field->hidinput->input;
131 struct ntrig_data *nd = hid_get_drvdata(hid);
133 /* No special handling needed for the pen */
134 if (field->application == HID_DG_PEN)
137 if (hid->claimed & HID_CLAIMED_INPUT) {
138 switch (usage->hid) {
140 /* Tag indicating the start of a multitouch group */
142 nd->first_contact_confidence = 0;
144 case HID_DG_TIPSWITCH:
145 /* Prevent emission of touch until validated */
147 case HID_DG_CONFIDENCE:
148 nd->confidence = value;
152 /* Clear the contact footer */
153 nd->mt_foot_count = 0;
158 case HID_DG_CONTACTID:
167 * when in single touch mode, this is the last
168 * report received in a finger event. We want
169 * to emit a normal (X, Y) position
171 if (!nd->reading_mt) {
172 input_report_key(input, BTN_TOOL_DOUBLETAP,
173 (nd->confidence != 0));
174 input_event(input, EV_ABS, ABS_X, nd->x);
175 input_event(input, EV_ABS, ABS_Y, nd->y);
180 * we receive this when the device is in multitouch
181 * mode. The first of the three values tagged with
182 * this usage tells if the contact point is real
186 /* Shouldn't get more than 4 footer packets, so skip */
187 if (nd->mt_foot_count >= 4)
190 nd->mt_footer[nd->mt_foot_count++] = value;
192 /* if the footer isn't complete break */
193 if (nd->mt_foot_count != 4)
196 /* Pen activity signal, trigger end of touch. */
197 if (nd->mt_footer[2]) {
202 /* If the contact was invalid */
203 if (!(nd->confidence && nd->mt_footer[0])
210 /* emit a normal (X, Y) for the first point only */
212 nd->first_contact_confidence = nd->confidence;
213 input_event(input, EV_ABS, ABS_X, nd->x);
214 input_event(input, EV_ABS, ABS_Y, nd->y);
216 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
217 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
219 input_event(input, EV_ABS,
220 ABS_MT_ORIENTATION, 1);
221 input_event(input, EV_ABS,
222 ABS_MT_TOUCH_MAJOR, nd->w);
223 input_event(input, EV_ABS,
224 ABS_MT_TOUCH_MINOR, nd->h);
226 input_event(input, EV_ABS,
227 ABS_MT_ORIENTATION, 0);
228 input_event(input, EV_ABS,
229 ABS_MT_TOUCH_MAJOR, nd->h);
230 input_event(input, EV_ABS,
231 ABS_MT_TOUCH_MINOR, nd->w);
233 input_mt_sync(field->hidinput->input);
236 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
242 if (nd->first_contact_confidence) {
244 case 0: /* for single touch devices */
246 input_report_key(input,
247 BTN_TOOL_DOUBLETAP, 1);
250 input_report_key(input,
251 BTN_TOOL_TRIPLETAP, 1);
255 input_report_key(input,
256 BTN_TOOL_QUADTAP, 1);
258 input_report_key(input, BTN_TOUCH, 1);
260 input_report_key(input,
261 BTN_TOOL_DOUBLETAP, 0);
262 input_report_key(input,
263 BTN_TOOL_TRIPLETAP, 0);
264 input_report_key(input,
265 BTN_TOOL_QUADTAP, 0);
266 input_report_key(input, BTN_TOUCH, 0);
271 /* fallback to the generic hidinput handling */
276 /* we have handled the hidinput part, now remains hiddev */
277 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
278 hid->hiddev_hid_event(hid, field, usage, value);
283 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
286 struct ntrig_data *nd;
287 struct hid_input *hidinput;
288 struct input_dev *input;
291 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
293 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
295 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
300 hid_set_drvdata(hdev, nd);
302 ret = hid_parse(hdev);
304 dev_err(&hdev->dev, "parse failed\n");
308 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
310 dev_err(&hdev->dev, "hw start failed\n");
315 list_for_each_entry(hidinput, &hdev->inputs, list) {
316 if (hidinput->report->maxfield < 1)
319 input = hidinput->input;
320 switch (hidinput->report->field[0]->application) {
322 input->name = "N-Trig Pen";
324 case HID_DG_TOUCHSCREEN:
325 /* These keys are redundant for fingers, clear them
326 * to prevent incorrect identification */
327 __clear_bit(BTN_TOOL_PEN, input->keybit);
328 __clear_bit(BTN_TOOL_FINGER, input->keybit);
329 __clear_bit(BTN_0, input->keybit);
331 * A little something special to enable
332 * two and three finger taps.
334 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
335 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
336 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
338 * The physical touchscreen (single touch)
339 * input has a value for physical, whereas
340 * the multitouch only has logical input
344 (hidinput->report->field[0]
346 "N-Trig Touchscreen" :
358 static void ntrig_remove(struct hid_device *hdev)
361 kfree(hid_get_drvdata(hdev));
364 static const struct hid_device_id ntrig_devices[] = {
365 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
366 .driver_data = NTRIG_DUPLICATE_USAGES },
369 MODULE_DEVICE_TABLE(hid, ntrig_devices);
371 static const struct hid_usage_id ntrig_grabbed_usages[] = {
372 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
373 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
376 static struct hid_driver ntrig_driver = {
378 .id_table = ntrig_devices,
379 .probe = ntrig_probe,
380 .remove = ntrig_remove,
381 .input_mapping = ntrig_input_mapping,
382 .input_mapped = ntrig_input_mapped,
383 .usage_table = ntrig_grabbed_usages,
384 .event = ntrig_event,
387 static int __init ntrig_init(void)
389 return hid_register_driver(&ntrig_driver);
392 static void __exit ntrig_exit(void)
394 hid_unregister_driver(&ntrig_driver);
397 module_init(ntrig_init);
398 module_exit(ntrig_exit);
399 MODULE_LICENSE("GPL");