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