hid-ntrig.c 9.6 KB

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