hid-wacom.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <linux/slab.h>
  23. #include "hid-ids.h"
  24. struct wacom_data {
  25. __u16 tool;
  26. unsigned char butstate;
  27. };
  28. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  29. u8 *raw_data, int size)
  30. {
  31. struct wacom_data *wdata = hid_get_drvdata(hdev);
  32. struct hid_input *hidinput;
  33. struct input_dev *input;
  34. unsigned char *data = (unsigned char *) raw_data;
  35. int tool, x, y, rw;
  36. if (!(hdev->claimed & HID_CLAIMED_INPUT))
  37. return 0;
  38. tool = 0;
  39. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  40. input = hidinput->input;
  41. /* Check if this is a tablet report */
  42. if (data[0] != 0x03)
  43. return 0;
  44. /* Get X & Y positions */
  45. x = le16_to_cpu(*(__le16 *) &data[2]);
  46. y = le16_to_cpu(*(__le16 *) &data[4]);
  47. /* Get current tool identifier */
  48. if (data[1] & 0x90) { /* If pen is in the in/active area */
  49. switch ((data[1] >> 5) & 3) {
  50. case 0: /* Pen */
  51. tool = BTN_TOOL_PEN;
  52. break;
  53. case 1: /* Rubber */
  54. tool = BTN_TOOL_RUBBER;
  55. break;
  56. case 2: /* Mouse with wheel */
  57. case 3: /* Mouse without wheel */
  58. tool = BTN_TOOL_MOUSE;
  59. break;
  60. }
  61. /* Reset tool if out of active tablet area */
  62. if (!(data[1] & 0x10))
  63. tool = 0;
  64. }
  65. /* If tool changed, notify input subsystem */
  66. if (wdata->tool != tool) {
  67. if (wdata->tool) {
  68. /* Completely reset old tool state */
  69. if (wdata->tool == BTN_TOOL_MOUSE) {
  70. input_report_key(input, BTN_LEFT, 0);
  71. input_report_key(input, BTN_RIGHT, 0);
  72. input_report_key(input, BTN_MIDDLE, 0);
  73. input_report_abs(input, ABS_DISTANCE,
  74. input->absmax[ABS_DISTANCE]);
  75. } else {
  76. input_report_key(input, BTN_TOUCH, 0);
  77. input_report_key(input, BTN_STYLUS, 0);
  78. input_report_key(input, BTN_STYLUS2, 0);
  79. input_report_abs(input, ABS_PRESSURE, 0);
  80. }
  81. input_report_key(input, wdata->tool, 0);
  82. input_sync(input);
  83. }
  84. wdata->tool = tool;
  85. if (tool)
  86. input_report_key(input, tool, 1);
  87. }
  88. if (tool) {
  89. input_report_abs(input, ABS_X, x);
  90. input_report_abs(input, ABS_Y, y);
  91. switch ((data[1] >> 5) & 3) {
  92. case 2: /* Mouse with wheel */
  93. input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
  94. rw = (data[6] & 0x01) ? -1 :
  95. (data[6] & 0x02) ? 1 : 0;
  96. input_report_rel(input, REL_WHEEL, rw);
  97. /* fall through */
  98. case 3: /* Mouse without wheel */
  99. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  100. input_report_key(input, BTN_RIGHT, data[1] & 0x02);
  101. /* Compute distance between mouse and tablet */
  102. rw = 44 - (data[6] >> 2);
  103. if (rw < 0)
  104. rw = 0;
  105. else if (rw > 31)
  106. rw = 31;
  107. input_report_abs(input, ABS_DISTANCE, rw);
  108. break;
  109. default:
  110. input_report_abs(input, ABS_PRESSURE,
  111. data[6] | (((__u16) (data[1] & 0x08)) << 5));
  112. input_report_key(input, BTN_TOUCH, data[1] & 0x01);
  113. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  114. input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
  115. break;
  116. }
  117. input_sync(input);
  118. }
  119. /* Report the state of the two buttons at the top of the tablet
  120. * as two extra fingerpad keys (buttons 4 & 5). */
  121. rw = data[7] & 0x03;
  122. if (rw != wdata->butstate) {
  123. wdata->butstate = rw;
  124. input_report_key(input, BTN_0, rw & 0x02);
  125. input_report_key(input, BTN_1, rw & 0x01);
  126. input_report_key(input, BTN_TOOL_FINGER, 0xf0);
  127. input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
  128. input_sync(input);
  129. }
  130. return 1;
  131. }
  132. static int wacom_probe(struct hid_device *hdev,
  133. const struct hid_device_id *id)
  134. {
  135. struct hid_input *hidinput;
  136. struct input_dev *input;
  137. struct wacom_data *wdata;
  138. char rep_data[2];
  139. int ret;
  140. int limit;
  141. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  142. if (wdata == NULL) {
  143. dev_err(&hdev->dev, "can't alloc wacom descriptor\n");
  144. return -ENOMEM;
  145. }
  146. hid_set_drvdata(hdev, wdata);
  147. /* Parse the HID report now */
  148. ret = hid_parse(hdev);
  149. if (ret) {
  150. dev_err(&hdev->dev, "parse failed\n");
  151. goto err_free;
  152. }
  153. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  154. if (ret) {
  155. dev_err(&hdev->dev, "hw start failed\n");
  156. goto err_free;
  157. }
  158. /*
  159. * Note that if the raw queries fail, it's not a hard failure and it
  160. * is safe to continue
  161. */
  162. /* Set Wacom mode2 */
  163. rep_data[0] = 0x03; rep_data[1] = 0x00;
  164. limit = 3;
  165. do {
  166. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  167. HID_FEATURE_REPORT);
  168. } while (ret < 0 && limit-- > 0);
  169. if (ret < 0)
  170. dev_warn(&hdev->dev, "failed to poke device #1, %d\n", ret);
  171. /* 0x06 - high reporting speed, 0x05 - low speed */
  172. rep_data[0] = 0x06; rep_data[1] = 0x00;
  173. limit = 3;
  174. do {
  175. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  176. HID_FEATURE_REPORT);
  177. } while (ret < 0 && limit-- > 0);
  178. if (ret < 0)
  179. dev_warn(&hdev->dev, "failed to poke device #2, %d\n", ret);
  180. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  181. input = hidinput->input;
  182. /* Basics */
  183. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  184. input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) |
  185. BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE);
  186. input->relbit[0] |= BIT(REL_WHEEL);
  187. set_bit(BTN_TOOL_PEN, input->keybit);
  188. set_bit(BTN_TOUCH, input->keybit);
  189. set_bit(BTN_STYLUS, input->keybit);
  190. set_bit(BTN_STYLUS2, input->keybit);
  191. set_bit(BTN_LEFT, input->keybit);
  192. set_bit(BTN_RIGHT, input->keybit);
  193. set_bit(BTN_MIDDLE, input->keybit);
  194. /* Pad */
  195. input->evbit[0] |= BIT(EV_MSC);
  196. input->mscbit[0] |= BIT(MSC_SERIAL);
  197. set_bit(BTN_0, input->keybit);
  198. set_bit(BTN_1, input->keybit);
  199. set_bit(BTN_TOOL_FINGER, input->keybit);
  200. /* Distance, rubber and mouse */
  201. input->absbit[0] |= BIT(ABS_DISTANCE);
  202. set_bit(BTN_TOOL_RUBBER, input->keybit);
  203. set_bit(BTN_TOOL_MOUSE, input->keybit);
  204. input->absmax[ABS_PRESSURE] = 511;
  205. input->absmax[ABS_DISTANCE] = 32;
  206. input->absmax[ABS_X] = 16704;
  207. input->absmax[ABS_Y] = 12064;
  208. input->absfuzz[ABS_X] = 4;
  209. input->absfuzz[ABS_Y] = 4;
  210. return 0;
  211. err_free:
  212. kfree(wdata);
  213. return ret;
  214. }
  215. static void wacom_remove(struct hid_device *hdev)
  216. {
  217. hid_hw_stop(hdev);
  218. kfree(hid_get_drvdata(hdev));
  219. }
  220. static const struct hid_device_id wacom_devices[] = {
  221. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(hid, wacom_devices);
  225. static struct hid_driver wacom_driver = {
  226. .name = "wacom",
  227. .id_table = wacom_devices,
  228. .probe = wacom_probe,
  229. .remove = wacom_remove,
  230. .raw_event = wacom_raw_event,
  231. };
  232. static int __init wacom_init(void)
  233. {
  234. int ret;
  235. ret = hid_register_driver(&wacom_driver);
  236. if (ret)
  237. printk(KERN_ERR "can't register wacom driver\n");
  238. printk(KERN_ERR "wacom driver registered\n");
  239. return ret;
  240. }
  241. static void __exit wacom_exit(void)
  242. {
  243. hid_unregister_driver(&wacom_driver);
  244. }
  245. module_init(wacom_init);
  246. module_exit(wacom_exit);
  247. MODULE_LICENSE("GPL");