Merge remote branch 'alsa/devel' into topic/misc
[pandora-kernel.git] / drivers / hid / hid-egalax.c
1 /*
2  *  HID driver for eGalax dual-touch panels
3  *
4  *  Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
5  *
6  */
7
8 /*
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  */
14
15 #include <linux/device.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/slab.h>
20 #include "usbhid/usbhid.h"
21
22 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
23 MODULE_DESCRIPTION("eGalax dual-touch panel");
24 MODULE_LICENSE("GPL");
25
26 #include "hid-ids.h"
27
28 struct egalax_data {
29         __u16 x, y, z;
30         __u8 id;
31         bool first;             /* is this the first finger in the frame? */
32         bool valid;             /* valid finger data, or just placeholder? */
33         bool activity;          /* at least one active finger previously? */
34         __u16 lastx, lasty;     /* latest valid (x, y) in the frame */
35 };
36
37 static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
38                 struct hid_field *field, struct hid_usage *usage,
39                 unsigned long **bit, int *max)
40 {
41         switch (usage->hid & HID_USAGE_PAGE) {
42
43         case HID_UP_GENDESK:
44                 switch (usage->hid) {
45                 case HID_GD_X:
46                         hid_map_usage(hi, usage, bit, max,
47                                         EV_ABS, ABS_MT_POSITION_X);
48                         /* touchscreen emulation */
49                         input_set_abs_params(hi->input, ABS_X,
50                                                 field->logical_minimum,
51                                                 field->logical_maximum, 0, 0);
52                         return 1;
53                 case HID_GD_Y:
54                         hid_map_usage(hi, usage, bit, max,
55                                         EV_ABS, ABS_MT_POSITION_Y);
56                         /* touchscreen emulation */
57                         input_set_abs_params(hi->input, ABS_Y,
58                                                 field->logical_minimum,
59                                                 field->logical_maximum, 0, 0);
60                         return 1;
61                 }
62                 return 0;
63
64         case HID_UP_DIGITIZER:
65                 switch (usage->hid) {
66                 case HID_DG_TIPSWITCH:
67                         /* touchscreen emulation */
68                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
69                         return 1;
70                 case HID_DG_INRANGE:
71                 case HID_DG_CONFIDENCE:
72                 case HID_DG_CONTACTCOUNT:
73                 case HID_DG_CONTACTMAX:
74                         return -1;
75                 case HID_DG_CONTACTID:
76                         hid_map_usage(hi, usage, bit, max,
77                                         EV_ABS, ABS_MT_TRACKING_ID);
78                         return 1;
79                 case HID_DG_TIPPRESSURE:
80                         hid_map_usage(hi, usage, bit, max,
81                                         EV_ABS, ABS_MT_PRESSURE);
82                         return 1;
83                 }
84                 return 0;
85         }
86
87         /* ignore others (from other reports we won't get anyway) */
88         return -1;
89 }
90
91 static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
92                 struct hid_field *field, struct hid_usage *usage,
93                 unsigned long **bit, int *max)
94 {
95         if (usage->type == EV_KEY || usage->type == EV_ABS)
96                 clear_bit(usage->code, *bit);
97
98         return 0;
99 }
100
101 /*
102  * this function is called when a whole finger has been parsed,
103  * so that it can decide what to send to the input layer.
104  */
105 static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
106 {
107         td->first = !td->first; /* touchscreen emulation */
108
109         if (td->valid) {
110                 /* emit multitouch events */
111                 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
112                 input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
113                 input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
114                 input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
115
116                 input_mt_sync(input);
117
118                 /*
119                  * touchscreen emulation: store (x, y) as
120                  * the last valid values in this frame
121                  */
122                 td->lastx = td->x;
123                 td->lasty = td->y;
124         }
125
126         /*
127          * touchscreen emulation: if this is the second finger and at least
128          * one in this frame is valid, the latest valid in the frame is
129          * the oldest on the panel, the one we want for single touch
130          */
131         if (!td->first && td->activity) {
132                 input_event(input, EV_ABS, ABS_X, td->lastx);
133                 input_event(input, EV_ABS, ABS_Y, td->lasty);
134         }
135
136         if (!td->valid) {
137                 /*
138                  * touchscreen emulation: if the first finger is invalid
139                  * and there previously was finger activity, this is a release
140                  */ 
141                 if (td->first && td->activity) {
142                         input_event(input, EV_KEY, BTN_TOUCH, 0);
143                         td->activity = false;
144                 }
145                 return;
146         }
147
148
149         /* touchscreen emulation: if no previous activity, emit touch event */
150         if (!td->activity) {
151                 input_event(input, EV_KEY, BTN_TOUCH, 1);
152                 td->activity = true;
153         }
154 }
155
156
157 static int egalax_event(struct hid_device *hid, struct hid_field *field,
158                                 struct hid_usage *usage, __s32 value)
159 {
160         struct egalax_data *td = hid_get_drvdata(hid);
161
162         /* Note, eGalax has two product lines: the first is resistive and
163          * uses a standard parallel multitouch protocol (product ID ==
164          * 48xx).  The second is capacitive and uses an unusual "serial"
165          * protocol with a different message for each multitouch finger
166          * (product ID == 72xx).  We do not yet generate a correct event
167          * sequence for the capacitive/serial protocol.
168          */
169         if (hid->claimed & HID_CLAIMED_INPUT) {
170                 struct input_dev *input = field->hidinput->input;
171
172                 switch (usage->hid) {
173                 case HID_DG_INRANGE:
174                 case HID_DG_CONFIDENCE:
175                         /* avoid interference from generic hidinput handling */
176                         break;
177                 case HID_DG_TIPSWITCH:
178                         td->valid = value;
179                         break;
180                 case HID_DG_TIPPRESSURE:
181                         td->z = value;
182                         break;
183                 case HID_DG_CONTACTID:
184                         td->id = value;
185                         break;
186                 case HID_GD_X:
187                         td->x = value;
188                         break;
189                 case HID_GD_Y:
190                         td->y = value;
191                         /* this is the last field in a finger */
192                         egalax_filter_event(td, input);
193                         break;
194                 case HID_DG_CONTACTCOUNT:
195                         /* touch emulation: this is the last field in a frame */
196                         td->first = false;
197                         break;
198
199                 default:
200                         /* fallback to the generic hidinput handling */
201                         return 0;
202                 }
203         }
204
205         /* we have handled the hidinput part, now remains hiddev */
206         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
207                 hid->hiddev_hid_event(hid, field, usage, value);
208
209         return 1;
210 }
211
212 static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
213 {
214         int ret;
215         struct egalax_data *td;
216         struct hid_report *report;
217
218         td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
219         if (!td) {
220                 dev_err(&hdev->dev, "cannot allocate eGalax data\n");
221                 return -ENOMEM;
222         }
223         hid_set_drvdata(hdev, td);
224
225         ret = hid_parse(hdev);
226         if (ret)
227                 goto end;
228
229         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
230         if (ret)
231                 goto end;
232
233         report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5]; 
234         if (report) {
235                 report->field[0]->value[0] = 2;
236                 usbhid_submit_report(hdev, report, USB_DIR_OUT);
237         }
238
239 end:
240         if (ret)
241                 kfree(td);
242
243         return ret;
244 }
245
246 static void egalax_remove(struct hid_device *hdev)
247 {
248         hid_hw_stop(hdev);
249         kfree(hid_get_drvdata(hdev));
250         hid_set_drvdata(hdev, NULL);
251 }
252
253 static const struct hid_device_id egalax_devices[] = {
254         { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
255                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
256         { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
257                         USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
258         { }
259 };
260 MODULE_DEVICE_TABLE(hid, egalax_devices);
261
262 static const struct hid_usage_id egalax_grabbed_usages[] = {
263         { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
264         { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
265 };
266
267 static struct hid_driver egalax_driver = {
268         .name = "egalax-touch",
269         .id_table = egalax_devices,
270         .probe = egalax_probe,
271         .remove = egalax_remove,
272         .input_mapping = egalax_input_mapping,
273         .input_mapped = egalax_input_mapped,
274         .usage_table = egalax_grabbed_usages,
275         .event = egalax_event,
276 };
277
278 static int __init egalax_init(void)
279 {
280         return hid_register_driver(&egalax_driver);
281 }
282
283 static void __exit egalax_exit(void)
284 {
285         hid_unregister_driver(&egalax_driver);
286 }
287
288 module_init(egalax_init);
289 module_exit(egalax_exit);
290