hid-wacom.c 11 KB

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