hid-ntrig.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. __s32 x, y, id, w, h;
  23. bool reading_a_point, found_contact_id;
  24. };
  25. /*
  26. * this driver is aimed at two firmware versions in circulation:
  27. * - dual pen/finger single touch
  28. * - finger multitouch, pen not working
  29. */
  30. static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  31. struct hid_field *field, struct hid_usage *usage,
  32. unsigned long **bit, int *max)
  33. {
  34. /* No special mappings needed for the pen */
  35. if (field->application == HID_DG_PEN)
  36. return 0;
  37. switch (usage->hid & HID_USAGE_PAGE) {
  38. case HID_UP_GENDESK:
  39. switch (usage->hid) {
  40. case HID_GD_X:
  41. hid_map_usage(hi, usage, bit, max,
  42. EV_ABS, ABS_MT_POSITION_X);
  43. input_set_abs_params(hi->input, ABS_X,
  44. field->logical_minimum,
  45. field->logical_maximum, 0, 0);
  46. return 1;
  47. case HID_GD_Y:
  48. hid_map_usage(hi, usage, bit, max,
  49. EV_ABS, ABS_MT_POSITION_Y);
  50. input_set_abs_params(hi->input, ABS_Y,
  51. field->logical_minimum,
  52. field->logical_maximum, 0, 0);
  53. return 1;
  54. }
  55. return 0;
  56. case HID_UP_DIGITIZER:
  57. switch (usage->hid) {
  58. /* we do not want to map these for now */
  59. case HID_DG_CONTACTID: /* value is useless */
  60. case HID_DG_INPUTMODE:
  61. case HID_DG_DEVICEINDEX:
  62. case HID_DG_CONTACTCOUNT:
  63. case HID_DG_CONTACTMAX:
  64. return -1;
  65. /* original mapping by Rafi Rubin */
  66. case HID_DG_CONFIDENCE:
  67. nt_map_key_clear(BTN_TOOL_DOUBLETAP);
  68. return 1;
  69. /* width/height mapped on TouchMajor/TouchMinor/Orientation */
  70. case HID_DG_WIDTH:
  71. hid_map_usage(hi, usage, bit, max,
  72. EV_ABS, ABS_MT_TOUCH_MAJOR);
  73. return 1;
  74. case HID_DG_HEIGHT:
  75. hid_map_usage(hi, usage, bit, max,
  76. EV_ABS, ABS_MT_TOUCH_MINOR);
  77. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  78. 0, 1, 0, 0);
  79. return 1;
  80. }
  81. return 0;
  82. case 0xff000000:
  83. /* we do not want to map these: no input-oriented meaning */
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  89. struct hid_field *field, struct hid_usage *usage,
  90. unsigned long **bit, int *max)
  91. {
  92. /* No special mappings needed for the pen */
  93. if (field->application == HID_DG_PEN)
  94. return 0;
  95. if (usage->type == EV_KEY || usage->type == EV_REL
  96. || usage->type == EV_ABS)
  97. clear_bit(usage->code, *bit);
  98. return 0;
  99. }
  100. /*
  101. * this function is called upon all reports
  102. * so that we can filter contact point information,
  103. * decide whether we are in multi or single touch mode
  104. * and call input_mt_sync after each point if necessary
  105. */
  106. static int ntrig_event (struct hid_device *hid, struct hid_field *field,
  107. struct hid_usage *usage, __s32 value)
  108. {
  109. struct input_dev *input = field->hidinput->input;
  110. struct ntrig_data *nd = hid_get_drvdata(hid);
  111. /* No special handling needed for the pen */
  112. if (field->application == HID_DG_PEN)
  113. return 0;
  114. if (hid->claimed & HID_CLAIMED_INPUT) {
  115. switch (usage->hid) {
  116. case HID_GD_X:
  117. nd->x = value;
  118. nd->reading_a_point = 1;
  119. break;
  120. case HID_GD_Y:
  121. nd->y = value;
  122. break;
  123. case HID_DG_CONTACTID:
  124. nd->id = value;
  125. /* we receive this only when in multitouch mode */
  126. nd->found_contact_id = 1;
  127. break;
  128. case HID_DG_WIDTH:
  129. nd->w = value;
  130. break;
  131. case HID_DG_HEIGHT:
  132. nd->h = value;
  133. /*
  134. * when in single touch mode, this is the last
  135. * report received in a finger event. We want
  136. * to emit a normal (X, Y) position
  137. */
  138. if (!nd->found_contact_id) {
  139. input_event(input, EV_ABS, ABS_X, nd->x);
  140. input_event(input, EV_ABS, ABS_Y, nd->y);
  141. }
  142. break;
  143. case 0xff000002:
  144. /*
  145. * we receive this when the device is in multitouch
  146. * mode. The first of the three values tagged with
  147. * this usage tells if the contact point is real
  148. * or a placeholder
  149. */
  150. if (!nd->reading_a_point || value != 1)
  151. break;
  152. /* emit a normal (X, Y) for the first point only */
  153. if (nd->id == 0) {
  154. input_event(input, EV_ABS, ABS_X, nd->x);
  155. input_event(input, EV_ABS, ABS_Y, nd->y);
  156. }
  157. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  158. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  159. if (nd->w > nd->h) {
  160. input_event(input, EV_ABS,
  161. ABS_MT_ORIENTATION, 1);
  162. input_event(input, EV_ABS,
  163. ABS_MT_TOUCH_MAJOR, nd->w);
  164. input_event(input, EV_ABS,
  165. ABS_MT_TOUCH_MINOR, nd->h);
  166. } else {
  167. input_event(input, EV_ABS,
  168. ABS_MT_ORIENTATION, 0);
  169. input_event(input, EV_ABS,
  170. ABS_MT_TOUCH_MAJOR, nd->h);
  171. input_event(input, EV_ABS,
  172. ABS_MT_TOUCH_MINOR, nd->w);
  173. }
  174. input_mt_sync(field->hidinput->input);
  175. nd->reading_a_point = 0;
  176. nd->found_contact_id = 0;
  177. break;
  178. default:
  179. /* fallback to the generic hidinput handling */
  180. return 0;
  181. }
  182. }
  183. /* we have handled the hidinput part, now remains hiddev */
  184. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
  185. hid->hiddev_hid_event(hid, field, usage, value);
  186. return 1;
  187. }
  188. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  189. {
  190. int ret;
  191. struct ntrig_data *nd;
  192. struct hid_input *hidinput;
  193. struct input_dev *input;
  194. if (id->driver_data)
  195. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  196. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  197. if (!nd) {
  198. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  199. return -ENOMEM;
  200. }
  201. nd->reading_a_point = 0;
  202. nd->found_contact_id = 0;
  203. hid_set_drvdata(hdev, nd);
  204. ret = hid_parse(hdev);
  205. if (ret) {
  206. dev_err(&hdev->dev, "parse failed\n");
  207. goto err_free;
  208. }
  209. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  210. if (ret) {
  211. dev_err(&hdev->dev, "hw start failed\n");
  212. goto err_free;
  213. }
  214. list_for_each_entry(hidinput, &hdev->inputs, list) {
  215. input = hidinput->input;
  216. switch (hidinput->report->field[0]->application) {
  217. case HID_DG_PEN:
  218. input->name = "N-Trig Pen";
  219. break;
  220. case HID_DG_TOUCHSCREEN:
  221. /*
  222. * The physical touchscreen (single touch)
  223. * input has a value for physical, whereas
  224. * the multitouch only has logical input
  225. * fields.
  226. */
  227. input->name =
  228. (hidinput->report->field[0]
  229. ->physical) ?
  230. "N-Trig Touchscreen" :
  231. "N-Trig MultiTouch";
  232. break;
  233. }
  234. }
  235. return 0;
  236. err_free:
  237. kfree(nd);
  238. return ret;
  239. }
  240. static void ntrig_remove(struct hid_device *hdev)
  241. {
  242. hid_hw_stop(hdev);
  243. kfree(hid_get_drvdata(hdev));
  244. }
  245. static const struct hid_device_id ntrig_devices[] = {
  246. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  247. .driver_data = NTRIG_DUPLICATE_USAGES },
  248. { }
  249. };
  250. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  251. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  252. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  253. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  254. };
  255. static struct hid_driver ntrig_driver = {
  256. .name = "ntrig",
  257. .id_table = ntrig_devices,
  258. .probe = ntrig_probe,
  259. .remove = ntrig_remove,
  260. .input_mapping = ntrig_input_mapping,
  261. .input_mapped = ntrig_input_mapped,
  262. .usage_table = ntrig_grabbed_usages,
  263. .event = ntrig_event,
  264. };
  265. static int __init ntrig_init(void)
  266. {
  267. return hid_register_driver(&ntrig_driver);
  268. }
  269. static void __exit ntrig_exit(void)
  270. {
  271. hid_unregister_driver(&ntrig_driver);
  272. }
  273. module_init(ntrig_init);
  274. module_exit(ntrig_exit);
  275. MODULE_LICENSE("GPL");