hid-wacom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  23. #include <linux/power_supply.h>
  24. #endif
  25. #include "hid-ids.h"
  26. struct wacom_data {
  27. __u16 tool;
  28. unsigned char butstate;
  29. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  30. int battery_capacity;
  31. struct power_supply battery;
  32. struct power_supply ac;
  33. #endif
  34. };
  35. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  36. /*percent of battery capacity, 0 means AC online*/
  37. static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
  38. static enum power_supply_property wacom_battery_props[] = {
  39. POWER_SUPPLY_PROP_PRESENT,
  40. POWER_SUPPLY_PROP_CAPACITY
  41. };
  42. static enum power_supply_property wacom_ac_props[] = {
  43. POWER_SUPPLY_PROP_PRESENT,
  44. POWER_SUPPLY_PROP_ONLINE
  45. };
  46. static int wacom_battery_get_property(struct power_supply *psy,
  47. enum power_supply_property psp,
  48. union power_supply_propval *val)
  49. {
  50. struct wacom_data *wdata = container_of(psy,
  51. struct wacom_data, battery);
  52. int power_state = batcap[wdata->battery_capacity];
  53. int ret = 0;
  54. switch (psp) {
  55. case POWER_SUPPLY_PROP_PRESENT:
  56. val->intval = 1;
  57. break;
  58. case POWER_SUPPLY_PROP_CAPACITY:
  59. /* show 100% battery capacity when charging */
  60. if (power_state == 0)
  61. val->intval = 100;
  62. else
  63. val->intval = power_state;
  64. break;
  65. default:
  66. ret = -EINVAL;
  67. break;
  68. }
  69. return ret;
  70. }
  71. static int wacom_ac_get_property(struct power_supply *psy,
  72. enum power_supply_property psp,
  73. union power_supply_propval *val)
  74. {
  75. struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
  76. int power_state = batcap[wdata->battery_capacity];
  77. int ret = 0;
  78. switch (psp) {
  79. case POWER_SUPPLY_PROP_PRESENT:
  80. /* fall through */
  81. case POWER_SUPPLY_PROP_ONLINE:
  82. if (power_state == 0)
  83. val->intval = 1;
  84. else
  85. val->intval = 0;
  86. break;
  87. default:
  88. ret = -EINVAL;
  89. break;
  90. }
  91. return ret;
  92. }
  93. #endif
  94. static void wacom_poke(struct hid_device *hdev, u8 speed)
  95. {
  96. int limit, ret;
  97. char rep_data[2];
  98. rep_data[0] = 0x03 ; rep_data[1] = 0x00;
  99. limit = 3;
  100. do {
  101. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  102. HID_FEATURE_REPORT);
  103. } while (ret < 0 && limit-- > 0);
  104. if (ret >= 0) {
  105. if (speed == 0)
  106. rep_data[0] = 0x05;
  107. else
  108. rep_data[0] = 0x06;
  109. rep_data[1] = 0x00;
  110. limit = 3;
  111. do {
  112. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  113. HID_FEATURE_REPORT);
  114. } while (ret < 0 && limit-- > 0);
  115. if (ret >= 0)
  116. return;
  117. }
  118. /*
  119. * Note that if the raw queries fail, it's not a hard failure and it
  120. * is safe to continue
  121. */
  122. dev_warn(&hdev->dev, "failed to poke device, command %d, err %d\n",
  123. rep_data[0], ret);
  124. return;
  125. }
  126. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  127. u8 *raw_data, int size)
  128. {
  129. struct wacom_data *wdata = hid_get_drvdata(hdev);
  130. struct hid_input *hidinput;
  131. struct input_dev *input;
  132. unsigned char *data = (unsigned char *) raw_data;
  133. int tool, x, y, rw;
  134. if (!(hdev->claimed & HID_CLAIMED_INPUT))
  135. return 0;
  136. tool = 0;
  137. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  138. input = hidinput->input;
  139. /* Check if this is a tablet report */
  140. if (data[0] != 0x03)
  141. return 0;
  142. /* Get X & Y positions */
  143. x = le16_to_cpu(*(__le16 *) &data[2]);
  144. y = le16_to_cpu(*(__le16 *) &data[4]);
  145. /* Get current tool identifier */
  146. if (data[1] & 0x90) { /* If pen is in the in/active area */
  147. switch ((data[1] >> 5) & 3) {
  148. case 0: /* Pen */
  149. tool = BTN_TOOL_PEN;
  150. break;
  151. case 1: /* Rubber */
  152. tool = BTN_TOOL_RUBBER;
  153. break;
  154. case 2: /* Mouse with wheel */
  155. case 3: /* Mouse without wheel */
  156. tool = BTN_TOOL_MOUSE;
  157. break;
  158. }
  159. /* Reset tool if out of active tablet area */
  160. if (!(data[1] & 0x10))
  161. tool = 0;
  162. }
  163. /* If tool changed, notify input subsystem */
  164. if (wdata->tool != tool) {
  165. if (wdata->tool) {
  166. /* Completely reset old tool state */
  167. if (wdata->tool == BTN_TOOL_MOUSE) {
  168. input_report_key(input, BTN_LEFT, 0);
  169. input_report_key(input, BTN_RIGHT, 0);
  170. input_report_key(input, BTN_MIDDLE, 0);
  171. input_report_abs(input, ABS_DISTANCE,
  172. input->absmax[ABS_DISTANCE]);
  173. } else {
  174. input_report_key(input, BTN_TOUCH, 0);
  175. input_report_key(input, BTN_STYLUS, 0);
  176. input_report_key(input, BTN_STYLUS2, 0);
  177. input_report_abs(input, ABS_PRESSURE, 0);
  178. }
  179. input_report_key(input, wdata->tool, 0);
  180. input_sync(input);
  181. }
  182. wdata->tool = tool;
  183. if (tool)
  184. input_report_key(input, tool, 1);
  185. }
  186. if (tool) {
  187. input_report_abs(input, ABS_X, x);
  188. input_report_abs(input, ABS_Y, y);
  189. switch ((data[1] >> 5) & 3) {
  190. case 2: /* Mouse with wheel */
  191. input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
  192. rw = (data[6] & 0x01) ? -1 :
  193. (data[6] & 0x02) ? 1 : 0;
  194. input_report_rel(input, REL_WHEEL, rw);
  195. /* fall through */
  196. case 3: /* Mouse without wheel */
  197. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  198. input_report_key(input, BTN_RIGHT, data[1] & 0x02);
  199. /* Compute distance between mouse and tablet */
  200. rw = 44 - (data[6] >> 2);
  201. if (rw < 0)
  202. rw = 0;
  203. else if (rw > 31)
  204. rw = 31;
  205. input_report_abs(input, ABS_DISTANCE, rw);
  206. break;
  207. default:
  208. input_report_abs(input, ABS_PRESSURE,
  209. data[6] | (((__u16) (data[1] & 0x08)) << 5));
  210. input_report_key(input, BTN_TOUCH, data[1] & 0x01);
  211. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  212. input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
  213. break;
  214. }
  215. input_sync(input);
  216. }
  217. /* Report the state of the two buttons at the top of the tablet
  218. * as two extra fingerpad keys (buttons 4 & 5). */
  219. rw = data[7] & 0x03;
  220. if (rw != wdata->butstate) {
  221. wdata->butstate = rw;
  222. input_report_key(input, BTN_0, rw & 0x02);
  223. input_report_key(input, BTN_1, rw & 0x01);
  224. input_report_key(input, BTN_TOOL_FINGER, 0xf0);
  225. input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
  226. input_sync(input);
  227. }
  228. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  229. /* Store current battery capacity */
  230. rw = (data[7] >> 2 & 0x07);
  231. if (rw != wdata->battery_capacity)
  232. wdata->battery_capacity = rw;
  233. #endif
  234. return 1;
  235. }
  236. static int wacom_probe(struct hid_device *hdev,
  237. const struct hid_device_id *id)
  238. {
  239. struct hid_input *hidinput;
  240. struct input_dev *input;
  241. struct wacom_data *wdata;
  242. int ret;
  243. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  244. if (wdata == NULL) {
  245. dev_err(&hdev->dev, "can't alloc wacom descriptor\n");
  246. return -ENOMEM;
  247. }
  248. hid_set_drvdata(hdev, wdata);
  249. /* Parse the HID report now */
  250. ret = hid_parse(hdev);
  251. if (ret) {
  252. dev_err(&hdev->dev, "parse failed\n");
  253. goto err_free;
  254. }
  255. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  256. if (ret) {
  257. dev_err(&hdev->dev, "hw start failed\n");
  258. goto err_free;
  259. }
  260. /* Set Wacom mode 2 with high reporting speed */
  261. wacom_poke(hdev, 1);
  262. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  263. wdata->battery.properties = wacom_battery_props;
  264. wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
  265. wdata->battery.get_property = wacom_battery_get_property;
  266. wdata->battery.name = "wacom_battery";
  267. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  268. wdata->battery.use_for_apm = 0;
  269. ret = power_supply_register(&hdev->dev, &wdata->battery);
  270. if (ret) {
  271. dev_warn(&hdev->dev,
  272. "can't create sysfs battery attribute, err: %d\n", ret);
  273. /*
  274. * battery attribute is not critical for the tablet, but if it
  275. * failed then there is no need to create ac attribute
  276. */
  277. goto move_on;
  278. }
  279. wdata->ac.properties = wacom_ac_props;
  280. wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
  281. wdata->ac.get_property = wacom_ac_get_property;
  282. wdata->ac.name = "wacom_ac";
  283. wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
  284. wdata->ac.use_for_apm = 0;
  285. ret = power_supply_register(&hdev->dev, &wdata->ac);
  286. if (ret) {
  287. dev_warn(&hdev->dev,
  288. "can't create ac battery attribute, err: %d\n", ret);
  289. /*
  290. * ac attribute is not critical for the tablet, but if it
  291. * failed then we don't want to battery attribute to exist
  292. */
  293. power_supply_unregister(&wdata->battery);
  294. }
  295. move_on:
  296. #endif
  297. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  298. input = hidinput->input;
  299. /* Basics */
  300. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  301. input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) |
  302. BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE);
  303. input->relbit[0] |= BIT(REL_WHEEL);
  304. set_bit(BTN_TOOL_PEN, input->keybit);
  305. set_bit(BTN_TOUCH, input->keybit);
  306. set_bit(BTN_STYLUS, input->keybit);
  307. set_bit(BTN_STYLUS2, input->keybit);
  308. set_bit(BTN_LEFT, input->keybit);
  309. set_bit(BTN_RIGHT, input->keybit);
  310. set_bit(BTN_MIDDLE, input->keybit);
  311. /* Pad */
  312. input->evbit[0] |= BIT(EV_MSC);
  313. input->mscbit[0] |= BIT(MSC_SERIAL);
  314. set_bit(BTN_0, input->keybit);
  315. set_bit(BTN_1, input->keybit);
  316. set_bit(BTN_TOOL_FINGER, input->keybit);
  317. /* Distance, rubber and mouse */
  318. input->absbit[0] |= BIT(ABS_DISTANCE);
  319. set_bit(BTN_TOOL_RUBBER, input->keybit);
  320. set_bit(BTN_TOOL_MOUSE, input->keybit);
  321. input->absmax[ABS_PRESSURE] = 511;
  322. input->absmax[ABS_DISTANCE] = 32;
  323. input->absmax[ABS_X] = 16704;
  324. input->absmax[ABS_Y] = 12064;
  325. input->absfuzz[ABS_X] = 4;
  326. input->absfuzz[ABS_Y] = 4;
  327. return 0;
  328. err_free:
  329. kfree(wdata);
  330. return ret;
  331. }
  332. static void wacom_remove(struct hid_device *hdev)
  333. {
  334. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  335. struct wacom_data *wdata = hid_get_drvdata(hdev);
  336. #endif
  337. hid_hw_stop(hdev);
  338. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  339. power_supply_unregister(&wdata->battery);
  340. power_supply_unregister(&wdata->ac);
  341. #endif
  342. kfree(hid_get_drvdata(hdev));
  343. }
  344. static const struct hid_device_id wacom_devices[] = {
  345. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  346. { }
  347. };
  348. MODULE_DEVICE_TABLE(hid, wacom_devices);
  349. static struct hid_driver wacom_driver = {
  350. .name = "wacom",
  351. .id_table = wacom_devices,
  352. .probe = wacom_probe,
  353. .remove = wacom_remove,
  354. .raw_event = wacom_raw_event,
  355. };
  356. static int __init wacom_init(void)
  357. {
  358. int ret;
  359. ret = hid_register_driver(&wacom_driver);
  360. if (ret)
  361. printk(KERN_ERR "can't register wacom driver\n");
  362. printk(KERN_ERR "wacom driver registered\n");
  363. return ret;
  364. }
  365. static void __exit wacom_exit(void)
  366. {
  367. hid_unregister_driver(&wacom_driver);
  368. }
  369. module_init(wacom_init);
  370. module_exit(wacom_exit);
  371. MODULE_LICENSE("GPL");