hid-ntrig.c 25 KB

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