hid-wacom.c 12 KB

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