hid-egalax.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * HID driver for eGalax dual-touch panels
  3. *
  4. * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
  5. * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
  6. * Copyright (c) 2010 Canonical, Ltd.
  7. *
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/hid.h>
  17. #include <linux/module.h>
  18. #include <linux/usb.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/slab.h>
  21. #include "usbhid/usbhid.h"
  22. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  23. MODULE_DESCRIPTION("eGalax dual-touch panel");
  24. MODULE_LICENSE("GPL");
  25. #include "hid-ids.h"
  26. #define MAX_SLOTS 2
  27. /* estimated signal-to-noise ratios */
  28. #define SN_MOVE 4096
  29. #define SN_PRESSURE 32
  30. struct egalax_data {
  31. int valid;
  32. int slot;
  33. int touch;
  34. int x, y, z;
  35. };
  36. static void set_abs(struct input_dev *input, unsigned int code,
  37. struct hid_field *field, int snratio)
  38. {
  39. int fmin = field->logical_minimum;
  40. int fmax = field->logical_maximum;
  41. int fuzz = snratio ? (fmax - fmin) / snratio : 0;
  42. input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
  43. }
  44. static int egalax_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  45. struct hid_field *field, struct hid_usage *usage,
  46. unsigned long **bit, int *max)
  47. {
  48. struct input_dev *input = hi->input;
  49. switch (usage->hid & HID_USAGE_PAGE) {
  50. case HID_UP_GENDESK:
  51. switch (usage->hid) {
  52. case HID_GD_X:
  53. field->logical_maximum = 32760;
  54. hid_map_usage(hi, usage, bit, max,
  55. EV_ABS, ABS_MT_POSITION_X);
  56. set_abs(input, ABS_MT_POSITION_X, field, SN_MOVE);
  57. /* touchscreen emulation */
  58. set_abs(input, ABS_X, field, SN_MOVE);
  59. return 1;
  60. case HID_GD_Y:
  61. field->logical_maximum = 32760;
  62. hid_map_usage(hi, usage, bit, max,
  63. EV_ABS, ABS_MT_POSITION_Y);
  64. set_abs(input, ABS_MT_POSITION_Y, field, SN_MOVE);
  65. /* touchscreen emulation */
  66. set_abs(input, ABS_Y, field, SN_MOVE);
  67. return 1;
  68. }
  69. return 0;
  70. case HID_UP_DIGITIZER:
  71. switch (usage->hid) {
  72. case HID_DG_TIPSWITCH:
  73. /* touchscreen emulation */
  74. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  75. input_set_capability(input, EV_KEY, BTN_TOUCH);
  76. return 1;
  77. case HID_DG_INRANGE:
  78. case HID_DG_CONFIDENCE:
  79. case HID_DG_CONTACTCOUNT:
  80. case HID_DG_CONTACTMAX:
  81. return -1;
  82. case HID_DG_CONTACTID:
  83. input_mt_init_slots(input, MAX_SLOTS);
  84. return 1;
  85. case HID_DG_TIPPRESSURE:
  86. field->logical_minimum = 0;
  87. hid_map_usage(hi, usage, bit, max,
  88. EV_ABS, ABS_MT_PRESSURE);
  89. set_abs(input, ABS_MT_PRESSURE, field, SN_PRESSURE);
  90. /* touchscreen emulation */
  91. set_abs(input, ABS_PRESSURE, field, SN_PRESSURE);
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. /* ignore others (from other reports we won't get anyway) */
  97. return -1;
  98. }
  99. static int egalax_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  100. struct hid_field *field, struct hid_usage *usage,
  101. unsigned long **bit, int *max)
  102. {
  103. /* tell hid-input to skip setup of these event types */
  104. if (usage->type == EV_KEY || usage->type == EV_ABS)
  105. set_bit(usage->type, hi->input->evbit);
  106. return -1;
  107. }
  108. /*
  109. * this function is called when a whole finger has been parsed,
  110. * so that it can decide what to send to the input layer.
  111. */
  112. static void egalax_filter_event(struct egalax_data *td, struct input_dev *input)
  113. {
  114. input_mt_slot(input, td->slot);
  115. input_mt_report_slot_state(input, MT_TOOL_FINGER, td->touch);
  116. if (td->touch) {
  117. input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
  118. input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
  119. input_event(input, EV_ABS, ABS_MT_PRESSURE, td->z);
  120. }
  121. input_mt_report_pointer_emulation(input, true);
  122. }
  123. static int egalax_event(struct hid_device *hid, struct hid_field *field,
  124. struct hid_usage *usage, __s32 value)
  125. {
  126. struct egalax_data *td = hid_get_drvdata(hid);
  127. /* Note, eGalax has two product lines: the first is resistive and
  128. * uses a standard parallel multitouch protocol (product ID ==
  129. * 48xx). The second is capacitive and uses an unusual "serial"
  130. * protocol with a different message for each multitouch finger
  131. * (product ID == 72xx).
  132. */
  133. if (hid->claimed & HID_CLAIMED_INPUT) {
  134. struct input_dev *input = field->hidinput->input;
  135. switch (usage->hid) {
  136. case HID_DG_INRANGE:
  137. td->valid = value;
  138. break;
  139. case HID_DG_CONFIDENCE:
  140. /* avoid interference from generic hidinput handling */
  141. break;
  142. case HID_DG_TIPSWITCH:
  143. td->touch = value;
  144. break;
  145. case HID_DG_TIPPRESSURE:
  146. td->z = value;
  147. break;
  148. case HID_DG_CONTACTID:
  149. td->slot = clamp_val(value, 0, MAX_SLOTS - 1);
  150. break;
  151. case HID_GD_X:
  152. td->x = value;
  153. break;
  154. case HID_GD_Y:
  155. td->y = value;
  156. /* this is the last field in a finger */
  157. if (td->valid)
  158. egalax_filter_event(td, input);
  159. break;
  160. case HID_DG_CONTACTCOUNT:
  161. /* touch emulation: this is the last field in a frame */
  162. break;
  163. default:
  164. /* fallback to the generic hidinput handling */
  165. return 0;
  166. }
  167. }
  168. /* we have handled the hidinput part, now remains hiddev */
  169. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  170. hid->hiddev_hid_event(hid, field, usage, value);
  171. return 1;
  172. }
  173. static int egalax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  174. {
  175. int ret;
  176. struct egalax_data *td;
  177. struct hid_report *report;
  178. td = kzalloc(sizeof(struct egalax_data), GFP_KERNEL);
  179. if (!td) {
  180. hid_err(hdev, "cannot allocate eGalax data\n");
  181. return -ENOMEM;
  182. }
  183. hid_set_drvdata(hdev, td);
  184. ret = hid_parse(hdev);
  185. if (ret)
  186. goto end;
  187. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  188. if (ret)
  189. goto end;
  190. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[5];
  191. if (report) {
  192. report->field[0]->value[0] = 2;
  193. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  194. }
  195. end:
  196. if (ret)
  197. kfree(td);
  198. return ret;
  199. }
  200. static void egalax_remove(struct hid_device *hdev)
  201. {
  202. hid_hw_stop(hdev);
  203. kfree(hid_get_drvdata(hdev));
  204. hid_set_drvdata(hdev, NULL);
  205. }
  206. static const struct hid_device_id egalax_devices[] = {
  207. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  208. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
  209. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  210. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
  211. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  212. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
  213. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  214. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
  215. { HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
  216. USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
  217. { }
  218. };
  219. MODULE_DEVICE_TABLE(hid, egalax_devices);
  220. static const struct hid_usage_id egalax_grabbed_usages[] = {
  221. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  222. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  223. };
  224. static struct hid_driver egalax_driver = {
  225. .name = "egalax-touch",
  226. .id_table = egalax_devices,
  227. .probe = egalax_probe,
  228. .remove = egalax_remove,
  229. .input_mapping = egalax_input_mapping,
  230. .input_mapped = egalax_input_mapped,
  231. .usage_table = egalax_grabbed_usages,
  232. .event = egalax_event,
  233. };
  234. static int __init egalax_init(void)
  235. {
  236. return hid_register_driver(&egalax_driver);
  237. }
  238. static void __exit egalax_exit(void)
  239. {
  240. hid_unregister_driver(&egalax_driver);
  241. }
  242. module_init(egalax_init);
  243. module_exit(egalax_exit);