Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
94011f93 | 2 | /* |
57fd637a | 3 | * HID driver for N-Trig touchscreens |
94011f93 | 4 | * |
6549981b SC |
5 | * Copyright (c) 2008-2010 Rafi Rubin |
6 | * Copyright (c) 2009-2010 Stephane Chatty | |
94011f93 RR |
7 | */ |
8 | ||
9 | /* | |
94011f93 RR |
10 | */ |
11 | ||
12 | #include <linux/device.h> | |
13 | #include <linux/hid.h> | |
6549981b SC |
14 | #include <linux/usb.h> |
15 | #include "usbhid/usbhid.h" | |
94011f93 | 16 | #include <linux/module.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
94011f93 RR |
18 | |
19 | #include "hid-ids.h" | |
20 | ||
21 | #define NTRIG_DUPLICATE_USAGES 0x001 | |
22 | ||
369db2a6 | 23 | static unsigned int min_width; |
ab3f4980 RR |
24 | module_param(min_width, uint, 0644); |
25 | MODULE_PARM_DESC(min_width, "Minimum touch contact width to accept."); | |
26 | ||
369db2a6 | 27 | static unsigned int min_height; |
ab3f4980 RR |
28 | module_param(min_height, uint, 0644); |
29 | MODULE_PARM_DESC(min_height, "Minimum touch contact height to accept."); | |
30 | ||
369db2a6 | 31 | static unsigned int activate_slack = 1; |
ab3f4980 RR |
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 | ||
369db2a6 | 36 | static unsigned int deactivate_slack = 4; |
ab3f4980 RR |
37 | module_param(deactivate_slack, uint, 0644); |
38 | MODULE_PARM_DESC(deactivate_slack, "Number of empty frames to ignore before " | |
39 | "deactivating touch."); | |
40 | ||
369db2a6 | 41 | static unsigned int activation_width = 64; |
ab3f4980 RR |
42 | module_param(activation_width, uint, 0644); |
43 | MODULE_PARM_DESC(activation_width, "Width threshold to immediately start " | |
44 | "processing touch events."); | |
45 | ||
369db2a6 | 46 | static unsigned int activation_height = 32; |
ab3f4980 RR |
47 | module_param(activation_height, uint, 0644); |
48 | MODULE_PARM_DESC(activation_height, "Height threshold to immediately start " | |
49 | "processing touch events."); | |
369db2a6 | 50 | |
57fd637a | 51 | struct ntrig_data { |
dbf2b17d RR |
52 | /* Incoming raw values for a single contact */ |
53 | __u16 x, y, w, h; | |
54 | __u16 id; | |
250d3775 RR |
55 | |
56 | bool tipswitch; | |
57 | bool confidence; | |
58 | bool first_contact_touch; | |
dbf2b17d RR |
59 | |
60 | bool reading_mt; | |
dbf2b17d RR |
61 | |
62 | __u8 mt_footer[4]; | |
63 | __u8 mt_foot_count; | |
369db2a6 RR |
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; | |
57fd637a SC |
86 | }; |
87 | ||
eab32f5f | 88 | |
0277873c RR |
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 | ||
7b2a64c9 RR |
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 | ||
875b4e37 KC |
114 | if (!report || report->maxfield < 1 || |
115 | report->field[0]->report_count < 1) | |
7b2a64c9 RR |
116 | return -EINVAL; |
117 | ||
d8814272 | 118 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
b7966a4d | 119 | hid_hw_wait(hdev); |
7b2a64c9 RR |
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 | ||
d8814272 | 137 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
7b2a64c9 RR |
138 | } |
139 | ||
0277873c RR |
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 | ||
4291ee30 | 160 | hid_info(hdev, "Firmware version: %s (%02x%02x %02x%02x)\n", |
0277873c RR |
161 | buf, data[2], data[3], data[4], data[5]); |
162 | } | |
163 | ||
164 | err_free: | |
165 | kfree(data); | |
166 | } | |
167 | ||
eab32f5f RR |
168 | static ssize_t show_phys_width(struct device *dev, |
169 | struct device_attribute *attr, | |
170 | char *buf) | |
171 | { | |
ee79a8f8 | 172 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 184 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 196 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 208 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 220 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 232 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
233 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
234 | ||
235 | unsigned long val; | |
236 | ||
dfc450b5 | 237 | if (kstrtoul(buf, 0, &val)) |
eab32f5f RR |
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 | { | |
ee79a8f8 | 255 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 267 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
268 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
269 | ||
270 | unsigned long val; | |
271 | ||
dfc450b5 | 272 | if (kstrtoul(buf, 0, &val)) |
eab32f5f RR |
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 | { | |
ee79a8f8 | 291 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 301 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
302 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
303 | ||
304 | unsigned long val; | |
305 | ||
dfc450b5 | 306 | if (kstrtoul(buf, 0, &val)) |
eab32f5f RR |
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 | { | |
ee79a8f8 | 324 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 336 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
337 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
338 | ||
339 | unsigned long val; | |
340 | ||
dfc450b5 | 341 | if (kstrtoul(buf, 0, &val)) |
eab32f5f RR |
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 | { | |
ee79a8f8 | 360 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 372 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
373 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
374 | ||
375 | unsigned long val; | |
376 | ||
dfc450b5 | 377 | if (kstrtoul(buf, 0, &val)) |
eab32f5f RR |
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 | { | |
ee79a8f8 | 396 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
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 | { | |
ee79a8f8 | 406 | struct hid_device *hdev = to_hid_device(dev); |
eab32f5f RR |
407 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
408 | ||
409 | unsigned long val; | |
410 | ||
dfc450b5 | 411 | if (kstrtoul(buf, 0, &val)) |
eab32f5f RR |
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 | ||
1d710da2 | 444 | static const struct attribute_group ntrig_attribute_group = { |
eab32f5f RR |
445 | .attrs = sysfs_attrs |
446 | }; | |
447 | ||
57fd637a SC |
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 | ||
94011f93 | 454 | static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
a52dc34c RR |
455 | struct hid_field *field, struct hid_usage *usage, |
456 | unsigned long **bit, int *max) | |
94011f93 | 457 | { |
369db2a6 RR |
458 | struct ntrig_data *nd = hid_get_drvdata(hdev); |
459 | ||
dbf2b17d RR |
460 | /* No special mappings needed for the pen and single touch */ |
461 | if (field->physical) | |
943ed464 RR |
462 | return 0; |
463 | ||
57fd637a | 464 | switch (usage->hid & HID_USAGE_PAGE) { |
57fd637a SC |
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); | |
369db2a6 RR |
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 | } | |
57fd637a SC |
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); | |
369db2a6 RR |
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 | } | |
57fd637a SC |
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 */ | |
dbf2b17d | 517 | case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */ |
57fd637a SC |
518 | case HID_DG_INPUTMODE: |
519 | case HID_DG_DEVICEINDEX: | |
57fd637a SC |
520 | case HID_DG_CONTACTMAX: |
521 | return -1; | |
522 | ||
57fd637a SC |
523 | /* width/height mapped on TouchMajor/TouchMinor/Orientation */ |
524 | case HID_DG_WIDTH: | |
525 | hid_map_usage(hi, usage, bit, max, | |
a52dc34c | 526 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
57fd637a SC |
527 | return 1; |
528 | case HID_DG_HEIGHT: | |
529 | hid_map_usage(hi, usage, bit, max, | |
a52dc34c | 530 | EV_ABS, ABS_MT_TOUCH_MINOR); |
57fd637a | 531 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
a52dc34c | 532 | 0, 1, 0, 0); |
57fd637a SC |
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; | |
94011f93 | 540 | } |
57fd637a | 541 | |
94011f93 RR |
542 | return 0; |
543 | } | |
544 | ||
545 | static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi, | |
a52dc34c RR |
546 | struct hid_field *field, struct hid_usage *usage, |
547 | unsigned long **bit, int *max) | |
94011f93 | 548 | { |
dbf2b17d RR |
549 | /* No special mappings needed for the pen and single touch */ |
550 | if (field->physical) | |
943ed464 | 551 | return 0; |
b0549cf1 | 552 | |
94011f93 RR |
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 | } | |
57fd637a SC |
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, | |
a52dc34c | 567 | struct hid_usage *usage, __s32 value) |
57fd637a | 568 | { |
57fd637a | 569 | struct ntrig_data *nd = hid_get_drvdata(hid); |
f41a52d3 RR |
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; | |
57fd637a | 582 | |
943ed464 RR |
583 | /* No special handling needed for the pen */ |
584 | if (field->application == HID_DG_PEN) | |
585 | return 0; | |
586 | ||
ff404629 RR |
587 | switch (usage->hid) { |
588 | case 0xff000001: | |
589 | /* Tag indicating the start of a multitouch group */ | |
04230f46 GS |
590 | nd->reading_mt = true; |
591 | nd->first_contact_touch = false; | |
ff404629 RR |
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) { | |
57fd637a | 622 | /* |
ff404629 RR |
623 | * TipSwitch indicates the presence of a |
624 | * finger in single touch mode. | |
57fd637a | 625 | */ |
ff404629 RR |
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) | |
57fd637a | 644 | break; |
dbf2b17d | 645 | |
ff404629 | 646 | nd->mt_footer[nd->mt_foot_count++] = value; |
dbf2b17d | 647 | |
ff404629 RR |
648 | /* if the footer isn't complete break */ |
649 | if (nd->mt_foot_count != 4) | |
650 | break; | |
dbf2b17d | 651 | |
ff404629 RR |
652 | /* Pen activity signal. */ |
653 | if (nd->mt_footer[2]) { | |
654 | /* | |
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. | |
660 | */ | |
661 | nd->act_state = deactivate_slack - 1; | |
04230f46 | 662 | nd->confidence = false; |
ff404629 RR |
663 | break; |
664 | } | |
dbf2b17d | 665 | |
ff404629 RR |
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) | |
04230f46 | 678 | nd->confidence = false; |
ff404629 RR |
679 | } else |
680 | break; | |
dbf2b17d | 681 | |
ff404629 | 682 | if (nd->act_state > 0) { |
369db2a6 | 683 | /* |
ff404629 | 684 | * Contact meets the activation size threshold |
369db2a6 | 685 | */ |
ff404629 RR |
686 | if (nd->w >= nd->activation_width && |
687 | nd->h >= nd->activation_height) { | |
688 | if (nd->id) | |
369db2a6 | 689 | /* |
ff404629 | 690 | * first contact, activate now |
369db2a6 | 691 | */ |
ff404629 RR |
692 | nd->act_state = 0; |
693 | else { | |
694 | /* | |
695 | * avoid corrupting this frame | |
696 | * but ensure next frame will | |
697 | * be active | |
698 | */ | |
699 | nd->act_state = 1; | |
369db2a6 | 700 | break; |
ff404629 RR |
701 | } |
702 | } else | |
250d3775 | 703 | /* |
ff404629 RR |
704 | * Defer adjusting the activation state |
705 | * until the end of the frame. | |
250d3775 | 706 | */ |
ff404629 RR |
707 | break; |
708 | } | |
369db2a6 | 709 | |
ff404629 RR |
710 | /* Discarding this contact */ |
711 | if (!nd->confidence) | |
712 | break; | |
369db2a6 | 713 | |
ff404629 RR |
714 | /* emit a normal (X, Y) for the first point only */ |
715 | if (nd->id == 0) { | |
369db2a6 | 716 | /* |
ff404629 RR |
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. | |
369db2a6 | 721 | */ |
ff404629 RR |
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); | |
725 | } | |
dbf2b17d | 726 | |
ff404629 RR |
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; | |
dbf2b17d | 752 | |
ff404629 RR |
753 | case HID_DG_CONTACTCOUNT: /* End of a multitouch group */ |
754 | if (!nd->reading_mt) /* Just to be sure */ | |
755 | break; | |
dbf2b17d | 756 | |
04230f46 | 757 | nd->reading_mt = false; |
ff404629 RR |
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; | |
369db2a6 RR |
796 | |
797 | /* | |
ff404629 RR |
798 | * Entered this block inactive and no |
799 | * coordinates sent this frame, so hold off | |
800 | * on button state. | |
369db2a6 | 801 | */ |
ff404629 RR |
802 | break; |
803 | } else { /* Currently active */ | |
804 | if (value && nd->act_state >= | |
805 | nd->deactivate_slack) | |
369db2a6 | 806 | /* |
ff404629 RR |
807 | * Live point: clear accumulated |
808 | * deactivation count. | |
369db2a6 | 809 | */ |
ff404629 RR |
810 | nd->act_state = 0; |
811 | else if (nd->act_state <= nd->deactivate_slack) | |
369db2a6 | 812 | /* |
ff404629 RR |
813 | * We've consumed the deactivation |
814 | * slack, time to deactivate and reset. | |
369db2a6 | 815 | */ |
ff404629 RR |
816 | nd->act_state = |
817 | nd->activate_slack; | |
818 | else { /* Move towards deactivation */ | |
819 | nd->act_state--; | |
820 | break; | |
dbf2b17d | 821 | } |
ff404629 | 822 | } |
57fd637a | 823 | |
ff404629 RR |
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); | |
57fd637a | 841 | } |
ff404629 RR |
842 | break; |
843 | ||
844 | default: | |
845 | /* fall-back to the generic hidinput handling */ | |
846 | return 0; | |
57fd637a SC |
847 | } |
848 | ||
f41a52d3 RR |
849 | not_claimed_input: |
850 | ||
57fd637a | 851 | /* we have handled the hidinput part, now remains hiddev */ |
943ed464 RR |
852 | if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event) |
853 | hid->hiddev_hid_event(hid, field, usage, value); | |
57fd637a SC |
854 | |
855 | return 1; | |
856 | } | |
857 | ||
9154301a | 858 | static int ntrig_input_configured(struct hid_device *hid, |
1b474fe8 BT |
859 | struct hid_input *hidinput) |
860 | ||
861 | { | |
862 | struct input_dev *input = hidinput->input; | |
863 | ||
864 | if (hidinput->report->maxfield < 1) | |
9154301a | 865 | return 0; |
1b474fe8 BT |
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 | } | |
9154301a DT |
889 | |
890 | return 0; | |
1b474fe8 BT |
891 | } |
892 | ||
57fd637a SC |
893 | static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id) |
894 | { | |
895 | int ret; | |
896 | struct ntrig_data *nd; | |
6549981b | 897 | struct hid_report *report; |
943ed464 RR |
898 | |
899 | if (id->driver_data) | |
6638deda RR |
900 | hdev->quirks |= HID_QUIRK_MULTI_INPUT |
901 | | HID_QUIRK_NO_INIT_REPORTS; | |
57fd637a SC |
902 | |
903 | nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL); | |
904 | if (!nd) { | |
4291ee30 | 905 | hid_err(hdev, "cannot allocate N-Trig data\n"); |
57fd637a SC |
906 | return -ENOMEM; |
907 | } | |
dbf2b17d | 908 | |
04230f46 | 909 | nd->reading_mt = false; |
369db2a6 RR |
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; | |
4cc98345 WJS |
915 | nd->sensor_logical_width = 1; |
916 | nd->sensor_logical_height = 1; | |
917 | nd->sensor_physical_width = 1; | |
918 | nd->sensor_physical_height = 1; | |
369db2a6 | 919 | |
57fd637a SC |
920 | hid_set_drvdata(hdev, nd); |
921 | ||
922 | ret = hid_parse(hdev); | |
943ed464 | 923 | if (ret) { |
4291ee30 | 924 | hid_err(hdev, "parse failed\n"); |
943ed464 RR |
925 | goto err_free; |
926 | } | |
927 | ||
928 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); | |
929 | if (ret) { | |
4291ee30 | 930 | hid_err(hdev, "hw start failed\n"); |
943ed464 RR |
931 | goto err_free; |
932 | } | |
57fd637a | 933 | |
c0858552 JK |
934 | /* This is needed for devices with more recent firmware versions */ |
935 | report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a]; | |
7b2a64c9 RR |
936 | if (report) { |
937 | /* Let the device settle to ensure the wakeup message gets | |
938 | * through */ | |
b7966a4d | 939 | hid_hw_wait(hdev); |
d8814272 | 940 | hid_hw_request(hdev, report, HID_REQ_GET_REPORT); |
7b2a64c9 RR |
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 | } | |
6549981b | 949 | |
0277873c RR |
950 | ntrig_report_version(hdev); |
951 | ||
eab32f5f RR |
952 | ret = sysfs_create_group(&hdev->dev.kobj, |
953 | &ntrig_attribute_group); | |
44d4d51d ZJ |
954 | if (ret) |
955 | hid_err(hdev, "cannot create sysfs group\n"); | |
6549981b | 956 | |
943ed464 RR |
957 | return 0; |
958 | err_free: | |
959 | kfree(nd); | |
57fd637a SC |
960 | return ret; |
961 | } | |
962 | ||
963 | static void ntrig_remove(struct hid_device *hdev) | |
964 | { | |
eab32f5f | 965 | sysfs_remove_group(&hdev->dev.kobj, |
a52dc34c | 966 | &ntrig_attribute_group); |
57fd637a SC |
967 | hid_hw_stop(hdev); |
968 | kfree(hid_get_drvdata(hdev)); | |
969 | } | |
970 | ||
94011f93 RR |
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 }, | |
6e32819e | 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 }, | |
94011f93 RR |
1010 | { } |
1011 | }; | |
1012 | MODULE_DEVICE_TABLE(hid, ntrig_devices); | |
1013 | ||
57fd637a SC |
1014 | static const struct hid_usage_id ntrig_grabbed_usages[] = { |
1015 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, | |
943ed464 | 1016 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 } |
57fd637a SC |
1017 | }; |
1018 | ||
94011f93 RR |
1019 | static struct hid_driver ntrig_driver = { |
1020 | .name = "ntrig", | |
1021 | .id_table = ntrig_devices, | |
57fd637a SC |
1022 | .probe = ntrig_probe, |
1023 | .remove = ntrig_remove, | |
94011f93 RR |
1024 | .input_mapping = ntrig_input_mapping, |
1025 | .input_mapped = ntrig_input_mapped, | |
1b474fe8 | 1026 | .input_configured = ntrig_input_configured, |
57fd637a SC |
1027 | .usage_table = ntrig_grabbed_usages, |
1028 | .event = ntrig_event, | |
94011f93 | 1029 | }; |
f425458e | 1030 | module_hid_driver(ntrig_driver); |
94011f93 | 1031 | |
94011f93 | 1032 | MODULE_LICENSE("GPL"); |