hid-wacom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  266. struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
  267. int *max)
  268. {
  269. struct input_dev *input = hi->input;
  270. __set_bit(INPUT_PROP_POINTER, input->propbit);
  271. /* Basics */
  272. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  273. __set_bit(REL_WHEEL, input->relbit);
  274. __set_bit(BTN_TOOL_PEN, input->keybit);
  275. __set_bit(BTN_TOUCH, input->keybit);
  276. __set_bit(BTN_STYLUS, input->keybit);
  277. __set_bit(BTN_STYLUS2, input->keybit);
  278. __set_bit(BTN_LEFT, input->keybit);
  279. __set_bit(BTN_RIGHT, input->keybit);
  280. __set_bit(BTN_MIDDLE, input->keybit);
  281. /* Pad */
  282. input->evbit[0] |= BIT(EV_MSC);
  283. __set_bit(MSC_SERIAL, input->mscbit);
  284. __set_bit(BTN_0, input->keybit);
  285. __set_bit(BTN_1, input->keybit);
  286. __set_bit(BTN_TOOL_FINGER, input->keybit);
  287. /* Distance, rubber and mouse */
  288. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  289. __set_bit(BTN_TOOL_MOUSE, input->keybit);
  290. input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
  291. input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
  292. input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
  293. input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
  294. return 0;
  295. }
  296. static int wacom_probe(struct hid_device *hdev,
  297. const struct hid_device_id *id)
  298. {
  299. struct wacom_data *wdata;
  300. int ret;
  301. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  302. if (wdata == NULL) {
  303. hid_err(hdev, "can't alloc wacom descriptor\n");
  304. return -ENOMEM;
  305. }
  306. hid_set_drvdata(hdev, wdata);
  307. /* Parse the HID report now */
  308. ret = hid_parse(hdev);
  309. if (ret) {
  310. hid_err(hdev, "parse failed\n");
  311. goto err_free;
  312. }
  313. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  314. if (ret) {
  315. hid_err(hdev, "hw start failed\n");
  316. goto err_free;
  317. }
  318. ret = device_create_file(&hdev->dev, &dev_attr_speed);
  319. if (ret)
  320. hid_warn(hdev,
  321. "can't create sysfs speed attribute err: %d\n", ret);
  322. /* Set Wacom mode 2 with high reporting speed */
  323. wacom_poke(hdev, 1);
  324. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  325. wdata->battery.properties = wacom_battery_props;
  326. wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
  327. wdata->battery.get_property = wacom_battery_get_property;
  328. wdata->battery.name = "wacom_battery";
  329. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  330. wdata->battery.use_for_apm = 0;
  331. ret = power_supply_register(&hdev->dev, &wdata->battery);
  332. if (ret) {
  333. hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
  334. ret);
  335. goto err_battery;
  336. }
  337. wdata->ac.properties = wacom_ac_props;
  338. wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
  339. wdata->ac.get_property = wacom_ac_get_property;
  340. wdata->ac.name = "wacom_ac";
  341. wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
  342. wdata->ac.use_for_apm = 0;
  343. ret = power_supply_register(&hdev->dev, &wdata->ac);
  344. if (ret) {
  345. hid_warn(hdev,
  346. "can't create ac battery attribute, err: %d\n", ret);
  347. goto err_ac;
  348. }
  349. #endif
  350. return 0;
  351. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  352. err_ac:
  353. power_supply_unregister(&wdata->battery);
  354. err_battery:
  355. device_remove_file(&hdev->dev, &dev_attr_speed);
  356. hid_hw_stop(hdev);
  357. #endif
  358. err_free:
  359. kfree(wdata);
  360. return ret;
  361. }
  362. static void wacom_remove(struct hid_device *hdev)
  363. {
  364. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  365. struct wacom_data *wdata = hid_get_drvdata(hdev);
  366. #endif
  367. device_remove_file(&hdev->dev, &dev_attr_speed);
  368. hid_hw_stop(hdev);
  369. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  370. power_supply_unregister(&wdata->battery);
  371. power_supply_unregister(&wdata->ac);
  372. #endif
  373. kfree(hid_get_drvdata(hdev));
  374. }
  375. static const struct hid_device_id wacom_devices[] = {
  376. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  377. { }
  378. };
  379. MODULE_DEVICE_TABLE(hid, wacom_devices);
  380. static struct hid_driver wacom_driver = {
  381. .name = "wacom",
  382. .id_table = wacom_devices,
  383. .probe = wacom_probe,
  384. .remove = wacom_remove,
  385. .raw_event = wacom_raw_event,
  386. .input_mapped = wacom_input_mapped,
  387. };
  388. static int __init wacom_init(void)
  389. {
  390. int ret;
  391. ret = hid_register_driver(&wacom_driver);
  392. if (ret)
  393. pr_err("can't register wacom driver\n");
  394. return ret;
  395. }
  396. static void __exit wacom_exit(void)
  397. {
  398. hid_unregister_driver(&wacom_driver);
  399. }
  400. module_init(wacom_init);
  401. module_exit(wacom_exit);
  402. MODULE_LICENSE("GPL");