hid-wacom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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) 2008 Jiri Slaby <jirislaby@gmail.com>
  9. * Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>
  10. * Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>
  11. * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
  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/leds.h>
  24. #include <linux/slab.h>
  25. #include <linux/power_supply.h>
  26. #include "hid-ids.h"
  27. #define PAD_DEVICE_ID 0x0F
  28. #define WAC_CMD_LED_CONTROL 0x20
  29. #define WAC_CMD_ICON_START_STOP 0x21
  30. #define WAC_CMD_ICON_TRANSFER 0x26
  31. struct wacom_data {
  32. __u16 tool;
  33. __u16 butstate;
  34. __u8 whlstate;
  35. __u8 features;
  36. __u32 id;
  37. __u32 serial;
  38. unsigned char high_speed;
  39. __u8 battery_capacity;
  40. __u8 power_raw;
  41. __u8 ps_connected;
  42. struct power_supply battery;
  43. struct power_supply ac;
  44. __u8 led_selector;
  45. struct led_classdev *leds[4];
  46. };
  47. /*percent of battery capacity for Graphire
  48. 8th value means AC online and show 100% capacity */
  49. static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
  50. /*percent of battery capacity for Intuos4 WL, AC has a separate bit*/
  51. static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
  52. static enum power_supply_property wacom_battery_props[] = {
  53. POWER_SUPPLY_PROP_PRESENT,
  54. POWER_SUPPLY_PROP_CAPACITY,
  55. POWER_SUPPLY_PROP_SCOPE,
  56. };
  57. static enum power_supply_property wacom_ac_props[] = {
  58. POWER_SUPPLY_PROP_PRESENT,
  59. POWER_SUPPLY_PROP_ONLINE,
  60. POWER_SUPPLY_PROP_SCOPE,
  61. };
  62. static void wacom_scramble(__u8 *image)
  63. {
  64. __u16 mask;
  65. __u16 s1;
  66. __u16 s2;
  67. __u16 r1 ;
  68. __u16 r2 ;
  69. __u16 r;
  70. __u8 buf[256];
  71. int i, w, x, y, z;
  72. for (x = 0; x < 32; x++) {
  73. for (y = 0; y < 8; y++)
  74. buf[(8 * x) + (7 - y)] = image[(8 * x) + y];
  75. }
  76. /* Change 76543210 into GECA6420 as required by Intuos4 WL
  77. * HGFEDCBA HFDB7531
  78. */
  79. for (x = 0; x < 4; x++) {
  80. for (y = 0; y < 4; y++) {
  81. for (z = 0; z < 8; z++) {
  82. mask = 0x0001;
  83. r1 = 0;
  84. r2 = 0;
  85. i = (x << 6) + (y << 4) + z;
  86. s1 = buf[i];
  87. s2 = buf[i+8];
  88. for (w = 0; w < 8; w++) {
  89. r1 |= (s1 & mask);
  90. r2 |= (s2 & mask);
  91. s1 <<= 1;
  92. s2 <<= 1;
  93. mask <<= 2;
  94. }
  95. r = r1 | (r2 << 1);
  96. i = (x << 6) + (y << 4) + (z << 1);
  97. image[i] = 0xFF & r;
  98. image[i+1] = (0xFF00 & r) >> 8;
  99. }
  100. }
  101. }
  102. }
  103. static void wacom_set_image(struct hid_device *hdev, const char *image,
  104. __u8 icon_no)
  105. {
  106. __u8 rep_data[68];
  107. __u8 p[256];
  108. int ret, i, j;
  109. for (i = 0; i < 256; i++)
  110. p[i] = image[i];
  111. rep_data[0] = WAC_CMD_ICON_START_STOP;
  112. rep_data[1] = 0;
  113. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  114. HID_FEATURE_REPORT);
  115. if (ret < 0)
  116. goto err;
  117. rep_data[0] = WAC_CMD_ICON_TRANSFER;
  118. rep_data[1] = icon_no & 0x07;
  119. wacom_scramble(p);
  120. for (i = 0; i < 4; i++) {
  121. for (j = 0; j < 64; j++)
  122. rep_data[j + 3] = p[(i << 6) + j];
  123. rep_data[2] = i;
  124. ret = hdev->hid_output_raw_report(hdev, rep_data, 67,
  125. HID_FEATURE_REPORT);
  126. }
  127. rep_data[0] = WAC_CMD_ICON_START_STOP;
  128. rep_data[1] = 0;
  129. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  130. HID_FEATURE_REPORT);
  131. err:
  132. return;
  133. }
  134. static void wacom_leds_set_brightness(struct led_classdev *led_dev,
  135. enum led_brightness value)
  136. {
  137. struct device *dev = led_dev->dev->parent;
  138. struct hid_device *hdev;
  139. struct wacom_data *wdata;
  140. unsigned char *buf;
  141. __u8 led = 0;
  142. int i;
  143. hdev = container_of(dev, struct hid_device, dev);
  144. wdata = hid_get_drvdata(hdev);
  145. for (i = 0; i < 4; ++i) {
  146. if (wdata->leds[i] == led_dev)
  147. wdata->led_selector = i;
  148. }
  149. led = wdata->led_selector | 0x04;
  150. buf = kzalloc(9, GFP_KERNEL);
  151. if (buf) {
  152. buf[0] = WAC_CMD_LED_CONTROL;
  153. buf[1] = led;
  154. buf[2] = value >> 2;
  155. buf[3] = value;
  156. /* use fixed brightness for OLEDs */
  157. buf[4] = 0x08;
  158. hdev->hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT);
  159. kfree(buf);
  160. }
  161. return;
  162. }
  163. static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)
  164. {
  165. struct wacom_data *wdata;
  166. struct device *dev = led_dev->dev->parent;
  167. int value = 0;
  168. int i;
  169. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  170. for (i = 0; i < 4; ++i) {
  171. if (wdata->leds[i] == led_dev) {
  172. value = wdata->leds[i]->brightness;
  173. break;
  174. }
  175. }
  176. return value;
  177. }
  178. static int wacom_initialize_leds(struct hid_device *hdev)
  179. {
  180. struct wacom_data *wdata = hid_get_drvdata(hdev);
  181. struct led_classdev *led;
  182. struct device *dev = &hdev->dev;
  183. size_t namesz = strlen(dev_name(dev)) + 12;
  184. char *name;
  185. int i, ret;
  186. wdata->led_selector = 0;
  187. for (i = 0; i < 4; i++) {
  188. led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
  189. if (!led) {
  190. hid_warn(hdev,
  191. "can't allocate memory for LED selector\n");
  192. ret = -ENOMEM;
  193. goto err;
  194. }
  195. name = (void *)&led[1];
  196. snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);
  197. led->name = name;
  198. led->brightness = 0;
  199. led->max_brightness = 127;
  200. led->brightness_get = wacom_leds_get_brightness;
  201. led->brightness_set = wacom_leds_set_brightness;
  202. wdata->leds[i] = led;
  203. ret = led_classdev_register(dev, wdata->leds[i]);
  204. if (ret) {
  205. wdata->leds[i] = NULL;
  206. kfree(led);
  207. hid_warn(hdev, "can't register LED\n");
  208. goto err;
  209. }
  210. }
  211. err:
  212. return ret;
  213. }
  214. static void wacom_destroy_leds(struct hid_device *hdev)
  215. {
  216. struct wacom_data *wdata = hid_get_drvdata(hdev);
  217. struct led_classdev *led;
  218. int i;
  219. for (i = 0; i < 4; ++i) {
  220. if (wdata->leds[i]) {
  221. led = wdata->leds[i];
  222. wdata->leds[i] = NULL;
  223. led_classdev_unregister(led);
  224. kfree(led);
  225. }
  226. }
  227. }
  228. static int wacom_battery_get_property(struct power_supply *psy,
  229. enum power_supply_property psp,
  230. union power_supply_propval *val)
  231. {
  232. struct wacom_data *wdata = container_of(psy,
  233. struct wacom_data, battery);
  234. int ret = 0;
  235. switch (psp) {
  236. case POWER_SUPPLY_PROP_PRESENT:
  237. val->intval = 1;
  238. break;
  239. case POWER_SUPPLY_PROP_SCOPE:
  240. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  241. break;
  242. case POWER_SUPPLY_PROP_CAPACITY:
  243. val->intval = wdata->battery_capacity;
  244. break;
  245. default:
  246. ret = -EINVAL;
  247. break;
  248. }
  249. return ret;
  250. }
  251. static int wacom_ac_get_property(struct power_supply *psy,
  252. enum power_supply_property psp,
  253. union power_supply_propval *val)
  254. {
  255. struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
  256. int ret = 0;
  257. switch (psp) {
  258. case POWER_SUPPLY_PROP_PRESENT:
  259. /* fall through */
  260. case POWER_SUPPLY_PROP_ONLINE:
  261. val->intval = wdata->ps_connected;
  262. break;
  263. case POWER_SUPPLY_PROP_SCOPE:
  264. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  265. break;
  266. default:
  267. ret = -EINVAL;
  268. break;
  269. }
  270. return ret;
  271. }
  272. static void wacom_set_features(struct hid_device *hdev, u8 speed)
  273. {
  274. struct wacom_data *wdata = hid_get_drvdata(hdev);
  275. int limit, ret;
  276. __u8 rep_data[2];
  277. switch (hdev->product) {
  278. case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
  279. rep_data[0] = 0x03 ; rep_data[1] = 0x00;
  280. limit = 3;
  281. do {
  282. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  283. HID_FEATURE_REPORT);
  284. } while (ret < 0 && limit-- > 0);
  285. if (ret >= 0) {
  286. if (speed == 0)
  287. rep_data[0] = 0x05;
  288. else
  289. rep_data[0] = 0x06;
  290. rep_data[1] = 0x00;
  291. limit = 3;
  292. do {
  293. ret = hdev->hid_output_raw_report(hdev,
  294. rep_data, 2, HID_FEATURE_REPORT);
  295. } while (ret < 0 && limit-- > 0);
  296. if (ret >= 0) {
  297. wdata->high_speed = speed;
  298. return;
  299. }
  300. }
  301. /*
  302. * Note that if the raw queries fail, it's not a hard failure
  303. * and it is safe to continue
  304. */
  305. hid_warn(hdev, "failed to poke device, command %d, err %d\n",
  306. rep_data[0], ret);
  307. break;
  308. case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
  309. if (speed == 1)
  310. wdata->features &= ~0x20;
  311. else
  312. wdata->features |= 0x20;
  313. rep_data[0] = 0x03;
  314. rep_data[1] = wdata->features;
  315. ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
  316. HID_FEATURE_REPORT);
  317. if (ret >= 0)
  318. wdata->high_speed = speed;
  319. break;
  320. }
  321. return;
  322. }
  323. static ssize_t wacom_show_speed(struct device *dev,
  324. struct device_attribute
  325. *attr, char *buf)
  326. {
  327. struct wacom_data *wdata = dev_get_drvdata(dev);
  328. return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
  329. }
  330. static ssize_t wacom_store_speed(struct device *dev,
  331. struct device_attribute *attr,
  332. const char *buf, size_t count)
  333. {
  334. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  335. int new_speed;
  336. if (sscanf(buf, "%1d", &new_speed ) != 1)
  337. return -EINVAL;
  338. if (new_speed == 0 || new_speed == 1) {
  339. wacom_set_features(hdev, new_speed);
  340. return strnlen(buf, PAGE_SIZE);
  341. } else
  342. return -EINVAL;
  343. }
  344. static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
  345. wacom_show_speed, wacom_store_speed);
  346. #define WACOM_STORE(OLED_ID) \
  347. static ssize_t wacom_oled##OLED_ID##_store(struct device *dev, \
  348. struct device_attribute *attr, \
  349. const char *buf, size_t count) \
  350. { \
  351. struct hid_device *hdev = container_of(dev, struct hid_device, \
  352. dev); \
  353. \
  354. if (count != 256) \
  355. return -EINVAL; \
  356. \
  357. wacom_set_image(hdev, buf, OLED_ID); \
  358. \
  359. return count; \
  360. } \
  361. \
  362. static DEVICE_ATTR(oled##OLED_ID##_img, S_IWUSR | S_IWGRP, NULL, \
  363. wacom_oled##OLED_ID##_store)
  364. WACOM_STORE(0);
  365. WACOM_STORE(1);
  366. WACOM_STORE(2);
  367. WACOM_STORE(3);
  368. WACOM_STORE(4);
  369. WACOM_STORE(5);
  370. WACOM_STORE(6);
  371. WACOM_STORE(7);
  372. static int wacom_gr_parse_report(struct hid_device *hdev,
  373. struct wacom_data *wdata,
  374. struct input_dev *input, unsigned char *data)
  375. {
  376. int tool, x, y, rw;
  377. tool = 0;
  378. /* Get X & Y positions */
  379. x = le16_to_cpu(*(__le16 *) &data[2]);
  380. y = le16_to_cpu(*(__le16 *) &data[4]);
  381. /* Get current tool identifier */
  382. if (data[1] & 0x90) { /* If pen is in the in/active area */
  383. switch ((data[1] >> 5) & 3) {
  384. case 0: /* Pen */
  385. tool = BTN_TOOL_PEN;
  386. break;
  387. case 1: /* Rubber */
  388. tool = BTN_TOOL_RUBBER;
  389. break;
  390. case 2: /* Mouse with wheel */
  391. case 3: /* Mouse without wheel */
  392. tool = BTN_TOOL_MOUSE;
  393. break;
  394. }
  395. /* Reset tool if out of active tablet area */
  396. if (!(data[1] & 0x10))
  397. tool = 0;
  398. }
  399. /* If tool changed, notify input subsystem */
  400. if (wdata->tool != tool) {
  401. if (wdata->tool) {
  402. /* Completely reset old tool state */
  403. if (wdata->tool == BTN_TOOL_MOUSE) {
  404. input_report_key(input, BTN_LEFT, 0);
  405. input_report_key(input, BTN_RIGHT, 0);
  406. input_report_key(input, BTN_MIDDLE, 0);
  407. input_report_abs(input, ABS_DISTANCE,
  408. input_abs_get_max(input, ABS_DISTANCE));
  409. } else {
  410. input_report_key(input, BTN_TOUCH, 0);
  411. input_report_key(input, BTN_STYLUS, 0);
  412. input_report_key(input, BTN_STYLUS2, 0);
  413. input_report_abs(input, ABS_PRESSURE, 0);
  414. }
  415. input_report_key(input, wdata->tool, 0);
  416. input_sync(input);
  417. }
  418. wdata->tool = tool;
  419. if (tool)
  420. input_report_key(input, tool, 1);
  421. }
  422. if (tool) {
  423. input_report_abs(input, ABS_X, x);
  424. input_report_abs(input, ABS_Y, y);
  425. switch ((data[1] >> 5) & 3) {
  426. case 2: /* Mouse with wheel */
  427. input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
  428. rw = (data[6] & 0x01) ? -1 :
  429. (data[6] & 0x02) ? 1 : 0;
  430. input_report_rel(input, REL_WHEEL, rw);
  431. /* fall through */
  432. case 3: /* Mouse without wheel */
  433. input_report_key(input, BTN_LEFT, data[1] & 0x01);
  434. input_report_key(input, BTN_RIGHT, data[1] & 0x02);
  435. /* Compute distance between mouse and tablet */
  436. rw = 44 - (data[6] >> 2);
  437. if (rw < 0)
  438. rw = 0;
  439. else if (rw > 31)
  440. rw = 31;
  441. input_report_abs(input, ABS_DISTANCE, rw);
  442. break;
  443. default:
  444. input_report_abs(input, ABS_PRESSURE,
  445. data[6] | (((__u16) (data[1] & 0x08)) << 5));
  446. input_report_key(input, BTN_TOUCH, data[1] & 0x01);
  447. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  448. input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
  449. break;
  450. }
  451. input_sync(input);
  452. }
  453. /* Report the state of the two buttons at the top of the tablet
  454. * as two extra fingerpad keys (buttons 4 & 5). */
  455. rw = data[7] & 0x03;
  456. if (rw != wdata->butstate) {
  457. wdata->butstate = rw;
  458. input_report_key(input, BTN_0, rw & 0x02);
  459. input_report_key(input, BTN_1, rw & 0x01);
  460. input_report_key(input, BTN_TOOL_FINGER, 0xf0);
  461. input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
  462. input_sync(input);
  463. }
  464. /* Store current battery capacity and power supply state*/
  465. rw = (data[7] >> 2 & 0x07);
  466. if (rw != wdata->power_raw) {
  467. wdata->power_raw = rw;
  468. wdata->battery_capacity = batcap_gr[rw];
  469. if (rw == 7)
  470. wdata->ps_connected = 1;
  471. else
  472. wdata->ps_connected = 0;
  473. }
  474. return 1;
  475. }
  476. static void wacom_i4_parse_button_report(struct wacom_data *wdata,
  477. struct input_dev *input, unsigned char *data)
  478. {
  479. __u16 new_butstate;
  480. __u8 new_whlstate;
  481. __u8 sync = 0;
  482. new_whlstate = data[1];
  483. if (new_whlstate != wdata->whlstate) {
  484. wdata->whlstate = new_whlstate;
  485. if (new_whlstate & 0x80) {
  486. input_report_key(input, BTN_TOUCH, 1);
  487. input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
  488. input_report_key(input, BTN_TOOL_FINGER, 1);
  489. } else {
  490. input_report_key(input, BTN_TOUCH, 0);
  491. input_report_abs(input, ABS_WHEEL, 0);
  492. input_report_key(input, BTN_TOOL_FINGER, 0);
  493. }
  494. sync = 1;
  495. }
  496. new_butstate = (data[3] << 1) | (data[2] & 0x01);
  497. if (new_butstate != wdata->butstate) {
  498. wdata->butstate = new_butstate;
  499. input_report_key(input, BTN_0, new_butstate & 0x001);
  500. input_report_key(input, BTN_1, new_butstate & 0x002);
  501. input_report_key(input, BTN_2, new_butstate & 0x004);
  502. input_report_key(input, BTN_3, new_butstate & 0x008);
  503. input_report_key(input, BTN_4, new_butstate & 0x010);
  504. input_report_key(input, BTN_5, new_butstate & 0x020);
  505. input_report_key(input, BTN_6, new_butstate & 0x040);
  506. input_report_key(input, BTN_7, new_butstate & 0x080);
  507. input_report_key(input, BTN_8, new_butstate & 0x100);
  508. input_report_key(input, BTN_TOOL_FINGER, 1);
  509. sync = 1;
  510. }
  511. if (sync) {
  512. input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
  513. input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
  514. input_sync(input);
  515. }
  516. }
  517. static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
  518. struct input_dev *input, unsigned char *data)
  519. {
  520. __u16 x, y, pressure;
  521. __u8 distance;
  522. __u8 tilt_x, tilt_y;
  523. switch (data[1]) {
  524. case 0x80: /* Out of proximity report */
  525. input_report_key(input, BTN_TOUCH, 0);
  526. input_report_abs(input, ABS_PRESSURE, 0);
  527. input_report_key(input, BTN_STYLUS, 0);
  528. input_report_key(input, BTN_STYLUS2, 0);
  529. input_report_key(input, wdata->tool, 0);
  530. input_report_abs(input, ABS_MISC, 0);
  531. input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
  532. wdata->tool = 0;
  533. input_sync(input);
  534. break;
  535. case 0xC2: /* Tool report */
  536. wdata->id = ((data[2] << 4) | (data[3] >> 4) |
  537. ((data[7] & 0x0f) << 20) |
  538. ((data[8] & 0xf0) << 12));
  539. wdata->serial = ((data[3] & 0x0f) << 28) +
  540. (data[4] << 20) + (data[5] << 12) +
  541. (data[6] << 4) + (data[7] >> 4);
  542. switch (wdata->id) {
  543. case 0x100802:
  544. wdata->tool = BTN_TOOL_PEN;
  545. break;
  546. case 0x10080A:
  547. wdata->tool = BTN_TOOL_RUBBER;
  548. break;
  549. }
  550. break;
  551. default: /* Position/pressure report */
  552. x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
  553. y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
  554. pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
  555. | (data[1] & 0x01);
  556. distance = (data[9] >> 2) & 0x3f;
  557. tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
  558. tilt_y = data[8] & 0x7f;
  559. input_report_key(input, BTN_TOUCH, pressure > 1);
  560. input_report_key(input, BTN_STYLUS, data[1] & 0x02);
  561. input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
  562. input_report_key(input, wdata->tool, 1);
  563. input_report_abs(input, ABS_X, x);
  564. input_report_abs(input, ABS_Y, y);
  565. input_report_abs(input, ABS_PRESSURE, pressure);
  566. input_report_abs(input, ABS_DISTANCE, distance);
  567. input_report_abs(input, ABS_TILT_X, tilt_x);
  568. input_report_abs(input, ABS_TILT_Y, tilt_y);
  569. input_report_abs(input, ABS_MISC, wdata->id);
  570. input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
  571. input_report_key(input, wdata->tool, 1);
  572. input_sync(input);
  573. break;
  574. }
  575. return;
  576. }
  577. static void wacom_i4_parse_report(struct hid_device *hdev,
  578. struct wacom_data *wdata,
  579. struct input_dev *input, unsigned char *data)
  580. {
  581. switch (data[0]) {
  582. case 0x00: /* Empty report */
  583. break;
  584. case 0x02: /* Pen report */
  585. wacom_i4_parse_pen_report(wdata, input, data);
  586. break;
  587. case 0x03: /* Features Report */
  588. wdata->features = data[2];
  589. break;
  590. case 0x0C: /* Button report */
  591. wacom_i4_parse_button_report(wdata, input, data);
  592. break;
  593. default:
  594. hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
  595. break;
  596. }
  597. }
  598. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  599. u8 *raw_data, int size)
  600. {
  601. struct wacom_data *wdata = hid_get_drvdata(hdev);
  602. struct hid_input *hidinput;
  603. struct input_dev *input;
  604. unsigned char *data = (unsigned char *) raw_data;
  605. int i;
  606. __u8 power_raw;
  607. if (!(hdev->claimed & HID_CLAIMED_INPUT))
  608. return 0;
  609. hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
  610. input = hidinput->input;
  611. switch (hdev->product) {
  612. case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
  613. if (data[0] == 0x03) {
  614. return wacom_gr_parse_report(hdev, wdata, input, data);
  615. } else {
  616. hid_err(hdev, "Unknown report: %d,%d size:%d\n",
  617. data[0], data[1], size);
  618. return 0;
  619. }
  620. break;
  621. case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
  622. i = 1;
  623. switch (data[0]) {
  624. case 0x04:
  625. wacom_i4_parse_report(hdev, wdata, input, data + i);
  626. i += 10;
  627. /* fall through */
  628. case 0x03:
  629. wacom_i4_parse_report(hdev, wdata, input, data + i);
  630. i += 10;
  631. wacom_i4_parse_report(hdev, wdata, input, data + i);
  632. power_raw = data[i+10];
  633. if (power_raw != wdata->power_raw) {
  634. wdata->power_raw = power_raw;
  635. wdata->battery_capacity = batcap_i4[power_raw & 0x07];
  636. wdata->ps_connected = power_raw & 0x08;
  637. }
  638. break;
  639. default:
  640. hid_err(hdev, "Unknown report: %d,%d size:%d\n",
  641. data[0], data[1], size);
  642. return 0;
  643. }
  644. }
  645. return 1;
  646. }
  647. static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  648. struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
  649. int *max)
  650. {
  651. struct input_dev *input = hi->input;
  652. __set_bit(INPUT_PROP_POINTER, input->propbit);
  653. /* Basics */
  654. input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
  655. __set_bit(REL_WHEEL, input->relbit);
  656. __set_bit(BTN_TOOL_PEN, input->keybit);
  657. __set_bit(BTN_TOUCH, input->keybit);
  658. __set_bit(BTN_STYLUS, input->keybit);
  659. __set_bit(BTN_STYLUS2, input->keybit);
  660. __set_bit(BTN_LEFT, input->keybit);
  661. __set_bit(BTN_RIGHT, input->keybit);
  662. __set_bit(BTN_MIDDLE, input->keybit);
  663. /* Pad */
  664. input_set_capability(input, EV_MSC, MSC_SERIAL);
  665. __set_bit(BTN_0, input->keybit);
  666. __set_bit(BTN_1, input->keybit);
  667. __set_bit(BTN_TOOL_FINGER, input->keybit);
  668. /* Distance, rubber and mouse */
  669. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  670. __set_bit(BTN_TOOL_MOUSE, input->keybit);
  671. switch (hdev->product) {
  672. case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
  673. input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
  674. input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
  675. input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
  676. input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
  677. break;
  678. case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
  679. __set_bit(ABS_WHEEL, input->absbit);
  680. __set_bit(ABS_MISC, input->absbit);
  681. __set_bit(BTN_2, input->keybit);
  682. __set_bit(BTN_3, input->keybit);
  683. __set_bit(BTN_4, input->keybit);
  684. __set_bit(BTN_5, input->keybit);
  685. __set_bit(BTN_6, input->keybit);
  686. __set_bit(BTN_7, input->keybit);
  687. __set_bit(BTN_8, input->keybit);
  688. input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
  689. input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
  690. input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
  691. input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
  692. input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
  693. input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
  694. input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
  695. break;
  696. }
  697. return 0;
  698. }
  699. static int wacom_probe(struct hid_device *hdev,
  700. const struct hid_device_id *id)
  701. {
  702. struct wacom_data *wdata;
  703. int ret;
  704. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  705. if (wdata == NULL) {
  706. hid_err(hdev, "can't alloc wacom descriptor\n");
  707. return -ENOMEM;
  708. }
  709. hid_set_drvdata(hdev, wdata);
  710. /* Parse the HID report now */
  711. ret = hid_parse(hdev);
  712. if (ret) {
  713. hid_err(hdev, "parse failed\n");
  714. goto err_free;
  715. }
  716. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  717. if (ret) {
  718. hid_err(hdev, "hw start failed\n");
  719. goto err_free;
  720. }
  721. ret = device_create_file(&hdev->dev, &dev_attr_speed);
  722. if (ret)
  723. hid_warn(hdev,
  724. "can't create sysfs speed attribute err: %d\n", ret);
  725. #define OLED_INIT(OLED_ID) \
  726. do { \
  727. ret = device_create_file(&hdev->dev, \
  728. &dev_attr_oled##OLED_ID##_img); \
  729. if (ret) \
  730. hid_warn(hdev, \
  731. "can't create sysfs oled attribute, err: %d\n", ret);\
  732. } while (0)
  733. OLED_INIT(0);
  734. OLED_INIT(1);
  735. OLED_INIT(2);
  736. OLED_INIT(3);
  737. OLED_INIT(4);
  738. OLED_INIT(5);
  739. OLED_INIT(6);
  740. OLED_INIT(7);
  741. wdata->features = 0;
  742. wacom_set_features(hdev, 1);
  743. if (hdev->product == USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) {
  744. sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
  745. ret = wacom_initialize_leds(hdev);
  746. if (ret)
  747. hid_warn(hdev,
  748. "can't create led attribute, err: %d\n", ret);
  749. }
  750. wdata->battery.properties = wacom_battery_props;
  751. wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
  752. wdata->battery.get_property = wacom_battery_get_property;
  753. wdata->battery.name = "wacom_battery";
  754. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  755. wdata->battery.use_for_apm = 0;
  756. ret = power_supply_register(&hdev->dev, &wdata->battery);
  757. if (ret) {
  758. hid_err(hdev, "can't create sysfs battery attribute, err: %d\n",
  759. ret);
  760. goto err_battery;
  761. }
  762. power_supply_powers(&wdata->battery, &hdev->dev);
  763. wdata->ac.properties = wacom_ac_props;
  764. wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
  765. wdata->ac.get_property = wacom_ac_get_property;
  766. wdata->ac.name = "wacom_ac";
  767. wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
  768. wdata->ac.use_for_apm = 0;
  769. ret = power_supply_register(&hdev->dev, &wdata->ac);
  770. if (ret) {
  771. hid_err(hdev,
  772. "can't create ac battery attribute, err: %d\n", ret);
  773. goto err_ac;
  774. }
  775. power_supply_powers(&wdata->ac, &hdev->dev);
  776. return 0;
  777. err_ac:
  778. power_supply_unregister(&wdata->battery);
  779. err_battery:
  780. wacom_destroy_leds(hdev);
  781. device_remove_file(&hdev->dev, &dev_attr_oled0_img);
  782. device_remove_file(&hdev->dev, &dev_attr_oled1_img);
  783. device_remove_file(&hdev->dev, &dev_attr_oled2_img);
  784. device_remove_file(&hdev->dev, &dev_attr_oled3_img);
  785. device_remove_file(&hdev->dev, &dev_attr_oled4_img);
  786. device_remove_file(&hdev->dev, &dev_attr_oled5_img);
  787. device_remove_file(&hdev->dev, &dev_attr_oled6_img);
  788. device_remove_file(&hdev->dev, &dev_attr_oled7_img);
  789. device_remove_file(&hdev->dev, &dev_attr_speed);
  790. hid_hw_stop(hdev);
  791. err_free:
  792. kfree(wdata);
  793. return ret;
  794. }
  795. static void wacom_remove(struct hid_device *hdev)
  796. {
  797. struct wacom_data *wdata = hid_get_drvdata(hdev);
  798. wacom_destroy_leds(hdev);
  799. device_remove_file(&hdev->dev, &dev_attr_oled0_img);
  800. device_remove_file(&hdev->dev, &dev_attr_oled1_img);
  801. device_remove_file(&hdev->dev, &dev_attr_oled2_img);
  802. device_remove_file(&hdev->dev, &dev_attr_oled3_img);
  803. device_remove_file(&hdev->dev, &dev_attr_oled4_img);
  804. device_remove_file(&hdev->dev, &dev_attr_oled5_img);
  805. device_remove_file(&hdev->dev, &dev_attr_oled6_img);
  806. device_remove_file(&hdev->dev, &dev_attr_oled7_img);
  807. device_remove_file(&hdev->dev, &dev_attr_speed);
  808. hid_hw_stop(hdev);
  809. power_supply_unregister(&wdata->battery);
  810. power_supply_unregister(&wdata->ac);
  811. kfree(hid_get_drvdata(hdev));
  812. }
  813. static const struct hid_device_id wacom_devices[] = {
  814. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
  815. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
  816. { }
  817. };
  818. MODULE_DEVICE_TABLE(hid, wacom_devices);
  819. static struct hid_driver wacom_driver = {
  820. .name = "wacom",
  821. .id_table = wacom_devices,
  822. .probe = wacom_probe,
  823. .remove = wacom_remove,
  824. .raw_event = wacom_raw_event,
  825. .input_mapped = wacom_input_mapped,
  826. };
  827. module_hid_driver(wacom_driver);
  828. MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
  829. MODULE_LICENSE("GPL");