blob: 66b2c5d919af5f743556cad55c93aed8b83c33fc [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
Henrik Rydberg17c76062010-10-11 21:20:51 +020028/* estimated signal-to-noise ratios */
29#define SN_MOVE 4096
30#define SN_PRESSURE 32
31
Stephane Chatty0c3910c2010-04-10 16:43:08 +020032struct egalax_data {
33 __u16 x, y, z;
34 __u8 id;
35 bool first; /* is this the first finger in the frame? */
36 bool valid; /* valid finger data, or just placeholder? */
37 bool activity; /* at least one active finger previously? */
Philipp Merkelf5166112010-10-01 15:38:59 +020038 __u16 lastx, lasty, lastz; /* latest valid (x, y, z) in the frame */
Stephane Chatty0c3910c2010-04-10 16:43:08 +020039};
40
Henrik Rydberg4a864182010-11-27 17:56:17 +010041static void set_abs(struct input_dev *input, unsigned int code,
42 struct hid_field *field, int snratio)
43{
44 int fmin = field->logical_minimum;
45 int fmax = field->logical_maximum;
46 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
47 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
48}
49
Stephane Chatty0c3910c2010-04-10 16:43:08 +020050static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
51 struct hid_field *field, struct hid_usage *usage,
52 unsigned long **bit, int *max)
53{
Henrik Rydberg4a864182010-11-27 17:56:17 +010054 struct input_dev *input = hi->input;
55
Stephane Chatty0c3910c2010-04-10 16:43:08 +020056 switch (usage->hid & HID_USAGE_PAGE) {
57
58 case HID_UP_GENDESK:
59 switch (usage->hid) {
60 case HID_GD_X:
Henrik Rydbergb88cbd32010-10-13 22:18:32 +020061 field->logical_maximum = 32760;
Stephane Chatty0c3910c2010-04-10 16:43:08 +020062 hid_map_usage(hi, usage, bit, max,
63 EV_ABS, ABS_MT_POSITION_X);
Henrik Rydberg17c76062010-10-11 21:20:51 +020064 set_abs(input, ABS_MT_POSITION_X, field, SN_MOVE);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020065 /* touchscreen emulation */
Henrik Rydberg17c76062010-10-11 21:20:51 +020066 set_abs(input, ABS_X, field, SN_MOVE);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020067 return 1;
68 case HID_GD_Y:
Henrik Rydbergb88cbd32010-10-13 22:18:32 +020069 field->logical_maximum = 32760;
Stephane Chatty0c3910c2010-04-10 16:43:08 +020070 hid_map_usage(hi, usage, bit, max,
71 EV_ABS, ABS_MT_POSITION_Y);
Henrik Rydberg17c76062010-10-11 21:20:51 +020072 set_abs(input, ABS_MT_POSITION_Y, field, SN_MOVE);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020073 /* touchscreen emulation */
Henrik Rydberg17c76062010-10-11 21:20:51 +020074 set_abs(input, ABS_Y, field, SN_MOVE);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020075 return 1;
76 }
77 return 0;
78
79 case HID_UP_DIGITIZER:
80 switch (usage->hid) {
81 case HID_DG_TIPSWITCH:
82 /* touchscreen emulation */
83 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
Henrik Rydberg4a864182010-11-27 17:56:17 +010084 input_set_capability(input, EV_KEY, BTN_TOUCH);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020085 return 1;
86 case HID_DG_INRANGE:
87 case HID_DG_CONFIDENCE:
88 case HID_DG_CONTACTCOUNT:
89 case HID_DG_CONTACTMAX:
90 return -1;
91 case HID_DG_CONTACTID:
92 hid_map_usage(hi, usage, bit, max,
93 EV_ABS, ABS_MT_TRACKING_ID);
Henrik Rydberg4a864182010-11-27 17:56:17 +010094 set_abs(input, ABS_MT_TRACKING_ID, field, 0);
Stephane Chatty0c3910c2010-04-10 16:43:08 +020095 return 1;
96 case HID_DG_TIPPRESSURE:
Henrik Rydberg80a469e2010-10-11 21:16:43 +020097 field->logical_minimum = 0;
Stephane Chatty0c3910c2010-04-10 16:43:08 +020098 hid_map_usage(hi, usage, bit, max,
99 EV_ABS, ABS_MT_PRESSURE);
Henrik Rydberg17c76062010-10-11 21:20:51 +0200100 set_abs(input, ABS_MT_PRESSURE, field, SN_PRESSURE);
Philipp Merkelf5166112010-10-01 15:38:59 +0200101 /* touchscreen emulation */
Henrik Rydberg17c76062010-10-11 21:20:51 +0200102 set_abs(input, ABS_PRESSURE, field, SN_PRESSURE);
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200103 return 1;
104 }
105 return 0;
106 }
107
108 /* ignore others (from other reports we won't get anyway) */
109 return -1;
110}
111
112static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
113 struct hid_field *field, struct hid_usage *usage,
114 unsigned long **bit, int *max)
115{
Henrik Rydberg4a864182010-11-27 17:56:17 +0100116 /* tell hid-input to skip setup of these event types */
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200117 if (usage->type == EV_KEY || usage->type == EV_ABS)
Henrik Rydberg4a864182010-11-27 17:56:17 +0100118 set_bit(usage->type, hi->input->evbit);
119 return -1;
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200120}
121
122/*
123 * this function is called when a whole finger has been parsed,
124 * so that it can decide what to send to the input layer.
125 */
126static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
127{
128 td->first = !td->first; /* touchscreen emulation */
129
130 if (td->valid) {
131 /* emit multitouch events */
132 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
Philipp Merkelf5166112010-10-01 15:38:59 +0200133 input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x >> 3);
134 input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y >> 3);
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200135 input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
136
137 input_mt_sync(input);
138
139 /*
140 * touchscreen emulation: store (x, y) as
141 * the last valid values in this frame
142 */
143 td->lastx = td->x;
144 td->lasty = td->y;
Philipp Merkelf5166112010-10-01 15:38:59 +0200145 td->lastz = td->z;
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200146 }
147
148 /*
149 * touchscreen emulation: if this is the second finger and at least
150 * one in this frame is valid, the latest valid in the frame is
151 * the oldest on the panel, the one we want for single touch
152 */
153 if (!td->first && td->activity) {
Philipp Merkelf5166112010-10-01 15:38:59 +0200154 input_event(input, EV_ABS, ABS_X, td->lastx >> 3);
155 input_event(input, EV_ABS, ABS_Y, td->lasty >> 3);
156 input_event(input, EV_ABS, ABS_PRESSURE, td->lastz);
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200157 }
158
159 if (!td->valid) {
160 /*
161 * touchscreen emulation: if the first finger is invalid
162 * and there previously was finger activity, this is a release
163 */
164 if (td->first && td->activity) {
165 input_event(input, EV_KEY, BTN_TOUCH, 0);
166 td->activity = false;
167 }
168 return;
169 }
170
171
172 /* touchscreen emulation: if no previous activity, emit touch event */
173 if (!td->activity) {
174 input_event(input, EV_KEY, BTN_TOUCH, 1);
175 td->activity = true;
176 }
177}
178
179
180static int egalax_event(struct hid_device *hid, struct hid_field *field,
181 struct hid_usage *usage, __s32 value)
182{
183 struct egalax_data *td = hid_get_drvdata(hid);
184
Chris Ball83e44912010-08-16 16:01:28 +0200185 /* Note, eGalax has two product lines: the first is resistive and
186 * uses a standard parallel multitouch protocol (product ID ==
187 * 48xx). The second is capacitive and uses an unusual "serial"
188 * protocol with a different message for each multitouch finger
189 * (product ID == 72xx). We do not yet generate a correct event
190 * sequence for the capacitive/serial protocol.
191 */
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200192 if (hid->claimed & HID_CLAIMED_INPUT) {
193 struct input_dev *input = field->hidinput->input;
194
195 switch (usage->hid) {
196 case HID_DG_INRANGE:
197 case HID_DG_CONFIDENCE:
198 /* avoid interference from generic hidinput handling */
199 break;
200 case HID_DG_TIPSWITCH:
201 td->valid = value;
202 break;
203 case HID_DG_TIPPRESSURE:
204 td->z = value;
205 break;
206 case HID_DG_CONTACTID:
207 td->id = value;
208 break;
209 case HID_GD_X:
210 td->x = value;
211 break;
212 case HID_GD_Y:
213 td->y = value;
214 /* this is the last field in a finger */
215 egalax_filter_event(td, input);
216 break;
217 case HID_DG_CONTACTCOUNT:
218 /* touch emulation: this is the last field in a frame */
219 td->first = false;
220 break;
221
222 default:
223 /* fallback to the generic hidinput handling */
224 return 0;
225 }
226 }
227
228 /* we have handled the hidinput part, now remains hiddev */
229 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
230 hid->hiddev_hid_event(hid, field, usage, value);
231
232 return 1;
233}
234
235static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
236{
237 int ret;
238 struct egalax_data *td;
239 struct hid_report *report;
240
241 td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
242 if (!td) {
243 dev_err(&hdev->dev, "cannot allocate eGalax data\n");
244 return -ENOMEM;
245 }
246 hid_set_drvdata(hdev, td);
247
248 ret = hid_parse(hdev);
249 if (ret)
250 goto end;
251
252 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
253 if (ret)
254 goto end;
255
256 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
257 if (report) {
258 report->field[0]->value[0] = 2;
259 usbhid_submit_report(hdev, report, USB_DIR_OUT);
260 }
261
262end:
263 if (ret)
264 kfree(td);
265
266 return ret;
267}
268
269static void egalax_remove(struct hid_device *hdev)
270{
271 hid_hw_stop(hdev);
272 kfree(hid_get_drvdata(hdev));
273 hid_set_drvdata(hdev, NULL);
274}
275
276static const struct hid_device_id egalax_devices[] = {
277 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
278 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
Chris Ball83e44912010-08-16 16:01:28 +0200279 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
280 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
Stephane Chatty0c3910c2010-04-10 16:43:08 +0200281 { }
282};
283MODULE_DEVICE_TABLE(hid, egalax_devices);
284
285static const struct hid_usage_id egalax_grabbed_usages[] = {
286 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
287 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
288};
289
290static struct hid_driver egalax_driver = {
291 .name = "egalax-touch",
292 .id_table = egalax_devices,
293 .probe = egalax_probe,
294 .remove = egalax_remove,
295 .input_mapping = egalax_input_mapping,
296 .input_mapped = egalax_input_mapped,
297 .usage_table = egalax_grabbed_usages,
298 .event = egalax_event,
299};
300
301static int __init egalax_init(void)
302{
303 return hid_register_driver(&egalax_driver);
304}
305
306static void __exit egalax_exit(void)
307{
308 hid_unregister_driver(&egalax_driver);
309}
310
311module_init(egalax_init);
312module_exit(egalax_exit);
313