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, \
29 bool reading_a_point, found_contact_id;
36 * this driver is aimed at two firmware versions in circulation:
37 * - dual pen/finger single touch
38 * - finger multitouch, pen not working
41 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
42 struct hid_field *field, struct hid_usage *usage,
43 unsigned long **bit, int *max)
45 /* No special mappings needed for the pen */
46 if (field->application == HID_DG_PEN)
49 switch (usage->hid & HID_USAGE_PAGE) {
54 hid_map_usage(hi, usage, bit, max,
55 EV_ABS, ABS_MT_POSITION_X);
56 input_set_abs_params(hi->input, ABS_X,
57 field->logical_minimum,
58 field->logical_maximum, 0, 0);
61 hid_map_usage(hi, usage, bit, max,
62 EV_ABS, ABS_MT_POSITION_Y);
63 input_set_abs_params(hi->input, ABS_Y,
64 field->logical_minimum,
65 field->logical_maximum, 0, 0);
70 case HID_UP_DIGITIZER:
72 /* we do not want to map these for now */
73 case HID_DG_CONTACTID: /* value is useless */
74 case HID_DG_INPUTMODE:
75 case HID_DG_DEVICEINDEX:
76 case HID_DG_CONTACTCOUNT:
77 case HID_DG_CONTACTMAX:
80 /* original mapping by Rafi Rubin */
81 case HID_DG_CONFIDENCE:
82 nt_map_key_clear(BTN_TOOL_DOUBLETAP);
85 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
87 hid_map_usage(hi, usage, bit, max,
88 EV_ABS, ABS_MT_TOUCH_MAJOR);
91 hid_map_usage(hi, usage, bit, max,
92 EV_ABS, ABS_MT_TOUCH_MINOR);
93 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
100 /* we do not want to map these: no input-oriented meaning */
107 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
108 struct hid_field *field, struct hid_usage *usage,
109 unsigned long **bit, int *max)
111 /* No special mappings needed for the pen */
112 if (field->application == HID_DG_PEN)
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) {
141 if (field->application & 0x3)
142 nd->pen_active = (value != 0);
144 nd->finger_active = (value != 0);
148 nd->inverted = value;
153 nd->reading_a_point = 1;
158 case HID_DG_CONTACTID:
160 /* we receive this only when in multitouch mode */
161 nd->found_contact_id = 1;
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->found_contact_id) {
174 if (nd->pen_active && nd->finger_active) {
175 input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
176 input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
178 input_event(input, EV_ABS, ABS_X, nd->x);
179 input_event(input, EV_ABS, ABS_Y, nd->y);
182 case HID_DG_TIPPRESSURE:
184 * when in single touch mode, this is the last
185 * report received in a pen event. We want
186 * to emit a normal (X, Y) position
188 if (! nd->found_contact_id) {
189 if (nd->pen_active && nd->finger_active) {
190 input_report_key(input,
191 nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN
193 input_report_key(input,
194 nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN
197 input_event(input, EV_ABS, ABS_X, nd->x);
198 input_event(input, EV_ABS, ABS_Y, nd->y);
199 input_event(input, EV_ABS, ABS_PRESSURE, value);
204 * we receive this when the device is in multitouch
205 * mode. The first of the three values tagged with
206 * this usage tells if the contact point is real
209 if (!nd->reading_a_point || value != 1)
211 /* emit a normal (X, Y) for the first point only */
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);
234 nd->reading_a_point = 0;
235 nd->found_contact_id = 0;
239 /* fallback to the generic hidinput handling */
244 /* we have handled the hidinput part, now remains hiddev */
245 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
246 hid->hiddev_hid_event(hid, field, usage, value);
251 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
254 struct ntrig_data *nd;
255 struct hid_input *hidinput;
256 struct input_dev *input;
259 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
261 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
263 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
266 nd->reading_a_point = 0;
267 nd->found_contact_id = 0;
268 hid_set_drvdata(hdev, nd);
270 ret = hid_parse(hdev);
272 dev_err(&hdev->dev, "parse failed\n");
276 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
278 dev_err(&hdev->dev, "hw start failed\n");
283 list_for_each_entry(hidinput, &hdev->inputs, list) {
284 input = hidinput->input;
285 switch (hidinput->report->field[0]->application) {
287 input->name = "N-Trig Pen";
289 case HID_DG_TOUCHSCREEN:
291 * The physical touchscreen (single touch)
292 * input has a value for physical, whereas
293 * the multitouch only has logical input
297 (hidinput->report->field[0]
299 "N-Trig Touchscreen" :
311 static void ntrig_remove(struct hid_device *hdev)
314 kfree(hid_get_drvdata(hdev));
317 static const struct hid_device_id ntrig_devices[] = {
318 { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
319 .driver_data = NTRIG_DUPLICATE_USAGES },
322 MODULE_DEVICE_TABLE(hid, ntrig_devices);
324 static const struct hid_usage_id ntrig_grabbed_usages[] = {
325 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
326 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
329 static struct hid_driver ntrig_driver = {
331 .id_table = ntrig_devices,
332 .probe = ntrig_probe,
333 .remove = ntrig_remove,
334 .input_mapping = ntrig_input_mapping,
335 .input_mapped = ntrig_input_mapped,
336 .usage_table = ntrig_grabbed_usages,
337 .event = ntrig_event,
340 static int __init ntrig_init(void)
342 return hid_register_driver(&ntrig_driver);
345 static void __exit ntrig_exit(void)
347 hid_unregister_driver(&ntrig_driver);
350 module_init(ntrig_init);
351 module_exit(ntrig_exit);
352 MODULE_LICENSE("GPL");