USB: gadget: f_audio_source: Adjust packet timing to reduce glitches
[linux-2.6.git] / drivers / usb / class / cdc-wdm.c
1 /*
2  * cdc-wdm.c
3  *
4  * This driver supports USB CDC WCM Device Management.
5  *
6  * Copyright (c) 2007-2009 Oliver Neukum
7  *
8  * Some code taken from cdc-acm.c
9  *
10  * Released under the GPLv2.
11  *
12  * Many thanks to Carl Nordbeck
13  */
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/uaccess.h>
20 #include <linux/bitops.h>
21 #include <linux/poll.h>
22 #include <linux/usb.h>
23 #include <linux/usb/cdc.h>
24 #include <asm/byteorder.h>
25 #include <asm/unaligned.h>
26
27 /*
28  * Version Information
29  */
30 #define DRIVER_VERSION "v0.03"
31 #define DRIVER_AUTHOR "Oliver Neukum"
32 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
33
34 static const struct usb_device_id wdm_ids[] = {
35         {
36                 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
37                                  USB_DEVICE_ID_MATCH_INT_SUBCLASS,
38                 .bInterfaceClass = USB_CLASS_COMM,
39                 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
40         },
41         { }
42 };
43
44 MODULE_DEVICE_TABLE (usb, wdm_ids);
45
46 #define WDM_MINOR_BASE  176
47
48
49 #define WDM_IN_USE              1
50 #define WDM_DISCONNECTING       2
51 #define WDM_RESULT              3
52 #define WDM_READ                4
53 #define WDM_INT_STALL           5
54 #define WDM_POLL_RUNNING        6
55 #define WDM_RESPONDING          7
56 #define WDM_SUSPENDING          8
57
58 #define WDM_MAX                 16
59
60 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
61 #define WDM_DEFAULT_BUFSIZE     256
62
63 static DEFINE_MUTEX(wdm_mutex);
64
65 /* --- method tables --- */
66
67 struct wdm_device {
68         u8                      *inbuf; /* buffer for response */
69         u8                      *outbuf; /* buffer for command */
70         u8                      *sbuf; /* buffer for status */
71         u8                      *ubuf; /* buffer for copy to user space */
72
73         struct urb              *command;
74         struct urb              *response;
75         struct urb              *validity;
76         struct usb_interface    *intf;
77         struct usb_ctrlrequest  *orq;
78         struct usb_ctrlrequest  *irq;
79         spinlock_t              iuspin;
80
81         unsigned long           flags;
82         u16                     bufsize;
83         u16                     wMaxCommand;
84         u16                     wMaxPacketSize;
85         u16                     bMaxPacketSize0;
86         __le16                  inum;
87         int                     reslength;
88         int                     length;
89         int                     read;
90         int                     count;
91         dma_addr_t              shandle;
92         dma_addr_t              ihandle;
93         struct mutex            wlock;
94         struct mutex            rlock;
95         wait_queue_head_t       wait;
96         struct work_struct      rxwork;
97         int                     werr;
98         int                     rerr;
99 };
100
101 static struct usb_driver wdm_driver;
102
103 /* --- callbacks --- */
104 static void wdm_out_callback(struct urb *urb)
105 {
106         struct wdm_device *desc;
107         desc = urb->context;
108         spin_lock(&desc->iuspin);
109         desc->werr = urb->status;
110         spin_unlock(&desc->iuspin);
111         clear_bit(WDM_IN_USE, &desc->flags);
112         kfree(desc->outbuf);
113         wake_up(&desc->wait);
114 }
115
116 static void wdm_in_callback(struct urb *urb)
117 {
118         struct wdm_device *desc = urb->context;
119         int status = urb->status;
120
121         spin_lock(&desc->iuspin);
122         clear_bit(WDM_RESPONDING, &desc->flags);
123
124         if (status) {
125                 switch (status) {
126                 case -ENOENT:
127                         dev_dbg(&desc->intf->dev,
128                                 "nonzero urb status received: -ENOENT");
129                         goto skip_error;
130                 case -ECONNRESET:
131                         dev_dbg(&desc->intf->dev,
132                                 "nonzero urb status received: -ECONNRESET");
133                         goto skip_error;
134                 case -ESHUTDOWN:
135                         dev_dbg(&desc->intf->dev,
136                                 "nonzero urb status received: -ESHUTDOWN");
137                         goto skip_error;
138                 case -EPIPE:
139                         dev_err(&desc->intf->dev,
140                                 "nonzero urb status received: -EPIPE\n");
141                         break;
142                 default:
143                         dev_err(&desc->intf->dev,
144                                 "Unexpected error %d\n", status);
145                         break;
146                 }
147         }
148
149         desc->rerr = status;
150         desc->reslength = urb->actual_length;
151         memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
152         desc->length += desc->reslength;
153 skip_error:
154         wake_up(&desc->wait);
155
156         set_bit(WDM_READ, &desc->flags);
157         spin_unlock(&desc->iuspin);
158 }
159
160 static void wdm_int_callback(struct urb *urb)
161 {
162         int rv = 0;
163         int status = urb->status;
164         struct wdm_device *desc;
165         struct usb_ctrlrequest *req;
166         struct usb_cdc_notification *dr;
167
168         desc = urb->context;
169         req = desc->irq;
170         dr = (struct usb_cdc_notification *)desc->sbuf;
171
172         if (status) {
173                 switch (status) {
174                 case -ESHUTDOWN:
175                 case -ENOENT:
176                 case -ECONNRESET:
177                         return; /* unplug */
178                 case -EPIPE:
179                         set_bit(WDM_INT_STALL, &desc->flags);
180                         dev_err(&desc->intf->dev, "Stall on int endpoint\n");
181                         goto sw; /* halt is cleared in work */
182                 default:
183                         dev_err(&desc->intf->dev,
184                                 "nonzero urb status received: %d\n", status);
185                         break;
186                 }
187         }
188
189         if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
190                 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
191                         urb->actual_length);
192                 goto exit;
193         }
194
195         switch (dr->bNotificationType) {
196         case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
197                 dev_dbg(&desc->intf->dev,
198                         "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
199                         dr->wIndex, dr->wLength);
200                 break;
201
202         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
203
204                 dev_dbg(&desc->intf->dev,
205                         "NOTIFY_NETWORK_CONNECTION %s network",
206                         dr->wValue ? "connected to" : "disconnected from");
207                 goto exit;
208         default:
209                 clear_bit(WDM_POLL_RUNNING, &desc->flags);
210                 dev_err(&desc->intf->dev,
211                         "unknown notification %d received: index %d len %d\n",
212                         dr->bNotificationType, dr->wIndex, dr->wLength);
213                 goto exit;
214         }
215
216         req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
217         req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
218         req->wValue = 0;
219         req->wIndex = desc->inum;
220         req->wLength = cpu_to_le16(desc->wMaxCommand);
221
222         usb_fill_control_urb(
223                 desc->response,
224                 interface_to_usbdev(desc->intf),
225                 /* using common endpoint 0 */
226                 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
227                 (unsigned char *)req,
228                 desc->inbuf,
229                 desc->wMaxCommand,
230                 wdm_in_callback,
231                 desc
232         );
233         desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
234         spin_lock(&desc->iuspin);
235         clear_bit(WDM_READ, &desc->flags);
236         set_bit(WDM_RESPONDING, &desc->flags);
237         if (!test_bit(WDM_DISCONNECTING, &desc->flags)
238                 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
239                 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
240                 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
241                         __func__, rv);
242         }
243         spin_unlock(&desc->iuspin);
244         if (rv < 0) {
245                 clear_bit(WDM_RESPONDING, &desc->flags);
246                 if (rv == -EPERM)
247                         return;
248                 if (rv == -ENOMEM) {
249 sw:
250                         rv = schedule_work(&desc->rxwork);
251                         if (rv)
252                                 dev_err(&desc->intf->dev,
253                                         "Cannot schedule work\n");
254                 }
255         }
256 exit:
257         rv = usb_submit_urb(urb, GFP_ATOMIC);
258         if (rv)
259                 dev_err(&desc->intf->dev,
260                         "%s - usb_submit_urb failed with result %d\n",
261                         __func__, rv);
262
263 }
264
265 static void kill_urbs(struct wdm_device *desc)
266 {
267         /* the order here is essential */
268         usb_kill_urb(desc->command);
269         usb_kill_urb(desc->validity);
270         usb_kill_urb(desc->response);
271 }
272
273 static void free_urbs(struct wdm_device *desc)
274 {
275         usb_free_urb(desc->validity);
276         usb_free_urb(desc->response);
277         usb_free_urb(desc->command);
278 }
279
280 static void cleanup(struct wdm_device *desc)
281 {
282         usb_free_coherent(interface_to_usbdev(desc->intf),
283                           desc->wMaxPacketSize,
284                           desc->sbuf,
285                           desc->validity->transfer_dma);
286         usb_free_coherent(interface_to_usbdev(desc->intf),
287                           desc->bMaxPacketSize0,
288                           desc->inbuf,
289                           desc->response->transfer_dma);
290         kfree(desc->orq);
291         kfree(desc->irq);
292         kfree(desc->ubuf);
293         free_urbs(desc);
294         kfree(desc);
295 }
296
297 static ssize_t wdm_write
298 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
299 {
300         u8 *buf;
301         int rv = -EMSGSIZE, r, we;
302         struct wdm_device *desc = file->private_data;
303         struct usb_ctrlrequest *req;
304
305         if (count > desc->wMaxCommand)
306                 count = desc->wMaxCommand;
307
308         spin_lock_irq(&desc->iuspin);
309         we = desc->werr;
310         desc->werr = 0;
311         spin_unlock_irq(&desc->iuspin);
312         if (we < 0)
313                 return -EIO;
314
315         desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
316         if (!buf) {
317                 rv = -ENOMEM;
318                 goto outnl;
319         }
320
321         r = copy_from_user(buf, buffer, count);
322         if (r > 0) {
323                 kfree(buf);
324                 rv = -EFAULT;
325                 goto outnl;
326         }
327
328         /* concurrent writes and disconnect */
329         r = mutex_lock_interruptible(&desc->wlock);
330         rv = -ERESTARTSYS;
331         if (r) {
332                 kfree(buf);
333                 goto outnl;
334         }
335
336         if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
337                 kfree(buf);
338                 rv = -ENODEV;
339                 goto outnp;
340         }
341
342         r = usb_autopm_get_interface(desc->intf);
343         if (r < 0) {
344                 kfree(buf);
345                 goto outnp;
346         }
347
348         if (!(file->f_flags & O_NONBLOCK))
349                 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
350                                                                 &desc->flags));
351         else
352                 if (test_bit(WDM_IN_USE, &desc->flags))
353                         r = -EAGAIN;
354         if (r < 0) {
355                 kfree(buf);
356                 goto out;
357         }
358
359         req = desc->orq;
360         usb_fill_control_urb(
361                 desc->command,
362                 interface_to_usbdev(desc->intf),
363                 /* using common endpoint 0 */
364                 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
365                 (unsigned char *)req,
366                 buf,
367                 count,
368                 wdm_out_callback,
369                 desc
370         );
371
372         req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
373                              USB_RECIP_INTERFACE);
374         req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
375         req->wValue = 0;
376         req->wIndex = desc->inum;
377         req->wLength = cpu_to_le16(count);
378         set_bit(WDM_IN_USE, &desc->flags);
379
380         rv = usb_submit_urb(desc->command, GFP_KERNEL);
381         if (rv < 0) {
382                 kfree(buf);
383                 clear_bit(WDM_IN_USE, &desc->flags);
384                 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
385         } else {
386                 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
387                         req->wIndex);
388         }
389 out:
390         usb_autopm_put_interface(desc->intf);
391 outnp:
392         mutex_unlock(&desc->wlock);
393 outnl:
394         return rv < 0 ? rv : count;
395 }
396
397 static ssize_t wdm_read
398 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
399 {
400         int rv, cntr = 0;
401         int i = 0;
402         struct wdm_device *desc = file->private_data;
403
404
405         rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
406         if (rv < 0)
407                 return -ERESTARTSYS;
408
409         if (desc->length == 0) {
410                 desc->read = 0;
411 retry:
412                 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
413                         rv = -ENODEV;
414                         goto err;
415                 }
416                 i++;
417                 if (file->f_flags & O_NONBLOCK) {
418                         if (!test_bit(WDM_READ, &desc->flags)) {
419                                 rv = cntr ? cntr : -EAGAIN;
420                                 goto err;
421                         }
422                         rv = 0;
423                 } else {
424                         rv = wait_event_interruptible(desc->wait,
425                                 test_bit(WDM_READ, &desc->flags));
426                 }
427
428                 /* may have happened while we slept */
429                 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
430                         rv = -ENODEV;
431                         goto err;
432                 }
433                 usb_mark_last_busy(interface_to_usbdev(desc->intf));
434                 if (rv < 0) {
435                         rv = -ERESTARTSYS;
436                         goto err;
437                 }
438
439                 spin_lock_irq(&desc->iuspin);
440
441                 if (desc->rerr) { /* read completed, error happened */
442                         desc->rerr = 0;
443                         spin_unlock_irq(&desc->iuspin);
444                         rv = -EIO;
445                         goto err;
446                 }
447                 /*
448                  * recheck whether we've lost the race
449                  * against the completion handler
450                  */
451                 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
452                         spin_unlock_irq(&desc->iuspin);
453                         goto retry;
454                 }
455                 if (!desc->reslength) { /* zero length read */
456                         spin_unlock_irq(&desc->iuspin);
457                         goto retry;
458                 }
459                 clear_bit(WDM_READ, &desc->flags);
460                 spin_unlock_irq(&desc->iuspin);
461         }
462
463         cntr = count > desc->length ? desc->length : count;
464         rv = copy_to_user(buffer, desc->ubuf, cntr);
465         if (rv > 0) {
466                 rv = -EFAULT;
467                 goto err;
468         }
469
470         for (i = 0; i < desc->length - cntr; i++)
471                 desc->ubuf[i] = desc->ubuf[i + cntr];
472
473         spin_lock_irq(&desc->iuspin);
474         desc->length -= cntr;
475         spin_unlock_irq(&desc->iuspin);
476         /* in case we had outstanding data */
477         if (!desc->length)
478                 clear_bit(WDM_READ, &desc->flags);
479         rv = cntr;
480
481 err:
482         mutex_unlock(&desc->rlock);
483         return rv;
484 }
485
486 static int wdm_flush(struct file *file, fl_owner_t id)
487 {
488         struct wdm_device *desc = file->private_data;
489
490         wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
491         if (desc->werr < 0)
492                 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
493                         desc->werr);
494
495         return desc->werr;
496 }
497
498 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
499 {
500         struct wdm_device *desc = file->private_data;
501         unsigned long flags;
502         unsigned int mask = 0;
503
504         spin_lock_irqsave(&desc->iuspin, flags);
505         if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
506                 mask = POLLERR;
507                 spin_unlock_irqrestore(&desc->iuspin, flags);
508                 goto desc_out;
509         }
510         if (test_bit(WDM_READ, &desc->flags))
511                 mask = POLLIN | POLLRDNORM;
512         if (desc->rerr || desc->werr)
513                 mask |= POLLERR;
514         if (!test_bit(WDM_IN_USE, &desc->flags))
515                 mask |= POLLOUT | POLLWRNORM;
516         spin_unlock_irqrestore(&desc->iuspin, flags);
517
518         poll_wait(file, &desc->wait, wait);
519
520 desc_out:
521         return mask;
522 }
523
524 static int wdm_open(struct inode *inode, struct file *file)
525 {
526         int minor = iminor(inode);
527         int rv = -ENODEV;
528         struct usb_interface *intf;
529         struct wdm_device *desc;
530
531         mutex_lock(&wdm_mutex);
532         intf = usb_find_interface(&wdm_driver, minor);
533         if (!intf)
534                 goto out;
535
536         desc = usb_get_intfdata(intf);
537         if (test_bit(WDM_DISCONNECTING, &desc->flags))
538                 goto out;
539         file->private_data = desc;
540
541         rv = usb_autopm_get_interface(desc->intf);
542         if (rv < 0) {
543                 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
544                 goto out;
545         }
546         intf->needs_remote_wakeup = 1;
547
548         /* using write lock to protect desc->count */
549         mutex_lock(&desc->wlock);
550         if (!desc->count++) {
551                 desc->werr = 0;
552                 desc->rerr = 0;
553                 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
554                 if (rv < 0) {
555                         desc->count--;
556                         dev_err(&desc->intf->dev,
557                                 "Error submitting int urb - %d\n", rv);
558                 }
559         } else {
560                 rv = 0;
561         }
562         mutex_unlock(&desc->wlock);
563         usb_autopm_put_interface(desc->intf);
564 out:
565         mutex_unlock(&wdm_mutex);
566         return rv;
567 }
568
569 static int wdm_release(struct inode *inode, struct file *file)
570 {
571         struct wdm_device *desc = file->private_data;
572
573         mutex_lock(&wdm_mutex);
574
575         /* using write lock to protect desc->count */
576         mutex_lock(&desc->wlock);
577         desc->count--;
578         mutex_unlock(&desc->wlock);
579
580         if (!desc->count) {
581                 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
582                 kill_urbs(desc);
583                 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
584                         desc->intf->needs_remote_wakeup = 0;
585         }
586         mutex_unlock(&wdm_mutex);
587         return 0;
588 }
589
590 static const struct file_operations wdm_fops = {
591         .owner =        THIS_MODULE,
592         .read =         wdm_read,
593         .write =        wdm_write,
594         .open =         wdm_open,
595         .flush =        wdm_flush,
596         .release =      wdm_release,
597         .poll =         wdm_poll,
598         .llseek =       noop_llseek,
599 };
600
601 static struct usb_class_driver wdm_class = {
602         .name =         "cdc-wdm%d",
603         .fops =         &wdm_fops,
604         .minor_base =   WDM_MINOR_BASE,
605 };
606
607 /* --- error handling --- */
608 static void wdm_rxwork(struct work_struct *work)
609 {
610         struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
611         unsigned long flags;
612         int rv;
613
614         spin_lock_irqsave(&desc->iuspin, flags);
615         if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
616                 spin_unlock_irqrestore(&desc->iuspin, flags);
617         } else {
618                 spin_unlock_irqrestore(&desc->iuspin, flags);
619                 rv = usb_submit_urb(desc->response, GFP_KERNEL);
620                 if (rv < 0 && rv != -EPERM) {
621                         spin_lock_irqsave(&desc->iuspin, flags);
622                         if (!test_bit(WDM_DISCONNECTING, &desc->flags))
623                                 schedule_work(&desc->rxwork);
624                         spin_unlock_irqrestore(&desc->iuspin, flags);
625                 }
626         }
627 }
628
629 /* --- hotplug --- */
630
631 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
632 {
633         int rv = -EINVAL;
634         struct usb_device *udev = interface_to_usbdev(intf);
635         struct wdm_device *desc;
636         struct usb_host_interface *iface;
637         struct usb_endpoint_descriptor *ep;
638         struct usb_cdc_dmm_desc *dmhd;
639         u8 *buffer = intf->altsetting->extra;
640         int buflen = intf->altsetting->extralen;
641         u16 maxcom = WDM_DEFAULT_BUFSIZE;
642
643         if (!buffer)
644                 goto out;
645
646         while (buflen > 2) {
647                 if (buffer [1] != USB_DT_CS_INTERFACE) {
648                         dev_err(&intf->dev, "skipping garbage\n");
649                         goto next_desc;
650                 }
651
652                 switch (buffer [2]) {
653                 case USB_CDC_HEADER_TYPE:
654                         break;
655                 case USB_CDC_DMM_TYPE:
656                         dmhd = (struct usb_cdc_dmm_desc *)buffer;
657                         maxcom = le16_to_cpu(dmhd->wMaxCommand);
658                         dev_dbg(&intf->dev,
659                                 "Finding maximum buffer length: %d", maxcom);
660                         break;
661                 default:
662                         dev_err(&intf->dev,
663                                 "Ignoring extra header, type %d, length %d\n",
664                                 buffer[2], buffer[0]);
665                         break;
666                 }
667 next_desc:
668                 buflen -= buffer[0];
669                 buffer += buffer[0];
670         }
671
672         rv = -ENOMEM;
673         desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
674         if (!desc)
675                 goto out;
676         mutex_init(&desc->rlock);
677         mutex_init(&desc->wlock);
678         spin_lock_init(&desc->iuspin);
679         init_waitqueue_head(&desc->wait);
680         desc->wMaxCommand = maxcom;
681         /* this will be expanded and needed in hardware endianness */
682         desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
683         desc->intf = intf;
684         INIT_WORK(&desc->rxwork, wdm_rxwork);
685
686         rv = -EINVAL;
687         iface = intf->cur_altsetting;
688         if (iface->desc.bNumEndpoints != 1)
689                 goto err;
690         ep = &iface->endpoint[0].desc;
691         if (!ep || !usb_endpoint_is_int_in(ep))
692                 goto err;
693
694         desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
695         desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
696
697         desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
698         if (!desc->orq)
699                 goto err;
700         desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
701         if (!desc->irq)
702                 goto err;
703
704         desc->validity = usb_alloc_urb(0, GFP_KERNEL);
705         if (!desc->validity)
706                 goto err;
707
708         desc->response = usb_alloc_urb(0, GFP_KERNEL);
709         if (!desc->response)
710                 goto err;
711
712         desc->command = usb_alloc_urb(0, GFP_KERNEL);
713         if (!desc->command)
714                 goto err;
715
716         desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
717         if (!desc->ubuf)
718                 goto err;
719
720         desc->sbuf = usb_alloc_coherent(interface_to_usbdev(intf),
721                                         desc->wMaxPacketSize,
722                                         GFP_KERNEL,
723                                         &desc->validity->transfer_dma);
724         if (!desc->sbuf)
725                 goto err;
726
727         desc->inbuf = usb_alloc_coherent(interface_to_usbdev(intf),
728                                          desc->wMaxCommand,
729                                          GFP_KERNEL,
730                                          &desc->response->transfer_dma);
731         if (!desc->inbuf)
732                 goto err2;
733
734         usb_fill_int_urb(
735                 desc->validity,
736                 interface_to_usbdev(intf),
737                 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
738                 desc->sbuf,
739                 desc->wMaxPacketSize,
740                 wdm_int_callback,
741                 desc,
742                 ep->bInterval
743         );
744         desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
745
746         usb_set_intfdata(intf, desc);
747         rv = usb_register_dev(intf, &wdm_class);
748         if (rv < 0)
749                 goto err3;
750         else
751                 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
752                         intf->minor - WDM_MINOR_BASE);
753 out:
754         return rv;
755 err3:
756         usb_set_intfdata(intf, NULL);
757         usb_free_coherent(interface_to_usbdev(desc->intf),
758                           desc->bMaxPacketSize0,
759                         desc->inbuf,
760                         desc->response->transfer_dma);
761 err2:
762         usb_free_coherent(interface_to_usbdev(desc->intf),
763                           desc->wMaxPacketSize,
764                           desc->sbuf,
765                           desc->validity->transfer_dma);
766 err:
767         free_urbs(desc);
768         kfree(desc->ubuf);
769         kfree(desc->orq);
770         kfree(desc->irq);
771         kfree(desc);
772         return rv;
773 }
774
775 static void wdm_disconnect(struct usb_interface *intf)
776 {
777         struct wdm_device *desc;
778         unsigned long flags;
779
780         usb_deregister_dev(intf, &wdm_class);
781         mutex_lock(&wdm_mutex);
782         desc = usb_get_intfdata(intf);
783
784         /* the spinlock makes sure no new urbs are generated in the callbacks */
785         spin_lock_irqsave(&desc->iuspin, flags);
786         set_bit(WDM_DISCONNECTING, &desc->flags);
787         set_bit(WDM_READ, &desc->flags);
788         /* to terminate pending flushes */
789         clear_bit(WDM_IN_USE, &desc->flags);
790         spin_unlock_irqrestore(&desc->iuspin, flags);
791         wake_up_all(&desc->wait);
792         mutex_lock(&desc->rlock);
793         mutex_lock(&desc->wlock);
794         kill_urbs(desc);
795         cancel_work_sync(&desc->rxwork);
796         mutex_unlock(&desc->wlock);
797         mutex_unlock(&desc->rlock);
798         if (!desc->count)
799                 cleanup(desc);
800         mutex_unlock(&wdm_mutex);
801 }
802
803 #ifdef CONFIG_PM
804 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
805 {
806         struct wdm_device *desc = usb_get_intfdata(intf);
807         int rv = 0;
808
809         dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
810
811         /* if this is an autosuspend the caller does the locking */
812         if (!(message.event & PM_EVENT_AUTO)) {
813                 mutex_lock(&desc->rlock);
814                 mutex_lock(&desc->wlock);
815         }
816         spin_lock_irq(&desc->iuspin);
817
818         if ((message.event & PM_EVENT_AUTO) &&
819                         (test_bit(WDM_IN_USE, &desc->flags)
820                         || test_bit(WDM_RESPONDING, &desc->flags))) {
821                 spin_unlock_irq(&desc->iuspin);
822                 rv = -EBUSY;
823         } else {
824
825                 set_bit(WDM_SUSPENDING, &desc->flags);
826                 spin_unlock_irq(&desc->iuspin);
827                 /* callback submits work - order is essential */
828                 kill_urbs(desc);
829                 cancel_work_sync(&desc->rxwork);
830         }
831         if (!(message.event & PM_EVENT_AUTO)) {
832                 mutex_unlock(&desc->wlock);
833                 mutex_unlock(&desc->rlock);
834         }
835
836         return rv;
837 }
838 #endif
839
840 static int recover_from_urb_loss(struct wdm_device *desc)
841 {
842         int rv = 0;
843
844         if (desc->count) {
845                 rv = usb_submit_urb(desc->validity, GFP_NOIO);
846                 if (rv < 0)
847                         dev_err(&desc->intf->dev,
848                                 "Error resume submitting int urb - %d\n", rv);
849         }
850         return rv;
851 }
852
853 #ifdef CONFIG_PM
854 static int wdm_resume(struct usb_interface *intf)
855 {
856         struct wdm_device *desc = usb_get_intfdata(intf);
857         int rv;
858
859         dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
860
861         clear_bit(WDM_SUSPENDING, &desc->flags);
862         rv = recover_from_urb_loss(desc);
863
864         return rv;
865 }
866 #endif
867
868 static int wdm_pre_reset(struct usb_interface *intf)
869 {
870         struct wdm_device *desc = usb_get_intfdata(intf);
871
872         mutex_lock(&desc->rlock);
873         mutex_lock(&desc->wlock);
874         kill_urbs(desc);
875
876         /*
877          * we notify everybody using poll of
878          * an exceptional situation
879          * must be done before recovery lest a spontaneous
880          * message from the device is lost
881          */
882         spin_lock_irq(&desc->iuspin);
883         desc->rerr = -EINTR;
884         spin_unlock_irq(&desc->iuspin);
885         wake_up_all(&desc->wait);
886         return 0;
887 }
888
889 static int wdm_post_reset(struct usb_interface *intf)
890 {
891         struct wdm_device *desc = usb_get_intfdata(intf);
892         int rv;
893
894         rv = recover_from_urb_loss(desc);
895         mutex_unlock(&desc->wlock);
896         mutex_unlock(&desc->rlock);
897         return 0;
898 }
899
900 static struct usb_driver wdm_driver = {
901         .name =         "cdc_wdm",
902         .probe =        wdm_probe,
903         .disconnect =   wdm_disconnect,
904 #ifdef CONFIG_PM
905         .suspend =      wdm_suspend,
906         .resume =       wdm_resume,
907         .reset_resume = wdm_resume,
908 #endif
909         .pre_reset =    wdm_pre_reset,
910         .post_reset =   wdm_post_reset,
911         .id_table =     wdm_ids,
912         .supports_autosuspend = 1,
913 };
914
915 /* --- low level module stuff --- */
916
917 static int __init wdm_init(void)
918 {
919         int rv;
920
921         rv = usb_register(&wdm_driver);
922
923         return rv;
924 }
925
926 static void __exit wdm_exit(void)
927 {
928         usb_deregister(&wdm_driver);
929 }
930
931 module_init(wdm_init);
932 module_exit(wdm_exit);
933
934 MODULE_AUTHOR(DRIVER_AUTHOR);
935 MODULE_DESCRIPTION(DRIVER_DESC);
936 MODULE_LICENSE("GPL");