aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hid/hid-multitouch.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hid/hid-multitouch.c')
-rw-r--r--drivers/hid/hid-multitouch.c516
1 files changed, 516 insertions, 0 deletions
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
new file mode 100644
index 000000000000..07d3183fdde5
--- /dev/null
+++ b/drivers/hid/hid-multitouch.c
@@ -0,0 +1,516 @@
1/*
2 * HID driver for multitouch panels
3 *
4 * Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr>
5 * Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6 * Copyright (c) 2010-2011 Ecole Nationale de l'Aviation Civile, France
7 *
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>
20#include <linux/slab.h>
21#include <linux/usb.h>
22#include <linux/input/mt.h>
23#include "usbhid/usbhid.h"
24
25
26MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
27MODULE_DESCRIPTION("HID multitouch panels");
28MODULE_LICENSE("GPL");
29
30#include "hid-ids.h"
31
32/* quirks to control the device */
33#define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
34#define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
35#define MT_QUIRK_CYPRESS (1 << 2)
36#define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
37#define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
38#define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
39
40struct mt_slot {
41 __s32 x, y, p, w, h;
42 __s32 contactid; /* the device ContactID assigned to this slot */
43 bool touch_state; /* is the touch valid? */
44 bool seen_in_this_frame;/* has this slot been updated */
45};
46
47struct mt_device {
48 struct mt_slot curdata; /* placeholder of incoming data */
49 struct mt_class *mtclass; /* our mt device class */
50 unsigned last_field_index; /* last field index of the report */
51 unsigned last_slot_field; /* the last field of a slot */
52 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
53 __u8 num_received; /* how many contacts we received */
54 __u8 num_expected; /* expected last contact index */
55 bool curvalid; /* is the current contact valid? */
56 struct mt_slot slots[0]; /* first slot */
57};
58
59struct mt_class {
60 __s32 name; /* MT_CLS */
61 __s32 quirks;
62 __s32 sn_move; /* Signal/noise ratio for move events */
63 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
64 __u8 maxcontacts;
65};
66
67/* classes of device behavior */
68#define MT_CLS_DEFAULT 1
69#define MT_CLS_DUAL1 2
70#define MT_CLS_DUAL2 3
71#define MT_CLS_CYPRESS 4
72
73/*
74 * these device-dependent functions determine what slot corresponds
75 * to a valid contact that was just read.
76 */
77
78static int cypress_compute_slot(struct mt_device *td)
79{
80 if (td->curdata.contactid != 0 || td->num_received == 0)
81 return td->curdata.contactid;
82 else
83 return -1;
84}
85
86static int find_slot_from_contactid(struct mt_device *td)
87{
88 int i;
89 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
90 if (td->slots[i].contactid == td->curdata.contactid &&
91 td->slots[i].touch_state)
92 return i;
93 }
94 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
95 if (!td->slots[i].seen_in_this_frame &&
96 !td->slots[i].touch_state)
97 return i;
98 }
99 /* should not occurs. If this happens that means
100 * that the device sent more touches that it says
101 * in the report descriptor. It is ignored then. */
102 return -1;
103}
104
105struct mt_class mt_classes[] = {
106 { .name = MT_CLS_DEFAULT,
107 .quirks = MT_QUIRK_VALID_IS_INRANGE,
108 .maxcontacts = 10 },
109 { .name = MT_CLS_DUAL1,
110 .quirks = MT_QUIRK_VALID_IS_INRANGE |
111 MT_QUIRK_SLOT_IS_CONTACTID,
112 .maxcontacts = 2 },
113 { .name = MT_CLS_DUAL2,
114 .quirks = MT_QUIRK_VALID_IS_INRANGE |
115 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
116 .maxcontacts = 2 },
117 { .name = MT_CLS_CYPRESS,
118 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
119 MT_QUIRK_CYPRESS,
120 .maxcontacts = 10 },
121
122 { }
123};
124
125static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
126 struct hid_field *field, struct hid_usage *usage)
127{
128 if (usage->hid == HID_DG_INPUTMODE) {
129 struct mt_device *td = hid_get_drvdata(hdev);
130 td->inputmode = field->report->id;
131 }
132}
133
134static void set_abs(struct input_dev *input, unsigned int code,
135 struct hid_field *field, int snratio)
136{
137 int fmin = field->logical_minimum;
138 int fmax = field->logical_maximum;
139 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
140 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
141}
142
143static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
144 struct hid_field *field, struct hid_usage *usage,
145 unsigned long **bit, int *max)
146{
147 struct mt_device *td = hid_get_drvdata(hdev);
148 struct mt_class *cls = td->mtclass;
149 switch (usage->hid & HID_USAGE_PAGE) {
150
151 case HID_UP_GENDESK:
152 switch (usage->hid) {
153 case HID_GD_X:
154 hid_map_usage(hi, usage, bit, max,
155 EV_ABS, ABS_MT_POSITION_X);
156 set_abs(hi->input, ABS_MT_POSITION_X, field,
157 cls->sn_move);
158 /* touchscreen emulation */
159 set_abs(hi->input, ABS_X, field, cls->sn_move);
160 td->last_slot_field = usage->hid;
161 return 1;
162 case HID_GD_Y:
163 hid_map_usage(hi, usage, bit, max,
164 EV_ABS, ABS_MT_POSITION_Y);
165 set_abs(hi->input, ABS_MT_POSITION_Y, field,
166 cls->sn_move);
167 /* touchscreen emulation */
168 set_abs(hi->input, ABS_Y, field, cls->sn_move);
169 td->last_slot_field = usage->hid;
170 return 1;
171 }
172 return 0;
173
174 case HID_UP_DIGITIZER:
175 switch (usage->hid) {
176 case HID_DG_INRANGE:
177 td->last_slot_field = usage->hid;
178 return 1;
179 case HID_DG_CONFIDENCE:
180 td->last_slot_field = usage->hid;
181 return 1;
182 case HID_DG_TIPSWITCH:
183 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
184 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
185 td->last_slot_field = usage->hid;
186 return 1;
187 case HID_DG_CONTACTID:
188 input_mt_init_slots(hi->input,
189 td->mtclass->maxcontacts);
190 td->last_slot_field = usage->hid;
191 return 1;
192 case HID_DG_WIDTH:
193 hid_map_usage(hi, usage, bit, max,
194 EV_ABS, ABS_MT_TOUCH_MAJOR);
195 td->last_slot_field = usage->hid;
196 return 1;
197 case HID_DG_HEIGHT:
198 hid_map_usage(hi, usage, bit, max,
199 EV_ABS, ABS_MT_TOUCH_MINOR);
200 field->logical_maximum = 1;
201 field->logical_minimum = 0;
202 set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
203 td->last_slot_field = usage->hid;
204 return 1;
205 case HID_DG_TIPPRESSURE:
206 hid_map_usage(hi, usage, bit, max,
207 EV_ABS, ABS_MT_PRESSURE);
208 set_abs(hi->input, ABS_MT_PRESSURE, field,
209 cls->sn_pressure);
210 /* touchscreen emulation */
211 set_abs(hi->input, ABS_PRESSURE, field,
212 cls->sn_pressure);
213 td->last_slot_field = usage->hid;
214 return 1;
215 case HID_DG_CONTACTCOUNT:
216 td->last_field_index = field->report->maxfield - 1;
217 return 1;
218 case HID_DG_CONTACTMAX:
219 /* we don't set td->last_slot_field as contactcount and
220 * contact max are global to the report */
221 return -1;
222 }
223 /* let hid-input decide for the others */
224 return 0;
225
226 case 0xff000000:
227 /* we do not want to map these: no input-oriented meaning */
228 return -1;
229 }
230
231 return 0;
232}
233
234static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
235 struct hid_field *field, struct hid_usage *usage,
236 unsigned long **bit, int *max)
237{
238 if (usage->type == EV_KEY || usage->type == EV_ABS)
239 set_bit(usage->type, hi->input->evbit);
240
241 return -1;
242}
243
244static int mt_compute_slot(struct mt_device *td)
245{
246 __s32 quirks = td->mtclass->quirks;
247
248 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
249 return td->curdata.contactid;
250
251 if (quirks & MT_QUIRK_CYPRESS)
252 return cypress_compute_slot(td);
253
254 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
255 return td->num_received;
256
257 return find_slot_from_contactid(td);
258}
259
260/*
261 * this function is called when a whole contact has been processed,
262 * so that it can assign it to a slot and store the data there
263 */
264static void mt_complete_slot(struct mt_device *td)
265{
266 td->curdata.seen_in_this_frame = true;
267 if (td->curvalid) {
268 int slotnum = mt_compute_slot(td);
269
270 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
271 td->slots[slotnum] = td->curdata;
272 }
273 td->num_received++;
274}
275
276
277/*
278 * this function is called when a whole packet has been received and processed,
279 * so that it can decide what to send to the input layer.
280 */
281static void mt_emit_event(struct mt_device *td, struct input_dev *input)
282{
283 int i;
284
285 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
286 struct mt_slot *s = &(td->slots[i]);
287 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
288 !s->seen_in_this_frame) {
289 s->touch_state = false;
290 }
291
292 input_mt_slot(input, i);
293 input_mt_report_slot_state(input, MT_TOOL_FINGER,
294 s->touch_state);
295 if (s->touch_state) {
296 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
297 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
298 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
299 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
300 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
301 }
302 s->seen_in_this_frame = false;
303
304 }
305
306 input_mt_report_pointer_emulation(input, true);
307 input_sync(input);
308 td->num_received = 0;
309}
310
311
312
313static int mt_event(struct hid_device *hid, struct hid_field *field,
314 struct hid_usage *usage, __s32 value)
315{
316 struct mt_device *td = hid_get_drvdata(hid);
317 __s32 quirks = td->mtclass->quirks;
318
319 if (hid->claimed & HID_CLAIMED_INPUT) {
320 switch (usage->hid) {
321 case HID_DG_INRANGE:
322 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
323 td->curvalid = value;
324 break;
325 case HID_DG_TIPSWITCH:
326 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
327 td->curvalid = value;
328 td->curdata.touch_state = value;
329 break;
330 case HID_DG_CONFIDENCE:
331 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
332 td->curvalid = value;
333 break;
334 case HID_DG_CONTACTID:
335 td->curdata.contactid = value;
336 break;
337 case HID_DG_TIPPRESSURE:
338 td->curdata.p = value;
339 break;
340 case HID_GD_X:
341 td->curdata.x = value;
342 break;
343 case HID_GD_Y:
344 td->curdata.y = value;
345 break;
346 case HID_DG_WIDTH:
347 td->curdata.w = value;
348 break;
349 case HID_DG_HEIGHT:
350 td->curdata.h = value;
351 break;
352 case HID_DG_CONTACTCOUNT:
353 /*
354 * Includes multi-packet support where subsequent
355 * packets are sent with zero contactcount.
356 */
357 if (value)
358 td->num_expected = value;
359 break;
360
361 default:
362 /* fallback to the generic hidinput handling */
363 return 0;
364 }
365
366 if (usage->hid == td->last_slot_field)
367 mt_complete_slot(td);
368
369 if (field->index == td->last_field_index
370 && td->num_received >= td->num_expected)
371 mt_emit_event(td, field->hidinput->input);
372
373 }
374
375 /* we have handled the hidinput part, now remains hiddev */
376 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
377 hid->hiddev_hid_event(hid, field, usage, value);
378
379 return 1;
380}
381
382static void mt_set_input_mode(struct hid_device *hdev)
383{
384 struct mt_device *td = hid_get_drvdata(hdev);
385 struct hid_report *r;
386 struct hid_report_enum *re;
387
388 if (td->inputmode < 0)
389 return;
390
391 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
392 r = re->report_id_hash[td->inputmode];
393 if (r) {
394 r->field[0]->value[0] = 0x02;
395 usbhid_submit_report(hdev, r, USB_DIR_OUT);
396 }
397}
398
399static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
400{
401 int ret, i;
402 struct mt_device *td;
403 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
404
405 for (i = 0; mt_classes[i].name ; i++) {
406 if (id->driver_data == mt_classes[i].name) {
407 mtclass = &(mt_classes[i]);
408 break;
409 }
410 }
411
412 /* This allows the driver to correctly support devices
413 * that emit events over several HID messages.
414 */
415 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
416
417 td = kzalloc(sizeof(struct mt_device) +
418 mtclass->maxcontacts * sizeof(struct mt_slot),
419 GFP_KERNEL);
420 if (!td) {
421 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
422 return -ENOMEM;
423 }
424 td->mtclass = mtclass;
425 td->inputmode = -1;
426 hid_set_drvdata(hdev, td);
427
428 ret = hid_parse(hdev);
429 if (ret != 0)
430 goto fail;
431
432 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
433 if (ret)
434 goto fail;
435
436 mt_set_input_mode(hdev);
437
438 return 0;
439
440fail:
441 kfree(td);
442 return ret;
443}
444
445#ifdef CONFIG_PM
446static int mt_reset_resume(struct hid_device *hdev)
447{
448 mt_set_input_mode(hdev);
449 return 0;
450}
451#endif
452
453static void mt_remove(struct hid_device *hdev)
454{
455 struct mt_device *td = hid_get_drvdata(hdev);
456 hid_hw_stop(hdev);
457 kfree(td);
458 hid_set_drvdata(hdev, NULL);
459}
460
461static const struct hid_device_id mt_devices[] = {
462
463 /* Cypress panel */
464 { .driver_data = MT_CLS_CYPRESS,
465 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
466 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
467
468 /* GeneralTouch panel */
469 { .driver_data = MT_CLS_DUAL2,
470 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
471 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
472
473 /* PixCir-based panels */
474 { .driver_data = MT_CLS_DUAL1,
475 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
476 USB_DEVICE_ID_HANVON_MULTITOUCH) },
477 { .driver_data = MT_CLS_DUAL1,
478 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
479 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
480
481 { }
482};
483MODULE_DEVICE_TABLE(hid, mt_devices);
484
485static const struct hid_usage_id mt_grabbed_usages[] = {
486 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
487 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
488};
489
490static struct hid_driver mt_driver = {
491 .name = "hid-multitouch",
492 .id_table = mt_devices,
493 .probe = mt_probe,
494 .remove = mt_remove,
495 .input_mapping = mt_input_mapping,
496 .input_mapped = mt_input_mapped,
497 .feature_mapping = mt_feature_mapping,
498 .usage_table = mt_grabbed_usages,
499 .event = mt_event,
500#ifdef CONFIG_PM
501 .reset_resume = mt_reset_resume,
502#endif
503};
504
505static int __init mt_init(void)
506{
507 return hid_register_driver(&mt_driver);
508}
509
510static void __exit mt_exit(void)
511{
512 hid_unregister_driver(&mt_driver);
513}
514
515module_init(mt_init);
516module_exit(mt_exit);