]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/hid/hid-3m-pct.c
Merge branch 'x86-uv-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6.git] / drivers / hid / hid-3m-pct.c
1 /*
2  *  HID driver for 3M PCT 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 #include <linux/slab.h>
19 #include <linux/usb.h>
20
21 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
22 MODULE_DESCRIPTION("3M PCT multitouch panels");
23 MODULE_LICENSE("GPL");
24
25 #include "hid-ids.h"
26
27 struct mmm_finger {
28         __s32 x, y;
29         __u8 rank;
30         bool touch, valid;
31 };
32
33 struct mmm_data {
34         struct mmm_finger f[10];
35         __u8 curid, num;
36         bool touch, valid;
37 };
38
39 static int mmm_input_mapping(struct hid_device *hdev, struct hid_input *hi,
40                 struct hid_field *field, struct hid_usage *usage,
41                 unsigned long **bit, int *max)
42 {
43         switch (usage->hid & HID_USAGE_PAGE) {
44
45         case HID_UP_BUTTON:
46                 return -1;
47
48         case HID_UP_GENDESK:
49                 switch (usage->hid) {
50                 case HID_GD_X:
51                         hid_map_usage(hi, usage, bit, max,
52                                         EV_ABS, ABS_MT_POSITION_X);
53                         /* touchscreen emulation */
54                         input_set_abs_params(hi->input, ABS_X,
55                                                 field->logical_minimum,
56                                                 field->logical_maximum, 0, 0);
57                         return 1;
58                 case HID_GD_Y:
59                         hid_map_usage(hi, usage, bit, max,
60                                         EV_ABS, ABS_MT_POSITION_Y);
61                         /* touchscreen emulation */
62                         input_set_abs_params(hi->input, ABS_Y,
63                                                 field->logical_minimum,
64                                                 field->logical_maximum, 0, 0);
65                         return 1;
66                 }
67                 return 0;
68
69         case HID_UP_DIGITIZER:
70                 switch (usage->hid) {
71                 /* we do not want to map these: no input-oriented meaning */
72                 case 0x14:
73                 case 0x23:
74                 case HID_DG_INPUTMODE:
75                 case HID_DG_DEVICEINDEX:
76                 case HID_DG_CONTACTCOUNT:
77                 case HID_DG_CONTACTMAX:
78                 case HID_DG_INRANGE:
79                 case HID_DG_CONFIDENCE:
80                         return -1;
81                 case HID_DG_TIPSWITCH:
82                         /* touchscreen emulation */
83                         hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
84                         return 1;
85                 case HID_DG_CONTACTID:
86                         hid_map_usage(hi, usage, bit, max,
87                                         EV_ABS, ABS_MT_TRACKING_ID);
88                         return 1;
89                 }
90                 /* let hid-input decide for the others */
91                 return 0;
92
93         case 0xff000000:
94                 /* we do not want to map these: no input-oriented meaning */
95                 return -1;
96         }
97
98         return 0;
99 }
100
101 static int mmm_input_mapped(struct hid_device *hdev, struct hid_input *hi,
102                 struct hid_field *field, struct hid_usage *usage,
103                 unsigned long **bit, int *max)
104 {
105         if (usage->type == EV_KEY || usage->type == EV_ABS)
106                 clear_bit(usage->code, *bit);
107
108         return 0;
109 }
110
111 /*
112  * this function is called when a whole packet has been received and processed,
113  * so that it can decide what to send to the input layer.
114  */
115 static void mmm_filter_event(struct mmm_data *md, struct input_dev *input)
116 {
117         struct mmm_finger *oldest = 0;
118         bool pressed = false, released = false;
119         int i;
120
121         /*
122          * we need to iterate on all fingers to decide if we have a press
123          * or a release event in our touchscreen emulation.
124          */
125         for (i = 0; i < 10; ++i) {
126                 struct mmm_finger *f = &md->f[i];
127                 if (!f->valid) {
128                         /* this finger is just placeholder data, ignore */
129                 } else if (f->touch) {
130                         /* this finger is on the screen */
131                         input_event(input, EV_ABS, ABS_MT_TRACKING_ID, i);
132                         input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x);
133                         input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y);
134                         input_mt_sync(input);
135                         /*
136                          * touchscreen emulation: maintain the age rank
137                          * of this finger, decide if we have a press
138                          */
139                         if (f->rank == 0) {
140                                 f->rank = ++(md->num);
141                                 if (f->rank == 1)
142                                         pressed = true;
143                         }
144                         if (f->rank == 1)
145                                 oldest = f;
146                 } else {
147                         /* this finger took off the screen */
148                         /* touchscreen emulation: maintain age rank of others */
149                         int j;
150
151                         for (j = 0; j < 10; ++j) {
152                                 struct mmm_finger *g = &md->f[j];
153                                 if (g->rank > f->rank) {
154                                         g->rank--;
155                                         if (g->rank == 1)
156                                                 oldest = g;
157                                 }
158                         }
159                         f->rank = 0;
160                         --(md->num);
161                         if (md->num == 0)
162                                 released = true;
163                 }
164                 f->valid = 0;
165         }
166
167         /* touchscreen emulation */
168         if (oldest) {
169                 if (pressed)
170                         input_event(input, EV_KEY, BTN_TOUCH, 1);
171                 input_event(input, EV_ABS, ABS_X, oldest->x);
172                 input_event(input, EV_ABS, ABS_Y, oldest->y);
173         } else if (released) {
174                 input_event(input, EV_KEY, BTN_TOUCH, 0);
175         }
176 }
177
178 /*
179  * this function is called upon all reports
180  * so that we can accumulate contact point information,
181  * and call input_mt_sync after each point.
182  */
183 static int mmm_event(struct hid_device *hid, struct hid_field *field,
184                                 struct hid_usage *usage, __s32 value)
185 {
186         struct mmm_data *md = hid_get_drvdata(hid);
187         /*
188          * strangely, this function can be called before
189          * field->hidinput is initialized!
190          */
191         if (hid->claimed & HID_CLAIMED_INPUT) {
192                 struct input_dev *input = field->hidinput->input;
193                 switch (usage->hid) {
194                 case HID_DG_TIPSWITCH:
195                         md->touch = value;
196                         break;
197                 case HID_DG_CONFIDENCE:
198                         md->valid = value;
199                         break;
200                 case HID_DG_CONTACTID:
201                         if (md->valid) {
202                                 md->curid = value;
203                                 md->f[value].touch = md->touch;
204                                 md->f[value].valid = 1;
205                         }
206                         break;
207                 case HID_GD_X:
208                         if (md->valid)
209                                 md->f[md->curid].x = value;
210                         break;
211                 case HID_GD_Y:
212                         if (md->valid)
213                                 md->f[md->curid].y = value;
214                         break;
215                 case HID_DG_CONTACTCOUNT:
216                         mmm_filter_event(md, input);
217                         break;
218                 }
219         }
220
221         /* we have handled the hidinput part, now remains hiddev */
222         if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
223                 hid->hiddev_hid_event(hid, field, usage, value);
224
225         return 1;
226 }
227
228 static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id)
229 {
230         int ret;
231         struct mmm_data *md;
232
233         md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL);
234         if (!md) {
235                 dev_err(&hdev->dev, "cannot allocate 3M data\n");
236                 return -ENOMEM;
237         }
238         hid_set_drvdata(hdev, md);
239
240         ret = hid_parse(hdev);
241         if (!ret)
242                 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
243
244         if (ret)
245                 kfree(md);
246         return ret;
247 }
248
249 static void mmm_remove(struct hid_device *hdev)
250 {
251         hid_hw_stop(hdev);
252         kfree(hid_get_drvdata(hdev));
253         hid_set_drvdata(hdev, NULL);
254 }
255
256 static const struct hid_device_id mmm_devices[] = {
257         { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
258         { }
259 };
260 MODULE_DEVICE_TABLE(hid, mmm_devices);
261
262 static const struct hid_usage_id mmm_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 mmm_driver = {
268         .name = "3m-pct",
269         .id_table = mmm_devices,
270         .probe = mmm_probe,
271         .remove = mmm_remove,
272         .input_mapping = mmm_input_mapping,
273         .input_mapped = mmm_input_mapped,
274         .usage_table = mmm_grabbed_usages,
275         .event = mmm_event,
276 };
277
278 static int __init mmm_init(void)
279 {
280         return hid_register_driver(&mmm_driver);
281 }
282
283 static void __exit mmm_exit(void)
284 {
285         hid_unregister_driver(&mmm_driver);
286 }
287
288 module_init(mmm_init);
289 module_exit(mmm_exit);
290 MODULE_LICENSE("GPL");
291