blob: 69f8744deac8cede73ca2af5b443445cfeaf82c0 [file] [log] [blame]
Benjamin Tissoires5519cab2011-01-07 23:45:50 +01001/*
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>");
Benjamin Tissoiresef2fafb2011-01-31 11:28:21 +010027MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010028MODULE_DESCRIPTION("HID multitouch panels");
29MODULE_LICENSE("GPL");
30
31#include "hid-ids.h"
32
33/* quirks to control the device */
34#define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
35#define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
Benjamin Tissoires2d936662011-01-11 16:45:54 +010036#define MT_QUIRK_CYPRESS (1 << 2)
Benjamin Tissoires5572da02011-01-07 23:47:27 +010037#define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
Benjamin Tissoires2d936662011-01-11 16:45:54 +010038#define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
39#define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010040
41struct mt_slot {
42 __s32 x, y, p, w, h;
43 __s32 contactid; /* the device ContactID assigned to this slot */
44 bool touch_state; /* is the touch valid? */
45 bool seen_in_this_frame;/* has this slot been updated */
46};
47
48struct mt_device {
49 struct mt_slot curdata; /* placeholder of incoming data */
50 struct mt_class *mtclass; /* our mt device class */
51 unsigned last_field_index; /* last field index of the report */
52 unsigned last_slot_field; /* the last field of a slot */
53 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
54 __u8 num_received; /* how many contacts we received */
55 __u8 num_expected; /* expected last contact index */
56 bool curvalid; /* is the current contact valid? */
57 struct mt_slot slots[0]; /* first slot */
58};
59
60struct mt_class {
Benjamin Tissoires2d936662011-01-11 16:45:54 +010061 __s32 name; /* MT_CLS */
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010062 __s32 quirks;
63 __s32 sn_move; /* Signal/noise ratio for move events */
64 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
65 __u8 maxcontacts;
66};
67
68/* classes of device behavior */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +010069#define MT_CLS_DEFAULT 1
70#define MT_CLS_DUAL_INRANGE_CONTACTID 2
71#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3
72#define MT_CLS_CYPRESS 4
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010073
74/*
75 * these device-dependent functions determine what slot corresponds
76 * to a valid contact that was just read.
77 */
78
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +010079static int cypress_compute_slot(struct mt_device *td)
80{
81 if (td->curdata.contactid != 0 || td->num_received == 0)
82 return td->curdata.contactid;
83 else
84 return -1;
85}
86
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010087static int find_slot_from_contactid(struct mt_device *td)
88{
89 int i;
90 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
91 if (td->slots[i].contactid == td->curdata.contactid &&
92 td->slots[i].touch_state)
93 return i;
94 }
95 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
96 if (!td->slots[i].seen_in_this_frame &&
97 !td->slots[i].touch_state)
98 return i;
99 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100100 /* should not occurs. If this happens that means
101 * that the device sent more touches that it says
102 * in the report descriptor. It is ignored then. */
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100103 return -1;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100104}
105
106struct mt_class mt_classes[] = {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100107 { .name = MT_CLS_DEFAULT,
Benjamin Tissoiresb79b36b2011-01-31 11:28:19 +0100108 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100109 .maxcontacts = 10 },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100110 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100111 .quirks = MT_QUIRK_VALID_IS_INRANGE |
112 MT_QUIRK_SLOT_IS_CONTACTID,
113 .maxcontacts = 2 },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100114 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100115 .quirks = MT_QUIRK_VALID_IS_INRANGE |
116 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
117 .maxcontacts = 2 },
118 { .name = MT_CLS_CYPRESS,
119 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
120 MT_QUIRK_CYPRESS,
121 .maxcontacts = 10 },
122
123 { }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100124};
125
126static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
127 struct hid_field *field, struct hid_usage *usage)
128{
129 if (usage->hid == HID_DG_INPUTMODE) {
130 struct mt_device *td = hid_get_drvdata(hdev);
131 td->inputmode = field->report->id;
132 }
133}
134
135static void set_abs(struct input_dev *input, unsigned int code,
136 struct hid_field *field, int snratio)
137{
138 int fmin = field->logical_minimum;
139 int fmax = field->logical_maximum;
140 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
141 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
142}
143
144static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
145 struct hid_field *field, struct hid_usage *usage,
146 unsigned long **bit, int *max)
147{
148 struct mt_device *td = hid_get_drvdata(hdev);
149 struct mt_class *cls = td->mtclass;
150 switch (usage->hid & HID_USAGE_PAGE) {
151
152 case HID_UP_GENDESK:
153 switch (usage->hid) {
154 case HID_GD_X:
155 hid_map_usage(hi, usage, bit, max,
156 EV_ABS, ABS_MT_POSITION_X);
157 set_abs(hi->input, ABS_MT_POSITION_X, field,
158 cls->sn_move);
159 /* touchscreen emulation */
160 set_abs(hi->input, ABS_X, field, cls->sn_move);
161 td->last_slot_field = usage->hid;
162 return 1;
163 case HID_GD_Y:
164 hid_map_usage(hi, usage, bit, max,
165 EV_ABS, ABS_MT_POSITION_Y);
166 set_abs(hi->input, ABS_MT_POSITION_Y, field,
167 cls->sn_move);
168 /* touchscreen emulation */
169 set_abs(hi->input, ABS_Y, field, cls->sn_move);
170 td->last_slot_field = usage->hid;
171 return 1;
172 }
173 return 0;
174
175 case HID_UP_DIGITIZER:
176 switch (usage->hid) {
177 case HID_DG_INRANGE:
178 td->last_slot_field = usage->hid;
179 return 1;
180 case HID_DG_CONFIDENCE:
181 td->last_slot_field = usage->hid;
182 return 1;
183 case HID_DG_TIPSWITCH:
184 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
185 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
186 td->last_slot_field = usage->hid;
187 return 1;
188 case HID_DG_CONTACTID:
189 input_mt_init_slots(hi->input,
190 td->mtclass->maxcontacts);
191 td->last_slot_field = usage->hid;
192 return 1;
193 case HID_DG_WIDTH:
194 hid_map_usage(hi, usage, bit, max,
195 EV_ABS, ABS_MT_TOUCH_MAJOR);
196 td->last_slot_field = usage->hid;
197 return 1;
198 case HID_DG_HEIGHT:
199 hid_map_usage(hi, usage, bit, max,
200 EV_ABS, ABS_MT_TOUCH_MINOR);
201 field->logical_maximum = 1;
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100202 field->logical_minimum = 0;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100203 set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
204 td->last_slot_field = usage->hid;
205 return 1;
206 case HID_DG_TIPPRESSURE:
207 hid_map_usage(hi, usage, bit, max,
208 EV_ABS, ABS_MT_PRESSURE);
209 set_abs(hi->input, ABS_MT_PRESSURE, field,
210 cls->sn_pressure);
211 /* touchscreen emulation */
212 set_abs(hi->input, ABS_PRESSURE, field,
213 cls->sn_pressure);
214 td->last_slot_field = usage->hid;
215 return 1;
216 case HID_DG_CONTACTCOUNT:
217 td->last_field_index = field->report->maxfield - 1;
218 return 1;
219 case HID_DG_CONTACTMAX:
220 /* we don't set td->last_slot_field as contactcount and
221 * contact max are global to the report */
222 return -1;
223 }
224 /* let hid-input decide for the others */
225 return 0;
226
227 case 0xff000000:
228 /* we do not want to map these: no input-oriented meaning */
229 return -1;
230 }
231
232 return 0;
233}
234
235static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
236 struct hid_field *field, struct hid_usage *usage,
237 unsigned long **bit, int *max)
238{
239 if (usage->type == EV_KEY || usage->type == EV_ABS)
240 set_bit(usage->type, hi->input->evbit);
241
242 return -1;
243}
244
245static int mt_compute_slot(struct mt_device *td)
246{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100247 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100248
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100249 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
250 return td->curdata.contactid;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100251
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100252 if (quirks & MT_QUIRK_CYPRESS)
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100253 return cypress_compute_slot(td);
254
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100255 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
256 return td->num_received;
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100257
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100258 return find_slot_from_contactid(td);
259}
260
261/*
262 * this function is called when a whole contact has been processed,
263 * so that it can assign it to a slot and store the data there
264 */
265static void mt_complete_slot(struct mt_device *td)
266{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100267 td->curdata.seen_in_this_frame = true;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100268 if (td->curvalid) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100269 int slotnum = mt_compute_slot(td);
270
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100271 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
272 td->slots[slotnum] = td->curdata;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100273 }
274 td->num_received++;
275}
276
277
278/*
279 * this function is called when a whole packet has been received and processed,
280 * so that it can decide what to send to the input layer.
281 */
282static void mt_emit_event(struct mt_device *td, struct input_dev *input)
283{
284 int i;
285
286 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
287 struct mt_slot *s = &(td->slots[i]);
288 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
289 !s->seen_in_this_frame) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100290 s->touch_state = false;
291 }
292
293 input_mt_slot(input, i);
294 input_mt_report_slot_state(input, MT_TOOL_FINGER,
295 s->touch_state);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100296 if (s->touch_state) {
297 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
298 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
299 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
300 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
301 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
302 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100303 s->seen_in_this_frame = false;
304
305 }
306
307 input_mt_report_pointer_emulation(input, true);
308 input_sync(input);
309 td->num_received = 0;
310}
311
312
313
314static int mt_event(struct hid_device *hid, struct hid_field *field,
315 struct hid_usage *usage, __s32 value)
316{
317 struct mt_device *td = hid_get_drvdata(hid);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100318 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100319
320 if (hid->claimed & HID_CLAIMED_INPUT) {
321 switch (usage->hid) {
322 case HID_DG_INRANGE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100323 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
324 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100325 break;
326 case HID_DG_TIPSWITCH:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100327 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
328 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100329 td->curdata.touch_state = value;
330 break;
331 case HID_DG_CONFIDENCE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100332 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
333 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100334 break;
335 case HID_DG_CONTACTID:
336 td->curdata.contactid = value;
337 break;
338 case HID_DG_TIPPRESSURE:
339 td->curdata.p = value;
340 break;
341 case HID_GD_X:
342 td->curdata.x = value;
343 break;
344 case HID_GD_Y:
345 td->curdata.y = value;
346 break;
347 case HID_DG_WIDTH:
348 td->curdata.w = value;
349 break;
350 case HID_DG_HEIGHT:
351 td->curdata.h = value;
352 break;
353 case HID_DG_CONTACTCOUNT:
354 /*
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100355 * Includes multi-packet support where subsequent
356 * packets are sent with zero contactcount.
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100357 */
358 if (value)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100359 td->num_expected = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100360 break;
361
362 default:
363 /* fallback to the generic hidinput handling */
364 return 0;
365 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100366
367 if (usage->hid == td->last_slot_field)
368 mt_complete_slot(td);
369
370 if (field->index == td->last_field_index
371 && td->num_received >= td->num_expected)
372 mt_emit_event(td, field->hidinput->input);
373
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100374 }
375
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100376 /* we have handled the hidinput part, now remains hiddev */
377 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
378 hid->hiddev_hid_event(hid, field, usage, value);
379
380 return 1;
381}
382
383static void mt_set_input_mode(struct hid_device *hdev)
384{
385 struct mt_device *td = hid_get_drvdata(hdev);
386 struct hid_report *r;
387 struct hid_report_enum *re;
388
389 if (td->inputmode < 0)
390 return;
391
392 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
393 r = re->report_id_hash[td->inputmode];
394 if (r) {
395 r->field[0]->value[0] = 0x02;
396 usbhid_submit_report(hdev, r, USB_DIR_OUT);
397 }
398}
399
400static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
401{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100402 int ret, i;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100403 struct mt_device *td;
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100404 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
405
406 for (i = 0; mt_classes[i].name ; i++) {
407 if (id->driver_data == mt_classes[i].name) {
408 mtclass = &(mt_classes[i]);
409 break;
410 }
411 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100412
413 /* This allows the driver to correctly support devices
414 * that emit events over several HID messages.
415 */
416 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
417
418 td = kzalloc(sizeof(struct mt_device) +
419 mtclass->maxcontacts * sizeof(struct mt_slot),
420 GFP_KERNEL);
421 if (!td) {
422 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
423 return -ENOMEM;
424 }
425 td->mtclass = mtclass;
426 td->inputmode = -1;
427 hid_set_drvdata(hdev, td);
428
429 ret = hid_parse(hdev);
430 if (ret != 0)
431 goto fail;
432
433 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100434 if (ret)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100435 goto fail;
436
437 mt_set_input_mode(hdev);
438
439 return 0;
440
441fail:
442 kfree(td);
443 return ret;
444}
445
446#ifdef CONFIG_PM
447static int mt_reset_resume(struct hid_device *hdev)
448{
449 mt_set_input_mode(hdev);
450 return 0;
451}
452#endif
453
454static void mt_remove(struct hid_device *hdev)
455{
456 struct mt_device *td = hid_get_drvdata(hdev);
457 hid_hw_stop(hdev);
458 kfree(td);
459 hid_set_drvdata(hdev, NULL);
460}
461
462static const struct hid_device_id mt_devices[] = {
463
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100464 /* Cypress panel */
465 { .driver_data = MT_CLS_CYPRESS,
466 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
467 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
468
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100469 /* GeneralTouch panel */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100470 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100471 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
472 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
473
Benjamin Tissoires4dfcced2011-01-31 11:28:22 +0100474 /* IRTOUCH panels */
475 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
476 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
477 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
478
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100479 /* PixCir-based panels */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100480 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100481 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
482 USB_DEVICE_ID_HANVON_MULTITOUCH) },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100483 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100484 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
485 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
486
487 { }
488};
489MODULE_DEVICE_TABLE(hid, mt_devices);
490
491static const struct hid_usage_id mt_grabbed_usages[] = {
492 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
493 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
494};
495
496static struct hid_driver mt_driver = {
497 .name = "hid-multitouch",
498 .id_table = mt_devices,
499 .probe = mt_probe,
500 .remove = mt_remove,
501 .input_mapping = mt_input_mapping,
502 .input_mapped = mt_input_mapped,
503 .feature_mapping = mt_feature_mapping,
504 .usage_table = mt_grabbed_usages,
505 .event = mt_event,
506#ifdef CONFIG_PM
507 .reset_resume = mt_reset_resume,
508#endif
509};
510
511static int __init mt_init(void)
512{
513 return hid_register_driver(&mt_driver);
514}
515
516static void __exit mt_exit(void)
517{
518 hid_unregister_driver(&mt_driver);
519}
520
521module_init(mt_init);
522module_exit(mt_exit);