HID: 3m: Correct touchscreen emulation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-3m-pct.c
blob39628e02a4288f290211ab8de753f8af0a67210e
1 /*
2 * HID driver for 3M PCT multitouch panels
4 * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
5 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (c) 2010 Canonical, Ltd.
8 */
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
17 #include <linux/device.h>
18 #include <linux/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/usb.h>
23 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
24 MODULE_DESCRIPTION("3M PCT multitouch panels");
25 MODULE_LICENSE("GPL");
27 #include "hid-ids.h"
29 #define MAX_SLOTS 60
30 #define MAX_TRKID USHRT_MAX
31 #define MAX_EVENTS 360
33 /* estimated signal-to-noise ratios */
34 #define SN_MOVE 2048
35 #define SN_WIDTH 128
37 struct mmm_finger {
38 __s32 x, y, w, h;
39 __u16 id;
40 bool prev_touch;
41 bool touch, valid;
44 struct mmm_data {
45 struct mmm_finger f[MAX_SLOTS];
46 __u16 id;
47 __u8 curid;
48 __u8 nexp, nreal;
49 bool touch, valid;
52 static int mmm_input_mapping(struct hid_device *hdev, struct hid_input *hi,
53 struct hid_field *field, struct hid_usage *usage,
54 unsigned long **bit, int *max)
56 int f1 = field->logical_minimum;
57 int f2 = field->logical_maximum;
58 int df = f2 - f1;
60 switch (usage->hid & HID_USAGE_PAGE) {
62 case HID_UP_BUTTON:
63 return -1;
65 case HID_UP_GENDESK:
66 switch (usage->hid) {
67 case HID_GD_X:
68 hid_map_usage(hi, usage, bit, max,
69 EV_ABS, ABS_MT_POSITION_X);
70 input_set_abs_params(hi->input, ABS_MT_POSITION_X,
71 f1, f2, df / SN_MOVE, 0);
72 /* touchscreen emulation */
73 input_set_abs_params(hi->input, ABS_X,
74 f1, f2, df / SN_MOVE, 0);
75 return 1;
76 case HID_GD_Y:
77 hid_map_usage(hi, usage, bit, max,
78 EV_ABS, ABS_MT_POSITION_Y);
79 input_set_abs_params(hi->input, ABS_MT_POSITION_Y,
80 f1, f2, df / SN_MOVE, 0);
81 /* touchscreen emulation */
82 input_set_abs_params(hi->input, ABS_Y,
83 f1, f2, df / SN_MOVE, 0);
84 return 1;
86 return 0;
88 case HID_UP_DIGITIZER:
89 switch (usage->hid) {
90 /* we do not want to map these: no input-oriented meaning */
91 case 0x14:
92 case 0x23:
93 case HID_DG_INPUTMODE:
94 case HID_DG_DEVICEINDEX:
95 case HID_DG_CONTACTCOUNT:
96 case HID_DG_CONTACTMAX:
97 case HID_DG_INRANGE:
98 case HID_DG_CONFIDENCE:
99 return -1;
100 case HID_DG_TIPSWITCH:
101 /* touchscreen emulation */
102 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
103 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
104 return 1;
105 case HID_DG_WIDTH:
106 hid_map_usage(hi, usage, bit, max,
107 EV_ABS, ABS_MT_TOUCH_MAJOR);
108 input_set_abs_params(hi->input, ABS_MT_TOUCH_MAJOR,
109 f1, f2, df / SN_WIDTH, 0);
110 return 1;
111 case HID_DG_HEIGHT:
112 hid_map_usage(hi, usage, bit, max,
113 EV_ABS, ABS_MT_TOUCH_MINOR);
114 input_set_abs_params(hi->input, ABS_MT_TOUCH_MINOR,
115 f1, f2, df / SN_WIDTH, 0);
116 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
117 0, 1, 0, 0);
118 return 1;
119 case HID_DG_CONTACTID:
120 field->logical_maximum = MAX_TRKID;
121 hid_map_usage(hi, usage, bit, max,
122 EV_ABS, ABS_MT_TRACKING_ID);
123 input_set_abs_params(hi->input, ABS_MT_TRACKING_ID,
124 0, MAX_TRKID, 0, 0);
125 if (!hi->input->mt)
126 input_mt_create_slots(hi->input, MAX_SLOTS);
127 input_set_events_per_packet(hi->input, MAX_EVENTS);
128 return 1;
130 /* let hid-input decide for the others */
131 return 0;
133 case 0xff000000:
134 /* we do not want to map these: no input-oriented meaning */
135 return -1;
138 return 0;
141 static int mmm_input_mapped(struct hid_device *hdev, struct hid_input *hi,
142 struct hid_field *field, struct hid_usage *usage,
143 unsigned long **bit, int *max)
145 /* tell hid-input to skip setup of these event types */
146 if (usage->type == EV_KEY || usage->type == EV_ABS)
147 set_bit(usage->type, hi->input->evbit);
148 return -1;
152 * this function is called when a whole packet has been received and processed,
153 * so that it can decide what to send to the input layer.
155 static void mmm_filter_event(struct mmm_data *md, struct input_dev *input)
157 struct mmm_finger *oldest = 0;
158 int i;
159 for (i = 0; i < MAX_SLOTS; ++i) {
160 struct mmm_finger *f = &md->f[i];
161 if (!f->valid) {
162 /* this finger is just placeholder data, ignore */
163 continue;
165 input_mt_slot(input, i);
166 if (f->touch) {
167 /* this finger is on the screen */
168 int wide = (f->w > f->h);
169 if (!f->prev_touch)
170 f->id = md->id++;
171 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, f->id);
172 input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x);
173 input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y);
174 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
175 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR,
176 wide ? f->w : f->h);
177 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR,
178 wide ? f->h : f->w);
179 /* touchscreen emulation: pick the oldest contact */
180 if (!oldest || ((f->id - oldest->id) & (SHRT_MAX + 1)))
181 oldest = f;
182 } else {
183 /* this finger took off the screen */
184 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, -1);
186 f->prev_touch = f->touch;
187 f->valid = 0;
190 /* touchscreen emulation */
191 if (oldest) {
192 input_event(input, EV_KEY, BTN_TOUCH, 1);
193 input_event(input, EV_ABS, ABS_X, oldest->x);
194 input_event(input, EV_ABS, ABS_Y, oldest->y);
195 } else {
196 input_event(input, EV_KEY, BTN_TOUCH, 0);
198 input_sync(input);
202 * this function is called upon all reports
203 * so that we can accumulate contact point information,
204 * and call input_mt_sync after each point.
206 static int mmm_event(struct hid_device *hid, struct hid_field *field,
207 struct hid_usage *usage, __s32 value)
209 struct mmm_data *md = hid_get_drvdata(hid);
211 * strangely, this function can be called before
212 * field->hidinput is initialized!
214 if (hid->claimed & HID_CLAIMED_INPUT) {
215 struct input_dev *input = field->hidinput->input;
216 switch (usage->hid) {
217 case HID_DG_TIPSWITCH:
218 md->touch = value;
219 break;
220 case HID_DG_CONFIDENCE:
221 md->valid = value;
222 break;
223 case HID_DG_WIDTH:
224 if (md->valid)
225 md->f[md->curid].w = value;
226 break;
227 case HID_DG_HEIGHT:
228 if (md->valid)
229 md->f[md->curid].h = value;
230 break;
231 case HID_DG_CONTACTID:
232 value = clamp_val(value, 0, MAX_SLOTS - 1);
233 if (md->valid) {
234 md->curid = value;
235 md->f[value].touch = md->touch;
236 md->f[value].valid = 1;
237 md->nreal++;
239 break;
240 case HID_GD_X:
241 if (md->valid)
242 md->f[md->curid].x = value;
243 break;
244 case HID_GD_Y:
245 if (md->valid)
246 md->f[md->curid].y = value;
247 break;
248 case HID_DG_CONTACTCOUNT:
249 if (value)
250 md->nexp = value;
251 if (md->nreal >= md->nexp) {
252 mmm_filter_event(md, input);
253 md->nreal = 0;
255 break;
259 /* we have handled the hidinput part, now remains hiddev */
260 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
261 hid->hiddev_hid_event(hid, field, usage, value);
263 return 1;
266 static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id)
268 int ret;
269 struct mmm_data *md;
271 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
273 md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL);
274 if (!md) {
275 dev_err(&hdev->dev, "cannot allocate 3M data\n");
276 return -ENOMEM;
278 hid_set_drvdata(hdev, md);
280 ret = hid_parse(hdev);
281 if (!ret)
282 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
284 if (ret)
285 kfree(md);
286 return ret;
289 static void mmm_remove(struct hid_device *hdev)
291 hid_hw_stop(hdev);
292 kfree(hid_get_drvdata(hdev));
293 hid_set_drvdata(hdev, NULL);
296 static const struct hid_device_id mmm_devices[] = {
297 { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
298 { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) },
301 MODULE_DEVICE_TABLE(hid, mmm_devices);
303 static const struct hid_usage_id mmm_grabbed_usages[] = {
304 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
305 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
308 static struct hid_driver mmm_driver = {
309 .name = "3m-pct",
310 .id_table = mmm_devices,
311 .probe = mmm_probe,
312 .remove = mmm_remove,
313 .input_mapping = mmm_input_mapping,
314 .input_mapped = mmm_input_mapped,
315 .usage_table = mmm_grabbed_usages,
316 .event = mmm_event,
319 static int __init mmm_init(void)
321 return hid_register_driver(&mmm_driver);
324 static void __exit mmm_exit(void)
326 hid_unregister_driver(&mmm_driver);
329 module_init(mmm_init);
330 module_exit(mmm_exit);