hid-wacom.c 24.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 *  Bluetooth Wacom Tablet support
 *
 *  Copyright (c) 1999 Andreas Gal
 *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
 *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
 *  Copyright (c) 2006-2007 Jiri Kosina
 *  Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
 *  Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>
 *  Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>
11
 *  Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
12 13 14 15 16 17 18 19 20
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 */

21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

23 24 25
#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>
26
#include <linux/leds.h>
27
#include <linux/slab.h>
28
#include <linux/power_supply.h>
29 30 31

#include "hid-ids.h"

32 33
#define PAD_DEVICE_ID	0x0F

34
#define WAC_CMD_LED_CONTROL     0x20
35 36
#define WAC_CMD_ICON_START_STOP     0x21
#define WAC_CMD_ICON_TRANSFER       0x26
37

38 39
struct wacom_data {
	__u16 tool;
40
	__u16 butstate;
41
	__u8 whlstate;
42
	__u8 features;
43 44
	__u32 id;
	__u32 serial;
45
	unsigned char high_speed;
46 47 48
	__u8 battery_capacity;
	__u8 power_raw;
	__u8 ps_connected;
49 50
	struct power_supply battery;
	struct power_supply ac;
51 52
	__u8 led_selector;
	struct led_classdev *leds[4];
53 54
};

55 56 57
/*percent of battery capacity for Graphire
  8th value means AC online and show 100% capacity */
static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
58 59
/*percent of battery capacity for Intuos4 WL, AC has a separate bit*/
static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
60 61 62

static enum power_supply_property wacom_battery_props[] = {
	POWER_SUPPLY_PROP_PRESENT,
63 64
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_SCOPE,
65 66 67 68
};

static enum power_supply_property wacom_ac_props[] = {
	POWER_SUPPLY_PROP_PRESENT,
69 70
	POWER_SUPPLY_PROP_ONLINE,
	POWER_SUPPLY_PROP_SCOPE,
71 72
};

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
static void wacom_scramble(__u8 *image)
{
	__u16 mask;
	__u16 s1;
	__u16 s2;
	__u16 r1 ;
	__u16 r2 ;
	__u16 r;
	__u8 buf[256];
	int i, w, x, y, z;

	for (x = 0; x < 32; x++) {
		for (y = 0; y < 8; y++)
			buf[(8 * x) + (7 - y)] = image[(8 * x) + y];
	}

	/* Change 76543210 into GECA6420 as required by Intuos4 WL
	 *        HGFEDCBA      HFDB7531
	 */
	for (x = 0; x < 4; x++) {
		for (y = 0; y < 4; y++) {
			for (z = 0; z < 8; z++) {
				mask = 0x0001;
				r1 = 0;
				r2 = 0;
				i = (x << 6) + (y << 4) + z;
				s1 = buf[i];
				s2 = buf[i+8];
				for (w = 0; w < 8; w++) {
					r1 |= (s1 & mask);
					r2 |= (s2 & mask);
					s1 <<= 1;
					s2 <<= 1;
					mask <<= 2;
				}
				r = r1 | (r2 << 1);
				i = (x << 6) + (y << 4) + (z << 1);
				image[i] = 0xFF & r;
				image[i+1] = (0xFF00 & r) >> 8;
			}
		}
	}
}

static void wacom_set_image(struct hid_device *hdev, const char *image,
						__u8 icon_no)
{
	__u8 rep_data[68];
	__u8 p[256];
	int ret, i, j;

	for (i = 0; i < 256; i++)
		p[i] = image[i];

	rep_data[0] = WAC_CMD_ICON_START_STOP;
	rep_data[1] = 0;
	ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
				HID_FEATURE_REPORT);
	if (ret < 0)
		goto err;

	rep_data[0] = WAC_CMD_ICON_TRANSFER;
	rep_data[1] = icon_no & 0x07;

	wacom_scramble(p);

	for (i = 0; i < 4; i++) {
		for (j = 0; j < 64; j++)
			rep_data[j + 3] = p[(i << 6) + j];

		rep_data[2] = i;
		ret = hdev->hid_output_raw_report(hdev, rep_data, 67,
					HID_FEATURE_REPORT);
	}

	rep_data[0] = WAC_CMD_ICON_START_STOP;
	rep_data[1] = 0;

	ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
				HID_FEATURE_REPORT);

