hid-wacom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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_gr_parse_report(struct hid_device *hdev,
  156. struct wacom_data *wdata,
  157. struct input_dev *input, unsigned char *data)
  158. {
  159. int tool, x, y, rw;
  160. tool = 0;
  161. /* Get X & Y positions */
  162. x = le16_to_cpu(*(__le16 *) &data[2]);
  163. y = le16_to_cpu(*(__le16 *) &data[4]);
  164. /* Get current tool identifier */
  165. if (data[1] & 0x90) { /* If pen is in the in/active area */
  166. switch ((data[1] >> 5) & 3) {
  167. case 0: /* Pen */
  168. tool = BTN_TOOL_PEN;
  169. break;
  170. case 1: /* Rubber */
  171. tool = BTN_TOOL_RUBBER;
  172. break;
  173. case 2: /* Mouse with wheel */
  174. case 3: /* Mouse without wheel */
  175. tool = BTN_TOOL_MOUSE;
  176. break;
  177. }
  178. /* Reset tool if out of active tablet area */
  179. if (!(data[1] & 0x10))
  180. tool = 0;
  181. }
  182. /* If tool changed, notify input subsystem */
  183. if (wdata->tool != tool) {
  184. if (wdata->tool) {
  185. /* Completely reset old tool state */
  186. if (wdata->tool == BTN_TOOL_MOUSE) {
  187. input_report_key(input, BTN_LEFT, 0);
  188. input_report_key(input, BTN_RIGHT, 0);
  189. input_report_key(input, BTN_MIDDLE, 0);
  190. input_report_abs(input, ABS_DISTANCE,
  191. input_abs_get_max(input, ABS_DISTANCE));
  192. } else {
  193. input_report_key(input, BTN_TOUCH, 0);
  194. input_report_key(input, BTN_STYLUS, 0);
  195. input_report_key(input, BTN_STYLUS2, 0);
  196. input_report_abs(input, ABS_PRESSURE, 0);
  197. }
  198. input_report_key(input, wdata->tool, 0);
  199. input_sync(input);
  200. }
  201. wdata->tool = tool;
  202. if (tool)
  203. input_report_key(input, tool, 1);
  204. }
  205. if (tool) {
  206. input_report_abs(input, ABS_X, x);
  207. input_report_abs(input, ABS_Y, y);
  208. switch ((data[1] >> 5) & 3) {
  209. case 2: /* Mouse with wheel */
  210. input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
  211. rw = (data[6] & 0x01) ? -1 :
  212. (data[6] & 0x02) ? 1 : 0;
  213. input_report_rel(input, REL_WHEEL, rw);
  214. /* fall through */
  215. case 3: /* Mouse without wheel */
  216. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  217. input_report_key(input, BTN_RIGHT, data[1] & 0x02);
  218. /* Compute distance between mouse and tablet */
  219. rw = 44 - (data[6] >> 2);
  220. if (rw < 0)
  221. rw = 0;
  222. else if (rw > 31)
  223. rw = 31;
  224. input_report_abs(input, ABS_DISTANCE, rw);
  225. break;
  226. default:
  227. input_report_abs(input, ABS_PRESSURE,
  228. data[6] | (((__u16) (data[1] & 0x08)) << 5));
  229. input_report_key(input, BTN_TOUCH, data[1] & 0x01);
  230. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  231. input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
  232. break;
  233. }
  234. input_sync(input);
  235. }
  236. /* Report the state of the two buttons at the top of the tablet
  237. * as two extra fingerpad keys (buttons 4 & 5). */
  238. rw = data[7] & 0x03;
  239. if (rw != wdata->butstate) {
  240. wdata->butstate = rw;
  241. input_report_key(input, BTN_0, rw & 0x02);
  242. input_report_key(input, BTN_1, rw & 0x01);
  243. input_report_key(input, BTN_TOOL_FINGER, 0xf0);
  244. input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
  245. input_sync(input);
  246. }
  247. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  248. /* Store current battery capacity */
  249. rw = (data[7] >> 2 & 0x07);
  250. if (rw != wdata->battery_capacity)
  251. wdata->battery_capacity = rw;
  252. #endif
  253. return 1;
  254. }
  255. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  256. u8 *raw_data, int size)
  257. {
  258. struct wacom_data *wdata = hid_get_drvdata(hdev);
  259. struct hid_input *hidinput;
  260. struct input_dev *input;
  261. unsigned char *data = (unsigned char *) raw_data;
  262. if (!(hdev->claimed & HID_CLAIMED_INPUT))
  263. return 0;
  264. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  265. input = hidinput->input;
  266. /* Check if this is a tablet report */
  267. if (data[0] != 0x03)
  268. return 0;
  269. return wacom_gr_parse_report(hdev, wdata, input, data);
  270. }
  271. static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  272. struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
  273. int *max)
  274. {
  275. struct input_dev *input = hi->input;
  276. __set_bit(INPUT_PROP_POINTER, input->propbit);
  277. /* Basics */
  278. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  279. __set_bit(REL_WHEEL, input->relbit);
  280. __set_bit(BTN_TOOL_PEN, input->keybit);
  281. __set_bit(BTN_TOUCH, input->keybit);
  282. __set_bit(BTN_STYLUS, input->keybit);
  283. __set_bit(BTN_STYLUS2, input->keybit);
  284. __set_bit(BTN_LEFT, input->keybit);
  285. __set_bit(BTN_RIGHT, input->keybit);
  286. __set_bit(BTN_MIDDLE, input->keybit);
  287. /* Pad */
  288. input->evbit[0] |= BIT(EV_MSC);
  289. __set_bit(MSC_SERIAL, input->mscbit);
  290. __set_bit(BTN_0, input->keybit);
  291. __set_bit(BTN_1, input->keybit);
  292. __set_bit(BTN_TOOL_FINGER, input->keybit);
  293. /* Distance, rubber and mouse */
  294. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  295. __set_bit(BTN_TOOL_MOUSE, input->keybit);
  296. input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
  297. input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
  298. input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
  299. input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
  300. return 0;
  301. }
  302. static int wacom_probe(struct hid_device *hdev,
  303. const struct hid_device_id *id)
  304. {
  305. struct wacom_data *wdata;
  306. int ret;
  307. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  308. if (wdata == NULL) {
  309. hid_err(hdev, "can't alloc wacom descriptor\n");
  310. return -ENOMEM;
  311. }
  312. hid_set_drvdata(hdev, wdata);
  313. /* Parse the HID report now */
  314. ret = hid_parse(hdev);
  315. if (ret) {
  316. hid_err(hdev, "parse failed\n");
  317. goto err_free;
  318. }
  319. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  320. if (ret) {
  321. hid_err(hdev, "hw start failed\n");
  322. goto err_free;
  323. }
  324. ret = device_create_file(&hdev->dev, &dev_attr_speed);
  325. if (ret)
  326. hid_warn(hdev,
  327. "can't create sysfs speed attribute err: %d\n", ret);
  328. /* Set Wacom mode 2 with high reporting speed */
  329. wacom_poke(hdev, 1);
  330. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  331. wdata->battery.properties = wacom_battery_props;
  332. wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
  333. wdata->battery.get_property = wacom_battery_get_property;
  334. wdata->battery.name = "wacom_battery";
  335. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  336. wdata->battery.use_for_apm = 0;
  337. ret = power_supply_register(&hdev->dev, &wdata->battery);
  338. if (ret) {
  339. hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
  340. ret);
  341. goto err_battery;
  342. }
  343. wdata->ac.properties = wacom_ac_props;
  344. wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
  345. wdata->ac.get_property = wacom_ac_get_property;
  346. wdata->ac.name = "wacom_ac";
  347. wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
  348. wdata->ac.use_for_apm = 0;
  349. ret = power_supply_register(&hdev->dev, &wdata->ac);
  350. if (ret) {
  351. hid_warn(hdev,
  352. "can't create ac battery attribute, err: %d\n", ret);
  353. goto err_ac;
  354. }
  355. #endif
  356. return 0;
  357. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  358. err_ac:
  359. power_supply_unregister(&wdata->battery);
  360. err_battery:
  361. device_remove_file(&hdev->dev, &dev_attr_speed);
  362. hid_hw_stop(hdev);
  363. #endif
  364. err_free:
  365. kfree(wdata);
  366. return ret;
  367. }
  368. static void wacom_remove(struct hid_device *hdev)
  369. {
  370. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  371. struct wacom_data *wdata = hid_get_drvdata(hdev);
  372. #endif
  373. device_remove_file(&hdev->dev, &dev_attr_speed);
  374. hid_hw_stop(hdev);
  375. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  376. power_supply_unregister(&wdata->battery);
  377. power_supply_unregister(&wdata->ac);
  378. #endif
  379. kfree(hid_get_drvdata(hdev));
  380. }
  381. static const struct hid_device_id wacom_devices[] = {
  382. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(hid, wacom_devices);
  386. static struct hid_driver wacom_driver = {
  387. .name = "wacom",
  388. .id_table = wacom_devices,
  389. .probe = wacom_probe,
  390. .remove = wacom_remove,
  391. .raw_event = wacom_raw_event,
  392. .input_mapped = wacom_input_mapped,
  393. };
  394. static int __init wacom_init(void)
  395. {
  396. int ret;
  397. ret = hid_register_driver(&wacom_driver);
  398. if (ret)
  399. pr_err("can't register wacom driver\n");
  400. return ret;
  401. }
  402. static void __exit wacom_exit(void)
  403. {
  404. hid_unregister_driver(&wacom_driver);
  405. }
  406. module_init(wacom_init);
  407. module_exit(wacom_exit);
  408. MODULE_LICENSE("GPL");