Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / drivers / hid / hid-mosart.c
1 /*
2  *  HID driver for the multitouch panel on the ASUS EeePC T91MT
3  *
4  *  Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
5  *  Copyright (c) 2010 Teemu Tuominen <teemu.tuominen@cybercom.com>
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 #include <linux/usb.h>
20 #include "usbhid/usbhid.h"
21
22 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
23 MODULE_DESCRIPTION("MosArt dual-touch panel");
24 MODULE_LICENSE("GPL");
25
26 #include "hid-ids.h"
27
28 struct mosart_data {
29         __u16 x, y;
30         __u8 id;
31         bool valid;             /* valid finger data, or just placeholder? */
32         bool first;             /* is this the first finger in this frame? */
33         bool activity_now;      /* at least one active finger in this frame? */
34         bool activity;          /* at least one active finger previously? */
35 };
36
37 static int mosart_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_CONFIDENCE:
67                 case HID_DG_TIPSWITCH:
68                 case HID_DG_INPUTMODE:
69                 case HID_DG_DEVICEINDEX:
70                 case HID_DG_CONTACTCOUNT:
71                 case HID_DG_CONTACTMAX:
72                 case HID_DG_TIPPRESSURE:
73                 case HID_DG_WIDTH:
74                 case HID_DG_HEIGHT:
75                         return -1;
76                 case HID_DG_INRANGE:
77                         /* touchscreen emulation */
78                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
79                         return 1;
80
81                 case HID_DG_CONTACTID:
82                         hid_map_usage(hi, usage, bit, max,
83                                         EV_ABS, ABS_MT_TRACKING_ID);
84                         return 1;
85
86                 }
87                 return 0;
88
89         case 0xff000000:
90                 /* ignore HID features */
91                 return -1;
92         }
93
94         return 0;
95 }
96
97 static int mosart_input_mapped(struct hid_device *hdev, struct hid_input *hi,
98                 struct hid_field *field, struct hid_usage *usage,
99                 unsigned long **bit, int *max)
100 {
101         if (usage->type == EV_KEY || usage->type == EV_ABS)
102                 clear_bit(usage->code, *bit);
103
104         return 0;
105 }
106
107 /*
108  * this function is called when a whole finger has been parsed,
109  * so that it can decide what to send to the input layer.
110  */
111 static void mosart_filter_event(struct mosart_data *td, struct input_dev *input)
112 {
113         td->first = !td->first; /* touchscreen emulation */
114
115         if (!td->valid) {
116                 /*
117                  * touchscreen emulation: if no finger in this frame is valid
118                  * and there previously was finger activity, this is a release
119                  */ 
120                 if (!td->first && !td->activity_now && td->activity) {
121                         input_event(input, EV_KEY, BTN_TOUCH, 0);
122                         td->activity = false;
123                 }
124                 return;
125         }
126
127         input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
128         input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
129         input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
130
131         input_mt_sync(input);
132         td->valid = false;
133
134         /* touchscreen emulation: if first active finger in this frame... */
135         if (!td->activity_now) {
136                 /* if there was no previous activity, emit touch event */
137                 if (!td->activity) {
138                         input_event(input, EV_KEY, BTN_TOUCH, 1);
139                         td->activity = true;
140                 }
141                 td->activity_now = true;
142                 /* and in any case this is our preferred finger */
143                 input_event(input, EV_ABS, ABS_X, td->x);
144                 input_event(input, EV_ABS, ABS_Y, td->y);
145         }
146 }
147
148
149 static int mosart_event(struct hid_device *hid, struct hid_field *field,
150                                 struct hid_usage *usage, __s32 value)
151 {
152         struct mosart_data *td = hid_get_drvdata(hid);
153
154         if (hid->claimed & HID_CLAIMED_INPUT) {
155                 struct input_dev *input = field->hidinput->input;
156                 switch (usage->hid) {
157                 case HID_DG_INRANGE:
158                         td->valid = !!value;
159                         break;
160                 case HID_GD_X:
161                         td->x = value;
162                         break;
163                 case HID_GD_Y:
164                         td->y = value;
165                         mosart_filter_event(td, input);
166                         break;
167                 case HID_DG_CONTACTID:
168                         td->id = value;
169                         break;
170                 case HID_DG_CONTACTCOUNT:
171                         /* touch emulation: this is the last field in a frame */
172                         td->first = false;
173                         td->activity_now = false;
174                         break;
175                 case HID_DG_CONFIDENCE:
176                 case HID_DG_TIPSWITCH:
177                         /* avoid interference from generic hidinput handling */
178                         break;
179
180                 default:
181                         /* fallback to the generic hidinput handling */
182                         return 0;
183                 }
184         }
185
186         /* we have handled the hidinput part, now remains hiddev */
187         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
188                 hid->hiddev_hid_event(hid, field, usage, value);
189
190         return 1;
191 }
192
193 static int mosart_probe(struct hid_device *hdev, const struct hid_device_id *id)
194 {
195         int ret;
196         struct mosart_data *td;
197
198
199         td = kmalloc(sizeof(struct mosart_data), GFP_KERNEL);
200         if (!td) {
201                 dev_err(&hdev->dev, "cannot allocate MosArt data\n");
202                 return -ENOMEM;
203         }
204         td->valid = false;
205         td->activity = false;
206         td->activity_now = false;
207         td->first = false;
208         hid_set_drvdata(hdev, td);
209
210         /* currently, it's better to have one evdev device only */
211 #if 0
212         hdev->quirks |= HID_QUIRK_MULTI_INPUT;
213 #endif
214
215         ret = hid_parse(hdev);
216         if (ret == 0)
217                 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
218
219         if (ret == 0) {
220                 struct hid_report_enum *re = hdev->report_enum
221                                                 + HID_FEATURE_REPORT;
222                 struct hid_report *r = re->report_id_hash[7];
223
224                 r->field[0]->value[0] = 0x02;
225                 usbhid_submit_report(hdev, r, USB_DIR_OUT);
226         } else 
227                 kfree(td);
228
229         return ret;
230 }
231
232 static void mosart_remove(struct hid_device *hdev)
233 {
234         hid_hw_stop(hdev);
235         kfree(hid_get_drvdata(hdev));
236         hid_set_drvdata(hdev, NULL);
237 }
238
239 static const struct hid_device_id mosart_devices[] = {
240         { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_T91MT) },
241         { }
242 };
243 MODULE_DEVICE_TABLE(hid, mosart_devices);
244
245 static const struct hid_usage_id mosart_grabbed_usages[] = {
246         { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
247         { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
248 };
249
250 static struct hid_driver mosart_driver = {
251         .name = "mosart",
252         .id_table = mosart_devices,
253         .probe = mosart_probe,
254         .remove = mosart_remove,
255         .input_mapping = mosart_input_mapping,
256         .input_mapped = mosart_input_mapped,
257         .usage_table = mosart_grabbed_usages,
258         .event = mosart_event,
259 };
260
261 static int __init mosart_init(void)
262 {
263         return hid_register_driver(&mosart_driver);
264 }
265
266 static void __exit mosart_exit(void)
267 {
268         hid_unregister_driver(&mosart_driver);
269 }
270
271 module_init(mosart_init);
272 module_exit(mosart_exit);
273