hid-ntrig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. static unsigned int min_height;
  24. static unsigned int activate_slack = 1;
  25. static unsigned int deactivate_slack = 4;
  26. static unsigned int activation_width = 64;
  27. static unsigned int activation_height = 32;
  28. struct ntrig_data {
  29. /* Incoming raw values for a single contact */
  30. __u16 x, y, w, h;
  31. __u16 id;
  32. bool tipswitch;
  33. bool confidence;
  34. bool first_contact_touch;
  35. bool reading_mt;
  36. __u8 mt_footer[4];
  37. __u8 mt_foot_count;
  38. /* The current activation state. */
  39. __s8 act_state;
  40. /* Empty frames to ignore before recognizing the end of activity */
  41. __s8 deactivate_slack;
  42. /* Frames to ignore before acknowledging the start of activity */
  43. __s8 activate_slack;
  44. /* Minimum size contact to accept */
  45. __u16 min_width;
  46. __u16 min_height;
  47. /* Threshold to override activation slack */
  48. __u16 activation_width;
  49. __u16 activation_height;
  50. __u16 sensor_logical_width;
  51. __u16 sensor_logical_height;
  52. __u16 sensor_physical_width;
  53. __u16 sensor_physical_height;
  54. };
  55. /*
  56. * this driver is aimed at two firmware versions in circulation:
  57. * - dual pen/finger single touch
  58. * - finger multitouch, pen not working
  59. */
  60. static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  61. struct hid_field *field, struct hid_usage *usage,
  62. unsigned long **bit, int *max)
  63. {
  64. struct ntrig_data *nd = hid_get_drvdata(hdev);
  65. /* No special mappings needed for the pen and single touch */
  66. if (field->physical)
  67. return 0;
  68. switch (usage->hid & HID_USAGE_PAGE) {
  69. case HID_UP_GENDESK:
  70. switch (usage->hid) {
  71. case HID_GD_X:
  72. hid_map_usage(hi, usage, bit, max,
  73. EV_ABS, ABS_MT_POSITION_X);
  74. input_set_abs_params(hi->input, ABS_X,
  75. field->logical_minimum,
  76. field->logical_maximum, 0, 0);
  77. if (!nd->sensor_logical_width) {
  78. nd->sensor_logical_width =
  79. field->logical_maximum -
  80. field->logical_minimum;
  81. nd->sensor_physical_width =
  82. field->physical_maximum -
  83. field->physical_minimum;
  84. nd->activation_width = activation_width *
  85. nd->sensor_logical_width /
  86. nd->sensor_physical_width;
  87. nd->min_width = min_width *
  88. nd->sensor_logical_width /
  89. nd->sensor_physical_width;
  90. }
  91. return 1;
  92. case HID_GD_Y:
  93. hid_map_usage(hi, usage, bit, max,
  94. EV_ABS, ABS_MT_POSITION_Y);
  95. input_set_abs_params(hi->input, ABS_Y,
  96. field->logical_minimum,
  97. field->logical_maximum, 0, 0);
  98. if (!nd->sensor_logical_height) {
  99. nd->sensor_logical_height =
  100. field->logical_maximum -
  101. field->logical_minimum;
  102. nd->sensor_physical_height =
  103. field->physical_maximum -
  104. field->physical_minimum;
  105. nd->activation_height = activation_height *
  106. nd->sensor_logical_height /
  107. nd->sensor_physical_height;
  108. nd->min_height = min_height *
  109. nd->sensor_logical_height /
  110. nd->sensor_physical_height;
  111. }
  112. return 1;
  113. }
  114. return 0;
  115. case HID_UP_DIGITIZER:
  116. switch (usage->hid) {
  117. /* we do not want to map these for now */
  118. case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
  119. case HID_DG_INPUTMODE:
  120. case HID_DG_DEVICEINDEX:
  121. case HID_DG_CONTACTMAX:
  122. return -1;
  123. /* width/height mapped on TouchMajor/TouchMinor/Orientation */
  124. case HID_DG_WIDTH:
  125. hid_map_usage(hi, usage, bit, max,
  126. EV_ABS, ABS_MT_TOUCH_MAJOR);
  127. return 1;
  128. case HID_DG_HEIGHT:
  129. hid_map_usage(hi, usage, bit, max,
  130. EV_ABS, ABS_MT_TOUCH_MINOR);
  131. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  132. 0, 1, 0, 0);
  133. return 1;
  134. }
  135. return 0;
  136. case 0xff000000:
  137. /* we do not want to map these: no input-oriented meaning */
  138. return -1;
  139. }
  140. return 0;
  141. }
  142. static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  143. struct hid_field *field, struct hid_usage *usage,
  144. unsigned long **bit, int *max)
  145. {
  146. /* No special mappings needed for the pen and single touch */
  147. if (field->physical)
  148. return 0;
  149. if (usage->type == EV_KEY || usage->type == EV_REL
  150. || usage->type == EV_ABS)
  151. clear_bit(usage->code, *bit);
  152. return 0;
  153. }
  154. /*
  155. * this function is called upon all reports
  156. * so that we can filter contact point information,
  157. * decide whether we are in multi or single touch mode
  158. * and call input_mt_sync after each point if necessary
  159. */
  160. static int ntrig_event (struct hid_device *hid, struct hid_field *field,
  161. struct hid_usage *usage, __s32 value)
  162. {
  163. struct input_dev *input = field->hidinput->input;
  164. struct ntrig_data *nd = hid_get_drvdata(hid);
  165. /* No special handling needed for the pen */
  166. if (field->application == HID_DG_PEN)
  167. return 0;
  168. if (hid->claimed & HID_CLAIMED_INPUT) {
  169. switch (usage->hid) {
  170. case 0xff000001:
  171. /* Tag indicating the start of a multitouch group */
  172. nd->reading_mt = 1;
  173. nd->first_contact_touch = 0;
  174. break;
  175. case HID_DG_TIPSWITCH:
  176. nd->tipswitch = value;
  177. /* Prevent emission of touch until validated */
  178. return 1;
  179. case HID_DG_CONFIDENCE:
  180. nd->confidence = value;
  181. break;
  182. case HID_GD_X:
  183. nd->x = value;
  184. /* Clear the contact footer */
  185. nd->mt_foot_count = 0;
  186. break;
  187. case HID_GD_Y:
  188. nd->y = value;
  189. break;
  190. case HID_DG_CONTACTID:
  191. nd->id = value;
  192. break;
  193. case HID_DG_WIDTH:
  194. nd->w = value;
  195. break;
  196. case HID_DG_HEIGHT:
  197. nd->h = value;
  198. /*
  199. * when in single touch mode, this is the last
  200. * report received in a finger event. We want
  201. * to emit a normal (X, Y) position
  202. */
  203. if (!nd->reading_mt) {
  204. /*
  205. * TipSwitch indicates the presence of a
  206. * finger in single touch mode.
  207. */
  208. input_report_key(input, BTN_TOUCH,
  209. nd->tipswitch);
  210. input_report_key(input, BTN_TOOL_DOUBLETAP,
  211. nd->tipswitch);
  212. input_event(input, EV_ABS, ABS_X, nd->x);
  213. input_event(input, EV_ABS, ABS_Y, nd->y);
  214. }
  215. break;
  216. case 0xff000002:
  217. /*
  218. * we receive this when the device is in multitouch
  219. * mode. The first of the three values tagged with
  220. * this usage tells if the contact point is real
  221. * or a placeholder
  222. */
  223. /* Shouldn't get more than 4 footer packets, so skip */
  224. if (nd->mt_foot_count >= 4)
  225. break;
  226. nd->mt_footer[nd->mt_foot_count++] = value;
  227. /* if the footer isn't complete break */
  228. if (nd->mt_foot_count != 4)
  229. break;
  230. /* Pen activity signal. */
  231. if (nd->mt_footer[2]) {
  232. /*
  233. * When the pen deactivates touch, we see a
  234. * bogus frame with ContactCount > 0.
  235. * We can
  236. * save a bit of work by ensuring act_state < 0
  237. * even if deactivation slack is turned off.
  238. */
  239. nd->act_state = deactivate_slack - 1;
  240. nd->confidence = 0;
  241. break;
  242. }
  243. /*
  244. * The first footer value indicates the presence of a
  245. * finger.
  246. */
  247. if (nd->mt_footer[0]) {
  248. /*
  249. * We do not want to process contacts under
  250. * the size threshold, but do not want to
  251. * ignore them for activation state
  252. */
  253. if (nd->w < nd->min_width ||
  254. nd->h < nd->min_height)
  255. nd->confidence = 0;
  256. } else
  257. break;
  258. if (nd->act_state > 0) {
  259. /*
  260. * Contact meets the activation size threshold
  261. */
  262. if (nd->w >= nd->activation_width &&
  263. nd->h >= nd->activation_height) {
  264. if (nd->id)
  265. /*
  266. * first contact, activate now
  267. */
  268. nd->act_state = 0;
  269. else {
  270. /*
  271. * avoid corrupting this frame
  272. * but ensure next frame will
  273. * be active
  274. */
  275. nd->act_state = 1;
  276. break;
  277. }
  278. } else
  279. /*
  280. * Defer adjusting the activation state
  281. * until the end of the frame.
  282. */
  283. break;
  284. }
  285. /* Discarding this contact */
  286. if (!nd->confidence)
  287. break;
  288. /* emit a normal (X, Y) for the first point only */
  289. if (nd->id == 0) {
  290. /*
  291. * TipSwitch is superfluous in multitouch
  292. * mode. The footer events tell us
  293. * if there is a finger on the screen or
  294. * not.
  295. */
  296. nd->first_contact_touch = nd->confidence;
  297. input_event(input, EV_ABS, ABS_X, nd->x);
  298. input_event(input, EV_ABS, ABS_Y, nd->y);
  299. }
  300. /* Emit MT events */
  301. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  302. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  303. /*
  304. * Translate from height and width to size
  305. * and orientation.
  306. */
  307. if (nd->w > nd->h) {
  308. input_event(input, EV_ABS,
  309. ABS_MT_ORIENTATION, 1);
  310. input_event(input, EV_ABS,
  311. ABS_MT_TOUCH_MAJOR, nd->w);
  312. input_event(input, EV_ABS,
  313. ABS_MT_TOUCH_MINOR, nd->h);
  314. } else {
  315. input_event(input, EV_ABS,
  316. ABS_MT_ORIENTATION, 0);
  317. input_event(input, EV_ABS,
  318. ABS_MT_TOUCH_MAJOR, nd->h);
  319. input_event(input, EV_ABS,
  320. ABS_MT_TOUCH_MINOR, nd->w);
  321. }
  322. input_mt_sync(field->hidinput->input);
  323. break;
  324. case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
  325. if (!nd->reading_mt) /* Just to be sure */
  326. break;
  327. nd->reading_mt = 0;
  328. /*
  329. * Activation state machine logic:
  330. *
  331. * Fundamental states:
  332. * state > 0: Inactive
  333. * state <= 0: Active
  334. * state < -deactivate_slack:
  335. * Pen termination of touch
  336. *
  337. * Specific values of interest
  338. * state == activate_slack
  339. * no valid input since the last reset
  340. *
  341. * state == 0
  342. * general operational state
  343. *
  344. * state == -deactivate_slack
  345. * read sufficient empty frames to accept
  346. * the end of input and reset
  347. */
  348. if (nd->act_state > 0) { /* Currently inactive */
  349. if (value)
  350. /*
  351. * Consider each live contact as
  352. * evidence of intentional activity.
  353. */
  354. nd->act_state = (nd->act_state > value)
  355. ? nd->act_state - value
  356. : 0;
  357. else
  358. /*
  359. * Empty frame before we hit the
  360. * activity threshold, reset.
  361. */
  362. nd->act_state = nd->activate_slack;
  363. /*
  364. * Entered this block inactive and no
  365. * coordinates sent this frame, so hold off
  366. * on button state.
  367. */
  368. break;
  369. } else { /* Currently active */
  370. if (value && nd->act_state >=
  371. nd->deactivate_slack)
  372. /*
  373. * Live point: clear accumulated
  374. * deactivation count.
  375. */
  376. nd->act_state = 0;
  377. else if (nd->act_state <= nd->deactivate_slack)
  378. /*
  379. * We've consumed the deactivation
  380. * slack, time to deactivate and reset.
  381. */
  382. nd->act_state =
  383. nd->activate_slack;
  384. else { /* Move towards deactivation */
  385. nd->act_state--;
  386. break;
  387. }
  388. }
  389. if (nd->first_contact_touch && nd->act_state <= 0) {
  390. /*
  391. * Check to see if we're ready to start
  392. * emitting touch events.
  393. *
  394. * Note: activation slack will decrease over
  395. * the course of the frame, and it will be
  396. * inconsistent from the start to the end of
  397. * the frame. However if the frame starts
  398. * with slack, first_contact_touch will still
  399. * be 0 and we will not get to this point.
  400. */
  401. input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
  402. input_report_key(input, BTN_TOUCH, 1);
  403. } else {
  404. input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
  405. input_report_key(input, BTN_TOUCH, 0);
  406. }
  407. break;
  408. default:
  409. /* fall-back to the generic hidinput handling */
  410. return 0;
  411. }
  412. }
  413. /* we have handled the hidinput part, now remains hiddev */
  414. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
  415. hid->hiddev_hid_event(hid, field, usage, value);
  416. return 1;
  417. }
  418. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  419. {
  420. int ret;
  421. struct ntrig_data *nd;
  422. struct hid_input *hidinput;
  423. struct input_dev *input;
  424. struct hid_report *report;
  425. if (id->driver_data)
  426. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  427. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  428. if (!nd) {
  429. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  430. return -ENOMEM;
  431. }
  432. nd->reading_mt = 0;
  433. nd->min_width = 0;
  434. nd->min_height = 0;
  435. nd->activate_slack = activate_slack;
  436. nd->act_state = activate_slack;
  437. nd->deactivate_slack = -deactivate_slack;
  438. nd->sensor_logical_width = 0;
  439. nd->sensor_logical_height = 0;
  440. nd->sensor_physical_width = 0;
  441. nd->sensor_physical_height = 0;
  442. hid_set_drvdata(hdev, nd);
  443. ret = hid_parse(hdev);
  444. if (ret) {
  445. dev_err(&hdev->dev, "parse failed\n");
  446. goto err_free;
  447. }
  448. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  449. if (ret) {
  450. dev_err(&hdev->dev, "hw start failed\n");
  451. goto err_free;
  452. }
  453. list_for_each_entry(hidinput, &hdev->inputs, list) {
  454. if (hidinput->report->maxfield < 1)
  455. continue;
  456. input = hidinput->input;
  457. switch (hidinput->report->field[0]->application) {
  458. case HID_DG_PEN:
  459. input->name = "N-Trig Pen";
  460. break;
  461. case HID_DG_TOUCHSCREEN:
  462. /* These keys are redundant for fingers, clear them
  463. * to prevent incorrect identification */
  464. __clear_bit(BTN_TOOL_PEN, input->keybit);
  465. __clear_bit(BTN_TOOL_FINGER, input->keybit);
  466. __clear_bit(BTN_0, input->keybit);
  467. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  468. /*
  469. * The physical touchscreen (single touch)
  470. * input has a value for physical, whereas
  471. * the multitouch only has logical input
  472. * fields.
  473. */
  474. input->name =
  475. (hidinput->report->field[0]
  476. ->physical) ?
  477. "N-Trig Touchscreen" :
  478. "N-Trig MultiTouch";
  479. break;
  480. }
  481. }
  482. /* This is needed for devices with more recent firmware versions */
  483. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
  484. if (report)
  485. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  486. return 0;
  487. err_free:
  488. kfree(nd);
  489. return ret;
  490. }
  491. static void ntrig_remove(struct hid_device *hdev)
  492. {
  493. hid_hw_stop(hdev);
  494. kfree(hid_get_drvdata(hdev));
  495. }
  496. static const struct hid_device_id ntrig_devices[] = {
  497. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  498. .driver_data = NTRIG_DUPLICATE_USAGES },
  499. { }
  500. };
  501. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  502. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  503. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  504. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  505. };
  506. static struct hid_driver ntrig_driver = {
  507. .name = "ntrig",
  508. .id_table = ntrig_devices,
  509. .probe = ntrig_probe,
  510. .remove = ntrig_remove,
  511. .input_mapping = ntrig_input_mapping,
  512. .input_mapped = ntrig_input_mapped,
  513. .usage_table = ntrig_grabbed_usages,
  514. .event = ntrig_event,
  515. };
  516. static int __init ntrig_init(void)
  517. {
  518. return hid_register_driver(&ntrig_driver);
  519. }
  520. static void __exit ntrig_exit(void)
  521. {
  522. hid_unregister_driver(&ntrig_driver);
  523. }
  524. module_init(ntrig_init);
  525. module_exit(ntrig_exit);
  526. MODULE_LICENSE("GPL");