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