hid-egalax.c 6.7 KB

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