hid-quanta.c 6.3 KB

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