]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/tablet/wacom_sys.c
Merge remote-tracking branch 'input/next'
[karo-tx-linux.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_COLLECTION                  0xc0
32
33 enum {
34         WCM_UNDEFINED = 0,
35         WCM_DESKTOP,
36         WCM_DIGITIZER,
37 };
38
39 struct hid_descriptor {
40         struct usb_descriptor_header header;
41         __le16   bcdHID;
42         u8       bCountryCode;
43         u8       bNumDescriptors;
44         u8       bDescriptorType;
45         __le16   wDescriptorLength;
46 } __attribute__ ((packed));
47
48 /* defines to get/set USB message */
49 #define USB_REQ_GET_REPORT      0x01
50 #define USB_REQ_SET_REPORT      0x09
51
52 #define WAC_HID_FEATURE_REPORT  0x03
53 #define WAC_MSG_RETRIES         5
54
55 #define WAC_CMD_LED_CONTROL     0x20
56 #define WAC_CMD_ICON_START      0x21
57 #define WAC_CMD_ICON_XFER       0x23
58 #define WAC_CMD_RETRIES         10
59
60 static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
61                             void *buf, size_t size, unsigned int retries)
62 {
63         struct usb_device *dev = interface_to_usbdev(intf);
64         int retval;
65
66         do {
67                 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
68                                 USB_REQ_GET_REPORT,
69                                 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
70                                 (type << 8) + id,
71                                 intf->altsetting[0].desc.bInterfaceNumber,
72                                 buf, size, 100);
73         } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
74
75         return retval;
76 }
77
78 static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
79                             void *buf, size_t size, unsigned int retries)
80 {
81         struct usb_device *dev = interface_to_usbdev(intf);
82         int retval;
83
84         do {
85                 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
86                                 USB_REQ_SET_REPORT,
87                                 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
88                                 (type << 8) + id,
89                                 intf->altsetting[0].desc.bInterfaceNumber,
90                                 buf, size, 1000);
91         } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
92
93         return retval;
94 }
95
96 static void wacom_sys_irq(struct urb *urb)
97 {
98         struct wacom *wacom = urb->context;
99         int retval;
100
101         switch (urb->status) {
102         case 0:
103                 /* success */
104                 break;
105         case -ECONNRESET:
106         case -ENOENT:
107         case -ESHUTDOWN:
108                 /* this urb is terminated, clean up */
109                 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
110                 return;
111         default:
112                 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
113                 goto exit;
114         }
115
116         wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
117
118  exit:
119         usb_mark_last_busy(wacom->usbdev);
120         retval = usb_submit_urb(urb, GFP_ATOMIC);
121         if (retval)
122                 err ("%s - usb_submit_urb failed with result %d",
123                      __func__, retval);
124 }
125
126 static int wacom_open(struct input_dev *dev)
127 {
128         struct wacom *wacom = input_get_drvdata(dev);
129         int retval = 0;
130
131         if (usb_autopm_get_interface(wacom->intf) < 0)
132                 return -EIO;
133
134         mutex_lock(&wacom->lock);
135
136         if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
137                 retval = -EIO;
138                 goto out;
139         }
140
141         wacom->open = true;
142         wacom->intf->needs_remote_wakeup = 1;
143
144 out:
145         mutex_unlock(&wacom->lock);
146         usb_autopm_put_interface(wacom->intf);
147         return retval;
148 }
149
150 static void wacom_close(struct input_dev *dev)
151 {
152         struct wacom *wacom = input_get_drvdata(dev);
153         int autopm_error;
154
155         autopm_error = usb_autopm_get_interface(wacom->intf);
156
157         mutex_lock(&wacom->lock);
158         usb_kill_urb(wacom->irq);
159         wacom->open = false;
160         wacom->intf->needs_remote_wakeup = 0;
161         mutex_unlock(&wacom->lock);
162
163         if (!autopm_error)
164                 usb_autopm_put_interface(wacom->intf);
165 }
166
167 static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
168                            struct wacom_features *features)
169 {
170         struct usb_device *dev = interface_to_usbdev(intf);
171         char limit = 0;
172         /* result has to be defined as int for some devices */
173         int result = 0;
174         int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
175         unsigned char *report;
176
177         report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
178         if (!report)
179                 return -ENOMEM;
180
181         /* retrive report descriptors */
182         do {
183                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
184                         USB_REQ_GET_DESCRIPTOR,
185                         USB_RECIP_INTERFACE | USB_DIR_IN,
186                         HID_DEVICET_REPORT << 8,
187                         intf->altsetting[0].desc.bInterfaceNumber, /* interface */
188                         report,
189                         hid_desc->wDescriptorLength,
190                         5000); /* 5 secs */
191         } while (result < 0 && limit++ < WAC_MSG_RETRIES);
192
193         /* No need to parse the Descriptor. It isn't an error though */
194         if (result < 0)
195                 goto out;
196
197         for (i = 0; i < hid_desc->wDescriptorLength; i++) {
198
199                 switch (report[i]) {
200                 case HID_USAGE_PAGE:
201                         switch (report[i + 1]) {
202                         case HID_USAGE_PAGE_DIGITIZER:
203                                 usage = WCM_DIGITIZER;
204                                 i++;
205                                 break;
206
207                         case HID_USAGE_PAGE_DESKTOP:
208                                 usage = WCM_DESKTOP;
209                                 i++;
210                                 break;
211                         }
212                         break;
213
214                 case HID_USAGE:
215                         switch (report[i + 1]) {
216                         case HID_USAGE_X:
217                                 if (usage == WCM_DESKTOP) {
218                                         if (finger) {
219                                                 features->device_type = BTN_TOOL_FINGER;
220                                                 if (features->type == TABLETPC2FG) {
221                                                         /* need to reset back */
222                                                         features->pktlen = WACOM_PKGLEN_TPC2FG;
223                                                         features->device_type = BTN_TOOL_DOUBLETAP;
224                                                 }
225                                                 if (features->type == BAMBOO_PT) {
226                                                         /* need to reset back */
227                                                         features->pktlen = WACOM_PKGLEN_BBTOUCH;
228                                                         features->device_type = BTN_TOOL_DOUBLETAP;
229                                                         features->x_phy =
230                                                                 get_unaligned_le16(&report[i + 5]);
231                                                         features->x_max =
232                                                                 get_unaligned_le16(&report[i + 8]);
233                                                         i += 15;
234                                                 } else {
235                                                         features->x_max =
236                                                                 get_unaligned_le16(&report[i + 3]);
237                                                         features->x_phy =
238                                                                 get_unaligned_le16(&report[i + 6]);
239                                                         features->unit = report[i + 9];
240                                                         features->unitExpo = report[i + 11];
241                                                         i += 12;
242                                                 }
243                                         } else if (pen) {
244                                                 /* penabled only accepts exact bytes of data */
245                                                 if (features->type == TABLETPC2FG)
246                                                         features->pktlen = WACOM_PKGLEN_GRAPHIRE;
247                                                 if (features->type == BAMBOO_PT)
248                                                         features->pktlen = WACOM_PKGLEN_BBFUN;
249                                                 features->device_type = BTN_TOOL_PEN;
250                                                 features->x_max =
251                                                         get_unaligned_le16(&report[i + 3]);
252                                                 i += 4;
253                                         }
254                                 }
255                                 break;
256
257                         case HID_USAGE_Y:
258                                 if (usage == WCM_DESKTOP) {
259                                         if (finger) {
260                                                 features->device_type = BTN_TOOL_FINGER;
261                                                 if (features->type == TABLETPC2FG) {
262                                                         /* need to reset back */
263                                                         features->pktlen = WACOM_PKGLEN_TPC2FG;
264                                                         features->device_type = BTN_TOOL_DOUBLETAP;
265                                                         features->y_max =
266                                                                 get_unaligned_le16(&report[i + 3]);
267                                                         features->y_phy =
268                                                                 get_unaligned_le16(&report[i + 6]);
269                                                         i += 7;
270                                                 } else if (features->type == BAMBOO_PT) {
271                                                         /* need to reset back */
272                                                         features->pktlen = WACOM_PKGLEN_BBTOUCH;
273                                                         features->device_type = BTN_TOOL_DOUBLETAP;
274                                                         features->y_phy =
275                                                                 get_unaligned_le16(&report[i + 3]);
276                                                         features->y_max =
277                                                                 get_unaligned_le16(&report[i + 6]);
278                                                         i += 12;
279                                                 } else {
280                                                         features->y_max =
281                                                                 features->x_max;
282                                                         features->y_phy =
283                                                                 get_unaligned_le16(&report[i + 3]);
284                                                         i += 4;
285                                                 }
286                                         } else if (pen) {
287                                                 /* penabled only accepts exact bytes of data */
288                                                 if (features->type == TABLETPC2FG)
289                                                         features->pktlen = WACOM_PKGLEN_GRAPHIRE;
290                                                 if (features->type == BAMBOO_PT)
291                                                         features->pktlen = WACOM_PKGLEN_BBFUN;
292                                                 features->device_type = BTN_TOOL_PEN;
293                                                 features->y_max =
294                                                         get_unaligned_le16(&report[i + 3]);
295                                                 i += 4;
296                                         }
297                                 }
298                                 break;
299
300                         case HID_USAGE_FINGER:
301                                 finger = 1;
302                                 i++;
303                                 break;
304
305                         case HID_USAGE_STYLUS:
306                                 pen = 1;
307                                 i++;
308                                 break;
309                         }
310                         break;
311
312                 case HID_COLLECTION:
313                         /* reset UsagePage and Finger */
314                         finger = usage = 0;
315                         break;
316                 }
317         }
318
319  out:
320         result = 0;
321         kfree(report);
322         return result;
323 }
324
325 static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
326 {
327         unsigned char *rep_data;
328         int limit = 0, report_id = 2;
329         int error = -ENOMEM;
330
331         rep_data = kmalloc(4, GFP_KERNEL);
332         if (!rep_data)
333                 return error;
334
335         /* ask to report tablet data if it is MT Tablet PC or
336          * not a Tablet PC */
337         if (features->type == TABLETPC2FG) {
338                 do {
339                         rep_data[0] = 3;
340                         rep_data[1] = 4;
341                         rep_data[2] = 0;
342                         rep_data[3] = 0;
343                         report_id = 3;
344                         error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
345                                                  report_id, rep_data, 4, 1);
346                         if (error >= 0)
347                                 error = wacom_get_report(intf,
348                                                 WAC_HID_FEATURE_REPORT,
349                                                 report_id, rep_data, 4, 1);
350                 } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
351         } else if (features->type != TABLETPC) {
352                 do {
353                         rep_data[0] = 2;
354                         rep_data[1] = 2;
355                         error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
356                                                  report_id, rep_data, 2, 1);
357                         if (error >= 0)
358                                 error = wacom_get_report(intf,
359                                                 WAC_HID_FEATURE_REPORT,
360                                                 report_id, rep_data, 2, 1);
361                 } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
362         }
363
364         kfree(rep_data);
365
366         return error < 0 ? error : 0;
367 }
368
369 static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
370                 struct wacom_features *features)
371 {
372         int error = 0;
373         struct usb_host_interface *interface = intf->cur_altsetting;
374         struct hid_descriptor *hid_desc;
375
376         /* default features */
377         features->device_type = BTN_TOOL_PEN;
378         features->x_fuzz = 4;
379         features->y_fuzz = 4;
380         features->pressure_fuzz = 0;
381         features->distance_fuzz = 0;
382
383         /* only Tablet PCs and Bamboo P&T need to retrieve the info */
384         if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
385             (features->type != BAMBOO_PT))
386                 goto out;
387
388         if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
389                 if (usb_get_extra_descriptor(&interface->endpoint[0],
390                                 HID_DEVICET_REPORT, &hid_desc)) {
391                         printk("wacom: can not retrieve extra class descriptor\n");
392                         error = 1;
393                         goto out;
394                 }
395         }
396         error = wacom_parse_hid(intf, hid_desc, features);
397         if (error)
398                 goto out;
399
400  out:
401         return error;
402 }
403
404 struct wacom_usbdev_data {
405         struct list_head list;
406         struct kref kref;
407         struct usb_device *dev;
408         struct wacom_shared shared;
409 };
410
411 static LIST_HEAD(wacom_udev_list);
412 static DEFINE_MUTEX(wacom_udev_list_lock);
413
414 static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
415 {
416         struct wacom_usbdev_data *data;
417
418         list_for_each_entry(data, &wacom_udev_list, list) {
419                 if (data->dev == dev) {
420                         kref_get(&data->kref);
421                         return data;
422                 }
423         }
424
425         return NULL;
426 }
427
428 static int wacom_add_shared_data(struct wacom_wac *wacom,
429                                  struct usb_device *dev)
430 {
431         struct wacom_usbdev_data *data;
432         int retval = 0;
433
434         mutex_lock(&wacom_udev_list_lock);
435
436         data = wacom_get_usbdev_data(dev);
437         if (!data) {
438                 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
439                 if (!data) {
440                         retval = -ENOMEM;
441                         goto out;
442                 }
443
444                 kref_init(&data->kref);
445                 data->dev = dev;
446                 list_add_tail(&data->list, &wacom_udev_list);
447         }
448
449         wacom->shared = &data->shared;
450
451 out:
452         mutex_unlock(&wacom_udev_list_lock);
453         return retval;
454 }
455
456 static void wacom_release_shared_data(struct kref *kref)
457 {
458         struct wacom_usbdev_data *data =
459                 container_of(kref, struct wacom_usbdev_data, kref);
460
461         mutex_lock(&wacom_udev_list_lock);
462         list_del(&data->list);
463         mutex_unlock(&wacom_udev_list_lock);
464
465         kfree(data);
466 }
467
468 static void wacom_remove_shared_data(struct wacom_wac *wacom)
469 {
470         struct wacom_usbdev_data *data;
471
472         if (wacom->shared) {
473                 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
474                 kref_put(&data->kref, wacom_release_shared_data);
475                 wacom->shared = NULL;
476         }
477 }
478
479 static int wacom_led_control(struct wacom *wacom)
480 {
481         unsigned char *buf;
482         int retval;
483
484         buf = kzalloc(9, GFP_KERNEL);
485         if (!buf)
486                 return -ENOMEM;
487
488         buf[0] = WAC_CMD_LED_CONTROL;
489         buf[1] = wacom->led.select >= 0 ? wacom->led.select | 4 : 0;
490         buf[2] = wacom->led.llv;
491         buf[3] = wacom->led.hlv;
492         buf[4] = wacom->led.img_lum;
493
494         retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
495                                   buf, 9, WAC_CMD_RETRIES);
496         kfree(buf);
497
498         return retval;
499 }
500
501 static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
502 {
503         unsigned char *buf;
504         int i, retval;
505
506         buf = kzalloc(259, GFP_KERNEL);
507         if (!buf)
508                 return -ENOMEM;
509
510         /* Send 'start' command */
511         buf[0] = WAC_CMD_ICON_START;
512         buf[1] = 1;
513         retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
514                                   buf, 2, WAC_CMD_RETRIES);
515         if (retval < 0)
516                 goto out;
517
518         buf[0] = WAC_CMD_ICON_XFER;
519         buf[1] = button_id & 0x07;
520         for (i = 0; i < 4; i++) {
521                 buf[2] = i;
522                 memcpy(buf + 3, img + i * 256, 256);
523
524                 retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
525                                           buf, 259, WAC_CMD_RETRIES);
526                 if (retval < 0)
527                         break;
528         }
529
530         /* Send 'stop' */
531         buf[0] = WAC_CMD_ICON_START;
532         buf[1] = 0;
533         wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
534                          buf, 2, WAC_CMD_RETRIES);
535
536 out:
537         kfree(buf);
538         return retval;
539 }
540
541 static ssize_t wacom_led_select_store(struct device *dev,
542                                       struct device_attribute *attr,
543                                       const char *buf, size_t count)
544 {
545         struct wacom *wacom = dev_get_drvdata(dev);
546         unsigned int id;
547         int err;
548
549         err = kstrtouint(buf, 10, &id);
550         if (err)
551                 return err;
552
553         mutex_lock(&wacom->lock);
554
555         wacom->led.select = id;
556         err = wacom_led_control(wacom);
557
558         mutex_unlock(&wacom->lock);
559
560         return err < 0 ? err : count;
561 }
562
563 static DEVICE_ATTR(status_led_select, S_IWUSR, NULL, wacom_led_select_store);
564
565 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
566                                      const char *buf, size_t count)
567 {
568         unsigned int value;
569         int err;
570
571         err = kstrtouint(buf, 10, &value);
572         if (err)
573                 return err;
574
575         mutex_lock(&wacom->lock);
576
577         *dest = value & 0x7f;
578         err = wacom_led_control(wacom);
579
580         mutex_unlock(&wacom->lock);
581
582         return err < 0 ? err : count;
583 }
584
585 #define DEVICE_LUMINANCE_ATTR(name, field)                              \
586 static ssize_t wacom_##name##_luminance_store(struct device *dev,       \
587         struct device_attribute *attr, const char *buf, size_t count)   \
588 {                                                                       \
589         struct wacom *wacom = dev_get_drvdata(dev);                     \
590                                                                         \
591         return wacom_luminance_store(wacom, &wacom->led.field,          \
592                                      buf, count);                       \
593 }                                                                       \
594 static DEVICE_ATTR(name##_luminance, S_IWUSR,                           \
595                    NULL, wacom_##name##_luminance_store)
596
597 DEVICE_LUMINANCE_ATTR(status0, llv);
598 DEVICE_LUMINANCE_ATTR(status1, hlv);
599 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
600
601 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
602                                         const char *buf, size_t count)
603 {
604         struct wacom *wacom = dev_get_drvdata(dev);
605         int err;
606
607         if (count != 1024)
608                 return -EINVAL;
609
610         mutex_lock(&wacom->lock);
611
612         err = wacom_led_putimage(wacom, button_id, buf);
613
614         mutex_unlock(&wacom->lock);
615
616         return err < 0 ? err : count;
617 }
618
619 #define DEVICE_BTNIMG_ATTR(BUTTON_ID)                                   \
620 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,      \
621         struct device_attribute *attr, const char *buf, size_t count)   \
622 {                                                                       \
623         return wacom_button_image_store(dev, BUTTON_ID, buf, count);    \
624 }                                                                       \
625 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR,                 \
626                    NULL, wacom_btnimg##BUTTON_ID##_store)
627
628 DEVICE_BTNIMG_ATTR(0);
629 DEVICE_BTNIMG_ATTR(1);
630 DEVICE_BTNIMG_ATTR(2);
631 DEVICE_BTNIMG_ATTR(3);
632 DEVICE_BTNIMG_ATTR(4);
633 DEVICE_BTNIMG_ATTR(5);
634 DEVICE_BTNIMG_ATTR(6);
635 DEVICE_BTNIMG_ATTR(7);
636
637 static struct attribute *wacom_led_attrs[] = {
638         &dev_attr_status0_luminance.attr,
639         &dev_attr_status1_luminance.attr,
640         &dev_attr_status_led_select.attr,
641         &dev_attr_buttons_luminance.attr,
642         &dev_attr_button0_rawimg.attr,
643         &dev_attr_button1_rawimg.attr,
644         &dev_attr_button2_rawimg.attr,
645         &dev_attr_button3_rawimg.attr,
646         &dev_attr_button4_rawimg.attr,
647         &dev_attr_button5_rawimg.attr,
648         &dev_attr_button6_rawimg.attr,
649         &dev_attr_button7_rawimg.attr,
650         NULL
651 };
652
653 static struct attribute_group wacom_led_attr_group = {
654         .name = "wacom_led",
655         .attrs = wacom_led_attrs,
656 };
657
658 static int wacom_initialize_leds(struct wacom *wacom)
659 {
660         int error;
661
662         if (wacom->wacom_wac.features.type >= INTUOS4 &&
663             wacom->wacom_wac.features.type <= INTUOS4L) {
664
665                 /* Initialize default values */
666                 wacom->led.select = 0;
667                 wacom->led.llv = 30;
668                 wacom->led.hlv = 20;
669                 wacom->led.img_lum = 10;
670                 wacom_led_control(wacom);
671
672                 error = sysfs_create_group(&wacom->intf->dev.kobj,
673                                            &wacom_led_attr_group);
674                 if (error) {
675                         dev_err(&wacom->intf->dev,
676                                 "cannot create sysfs group err: %d\n", error);
677                         return error;
678                 }
679         }
680
681         return 0;
682 }
683
684 static void wacom_destroy_leds(struct wacom *wacom)
685 {
686         if (wacom->wacom_wac.features.type >= INTUOS4 &&
687             wacom->wacom_wac.features.type <= INTUOS4L) {
688                 sysfs_remove_group(&wacom->intf->dev.kobj,
689                                    &wacom_led_attr_group);
690         }
691 }
692
693 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
694 {
695         struct usb_device *dev = interface_to_usbdev(intf);
696         struct usb_endpoint_descriptor *endpoint;
697         struct wacom *wacom;
698         struct wacom_wac *wacom_wac;
699         struct wacom_features *features;
700         struct input_dev *input_dev;
701         int error;
702
703         if (!id->driver_info)
704                 return -EINVAL;
705
706         wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
707         input_dev = input_allocate_device();
708         if (!wacom || !input_dev) {
709                 error = -ENOMEM;
710                 goto fail1;
711         }
712
713         wacom_wac = &wacom->wacom_wac;
714         wacom_wac->features = *((struct wacom_features *)id->driver_info);
715         features = &wacom_wac->features;
716         if (features->pktlen > WACOM_PKGLEN_MAX) {
717                 error = -EINVAL;
718                 goto fail1;
719         }
720
721         wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
722                                              GFP_KERNEL, &wacom->data_dma);
723         if (!wacom_wac->data) {
724                 error = -ENOMEM;
725                 goto fail1;
726         }
727
728         wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
729         if (!wacom->irq) {
730                 error = -ENOMEM;
731                 goto fail2;
732         }
733
734         wacom->usbdev = dev;
735         wacom->intf = intf;
736         mutex_init(&wacom->lock);
737         usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
738         strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
739
740         wacom_wac->input = input_dev;
741
742         endpoint = &intf->cur_altsetting->endpoint[0].desc;
743
744         /* Retrieve the physical and logical size for OEM devices */
745         error = wacom_retrieve_hid_descriptor(intf, features);
746         if (error)
747                 goto fail3;
748
749         wacom_setup_device_quirks(features);
750
751         strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
752
753         if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
754                 /* Append the device type to the name */
755                 strlcat(wacom_wac->name,
756                         features->device_type == BTN_TOOL_PEN ?
757                                 " Pen" : " Finger",
758                         sizeof(wacom_wac->name));
759
760                 error = wacom_add_shared_data(wacom_wac, dev);
761                 if (error)
762                         goto fail3;
763         }
764
765         input_dev->name = wacom_wac->name;
766         input_dev->dev.parent = &intf->dev;
767         input_dev->open = wacom_open;
768         input_dev->close = wacom_close;
769         usb_to_input_id(dev, &input_dev->id);
770         input_set_drvdata(input_dev, wacom);
771
772         wacom_setup_input_capabilities(input_dev, wacom_wac);
773
774         usb_fill_int_urb(wacom->irq, dev,
775                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
776                          wacom_wac->data, features->pktlen,
777                          wacom_sys_irq, wacom, endpoint->bInterval);
778         wacom->irq->transfer_dma = wacom->data_dma;
779         wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
780
781         error = wacom_initialize_leds(wacom);
782         if (error)
783                 goto fail4;
784
785         error = input_register_device(input_dev);
786         if (error)
787                 goto fail5;
788
789         /* Note that if query fails it is not a hard failure */
790         wacom_query_tablet_data(intf, features);
791
792         usb_set_intfdata(intf, wacom);
793         return 0;
794
795  fail5: wacom_destroy_leds(wacom);
796  fail4: wacom_remove_shared_data(wacom_wac);
797  fail3: usb_free_urb(wacom->irq);
798  fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
799  fail1: input_free_device(input_dev);
800         kfree(wacom);
801         return error;
802 }
803
804 static void wacom_disconnect(struct usb_interface *intf)
805 {
806         struct wacom *wacom = usb_get_intfdata(intf);
807
808         usb_set_intfdata(intf, NULL);
809
810         usb_kill_urb(wacom->irq);
811         input_unregister_device(wacom->wacom_wac.input);
812         wacom_destroy_leds(wacom);
813         usb_free_urb(wacom->irq);
814         usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
815                         wacom->wacom_wac.data, wacom->data_dma);
816         wacom_remove_shared_data(&wacom->wacom_wac);
817         kfree(wacom);
818 }
819
820 static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
821 {
822         struct wacom *wacom = usb_get_intfdata(intf);
823
824         mutex_lock(&wacom->lock);
825         usb_kill_urb(wacom->irq);
826         mutex_unlock(&wacom->lock);
827
828         return 0;
829 }
830
831 static int wacom_resume(struct usb_interface *intf)
832 {
833         struct wacom *wacom = usb_get_intfdata(intf);
834         struct wacom_features *features = &wacom->wacom_wac.features;
835         int rv = 0;
836
837         mutex_lock(&wacom->lock);
838
839         /* switch to wacom mode first */
840         wacom_query_tablet_data(intf, features);
841         wacom_led_control(wacom);
842
843         if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
844                 rv = -EIO;
845
846         mutex_unlock(&wacom->lock);
847
848         return rv;
849 }
850
851 static int wacom_reset_resume(struct usb_interface *intf)
852 {
853         return wacom_resume(intf);
854 }
855
856 static struct usb_driver wacom_driver = {
857         .name =         "wacom",
858         .id_table =     wacom_ids,
859         .probe =        wacom_probe,
860         .disconnect =   wacom_disconnect,
861         .suspend =      wacom_suspend,
862         .resume =       wacom_resume,
863         .reset_resume = wacom_reset_resume,
864         .supports_autosuspend = 1,
865 };
866
867 static int __init wacom_init(void)
868 {
869         int result;
870
871         result = usb_register(&wacom_driver);
872         if (result == 0)
873                 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
874                        DRIVER_DESC "\n");
875         return result;
876 }
877
878 static void __exit wacom_exit(void)
879 {
880         usb_deregister(&wacom_driver);
881 }
882
883 module_init(wacom_init);
884 module_exit(wacom_exit);