Commit | Line | Data |
---|---|---|
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 | ||
26 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); | |
ef2fafb3 | 27 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
5519cab4 BT |
28 | MODULE_DESCRIPTION("HID multitouch panels"); |
29 | MODULE_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 | |
41 | struct 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 | ||
48 | struct 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 | ||
60 | struct 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 |
79 | static 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 |
87 | static 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 | ||
106 | struct 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 | ||
126 | static 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 | ||
135 | static 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 | ||
144 | static 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 | ||
235 | static 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 | ||
245 | static 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 | */ | |
265 | static 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 | */ | |
282 | static 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 | ||
314 | static 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 | |
2d93666e BT |
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); | |
5519cab4 | 373 | |
2d93666e | 374 | } |
5519cab4 BT |
375 | |
376 | /* 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 | ||
383 | static 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 | ||
400 | static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
401 | { | |
2d93666e | 402 | int ret, i; |
5519cab4 | 403 | struct mt_device *td; |
2d93666e BT |
404 | 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 | } | |
5519cab4 BT |
412 | |
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); | |
2d93666e | 434 | if (ret) |
5519cab4 BT |
435 | goto fail; |
436 | ||
437 | mt_set_input_mode(hdev); | |
438 | ||
439 | return 0; | |
440 | ||
441 | fail: | |
442 | kfree(td); | |
443 | return ret; | |
444 | } | |
445 | ||
446 | #ifdef CONFIG_PM | |
447 | static int mt_reset_resume(struct hid_device *hdev) | |
448 | { | |
449 | mt_set_input_mode(hdev); | |
450 | return 0; | |
451 | } | |
452 | #endif | |
453 | ||
454 | static 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 | ||
462 | static const struct hid_device_id mt_devices[] = { | |
463 | ||
a3b5e577 BT |
464 | /* Cypress panel */ |
465 | { .driver_data = MT_CLS_CYPRESS, | |
466 | HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, | |
467 | USB_DEVICE_ID_CYPRESS_TRUETOUCH) }, | |
468 | ||
5572da08 | 469 | /* GeneralTouch panel */ |
1e9cf35b | 470 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
5572da08 BT |
471 | HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, |
472 | USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) }, | |
473 | ||
5519cab4 | 474 | /* PixCir-based panels */ |
1e9cf35b | 475 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
5519cab4 BT |
476 | HID_USB_DEVICE(USB_VENDOR_ID_HANVON, |
477 | USB_DEVICE_ID_HANVON_MULTITOUCH) }, | |
1e9cf35b | 478 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
5519cab4 BT |
479 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
480 | USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) }, | |
481 | ||
482 | { } | |
483 | }; | |
484 | MODULE_DEVICE_TABLE(hid, mt_devices); | |
485 | ||
486 | static const struct hid_usage_id mt_grabbed_usages[] = { | |
487 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, | |
488 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} | |
489 | }; | |
490 | ||
491 | static struct hid_driver mt_driver = { | |
492 | .name = "hid-multitouch", | |
493 | .id_table = mt_devices, | |
494 | .probe = mt_probe, | |
495 | .remove = mt_remove, | |
496 | .input_mapping = mt_input_mapping, | |
497 | .input_mapped = mt_input_mapped, | |
498 | .feature_mapping = mt_feature_mapping, | |
499 | .usage_table = mt_grabbed_usages, | |
500 | .event = mt_event, | |
501 | #ifdef CONFIG_PM | |
502 | .reset_resume = mt_reset_resume, | |
503 | #endif | |
504 | }; | |
505 | ||
506 | static int __init mt_init(void) | |
507 | { | |
508 | return hid_register_driver(&mt_driver); | |
509 | } | |
510 | ||
511 | static void __exit mt_exit(void) | |
512 | { | |
513 | hid_unregister_driver(&mt_driver); | |
514 | } | |
515 | ||
516 | module_init(mt_init); | |
517 | module_exit(mt_exit); |