]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/hid-multitouch.c
HID: hid-multitouch: Send events per slot if CONTACTCOUNT is missing
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / hid-multitouch.c
CommitLineData
5519cab4
BT
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>");
ef2fafb3 27MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
5519cab4
BT
28MODULE_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)
2d93666e 36#define MT_QUIRK_CYPRESS (1 << 2)
5572da08 37#define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
2d93666e
BT
38#define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
39#define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
5519cab4
BT
40
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 {
2d93666e 61 __s32 name; /* MT_CLS */
5519cab4
BT
62 __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 */
1e9cf35b
BT
69#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
5519cab4
BT
73
74/*
75 * these device-dependent functions determine what slot corresponds
76 * to a valid contact that was just read.
77 */
78
a3b5e577
BT
79static 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
5519cab4
BT
87static 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 }
5519cab4
BT
100 /* 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. */
2d93666e 103 return -1;
5519cab4
BT
104}
105
106struct mt_class mt_classes[] = {
2d93666e 107 { .name = MT_CLS_DEFAULT,
b79b36be 108 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
2d93666e 109 .maxcontacts = 10 },
1e9cf35b 110 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
2d93666e
BT
111 .quirks = MT_QUIRK_VALID_IS_INRANGE |
112 MT_QUIRK_SLOT_IS_CONTACTID,
113 .maxcontacts = 2 },
1e9cf35b 114 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2d93666e
BT
115 .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 { }
5519cab4
BT
124};
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;
2d93666e 202 field->logical_minimum = 0;
5519cab4
BT
203 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{
2d93666e 247 __s32 quirks = td->mtclass->quirks;
5519cab4 248
2d93666e
BT
249 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
250 return td->curdata.contactid;
5519cab4 251
2d93666e 252 if (quirks & MT_QUIRK_CYPRESS)
a3b5e577
BT
253 return cypress_compute_slot(td);
254
2d93666e
BT
255 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
256 return td->num_received;
5572da08 257
5519cab4
BT
258 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{
2d93666e 267 td->curdata.seen_in_this_frame = true;
5519cab4 268 if (td->curvalid) {
5519cab4
BT
269 int slotnum = mt_compute_slot(td);
270
2d93666e
BT
271 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
272 td->slots[slotnum] = td->curdata;
5519cab4
BT
273 }
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) {
5519cab4
BT
290 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);
2d93666e
BT
296 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 }
5519cab4
BT
303 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);
2d93666e 318 __s32 quirks = td->mtclass->quirks;
5519cab4
BT
319
320 if (hid->claimed & HID_CLAIMED_INPUT) {
321 switch (usage->hid) {
322 case HID_DG_INRANGE:
2d93666e
BT
323 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
324 td->curvalid = value;
5519cab4
BT
325 break;
326 case HID_DG_TIPSWITCH:
2d93666e
BT
327 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
328 td->curvalid = value;
5519cab4
BT
329 td->curdata.touch_state = value;
330 break;
331 case HID_DG_CONFIDENCE:
2d93666e
BT
332 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
333 td->curvalid = value;
5519cab4
BT
334 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 /*
2d93666e
BT
355 * Includes multi-packet support where subsequent
356 * packets are sent with zero contactcount.
5519cab4
BT
357 */
358 if (value)
2d93666e 359 td->num_expected = value;
5519cab4
BT
360 break;
361
362 default:
363 /* fallback to the generic hidinput handling */
364 return 0;
365 }
5519cab4 366
f153fc39 367 if (usage->hid == td->last_slot_field) {
2d93666e 368 mt_complete_slot(td);
f153fc39
HR
369 if (!td->last_field_index)
370 mt_emit_event(td, field->hidinput->input);
371 }
2d93666e
BT
372
373 if (field->index == td->last_field_index
374 && td->num_received >= td->num_expected)
375 mt_emit_event(td, field->hidinput->input);
5519cab4 376
2d93666e 377 }
5519cab4
BT
378
379 /* we have handled the hidinput part, now remains hiddev */
380 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
381 hid->hiddev_hid_event(hid, field, usage, value);
382
383 return 1;
384}
385
386static void mt_set_input_mode(struct hid_device *hdev)
387{
388 struct mt_device *td = hid_get_drvdata(hdev);
389 struct hid_report *r;
390 struct hid_report_enum *re;
391
392 if (td->inputmode < 0)
393 return;
394
395 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
396 r = re->report_id_hash[td->inputmode];
397 if (r) {
398 r->field[0]->value[0] = 0x02;
399 usbhid_submit_report(hdev, r, USB_DIR_OUT);
400 }
401}
402
403static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
404{
2d93666e 405 int ret, i;
5519cab4 406 struct mt_device *td;
2d93666e
BT
407 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
408
409 for (i = 0; mt_classes[i].name ; i++) {
410 if (id->driver_data == mt_classes[i].name) {
411 mtclass = &(mt_classes[i]);
412 break;
413 }
414 }
5519cab4
BT
415
416 /* This allows the driver to correctly support devices
417 * that emit events over several HID messages.
418 */
419 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
420
421 td = kzalloc(sizeof(struct mt_device) +
422 mtclass->maxcontacts * sizeof(struct mt_slot),
423 GFP_KERNEL);
424 if (!td) {
425 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
426 return -ENOMEM;
427 }
428 td->mtclass = mtclass;
429 td->inputmode = -1;
430 hid_set_drvdata(hdev, td);
431
432 ret = hid_parse(hdev);
433 if (ret != 0)
434 goto fail;
435
436 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2d93666e 437 if (ret)
5519cab4
BT
438 goto fail;
439
440 mt_set_input_mode(hdev);
441
442 return 0;
443
444fail:
445 kfree(td);
446 return ret;
447}
448
449#ifdef CONFIG_PM
450static int mt_reset_resume(struct hid_device *hdev)
451{
452 mt_set_input_mode(hdev);
453 return 0;
454}
455#endif
456
457static void mt_remove(struct hid_device *hdev)
458{
459 struct mt_device *td = hid_get_drvdata(hdev);
460 hid_hw_stop(hdev);
461 kfree(td);
462 hid_set_drvdata(hdev, NULL);
463}
464
465static const struct hid_device_id mt_devices[] = {
466
a3b5e577
BT
467 /* Cypress panel */
468 { .driver_data = MT_CLS_CYPRESS,
469 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
470 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
471
5572da08 472 /* GeneralTouch panel */
1e9cf35b 473 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
5572da08
BT
474 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
475 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
476
4dfcced8
BT
477 /* IRTOUCH panels */
478 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
479 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
480 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
481
5519cab4 482 /* PixCir-based panels */
1e9cf35b 483 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
5519cab4
BT
484 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
485 USB_DEVICE_ID_HANVON_MULTITOUCH) },
1e9cf35b 486 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
5519cab4
BT
487 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
488 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
489
490 { }
491};
492MODULE_DEVICE_TABLE(hid, mt_devices);
493
494static const struct hid_usage_id mt_grabbed_usages[] = {
495 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
496 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
497};
498
499static struct hid_driver mt_driver = {
500 .name = "hid-multitouch",
501 .id_table = mt_devices,
502 .probe = mt_probe,
503 .remove = mt_remove,
504 .input_mapping = mt_input_mapping,
505 .input_mapped = mt_input_mapped,
506 .feature_mapping = mt_feature_mapping,
507 .usage_table = mt_grabbed_usages,
508 .event = mt_event,
509#ifdef CONFIG_PM
510 .reset_resume = mt_reset_resume,
511#endif
512};
513
514static int __init mt_init(void)
515{
516 return hid_register_driver(&mt_driver);
517}
518
519static void __exit mt_exit(void)
520{
521 hid_unregister_driver(&mt_driver);
522}
523
524module_init(mt_init);
525module_exit(mt_exit);