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