hid-quanta.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * HID driver for Quanta Optical Touch dual-touch panels
  3. *
  4. * Copyright (c) 2009-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. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  17. MODULE_DESCRIPTION("Quanta dual-touch panel");
  18. MODULE_LICENSE("GPL");
  19. #include "hid-ids.h"
  20. struct quanta_data {
  21. __u16 x, y;
  22. __u8 id;
  23. bool valid; /* valid finger data, or just placeholder? */
  24. bool first; /* is this the first finger in this frame? */
  25. bool activity_now; /* at least one active finger in this frame? */
  26. bool activity; /* at least one active finger previously? */
  27. };
  28. static int quanta_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  29. struct hid_field *field, struct hid_usage *usage,
  30. unsigned long **bit, int *max)
  31. {
  32. switch (usage->hid & HID_USAGE_PAGE) {
  33. case HID_UP_GENDESK:
  34. switch (usage->hid) {
  35. case HID_GD_X:
  36. hid_map_usage(hi, usage, bit, max,
  37. EV_ABS, ABS_MT_POSITION_X);
  38. /* touchscreen emulation */
  39. input_set_abs_params(hi->input, ABS_X,
  40. field->logical_minimum,
  41. field->logical_maximum, 0, 0);
  42. return 1;
  43. case HID_GD_Y:
  44. hid_map_usage(hi, usage, bit, max,
  45. EV_ABS, ABS_MT_POSITION_Y);
  46. /* touchscreen emulation */
  47. input_set_abs_params(hi->input, ABS_Y,
  48. field->logical_minimum,
  49. field->logical_maximum, 0, 0);
  50. return 1;
  51. }
  52. return 0;
  53. case HID_UP_DIGITIZER:
  54. switch (usage->hid) {
  55. case HID_DG_CONFIDENCE:
  56. case HID_DG_TIPSWITCH:
  57. case HID_DG_INPUTMODE:
  58. case HID_DG_DEVICEINDEX:
  59. case HID_DG_CONTACTCOUNT:
  60. case HID_DG_CONTACTMAX:
  61. case HID_DG_TIPPRESSURE:
  62. case HID_DG_WIDTH:
  63. case HID_DG_HEIGHT:
  64. return -1;
  65. case HID_DG_INRANGE:
  66. /* touchscreen emulation */
  67. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  68. return 1;
  69. case HID_DG_CONTACTID:
  70. hid_map_usage(hi, usage, bit, max,
  71. EV_ABS, ABS_MT_TRACKING_ID);
  72. return 1;
  73. }
  74. return 0;
  75. case 0xff000000:
  76. /* ignore vendor-specific features */
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. static int quanta_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 quanta_filter_event(struct quanta_data *td, struct input_dev *input)
  94. {
  95. td->first = !td->first; /* touchscreen emulation */
  96. if (!td->valid) {
  97. /*
  98. * touchscreen emulation: if no finger in this frame is valid
  99. * and there previously was finger activity, this is a release
  100. */
  101. if (!td->first && !td->activity_now && td->activity) {
  102. input_event(input, EV_KEY, BTN_TOUCH, 0);
  103. td->activity = false;
  104. }
  105. return;
  106. }
  107. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
  108. input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
  109. input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
  110. input_mt_sync(input);
  111. td->valid = false;
  112. /* touchscreen emulation: if first active finger in this frame... */
  113. if (!td->activity_now) {
  114. /* if there was no previous activity, emit touch event */
  115. if (!td->activity) {
  116. input_event(input, EV_KEY, BTN_TOUCH, 1);
  117. td->activity = true;
  118. }
  119. td->activity_now = true;
  120. /* and in any case this is our preferred finger */
  121. input_event(input, EV_ABS, ABS_X, td->x);
  122. input_event(input, EV_ABS, ABS_Y, td->y);
  123. }
  124. }
  125. static int quanta_event(struct hid_device *hid, struct hid_field *field,
  126. struct hid_usage *usage, __s32 value)
  127. {
  128. struct quanta_data *td = hid_get_drvdata(hid);
  129. if (hid->claimed & HID_CLAIMED_INPUT) {
  130. struct input_dev *input = field->hidinput->input;
  131. switch (usage->hid) {
  132. case HID_DG_INRANGE:
  133. td->valid = !!value;
  134. break;
  135. case HID_GD_X:
  136. td->x = value;
  137. break;
  138. case HID_GD_Y:
  139. td->y = value;
  140. quanta_filter_event(td, input);
  141. break;
  142. case HID_DG_CONTACTID:
  143. td->id = value;
  144. break;
  145. case HID_DG_CONTACTCOUNT:
  146. /* touch emulation: this is the last field in a frame */
  147. td->first = false;
  148. td->activity_now = false;
  149. break;
  150. case HID_DG_CONFIDENCE:
  151. case HID_DG_TIPSWITCH:
  152. /* avoid interference from generic hidinput handling */
  153. break;
  154. default:
  155. /* fallback to the generic hidinput handling */
  156. return 0;
  157. }
  158. }
  159. /* we have handled the hidinput part, now remains hiddev */
  160. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  161. hid->hiddev_hid_event(hid, field, usage, value);
  162. return 1;
  163. }
  164. static int quanta_probe(struct hid_device *hdev, const struct hid_device_id *id)
  165. {
  166. int ret;
  167. struct quanta_data *td;
  168. td = kmalloc(sizeof(struct quanta_data), GFP_KERNEL);
  169. if (!td) {
  170. dev_err(&hdev->dev, "cannot allocate Quanta Touch data\n");
  171. return -ENOMEM;
  172. }
  173. td->valid = false;
  174. td->activity = false;
  175. td->activity_now = false;
  176. td->first = false;
  177. hid_set_drvdata(hdev, td);
  178. ret = hid_parse(hdev);
  179. if (!ret)
  180. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  181. if (ret)
  182. kfree(td);
  183. return ret;
  184. }
  185. static void quanta_remove(struct hid_device *hdev)
  186. {
  187. hid_hw_stop(hdev);
  188. kfree(hid_get_drvdata(hdev));
  189. hid_set_drvdata(hdev, NULL);
  190. }
  191. static const struct hid_device_id quanta_devices[] = {
  192. { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
  193. USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) },
  194. { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA,
  195. USB_DEVICE_ID_PIXART_IMAGING_INC_OPTICAL_TOUCH_SCREEN) },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(hid, quanta_devices);
  199. static const struct hid_usage_id quanta_grabbed_usages[] = {
  200. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  201. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  202. };
  203. static struct hid_driver quanta_driver = {
  204. .name = "quanta-touch",
  205. .id_table = quanta_devices,
  206. .probe = quanta_probe,
  207. .remove = quanta_remove,
  208. .input_mapping = quanta_input_mapping,
  209. .input_mapped = quanta_input_mapped,
  210. .usage_table = quanta_grabbed_usages,
  211. .event = quanta_event,
  212. };
  213. static int __init quanta_init(void)
  214. {
  215. return hid_register_driver(&quanta_driver);
  216. }
  217. static void __exit quanta_exit(void)
  218. {
  219. hid_unregister_driver(&quanta_driver);
  220. }
  221. module_init(quanta_init);
  222. module_exit(quanta_exit);