Stephane Chatty | 49e4739 | 2010-01-13 00:29:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Quanta Optical Touch dual-touch panels |
| 3 | * |
| 4 | * Copyright (c) 2009-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 | |
Stephane Chatty | 49e4739 | 2010-01-13 00:29:16 +0100 | [diff] [blame] | 19 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); |
| 20 | MODULE_DESCRIPTION("Quanta dual-touch panel"); |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | |
| 23 | #include "hid-ids.h" |
| 24 | |
| 25 | struct quanta_data { |
| 26 | __u16 x, y; |
| 27 | __u8 id; |
| 28 | bool valid; /* valid finger data, or just placeholder? */ |
| 29 | bool first; /* is this the first finger in this frame? */ |
| 30 | bool activity_now; /* at least one active finger in this frame? */ |
| 31 | bool activity; /* at least one active finger previously? */ |
| 32 | }; |
| 33 | |
| 34 | static int quanta_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 35 | struct hid_field *field, struct hid_usage *usage, |
| 36 | unsigned long **bit, int *max) |
| 37 | { |
| 38 | switch (usage->hid & HID_USAGE_PAGE) { |
| 39 | |
| 40 | case HID_UP_GENDESK: |
| 41 | switch (usage->hid) { |
| 42 | case HID_GD_X: |
| 43 | hid_map_usage(hi, usage, bit, max, |
| 44 | EV_ABS, ABS_MT_POSITION_X); |
| 45 | /* touchscreen emulation */ |
| 46 | input_set_abs_params(hi->input, ABS_X, |
| 47 | field->logical_minimum, |
| 48 | field->logical_maximum, 0, 0); |
| 49 | return 1; |
| 50 | case HID_GD_Y: |
| 51 | hid_map_usage(hi, usage, bit, max, |
| 52 | EV_ABS, ABS_MT_POSITION_Y); |
| 53 | /* touchscreen emulation */ |
| 54 | input_set_abs_params(hi->input, ABS_Y, |
| 55 | field->logical_minimum, |
| 56 | field->logical_maximum, 0, 0); |
| 57 | return 1; |
| 58 | } |
| 59 | return 0; |
| 60 | |
| 61 | case HID_UP_DIGITIZER: |
| 62 | switch (usage->hid) { |
| 63 | case HID_DG_CONFIDENCE: |
| 64 | case HID_DG_TIPSWITCH: |
| 65 | case HID_DG_INPUTMODE: |
| 66 | case HID_DG_DEVICEINDEX: |
| 67 | case HID_DG_CONTACTCOUNT: |
| 68 | case HID_DG_CONTACTMAX: |
| 69 | case HID_DG_TIPPRESSURE: |
| 70 | case HID_DG_WIDTH: |
| 71 | case HID_DG_HEIGHT: |
| 72 | return -1; |
| 73 | case HID_DG_INRANGE: |
| 74 | /* touchscreen emulation */ |
| 75 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); |
| 76 | return 1; |
| 77 | case HID_DG_CONTACTID: |
| 78 | hid_map_usage(hi, usage, bit, max, |
| 79 | EV_ABS, ABS_MT_TRACKING_ID); |
| 80 | return 1; |
| 81 | } |
| 82 | return 0; |
| 83 | |
| 84 | case 0xff000000: |
| 85 | /* ignore vendor-specific features */ |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int quanta_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 93 | struct hid_field *field, struct hid_usage *usage, |
| 94 | unsigned long **bit, int *max) |
| 95 | { |
| 96 | if (usage->type == EV_KEY || usage->type == EV_ABS) |
| 97 | clear_bit(usage->code, *bit); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * this function is called when a whole finger has been parsed, |
| 104 | * so that it can decide what to send to the input layer. |
| 105 | */ |
| 106 | static void quanta_filter_event(struct quanta_data *td, struct input_dev *input) |
| 107 | { |
| 108 | |
| 109 | td->first = !td->first; /* touchscreen emulation */ |
| 110 | |
| 111 | if (!td->valid) { |
| 112 | /* |
| 113 | * touchscreen emulation: if no finger in this frame is valid |
| 114 | * and there previously was finger activity, this is a release |
| 115 | */ |
| 116 | if (!td->first && !td->activity_now && td->activity) { |
| 117 | input_event(input, EV_KEY, BTN_TOUCH, 0); |
| 118 | td->activity = false; |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id); |
| 124 | input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x); |
| 125 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y); |
| 126 | |
| 127 | input_mt_sync(input); |
| 128 | td->valid = false; |
| 129 | |
| 130 | /* touchscreen emulation: if first active finger in this frame... */ |
| 131 | if (!td->activity_now) { |
| 132 | /* if there was no previous activity, emit touch event */ |
| 133 | if (!td->activity) { |
| 134 | input_event(input, EV_KEY, BTN_TOUCH, 1); |
| 135 | td->activity = true; |
| 136 | } |
| 137 | td->activity_now = true; |
| 138 | /* and in any case this is our preferred finger */ |
| 139 | input_event(input, EV_ABS, ABS_X, td->x); |
| 140 | input_event(input, EV_ABS, ABS_Y, td->y); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static int quanta_event(struct hid_device *hid, struct hid_field *field, |
| 146 | struct hid_usage *usage, __s32 value) |
| 147 | { |
| 148 | struct quanta_data *td = hid_get_drvdata(hid); |
| 149 | |
| 150 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 151 | struct input_dev *input = field->hidinput->input; |
| 152 | |
| 153 | switch (usage->hid) { |
| 154 | case HID_DG_INRANGE: |
| 155 | td->valid = !!value; |
| 156 | break; |
| 157 | case HID_GD_X: |
| 158 | td->x = value; |
| 159 | break; |
| 160 | case HID_GD_Y: |
| 161 | td->y = value; |
| 162 | quanta_filter_event(td, input); |
| 163 | break; |
| 164 | case HID_DG_CONTACTID: |
| 165 | td->id = value; |
| 166 | break; |
| 167 | case HID_DG_CONTACTCOUNT: |
| 168 | /* touch emulation: this is the last field in a frame */ |
| 169 | td->first = false; |
| 170 | td->activity_now = false; |
| 171 | break; |
| 172 | case HID_DG_CONFIDENCE: |
| 173 | case HID_DG_TIPSWITCH: |
| 174 | /* avoid interference from generic hidinput handling */ |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | /* fallback to the generic hidinput handling */ |
| 179 | return 0; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* we have handled the hidinput part, now remains hiddev */ |
| 184 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) |
| 185 | hid->hiddev_hid_event(hid, field, usage, value); |
| 186 | |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | static int quanta_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 191 | { |
| 192 | int ret; |
| 193 | struct quanta_data *td; |
| 194 | |
| 195 | td = kmalloc(sizeof(struct quanta_data), GFP_KERNEL); |
| 196 | if (!td) { |
| 197 | dev_err(&hdev->dev, "cannot allocate Quanta Touch data\n"); |
| 198 | return -ENOMEM; |
| 199 | } |
| 200 | td->valid = false; |
| 201 | td->activity = false; |
| 202 | td->activity_now = false; |
| 203 | td->first = false; |
| 204 | hid_set_drvdata(hdev, td); |
| 205 | |
| 206 | ret = hid_parse(hdev); |
| 207 | if (!ret) |
| 208 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 209 | |
| 210 | if (ret) |
| 211 | kfree(td); |
| 212 | |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static void quanta_remove(struct hid_device *hdev) |
| 217 | { |
| 218 | hid_hw_stop(hdev); |
| 219 | kfree(hid_get_drvdata(hdev)); |
| 220 | hid_set_drvdata(hdev, NULL); |
| 221 | } |
| 222 | |
| 223 | static const struct hid_device_id quanta_devices[] = { |
| 224 | { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, |
| 225 | USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) }, |
Alex Neblett | f54405d | 2010-02-02 21:16:03 -0800 | [diff] [blame] | 226 | { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, |
| 227 | USB_DEVICE_ID_PIXART_IMAGING_INC_OPTICAL_TOUCH_SCREEN) }, |
Stephane Chatty | 49e4739 | 2010-01-13 00:29:16 +0100 | [diff] [blame] | 228 | { } |
| 229 | }; |
| 230 | MODULE_DEVICE_TABLE(hid, quanta_devices); |
| 231 | |
| 232 | static const struct hid_usage_id quanta_grabbed_usages[] = { |
| 233 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
| 234 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} |
| 235 | }; |
| 236 | |
| 237 | static struct hid_driver quanta_driver = { |
| 238 | .name = "quanta-touch", |
| 239 | .id_table = quanta_devices, |
| 240 | .probe = quanta_probe, |
| 241 | .remove = quanta_remove, |
| 242 | .input_mapping = quanta_input_mapping, |
| 243 | .input_mapped = quanta_input_mapped, |
| 244 | .usage_table = quanta_grabbed_usages, |
| 245 | .event = quanta_event, |
| 246 | }; |
| 247 | |
| 248 | static int __init quanta_init(void) |
| 249 | { |
| 250 | return hid_register_driver(&quanta_driver); |
| 251 | } |
| 252 | |
| 253 | static void __exit quanta_exit(void) |
| 254 | { |
| 255 | hid_unregister_driver(&quanta_driver); |
| 256 | } |
| 257 | |
| 258 | module_init(quanta_init); |
| 259 | module_exit(quanta_exit); |
| 260 | |