hid-ntrig.c 26 KB

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