hid-wacom.c 12 KB

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