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