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