hid-wacom.c 25 KB

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