blob: 3b05dfe910bcbe4377360dde15334dec8313d3cb [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)
35
36struct mt_slot {
37 __s32 x, y, p, w, h;
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 */
41};
42
43struct mt_device {
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 */
53};
54
55struct mt_class {
56 __s32 quirks;
57 __s32 sn_move; /* Signal/noise ratio for move events */
58 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
59 __u8 maxcontacts;
60};
61
62/* classes of device behavior */
63#define MT_CLS_DEFAULT 0
64#define MT_CLS_DUAL1 1
65
66/*
67 * these device-dependent functions determine what slot corresponds
68 * to a valid contact that was just read.
69 */
70
71static int slot_is_contactid(struct mt_device *td)
72{
73 return td->curdata.contactid;
74}
75
76static int find_slot_from_contactid(struct mt_device *td)
77{
78 int i;
79 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
80 if (td->slots[i].contactid == td->curdata.contactid &&
81 td->slots[i].touch_state)
82 return i;
83 }
84 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
85 if (!td->slots[i].seen_in_this_frame &&
86 !td->slots[i].touch_state)
87 return i;
88 }
89 return -1;
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. */
93}
94
95struct mt_class mt_classes[] = {
96 { 0, 0, 0, 10 }, /* MT_CLS_DEFAULT */
97 { MT_QUIRK_SLOT_IS_CONTACTID, 0, 0, 2 }, /* MT_CLS_DUAL1 */
98};
99
100static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
101 struct hid_field *field, struct hid_usage *usage)
102{
103 if (usage->hid == HID_DG_INPUTMODE) {
104 struct mt_device *td = hid_get_drvdata(hdev);
105 td->inputmode = field->report->id;
106 }
107}
108
109static void set_abs(struct input_dev *input, unsigned int code,
110 struct hid_field *field, int snratio)
111{
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);
116}
117
118static 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)
121{
122 struct mt_device *td = hid_get_drvdata(hdev);
123 struct mt_class *cls = td->mtclass;
124 switch (usage->hid & HID_USAGE_PAGE) {
125
126 case HID_UP_GENDESK:
127 switch (usage->hid) {
128 case HID_GD_X:
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,
132 cls->sn_move);
133 /* touchscreen emulation */
134 set_abs(hi->input, ABS_X, field, cls->sn_move);
135 td->last_slot_field = usage->hid;
136 return 1;
137 case HID_GD_Y:
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,
141 cls->sn_move);
142 /* touchscreen emulation */
143 set_abs(hi->input, ABS_Y, field, cls->sn_move);
144 td->last_slot_field = usage->hid;
145 return 1;
146 }
147 return 0;
148
149 case HID_UP_DIGITIZER:
150 switch (usage->hid) {
151 case HID_DG_INRANGE:
152 td->last_slot_field = usage->hid;
153 return 1;
154 case HID_DG_CONFIDENCE:
155 td->last_slot_field = usage->hid;
156 return 1;
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;
161 return 1;
162 case HID_DG_CONTACTID:
163 input_mt_init_slots(hi->input,
164 td->mtclass->maxcontacts);
165 td->last_slot_field = usage->hid;
166 return 1;
167 case HID_DG_WIDTH:
168 hid_map_usage(hi, usage, bit, max,
169 EV_ABS, ABS_MT_TOUCH_MAJOR);
170 td->last_slot_field = usage->hid;
171 return 1;
172 case HID_DG_HEIGHT:
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;
179 return 1;
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,
184 cls->sn_pressure);
185 /* touchscreen emulation */
186 set_abs(hi->input, ABS_PRESSURE, field,
187 cls->sn_pressure);
188 td->last_slot_field = usage->hid;
189 return 1;
190 case HID_DG_CONTACTCOUNT:
191 td->last_field_index = field->report->maxfield - 1;
192 return 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 */
196 return -1;
197 }
198 /* let hid-input decide for the others */
199 return 0;
200
201 case 0xff000000:
202 /* we do not want to map these: no input-oriented meaning */
203 return -1;
204 }
205
206 return 0;
207}
208
209static 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)
212{
213 if (usage->type == EV_KEY || usage->type == EV_ABS)
214 set_bit(usage->type, hi->input->evbit);
215
216 return -1;
217}
218
219static int mt_compute_slot(struct mt_device *td)
220{
221 struct mt_class *cls = td->mtclass;
222
223 if (cls->quirks & MT_QUIRK_SLOT_IS_CONTACTID)
224 return slot_is_contactid(td);
225
226 return find_slot_from_contactid(td);
227}
228
229/*
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
232 */
233static void mt_complete_slot(struct mt_device *td)
234{
235 if (td->curvalid) {
236 struct mt_slot *slot;
237 int slotnum = mt_compute_slot(td);
238
239 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts) {
240 slot = td->slots + slotnum;
241
242 memcpy(slot, &(td->curdata), sizeof(struct mt_slot));
243 slot->seen_in_this_frame = true;
244 }
245 }
246 td->num_received++;
247}
248
249
250/*
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.
253 */
254static void mt_emit_event(struct mt_device *td, struct input_dev *input)
255{
256 int i;
257
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) {
262 /*
263 * this slot does not contain useful data,
264 * notify its closure
265 */
266 s->touch_state = false;
267 }
268
269 input_mt_slot(input, i);
270 input_mt_report_slot_state(input, MT_TOOL_FINGER,
271 s->touch_state);
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;
278
279 }
280
281 input_mt_report_pointer_emulation(input, true);
282 input_sync(input);
283 td->num_received = 0;
284}
285
286
287
288static int mt_event(struct hid_device *hid, struct hid_field *field,
289 struct hid_usage *usage, __s32 value)
290{
291 struct mt_device *td = hid_get_drvdata(hid);
292
293 if (hid->claimed & HID_CLAIMED_INPUT) {
294 switch (usage->hid) {
295 case HID_DG_INRANGE:
296 break;
297 case HID_DG_TIPSWITCH:
298 td->curvalid = value;
299 td->curdata.touch_state = value;
300 break;
301 case HID_DG_CONFIDENCE:
302 break;
303 case HID_DG_CONTACTID:
304 td->curdata.contactid = value;
305 break;
306 case HID_DG_TIPPRESSURE:
307 td->curdata.p = value;
308 break;
309 case HID_GD_X:
310 td->curdata.x = value;
311 break;
312 case HID_GD_Y:
313 td->curdata.y = value;
314 break;
315 case HID_DG_WIDTH:
316 td->curdata.w = value;
317 break;
318 case HID_DG_HEIGHT:
319 td->curdata.h = value;
320 break;
321 case HID_DG_CONTACTCOUNT:
322 /*
323 * We must not overwrite the previous value (some
324 * devices send one sequence splitted over several
325 * messages)
326 */
327 if (value)
328 td->num_expected = value - 1;
329 break;
330
331 default:
332 /* fallback to the generic hidinput handling */
333 return 0;
334 }
335 }
336
337 if (usage->hid == td->last_slot_field)
338 mt_complete_slot(td);
339
340 if (field->index == td->last_field_index
341 && td->num_received > td->num_expected)
342 mt_emit_event(td, field->hidinput->input);
343
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);
347
348 return 1;
349}
350
351static void mt_set_input_mode(struct hid_device *hdev)
352{
353 struct mt_device *td = hid_get_drvdata(hdev);
354 struct hid_report *r;
355 struct hid_report_enum *re;
356
357 if (td->inputmode < 0)
358 return;
359
360 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
361 r = re->report_id_hash[td->inputmode];
362 if (r) {
363 r->field[0]->value[0] = 0x02;
364 usbhid_submit_report(hdev, r, USB_DIR_OUT);
365 }
366}
367
368static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
369{
370 int ret;
371 struct mt_device *td;
372 struct mt_class *mtclass = mt_classes + id->driver_data;
373
374 /* This allows the driver to correctly support devices
375 * that emit events over several HID messages.
376 */
377 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
378
379 td = kzalloc(sizeof(struct mt_device) +
380 mtclass->maxcontacts * sizeof(struct mt_slot),
381 GFP_KERNEL);
382 if (!td) {
383 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
384 return -ENOMEM;
385 }
386 td->mtclass = mtclass;
387 td->inputmode = -1;
388 hid_set_drvdata(hdev, td);
389
390 ret = hid_parse(hdev);
391 if (ret != 0)
392 goto fail;
393
394 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
395 if (ret != 0)
396 goto fail;
397
398 mt_set_input_mode(hdev);
399
400 return 0;
401
402fail:
403 kfree(td);
404 return ret;
405}
406
407#ifdef CONFIG_PM
408static int mt_reset_resume(struct hid_device *hdev)
409{
410 mt_set_input_mode(hdev);
411 return 0;
412}
413#endif
414
415static void mt_remove(struct hid_device *hdev)
416{
417 struct mt_device *td = hid_get_drvdata(hdev);
418 hid_hw_stop(hdev);
419 kfree(td);
420 hid_set_drvdata(hdev, NULL);
421}
422
423static const struct hid_device_id mt_devices[] = {
424
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) },
432
433 { }
434};
435MODULE_DEVICE_TABLE(hid, mt_devices);
436
437static 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}
440};
441
442static struct hid_driver mt_driver = {
443 .name = "hid-multitouch",
444 .id_table = mt_devices,
445 .probe = mt_probe,
446 .remove = mt_remove,
447 .input_mapping = mt_input_mapping,
448 .input_mapped = mt_input_mapped,
449 .feature_mapping = mt_feature_mapping,
450 .usage_table = mt_grabbed_usages,
451 .event = mt_event,
452#ifdef CONFIG_PM
453 .reset_resume = mt_reset_resume,
454#endif
455};
456
457static int __init mt_init(void)
458{
459 return hid_register_driver(&mt_driver);
460}
461
462static void __exit mt_exit(void)
463{
464 hid_unregister_driver(&mt_driver);
465}
466
467module_init(mt_init);
468module_exit(mt_exit);