hid-ntrig.c 27 KB

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