err:
	return;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static void wacom_leds_set_brightness(struct led_classdev *led_dev,
						enum led_brightness value)
{
	struct device *dev = led_dev->dev->parent;
	struct hid_device *hdev;
	struct wacom_data *wdata;
	unsigned char *buf;
	__u8 led = 0;
	int i;

	hdev = container_of(dev, struct hid_device, dev);
	wdata = hid_get_drvdata(hdev);
	for (i = 0; i < 4; ++i) {
		if (wdata->leds[i] == led_dev)
			wdata->led_selector = i;
	}

	led = wdata->led_selector | 0x04;
	buf = kzalloc(9, GFP_KERNEL);
	if (buf) {
		buf[0] = WAC_CMD_LED_CONTROL;
		buf[1] = led;
180 181
		buf[2] = value >> 2;
		buf[3] = value;
182 183
		/* use fixed brightness for OLEDs */
		buf[4] = 0x08;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		hdev->hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT);
		kfree(buf);
	}

	return;
}

static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)
{
	struct wacom_data *wdata;
	struct device *dev = led_dev->dev->parent;
	int value = 0;
	int i;

	wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));

	for (i = 0; i < 4; ++i) {
		if (wdata->leds[i] == led_dev) {
			value = wdata->leds[i]->brightness;
			break;
		}
	}

	return value;
}


static int wacom_initialize_leds(struct hid_device *hdev)
{
	struct wacom_data *wdata = hid_get_drvdata(hdev);
	struct led_classdev *led;
	struct device *dev = &hdev->dev;
	size_t namesz = strlen(dev_name(dev)) + 12;
	char *name;
	int i, ret;

	wdata->led_selector = 0;

	for (i = 0; i < 4; i++) {
		led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
		if (!led) {
			hid_warn(hdev,
				 "can't allocate memory for LED selector\n");
			ret = -ENOMEM;
			goto err;
		}

		name = (void *)&led[1];
		snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);
		led->name = name;
		led->brightness = 0;
		led->max_brightness = 127;
		led->brightness_get = wacom_leds_get_brightness;
		led->brightness_set = wacom_leds_set_brightness;

		wdata->leds[i] = led;

		ret = led_classdev_register(dev, wdata->leds[i]);

		if (ret) {
			wdata->leds[i] = NULL;
			kfree(led);
			hid_warn(hdev, "can't register LED\n");
			goto err;
		}
	}

err:
	return ret;
}

static void wacom_destroy_leds(struct hid_device *hdev)
{
	struct wacom_data *wdata = hid_get_drvdata(hdev);
	struct led_classdev *led;
	int i;

	for (i = 0; i < 4; ++i) {
		if (wdata->leds[i]) {
			led = wdata->leds[i];
			wdata->leds[i] = NULL;
			led_classdev_unregister(led);
			kfree(led);
		}
	}

}

272 273 274 275 276 277 278 279 280 281 282 283
static int wacom_battery_get_property(struct power_supply *psy,
				enum power_supply_property psp,
				union power_supply_propval *val)
{
	struct wacom_data *wdata = container_of(psy,
					struct wacom_data, battery);
	int ret = 0;

	switch (psp) {
	case POWER_SUPPLY_PROP_PRESENT:
		val->intval = 1;
		break;
284 285 286
	case POWER_SUPPLY_PROP_SCOPE:
		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
		break;
287
	case POWER_SUPPLY_PROP_CAPACITY:
288
		val->intval = wdata->battery_capacity;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		break;
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}

static int wacom_ac_get_property(struct power_supply *psy,
				enum power_supply_property psp,
				union power_supply_propval *val)
{
	struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
	int ret = 0;

