blob: 38b2364c88670ead902c34cf26131f9b6e6efcb7 [file] [log] [blame]
Rafi Rubin94011f92008-11-19 15:54:46 +01001/*
Stephane Chatty57fd6372009-05-20 15:49:35 +02002 * HID driver for N-Trig touchscreens
Rafi Rubin94011f92008-11-19 15:54:46 +01003 *
Rafi Rubin94011f92008-11-19 15:54:46 +01004 * Copyright (c) 2008 Rafi Rubin
Stephane Chatty57fd6372009-05-20 15:49:35 +02005 * Copyright (c) 2009 Stephane Chatty
Rafi Rubin94011f92008-11-19 15:54:46 +01006 *
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
Stephane Chatty57fd6372009-05-20 15:49:35 +020027struct ntrig_data {
28 __s32 x, y, id, w, h;
Rafi Rubin943ed462010-02-11 22:14:05 -050029 bool reading_a_point, found_contact_id;
30 bool pen_active;
31 bool finger_active;
32 bool inverted;
Stephane Chatty57fd6372009-05-20 15:49:35 +020033};
34
35/*
36 * this driver is aimed at two firmware versions in circulation:
37 * - dual pen/finger single touch
38 * - finger multitouch, pen not working
39 */
40
Rafi Rubin94011f92008-11-19 15:54:46 +010041static 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)
44{
Rafi Rubin943ed462010-02-11 22:14:05 -050045 /* No special mappings needed for the pen */
46 if (field->application == HID_DG_PEN)
47 return 0;
48
Stephane Chatty57fd6372009-05-20 15:49:35 +020049 switch (usage->hid & HID_USAGE_PAGE) {
50
51 case HID_UP_GENDESK:
52 switch (usage->hid) {
53 case HID_GD_X:
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);
59 return 1;
60 case HID_GD_Y:
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);
66 return 1;
67 }
68 return 0;
69
70 case HID_UP_DIGITIZER:
71 switch (usage->hid) {
72 /* we do not want to map these for now */
Stephane Chatty57fd6372009-05-20 15:49:35 +020073 case HID_DG_CONTACTID: /* value is useless */
Stephane Chatty57fd6372009-05-20 15:49:35 +020074 case HID_DG_INPUTMODE:
75 case HID_DG_DEVICEINDEX:
76 case HID_DG_CONTACTCOUNT:
77 case HID_DG_CONTACTMAX:
78 return -1;
79
80 /* original mapping by Rafi Rubin */
81 case HID_DG_CONFIDENCE:
82 nt_map_key_clear(BTN_TOOL_DOUBLETAP);
83 return 1;
84
85 /* width/height mapped on TouchMajor/TouchMinor/Orientation */
86 case HID_DG_WIDTH:
87 hid_map_usage(hi, usage, bit, max,
88 EV_ABS, ABS_MT_TOUCH_MAJOR);
89 return 1;
90 case HID_DG_HEIGHT:
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,
94 0, 1, 0, 0);
95 return 1;
96 }
97 return 0;
98
99 case 0xff000000:
100 /* we do not want to map these: no input-oriented meaning */
101 return -1;
Rafi Rubin94011f92008-11-19 15:54:46 +0100102 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200103
Rafi Rubin94011f92008-11-19 15:54:46 +0100104 return 0;
105}
106
107static 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)
110{
Rafi Rubin943ed462010-02-11 22:14:05 -0500111 /* No special mappings needed for the pen */
112 if (field->application == HID_DG_PEN)
113 return 0;
Rafi Rubin94011f92008-11-19 15:54:46 +0100114 if (usage->type == EV_KEY || usage->type == EV_REL
115 || usage->type == EV_ABS)
116 clear_bit(usage->code, *bit);
117
118 return 0;
119}
Stephane Chatty57fd6372009-05-20 15:49:35 +0200120
121/*
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
126 */
127static int ntrig_event (struct hid_device *hid, struct hid_field *field,
128 struct hid_usage *usage, __s32 value)
129{
130 struct input_dev *input = field->hidinput->input;
131 struct ntrig_data *nd = hid_get_drvdata(hid);
132
Rafi Rubin943ed462010-02-11 22:14:05 -0500133 /* No special handling needed for the pen */
134 if (field->application == HID_DG_PEN)
135 return 0;
136
Stephane Chatty57fd6372009-05-20 15:49:35 +0200137 if (hid->claimed & HID_CLAIMED_INPUT) {
138 switch (usage->hid) {
Rafi Rubin837b4752009-06-23 14:09:26 -0400139
140 case HID_DG_INRANGE:
141 if (field->application & 0x3)
142 nd->pen_active = (value != 0);
143 else
144 nd->finger_active = (value != 0);
145 return 0;
146
147 case HID_DG_INVERT:
148 nd->inverted = value;
149 return 0;
150
Stephane Chatty57fd6372009-05-20 15:49:35 +0200151 case HID_GD_X:
152 nd->x = value;
153 nd->reading_a_point = 1;
154 break;
155 case HID_GD_Y:
156 nd->y = value;
157 break;
158 case HID_DG_CONTACTID:
159 nd->id = value;
160 /* we receive this only when in multitouch mode */
161 nd->found_contact_id = 1;
162 break;
163 case HID_DG_WIDTH:
164 nd->w = value;
165 break;
166 case HID_DG_HEIGHT:
167 nd->h = value;
168 /*
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
172 */
Rafi Rubin837b4752009-06-23 14:09:26 -0400173 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);
177 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200178 input_event(input, EV_ABS, ABS_X, nd->x);
179 input_event(input, EV_ABS, ABS_Y, nd->y);
180 }
181 break;
182 case HID_DG_TIPPRESSURE:
183 /*
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
187 */
188 if (! nd->found_contact_id) {
Rafi Rubin837b4752009-06-23 14:09:26 -0400189 if (nd->pen_active && nd->finger_active) {
190 input_report_key(input,
191 nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN
192 , 0);
193 input_report_key(input,
194 nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN
195 , 1);
196 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200197 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);
200 }
201 break;
202 case 0xff000002:
203 /*
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
207 * or a placeholder
208 */
209 if (!nd->reading_a_point || value != 1)
210 break;
211 /* emit a normal (X, Y) for the first point only */
212 if (nd->id == 0) {
213 input_event(input, EV_ABS, ABS_X, nd->x);
214 input_event(input, EV_ABS, ABS_Y, nd->y);
215 }
216 input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
217 input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
218 if (nd->w > nd->h) {
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);
225 } else {
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);
232 }
233 input_mt_sync(field->hidinput->input);
234 nd->reading_a_point = 0;
235 nd->found_contact_id = 0;
236 break;
237
238 default:
239 /* fallback to the generic hidinput handling */
240 return 0;
241 }
242 }
243
244 /* we have handled the hidinput part, now remains hiddev */
Rafi Rubin943ed462010-02-11 22:14:05 -0500245 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
246 hid->hiddev_hid_event(hid, field, usage, value);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200247
248 return 1;
249}
250
251static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
252{
253 int ret;
254 struct ntrig_data *nd;
Rafi Rubin943ed462010-02-11 22:14:05 -0500255 struct hid_input *hidinput;
256 struct input_dev *input;
257
258 if (id->driver_data)
259 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
Stephane Chatty57fd6372009-05-20 15:49:35 +0200260
261 nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
262 if (!nd) {
263 dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
264 return -ENOMEM;
265 }
266 nd->reading_a_point = 0;
267 nd->found_contact_id = 0;
268 hid_set_drvdata(hdev, nd);
269
270 ret = hid_parse(hdev);
Rafi Rubin943ed462010-02-11 22:14:05 -0500271 if (ret) {
272 dev_err(&hdev->dev, "parse failed\n");
273 goto err_free;
274 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200275
Rafi Rubin943ed462010-02-11 22:14:05 -0500276 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
277 if (ret) {
278 dev_err(&hdev->dev, "hw start failed\n");
279 goto err_free;
280 }
Rafi Rubin837b4752009-06-23 14:09:26 -0400281
Rafi Rubin943ed462010-02-11 22:14:05 -0500282
283 list_for_each_entry(hidinput, &hdev->inputs, list) {
284 input = hidinput->input;
285 switch (hidinput->report->field[0]->application) {
286 case HID_DG_PEN:
287 input->name = "N-Trig Pen";
288 break;
289 case HID_DG_TOUCHSCREEN:
290 /*
291 * The physical touchscreen (single touch)
292 * input has a value for physical, whereas
293 * the multitouch only has logical input
294 * fields.
295 */
296 input->name =
297 (hidinput->report->field[0]
298 ->physical) ?
299 "N-Trig Touchscreen" :
300 "N-Trig MultiTouch";
301 break;
302 }
303 }
304
305 return 0;
306err_free:
307 kfree(nd);
Stephane Chatty57fd6372009-05-20 15:49:35 +0200308 return ret;
309}
310
311static void ntrig_remove(struct hid_device *hdev)
312{
313 hid_hw_stop(hdev);
314 kfree(hid_get_drvdata(hdev));
315}
316
Rafi Rubin94011f92008-11-19 15:54:46 +0100317static 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 },
320 { }
321};
322MODULE_DEVICE_TABLE(hid, ntrig_devices);
323
Stephane Chatty57fd6372009-05-20 15:49:35 +0200324static const struct hid_usage_id ntrig_grabbed_usages[] = {
325 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
Rafi Rubin943ed462010-02-11 22:14:05 -0500326 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
Stephane Chatty57fd6372009-05-20 15:49:35 +0200327};
328
Rafi Rubin94011f92008-11-19 15:54:46 +0100329static struct hid_driver ntrig_driver = {
330 .name = "ntrig",
331 .id_table = ntrig_devices,
Stephane Chatty57fd6372009-05-20 15:49:35 +0200332 .probe = ntrig_probe,
333 .remove = ntrig_remove,
Rafi Rubin94011f92008-11-19 15:54:46 +0100334 .input_mapping = ntrig_input_mapping,
335 .input_mapped = ntrig_input_mapped,
Stephane Chatty57fd6372009-05-20 15:49:35 +0200336 .usage_table = ntrig_grabbed_usages,
337 .event = ntrig_event,
Rafi Rubin94011f92008-11-19 15:54:46 +0100338};
339
Peter Huewea24f4232009-07-02 19:08:38 +0200340static int __init ntrig_init(void)
Rafi Rubin94011f92008-11-19 15:54:46 +0100341{
342 return hid_register_driver(&ntrig_driver);
343}
344
Peter Huewea24f4232009-07-02 19:08:38 +0200345static void __exit ntrig_exit(void)
Rafi Rubin94011f92008-11-19 15:54:46 +0100346{
347 hid_unregister_driver(&ntrig_driver);
348}
349
350module_init(ntrig_init);
351module_exit(ntrig_exit);
352MODULE_LICENSE("GPL");