Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / usb / class / cdc-wdm.c
blob7b45f6612a50f8a28b1aa44ef6c7bf6b62b9582b
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 const 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
55 #define WDM_RESPONDING 7
56 #define WDM_SUSPENDING 8
58 #define WDM_MAX 16
60 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
61 #define WDM_DEFAULT_BUFSIZE 256
63 static DEFINE_MUTEX(wdm_mutex);
65 /* --- method tables --- */
67 struct wdm_device {
68 u8 *inbuf; /* buffer for response */
69 u8 *outbuf; /* buffer for command */
70 u8 *sbuf; /* buffer for status */
71 u8 *ubuf; /* buffer for copy to user space */
73 struct urb *command;
74 struct urb *response;
75 struct urb *validity;
76 struct usb_interface *intf;
77 struct usb_ctrlrequest *orq;
78 struct usb_ctrlrequest *irq;
79 spinlock_t iuspin;
81 unsigned long flags;
82 u16 bufsize;
83 u16 wMaxCommand;
84 u16 wMaxPacketSize;
85 u16 bMaxPacketSize0;
86 __le16 inum;
87 int reslength;
88 int length;
89 int read;
90 int count;
91 dma_addr_t shandle;
92 dma_addr_t ihandle;
93 struct mutex wlock;
94 struct mutex rlock;
95 wait_queue_head_t wait;
96 struct work_struct rxwork;
97 int werr;
98 int rerr;
101 static struct usb_driver wdm_driver;
103 /* --- callbacks --- */
104 static void wdm_out_callback(struct urb *urb)
106 struct wdm_device *desc;
107 desc = urb->context;
108 spin_lock(&desc->iuspin);
109 desc->werr = urb->status;
110 spin_unlock(&desc->iuspin);
111 kfree(desc->outbuf);
112 desc->outbuf = NULL;
113 clear_bit(WDM_IN_USE, &desc->flags);
114 wake_up(&desc->wait);
117 static void wdm_in_callback(struct urb *urb)
119 struct wdm_device *desc = urb->context;
120 int status = urb->status;
122 spin_lock(&desc->iuspin);
123 clear_bit(WDM_RESPONDING, &desc->flags);
125 if (status) {
126 switch (status) {
127 case -ENOENT:
128 dev_dbg(&desc->intf->dev,
129 "nonzero urb status received: -ENOENT");
130 goto skip_error;
131 case -ECONNRESET:
132 dev_dbg(&desc->intf->dev,
133 "nonzero urb status received: -ECONNRESET");
134 goto skip_error;
135 case -ESHUTDOWN:
136 dev_dbg(&desc->intf->dev,
137 "nonzero urb status received: -ESHUTDOWN");
138 goto skip_error;
139 case -EPIPE:
140 dev_err(&desc->intf->dev,
141 "nonzero urb status received: -EPIPE\n");
142 break;
143 default:
144 dev_err(&desc->intf->dev,
145 "Unexpected error %d\n", status);
146 break;
150 desc->rerr = status;
151 desc->reslength = urb->actual_length;
152 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
153 desc->length += desc->reslength;
154 skip_error:
155 wake_up(&desc->wait);
157 set_bit(WDM_READ, &desc->flags);
158 spin_unlock(&desc->iuspin);
161 static void wdm_int_callback(struct urb *urb)
163 int rv = 0;
164 int status = urb->status;
165 struct wdm_device *desc;
166 struct usb_ctrlrequest *req;
167 struct usb_cdc_notification *dr;
169 desc = urb->context;
170 req = desc->irq;
171 dr = (struct usb_cdc_notification *)desc->sbuf;
173 if (status) {
174 switch (status) {
175 case -ESHUTDOWN:
176 case -ENOENT:
177 case -ECONNRESET:
178 return; /* unplug */
179 case -EPIPE:
180 set_bit(WDM_INT_STALL, &desc->flags);
181 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
182 goto sw; /* halt is cleared in work */
183 default:
184 dev_err(&desc->intf->dev,
185 "nonzero urb status received: %d\n", status);
186 break;
190 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
191 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
192 urb->actual_length);
193 goto exit;
196 switch (dr->bNotificationType) {
197 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
198 dev_dbg(&desc->intf->dev,
199 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
200 dr->wIndex, dr->wLength);
201 break;
203 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
205 dev_dbg(&desc->intf->dev,
206 "NOTIFY_NETWORK_CONNECTION %s network",
207 dr->wValue ? "connected to" : "disconnected from");
208 goto exit;
209 default:
210 clear_bit(WDM_POLL_RUNNING, &desc->flags);
211 dev_err(&desc->intf->dev,
212 "unknown notification %d received: index %d len %d\n",
213 dr->bNotificationType, dr->wIndex, dr->wLength);
214 goto exit;
217 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
218 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
219 req->wValue = 0;
220 req->wIndex = desc->inum;
221 req->wLength = cpu_to_le16(desc->wMaxCommand);
223 usb_fill_control_urb(
224 desc->response,
225 interface_to_usbdev(desc->intf),
226 /* using common endpoint 0 */
227 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
228 (unsigned char *)req,
229 desc->inbuf,
230 desc->wMaxCommand,
231 wdm_in_callback,
232 desc
234 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
235 spin_lock(&desc->iuspin);
236 clear_bit(WDM_READ, &desc->flags);
237 set_bit(WDM_RESPONDING, &desc->flags);
238 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
239 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
240 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
241 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
242 __func__, rv);
244 spin_unlock(&desc->iuspin);
245 if (rv < 0) {
246 clear_bit(WDM_RESPONDING, &desc->flags);
247 if (rv == -EPERM)
248 return;
249 if (rv == -ENOMEM) {
251 rv = schedule_work(&desc->rxwork);
252 if (rv)
253 dev_err(&desc->intf->dev,
254 "Cannot schedule work\n");
257 exit:
258 rv = usb_submit_urb(urb, GFP_ATOMIC);
259 if (rv)
260 dev_err(&desc->intf->dev,
261 "%s - usb_submit_urb failed with result %d\n",
262 __func__, rv);
266 static void kill_urbs(struct wdm_device *desc)
268 /* the order here is essential */
269 usb_kill_urb(desc->command);
270 usb_kill_urb(desc->validity);
271 usb_kill_urb(desc->response);
274 static void free_urbs(struct wdm_device *desc)
276 usb_free_urb(desc->validity);
277 usb_free_urb(desc->response);
278 usb_free_urb(desc->command);
281 static void cleanup(struct wdm_device *desc)
283 usb_free_coherent(interface_to_usbdev(desc->intf),
284 desc->wMaxPacketSize,
285 desc->sbuf,
286 desc->validity->transfer_dma);
287 usb_free_coherent(interface_to_usbdev(desc->intf),
288 desc->bMaxPacketSize0,
289 desc->inbuf,
290 desc->response->transfer_dma);
291 kfree(desc->orq);
292 kfree(desc->irq);
293 kfree(desc->ubuf);
294 free_urbs(desc);
295 kfree(desc);
298 static ssize_t wdm_write
299 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
301 u8 *buf;
302 int rv = -EMSGSIZE, r, we;
303 struct wdm_device *desc = file->private_data;
304 struct usb_ctrlrequest *req;
306 if (count > desc->wMaxCommand)
307 count = desc->wMaxCommand;
309 spin_lock_irq(&desc->iuspin);
310 we = desc->werr;
311 desc->werr = 0;
312 spin_unlock_irq(&desc->iuspin);
313 if (we < 0)
314 return -EIO;
316 buf = kmalloc(count, GFP_KERNEL);
317 if (!buf) {
318 rv = -ENOMEM;
319 goto outnl;
322 r = copy_from_user(buf, buffer, count);
323 if (r > 0) {
324 kfree(buf);
325 rv = -EFAULT;
326 goto outnl;
329 /* concurrent writes and disconnect */
330 r = mutex_lock_interruptible(&desc->wlock);
331 rv = -ERESTARTSYS;
332 if (r) {
333 kfree(buf);
334 goto outnl;
337 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
338 kfree(buf);
339 rv = -ENODEV;
340 goto outnp;
343 r = usb_autopm_get_interface(desc->intf);
344 if (r < 0) {
345 kfree(buf);
346 goto outnp;
349 if (!(file->f_flags & O_NONBLOCK))
350 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
351 &desc->flags));
352 else
353 if (test_bit(WDM_IN_USE, &desc->flags))
354 r = -EAGAIN;
355 if (r < 0) {
356 kfree(buf);
357 goto out;
360 req = desc->orq;
361 usb_fill_control_urb(
362 desc->command,
363 interface_to_usbdev(desc->intf),
364 /* using common endpoint 0 */
365 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
366 (unsigned char *)req,
367 buf,
368 count,
369 wdm_out_callback,
370 desc
373 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
374 USB_RECIP_INTERFACE);
375 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
376 req->wValue = 0;
377 req->wIndex = desc->inum;
378 req->wLength = cpu_to_le16(count);
379 set_bit(WDM_IN_USE, &desc->flags);
380 desc->outbuf = buf;
382 rv = usb_submit_urb(desc->command, GFP_KERNEL);
383 if (rv < 0) {
384 kfree(buf);
385 desc->outbuf = NULL;
386 clear_bit(WDM_IN_USE, &desc->flags);
387 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
388 } else {
389 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
390 req->wIndex);
392 out:
393 usb_autopm_put_interface(desc->intf);
394 outnp:
395 mutex_unlock(&desc->wlock);
396 outnl:
397 return rv < 0 ? rv : count;
400 static ssize_t wdm_read
401 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
403 int rv, cntr;
404 int i = 0;
405 struct wdm_device *desc = file->private_data;
408 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
409 if (rv < 0)
410 return -ERESTARTSYS;
412 cntr = ACCESS_ONCE(desc->length);
413 if (cntr == 0) {
414 desc->read = 0;
415 retry:
416 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
417 rv = -ENODEV;
418 goto err;
420 i++;
421 if (file->f_flags & O_NONBLOCK) {
422 if (!test_bit(WDM_READ, &desc->flags)) {
423 rv = cntr ? cntr : -EAGAIN;
424 goto err;
426 rv = 0;
427 } else {
428 rv = wait_event_interruptible(desc->wait,
429 test_bit(WDM_READ, &desc->flags));
432 /* may have happened while we slept */
433 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
434 rv = -ENODEV;
435 goto err;
437 usb_mark_last_busy(interface_to_usbdev(desc->intf));
438 if (rv < 0) {
439 rv = -ERESTARTSYS;
440 goto err;
443 spin_lock_irq(&desc->iuspin);
445 if (desc->rerr) { /* read completed, error happened */
446 desc->rerr = 0;
447 spin_unlock_irq(&desc->iuspin);
448 rv = -EIO;
449 goto err;
452 * recheck whether we've lost the race
453 * against the completion handler
455 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
456 spin_unlock_irq(&desc->iuspin);
457 goto retry;
459 if (!desc->reslength) { /* zero length read */
460 spin_unlock_irq(&desc->iuspin);
461 goto retry;
463 cntr = desc->length;
464 spin_unlock_irq(&desc->iuspin);
467 if (cntr > count)
468 cntr = count;
469 rv = copy_to_user(buffer, desc->ubuf, cntr);
470 if (rv > 0) {
471 rv = -EFAULT;
472 goto err;
475 spin_lock_irq(&desc->iuspin);
477 for (i = 0; i < desc->length - cntr; i++)
478 desc->ubuf[i] = desc->ubuf[i + cntr];
480 desc->length -= cntr;
481 /* in case we had outstanding data */
482 if (!desc->length)
483 clear_bit(WDM_READ, &desc->flags);
485 spin_unlock_irq(&desc->iuspin);
487 rv = cntr;
489 err:
490 mutex_unlock(&desc->rlock);
491 return rv;
494 static int wdm_flush(struct file *file, fl_owner_t id)
496 struct wdm_device *desc = file->private_data;
498 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
499 if (desc->werr < 0)
500 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
501 desc->werr);
503 return desc->werr;
506 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
508 struct wdm_device *desc = file->private_data;
509 unsigned long flags;
510 unsigned int mask = 0;
512 spin_lock_irqsave(&desc->iuspin, flags);
513 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
514 mask = POLLERR;
515 spin_unlock_irqrestore(&desc->iuspin, flags);
516 goto desc_out;
518 if (test_bit(WDM_READ, &desc->flags))
519 mask = POLLIN | POLLRDNORM;
520 if (desc->rerr || desc->werr)
521 mask |= POLLERR;
522 if (!test_bit(WDM_IN_USE, &desc->flags))
523 mask |= POLLOUT | POLLWRNORM;
524 spin_unlock_irqrestore(&desc->iuspin, flags);
526 poll_wait(file, &desc->wait, wait);
528 desc_out:
529 return mask;
532 static int wdm_open(struct inode *inode, struct file *file)
534 int minor = iminor(inode);
535 int rv = -ENODEV;
536 struct usb_interface *intf;
537 struct wdm_device *desc;
539 mutex_lock(&wdm_mutex);
540 intf = usb_find_interface(&wdm_driver, minor);
541 if (!intf)
542 goto out;
544 desc = usb_get_intfdata(intf);
545 if (test_bit(WDM_DISCONNECTING, &desc->flags))
546 goto out;
547 file->private_data = desc;
549 rv = usb_autopm_get_interface(desc->intf);
550 if (rv < 0) {
551 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
552 goto out;
554 intf->needs_remote_wakeup = 1;
556 /* using write lock to protect desc->count */
557 mutex_lock(&desc->wlock);
558 if (!desc->count++) {
559 desc->werr = 0;
560 desc->rerr = 0;
561 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
562 if (rv < 0) {
563 desc->count--;
564 dev_err(&desc->intf->dev,
565 "Error submitting int urb - %d\n", rv);
567 } else {
568 rv = 0;
570 mutex_unlock(&desc->wlock);
571 usb_autopm_put_interface(desc->intf);
572 out:
573 mutex_unlock(&wdm_mutex);
574 return rv;
577 static int wdm_release(struct inode *inode, struct file *file)
579 struct wdm_device *desc = file->private_data;
581 mutex_lock(&wdm_mutex);
583 /* using write lock to protect desc->count */
584 mutex_lock(&desc->wlock);
585 desc->count--;
586 mutex_unlock(&desc->wlock);
588 if (!desc->count) {
589 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
590 kill_urbs(desc);
591 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
592 desc->intf->needs_remote_wakeup = 0;
594 mutex_unlock(&wdm_mutex);
595 return 0;
598 static const struct file_operations wdm_fops = {
599 .owner = THIS_MODULE,
600 .read = wdm_read,
601 .write = wdm_write,
602 .open = wdm_open,
603 .flush = wdm_flush,
604 .release = wdm_release,
605 .poll = wdm_poll,
606 .llseek = noop_llseek,
609 static struct usb_class_driver wdm_class = {
610 .name = "cdc-wdm%d",
611 .fops = &wdm_fops,
612 .minor_base = WDM_MINOR_BASE,
615 /* --- error handling --- */
616 static void wdm_rxwork(struct work_struct *work)
618 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
619 unsigned long flags;
620 int rv;
622 spin_lock_irqsave(&desc->iuspin, flags);
623 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
624 spin_unlock_irqrestore(&desc->iuspin, flags);
625 } else {
626 spin_unlock_irqrestore(&desc->iuspin, flags);
627 rv = usb_submit_urb(desc->response, GFP_KERNEL);
628 if (rv < 0 && rv != -EPERM) {
629 spin_lock_irqsave(&desc->iuspin, flags);
630 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
631 schedule_work(&desc->rxwork);
632 spin_unlock_irqrestore(&desc->iuspin, flags);
637 /* --- hotplug --- */
639 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
641 int rv = -EINVAL;
642 struct usb_device *udev = interface_to_usbdev(intf);
643 struct wdm_device *desc;
644 struct usb_host_interface *iface;
645 struct usb_endpoint_descriptor *ep;
646 struct usb_cdc_dmm_desc *dmhd;
647 u8 *buffer = intf->altsetting->extra;
648 int buflen = intf->altsetting->extralen;
649 u16 maxcom = WDM_DEFAULT_BUFSIZE;
651 if (!buffer)
652 goto out;
654 while (buflen > 2) {
655 if (buffer [1] != USB_DT_CS_INTERFACE) {
656 dev_err(&intf->dev, "skipping garbage\n");
657 goto next_desc;
660 switch (buffer [2]) {
661 case USB_CDC_HEADER_TYPE:
662 break;
663 case USB_CDC_DMM_TYPE:
664 dmhd = (struct usb_cdc_dmm_desc *)buffer;
665 maxcom = le16_to_cpu(dmhd->wMaxCommand);
666 dev_dbg(&intf->dev,
667 "Finding maximum buffer length: %d", maxcom);
668 break;
669 default:
670 dev_err(&intf->dev,
671 "Ignoring extra header, type %d, length %d\n",
672 buffer[2], buffer[0]);
673 break;
675 next_desc:
676 buflen -= buffer[0];
677 buffer += buffer[0];
680 rv = -ENOMEM;
681 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
682 if (!desc)
683 goto out;
684 mutex_init(&desc->rlock);
685 mutex_init(&desc->wlock);
686 spin_lock_init(&desc->iuspin);
687 init_waitqueue_head(&desc->wait);
688 desc->wMaxCommand = maxcom;
689 /* this will be expanded and needed in hardware endianness */
690 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
691 desc->intf = intf;
692 INIT_WORK(&desc->rxwork, wdm_rxwork);
694 rv = -EINVAL;
695 iface = intf->cur_altsetting;
696 if (iface->desc.bNumEndpoints != 1)
697 goto err;
698 ep = &iface->endpoint[0].desc;
699 if (!ep || !usb_endpoint_is_int_in(ep))
700 goto err;
702 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
703 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
705 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
706 if (!desc->orq)
707 goto err;
708 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
709 if (!desc->irq)
710 goto err;
712 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
713 if (!desc->validity)
714 goto err;
716 desc->response = usb_alloc_urb(0, GFP_KERNEL);
717 if (!desc->response)
718 goto err;
720 desc->command = usb_alloc_urb(0, GFP_KERNEL);
721 if (!desc->command)
722 goto err;
724 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
725 if (!desc->ubuf)
726 goto err;
728 desc->sbuf = usb_alloc_coherent(interface_to_usbdev(intf),
729 desc->wMaxPacketSize,
730 GFP_KERNEL,
731 &desc->validity->transfer_dma);
732 if (!desc->sbuf)
733 goto err;
735 desc->inbuf = usb_alloc_coherent(interface_to_usbdev(intf),
736 desc->wMaxCommand,
737 GFP_KERNEL,
738 &desc->response->transfer_dma);
739 if (!desc->inbuf)
740 goto err2;
742 usb_fill_int_urb(
743 desc->validity,
744 interface_to_usbdev(intf),
745 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
746 desc->sbuf,
747 desc->wMaxPacketSize,
748 wdm_int_callback,
749 desc,
750 ep->bInterval
752 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
754 usb_set_intfdata(intf, desc);
755 rv = usb_register_dev(intf, &wdm_class);
756 if (rv < 0)
757 goto err3;
758 else
759 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
760 intf->minor - WDM_MINOR_BASE);
761 out:
762 return rv;
763 err3:
764 usb_set_intfdata(intf, NULL);
765 usb_free_coherent(interface_to_usbdev(desc->intf),
766 desc->bMaxPacketSize0,
767 desc->inbuf,
768 desc->response->transfer_dma);
769 err2:
770 usb_free_coherent(interface_to_usbdev(desc->intf),
771 desc->wMaxPacketSize,
772 desc->sbuf,
773 desc->validity->transfer_dma);
774 err:
775 free_urbs(desc);
776 kfree(desc->ubuf);
777 kfree(desc->orq);
778 kfree(desc->irq);
779 kfree(desc);
780 return rv;
783 static void wdm_disconnect(struct usb_interface *intf)
785 struct wdm_device *desc;
786 unsigned long flags;
788 usb_deregister_dev(intf, &wdm_class);
789 mutex_lock(&wdm_mutex);
790 desc = usb_get_intfdata(intf);
792 /* the spinlock makes sure no new urbs are generated in the callbacks */
793 spin_lock_irqsave(&desc->iuspin, flags);
794 set_bit(WDM_DISCONNECTING, &desc->flags);
795 set_bit(WDM_READ, &desc->flags);
796 /* to terminate pending flushes */
797 clear_bit(WDM_IN_USE, &desc->flags);
798 spin_unlock_irqrestore(&desc->iuspin, flags);
799 wake_up_all(&desc->wait);
800 mutex_lock(&desc->rlock);
801 mutex_lock(&desc->wlock);
802 kill_urbs(desc);
803 cancel_work_sync(&desc->rxwork);
804 mutex_unlock(&desc->wlock);
805 mutex_unlock(&desc->rlock);
806 if (!desc->count)
807 cleanup(desc);
808 mutex_unlock(&wdm_mutex);
811 #ifdef CONFIG_PM
812 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
814 struct wdm_device *desc = usb_get_intfdata(intf);
815 int rv = 0;
817 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
819 /* if this is an autosuspend the caller does the locking */
820 if (!PMSG_IS_AUTO(message)) {
821 mutex_lock(&desc->rlock);
822 mutex_lock(&desc->wlock);
824 spin_lock_irq(&desc->iuspin);
826 if (PMSG_IS_AUTO(message) &&
827 (test_bit(WDM_IN_USE, &desc->flags)
828 || test_bit(WDM_RESPONDING, &desc->flags))) {
829 spin_unlock_irq(&desc->iuspin);
830 rv = -EBUSY;
831 } else {
833 set_bit(WDM_SUSPENDING, &desc->flags);
834 spin_unlock_irq(&desc->iuspin);
835 /* callback submits work - order is essential */
836 kill_urbs(desc);
837 cancel_work_sync(&desc->rxwork);
839 if (!PMSG_IS_AUTO(message)) {
840 mutex_unlock(&desc->wlock);
841 mutex_unlock(&desc->rlock);
844 return rv;
846 #endif
848 static int recover_from_urb_loss(struct wdm_device *desc)
850 int rv = 0;
852 if (desc->count) {
853 rv = usb_submit_urb(desc->validity, GFP_NOIO);
854 if (rv < 0)
855 dev_err(&desc->intf->dev,
856 "Error resume submitting int urb - %d\n", rv);
858 return rv;
861 #ifdef CONFIG_PM
862 static int wdm_resume(struct usb_interface *intf)
864 struct wdm_device *desc = usb_get_intfdata(intf);
865 int rv;
867 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
869 clear_bit(WDM_SUSPENDING, &desc->flags);
870 rv = recover_from_urb_loss(desc);
872 return rv;
874 #endif
876 static int wdm_pre_reset(struct usb_interface *intf)
878 struct wdm_device *desc = usb_get_intfdata(intf);
880 mutex_lock(&desc->rlock);
881 mutex_lock(&desc->wlock);
882 kill_urbs(desc);
885 * we notify everybody using poll of
886 * an exceptional situation
887 * must be done before recovery lest a spontaneous
888 * message from the device is lost
890 spin_lock_irq(&desc->iuspin);
891 desc->rerr = -EINTR;
892 spin_unlock_irq(&desc->iuspin);
893 wake_up_all(&desc->wait);
894 return 0;
897 static int wdm_post_reset(struct usb_interface *intf)
899 struct wdm_device *desc = usb_get_intfdata(intf);
900 int rv;
902 rv = recover_from_urb_loss(desc);
903 mutex_unlock(&desc->wlock);
904 mutex_unlock(&desc->rlock);
905 return 0;
908 static struct usb_driver wdm_driver = {
909 .name = "cdc_wdm",
910 .probe = wdm_probe,
911 .disconnect = wdm_disconnect,
912 #ifdef CONFIG_PM
913 .suspend = wdm_suspend,
914 .resume = wdm_resume,
915 .reset_resume = wdm_resume,
916 #endif
917 .pre_reset = wdm_pre_reset,
918 .post_reset = wdm_post_reset,
919 .id_table = wdm_ids,
920 .supports_autosuspend = 1,
923 module_usb_driver(wdm_driver);
925 MODULE_AUTHOR(DRIVER_AUTHOR);
926 MODULE_DESCRIPTION(DRIVER_DESC);
927 MODULE_LICENSE("GPL");