hid-egalax.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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, lastz; /* latest valid (x, y, z) 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. /* touchscreen emulation */
  75. input_set_abs_params(hi->input, ABS_PRESSURE,
  76. field->logical_minimum,
  77. field->logical_maximum, 0, 0);
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. /* ignore others (from other reports we won't get anyway) */
  83. return -1;
  84. }
  85. static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  86. struct hid_field *field, struct hid_usage *usage,
  87. unsigned long **bit, int *max)
  88. {
  89. if (usage->type == EV_KEY || usage->type == EV_ABS)
  90. clear_bit(usage->code, *bit);
  91. return 0;
  92. }
  93. /*
  94. * this function is called when a whole finger has been parsed,
  95. * so that it can decide what to send to the input layer.
  96. */
  97. static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
  98. {
  99. td->first = !td->first; /* touchscreen emulation */
  100. if (td->valid) {
  101. /* emit multitouch events */
  102. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
  103. input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x >> 3);
  104. input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y >> 3);
  105. input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
  106. input_mt_sync(input);
  107. /*
  108. * touchscreen emulation: store (x, y) as
  109. * the last valid values in this frame
  110. */
  111. td->lastx = td->x;
  112. td->lasty = td->y;
  113. td->lastz = td->z;
  114. }
  115. /*
  116. * touchscreen emulation: if this is the second finger and at least
  117. * one in this frame is valid, the latest valid in the frame is
  118. * the oldest on the panel, the one we want for single touch
  119. */
  120. if (!td->first && td->activity) {
  121. input_event(input, EV_ABS, ABS_X, td->lastx >> 3);
  122. input_event(input, EV_ABS, ABS_Y, td->lasty >> 3);
  123. input_event(input, EV_ABS, ABS_PRESSURE, td->lastz);
  124. }
  125. if (!td->valid) {
  126. /*
  127. * touchscreen emulation: if the first finger is invalid
  128. * and there previously was finger activity, this is a release
  129. */
  130. if (td->first && td->activity) {
  131. input_event(input, EV_KEY, BTN_TOUCH, 0);
  132. td->activity = false;
  133. }
  134. return;
  135. }
  136. /* touchscreen emulation: if no previous activity, emit touch event */
  137. if (!td->activity) {
  138. input_event(input, EV_KEY, BTN_TOUCH, 1);
  139. td->activity = true;
  140. }
  141. }
  142. static int egalax_event(struct hid_device *hid, struct hid_field *field,
  143. struct hid_usage *usage, __s32 value)
  144. {
  145. struct egalax_data *td = hid_get_drvdata(hid);
  146. /* Note, eGalax has two product lines: the first is resistive and
  147. * uses a standard parallel multitouch protocol (product ID ==
  148. * 48xx). The second is capacitive and uses an unusual "serial"
  149. * protocol with a different message for each multitouch finger
  150. * (product ID == 72xx). We do not yet generate a correct event
  151. * sequence for the capacitive/serial protocol.
  152. */
  153. if (hid->claimed & HID_CLAIMED_INPUT) {
  154. struct input_dev *input = field->hidinput->input;
  155. switch (usage->hid) {
  156. case HID_DG_INRANGE:
  157. case HID_DG_CONFIDENCE:
  158. /* avoid interference from generic hidinput handling */
  159. break;
  160. case HID_DG_TIPSWITCH:
  161. td->valid = value;
  162. break;
  163. case HID_DG_TIPPRESSURE:
  164. td->z = value;
  165. break;
  166. case HID_DG_CONTACTID:
  167. td->id = value;
  168. break;
  169. case HID_GD_X:
  170. td->x = value;
  171. break;
  172. case HID_GD_Y:
  173. td->y = value;
  174. /* this is the last field in a finger */
  175. egalax_filter_event(td, input);
  176. break;
  177. case HID_DG_CONTACTCOUNT:
  178. /* touch emulation: this is the last field in a frame */
  179. td->first = false;
  180. break;
  181. default:
  182. /* fallback to the generic hidinput handling */
  183. return 0;
  184. }
  185. }
  186. /* we have handled the hidinput part, now remains hiddev */
  187. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  188. hid->hiddev_hid_event(hid, field, usage, value);
  189. return 1;
  190. }
  191. static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  192. {
  193. int ret;
  194. struct egalax_data *td;
  195. struct hid_report *report;
  196. td = kzalloc(sizeof(struct egalax_data), GFP_KERNEL);
  197. if (!td) {
  198. dev_err(&hdev->dev, "cannot allocate eGalax data\n");
  199. return -ENOMEM;
  200. }
  201. hid_set_drvdata(hdev, td);
  202. ret = hid_parse(hdev);
  203. if (ret)
  204. goto end;
  205. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  206. if (ret)
  207. goto end;
  208. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
  209. if (report) {
  210. report->field[0]->value[0] = 2;
  211. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  212. }
  213. end:
  214. if (ret)
  215. kfree(td);
  216. return ret;
  217. }
  218. static void egalax_remove(struct hid_device *hdev)
  219. {
  220. hid_hw_stop(hdev);
  221. kfree(hid_get_drvdata(hdev));
  222. hid_set_drvdata(hdev, NULL);
  223. }
  224. static const struct hid_device_id egalax_devices[] = {
  225. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  226. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
  227. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  228. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(hid, egalax_devices);
  232. static const struct hid_usage_id egalax_grabbed_usages[] = {
  233. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  234. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  235. };
  236. static struct hid_driver egalax_driver = {
  237. .name = "egalax-touch",
  238. .id_table = egalax_devices,
  239. .probe = egalax_probe,
  240. .remove = egalax_remove,
  241. .input_mapping = egalax_input_mapping,
  242. .input_mapped = egalax_input_mapped,
  243. .usage_table = egalax_grabbed_usages,
  244. .event = egalax_event,
  245. };
  246. static int __init egalax_init(void)
  247. {
  248. return hid_register_driver(&egalax_driver);
  249. }
  250. static void __exit egalax_exit(void)
  251. {
  252. hid_unregister_driver(&egalax_driver);
  253. }
  254. module_init(egalax_init);
  255. module_exit(egalax_exit);