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>
22 #define NTRIG_DUPLICATE_USAGES 0x001
24 #define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
28 /* Incoming raw values for a single contact */
34 __u8 first_contact_confidence;
41 * this driver is aimed at two firmware versions in circulation:
42 * - dual pen/finger single touch
43 * - finger multitouch, pen not working
46 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
47 struct hid_field *field, struct hid_usage *usage,
48 unsigned long **bit, int *max)
50 /* No special mappings needed for the pen and single touch */
54 switch (usage->hid & HID_USAGE_PAGE) {
58 hid_map_usage(hi, usage, bit, max,
59 EV_ABS, ABS_MT_POSITION_X);
60 input_set_abs_params(hi->input, ABS_X,
61 field->logical_minimum,
62 field->logical_maximum, 0, 0);
65 hid_map_usage(hi, usage, bit, max,
66 EV_ABS, ABS_MT_POSITION_Y);
67 input_set_abs_params(hi->input, ABS_Y,
68 field->logical_minimum,
69 field->logical_maximum, 0, 0);
74 case HID_UP_DIGITIZER:
76 /* we do not want to map these for now */
77 case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
78 case HID_DG_INPUTMODE:
79 case HID_DG_DEVICEINDEX:
80 case HID_DG_CONTACTMAX:
83 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
85 hid_map_usage(hi, usage, bit, max,
86 EV_ABS, ABS_MT_TOUCH_MAJOR);
89 hid_map_usage(hi, usage, bit, max,
90 EV_ABS, ABS_MT_TOUCH_MINOR);
91 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
98 /* we do not want to map these: no input-oriented meaning */
105 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
106 struct hid_field *field, struct hid_usage *usage,
107 unsigned long **bit, int *max)
109 /* No special mappings needed for the pen and single touch */
113 if (usage->type == EV_KEY || usage->type == EV_REL
114 || usage->type == EV_ABS)
115 clear_bit(usage->code, *bit);
121 * this function is called upon all reports
122 * so that we can filter contact point information,
123 * decide whether we are in multi or single touch mode
124 * and call input_mt_sync after each point if necessary
126 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
127 struct hid_usage *usage, __s32 value)
129 struct input_dev *input = field->hidinput->input;
130 struct ntrig_data *nd = hid_get_drvdata(hid);
132 /* No special handling needed for the pen */
133 if (field->application == HID_DG_PEN)
136 if (hid->claimed & HID_CLAIMED_INPUT) {
137 switch (usage->hid) {
139 /* Tag indicating the start of a multitouch group */
141 nd->first_contact_confidence = 0;
143 case HID_DG_CONFIDENCE:
144 nd->confidence = value;
148 /* Clear the contact footer */
149 nd->mt_foot_count = 0;
154 case HID_DG_CONTACTID:
163 * when in single touch mode, this is the last
164 * report received in a finger event. We want
165 * to emit a normal (X, Y) position
167 if (!nd->reading_mt) {
168 input_report_key(input, BTN_TOOL_DOUBLETAP,
169 (nd->confidence != 0));
170 input_event(input, EV_ABS, ABS_X, nd->x);
171 input_event(input, EV_ABS, ABS_Y, nd->y);
176 * we receive this when the device is in multitouch
177 * mode. The first of the three values tagged with
178 * this usage tells if the contact point is real
182 /* Shouldn't get more than 4 footer packets, so skip */
183 if (nd->mt_foot_count >= 4)
186 nd->mt_footer[nd->mt_foot_count++] = value;
188 /* if the footer isn't complete break */
189 if (nd->mt_foot_count != 4)
192 /* Pen activity signal, trigger end of touch. */
193 if (nd->mt_footer[2]) {
198 /* If the contact was invalid */
199 if (!(nd->confidence && nd->mt_footer[0])
206 /* emit a normal (X, Y) for the first point only */
208 nd->first_contact_confidence = nd->confidence;
209 input_event(input, EV_ABS, ABS_X, nd->x);
210 input_event(input, EV_ABS, ABS_Y, nd->y);
212 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
213 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
215 input_event(input, EV_ABS,
216 ABS_MT_ORIENTATION, 1);
217 input_event(input, EV_ABS,
218 ABS_MT_TOUCH_MAJOR, nd->w);
219 input_event(input, EV_ABS,
220 ABS_MT_TOUCH_MINOR, nd->h);
222 input_event(input, EV_ABS,
223 ABS_MT_ORIENTATION, 0);
224 input_event(input, EV_ABS,
225 ABS_MT_TOUCH_MAJOR, nd->h);
226 input_event(input, EV_ABS,
227 ABS_MT_TOUCH_MINOR, nd->w);
229 input_mt_sync(field->hidinput->input);
232 case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
238 if (nd->first_contact_confidence) {
240 case 0: /* for single touch devices */
242 input_report_key(input,
243 BTN_TOOL_DOUBLETAP, 1);
246 input_report_key(input,
247 BTN_TOOL_TRIPLETAP, 1);
251 input_report_key(input,
252 BTN_TOOL_QUADTAP, 1);
254 input_report_key(input, BTN_TOUCH, 1);
256 input_report_key(input,
257 BTN_TOOL_DOUBLETAP, 0);
258 input_report_key(input,
259 BTN_TOOL_TRIPLETAP, 0);
260 input_report_key(input,
261 BTN_TOOL_QUADTAP, 0);
266 /* fallback to the generic hidinput handling */
271 /* we have handled the hidinput part, now remains hiddev */
272 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
273 hid->hiddev_hid_event(hid, field, usage, value);
278 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
281 struct ntrig_data *nd;
282 struct hid_input *hidinput;
283 struct input_dev *input;
286 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
288 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
290 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
295 hid_set_drvdata(hdev, nd);
297 ret = hid_parse(hdev);
299 dev_err(&hdev->dev, "parse failed\n");
303 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
305 dev_err(&hdev->dev, "hw start failed\n");
310 list_for_each_entry(hidinput, &hdev->inputs, list) {
311 input = hidinput->input;
312 switch (hidinput->report->field[0]->application) {
314 input->name = "N-Trig Pen";
316 case HID_DG_TOUCHSCREEN:
317 __clear_bit(BTN_TOOL_PEN, input->keybit);
319 * A little something special to enable
320 * two and three finger taps.
322 __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
323 __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
324 __set_bit(BTN_TOOL_QUADTAP, input->keybit);
326 * The physical touchscreen (single touch)
327 * input has a value for physical, whereas
328 * the multitouch only has logical input
332 (hidinput->report->field[0]
334 "N-Trig Touchscreen" :
346 static void ntrig_remove(struct hid_device *hdev)
349 kfree(hid_get_drvdata(hdev));
352 static const struct hid_device_id ntrig_devices[] = {
353 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
354 .driver_data = NTRIG_DUPLICATE_USAGES },
357 MODULE_DEVICE_TABLE(hid, ntrig_devices);
359 static const struct hid_usage_id ntrig_grabbed_usages[] = {
360 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
361 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
364 static struct hid_driver ntrig_driver = {
366 .id_table = ntrig_devices,
367 .probe = ntrig_probe,
368 .remove = ntrig_remove,
369 .input_mapping = ntrig_input_mapping,
370 .input_mapped = ntrig_input_mapped,
371 .usage_table = ntrig_grabbed_usages,
372 .event = ntrig_event,
375 static int __init ntrig_init(void)
377 return hid_register_driver(&ntrig_driver);
380 static void __exit ntrig_exit(void)
382 hid_unregister_driver(&ntrig_driver);
385 module_init(ntrig_init);
386 module_exit(ntrig_exit);
387 MODULE_LICENSE("GPL");