blob: 0f02358888a936235c5744ddf167f9a0553e9e9f [file] [log] [blame]
Bastien Noceraca2dcd42009-05-11 17:18:12 +02001/*
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>
Przemo Firszt78761ff2011-11-05 11:28:22 +000012 * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
Bastien Noceraca2dcd42009-05-11 17:18:12 +020013 */
14
15/*
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 */
21
Joe Perches4291ee32010-12-09 19:29:03 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Bastien Noceraca2dcd42009-05-11 17:18:12 +020024#include <linux/device.h>
25#include <linux/hid.h>
26#include <linux/module.h>
Przemo Firsztd13f5452012-05-10 19:23:52 +010027#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Przemo Firszt59d23342010-03-15 19:16:23 +000029#include <linux/power_supply.h>
Bastien Noceraca2dcd42009-05-11 17:18:12 +020030
31#include "hid-ids.h"
32
Przemo Firszt6245bde2012-02-28 17:19:05 +000033#define PAD_DEVICE_ID 0x0F
34
Przemo Firsztd13f5452012-05-10 19:23:52 +010035#define WAC_CMD_LED_CONTROL 0x20
Przemo Firszte3c399e2012-07-31 18:27:56 +010036#define WAC_CMD_ICON_START_STOP 0x21
37#define WAC_CMD_ICON_TRANSFER 0x26
Przemo Firsztd13f5452012-05-10 19:23:52 +010038
Bastien Noceraca2dcd42009-05-11 17:18:12 +020039struct wacom_data {
40 __u16 tool;
Przemo Firszt6245bde2012-02-28 17:19:05 +000041 __u16 butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +000042 __u8 whlstate;
Przemo Firszt78761ff2011-11-05 11:28:22 +000043 __u8 features;
Przemo Firszt24709002012-02-24 13:52:32 +000044 __u32 id;
45 __u32 serial;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +000046 unsigned char high_speed;
Przemo Firszte31e3832012-03-22 18:54:07 +000047 __u8 battery_capacity;
48 __u8 power_raw;
49 __u8 ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +000050 struct power_supply battery;
51 struct power_supply ac;
Przemo Firsztd13f5452012-05-10 19:23:52 +010052 __u8 led_selector;
53 struct led_classdev *leds[4];
Bastien Noceraca2dcd42009-05-11 17:18:12 +020054};
55
Przemo Firszte31e3832012-03-22 18:54:07 +000056/*percent of battery capacity for Graphire
57 8th value means AC online and show 100% capacity */
58static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
Przemo Firszt4202f1d2012-03-22 18:54:08 +000059/*percent of battery capacity for Intuos4 WL, AC has a separate bit*/
60static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
Przemo Firszt59d23342010-03-15 19:16:23 +000061
62static enum power_supply_property wacom_battery_props[] = {
63 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080064 POWER_SUPPLY_PROP_CAPACITY,
65 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000066};
67
68static enum power_supply_property wacom_ac_props[] = {
69 POWER_SUPPLY_PROP_PRESENT,
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080070 POWER_SUPPLY_PROP_ONLINE,
71 POWER_SUPPLY_PROP_SCOPE,
Przemo Firszt59d23342010-03-15 19:16:23 +000072};
73
Przemo Firszte3c399e2012-07-31 18:27:56 +010074static void wacom_scramble(__u8 *image)
75{
76 __u16 mask;
77 __u16 s1;
78 __u16 s2;
79 __u16 r1 ;
80 __u16 r2 ;
81 __u16 r;
82 __u8 buf[256];
83 int i, w, x, y, z;
84
85 for (x = 0; x < 32; x++) {
86 for (y = 0; y < 8; y++)
87 buf[(8 * x) + (7 - y)] = image[(8 * x) + y];
88 }
89
90 /* Change 76543210 into GECA6420 as required by Intuos4 WL
91 * HGFEDCBA HFDB7531
92 */
93 for (x = 0; x < 4; x++) {
94 for (y = 0; y < 4; y++) {
95 for (z = 0; z < 8; z++) {
96 mask = 0x0001;
97 r1 = 0;
98 r2 = 0;
99 i = (x << 6) + (y << 4) + z;
100 s1 = buf[i];
101 s2 = buf[i+8];
102 for (w = 0; w < 8; w++) {
103 r1 |= (s1 & mask);
104 r2 |= (s2 & mask);
105 s1 <<= 1;
106 s2 <<= 1;
107 mask <<= 2;
108 }
109 r = r1 | (r2 << 1);
110 i = (x << 6) + (y << 4) + (z << 1);
111 image[i] = 0xFF & r;
112 image[i+1] = (0xFF00 & r) >> 8;
113 }
114 }
115 }
116}
117
118static void wacom_set_image(struct hid_device *hdev, const char *image,
119 __u8 icon_no)
120{
121 __u8 rep_data[68];
122 __u8 p[256];
123 int ret, i, j;
124
125 for (i = 0; i < 256; i++)
126 p[i] = image[i];
127
128 rep_data[0] = WAC_CMD_ICON_START_STOP;
129 rep_data[1] = 0;
130 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
131 HID_FEATURE_REPORT);
132 if (ret < 0)
133 goto err;
134
135 rep_data[0] = WAC_CMD_ICON_TRANSFER;
136 rep_data[1] = icon_no & 0x07;
137
138 wacom_scramble(p);
139
140 for (i = 0; i < 4; i++) {
141 for (j = 0; j < 64; j++)
142 rep_data[j + 3] = p[(i << 6) + j];
143
144 rep_data[2] = i;
145 ret = hdev->hid_output_raw_report(hdev, rep_data, 67,
146 HID_FEATURE_REPORT);
147 }
148
149 rep_data[0] = WAC_CMD_ICON_START_STOP;
150 rep_data[1] = 0;
151
152 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
153 HID_FEATURE_REPORT);
154
155err:
156 return;
157}
158
Przemo Firsztd13f5452012-05-10 19:23:52 +0100159static void wacom_leds_set_brightness(struct led_classdev *led_dev,
160 enum led_brightness value)
161{
162 struct device *dev = led_dev->dev->parent;
163 struct hid_device *hdev;
164 struct wacom_data *wdata;
165 unsigned char *buf;
166 __u8 led = 0;
167 int i;
168
169 hdev = container_of(dev, struct hid_device, dev);
170 wdata = hid_get_drvdata(hdev);
171 for (i = 0; i < 4; ++i) {
172 if (wdata->leds[i] == led_dev)
173 wdata->led_selector = i;
174 }
175
176 led = wdata->led_selector | 0x04;
177 buf = kzalloc(9, GFP_KERNEL);
178 if (buf) {
179 buf[0] = WAC_CMD_LED_CONTROL;
180 buf[1] = led;
Przemo Firszt530a76c2012-07-31 18:27:55 +0100181 buf[2] = value >> 2;
182 buf[3] = value;
Przemo Firszte3c399e2012-07-31 18:27:56 +0100183 /* use fixed brightness for OLEDs */
184 buf[4] = 0x08;
Przemo Firsztd13f5452012-05-10 19:23:52 +0100185 hdev->hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT);
186 kfree(buf);
187 }
188
189 return;
190}
191
192static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)
193{
194 struct wacom_data *wdata;
195 struct device *dev = led_dev->dev->parent;
196 int value = 0;
197 int i;
198
199 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
200
201 for (i = 0; i < 4; ++i) {
202 if (wdata->leds[i] == led_dev) {
203 value = wdata->leds[i]->brightness;
204 break;
205 }
206 }
207
208 return value;
209}
210
211
212static int wacom_initialize_leds(struct hid_device *hdev)
213{
214 struct wacom_data *wdata = hid_get_drvdata(hdev);
215 struct led_classdev *led;
216 struct device *dev = &hdev->dev;
217 size_t namesz = strlen(dev_name(dev)) + 12;
218 char *name;
219 int i, ret;
220
221 wdata->led_selector = 0;
222
223 for (i = 0; i < 4; i++) {
224 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
225 if (!led) {
226 hid_warn(hdev,
227 "can't allocate memory for LED selector\n");
228 ret = -ENOMEM;
229 goto err;
230 }
231
232 name = (void *)&led[1];
233 snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);
234 led->name = name;
235 led->brightness = 0;
236 led->max_brightness = 127;
237 led->brightness_get = wacom_leds_get_brightness;
238 led->brightness_set = wacom_leds_set_brightness;
239
240 wdata->leds[i] = led;
241
242 ret = led_classdev_register(dev, wdata->leds[i]);
243
244 if (ret) {
245 wdata->leds[i] = NULL;
246 kfree(led);
247 hid_warn(hdev, "can't register LED\n");
248 goto err;
249 }
250 }
251
252err:
253 return ret;
254}
255
256static void wacom_destroy_leds(struct hid_device *hdev)
257{
258 struct wacom_data *wdata = hid_get_drvdata(hdev);
259 struct led_classdev *led;
260 int i;
261
262 for (i = 0; i < 4; ++i) {
263 if (wdata->leds[i]) {
264 led = wdata->leds[i];
265 wdata->leds[i] = NULL;
266 led_classdev_unregister(led);
267 kfree(led);
268 }
269 }
270
271}
272
Przemo Firszt59d23342010-03-15 19:16:23 +0000273static int wacom_battery_get_property(struct power_supply *psy,
274 enum power_supply_property psp,
275 union power_supply_propval *val)
276{
277 struct wacom_data *wdata = container_of(psy,
278 struct wacom_data, battery);
Przemo Firszt59d23342010-03-15 19:16:23 +0000279 int ret = 0;
280
281 switch (psp) {
282 case POWER_SUPPLY_PROP_PRESENT:
283 val->intval = 1;
284 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800285 case POWER_SUPPLY_PROP_SCOPE:
286 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
287 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000288 case POWER_SUPPLY_PROP_CAPACITY:
Przemo Firszte31e3832012-03-22 18:54:07 +0000289 val->intval = wdata->battery_capacity;
Przemo Firszt59d23342010-03-15 19:16:23 +0000290 break;
291 default:
292 ret = -EINVAL;
293 break;
294 }
295 return ret;
296}
297
298static int wacom_ac_get_property(struct power_supply *psy,
299 enum power_supply_property psp,
300 union power_supply_propval *val)
301{
302 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
Przemo Firszt59d23342010-03-15 19:16:23 +0000303 int ret = 0;
304
305 switch (psp) {
306 case POWER_SUPPLY_PROP_PRESENT:
307 /* fall through */
308 case POWER_SUPPLY_PROP_ONLINE:
Przemo Firszte31e3832012-03-22 18:54:07 +0000309 val->intval = wdata->ps_connected;
Przemo Firszt59d23342010-03-15 19:16:23 +0000310 break;
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800311 case POWER_SUPPLY_PROP_SCOPE:
312 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
313 break;
Przemo Firszt59d23342010-03-15 19:16:23 +0000314 default:
315 ret = -EINVAL;
316 break;
317 }
318 return ret;
319}
Przemo Firszt59d23342010-03-15 19:16:23 +0000320
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100321static void wacom_set_features(struct hid_device *hdev, u8 speed)
Przemo Firszt78761ff2011-11-05 11:28:22 +0000322{
Przemo Firsztbe4925b2012-05-15 19:32:26 +0100323 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100324 int limit, ret;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000325 __u8 rep_data[2];
326
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100327 switch (hdev->product) {
328 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
329 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100330 limit = 3;
331 do {
332 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
333 HID_FEATURE_REPORT);
334 } while (ret < 0 && limit-- > 0);
335
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000336 if (ret >= 0) {
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100337 if (speed == 0)
338 rep_data[0] = 0x05;
339 else
340 rep_data[0] = 0x06;
341
342 rep_data[1] = 0x00;
343 limit = 3;
344 do {
345 ret = hdev->hid_output_raw_report(hdev,
346 rep_data, 2, HID_FEATURE_REPORT);
347 } while (ret < 0 && limit-- > 0);
348
349 if (ret >= 0) {
350 wdata->high_speed = speed;
351 return;
352 }
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000353 }
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100354
355 /*
356 * Note that if the raw queries fail, it's not a hard failure
357 * and it is safe to continue
358 */
359 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
360 rep_data[0], ret);
361 break;
362 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
363 if (speed == 1)
364 wdata->features &= ~0x20;
365 else
366 wdata->features |= 0x20;
367
368 rep_data[0] = 0x03;
369 rep_data[1] = wdata->features;
370
371 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
372 HID_FEATURE_REPORT);
373 if (ret >= 0)
374 wdata->high_speed = speed;
375 break;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100376 }
377
Przemo Firszt06c7c312010-03-22 09:55:04 +0100378 return;
379}
380
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000381static ssize_t wacom_show_speed(struct device *dev,
382 struct device_attribute
383 *attr, char *buf)
384{
385 struct wacom_data *wdata = dev_get_drvdata(dev);
386
387 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
388}
389
390static ssize_t wacom_store_speed(struct device *dev,
391 struct device_attribute *attr,
392 const char *buf, size_t count)
393{
394 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
395 int new_speed;
396
397 if (sscanf(buf, "%1d", &new_speed ) != 1)
398 return -EINVAL;
399
400 if (new_speed == 0 || new_speed == 1) {
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100401 wacom_set_features(hdev, new_speed);
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000402 return strnlen(buf, PAGE_SIZE);
403 } else
404 return -EINVAL;
405}
406
Jiri Kosinaedd21262010-11-18 16:28:43 +0100407static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000408 wacom_show_speed, wacom_store_speed);
409
Przemo Firszte3c399e2012-07-31 18:27:56 +0100410#define WACOM_STORE(OLED_ID) \
411static ssize_t wacom_oled##OLED_ID##_store(struct device *dev, \
412 struct device_attribute *attr, \
413 const char *buf, size_t count) \
414{ \
415 struct hid_device *hdev = container_of(dev, struct hid_device, \
416 dev); \
417 \
418 if (count != 256) \
419 return -EINVAL; \
420 \
421 wacom_set_image(hdev, buf, OLED_ID); \
422 \
423 return count; \
424} \
425 \
426static DEVICE_ATTR(oled##OLED_ID##_img, S_IWUSR | S_IWGRP, NULL, \
427 wacom_oled##OLED_ID##_store)
428
429WACOM_STORE(0);
430WACOM_STORE(1);
431WACOM_STORE(2);
432WACOM_STORE(3);
433WACOM_STORE(4);
434WACOM_STORE(5);
435WACOM_STORE(6);
436WACOM_STORE(7);
437
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000438static int wacom_gr_parse_report(struct hid_device *hdev,
439 struct wacom_data *wdata,
440 struct input_dev *input, unsigned char *data)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200441{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200442 int tool, x, y, rw;
443
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200444 tool = 0;
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200445 /* Get X & Y positions */
446 x = le16_to_cpu(*(__le16 *) &data[2]);
447 y = le16_to_cpu(*(__le16 *) &data[4]);
448
449 /* Get current tool identifier */
450 if (data[1] & 0x90) { /* If pen is in the in/active area */
451 switch ((data[1] >> 5) & 3) {
452 case 0: /* Pen */
453 tool = BTN_TOOL_PEN;
454 break;
455
456 case 1: /* Rubber */
457 tool = BTN_TOOL_RUBBER;
458 break;
459
460 case 2: /* Mouse with wheel */
461 case 3: /* Mouse without wheel */
462 tool = BTN_TOOL_MOUSE;
463 break;
464 }
465
466 /* Reset tool if out of active tablet area */
467 if (!(data[1] & 0x10))
468 tool = 0;
469 }
470
471 /* If tool changed, notify input subsystem */
472 if (wdata->tool != tool) {
473 if (wdata->tool) {
474 /* Completely reset old tool state */
475 if (wdata->tool == BTN_TOOL_MOUSE) {
476 input_report_key(input, BTN_LEFT, 0);
477 input_report_key(input, BTN_RIGHT, 0);
478 input_report_key(input, BTN_MIDDLE, 0);
479 input_report_abs(input, ABS_DISTANCE,
Daniel Mack987a6c02010-08-02 20:15:17 -0700480 input_abs_get_max(input, ABS_DISTANCE));
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200481 } else {
482 input_report_key(input, BTN_TOUCH, 0);
483 input_report_key(input, BTN_STYLUS, 0);
484 input_report_key(input, BTN_STYLUS2, 0);
485 input_report_abs(input, ABS_PRESSURE, 0);
486 }
487 input_report_key(input, wdata->tool, 0);
488 input_sync(input);
489 }
490 wdata->tool = tool;
491 if (tool)
492 input_report_key(input, tool, 1);
493 }
494
495 if (tool) {
496 input_report_abs(input, ABS_X, x);
497 input_report_abs(input, ABS_Y, y);
498
499 switch ((data[1] >> 5) & 3) {
500 case 2: /* Mouse with wheel */
501 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
502 rw = (data[6] & 0x01) ? -1 :
503 (data[6] & 0x02) ? 1 : 0;
504 input_report_rel(input, REL_WHEEL, rw);
505 /* fall through */
506
507 case 3: /* Mouse without wheel */
508 input_report_key(input, BTN_LEFT, data[1] & 0x01);
509 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
510 /* Compute distance between mouse and tablet */
511 rw = 44 - (data[6] >> 2);
512 if (rw < 0)
513 rw = 0;
514 else if (rw > 31)
515 rw = 31;
516 input_report_abs(input, ABS_DISTANCE, rw);
517 break;
518
519 default:
520 input_report_abs(input, ABS_PRESSURE,
521 data[6] | (((__u16) (data[1] & 0x08)) << 5));
522 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
523 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
524 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
525 break;
526 }
527
528 input_sync(input);
529 }
530
531 /* Report the state of the two buttons at the top of the tablet
532 * as two extra fingerpad keys (buttons 4 & 5). */
533 rw = data[7] & 0x03;
534 if (rw != wdata->butstate) {
535 wdata->butstate = rw;
536 input_report_key(input, BTN_0, rw & 0x02);
537 input_report_key(input, BTN_1, rw & 0x01);
Przemo Firszt0e253fd2010-01-09 15:20:03 +0100538 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200539 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
540 input_sync(input);
541 }
542
Przemo Firszte31e3832012-03-22 18:54:07 +0000543 /* Store current battery capacity and power supply state*/
Przemo Firszt59d23342010-03-15 19:16:23 +0000544 rw = (data[7] >> 2 & 0x07);
Przemo Firszte31e3832012-03-22 18:54:07 +0000545 if (rw != wdata->power_raw) {
546 wdata->power_raw = rw;
547 wdata->battery_capacity = batcap_gr[rw];
548 if (rw == 7)
549 wdata->ps_connected = 1;
550 else
551 wdata->ps_connected = 0;
552 }
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200553 return 1;
554}
Przemo Firszt78761ff2011-11-05 11:28:22 +0000555
Przemo Firszt6245bde2012-02-28 17:19:05 +0000556static void wacom_i4_parse_button_report(struct wacom_data *wdata,
557 struct input_dev *input, unsigned char *data)
558{
559 __u16 new_butstate;
Przemo Firszt7e503a32012-03-14 19:55:04 +0000560 __u8 new_whlstate;
561 __u8 sync = 0;
562
563 new_whlstate = data[1];
564 if (new_whlstate != wdata->whlstate) {
565 wdata->whlstate = new_whlstate;
566 if (new_whlstate & 0x80) {
567 input_report_key(input, BTN_TOUCH, 1);
568 input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
569 input_report_key(input, BTN_TOOL_FINGER, 1);
570 } else {
571 input_report_key(input, BTN_TOUCH, 0);
572 input_report_abs(input, ABS_WHEEL, 0);
573 input_report_key(input, BTN_TOOL_FINGER, 0);
574 }
575 sync = 1;
576 }
Przemo Firszt6245bde2012-02-28 17:19:05 +0000577
578 new_butstate = (data[3] << 1) | (data[2] & 0x01);
579 if (new_butstate != wdata->butstate) {
580 wdata->butstate = new_butstate;
581 input_report_key(input, BTN_0, new_butstate & 0x001);
582 input_report_key(input, BTN_1, new_butstate & 0x002);
583 input_report_key(input, BTN_2, new_butstate & 0x004);
584 input_report_key(input, BTN_3, new_butstate & 0x008);
585 input_report_key(input, BTN_4, new_butstate & 0x010);
586 input_report_key(input, BTN_5, new_butstate & 0x020);
587 input_report_key(input, BTN_6, new_butstate & 0x040);
588 input_report_key(input, BTN_7, new_butstate & 0x080);
589 input_report_key(input, BTN_8, new_butstate & 0x100);
590 input_report_key(input, BTN_TOOL_FINGER, 1);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000591 sync = 1;
592 }
593
594 if (sync) {
Przemo Firszt6245bde2012-02-28 17:19:05 +0000595 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
596 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
597 input_sync(input);
598 }
599}
600
Przemo Firszt78761ff2011-11-05 11:28:22 +0000601static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
602 struct input_dev *input, unsigned char *data)
603{
604 __u16 x, y, pressure;
Przemo Firszte0829e92012-02-28 17:19:04 +0000605 __u8 distance;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100606 __u8 tilt_x, tilt_y;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000607
608 switch (data[1]) {
609 case 0x80: /* Out of proximity report */
Przemo Firszt78761ff2011-11-05 11:28:22 +0000610 input_report_key(input, BTN_TOUCH, 0);
611 input_report_abs(input, ABS_PRESSURE, 0);
Przemo Firszt693f45b2012-03-09 13:20:51 +0000612 input_report_key(input, BTN_STYLUS, 0);
613 input_report_key(input, BTN_STYLUS2, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000614 input_report_key(input, wdata->tool, 0);
Przemo Firszt24709002012-02-24 13:52:32 +0000615 input_report_abs(input, ABS_MISC, 0);
616 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
Przemo Firszt32db7372012-02-19 20:20:29 +0000617 wdata->tool = 0;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000618 input_sync(input);
619 break;
620 case 0xC2: /* Tool report */
Przemo Firszt24709002012-02-24 13:52:32 +0000621 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
Przemo Firszt78761ff2011-11-05 11:28:22 +0000622 ((data[7] & 0x0f) << 20) |
Przemo Firszt24709002012-02-24 13:52:32 +0000623 ((data[8] & 0xf0) << 12));
624 wdata->serial = ((data[3] & 0x0f) << 28) +
625 (data[4] << 20) + (data[5] << 12) +
626 (data[6] << 4) + (data[7] >> 4);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000627
Przemo Firszt24709002012-02-24 13:52:32 +0000628 switch (wdata->id) {
629 case 0x100802:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000630 wdata->tool = BTN_TOOL_PEN;
631 break;
Przemo Firszt24709002012-02-24 13:52:32 +0000632 case 0x10080A:
Przemo Firszt78761ff2011-11-05 11:28:22 +0000633 wdata->tool = BTN_TOOL_RUBBER;
634 break;
635 }
636 break;
637 default: /* Position/pressure report */
638 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
639 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
640 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
641 | (data[1] & 0x01);
Przemo Firszte0829e92012-02-28 17:19:04 +0000642 distance = (data[9] >> 2) & 0x3f;
Przemo Firsztc653dab2012-05-04 18:00:10 +0100643 tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
644 tilt_y = data[8] & 0x7f;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000645
646 input_report_key(input, BTN_TOUCH, pressure > 1);
647
648 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
649 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
650 input_report_key(input, wdata->tool, 1);
651 input_report_abs(input, ABS_X, x);
652 input_report_abs(input, ABS_Y, y);
653 input_report_abs(input, ABS_PRESSURE, pressure);
Przemo Firszte0829e92012-02-28 17:19:04 +0000654 input_report_abs(input, ABS_DISTANCE, distance);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100655 input_report_abs(input, ABS_TILT_X, tilt_x);
656 input_report_abs(input, ABS_TILT_Y, tilt_y);
Przemo Firszt24709002012-02-24 13:52:32 +0000657 input_report_abs(input, ABS_MISC, wdata->id);
658 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
659 input_report_key(input, wdata->tool, 1);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000660 input_sync(input);
661 break;
662 }
663
664 return;
665}
666
667static void wacom_i4_parse_report(struct hid_device *hdev,
668 struct wacom_data *wdata,
669 struct input_dev *input, unsigned char *data)
670{
671 switch (data[0]) {
672 case 0x00: /* Empty report */
673 break;
674 case 0x02: /* Pen report */
675 wacom_i4_parse_pen_report(wdata, input, data);
676 break;
677 case 0x03: /* Features Report */
678 wdata->features = data[2];
679 break;
680 case 0x0C: /* Button report */
Przemo Firszt6245bde2012-02-28 17:19:05 +0000681 wacom_i4_parse_button_report(wdata, input, data);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000682 break;
683 default:
684 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
685 break;
686 }
687}
688
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000689static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
690 u8 *raw_data, int size)
691{
692 struct wacom_data *wdata = hid_get_drvdata(hdev);
693 struct hid_input *hidinput;
694 struct input_dev *input;
695 unsigned char *data = (unsigned char *) raw_data;
Przemo Firszt78761ff2011-11-05 11:28:22 +0000696 int i;
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000697 __u8 power_raw;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000698
699 if (!(hdev->claimed & HID_CLAIMED_INPUT))
700 return 0;
701
702 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
703 input = hidinput->input;
704
Przemo Firszt78761ff2011-11-05 11:28:22 +0000705 switch (hdev->product) {
706 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
Przemo Firszt163a6ae2012-05-15 19:32:25 +0100707 if (data[0] == 0x03) {
708 return wacom_gr_parse_report(hdev, wdata, input, data);
709 } else {
710 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
711 data[0], data[1], size);
712 return 0;
713 }
Przemo Firszt78761ff2011-11-05 11:28:22 +0000714 break;
715 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
716 i = 1;
717
718 switch (data[0]) {
719 case 0x04:
720 wacom_i4_parse_report(hdev, wdata, input, data + i);
721 i += 10;
722 /* fall through */
723 case 0x03:
724 wacom_i4_parse_report(hdev, wdata, input, data + i);
725 i += 10;
726 wacom_i4_parse_report(hdev, wdata, input, data + i);
Przemo Firszt4202f1d2012-03-22 18:54:08 +0000727 power_raw = data[i+10];
728 if (power_raw != wdata->power_raw) {
729 wdata->power_raw = power_raw;
730 wdata->battery_capacity = batcap_i4[power_raw & 0x07];
731 wdata->ps_connected = power_raw & 0x08;
732 }
733
Przemo Firszt78761ff2011-11-05 11:28:22 +0000734 break;
735 default:
736 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
737 data[0], data[1], size);
738 return 0;
739 }
740 }
741 return 1;
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000742}
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200743
David Herrmann3797ef62011-10-08 23:20:17 +0200744static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
745 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
746 int *max)
747{
748 struct input_dev *input = hi->input;
749
Jiri Kosinaf6f12422011-10-25 09:58:12 +0200750 __set_bit(INPUT_PROP_POINTER, input->propbit);
751
David Herrmann3797ef62011-10-08 23:20:17 +0200752 /* Basics */
753 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
754
755 __set_bit(REL_WHEEL, input->relbit);
756
757 __set_bit(BTN_TOOL_PEN, input->keybit);
758 __set_bit(BTN_TOUCH, input->keybit);
759 __set_bit(BTN_STYLUS, input->keybit);
760 __set_bit(BTN_STYLUS2, input->keybit);
761 __set_bit(BTN_LEFT, input->keybit);
762 __set_bit(BTN_RIGHT, input->keybit);
763 __set_bit(BTN_MIDDLE, input->keybit);
764
765 /* Pad */
Przemo Firszt9a911da2012-03-14 19:55:03 +0000766 input_set_capability(input, EV_MSC, MSC_SERIAL);
David Herrmann3797ef62011-10-08 23:20:17 +0200767
768 __set_bit(BTN_0, input->keybit);
769 __set_bit(BTN_1, input->keybit);
770 __set_bit(BTN_TOOL_FINGER, input->keybit);
771
772 /* Distance, rubber and mouse */
773 __set_bit(BTN_TOOL_RUBBER, input->keybit);
774 __set_bit(BTN_TOOL_MOUSE, input->keybit);
775
Przemo Firszt78761ff2011-11-05 11:28:22 +0000776 switch (hdev->product) {
777 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
778 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
779 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
780 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
781 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
782 break;
783 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
Przemo Firszt7e503a32012-03-14 19:55:04 +0000784 __set_bit(ABS_WHEEL, input->absbit);
Przemo Firszt2c653e62012-02-24 13:47:48 +0000785 __set_bit(ABS_MISC, input->absbit);
Przemo Firszt6245bde2012-02-28 17:19:05 +0000786 __set_bit(BTN_2, input->keybit);
787 __set_bit(BTN_3, input->keybit);
788 __set_bit(BTN_4, input->keybit);
789 __set_bit(BTN_5, input->keybit);
790 __set_bit(BTN_6, input->keybit);
791 __set_bit(BTN_7, input->keybit);
792 __set_bit(BTN_8, input->keybit);
Przemo Firszt7e503a32012-03-14 19:55:04 +0000793 input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000794 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
795 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
796 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
Przemo Firszte0829e92012-02-28 17:19:04 +0000797 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
Przemo Firsztc653dab2012-05-04 18:00:10 +0100798 input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
799 input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
Przemo Firszt78761ff2011-11-05 11:28:22 +0000800 break;
801 }
David Herrmann3797ef62011-10-08 23:20:17 +0200802
803 return 0;
804}
805
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200806static int wacom_probe(struct hid_device *hdev,
807 const struct hid_device_id *id)
808{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200809 struct wacom_data *wdata;
810 int ret;
811
812 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
813 if (wdata == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800814 hid_err(hdev, "can't alloc wacom descriptor\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200815 return -ENOMEM;
816 }
817
818 hid_set_drvdata(hdev, wdata);
819
Bastien Nocera46a709b2010-01-20 12:00:53 +0000820 /* Parse the HID report now */
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200821 ret = hid_parse(hdev);
822 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800823 hid_err(hdev, "parse failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200824 goto err_free;
825 }
826
827 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
828 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800829 hid_err(hdev, "hw start failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200830 goto err_free;
831 }
832
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000833 ret = device_create_file(&hdev->dev, &dev_attr_speed);
834 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800835 hid_warn(hdev,
836 "can't create sysfs speed attribute err: %d\n", ret);
Jiri Kosina342f31e2010-02-03 15:52:31 +0100837
Przemo Firszte3c399e2012-07-31 18:27:56 +0100838#define OLED_INIT(OLED_ID) \
839 do { \
840 ret = device_create_file(&hdev->dev, \
841 &dev_attr_oled##OLED_ID##_img); \
842 if (ret) \
843 hid_warn(hdev, \
844 "can't create sysfs oled attribute, err: %d\n", ret);\
845 } while (0)
846
847OLED_INIT(0);
848OLED_INIT(1);
849OLED_INIT(2);
850OLED_INIT(3);
851OLED_INIT(4);
852OLED_INIT(5);
853OLED_INIT(6);
854OLED_INIT(7);
855
Przemo Firsztd70a2ff2012-05-15 19:32:28 +0100856 wdata->features = 0;
857 wacom_set_features(hdev, 1);
858
859 if (hdev->product == USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) {
Przemo Firszta72c5dd2012-02-17 17:47:10 +0000860 sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
Przemo Firsztd13f5452012-05-10 19:23:52 +0100861 ret = wacom_initialize_leds(hdev);
862 if (ret) {
863 hid_warn(hdev,
864 "can't create led attribute, err: %d\n", ret);
865 goto destroy_leds;
866 }
Przemo Firszt78761ff2011-11-05 11:28:22 +0000867 }
Bastien Nocera46a709b2010-01-20 12:00:53 +0000868
Przemo Firszt59d23342010-03-15 19:16:23 +0000869 wdata->battery.properties = wacom_battery_props;
870 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
871 wdata->battery.get_property = wacom_battery_get_property;
872 wdata->battery.name = "wacom_battery";
873 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
874 wdata->battery.use_for_apm = 0;
Bastien Nocera46a709b2010-01-20 12:00:53 +0000875
Jeremy Fitzhardinge35b4c012011-12-09 09:35:00 -0800876
Przemo Firszt59d23342010-03-15 19:16:23 +0000877 ret = power_supply_register(&hdev->dev, &wdata->battery);
878 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800879 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
880 ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200881 goto err_battery;
Przemo Firszt59d23342010-03-15 19:16:23 +0000882 }
883
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000884 power_supply_powers(&wdata->battery, &hdev->dev);
885
Przemo Firszt59d23342010-03-15 19:16:23 +0000886 wdata->ac.properties = wacom_ac_props;
887 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
888 wdata->ac.get_property = wacom_ac_get_property;
889 wdata->ac.name = "wacom_ac";
890 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
891 wdata->ac.use_for_apm = 0;
892
893 ret = power_supply_register(&hdev->dev, &wdata->ac);
894 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800895 hid_warn(hdev,
896 "can't create ac battery attribute, err: %d\n", ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200897 goto err_ac;
Przemo Firszt59d23342010-03-15 19:16:23 +0000898 }
Przemo Firsztd7cb3db2012-02-05 22:35:24 +0000899
900 power_supply_powers(&wdata->ac, &hdev->dev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200901 return 0;
Daniel Mack987a6c02010-08-02 20:15:17 -0700902
David Herrmanndde58cf2011-09-05 18:45:28 +0200903err_ac:
904 power_supply_unregister(&wdata->battery);
905err_battery:
906 device_remove_file(&hdev->dev, &dev_attr_speed);
907 hid_hw_stop(hdev);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100908destroy_leds:
909 wacom_destroy_leds(hdev);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200910err_free:
911 kfree(wdata);
912 return ret;
913}
914
915static void wacom_remove(struct hid_device *hdev)
916{
Przemo Firszt59d23342010-03-15 19:16:23 +0000917 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firsztd13f5452012-05-10 19:23:52 +0100918
919 wacom_destroy_leds(hdev);
Przemo Firszte3c399e2012-07-31 18:27:56 +0100920 device_remove_file(&hdev->dev, &dev_attr_oled0_img);
921 device_remove_file(&hdev->dev, &dev_attr_oled1_img);
922 device_remove_file(&hdev->dev, &dev_attr_oled2_img);
923 device_remove_file(&hdev->dev, &dev_attr_oled3_img);
924 device_remove_file(&hdev->dev, &dev_attr_oled4_img);
925 device_remove_file(&hdev->dev, &dev_attr_oled5_img);
926 device_remove_file(&hdev->dev, &dev_attr_oled6_img);
927 device_remove_file(&hdev->dev, &dev_attr_oled7_img);
David Herrmann90866172011-09-05 18:45:29 +0200928 device_remove_file(&hdev->dev, &dev_attr_speed);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200929 hid_hw_stop(hdev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000930
Przemo Firszt59d23342010-03-15 19:16:23 +0000931 power_supply_unregister(&wdata->battery);
932 power_supply_unregister(&wdata->ac);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200933 kfree(hid_get_drvdata(hdev));
934}
935
936static const struct hid_device_id wacom_devices[] = {
937 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Przemo Firszt78761ff2011-11-05 11:28:22 +0000938 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
Jiri Kosinad5e0a062010-07-20 17:48:48 +0200939
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200940 { }
941};
942MODULE_DEVICE_TABLE(hid, wacom_devices);
943
944static struct hid_driver wacom_driver = {
945 .name = "wacom",
946 .id_table = wacom_devices,
947 .probe = wacom_probe,
948 .remove = wacom_remove,
949 .raw_event = wacom_raw_event,
David Herrmann3797ef62011-10-08 23:20:17 +0200950 .input_mapped = wacom_input_mapped,
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200951};
952
Peter Huewea24f423b2009-07-02 19:08:38 +0200953static int __init wacom_init(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200954{
955 int ret;
956
957 ret = hid_register_driver(&wacom_driver);
958 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800959 pr_err("can't register wacom driver\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200960 return ret;
961}
962
Peter Huewea24f423b2009-07-02 19:08:38 +0200963static void __exit wacom_exit(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200964{
965 hid_unregister_driver(&wacom_driver);
966}
967
968module_init(wacom_init);
969module_exit(wacom_exit);
Przemo Firszt1fa2f722012-03-28 22:13:37 +0100970MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200971MODULE_LICENSE("GPL");