blob: aac8a6326bbd22f689691bed99335835e80395ee [file] [log] [blame]
Stephane Chatty0c3910c2010-04-10 16:43:08 +02001/*
2 * HID driver for eGalax dual-touch panels
3 *
4 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
5 *
6 */
7
8/*
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)
12 * any later version.
13 */
14
15#include <linux/device.h>
16#include <linux/hid.h>
17#include <linux/module.h>
18#include <linux/usb.h>
Jiri Kosinac872b0f2010-04-10 21:29:09 +020019#include <linux/slab.h>
Stephane Chatty0c3910c2010-04-10 16:43:08 +020020#include "usbhid/usbhid.h"
21
22MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
23MODULE_DESCRIPTION("eGalax dual-touch panel");
24MODULE_LICENSE("GPL");
25
26#include "hid-ids.h"
27
28struct egalax_data {
29 __u16 x, y, z;
30 __u8 id;
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? */
Philipp Merkelf5166112010-10-01 15:38:59 +020034 __u16 lastx, lasty, lastz; /* latest valid (x, y, z) in the frame */
Stephane Chatty0c3910c2010-04-10 16:43:08 +020035};
36
Henrik Rydberg4a864182010-11-27 17:56:17 +010037static void set_abs(struct input_dev *input, unsigned int code,
38 struct hid_field *field, int snratio)
39{
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);
44}
45
Stephane Chatty0c3910c2010-04-10 16:43:08 +020046static 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)
49{
Henrik Rydberg4a864182010-11-27 17:56:17 +010050 struct input_dev *input = hi->input;
51
Stephane Chatty0c3910c2010-04-10 16:43:08 +020052 switch (usage->hid & HID_USAGE_PAGE) {
53
54 case HID_UP_GENDESK:
55 switch (usage->hid) {
56 case HID_GD_X:
Henrik Rydbergb88cbd32010-10-13 22:18:32 +020057 field->logical_maximum = 32760;
Stephane Chatty0c3910c2010-04-10 16:43:08 +020058 hid_map_usage(hi, usage, bit, max,
59 EV_ABS, ABS_MT_POSITION_X);
Henrik Rydberg4a864182010-11-27 17:56:17 +010060 set_abs(input, ABS_MT_POSITION_X, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020061 /* touchscreen emulation */
Henrik Rydberg4a864182010-11-27 17:56:17 +010062 set_abs(input, ABS_X, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020063 return 1;
64 case HID_GD_Y:
Henrik Rydbergb88cbd32010-10-13 22:18:32 +020065 field->logical_maximum = 32760;
Stephane Chatty0c3910c2010-04-10 16:43:08 +020066 hid_map_usage(hi, usage, bit, max,
67 EV_ABS, ABS_MT_POSITION_Y);
Henrik Rydberg4a864182010-11-27 17:56:17 +010068 set_abs(input, ABS_MT_POSITION_Y, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020069 /* touchscreen emulation */
Henrik Rydberg4a864182010-11-27 17:56:17 +010070 set_abs(input, ABS_Y, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020071 return 1;
72 }
73 return 0;
74
75 case HID_UP_DIGITIZER:
76 switch (usage->hid) {
77 case HID_DG_TIPSWITCH:
78 /* touchscreen emulation */
79 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
Henrik Rydberg4a864182010-11-27 17:56:17 +010080 input_set_capability(input, EV_KEY, BTN_TOUCH);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020081 return 1;
82 case HID_DG_INRANGE:
83 case HID_DG_CONFIDENCE:
84 case HID_DG_CONTACTCOUNT:
85 case HID_DG_CONTACTMAX:
86 return -1;
87 case HID_DG_CONTACTID:
88 hid_map_usage(hi, usage, bit, max,
89 EV_ABS, ABS_MT_TRACKING_ID);
Henrik Rydberg4a864182010-11-27 17:56:17 +010090 set_abs(input, ABS_MT_TRACKING_ID, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020091 return 1;
92 case HID_DG_TIPPRESSURE:
Henrik Rydberg80a469e2010-10-11 21:16:43 +020093 field->logical_minimum = 0;
Stephane Chatty0c3910c2010-04-10 16:43:08 +020094 hid_map_usage(hi, usage, bit, max,
95 EV_ABS, ABS_MT_PRESSURE);
Henrik Rydberg4a864182010-11-27 17:56:17 +010096 set_abs(input, ABS_MT_PRESSURE, field, 0);
Philipp Merkelf5166112010-10-01 15:38:59 +020097 /* touchscreen emulation */
Henrik Rydberg4a864182010-11-27 17:56:17 +010098 set_abs(input, ABS_PRESSURE, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020099 return 1;
100 }
101 return 0;
102 }
103
104 /* ignore others (from other reports we won't get anyway) */
105 return -1;
106}
107
108static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
109 struct hid_field *field, struct hid_usage *usage,
110 unsigned long **bit, int *max)
111{
Henrik Rydberg4a864182010-11-27 17:56:17 +0100112 /* tell hid-input to skip setup of these event types */
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200113 if (usage->type == EV_KEY || usage->type == EV_ABS)
Henrik Rydberg4a864182010-11-27 17:56:17 +0100114 set_bit(usage->type, hi->input->evbit);
115 return -1;
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200116}
117
118/*
119 * this function is called when a whole finger has been parsed,
120 * so that it can decide what to send to the input layer.
121 */
122static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
123{
124 td->first = !td->first; /* touchscreen emulation */
125
126 if (td->valid) {
127 /* emit multitouch events */
128 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
Philipp Merkelf5166112010-10-01 15:38:59 +0200129 input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x >> 3);
130 input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y >> 3);
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200131 input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
132
133 input_mt_sync(input);
134
135 /*
136 * touchscreen emulation: store (x, y) as
137 * the last valid values in this frame
138 */
139 td->lastx = td->x;
140 td->lasty = td->y;
Philipp Merkelf5166112010-10-01 15:38:59 +0200141 td->lastz = td->z;
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200142 }
143
144 /*
145 * touchscreen emulation: if this is the second finger and at least
146 * one in this frame is valid, the latest valid in the frame is
147 * the oldest on the panel, the one we want for single touch
148 */
149 if (!td->first && td->activity) {
Philipp Merkelf5166112010-10-01 15:38:59 +0200150 input_event(input, EV_ABS, ABS_X, td->lastx >> 3);
151 input_event(input, EV_ABS, ABS_Y, td->lasty >> 3);
152 input_event(input, EV_ABS, ABS_PRESSURE, td->lastz);
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200153 }
154
155 if (!td->valid) {
156 /*
157 * touchscreen emulation: if the first finger is invalid
158 * and there previously was finger activity, this is a release
159 */
160 if (td->first && td->activity) {
161 input_event(input, EV_KEY, BTN_TOUCH, 0);
162 td->activity = false;
163 }
164 return;
165 }
166
167
168 /* touchscreen emulation: if no previous activity, emit touch event */
169 if (!td->activity) {
170 input_event(input, EV_KEY, BTN_TOUCH, 1);
171 td->activity = true;
172 }
173}
174
175
176static int egalax_event(struct hid_device *hid, struct hid_field *field,
177 struct hid_usage *usage, __s32 value)
178{
179 struct egalax_data *td = hid_get_drvdata(hid);
180
Chris Ball83e44912010-08-16 16:01:28 +0200181 /* Note, eGalax has two product lines: the first is resistive and
182 * uses a standard parallel multitouch protocol (product ID ==
183 * 48xx). The second is capacitive and uses an unusual "serial"
184 * protocol with a different message for each multitouch finger
185 * (product ID == 72xx). We do not yet generate a correct event
186 * sequence for the capacitive/serial protocol.
187 */
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200188 if (hid->claimed & HID_CLAIMED_INPUT) {
189 struct input_dev *input = field->hidinput->input;
190
191 switch (usage->hid) {
192 case HID_DG_INRANGE:
193 case HID_DG_CONFIDENCE:
194 /* avoid interference from generic hidinput handling */
195 break;
196 case HID_DG_TIPSWITCH:
197 td->valid = value;
198 break;
199 case HID_DG_TIPPRESSURE:
200 td->z = value;
201 break;
202 case HID_DG_CONTACTID:
203 td->id = value;
204 break;
205 case HID_GD_X:
206 td->x = value;
207 break;
208 case HID_GD_Y:
209 td->y = value;
210 /* this is the last field in a finger */
211 egalax_filter_event(td, input);
212 break;
213 case HID_DG_CONTACTCOUNT:
214 /* touch emulation: this is the last field in a frame */
215 td->first = false;
216 break;
217
218 default:
219 /* fallback to the generic hidinput handling */
220 return 0;
221 }
222 }
223
224 /* we have handled the hidinput part, now remains hiddev */
225 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
226 hid->hiddev_hid_event(hid, field, usage, value);
227
228 return 1;
229}
230
231static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
232{
233 int ret;
234 struct egalax_data *td;
235 struct hid_report *report;
236
237 td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
238 if (!td) {
239 dev_err(&hdev->dev, "cannot allocate eGalax data\n");
240 return -ENOMEM;
241 }
242 hid_set_drvdata(hdev, td);
243
244 ret = hid_parse(hdev);
245 if (ret)
246 goto end;
247
248 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
249 if (ret)
250 goto end;
251
252 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
253 if (report) {
254 report->field[0]->value[0] = 2;
255 usbhid_submit_report(hdev, report, USB_DIR_OUT);
256 }
257
258end:
259 if (ret)
260 kfree(td);
261
262 return ret;
263}
264
265static void egalax_remove(struct hid_device *hdev)
266{
267 hid_hw_stop(hdev);
268 kfree(hid_get_drvdata(hdev));
269 hid_set_drvdata(hdev, NULL);
270}
271
272static const struct hid_device_id egalax_devices[] = {
273 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
274 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
Chris Ball83e44912010-08-16 16:01:28 +0200275 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
276 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200277 { }
278};
279MODULE_DEVICE_TABLE(hid, egalax_devices);
280
281static const struct hid_usage_id egalax_grabbed_usages[] = {
282 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
283 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
284};
285
286static struct hid_driver egalax_driver = {
287 .name = "egalax-touch",
288 .id_table = egalax_devices,
289 .probe = egalax_probe,
290 .remove = egalax_remove,
291 .input_mapping = egalax_input_mapping,
292 .input_mapped = egalax_input_mapped,
293 .usage_table = egalax_grabbed_usages,
294 .event = egalax_event,
295};
296
297static int __init egalax_init(void)
298{
299 return hid_register_driver(&egalax_driver);
300}
301
302static void __exit egalax_exit(void)
303{
304 hid_unregister_driver(&egalax_driver);
305}
306
307module_init(egalax_init);
308module_exit(egalax_exit);
309