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