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; |
Rafi Rubin | ab3f498 | 2010-05-04 14:20:16 -0400 | [diff] [blame] | 28 | module_param(min_width, uint, 0644); |
| 29 | MODULE_PARM_DESC(min_width, "Minimum touch contact width to accept."); |
| 30 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 31 | static unsigned int min_height; |
Rafi Rubin | ab3f498 | 2010-05-04 14:20:16 -0400 | [diff] [blame] | 32 | module_param(min_height, uint, 0644); |
| 33 | MODULE_PARM_DESC(min_height, "Minimum touch contact height to accept."); |
| 34 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 35 | static unsigned int activate_slack = 1; |
Rafi Rubin | ab3f498 | 2010-05-04 14:20:16 -0400 | [diff] [blame] | 36 | module_param(activate_slack, uint, 0644); |
| 37 | MODULE_PARM_DESC(activate_slack, "Number of touch frames to ignore at " |
| 38 | "the start of touch input."); |
| 39 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 40 | static unsigned int deactivate_slack = 4; |
Rafi Rubin | ab3f498 | 2010-05-04 14:20:16 -0400 | [diff] [blame] | 41 | module_param(deactivate_slack, uint, 0644); |
| 42 | MODULE_PARM_DESC(deactivate_slack, "Number of empty frames to ignore before " |
| 43 | "deactivating touch."); |
| 44 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 45 | static unsigned int activation_width = 64; |
Rafi Rubin | ab3f498 | 2010-05-04 14:20:16 -0400 | [diff] [blame] | 46 | module_param(activation_width, uint, 0644); |
| 47 | MODULE_PARM_DESC(activation_width, "Width threshold to immediately start " |
| 48 | "processing touch events."); |
| 49 | |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 50 | static unsigned int activation_height = 32; |
Rafi Rubin | ab3f498 | 2010-05-04 14:20:16 -0400 | [diff] [blame] | 51 | module_param(activation_height, uint, 0644); |
| 52 | MODULE_PARM_DESC(activation_height, "Height threshold to immediately start " |
| 53 | "processing touch events."); |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 54 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 55 | struct ntrig_data { |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 56 | /* Incoming raw values for a single contact */ |
| 57 | __u16 x, y, w, h; |
| 58 | __u16 id; |
Rafi Rubin | 250d377 | 2010-05-03 05:08:29 -0400 | [diff] [blame] | 59 | |
| 60 | bool tipswitch; |
| 61 | bool confidence; |
| 62 | bool first_contact_touch; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 63 | |
| 64 | bool reading_mt; |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 65 | |
| 66 | __u8 mt_footer[4]; |
| 67 | __u8 mt_foot_count; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 68 | |
| 69 | /* The current activation state. */ |
| 70 | __s8 act_state; |
| 71 | |
| 72 | /* Empty frames to ignore before recognizing the end of activity */ |
| 73 | __s8 deactivate_slack; |
| 74 | |
| 75 | /* Frames to ignore before acknowledging the start of activity */ |
| 76 | __s8 activate_slack; |
| 77 | |
| 78 | /* Minimum size contact to accept */ |
| 79 | __u16 min_width; |
| 80 | __u16 min_height; |
| 81 | |
| 82 | /* Threshold to override activation slack */ |
| 83 | __u16 activation_width; |
| 84 | __u16 activation_height; |
| 85 | |
| 86 | __u16 sensor_logical_width; |
| 87 | __u16 sensor_logical_height; |
| 88 | __u16 sensor_physical_width; |
| 89 | __u16 sensor_physical_height; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 92 | |
Rafi Rubin | 0277873 | 2010-09-08 11:46:14 +0200 | [diff] [blame] | 93 | /* |
| 94 | * This function converts the 4 byte raw firmware code into |
| 95 | * a string containing 5 comma separated numbers. |
| 96 | */ |
| 97 | static int ntrig_version_string(unsigned char *raw, char *buf) |
| 98 | { |
| 99 | __u8 a = (raw[1] & 0x0e) >> 1; |
| 100 | __u8 b = (raw[0] & 0x3c) >> 2; |
| 101 | __u8 c = ((raw[0] & 0x03) << 3) | ((raw[3] & 0xe0) >> 5); |
| 102 | __u8 d = ((raw[3] & 0x07) << 3) | ((raw[2] & 0xe0) >> 5); |
| 103 | __u8 e = raw[2] & 0x07; |
| 104 | |
| 105 | /* |
| 106 | * As yet unmapped bits: |
| 107 | * 0b11000000 0b11110001 0b00011000 0b00011000 |
| 108 | */ |
| 109 | |
| 110 | return sprintf(buf, "%u.%u.%u.%u.%u", a, b, c, d, e); |
| 111 | } |
| 112 | |
Rafi Rubin | 7b2a64c | 2011-03-09 23:33:52 -0500 | [diff] [blame] | 113 | static inline int ntrig_get_mode(struct hid_device *hdev) |
| 114 | { |
| 115 | struct hid_report *report = hdev->report_enum[HID_FEATURE_REPORT]. |
| 116 | report_id_hash[0x0d]; |
| 117 | |
Kees Cook | 875b4e3 | 2013-08-28 22:31:28 +0200 | [diff] [blame] | 118 | if (!report || report->maxfield < 1 || |
| 119 | report->field[0]->report_count < 1) |
Rafi Rubin | 7b2a64c | 2011-03-09 23:33:52 -0500 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 122 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 123 | hid_hw_wait(hdev); |
Rafi Rubin | 7b2a64c | 2011-03-09 23:33:52 -0500 | [diff] [blame] | 124 | return (int)report->field[0]->value[0]; |
| 125 | } |
| 126 | |
| 127 | static inline void ntrig_set_mode(struct hid_device *hdev, const int mode) |
| 128 | { |
| 129 | struct hid_report *report; |
| 130 | __u8 mode_commands[4] = { 0xe, 0xf, 0x1b, 0x10 }; |
| 131 | |
| 132 | if (mode < 0 || mode > 3) |
| 133 | return; |
| 134 | |
| 135 | report = hdev->report_enum[HID_FEATURE_REPORT]. |
| 136 | report_id_hash[mode_commands[mode]]; |
| 137 | |
| 138 | if (!report) |
| 139 | return; |
| 140 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 141 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
Rafi Rubin | 7b2a64c | 2011-03-09 23:33:52 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Rafi Rubin | 0277873 | 2010-09-08 11:46:14 +0200 | [diff] [blame] | 144 | static void ntrig_report_version(struct hid_device *hdev) |
| 145 | { |
| 146 | int ret; |
| 147 | char buf[20]; |
| 148 | struct usb_device *usb_dev = hid_to_usb_dev(hdev); |
| 149 | unsigned char *data = kmalloc(8, GFP_KERNEL); |
| 150 | |
| 151 | if (!data) |
| 152 | goto err_free; |
| 153 | |
| 154 | ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), |
| 155 | USB_REQ_CLEAR_FEATURE, |
| 156 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | |
| 157 | USB_DIR_IN, |
| 158 | 0x30c, 1, data, 8, |
| 159 | USB_CTRL_SET_TIMEOUT); |
| 160 | |
| 161 | if (ret == 8) { |
| 162 | ret = ntrig_version_string(&data[2], buf); |
| 163 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 164 | hid_info(hdev, "Firmware version: %s (%02x%02x %02x%02x)\n", |
Rafi Rubin | 0277873 | 2010-09-08 11:46:14 +0200 | [diff] [blame] | 165 | buf, data[2], data[3], data[4], data[5]); |
| 166 | } |
| 167 | |
| 168 | err_free: |
| 169 | kfree(data); |
| 170 | } |
| 171 | |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 172 | static ssize_t show_phys_width(struct device *dev, |
| 173 | struct device_attribute *attr, |
| 174 | char *buf) |
| 175 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 176 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 177 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 178 | |
| 179 | return sprintf(buf, "%d\n", nd->sensor_physical_width); |
| 180 | } |
| 181 | |
| 182 | static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL); |
| 183 | |
| 184 | static ssize_t show_phys_height(struct device *dev, |
| 185 | struct device_attribute *attr, |
| 186 | char *buf) |
| 187 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 188 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 189 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 190 | |
| 191 | return sprintf(buf, "%d\n", nd->sensor_physical_height); |
| 192 | } |
| 193 | |
| 194 | static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL); |
| 195 | |
| 196 | static ssize_t show_log_width(struct device *dev, |
| 197 | struct device_attribute *attr, |
| 198 | char *buf) |
| 199 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 200 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 201 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 202 | |
| 203 | return sprintf(buf, "%d\n", nd->sensor_logical_width); |
| 204 | } |
| 205 | |
| 206 | static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL); |
| 207 | |
| 208 | static ssize_t show_log_height(struct device *dev, |
| 209 | struct device_attribute *attr, |
| 210 | char *buf) |
| 211 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 212 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 213 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 214 | |
| 215 | return sprintf(buf, "%d\n", nd->sensor_logical_height); |
| 216 | } |
| 217 | |
| 218 | static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL); |
| 219 | |
| 220 | static ssize_t show_min_width(struct device *dev, |
| 221 | struct device_attribute *attr, |
| 222 | char *buf) |
| 223 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 224 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 225 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 226 | |
| 227 | return sprintf(buf, "%d\n", nd->min_width * |
| 228 | nd->sensor_physical_width / |
| 229 | nd->sensor_logical_width); |
| 230 | } |
| 231 | |
| 232 | static ssize_t set_min_width(struct device *dev, |
| 233 | struct device_attribute *attr, |
| 234 | const char *buf, size_t count) |
| 235 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 236 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 237 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 238 | |
| 239 | unsigned long val; |
| 240 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 241 | if (kstrtoul(buf, 0, &val)) |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | |
| 244 | if (val > nd->sensor_physical_width) |
| 245 | return -EINVAL; |
| 246 | |
| 247 | nd->min_width = val * nd->sensor_logical_width / |
| 248 | nd->sensor_physical_width; |
| 249 | |
| 250 | return count; |
| 251 | } |
| 252 | |
| 253 | static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width); |
| 254 | |
| 255 | static ssize_t show_min_height(struct device *dev, |
| 256 | struct device_attribute *attr, |
| 257 | char *buf) |
| 258 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 259 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 260 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 261 | |
| 262 | return sprintf(buf, "%d\n", nd->min_height * |
| 263 | nd->sensor_physical_height / |
| 264 | nd->sensor_logical_height); |
| 265 | } |
| 266 | |
| 267 | static ssize_t set_min_height(struct device *dev, |
| 268 | struct device_attribute *attr, |
| 269 | const char *buf, size_t count) |
| 270 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 271 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 272 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 273 | |
| 274 | unsigned long val; |
| 275 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 276 | if (kstrtoul(buf, 0, &val)) |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | |
| 279 | if (val > nd->sensor_physical_height) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | nd->min_height = val * nd->sensor_logical_height / |
| 283 | nd->sensor_physical_height; |
| 284 | |
| 285 | return count; |
| 286 | } |
| 287 | |
| 288 | static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height, |
| 289 | set_min_height); |
| 290 | |
| 291 | static ssize_t show_activate_slack(struct device *dev, |
| 292 | struct device_attribute *attr, |
| 293 | char *buf) |
| 294 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 295 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 296 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 297 | |
| 298 | return sprintf(buf, "%d\n", nd->activate_slack); |
| 299 | } |
| 300 | |
| 301 | static ssize_t set_activate_slack(struct device *dev, |
| 302 | struct device_attribute *attr, |
| 303 | const char *buf, size_t count) |
| 304 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 305 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 306 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 307 | |
| 308 | unsigned long val; |
| 309 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 310 | if (kstrtoul(buf, 0, &val)) |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | |
| 313 | if (val > 0x7f) |
| 314 | return -EINVAL; |
| 315 | |
| 316 | nd->activate_slack = val; |
| 317 | |
| 318 | return count; |
| 319 | } |
| 320 | |
| 321 | static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack, |
| 322 | set_activate_slack); |
| 323 | |
| 324 | static ssize_t show_activation_width(struct device *dev, |
| 325 | struct device_attribute *attr, |
| 326 | char *buf) |
| 327 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 328 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 329 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 330 | |
| 331 | return sprintf(buf, "%d\n", nd->activation_width * |
| 332 | nd->sensor_physical_width / |
| 333 | nd->sensor_logical_width); |
| 334 | } |
| 335 | |
| 336 | static ssize_t set_activation_width(struct device *dev, |
| 337 | struct device_attribute *attr, |
| 338 | const char *buf, size_t count) |
| 339 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 340 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 341 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 342 | |
| 343 | unsigned long val; |
| 344 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 345 | if (kstrtoul(buf, 0, &val)) |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 346 | return -EINVAL; |
| 347 | |
| 348 | if (val > nd->sensor_physical_width) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | nd->activation_width = val * nd->sensor_logical_width / |
| 352 | nd->sensor_physical_width; |
| 353 | |
| 354 | return count; |
| 355 | } |
| 356 | |
| 357 | static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width, |
| 358 | set_activation_width); |
| 359 | |
| 360 | static ssize_t show_activation_height(struct device *dev, |
| 361 | struct device_attribute *attr, |
| 362 | char *buf) |
| 363 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 364 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 365 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 366 | |
| 367 | return sprintf(buf, "%d\n", nd->activation_height * |
| 368 | nd->sensor_physical_height / |
| 369 | nd->sensor_logical_height); |
| 370 | } |
| 371 | |
| 372 | static ssize_t set_activation_height(struct device *dev, |
| 373 | struct device_attribute *attr, |
| 374 | const char *buf, size_t count) |
| 375 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 376 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 377 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 378 | |
| 379 | unsigned long val; |
| 380 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 381 | if (kstrtoul(buf, 0, &val)) |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 382 | return -EINVAL; |
| 383 | |
| 384 | if (val > nd->sensor_physical_height) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | nd->activation_height = val * nd->sensor_logical_height / |
| 388 | nd->sensor_physical_height; |
| 389 | |
| 390 | return count; |
| 391 | } |
| 392 | |
| 393 | static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO, |
| 394 | show_activation_height, set_activation_height); |
| 395 | |
| 396 | static ssize_t show_deactivate_slack(struct device *dev, |
| 397 | struct device_attribute *attr, |
| 398 | char *buf) |
| 399 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 400 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 401 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 402 | |
| 403 | return sprintf(buf, "%d\n", -nd->deactivate_slack); |
| 404 | } |
| 405 | |
| 406 | static ssize_t set_deactivate_slack(struct device *dev, |
| 407 | struct device_attribute *attr, |
| 408 | const char *buf, size_t count) |
| 409 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 410 | struct hid_device *hdev = to_hid_device(dev); |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 411 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 412 | |
| 413 | unsigned long val; |
| 414 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 415 | if (kstrtoul(buf, 0, &val)) |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 416 | return -EINVAL; |
| 417 | |
| 418 | /* |
| 419 | * No more than 8 terminal frames have been observed so far |
| 420 | * and higher slack is highly likely to leave the single |
| 421 | * touch emulation stuck down. |
| 422 | */ |
| 423 | if (val > 7) |
| 424 | return -EINVAL; |
| 425 | |
| 426 | nd->deactivate_slack = -val; |
| 427 | |
| 428 | return count; |
| 429 | } |
| 430 | |
| 431 | static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack, |
| 432 | set_deactivate_slack); |
| 433 | |
| 434 | static struct attribute *sysfs_attrs[] = { |
| 435 | &dev_attr_sensor_physical_width.attr, |
| 436 | &dev_attr_sensor_physical_height.attr, |
| 437 | &dev_attr_sensor_logical_width.attr, |
| 438 | &dev_attr_sensor_logical_height.attr, |
| 439 | &dev_attr_min_height.attr, |
| 440 | &dev_attr_min_width.attr, |
| 441 | &dev_attr_activate_slack.attr, |
| 442 | &dev_attr_activation_width.attr, |
| 443 | &dev_attr_activation_height.attr, |
| 444 | &dev_attr_deactivate_slack.attr, |
| 445 | NULL |
| 446 | }; |
| 447 | |
| 448 | static struct attribute_group ntrig_attribute_group = { |
| 449 | .attrs = sysfs_attrs |
| 450 | }; |
| 451 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 452 | /* |
| 453 | * this driver is aimed at two firmware versions in circulation: |
| 454 | * - dual pen/finger single touch |
| 455 | * - finger multitouch, pen not working |
| 456 | */ |
| 457 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 458 | static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 459 | struct hid_field *field, struct hid_usage *usage, |
| 460 | unsigned long **bit, int *max) |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 461 | { |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 462 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
| 463 | |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 464 | /* No special mappings needed for the pen and single touch */ |
| 465 | if (field->physical) |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 466 | return 0; |
| 467 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 468 | switch (usage->hid & HID_USAGE_PAGE) { |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 469 | case HID_UP_GENDESK: |
| 470 | switch (usage->hid) { |
| 471 | case HID_GD_X: |
| 472 | hid_map_usage(hi, usage, bit, max, |
| 473 | EV_ABS, ABS_MT_POSITION_X); |
| 474 | input_set_abs_params(hi->input, ABS_X, |
| 475 | field->logical_minimum, |
| 476 | field->logical_maximum, 0, 0); |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 477 | |
| 478 | if (!nd->sensor_logical_width) { |
| 479 | nd->sensor_logical_width = |
| 480 | field->logical_maximum - |
| 481 | field->logical_minimum; |
| 482 | nd->sensor_physical_width = |
| 483 | field->physical_maximum - |
| 484 | field->physical_minimum; |
| 485 | nd->activation_width = activation_width * |
| 486 | nd->sensor_logical_width / |
| 487 | nd->sensor_physical_width; |
| 488 | nd->min_width = min_width * |
| 489 | nd->sensor_logical_width / |
| 490 | nd->sensor_physical_width; |
| 491 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 492 | return 1; |
| 493 | case HID_GD_Y: |
| 494 | hid_map_usage(hi, usage, bit, max, |
| 495 | EV_ABS, ABS_MT_POSITION_Y); |
| 496 | input_set_abs_params(hi->input, ABS_Y, |
| 497 | field->logical_minimum, |
| 498 | field->logical_maximum, 0, 0); |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 499 | |
| 500 | if (!nd->sensor_logical_height) { |
| 501 | nd->sensor_logical_height = |
| 502 | field->logical_maximum - |
| 503 | field->logical_minimum; |
| 504 | nd->sensor_physical_height = |
| 505 | field->physical_maximum - |
| 506 | field->physical_minimum; |
| 507 | nd->activation_height = activation_height * |
| 508 | nd->sensor_logical_height / |
| 509 | nd->sensor_physical_height; |
| 510 | nd->min_height = min_height * |
| 511 | nd->sensor_logical_height / |
| 512 | nd->sensor_physical_height; |
| 513 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 514 | return 1; |
| 515 | } |
| 516 | return 0; |
| 517 | |
| 518 | case HID_UP_DIGITIZER: |
| 519 | switch (usage->hid) { |
| 520 | /* we do not want to map these for now */ |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 521 | case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */ |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 522 | case HID_DG_INPUTMODE: |
| 523 | case HID_DG_DEVICEINDEX: |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 524 | case HID_DG_CONTACTMAX: |
| 525 | return -1; |
| 526 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 527 | /* width/height mapped on TouchMajor/TouchMinor/Orientation */ |
| 528 | case HID_DG_WIDTH: |
| 529 | hid_map_usage(hi, usage, bit, max, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 530 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 531 | return 1; |
| 532 | case HID_DG_HEIGHT: |
| 533 | hid_map_usage(hi, usage, bit, max, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 534 | EV_ABS, ABS_MT_TOUCH_MINOR); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 535 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 536 | 0, 1, 0, 0); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 537 | return 1; |
| 538 | } |
| 539 | return 0; |
| 540 | |
| 541 | case 0xff000000: |
| 542 | /* we do not want to map these: no input-oriented meaning */ |
| 543 | return -1; |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 544 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 545 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 550 | struct hid_field *field, struct hid_usage *usage, |
| 551 | unsigned long **bit, int *max) |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 552 | { |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 553 | /* No special mappings needed for the pen and single touch */ |
| 554 | if (field->physical) |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 555 | return 0; |
Rafi Rubin | b0549cf | 2010-02-11 22:14:06 -0500 | [diff] [blame] | 556 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 557 | if (usage->type == EV_KEY || usage->type == EV_REL |
| 558 | || usage->type == EV_ABS) |
| 559 | clear_bit(usage->code, *bit); |
| 560 | |
| 561 | return 0; |
| 562 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * this function is called upon all reports |
| 566 | * so that we can filter contact point information, |
| 567 | * decide whether we are in multi or single touch mode |
| 568 | * and call input_mt_sync after each point if necessary |
| 569 | */ |
| 570 | static int ntrig_event (struct hid_device *hid, struct hid_field *field, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 571 | struct hid_usage *usage, __s32 value) |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 572 | { |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 573 | struct ntrig_data *nd = hid_get_drvdata(hid); |
Rafi Rubin | f41a52d | 2011-03-08 00:24:29 -0500 | [diff] [blame] | 574 | struct input_dev *input; |
| 575 | |
| 576 | /* Skip processing if not a claimed input */ |
| 577 | if (!(hid->claimed & HID_CLAIMED_INPUT)) |
| 578 | goto not_claimed_input; |
| 579 | |
| 580 | /* This function is being called before the structures are fully |
| 581 | * initialized */ |
| 582 | if(!(field->hidinput && field->hidinput->input)) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | input = field->hidinput->input; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 586 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 587 | /* No special handling needed for the pen */ |
| 588 | if (field->application == HID_DG_PEN) |
| 589 | return 0; |
| 590 | |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 591 | switch (usage->hid) { |
| 592 | case 0xff000001: |
| 593 | /* Tag indicating the start of a multitouch group */ |
| 594 | nd->reading_mt = 1; |
| 595 | nd->first_contact_touch = 0; |
| 596 | break; |
| 597 | case HID_DG_TIPSWITCH: |
| 598 | nd->tipswitch = value; |
| 599 | /* Prevent emission of touch until validated */ |
| 600 | return 1; |
| 601 | case HID_DG_CONFIDENCE: |
| 602 | nd->confidence = value; |
| 603 | break; |
| 604 | case HID_GD_X: |
| 605 | nd->x = value; |
| 606 | /* Clear the contact footer */ |
| 607 | nd->mt_foot_count = 0; |
| 608 | break; |
| 609 | case HID_GD_Y: |
| 610 | nd->y = value; |
| 611 | break; |
| 612 | case HID_DG_CONTACTID: |
| 613 | nd->id = value; |
| 614 | break; |
| 615 | case HID_DG_WIDTH: |
| 616 | nd->w = value; |
| 617 | break; |
| 618 | case HID_DG_HEIGHT: |
| 619 | nd->h = value; |
| 620 | /* |
| 621 | * when in single touch mode, this is the last |
| 622 | * report received in a finger event. We want |
| 623 | * to emit a normal (X, Y) position |
| 624 | */ |
| 625 | if (!nd->reading_mt) { |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 626 | /* |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 627 | * TipSwitch indicates the presence of a |
| 628 | * finger in single touch mode. |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 629 | */ |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 630 | input_report_key(input, BTN_TOUCH, |
| 631 | nd->tipswitch); |
| 632 | input_report_key(input, BTN_TOOL_DOUBLETAP, |
| 633 | nd->tipswitch); |
| 634 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 635 | input_event(input, EV_ABS, ABS_Y, nd->y); |
| 636 | } |
| 637 | break; |
| 638 | case 0xff000002: |
| 639 | /* |
| 640 | * we receive this when the device is in multitouch |
| 641 | * mode. The first of the three values tagged with |
| 642 | * this usage tells if the contact point is real |
| 643 | * or a placeholder |
| 644 | */ |
| 645 | |
| 646 | /* Shouldn't get more than 4 footer packets, so skip */ |
| 647 | if (nd->mt_foot_count >= 4) |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 648 | break; |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 649 | |
| 650 | nd->mt_footer[nd->mt_foot_count++] = value; |
| 651 | |
| 652 | /* if the footer isn't complete break */ |
| 653 | if (nd->mt_foot_count != 4) |
| 654 | break; |
| 655 | |
| 656 | /* Pen activity signal. */ |
| 657 | if (nd->mt_footer[2]) { |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 658 | /* |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 659 | * When the pen deactivates touch, we see a |
| 660 | * bogus frame with ContactCount > 0. |
| 661 | * We can |
| 662 | * save a bit of work by ensuring act_state < 0 |
| 663 | * even if deactivation slack is turned off. |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 664 | */ |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 665 | nd->act_state = deactivate_slack - 1; |
| 666 | nd->confidence = 0; |
| 667 | break; |
| 668 | } |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 669 | |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 670 | /* |
| 671 | * The first footer value indicates the presence of a |
| 672 | * finger. |
| 673 | */ |
| 674 | if (nd->mt_footer[0]) { |
| 675 | /* |
| 676 | * We do not want to process contacts under |
| 677 | * the size threshold, but do not want to |
| 678 | * ignore them for activation state |
| 679 | */ |
| 680 | if (nd->w < nd->min_width || |
| 681 | nd->h < nd->min_height) |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 682 | nd->confidence = 0; |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 683 | } else |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 684 | break; |
| 685 | |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 686 | if (nd->act_state > 0) { |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 687 | /* |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 688 | * Contact meets the activation size threshold |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 689 | */ |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 690 | if (nd->w >= nd->activation_width && |
| 691 | nd->h >= nd->activation_height) { |
| 692 | if (nd->id) |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 693 | /* |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 694 | * first contact, activate now |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 695 | */ |
| 696 | nd->act_state = 0; |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 697 | else { |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 698 | /* |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 699 | * avoid corrupting this frame |
| 700 | * but ensure next frame will |
| 701 | * be active |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 702 | */ |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 703 | nd->act_state = 1; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 704 | break; |
| 705 | } |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 706 | } else |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 707 | /* |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 708 | * Defer adjusting the activation state |
| 709 | * until the end of the frame. |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 710 | */ |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | |
| 714 | /* Discarding this contact */ |
| 715 | if (!nd->confidence) |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 716 | break; |
| 717 | |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 718 | /* emit a normal (X, Y) for the first point only */ |
| 719 | if (nd->id == 0) { |
| 720 | /* |
| 721 | * TipSwitch is superfluous in multitouch |
| 722 | * mode. The footer events tell us |
| 723 | * if there is a finger on the screen or |
| 724 | * not. |
| 725 | */ |
| 726 | nd->first_contact_touch = nd->confidence; |
| 727 | input_event(input, EV_ABS, ABS_X, nd->x); |
| 728 | input_event(input, EV_ABS, ABS_Y, nd->y); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 729 | } |
Rafi Rubin | ff40462 | 2011-03-07 21:13:28 -0500 | [diff] [blame] | 730 | |
| 731 | /* Emit MT events */ |
| 732 | input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x); |
| 733 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y); |
| 734 | |
| 735 | /* |
| 736 | * Translate from height and width to size |
| 737 | * and orientation. |
| 738 | */ |
| 739 | if (nd->w > nd->h) { |
| 740 | input_event(input, EV_ABS, |
| 741 | ABS_MT_ORIENTATION, 1); |
| 742 | input_event(input, EV_ABS, |
| 743 | ABS_MT_TOUCH_MAJOR, nd->w); |
| 744 | input_event(input, EV_ABS, |
| 745 | ABS_MT_TOUCH_MINOR, nd->h); |
| 746 | } else { |
| 747 | input_event(input, EV_ABS, |
| 748 | ABS_MT_ORIENTATION, 0); |
| 749 | input_event(input, EV_ABS, |
| 750 | ABS_MT_TOUCH_MAJOR, nd->h); |
| 751 | input_event(input, EV_ABS, |
| 752 | ABS_MT_TOUCH_MINOR, nd->w); |
| 753 | } |
| 754 | input_mt_sync(field->hidinput->input); |
| 755 | break; |
| 756 | |
| 757 | case HID_DG_CONTACTCOUNT: /* End of a multitouch group */ |
| 758 | if (!nd->reading_mt) /* Just to be sure */ |
| 759 | break; |
| 760 | |
| 761 | nd->reading_mt = 0; |
| 762 | |
| 763 | |
| 764 | /* |
| 765 | * Activation state machine logic: |
| 766 | * |
| 767 | * Fundamental states: |
| 768 | * state > 0: Inactive |
| 769 | * state <= 0: Active |
| 770 | * state < -deactivate_slack: |
| 771 | * Pen termination of touch |
| 772 | * |
| 773 | * Specific values of interest |
| 774 | * state == activate_slack |
| 775 | * no valid input since the last reset |
| 776 | * |
| 777 | * state == 0 |
| 778 | * general operational state |
| 779 | * |
| 780 | * state == -deactivate_slack |
| 781 | * read sufficient empty frames to accept |
| 782 | * the end of input and reset |
| 783 | */ |
| 784 | |
| 785 | if (nd->act_state > 0) { /* Currently inactive */ |
| 786 | if (value) |
| 787 | /* |
| 788 | * Consider each live contact as |
| 789 | * evidence of intentional activity. |
| 790 | */ |
| 791 | nd->act_state = (nd->act_state > value) |
| 792 | ? nd->act_state - value |
| 793 | : 0; |
| 794 | else |
| 795 | /* |
| 796 | * Empty frame before we hit the |
| 797 | * activity threshold, reset. |
| 798 | */ |
| 799 | nd->act_state = nd->activate_slack; |
| 800 | |
| 801 | /* |
| 802 | * Entered this block inactive and no |
| 803 | * coordinates sent this frame, so hold off |
| 804 | * on button state. |
| 805 | */ |
| 806 | break; |
| 807 | } else { /* Currently active */ |
| 808 | if (value && nd->act_state >= |
| 809 | nd->deactivate_slack) |
| 810 | /* |
| 811 | * Live point: clear accumulated |
| 812 | * deactivation count. |
| 813 | */ |
| 814 | nd->act_state = 0; |
| 815 | else if (nd->act_state <= nd->deactivate_slack) |
| 816 | /* |
| 817 | * We've consumed the deactivation |
| 818 | * slack, time to deactivate and reset. |
| 819 | */ |
| 820 | nd->act_state = |
| 821 | nd->activate_slack; |
| 822 | else { /* Move towards deactivation */ |
| 823 | nd->act_state--; |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (nd->first_contact_touch && nd->act_state <= 0) { |
| 829 | /* |
| 830 | * Check to see if we're ready to start |
| 831 | * emitting touch events. |
| 832 | * |
| 833 | * Note: activation slack will decrease over |
| 834 | * the course of the frame, and it will be |
| 835 | * inconsistent from the start to the end of |
| 836 | * the frame. However if the frame starts |
| 837 | * with slack, first_contact_touch will still |
| 838 | * be 0 and we will not get to this point. |
| 839 | */ |
| 840 | input_report_key(input, BTN_TOOL_DOUBLETAP, 1); |
| 841 | input_report_key(input, BTN_TOUCH, 1); |
| 842 | } else { |
| 843 | input_report_key(input, BTN_TOOL_DOUBLETAP, 0); |
| 844 | input_report_key(input, BTN_TOUCH, 0); |
| 845 | } |
| 846 | break; |
| 847 | |
| 848 | default: |
| 849 | /* fall-back to the generic hidinput handling */ |
| 850 | return 0; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Rafi Rubin | f41a52d | 2011-03-08 00:24:29 -0500 | [diff] [blame] | 853 | not_claimed_input: |
| 854 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 855 | /* we have handled the hidinput part, now remains hiddev */ |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 856 | if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event) |
| 857 | hid->hiddev_hid_event(hid, field, usage, value); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 858 | |
| 859 | return 1; |
| 860 | } |
| 861 | |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 862 | static int ntrig_input_configured(struct hid_device *hid, |
Benjamin Tissoires | 1b474fe | 2013-02-08 15:51:30 +0100 | [diff] [blame] | 863 | struct hid_input *hidinput) |
| 864 | |
| 865 | { |
| 866 | struct input_dev *input = hidinput->input; |
| 867 | |
| 868 | if (hidinput->report->maxfield < 1) |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 869 | return 0; |
Benjamin Tissoires | 1b474fe | 2013-02-08 15:51:30 +0100 | [diff] [blame] | 870 | |
| 871 | switch (hidinput->report->field[0]->application) { |
| 872 | case HID_DG_PEN: |
| 873 | input->name = "N-Trig Pen"; |
| 874 | break; |
| 875 | case HID_DG_TOUCHSCREEN: |
| 876 | /* These keys are redundant for fingers, clear them |
| 877 | * to prevent incorrect identification */ |
| 878 | __clear_bit(BTN_TOOL_PEN, input->keybit); |
| 879 | __clear_bit(BTN_TOOL_FINGER, input->keybit); |
| 880 | __clear_bit(BTN_0, input->keybit); |
| 881 | __set_bit(BTN_TOOL_DOUBLETAP, input->keybit); |
| 882 | /* |
| 883 | * The physical touchscreen (single touch) |
| 884 | * input has a value for physical, whereas |
| 885 | * the multitouch only has logical input |
| 886 | * fields. |
| 887 | */ |
| 888 | input->name = (hidinput->report->field[0]->physical) ? |
| 889 | "N-Trig Touchscreen" : |
| 890 | "N-Trig MultiTouch"; |
| 891 | break; |
| 892 | } |
Dmitry Torokhov | 9154301 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 893 | |
| 894 | return 0; |
Benjamin Tissoires | 1b474fe | 2013-02-08 15:51:30 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 897 | static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 898 | { |
| 899 | int ret; |
| 900 | struct ntrig_data *nd; |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 901 | struct hid_report *report; |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 902 | |
| 903 | if (id->driver_data) |
Rafi Rubin | 6638ded | 2011-03-09 23:33:51 -0500 | [diff] [blame] | 904 | hdev->quirks |= HID_QUIRK_MULTI_INPUT |
| 905 | | HID_QUIRK_NO_INIT_REPORTS; |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 906 | |
| 907 | nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL); |
| 908 | if (!nd) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 909 | hid_err(hdev, "cannot allocate N-Trig data\n"); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 910 | return -ENOMEM; |
| 911 | } |
Rafi Rubin | dbf2b17 | 2010-02-12 21:13:05 -0500 | [diff] [blame] | 912 | |
| 913 | nd->reading_mt = 0; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 914 | nd->min_width = 0; |
| 915 | nd->min_height = 0; |
| 916 | nd->activate_slack = activate_slack; |
| 917 | nd->act_state = activate_slack; |
| 918 | nd->deactivate_slack = -deactivate_slack; |
Wen-chien Jesse Sung | 4cc9834 | 2012-09-25 18:51:07 +0800 | [diff] [blame] | 919 | nd->sensor_logical_width = 1; |
| 920 | nd->sensor_logical_height = 1; |
| 921 | nd->sensor_physical_width = 1; |
| 922 | nd->sensor_physical_height = 1; |
Rafi Rubin | 369db2a | 2010-05-04 14:20:15 -0400 | [diff] [blame] | 923 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 924 | hid_set_drvdata(hdev, nd); |
| 925 | |
| 926 | ret = hid_parse(hdev); |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 927 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 928 | hid_err(hdev, "parse failed\n"); |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 929 | goto err_free; |
| 930 | } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 931 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 932 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); |
| 933 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 934 | hid_err(hdev, "hw start failed\n"); |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 935 | goto err_free; |
| 936 | } |
Rafi Rubin | 837b475 | 2009-06-23 14:09:26 -0400 | [diff] [blame] | 937 | |
Jiri Kosina | c085855 | 2010-04-07 12:10:29 +0200 | [diff] [blame] | 938 | /* This is needed for devices with more recent firmware versions */ |
| 939 | report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a]; |
Rafi Rubin | 7b2a64c | 2011-03-09 23:33:52 -0500 | [diff] [blame] | 940 | if (report) { |
| 941 | /* Let the device settle to ensure the wakeup message gets |
| 942 | * through */ |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 943 | hid_hw_wait(hdev); |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 944 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
Rafi Rubin | 7b2a64c | 2011-03-09 23:33:52 -0500 | [diff] [blame] | 945 | |
| 946 | /* |
| 947 | * Sanity check: if the current mode is invalid reset it to |
| 948 | * something reasonable. |
| 949 | */ |
| 950 | if (ntrig_get_mode(hdev) >= 4) |
| 951 | ntrig_set_mode(hdev, 3); |
| 952 | } |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 953 | |
Rafi Rubin | 0277873 | 2010-09-08 11:46:14 +0200 | [diff] [blame] | 954 | ntrig_report_version(hdev); |
| 955 | |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 956 | ret = sysfs_create_group(&hdev->dev.kobj, |
| 957 | &ntrig_attribute_group); |
Stephane Chatty | 6549981 | 2010-04-06 22:22:58 +0200 | [diff] [blame] | 958 | |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 959 | return 0; |
| 960 | err_free: |
| 961 | kfree(nd); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static void ntrig_remove(struct hid_device *hdev) |
| 966 | { |
Rafi Rubin | eab32f5 | 2010-05-04 14:20:17 -0400 | [diff] [blame] | 967 | sysfs_remove_group(&hdev->dev.kobj, |
Rafi Rubin | a52dc34 | 2010-08-26 00:54:55 -0400 | [diff] [blame] | 968 | &ntrig_attribute_group); |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 969 | hid_hw_stop(hdev); |
| 970 | kfree(hid_get_drvdata(hdev)); |
| 971 | } |
| 972 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 973 | static const struct hid_device_id ntrig_devices[] = { |
| 974 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN), |
| 975 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
micki | 6e32819 | 2010-06-19 11:37:29 +0300 | [diff] [blame] | 976 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1), |
| 977 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 978 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2), |
| 979 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 980 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3), |
| 981 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 982 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4), |
| 983 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 984 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5), |
| 985 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 986 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6), |
| 987 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 988 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7), |
| 989 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 990 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8), |
| 991 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 992 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9), |
| 993 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 994 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10), |
| 995 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 996 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11), |
| 997 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 998 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12), |
| 999 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 1000 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13), |
| 1001 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 1002 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14), |
| 1003 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 1004 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15), |
| 1005 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 1006 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16), |
| 1007 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 1008 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17), |
| 1009 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
| 1010 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18), |
| 1011 | .driver_data = NTRIG_DUPLICATE_USAGES }, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1012 | { } |
| 1013 | }; |
| 1014 | MODULE_DEVICE_TABLE(hid, ntrig_devices); |
| 1015 | |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 1016 | static const struct hid_usage_id ntrig_grabbed_usages[] = { |
| 1017 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
Rafi Rubin | 943ed46 | 2010-02-11 22:14:05 -0500 | [diff] [blame] | 1018 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 } |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 1019 | }; |
| 1020 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1021 | static struct hid_driver ntrig_driver = { |
| 1022 | .name = "ntrig", |
| 1023 | .id_table = ntrig_devices, |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 1024 | .probe = ntrig_probe, |
| 1025 | .remove = ntrig_remove, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1026 | .input_mapping = ntrig_input_mapping, |
| 1027 | .input_mapped = ntrig_input_mapped, |
Benjamin Tissoires | 1b474fe | 2013-02-08 15:51:30 +0100 | [diff] [blame] | 1028 | .input_configured = ntrig_input_configured, |
Stephane Chatty | 57fd637 | 2009-05-20 15:49:35 +0200 | [diff] [blame] | 1029 | .usage_table = ntrig_grabbed_usages, |
| 1030 | .event = ntrig_event, |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1031 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 1032 | module_hid_driver(ntrig_driver); |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1033 | |
Rafi Rubin | 94011f9 | 2008-11-19 15:54:46 +0100 | [diff] [blame] | 1034 | MODULE_LICENSE("GPL"); |