2 * HID driver for multitouch panels
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
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)
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"
26 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
27 MODULE_DESCRIPTION("HID multitouch panels");
28 MODULE_LICENSE("GPL");
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)
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 */
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 */
59 __s32 sn_move; /* Signal/noise ratio for move events */
60 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
64 /* classes of device behavior */
65 #define MT_CLS_DEFAULT 0
66 #define MT_CLS_DUAL1 1
67 #define MT_CLS_DUAL2 2
68 #define MT_CLS_CYPRESS 3
71 * these device-dependent functions determine what slot corresponds
72 * to a valid contact that was just read.
75 static int slot_is_contactid(struct mt_device *td)
77 return td->curdata.contactid;
80 static int slot_is_contactnumber(struct mt_device *td)
82 return td->num_received;
85 static int cypress_compute_slot(struct mt_device *td)
87 if (td->curdata.contactid != 0 || td->num_received == 0)
88 return td->curdata.contactid;
93 static int find_slot_from_contactid(struct mt_device *td)
96 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
97 if (td->slots[i].contactid == td->curdata.contactid &&
98 td->slots[i].touch_state)
101 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
102 if (!td->slots[i].seen_in_this_frame &&
103 !td->slots[i].touch_state)
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. */
112 struct mt_class mt_classes[] = {
113 { 0, 0, 0, 10 }, /* MT_CLS_DEFAULT */
114 { MT_QUIRK_SLOT_IS_CONTACTID, 0, 0, 2 }, /* MT_CLS_DUAL1 */
115 { MT_QUIRK_SLOT_IS_CONTACTNUMBER, 0, 0, 10 }, /* MT_CLS_DUAL2 */
116 { MT_QUIRK_CYPRESS | MT_QUIRK_NOT_SEEN_MEANS_UP, 0, 0, 10 }, /* MT_CLS_CYPRESS */
119 static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
120 struct hid_field *field, struct hid_usage *usage)
122 if (usage->hid == HID_DG_INPUTMODE) {
123 struct mt_device *td = hid_get_drvdata(hdev);
124 td->inputmode = field->report->id;
128 static void set_abs(struct input_dev *input, unsigned int code,
129 struct hid_field *field, int snratio)
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);
137 static 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)
141 struct mt_device *td = hid_get_drvdata(hdev);
142 struct mt_class *cls = td->mtclass;
143 switch (usage->hid & HID_USAGE_PAGE) {
146 switch (usage->hid) {
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,
152 /* touchscreen emulation */
153 set_abs(hi->input, ABS_X, field, cls->sn_move);
154 td->last_slot_field = usage->hid;
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,
161 /* touchscreen emulation */
162 set_abs(hi->input, ABS_Y, field, cls->sn_move);
163 td->last_slot_field = usage->hid;
168 case HID_UP_DIGITIZER:
169 switch (usage->hid) {
171 td->last_slot_field = usage->hid;
173 case HID_DG_CONFIDENCE:
174 td->last_slot_field = usage->hid;
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;
181 case HID_DG_CONTACTID:
182 input_mt_init_slots(hi->input,
183 td->mtclass->maxcontacts);
184 td->last_slot_field = usage->hid;
187 hid_map_usage(hi, usage, bit, max,
188 EV_ABS, ABS_MT_TOUCH_MAJOR);
189 td->last_slot_field = usage->hid;
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;
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,
204 /* touchscreen emulation */
205 set_abs(hi->input, ABS_PRESSURE, field,
207 td->last_slot_field = usage->hid;
209 case HID_DG_CONTACTCOUNT:
210 td->last_field_index = field->report->maxfield - 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 */
217 /* let hid-input decide for the others */
221 /* we do not want to map these: no input-oriented meaning */
228 static 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)
232 if (usage->type == EV_KEY || usage->type == EV_ABS)
233 set_bit(usage->type, hi->input->evbit);
238 static int mt_compute_slot(struct mt_device *td)
240 struct mt_class *cls = td->mtclass;
242 if (cls->quirks & MT_QUIRK_SLOT_IS_CONTACTID)
243 return slot_is_contactid(td);
245 if (cls->quirks & MT_QUIRK_CYPRESS)
246 return cypress_compute_slot(td);
248 if (cls->quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
249 return slot_is_contactnumber(td);
251 return find_slot_from_contactid(td);
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
258 static void mt_complete_slot(struct mt_device *td)
261 struct mt_slot *slot;
262 int slotnum = mt_compute_slot(td);
264 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts) {
265 slot = td->slots + slotnum;
267 memcpy(slot, &(td->curdata), sizeof(struct mt_slot));
268 slot->seen_in_this_frame = true;
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.
279 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
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) {
288 * this slot does not contain useful data,
291 s->touch_state = false;
294 input_mt_slot(input, i);
295 input_mt_report_slot_state(input, MT_TOOL_FINGER,
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;
306 input_mt_report_pointer_emulation(input, true);
308 td->num_received = 0;
313 static int mt_event(struct hid_device *hid, struct hid_field *field,
314 struct hid_usage *usage, __s32 value)
316 struct mt_device *td = hid_get_drvdata(hid);
318 if (hid->claimed & HID_CLAIMED_INPUT) {
319 switch (usage->hid) {
322 case HID_DG_TIPSWITCH:
323 td->curvalid = value;
324 td->curdata.touch_state = value;
326 case HID_DG_CONFIDENCE:
328 case HID_DG_CONTACTID:
329 td->curdata.contactid = value;
331 case HID_DG_TIPPRESSURE:
332 td->curdata.p = value;
335 td->curdata.x = value;
338 td->curdata.y = value;
341 td->curdata.w = value;
344 td->curdata.h = value;
346 case HID_DG_CONTACTCOUNT:
348 * We must not overwrite the previous value (some
349 * devices send one sequence splitted over several
353 td->num_expected = value - 1;
357 /* fallback to the generic hidinput handling */
362 if (usage->hid == td->last_slot_field)
363 mt_complete_slot(td);
365 if (field->index == td->last_field_index
366 && td->num_received > td->num_expected)
367 mt_emit_event(td, field->hidinput->input);
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);
376 static void mt_set_input_mode(struct hid_device *hdev)
378 struct mt_device *td = hid_get_drvdata(hdev);
379 struct hid_report *r;
380 struct hid_report_enum *re;
382 if (td->inputmode < 0)
385 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
386 r = re->report_id_hash[td->inputmode];
388 r->field[0]->value[0] = 0x02;
389 usbhid_submit_report(hdev, r, USB_DIR_OUT);
393 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
396 struct mt_device *td;
397 struct mt_class *mtclass = mt_classes + id->driver_data;
399 /* This allows the driver to correctly support devices
400 * that emit events over several HID messages.
402 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
404 td = kzalloc(sizeof(struct mt_device) +
405 mtclass->maxcontacts * sizeof(struct mt_slot),
408 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
411 td->mtclass = mtclass;
413 hid_set_drvdata(hdev, td);
415 ret = hid_parse(hdev);
419 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
423 mt_set_input_mode(hdev);
433 static int mt_reset_resume(struct hid_device *hdev)
435 mt_set_input_mode(hdev);
440 static void mt_remove(struct hid_device *hdev)
442 struct mt_device *td = hid_get_drvdata(hdev);
445 hid_set_drvdata(hdev, NULL);
448 static const struct hid_device_id mt_devices[] = {
451 { .driver_data = MT_CLS_CYPRESS,
452 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
453 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
455 /* 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) },
460 /* 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) },
470 MODULE_DEVICE_TABLE(hid, mt_devices);
472 static 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}
477 static struct hid_driver mt_driver = {
478 .name = "hid-multitouch",
479 .id_table = mt_devices,
482 .input_mapping = mt_input_mapping,
483 .input_mapped = mt_input_mapped,
484 .feature_mapping = mt_feature_mapping,
485 .usage_table = mt_grabbed_usages,
488 .reset_resume = mt_reset_resume,
492 static int __init mt_init(void)
494 return hid_register_driver(&mt_driver);
497 static void __exit mt_exit(void)
499 hid_unregister_driver(&mt_driver);
502 module_init(mt_init);
503 module_exit(mt_exit);