	switch (psp) {
	case POWER_SUPPLY_PROP_PRESENT:
		/* fall through */
	case POWER_SUPPLY_PROP_ONLINE:
308
		val->intval = wdata->ps_connected;
309
		break;
310 311 312
	case POWER_SUPPLY_PROP_SCOPE:
		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
		break;
313 314 315 316 317 318 319
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}

320
static void wacom_set_features(struct hid_device *hdev, u8 speed)
321
{
322
	struct wacom_data *wdata = hid_get_drvdata(hdev);
323
	int limit, ret;
P
Przemo Firszt 已提交
324
	__u8 rep_data[2];
325

P
Przemo Firszt 已提交
326 327 328
	switch (hdev->product) {
	case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
		rep_data[0] = 0x03 ; rep_data[1] = 0x00;
329 330 331 332 333 334
		limit = 3;
		do {
			ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
					HID_FEATURE_REPORT);
		} while (ret < 0 && limit-- > 0);

335
		if (ret >= 0) {
P
Przemo Firszt 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
			if (speed == 0)
				rep_data[0] = 0x05;
			else
				rep_data[0] = 0x06;

			rep_data[1] = 0x00;
			limit = 3;
			do {
				ret = hdev->hid_output_raw_report(hdev,
					rep_data, 2, HID_FEATURE_REPORT);
			} while (ret < 0 && limit-- > 0);

			if (ret >= 0) {
				wdata->high_speed = speed;
				return;
			}
352
		}
P
Przemo Firszt 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

		/*
		 * Note that if the raw queries fail, it's not a hard failure
		 * and it is safe to continue
		 */
		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
			 rep_data[0], ret);
		break;
	case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
		if (speed == 1)
			wdata->features &= ~0x20;
		else
			wdata->features |= 0x20;

		rep_data[0] = 0x03;
		rep_data[1] = wdata->features;

		ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
					HID_FEATURE_REPORT);
		if (ret >= 0)
			wdata->high_speed = speed;
		break;
375 376 377 378 379
	}

	return;
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
static ssize_t wacom_show_speed(struct device *dev,
				struct device_attribute
				*attr, char *buf)
{
	struct wacom_data *wdata = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
}

static ssize_t wacom_store_speed(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
	int new_speed;

	if (sscanf(buf, "%1d", &new_speed ) != 1)
		return -EINVAL;

	if (new_speed == 0 || new_speed == 1) {
P
Przemo Firszt 已提交
400
		wacom_set_features(hdev, new_speed);
401 402 403 404 405
		return strnlen(buf, PAGE_SIZE);
	} else
		return -EINVAL;
}

406
static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
407 408
		wacom_show_speed, wacom_store_speed);

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
#define WACOM_STORE(OLED_ID)						\
static ssize_t wacom_oled##OLED_ID##_store(struct device *dev,		\
				struct device_attribute *attr,		\
				const char *buf, size_t count)		\
{									\
	struct hid_device *hdev = container_of(dev, struct hid_device,	\
				dev);					\
									\
	if (count != 256)						\
		return -EINVAL;						\
									\
	wacom_set_image(hdev, buf, OLED_ID);				\
									\
	return count;							\
}									\
									\
