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