Merge branch 'for-linus' of git://git.open-osd.org/linux-open-osd
[pandora-kernel.git] / drivers / hid / hid-stantum.c
1 /*
2  *  HID driver for Stantum multitouch panels
3  *
4  *  Copyright (c) 2009 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
19 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
20 MODULE_DESCRIPTION("Stantum HID multitouch panels");
21 MODULE_LICENSE("GPL");
22
23 #include "hid-ids.h"
24
25 struct stantum_data {
26         __s32 x, y, z, w, h;    /* x, y, pressure, width, height */
27         __u16 id;               /* touch id */
28         bool valid;             /* valid finger data, or just placeholder? */
29         bool first;             /* first finger in the HID packet? */
30         bool activity;          /* at least one active finger so far? */
31 };
32
33 static int stantum_input_mapping(struct hid_device *hdev, struct hid_input *hi,
34                 struct hid_field *field, struct hid_usage *usage,
35                 unsigned long **bit, int *max)
36 {
37         switch (usage->hid & HID_USAGE_PAGE) {
38
39         case HID_UP_GENDESK:
40                 switch (usage->hid) {
41                 case HID_GD_X:
42                         hid_map_usage(hi, usage, bit, max,
43                                         EV_ABS, ABS_MT_POSITION_X);
44                         /* touchscreen emulation */
45                         input_set_abs_params(hi->input, ABS_X,
46                                                 field->logical_minimum,
47                                                 field->logical_maximum, 0, 0);
48                         return 1;
49                 case HID_GD_Y:
50                         hid_map_usage(hi, usage, bit, max,
51                                         EV_ABS, ABS_MT_POSITION_Y);
52                         /* touchscreen emulation */
53                         input_set_abs_params(hi->input, ABS_Y,
54                                                 field->logical_minimum,
55                                                 field->logical_maximum, 0, 0);
56                         return 1;
57                 }
58                 return 0;
59
60         case HID_UP_DIGITIZER:
61                 switch (usage->hid) {
62                 case HID_DG_INRANGE:
63                 case HID_DG_CONFIDENCE:
64                 case HID_DG_INPUTMODE:
65                 case HID_DG_DEVICEINDEX:
66                 case HID_DG_CONTACTCOUNT:
67                 case HID_DG_CONTACTMAX:
68                         return -1;
69
70                 case HID_DG_TIPSWITCH:
71                         /* touchscreen emulation */
72                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
73                         return 1;
74
75                 case HID_DG_WIDTH:
76                         hid_map_usage(hi, usage, bit, max,
77                                         EV_ABS, ABS_MT_TOUCH_MAJOR);
78                         return 1;
79                 case HID_DG_HEIGHT:
80                         hid_map_usage(hi, usage, bit, max,
81                                         EV_ABS, ABS_MT_TOUCH_MINOR);
82                         input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
83                                         1, 1, 0, 0);
84                         return 1;
85                 case HID_DG_TIPPRESSURE:
86                         hid_map_usage(hi, usage, bit, max,
87                                         EV_ABS, ABS_MT_PRESSURE);
88                         return 1;
89
90                 case HID_DG_CONTACTID:
91                         hid_map_usage(hi, usage, bit, max,
92                                         EV_ABS, ABS_MT_TRACKING_ID);
93                         return 1;
94
95                 }
96                 return 0;
97
98         case 0xff000000:
99                 /* no input-oriented meaning */
100                 return -1;
101         }
102
103         return 0;
104 }
105
106 static int stantum_input_mapped(struct hid_device *hdev, struct hid_input *hi,
107                 struct hid_field *field, struct hid_usage *usage,
108                 unsigned long **bit, int *max)
109 {
110         if (usage->type == EV_KEY || usage->type == EV_ABS)
111                 clear_bit(usage->code, *bit);
112
113         return 0;
114 }
115
116 /*
117  * this function is called when a whole finger has been parsed,
118  * so that it can decide what to send to the input layer.
119  */
120 static void stantum_filter_event(struct stantum_data *sd,
121                                         struct input_dev *input)
122 {
123         bool wide;
124
125         if (!sd->valid) {
126                 /*
127                  * touchscreen emulation: if the first finger is not valid and
128                  * there previously was finger activity, this is a release
129                  */
130                 if (sd->first && sd->activity) {
131                         input_event(input, EV_KEY, BTN_TOUCH, 0);
132                         sd->activity = false;
133                 }
134                 return;
135         }
136
137         input_event(input, EV_ABS, ABS_MT_TRACKING_ID, sd->id);
138         input_event(input, EV_ABS, ABS_MT_POSITION_X, sd->x);
139         input_event(input, EV_ABS, ABS_MT_POSITION_Y, sd->y);
140
141         wide = (sd->w > sd->h);
142         input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
143         input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, wide ? sd->w : sd->h);
144         input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, wide ? sd->h : sd->w);
145
146         input_event(input, EV_ABS, ABS_MT_PRESSURE, sd->z);
147
148         input_mt_sync(input);
149         sd->valid = false;
150
151         /* touchscreen emulation */
152         if (sd->first) {
153                 if (!sd->activity) {
154                         input_event(input, EV_KEY, BTN_TOUCH, 1);
155                         sd->activity = true;
156                 }
157                 input_event(input, EV_ABS, ABS_X, sd->x);
158                 input_event(input, EV_ABS, ABS_Y, sd->y);
159         }
160         sd->first = false;
161 }
162
163
164 static int stantum_event(struct hid_device *hid, struct hid_field *field,
165                                 struct hid_usage *usage, __s32 value)
166 {
167         struct stantum_data *sd = hid_get_drvdata(hid);
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                         /* this is the last field in a finger */
175                         stantum_filter_event(sd, input);
176                         break;
177                 case HID_DG_WIDTH:
178                         sd->w = value;
179                         break;
180                 case HID_DG_HEIGHT:
181                         sd->h = value;
182                         break;
183                 case HID_GD_X:
184                         sd->x = value;
185                         break;
186                 case HID_GD_Y:
187                         sd->y = value;
188                         break;
189                 case HID_DG_TIPPRESSURE:
190                         sd->z = value;
191                         break;
192                 case HID_DG_CONTACTID:
193                         sd->id = value;
194                         break;
195                 case HID_DG_CONFIDENCE:
196                         sd->valid = !!value;
197                         break;
198                 case 0xff000002:
199                         /* this comes only before the first finger */
200                         sd->first = true;
201                         break;
202
203                 default:
204                         /* ignore the others */
205                         return 1;
206                 }
207         }
208
209         /* we have handled the hidinput part, now remains hiddev */
210         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
211                 hid->hiddev_hid_event(hid, field, usage, value);
212
213         return 1;
214 }
215
216 static int stantum_probe(struct hid_device *hdev,
217                                 const struct hid_device_id *id)
218 {
219         int ret;
220         struct stantum_data *sd;
221
222         sd = kmalloc(sizeof(struct stantum_data), GFP_KERNEL);
223         if (!sd) {
224                 dev_err(&hdev->dev, "cannot allocate Stantum data\n");
225                 return -ENOMEM;
226         }
227         sd->valid = false;
228         sd->first = false;
229         sd->activity = false;
230         hid_set_drvdata(hdev, sd);
231
232         ret = hid_parse(hdev);
233         if (!ret)
234                 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
235
236         if (ret)
237                 kfree(sd);
238
239         return ret;
240 }
241
242 static void stantum_remove(struct hid_device *hdev)
243 {
244         hid_hw_stop(hdev);
245         kfree(hid_get_drvdata(hdev));
246         hid_set_drvdata(hdev, NULL);
247 }
248
249 static const struct hid_device_id stantum_devices[] = {
250         { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) },
251         { }
252 };
253 MODULE_DEVICE_TABLE(hid, stantum_devices);
254
255 static const struct hid_usage_id stantum_grabbed_usages[] = {
256         { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
257         { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
258 };
259
260 static struct hid_driver stantum_driver = {
261         .name = "stantum",
262         .id_table = stantum_devices,
263         .probe = stantum_probe,
264         .remove = stantum_remove,
265         .input_mapping = stantum_input_mapping,
266         .input_mapped = stantum_input_mapped,
267         .usage_table = stantum_grabbed_usages,
268         .event = stantum_event,
269 };
270
271 static int __init stantum_init(void)
272 {
273         return hid_register_driver(&stantum_driver);
274 }
275
276 static void __exit stantum_exit(void)
277 {
278         hid_unregister_driver(&stantum_driver);
279 }
280
281 module_init(stantum_init);
282 module_exit(stantum_exit);
283