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