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 | * |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 4 | * Copyright (c) 2008-2010 Rafi Rubin |
| 5 | * Copyright (c) 2009-2010 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> |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 18 | #include <linux/usb.h> |
| 19 | #include "usbhid/usbhid.h" |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 22 | |
| 23 | #include "hid-ids.h" |
| 24 | |
| 25 | #define NTRIG_DUPLICATE_USAGES 0x001 |
| 26 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 27 | static unsigned int min_width; |
| 28 | static unsigned int min_height; |
| 29 | static unsigned int activate_slack = 1; |
| 30 | static unsigned int deactivate_slack = 4; |
| 31 | static unsigned int activation_width = 64; |
| 32 | static unsigned int activation_height = 32; |
| 33 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 34 | struct ntrig_data { |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 35 | /* Incoming raw values for a single contact */ |
| 36 | __u16 x, y, w, h; |
| 37 | __u16 id; |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 38 | |
| 39 | bool tipswitch; |
| 40 | bool confidence; |
| 41 | bool first_contact_touch; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 42 | |
| 43 | bool reading_mt; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 44 | |
| 45 | __u8 mt_footer[4]; |
| 46 | __u8 mt_foot_count; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 47 | |
| 48 | /* The current activation state. */ |
| 49 | __s8 act_state; |
| 50 | |
| 51 | /* Empty frames to ignore before recognizing the end of activity */ |
| 52 | __s8 deactivate_slack; |
| 53 | |
| 54 | /* Frames to ignore before acknowledging the start of activity */ |
| 55 | __s8 activate_slack; |
| 56 | |
| 57 | /* Minimum size contact to accept */ |
| 58 | __u16 min_width; |
| 59 | __u16 min_height; |
| 60 | |
| 61 | /* Threshold to override activation slack */ |
| 62 | __u16 activation_width; |
| 63 | __u16 activation_height; |
| 64 | |
| 65 | __u16 sensor_logical_width; |
| 66 | __u16 sensor_logical_height; |
| 67 | __u16 sensor_physical_width; |
| 68 | __u16 sensor_physical_height; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * this driver is aimed at two firmware versions in circulation: |
| 73 | * - dual pen/finger single touch |
| 74 | * - finger multitouch, pen not working |
| 75 | */ |
| 76 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 77 | static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 78 | struct hid_field *field, struct hid_usage *usage, |
| 79 | unsigned long **bit, int *max) |
| 80 | { |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 81 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 82 | |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 83 | /* No special mappings needed for the pen and single touch */ |
| 84 | if (field->physical) |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 85 | return 0; |
| 86 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 87 | switch (usage->hid & HID_USAGE_PAGE) { |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 88 | case HID_UP_GENDESK: |
| 89 | switch (usage->hid) { |
| 90 | case HID_GD_X: |
| 91 | hid_map_usage(hi, usage, bit, max, |
| 92 | EV_ABS, ABS_MT_POSITION_X); |
| 93 | input_set_abs_params(hi->input, ABS_X, |
| 94 | field->logical_minimum, |
| 95 | field->logical_maximum, 0, 0); |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 96 | |
| 97 | if (!nd->sensor_logical_width) { |
| 98 | nd->sensor_logical_width = |
| 99 | field->logical_maximum - |
| 100 | field->logical_minimum; |
| 101 | nd->sensor_physical_width = |
| 102 | field->physical_maximum - |
| 103 | field->physical_minimum; |
| 104 | nd->activation_width = activation_width * |
| 105 | nd->sensor_logical_width / |
| 106 | nd->sensor_physical_width; |
| 107 | nd->min_width = min_width * |
| 108 | nd->sensor_logical_width / |
| 109 | nd->sensor_physical_width; |
| 110 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 111 | return 1; |
| 112 | case HID_GD_Y: |
| 113 | hid_map_usage(hi, usage, bit, max, |
| 114 | EV_ABS, ABS_MT_POSITION_Y); |
| 115 | input_set_abs_params(hi->input, ABS_Y, |
| 116 | field->logical_minimum, |
| 117 | field->logical_maximum, 0, 0); |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 118 | |
| 119 | if (!nd->sensor_logical_height) { |
| 120 | nd->sensor_logical_height = |
| 121 | field->logical_maximum - |
| 122 | field->logical_minimum; |
| 123 | nd->sensor_physical_height = |
| 124 | field->physical_maximum - |
| 125 | field->physical_minimum; |
| 126 | nd->activation_height = activation_height * |
| 127 | nd->sensor_logical_height / |
| 128 | nd->sensor_physical_height; |
| 129 | nd->min_height = min_height * |
| 130 | nd->sensor_logical_height / |
| 131 | nd->sensor_physical_height; |
| 132 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 133 | return 1; |
| 134 | } |
| 135 | return 0; |
| 136 | |
| 137 | case HID_UP_DIGITIZER: |
| 138 | switch (usage->hid) { |
| 139 | /* we do not want to map these for now */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 140 | case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 141 | case HID_DG_INPUTMODE: |
| 142 | case HID_DG_DEVICEINDEX: |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 143 | case HID_DG_CONTACTMAX: |
| 144 | return -1; |
| 145 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 146 | /* width/height mapped on TouchMajor/TouchMinor/Orientation */ |
| 147 | case HID_DG_WIDTH: |
| 148 | hid_map_usage(hi, usage, bit, max, |
| 149 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
| 150 | return 1; |
| 151 | case HID_DG_HEIGHT: |
| 152 | hid_map_usage(hi, usage, bit, max, |
| 153 | EV_ABS, ABS_MT_TOUCH_MINOR); |
| 154 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
| 155 | 0, 1, 0, 0); |
| 156 | return 1; |
| 157 | } |
| 158 | return 0; |
| 159 | |
| 160 | case 0xff000000: |
| 161 | /* we do not want to map these: no input-oriented meaning */ |
| 162 | return -1; |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 163 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 164 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 169 | struct hid_field *field, struct hid_usage *usage, |
| 170 | unsigned long **bit, int *max) |
| 171 | { |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 172 | /* No special mappings needed for the pen and single touch */ |
| 173 | if (field->physical) |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 174 | return 0; |
Rafi Rubin | b0549cf | 2010-02-11 22:14:06 -0500 | [diff] [blame] | 175 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 176 | if (usage->type == EV_KEY || usage->type == EV_REL |
| 177 | || usage->type == EV_ABS) |
| 178 | clear_bit(usage->code, *bit); |
| 179 | |
| 180 | return 0; |
| 181 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * this function is called upon all reports |
| 185 | * so that we can filter contact point information, |
| 186 | * decide whether we are in multi or single touch mode |
| 187 | * and call input_mt_sync after each point if necessary |
| 188 | */ |
| 189 | static int ntrig_event (struct hid_device *hid, struct hid_field *field, |
| 190 | struct hid_usage *usage, __s32 value) |
| 191 | { |
| 192 | struct input_dev *input = field->hidinput->input; |
| 193 | struct ntrig_data *nd = hid_get_drvdata(hid); |
| 194 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 195 | /* No special handling needed for the pen */ |
| 196 | if (field->application == HID_DG_PEN) |
| 197 | return 0; |
| 198 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 199 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 200 | switch (usage->hid) { |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 201 | case 0xff000001: |
| 202 | /* Tag indicating the start of a multitouch group */ |
| 203 | nd->reading_mt = 1; |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 204 | nd->first_contact_touch = 0; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 205 | break; |
Rafi Rubin | 2886539 | 2010-03-10 16:10:28 +0100 | [diff] [blame] | 206 | case HID_DG_TIPSWITCH: |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 207 | nd->tipswitch = value; |
Rafi Rubin | 2886539 | 2010-03-10 16:10:28 +0100 | [diff] [blame] | 208 | /* Prevent emission of touch until validated */ |
| 209 | return 1; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 210 | case HID_DG_CONFIDENCE: |
| 211 | nd->confidence = value; |
| 212 | break; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 213 | case HID_GD_X: |
| 214 | nd->x = value; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 215 | /* Clear the contact footer */ |
| 216 | nd->mt_foot_count = 0; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 217 | break; |
| 218 | case HID_GD_Y: |
| 219 | nd->y = value; |
| 220 | break; |
| 221 | case HID_DG_CONTACTID: |
| 222 | nd->id = value; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 223 | break; |
| 224 | case HID_DG_WIDTH: |
| 225 | nd->w = value; |
| 226 | break; |
| 227 | case HID_DG_HEIGHT: |
| 228 | nd->h = value; |
| 229 | /* |
| 230 | * when in single touch mode, this is the last |
| 231 | * report received in a finger event. We want |
| 232 | * to emit a normal (X, Y) position |
| 233 | */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 234 | if (!nd->reading_mt) { |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 235 | /* |
| 236 | * TipSwitch indicates the presence of a |
| 237 | * finger in single touch mode. |
| 238 | */ |
Rafi Rubin | 2170c5a | 2010-04-09 17:58:25 -0400 | [diff] [blame] | 239 | input_report_key(input, BTN_TOUCH, |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 240 | nd->tipswitch); |
| 241 | input_report_key(input, BTN_TOOL_DOUBLETAP, |
| 242 | nd->tipswitch); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 243 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 244 | input_event(input, EV_ABS, ABS_Y, nd->y); |
| 245 | } |
| 246 | break; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 247 | case 0xff000002: |
| 248 | /* |
| 249 | * we receive this when the device is in multitouch |
| 250 | * mode. The first of the three values tagged with |
| 251 | * this usage tells if the contact point is real |
| 252 | * or a placeholder |
| 253 | */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 254 | |
| 255 | /* Shouldn't get more than 4 footer packets, so skip */ |
| 256 | if (nd->mt_foot_count >= 4) |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 257 | break; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 258 | |
| 259 | nd->mt_footer[nd->mt_foot_count++] = value; |
| 260 | |
| 261 | /* if the footer isn't complete break */ |
| 262 | if (nd->mt_foot_count != 4) |
| 263 | break; |
| 264 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 265 | /* Pen activity signal. */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 266 | if (nd->mt_footer[2]) { |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 267 | /* |
| 268 | * When the pen deactivates touch, we see a |
| 269 | * bogus frame with ContactCount > 0. |
| 270 | * We can |
| 271 | * save a bit of work by ensuring act_state < 0 |
| 272 | * even if deactivation slack is turned off. |
| 273 | */ |
| 274 | nd->act_state = deactivate_slack - 1; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 275 | nd->confidence = 0; |
| 276 | break; |
| 277 | } |
| 278 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 279 | /* |
| 280 | * The first footer value indicates the presence of a |
| 281 | * finger. |
| 282 | */ |
| 283 | if (nd->mt_footer[0]) { |
| 284 | /* |
| 285 | * We do not want to process contacts under |
| 286 | * the size threshold, but do not want to |
| 287 | * ignore them for activation state |
| 288 | */ |
| 289 | if (nd->w < nd->min_width || |
| 290 | nd->h < nd->min_height) |
| 291 | nd->confidence = 0; |
| 292 | } else |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 293 | break; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 294 | |
| 295 | if (nd->act_state > 0) { |
| 296 | /* |
| 297 | * Contact meets the activation size threshold |
| 298 | */ |
| 299 | if (nd->w >= nd->activation_width && |
| 300 | nd->h >= nd->activation_height) { |
| 301 | if (nd->id) |
| 302 | /* |
| 303 | * first contact, activate now |
| 304 | */ |
| 305 | nd->act_state = 0; |
| 306 | else { |
| 307 | /* |
| 308 | * avoid corrupting this frame |
| 309 | * but ensure next frame will |
| 310 | * be active |
| 311 | */ |
| 312 | nd->act_state = 1; |
| 313 | break; |
| 314 | } |
| 315 | } else |
| 316 | /* |
| 317 | * Defer adjusting the activation state |
| 318 | * until the end of the frame. |
| 319 | */ |
| 320 | break; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 323 | /* Discarding this contact */ |
| 324 | if (!nd->confidence) |
| 325 | break; |
| 326 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 327 | /* emit a normal (X, Y) for the first point only */ |
| 328 | if (nd->id == 0) { |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 329 | /* |
| 330 | * TipSwitch is superfluous in multitouch |
| 331 | * mode. The footer events tell us |
| 332 | * if there is a finger on the screen or |
| 333 | * not. |
| 334 | */ |
| 335 | nd->first_contact_touch = nd->confidence; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 336 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 337 | input_event(input, EV_ABS, ABS_Y, nd->y); |
| 338 | } |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 339 | |
| 340 | /* Emit MT events */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 341 | input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x); |
| 342 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y); |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 343 | |
| 344 | /* |
| 345 | * Translate from height and width to size |
| 346 | * and orientation. |
| 347 | */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 348 | if (nd->w > nd->h) { |
| 349 | input_event(input, EV_ABS, |
| 350 | ABS_MT_ORIENTATION, 1); |
| 351 | input_event(input, EV_ABS, |
| 352 | ABS_MT_TOUCH_MAJOR, nd->w); |
| 353 | input_event(input, EV_ABS, |
| 354 | ABS_MT_TOUCH_MINOR, nd->h); |
| 355 | } else { |
| 356 | input_event(input, EV_ABS, |
| 357 | ABS_MT_ORIENTATION, 0); |
| 358 | input_event(input, EV_ABS, |
| 359 | ABS_MT_TOUCH_MAJOR, nd->h); |
| 360 | input_event(input, EV_ABS, |
| 361 | ABS_MT_TOUCH_MINOR, nd->w); |
| 362 | } |
| 363 | input_mt_sync(field->hidinput->input); |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 364 | break; |
| 365 | |
| 366 | case HID_DG_CONTACTCOUNT: /* End of a multitouch group */ |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 367 | if (!nd->reading_mt) /* Just to be sure */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 368 | break; |
| 369 | |
| 370 | nd->reading_mt = 0; |
| 371 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 372 | |
| 373 | /* |
| 374 | * Activation state machine logic: |
| 375 | * |
| 376 | * Fundamental states: |
| 377 | * state > 0: Inactive |
| 378 | * state <= 0: Active |
| 379 | * state < -deactivate_slack: |
| 380 | * Pen termination of touch |
| 381 | * |
| 382 | * Specific values of interest |
| 383 | * state == activate_slack |
| 384 | * no valid input since the last reset |
| 385 | * |
| 386 | * state == 0 |
| 387 | * general operational state |
| 388 | * |
| 389 | * state == -deactivate_slack |
| 390 | * read sufficient empty frames to accept |
| 391 | * the end of input and reset |
| 392 | */ |
| 393 | |
| 394 | if (nd->act_state > 0) { /* Currently inactive */ |
| 395 | if (value) |
| 396 | /* |
| 397 | * Consider each live contact as |
| 398 | * evidence of intentional activity. |
| 399 | */ |
| 400 | nd->act_state = (nd->act_state > value) |
| 401 | ? nd->act_state - value |
| 402 | : 0; |
| 403 | else |
| 404 | /* |
| 405 | * Empty frame before we hit the |
| 406 | * activity threshold, reset. |
| 407 | */ |
| 408 | nd->act_state = nd->activate_slack; |
| 409 | |
| 410 | /* |
| 411 | * Entered this block inactive and no |
| 412 | * coordinates sent this frame, so hold off |
| 413 | * on button state. |
| 414 | */ |
| 415 | break; |
| 416 | } else { /* Currently active */ |
| 417 | if (value && nd->act_state >= |
| 418 | nd->deactivate_slack) |
| 419 | /* |
| 420 | * Live point: clear accumulated |
| 421 | * deactivation count. |
| 422 | */ |
| 423 | nd->act_state = 0; |
| 424 | else if (nd->act_state <= nd->deactivate_slack) |
| 425 | /* |
| 426 | * We've consumed the deactivation |
| 427 | * slack, time to deactivate and reset. |
| 428 | */ |
| 429 | nd->act_state = |
| 430 | nd->activate_slack; |
| 431 | else { /* Move towards deactivation */ |
| 432 | nd->act_state--; |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (nd->first_contact_touch && nd->act_state <= 0) { |
| 438 | /* |
| 439 | * Check to see if we're ready to start |
| 440 | * emitting touch events. |
| 441 | * |
| 442 | * Note: activation slack will decrease over |
| 443 | * the course of the frame, and it will be |
| 444 | * inconsistent from the start to the end of |
| 445 | * the frame. However if the frame starts |
| 446 | * with slack, first_contact_touch will still |
| 447 | * be 0 and we will not get to this point. |
| 448 | */ |
Rafi Rubin | ed7e2ca | 2010-05-03 05:08:30 -0400 | [diff] [blame] | 449 | input_report_key(input, BTN_TOOL_DOUBLETAP, 1); |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 450 | input_report_key(input, BTN_TOUCH, 1); |
| 451 | } else { |
Rafi Rubin | ed7e2ca | 2010-05-03 05:08:30 -0400 | [diff] [blame] | 452 | input_report_key(input, BTN_TOOL_DOUBLETAP, 0); |
Rafi Rubin | 2886539 | 2010-03-10 16:10:28 +0100 | [diff] [blame] | 453 | input_report_key(input, BTN_TOUCH, 0); |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 454 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | default: |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 458 | /* fall-back to the generic hidinput handling */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /* we have handled the hidinput part, now remains hiddev */ |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 464 | if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event) |
| 465 | hid->hiddev_hid_event(hid, field, usage, value); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 466 | |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 471 | { |
| 472 | int ret; |
| 473 | struct ntrig_data *nd; |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 474 | struct hid_input *hidinput; |
| 475 | struct input_dev *input; |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 476 | struct hid_report *report; |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 477 | |
| 478 | if (id->driver_data) |
| 479 | hdev->quirks |= HID_QUIRK_MULTI_INPUT; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 480 | |
| 481 | nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL); |
| 482 | if (!nd) { |
| 483 | dev_err(&hdev->dev, "cannot allocate N-Trig data\n"); |
| 484 | return -ENOMEM; |
| 485 | } |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 486 | |
| 487 | nd->reading_mt = 0; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame^] | 488 | nd->min_width = 0; |
| 489 | nd->min_height = 0; |
| 490 | nd->activate_slack = activate_slack; |
| 491 | nd->act_state = activate_slack; |
| 492 | nd->deactivate_slack = -deactivate_slack; |
| 493 | nd->sensor_logical_width = 0; |
| 494 | nd->sensor_logical_height = 0; |
| 495 | nd->sensor_physical_width = 0; |
| 496 | nd->sensor_physical_height = 0; |
| 497 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 498 | hid_set_drvdata(hdev, nd); |
| 499 | |
| 500 | ret = hid_parse(hdev); |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 501 | if (ret) { |
| 502 | dev_err(&hdev->dev, "parse failed\n"); |
| 503 | goto err_free; |
| 504 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 505 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 506 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); |
| 507 | if (ret) { |
| 508 | dev_err(&hdev->dev, "hw start failed\n"); |
| 509 | goto err_free; |
| 510 | } |
Rafi Rubin | 837b475 | 2009-06-23 14:09:26 -0400 | [diff] [blame] | 511 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 512 | |
| 513 | list_for_each_entry(hidinput, &hdev->inputs, list) { |
Rafi Rubin | 2886539 | 2010-03-10 16:10:28 +0100 | [diff] [blame] | 514 | if (hidinput->report->maxfield < 1) |
| 515 | continue; |
| 516 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 517 | input = hidinput->input; |
| 518 | switch (hidinput->report->field[0]->application) { |
| 519 | case HID_DG_PEN: |
| 520 | input->name = "N-Trig Pen"; |
| 521 | break; |
| 522 | case HID_DG_TOUCHSCREEN: |
Rafi Rubin | 2886539 | 2010-03-10 16:10:28 +0100 | [diff] [blame] | 523 | /* These keys are redundant for fingers, clear them |
| 524 | * to prevent incorrect identification */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 525 | __clear_bit(BTN_TOOL_PEN, input->keybit); |
Rafi Rubin | 2886539 | 2010-03-10 16:10:28 +0100 | [diff] [blame] | 526 | __clear_bit(BTN_TOOL_FINGER, input->keybit); |
| 527 | __clear_bit(BTN_0, input->keybit); |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 528 | __set_bit(BTN_TOOL_DOUBLETAP, input->keybit); |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 529 | /* |
| 530 | * The physical touchscreen (single touch) |
| 531 | * input has a value for physical, whereas |
| 532 | * the multitouch only has logical input |
| 533 | * fields. |
| 534 | */ |
| 535 | input->name = |
| 536 | (hidinput->report->field[0] |
| 537 | ->physical) ? |
| 538 | "N-Trig Touchscreen" : |
| 539 | "N-Trig MultiTouch"; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | |
Jiri Kosina | c085855 | 2010-04-07 12:10:29 +0200 | [diff] [blame] | 544 | /* This is needed for devices with more recent firmware versions */ |
| 545 | report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a]; |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 546 | if (report) |
| 547 | usbhid_submit_report(hdev, report, USB_DIR_OUT); |
| 548 | |
| 549 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 550 | return 0; |
| 551 | err_free: |
| 552 | kfree(nd); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | static void ntrig_remove(struct hid_device *hdev) |
| 557 | { |
| 558 | hid_hw_stop(hdev); |
| 559 | kfree(hid_get_drvdata(hdev)); |
| 560 | } |
| 561 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 562 | static const struct hid_device_id ntrig_devices[] = { |
| 563 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN), |
| 564 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 565 | { } |
| 566 | }; |
| 567 | MODULE_DEVICE_TABLE(hid, ntrig_devices); |
| 568 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 569 | static const struct hid_usage_id ntrig_grabbed_usages[] = { |
| 570 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 571 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 572 | }; |
| 573 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 574 | static struct hid_driver ntrig_driver = { |
| 575 | .name = "ntrig", |
| 576 | .id_table = ntrig_devices, |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 577 | .probe = ntrig_probe, |
| 578 | .remove = ntrig_remove, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 579 | .input_mapping = ntrig_input_mapping, |
| 580 | .input_mapped = ntrig_input_mapped, |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 581 | .usage_table = ntrig_grabbed_usages, |
| 582 | .event = ntrig_event, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 583 | }; |
| 584 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 585 | static int __init ntrig_init(void) |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 586 | { |
| 587 | return hid_register_driver(&ntrig_driver); |
| 588 | } |
| 589 | |
Peter Huewe | a24f423 | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 590 | static void __exit ntrig_exit(void) |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 591 | { |
| 592 | hid_unregister_driver(&ntrig_driver); |
| 593 | } |
| 594 | |
| 595 | module_init(ntrig_init); |
| 596 | module_exit(ntrig_exit); |
| 597 | MODULE_LICENSE("GPL"); |