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)
38 __s32 contactid; /* the device ContactID assigned to this slot */
39 bool touch_state; /* is the touch valid? */
40 bool seen_in_this_frame;/* has this slot been updated */
44 struct mt_slot curdata; /* placeholder of incoming data */
45 struct mt_class *mtclass; /* our mt device class */
46 unsigned last_field_index; /* last field index of the report */
47 unsigned last_slot_field; /* the last field of a slot */
48 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
49 __u8 num_received; /* how many contacts we received */
50 __u8 num_expected; /* expected last contact index */
51 bool curvalid; /* is the current contact valid? */
52 struct mt_slot slots[0]; /* first slot */
57 __s32 sn_move; /* Signal/noise ratio for move events */
58 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
62 /* classes of device behavior */
63 #define MT_CLS_DEFAULT 0
64 #define MT_CLS_DUAL1 1
67 * these device-dependent functions determine what slot corresponds
68 * to a valid contact that was just read.
71 static int slot_is_contactid(struct mt_device *td)
73 return td->curdata.contactid;
76 static int find_slot_from_contactid(struct mt_device *td)
79 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
80 if (td->slots[i].contactid == td->curdata.contactid &&
81 td->slots[i].touch_state)
84 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
85 if (!td->slots[i].seen_in_this_frame &&
86 !td->slots[i].touch_state)
90 /* should not occurs. If this happens that means
91 * that the device sent more touches that it says
92 * in the report descriptor. It is ignored then. */
95 struct mt_class mt_classes[] = {
96 { 0, 0, 0, 10 }, /* MT_CLS_DEFAULT */
97 { MT_QUIRK_SLOT_IS_CONTACTID, 0, 0, 2 }, /* MT_CLS_DUAL1 */
100 static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
101 struct hid_field *field, struct hid_usage *usage)
103 if (usage->hid == HID_DG_INPUTMODE) {
104 struct mt_device *td = hid_get_drvdata(hdev);
105 td->inputmode = field->report->id;
109 static void set_abs(struct input_dev *input, unsigned int code,
110 struct hid_field *field, int snratio)
112 int fmin = field->logical_minimum;
113 int fmax = field->logical_maximum;
114 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
115 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
118 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
119 struct hid_field *field, struct hid_usage *usage,
120 unsigned long **bit, int *max)
122 struct mt_device *td = hid_get_drvdata(hdev);
123 struct mt_class *cls = td->mtclass;
124 switch (usage->hid & HID_USAGE_PAGE) {
127 switch (usage->hid) {
129 hid_map_usage(hi, usage, bit, max,
130 EV_ABS, ABS_MT_POSITION_X);
131 set_abs(hi->input, ABS_MT_POSITION_X, field,
133 /* touchscreen emulation */
134 set_abs(hi->input, ABS_X, field, cls->sn_move);
135 td->last_slot_field = usage->hid;
138 hid_map_usage(hi, usage, bit, max,
139 EV_ABS, ABS_MT_POSITION_Y);
140 set_abs(hi->input, ABS_MT_POSITION_Y, field,
142 /* touchscreen emulation */
143 set_abs(hi->input, ABS_Y, field, cls->sn_move);
144 td->last_slot_field = usage->hid;
149 case HID_UP_DIGITIZER:
150 switch (usage->hid) {
152 td->last_slot_field = usage->hid;
154 case HID_DG_CONFIDENCE:
155 td->last_slot_field = usage->hid;
157 case HID_DG_TIPSWITCH:
158 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
159 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
160 td->last_slot_field = usage->hid;
162 case HID_DG_CONTACTID:
163 input_mt_init_slots(hi->input,
164 td->mtclass->maxcontacts);
165 td->last_slot_field = usage->hid;
168 hid_map_usage(hi, usage, bit, max,
169 EV_ABS, ABS_MT_TOUCH_MAJOR);
170 td->last_slot_field = usage->hid;
173 hid_map_usage(hi, usage, bit, max,
174 EV_ABS, ABS_MT_TOUCH_MINOR);
175 field->logical_maximum = 1;
176 field->logical_minimum = 1;
177 set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
178 td->last_slot_field = usage->hid;
180 case HID_DG_TIPPRESSURE:
181 hid_map_usage(hi, usage, bit, max,
182 EV_ABS, ABS_MT_PRESSURE);
183 set_abs(hi->input, ABS_MT_PRESSURE, field,
185 /* touchscreen emulation */
186 set_abs(hi->input, ABS_PRESSURE, field,
188 td->last_slot_field = usage->hid;
190 case HID_DG_CONTACTCOUNT:
191 td->last_field_index = field->report->maxfield - 1;
193 case HID_DG_CONTACTMAX:
194 /* we don't set td->last_slot_field as contactcount and
195 * contact max are global to the report */
198 /* let hid-input decide for the others */
202 /* we do not want to map these: no input-oriented meaning */
209 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
210 struct hid_field *field, struct hid_usage *usage,
211 unsigned long **bit, int *max)
213 if (usage->type == EV_KEY || usage->type == EV_ABS)
214 set_bit(usage->type, hi->input->evbit);
219 static int mt_compute_slot(struct mt_device *td)
221 struct mt_class *cls = td->mtclass;
223 if (cls->quirks & MT_QUIRK_SLOT_IS_CONTACTID)
224 return slot_is_contactid(td);
226 return find_slot_from_contactid(td);
230 * this function is called when a whole contact has been processed,
231 * so that it can assign it to a slot and store the data there
233 static void mt_complete_slot(struct mt_device *td)
236 struct mt_slot *slot;
237 int slotnum = mt_compute_slot(td);
239 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts) {
240 slot = td->slots + slotnum;
242 memcpy(slot, &(td->curdata), sizeof(struct mt_slot));
243 slot->seen_in_this_frame = true;
251 * this function is called when a whole packet has been received and processed,
252 * so that it can decide what to send to the input layer.
254 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
258 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
259 struct mt_slot *s = &(td->slots[i]);
260 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
261 !s->seen_in_this_frame) {
263 * this slot does not contain useful data,
266 s->touch_state = false;
269 input_mt_slot(input, i);
270 input_mt_report_slot_state(input, MT_TOOL_FINGER,
272 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
273 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
274 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
275 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
276 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
277 s->seen_in_this_frame = false;
281 input_mt_report_pointer_emulation(input, true);
283 td->num_received = 0;
288 static int mt_event(struct hid_device *hid, struct hid_field *field,
289 struct hid_usage *usage, __s32 value)
291 struct mt_device *td = hid_get_drvdata(hid);
293 if (hid->claimed & HID_CLAIMED_INPUT) {
294 switch (usage->hid) {
297 case HID_DG_TIPSWITCH:
298 td->curvalid = value;
299 td->curdata.touch_state = value;
301 case HID_DG_CONFIDENCE:
303 case HID_DG_CONTACTID:
304 td->curdata.contactid = value;
306 case HID_DG_TIPPRESSURE:
307 td->curdata.p = value;
310 td->curdata.x = value;
313 td->curdata.y = value;
316 td->curdata.w = value;
319 td->curdata.h = value;
321 case HID_DG_CONTACTCOUNT:
323 * We must not overwrite the previous value (some
324 * devices send one sequence splitted over several
328 td->num_expected = value - 1;
332 /* fallback to the generic hidinput handling */
337 if (usage->hid == td->last_slot_field)
338 mt_complete_slot(td);
340 if (field->index == td->last_field_index
341 && td->num_received > td->num_expected)
342 mt_emit_event(td, field->hidinput->input);
344 /* we have handled the hidinput part, now remains hiddev */
345 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
346 hid->hiddev_hid_event(hid, field, usage, value);
351 static void mt_set_input_mode(struct hid_device *hdev)
353 struct mt_device *td = hid_get_drvdata(hdev);
354 struct hid_report *r;
355 struct hid_report_enum *re;
357 if (td->inputmode < 0)
360 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
361 r = re->report_id_hash[td->inputmode];
363 r->field[0]->value[0] = 0x02;
364 usbhid_submit_report(hdev, r, USB_DIR_OUT);
368 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
371 struct mt_device *td;
372 struct mt_class *mtclass = mt_classes + id->driver_data;
374 /* This allows the driver to correctly support devices
375 * that emit events over several HID messages.
377 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
379 td = kzalloc(sizeof(struct mt_device) +
380 mtclass->maxcontacts * sizeof(struct mt_slot),
383 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
386 td->mtclass = mtclass;
388 hid_set_drvdata(hdev, td);
390 ret = hid_parse(hdev);
394 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
398 mt_set_input_mode(hdev);
408 static int mt_reset_resume(struct hid_device *hdev)
410 mt_set_input_mode(hdev);
415 static void mt_remove(struct hid_device *hdev)
417 struct mt_device *td = hid_get_drvdata(hdev);
420 hid_set_drvdata(hdev, NULL);
423 static const struct hid_device_id mt_devices[] = {
425 /* PixCir-based panels */
426 { .driver_data = MT_CLS_DUAL1,
427 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
428 USB_DEVICE_ID_HANVON_MULTITOUCH) },
429 { .driver_data = MT_CLS_DUAL1,
430 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
431 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
435 MODULE_DEVICE_TABLE(hid, mt_devices);
437 static const struct hid_usage_id mt_grabbed_usages[] = {
438 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
439 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
442 static struct hid_driver mt_driver = {
443 .name = "hid-multitouch",
444 .id_table = mt_devices,
447 .input_mapping = mt_input_mapping,
448 .input_mapped = mt_input_mapped,
449 .feature_mapping = mt_feature_mapping,
450 .usage_table = mt_grabbed_usages,
453 .reset_resume = mt_reset_resume,
457 static int __init mt_init(void)
459 return hid_register_driver(&mt_driver);
462 static void __exit mt_exit(void)
464 hid_unregister_driver(&mt_driver);
467 module_init(mt_init);
468 module_exit(mt_exit);