hid-wacom.c 12 KB

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