hid-ntrig.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. char reading_a_point, found_contact_id;
  24. char pen_active;
  25. char finger_active;
  26. char inverted;
  27. };
  28. /*
  29. * this driver is aimed at two firmware versions in circulation:
  30. * - dual pen/finger single touch
  31. * - finger multitouch, pen not working
  32. */
  33. static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  34. struct hid_field *field, struct hid_usage *usage,
  35. unsigned long **bit, int *max)
  36. {
  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. if (usage->type == EV_KEY || usage->type == EV_REL
  93. || usage->type == EV_ABS)
  94. clear_bit(usage->code, *bit);
  95. return 0;
  96. }
  97. /*
  98. * this function is called upon all reports
  99. * so that we can filter contact point information,
  100. * decide whether we are in multi or single touch mode
  101. * and call input_mt_sync after each point if necessary
  102. */
  103. static int ntrig_event (struct hid_device *hid, struct hid_field *field,
  104. struct hid_usage *usage, __s32 value)
  105. {
  106. struct input_dev *input = field->hidinput->input;
  107. struct ntrig_data *nd = hid_get_drvdata(hid);
  108. if (hid->claimed & HID_CLAIMED_INPUT) {
  109. switch (usage->hid) {
  110. case HID_DG_INRANGE:
  111. if (field->application & 0x3)
  112. nd->pen_active = (value != 0);
  113. else
  114. nd->finger_active = (value != 0);
  115. return 0;
  116. case HID_DG_INVERT:
  117. nd->inverted = value;
  118. return 0;
  119. case HID_GD_X:
  120. nd->x = value;
  121. nd->reading_a_point = 1;
  122. break;
  123. case HID_GD_Y:
  124. nd->y = value;
  125. break;
  126. case HID_DG_CONTACTID:
  127. nd->id = value;
  128. /* we receive this only when in multitouch mode */
  129. nd->found_contact_id = 1;
  130. break;
  131. case HID_DG_WIDTH:
  132. nd->w = value;
  133. break;
  134. case HID_DG_HEIGHT:
  135. nd->h = value;
  136. /*
  137. * when in single touch mode, this is the last
  138. * report received in a finger event. We want
  139. * to emit a normal (X, Y) position
  140. */
  141. if (!nd->found_contact_id) {
  142. if (nd->pen_active && nd->finger_active) {
  143. input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
  144. input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
  145. }
  146. input_event(input, EV_ABS, ABS_X, nd->x);
  147. input_event(input, EV_ABS, ABS_Y, nd->y);
  148. }
  149. break;
  150. case HID_DG_TIPPRESSURE:
  151. /*
  152. * when in single touch mode, this is the last
  153. * report received in a pen event. We want
  154. * to emit a normal (X, Y) position
  155. */
  156. if (! nd->found_contact_id) {
  157. if (nd->pen_active && nd->finger_active) {
  158. input_report_key(input,
  159. nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN
  160. , 0);
  161. input_report_key(input,
  162. nd->inverted ? BTN_TOOL_RUBBER : BTN_TOOL_PEN
  163. , 1);
  164. }
  165. input_event(input, EV_ABS, ABS_X, nd->x);
  166. input_event(input, EV_ABS, ABS_Y, nd->y);
  167. input_event(input, EV_ABS, ABS_PRESSURE, value);
  168. }
  169. break;
  170. case 0xff000002:
  171. /*
  172. * we receive this when the device is in multitouch
  173. * mode. The first of the three values tagged with
  174. * this usage tells if the contact point is real
  175. * or a placeholder
  176. */
  177. if (!nd->reading_a_point || value != 1)
  178. break;
  179. /* emit a normal (X, Y) for the first point only */
  180. if (nd->id == 0) {
  181. input_event(input, EV_ABS, ABS_X, nd->x);
  182. input_event(input, EV_ABS, ABS_Y, nd->y);
  183. }
  184. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  185. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  186. if (nd->w > nd->h) {
  187. input_event(input, EV_ABS,
  188. ABS_MT_ORIENTATION, 1);
  189. input_event(input, EV_ABS,
  190. ABS_MT_TOUCH_MAJOR, nd->w);
  191. input_event(input, EV_ABS,
  192. ABS_MT_TOUCH_MINOR, nd->h);
  193. } else {
  194. input_event(input, EV_ABS,
  195. ABS_MT_ORIENTATION, 0);
  196. input_event(input, EV_ABS,
  197. ABS_MT_TOUCH_MAJOR, nd->h);
  198. input_event(input, EV_ABS,
  199. ABS_MT_TOUCH_MINOR, nd->w);
  200. }
  201. input_mt_sync(field->hidinput->input);
  202. nd->reading_a_point = 0;
  203. nd->found_contact_id = 0;
  204. break;
  205. default:
  206. /* fallback to the generic hidinput handling */
  207. return 0;
  208. }
  209. }
  210. /* we have handled the hidinput part, now remains hiddev */
  211. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  212. hid->hiddev_hid_event(hid, field, usage, value);
  213. return 1;
  214. }
  215. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  216. {
  217. int ret;
  218. struct ntrig_data *nd;
  219. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  220. if (!nd) {
  221. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  222. return -ENOMEM;
  223. }
  224. nd->reading_a_point = 0;
  225. nd->found_contact_id = 0;
  226. hid_set_drvdata(hdev, nd);
  227. ret = hid_parse(hdev);
  228. if (!ret)
  229. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  230. if (ret)
  231. kfree (nd);
  232. return ret;
  233. }
  234. static void ntrig_remove(struct hid_device *hdev)
  235. {
  236. hid_hw_stop(hdev);
  237. kfree(hid_get_drvdata(hdev));
  238. }
  239. static const struct hid_device_id ntrig_devices[] = {
  240. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  241. .driver_data = NTRIG_DUPLICATE_USAGES },
  242. { }
  243. };
  244. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  245. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  246. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  247. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  248. };
  249. static struct hid_driver ntrig_driver = {
  250. .name = "ntrig",
  251. .id_table = ntrig_devices,
  252. .probe = ntrig_probe,
  253. .remove = ntrig_remove,
  254. .input_mapping = ntrig_input_mapping,
  255. .input_mapped = ntrig_input_mapped,
  256. .usage_table = ntrig_grabbed_usages,
  257. .event = ntrig_event,
  258. };
  259. static int __init ntrig_init(void)
  260. {
  261. return hid_register_driver(&ntrig_driver);
  262. }
  263. static void __exit ntrig_exit(void)
  264. {
  265. hid_unregister_driver(&ntrig_driver);
  266. }
  267. module_init(ntrig_init);
  268. module_exit(ntrig_exit);
  269. MODULE_LICENSE("GPL");