hid-mosart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * HID driver for the multitouch panel on the ASUS EeePC T91MT
  3. *
  4. * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
  5. * Copyright (c) 2010 Teemu Tuominen <teemu.tuominen@cybercom.com>
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/hid.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include "usbhid/usbhid.h"
  20. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  21. MODULE_DESCRIPTION("MosArt dual-touch panel");
  22. MODULE_LICENSE("GPL");
  23. #include "hid-ids.h"
  24. struct mosart_data {
  25. __u16 x, y;
  26. __u8 id;
  27. bool valid; /* valid finger data, or just placeholder? */
  28. bool first; /* is this the first finger in this frame? */
  29. bool activity_now; /* at least one active finger in this frame? */
  30. bool activity; /* at least one active finger previously? */
  31. };
  32. static int mosart_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  33. struct hid_field *field, struct hid_usage *usage,
  34. unsigned long **bit, int *max)
  35. {
  36. switch (usage->hid & HID_USAGE_PAGE) {
  37. case HID_UP_GENDESK:
  38. switch (usage->hid) {
  39. case HID_GD_X:
  40. hid_map_usage(hi, usage, bit, max,
  41. EV_ABS, ABS_MT_POSITION_X);
  42. /* touchscreen emulation */
  43. input_set_abs_params(hi->input, ABS_X,
  44. field->logical_minimum,
  45. field->logical_maximum, 0, 0);
  46. return 1;
  47. case HID_GD_Y:
  48. hid_map_usage(hi, usage, bit, max,
  49. EV_ABS, ABS_MT_POSITION_Y);
  50. /* touchscreen emulation */
  51. input_set_abs_params(hi->input, ABS_Y,
  52. field->logical_minimum,
  53. field->logical_maximum, 0, 0);
  54. return 1;
  55. }
  56. return 0;
  57. case HID_UP_DIGITIZER:
  58. switch (usage->hid) {
  59. case HID_DG_CONFIDENCE:
  60. case HID_DG_TIPSWITCH:
  61. case HID_DG_INPUTMODE:
  62. case HID_DG_DEVICEINDEX:
  63. case HID_DG_CONTACTCOUNT:
  64. case HID_DG_CONTACTMAX:
  65. case HID_DG_TIPPRESSURE:
  66. case HID_DG_WIDTH:
  67. case HID_DG_HEIGHT:
  68. return -1;
  69. case HID_DG_INRANGE:
  70. /* touchscreen emulation */
  71. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  72. return 1;
  73. case HID_DG_CONTACTID:
  74. hid_map_usage(hi, usage, bit, max,
  75. EV_ABS, ABS_MT_TRACKING_ID);
  76. return 1;
  77. }
  78. return 0;
  79. case 0xff000000:
  80. /* ignore HID features */
  81. return -1;
  82. case HID_UP_BUTTON:
  83. /* ignore buttons */
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. static int mosart_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  89. struct hid_field *field, struct hid_usage *usage,
  90. unsigned long **bit, int *max)
  91. {
  92. if (usage->type == EV_KEY || usage->type == EV_ABS)
  93. clear_bit(usage->code, *bit);
  94. return 0;
  95. }
  96. /*
  97. * this function is called when a whole finger has been parsed,
  98. * so that it can decide what to send to the input layer.
  99. */
  100. static void mosart_filter_event(struct mosart_data *td, struct input_dev *input)
  101. {
  102. td->first = !td->first; /* touchscreen emulation */
  103. if (!td->valid) {
  104. /*
  105. * touchscreen emulation: if no finger in this frame is valid
  106. * and there previously was finger activity, this is a release
  107. */
  108. if (!td->first && !td->activity_now && td->activity) {
  109. input_event(input, EV_KEY, BTN_TOUCH, 0);
  110. td->activity = false;
  111. }
  112. return;
  113. }
  114. input_event(input, EV_ABS, ABS_MT_TRACKING_ID, td->id);
  115. input_event(input, EV_ABS, ABS_MT_POSITION_X, td->x);
  116. input_event(input, EV_ABS, ABS_MT_POSITION_Y, td->y);
  117. input_mt_sync(input);
  118. td->valid = false;
  119. /* touchscreen emulation: if first active finger in this frame... */
  120. if (!td->activity_now) {
  121. /* if there was no previous activity, emit touch event */
  122. if (!td->activity) {
  123. input_event(input, EV_KEY, BTN_TOUCH, 1);
  124. td->activity = true;
  125. }
  126. td->activity_now = true;
  127. /* and in any case this is our preferred finger */
  128. input_event(input, EV_ABS, ABS_X, td->x);
  129. input_event(input, EV_ABS, ABS_Y, td->y);
  130. }
  131. }
  132. static int mosart_event(struct hid_device *hid, struct hid_field *field,
  133. struct hid_usage *usage, __s32 value)
  134. {
  135. struct mosart_data *td = hid_get_drvdata(hid);
  136. if (hid->claimed & HID_CLAIMED_INPUT) {
  137. struct input_dev *input = field->hidinput->input;
  138. switch (usage->hid) {
  139. case HID_DG_INRANGE:
  140. td->valid = !!value;
  141. break;
  142. case HID_GD_X:
  143. td->x = value;
  144. break;
  145. case HID_GD_Y:
  146. td->y = value;
  147. mosart_filter_event(td, input);
  148. break;
  149. case HID_DG_CONTACTID:
  150. td->id = value;
  151. break;
  152. case HID_DG_CONTACTCOUNT:
  153. /* touch emulation: this is the last field in a frame */
  154. td->first = false;
  155. td->activity_now = false;
  156. break;
  157. case HID_DG_CONFIDENCE:
  158. case HID_DG_TIPSWITCH:
  159. /* avoid interference from generic hidinput handling */
  160. break;
  161. default:
  162. /* fallback to the generic hidinput handling */
  163. return 0;
  164. }
  165. }
  166. /* we have handled the hidinput part, now remains hiddev */
  167. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  168. hid->hiddev_hid_event(hid, field, usage, value);
  169. return 1;
  170. }
  171. static int mosart_probe(struct hid_device *hdev, const struct hid_device_id *id)
  172. {
  173. int ret;
  174. struct mosart_data *td;
  175. td = kmalloc(sizeof(struct mosart_data), GFP_KERNEL);
  176. if (!td) {
  177. hid_err(hdev, "cannot allocate MosArt data\n");
  178. return -ENOMEM;
  179. }
  180. td->valid = false;
  181. td->activity = false;
  182. td->activity_now = false;
  183. td->first = false;
  184. hid_set_drvdata(hdev, td);
  185. /* currently, it's better to have one evdev device only */
  186. #if 0
  187. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  188. #endif
  189. ret = hid_parse(hdev);
  190. if (ret == 0)
  191. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  192. if (ret == 0) {
  193. struct hid_report_enum *re = hdev->report_enum
  194. + HID_FEATURE_REPORT;
  195. struct hid_report *r = re->report_id_hash[7];
  196. r->field[0]->value[0] = 0x02;
  197. usbhid_submit_report(hdev, r, USB_DIR_OUT);
  198. } else
  199. kfree(td);
  200. return ret;
  201. }
  202. #ifdef CONFIG_PM
  203. static int mosart_reset_resume(struct hid_device *hdev)
  204. {
  205. struct hid_report_enum *re = hdev->report_enum
  206. + HID_FEATURE_REPORT;
  207. struct hid_report *r = re->report_id_hash[7];
  208. r->field[0]->value[0] = 0x02;
  209. usbhid_submit_report(hdev, r, USB_DIR_OUT);
  210. return 0;
  211. }
  212. #endif
  213. static void mosart_remove(struct hid_device *hdev)
  214. {
  215. hid_hw_stop(hdev);
  216. kfree(hid_get_drvdata(hdev));
  217. hid_set_drvdata(hdev, NULL);
  218. }
  219. static const struct hid_device_id mosart_devices[] = {
  220. { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_T91MT) },
  221. { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(hid, mosart_devices);
  225. static const struct hid_usage_id mosart_grabbed_usages[] = {
  226. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  227. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  228. };
  229. static struct hid_driver mosart_driver = {
  230. .name = "mosart",
  231. .id_table = mosart_devices,
  232. .probe = mosart_probe,
  233. .remove = mosart_remove,
  234. .input_mapping = mosart_input_mapping,
  235. .input_mapped = mosart_input_mapped,
  236. .usage_table = mosart_grabbed_usages,
  237. .event = mosart_event,
  238. #ifdef CONFIG_PM
  239. .reset_resume = mosart_reset_resume,
  240. #endif
  241. };
  242. static int __init mosart_init(void)
  243. {
  244. return hid_register_driver(&mosart_driver);
  245. }
  246. static void __exit mosart_exit(void)
  247. {
  248. hid_unregister_driver(&mosart_driver);
  249. }
  250. module_init(mosart_init);
  251. module_exit(mosart_exit);