hid-cando.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * HID driver for Cando 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/slab.h>
  17. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  18. MODULE_DESCRIPTION("Cando dual-touch panel");
  19. MODULE_LICENSE("GPL");
  20. #include "hid-ids.h"
  21. struct cando_data {
  22. __u16 x, y;
  23. __u8 id;
  24. __s8 oldest; /* id of the oldest finger in previous frame */
  25. bool valid; /* valid finger data, or just placeholder? */
  26. bool first; /* is this the first finger in this frame? */
  27. __s8 firstid; /* id of the first finger in the frame */
  28. __u16 firstx, firsty; /* (x, y) of the first finger in the frame */
  29. };
  30. static int cando_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  31. struct hid_field *field, struct hid_usage *usage,
  32. unsigned long **bit, int *max)
  33. {
  34. switch (usage->hid & HID_USAGE_PAGE) {
  35. case HID_UP_GENDESK:
  36. switch (usage->hid) {
  37. case HID_GD_X:
  38. hid_map_usage(hi, usage, bit, max,
  39. EV_ABS, ABS_MT_POSITION_X);
  40. /* touchscreen emulation */
  41. input_set_abs_params(hi->input, ABS_X,
  42. field->logical_minimum,
  43. field->logical_maximum, 0, 0);
  44. return 1;
  45. case HID_GD_Y:
  46. hid_map_usage(hi, usage, bit, max,
  47. EV_ABS, ABS_MT_POSITION_Y);
  48. /* touchscreen emulation */
  49. input_set_abs_params(hi->input, ABS_Y,
  50. field->logical_minimum,
  51. field->logical_maximum, 0, 0);
  52. return 1;
  53. }
  54. return 0;
  55. case HID_UP_DIGITIZER:
  56. switch (usage->hid) {
  57. case HID_DG_TIPSWITCH:
  58. case HID_DG_CONTACTMAX:
  59. return -1;
  60. case HID_DG_INRANGE:
  61. /* touchscreen emulation */
  62. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  63. return 1;
  64. case HID_DG_CONTACTID:
  65. hid_map_usage(hi, usage, bit, max,
  66. EV_ABS, ABS_MT_TRACKING_ID);
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. return 0;
  72. }
  73. static int cando_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  74. struct hid_field *field, struct hid_usage *usage,
  75. unsigned long **bit, int *max)
  76. {
  77. if (usage->type == EV_KEY || usage->type == EV_ABS)
  78. clear_bit(usage->code, *bit);
  79. return 0;
  80. }
  81. /*
  82. * this function is called when a whole finger has been parsed,
  83. * so that it can decide what to send to the input layer.
  84. */
  85. static void cando_filter_event(struct cando_data *td, struct input_dev *input)
  86. {
  87. td->first = !td->first; /* touchscreen emulation */
  88. if (!td->valid) {
  89. /*
  90. * touchscreen emulation: if this is the second finger and
  91. * the first was valid, the first was the oldest; if the
  92. * first was not valid and there was a valid finger in the
  93. * previous frame, this is a release.
  94. */
  95. if (td->first) {
  96. td->firstid = -1;
  97. } else if (td->firstid >= 0) {
  98. input_event(input, EV_ABS, ABS_X, td->firstx);
  99. input_event(input, EV_ABS, ABS_Y, td->firsty);
  100. td->oldest = td->firstid;
  101. } else if (td->oldest >= 0) {
  102. input_event(input, EV_KEY, BTN_TOUCH, 0);
  103. td->oldest = -1;
  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. /*
  112. * touchscreen emulation: if there was no touching finger previously,
  113. * emit touch event
  114. */
  115. if (td->oldest < 0) {
  116. input_event(input, EV_KEY, BTN_TOUCH, 1);
  117. td->oldest = td->id;
  118. }
  119. /*
  120. * touchscreen emulation: if this is the first finger, wait for the
  121. * second; the oldest is then the second if it was the oldest already
  122. * or if there was no first, the first otherwise.
  123. */
  124. if (td->first) {
  125. td->firstx = td->x;
  126. td->firsty = td->y;
  127. td->firstid = td->id;
  128. } else {
  129. int x, y, oldest;
  130. if (td->id == td->oldest || td->firstid < 0) {
  131. x = td->x;
  132. y = td->y;
  133. oldest = td->id;
  134. } else {
  135. x = td->firstx;
  136. y = td->firsty;
  137. oldest = td->firstid;
  138. }
  139. input_event(input, EV_ABS, ABS_X, x);
  140. input_event(input, EV_ABS, ABS_Y, y);
  141. td->oldest = oldest;
  142. }
  143. }
  144. static int cando_event(struct hid_device *hid, struct hid_field *field,
  145. struct hid_usage *usage, __s32 value)
  146. {
  147. struct cando_data *td = hid_get_drvdata(hid);
  148. if (hid->claimed & HID_CLAIMED_INPUT) {
  149. struct input_dev *input = field->hidinput->input;
  150. switch (usage->hid) {
  151. case HID_DG_INRANGE:
  152. td->valid = value;
  153. break;
  154. case HID_DG_CONTACTID:
  155. td->id = value;
  156. break;
  157. case HID_GD_X:
  158. td->x = value;
  159. break;
  160. case HID_GD_Y:
  161. td->y = value;
  162. cando_filter_event(td, input);
  163. break;
  164. case HID_DG_TIPSWITCH:
  165. /* avoid interference from generic hidinput handling */
  166. break;
  167. default:
  168. /* fallback to the generic hidinput handling */
  169. return 0;
  170. }
  171. }
  172. /* we have handled the hidinput part, now remains hiddev */
  173. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  174. hid->hiddev_hid_event(hid, field, usage, value);
  175. return 1;
  176. }
  177. static int cando_probe(struct hid_device *hdev, const struct hid_device_id *id)
  178. {
  179. int ret;
  180. struct cando_data *td;
  181. td = kmalloc(sizeof(struct cando_data), GFP_KERNEL);
  182. if (!td) {
  183. hid_err(hdev, "cannot allocate Cando Touch data\n");
  184. return -ENOMEM;
  185. }
  186. hid_set_drvdata(hdev, td);
  187. td->first = false;
  188. td->oldest = -1;
  189. td->valid = false;
  190. ret = hid_parse(hdev);
  191. if (!ret)
  192. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  193. if (ret)
  194. kfree(td);
  195. return ret;
  196. }
  197. static void cando_remove(struct hid_device *hdev)
  198. {
  199. hid_hw_stop(hdev);
  200. kfree(hid_get_drvdata(hdev));
  201. hid_set_drvdata(hdev, NULL);
  202. }
  203. static const struct hid_device_id cando_devices[] = {
  204. { HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
  205. USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
  206. { HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
  207. USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
  208. { HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
  209. USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
  210. { }
  211. };
  212. MODULE_DEVICE_TABLE(hid, cando_devices);
  213. static const struct hid_usage_id cando_grabbed_usages[] = {
  214. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  215. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  216. };
  217. static struct hid_driver cando_driver = {
  218. .name = "cando-touch",
  219. .id_table = cando_devices,
  220. .probe = cando_probe,
  221. .remove = cando_remove,
  222. .input_mapping = cando_input_mapping,
  223. .input_mapped = cando_input_mapped,
  224. .usage_table = cando_grabbed_usages,
  225. .event = cando_event,
  226. };
  227. static int __init cando_init(void)
  228. {
  229. return hid_register_driver(&cando_driver);
  230. }
  231. static void __exit cando_exit(void)
  232. {
  233. hid_unregister_driver(&cando_driver);
  234. }
  235. module_init(cando_init);
  236. module_exit(cando_exit);