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