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_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
28 MODULE_DESCRIPTION("HID multitouch panels");
29 MODULE_LICENSE("GPL");
33 /* quirks to control the device */
34 #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
35 #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
36 #define MT_QUIRK_CYPRESS (1 << 2)
37 #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
38 #define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
39 #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
43 __s32 contactid
; /* the device ContactID assigned to this slot */
44 bool touch_state
; /* is the touch valid? */
45 bool seen_in_this_frame
;/* has this slot been updated */
49 struct mt_slot curdata
; /* placeholder of incoming data */
50 struct mt_class
*mtclass
; /* our mt device class */
51 unsigned last_field_index
; /* last field index of the report */
52 unsigned last_slot_field
; /* the last field of a slot */
53 __s8 inputmode
; /* InputMode HID feature, -1 if non-existent */
54 __u8 num_received
; /* how many contacts we received */
55 __u8 num_expected
; /* expected last contact index */
56 bool curvalid
; /* is the current contact valid? */
57 struct mt_slot slots
[0]; /* first slot */
61 __s32 name
; /* MT_CLS */
63 __s32 sn_move
; /* Signal/noise ratio for move events */
64 __s32 sn_pressure
; /* Signal/noise ratio for pressure events */
68 /* classes of device behavior */
69 #define MT_CLS_DEFAULT 1
70 #define MT_CLS_DUAL_INRANGE_CONTACTID 2
71 #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3
72 #define MT_CLS_CYPRESS 4
75 * these device-dependent functions determine what slot corresponds
76 * to a valid contact that was just read.
79 static int cypress_compute_slot(struct mt_device
*td
)
81 if (td
->curdata
.contactid
!= 0 || td
->num_received
== 0)
82 return td
->curdata
.contactid
;
87 static int find_slot_from_contactid(struct mt_device
*td
)
90 for (i
= 0; i
< td
->mtclass
->maxcontacts
; ++i
) {
91 if (td
->slots
[i
].contactid
== td
->curdata
.contactid
&&
92 td
->slots
[i
].touch_state
)
95 for (i
= 0; i
< td
->mtclass
->maxcontacts
; ++i
) {
96 if (!td
->slots
[i
].seen_in_this_frame
&&
97 !td
->slots
[i
].touch_state
)
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. */
106 struct mt_class mt_classes
[] = {
107 { .name
= MT_CLS_DEFAULT
,
108 .quirks
= MT_QUIRK_NOT_SEEN_MEANS_UP
,
110 { .name
= MT_CLS_DUAL_INRANGE_CONTACTID
,
111 .quirks
= MT_QUIRK_VALID_IS_INRANGE
|
112 MT_QUIRK_SLOT_IS_CONTACTID
,
114 { .name
= MT_CLS_DUAL_INRANGE_CONTACTNUMBER
,
115 .quirks
= MT_QUIRK_VALID_IS_INRANGE
|
116 MT_QUIRK_SLOT_IS_CONTACTNUMBER
,
118 { .name
= MT_CLS_CYPRESS
,
119 .quirks
= MT_QUIRK_NOT_SEEN_MEANS_UP
|
126 static void mt_feature_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
127 struct hid_field
*field
, struct hid_usage
*usage
)
129 if (usage
->hid
== HID_DG_INPUTMODE
) {
130 struct mt_device
*td
= hid_get_drvdata(hdev
);
131 td
->inputmode
= field
->report
->id
;
135 static void set_abs(struct input_dev
*input
, unsigned int code
,
136 struct hid_field
*field
, int snratio
)
138 int fmin
= field
->logical_minimum
;
139 int fmax
= field
->logical_maximum
;
140 int fuzz
= snratio
? (fmax
- fmin
) / snratio
: 0;
141 input_set_abs_params(input
, code
, fmin
, fmax
, fuzz
, 0);
144 static int mt_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
145 struct hid_field
*field
, struct hid_usage
*usage
,
146 unsigned long **bit
, int *max
)
148 struct mt_device
*td
= hid_get_drvdata(hdev
);
149 struct mt_class
*cls
= td
->mtclass
;
150 switch (usage
->hid
& HID_USAGE_PAGE
) {
153 switch (usage
->hid
) {
155 hid_map_usage(hi
, usage
, bit
, max
,
156 EV_ABS
, ABS_MT_POSITION_X
);
157 set_abs(hi
->input
, ABS_MT_POSITION_X
, field
,
159 /* touchscreen emulation */
160 set_abs(hi
->input
, ABS_X
, field
, cls
->sn_move
);
161 td
->last_slot_field
= usage
->hid
;
164 hid_map_usage(hi
, usage
, bit
, max
,
165 EV_ABS
, ABS_MT_POSITION_Y
);
166 set_abs(hi
->input
, ABS_MT_POSITION_Y
, field
,
168 /* touchscreen emulation */
169 set_abs(hi
->input
, ABS_Y
, field
, cls
->sn_move
);
170 td
->last_slot_field
= usage
->hid
;
175 case HID_UP_DIGITIZER
:
176 switch (usage
->hid
) {
178 td
->last_slot_field
= usage
->hid
;
180 case HID_DG_CONFIDENCE
:
181 td
->last_slot_field
= usage
->hid
;
183 case HID_DG_TIPSWITCH
:
184 hid_map_usage(hi
, usage
, bit
, max
, EV_KEY
, BTN_TOUCH
);
185 input_set_capability(hi
->input
, EV_KEY
, BTN_TOUCH
);
186 td
->last_slot_field
= usage
->hid
;
188 case HID_DG_CONTACTID
:
189 input_mt_init_slots(hi
->input
,
190 td
->mtclass
->maxcontacts
);
191 td
->last_slot_field
= usage
->hid
;
194 hid_map_usage(hi
, usage
, bit
, max
,
195 EV_ABS
, ABS_MT_TOUCH_MAJOR
);
196 td
->last_slot_field
= usage
->hid
;
199 hid_map_usage(hi
, usage
, bit
, max
,
200 EV_ABS
, ABS_MT_TOUCH_MINOR
);
201 field
->logical_maximum
= 1;
202 field
->logical_minimum
= 0;
203 set_abs(hi
->input
, ABS_MT_ORIENTATION
, field
, 0);
204 td
->last_slot_field
= usage
->hid
;
206 case HID_DG_TIPPRESSURE
:
207 hid_map_usage(hi
, usage
, bit
, max
,
208 EV_ABS
, ABS_MT_PRESSURE
);
209 set_abs(hi
->input
, ABS_MT_PRESSURE
, field
,
211 /* touchscreen emulation */
212 set_abs(hi
->input
, ABS_PRESSURE
, field
,
214 td
->last_slot_field
= usage
->hid
;
216 case HID_DG_CONTACTCOUNT
:
217 td
->last_field_index
= field
->report
->maxfield
- 1;
219 case HID_DG_CONTACTMAX
:
220 /* we don't set td->last_slot_field as contactcount and
221 * contact max are global to the report */
224 /* let hid-input decide for the others */
228 /* we do not want to map these: no input-oriented meaning */
235 static int mt_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
236 struct hid_field
*field
, struct hid_usage
*usage
,
237 unsigned long **bit
, int *max
)
239 if (usage
->type
== EV_KEY
|| usage
->type
== EV_ABS
)
240 set_bit(usage
->type
, hi
->input
->evbit
);
245 static int mt_compute_slot(struct mt_device
*td
)
247 __s32 quirks
= td
->mtclass
->quirks
;
249 if (quirks
& MT_QUIRK_SLOT_IS_CONTACTID
)
250 return td
->curdata
.contactid
;
252 if (quirks
& MT_QUIRK_CYPRESS
)
253 return cypress_compute_slot(td
);
255 if (quirks
& MT_QUIRK_SLOT_IS_CONTACTNUMBER
)
256 return td
->num_received
;
258 return find_slot_from_contactid(td
);
262 * this function is called when a whole contact has been processed,
263 * so that it can assign it to a slot and store the data there
265 static void mt_complete_slot(struct mt_device
*td
)
267 td
->curdata
.seen_in_this_frame
= true;
269 int slotnum
= mt_compute_slot(td
);
271 if (slotnum
>= 0 && slotnum
< td
->mtclass
->maxcontacts
)
272 td
->slots
[slotnum
] = td
->curdata
;
279 * this function is called when a whole packet has been received and processed,
280 * so that it can decide what to send to the input layer.
282 static void mt_emit_event(struct mt_device
*td
, struct input_dev
*input
)
286 for (i
= 0; i
< td
->mtclass
->maxcontacts
; ++i
) {
287 struct mt_slot
*s
= &(td
->slots
[i
]);
288 if ((td
->mtclass
->quirks
& MT_QUIRK_NOT_SEEN_MEANS_UP
) &&
289 !s
->seen_in_this_frame
) {
290 s
->touch_state
= false;
293 input_mt_slot(input
, i
);
294 input_mt_report_slot_state(input
, MT_TOOL_FINGER
,
296 if (s
->touch_state
) {
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
);
303 s
->seen_in_this_frame
= false;
307 input_mt_report_pointer_emulation(input
, true);
309 td
->num_received
= 0;
314 static int mt_event(struct hid_device
*hid
, struct hid_field
*field
,
315 struct hid_usage
*usage
, __s32 value
)
317 struct mt_device
*td
= hid_get_drvdata(hid
);
318 __s32 quirks
= td
->mtclass
->quirks
;
320 if (hid
->claimed
& HID_CLAIMED_INPUT
) {
321 switch (usage
->hid
) {
323 if (quirks
& MT_QUIRK_VALID_IS_INRANGE
)
324 td
->curvalid
= value
;
326 case HID_DG_TIPSWITCH
:
327 if (quirks
& MT_QUIRK_NOT_SEEN_MEANS_UP
)
328 td
->curvalid
= value
;
329 td
->curdata
.touch_state
= value
;
331 case HID_DG_CONFIDENCE
:
332 if (quirks
& MT_QUIRK_VALID_IS_CONFIDENCE
)
333 td
->curvalid
= value
;
335 case HID_DG_CONTACTID
:
336 td
->curdata
.contactid
= value
;
338 case HID_DG_TIPPRESSURE
:
339 td
->curdata
.p
= value
;
342 td
->curdata
.x
= value
;
345 td
->curdata
.y
= value
;
348 td
->curdata
.w
= value
;
351 td
->curdata
.h
= value
;
353 case HID_DG_CONTACTCOUNT
:
355 * Includes multi-packet support where subsequent
356 * packets are sent with zero contactcount.
359 td
->num_expected
= value
;
363 /* fallback to the generic hidinput handling */
367 if (usage
->hid
== td
->last_slot_field
)
368 mt_complete_slot(td
);
370 if (field
->index
== td
->last_field_index
371 && td
->num_received
>= td
->num_expected
)
372 mt_emit_event(td
, field
->hidinput
->input
);
376 /* we have handled the hidinput part, now remains hiddev */
377 if (hid
->claimed
& HID_CLAIMED_HIDDEV
&& hid
->hiddev_hid_event
)
378 hid
->hiddev_hid_event(hid
, field
, usage
, value
);
383 static void mt_set_input_mode(struct hid_device
*hdev
)
385 struct mt_device
*td
= hid_get_drvdata(hdev
);
386 struct hid_report
*r
;
387 struct hid_report_enum
*re
;
389 if (td
->inputmode
< 0)
392 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
393 r
= re
->report_id_hash
[td
->inputmode
];
395 r
->field
[0]->value
[0] = 0x02;
396 usbhid_submit_report(hdev
, r
, USB_DIR_OUT
);
400 static int mt_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
403 struct mt_device
*td
;
404 struct mt_class
*mtclass
= mt_classes
; /* MT_CLS_DEFAULT */
406 for (i
= 0; mt_classes
[i
].name
; i
++) {
407 if (id
->driver_data
== mt_classes
[i
].name
) {
408 mtclass
= &(mt_classes
[i
]);
413 /* This allows the driver to correctly support devices
414 * that emit events over several HID messages.
416 hdev
->quirks
|= HID_QUIRK_NO_INPUT_SYNC
;
418 td
= kzalloc(sizeof(struct mt_device
) +
419 mtclass
->maxcontacts
* sizeof(struct mt_slot
),
422 dev_err(&hdev
->dev
, "cannot allocate multitouch data\n");
425 td
->mtclass
= mtclass
;
427 hid_set_drvdata(hdev
, td
);
429 ret
= hid_parse(hdev
);
433 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
437 mt_set_input_mode(hdev
);
447 static int mt_reset_resume(struct hid_device
*hdev
)
449 mt_set_input_mode(hdev
);
454 static void mt_remove(struct hid_device
*hdev
)
456 struct mt_device
*td
= hid_get_drvdata(hdev
);
459 hid_set_drvdata(hdev
, NULL
);
462 static const struct hid_device_id mt_devices
[] = {
465 { .driver_data
= MT_CLS_CYPRESS
,
466 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS
,
467 USB_DEVICE_ID_CYPRESS_TRUETOUCH
) },
469 /* GeneralTouch panel */
470 { .driver_data
= MT_CLS_DUAL_INRANGE_CONTACTNUMBER
,
471 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH
,
472 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS
) },
475 { .driver_data
= MT_CLS_DUAL_INRANGE_CONTACTID
,
476 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS
,
477 USB_DEVICE_ID_IRTOUCH_INFRARED_USB
) },
479 /* PixCir-based panels */
480 { .driver_data
= MT_CLS_DUAL_INRANGE_CONTACTID
,
481 HID_USB_DEVICE(USB_VENDOR_ID_HANVON
,
482 USB_DEVICE_ID_HANVON_MULTITOUCH
) },
483 { .driver_data
= MT_CLS_DUAL_INRANGE_CONTACTID
,
484 HID_USB_DEVICE(USB_VENDOR_ID_CANDO
,
485 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH
) },
489 MODULE_DEVICE_TABLE(hid
, mt_devices
);
491 static const struct hid_usage_id mt_grabbed_usages
[] = {
492 { HID_ANY_ID
, HID_ANY_ID
, HID_ANY_ID
},
493 { HID_ANY_ID
- 1, HID_ANY_ID
- 1, HID_ANY_ID
- 1}
496 static struct hid_driver mt_driver
= {
497 .name
= "hid-multitouch",
498 .id_table
= mt_devices
,
501 .input_mapping
= mt_input_mapping
,
502 .input_mapped
= mt_input_mapped
,
503 .feature_mapping
= mt_feature_mapping
,
504 .usage_table
= mt_grabbed_usages
,
507 .reset_resume
= mt_reset_resume
,
511 static int __init
mt_init(void)
513 return hid_register_driver(&mt_driver
);
516 static void __exit
mt_exit(void)
518 hid_unregister_driver(&mt_driver
);
521 module_init(mt_init
);
522 module_exit(mt_exit
);