Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
   1/*
   2 * drivers/input/tablet/wacom_sys.c
   3 *
   4 *  USB Wacom tablet support - system specific code
   5 */
   6
   7/*
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 */
  13
  14#include "wacom_wac.h"
  15#include "wacom.h"
  16
  17/* defines to get HID report descriptor */
  18#define HID_DEVICET_HID		(USB_TYPE_CLASS | 0x01)
  19#define HID_DEVICET_REPORT	(USB_TYPE_CLASS | 0x02)
  20#define HID_USAGE_UNDEFINED		0x00
  21#define HID_USAGE_PAGE			0x05
  22#define HID_USAGE_PAGE_DIGITIZER	0x0d
  23#define HID_USAGE_PAGE_DESKTOP		0x01
  24#define HID_USAGE			0x09
  25#define HID_USAGE_X			((HID_USAGE_PAGE_DESKTOP << 16) | 0x30)
  26#define HID_USAGE_Y			((HID_USAGE_PAGE_DESKTOP << 16) | 0x31)
  27#define HID_USAGE_PRESSURE		((HID_USAGE_PAGE_DIGITIZER << 16) | 0x30)
  28#define HID_USAGE_X_TILT		((HID_USAGE_PAGE_DIGITIZER << 16) | 0x3d)
  29#define HID_USAGE_Y_TILT		((HID_USAGE_PAGE_DIGITIZER << 16) | 0x3e)
  30#define HID_USAGE_FINGER		((HID_USAGE_PAGE_DIGITIZER << 16) | 0x22)
  31#define HID_USAGE_STYLUS		((HID_USAGE_PAGE_DIGITIZER << 16) | 0x20)
  32#define HID_USAGE_CONTACTMAX		((HID_USAGE_PAGE_DIGITIZER << 16) | 0x55)
  33#define HID_COLLECTION			0xa1
  34#define HID_COLLECTION_LOGICAL		0x02
  35#define HID_COLLECTION_END		0xc0
  36
  37struct hid_descriptor {
  38	struct usb_descriptor_header header;
  39	__le16   bcdHID;
  40	u8       bCountryCode;
  41	u8       bNumDescriptors;
  42	u8       bDescriptorType;
  43	__le16   wDescriptorLength;
  44} __attribute__ ((packed));
  45
  46/* defines to get/set USB message */
  47#define USB_REQ_GET_REPORT	0x01
  48#define USB_REQ_SET_REPORT	0x09
  49
  50#define WAC_HID_FEATURE_REPORT	0x03
  51#define WAC_MSG_RETRIES		5
  52
  53#define WAC_CMD_LED_CONTROL	0x20
  54#define WAC_CMD_ICON_START	0x21
  55#define WAC_CMD_ICON_XFER	0x23
  56#define WAC_CMD_RETRIES		10
  57
  58static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
  59			    void *buf, size_t size, unsigned int retries)
  60{
  61	struct usb_device *dev = interface_to_usbdev(intf);
  62	int retval;
  63
  64	do {
  65		retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  66				USB_REQ_GET_REPORT,
  67				USB_DIR_IN | USB_TYPE_CLASS |
  68				USB_RECIP_INTERFACE,
  69				(type << 8) + id,
  70				intf->altsetting[0].desc.bInterfaceNumber,
  71				buf, size, 100);
  72	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
  73
  74	return retval;
  75}
  76
  77static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
  78			    void *buf, size_t size, unsigned int retries)
  79{
  80	struct usb_device *dev = interface_to_usbdev(intf);
  81	int retval;
  82
  83	do {
  84		retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  85				USB_REQ_SET_REPORT,
  86				USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  87				(type << 8) + id,
  88				intf->altsetting[0].desc.bInterfaceNumber,
  89				buf, size, 1000);
  90	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
  91
  92	return retval;
  93}
  94
  95static void wacom_sys_irq(struct urb *urb)
  96{
  97	struct wacom *wacom = urb->context;
  98	struct device *dev = &wacom->intf->dev;
  99	int retval;
 100
 101	switch (urb->status) {
 102	case 0:
 103		/* success */
 104		break;
 105	case -ECONNRESET:
 106	case -ENOENT:
 107	case -ESHUTDOWN:
 108		/* this urb is terminated, clean up */
 109		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
 110			__func__, urb->status);
 111		return;
 112	default:
 113		dev_dbg(dev, "%s - nonzero urb status received: %d\n",
 114			__func__, urb->status);
 115		goto exit;
 116	}
 117
 118	wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
 119
 120 exit:
 121	usb_mark_last_busy(wacom->usbdev);
 122	retval = usb_submit_urb(urb, GFP_ATOMIC);
 123	if (retval)
 124		dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
 125			__func__, retval);
 126}
 127
 128static int wacom_open(struct input_dev *dev)
 129{
 130	struct wacom *wacom = input_get_drvdata(dev);
 131	int retval = 0;
 132
 133	if (usb_autopm_get_interface(wacom->intf) < 0)
 134		return -EIO;
 135
 136	mutex_lock(&wacom->lock);
 137
 138	if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
 139		retval = -EIO;
 140		goto out;
 141	}
 142
 143	wacom->open = true;
 144	wacom->intf->needs_remote_wakeup = 1;
 145
 146out:
 147	mutex_unlock(&wacom->lock);
 148	usb_autopm_put_interface(wacom->intf);
 149	return retval;
 150}
 151
 152static void wacom_close(struct input_dev *dev)
 153{
 154	struct wacom *wacom = input_get_drvdata(dev);
 155	int autopm_error;
 156
 157	autopm_error = usb_autopm_get_interface(wacom->intf);
 158
 159	mutex_lock(&wacom->lock);
 160	usb_kill_urb(wacom->irq);
 161	wacom->open = false;
 162	wacom->intf->needs_remote_wakeup = 0;
 163	mutex_unlock(&wacom->lock);
 164
 165	if (!autopm_error)
 166		usb_autopm_put_interface(wacom->intf);
 167}
 168
 169/*
 170 * Calculate the resolution of the X or Y axis, given appropriate HID data.
 171 * This function is little more than hidinput_calc_abs_res stripped down.
 172 */
 173static int wacom_calc_hid_res(int logical_extents, int physical_extents,
 174                              unsigned char unit, unsigned char exponent)
 175{
 176	int prev, unit_exponent;
 177
 178	/* Check if the extents are sane */
 179	if (logical_extents <= 0 || physical_extents <= 0)
 180		return 0;
 181
 182	/* Get signed value of nybble-sized twos-compliment exponent */
 183	unit_exponent = exponent;
 184	if (unit_exponent > 7)
 185		unit_exponent -= 16;
 186
 187	/* Convert physical_extents to millimeters */
 188	if (unit == 0x11) {		/* If centimeters */
 189		unit_exponent += 1;
 190	} else if (unit == 0x13) {	/* If inches */
 191		prev = physical_extents;
 192		physical_extents *= 254;
 193		if (physical_extents < prev)
 194			return 0;
 195		unit_exponent -= 1;
 196	} else {
 197		return 0;
 198	}
 199
 200	/* Apply negative unit exponent */
 201	for (; unit_exponent < 0; unit_exponent++) {
 202		prev = logical_extents;
 203		logical_extents *= 10;
 204		if (logical_extents < prev)
 205			return 0;
 206	}
 207	/* Apply positive unit exponent */
 208	for (; unit_exponent > 0; unit_exponent--) {
 209		prev = physical_extents;
 210		physical_extents *= 10;
 211		if (physical_extents < prev)
 212			return 0;
 213	}
 214
 215	/* Calculate resolution */
 216	return logical_extents / physical_extents;
 217}
 218
 219static int wacom_parse_logical_collection(unsigned char *report,
 220					  struct wacom_features *features)
 221{
 222	int length = 0;
 223
 224	if (features->type == BAMBOO_PT) {
 225
 226		/* Logical collection is only used by 3rd gen Bamboo Touch */
 227		features->pktlen = WACOM_PKGLEN_BBTOUCH3;
 228		features->device_type = BTN_TOOL_FINGER;
 229
 230		features->x_max = features->y_max =
 231			get_unaligned_le16(&report[10]);
 232
 233		length = 11;
 234	}
 235	return length;
 236}
 237
 238static void wacom_retrieve_report_data(struct usb_interface *intf,
 239				       struct wacom_features *features)
 240{
 241	int result = 0;
 242	unsigned char *rep_data;
 243
 244	rep_data = kmalloc(2, GFP_KERNEL);
 245	if (rep_data) {
 246
 247		rep_data[0] = 12;
 248		result = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
 249					  rep_data[0], rep_data, 2,
 250					  WAC_MSG_RETRIES);
 251
 252		if (result >= 0 && rep_data[1] > 2)
 253			features->touch_max = rep_data[1];
 254
 255		kfree(rep_data);
 256	}
 257}
 258
 259/*
 260 * Interface Descriptor of wacom devices can be incomplete and
 261 * inconsistent so wacom_features table is used to store stylus
 262 * device's packet lengths, various maximum values, and tablet
 263 * resolution based on product ID's.
 264 *
 265 * For devices that contain 2 interfaces, wacom_features table is
 266 * inaccurate for the touch interface.  Since the Interface Descriptor
 267 * for touch interfaces has pretty complete data, this function exists
 268 * to query tablet for this missing information instead of hard coding in
 269 * an additional table.
 270 *
 271 * A typical Interface Descriptor for a stylus will contain a
 272 * boot mouse application collection that is not of interest and this
 273 * function will ignore it.
 274 *
 275 * It also contains a digitizer application collection that also is not
 276 * of interest since any information it contains would be duplicate
 277 * of what is in wacom_features. Usually it defines a report of an array
 278 * of bytes that could be used as max length of the stylus packet returned.
 279 * If it happens to define a Digitizer-Stylus Physical Collection then
 280 * the X and Y logical values contain valid data but it is ignored.
 281 *
 282 * A typical Interface Descriptor for a touch interface will contain a
 283 * Digitizer-Finger Physical Collection which will define both logical
 284 * X/Y maximum as well as the physical size of tablet. Since touch
 285 * interfaces haven't supported pressure or distance, this is enough
 286 * information to override invalid values in the wacom_features table.
 287 *
 288 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
 289 * Collection. Instead they define a Logical Collection with a single
 290 * Logical Maximum for both X and Y.
 291 *
 292 * Intuos5 touch interface does not contain useful data. We deal with
 293 * this after returning from this function.
 294 */
 295static int wacom_parse_hid(struct usb_interface *intf,
 296			   struct hid_descriptor *hid_desc,
 297			   struct wacom_features *features)
 298{
 299	struct usb_device *dev = interface_to_usbdev(intf);
 300	char limit = 0;
 301	/* result has to be defined as int for some devices */
 302	int result = 0, touch_max = 0;
 303	int i = 0, page = 0, finger = 0, pen = 0;
 304	unsigned char *report;
 305
 306	report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
 307	if (!report)
 308		return -ENOMEM;
 309
 310	/* retrive report descriptors */
 311	do {
 312		result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
 313			USB_REQ_GET_DESCRIPTOR,
 314			USB_RECIP_INTERFACE | USB_DIR_IN,
 315			HID_DEVICET_REPORT << 8,
 316			intf->altsetting[0].desc.bInterfaceNumber, /* interface */
 317			report,
 318			hid_desc->wDescriptorLength,
 319			5000); /* 5 secs */
 320	} while (result < 0 && limit++ < WAC_MSG_RETRIES);
 321
 322	/* No need to parse the Descriptor. It isn't an error though */
 323	if (result < 0)
 324		goto out;
 325
 326	for (i = 0; i < hid_desc->wDescriptorLength; i++) {
 327
 328		switch (report[i]) {
 329		case HID_USAGE_PAGE:
 330			page = report[i + 1];
 331			i++;
 332			break;
 333
 334		case HID_USAGE:
 335			switch (page << 16 | report[i + 1]) {
 336			case HID_USAGE_X:
 337				if (finger) {
 338					features->device_type = BTN_TOOL_FINGER;
 339					/* touch device at least supports one touch point */
 340					touch_max = 1;
 341					switch (features->type) {
 342					case TABLETPC2FG:
 343						features->pktlen = WACOM_PKGLEN_TPC2FG;
 344						break;
 345
 346					case MTSCREEN:
 347					case WACOM_24HDT:
 348						features->pktlen = WACOM_PKGLEN_MTOUCH;
 349						break;
 350
 351					case MTTPC:
 352						features->pktlen = WACOM_PKGLEN_MTTPC;
 353						break;
 354
 355					case BAMBOO_PT:
 356						features->pktlen = WACOM_PKGLEN_BBTOUCH;
 357						break;
 358
 359					default:
 360						features->pktlen = WACOM_PKGLEN_GRAPHIRE;
 361						break;
 362					}
 363
 364					switch (features->type) {
 365					case BAMBOO_PT:
 366						features->x_phy =
 367							get_unaligned_le16(&report[i + 5]);
 368						features->x_max =
 369							get_unaligned_le16(&report[i + 8]);
 370						i += 15;
 371						break;
 372
 373					case WACOM_24HDT:
 374						features->x_max =
 375							get_unaligned_le16(&report[i + 3]);
 376						features->x_phy =
 377							get_unaligned_le16(&report[i + 8]);
 378						features->unit = report[i - 1];
 379						features->unitExpo = report[i - 3];
 380						i += 12;
 381						break;
 382
 383					default:
 384						features->x_max =
 385							get_unaligned_le16(&report[i + 3]);
 386						features->x_phy =
 387							get_unaligned_le16(&report[i + 6]);
 388						features->unit = report[i + 9];
 389						features->unitExpo = report[i + 11];
 390						i += 12;
 391						break;
 392					}
 393				} else if (pen) {
 394					/* penabled only accepts exact bytes of data */
 395					if (features->type >= TABLETPC)
 396						features->pktlen = WACOM_PKGLEN_GRAPHIRE;
 397					features->device_type = BTN_TOOL_PEN;
 398					features->x_max =
 399						get_unaligned_le16(&report[i + 3]);
 400					i += 4;
 401				}
 402				break;
 403
 404			case HID_USAGE_Y:
 405				if (finger) {
 406					switch (features->type) {
 407					case TABLETPC2FG:
 408					case MTSCREEN:
 409					case MTTPC:
 410						features->y_max =
 411							get_unaligned_le16(&report[i + 3]);
 412						features->y_phy =
 413							get_unaligned_le16(&report[i + 6]);
 414						i += 7;
 415						break;
 416
 417					case WACOM_24HDT:
 418						features->y_max =
 419							get_unaligned_le16(&report[i + 3]);
 420						features->y_phy =
 421							get_unaligned_le16(&report[i - 2]);
 422						i += 7;
 423						break;
 424
 425					case BAMBOO_PT:
 426						features->y_phy =
 427							get_unaligned_le16(&report[i + 3]);
 428						features->y_max =
 429							get_unaligned_le16(&report[i + 6]);
 430						i += 12;
 431						break;
 432
 433					default:
 434						features->y_max =
 435							features->x_max;
 436						features->y_phy =
 437							get_unaligned_le16(&report[i + 3]);
 438						i += 4;
 439						break;
 440					}
 441				} else if (pen) {
 442					features->y_max =
 443						get_unaligned_le16(&report[i + 3]);
 444					i += 4;
 445				}
 446				break;
 447
 448			case HID_USAGE_FINGER:
 449				finger = 1;
 450				i++;
 451				break;
 452
 453			/*
 454			 * Requiring Stylus Usage will ignore boot mouse
 455			 * X/Y values and some cases of invalid Digitizer X/Y
 456			 * values commonly reported.
 457			 */
 458			case HID_USAGE_STYLUS:
 459				pen = 1;
 460				i++;
 461				break;
 462
 463			case HID_USAGE_CONTACTMAX:
 464				/* leave touch_max as is if predefined */
 465				if (!features->touch_max)
 466					wacom_retrieve_report_data(intf, features);
 467				i++;
 468				break;
 469
 470			case HID_USAGE_PRESSURE:
 471				if (pen) {
 472					features->pressure_max =
 473						get_unaligned_le16(&report[i + 3]);
 474					i += 4;
 475				}
 476				break;
 477			}
 478			break;
 479
 480		case HID_COLLECTION_END:
 481			/* reset UsagePage and Finger */
 482			finger = page = 0;
 483			break;
 484
 485		case HID_COLLECTION:
 486			i++;
 487			switch (report[i]) {
 488			case HID_COLLECTION_LOGICAL:
 489				i += wacom_parse_logical_collection(&report[i],
 490								    features);
 491				break;
 492			}
 493			break;
 494		}
 495	}
 496
 497 out:
 498	if (!features->touch_max && touch_max)
 499		features->touch_max = touch_max;
 500	result = 0;
 501	kfree(report);
 502	return result;
 503}
 504
 505static int wacom_set_device_mode(struct usb_interface *intf, int report_id, int length, int mode)
 506{
 507	unsigned char *rep_data;
 508	int error = -ENOMEM, limit = 0;
 509
 510	rep_data = kzalloc(length, GFP_KERNEL);
 511	if (!rep_data)
 512		return error;
 513
 514	do {
 515		rep_data[0] = report_id;
 516		rep_data[1] = mode;
 517
 518		error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
 519		                         report_id, rep_data, length, 1);
 520	} while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
 521
 522	kfree(rep_data);
 523
 524	return error < 0 ? error : 0;
 525}
 526
 527/*
 528 * Switch the tablet into its most-capable mode. Wacom tablets are
 529 * typically configured to power-up in a mode which sends mouse-like
 530 * reports to the OS. To get absolute position, pressure data, etc.
 531 * from the tablet, it is necessary to switch the tablet out of this
 532 * mode and into one which sends the full range of tablet data.
 533 */
 534static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
 535{
 536	if (features->device_type == BTN_TOOL_FINGER) {
 537		if (features->type > TABLETPC) {
 538			/* MT Tablet PC touch */
 539			return wacom_set_device_mode(intf, 3, 4, 4);
 540		}
 541		else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
 542			return wacom_set_device_mode(intf, 18, 3, 2);
 543		}
 544	} else if (features->device_type == BTN_TOOL_PEN) {
 545		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
 546			return wacom_set_device_mode(intf, 2, 2, 2);
 547		}
 548	}
 549
 550	return 0;
 551}
 552
 553static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
 554					 struct wacom_features *features)
 555{
 556	int error = 0;
 557	struct usb_host_interface *interface = intf->cur_altsetting;
 558	struct hid_descriptor *hid_desc;
 559
 560	/* default features */
 561	features->device_type = BTN_TOOL_PEN;
 562	features->x_fuzz = 4;
 563	features->y_fuzz = 4;
 564	features->pressure_fuzz = 0;
 565	features->distance_fuzz = 0;
 566
 567	/*
 568	 * The wireless device HID is basic and layout conflicts with
 569	 * other tablets (monitor and touch interface can look like pen).
 570	 * Skip the query for this type and modify defaults based on
 571	 * interface number.
 572	 */
 573	if (features->type == WIRELESS) {
 574		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
 575			features->device_type = 0;
 576		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
 577			features->device_type = BTN_TOOL_FINGER;
 578			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
 579		}
 580	}
 581
 582	/* only devices that support touch need to retrieve the info */
 583	if (features->type < BAMBOO_PT) {
 584		goto out;
 585	}
 586
 587	error = usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc);
 588	if (error) {
 589		error = usb_get_extra_descriptor(&interface->endpoint[0],
 590						 HID_DEVICET_REPORT, &hid_desc);
 591		if (error) {
 592			dev_err(&intf->dev,
 593				"can not retrieve extra class descriptor\n");
 594			goto out;
 595		}
 596	}
 597	error = wacom_parse_hid(intf, hid_desc, features);
 598
 599 out:
 600	return error;
 601}
 602
 603struct wacom_usbdev_data {
 604	struct list_head list;
 605	struct kref kref;
 606	struct usb_device *dev;
 607	struct wacom_shared shared;
 608};
 609
 610static LIST_HEAD(wacom_udev_list);
 611static DEFINE_MUTEX(wacom_udev_list_lock);
 612
 613static struct usb_device *wacom_get_sibling(struct usb_device *dev, int vendor, int product)
 614{
 615	int port1;
 616	struct usb_device *sibling;
 617
 618	if (vendor == 0 && product == 0)
 619		return dev;
 620
 621	if (dev->parent == NULL)
 622		return NULL;
 623
 624	usb_hub_for_each_child(dev->parent, port1, sibling) {
 625		struct usb_device_descriptor *d;
 626		if (sibling == NULL)
 627			continue;
 628
 629		d = &sibling->descriptor;
 630		if (d->idVendor == vendor && d->idProduct == product)
 631			return sibling;
 632	}
 633
 634	return NULL;
 635}
 636
 637static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
 638{
 639	struct wacom_usbdev_data *data;
 640
 641	list_for_each_entry(data, &wacom_udev_list, list) {
 642		if (data->dev == dev) {
 643			kref_get(&data->kref);
 644			return data;
 645		}
 646	}
 647
 648	return NULL;
 649}
 650
 651static int wacom_add_shared_data(struct wacom_wac *wacom,
 652				 struct usb_device *dev)
 653{
 654	struct wacom_usbdev_data *data;
 655	int retval = 0;
 656
 657	mutex_lock(&wacom_udev_list_lock);
 658
 659	data = wacom_get_usbdev_data(dev);
 660	if (!data) {
 661		data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
 662		if (!data) {
 663			retval = -ENOMEM;
 664			goto out;
 665		}
 666
 667		kref_init(&data->kref);
 668		data->dev = dev;
 669		list_add_tail(&data->list, &wacom_udev_list);
 670	}
 671
 672	wacom->shared = &data->shared;
 673
 674out:
 675	mutex_unlock(&wacom_udev_list_lock);
 676	return retval;
 677}
 678
 679static void wacom_release_shared_data(struct kref *kref)
 680{
 681	struct wacom_usbdev_data *data =
 682		container_of(kref, struct wacom_usbdev_data, kref);
 683
 684	mutex_lock(&wacom_udev_list_lock);
 685	list_del(&data->list);
 686	mutex_unlock(&wacom_udev_list_lock);
 687
 688	kfree(data);
 689}
 690
 691static void wacom_remove_shared_data(struct wacom_wac *wacom)
 692{
 693	struct wacom_usbdev_data *data;
 694
 695	if (wacom->shared) {
 696		data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
 697		kref_put(&data->kref, wacom_release_shared_data);
 698		wacom->shared = NULL;
 699	}
 700}
 701
 702static int wacom_led_control(struct wacom *wacom)
 703{
 704	unsigned char *buf;
 705	int retval;
 706
 707	buf = kzalloc(9, GFP_KERNEL);
 708	if (!buf)
 709		return -ENOMEM;
 710
 711	if (wacom->wacom_wac.features.type >= INTUOS5S &&
 712	    wacom->wacom_wac.features.type <= INTUOSPL) {
 713		/*
 714		 * Touch Ring and crop mark LED luminance may take on
 715		 * one of four values:
 716		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
 717		 */
 718		int ring_led = wacom->led.select[0] & 0x03;
 719		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
 720		int crop_lum = 0;
 721
 722		buf[0] = WAC_CMD_LED_CONTROL;
 723		buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
 724	}
 725	else {
 726		int led = wacom->led.select[0] | 0x4;
 727
 728		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
 729		    wacom->wacom_wac.features.type == WACOM_24HD)
 730			led |= (wacom->led.select[1] << 4) | 0x40;
 731
 732		buf[0] = WAC_CMD_LED_CONTROL;
 733		buf[1] = led;
 734		buf[2] = wacom->led.llv;
 735		buf[3] = wacom->led.hlv;
 736		buf[4] = wacom->led.img_lum;
 737	}
 738
 739	retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
 740				  buf, 9, WAC_CMD_RETRIES);
 741	kfree(buf);
 742
 743	return retval;
 744}
 745
 746static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
 747{
 748	unsigned char *buf;
 749	int i, retval;
 750
 751	buf = kzalloc(259, GFP_KERNEL);
 752	if (!buf)
 753		return -ENOMEM;
 754
 755	/* Send 'start' command */
 756	buf[0] = WAC_CMD_ICON_START;
 757	buf[1] = 1;
 758	retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
 759				  buf, 2, WAC_CMD_RETRIES);
 760	if (retval < 0)
 761		goto out;
 762
 763	buf[0] = WAC_CMD_ICON_XFER;
 764	buf[1] = button_id & 0x07;
 765	for (i = 0; i < 4; i++) {
 766		buf[2] = i;
 767		memcpy(buf + 3, img + i * 256, 256);
 768
 769		retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
 770					  buf, 259, WAC_CMD_RETRIES);
 771		if (retval < 0)
 772			break;
 773	}
 774
 775	/* Send 'stop' */
 776	buf[0] = WAC_CMD_ICON_START;
 777	buf[1] = 0;
 778	wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
 779			 buf, 2, WAC_CMD_RETRIES);
 780
 781out:
 782	kfree(buf);
 783	return retval;
 784}
 785
 786static ssize_t wacom_led_select_store(struct device *dev, int set_id,
 787				      const char *buf, size_t count)
 788{
 789	struct wacom *wacom = dev_get_drvdata(dev);
 790	unsigned int id;
 791	int err;
 792
 793	err = kstrtouint(buf, 10, &id);
 794	if (err)
 795		return err;
 796
 797	mutex_lock(&wacom->lock);
 798
 799	wacom->led.select[set_id] = id & 0x3;
 800	err = wacom_led_control(wacom);
 801
 802	mutex_unlock(&wacom->lock);
 803
 804	return err < 0 ? err : count;
 805}
 806
 807#define DEVICE_LED_SELECT_ATTR(SET_ID)					\
 808static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
 809	struct device_attribute *attr, const char *buf, size_t count)	\
 810{									\
 811	return wacom_led_select_store(dev, SET_ID, buf, count);		\
 812}									\
 813static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
 814	struct device_attribute *attr, char *buf)			\
 815{									\
 816	struct wacom *wacom = dev_get_drvdata(dev);			\
 817	return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]);	\
 818}									\
 819static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR,	\
 820		    wacom_led##SET_ID##_select_show,			\
 821		    wacom_led##SET_ID##_select_store)
 822
 823DEVICE_LED_SELECT_ATTR(0);
 824DEVICE_LED_SELECT_ATTR(1);
 825
 826static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
 827				     const char *buf, size_t count)
 828{
 829	unsigned int value;
 830	int err;
 831
 832	err = kstrtouint(buf, 10, &value);
 833	if (err)
 834		return err;
 835
 836	mutex_lock(&wacom->lock);
 837
 838	*dest = value & 0x7f;
 839	err = wacom_led_control(wacom);
 840
 841	mutex_unlock(&wacom->lock);
 842
 843	return err < 0 ? err : count;
 844}
 845
 846#define DEVICE_LUMINANCE_ATTR(name, field)				\
 847static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
 848	struct device_attribute *attr, const char *buf, size_t count)	\
 849{									\
 850	struct wacom *wacom = dev_get_drvdata(dev);			\
 851									\
 852	return wacom_luminance_store(wacom, &wacom->led.field,		\
 853				     buf, count);			\
 854}									\
 855static DEVICE_ATTR(name##_luminance, S_IWUSR,				\
 856		   NULL, wacom_##name##_luminance_store)
 857
 858DEVICE_LUMINANCE_ATTR(status0, llv);
 859DEVICE_LUMINANCE_ATTR(status1, hlv);
 860DEVICE_LUMINANCE_ATTR(buttons, img_lum);
 861
 862static ssize_t wacom_button_image_store(struct device *dev, int button_id,
 863					const char *buf, size_t count)
 864{
 865	struct wacom *wacom = dev_get_drvdata(dev);
 866	int err;
 867
 868	if (count != 1024)
 869		return -EINVAL;
 870
 871	mutex_lock(&wacom->lock);
 872
 873	err = wacom_led_putimage(wacom, button_id, buf);
 874
 875	mutex_unlock(&wacom->lock);
 876
 877	return err < 0 ? err : count;
 878}
 879
 880#define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
 881static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
 882	struct device_attribute *attr, const char *buf, size_t count)	\
 883{									\
 884	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
 885}									\
 886static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR,			\
 887		   NULL, wacom_btnimg##BUTTON_ID##_store)
 888
 889DEVICE_BTNIMG_ATTR(0);
 890DEVICE_BTNIMG_ATTR(1);
 891DEVICE_BTNIMG_ATTR(2);
 892DEVICE_BTNIMG_ATTR(3);
 893DEVICE_BTNIMG_ATTR(4);
 894DEVICE_BTNIMG_ATTR(5);
 895DEVICE_BTNIMG_ATTR(6);
 896DEVICE_BTNIMG_ATTR(7);
 897
 898static struct attribute *cintiq_led_attrs[] = {
 899	&dev_attr_status_led0_select.attr,
 900	&dev_attr_status_led1_select.attr,
 901	NULL
 902};
 903
 904static struct attribute_group cintiq_led_attr_group = {
 905	.name = "wacom_led",
 906	.attrs = cintiq_led_attrs,
 907};
 908
 909static struct attribute *intuos4_led_attrs[] = {
 910	&dev_attr_status0_luminance.attr,
 911	&dev_attr_status1_luminance.attr,
 912	&dev_attr_status_led0_select.attr,
 913	&dev_attr_buttons_luminance.attr,
 914	&dev_attr_button0_rawimg.attr,
 915	&dev_attr_button1_rawimg.attr,
 916	&dev_attr_button2_rawimg.attr,
 917	&dev_attr_button3_rawimg.attr,
 918	&dev_attr_button4_rawimg.attr,
 919	&dev_attr_button5_rawimg.attr,
 920	&dev_attr_button6_rawimg.attr,
 921	&dev_attr_button7_rawimg.attr,
 922	NULL
 923};
 924
 925static struct attribute_group intuos4_led_attr_group = {
 926	.name = "wacom_led",
 927	.attrs = intuos4_led_attrs,
 928};
 929
 930static struct attribute *intuos5_led_attrs[] = {
 931	&dev_attr_status0_luminance.attr,
 932	&dev_attr_status_led0_select.attr,
 933	NULL
 934};
 935
 936static struct attribute_group intuos5_led_attr_group = {
 937	.name = "wacom_led",
 938	.attrs = intuos5_led_attrs,
 939};
 940
 941static int wacom_initialize_leds(struct wacom *wacom)
 942{
 943	int error;
 944
 945	/* Initialize default values */
 946	switch (wacom->wacom_wac.features.type) {
 947	case INTUOS4S:
 948	case INTUOS4:
 949	case INTUOS4L:
 950		wacom->led.select[0] = 0;
 951		wacom->led.select[1] = 0;
 952		wacom->led.llv = 10;
 953		wacom->led.hlv = 20;
 954		wacom->led.img_lum = 10;
 955		error = sysfs_create_group(&wacom->intf->dev.kobj,
 956					   &intuos4_led_attr_group);
 957		break;
 958
 959	case WACOM_24HD:
 960	case WACOM_21UX2:
 961		wacom->led.select[0] = 0;
 962		wacom->led.select[1] = 0;
 963		wacom->led.llv = 0;
 964		wacom->led.hlv = 0;
 965		wacom->led.img_lum = 0;
 966
 967		error = sysfs_create_group(&wacom->intf->dev.kobj,
 968					   &cintiq_led_attr_group);
 969		break;
 970
 971	case INTUOS5S:
 972	case INTUOS5:
 973	case INTUOS5L:
 974	case INTUOSPS:
 975	case INTUOSPM:
 976	case INTUOSPL:
 977		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
 978			wacom->led.select[0] = 0;
 979			wacom->led.select[1] = 0;
 980			wacom->led.llv = 32;
 981			wacom->led.hlv = 0;
 982			wacom->led.img_lum = 0;
 983
 984			error = sysfs_create_group(&wacom->intf->dev.kobj,
 985						  &intuos5_led_attr_group);
 986		} else
 987			return 0;
 988		break;
 989
 990	default:
 991		return 0;
 992	}
 993
 994	if (error) {
 995		dev_err(&wacom->intf->dev,
 996			"cannot create sysfs group err: %d\n", error);
 997		return error;
 998	}
 999	wacom_led_control(wacom);
