hid-egalax.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * HID driver for eGalax dual-touch panels
  3. *
  4. * Copyright (c) 2010 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/usb.h>
  17. #include <linux/slab.h>
  18. #include "usbhid/usbhid.h"
  19. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  20. MODULE_DESCRIPTION("eGalax dual-touch panel");
  21. MODULE_LICENSE("GPL");
  22. #include "hid-ids.h"
  23. struct egalax_data {
  24. __u16 x, y, z;
  25. __u8 id;
  26. bool first; /* is this the first finger in the frame? */
  27. bool valid; /* valid finger data, or just placeholder? */
  28. bool activity; /* at least one active finger previously? */
  29. __u16 lastx, lasty; /* latest valid (x, y) in the frame */
  30. };
  31. static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  32. struct hid_field *field, struct hid_usage *usage,
  33. unsigned long **bit, int *max)
  34. {
  35. switch (usage->hid & HID_USAGE_PAGE) {
  36. case HID_UP_GENDESK:
  37. switch (usage->hid) {
  38. case HID_GD_X:
  39. hid_map_usage(hi, usage, bit, max,
  40. EV_ABS, ABS_MT_POSITION_X);
  41. /* touchscreen emulation */
  42. input_set_abs_params(hi->input, ABS_X,
  43. field->logical_minimum,
  44. field->logical_maximum, 0, 0);
  45. return 1;
  46. case HID_GD_Y:
  47. hid_map_usage(hi, usage, bit, max,
  48. EV_ABS, ABS_MT_POSITION_Y);
  49. /* touchscreen emulation */
  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. case HID_DG_TIPSWITCH:
  59. /* touchscreen emulation */
  60. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  61. return 1;
  62. case HID_DG_INRANGE:
  63. case HID_DG_CONFIDENCE:
  64. case HID_DG_CONTACTCOUNT:
  65. case HID_DG_CONTACTMAX:
  66. return -1;
  67. case HID_DG_CONTACTID:
  68. hid_map_usage(hi, usage, bit, max,
  69. EV_ABS, ABS_MT_TRACKING_ID);
  70. return 1;
  71. case HID_DG_TIPPRESSURE:
  72. hid_map_usage(hi, usage, bit, max,
  73. EV_ABS, ABS_MT_PRESSURE);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. /* ignore others (from other reports we won't get anyway) */
  79. return -1;
  80. }
  81. static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  82. struct hid_field *field, struct hid_usage *usage,
  83. unsigned long **bit, int *max)
  84. {
  85. if (usage->type == EV_KEY || usage->type == EV_ABS)
  86. clear_bit(usage->code, *bit);
  87. return 0;
  88. }
  89. /*
  90. * this function is called when a whole finger has been parsed,
  91. * so that it can decide what to send to the input layer.
  92. */
  93. static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
  94. {
  95. td->first = !td->first; /* touchscreen emulation */
  96. if (td->valid) {
  97. /* emit multitouch events */
  98. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
  99. input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
  100. input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
  101. input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
  102. input_mt_sync(input);
  103. /*
  104. * touchscreen emulation: store (x, y) as
  105. * the last valid values in this frame
  106. */
  107. td->lastx = td->x;
  108. td->lasty = td->y;
  109. }
  110. /*
  111. * touchscreen emulation: if this is the second finger and at least
  112. * one in this frame is valid, the latest valid in the frame is
  113. * the oldest on the panel, the one we want for single touch
  114. */
  115. if (!td->first && td->activity) {
  116. input_event(input, EV_ABS, ABS_X, td->lastx);
  117. input_event(input, EV_ABS, ABS_Y, td->lasty);
  118. }
  119. if (!td->valid) {
  120. /*
  121. * touchscreen emulation: if the first finger is invalid
  122. * and there previously was finger activity, this is a release
  123. */
  124. if (td->first && td->activity) {
  125. input_event(input, EV_KEY, BTN_TOUCH, 0);
  126. td->activity = false;
  127. }
  128. return;
  129. }
  130. /* touchscreen emulation: if no previous activity, emit touch event */
  131. if (!td->activity) {
  132. input_event(input, EV_KEY, BTN_TOUCH, 1);
  133. td->activity = true;
  134. }
  135. }
  136. static int egalax_event(struct hid_device *hid, struct hid_field *field,
  137. struct hid_usage *usage, __s32 value)
  138. {
  139. struct egalax_data *td = hid_get_drvdata(hid);
  140. if (hid->claimed & HID_CLAIMED_INPUT) {
  141. struct input_dev *input = field->hidinput->input;
  142. switch (usage->hid) {
  143. case HID_DG_INRANGE:
  144. case HID_DG_CONFIDENCE:
  145. /* avoid interference from generic hidinput handling */
  146. break;
  147. case HID_DG_TIPSWITCH:
  148. td->valid = value;
  149. break;
  150. case HID_DG_TIPPRESSURE:
  151. td->z = value;
  152. break;
  153. case HID_DG_CONTACTID:
  154. td->id = value;
  155. break;
  156. case HID_GD_X:
  157. td->x = value;
  158. break;
  159. case HID_GD_Y:
  160. td->y = value;
  161. /* this is the last field in a finger */
  162. egalax_filter_event(td, input);
  163. break;
  164. case HID_DG_CONTACTCOUNT:
  165. /* touch emulation: this is the last field in a frame */
  166. td->first = false;
  167. break;
  168. default:
  169. /* fallback to the generic hidinput handling */
  170. return 0;
  171. }
  172. }
  173. /* we have handled the hidinput part, now remains hiddev */
  174. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  175. hid->hiddev_hid_event(hid, field, usage, value);
  176. return 1;
  177. }
  178. static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  179. {
  180. int ret;
  181. struct egalax_data *td;
  182. struct hid_report *report;
  183. td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
  184. if (!td) {
  185. dev_err(&hdev->dev, "cannot allocate eGalax data\n");
  186. return -ENOMEM;
  187. }
  188. hid_set_drvdata(hdev, td);
  189. ret = hid_parse(hdev);
  190. if (ret)
  191. goto end;
  192. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  193. if (ret)
  194. goto end;
  195. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
  196. if (report) {
  197. report->field[0]->value[0] = 2;
  198. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  199. }
  200. end:
  201. if (ret)
  202. kfree(td);
  203. return ret;
  204. }
  205. static void egalax_remove(struct hid_device *hdev)
  206. {
  207. hid_hw_stop(hdev);
  208. kfree(hid_get_drvdata(hdev));
  209. hid_set_drvdata(hdev, NULL);
  210. }
  211. static const struct hid_device_id egalax_devices[] = {
  212. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  213. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
  214. { }
  215. };
  216. MODULE_DEVICE_TABLE(hid, egalax_devices);
  217. static const struct hid_usage_id egalax_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 egalax_driver = {
  222. .name = "egalax-touch",
  223. .id_table = egalax_devices,
  224. .probe = egalax_probe,
  225. .remove = egalax_remove,
  226. .input_mapping = egalax_input_mapping,
  227. .input_mapped = egalax_input_mapped,
  228. .usage_table = egalax_grabbed_usages,
  229. .event = egalax_event,
  230. };
  231. static int __init egalax_init(void)
  232. {
  233. return hid_register_driver(&egalax_driver);
  234. }
  235. static void __exit egalax_exit(void)
  236. {
  237. hid_unregister_driver(&egalax_driver);
  238. }
  239. module_init(egalax_init);
  240. module_exit(egalax_exit);