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