blob: 39628e02a4288f290211ab8de753f8af0a67210e [file] [log] [blame]
Stephane Chattyb6353f42009-12-22 23:04:17 +01001/*
2 * HID driver for 3M PCT multitouch panels
3 *
Stephane Chatty6dec1432010-04-11 14:51:24 +02004 * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +02005 * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (c) 2010 Canonical, Ltd.
Stephane Chattyb6353f42009-12-22 23:04:17 +01007 *
8 */
9
10/*
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.
15 */
16
17#include <linux/device.h>
18#include <linux/hid.h>
19#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Stephane Chattyb6353f42009-12-22 23:04:17 +010021#include <linux/usb.h>
22
Stephane Chattyb6353f42009-12-22 23:04:17 +010023MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
24MODULE_DESCRIPTION("3M PCT multitouch panels");
25MODULE_LICENSE("GPL");
26
27#include "hid-ids.h"
28
Henrik Rydberg41035902010-09-21 16:11:44 +020029#define MAX_SLOTS 60
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020030#define MAX_TRKID USHRT_MAX
31#define MAX_EVENTS 360
32
33/* estimated signal-to-noise ratios */
34#define SN_MOVE 2048
35#define SN_WIDTH 128
Henrik Rydberg41035902010-09-21 16:11:44 +020036
Stephane Chattyb6353f42009-12-22 23:04:17 +010037struct mmm_finger {
Stephane Chatty6dec1432010-04-11 14:51:24 +020038 __s32 x, y, w, h;
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020039 __u16 id;
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020040 bool prev_touch;
Stephane Chattyb6353f42009-12-22 23:04:17 +010041 bool touch, valid;
42};
43
44struct mmm_data {
Henrik Rydberg41035902010-09-21 16:11:44 +020045 struct mmm_finger f[MAX_SLOTS];
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020046 __u16 id;
Henrik Rydberg1d958c82010-09-21 23:22:39 +020047 __u8 curid;
Henrik Rydberg41035902010-09-21 16:11:44 +020048 __u8 nexp, nreal;
Stephane Chattyb6353f42009-12-22 23:04:17 +010049 bool touch, valid;
50};
51
52static 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)
55{
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020056 int f1 = field->logical_minimum;
57 int f2 = field->logical_maximum;
58 int df = f2 - f1;
59
Stephane Chattyb6353f42009-12-22 23:04:17 +010060 switch (usage->hid & HID_USAGE_PAGE) {
61
62 case HID_UP_BUTTON:
63 return -1;
64
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);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020070 input_set_abs_params(hi->input, ABS_MT_POSITION_X,
71 f1, f2, df / SN_MOVE, 0);
Stephane Chattyb6353f42009-12-22 23:04:17 +010072 /* touchscreen emulation */
73 input_set_abs_params(hi->input, ABS_X,
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020074 f1, f2, df / SN_MOVE, 0);
Stephane Chattyb6353f42009-12-22 23:04:17 +010075 return 1;
76 case HID_GD_Y:
77 hid_map_usage(hi, usage, bit, max,
78 EV_ABS, ABS_MT_POSITION_Y);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020079 input_set_abs_params(hi->input, ABS_MT_POSITION_Y,
80 f1, f2, df / SN_MOVE, 0);
Stephane Chattyb6353f42009-12-22 23:04:17 +010081 /* touchscreen emulation */
82 input_set_abs_params(hi->input, ABS_Y,
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +020083 f1, f2, df / SN_MOVE, 0);
Stephane Chattyb6353f42009-12-22 23:04:17 +010084 return 1;
85 }
86 return 0;
87
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);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200103 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100104 return 1;
Stephane Chatty6dec1432010-04-11 14:51:24 +0200105 case HID_DG_WIDTH:
106 hid_map_usage(hi, usage, bit, max,
107 EV_ABS, ABS_MT_TOUCH_MAJOR);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200108 input_set_abs_params(hi->input, ABS_MT_TOUCH_MAJOR,
109 f1, f2, df / SN_WIDTH, 0);
Stephane Chatty6dec1432010-04-11 14:51:24 +0200110 return 1;
111 case HID_DG_HEIGHT:
112 hid_map_usage(hi, usage, bit, max,
113 EV_ABS, ABS_MT_TOUCH_MINOR);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200114 input_set_abs_params(hi->input, ABS_MT_TOUCH_MINOR,
115 f1, f2, df / SN_WIDTH, 0);
Stephane Chatty6dec1432010-04-11 14:51:24 +0200116 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
Henrik Rydberg46c4ba02010-09-21 16:16:09 +0200117 0, 1, 0, 0);
Stephane Chatty6dec1432010-04-11 14:51:24 +0200118 return 1;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100119 case HID_DG_CONTACTID:
Henrik Rydberg41035902010-09-21 16:11:44 +0200120 field->logical_maximum = MAX_TRKID;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100121 hid_map_usage(hi, usage, bit, max,
122 EV_ABS, ABS_MT_TRACKING_ID);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200123 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);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100128 return 1;
129 }
130 /* let hid-input decide for the others */
131 return 0;
132
133 case 0xff000000:
134 /* we do not want to map these: no input-oriented meaning */
135 return -1;
136 }
137
138 return 0;
139}
140
141static 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)
144{
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200145 /* tell hid-input to skip setup of these event types */
Stephane Chattyb6353f42009-12-22 23:04:17 +0100146 if (usage->type == EV_KEY || usage->type == EV_ABS)
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200147 set_bit(usage->type, hi->input->evbit);
148 return -1;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100149}
150
151/*
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.
154 */
155static void mmm_filter_event(struct mmm_data *md, struct input_dev *input)
156{
157 struct mmm_finger *oldest = 0;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100158 int i;
Henrik Rydberg41035902010-09-21 16:11:44 +0200159 for (i = 0; i < MAX_SLOTS; ++i) {
Stephane Chattyb6353f42009-12-22 23:04:17 +0100160 struct mmm_finger *f = &md->f[i];
161 if (!f->valid) {
162 /* this finger is just placeholder data, ignore */
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200163 continue;
164 }
165 input_mt_slot(input, i);
166 if (f->touch) {
Stephane Chattyb6353f42009-12-22 23:04:17 +0100167 /* this finger is on the screen */
Stephane Chatty6dec1432010-04-11 14:51:24 +0200168 int wide = (f->w > f->h);
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200169 if (!f->prev_touch)
170 f->id = md->id++;
171 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, f->id);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100172 input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x);
173 input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y);
Stephane Chatty6dec1432010-04-11 14:51:24 +0200174 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);
Henrik Rydberg1d958c82010-09-21 23:22:39 +0200179 /* touchscreen emulation: pick the oldest contact */
180 if (!oldest || ((f->id - oldest->id) & (SHRT_MAX + 1)))
Stephane Chattyb6353f42009-12-22 23:04:17 +0100181 oldest = f;
182 } else {
183 /* this finger took off the screen */
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200184 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, -1);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100185 }
Henrik Rydberg1f01a1f2010-09-21 22:12:12 +0200186 f->prev_touch = f->touch;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100187 f->valid = 0;
188 }
189
190 /* touchscreen emulation */
191 if (oldest) {
Henrik Rydberg1d958c82010-09-21 23:22:39 +0200192 input_event(input, EV_KEY, BTN_TOUCH, 1);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100193 input_event(input, EV_ABS, ABS_X, oldest->x);
194 input_event(input, EV_ABS, ABS_Y, oldest->y);
Henrik Rydberg1d958c82010-09-21 23:22:39 +0200195 } else {
Stephane Chattyb6353f42009-12-22 23:04:17 +0100196 input_event(input, EV_KEY, BTN_TOUCH, 0);
197 }
Henrik Rydberg41035902010-09-21 16:11:44 +0200198 input_sync(input);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100199}
200
201/*
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.
205 */
206static int mmm_event(struct hid_device *hid, struct hid_field *field,
207 struct hid_usage *usage, __s32 value)
208{
209 struct mmm_data *md = hid_get_drvdata(hid);
210 /*
211 * strangely, this function can be called before
212 * field->hidinput is initialized!
213 */
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;
Stephane Chatty6dec1432010-04-11 14:51:24 +0200223 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;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100231 case HID_DG_CONTACTID:
Henrik Rydberg41035902010-09-21 16:11:44 +0200232 value = clamp_val(value, 0, MAX_SLOTS - 1);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100233 if (md->valid) {
234 md->curid = value;
235 md->f[value].touch = md->touch;
236 md->f[value].valid = 1;
Henrik Rydberg41035902010-09-21 16:11:44 +0200237 md->nreal++;
Stephane Chattyb6353f42009-12-22 23:04:17 +0100238 }
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:
Henrik Rydberg41035902010-09-21 16:11:44 +0200249 if (value)
250 md->nexp = value;
251 if (md->nreal >= md->nexp) {
252 mmm_filter_event(md, input);
253 md->nreal = 0;
254 }
Stephane Chattyb6353f42009-12-22 23:04:17 +0100255 break;
256 }
257 }
258
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);
262
263 return 1;
264}
265
266static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id)
267{
268 int ret;
269 struct mmm_data *md;
270
Henrik Rydberg41035902010-09-21 16:11:44 +0200271 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
272
Stephane Chattyb6353f42009-12-22 23:04:17 +0100273 md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL);
274 if (!md) {
275 dev_err(&hdev->dev, "cannot allocate 3M data\n");
276 return -ENOMEM;
277 }
278 hid_set_drvdata(hdev, md);
279
280 ret = hid_parse(hdev);
281 if (!ret)
282 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
283
284 if (ret)
285 kfree(md);
286 return ret;
287}
288
289static void mmm_remove(struct hid_device *hdev)
290{
291 hid_hw_stop(hdev);
292 kfree(hid_get_drvdata(hdev));
293 hid_set_drvdata(hdev, NULL);
294}
295
296static const struct hid_device_id mmm_devices[] = {
297 { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) },
Stephane Chatty6dec1432010-04-11 14:51:24 +0200298 { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) },
Stephane Chattyb6353f42009-12-22 23:04:17 +0100299 { }
300};
301MODULE_DEVICE_TABLE(hid, mmm_devices);
302
303static 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}
306};
307
308static 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,
317};
318
319static int __init mmm_init(void)
320{
321 return hid_register_driver(&mmm_driver);
322}
323
324static void __exit mmm_exit(void)
325{
326 hid_unregister_driver(&mmm_driver);
327}
328
329module_init(mmm_init);
330module_exit(mmm_exit);
Stephane Chattyb6353f42009-12-22 23:04:17 +0100331