ASoC: Ensure we delay long enough for WM8994 FLL to lock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / hid-egalax.c
blob5cd004ef57a5cb867656bde1b8c170f276b8cc30
1 /*
2 * HID driver for eGalax dual-touch panels
4 * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
6 */
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.
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");
26 #include "hid-ids.h"
28 struct 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? */
34 __u16 lastx, lasty, lastz; /* latest valid (x, y, z) in the frame */
37 static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
38 struct hid_field *field, struct hid_usage *usage,
39 unsigned long **bit, int *max)
41 switch (usage->hid & HID_USAGE_PAGE) {
43 case HID_UP_GENDESK:
44 switch (usage->hid) {
45 case HID_GD_X:
46 hid_map_usage(hi, usage, bit, max,
47 EV_ABS, ABS_MT_POSITION_X);
48 /* touchscreen emulation */
49 input_set_abs_params(hi->input, ABS_X,
50 field->logical_minimum,
51 field->logical_maximum, 0, 0);
52 return 1;
53 case HID_GD_Y:
54 hid_map_usage(hi, usage, bit, max,
55 EV_ABS, ABS_MT_POSITION_Y);
56 /* touchscreen emulation */
57 input_set_abs_params(hi->input, ABS_Y,
58 field->logical_minimum,
59 field->logical_maximum, 0, 0);
60 return 1;
62 return 0;
64 case HID_UP_DIGITIZER:
65 switch (usage->hid) {
66 case HID_DG_TIPSWITCH:
67 /* touchscreen emulation */
68 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
69 return 1;
70 case HID_DG_INRANGE:
71 case HID_DG_CONFIDENCE:
72 case HID_DG_CONTACTCOUNT:
73 case HID_DG_CONTACTMAX:
74 return -1;
75 case HID_DG_CONTACTID:
76 hid_map_usage(hi, usage, bit, max,
77 EV_ABS, ABS_MT_TRACKING_ID);
78 return 1;
79 case HID_DG_TIPPRESSURE:
80 hid_map_usage(hi, usage, bit, max,
81 EV_ABS, ABS_MT_PRESSURE);
82 /* touchscreen emulation */
83 input_set_abs_params(hi->input, ABS_PRESSURE,
84 field->logical_minimum,
85 field->logical_maximum, 0, 0);
86 return 1;
88 return 0;
91 /* ignore others (from other reports we won't get anyway) */
92 return -1;
95 static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
96 struct hid_field *field, struct hid_usage *usage,
97 unsigned long **bit, int *max)
99 if (usage->type == EV_KEY || usage->type == EV_ABS)
100 clear_bit(usage->code, *bit);
102 return 0;
106 * this function is called when a whole finger has been parsed,
107 * so that it can decide what to send to the input layer.
109 static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
111 td->first = !td->first; /* touchscreen emulation */
113 if (td->valid) {
114 /* emit multitouch events */
115 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
116 input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x >> 3);
117 input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y >> 3);
118 input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
120 input_mt_sync(input);
123 * touchscreen emulation: store (x, y) as
124 * the last valid values in this frame
126 td->lastx = td->x;
127 td->lasty = td->y;
128 td->lastz = td->z;
132 * touchscreen emulation: if this is the second finger and at least
133 * one in this frame is valid, the latest valid in the frame is
134 * the oldest on the panel, the one we want for single touch
136 if (!td->first && td->activity) {
137 input_event(input, EV_ABS, ABS_X, td->lastx >> 3);
138 input_event(input, EV_ABS, ABS_Y, td->lasty >> 3);
139 input_event(input, EV_ABS, ABS_PRESSURE, td->lastz);
142 if (!td->valid) {
144 * touchscreen emulation: if the first finger is invalid
145 * and there previously was finger activity, this is a release
147 if (td->first && td->activity) {
148 input_event(input, EV_KEY, BTN_TOUCH, 0);
149 td->activity = false;
151 return;
155 /* touchscreen emulation: if no previous activity, emit touch event */
156 if (!td->activity) {
157 input_event(input, EV_KEY, BTN_TOUCH, 1);
158 td->activity = true;
163 static int egalax_event(struct hid_device *hid, struct hid_field *field,
164 struct hid_usage *usage, __s32 value)
166 struct egalax_data *td = hid_get_drvdata(hid);
168 if (hid->claimed & HID_CLAIMED_INPUT) {
169 struct input_dev *input = field->hidinput->input;
171 switch (usage->hid) {
172 case HID_DG_INRANGE:
173 case HID_DG_CONFIDENCE:
174 /* avoid interference from generic hidinput handling */
175 break;
176 case HID_DG_TIPSWITCH:
177 td->valid = value;
178 break;
179 case HID_DG_TIPPRESSURE:
180 td->z = value;
181 break;
182 case HID_DG_CONTACTID:
183 td->id = value;
184 break;
185 case HID_GD_X:
186 td->x = value;
187 break;
188 case HID_GD_Y:
189 td->y = value;
190 /* this is the last field in a finger */
191 egalax_filter_event(td, input);
192 break;
193 case HID_DG_CONTACTCOUNT:
194 /* touch emulation: this is the last field in a frame */
195 td->first = false;
196 break;
198 default:
199 /* fallback to the generic hidinput handling */
200 return 0;
204 /* we have handled the hidinput part, now remains hiddev */
205 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
206 hid->hiddev_hid_event(hid, field, usage, value);
208 return 1;
211 static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
213 int ret;
214 struct egalax_data *td;
215 struct hid_report *report;
217 td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
218 if (!td) {
219 dev_err(&hdev->dev, "cannot allocate eGalax data\n");
220 return -ENOMEM;
222 hid_set_drvdata(hdev, td);
224 ret = hid_parse(hdev);
225 if (ret)
226 goto end;
228 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
229 if (ret)
230 goto end;
232 report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
233 if (report) {
234 report->field[0]->value[0] = 2;
235 usbhid_submit_report(hdev, report, USB_DIR_OUT);
238 end:
239 if (ret)
240 kfree(td);
242 return ret;
245 static void egalax_remove(struct hid_device *hdev)
247 hid_hw_stop(hdev);
248 kfree(hid_get_drvdata(hdev));
249 hid_set_drvdata(hdev, NULL);
252 static const struct hid_device_id egalax_devices[] = {
253 { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
254 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
257 MODULE_DEVICE_TABLE(hid, egalax_devices);
259 static const struct hid_usage_id egalax_grabbed_usages[] = {
260 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
261 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
264 static struct hid_driver egalax_driver = {
265 .name = "egalax-touch",
266 .id_table = egalax_devices,
267 .probe = egalax_probe,
268 .remove = egalax_remove,
269 .input_mapping = egalax_input_mapping,
270 .input_mapped = egalax_input_mapped,
271 .usage_table = egalax_grabbed_usages,
272 .event = egalax_event,
275 static int __init egalax_init(void)
277 return hid_register_driver(&egalax_driver);
280 static void __exit egalax_exit(void)
282 hid_unregister_driver(&egalax_driver);
285 module_init(egalax_init);
286 module_exit(egalax_exit);