static DEVICE_ATTR(oled##OLED_ID##_img, S_IWUSR | S_IWGRP, NULL,	\
				wacom_oled##OLED_ID##_store)

WACOM_STORE(0);
WACOM_STORE(1);
WACOM_STORE(2);
WACOM_STORE(3);
WACOM_STORE(4);
WACOM_STORE(5);
WACOM_STORE(6);
WACOM_STORE(7);

437 438 439
static int wacom_gr_parse_report(struct hid_device *hdev,
			struct wacom_data *wdata,
			struct input_dev *input, unsigned char *data)
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
{
	int tool, x, y, rw;

	tool = 0;
	/* Get X & Y positions */
	x = le16_to_cpu(*(__le16 *) &data[2]);
	y = le16_to_cpu(*(__le16 *) &data[4]);

	/* Get current tool identifier */
	if (data[1] & 0x90) { /* If pen is in the in/active area */
		switch ((data[1] >> 5) & 3) {
		case 0:	/* Pen */
			tool = BTN_TOOL_PEN;
			break;

		case 1: /* Rubber */
			tool = BTN_TOOL_RUBBER;
			break;

		case 2: /* Mouse with wheel */
		case 3: /* Mouse without wheel */
			tool = BTN_TOOL_MOUSE;
			break;
		}

		/* Reset tool if out of active tablet area */
		if (!(data[1] & 0x10))
			tool = 0;
	}

	/* If tool changed, notify input subsystem */
	if (wdata->tool != tool) {
		if (wdata->tool) {
			/* Completely reset old tool state */
			if (wdata->tool == BTN_TOOL_MOUSE) {
				input_report_key(input, BTN_LEFT, 0);
				input_report_key(input, BTN_RIGHT, 0);
				input_report_key(input, BTN_MIDDLE, 0);
				input_report_abs(input, ABS_DISTANCE,
479
					input_abs_get_max(input, ABS_DISTANCE));
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
			} else {
				input_report_key(input, BTN_TOUCH, 0);
				input_report_key(input, BTN_STYLUS, 0);
				input_report_key(input, BTN_STYLUS2, 0);
				input_report_abs(input, ABS_PRESSURE, 0);
			}
			input_report_key(input, wdata->tool, 0);
			input_sync(input);
		}
		wdata->tool = tool;
		if (tool)
			input_report_key(input, tool, 1);
	}

	if (tool) {
		input_report_abs(input, ABS_X, x);
		input_report_abs(input, ABS_Y, y);

		switch ((data[1] >> 5) & 3) {
		case 2: /* Mouse with wheel */
			input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
			rw = (data[6] & 0x01) ? -1 :
				(data[6] & 0x02) ? 1 : 0;
			input_report_rel(input, REL_WHEEL, rw);
			/* fall through */

		case 3: /* Mouse without wheel */
			input_report_key(input, BTN_LEFT, data[1] & 0x01);
			input_report_key(input, BTN_RIGHT, data[1] & 0x02);
			/* Compute distance between mouse and tablet */
			rw = 44 - (data[6] >> 2);
			if (rw < 0)
				rw = 0;
			else if (rw > 31)
				rw = 31;
			input_report_abs(input, ABS_DISTANCE, rw);
			break;

		default:
			input_report_abs(input, ABS_PRESSURE,
					data[6] | (((__u16) (data[1] & 0x08)) << 5));
			input_report_key(input, BTN_TOUCH, data[1] & 0x01);
			input_report_key(input, BTN_STYLUS, data[1] & 0x02);
			input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
			break;
		}

		input_sync(input);
	}

	/* Report the state of the two buttons at the top of the tablet
	 * as two extra fingerpad keys (buttons 4 & 5). */
	rw = data[7] & 0x03;
	if (rw != wdata->butstate) {
		wdata->butstate = rw;
		input_report_key(input, BTN_0, rw & 0x02);
		input_report_key(input, BTN_1, rw & 0x01);
537
		input_report_key(input, BTN_TOOL_FINGER, 0xf0);
538 539 540 541
		input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
		input_sync(input);
	}

542
	/* Store current battery capacity and power supply state*/
543
	rw = (data[7] >> 2 & 0x07);
544 545 546 547 548 549 550 551
	if (rw != wdata->power_raw) {
		wdata->power_raw = rw;
		wdata->battery_capacity = batcap_gr[rw];
		if (rw == 7)
			wdata->ps_connected = 1;
		else
			wdata->ps_connected = 0;
	}
552 553
	return 1;
}
554

555 556 557 558
static void wacom_i4_parse_button_report(struct wacom_data *wdata,
			struct input_dev *input, unsigned char *data)
{
	__u16 new_butstate;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	__u8 new_whlstate;
	__u8 sync = 0;

	new_whlstate = data[1];
	if (new_whlstate != wdata->whlstate) {
		wdata->whlstate = new_whlstate;
		if (new_whlstate & 0x80) {
			input_report_key(input, BTN_TOUCH, 1);
			input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
			input_report_key(input, BTN_TOOL_FINGER, 1);
		} else {
			input_report_key(input, BTN_TOUCH, 0);
			input_report_abs(input, ABS_WHEEL, 0);
			input_report_key(input, BTN_TOOL_FINGER, 0);
		}
		sync = 1;
	}
576 577 578 579 580 581 582 583 584 585 586 587 588 589

	new_butstate = (data[3] << 1) | (data[2] & 0x01);
	if (new_butstate != wdata->butstate) {
		wdata->butstate = new_butstate;
		input_report_key(input, BTN_0, new_butstate & 0x001);
		input_report_key(input, BTN_1, new_butstate & 0x002);
		input_report_key(input, BTN_2, new_butstate & 0x004);
		input_report_key(input, BTN_3, new_butstate & 0x008);
		input_report_key(input, BTN_4, new_butstate & 0x010);
		input_report_key(input, BTN_5, new_butstate & 0x020);
		input_report_key(input, BTN_6, new_butstate & 0x040);
		input_report_key(input, BTN_7, new_butstate & 0x080);
		input_report_key(input, BTN_8, new_butstate & 0x100);
		input_report_key(input, BTN_TOOL_FINGER, 1);
590 591 592 593
		sync = 1;
	}

	if (sync) {
594 595 596 597 598 599
		input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
		input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
		input_sync(input);
	}
}

600 601 602 603
static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
			struct input_dev *input, unsigned char *data)
{
	__u16 x, y, pressure;
604
	__u8 distance;
605
	__u8 tilt_x, tilt_y;
606 607 608 609 610

