HID: add support for the eGalax dual-touch panel
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / hid / hid-egalax.c
CommitLineData
0c3910c2
SC
1/*
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>
19#include "usbhid/usbhid.h"
20
21MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
22MODULE_DESCRIPTION("eGalax dual-touch panel");
23MODULE_LICENSE("GPL");
24
25#include "hid-ids.h"
26
27struct egalax_data {
28 __u16 x, y, z;
29 __u8 id;
30 bool first; /* is this the first finger in the frame? */
31 bool valid; /* valid finger data, or just placeholder? */
32 bool activity; /* at least one active finger previously? */
33 __u16 lastx, lasty; /* latest valid (x, y) in the frame */
34};
35
36static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
37 struct hid_field *field, struct hid_usage *usage,
38 unsigned long **bit, int *max)
39{
40 switch (usage->hid & HID_USAGE_PAGE) {
41
42 case HID_UP_GENDESK:
43 switch (usage->hid) {
44 case HID_GD_X:
45 hid_map_usage(hi, usage, bit, max,
46 EV_ABS, ABS_MT_POSITION_X);
47 /* touchscreen emulation */
48 input_set_abs_params(hi->input, ABS_X,
49 field->logical_minimum,
50 field->logical_maximum, 0, 0);
51 return 1;
52 case HID_GD_Y:
53 hid_map_usage(hi, usage, bit, max,
54 EV_ABS, ABS_MT_POSITION_Y);
55 /* touchscreen emulation */
56 input_set_abs_params(hi->input, ABS_Y,
57 field->logical_minimum,
58 field->logical_maximum, 0, 0);
59 return 1;
60 }
61 return 0;
62
63 case HID_UP_DIGITIZER:
64 switch (usage->hid) {
65 case HID_DG_TIPSWITCH:
66 /* touchscreen emulation */
67 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
68 return 1;
69 case HID_DG_INRANGE:
70 case HID_DG_CONFIDENCE:
71 case HID_DG_CONTACTCOUNT:
72 case HID_DG_CONTACTMAX:
73 return -1;
74 case HID_DG_CONTACTID:
75 hid_map_usage(hi, usage, bit, max,
76 EV_ABS, ABS_MT_TRACKING_ID);
77 return 1;
78 case HID_DG_TIPPRESSURE:
79 hid_map_usage(hi, usage, bit, max,
80 EV_ABS, ABS_MT_PRESSURE);
81 return 1;
82 }
83 return 0;
84 }
85
86 /* ignore others (from other reports we won't get anyway) */
87 return -1;
88}
89
90static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
91 struct hid_field *field, struct hid_usage *usage,
92 unsigned long **bit, int *max)
93{
94 if (usage->type == EV_KEY || usage->type == EV_ABS)
95 clear_bit(usage->code, *bit);
96
97 return 0;
98}
99
100/*
101 * this function is called when a whole finger has been parsed,
102 * so that it can decide what to send to the input layer.
103 */
104static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
105{
106 td->first = !td->first; /* touchscreen emulation */
107
108 if (td->valid) {
109 /* emit multitouch events */
110 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
111 input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
112 input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
113 input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
114
115 input_mt_sync(input);
116
117 /*
118 * touchscreen emulation: store (x, y) as
119 * the last valid values in this frame
120 */
121 td->lastx = td->x;
122 td->lasty = td->y;
123 }
124
125 /*
126 * touchscreen emulation: if this is the second finger and at least
127 * one in this frame is valid, the latest valid in the frame is
128 * the oldest on the panel, the one we want for single touch
129 */
130 if (!td->first && td->activity) {
131 input_event(input, EV_ABS, ABS_X, td->lastx);
132 input_event(input, EV_ABS, ABS_Y, td->lasty);
133 }
134
135 if (!td->valid) {
136 /*
137 * touchscreen emulation: if the first finger is invalid
138 * and there previously was finger activity, this is a release
139 */
140 if (td->first && td->activity) {
141 input_event(input, EV_KEY, BTN_TOUCH, 0);
142 td->activity = false;
143 }
144 return;
145 }
146
147
148 /* touchscreen emulation: if no previous activity, emit touch event */
149 if (!td->activity) {
150 input_event(input, EV_KEY, BTN_TOUCH, 1);
151 td->activity = true;
152 }
153}
154
155
156static int egalax_event(struct hid_device *hid, struct hid_field *field,
157 struct hid_usage *usage, __s32 value)
158{
159 struct egalax_data *td = hid_get_drvdata(hid);
160
161 if (hid->claimed & HID_CLAIMED_INPUT) {
162 struct input_dev *input = field->hidinput->input;
163
164 switch (usage->hid) {
165 case HID_DG_INRANGE:
166 case HID_DG_CONFIDENCE:
167 /* avoid interference from generic hidinput handling */
168 break;
169 case HID_DG_TIPSWITCH:
170 td->valid = value;
171 break;
172 case HID_DG_TIPPRESSURE:
173 td->z = value;
174 break;
175 case HID_DG_CONTACTID:
176 td->id = value;
177 break;
178 case HID_GD_X:
179 td->x = value;
180 break;
181 case HID_GD_Y:
182 td->y = value;
183 /* this is the last field in a finger */
184 egalax_filter_event(td, input);
185 break;
186 case HID_DG_CONTACTCOUNT:
187 /* touch emulation: this is the last field in a frame */
188 td->first = false;
189 break;
190
191 default:
192 /* fallback to the generic hidinput handling */
193 return 0;
194 }
195 }
196
197 /* we have handled the hidinput part, now remains hiddev */
198 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
199 hid->hiddev_hid_event(hid, field, usage, value);
200
201 return 1;
202}
203
204static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
205{
206 int ret;
207 struct egalax_data *td;
208 struct hid_report *report;
209
210 td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
211 if (!td) {
212 dev_err(&hdev->dev, "cannot allocate eGalax data\n");
213 return -ENOMEM;
214 }
215 hid_set_drvdata(hdev, td);
216
217 ret = hid_parse(hdev);
218 if (ret)
219 goto end;
220
221 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
222 if (ret)
223 goto end;
224
225 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
226 if (report) {
227 report->field[0]->value[0] = 2;
228 usbhid_submit_report(hdev, report, USB_DIR_OUT);
229 }
230
231end:
232 if (ret)
233 kfree(td);
234
235 return ret;
236}
237
238static void egalax_remove(struct hid_device *hdev)
239{
240 hid_hw_stop(hdev);
241 kfree(hid_get_drvdata(hdev));
242 hid_set_drvdata(hdev, NULL);
243}
244
245static const struct hid_device_id egalax_devices[] = {
246 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
247 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
248 { }
249};
250MODULE_DEVICE_TABLE(hid, egalax_devices);
251
252static const struct hid_usage_id egalax_grabbed_usages[] = {
253 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
254 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
255};
256
257static struct hid_driver egalax_driver = {
258 .name = "egalax-touch",
259 .id_table = egalax_devices,
260 .probe = egalax_probe,
261 .remove = egalax_remove,
262 .input_mapping = egalax_input_mapping,
263 .input_mapped = egalax_input_mapped,
264 .usage_table = egalax_grabbed_usages,
265 .event = egalax_event,
266};
267
268static int __init egalax_init(void)
269{
270 return hid_register_driver(&egalax_driver);
271}
272
273static void __exit egalax_exit(void)
274{
275 hid_unregister_driver(&egalax_driver);
276}
277
278module_init(egalax_init);
279module_exit(egalax_exit);
280