hid-ntrig.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. };
  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. switch (usage->hid & HID_USAGE_PAGE) {
  35. case HID_UP_GENDESK:
  36. switch (usage->hid) {
  37. case HID_GD_X:
  38. hid_map_usage(hi, usage, bit, max,
  39. EV_ABS, ABS_MT_POSITION_X);
  40. input_set_abs_params(hi->input, ABS_X,
  41. field->logical_minimum,
  42. field->logical_maximum, 0, 0);
  43. return 1;
  44. case HID_GD_Y:
  45. hid_map_usage(hi, usage, bit, max,
  46. EV_ABS, ABS_MT_POSITION_Y);
  47. input_set_abs_params(hi->input, ABS_Y,
  48. field->logical_minimum,
  49. field->logical_maximum, 0, 0);
  50. return 1;
  51. }
  52. return 0;
  53. case HID_UP_DIGITIZER:
  54. switch (usage->hid) {
  55. /* we do not want to map these for now */
  56. case HID_DG_INVERT: /* value is always 0 */
  57. case HID_DG_ERASER: /* value is always 0 */
  58. case HID_DG_CONTACTID: /* value is useless */
  59. case HID_DG_BARRELSWITCH: /* doubtful */
  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_GD_X:
  111. nd->x = value;
  112. nd->reading_a_point = 1;
  113. break;
  114. case HID_GD_Y:
  115. nd->y = value;
  116. break;
  117. case HID_DG_CONTACTID:
  118. nd->id = value;
  119. /* we receive this only when in multitouch mode */
  120. nd->found_contact_id = 1;
  121. break;
  122. case HID_DG_WIDTH:
  123. nd->w = value;
  124. break;
  125. case HID_DG_HEIGHT:
  126. nd->h = value;
  127. /*
  128. * when in single touch mode, this is the last
  129. * report received in a finger event. We want
  130. * to emit a normal (X, Y) position
  131. */
  132. if (! nd->found_contact_id) {
  133. input_event(input, EV_ABS, ABS_X, nd->x);
  134. input_event(input, EV_ABS, ABS_Y, nd->y);
  135. }
  136. break;
  137. case HID_DG_TIPPRESSURE:
  138. /*
  139. * when in single touch mode, this is the last
  140. * report received in a pen event. We want
  141. * to emit a normal (X, Y) position
  142. */
  143. if (! nd->found_contact_id) {
  144. input_event(input, EV_ABS, ABS_X, nd->x);
  145. input_event(input, EV_ABS, ABS_Y, nd->y);
  146. input_event(input, EV_ABS, ABS_PRESSURE, value);
  147. }
  148. break;
  149. case 0xff000002:
  150. /*
  151. * we receive this when the device is in multitouch
  152. * mode. The first of the three values tagged with
  153. * this usage tells if the contact point is real
  154. * or a placeholder
  155. */
  156. if (!nd->reading_a_point || value != 1)
  157. break;
  158. /* emit a normal (X, Y) for the first point only */
  159. if (nd->id == 0) {
  160. input_event(input, EV_ABS, ABS_X, nd->x);
  161. input_event(input, EV_ABS, ABS_Y, nd->y);
  162. }
  163. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  164. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  165. if (nd->w > nd->h) {
  166. input_event(input, EV_ABS,
  167. ABS_MT_ORIENTATION, 1);
  168. input_event(input, EV_ABS,
  169. ABS_MT_TOUCH_MAJOR, nd->w);
  170. input_event(input, EV_ABS,
  171. ABS_MT_TOUCH_MINOR, nd->h);
  172. } else {
  173. input_event(input, EV_ABS,
  174. ABS_MT_ORIENTATION, 0);
  175. input_event(input, EV_ABS,
  176. ABS_MT_TOUCH_MAJOR, nd->h);
  177. input_event(input, EV_ABS,
  178. ABS_MT_TOUCH_MINOR, nd->w);
  179. }
  180. input_mt_sync(field->hidinput->input);
  181. nd->reading_a_point = 0;
  182. nd->found_contact_id = 0;
  183. break;
  184. default:
  185. /* fallback to the generic hidinput handling */
  186. return 0;
  187. }
  188. }
  189. /* we have handled the hidinput part, now remains hiddev */
  190. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  191. hid->hiddev_hid_event(hid, field, usage, value);
  192. return 1;
  193. }
  194. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  195. {
  196. int ret;
  197. struct ntrig_data *nd;
  198. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  199. if (!nd) {
  200. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  201. return -ENOMEM;
  202. }
  203. nd->reading_a_point = 0;
  204. nd->found_contact_id = 0;
  205. hid_set_drvdata(hdev, nd);
  206. ret = hid_parse(hdev);
  207. if (!ret)
  208. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  209. if (ret)
  210. kfree (nd);
  211. return ret;
  212. }
  213. static void ntrig_remove(struct hid_device *hdev)
  214. {
  215. hid_hw_stop(hdev);
  216. kfree(hid_get_drvdata(hdev));
  217. }
  218. static const struct hid_device_id ntrig_devices[] = {
  219. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  220. .driver_data = NTRIG_DUPLICATE_USAGES },
  221. { }
  222. };
  223. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  224. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  225. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  226. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  227. };
  228. static struct hid_driver ntrig_driver = {
  229. .name = "ntrig",
  230. .id_table = ntrig_devices,
  231. .probe = ntrig_probe,
  232. .remove = ntrig_remove,
  233. .input_mapping = ntrig_input_mapping,
  234. .input_mapped = ntrig_input_mapped,
  235. .usage_table = ntrig_grabbed_usages,
  236. .event = ntrig_event,
  237. };
  238. static int ntrig_init(void)
  239. {
  240. return hid_register_driver(&ntrig_driver);
  241. }
  242. static void ntrig_exit(void)
  243. {
  244. hid_unregister_driver(&ntrig_driver);
  245. }
  246. module_init(ntrig_init);
  247. module_exit(ntrig_exit);
  248. MODULE_LICENSE("GPL");