hid-ntrig.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_report_key(input, BTN_TOUCH,
  156. (nd->confidence != 0));
  157. input_event(input, EV_ABS, ABS_X, nd->x);
  158. input_event(input, EV_ABS, ABS_Y, nd->y);
  159. }
  160. break;
  161. case 0xff000002:
  162. /*
  163. * we receive this when the device is in multitouch
  164. * mode. The first of the three values tagged with
  165. * this usage tells if the contact point is real
  166. * or a placeholder
  167. */
  168. /* Shouldn't get more than 4 footer packets, so skip */
  169. if (nd->mt_foot_count >= 4)
  170. break;
  171. nd->mt_footer[nd->mt_foot_count++] = value;
  172. /* if the footer isn't complete break */
  173. if (nd->mt_foot_count != 4)
  174. break;
  175. /* Pen activity signal, trigger end of touch. */
  176. if (nd->mt_footer[2]) {
  177. nd->confidence = 0;
  178. break;
  179. }
  180. /* If the contact was invalid */
  181. if (!(nd->confidence && nd->mt_footer[0])
  182. || nd->w <= 250
  183. || nd->h <= 190) {
  184. nd->confidence = 0;
  185. break;
  186. }
  187. /* emit a normal (X, Y) for the first point only */
  188. if (nd->id == 0) {
  189. nd->first_contact_confidence = nd->confidence;
  190. input_event(input, EV_ABS, ABS_X, nd->x);
  191. input_event(input, EV_ABS, ABS_Y, nd->y);
  192. }
  193. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  194. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  195. if (nd->w > nd->h) {
  196. input_event(input, EV_ABS,
  197. ABS_MT_ORIENTATION, 1);
  198. input_event(input, EV_ABS,
  199. ABS_MT_TOUCH_MAJOR, nd->w);
  200. input_event(input, EV_ABS,
  201. ABS_MT_TOUCH_MINOR, nd->h);
  202. } else {
  203. input_event(input, EV_ABS,
  204. ABS_MT_ORIENTATION, 0);
  205. input_event(input, EV_ABS,
  206. ABS_MT_TOUCH_MAJOR, nd->h);
  207. input_event(input, EV_ABS,
  208. ABS_MT_TOUCH_MINOR, nd->w);
  209. }
  210. input_mt_sync(field->hidinput->input);
  211. break;
  212. case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
  213. if (!nd->reading_mt)
  214. break;
  215. nd->reading_mt = 0;
  216. if (nd->first_contact_confidence) {
  217. switch (value) {
  218. case 0: /* for single touch devices */
  219. case 1:
  220. input_report_key(input,
  221. BTN_TOOL_DOUBLETAP, 1);
  222. break;
  223. case 2:
  224. input_report_key(input,
  225. BTN_TOOL_TRIPLETAP, 1);
  226. break;
  227. case 3:
  228. default:
  229. input_report_key(input,
  230. BTN_TOOL_QUADTAP, 1);
  231. }
  232. input_report_key(input, BTN_TOUCH, 1);
  233. } else {
  234. input_report_key(input,
  235. BTN_TOOL_DOUBLETAP, 0);
  236. input_report_key(input,
  237. BTN_TOOL_TRIPLETAP, 0);
  238. input_report_key(input,
  239. BTN_TOOL_QUADTAP, 0);
  240. input_report_key(input, BTN_TOUCH, 0);
  241. }
  242. break;
  243. default:
  244. /* fallback to the generic hidinput handling */
  245. return 0;
  246. }
  247. }
  248. /* we have handled the hidinput part, now remains hiddev */
  249. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
  250. hid->hiddev_hid_event(hid, field, usage, value);
  251. return 1;
  252. }
  253. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  254. {
  255. int ret;
  256. struct ntrig_data *nd;
  257. struct hid_input *hidinput;
  258. struct input_dev *input;
  259. struct hid_report *report;
  260. if (id->driver_data)
  261. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  262. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  263. if (!nd) {
  264. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  265. return -ENOMEM;
  266. }
  267. nd->reading_mt = 0;
  268. hid_set_drvdata(hdev, nd);
  269. ret = hid_parse(hdev);
  270. if (ret) {
  271. dev_err(&hdev->dev, "parse failed\n");
  272. goto err_free;
  273. }
  274. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  275. if (ret) {
  276. dev_err(&hdev->dev, "hw start failed\n");
  277. goto err_free;
  278. }
  279. list_for_each_entry(hidinput, &hdev->inputs, list) {
  280. if (hidinput->report->maxfield < 1)
  281. continue;
  282. input = hidinput->input;
  283. switch (hidinput->report->field[0]->application) {
  284. case HID_DG_PEN:
  285. input->name = "N-Trig Pen";
  286. break;
  287. case HID_DG_TOUCHSCREEN:
  288. /* These keys are redundant for fingers, clear them
  289. * to prevent incorrect identification */
  290. __clear_bit(BTN_TOOL_PEN, input->keybit);
  291. __clear_bit(BTN_TOOL_FINGER, input->keybit);
  292. __clear_bit(BTN_0, input->keybit);
  293. /*
  294. * A little something special to enable
  295. * two and three finger taps.
  296. */
  297. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  298. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  299. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  300. /*
  301. * The physical touchscreen (single touch)
  302. * input has a value for physical, whereas
  303. * the multitouch only has logical input
  304. * fields.
  305. */
  306. input->name =
  307. (hidinput->report->field[0]
  308. ->physical) ?
  309. "N-Trig Touchscreen" :
  310. "N-Trig MultiTouch";
  311. break;
  312. }
  313. }
  314. /* This is needed for devices with more recent firmware versions */
  315. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
  316. if (report)
  317. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  318. return 0;
  319. err_free:
  320. kfree(nd);
  321. return ret;
  322. }
  323. static void ntrig_remove(struct hid_device *hdev)
  324. {
  325. hid_hw_stop(hdev);
  326. kfree(hid_get_drvdata(hdev));
  327. }
  328. static const struct hid_device_id ntrig_devices[] = {
  329. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  330. .driver_data = NTRIG_DUPLICATE_USAGES },
  331. { }
  332. };
  333. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  334. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  335. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  336. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  337. };
  338. static struct hid_driver ntrig_driver = {
  339. .name = "ntrig",
  340. .id_table = ntrig_devices,
  341. .probe = ntrig_probe,
  342. .remove = ntrig_remove,
  343. .input_mapping = ntrig_input_mapping,
  344. .input_mapped = ntrig_input_mapped,
  345. .usage_table = ntrig_grabbed_usages,
  346. .event = ntrig_event,
  347. };
  348. static int __init ntrig_init(void)
  349. {
  350. return hid_register_driver(&ntrig_driver);
  351. }
  352. static void __exit ntrig_exit(void)
  353. {
  354. hid_unregister_driver(&ntrig_driver);
  355. }
  356. module_init(ntrig_init);
  357. module_exit(ntrig_exit);
  358. MODULE_LICENSE("GPL");