hid-ntrig.c 9.6 KB

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