Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1 | /* |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 2 | * HID driver for N-Trig touchscreens |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 3 | * |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 4 | * Copyright (c) 2008 Rafi Rubin |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 5 | * Copyright (c) 2009 Stephane Chatty |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 6 | * |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/hid.h> |
| 18 | #include <linux/module.h> |
| 19 | |
| 20 | #include "hid-ids.h" |
| 21 | |
| 22 | #define NTRIG_DUPLICATE_USAGES 0x001 |
| 23 | |
| 24 | #define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \ |
| 25 | EV_KEY, (c)) |
| 26 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 27 | struct ntrig_data { |
| 28 | __s32 x, y, id, w, h; |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 29 | bool reading_a_point, found_contact_id; |
| 30 | bool pen_active; |
| 31 | bool finger_active; |
| 32 | bool inverted; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * this driver is aimed at two firmware versions in circulation: |
| 37 | * - dual pen/finger single touch |
| 38 | * - finger multitouch, pen not working |
| 39 | */ |
| 40 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 41 | static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 42 | struct hid_field *field, struct hid_usage *usage, |
| 43 | unsigned long **bit, int *max) |
| 44 | { |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 45 | /* No special mappings needed for the pen */ |
| 46 | if (field->application == HID_DG_PEN) |
| 47 | return 0; |
| 48 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 49 | switch (usage->hid & HID_USAGE_PAGE) { |
| 50 | |
| 51 | case HID_UP_GENDESK: |
| 52 | switch (usage->hid) { |
| 53 | case HID_GD_X: |
| 54 | hid_map_usage(hi, usage, bit, max, |
| 55 | EV_ABS, ABS_MT_POSITION_X); |
| 56 | input_set_abs_params(hi->input, ABS_X, |
| 57 | field->logical_minimum, |
| 58 | field->logical_maximum, 0, 0); |
| 59 | return 1; |
| 60 | case HID_GD_Y: |
| 61 | hid_map_usage(hi, usage, bit, max, |
| 62 | EV_ABS, ABS_MT_POSITION_Y); |
| 63 | input_set_abs_params(hi->input, ABS_Y, |
| 64 | field->logical_minimum, |
| 65 | field->logical_maximum, 0, 0); |
| 66 | return 1; |
| 67 | } |
| 68 | return 0; |
| 69 | |
| 70 | case HID_UP_DIGITIZER: |
| 71 | switch (usage->hid) { |
| 72 | /* we do not want to map these for now */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 73 | case HID_DG_CONTACTID: /* value is useless */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 74 | case HID_DG_INPUTMODE: |
| 75 | case HID_DG_DEVICEINDEX: |
| 76 | case HID_DG_CONTACTCOUNT: |
| 77 | case HID_DG_CONTACTMAX: |
| 78 | return -1; |
| 79 | |
| 80 | /* original mapping by Rafi Rubin */ |
| 81 | case HID_DG_CONFIDENCE: |
| 82 | nt_map_key_clear(BTN_TOOL_DOUBLETAP); |
| 83 | return 1; |
| 84 | |
| 85 | /* width/height mapped on TouchMajor/TouchMinor/Orientation */ |
| 86 | case HID_DG_WIDTH: |
| 87 | hid_map_usage(hi, usage, bit, max, |
| 88 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
| 89 | return 1; |
| 90 | case HID_DG_HEIGHT: |
| 91 | hid_map_usage(hi, usage, bit, max, |
| 92 | EV_ABS, ABS_MT_TOUCH_MINOR); |
| 93 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
| 94 | 0, 1, 0, 0); |
| 95 | return 1; |
| 96 | } |
| 97 | return 0; |
| 98 | |
| 99 | case 0xff000000: |
| 100 | /* we do not want to map these: no input-oriented meaning */ |
| 101 | return -1; |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 102 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 103 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 108 | struct hid_field *field, struct hid_usage *usage, |
| 109 | unsigned long **bit, int *max) |
| 110 | { |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 111 | /* No special mappings needed for the pen */ |
| 112 | if (field->application == HID_DG_PEN) |
| 113 | return 0; |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 114 | if (usage->type == EV_KEY || usage->type == EV_REL |
| 115 | || usage->type == EV_ABS) |
| 116 | clear_bit(usage->code, *bit); |
| 117 | |
| 118 | return 0; |
| 119 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * this function is called upon all reports |
| 123 | * so that we can filter contact point information, |
| 124 | * decide whether we are in multi or single touch mode |
| 125 | * and call input_mt_sync after each point if necessary |
| 126 | */ |
| 127 | static int ntrig_event (struct hid_device *hid, struct hid_field *field, |
| 128 | struct hid_usage *usage, __s32 value) |
| 129 | { |
| 130 | struct input_dev *input = field->hidinput->input; |
| 131 | struct ntrig_data *nd = hid_get_drvdata(hid); |
| 132 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 133 | /* No special handling needed for the pen */ |
| 134 | if (field->application == HID_DG_PEN) |
| 135 | return 0; |
| 136 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 137 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 138 | switch (usage->hid) { |
Rafi Rubin | 837b475 | 2009-06-23 14:09:26 -0400 | [diff] [blame] | 139 | |
| 140 | case HID_DG_INRANGE: |
| 141 | if (field->application & 0x3) |
| 142 | nd->pen_active = (value != 0); |
| 143 | else |
| 144 | nd->finger_active = (value != 0); |
| 145 | return 0; |
| 146 | |
| 147 | case HID_DG_INVERT: |
| 148 | nd->inverted = value; |
| 149 | return 0; |
| 150 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 151 | case HID_GD_X: |
| 152 | nd->x = value; |
| 153 | nd->reading_a_point = 1; |
| 154 | break; |
| 155 | case HID_GD_Y: |
| 156 | nd->y = value; |
| 157 | break; |
| 158 | case HID_DG_CONTACTID: |
| 159 | nd->id = value; |
| 160 | /* we receive this only when in multitouch mode */ |
| 161 | nd->found_contact_id = 1; |
| 162 | break; |
| 163 | case HID_DG_WIDTH: |
| 164 | nd->w = value; |
| 165 | break; |
| 166 | case HID_DG_HEIGHT: |
| 167 | nd->h = value; |
| 168 | /* |
| 169 | * when in single touch mode, this is the last |
| 170 | * report received in a finger event. We want |
| 171 | * to emit a normal (X, Y) position |
| 172 | */ |
Rafi Rubin | 837b475 | 2009-06-23 14:09:26 -0400 | [diff] [blame] | 173 | if (!nd->found_contact_id) { |
| 174 | if (nd->pen_active && nd->finger_active) { |
| 175 | input_report_key(input, BTN_TOOL_DOUBLETAP, 0); |
| 176 | input_report_key(input, BTN_TOOL_DOUBLETAP, 1); |
| 177 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 178 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 179 | input_event(input, EV_ABS, ABS_Y, nd->y); |
| 180 | } |
| 181 | break; |
| 182 | case HID_DG_TIPPRESSURE: |
| 183 | /* |
| 184 | * when in single touch mode, this is the last |
| 185 | * report received in a pen event. We want |
| 186 | * to emit a normal (X, Y) position |
| 187 | */ |
| 188 | if (! nd->found_contact_id) { |
Rafi Rubin | 837b475 | 2009-06-23 14:09:26 -0400 | [diff] [blame] | 189 | if (nd->pen_active && nd->finger_active) { |
| 190 | input_report_key(input, |
| 191 | nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN |
| 192 | , 0); |
| 193 | input_report_key(input, |
| 194 | nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN |
| 195 | , 1); |
| 196 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 197 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 198 | input_event(input, EV_ABS, ABS_Y, nd->y); |
| 199 | input_event(input, EV_ABS, ABS_PRESSURE, value); |
| 200 | } |
| 201 | break; |
| 202 | case 0xff000002: |
| 203 | /* |
| 204 | * we receive this when the device is in multitouch |
| 205 | * mode. The first of the three values tagged with |
| 206 | * this usage tells if the contact point is real |
| 207 | * or a placeholder |
| 208 | */ |
| 209 | if (!nd->reading_a_point || value != 1) |
| 210 | break; |
| 211 | /* emit a normal (X, Y) for the first point only */ |
| 212 | if (nd->id == 0) { |
| 213 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 214 | input_event(input, EV_ABS, ABS_Y, nd->y); |
| 215 | } |
| 216 | input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x); |
| 217 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y); |
| 218 | if (nd->w > nd->h) { |
| 219 | input_event(input, EV_ABS, |
| 220 | ABS_MT_ORIENTATION, 1); |
| 221 | input_event(input, EV_ABS, |
| 222 | ABS_MT_TOUCH_MAJOR, nd->w); |
| 223 | input_event(input, EV_ABS, |
| 224 | ABS_MT_TOUCH_MINOR, nd->h); |
| 225 | } else { |
| 226 | input_event(input, EV_ABS, |
| 227 | ABS_MT_ORIENTATION, 0); |
| 228 | input_event(input, EV_ABS, |
| 229 | ABS_MT_TOUCH_MAJOR, nd->h); |
| 230 | input_event(input, EV_ABS, |
| 231 | ABS_MT_TOUCH_MINOR, nd->w); |
| 232 | } |
| 233 | input_mt_sync(field->hidinput->input); |
| 234 | nd->reading_a_point = 0; |
| 235 | nd->found_contact_id = 0; |
| 236 | break; |
| 237 | |
| 238 | default: |
| 239 | /* fallback to the generic hidinput handling */ |
| 240 | return 0; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* we have handled the hidinput part, now remains hiddev */ |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 245 | if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event) |
| 246 | hid->hiddev_hid_event(hid, field, usage, value); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 247 | |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 252 | { |
| 253 | int ret; |
| 254 | struct ntrig_data *nd; |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 255 | struct hid_input *hidinput; |
| 256 | struct input_dev *input; |
| 257 | |
| 258 | if (id->driver_data) |
| 259 | hdev->quirks |= HID_QUIRK_MULTI_INPUT; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 260 | |
| 261 | nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL); |
| 262 | if (!nd) { |
| 263 | dev_err(&hdev->dev, "cannot allocate N-Trig data\n"); |
| 264 | return -ENOMEM; |
| 265 | } |
| 266 | nd->reading_a_point = 0; |
| 267 | nd->found_contact_id = 0; |
| 268 | hid_set_drvdata(hdev, nd); |
| 269 | |
| 270 | ret = hid_parse(hdev); |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 271 | if (ret) { |
| 272 | dev_err(&hdev->dev, "parse failed\n"); |
| 273 | goto err_free; |
| 274 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 275 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 276 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); |
| 277 | if (ret) { |
| 278 | dev_err(&hdev->dev, "hw start failed\n"); |
| 279 | goto err_free; |
| 280 | } |
Rafi Rubin | 837b475 | 2009-06-23 14:09:26 -0400 | [diff] [blame] | 281 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 282 | |
| 283 | list_for_each_entry(hidinput, &hdev->inputs, list) { |
| 284 | input = hidinput->input; |
| 285 | switch (hidinput->report->field[0]->application) { |
| 286 | case HID_DG_PEN: |
| 287 | input->name = "N-Trig Pen"; |
| 288 | break; |
| 289 | case HID_DG_TOUCHSCREEN: |
| 290 | /* |
| 291 | * The physical touchscreen (single touch) |
| 292 | * input has a value for physical, whereas |
| 293 | * the multitouch only has logical input |
| 294 | * fields. |
| 295 | */ |
| 296 | input->name = |
| 297 | (hidinput->report->field[0] |
| 298 | ->physical) ? |
| 299 | "N-Trig Touchscreen" : |
| 300 | "N-Trig MultiTouch"; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | err_free: |
| 307 | kfree(nd); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static void ntrig_remove(struct hid_device *hdev) |
| 312 | { |
| 313 | hid_hw_stop(hdev); |
| 314 | kfree(hid_get_drvdata(hdev)); |
| 315 | } |
| 316 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 317 | static const struct hid_device_id ntrig_devices[] = { |
| 318 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN), |
| 319 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 320 | { } |
| 321 | }; |
| 322 | MODULE_DEVICE_TABLE(hid, ntrig_devices); |
| 323 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 324 | static const struct hid_usage_id ntrig_grabbed_usages[] = { |
| 325 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame^] | 326 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 327 | }; |
| 328 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 329 | static struct hid_driver ntrig_driver = { |
| 330 | .name = "ntrig", |
| 331 | .id_table = ntrig_devices, |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 332 | .probe = ntrig_probe, |
| 333 | .remove = ntrig_remove, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 334 | .input_mapping = ntrig_input_mapping, |
| 335 | .input_mapped = ntrig_input_mapped, |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 336 | .usage_table = ntrig_grabbed_usages, |
| 337 | .event = ntrig_event, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 338 | }; |
| 339 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 340 | static int __init ntrig_init(void) |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 341 | { |
| 342 | return hid_register_driver(&ntrig_driver); |
| 343 | } |
| 344 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 345 | static void __exit ntrig_exit(void) |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 346 | { |
| 347 | hid_unregister_driver(&ntrig_driver); |
| 348 | } |
| 349 | |
| 350 | module_init(ntrig_init); |
| 351 | module_exit(ntrig_exit); |
| 352 | MODULE_LICENSE("GPL"); |