	switch (data[1]) {
	case 0x80: /* Out of proximity report */
		input_report_key(input, BTN_TOUCH, 0);
		input_report_abs(input, ABS_PRESSURE, 0);
611 612
		input_report_key(input, BTN_STYLUS, 0);
		input_report_key(input, BTN_STYLUS2, 0);
613
		input_report_key(input, wdata->tool, 0);
614 615
		input_report_abs(input, ABS_MISC, 0);
		input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
616
		wdata->tool = 0;
617 618 619
		input_sync(input);
		break;
	case 0xC2: /* Tool report */
620
		wdata->id = ((data[2] << 4) | (data[3] >> 4) |
621
			((data[7] & 0x0f) << 20) |
622 623 624 625
			((data[8] & 0xf0) << 12));
		wdata->serial = ((data[3] & 0x0f) << 28) +
				(data[4] << 20) + (data[5] << 12) +
				(data[6] << 4) + (data[7] >> 4);
626

627 628
		switch (wdata->id) {
		case 0x100802:
629 630
			wdata->tool = BTN_TOOL_PEN;
			break;
631
		case 0x10080A:
632 633 634 635 636 637 638 639 640
			wdata->tool = BTN_TOOL_RUBBER;
			break;
		}
		break;
	default: /* Position/pressure report */
		x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
		y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
		pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
			| (data[1] & 0x01);
641
		distance = (data[9] >> 2) & 0x3f;
642 643
		tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);
		tilt_y = data[8] & 0x7f;
644 645 646 647 648 649 650 651 652

		input_report_key(input, BTN_TOUCH, pressure > 1);

		input_report_key(input, BTN_STYLUS, data[1] & 0x02);
		input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
		input_report_key(input, wdata->tool, 1);
		input_report_abs(input, ABS_X, x);
		input_report_abs(input, ABS_Y, y);
		input_report_abs(input, ABS_PRESSURE, pressure);
653
		input_report_abs(input, ABS_DISTANCE, distance);
654 655
		input_report_abs(input, ABS_TILT_X, tilt_x);
		input_report_abs(input, ABS_TILT_Y, tilt_y);
656 657 658
		input_report_abs(input, ABS_MISC, wdata->id);
		input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
		input_report_key(input, wdata->tool, 1);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
		input_sync(input);
		break;
	}

	return;
}

static void wacom_i4_parse_report(struct hid_device *hdev,
			struct wacom_data *wdata,
			struct input_dev *input, unsigned char *data)
{
	switch (data[0]) {
	case 0x00: /* Empty report */
		break;
	case 0x02: /* Pen report */
		wacom_i4_parse_pen_report(wdata, input, data);
		break;
	case 0x03: /* Features Report */
		wdata->features = data[2];
		break;
	case 0x0C: /* Button report */
680
		wacom_i4_parse_button_report(wdata, input, data);
681 682 683 684 685 686 687
		break;
	default:
		hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
		break;
	}
}

688 689 690 691 692 693 694
static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
		u8 *raw_data, int size)
{
	struct wacom_data *wdata = hid_get_drvdata(hdev);
	struct hid_input *hidinput;
	struct input_dev *input;
	unsigned char *data = (unsigned char *) raw_data;
695
	int i;
696
	__u8 power_raw;
697 698 699 700 701 702 703

	if (!(hdev->claimed & HID_CLAIMED_INPUT))
		return 0;

	hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
	input = hidinput->input;

704 705
	switch (hdev->product) {
	case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
706 707 708 709 710 711 712
		if (data[0] == 0x03) {
			return wacom_gr_parse_report(hdev, wdata, input, data);
		} else {
			hid_err(hdev, "Unknown report: %d,%d size:%d\n",
					data[0], data[1], size);
			return 0;
		}
713 714 715 716 717 718 719 720 721 722 723 724 725
		break;
	case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
		i = 1;

		switch (data[0]) {
		case 0x04:
			wacom_i4_parse_report(hdev, wdata, input, data + i);
			i += 10;
			/* fall through */
		case 0x03:
			wacom_i4_parse_report(hdev, wdata, input, data + i);
			i += 10;
			wacom_i4_parse_report(hdev, wdata, input, data + i);
726 727 728 729 730 731 732
			power_raw = data[i+10];
			if (power_raw != wdata->power_raw) {
				wdata->power_raw = power_raw;
				wdata->battery_capacity = batcap_i4[power_raw & 0x07];
				wdata->ps_connected = power_raw & 0x08;
			}

733 734 735 736 737 738 739 740
			break;
		default:
			hid_err(hdev, "Unknown report: %d,%d size:%d\n",
					data[0], data[1], size);
			return 0;
		}
	}
	return 1;
741
}
742

