Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6.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 #define WAC_HID_FEATURE_REPORT  0x03
52
53 static int usb_get_report(struct usb_interface *intf, unsigned char type,
54                                 unsigned char id, void *buf, int size)
55 {
56         return usb_control_msg(interface_to_usbdev(intf),
57                 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
58                 USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
59                 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
60                 buf, size, 100);
61 }
62
63 static int usb_set_report(struct usb_interface *intf, unsigned char type,
64                                 unsigned char id, void *buf, int size)
65 {
66         return usb_control_msg(interface_to_usbdev(intf),
67                 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
68                 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
69                 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
70                 buf, size, 1000);
71 }
72
73 static void wacom_sys_irq(struct urb *urb)
74 {
75         struct wacom *wacom = urb->context;
76         int retval;
77
78         switch (urb->status) {
79         case 0:
80                 /* success */
81                 break;
82         case -ECONNRESET:
83         case -ENOENT:
84         case -ESHUTDOWN:
85                 /* this urb is terminated, clean up */
86                 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
87                 return;
88         default:
89                 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
90                 goto exit;
91         }
92
93         wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
94
95  exit:
96         usb_mark_last_busy(wacom->usbdev);
97         retval = usb_submit_urb(urb, GFP_ATOMIC);
98         if (retval)
99                 err ("%s - usb_submit_urb failed with result %d",
100                      __func__, retval);
101 }
102
103 static int wacom_open(struct input_dev *dev)
104 {
105         struct wacom *wacom = input_get_drvdata(dev);
106
107         mutex_lock(&wacom->lock);
108
109         wacom->irq->dev = wacom->usbdev;
110
111         if (usb_autopm_get_interface(wacom->intf) < 0) {
112                 mutex_unlock(&wacom->lock);
113                 return -EIO;
114         }
115
116         if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
117                 usb_autopm_put_interface(wacom->intf);
118                 mutex_unlock(&wacom->lock);
119                 return -EIO;
120         }
121
122         wacom->open = true;
123         wacom->intf->needs_remote_wakeup = 1;
124
125         mutex_unlock(&wacom->lock);
126         return 0;
127 }
128
129 static void wacom_close(struct input_dev *dev)
130 {
131         struct wacom *wacom = input_get_drvdata(dev);
132
133         mutex_lock(&wacom->lock);
134         usb_kill_urb(wacom->irq);
135         wacom->open = false;
136         wacom->intf->needs_remote_wakeup = 0;
137         mutex_unlock(&wacom->lock);
138 }
139
140 static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
141                            struct wacom_features *features)
142 {
143         struct usb_device *dev = interface_to_usbdev(intf);
144         char limit = 0;
145         /* result has to be defined as int for some devices */
146         int result = 0;
147         int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
148         unsigned char *report;
149
150         report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
151         if (!report)
152                 return -ENOMEM;
153
154         /* retrive report descriptors */
155         do {
156                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
157                         USB_REQ_GET_DESCRIPTOR,
158                         USB_RECIP_INTERFACE | USB_DIR_IN,
159                         HID_DEVICET_REPORT << 8,
160                         intf->altsetting[0].desc.bInterfaceNumber, /* interface */
161                         report,
162                         hid_desc->wDescriptorLength,
163                         5000); /* 5 secs */
164         } while (result < 0 && limit++ < 5);
165
166         /* No need to parse the Descriptor. It isn't an error though */
167         if (result < 0)
168                 goto out;
169
170         for (i = 0; i < hid_desc->wDescriptorLength; i++) {
171
172                 switch (report[i]) {
173                 case HID_USAGE_PAGE:
174                         switch (report[i + 1]) {
175                         case HID_USAGE_PAGE_DIGITIZER:
176                                 usage = WCM_DIGITIZER;
177                                 i++;
178                                 break;
179
180                         case HID_USAGE_PAGE_DESKTOP:
181                                 usage = WCM_DESKTOP;
182                                 i++;
183                                 break;
184                         }
185                         break;
186
187                 case HID_USAGE:
188                         switch (report[i + 1]) {
189                         case HID_USAGE_X:
190                                 if (usage == WCM_DESKTOP) {
191                                         if (finger) {
192                                                 features->device_type = BTN_TOOL_DOUBLETAP;
193                                                 if (features->type == TABLETPC2FG) {
194                                                         /* need to reset back */
195                                                         features->pktlen = WACOM_PKGLEN_TPC2FG;
196                                                         features->device_type = BTN_TOOL_TRIPLETAP;
197                                                 }
198                                                 features->x_max =
199                                                         get_unaligned_le16(&report[i + 3]);
200                                                 features->x_phy =
201                                                         get_unaligned_le16(&report[i + 6]);
202                                                 features->unit = report[i + 9];
203                                                 features->unitExpo = report[i + 11];
204                                                 i += 12;
205                                         } else if (pen) {
206                                                 /* penabled only accepts exact bytes of data */
207                                                 if (features->type == TABLETPC2FG)
208                                                         features->pktlen = WACOM_PKGLEN_GRAPHIRE;
209                                                 features->device_type = BTN_TOOL_PEN;
210                                                 features->x_max =
211                                                         get_unaligned_le16(&report[i + 3]);
212                                                 i += 4;
213                                         }
214                                 } else if (usage == WCM_DIGITIZER) {
215                                         /* max pressure isn't reported
216                                         features->pressure_max = (unsigned short)
217                                                         (report[i+4] << 8  | report[i + 3]);
218                                         */
219                                         features->pressure_max = 255;
220                                         i += 4;
221                                 }
222                                 break;
223
224                         case HID_USAGE_Y:
225                                 if (usage == WCM_DESKTOP) {
226                                         if (finger) {
227                                                 features->device_type = BTN_TOOL_DOUBLETAP;
228                                                 if (features->type == TABLETPC2FG) {
229                                                         /* need to reset back */
230                                                         features->pktlen = WACOM_PKGLEN_TPC2FG;
231                                                         features->device_type = BTN_TOOL_TRIPLETAP;
232                                                         features->y_max =
233                                                                 get_unaligned_le16(&report[i + 3]);
234                                                         features->y_phy =
235                                                                 get_unaligned_le16(&report[i + 6]);
236                                                         i += 7;
237                                                 } else {
238                                                         features->y_max =
239                                                                 features->x_max;
240                                                         features->y_phy =
241                                                                 get_unaligned_le16(&report[i + 3]);
242                                                         i += 4;
243                                                 }
244                                         } else if (pen) {
245                                                 /* penabled only accepts exact bytes of data */
246                                                 if (features->type == TABLETPC2FG)
247                                                         features->pktlen = WACOM_PKGLEN_GRAPHIRE;
248                                                 features->device_type = BTN_TOOL_PEN;
249                                                 features->y_max =
250                                                         get_unaligned_le16(&report[i + 3]);
251                                                 i += 4;
252                                         }
253                                 }
254                                 break;
255
256                         case HID_USAGE_FINGER:
257                                 finger = 1;
258                                 i++;
259                                 break;
260
261                         case HID_USAGE_STYLUS:
262                                 pen = 1;
263                                 i++;
264                                 break;
265
266                         case HID_USAGE_UNDEFINED:
267                                 if (usage == WCM_DESKTOP && finger) /* capacity */
268                                         features->pressure_max =
269                                                 get_unaligned_le16(&report[i + 3]);
270                                 i += 4;
271                                 break;
272                         }
273                         break;
274
275                 case HID_COLLECTION:
276                         /* reset UsagePage and Finger */
277                         finger = usage = 0;
278                         break;
279                 }
280         }
281
282  out:
283         result = 0;
284         kfree(report);
285         return result;
286 }
287
288 static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
289 {
290         unsigned char *rep_data;
291         int limit = 0, report_id = 2;
292         int error = -ENOMEM;
293
294         rep_data = kmalloc(2, GFP_KERNEL);
295         if (!rep_data)
296                 return error;
297
298         /* ask to report tablet data if it is 2FGT or not a Tablet PC */
299         if (features->device_type == BTN_TOOL_TRIPLETAP) {
300                 do {
301                         rep_data[0] = 3;
302                         rep_data[1] = 4;
303                         report_id = 3;
304                         error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
305                                 report_id, rep_data, 2);
306                         if (error >= 0)
307                                 error = usb_get_report(intf,
308                                         WAC_HID_FEATURE_REPORT, report_id,
309                                         rep_data, 3);
310                 } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
311         } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
312                 do {
313                         rep_data[0] = 2;
314                         rep_data[1] = 2;
315                         error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
316                                 report_id, rep_data, 2);
317                         if (error >= 0)
318                                 error = usb_get_report(intf,
319                                         WAC_HID_FEATURE_REPORT, report_id,
320                                         rep_data, 2);
321                 } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
322         }
323
324         kfree(rep_data);
325
326         return error < 0 ? error : 0;
327 }
328
329 static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
330                 struct wacom_features *features)
331 {
332         int error = 0;
333         struct usb_host_interface *interface = intf->cur_altsetting;
334         struct hid_descriptor *hid_desc;
335
336         /* default device to penabled */
337         features->device_type = BTN_TOOL_PEN;
338
339         /* only Tablet PCs need to retrieve the info */
340         if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
341                 goto out;
342
343         if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
344                 if (usb_get_extra_descriptor(&interface->endpoint[0],
345                                 HID_DEVICET_REPORT, &hid_desc)) {
346                         printk("wacom: can not retrieve extra class descriptor\n");
347                         error = 1;
348                         goto out;
349                 }
350         }
351         error = wacom_parse_hid(intf, hid_desc, features);
352         if (error)
353                 goto out;
354
355         /* touch device found but size is not defined. use default */
356         if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
357                 features->x_max = 1023;
358                 features->y_max = 1023;
359         }
360
361  out:
362         return error;
363 }
364
365 struct wacom_usbdev_data {
366         struct list_head list;
367         struct kref kref;
368         struct usb_device *dev;
369         struct wacom_shared shared;
370 };
371
372 static LIST_HEAD(wacom_udev_list);
373 static DEFINE_MUTEX(wacom_udev_list_lock);
374
375 static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
376 {
377         struct wacom_usbdev_data *data;
378
379         list_for_each_entry(data, &wacom_udev_list, list) {
380                 if (data->dev == dev) {
381                         kref_get(&data->kref);
382                         return data;
383                 }
384         }
385
386         return NULL;
387 }
388
389 static int wacom_add_shared_data(struct wacom_wac *wacom,
390                                  struct usb_device *dev)
391 {
392         struct wacom_usbdev_data *data;
393         int retval = 0;
394
395         mutex_lock(&wacom_udev_list_lock);
396
397         data = wacom_get_usbdev_data(dev);
398         if (!data) {
399                 data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
400                 if (!data) {
401                         retval = -ENOMEM;
402                         goto out;
403                 }
404
405                 kref_init(&data->kref);
406                 data->dev = dev;
407                 list_add_tail(&data->list, &wacom_udev_list);
408         }
409
410         wacom->shared = &data->shared;
411
412 out:
413         mutex_unlock(&wacom_udev_list_lock);
414         return retval;
415 }
416
417 static void wacom_release_shared_data(struct kref *kref)
418 {
419         struct wacom_usbdev_data *data =
420                 container_of(kref, struct wacom_usbdev_data, kref);
421
422         mutex_lock(&wacom_udev_list_lock);
423         list_del(&data->list);
424         mutex_unlock(&wacom_udev_list_lock);
425
426         kfree(data);
427 }
428
429 static void wacom_remove_shared_data(struct wacom_wac *wacom)
430 {
431         struct wacom_usbdev_data *data;
432
433         if (wacom->shared) {
434                 data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
435                 kref_put(&data->kref, wacom_release_shared_data);
436                 wacom->shared = NULL;
437         }
438 }
439
440 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
441 {
442         struct usb_device *dev = interface_to_usbdev(intf);
443         struct usb_endpoint_descriptor *endpoint;
444         struct wacom *wacom;
445         struct wacom_wac *wacom_wac;
446         struct wacom_features *features;
447         struct input_dev *input_dev;
448         int error;
449
450         if (!id->driver_info)
451                 return -EINVAL;
452
453         wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
454         input_dev = input_allocate_device();
455         if (!wacom || !input_dev) {
456                 error = -ENOMEM;
457                 goto fail1;
458         }
459
460         wacom_wac = &wacom->wacom_wac;
461         wacom_wac->features = *((struct wacom_features *)id->driver_info);
462         features = &wacom_wac->features;
463         if (features->pktlen > WACOM_PKGLEN_MAX) {
464                 error = -EINVAL;
465                 goto fail1;
466         }
467
468         wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
469                                              GFP_KERNEL, &wacom->data_dma);
470         if (!wacom_wac->data) {
471                 error = -ENOMEM;
472                 goto fail1;
473         }
474
475         wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
476         if (!wacom->irq) {
477                 error = -ENOMEM;
478                 goto fail2;
479         }
480
481         wacom->usbdev = dev;
482         wacom->intf = intf;
483         mutex_init(&wacom->lock);
484         usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
485         strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
486
487         wacom_wac->input = input_dev;
488
489         endpoint = &intf->cur_altsetting->endpoint[0].desc;
490
491         /* Retrieve the physical and logical size for OEM devices */
492         error = wacom_retrieve_hid_descriptor(intf, features);
493         if (error)
494                 goto fail2;
495
496         strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
497
498         if (features->type == TABLETPC || features->type == TABLETPC2FG) {
499                 /* Append the device type to the name */
500                 strlcat(wacom_wac->name,
501                         features->device_type == BTN_TOOL_PEN ?
502                                 " Pen" : " Finger",
503                         sizeof(wacom_wac->name));
504
505                 error = wacom_add_shared_data(wacom_wac, dev);
506                 if (error)
507                         goto fail3;
508         }
509
510         input_dev->name = wacom_wac->name;
511         input_dev->name = wacom_wac->name;
512         input_dev->dev.parent = &intf->dev;
513         input_dev->open = wacom_open;
514         input_dev->close = wacom_close;
515         usb_to_input_id(dev, &input_dev->id);
516         input_set_drvdata(input_dev, wacom);
517
518         wacom_setup_input_capabilities(input_dev, wacom_wac);
519
520         usb_fill_int_urb(wacom->irq, dev,
521                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
522                          wacom_wac->data, features->pktlen,
523                          wacom_sys_irq, wacom, endpoint->bInterval);
524         wacom->irq->transfer_dma = wacom->data_dma;
525         wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
526
527         error = input_register_device(input_dev);
528         if (error)
529                 goto fail4;
530
531         /* Note that if query fails it is not a hard failure */
532         wacom_query_tablet_data(intf, features);
533
534         usb_set_intfdata(intf, wacom);
535         return 0;
536
537  fail4: wacom_remove_shared_data(wacom_wac);
538  fail3: usb_free_urb(wacom->irq);
539  fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
540  fail1: input_free_device(input_dev);
541         kfree(wacom);
542         return error;
543 }
544
545 static void wacom_disconnect(struct usb_interface *intf)
546 {
547         struct wacom *wacom = usb_get_intfdata(intf);
548
549         usb_set_intfdata(intf, NULL);
550
551         usb_kill_urb(wacom->irq);
552         input_unregister_device(wacom->wacom_wac.input);
553         usb_free_urb(wacom->irq);
554         usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
555                         wacom->wacom_wac.data, wacom->data_dma);
556         wacom_remove_shared_data(&wacom->wacom_wac);
557         kfree(wacom);
558 }
559
560 static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
561 {
562         struct wacom *wacom = usb_get_intfdata(intf);
563
564         mutex_lock(&wacom->lock);
565         usb_kill_urb(wacom->irq);
566         mutex_unlock(&wacom->lock);
567
568         return 0;
569 }
570
571 static int wacom_resume(struct usb_interface *intf)
572 {
573         struct wacom *wacom = usb_get_intfdata(intf);
574         struct wacom_features *features = &wacom->wacom_wac.features;
575         int rv;
576
577         mutex_lock(&wacom->lock);
578
579         /* switch to wacom mode first */
580         wacom_query_tablet_data(intf, features);
581
582         if (wacom->open)
583                 rv = usb_submit_urb(wacom->irq, GFP_NOIO);
584         else
585                 rv = 0;
586
587         mutex_unlock(&wacom->lock);
588
589         return rv;
590 }
591
592 static int wacom_reset_resume(struct usb_interface *intf)
593 {
594         return wacom_resume(intf);
595 }
596
597 static struct usb_driver wacom_driver = {
598         .name =         "wacom",
599         .id_table =     wacom_ids,
600         .probe =        wacom_probe,
601         .disconnect =   wacom_disconnect,
602         .suspend =      wacom_suspend,
603         .resume =       wacom_resume,
604         .reset_resume = wacom_reset_resume,
605         .supports_autosuspend = 1,
606 };
607
608 static int __init wacom_init(void)
609 {
610         int result;
611
612         result = usb_register(&wacom_driver);
613         if (result == 0)
614                 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
615                        DRIVER_DESC "\n");
616         return result;
617 }
618
619 static void __exit wacom_exit(void)
620 {
621         usb_deregister(&wacom_driver);
622 }
623
624 module_init(wacom_init);
625 module_exit(wacom_exit);