hid-ntrig.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_CONFIDENCE:
  123. nd->confidence = value;
  124. break;
  125. case HID_GD_X:
  126. nd->x = value;
  127. /* Clear the contact footer */
  128. nd->mt_foot_count = 0;
  129. break;
  130. case HID_GD_Y:
  131. nd->y = value;
  132. break;
  133. case HID_DG_CONTACTID:
  134. nd->id = value;
  135. break;
  136. case HID_DG_WIDTH:
  137. nd->w = value;
  138. break;
  139. case HID_DG_HEIGHT:
  140. nd->h = value;
  141. /*
  142. * when in single touch mode, this is the last
  143. * report received in a finger event. We want
  144. * to emit a normal (X, Y) position
  145. */
  146. if (!nd->reading_mt) {
  147. input_report_key(input, BTN_TOOL_DOUBLETAP,
  148. (nd->confidence != 0));
  149. input_event(input, EV_ABS, ABS_X, nd->x);
  150. input_event(input, EV_ABS, ABS_Y, nd->y);
  151. }
  152. break;
  153. case 0xff000002:
  154. /*
  155. * we receive this when the device is in multitouch
  156. * mode. The first of the three values tagged with
  157. * this usage tells if the contact point is real
  158. * or a placeholder
  159. */
  160. /* Shouldn't get more than 4 footer packets, so skip */
  161. if (nd->mt_foot_count >= 4)
  162. break;
  163. nd->mt_footer[nd->mt_foot_count++] = value;
  164. /* if the footer isn't complete break */
  165. if (nd->mt_foot_count != 4)
  166. break;
  167. /* Pen activity signal, trigger end of touch. */
  168. if (nd->mt_footer[2]) {
  169. nd->confidence = 0;
  170. break;
  171. }
  172. /* If the contact was invalid */
  173. if (!(nd->confidence && nd->mt_footer[0])
  174. || nd->w <= 250
  175. || nd->h <= 190) {
  176. nd->confidence = 0;
  177. break;
  178. }
  179. /* emit a normal (X, Y) for the first point only */
  180. if (nd->id == 0) {
  181. nd->first_contact_confidence = nd->confidence;
  182. input_event(input, EV_ABS, ABS_X, nd->x);
  183. input_event(input, EV_ABS, ABS_Y, nd->y);
  184. }
  185. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  186. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  187. if (nd->w > nd->h) {
  188. input_event(input, EV_ABS,
  189. ABS_MT_ORIENTATION, 1);
  190. input_event(input, EV_ABS,
  191. ABS_MT_TOUCH_MAJOR, nd->w);
  192. input_event(input, EV_ABS,
  193. ABS_MT_TOUCH_MINOR, nd->h);
  194. } else {
  195. input_event(input, EV_ABS,
  196. ABS_MT_ORIENTATION, 0);
  197. input_event(input, EV_ABS,
  198. ABS_MT_TOUCH_MAJOR, nd->h);
  199. input_event(input, EV_ABS,
  200. ABS_MT_TOUCH_MINOR, nd->w);
  201. }
  202. input_mt_sync(field->hidinput->input);
  203. break;
  204. case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
  205. if (!nd->reading_mt)
  206. break;
  207. nd->reading_mt = 0;
  208. if (nd->first_contact_confidence) {
  209. switch (value) {
  210. case 0: /* for single touch devices */
  211. case 1:
  212. input_report_key(input,
  213. BTN_TOOL_DOUBLETAP, 1);
  214. break;
  215. case 2:
  216. input_report_key(input,
  217. BTN_TOOL_TRIPLETAP, 1);
  218. break;
  219. case 3:
  220. default:
  221. input_report_key(input,
  222. BTN_TOOL_QUADTAP, 1);
  223. }
  224. input_report_key(input, BTN_TOUCH, 1);
  225. } else {
  226. input_report_key(input,
  227. BTN_TOOL_DOUBLETAP, 0);
  228. input_report_key(input,
  229. BTN_TOOL_TRIPLETAP, 0);
  230. input_report_key(input,
  231. BTN_TOOL_QUADTAP, 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. if (id->driver_data)
  251. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  252. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  253. if (!nd) {
  254. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  255. return -ENOMEM;
  256. }
  257. nd->reading_mt = 0;
  258. hid_set_drvdata(hdev, nd);
  259. ret = hid_parse(hdev);
  260. if (ret) {
  261. dev_err(&hdev->dev, "parse failed\n");
  262. goto err_free;
  263. }
  264. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  265. if (ret) {
  266. dev_err(&hdev->dev, "hw start failed\n");
  267. goto err_free;
  268. }
  269. list_for_each_entry(hidinput, &hdev->inputs, list) {
  270. input = hidinput->input;
  271. switch (hidinput->report->field[0]->application) {
  272. case HID_DG_PEN:
  273. input->name = "N-Trig Pen";
  274. break;
  275. case HID_DG_TOUCHSCREEN:
  276. __clear_bit(BTN_TOOL_PEN, input->keybit);
  277. /*
  278. * A little something special to enable
  279. * two and three finger taps.
  280. */
  281. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  282. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  283. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  284. /*
  285. * The physical touchscreen (single touch)
  286. * input has a value for physical, whereas
  287. * the multitouch only has logical input
  288. * fields.
  289. */
  290. input->name =
  291. (hidinput->report->field[0]
  292. ->physical) ?
  293. "N-Trig Touchscreen" :
  294. "N-Trig MultiTouch";
  295. break;
  296. }
  297. }
  298. return 0;
  299. err_free:
  300. kfree(nd);
  301. return ret;
  302. }
  303. static void ntrig_remove(struct hid_device *hdev)
  304. {
  305. hid_hw_stop(hdev);
  306. kfree(hid_get_drvdata(hdev));
  307. }
  308. static const struct hid_device_id ntrig_devices[] = {
  309. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  310. .driver_data = NTRIG_DUPLICATE_USAGES },
  311. { }
  312. };
  313. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  314. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  315. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  316. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  317. };
  318. static struct hid_driver ntrig_driver = {
  319. .name = "ntrig",
  320. .id_table = ntrig_devices,
  321. .probe = ntrig_probe,
  322. .remove = ntrig_remove,
  323. .input_mapping = ntrig_input_mapping,
  324. .input_mapped = ntrig_input_mapped,
  325. .usage_table = ntrig_grabbed_usages,
  326. .event = ntrig_event,
  327. };
  328. static int __init ntrig_init(void)
  329. {
  330. return hid_register_driver(&ntrig_driver);
  331. }
  332. static void __exit ntrig_exit(void)
  333. {
  334. hid_unregister_driver(&ntrig_driver);
  335. }
  336. module_init(ntrig_init);
  337. module_exit(ntrig_exit);
  338. MODULE_LICENSE("GPL");