hid-wacom.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Bluetooth Wacom Tablet support
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7. * Copyright (c) 2006-2007 Jiri Kosina
  8. * Copyright (c) 2007 Paul Walmsley
  9. * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
  10. * Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>
  11. * Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/hid.h>
  21. #include <linux/module.h>
  22. #include "hid-ids.h"
  23. struct wacom_data {
  24. __u16 tool;
  25. unsigned char butstate;
  26. };
  27. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  28. u8 *raw_data, int size)
  29. {
  30. struct wacom_data *wdata = hid_get_drvdata(hdev);
  31. struct hid_input *hidinput;
  32. struct input_dev *input;
  33. unsigned char *data = (unsigned char *) raw_data;
  34. int tool, x, y, rw;
  35. if (!(hdev->claimed & HID_CLAIMED_INPUT))
  36. return 0;
  37. tool = 0;
  38. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  39. input = hidinput->input;
  40. /* Check if this is a tablet report */
  41. if (data[0] != 0x03)
  42. return 0;
  43. /* Get X & Y positions */
  44. x = le16_to_cpu(*(__le16 *) &data[2]);
  45. y = le16_to_cpu(*(__le16 *) &data[4]);
  46. /* Get current tool identifier */
  47. if (data[1] & 0x90) { /* If pen is in the in/active area */
  48. switch ((data[1] >> 5) & 3) {
  49. case 0: /* Pen */
  50. tool = BTN_TOOL_PEN;
  51. break;
  52. case 1: /* Rubber */
  53. tool = BTN_TOOL_RUBBER;
  54. break;
  55. case 2: /* Mouse with wheel */
  56. case 3: /* Mouse without wheel */
  57. tool = BTN_TOOL_MOUSE;
  58. break;
  59. }
  60. /* Reset tool if out of active tablet area */
  61. if (!(data[1] & 0x10))
  62. tool = 0;
  63. }
  64. /* If tool changed, notify input subsystem */
  65. if (wdata->tool != tool) {
  66. if (wdata->tool) {
  67. /* Completely reset old tool state */
  68. if (wdata->tool == BTN_TOOL_MOUSE) {
  69. input_report_key(input, BTN_LEFT, 0);
  70. input_report_key(input, BTN_RIGHT, 0);
  71. input_report_key(input, BTN_MIDDLE, 0);
  72. input_report_abs(input, ABS_DISTANCE,
  73. input->absmax[ABS_DISTANCE]);
  74. } else {
  75. input_report_key(input, BTN_TOUCH, 0);
  76. input_report_key(input, BTN_STYLUS, 0);
  77. input_report_key(input, BTN_STYLUS2, 0);
  78. input_report_abs(input, ABS_PRESSURE, 0);
  79. }
  80. input_report_key(input, wdata->tool, 0);
  81. input_sync(input);
  82. }
  83. wdata->tool = tool;
  84. if (tool)
  85. input_report_key(input, tool, 1);
  86. }
  87. if (tool) {
  88. input_report_abs(input, ABS_X, x);
  89. input_report_abs(input, ABS_Y, y);
  90. switch ((data[1] >> 5) & 3) {
  91. case 2: /* Mouse with wheel */
  92. input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
  93. rw = (data[6] & 0x01) ? -1 :
  94. (data[6] & 0x02) ? 1 : 0;
  95. input_report_rel(input, REL_WHEEL, rw);
  96. /* fall through */
  97. case 3: /* Mouse without wheel */
  98. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  99. input_report_key(input, BTN_RIGHT, data[1] & 0x02);
  100. /* Compute distance between mouse and tablet */
  101. rw = 44 - (data[6] >> 2);
  102. if (rw < 0)
  103. rw = 0;
  104. else if (rw > 31)
  105. rw = 31;
  106. input_report_abs(input, ABS_DISTANCE, rw);
  107. break;
  108. default:
  109. input_report_abs(input, ABS_PRESSURE,
  110. data[6] | (((__u16) (data[1] & 0x08)) << 5));
  111. input_report_key(input, BTN_TOUCH, data[1] & 0x01);
  112. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  113. input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
  114. break;
  115. }
  116. input_sync(input);
  117. }
  118. /* Report the state of the two buttons at the top of the tablet
  119. * as two extra fingerpad keys (buttons 4 & 5). */
  120. rw = data[7] & 0x03;
  121. if (rw != wdata->butstate) {
  122. wdata->butstate = rw;
  123. input_report_key(input, BTN_0, rw & 0x02);
  124. input_report_key(input, BTN_1, rw & 0x01);
  125. input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
  126. input_sync(input);
  127. }
  128. return 1;
  129. }
  130. static int wacom_probe(struct hid_device *hdev,
  131. const struct hid_device_id *id)
  132. {
  133. struct hid_input *hidinput;
  134. struct input_dev *input;
  135. struct wacom_data *wdata;
  136. int ret;
  137. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  138. if (wdata == NULL) {
  139. dev_err(&hdev->dev, "can't alloc wacom descriptor\n");
  140. return -ENOMEM;
  141. }
  142. hid_set_drvdata(hdev, wdata);
  143. ret = hid_parse(hdev);
  144. if (ret) {
  145. dev_err(&hdev->dev, "parse failed\n");
  146. goto err_free;
  147. }
  148. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  149. if (ret) {
  150. dev_err(&hdev->dev, "hw start failed\n");
  151. goto err_free;
  152. }
  153. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  154. input = hidinput->input;
  155. /* Basics */
  156. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  157. input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) |
  158. BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE);
  159. input->relbit[0] |= BIT(REL_WHEEL);
  160. set_bit(BTN_TOOL_PEN, input->keybit);
  161. set_bit(BTN_TOUCH, input->keybit);
  162. set_bit(BTN_STYLUS, input->keybit);
  163. set_bit(BTN_STYLUS2, input->keybit);
  164. set_bit(BTN_LEFT, input->keybit);
  165. set_bit(BTN_RIGHT, input->keybit);
  166. set_bit(BTN_MIDDLE, input->keybit);
  167. /* Pad */
  168. input->evbit[0] |= BIT(EV_MSC);
  169. input->mscbit[0] |= BIT(MSC_SERIAL);
  170. /* Distance, rubber and mouse */
  171. input->absbit[0] |= BIT(ABS_DISTANCE);
  172. set_bit(BTN_TOOL_RUBBER, input->keybit);
  173. set_bit(BTN_TOOL_MOUSE, input->keybit);
  174. input->absmax[ABS_PRESSURE] = 511;
  175. input->absmax[ABS_DISTANCE] = 32;
  176. input->absmax[ABS_X] = 16704;
  177. input->absmax[ABS_Y] = 12064;
  178. input->absfuzz[ABS_X] = 4;
  179. input->absfuzz[ABS_Y] = 4;
  180. return 0;
  181. err_free:
  182. kfree(wdata);
  183. return ret;
  184. }
  185. static void wacom_remove(struct hid_device *hdev)
  186. {
  187. hid_hw_stop(hdev);
  188. kfree(hid_get_drvdata(hdev));
  189. }
  190. static const struct hid_device_id wacom_devices[] = {
  191. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(hid, wacom_devices);
  195. static struct hid_driver wacom_driver = {
  196. .name = "wacom",
  197. .id_table = wacom_devices,
  198. .probe = wacom_probe,
  199. .remove = wacom_remove,
  200. .raw_event = wacom_raw_event,
  201. };
  202. static int __init wacom_init(void)
  203. {
  204. int ret;
  205. ret = hid_register_driver(&wacom_driver);
  206. if (ret)
  207. printk(KERN_ERR "can't register wacom driver\n");
  208. printk(KERN_ERR "wacom driver registered\n");
  209. return ret;
  210. }
  211. static void __exit wacom_exit(void)
  212. {
  213. hid_unregister_driver(&wacom_driver);
  214. }
  215. module_init(wacom_init);
  216. module_exit(wacom_exit);
  217. MODULE_LICENSE("GPL");