1000
1001	return 0;
1002}
1003
1004static void wacom_destroy_leds(struct wacom *wacom)
1005{
1006	switch (wacom->wacom_wac.features.type) {
1007	case INTUOS4S:
1008	case INTUOS4:
1009	case INTUOS4L:
1010		sysfs_remove_group(&wacom->intf->dev.kobj,
1011				   &intuos4_led_attr_group);
1012		break;
1013
1014	case WACOM_24HD:
1015	case WACOM_21UX2:
1016		sysfs_remove_group(&wacom->intf->dev.kobj,
1017				   &cintiq_led_attr_group);
1018		break;
1019
1020	case INTUOS5S:
1021	case INTUOS5:
1022	case INTUOS5L:
1023	case INTUOSPS:
1024	case INTUOSPM:
1025	case INTUOSPL:
1026		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
1027			sysfs_remove_group(&wacom->intf->dev.kobj,
1028					   &intuos5_led_attr_group);
1029		break;
1030	}
1031}
1032
1033static enum power_supply_property wacom_battery_props[] = {
1034	POWER_SUPPLY_PROP_SCOPE,
1035	POWER_SUPPLY_PROP_CAPACITY
1036};
1037
1038static int wacom_battery_get_property(struct power_supply *psy,
1039				      enum power_supply_property psp,
1040				      union power_supply_propval *val)
1041{
1042	struct wacom *wacom = container_of(psy, struct wacom, battery);
1043	int ret = 0;
1044
1045	switch (psp) {
1046		case POWER_SUPPLY_PROP_SCOPE:
1047			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1048			break;
1049		case POWER_SUPPLY_PROP_CAPACITY:
1050			val->intval =
1051				wacom->wacom_wac.battery_capacity * 100 / 31;
1052			break;
1053		default:
1054			ret = -EINVAL;
1055			break;
1056	}
1057
1058	return ret;
1059}
1060
1061static int wacom_initialize_battery(struct wacom *wacom)
1062{
1063	int error = 0;
1064
1065	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
1066		wacom->battery.properties = wacom_battery_props;
1067		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1068		wacom->battery.get_property = wacom_battery_get_property;
1069		wacom->battery.name = "wacom_battery";
1070		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1071		wacom->battery.use_for_apm = 0;
1072
1073		error = power_supply_register(&wacom->usbdev->dev,
1074					      &wacom->battery);
1075
1076		if (!error)
1077			power_supply_powers(&wacom->battery,
1078					    &wacom->usbdev->dev);
1079	}
1080
1081	return error;
1082}
1083
1084static void wacom_destroy_battery(struct wacom *wacom)
1085{
1086	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
1087	    wacom->battery.dev) {
1088		power_supply_unregister(&wacom->battery);
1089		wacom->battery.dev = NULL;
1090	}
1091}
1092
1093static int wacom_register_input(struct wacom *wacom)
1094{
1095	struct input_dev *input_dev;
1096	struct usb_interface *intf = wacom->intf;
1097	struct usb_device *dev = interface_to_usbdev(intf);
1098	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1099	int error;
1100
1101	input_dev = input_allocate_device();
1102	if (!input_dev) {
1103		error = -ENOMEM;
1104		goto fail1;
1105	}
1106
1107	input_dev->name = wacom_wac->name;
1108	input_dev->dev.parent = &intf->dev;
1109	input_dev->open = wacom_open;
1110	input_dev->close = wacom_close;
1111	usb_to_input_id(dev, &input_dev->id);
1112	input_set_drvdata(input_dev, wacom);
1113
1114	wacom_wac->input = input_dev;
1115	error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1116	if (error)
1117		goto fail1;
1118
1119	error = input_register_device(input_dev);
1120	if (error)
1121		goto fail2;
1122
1123	return 0;
1124
1125fail2:
1126	input_free_device(input_dev);
1127	wacom_wac->input = NULL;
1128fail1:
1129	return error;
1130}
1131
1132static void wacom_wireless_work(struct work_struct *work)
1133{
1134	struct wacom *wacom = container_of(work, struct wacom, work);
1135	struct usb_device *usbdev = wacom->usbdev;
1136	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1137	struct wacom *wacom1, *wacom2;
1138	struct wacom_wac *wacom_wac1, *wacom_wac2;
1139	int error;
1140
1141	/*
1142	 * Regardless if this is a disconnect or a new tablet,
1143	 * remove any existing input and battery devices.
1144	 */
1145
1146	wacom_destroy_battery(wacom);
1147
1148	/* Stylus interface */
1149	wacom1 = usb_get_intfdata(usbdev->config->interface[1]);
1150	wacom_wac1 = &(wacom1->wacom_wac);
1151	if (wacom_wac1->input)
1152		input_unregister_device(wacom_wac1->input);
1153	wacom_wac1->input = NULL;
1154
1155	/* Touch interface */
1156	wacom2 = usb_get_intfdata(usbdev->config->interface[2]);
1157	wacom_wac2 = &(wacom2->wacom_wac);
1158	if (wacom_wac2->input)
1159		input_unregister_device(wacom_wac2->input);
1160	wacom_wac2->input = NULL;
1161
1162	if (wacom_wac->pid == 0) {
1163		dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
1164	} else {
1165		const struct usb_device_id *id = wacom_ids;
1166
1167		dev_info(&wacom->intf->dev,
1168			 "wireless tablet connected with PID %x\n",
1169			 wacom_wac->pid);
1170
1171		while (id->match_flags) {
1172			if (id->idVendor == USB_VENDOR_ID_WACOM &&
1173			    id->idProduct == wacom_wac->pid)
1174				break;
1175			id++;
1176		}
1177
1178		if (!id->match_flags) {
1179			dev_info(&wacom->intf->dev,
1180				 "ignoring unknown PID.\n");
1181			return;
1182		}
1183
1184		/* Stylus interface */
1185		wacom_wac1->features =
1186			*((struct wacom_features *)id->driver_info);
1187		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1188		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1189			 wacom_wac1->features.name);
1190		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1191		wacom_wac1->shared->type = wacom_wac1->features.type;
1192		error = wacom_register_input(wacom1);
1193		if (error)
1194			goto fail;
1195
1196		/* Touch interface */
1197		if (wacom_wac1->features.touch_max ||
1198		    wacom_wac1->features.type == INTUOSHT) {
1199			wacom_wac2->features =
1200				*((struct wacom_features *)id->driver_info);
1201			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1202			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1203			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1204			if (wacom_wac2->features.touch_max)
1205				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1206					 "%s (WL) Finger",wacom_wac2->features.name);
1207			else
1208				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1209					 "%s (WL) Pad",wacom_wac2->features.name);
1210			error = wacom_register_input(wacom2);
1211			if (error)
1212				goto fail;
1213
1214			if (wacom_wac1->features.type == INTUOSHT &&
1215			    wacom_wac1->features.touch_max)
1216				wacom_wac->shared->touch_input = wacom_wac2->input;
1217		}
1218
1219		error = wacom_initialize_battery(wacom);
1220		if (error)
1221			goto fail;
1222	}
1223
1224	return;
1225
1226fail:
1227	if (wacom_wac2->input) {
1228		input_unregister_device(wacom_wac2->input);
1229		wacom_wac2->input = NULL;
1230	}
1231
1232	if (wacom_wac1->input) {
1233		input_unregister_device(wacom_wac1->input);
1234		wacom_wac1->input = NULL;
1235	}
1236	return;
1237}
1238
1239/*
1240 * Not all devices report physical dimensions from HID.
1241 * Compute the default from hardcoded logical dimension
1242 * and resolution before driver overwrites them.
1243 */
1244static void wacom_set_default_phy(struct wacom_features *features)
1245{
1246	if (features->x_resolution) {
1247		features->x_phy = (features->x_max * 100) /
1248					features->x_resolution;
1249		features->y_phy = (features->y_max * 100) /
1250					features->y_resolution;
1251	}
1252}
1253
1254static void wacom_calculate_res(struct wacom_features *features)
1255{
1256	features->x_resolution = wacom_calc_hid_res(features->x_max,
1257						    features->x_phy,
1258						    features->unit,
1259						    features->unitExpo);
1260	features->y_resolution = wacom_calc_hid_res(features->y_max,
1261						    features->y_phy,
1262						    features->unit,
1263						    features->unitExpo);
1264}
1265
1266static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
1267{
1268	struct usb_device *dev = interface_to_usbdev(intf);
1269	struct usb_endpoint_descriptor *endpoint;
1270	struct wacom *wacom;
1271	struct wacom_wac *wacom_wac;
1272	struct wacom_features *features;
1273	int error;
1274
1275	if (!id->driver_info)
1276		return -EINVAL;
1277
1278	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1279	if (!wacom)
1280		return -ENOMEM;
1281
1282	wacom_wac = &wacom->wacom_wac;
1283	wacom_wac->features = *((struct wacom_features *)id->driver_info);
1284	features = &wacom_wac->features;
1285	if (features->pktlen > WACOM_PKGLEN_MAX) {
1286		error = -EINVAL;
1287		goto fail1;
1288	}
1289
1290	wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1291					     GFP_KERNEL, &wacom->data_dma);
1292	if (!wacom_wac->data) {
1293		error = -ENOMEM;
1294		goto fail1;
1295	}
1296
1297	wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
1298	if (!wacom->irq) {
1299		error = -ENOMEM;
1300		goto fail2;
1301	}
1302
1303	wacom->usbdev = dev;
1304	wacom->intf = intf;
1305	mutex_init(&wacom->lock);
1306	INIT_WORK(&wacom->work, wacom_wireless_work);
1307	usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1308	strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1309
1310	endpoint = &intf->cur_altsetting->endpoint[0].desc;
1311
1312	/* set the default size in case we do not get them from hid */
1313	wacom_set_default_phy(features);
1314
1315	/* Retrieve the physical and logical size for touch devices */
1316	error = wacom_retrieve_hid_descriptor(intf, features);
1317	if (error)
1318		goto fail3;
1319
1320	/*
1321	 * Intuos5 has no useful data about its touch interface in its
1322	 * HID descriptor. If this is the touch interface (wMaxPacketSize
1323	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1324	 */
1325	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1326		if (endpoint->wMaxPacketSize == WACOM_PKGLEN_BBTOUCH3) {
1327			features->device_type = BTN_TOOL_FINGER;
1328			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
1329
1330			features->x_max = 4096;
1331			features->y_max = 4096;
1332		} else {
1333			features->device_type = BTN_TOOL_PEN;
1334		}
1335	}
1336
1337	wacom_setup_device_quirks(features);
1338
1339	/* set unit to "100th of a mm" for devices not reported by HID */
1340	if (!features->unit) {
1341		features->unit = 0x11;
1342		features->unitExpo = 16 - 3;
1343	}
1344	wacom_calculate_res(features);
1345
1346	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1347
1348	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1349		struct usb_device *other_dev;
1350
1351		/* Append the device type to the name */
1352		if (features->device_type != BTN_TOOL_FINGER)
1353			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1354		else if (features->touch_max)
1355			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1356		else
1357			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1358
1359		other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1360		if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1361			other_dev = dev;
1362		error = wacom_add_shared_data(wacom_wac, other_dev);
1363		if (error)
1364			goto fail3;
1365	}
1366
1367	usb_fill_int_urb(wacom->irq, dev,
1368			 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1369			 wacom_wac->data, features->pktlen,
1370			 wacom_sys_irq, wacom, endpoint->bInterval);
1371	wacom->irq->transfer_dma = wacom->data_dma;
1372	wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1373
1374	error = wacom_initialize_leds(wacom);
1375	if (error)
1376		goto fail4;
1377
1378	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1379		error = wacom_register_input(wacom);
1380		if (error)
1381			goto fail5;
1382	}
1383
1384	/* Note that if query fails it is not a hard failure */
1385	wacom_query_tablet_data(intf, features);
1386
1387	usb_set_intfdata(intf, wacom);
1388
1389	if (features->quirks & WACOM_QUIRK_MONITOR) {
1390		if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
1391			error = -EIO;
1392			goto fail5;
1393		}
1394	}
1395
1396	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1397		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1398			wacom_wac->shared->touch_input = wacom_wac->input;
1399	}
1400
1401	return 0;
1402
1403 fail5: wacom_destroy_leds(wacom);
1404 fail4:	wacom_remove_shared_data(wacom_wac);
1405 fail3:	usb_free_urb(wacom->irq);
1406 fail2:	usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
1407 fail1:	kfree(wacom);
1408	return error;
1409}
1410
1411static void wacom_disconnect(struct usb_interface *intf)
1412{
1413	struct wacom *wacom = usb_get_intfdata(intf);
1414
1415	usb_set_intfdata(intf, NULL);
1416
1417	usb_kill_urb(wacom->irq);
1418	cancel_work_sync(&wacom->work);
1419	if (wacom->wacom_wac.input)
1420		input_unregister_device(wacom->wacom_wac.input);
1421	wacom_destroy_battery(wacom);
1422	wacom_destroy_leds(wacom);
1423	usb_free_urb(wacom->irq);
1424	usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
1425			wacom->wacom_wac.data, wacom->data_dma);
1426	wacom_remove_shared_data(&wacom->wacom_wac);
1427	kfree(wacom);
1428}
1429
1430static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1431{
1432	struct wacom *wacom = usb_get_intfdata(intf);
1433
1434	mutex_lock(&wacom->lock);
1435	usb_kill_urb(wacom->irq);
1436	mutex_unlock(&wacom->lock);
1437
1438	return 0;
1439}
1440
1441static int wacom_resume(struct usb_interface *intf)
1442{
1443	struct wacom *wacom = usb_get_intfdata(intf);
1444	struct wacom_features *features = &wacom->wacom_wac.features;
1445	int rv = 0;
1446
1447	mutex_lock(&wacom->lock);
1448
1449	/* switch to wacom mode first */
1450	wacom_query_tablet_data(intf, features);
1451	wacom_led_control(wacom);
1452
1453	if ((wacom->open || (features->quirks & WACOM_QUIRK_MONITOR)) &&
1454	    usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
1455		rv = -EIO;
1456
1457	mutex_unlock(&wacom->lock);
1458
1459	return rv;
1460}
1461
1462static int wacom_reset_resume(struct usb_interface *intf)
1463{
1464	return wacom_resume(intf);
1465}
1466
1467static struct usb_driver wacom_driver = {
1468	.name =		"wacom",
1469	.id_table =	wacom_ids,
1470	.probe =	wacom_probe,
1471	.disconnect =	wacom_disconnect,
1472	.suspend =	wacom_suspend,
1473	.resume =	wacom_resume,
1474	.reset_resume =	wacom_reset_resume,
1475	.supports_autosuspend = 1,
1476};
1477
1478module_usb_driver(wacom_driver);