hid-ntrig.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #define nt_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
  23. EV_KEY, (c))
  24. struct ntrig_data {
  25. /* Incoming raw values for a single contact */
  26. __u16 x, y, w, h;
  27. __u16 id;
  28. __u8 confidence;
  29. bool reading_mt;
  30. __u8 first_contact_confidence;
  31. __u8 mt_footer[4];
  32. __u8 mt_foot_count;
  33. };
  34. /*
  35. * this driver is aimed at two firmware versions in circulation:
  36. * - dual pen/finger single touch
  37. * - finger multitouch, pen not working
  38. */
  39. static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  40. struct hid_field *field, struct hid_usage *usage,
  41. unsigned long **bit, int *max)
  42. {
  43. /* No special mappings needed for the pen and single touch */
  44. if (field->physical)
  45. return 0;
  46. switch (usage->hid & HID_USAGE_PAGE) {
  47. case HID_UP_GENDESK:
  48. switch (usage->hid) {
  49. case HID_GD_X:
  50. hid_map_usage(hi, usage, bit, max,
  51. EV_ABS, ABS_MT_POSITION_X);
  52. input_set_abs_params(hi->input, ABS_X,
  53. field->logical_minimum,
  54. field->logical_maximum, 0, 0);
  55. return 1;
  56. case HID_GD_Y:
  57. hid_map_usage(hi, usage, bit, max,
  58. EV_ABS, ABS_MT_POSITION_Y);
  59. input_set_abs_params(hi->input, ABS_Y,
  60. field->logical_minimum,
  61. field->logical_maximum, 0, 0);
  62. return 1;
  63. }
  64. return 0;
  65. case HID_UP_DIGITIZER:
  66. switch (usage->hid) {
  67. /* we do not want to map these for now */
  68. case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
  69. case HID_DG_INPUTMODE:
  70. case HID_DG_DEVICEINDEX:
  71. case HID_DG_CONTACTMAX:
  72. return -1;
  73. /* width/height mapped on TouchMajor/TouchMinor/Orientation */
  74. case HID_DG_WIDTH:
  75. hid_map_usage(hi, usage, bit, max,
  76. EV_ABS, ABS_MT_TOUCH_MAJOR);
  77. return 1;
  78. case HID_DG_HEIGHT:
  79. hid_map_usage(hi, usage, bit, max,
  80. EV_ABS, ABS_MT_TOUCH_MINOR);
  81. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  82. 0, 1, 0, 0);
  83. return 1;
  84. }
  85. return 0;
  86. case 0xff000000:
  87. /* we do not want to map these: no input-oriented meaning */
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  93. struct hid_field *field, struct hid_usage *usage,
  94. unsigned long **bit, int *max)
  95. {
  96. /* No special mappings needed for the pen and single touch */
  97. if (field->physical)
  98. return 0;
  99. if (usage->type == EV_KEY || usage->type == EV_REL
  100. || usage->type == EV_ABS)
  101. clear_bit(usage->code, *bit);
  102. return 0;
  103. }
  104. /*
  105. * this function is called upon all reports
  106. * so that we can filter contact point information,
  107. * decide whether we are in multi or single touch mode
  108. * and call input_mt_sync after each point if necessary
  109. */
  110. static int ntrig_event (struct hid_device *hid, struct hid_field *field,
  111. struct hid_usage *usage, __s32 value)
  112. {
  113. struct input_dev *input = field->hidinput->input;
  114. struct ntrig_data *nd = hid_get_drvdata(hid);
  115. /* No special handling needed for the pen */
  116. if (field->application == HID_DG_PEN)
  117. return 0;
  118. if (hid->claimed & HID_CLAIMED_INPUT) {
  119. switch (usage->hid) {
  120. case 0xff000001:
  121. /* Tag indicating the start of a multitouch group */
  122. nd->reading_mt = 1;
  123. nd->first_contact_confidence = 0;
  124. break;
  125. case HID_DG_TIPSWITCH:
  126. /* Prevent emission of touch until validated */
  127. return 1;
  128. case HID_DG_CONFIDENCE:
  129. nd->confidence = value;
  130. break;
  131. case HID_GD_X:
  132. nd->x = value;
  133. /* Clear the contact footer */
  134. nd->mt_foot_count = 0;
  135. break;
  136. case HID_GD_Y:
  137. nd->y = value;
  138. break;
  139. case HID_DG_CONTACTID:
  140. nd->id = value;
  141. break;
  142. case HID_DG_WIDTH:
  143. nd->w = value;
  144. break;
  145. case HID_DG_HEIGHT:
  146. nd->h = value;
  147. /*
  148. * when in single touch mode, this is the last
  149. * report received in a finger event. We want
  150. * to emit a normal (X, Y) position
  151. */
  152. if (!nd->reading_mt) {
  153. input_report_key(input, BTN_TOOL_DOUBLETAP,
  154. (nd->confidence != 0));
  155. input_event(input, EV_ABS, ABS_X, nd->x);
  156. input_event(input, EV_ABS, ABS_Y, nd->y);
  157. }
  158. break;
  159. case 0xff000002:
  160. /*
  161. * we receive this when the device is in multitouch
  162. * mode. The first of the three values tagged with
  163. * this usage tells if the contact point is real
  164. * or a placeholder
  165. */
  166. /* Shouldn't get more than 4 footer packets, so skip */
  167. if (nd->mt_foot_count >= 4)
  168. break;
  169. nd->mt_footer[nd->mt_foot_count++] = value;
  170. /* if the footer isn't complete break */
  171. if (nd->mt_foot_count != 4)
  172. break;
  173. /* Pen activity signal, trigger end of touch. */
  174. if (nd->mt_footer[2]) {
  175. nd->confidence = 0;
  176. break;
  177. }
  178. /* If the contact was invalid */
  179. if (!(nd->confidence && nd->mt_footer[0])
  180. || nd->w <= 250
  181. || nd->h <= 190) {
  182. nd->confidence = 0;
  183. break;
  184. }
  185. /* emit a normal (X, Y) for the first point only */
  186. if (nd->id == 0) {
  187. nd->first_contact_confidence = nd->confidence;
  188. input_event(input, EV_ABS, ABS_X, nd->x);
  189. input_event(input, EV_ABS, ABS_Y, nd->y);
  190. }
  191. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  192. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  193. if (nd->w > nd->h) {
  194. input_event(input, EV_ABS,
  195. ABS_MT_ORIENTATION, 1);
  196. input_event(input, EV_ABS,
  197. ABS_MT_TOUCH_MAJOR, nd->w);
  198. input_event(input, EV_ABS,
  199. ABS_MT_TOUCH_MINOR, nd->h);
  200. } else {
  201. input_event(input, EV_ABS,
  202. ABS_MT_ORIENTATION, 0);
  203. input_event(input, EV_ABS,
  204. ABS_MT_TOUCH_MAJOR, nd->h);
  205. input_event(input, EV_ABS,
  206. ABS_MT_TOUCH_MINOR, nd->w);
  207. }
  208. input_mt_sync(field->hidinput->input);
  209. break;
  210. case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
  211. if (!nd->reading_mt)
  212. break;
  213. nd->reading_mt = 0;
  214. if (nd->first_contact_confidence) {
  215. switch (value) {
  216. case 0: /* for single touch devices */
  217. case 1:
  218. input_report_key(input,
  219. BTN_TOOL_DOUBLETAP, 1);
  220. break;
  221. case 2:
  222. input_report_key(input,
  223. BTN_TOOL_TRIPLETAP, 1);
  224. break;
  225. case 3:
  226. default:
  227. input_report_key(input,
  228. BTN_TOOL_QUADTAP, 1);
  229. }
  230. input_report_key(input, BTN_TOUCH, 1);
  231. } else {
  232. input_report_key(input,
  233. BTN_TOOL_DOUBLETAP, 0);
  234. input_report_key(input,
  235. BTN_TOOL_TRIPLETAP, 0);
  236. input_report_key(input,
  237. BTN_TOOL_QUADTAP, 0);
  238. input_report_key(input, BTN_TOUCH, 0);
  239. }
  240. break;
  241. default:
  242. /* fallback to the generic hidinput handling */
  243. return 0;
  244. }
  245. }
  246. /* we have handled the hidinput part, now remains hiddev */
  247. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
  248. hid->hiddev_hid_event(hid, field, usage, value);
  249. return 1;
  250. }
  251. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  252. {
  253. int ret;
  254. struct ntrig_data *nd;
  255. struct hid_input *hidinput;
  256. struct input_dev *input;
  257. struct hid_report *report;
  258. if (id->driver_data)
  259. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  260. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  261. if (!nd) {
  262. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  263. return -ENOMEM;
  264. }
  265. nd->reading_mt = 0;
  266. hid_set_drvdata(hdev, nd);
  267. ret = hid_parse(hdev);
  268. if (ret) {
  269. dev_err(&hdev->dev, "parse failed\n");
  270. goto err_free;
  271. }
  272. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  273. if (ret) {
  274. dev_err(&hdev->dev, "hw start failed\n");
  275. goto err_free;
  276. }
  277. list_for_each_entry(hidinput, &hdev->inputs, list) {
  278. if (hidinput->report->maxfield < 1)
  279. continue;
  280. input = hidinput->input;
  281. switch (hidinput->report->field[0]->application) {
  282. case HID_DG_PEN:
  283. input->name = "N-Trig Pen";
  284. break;
  285. case HID_DG_TOUCHSCREEN:
  286. /* These keys are redundant for fingers, clear them
  287. * to prevent incorrect identification */
  288. __clear_bit(BTN_TOOL_PEN, input->keybit);
  289. __clear_bit(BTN_TOOL_FINGER, input->keybit);
  290. __clear_bit(BTN_0, input->keybit);
  291. /*
  292. * A little something special to enable
  293. * two and three finger taps.
  294. */
  295. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  296. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  297. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  298. /*
  299. * The physical touchscreen (single touch)
  300. * input has a value for physical, whereas
  301. * the multitouch only has logical input
  302. * fields.
  303. */
  304. input->name =
  305. (hidinput->report->field[0]
  306. ->physical) ?
  307. "N-Trig Touchscreen" :
  308. "N-Trig MultiTouch";
  309. break;
  310. }
  311. }
  312. /* This is needed for devices with more recent firmware versions */
  313. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
  314. if (report)
  315. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  316. return 0;
  317. err_free:
  318. kfree(nd);
  319. return ret;
  320. }
  321. static void ntrig_remove(struct hid_device *hdev)
  322. {
  323. hid_hw_stop(hdev);
  324. kfree(hid_get_drvdata(hdev));
  325. }
  326. static const struct hid_device_id ntrig_devices[] = {
  327. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  328. .driver_data = NTRIG_DUPLICATE_USAGES },
  329. { }
  330. };
  331. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  332. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  333. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  334. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  335. };
  336. static struct hid_driver ntrig_driver = {
  337. .name = "ntrig",
  338. .id_table = ntrig_devices,
  339. .probe = ntrig_probe,
  340. .remove = ntrig_remove,
  341. .input_mapping = ntrig_input_mapping,
  342. .input_mapped = ntrig_input_mapped,
  343. .usage_table = ntrig_grabbed_usages,
  344. .event = ntrig_event,
  345. };
  346. static int __init ntrig_init(void)
  347. {
  348. return hid_register_driver(&ntrig_driver);
  349. }
  350. static void __exit ntrig_exit(void)
  351. {
  352. hid_unregister_driver(&ntrig_driver);
  353. }
  354. module_init(ntrig_init);
  355. module_exit(ntrig_exit);
  356. MODULE_LICENSE("GPL");