hid-ntrig.c 27 KB

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