hid-stantum.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include <linux/slab.h>
  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. return -1;
  62. case HID_DG_TIPSWITCH:
  63. /* touchscreen emulation */
  64. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  65. return 1;
  66. case HID_DG_WIDTH:
  67. hid_map_usage(hi, usage, bit, max,
  68. EV_ABS, ABS_MT_TOUCH_MAJOR);
  69. return 1;
  70. case HID_DG_HEIGHT:
  71. hid_map_usage(hi, usage, bit, max,
  72. EV_ABS, ABS_MT_TOUCH_MINOR);
  73. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  74. 1, 1, 0, 0);
  75. return 1;
  76. case HID_DG_TIPPRESSURE:
  77. hid_map_usage(hi, usage, bit, max,
  78. EV_ABS, ABS_MT_PRESSURE);
  79. return 1;
  80. case HID_DG_CONTACTID:
  81. hid_map_usage(hi, usage, bit, max,
  82. EV_ABS, ABS_MT_TRACKING_ID);
  83. return 1;
  84. }
  85. return 0;
  86. case 0xff000000:
  87. /* no input-oriented meaning */
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. static int stantum_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  93. struct hid_field *field, struct hid_usage *usage,
  94. unsigned long **bit, int *max)
  95. {
  96. if (usage->type == EV_KEY || usage->type == EV_ABS)
  97. clear_bit(usage->code, *bit);
  98. return 0;
  99. }
  100. /*
  101. * this function is called when a whole finger has been parsed,
  102. * so that it can decide what to send to the input layer.
  103. */
  104. static void stantum_filter_event(struct stantum_data *sd,
  105. struct input_dev *input)
  106. {
  107. bool wide;
  108. if (!sd->valid) {
  109. /*
  110. * touchscreen emulation: if the first finger is not valid and
  111. * there previously was finger activity, this is a release
  112. */
  113. if (sd->first && sd->activity) {
  114. input_event(input, EV_KEY, BTN_TOUCH, 0);
  115. sd->activity = false;
  116. }
  117. return;
  118. }
  119. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, sd->id);
  120. input_event(input, EV_ABS, ABS_MT_POSITION_X, sd->x);
  121. input_event(input, EV_ABS, ABS_MT_POSITION_Y, sd->y);
  122. wide = (sd->w > sd->h);
  123. input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
  124. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, wide ? sd->w : sd->h);
  125. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, wide ? sd->h : sd->w);
  126. input_event(input, EV_ABS, ABS_MT_PRESSURE, sd->z);
  127. input_mt_sync(input);
  128. sd->valid = false;
  129. /* touchscreen emulation */
  130. if (sd->first) {
  131. if (!sd->activity) {
  132. input_event(input, EV_KEY, BTN_TOUCH, 1);
  133. sd->activity = true;
  134. }
  135. input_event(input, EV_ABS, ABS_X, sd->x);
  136. input_event(input, EV_ABS, ABS_Y, sd->y);
  137. }
  138. sd->first = false;
  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. hid_err(hdev, "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. { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM, USB_DEVICE_ID_MTP_STM) },
  216. { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX, USB_DEVICE_ID_MTP_SITRONIX) },
  217. { }
  218. };
  219. MODULE_DEVICE_TABLE(hid, stantum_devices);
  220. static const struct hid_usage_id stantum_grabbed_usages[] = {
  221. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  222. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  223. };
  224. static struct hid_driver stantum_driver = {
  225. .name = "stantum",
  226. .id_table = stantum_devices,
  227. .probe = stantum_probe,
  228. .remove = stantum_remove,
  229. .input_mapping = stantum_input_mapping,
  230. .input_mapped = stantum_input_mapped,
  231. .usage_table = stantum_grabbed_usages,
  232. .event = stantum_event,
  233. };
  234. static int __init stantum_init(void)
  235. {
  236. return hid_register_driver(&stantum_driver);
  237. }
  238. static void __exit stantum_exit(void)
  239. {
  240. hid_unregister_driver(&stantum_driver);
  241. }
  242. module_init(stantum_init);
  243. module_exit(stantum_exit);