2 * HID driver for eGalax dual-touch panels
4 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <linux/device.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/slab.h>
20 #include "usbhid/usbhid.h"
22 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
23 MODULE_DESCRIPTION("eGalax dual-touch panel");
24 MODULE_LICENSE("GPL");
31 bool first; /* is this the first finger in the frame? */
32 bool valid; /* valid finger data, or just placeholder? */
33 bool activity; /* at least one active finger previously? */
34 __u16 lastx, lasty, lastz; /* latest valid (x, y, z) in the frame */
37 static void set_abs(struct input_dev *input, unsigned int code,
38 struct hid_field *field, int snratio)
40 int fmin = field->logical_minimum;
41 int fmax = field->logical_maximum;
42 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
43 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
46 static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
47 struct hid_field *field, struct hid_usage *usage,
48 unsigned long **bit, int *max)
50 struct input_dev *input = hi->input;
52 switch (usage->hid & HID_USAGE_PAGE) {
57 field->logical_maximum = 32760;
58 hid_map_usage(hi, usage, bit, max,
59 EV_ABS, ABS_MT_POSITION_X);
60 set_abs(input, ABS_MT_POSITION_X, field, 0);
61 /* touchscreen emulation */
62 set_abs(input, ABS_X, field, 0);
65 field->logical_maximum = 32760;
66 hid_map_usage(hi, usage, bit, max,
67 EV_ABS, ABS_MT_POSITION_Y);
68 set_abs(input, ABS_MT_POSITION_Y, field, 0);
69 /* touchscreen emulation */
70 set_abs(input, ABS_Y, field, 0);
75 case HID_UP_DIGITIZER:
77 case HID_DG_TIPSWITCH:
78 /* touchscreen emulation */
79 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
80 input_set_capability(input, EV_KEY, BTN_TOUCH);
83 case HID_DG_CONFIDENCE:
84 case HID_DG_CONTACTCOUNT:
85 case HID_DG_CONTACTMAX:
87 case HID_DG_CONTACTID:
88 hid_map_usage(hi, usage, bit, max,
89 EV_ABS, ABS_MT_TRACKING_ID);
90 set_abs(input, ABS_MT_TRACKING_ID, field, 0);
92 case HID_DG_TIPPRESSURE:
93 hid_map_usage(hi, usage, bit, max,
94 EV_ABS, ABS_MT_PRESSURE);
95 set_abs(input, ABS_MT_PRESSURE, field, 0);
96 /* touchscreen emulation */
97 set_abs(input, ABS_PRESSURE, field, 0);
103 /* ignore others (from other reports we won't get anyway) */
107 static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
108 struct hid_field *field, struct hid_usage *usage,
109 unsigned long **bit, int *max)
111 /* tell hid-input to skip setup of these event types */
112 if (usage->type == EV_KEY || usage->type == EV_ABS)
113 set_bit(usage->type, hi->input->evbit);
118 * this function is called when a whole finger has been parsed,
119 * so that it can decide what to send to the input layer.
121 static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
123 td->first = !td->first; /* touchscreen emulation */
126 /* emit multitouch events */
127 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
128 input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x >> 3);
129 input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y >> 3);
130 input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
132 input_mt_sync(input);
135 * touchscreen emulation: store (x, y) as
136 * the last valid values in this frame
144 * touchscreen emulation: if this is the second finger and at least
145 * one in this frame is valid, the latest valid in the frame is
146 * the oldest on the panel, the one we want for single touch
148 if (!td->first && td->activity) {
149 input_event(input, EV_ABS, ABS_X, td->lastx >> 3);
150 input_event(input, EV_ABS, ABS_Y, td->lasty >> 3);
151 input_event(input, EV_ABS, ABS_PRESSURE, td->lastz);
156 * touchscreen emulation: if the first finger is invalid
157 * and there previously was finger activity, this is a release
159 if (td->first && td->activity) {
160 input_event(input, EV_KEY, BTN_TOUCH, 0);
161 td->activity = false;
167 /* touchscreen emulation: if no previous activity, emit touch event */
169 input_event(input, EV_KEY, BTN_TOUCH, 1);
175 static int egalax_event(struct hid_device *hid, struct hid_field *field,
176 struct hid_usage *usage, __s32 value)
178 struct egalax_data *td = hid_get_drvdata(hid);
180 /* Note, eGalax has two product lines: the first is resistive and
181 * uses a standard parallel multitouch protocol (product ID ==
182 * 48xx). The second is capacitive and uses an unusual "serial"
183 * protocol with a different message for each multitouch finger
184 * (product ID == 72xx). We do not yet generate a correct event
185 * sequence for the capacitive/serial protocol.
187 if (hid->claimed & HID_CLAIMED_INPUT) {
188 struct input_dev *input = field->hidinput->input;
190 switch (usage->hid) {
192 case HID_DG_CONFIDENCE:
193 /* avoid interference from generic hidinput handling */
195 case HID_DG_TIPSWITCH:
198 case HID_DG_TIPPRESSURE:
201 case HID_DG_CONTACTID:
209 /* this is the last field in a finger */
210 egalax_filter_event(td, input);
212 case HID_DG_CONTACTCOUNT:
213 /* touch emulation: this is the last field in a frame */
218 /* fallback to the generic hidinput handling */
223 /* we have handled the hidinput part, now remains hiddev */
224 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
225 hid->hiddev_hid_event(hid, field, usage, value);
230 static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
233 struct egalax_data *td;
234 struct hid_report *report;
236 td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
238 dev_err(&hdev->dev, "cannot allocate eGalax data\n");
241 hid_set_drvdata(hdev, td);
243 ret = hid_parse(hdev);
247 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
251 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
253 report->field[0]->value[0] = 2;
254 usbhid_submit_report(hdev, report, USB_DIR_OUT);
264 static void egalax_remove(struct hid_device *hdev)
267 kfree(hid_get_drvdata(hdev));
268 hid_set_drvdata(hdev, NULL);
271 static const struct hid_device_id egalax_devices[] = {
272 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
273 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
274 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
275 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
278 MODULE_DEVICE_TABLE(hid, egalax_devices);
280 static const struct hid_usage_id egalax_grabbed_usages[] = {
281 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
282 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
285 static struct hid_driver egalax_driver = {
286 .name = "egalax-touch",
287 .id_table = egalax_devices,
288 .probe = egalax_probe,
289 .remove = egalax_remove,
290 .input_mapping = egalax_input_mapping,
291 .input_mapped = egalax_input_mapped,
292 .usage_table = egalax_grabbed_usages,
293 .event = egalax_event,
296 static int __init egalax_init(void)
298 return hid_register_driver(&egalax_driver);
301 static void __exit egalax_exit(void)
303 hid_unregister_driver(&egalax_driver);
306 module_init(egalax_init);
307 module_exit(egalax_exit);