743 744 745 746 747 748
static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
	struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
								int *max)
{
	struct input_dev *input = hi->input;

749 750
	__set_bit(INPUT_PROP_POINTER, input->propbit);

751 752 753 754 755 756 757 758 759 760 761 762 763 764
	/* Basics */
	input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);

	__set_bit(REL_WHEEL, input->relbit);

	__set_bit(BTN_TOOL_PEN, input->keybit);
	__set_bit(BTN_TOUCH, input->keybit);
	__set_bit(BTN_STYLUS, input->keybit);
	__set_bit(BTN_STYLUS2, input->keybit);
	__set_bit(BTN_LEFT, input->keybit);
	__set_bit(BTN_RIGHT, input->keybit);
	__set_bit(BTN_MIDDLE, input->keybit);

	/* Pad */
765
	input_set_capability(input, EV_MSC, MSC_SERIAL);
766 767 768 769 770 771 772 773 774

	__set_bit(BTN_0, input->keybit);
	__set_bit(BTN_1, input->keybit);
	__set_bit(BTN_TOOL_FINGER, input->keybit);

	/* Distance, rubber and mouse */
	__set_bit(BTN_TOOL_RUBBER, input->keybit);
	__set_bit(BTN_TOOL_MOUSE, input->keybit);

775 776 777 778 779 780 781 782
	switch (hdev->product) {
	case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
		input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
		input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
		input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
		input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
		break;
	case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
783
		__set_bit(ABS_WHEEL, input->absbit);
784
		__set_bit(ABS_MISC, input->absbit);
785 786 787 788 789 790 791
		__set_bit(BTN_2, input->keybit);
		__set_bit(BTN_3, input->keybit);
		__set_bit(BTN_4, input->keybit);
		__set_bit(BTN_5, input->keybit);
		__set_bit(BTN_6, input->keybit);
		__set_bit(BTN_7, input->keybit);
		__set_bit(BTN_8, input->keybit);
792
		input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
793 794 795
		input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
		input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
		input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
796
		input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
797 798
		input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);
		input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);
799 800
		break;
	}
801 802 803 804

	return 0;
}

