hid-egalax.c 7.9 KB

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