hid-stantum.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * HID driver for Stantum multitouch panels
  3. *
  4. * Copyright (c) 2009 Stephane Chatty <chatty@enac.fr>
  5. *
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. MODULE_VERSION("0.6");
  17. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  18. MODULE_DESCRIPTION("Stantum HID multitouch panels");
  19. MODULE_LICENSE("GPL");
  20. #include "hid-ids.h"
  21. struct stantum_data {
  22. __s32 x, y, z, w, h; /* x, y, pressure, width, height */
  23. __u16 id; /* touch id */
  24. bool valid; /* valid finger data, or just placeholder? */
  25. bool first; /* first finger in the HID packet? */
  26. bool activity; /* at least one active finger so far? */
  27. };
  28. static int stantum_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  29. struct hid_field *field, struct hid_usage *usage,
  30. unsigned long **bit, int *max)
  31. {
  32. switch (usage->hid & HID_USAGE_PAGE) {
  33. case HID_UP_GENDESK:
  34. switch (usage->hid) {
  35. case HID_GD_X:
  36. hid_map_usage(hi, usage, bit, max,
  37. EV_ABS, ABS_MT_POSITION_X);
  38. /* touchscreen emulation */
  39. input_set_abs_params(hi->input, ABS_X,
  40. field->logical_minimum,
  41. field->logical_maximum, 0, 0);
  42. return 1;
  43. case HID_GD_Y:
  44. hid_map_usage(hi, usage, bit, max,
  45. EV_ABS, ABS_MT_POSITION_Y);
  46. /* touchscreen emulation */
  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. case HID_DG_INRANGE:
  56. case HID_DG_CONFIDENCE:
  57. case HID_DG_INPUTMODE:
  58. case HID_DG_DEVICEINDEX:
  59. case HID_DG_CONTACTCOUNT:
  60. case HID_DG_CONTACTMAX:
  61. case HID_DG_TIPPRESSURE:
  62. return -1;
  63. case HID_DG_TIPSWITCH:
  64. /* touchscreen emulation */
  65. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  66. return 1;
  67. case HID_DG_WIDTH:
  68. hid_map_usage(hi, usage, bit, max,
  69. EV_ABS, ABS_MT_TOUCH_MAJOR);
  70. return 1;
  71. case HID_DG_HEIGHT:
  72. hid_map_usage(hi, usage, bit, max,
  73. EV_ABS, ABS_MT_TOUCH_MINOR);
  74. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  75. 1, 1, 0, 0);
  76. return 1;
  77. case HID_DG_CONTACTID:
  78. hid_map_usage(hi, usage, bit, max,
  79. EV_ABS, ABS_MT_TRACKING_ID);
  80. return 1;
  81. }
  82. return 0;
  83. case 0xff000000:
  84. /* no input-oriented meaning */
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. static int stantum_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. if (usage->type == EV_KEY || usage->type == EV_ABS)
  94. clear_bit(usage->code, *bit);
  95. return 0;
  96. }
  97. /*
  98. * this function is called when a whole finger has been parsed,
  99. * so that it can decide what to send to the input layer.
  100. */
  101. static void stantum_filter_event(struct stantum_data *sd,
  102. struct input_dev *input)
  103. {
  104. bool wide;
  105. if (!sd->valid) {
  106. /*
  107. * touchscreen emulation: if the first finger is not valid and
  108. * there previously was finger activity, this is a release
  109. */
  110. if (sd->first && sd->activity) {
  111. input_event(input, EV_KEY, BTN_TOUCH, 0);
  112. sd->activity = false;
  113. }
  114. return;
  115. }
  116. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, sd->id);
  117. input_event(input, EV_ABS, ABS_MT_POSITION_X, sd->x);
  118. input_event(input, EV_ABS, ABS_MT_POSITION_Y, sd->y);
  119. wide = (sd->w > sd->h);
  120. input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
  121. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, wide ? sd->w : sd->h);
  122. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, wide ? sd->h : sd->w);
  123. #if 0
  124. /* MT_PRESSURE does not exist yet */
  125. input_event(input, EV_ABS, ABS_MT_PRESSURE, sd->z);
  126. #endif
  127. input_mt_sync(input);
  128. sd->valid = false;
  129. sd->first = false;
  130. /* touchscreen emulation */
  131. if (sd->first) {
  132. if (!sd->activity) {
  133. input_event(input, EV_KEY, BTN_TOUCH, 1);
  134. sd->activity = true;
  135. }
  136. input_event(input, EV_ABS, ABS_X, sd->x);
  137. input_event(input, EV_ABS, ABS_Y, sd->y);
  138. }
  139. }
  140. static int stantum_event(struct hid_device *hid, struct hid_field *field,
  141. struct hid_usage *usage, __s32 value)
  142. {
  143. struct stantum_data *sd = hid_get_drvdata(hid);
  144. if (hid->claimed & HID_CLAIMED_INPUT) {
  145. struct input_dev *input = field->hidinput->input;
  146. switch (usage->hid) {
  147. case HID_DG_INRANGE:
  148. /* this is the last field in a finger */
  149. stantum_filter_event(sd, input);
  150. break;
  151. case HID_DG_WIDTH:
  152. sd->w = value;
  153. break;
  154. case HID_DG_HEIGHT:
  155. sd->h = value;
  156. break;
  157. case HID_GD_X:
  158. sd->x = value;
  159. break;
  160. case HID_GD_Y:
  161. sd->y = value;
  162. break;
  163. case HID_DG_TIPPRESSURE:
  164. sd->z = value;
  165. break;
  166. case HID_DG_CONTACTID:
  167. sd->id = value;
  168. break;
  169. case HID_DG_CONFIDENCE:
  170. sd->valid = !!value;
  171. break;
  172. case 0xff000002:
  173. /* this comes only before the first finger */
  174. sd->first = true;
  175. break;
  176. default:
  177. /* ignore the others */
  178. return 1;
  179. }
  180. }
  181. /* we have handled the hidinput part, now remains hiddev */
  182. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  183. hid->hiddev_hid_event(hid, field, usage, value);
  184. return 1;
  185. }
  186. static int stantum_probe(struct hid_device *hdev,
  187. const struct hid_device_id *id)
  188. {
  189. int ret;
  190. struct stantum_data *sd;
  191. sd = kmalloc(sizeof(struct stantum_data), GFP_KERNEL);
  192. if (!sd) {
  193. dev_err(&hdev->dev, "cannot allocate Stantum data\n");
  194. return -ENOMEM;
  195. }
  196. sd->valid = false;
  197. sd->first = false;
  198. sd->activity = false;
  199. hid_set_drvdata(hdev, sd);
  200. ret = hid_parse(hdev);
  201. if (!ret)
  202. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  203. if (ret)
  204. kfree(sd);
  205. return ret;
  206. }
  207. static void stantum_remove(struct hid_device *hdev)
  208. {
  209. hid_hw_stop(hdev);
  210. kfree(hid_get_drvdata(hdev));
  211. hid_set_drvdata(hdev, NULL);
  212. }
  213. static const struct hid_device_id stantum_devices[] = {
  214. { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) },
  215. { }
  216. };
  217. MODULE_DEVICE_TABLE(hid, stantum_devices);
  218. static const struct hid_usage_id stantum_grabbed_usages[] = {
  219. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  220. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  221. };
  222. static struct hid_driver stantum_driver = {
  223. .name = "stantum",
  224. .id_table = stantum_devices,
  225. .probe = stantum_probe,
  226. .remove = stantum_remove,
  227. .input_mapping = stantum_input_mapping,
  228. .input_mapped = stantum_input_mapped,
  229. .usage_table = stantum_grabbed_usages,
  230. .event = stantum_event,
  231. };
  232. static int __init stantum_init(void)
  233. {
  234. return hid_register_driver(&stantum_driver);
  235. }
  236. static void __exit stantum_exit(void)
  237. {
  238. hid_unregister_driver(&stantum_driver);
  239. }
  240. module_init(stantum_init);
  241. module_exit(stantum_exit);