ocfs2: Make the left masklogs compat.
[taoma-kernel.git] / drivers / hid / hid-multitouch.c
blob07d3183fdde50ca4fd3a57807e727b73094b3e6b
1 /*
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
8 */
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.
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");
30 #include "hid-ids.h"
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)
37 #define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
38 #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
40 struct mt_slot {
41 __s32 x, y, p, w, h;
42 __s32 contactid; /* the device ContactID assigned to this slot */
43 bool touch_state; /* is the touch valid? */
44 bool seen_in_this_frame;/* has this slot been updated */
47 struct mt_device {
48 struct mt_slot curdata; /* placeholder of incoming data */
49 struct mt_class *mtclass; /* our mt device class */
50 unsigned last_field_index; /* last field index of the report */
51 unsigned last_slot_field; /* the last field of a slot */
52 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
53 __u8 num_received; /* how many contacts we received */
54 __u8 num_expected; /* expected last contact index */
55 bool curvalid; /* is the current contact valid? */
56 struct mt_slot slots[0]; /* first slot */
59 struct mt_class {
60 __s32 name; /* MT_CLS */
61 __s32 quirks;
62 __s32 sn_move; /* Signal/noise ratio for move events */
63 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
64 __u8 maxcontacts;
67 /* classes of device behavior */
68 #define MT_CLS_DEFAULT 1
69 #define MT_CLS_DUAL1 2
70 #define MT_CLS_DUAL2 3
71 #define MT_CLS_CYPRESS 4
74 * these device-dependent functions determine what slot corresponds
75 * to a valid contact that was just read.
78 static int cypress_compute_slot(struct mt_device *td)
80 if (td->curdata.contactid != 0 || td->num_received == 0)
81 return td->curdata.contactid;
82 else
83 return -1;
86 static int find_slot_from_contactid(struct mt_device *td)
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;
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;
99 /* should not occurs. If this happens that means
100 * that the device sent more touches that it says
101 * in the report descriptor. It is ignored then. */
102 return -1;
105 struct mt_class mt_classes[] = {
106 { .name = MT_CLS_DEFAULT,
107 .quirks = MT_QUIRK_VALID_IS_INRANGE,
108 .maxcontacts = 10 },
109 { .name = MT_CLS_DUAL1,
110 .quirks = MT_QUIRK_VALID_IS_INRANGE |
111 MT_QUIRK_SLOT_IS_CONTACTID,
112 .maxcontacts = 2 },
113 { .name = MT_CLS_DUAL2,
114 .quirks = MT_QUIRK_VALID_IS_INRANGE |
115 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
116 .maxcontacts = 2 },
117 { .name = MT_CLS_CYPRESS,
118 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
119 MT_QUIRK_CYPRESS,
120 .maxcontacts = 10 },
125 static void mt_feature_mapping(struct hid_device *hdev, struct hid_input *hi,
126 struct hid_field *field, struct hid_usage *usage)
128 if (usage->hid == HID_DG_INPUTMODE) {
129 struct mt_device *td = hid_get_drvdata(hdev);
130 td->inputmode = field->report->id;
134 static void set_abs(struct input_dev *input, unsigned int code,
135 struct hid_field *field, int snratio)
137 int fmin = field->logical_minimum;
138 int fmax = field->logical_maximum;
139 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
140 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
143 static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
144 struct hid_field *field, struct hid_usage *usage,
145 unsigned long **bit, int *max)
147 struct mt_device *td = hid_get_drvdata(hdev);
148 struct mt_class *cls = td->mtclass;
149 switch (usage->hid & HID_USAGE_PAGE) {
151 case HID_UP_GENDESK:
152 switch (usage->hid) {
153 case HID_GD_X:
154 hid_map_usage(hi, usage, bit, max,
155 EV_ABS, ABS_MT_POSITION_X);
156 set_abs(hi->input, ABS_MT_POSITION_X, field,
157 cls->sn_move);
158 /* touchscreen emulation */
159 set_abs(hi->input, ABS_X, field, cls->sn_move);
160 td->last_slot_field = usage->hid;
161 return 1;
162 case HID_GD_Y:
163 hid_map_usage(hi, usage, bit, max,
164 EV_ABS, ABS_MT_POSITION_Y);
165 set_abs(hi->input, ABS_MT_POSITION_Y, field,
166 cls->sn_move);
167 /* touchscreen emulation */
168 set_abs(hi->input, ABS_Y, field, cls->sn_move);
169 td->last_slot_field = usage->hid;
170 return 1;
172 return 0;
174 case HID_UP_DIGITIZER:
175 switch (usage->hid) {
176 case HID_DG_INRANGE:
177 td->last_slot_field = usage->hid;
178 return 1;
179 case HID_DG_CONFIDENCE:
180 td->last_slot_field = usage->hid;
181 return 1;
182 case HID_DG_TIPSWITCH:
183 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
184 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
185 td->last_slot_field = usage->hid;
186 return 1;
187 case HID_DG_CONTACTID:
188 input_mt_init_slots(hi->input,
189 td->mtclass->maxcontacts);
190 td->last_slot_field = usage->hid;
191 return 1;
192 case HID_DG_WIDTH:
193 hid_map_usage(hi, usage, bit, max,
194 EV_ABS, ABS_MT_TOUCH_MAJOR);
195 td->last_slot_field = usage->hid;
196 return 1;
197 case HID_DG_HEIGHT:
198 hid_map_usage(hi, usage, bit, max,
199 EV_ABS, ABS_MT_TOUCH_MINOR);
200 field->logical_maximum = 1;
201 field->logical_minimum = 0;
202 set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
203 td->last_slot_field = usage->hid;
204 return 1;
205 case HID_DG_TIPPRESSURE:
206 hid_map_usage(hi, usage, bit, max,
207 EV_ABS, ABS_MT_PRESSURE);
208 set_abs(hi->input, ABS_MT_PRESSURE, field,
209 cls->sn_pressure);
210 /* touchscreen emulation */
211 set_abs(hi->input, ABS_PRESSURE, field,
212 cls->sn_pressure);
213 td->last_slot_field = usage->hid;
214 return 1;
215 case HID_DG_CONTACTCOUNT:
216 td->last_field_index = field->report->maxfield - 1;
217 return 1;
218 case HID_DG_CONTACTMAX:
219 /* we don't set td->last_slot_field as contactcount and
220 * contact max are global to the report */
221 return -1;
223 /* let hid-input decide for the others */
224 return 0;
226 case 0xff000000:
227 /* we do not want to map these: no input-oriented meaning */
228 return -1;
231 return 0;
234 static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
235 struct hid_field *field, struct hid_usage *usage,
236 unsigned long **bit, int *max)
238 if (usage->type == EV_KEY || usage->type == EV_ABS)
239 set_bit(usage->type, hi->input->evbit);
241 return -1;
244 static int mt_compute_slot(struct mt_device *td)
246 __s32 quirks = td->mtclass->quirks;
248 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
249 return td->curdata.contactid;
251 if (quirks & MT_QUIRK_CYPRESS)
252 return cypress_compute_slot(td);
254 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
255 return td->num_received;
257 return find_slot_from_contactid(td);
261 * this function is called when a whole contact has been processed,
262 * so that it can assign it to a slot and store the data there
264 static void mt_complete_slot(struct mt_device *td)
266 td->curdata.seen_in_this_frame = true;
267 if (td->curvalid) {
268 int slotnum = mt_compute_slot(td);
270 if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
271 td->slots[slotnum] = td->curdata;
273 td->num_received++;
278 * this function is called when a whole packet has been received and processed,
279 * so that it can decide what to send to the input layer.
281 static void mt_emit_event(struct mt_device *td, struct input_dev *input)
283 int i;
285 for (i = 0; i < td->mtclass->maxcontacts; ++i) {
286 struct mt_slot *s = &(td->slots[i]);
287 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
288 !s->seen_in_this_frame) {
289 s->touch_state = false;
292 input_mt_slot(input, i);
293 input_mt_report_slot_state(input, MT_TOOL_FINGER,
294 s->touch_state);
295 if (s->touch_state) {
296 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
297 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
298 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
299 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
300 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);
307 input_sync(input);
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);
317 __s32 quirks = td->mtclass->quirks;
319 if (hid->claimed & HID_CLAIMED_INPUT) {
320 switch (usage->hid) {
321 case HID_DG_INRANGE:
322 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
323 td->curvalid = value;
324 break;
325 case HID_DG_TIPSWITCH:
326 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
327 td->curvalid = value;
328 td->curdata.touch_state = value;
329 break;
330 case HID_DG_CONFIDENCE:
331 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
332 td->curvalid = value;
333 break;
334 case HID_DG_CONTACTID:
335 td->curdata.contactid = value;
336 break;
337 case HID_DG_TIPPRESSURE:
338 td->curdata.p = value;
339 break;
340 case HID_GD_X:
341 td->curdata.x = value;
342 break;
343 case HID_GD_Y:
344 td->curdata.y = value;
345 break;
346 case HID_DG_WIDTH:
347 td->curdata.w = value;
348 break;
349 case HID_DG_HEIGHT:
350 td->curdata.h = value;
351 break;
352 case HID_DG_CONTACTCOUNT:
354 * Includes multi-packet support where subsequent
355 * packets are sent with zero contactcount.
357 if (value)
358 td->num_expected = value;
359 break;
361 default:
362 /* fallback to the generic hidinput handling */
363 return 0;
366 if (usage->hid == td->last_slot_field)
367 mt_complete_slot(td);
369 if (field->index == td->last_field_index
370 && td->num_received >= td->num_expected)
371 mt_emit_event(td, field->hidinput->input);
375 /* we have handled the hidinput part, now remains hiddev */
376 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
377 hid->hiddev_hid_event(hid, field, usage, value);
379 return 1;
382 static void mt_set_input_mode(struct hid_device *hdev)
384 struct mt_device *td = hid_get_drvdata(hdev);
385 struct hid_report *r;
386 struct hid_report_enum *re;
388 if (td->inputmode < 0)
389 return;
391 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
392 r = re->report_id_hash[td->inputmode];
393 if (r) {
394 r->field[0]->value[0] = 0x02;
395 usbhid_submit_report(hdev, r, USB_DIR_OUT);
399 static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
401 int ret, i;
402 struct mt_device *td;
403 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
405 for (i = 0; mt_classes[i].name ; i++) {
406 if (id->driver_data == mt_classes[i].name) {
407 mtclass = &(mt_classes[i]);
408 break;
412 /* This allows the driver to correctly support devices
413 * that emit events over several HID messages.
415 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
417 td = kzalloc(sizeof(struct mt_device) +
418 mtclass->maxcontacts * sizeof(struct mt_slot),
419 GFP_KERNEL);
420 if (!td) {
421 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
422 return -ENOMEM;
424 td->mtclass = mtclass;
425 td->inputmode = -1;
426 hid_set_drvdata(hdev, td);
428 ret = hid_parse(hdev);
429 if (ret != 0)
430 goto fail;
432 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
433 if (ret)
434 goto fail;
436 mt_set_input_mode(hdev);
438 return 0;
440 fail:
441 kfree(td);
442 return ret;
445 #ifdef CONFIG_PM
446 static int mt_reset_resume(struct hid_device *hdev)
448 mt_set_input_mode(hdev);
449 return 0;
451 #endif
453 static void mt_remove(struct hid_device *hdev)
455 struct mt_device *td = hid_get_drvdata(hdev);
456 hid_hw_stop(hdev);
457 kfree(td);
458 hid_set_drvdata(hdev, NULL);
461 static const struct hid_device_id mt_devices[] = {
463 /* Cypress panel */
464 { .driver_data = MT_CLS_CYPRESS,
465 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
466 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
468 /* GeneralTouch panel */
469 { .driver_data = MT_CLS_DUAL2,
470 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
471 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
473 /* PixCir-based panels */
474 { .driver_data = MT_CLS_DUAL1,
475 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
476 USB_DEVICE_ID_HANVON_MULTITOUCH) },
477 { .driver_data = MT_CLS_DUAL1,
478 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
479 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
483 MODULE_DEVICE_TABLE(hid, mt_devices);
485 static const struct hid_usage_id mt_grabbed_usages[] = {
486 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
487 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
490 static struct hid_driver mt_driver = {
491 .name = "hid-multitouch",
492 .id_table = mt_devices,
493 .probe = mt_probe,
494 .remove = mt_remove,
495 .input_mapping = mt_input_mapping,
496 .input_mapped = mt_input_mapped,
497 .feature_mapping = mt_feature_mapping,
498 .usage_table = mt_grabbed_usages,
499 .event = mt_event,
500 #ifdef CONFIG_PM
501 .reset_resume = mt_reset_resume,
502 #endif
505 static int __init mt_init(void)
507 return hid_register_driver(&mt_driver);
510 static void __exit mt_exit(void)
512 hid_unregister_driver(&mt_driver);
515 module_init(mt_init);
516 module_exit(mt_exit);