hid-egalax.c 7.8 KB

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