805 806 807 808 809 810 811 812
static int wacom_probe(struct hid_device *hdev,
		const struct hid_device_id *id)
{
	struct wacom_data *wdata;
	int ret;

	wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
	if (wdata == NULL) {
813
		hid_err(hdev, "can't alloc wacom descriptor\n");
814 815 816 817 818
		return -ENOMEM;
	}

	hid_set_drvdata(hdev, wdata);

819
	/* Parse the HID report now */
820 821
	ret = hid_parse(hdev);
	if (ret) {
822
		hid_err(hdev, "parse failed\n");
823 824 825 826 827
		goto err_free;
	}

	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
	if (ret) {
828
		hid_err(hdev, "hw start failed\n");
829 830 831
		goto err_free;
	}

832 833
	ret = device_create_file(&hdev->dev, &dev_attr_speed);
	if (ret)
834 835
		hid_warn(hdev,
			 "can't create sysfs speed attribute err: %d\n", ret);
836

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
#define OLED_INIT(OLED_ID)						\
	do {								\
		ret = device_create_file(&hdev->dev,			\
				&dev_attr_oled##OLED_ID##_img);		\
		if (ret)						\
			hid_warn(hdev,					\
			 "can't create sysfs oled attribute, err: %d\n", ret);\
	} while (0)

OLED_INIT(0);
OLED_INIT(1);
OLED_INIT(2);
OLED_INIT(3);
OLED_INIT(4);
OLED_INIT(5);
OLED_INIT(6);
OLED_INIT(7);

P
Przemo Firszt 已提交
855 856 857 858
	wdata->features = 0;
	wacom_set_features(hdev, 1);

	if (hdev->product == USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) {
859
		sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
860
		ret = wacom_initialize_leds(hdev);
861
		if (ret)
862 863
			hid_warn(hdev,
				 "can't create led attribute, err: %d\n", ret);
864
	}
865

866 867 868 869 870 871
	wdata->battery.properties = wacom_battery_props;
	wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
	wdata->battery.get_property = wacom_battery_get_property;
	wdata->battery.name = "wacom_battery";
	wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
	wdata->battery.use_for_apm = 0;
872

873

874 875
	ret = power_supply_register(&hdev->dev, &wdata->battery);
	if (ret) {
876 877
		hid_err(hdev, "can't create sysfs battery attribute, err: %d\n",
			ret);
878
		goto err_battery;
879 880
	}

881 882
	power_supply_powers(&wdata->battery, &hdev->dev);

883 884 885 886 887 888 889 890 891
	wdata->ac.properties = wacom_ac_props;
	wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
	wdata->ac.get_property = wacom_ac_get_property;
	wdata->ac.name = "wacom_ac";
	wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
	wdata->ac.use_for_apm = 0;

	ret = power_supply_register(&hdev->dev, &wdata->ac);
	if (ret) {
892 893
		hid_err(hdev,
			"can't create ac battery attribute, err: %d\n", ret);
894
		goto err_ac;
895
	}
896 897

	power_supply_powers(&wdata->ac, &hdev->dev);
898
	return 0;
899

900 901 902
err_ac:
	power_supply_unregister(&wdata->battery);
err_battery:
903 904 905 906 907 908 909 910 911
	wacom_destroy_leds(hdev);
	device_remove_file(&hdev->dev, &dev_attr_oled0_img);
	device_remove_file(&hdev->dev, &dev_attr_oled1_img);
	device_remove_file(&hdev->dev, &dev_attr_oled2_img);
	device_remove_file(&hdev->dev, &dev_attr_oled3_img);
	device_remove_file(&hdev->dev, &dev_attr_oled4_img);
	device_remove_file(&hdev->dev, &dev_attr_oled5_img);
	device_remove_file(&hdev->dev, &dev_attr_oled6_img);
	device_remove_file(&hdev->dev, &dev_attr_oled7_img);
912 913
	device_remove_file(&hdev->dev, &dev_attr_speed);
	hid_hw_stop(hdev);
914 915 916 917 918 919 920
err_free:
	kfree(wdata);
	return ret;
}

static void wacom_remove(struct hid_device *hdev)
{
921
	struct wacom_data *wdata = hid_get_drvdata(hdev);
922 923

	wacom_destroy_leds(hdev);
924 925 926 927 928 929 930 931
	device_remove_file(&hdev->dev, &dev_attr_oled0_img);
	device_remove_file(&hdev->dev, &dev_attr_oled1_img);
	device_remove_file(&hdev->dev, &dev_attr_oled2_img);
	device_remove_file(&hdev->dev, &dev_attr_oled3_img);
	device_remove_file(&hdev->dev, &dev_attr_oled4_img);
	device_remove_file(&hdev->dev, &dev_attr_oled5_img);
	device_remove_file(&hdev->dev, &dev_attr_oled6_img);
	device_remove_file(&hdev->dev, &dev_attr_oled7_img);
932
	device_remove_file(&hdev->dev, &dev_attr_speed);
933
	hid_hw_stop(hdev);
934 935 936

	power_supply_unregister(&wdata->battery);
	power_supply_unregister(&wdata->ac);
937 938 939 940 941
	kfree(hid_get_drvdata(hdev));
}

static const struct hid_device_id wacom_devices[] = {
	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
942
	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
943

944 945 946 947 948 949 950 951 952 953
	{ }
};
MODULE_DEVICE_TABLE(hid, wacom_devices);

static struct hid_driver wacom_driver = {
	.name = "wacom",
	.id_table = wacom_devices,
	.probe = wacom_probe,
	.remove = wacom_remove,
	.raw_event = wacom_raw_event,
954
	.input_mapped = wacom_input_mapped,
955 956
};

957
static int __init wacom_init(void)
958 959 960 961 962
{
	int ret;

	ret = hid_register_driver(&wacom_driver);
	if (ret)
963
		pr_err("can't register wacom driver\n");
964 965 966
	return ret;
}

967
static void __exit wacom_exit(void)
968 969 970 971 972 973
{
	hid_unregister_driver(&wacom_driver);
}

module_init(wacom_init);
module_exit(wacom_exit);
974
MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");
975
MODULE_LICENSE("GPL");