ext3: Update MAINTAINERS for ext3 and JBD
[linux/fpc-iii.git] / drivers / usb / class / cdc-wdm.c
blobba589d4ca8bcb9e8783cc47bce18cab3faaa8301
1 /*
2 * cdc-wdm.c
4 * This driver supports USB CDC WCM Device Management.
6 * Copyright (c) 2007-2009 Oliver Neukum
8 * Some code taken from cdc-acm.c
10 * Released under the GPLv2.
12 * Many thanks to Carl Nordbeck
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>
28 * Version Information
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"
34 static struct usb_device_id wdm_ids[] = {
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
41 { }
44 MODULE_DEVICE_TABLE (usb, wdm_ids);
46 #define WDM_MINOR_BASE 176
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
57 #define WDM_MAX 16
60 static DEFINE_MUTEX(wdm_mutex);
62 /* --- method tables --- */
64 struct wdm_device {
65 u8 *inbuf; /* buffer for response */
66 u8 *outbuf; /* buffer for command */
67 u8 *sbuf; /* buffer for status */
68 u8 *ubuf; /* buffer for copy to user space */
70 struct urb *command;
71 struct urb *response;
72 struct urb *validity;
73 struct usb_interface *intf;
74 struct usb_ctrlrequest *orq;
75 struct usb_ctrlrequest *irq;
76 spinlock_t iuspin;
78 unsigned long flags;
79 u16 bufsize;
80 u16 wMaxCommand;
81 u16 wMaxPacketSize;
82 u16 bMaxPacketSize0;
83 __le16 inum;
84 int reslength;
85 int length;
86 int read;
87 int count;
88 dma_addr_t shandle;
89 dma_addr_t ihandle;
90 struct mutex wlock;
91 struct mutex rlock;
92 struct mutex plock;
93 wait_queue_head_t wait;
94 struct work_struct rxwork;
95 int werr;
96 int rerr;
99 static struct usb_driver wdm_driver;
101 /* --- callbacks --- */
102 static void wdm_out_callback(struct urb *urb)
104 struct wdm_device *desc;
105 desc = urb->context;
106 spin_lock(&desc->iuspin);
107 desc->werr = urb->status;
108 spin_unlock(&desc->iuspin);
109 clear_bit(WDM_IN_USE, &desc->flags);
110 kfree(desc->outbuf);
111 wake_up(&desc->wait);
114 static void wdm_in_callback(struct urb *urb)
116 struct wdm_device *desc = urb->context;
117 int status = urb->status;
119 spin_lock(&desc->iuspin);
121 if (status) {
122 switch (status) {
123 case -ENOENT:
124 dev_dbg(&desc->intf->dev,
125 "nonzero urb status received: -ENOENT");
126 break;
127 case -ECONNRESET:
128 dev_dbg(&desc->intf->dev,
129 "nonzero urb status received: -ECONNRESET");
130 break;
131 case -ESHUTDOWN:
132 dev_dbg(&desc->intf->dev,
133 "nonzero urb status received: -ESHUTDOWN");
134 break;
135 case -EPIPE:
136 dev_err(&desc->intf->dev,
137 "nonzero urb status received: -EPIPE\n");
138 break;
139 default:
140 dev_err(&desc->intf->dev,
141 "Unexpected error %d\n", status);
142 break;
146 desc->rerr = status;
147 desc->reslength = urb->actual_length;
148 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
149 desc->length += desc->reslength;
150 wake_up(&desc->wait);
152 set_bit(WDM_READ, &desc->flags);
153 spin_unlock(&desc->iuspin);
156 static void wdm_int_callback(struct urb *urb)
158 int rv = 0;
159 int status = urb->status;
160 struct wdm_device *desc;
161 struct usb_ctrlrequest *req;
162 struct usb_cdc_notification *dr;
164 desc = urb->context;
165 req = desc->irq;
166 dr = (struct usb_cdc_notification *)desc->sbuf;
168 if (status) {
169 switch (status) {
170 case -ESHUTDOWN:
171 case -ENOENT:
172 case -ECONNRESET:
173 return; /* unplug */
174 case -EPIPE:
175 set_bit(WDM_INT_STALL, &desc->flags);
176 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
177 goto sw; /* halt is cleared in work */
178 default:
179 dev_err(&desc->intf->dev,
180 "nonzero urb status received: %d\n", status);
181 break;
185 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
186 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
187 urb->actual_length);
188 goto exit;
191 switch (dr->bNotificationType) {
192 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
193 dev_dbg(&desc->intf->dev,
194 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
195 dr->wIndex, dr->wLength);
196 break;
198 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
200 dev_dbg(&desc->intf->dev,
201 "NOTIFY_NETWORK_CONNECTION %s network",
202 dr->wValue ? "connected to" : "disconnected from");
203 goto exit;
204 default:
205 clear_bit(WDM_POLL_RUNNING, &desc->flags);
206 dev_err(&desc->intf->dev,
207 "unknown notification %d received: index %d len %d\n",
208 dr->bNotificationType, dr->wIndex, dr->wLength);
209 goto exit;
212 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
213 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
214 req->wValue = 0;
215 req->wIndex = desc->inum;
216 req->wLength = cpu_to_le16(desc->wMaxCommand);
218 usb_fill_control_urb(
219 desc->response,
220 interface_to_usbdev(desc->intf),
221 /* using common endpoint 0 */
222 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
223 (unsigned char *)req,
224 desc->inbuf,
225 desc->wMaxCommand,
226 wdm_in_callback,
227 desc
229 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
230 spin_lock(&desc->iuspin);
231 clear_bit(WDM_READ, &desc->flags);
232 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
233 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
234 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
235 __func__, rv);
237 spin_unlock(&desc->iuspin);
238 if (rv < 0) {
239 if (rv == -EPERM)
240 return;
241 if (rv == -ENOMEM) {
243 rv = schedule_work(&desc->rxwork);
244 if (rv)
245 dev_err(&desc->intf->dev,
246 "Cannot schedule work\n");
249 exit:
250 rv = usb_submit_urb(urb, GFP_ATOMIC);
251 if (rv)
252 dev_err(&desc->intf->dev,
253 "%s - usb_submit_urb failed with result %d\n",
254 __func__, rv);
258 static void kill_urbs(struct wdm_device *desc)
260 /* the order here is essential */
261 usb_kill_urb(desc->command);
262 usb_kill_urb(desc->validity);
263 usb_kill_urb(desc->response);
266 static void free_urbs(struct wdm_device *desc)
268 usb_free_urb(desc->validity);
269 usb_free_urb(desc->response);
270 usb_free_urb(desc->command);
273 static void cleanup(struct wdm_device *desc)
275 usb_buffer_free(interface_to_usbdev(desc->intf),
276 desc->wMaxPacketSize,
277 desc->sbuf,
278 desc->validity->transfer_dma);
279 usb_buffer_free(interface_to_usbdev(desc->intf),
280 desc->wMaxCommand,
281 desc->inbuf,
282 desc->response->transfer_dma);
283 kfree(desc->orq);
284 kfree(desc->irq);
285 kfree(desc->ubuf);
286 free_urbs(desc);
287 kfree(desc);
290 static ssize_t wdm_write
291 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
293 u8 *buf;
294 int rv = -EMSGSIZE, r, we;
295 struct wdm_device *desc = file->private_data;
296 struct usb_ctrlrequest *req;
298 if (count > desc->wMaxCommand)
299 count = desc->wMaxCommand;
301 spin_lock_irq(&desc->iuspin);
302 we = desc->werr;
303 desc->werr = 0;
304 spin_unlock_irq(&desc->iuspin);
305 if (we < 0)
306 return -EIO;
308 r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */
309 rv = -ERESTARTSYS;
310 if (r)
311 goto outnl;
313 r = usb_autopm_get_interface(desc->intf);
314 if (r < 0)
315 goto outnp;
316 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
317 &desc->flags));
318 if (r < 0)
319 goto out;
321 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
322 rv = -ENODEV;
323 goto out;
326 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
327 if (!buf) {
328 rv = -ENOMEM;
329 goto out;
332 r = copy_from_user(buf, buffer, count);
333 if (r > 0) {
334 kfree(buf);
335 rv = -EFAULT;
336 goto out;
339 req = desc->orq;
340 usb_fill_control_urb(
341 desc->command,
342 interface_to_usbdev(desc->intf),
343 /* using common endpoint 0 */
344 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
345 (unsigned char *)req,
346 buf,
347 count,
348 wdm_out_callback,
349 desc
352 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
353 USB_RECIP_INTERFACE);
354 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
355 req->wValue = 0;
356 req->wIndex = desc->inum;
357 req->wLength = cpu_to_le16(count);
358 set_bit(WDM_IN_USE, &desc->flags);
360 rv = usb_submit_urb(desc->command, GFP_KERNEL);
361 if (rv < 0) {
362 kfree(buf);
363 clear_bit(WDM_IN_USE, &desc->flags);
364 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
365 } else {
366 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
367 req->wIndex);
369 out:
370 usb_autopm_put_interface(desc->intf);
371 outnp:
372 mutex_unlock(&desc->wlock);
373 outnl:
374 return rv < 0 ? rv : count;
377 static ssize_t wdm_read
378 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
380 int rv, cntr;
381 int i = 0;
382 struct wdm_device *desc = file->private_data;
385 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
386 if (rv < 0)
387 return -ERESTARTSYS;
389 if (desc->length == 0) {
390 desc->read = 0;
391 retry:
392 i++;
393 rv = wait_event_interruptible(desc->wait,
394 test_bit(WDM_READ, &desc->flags));
396 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
397 rv = -ENODEV;
398 goto err;
400 usb_mark_last_busy(interface_to_usbdev(desc->intf));
401 if (rv < 0) {
402 rv = -ERESTARTSYS;
403 goto err;
406 spin_lock_irq(&desc->iuspin);
408 if (desc->rerr) { /* read completed, error happened */
409 int t = desc->rerr;
410 desc->rerr = 0;
411 spin_unlock_irq(&desc->iuspin);
412 dev_err(&desc->intf->dev,
413 "reading had resulted in %d\n", t);
414 rv = -EIO;
415 goto err;
418 * recheck whether we've lost the race
419 * against the completion handler
421 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
422 spin_unlock_irq(&desc->iuspin);
423 goto retry;
425 if (!desc->reslength) { /* zero length read */
426 spin_unlock_irq(&desc->iuspin);
427 goto retry;
429 clear_bit(WDM_READ, &desc->flags);
430 spin_unlock_irq(&desc->iuspin);
433 cntr = count > desc->length ? desc->length : count;
434 rv = copy_to_user(buffer, desc->ubuf, cntr);
435 if (rv > 0) {
436 rv = -EFAULT;
437 goto err;
440 for (i = 0; i < desc->length - cntr; i++)
441 desc->ubuf[i] = desc->ubuf[i + cntr];
443 desc->length -= cntr;
444 /* in case we had outstanding data */
445 if (!desc->length)
446 clear_bit(WDM_READ, &desc->flags);
447 rv = cntr;
449 err:
450 mutex_unlock(&desc->rlock);
451 if (rv < 0)
452 dev_err(&desc->intf->dev, "wdm_read: exit error\n");
453 return rv;
456 static int wdm_flush(struct file *file, fl_owner_t id)
458 struct wdm_device *desc = file->private_data;
460 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
461 if (desc->werr < 0)
462 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
463 desc->werr);
465 return desc->werr;
468 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
470 struct wdm_device *desc = file->private_data;
471 unsigned long flags;
472 unsigned int mask = 0;
474 spin_lock_irqsave(&desc->iuspin, flags);
475 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
476 mask = POLLERR;
477 spin_unlock_irqrestore(&desc->iuspin, flags);
478 goto desc_out;
480 if (test_bit(WDM_READ, &desc->flags))
481 mask = POLLIN | POLLRDNORM;
482 if (desc->rerr || desc->werr)
483 mask |= POLLERR;
484 if (!test_bit(WDM_IN_USE, &desc->flags))
485 mask |= POLLOUT | POLLWRNORM;
486 spin_unlock_irqrestore(&desc->iuspin, flags);
488 poll_wait(file, &desc->wait, wait);
490 desc_out:
491 return mask;
494 static int wdm_open(struct inode *inode, struct file *file)
496 int minor = iminor(inode);
497 int rv = -ENODEV;
498 struct usb_interface *intf;
499 struct wdm_device *desc;
501 mutex_lock(&wdm_mutex);
502 intf = usb_find_interface(&wdm_driver, minor);
503 if (!intf)
504 goto out;
506 desc = usb_get_intfdata(intf);
507 if (test_bit(WDM_DISCONNECTING, &desc->flags))
508 goto out;
511 file->private_data = desc;
513 rv = usb_autopm_get_interface(desc->intf);
514 if (rv < 0) {
515 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
516 goto out;
518 intf->needs_remote_wakeup = 1;
520 mutex_lock(&desc->plock);
521 if (!desc->count++) {
522 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
523 if (rv < 0) {
524 desc->count--;
525 dev_err(&desc->intf->dev,
526 "Error submitting int urb - %d\n", rv);
528 } else {
529 rv = 0;
531 mutex_unlock(&desc->plock);
532 usb_autopm_put_interface(desc->intf);
533 out:
534 mutex_unlock(&wdm_mutex);
535 return rv;
538 static int wdm_release(struct inode *inode, struct file *file)
540 struct wdm_device *desc = file->private_data;
542 mutex_lock(&wdm_mutex);
543 mutex_lock(&desc->plock);
544 desc->count--;
545 mutex_unlock(&desc->plock);
547 if (!desc->count) {
548 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
549 kill_urbs(desc);
550 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
551 desc->intf->needs_remote_wakeup = 0;
553 mutex_unlock(&wdm_mutex);
554 return 0;
557 static const struct file_operations wdm_fops = {
558 .owner = THIS_MODULE,
559 .read = wdm_read,
560 .write = wdm_write,
561 .open = wdm_open,
562 .flush = wdm_flush,
563 .release = wdm_release,
564 .poll = wdm_poll
567 static struct usb_class_driver wdm_class = {
568 .name = "cdc-wdm%d",
569 .fops = &wdm_fops,
570 .minor_base = WDM_MINOR_BASE,
573 /* --- error handling --- */
574 static void wdm_rxwork(struct work_struct *work)
576 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
577 unsigned long flags;
578 int rv;
580 spin_lock_irqsave(&desc->iuspin, flags);
581 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
582 spin_unlock_irqrestore(&desc->iuspin, flags);
583 } else {
584 spin_unlock_irqrestore(&desc->iuspin, flags);
585 rv = usb_submit_urb(desc->response, GFP_KERNEL);
586 if (rv < 0 && rv != -EPERM) {
587 spin_lock_irqsave(&desc->iuspin, flags);
588 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
589 schedule_work(&desc->rxwork);
590 spin_unlock_irqrestore(&desc->iuspin, flags);
595 /* --- hotplug --- */
597 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
599 int rv = -EINVAL;
600 struct usb_device *udev = interface_to_usbdev(intf);
601 struct wdm_device *desc;
602 struct usb_host_interface *iface;
603 struct usb_endpoint_descriptor *ep;
604 struct usb_cdc_dmm_desc *dmhd;
605 u8 *buffer = intf->altsetting->extra;
606 int buflen = intf->altsetting->extralen;
607 u16 maxcom = 0;
609 if (!buffer)
610 goto out;
612 while (buflen > 2) {
613 if (buffer [1] != USB_DT_CS_INTERFACE) {
614 dev_err(&intf->dev, "skipping garbage\n");
615 goto next_desc;
618 switch (buffer [2]) {
619 case USB_CDC_HEADER_TYPE:
620 break;
621 case USB_CDC_DMM_TYPE:
622 dmhd = (struct usb_cdc_dmm_desc *)buffer;
623 maxcom = le16_to_cpu(dmhd->wMaxCommand);
624 dev_dbg(&intf->dev,
625 "Finding maximum buffer length: %d", maxcom);
626 break;
627 default:
628 dev_err(&intf->dev,
629 "Ignoring extra header, type %d, length %d\n",
630 buffer[2], buffer[0]);
631 break;
633 next_desc:
634 buflen -= buffer[0];
635 buffer += buffer[0];
638 rv = -ENOMEM;
639 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
640 if (!desc)
641 goto out;
642 mutex_init(&desc->wlock);
643 mutex_init(&desc->rlock);
644 mutex_init(&desc->plock);
645 spin_lock_init(&desc->iuspin);
646 init_waitqueue_head(&desc->wait);
647 desc->wMaxCommand = maxcom;
648 /* this will be expanded and needed in hardware endianness */
649 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
650 desc->intf = intf;
651 INIT_WORK(&desc->rxwork, wdm_rxwork);
653 rv = -EINVAL;
654 iface = intf->cur_altsetting;
655 if (iface->desc.bNumEndpoints != 1)
656 goto err;
657 ep = &iface->endpoint[0].desc;
658 if (!ep || !usb_endpoint_is_int_in(ep))
659 goto err;
661 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
662 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
664 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
665 if (!desc->orq)
666 goto err;
667 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
668 if (!desc->irq)
669 goto err;
671 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
672 if (!desc->validity)
673 goto err;
675 desc->response = usb_alloc_urb(0, GFP_KERNEL);
676 if (!desc->response)
677 goto err;
679 desc->command = usb_alloc_urb(0, GFP_KERNEL);
680 if (!desc->command)
681 goto err;
683 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
684 if (!desc->ubuf)
685 goto err;
687 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
688 desc->wMaxPacketSize,
689 GFP_KERNEL,
690 &desc->validity->transfer_dma);
691 if (!desc->sbuf)
692 goto err;
694 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
695 desc->bMaxPacketSize0,
696 GFP_KERNEL,
697 &desc->response->transfer_dma);
698 if (!desc->inbuf)
699 goto err2;
701 usb_fill_int_urb(
702 desc->validity,
703 interface_to_usbdev(intf),
704 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
705 desc->sbuf,
706 desc->wMaxPacketSize,
707 wdm_int_callback,
708 desc,
709 ep->bInterval
711 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
713 usb_set_intfdata(intf, desc);
714 rv = usb_register_dev(intf, &wdm_class);
715 if (rv < 0)
716 goto err3;
717 else
718 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
719 intf->minor - WDM_MINOR_BASE);
720 out:
721 return rv;
722 err3:
723 usb_set_intfdata(intf, NULL);
724 usb_buffer_free(interface_to_usbdev(desc->intf),
725 desc->bMaxPacketSize0,
726 desc->inbuf,
727 desc->response->transfer_dma);
728 err2:
729 usb_buffer_free(interface_to_usbdev(desc->intf),
730 desc->wMaxPacketSize,
731 desc->sbuf,
732 desc->validity->transfer_dma);
733 err:
734 free_urbs(desc);
735 kfree(desc->ubuf);
736 kfree(desc->orq);
737 kfree(desc->irq);
738 kfree(desc);
739 return rv;
742 static void wdm_disconnect(struct usb_interface *intf)
744 struct wdm_device *desc;
745 unsigned long flags;
747 usb_deregister_dev(intf, &wdm_class);
748 mutex_lock(&wdm_mutex);
749 desc = usb_get_intfdata(intf);
751 /* the spinlock makes sure no new urbs are generated in the callbacks */
752 spin_lock_irqsave(&desc->iuspin, flags);
753 set_bit(WDM_DISCONNECTING, &desc->flags);
754 set_bit(WDM_READ, &desc->flags);
755 /* to terminate pending flushes */
756 clear_bit(WDM_IN_USE, &desc->flags);
757 spin_unlock_irqrestore(&desc->iuspin, flags);
758 cancel_work_sync(&desc->rxwork);
759 kill_urbs(desc);
760 wake_up_all(&desc->wait);
761 if (!desc->count)
762 cleanup(desc);
763 mutex_unlock(&wdm_mutex);
766 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
768 struct wdm_device *desc = usb_get_intfdata(intf);
769 int rv = 0;
771 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
773 mutex_lock(&desc->plock);
774 #ifdef CONFIG_PM
775 if ((message.event & PM_EVENT_AUTO) &&
776 test_bit(WDM_IN_USE, &desc->flags)) {
777 rv = -EBUSY;
778 } else {
779 #endif
780 cancel_work_sync(&desc->rxwork);
781 kill_urbs(desc);
782 #ifdef CONFIG_PM
784 #endif
785 mutex_unlock(&desc->plock);
787 return rv;
790 static int recover_from_urb_loss(struct wdm_device *desc)
792 int rv = 0;
794 if (desc->count) {
795 rv = usb_submit_urb(desc->validity, GFP_NOIO);
796 if (rv < 0)
797 dev_err(&desc->intf->dev,
798 "Error resume submitting int urb - %d\n", rv);
800 return rv;
802 static int wdm_resume(struct usb_interface *intf)
804 struct wdm_device *desc = usb_get_intfdata(intf);
805 int rv;
807 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
808 mutex_lock(&desc->plock);
809 rv = recover_from_urb_loss(desc);
810 mutex_unlock(&desc->plock);
811 return rv;
814 static int wdm_pre_reset(struct usb_interface *intf)
816 struct wdm_device *desc = usb_get_intfdata(intf);
818 mutex_lock(&desc->plock);
819 return 0;
822 static int wdm_post_reset(struct usb_interface *intf)
824 struct wdm_device *desc = usb_get_intfdata(intf);
825 int rv;
827 rv = recover_from_urb_loss(desc);
828 mutex_unlock(&desc->plock);
829 return 0;
832 static struct usb_driver wdm_driver = {
833 .name = "cdc_wdm",
834 .probe = wdm_probe,
835 .disconnect = wdm_disconnect,
836 .suspend = wdm_suspend,
837 .resume = wdm_resume,
838 .reset_resume = wdm_resume,
839 .pre_reset = wdm_pre_reset,
840 .post_reset = wdm_post_reset,
841 .id_table = wdm_ids,
842 .supports_autosuspend = 1,
845 /* --- low level module stuff --- */
847 static int __init wdm_init(void)
849 int rv;
851 rv = usb_register(&wdm_driver);
853 return rv;
856 static void __exit wdm_exit(void)
858 usb_deregister(&wdm_driver);
861 module_init(wdm_init);
862 module_exit(wdm_exit);
864 MODULE_AUTHOR(DRIVER_AUTHOR);
865 MODULE_DESCRIPTION(DRIVER_DESC);
866 MODULE_LICENSE("GPL");