hid-wacom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
  13. */
  14. /*
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/device.h>
  22. #include <linux/hid.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  26. #include <linux/power_supply.h>
  27. #endif
  28. #include "hid-ids.h"
  29. struct wacom_data {
  30. __u16 tool;
  31. unsigned char butstate;
  32. __u8 features;
  33. unsigned char high_speed;
  34. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  35. int battery_capacity;
  36. struct power_supply battery;
  37. struct power_supply ac;
  38. #endif
  39. };
  40. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  41. /*percent of battery capacity, 0 means AC online*/
  42. static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
  43. static enum power_supply_property wacom_battery_props[] = {
  44. POWER_SUPPLY_PROP_PRESENT,
  45. POWER_SUPPLY_PROP_CAPACITY
  46. };
  47. static enum power_supply_property wacom_ac_props[] = {
  48. POWER_SUPPLY_PROP_PRESENT,
  49. POWER_SUPPLY_PROP_ONLINE
  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_CAPACITY:
  64. /* show 100% battery capacity when charging */
  65. if (power_state == 0)
  66. val->intval = 100;
  67. else
  68. val->intval = power_state;
  69. break;
  70. default:
  71. ret = -EINVAL;
  72. break;
  73. }
  74. return ret;
  75. }
  76. static int wacom_ac_get_property(struct power_supply *psy,
  77. enum power_supply_property psp,
  78. union power_supply_propval *val)
  79. {
  80. struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
  81. int power_state = batcap[wdata->battery_capacity];
  82. int ret = 0;
  83. switch (psp) {
  84. case POWER_SUPPLY_PROP_PRESENT:
  85. /* fall through */
  86. case POWER_SUPPLY_PROP_ONLINE:
  87. if (power_state == 0)
  88. val->intval = 1;
  89. else
  90. val->intval = 0;
  91. break;
  92. default:
  93. ret = -EINVAL;
  94. break;
  95. }
  96. return ret;
  97. }
  98. #endif
  99. static void wacom_set_features(struct hid_device *hdev)
  100. {
  101. int ret;
  102. __u8 rep_data[2];
  103. /*set high speed, tablet mode*/
  104. rep_data[0] = 0x03;
  105. rep_data[1] = 0x20;
  106. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  107. HID_FEATURE_REPORT);
  108. return;
  109. }
  110. static void wacom_poke(struct hid_device *hdev, u8 speed)
  111. {
  112. struct wacom_data *wdata = hid_get_drvdata(hdev);
  113. int limit, ret;
  114. char rep_data[2];
  115. rep_data[0] = 0x03 ; rep_data[1] = 0x00;
  116. limit = 3;
  117. do {
  118. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  119. HID_FEATURE_REPORT);
  120. } while (ret < 0 && limit-- > 0);
  121. if (ret >= 0) {
  122. if (speed == 0)
  123. rep_data[0] = 0x05;
  124. else
  125. rep_data[0] = 0x06;
  126. rep_data[1] = 0x00;
  127. limit = 3;
  128. do {
  129. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  130. HID_FEATURE_REPORT);
  131. } while (ret < 0 && limit-- > 0);
  132. if (ret >= 0) {
  133. wdata->high_speed = speed;
  134. return;
  135. }
  136. }
  137. /*
  138. * Note that if the raw queries fail, it's not a hard failure and it
  139. * is safe to continue
  140. */
  141. hid_warn(hdev, "failed to poke device, command %d, err %d\n",
  142. rep_data[0], ret);
  143. return;
  144. }
  145. static ssize_t wacom_show_speed(struct device *dev,
  146. struct device_attribute
  147. *attr, char *buf)
  148. {
  149. struct wacom_data *wdata = dev_get_drvdata(dev);
  150. return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
  151. }
  152. static ssize_t wacom_store_speed(struct device *dev,
  153. struct device_attribute *attr,
  154. const char *buf, size_t count)
  155. {
  156. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  157. int new_speed;
  158. if (sscanf(buf, "%1d", &new_speed ) != 1)
  159. return -EINVAL;
  160. if (new_speed == 0 || new_speed == 1) {
  161. wacom_poke(hdev, new_speed);
  162. return strnlen(buf, PAGE_SIZE);
  163. } else
  164. return -EINVAL;
  165. }
  166. static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
  167. wacom_show_speed, wacom_store_speed);
  168. static int wacom_gr_parse_report(struct hid_device *hdev,
  169. struct wacom_data *wdata,
  170. struct input_dev *input, unsigned char *data)
  171. {
  172. int tool, x, y, rw;
  173. tool = 0;
  174. /* Get X & Y positions */
  175. x = le16_to_cpu(*(__le16 *) &data[2]);
  176. y = le16_to_cpu(*(__le16 *) &data[4]);
  177. /* Get current tool identifier */
  178. if (data[1] & 0x90) { /* If pen is in the in/active area */
  179. switch ((data[1] >> 5) & 3) {
  180. case 0: /* Pen */
  181. tool = BTN_TOOL_PEN;
  182. break;
  183. case 1: /* Rubber */
  184. tool = BTN_TOOL_RUBBER;
  185. break;
  186. case 2: /* Mouse with wheel */
  187. case 3: /* Mouse without wheel */
  188. tool = BTN_TOOL_MOUSE;
  189. break;
  190. }
  191. /* Reset tool if out of active tablet area */
  192. if (!(data[1] & 0x10))
  193. tool = 0;
  194. }
  195. /* If tool changed, notify input subsystem */
  196. if (wdata->tool != tool) {
  197. if (wdata->tool) {
  198. /* Completely reset old tool state */
  199. if (wdata->tool == BTN_TOOL_MOUSE) {
  200. input_report_key(input, BTN_LEFT, 0);
  201. input_report_key(input, BTN_RIGHT, 0);
  202. input_report_key(input, BTN_MIDDLE, 0);
  203. input_report_abs(input, ABS_DISTANCE,
  204. input_abs_get_max(input, ABS_DISTANCE));
  205. } else {
  206. input_report_key(input, BTN_TOUCH, 0);
  207. input_report_key(input, BTN_STYLUS, 0);
  208. input_report_key(input, BTN_STYLUS2, 0);
  209. input_report_abs(input, ABS_PRESSURE, 0);
  210. }
  211. input_report_key(input, wdata->tool, 0);
  212. input_sync(input);
  213. }
  214. wdata->tool = tool;
  215. if (tool)
  216. input_report_key(input, tool, 1);
  217. }
  218. if (tool) {
  219. input_report_abs(input, ABS_X, x);
  220. input_report_abs(input, ABS_Y, y);
  221. switch ((data[1] >> 5) & 3) {
  222. case 2: /* Mouse with wheel */
  223. input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
  224. rw = (data[6] & 0x01) ? -1 :
  225. (data[6] & 0x02) ? 1 : 0;
  226. input_report_rel(input, REL_WHEEL, rw);
  227. /* fall through */
  228. case 3: /* Mouse without wheel */
  229. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  230. input_report_key(input, BTN_RIGHT, data[1] & 0x02);
  231. /* Compute distance between mouse and tablet */
  232. rw = 44 - (data[6] >> 2);
  233. if (rw < 0)
  234. rw = 0;
  235. else if (rw > 31)
  236. rw = 31;
  237. input_report_abs(input, ABS_DISTANCE, rw);
  238. break;
  239. default:
  240. input_report_abs(input, ABS_PRESSURE,
  241. data[6] | (((__u16) (data[1] & 0x08)) << 5));
  242. input_report_key(input, BTN_TOUCH, data[1] & 0x01);
  243. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  244. input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
  245. break;
  246. }
  247. input_sync(input);
  248. }
  249. /* Report the state of the two buttons at the top of the tablet
  250. * as two extra fingerpad keys (buttons 4 & 5). */
  251. rw = data[7] & 0x03;
  252. if (rw != wdata->butstate) {
  253. wdata->butstate = rw;
  254. input_report_key(input, BTN_0, rw & 0x02);
  255. input_report_key(input, BTN_1, rw & 0x01);
  256. input_report_key(input, BTN_TOOL_FINGER, 0xf0);
  257. input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
  258. input_sync(input);
  259. }
  260. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  261. /* Store current battery capacity */
  262. rw = (data[7] >> 2 & 0x07);
  263. if (rw != wdata->battery_capacity)
  264. wdata->battery_capacity = rw;
  265. #endif
  266. return 1;
  267. }
  268. static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
  269. struct input_dev *input, unsigned char *data)
  270. {
  271. __u16 x, y, pressure;
  272. __u32 id;
  273. switch (data[1]) {
  274. case 0x80: /* Out of proximity report */
  275. wdata->tool = 0;
  276. input_report_key(input, BTN_TOUCH, 0);
  277. input_report_abs(input, ABS_PRESSURE, 0);
  278. input_report_key(input, wdata->tool, 0);
  279. input_sync(input);
  280. break;
  281. case 0xC2: /* Tool report */
  282. id = ((data[2] << 4) | (data[3] >> 4) |
  283. ((data[7] & 0x0f) << 20) |
  284. ((data[8] & 0xf0) << 12)) & 0xfffff;
  285. switch (id) {
  286. case 0x802:
  287. wdata->tool = BTN_TOOL_PEN;
  288. break;
  289. case 0x80A:
  290. wdata->tool = BTN_TOOL_RUBBER;
  291. break;
  292. }
  293. break;
  294. default: /* Position/pressure report */
  295. x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
  296. y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
  297. pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
  298. | (data[1] & 0x01);
  299. input_report_key(input, BTN_TOUCH, pressure > 1);
  300. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  301. input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
  302. input_report_key(input, wdata->tool, 1);
  303. input_report_abs(input, ABS_X, x);
  304. input_report_abs(input, ABS_Y, y);
  305. input_report_abs(input, ABS_PRESSURE, pressure);
  306. input_sync(input);
  307. break;
  308. }
  309. return;
  310. }
  311. static void wacom_i4_parse_report(struct hid_device *hdev,
  312. struct wacom_data *wdata,
  313. struct input_dev *input, unsigned char *data)
  314. {
  315. switch (data[0]) {
  316. case 0x00: /* Empty report */
  317. break;
  318. case 0x02: /* Pen report */
  319. wacom_i4_parse_pen_report(wdata, input, data);
  320. break;
  321. case 0x03: /* Features Report */
  322. wdata->features = data[2];
  323. break;
  324. case 0x0C: /* Button report */
  325. break;
  326. default:
  327. hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
  328. break;
  329. }
  330. }
  331. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  332. u8 *raw_data, int size)
  333. {
  334. struct wacom_data *wdata = hid_get_drvdata(hdev);
  335. struct hid_input *hidinput;
  336. struct input_dev *input;
  337. unsigned char *data = (unsigned char *) raw_data;
  338. int i;
  339. if (!(hdev->claimed & HID_CLAIMED_INPUT))
  340. return 0;
  341. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  342. input = hidinput->input;
  343. /* Check if this is a tablet report */
  344. if (data[0] != 0x03)
  345. return 0;
  346. switch (hdev->product) {
  347. case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
  348. return wacom_gr_parse_report(hdev, wdata, input, data);
  349. break;
  350. case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
  351. i = 1;
  352. switch (data[0]) {
  353. case 0x04:
  354. wacom_i4_parse_report(hdev, wdata, input, data + i);
  355. i += 10;
  356. /* fall through */
  357. case 0x03:
  358. wacom_i4_parse_report(hdev, wdata, input, data + i);
  359. i += 10;
  360. wacom_i4_parse_report(hdev, wdata, input, data + i);
  361. break;
  362. default:
  363. hid_err(hdev, "Unknown report: %d,%d size:%d\n",
  364. data[0], data[1], size);
  365. return 0;
  366. }
  367. }
  368. return 1;
  369. }
  370. static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  371. struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
  372. int *max)
  373. {
  374. struct input_dev *input = hi->input;
  375. __set_bit(INPUT_PROP_POINTER, input->propbit);
  376. /* Basics */
  377. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  378. __set_bit(REL_WHEEL, input->relbit);
  379. __set_bit(BTN_TOOL_PEN, input->keybit);
  380. __set_bit(BTN_TOUCH, input->keybit);
  381. __set_bit(BTN_STYLUS, input->keybit);
  382. __set_bit(BTN_STYLUS2, input->keybit);
  383. __set_bit(BTN_LEFT, input->keybit);
  384. __set_bit(BTN_RIGHT, input->keybit);
  385. __set_bit(BTN_MIDDLE, input->keybit);
  386. /* Pad */
  387. input->evbit[0] |= BIT(EV_MSC);
  388. __set_bit(MSC_SERIAL, input->mscbit);
  389. __set_bit(BTN_0, input->keybit);
  390. __set_bit(BTN_1, input->keybit);
  391. __set_bit(BTN_TOOL_FINGER, input->keybit);
  392. /* Distance, rubber and mouse */
  393. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  394. __set_bit(BTN_TOOL_MOUSE, input->keybit);
  395. switch (hdev->product) {
  396. case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
  397. input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
  398. input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
  399. input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
  400. input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
  401. break;
  402. case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
  403. input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
  404. input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
  405. input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
  406. break;
  407. }
  408. return 0;
  409. }
  410. static int wacom_probe(struct hid_device *hdev,
  411. const struct hid_device_id *id)
  412. {
  413. struct wacom_data *wdata;
  414. int ret;
  415. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  416. if (wdata == NULL) {
  417. hid_err(hdev, "can't alloc wacom descriptor\n");
  418. return -ENOMEM;
  419. }
  420. hid_set_drvdata(hdev, wdata);
  421. /* Parse the HID report now */
  422. ret = hid_parse(hdev);
  423. if (ret) {
  424. hid_err(hdev, "parse failed\n");
  425. goto err_free;
  426. }
  427. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  428. if (ret) {
  429. hid_err(hdev, "hw start failed\n");
  430. goto err_free;
  431. }
  432. ret = device_create_file(&hdev->dev, &dev_attr_speed);
  433. if (ret)
  434. hid_warn(hdev,
  435. "can't create sysfs speed attribute err: %d\n", ret);
  436. switch (hdev->product) {
  437. case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
  438. /* Set Wacom mode 2 with high reporting speed */
  439. wacom_poke(hdev, 1);
  440. break;
  441. case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
  442. wdata->features = 0;
  443. wacom_set_features(hdev);
  444. break;
  445. }
  446. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  447. wdata->battery.properties = wacom_battery_props;
  448. wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
  449. wdata->battery.get_property = wacom_battery_get_property;
  450. wdata->battery.name = "wacom_battery";
  451. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  452. wdata->battery.use_for_apm = 0;
  453. ret = power_supply_register(&hdev->dev, &wdata->battery);
  454. if (ret) {
  455. hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
  456. ret);
  457. goto err_battery;
  458. }
  459. wdata->ac.properties = wacom_ac_props;
  460. wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
  461. wdata->ac.get_property = wacom_ac_get_property;
  462. wdata->ac.name = "wacom_ac";
  463. wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
  464. wdata->ac.use_for_apm = 0;
  465. ret = power_supply_register(&hdev->dev, &wdata->ac);
  466. if (ret) {
  467. hid_warn(hdev,
  468. "can't create ac battery attribute, err: %d\n", ret);
  469. goto err_ac;
  470. }
  471. #endif
  472. return 0;
  473. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  474. err_ac:
  475. power_supply_unregister(&wdata->battery);
  476. err_battery:
  477. device_remove_file(&hdev->dev, &dev_attr_speed);
  478. hid_hw_stop(hdev);
  479. #endif
  480. err_free:
  481. kfree(wdata);
  482. return ret;
  483. }
  484. static void wacom_remove(struct hid_device *hdev)
  485. {
  486. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  487. struct wacom_data *wdata = hid_get_drvdata(hdev);
  488. #endif
  489. device_remove_file(&hdev->dev, &dev_attr_speed);
  490. hid_hw_stop(hdev);
  491. #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
  492. power_supply_unregister(&wdata->battery);
  493. power_supply_unregister(&wdata->ac);
  494. #endif
  495. kfree(hid_get_drvdata(hdev));
  496. }
  497. static const struct hid_device_id wacom_devices[] = {
  498. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  499. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
  500. { }
  501. };
  502. MODULE_DEVICE_TABLE(hid, wacom_devices);
  503. static struct hid_driver wacom_driver = {
  504. .name = "wacom",
  505. .id_table = wacom_devices,
  506. .probe = wacom_probe,
  507. .remove = wacom_remove,
  508. .raw_event = wacom_raw_event,
  509. .input_mapped = wacom_input_mapped,
  510. };
  511. static int __init wacom_init(void)
  512. {
  513. int ret;
  514. ret = hid_register_driver(&wacom_driver);
  515. if (ret)
  516. pr_err("can't register wacom driver\n");
  517. return ret;
  518. }
  519. static void __exit wacom_exit(void)
  520. {
  521. hid_unregister_driver(&wacom_driver);
  522. }
  523. module_init(wacom_init);
  524. module_exit(wacom_exit);
  525. MODULE_LICENSE("GPL");