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