hid-egalax.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /* Note, eGalax has two product lines: the first is resistive and
  141. * uses a standard parallel multitouch protocol (product ID ==
  142. * 48xx). The second is capacitive and uses an unusual "serial"
  143. * protocol with a different message for each multitouch finger
  144. * (product ID == 72xx). We do not yet generate a correct event
  145. * sequence for the capacitive/serial protocol.
  146. */
  147. if (hid->claimed & HID_CLAIMED_INPUT) {
  148. struct input_dev *input = field->hidinput->input;
  149. switch (usage->hid) {
  150. case HID_DG_INRANGE:
  151. case HID_DG_CONFIDENCE:
  152. /* avoid interference from generic hidinput handling */
  153. break;
  154. case HID_DG_TIPSWITCH:
  155. td->valid = value;
  156. break;
  157. case HID_DG_TIPPRESSURE:
  158. td->z = value;
  159. break;
  160. case HID_DG_CONTACTID:
  161. td->id = value;
  162. break;
  163. case HID_GD_X:
  164. td->x = value;
  165. break;
  166. case HID_GD_Y:
  167. td->y = value;
  168. /* this is the last field in a finger */
  169. egalax_filter_event(td, input);
  170. break;
  171. case HID_DG_CONTACTCOUNT:
  172. /* touch emulation: this is the last field in a frame */
  173. td->first = false;
  174. break;
  175. default:
  176. /* fallback to the generic hidinput handling */
  177. return 0;
  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 egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  186. {
  187. int ret;
  188. struct egalax_data *td;
  189. struct hid_report *report;
  190. td = kmalloc(sizeof(struct egalax_data), GFP_KERNEL);
  191. if (!td) {
  192. dev_err(&hdev->dev, "cannot allocate eGalax data\n");
  193. return -ENOMEM;
  194. }
  195. hid_set_drvdata(hdev, td);
  196. ret = hid_parse(hdev);
  197. if (ret)
  198. goto end;
  199. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  200. if (ret)
  201. goto end;
  202. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
  203. if (report) {
  204. report->field[0]->value[0] = 2;
  205. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  206. }
  207. end:
  208. if (ret)
  209. kfree(td);
  210. return ret;
  211. }
  212. static void egalax_remove(struct hid_device *hdev)
  213. {
  214. hid_hw_stop(hdev);
  215. kfree(hid_get_drvdata(hdev));
  216. hid_set_drvdata(hdev, NULL);
  217. }
  218. static const struct hid_device_id egalax_devices[] = {
  219. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  220. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
  221. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  222. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(hid, egalax_devices);
  226. static const struct hid_usage_id egalax_grabbed_usages[] = {
  227. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  228. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  229. };
  230. static struct hid_driver egalax_driver = {
  231. .name = "egalax-touch",
  232. .id_table = egalax_devices,
  233. .probe = egalax_probe,
  234. .remove = egalax_remove,
  235. .input_mapping = egalax_input_mapping,
  236. .input_mapped = egalax_input_mapped,
  237. .usage_table = egalax_grabbed_usages,
  238. .event = egalax_event,
  239. };
  240. static int __init egalax_init(void)
  241. {
  242. return hid_register_driver(&egalax_driver);
  243. }
  244. static void __exit egalax_exit(void)
  245. {
  246. hid_unregister_driver(&egalax_driver);
  247. }
  248. module_init(egalax_init);
  249. module_exit(egalax_exit);