hid-ntrig.c 9.6 KB

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