Save sram context after changing MPU, DSP or core clocks
[linux-ginger.git] / drivers / usb / class / cdc-wdm.c
blob3e564bfe17d16b8673f26bdd32fc4da64e201fdc
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;
317 if (!file->f_flags && O_NONBLOCK)
318 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
319 &desc->flags));
320 else
321 if (test_bit(WDM_IN_USE, &desc->flags))
322 r = -EAGAIN;
323 if (r < 0)
324 goto out;
326 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
327 rv = -ENODEV;
328 goto out;
331 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
332 if (!buf) {
333 rv = -ENOMEM;
334 goto out;
337 r = copy_from_user(buf, buffer, count);
338 if (r > 0) {
339 kfree(buf);
340 rv = -EFAULT;
341 goto out;
344 req = desc->orq;
345 usb_fill_control_urb(
346 desc->command,
347 interface_to_usbdev(desc->intf),
348 /* using common endpoint 0 */
349 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
350 (unsigned char *)req,
351 buf,
352 count,
353 wdm_out_callback,
354 desc
357 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
358 USB_RECIP_INTERFACE);
359 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
360 req->wValue = 0;
361 req->wIndex = desc->inum;
362 req->wLength = cpu_to_le16(count);
363 set_bit(WDM_IN_USE, &desc->flags);
365 rv = usb_submit_urb(desc->command, GFP_KERNEL);
366 if (rv < 0) {
367 kfree(buf);
368 clear_bit(WDM_IN_USE, &desc->flags);
369 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
370 } else {
371 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
372 req->wIndex);
374 out:
375 usb_autopm_put_interface(desc->intf);
376 outnp:
377 mutex_unlock(&desc->wlock);
378 outnl:
379 return rv < 0 ? rv : count;
382 static ssize_t wdm_read
383 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
385 int rv, cntr = 0;
386 int i = 0;
387 struct wdm_device *desc = file->private_data;
390 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
391 if (rv < 0)
392 return -ERESTARTSYS;
394 if (desc->length == 0) {
395 desc->read = 0;
396 retry:
397 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
398 rv = -ENODEV;
399 goto err;
401 i++;
402 if (file->f_flags & O_NONBLOCK) {
403 if (!test_bit(WDM_READ, &desc->flags)) {
404 rv = cntr ? cntr : -EAGAIN;
405 goto err;
407 rv = 0;
408 } else {
409 rv = wait_event_interruptible(desc->wait,
410 test_bit(WDM_READ, &desc->flags));
413 /* may have happened while we slept */
414 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
415 rv = -ENODEV;
416 goto err;
418 usb_mark_last_busy(interface_to_usbdev(desc->intf));
419 if (rv < 0) {
420 rv = -ERESTARTSYS;
421 goto err;
424 spin_lock_irq(&desc->iuspin);
426 if (desc->rerr) { /* read completed, error happened */
427 int t = desc->rerr;
428 desc->rerr = 0;
429 spin_unlock_irq(&desc->iuspin);
430 dev_err(&desc->intf->dev,
431 "reading had resulted in %d\n", t);
432 rv = -EIO;
433 goto err;
436 * recheck whether we've lost the race
437 * against the completion handler
439 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
440 spin_unlock_irq(&desc->iuspin);
441 goto retry;
443 if (!desc->reslength) { /* zero length read */
444 spin_unlock_irq(&desc->iuspin);
445 goto retry;
447 clear_bit(WDM_READ, &desc->flags);
448 spin_unlock_irq(&desc->iuspin);
451 cntr = count > desc->length ? desc->length : count;
452 rv = copy_to_user(buffer, desc->ubuf, cntr);
453 if (rv > 0) {
454 rv = -EFAULT;
455 goto err;
458 for (i = 0; i < desc->length - cntr; i++)
459 desc->ubuf[i] = desc->ubuf[i + cntr];
461 desc->length -= cntr;
462 /* in case we had outstanding data */
463 if (!desc->length)
464 clear_bit(WDM_READ, &desc->flags);
465 rv = cntr;
467 err:
468 mutex_unlock(&desc->rlock);
469 if (rv < 0 && rv != -EAGAIN)
470 dev_err(&desc->intf->dev, "wdm_read: exit error\n");
471 return rv;
474 static int wdm_flush(struct file *file, fl_owner_t id)
476 struct wdm_device *desc = file->private_data;
478 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
479 if (desc->werr < 0)
480 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
481 desc->werr);
483 return desc->werr;
486 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
488 struct wdm_device *desc = file->private_data;
489 unsigned long flags;
490 unsigned int mask = 0;
492 spin_lock_irqsave(&desc->iuspin, flags);
493 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
494 mask = POLLERR;
495 spin_unlock_irqrestore(&desc->iuspin, flags);
496 goto desc_out;
498 if (test_bit(WDM_READ, &desc->flags))
499 mask = POLLIN | POLLRDNORM;
500 if (desc->rerr || desc->werr)
501 mask |= POLLERR;
502 if (!test_bit(WDM_IN_USE, &desc->flags))
503 mask |= POLLOUT | POLLWRNORM;
504 spin_unlock_irqrestore(&desc->iuspin, flags);
506 poll_wait(file, &desc->wait, wait);
508 desc_out:
509 return mask;
512 static int wdm_open(struct inode *inode, struct file *file)
514 int minor = iminor(inode);
515 int rv = -ENODEV;
516 struct usb_interface *intf;
517 struct wdm_device *desc;
519 mutex_lock(&wdm_mutex);
520 intf = usb_find_interface(&wdm_driver, minor);
521 if (!intf)
522 goto out;
524 desc = usb_get_intfdata(intf);
525 if (test_bit(WDM_DISCONNECTING, &desc->flags))
526 goto out;
527 file->private_data = desc;
529 rv = usb_autopm_get_interface(desc->intf);
530 if (rv < 0) {
531 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
532 goto out;
534 intf->needs_remote_wakeup = 1;
536 mutex_lock(&desc->plock);
537 if (!desc->count++) {
538 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
539 if (rv < 0) {
540 desc->count--;
541 dev_err(&desc->intf->dev,
542 "Error submitting int urb - %d\n", rv);
544 } else {
545 rv = 0;
547 mutex_unlock(&desc->plock);
548 usb_autopm_put_interface(desc->intf);
549 out:
550 mutex_unlock(&wdm_mutex);
551 return rv;
554 static int wdm_release(struct inode *inode, struct file *file)
556 struct wdm_device *desc = file->private_data;
558 mutex_lock(&wdm_mutex);
559 mutex_lock(&desc->plock);
560 desc->count--;
561 mutex_unlock(&desc->plock);
563 if (!desc->count) {
564 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
565 kill_urbs(desc);
566 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
567 desc->intf->needs_remote_wakeup = 0;
569 mutex_unlock(&wdm_mutex);
570 return 0;
573 static const struct file_operations wdm_fops = {
574 .owner = THIS_MODULE,
575 .read = wdm_read,
576 .write = wdm_write,
577 .open = wdm_open,
578 .flush = wdm_flush,
579 .release = wdm_release,
580 .poll = wdm_poll
583 static struct usb_class_driver wdm_class = {
584 .name = "cdc-wdm%d",
585 .fops = &wdm_fops,
586 .minor_base = WDM_MINOR_BASE,
589 /* --- error handling --- */
590 static void wdm_rxwork(struct work_struct *work)
592 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
593 unsigned long flags;
594 int rv;
596 spin_lock_irqsave(&desc->iuspin, flags);
597 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
598 spin_unlock_irqrestore(&desc->iuspin, flags);
599 } else {
600 spin_unlock_irqrestore(&desc->iuspin, flags);
601 rv = usb_submit_urb(desc->response, GFP_KERNEL);
602 if (rv < 0 && rv != -EPERM) {
603 spin_lock_irqsave(&desc->iuspin, flags);
604 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
605 schedule_work(&desc->rxwork);
606 spin_unlock_irqrestore(&desc->iuspin, flags);
611 /* --- hotplug --- */
613 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
615 int rv = -EINVAL;
616 struct usb_device *udev = interface_to_usbdev(intf);
617 struct wdm_device *desc;
618 struct usb_host_interface *iface;
619 struct usb_endpoint_descriptor *ep;
620 struct usb_cdc_dmm_desc *dmhd;
621 u8 *buffer = intf->altsetting->extra;
622 int buflen = intf->altsetting->extralen;
623 u16 maxcom = 0;
625 if (!buffer)
626 goto out;
628 while (buflen > 2) {
629 if (buffer [1] != USB_DT_CS_INTERFACE) {
630 dev_err(&intf->dev, "skipping garbage\n");
631 goto next_desc;
634 switch (buffer [2]) {
635 case USB_CDC_HEADER_TYPE:
636 break;
637 case USB_CDC_DMM_TYPE:
638 dmhd = (struct usb_cdc_dmm_desc *)buffer;
639 maxcom = le16_to_cpu(dmhd->wMaxCommand);
640 dev_dbg(&intf->dev,
641 "Finding maximum buffer length: %d", maxcom);
642 break;
643 default:
644 dev_err(&intf->dev,
645 "Ignoring extra header, type %d, length %d\n",
646 buffer[2], buffer[0]);
647 break;
649 next_desc:
650 buflen -= buffer[0];
651 buffer += buffer[0];
654 rv = -ENOMEM;
655 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
656 if (!desc)
657 goto out;
658 mutex_init(&desc->wlock);
659 mutex_init(&desc->rlock);
660 mutex_init(&desc->plock);
661 spin_lock_init(&desc->iuspin);
662 init_waitqueue_head(&desc->wait);
663 desc->wMaxCommand = maxcom;
664 /* this will be expanded and needed in hardware endianness */
665 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
666 desc->intf = intf;
667 INIT_WORK(&desc->rxwork, wdm_rxwork);
669 rv = -EINVAL;
670 iface = intf->cur_altsetting;
671 if (iface->desc.bNumEndpoints != 1)
672 goto err;
673 ep = &iface->endpoint[0].desc;
674 if (!ep || !usb_endpoint_is_int_in(ep))
675 goto err;
677 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
678 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
680 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
681 if (!desc->orq)
682 goto err;
683 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
684 if (!desc->irq)
685 goto err;
687 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
688 if (!desc->validity)
689 goto err;
691 desc->response = usb_alloc_urb(0, GFP_KERNEL);
692 if (!desc->response)
693 goto err;
695 desc->command = usb_alloc_urb(0, GFP_KERNEL);
696 if (!desc->command)
697 goto err;
699 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
700 if (!desc->ubuf)
701 goto err;
703 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
704 desc->wMaxPacketSize,
705 GFP_KERNEL,
706 &desc->validity->transfer_dma);
707 if (!desc->sbuf)
708 goto err;
710 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
711 desc->bMaxPacketSize0,
712 GFP_KERNEL,
713 &desc->response->transfer_dma);
714 if (!desc->inbuf)
715 goto err2;
717 usb_fill_int_urb(
718 desc->validity,
719 interface_to_usbdev(intf),
720 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
721 desc->sbuf,
722 desc->wMaxPacketSize,
723 wdm_int_callback,
724 desc,
725 ep->bInterval
727 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
729 usb_set_intfdata(intf, desc);
730 rv = usb_register_dev(intf, &wdm_class);
731 if (rv < 0)
732 goto err3;
733 else
734 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
735 intf->minor - WDM_MINOR_BASE);
736 out:
737 return rv;
738 err3:
739 usb_set_intfdata(intf, NULL);
740 usb_buffer_free(interface_to_usbdev(desc->intf),
741 desc->bMaxPacketSize0,
742 desc->inbuf,
743 desc->response->transfer_dma);
744 err2:
745 usb_buffer_free(interface_to_usbdev(desc->intf),
746 desc->wMaxPacketSize,
747 desc->sbuf,
748 desc->validity->transfer_dma);
749 err:
750 free_urbs(desc);
751 kfree(desc->ubuf);
752 kfree(desc->orq);
753 kfree(desc->irq);
754 kfree(desc);
755 return rv;
758 static void wdm_disconnect(struct usb_interface *intf)
760 struct wdm_device *desc;
761 unsigned long flags;
763 usb_deregister_dev(intf, &wdm_class);
764 mutex_lock(&wdm_mutex);
765 desc = usb_get_intfdata(intf);
767 /* the spinlock makes sure no new urbs are generated in the callbacks */
768 spin_lock_irqsave(&desc->iuspin, flags);
769 set_bit(WDM_DISCONNECTING, &desc->flags);
770 set_bit(WDM_READ, &desc->flags);
771 /* to terminate pending flushes */
772 clear_bit(WDM_IN_USE, &desc->flags);
773 spin_unlock_irqrestore(&desc->iuspin, flags);
774 cancel_work_sync(&desc->rxwork);
775 kill_urbs(desc);
776 wake_up_all(&desc->wait);
777 if (!desc->count)
778 cleanup(desc);
779 mutex_unlock(&wdm_mutex);
782 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
784 struct wdm_device *desc = usb_get_intfdata(intf);
785 int rv = 0;
787 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
789 mutex_lock(&desc->plock);
790 #ifdef CONFIG_PM
791 if ((message.event & PM_EVENT_AUTO) &&
792 test_bit(WDM_IN_USE, &desc->flags)) {
793 rv = -EBUSY;
794 } else {
795 #endif
796 cancel_work_sync(&desc->rxwork);
797 kill_urbs(desc);
798 #ifdef CONFIG_PM
800 #endif
801 mutex_unlock(&desc->plock);
803 return rv;
806 static int recover_from_urb_loss(struct wdm_device *desc)
808 int rv = 0;
810 if (desc->count) {
811 rv = usb_submit_urb(desc->validity, GFP_NOIO);
812 if (rv < 0)
813 dev_err(&desc->intf->dev,
814 "Error resume submitting int urb - %d\n", rv);
816 return rv;
818 static int wdm_resume(struct usb_interface *intf)
820 struct wdm_device *desc = usb_get_intfdata(intf);
821 int rv;
823 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
824 mutex_lock(&desc->plock);
825 rv = recover_from_urb_loss(desc);
826 mutex_unlock(&desc->plock);
827 return rv;
830 static int wdm_pre_reset(struct usb_interface *intf)
832 struct wdm_device *desc = usb_get_intfdata(intf);
834 mutex_lock(&desc->plock);
835 return 0;
838 static int wdm_post_reset(struct usb_interface *intf)
840 struct wdm_device *desc = usb_get_intfdata(intf);
841 int rv;
843 rv = recover_from_urb_loss(desc);
844 mutex_unlock(&desc->plock);
845 return 0;
848 static struct usb_driver wdm_driver = {
849 .name = "cdc_wdm",
850 .probe = wdm_probe,
851 .disconnect = wdm_disconnect,
852 .suspend = wdm_suspend,
853 .resume = wdm_resume,
854 .reset_resume = wdm_resume,
855 .pre_reset = wdm_pre_reset,
856 .post_reset = wdm_post_reset,
857 .id_table = wdm_ids,
858 .supports_autosuspend = 1,
861 /* --- low level module stuff --- */
863 static int __init wdm_init(void)
865 int rv;
867 rv = usb_register(&wdm_driver);
869 return rv;
872 static void __exit wdm_exit(void)
874 usb_deregister(&wdm_driver);
877 module_init(wdm_init);
878 module_exit(wdm_exit);
880 MODULE_AUTHOR(DRIVER_AUTHOR);
881 MODULE_DESCRIPTION(DRIVER_DESC);
882 MODULE_LICENSE("GPL");