Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 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> |
Jiri Kosina | c872b0f | 2010-04-10 21:29:09 +0200 | [diff] [blame] | 19 | #include <linux/slab.h> |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 20 | #include "usbhid/usbhid.h" |
| 21 | |
| 22 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); |
| 23 | MODULE_DESCRIPTION("eGalax dual-touch panel"); |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | |
| 26 | #include "hid-ids.h" |
| 27 | |
| 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? */ |
Philipp Merkel | f516611 | 2010-10-01 15:38:59 +0200 | [diff] [blame] | 34 | __u16 lastx, lasty, lastz; /* latest valid (x, y, z) in the frame */ |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 37 | static 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 Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 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) |
| 49 | { |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 50 | struct input_dev *input = hi->input; |
| 51 | |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 52 | switch (usage->hid & HID_USAGE_PAGE) { |
| 53 | |
| 54 | case HID_UP_GENDESK: |
| 55 | switch (usage->hid) { |
| 56 | case HID_GD_X: |
| 57 | hid_map_usage(hi, usage, bit, max, |
| 58 | EV_ABS, ABS_MT_POSITION_X); |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 59 | set_abs(input, ABS_MT_POSITION_X, field, 0); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 60 | /* touchscreen emulation */ |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 61 | set_abs(input, ABS_X, field, 0); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 62 | return 1; |
| 63 | case HID_GD_Y: |
| 64 | hid_map_usage(hi, usage, bit, max, |
| 65 | EV_ABS, ABS_MT_POSITION_Y); |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 66 | set_abs(input, ABS_MT_POSITION_Y, field, 0); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 67 | /* touchscreen emulation */ |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 68 | set_abs(input, ABS_Y, field, 0); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 69 | return 1; |
| 70 | } |
| 71 | return 0; |
| 72 | |
| 73 | case HID_UP_DIGITIZER: |
| 74 | switch (usage->hid) { |
| 75 | case HID_DG_TIPSWITCH: |
| 76 | /* touchscreen emulation */ |
| 77 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 78 | input_set_capability(input, EV_KEY, BTN_TOUCH); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 79 | return 1; |
| 80 | case HID_DG_INRANGE: |
| 81 | case HID_DG_CONFIDENCE: |
| 82 | case HID_DG_CONTACTCOUNT: |
| 83 | case HID_DG_CONTACTMAX: |
| 84 | return -1; |
| 85 | case HID_DG_CONTACTID: |
| 86 | hid_map_usage(hi, usage, bit, max, |
| 87 | EV_ABS, ABS_MT_TRACKING_ID); |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 88 | set_abs(input, ABS_MT_TRACKING_ID, field, 0); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 89 | return 1; |
| 90 | case HID_DG_TIPPRESSURE: |
| 91 | hid_map_usage(hi, usage, bit, max, |
| 92 | EV_ABS, ABS_MT_PRESSURE); |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 93 | set_abs(input, ABS_MT_PRESSURE, field, 0); |
Philipp Merkel | f516611 | 2010-10-01 15:38:59 +0200 | [diff] [blame] | 94 | /* touchscreen emulation */ |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 95 | set_abs(input, ABS_PRESSURE, field, 0); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 96 | return 1; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* ignore others (from other reports we won't get anyway) */ |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 106 | struct hid_field *field, struct hid_usage *usage, |
| 107 | unsigned long **bit, int *max) |
| 108 | { |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 109 | /* tell hid-input to skip setup of these event types */ |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 110 | if (usage->type == EV_KEY || usage->type == EV_ABS) |
Henrik Rydberg | 4a86418 | 2010-11-27 17:56:17 +0100 | [diff] [blame^] | 111 | set_bit(usage->type, hi->input->evbit); |
| 112 | return -1; |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * this function is called when a whole finger has been parsed, |
| 117 | * so that it can decide what to send to the input layer. |
| 118 | */ |
| 119 | static void egalax_filter_event(struct egalax_data *td, struct input_dev *input) |
| 120 | { |
| 121 | td->first = !td->first; /* touchscreen emulation */ |
| 122 | |
| 123 | if (td->valid) { |
| 124 | /* emit multitouch events */ |
| 125 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id); |
Philipp Merkel | f516611 | 2010-10-01 15:38:59 +0200 | [diff] [blame] | 126 | input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x >> 3); |
| 127 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y >> 3); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 128 | input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z); |
| 129 | |
| 130 | input_mt_sync(input); |
| 131 | |
| 132 | /* |
| 133 | * touchscreen emulation: store (x, y) as |
| 134 | * the last valid values in this frame |
| 135 | */ |
| 136 | td->lastx = td->x; |
| 137 | td->lasty = td->y; |
Philipp Merkel | f516611 | 2010-10-01 15:38:59 +0200 | [diff] [blame] | 138 | td->lastz = td->z; |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
| 142 | * touchscreen emulation: if this is the second finger and at least |
| 143 | * one in this frame is valid, the latest valid in the frame is |
| 144 | * the oldest on the panel, the one we want for single touch |
| 145 | */ |
| 146 | if (!td->first && td->activity) { |
Philipp Merkel | f516611 | 2010-10-01 15:38:59 +0200 | [diff] [blame] | 147 | input_event(input, EV_ABS, ABS_X, td->lastx >> 3); |
| 148 | input_event(input, EV_ABS, ABS_Y, td->lasty >> 3); |
| 149 | input_event(input, EV_ABS, ABS_PRESSURE, td->lastz); |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (!td->valid) { |
| 153 | /* |
| 154 | * touchscreen emulation: if the first finger is invalid |
| 155 | * and there previously was finger activity, this is a release |
| 156 | */ |
| 157 | if (td->first && td->activity) { |
| 158 | input_event(input, EV_KEY, BTN_TOUCH, 0); |
| 159 | td->activity = false; |
| 160 | } |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /* touchscreen emulation: if no previous activity, emit touch event */ |
| 166 | if (!td->activity) { |
| 167 | input_event(input, EV_KEY, BTN_TOUCH, 1); |
| 168 | td->activity = true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static int egalax_event(struct hid_device *hid, struct hid_field *field, |
| 174 | struct hid_usage *usage, __s32 value) |
| 175 | { |
| 176 | struct egalax_data *td = hid_get_drvdata(hid); |
| 177 | |
Chris Ball | 83e4491 | 2010-08-16 16:01:28 +0200 | [diff] [blame] | 178 | /* Note, eGalax has two product lines: the first is resistive and |
| 179 | * uses a standard parallel multitouch protocol (product ID == |
| 180 | * 48xx). The second is capacitive and uses an unusual "serial" |
| 181 | * protocol with a different message for each multitouch finger |
| 182 | * (product ID == 72xx). We do not yet generate a correct event |
| 183 | * sequence for the capacitive/serial protocol. |
| 184 | */ |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 185 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 186 | struct input_dev *input = field->hidinput->input; |
| 187 | |
| 188 | switch (usage->hid) { |
| 189 | case HID_DG_INRANGE: |
| 190 | case HID_DG_CONFIDENCE: |
| 191 | /* avoid interference from generic hidinput handling */ |
| 192 | break; |
| 193 | case HID_DG_TIPSWITCH: |
| 194 | td->valid = value; |
| 195 | break; |
| 196 | case HID_DG_TIPPRESSURE: |
| 197 | td->z = value; |
| 198 | break; |
| 199 | case HID_DG_CONTACTID: |
| 200 | td->id = value; |
| 201 | break; |
| 202 | case HID_GD_X: |
| 203 | td->x = value; |
| 204 | break; |
| 205 | case HID_GD_Y: |
| 206 | td->y = value; |
| 207 | /* this is the last field in a finger */ |
| 208 | egalax_filter_event(td, input); |
| 209 | break; |
| 210 | case HID_DG_CONTACTCOUNT: |
| 211 | /* touch emulation: this is the last field in a frame */ |
| 212 | td->first = false; |
| 213 | break; |
| 214 | |
| 215 | default: |
| 216 | /* fallback to the generic hidinput handling */ |
| 217 | return 0; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* we have handled the hidinput part, now remains hiddev */ |
| 222 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) |
| 223 | hid->hiddev_hid_event(hid, field, usage, value); |
| 224 | |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 229 | { |
| 230 | int ret; |
| 231 | struct egalax_data *td; |
| 232 | struct hid_report *report; |
| 233 | |
| 234 | td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL); |
| 235 | if (!td) { |
| 236 | dev_err(&hdev->dev, "cannot allocate eGalax data\n"); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | hid_set_drvdata(hdev, td); |
| 240 | |
| 241 | ret = hid_parse(hdev); |
| 242 | if (ret) |
| 243 | goto end; |
| 244 | |
| 245 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 246 | if (ret) |
| 247 | goto end; |
| 248 | |
| 249 | report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5]; |
| 250 | if (report) { |
| 251 | report->field[0]->value[0] = 2; |
| 252 | usbhid_submit_report(hdev, report, USB_DIR_OUT); |
| 253 | } |
| 254 | |
| 255 | end: |
| 256 | if (ret) |
| 257 | kfree(td); |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static void egalax_remove(struct hid_device *hdev) |
| 263 | { |
| 264 | hid_hw_stop(hdev); |
| 265 | kfree(hid_get_drvdata(hdev)); |
| 266 | hid_set_drvdata(hdev, NULL); |
| 267 | } |
| 268 | |
| 269 | static const struct hid_device_id egalax_devices[] = { |
| 270 | { HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 271 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) }, |
Chris Ball | 83e4491 | 2010-08-16 16:01:28 +0200 | [diff] [blame] | 272 | { HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 273 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) }, |
Stephane Chatty | 0c3910c | 2010-04-10 16:43:08 +0200 | [diff] [blame] | 274 | { } |
| 275 | }; |
| 276 | MODULE_DEVICE_TABLE(hid, egalax_devices); |
| 277 | |
| 278 | static const struct hid_usage_id egalax_grabbed_usages[] = { |
| 279 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
| 280 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} |
| 281 | }; |
| 282 | |
| 283 | static struct hid_driver egalax_driver = { |
| 284 | .name = "egalax-touch", |
| 285 | .id_table = egalax_devices, |
| 286 | .probe = egalax_probe, |
| 287 | .remove = egalax_remove, |
| 288 | .input_mapping = egalax_input_mapping, |
| 289 | .input_mapped = egalax_input_mapped, |
| 290 | .usage_table = egalax_grabbed_usages, |
| 291 | .event = egalax_event, |
| 292 | }; |
| 293 | |
| 294 | static int __init egalax_init(void) |
| 295 | { |
| 296 | return hid_register_driver(&egalax_driver); |
| 297 | } |
| 298 | |
| 299 | static void __exit egalax_exit(void) |
| 300 | { |
| 301 | hid_unregister_driver(&egalax_driver); |
| 302 | } |
| 303 | |
| 304 | module_init(egalax_init); |
| 305 | module_exit(egalax_exit); |
| 306 | |