Commit | Line | Data |
---|---|---|
94011f93 | 1 | /* |
57fd637a | 2 | * HID driver for N-Trig touchscreens |
94011f93 | 3 | * |
94011f93 | 4 | * Copyright (c) 2008 Rafi Rubin |
57fd637a | 5 | * Copyright (c) 2009 Stephane Chatty |
94011f93 RR |
6 | * |
7 | */ | |
8 | ||
9 | /* | |
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) | |
13 | * any later version. | |
14 | */ | |
15 | ||
16 | #include <linux/device.h> | |
17 | #include <linux/hid.h> | |
18 | #include <linux/module.h> | |
19 | ||
20 | #include "hid-ids.h" | |
21 | ||
22 | #define NTRIG_DUPLICATE_USAGES 0x001 | |
23 | ||
24 | #define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \ | |
25 | EV_KEY, (c)) | |
26 | ||
57fd637a | 27 | struct ntrig_data { |
dbf2b17d RR |
28 | /* Incoming raw values for a single contact */ |
29 | __u16 x, y, w, h; | |
30 | __u16 id; | |
31 | __u8 confidence; | |
32 | ||
33 | bool reading_mt; | |
34 | __u8 first_contact_confidence; | |
35 | ||
36 | __u8 mt_footer[4]; | |
37 | __u8 mt_foot_count; | |
57fd637a SC |
38 | }; |
39 | ||
40 | /* | |
41 | * this driver is aimed at two firmware versions in circulation: | |
42 | * - dual pen/finger single touch | |
43 | * - finger multitouch, pen not working | |
44 | */ | |
45 | ||
94011f93 RR |
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) | |
49 | { | |
dbf2b17d RR |
50 | /* No special mappings needed for the pen and single touch */ |
51 | if (field->physical) | |
943ed464 RR |
52 | return 0; |
53 | ||
57fd637a | 54 | switch (usage->hid & HID_USAGE_PAGE) { |
57fd637a SC |
55 | case HID_UP_GENDESK: |
56 | switch (usage->hid) { | |
57 | case HID_GD_X: | |
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); | |
63 | return 1; | |
64 | case HID_GD_Y: | |
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); | |
70 | return 1; | |
71 | } | |
72 | return 0; | |
73 | ||
74 | case HID_UP_DIGITIZER: | |
75 | switch (usage->hid) { | |
76 | /* we do not want to map these for now */ | |
dbf2b17d | 77 | case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */ |
57fd637a SC |
78 | case HID_DG_INPUTMODE: |
79 | case HID_DG_DEVICEINDEX: | |
57fd637a SC |
80 | case HID_DG_CONTACTMAX: |
81 | return -1; | |
82 | ||
57fd637a SC |
83 | /* width/height mapped on TouchMajor/TouchMinor/Orientation */ |
84 | case HID_DG_WIDTH: | |
85 | hid_map_usage(hi, usage, bit, max, | |
86 | EV_ABS, ABS_MT_TOUCH_MAJOR); | |
87 | return 1; | |
88 | case HID_DG_HEIGHT: | |
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, | |
92 | 0, 1, 0, 0); | |
93 | return 1; | |
94 | } | |
95 | return 0; | |
96 | ||
97 | case 0xff000000: | |
98 | /* we do not want to map these: no input-oriented meaning */ | |
99 | return -1; | |
94011f93 | 100 | } |
57fd637a | 101 | |
94011f93 RR |
102 | return 0; |
103 | } | |
104 | ||
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) | |
108 | { | |
dbf2b17d RR |
109 | /* No special mappings needed for the pen and single touch */ |
110 | if (field->physical) | |
943ed464 | 111 | return 0; |
b0549cf1 | 112 | |
94011f93 RR |
113 | if (usage->type == EV_KEY || usage->type == EV_REL |
114 | || usage->type == EV_ABS) | |
115 | clear_bit(usage->code, *bit); | |
116 | ||
117 | return 0; | |
118 | } | |
57fd637a SC |
119 | |
120 | /* | |
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 | |
125 | */ | |
126 | static int ntrig_event (struct hid_device *hid, struct hid_field *field, | |
127 | struct hid_usage *usage, __s32 value) | |
128 | { | |
129 | struct input_dev *input = field->hidinput->input; | |
130 | struct ntrig_data *nd = hid_get_drvdata(hid); | |
131 | ||
943ed464 RR |
132 | /* No special handling needed for the pen */ |
133 | if (field->application == HID_DG_PEN) | |
134 | return 0; | |
135 | ||
57fd637a SC |
136 | if (hid->claimed & HID_CLAIMED_INPUT) { |
137 | switch (usage->hid) { | |
dbf2b17d RR |
138 | case 0xff000001: |
139 | /* Tag indicating the start of a multitouch group */ | |
140 | nd->reading_mt = 1; | |
141 | nd->first_contact_confidence = 0; | |
142 | break; | |
2886539d RR |
143 | case HID_DG_TIPSWITCH: |
144 | /* Prevent emission of touch until validated */ | |
145 | return 1; | |
dbf2b17d RR |
146 | case HID_DG_CONFIDENCE: |
147 | nd->confidence = value; | |
148 | break; | |
57fd637a SC |
149 | case HID_GD_X: |
150 | nd->x = value; | |
dbf2b17d RR |
151 | /* Clear the contact footer */ |
152 | nd->mt_foot_count = 0; | |
57fd637a SC |
153 | break; |
154 | case HID_GD_Y: | |
155 | nd->y = value; | |
156 | break; | |
157 | case HID_DG_CONTACTID: | |
158 | nd->id = value; | |
57fd637a SC |
159 | break; |
160 | case HID_DG_WIDTH: | |
161 | nd->w = value; | |
162 | break; | |
163 | case HID_DG_HEIGHT: | |
164 | nd->h = value; | |
165 | /* | |
166 | * when in single touch mode, this is the last | |
167 | * report received in a finger event. We want | |
168 | * to emit a normal (X, Y) position | |
169 | */ | |
dbf2b17d | 170 | if (!nd->reading_mt) { |
ff5cf34c RR |
171 | input_report_key(input, BTN_TOOL_DOUBLETAP, |
172 | (nd->confidence != 0)); | |
57fd637a SC |
173 | input_event(input, EV_ABS, ABS_X, nd->x); |
174 | input_event(input, EV_ABS, ABS_Y, nd->y); | |
57fd637a SC |
175 | } |
176 | break; | |
177 | case 0xff000002: | |
178 | /* | |
179 | * we receive this when the device is in multitouch | |
180 | * mode. The first of the three values tagged with | |
181 | * this usage tells if the contact point is real | |
182 | * or a placeholder | |
183 | */ | |
dbf2b17d RR |
184 | |
185 | /* Shouldn't get more than 4 footer packets, so skip */ | |
186 | if (nd->mt_foot_count >= 4) | |
187 | break; | |
188 | ||
189 | nd->mt_footer[nd->mt_foot_count++] = value; | |
190 | ||
191 | /* if the footer isn't complete break */ | |
192 | if (nd->mt_foot_count != 4) | |
193 | break; | |
194 | ||
195 | /* Pen activity signal, trigger end of touch. */ | |
196 | if (nd->mt_footer[2]) { | |
197 | nd->confidence = 0; | |
57fd637a | 198 | break; |
dbf2b17d RR |
199 | } |
200 | ||
201 | /* If the contact was invalid */ | |
202 | if (!(nd->confidence && nd->mt_footer[0]) | |
203 | || nd->w <= 250 | |
204 | || nd->h <= 190) { | |
205 | nd->confidence = 0; | |
206 | break; | |
207 | } | |
208 | ||
57fd637a SC |
209 | /* emit a normal (X, Y) for the first point only */ |
210 | if (nd->id == 0) { | |
dbf2b17d | 211 | nd->first_contact_confidence = nd->confidence; |
57fd637a SC |
212 | input_event(input, EV_ABS, ABS_X, nd->x); |
213 | input_event(input, EV_ABS, ABS_Y, nd->y); | |
214 | } | |
215 | input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x); | |
216 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y); | |
217 | if (nd->w > nd->h) { | |
218 | input_event(input, EV_ABS, | |
219 | ABS_MT_ORIENTATION, 1); | |
220 | input_event(input, EV_ABS, | |
221 | ABS_MT_TOUCH_MAJOR, nd->w); | |
222 | input_event(input, EV_ABS, | |
223 | ABS_MT_TOUCH_MINOR, nd->h); | |
224 | } else { | |
225 | input_event(input, EV_ABS, | |
226 | ABS_MT_ORIENTATION, 0); | |
227 | input_event(input, EV_ABS, | |
228 | ABS_MT_TOUCH_MAJOR, nd->h); | |
229 | input_event(input, EV_ABS, | |
230 | ABS_MT_TOUCH_MINOR, nd->w); | |
231 | } | |
232 | input_mt_sync(field->hidinput->input); | |
dbf2b17d RR |
233 | break; |
234 | ||
235 | case HID_DG_CONTACTCOUNT: /* End of a multitouch group */ | |
236 | if (!nd->reading_mt) | |
237 | break; | |
238 | ||
239 | nd->reading_mt = 0; | |
240 | ||
241 | if (nd->first_contact_confidence) { | |
242 | switch (value) { | |
243 | case 0: /* for single touch devices */ | |
244 | case 1: | |
245 | input_report_key(input, | |
246 | BTN_TOOL_DOUBLETAP, 1); | |
247 | break; | |
248 | case 2: | |
249 | input_report_key(input, | |
250 | BTN_TOOL_TRIPLETAP, 1); | |
251 | break; | |
252 | case 3: | |
253 | default: | |
254 | input_report_key(input, | |
255 | BTN_TOOL_QUADTAP, 1); | |
256 | } | |
257 | input_report_key(input, BTN_TOUCH, 1); | |
258 | } else { | |
259 | input_report_key(input, | |
260 | BTN_TOOL_DOUBLETAP, 0); | |
261 | input_report_key(input, | |
262 | BTN_TOOL_TRIPLETAP, 0); | |
263 | input_report_key(input, | |
264 | BTN_TOOL_QUADTAP, 0); | |
2886539d | 265 | input_report_key(input, BTN_TOUCH, 0); |
dbf2b17d | 266 | } |
57fd637a SC |
267 | break; |
268 | ||
269 | default: | |
270 | /* fallback to the generic hidinput handling */ | |
271 | return 0; | |
272 | } | |
273 | } | |
274 | ||
275 | /* we have handled the hidinput part, now remains hiddev */ | |
943ed464 RR |
276 | if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event) |
277 | hid->hiddev_hid_event(hid, field, usage, value); | |
57fd637a SC |
278 | |
279 | return 1; | |
280 | } | |
281 | ||
282 | static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
283 | { | |
284 | int ret; | |
285 | struct ntrig_data *nd; | |
943ed464 RR |
286 | struct hid_input *hidinput; |
287 | struct input_dev *input; | |
288 | ||
289 | if (id->driver_data) | |
290 | hdev->quirks |= HID_QUIRK_MULTI_INPUT; | |
57fd637a SC |
291 | |
292 | nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL); | |
293 | if (!nd) { | |
294 | dev_err(&hdev->dev, "cannot allocate N-Trig data\n"); | |
295 | return -ENOMEM; | |
296 | } | |
dbf2b17d RR |
297 | |
298 | nd->reading_mt = 0; | |
57fd637a SC |
299 | hid_set_drvdata(hdev, nd); |
300 | ||
301 | ret = hid_parse(hdev); | |
943ed464 RR |
302 | if (ret) { |
303 | dev_err(&hdev->dev, "parse failed\n"); | |
304 | goto err_free; | |
305 | } | |
306 | ||
307 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); | |
308 | if (ret) { | |
309 | dev_err(&hdev->dev, "hw start failed\n"); | |
310 | goto err_free; | |
311 | } | |
57fd637a | 312 | |
837b4753 | 313 | |
943ed464 | 314 | list_for_each_entry(hidinput, &hdev->inputs, list) { |
2886539d RR |
315 | if (hidinput->report->maxfield < 1) |
316 | continue; | |
317 | ||
943ed464 RR |
318 | input = hidinput->input; |
319 | switch (hidinput->report->field[0]->application) { | |
320 | case HID_DG_PEN: | |
321 | input->name = "N-Trig Pen"; | |
322 | break; | |
323 | case HID_DG_TOUCHSCREEN: | |
2886539d RR |
324 | /* These keys are redundant for fingers, clear them |
325 | * to prevent incorrect identification */ | |
dbf2b17d | 326 | __clear_bit(BTN_TOOL_PEN, input->keybit); |
2886539d RR |
327 | __clear_bit(BTN_TOOL_FINGER, input->keybit); |
328 | __clear_bit(BTN_0, input->keybit); | |
dbf2b17d RR |
329 | /* |
330 | * A little something special to enable | |
331 | * two and three finger taps. | |
332 | */ | |
333 | __set_bit(BTN_TOOL_DOUBLETAP, input->keybit); | |
334 | __set_bit(BTN_TOOL_TRIPLETAP, input->keybit); | |
335 | __set_bit(BTN_TOOL_QUADTAP, input->keybit); | |
943ed464 RR |
336 | /* |
337 | * The physical touchscreen (single touch) | |
338 | * input has a value for physical, whereas | |
339 | * the multitouch only has logical input | |
340 | * fields. | |
341 | */ | |
342 | input->name = | |
343 | (hidinput->report->field[0] | |
344 | ->physical) ? | |
345 | "N-Trig Touchscreen" : | |
346 | "N-Trig MultiTouch"; | |
347 | break; | |
348 | } | |
349 | } | |
350 | ||
351 | return 0; | |
352 | err_free: | |
353 | kfree(nd); | |
57fd637a SC |
354 | return ret; |
355 | } | |
356 | ||
357 | static void ntrig_remove(struct hid_device *hdev) | |
358 | { | |
359 | hid_hw_stop(hdev); | |
360 | kfree(hid_get_drvdata(hdev)); | |
361 | } | |
362 | ||
94011f93 RR |
363 | static const struct hid_device_id ntrig_devices[] = { |
364 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN), | |
365 | .driver_data = NTRIG_DUPLICATE_USAGES }, | |
366 | { } | |
367 | }; | |
368 | MODULE_DEVICE_TABLE(hid, ntrig_devices); | |
369 | ||
57fd637a SC |
370 | static const struct hid_usage_id ntrig_grabbed_usages[] = { |
371 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, | |
943ed464 | 372 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 } |
57fd637a SC |
373 | }; |
374 | ||
94011f93 RR |
375 | static struct hid_driver ntrig_driver = { |
376 | .name = "ntrig", | |
377 | .id_table = ntrig_devices, | |
57fd637a SC |
378 | .probe = ntrig_probe, |
379 | .remove = ntrig_remove, | |
94011f93 RR |
380 | .input_mapping = ntrig_input_mapping, |
381 | .input_mapped = ntrig_input_mapped, | |
57fd637a SC |
382 | .usage_table = ntrig_grabbed_usages, |
383 | .event = ntrig_event, | |
94011f93 RR |
384 | }; |
385 | ||
a24f423b | 386 | static int __init ntrig_init(void) |
94011f93 RR |
387 | { |
388 | return hid_register_driver(&ntrig_driver); | |
389 | } | |
390 | ||
a24f423b | 391 | static void __exit ntrig_exit(void) |
94011f93 RR |
392 | { |
393 | hid_unregister_driver(&ntrig_driver); | |
394 | } | |
395 | ||
396 | module_init(ntrig_init); | |
397 | module_exit(ntrig_exit); | |
398 | MODULE_LICENSE("GPL"); |