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/ioctl.h>
17 #include <linux/slab.h>
18 #include <linux/module.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 #include <linux/usb/cdc-wdm.h>
29 #define DRIVER_AUTHOR "Oliver Neukum"
30 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
32 static const struct usb_device_id wdm_ids[] = {
34 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
35 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
36 .bInterfaceClass = USB_CLASS_COMM,
37 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
42 MODULE_DEVICE_TABLE (usb, wdm_ids);
44 #define WDM_MINOR_BASE 176
48 #define WDM_DISCONNECTING 2
51 #define WDM_INT_STALL 5
52 #define WDM_POLL_RUNNING 6
53 #define WDM_RESPONDING 7
54 #define WDM_SUSPENDING 8
55 #define WDM_RESETTING 9
56 #define WDM_OVERFLOW 10
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);
64 static DEFINE_SPINLOCK(wdm_device_list_lock);
65 static LIST_HEAD(wdm_device_list);
67 /* --- method tables --- */
70 u8 *inbuf; /* buffer for response */
71 u8 *outbuf; /* buffer for command */
72 u8 *sbuf; /* buffer for status */
73 u8 *ubuf; /* buffer for copy to user space */
78 struct usb_interface *intf;
79 struct usb_ctrlrequest *orq;
80 struct usb_ctrlrequest *irq;
96 wait_queue_head_t wait;
97 struct work_struct rxwork;
102 struct list_head device_list;
103 int (*manage_power)(struct usb_interface *, int);
106 static struct usb_driver wdm_driver;
108 /* return intfdata if we own the interface, else look up intf in the list */
109 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
111 struct wdm_device *desc;
113 spin_lock(&wdm_device_list_lock);
114 list_for_each_entry(desc, &wdm_device_list, device_list)
115 if (desc->intf == intf)
119 spin_unlock(&wdm_device_list_lock);
124 static struct wdm_device *wdm_find_device_by_minor(int minor)
126 struct wdm_device *desc;
128 spin_lock(&wdm_device_list_lock);
129 list_for_each_entry(desc, &wdm_device_list, device_list)
130 if (desc->intf->minor == minor)
134 spin_unlock(&wdm_device_list_lock);
139 /* --- callbacks --- */
140 static void wdm_out_callback(struct urb *urb)
142 struct wdm_device *desc;
144 spin_lock(&desc->iuspin);
145 desc->werr = urb->status;
146 spin_unlock(&desc->iuspin);
149 clear_bit(WDM_IN_USE, &desc->flags);
150 wake_up(&desc->wait);
153 /* forward declaration */
154 static int service_outstanding_interrupt(struct wdm_device *desc);
156 static void wdm_in_callback(struct urb *urb)
158 struct wdm_device *desc = urb->context;
159 int status = urb->status;
160 int length = urb->actual_length;
162 spin_lock(&desc->iuspin);
163 clear_bit(WDM_RESPONDING, &desc->flags);
168 dev_dbg(&desc->intf->dev,
169 "nonzero urb status received: -ENOENT\n");
172 dev_dbg(&desc->intf->dev,
173 "nonzero urb status received: -ECONNRESET\n");
176 dev_dbg(&desc->intf->dev,
177 "nonzero urb status received: -ESHUTDOWN\n");
180 dev_err(&desc->intf->dev,
181 "nonzero urb status received: -EPIPE\n");
184 dev_err(&desc->intf->dev,
185 "Unexpected error %d\n", status);
191 * only set a new error if there is no previous error.
192 * Errors are only cleared during read/open
193 * Avoid propagating -EPIPE (stall) to userspace since it is
194 * better handled as an empty read
196 if (desc->rerr == 0 && status != -EPIPE)
199 if (length + desc->length > desc->wMaxCommand) {
200 /* The buffer would overflow */
201 set_bit(WDM_OVERFLOW, &desc->flags);
203 /* we may already be in overflow */
204 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
205 memmove(desc->ubuf + desc->length, desc->inbuf, length);
206 desc->length += length;
207 desc->reslength = length;
211 set_bit(WDM_READ, &desc->flags);
212 wake_up(&desc->wait);
216 * Since there was an error, userspace may decide to not read
217 * any data after poll'ing.
218 * We should respond to further attempts from the device to send
219 * data, so that we can get unstuck.
221 service_outstanding_interrupt(desc);
224 spin_unlock(&desc->iuspin);
227 static void wdm_int_callback(struct urb *urb)
231 int status = urb->status;
232 struct wdm_device *desc;
233 struct usb_cdc_notification *dr;
236 dr = (struct usb_cdc_notification *)desc->sbuf;
245 set_bit(WDM_INT_STALL, &desc->flags);
246 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
247 goto sw; /* halt is cleared in work */
249 dev_err(&desc->intf->dev,
250 "nonzero urb status received: %d\n", status);
255 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
256 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
261 switch (dr->bNotificationType) {
262 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
263 dev_dbg(&desc->intf->dev,
264 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
265 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
268 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
270 dev_dbg(&desc->intf->dev,
271 "NOTIFY_NETWORK_CONNECTION %s network\n",
272 dr->wValue ? "connected to" : "disconnected from");
274 case USB_CDC_NOTIFY_SPEED_CHANGE:
275 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
279 clear_bit(WDM_POLL_RUNNING, &desc->flags);
280 dev_err(&desc->intf->dev,
281 "unknown notification %d received: index %d len %d\n",
282 dr->bNotificationType,
283 le16_to_cpu(dr->wIndex),
284 le16_to_cpu(dr->wLength));
288 spin_lock(&desc->iuspin);
289 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
290 if (!desc->resp_count++ && !responding
291 && !test_bit(WDM_DISCONNECTING, &desc->flags)
292 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
293 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
294 dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
296 spin_unlock(&desc->iuspin);
298 clear_bit(WDM_RESPONDING, &desc->flags);
303 rv = schedule_work(&desc->rxwork);
305 dev_err(&desc->intf->dev,
306 "Cannot schedule work\n");
310 rv = usb_submit_urb(urb, GFP_ATOMIC);
312 dev_err(&desc->intf->dev,
313 "%s - usb_submit_urb failed with result %d\n",
318 static void kill_urbs(struct wdm_device *desc)
320 /* the order here is essential */
321 usb_kill_urb(desc->command);
322 usb_kill_urb(desc->validity);
323 usb_kill_urb(desc->response);
326 static void free_urbs(struct wdm_device *desc)
328 usb_free_urb(desc->validity);
329 usb_free_urb(desc->response);
330 usb_free_urb(desc->command);
333 static void cleanup(struct wdm_device *desc)
344 static ssize_t wdm_write
345 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
348 int rv = -EMSGSIZE, r, we;
349 struct wdm_device *desc = file->private_data;
350 struct usb_ctrlrequest *req;
352 if (count > desc->wMaxCommand)
353 count = desc->wMaxCommand;
355 spin_lock_irq(&desc->iuspin);
358 spin_unlock_irq(&desc->iuspin);
360 return usb_translate_errors(we);
362 buf = memdup_user(buffer, count);
366 /* concurrent writes and disconnect */
367 r = mutex_lock_interruptible(&desc->wlock);
372 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
374 goto out_free_mem_lock;
377 r = usb_autopm_get_interface(desc->intf);
379 rv = usb_translate_errors(r);
380 goto out_free_mem_lock;
383 if (!(file->f_flags & O_NONBLOCK))
384 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
387 if (test_bit(WDM_IN_USE, &desc->flags))
390 if (test_bit(WDM_RESETTING, &desc->flags))
395 goto out_free_mem_pm;
399 usb_fill_control_urb(
401 interface_to_usbdev(desc->intf),
402 /* using common endpoint 0 */
403 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
404 (unsigned char *)req,
411 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
412 USB_RECIP_INTERFACE);
413 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
415 req->wIndex = desc->inum; /* already converted */
416 req->wLength = cpu_to_le16(count);
417 set_bit(WDM_IN_USE, &desc->flags);
420 rv = usb_submit_urb(desc->command, GFP_KERNEL);
423 clear_bit(WDM_IN_USE, &desc->flags);
424 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
425 rv = usb_translate_errors(rv);
426 goto out_free_mem_pm;
428 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
429 le16_to_cpu(req->wIndex));
432 usb_autopm_put_interface(desc->intf);
433 mutex_unlock(&desc->wlock);
437 usb_autopm_put_interface(desc->intf);
439 mutex_unlock(&desc->wlock);
446 * Submit the read urb if resp_count is non-zero.
448 * Called with desc->iuspin locked
450 static int service_outstanding_interrupt(struct wdm_device *desc)
454 /* submit read urb only if the device is waiting for it */
455 if (!desc->resp_count || !--desc->resp_count)
458 set_bit(WDM_RESPONDING, &desc->flags);
459 spin_unlock_irq(&desc->iuspin);
460 rv = usb_submit_urb(desc->response, GFP_KERNEL);
461 spin_lock_irq(&desc->iuspin);
463 dev_err(&desc->intf->dev,
464 "usb_submit_urb failed with result %d\n", rv);
466 /* make sure the next notification trigger a submit */
467 clear_bit(WDM_RESPONDING, &desc->flags);
468 desc->resp_count = 0;
474 static ssize_t wdm_read
475 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
479 struct wdm_device *desc = file->private_data;
482 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
486 cntr = ACCESS_ONCE(desc->length);
490 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
494 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
495 clear_bit(WDM_OVERFLOW, &desc->flags);
500 if (file->f_flags & O_NONBLOCK) {
501 if (!test_bit(WDM_READ, &desc->flags)) {
507 rv = wait_event_interruptible(desc->wait,
508 test_bit(WDM_READ, &desc->flags));
511 /* may have happened while we slept */
512 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
516 if (test_bit(WDM_RESETTING, &desc->flags)) {
520 usb_mark_last_busy(interface_to_usbdev(desc->intf));
526 spin_lock_irq(&desc->iuspin);
528 if (desc->rerr) { /* read completed, error happened */
529 rv = usb_translate_errors(desc->rerr);
531 spin_unlock_irq(&desc->iuspin);
535 * recheck whether we've lost the race
536 * against the completion handler
538 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
539 spin_unlock_irq(&desc->iuspin);
543 if (!desc->reslength) { /* zero length read */
544 dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
545 clear_bit(WDM_READ, &desc->flags);
546 rv = service_outstanding_interrupt(desc);
547 spin_unlock_irq(&desc->iuspin);
553 spin_unlock_irq(&desc->iuspin);
558 rv = copy_to_user(buffer, desc->ubuf, cntr);
564 spin_lock_irq(&desc->iuspin);
566 for (i = 0; i < desc->length - cntr; i++)
567 desc->ubuf[i] = desc->ubuf[i + cntr];
569 desc->length -= cntr;
570 /* in case we had outstanding data */
572 clear_bit(WDM_READ, &desc->flags);
573 service_outstanding_interrupt(desc);
575 spin_unlock_irq(&desc->iuspin);
579 mutex_unlock(&desc->rlock);
583 static int wdm_flush(struct file *file, fl_owner_t id)
585 struct wdm_device *desc = file->private_data;
587 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
589 /* cannot dereference desc->intf if WDM_DISCONNECTING */
590 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
591 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
594 return usb_translate_errors(desc->werr);
597 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
599 struct wdm_device *desc = file->private_data;
601 unsigned int mask = 0;
603 spin_lock_irqsave(&desc->iuspin, flags);
604 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
605 mask = POLLHUP | POLLERR;
606 spin_unlock_irqrestore(&desc->iuspin, flags);
609 if (test_bit(WDM_READ, &desc->flags))
610 mask = POLLIN | POLLRDNORM;
611 if (desc->rerr || desc->werr)
613 if (!test_bit(WDM_IN_USE, &desc->flags))
614 mask |= POLLOUT | POLLWRNORM;
615 spin_unlock_irqrestore(&desc->iuspin, flags);
617 poll_wait(file, &desc->wait, wait);
623 static int wdm_open(struct inode *inode, struct file *file)
625 int minor = iminor(inode);
627 struct usb_interface *intf;
628 struct wdm_device *desc;
630 mutex_lock(&wdm_mutex);
631 desc = wdm_find_device_by_minor(minor);
636 if (test_bit(WDM_DISCONNECTING, &desc->flags))
638 file->private_data = desc;
640 rv = usb_autopm_get_interface(desc->intf);
642 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
646 /* using write lock to protect desc->count */
647 mutex_lock(&desc->wlock);
648 if (!desc->count++) {
651 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
654 dev_err(&desc->intf->dev,
655 "Error submitting int urb - %d\n", rv);
656 rv = usb_translate_errors(rv);
661 mutex_unlock(&desc->wlock);
662 if (desc->count == 1)
663 desc->manage_power(intf, 1);
664 usb_autopm_put_interface(desc->intf);
666 mutex_unlock(&wdm_mutex);
670 static int wdm_release(struct inode *inode, struct file *file)
672 struct wdm_device *desc = file->private_data;
674 mutex_lock(&wdm_mutex);
676 /* using write lock to protect desc->count */
677 mutex_lock(&desc->wlock);
679 mutex_unlock(&desc->wlock);
682 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
683 dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
685 spin_lock_irq(&desc->iuspin);
686 desc->resp_count = 0;
687 spin_unlock_irq(&desc->iuspin);
688 desc->manage_power(desc->intf, 0);
690 /* must avoid dev_printk here as desc->intf is invalid */
691 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
695 mutex_unlock(&wdm_mutex);
699 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
701 struct wdm_device *desc = file->private_data;
705 case IOCTL_WDM_MAX_COMMAND:
706 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
715 static const struct file_operations wdm_fops = {
716 .owner = THIS_MODULE,
721 .release = wdm_release,
723 .unlocked_ioctl = wdm_ioctl,
724 .compat_ioctl = wdm_ioctl,
725 .llseek = noop_llseek,
728 static struct usb_class_driver wdm_class = {
731 .minor_base = WDM_MINOR_BASE,
734 /* --- error handling --- */
735 static void wdm_rxwork(struct work_struct *work)
737 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
742 spin_lock_irqsave(&desc->iuspin, flags);
743 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
744 spin_unlock_irqrestore(&desc->iuspin, flags);
746 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
747 spin_unlock_irqrestore(&desc->iuspin, flags);
749 rv = usb_submit_urb(desc->response, GFP_KERNEL);
750 if (rv < 0 && rv != -EPERM) {
751 spin_lock_irqsave(&desc->iuspin, flags);
752 clear_bit(WDM_RESPONDING, &desc->flags);
753 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
754 schedule_work(&desc->rxwork);
755 spin_unlock_irqrestore(&desc->iuspin, flags);
760 /* --- hotplug --- */
762 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
763 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
766 struct wdm_device *desc;
768 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
771 INIT_LIST_HEAD(&desc->device_list);
772 mutex_init(&desc->rlock);
773 mutex_init(&desc->wlock);
774 spin_lock_init(&desc->iuspin);
775 init_waitqueue_head(&desc->wait);
776 desc->wMaxCommand = bufsize;
777 /* this will be expanded and needed in hardware endianness */
778 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
780 INIT_WORK(&desc->rxwork, wdm_rxwork);
783 if (!usb_endpoint_is_int_in(ep))
786 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
788 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
791 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
795 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
799 desc->response = usb_alloc_urb(0, GFP_KERNEL);
803 desc->command = usb_alloc_urb(0, GFP_KERNEL);
807 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
811 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
815 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
821 interface_to_usbdev(intf),
822 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
824 desc->wMaxPacketSize,
830 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
831 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
832 desc->irq->wValue = 0;
833 desc->irq->wIndex = desc->inum; /* already converted */
834 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
836 usb_fill_control_urb(
838 interface_to_usbdev(intf),
839 /* using common endpoint 0 */
840 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
841 (unsigned char *)desc->irq,
848 desc->manage_power = manage_power;
850 spin_lock(&wdm_device_list_lock);
851 list_add(&desc->device_list, &wdm_device_list);
852 spin_unlock(&wdm_device_list_lock);
854 rv = usb_register_dev(intf, &wdm_class);
858 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
862 spin_lock(&wdm_device_list_lock);
863 list_del(&desc->device_list);
864 spin_unlock(&wdm_device_list_lock);
869 static int wdm_manage_power(struct usb_interface *intf, int on)
871 /* need autopm_get/put here to ensure the usbcore sees the new value */
872 int rv = usb_autopm_get_interface(intf);
874 intf->needs_remote_wakeup = on;
876 usb_autopm_put_interface(intf);
880 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
883 struct usb_host_interface *iface;
884 struct usb_endpoint_descriptor *ep;
885 struct usb_cdc_parsed_header hdr;
886 u8 *buffer = intf->altsetting->extra;
887 int buflen = intf->altsetting->extralen;
888 u16 maxcom = WDM_DEFAULT_BUFSIZE;
893 cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
895 if (hdr.usb_cdc_dmm_desc)
896 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
898 iface = intf->cur_altsetting;
899 if (iface->desc.bNumEndpoints != 1)
901 ep = &iface->endpoint[0].desc;
903 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
910 * usb_cdc_wdm_register - register a WDM subdriver
911 * @intf: usb interface the subdriver will associate with
912 * @ep: interrupt endpoint to monitor for notifications
913 * @bufsize: maximum message size to support for read/write
915 * Create WDM usb class character device and associate it with intf
916 * without binding, allowing another driver to manage the interface.
918 * The subdriver will manage the given interrupt endpoint exclusively
919 * and will issue control requests referring to the given intf. It
920 * will otherwise avoid interferring, and in particular not do
921 * usb_set_intfdata/usb_get_intfdata on intf.
923 * The return value is a pointer to the subdriver's struct usb_driver.
924 * The registering driver is responsible for calling this subdriver's
925 * disconnect, suspend, resume, pre_reset and post_reset methods from
928 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
929 struct usb_endpoint_descriptor *ep,
931 int (*manage_power)(struct usb_interface *, int))
935 rv = wdm_create(intf, ep, bufsize, manage_power);
943 EXPORT_SYMBOL(usb_cdc_wdm_register);
945 static void wdm_disconnect(struct usb_interface *intf)
947 struct wdm_device *desc;
950 usb_deregister_dev(intf, &wdm_class);
951 desc = wdm_find_device(intf);
952 mutex_lock(&wdm_mutex);
954 /* the spinlock makes sure no new urbs are generated in the callbacks */
955 spin_lock_irqsave(&desc->iuspin, flags);
956 set_bit(WDM_DISCONNECTING, &desc->flags);
957 set_bit(WDM_READ, &desc->flags);
958 /* to terminate pending flushes */
959 clear_bit(WDM_IN_USE, &desc->flags);
960 spin_unlock_irqrestore(&desc->iuspin, flags);
961 wake_up_all(&desc->wait);
962 mutex_lock(&desc->rlock);
963 mutex_lock(&desc->wlock);
965 cancel_work_sync(&desc->rxwork);
966 mutex_unlock(&desc->wlock);
967 mutex_unlock(&desc->rlock);
969 /* the desc->intf pointer used as list key is now invalid */
970 spin_lock(&wdm_device_list_lock);
971 list_del(&desc->device_list);
972 spin_unlock(&wdm_device_list_lock);
977 dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
978 mutex_unlock(&wdm_mutex);
982 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
984 struct wdm_device *desc = wdm_find_device(intf);
987 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
989 /* if this is an autosuspend the caller does the locking */
990 if (!PMSG_IS_AUTO(message)) {
991 mutex_lock(&desc->rlock);
992 mutex_lock(&desc->wlock);
994 spin_lock_irq(&desc->iuspin);
996 if (PMSG_IS_AUTO(message) &&
997 (test_bit(WDM_IN_USE, &desc->flags)
998 || test_bit(WDM_RESPONDING, &desc->flags))) {
999 spin_unlock_irq(&desc->iuspin);
1003 set_bit(WDM_SUSPENDING, &desc->flags);
1004 spin_unlock_irq(&desc->iuspin);
1005 /* callback submits work - order is essential */
1007 cancel_work_sync(&desc->rxwork);
1009 if (!PMSG_IS_AUTO(message)) {
1010 mutex_unlock(&desc->wlock);
1011 mutex_unlock(&desc->rlock);
1018 static int recover_from_urb_loss(struct wdm_device *desc)
1023 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1025 dev_err(&desc->intf->dev,
1026 "Error resume submitting int urb - %d\n", rv);
1032 static int wdm_resume(struct usb_interface *intf)
1034 struct wdm_device *desc = wdm_find_device(intf);
1037 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1039 clear_bit(WDM_SUSPENDING, &desc->flags);
1040 rv = recover_from_urb_loss(desc);
1046 static int wdm_pre_reset(struct usb_interface *intf)
1048 struct wdm_device *desc = wdm_find_device(intf);
1051 * we notify everybody using poll of
1052 * an exceptional situation
1053 * must be done before recovery lest a spontaneous
1054 * message from the device is lost
1056 spin_lock_irq(&desc->iuspin);
1057 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1058 set_bit(WDM_READ, &desc->flags); /* unblock read */
1059 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1060 desc->rerr = -EINTR;
1061 spin_unlock_irq(&desc->iuspin);
1062 wake_up_all(&desc->wait);
1063 mutex_lock(&desc->rlock);
1064 mutex_lock(&desc->wlock);
1066 cancel_work_sync(&desc->rxwork);
1070 static int wdm_post_reset(struct usb_interface *intf)
1072 struct wdm_device *desc = wdm_find_device(intf);
1075 clear_bit(WDM_OVERFLOW, &desc->flags);
1076 clear_bit(WDM_RESETTING, &desc->flags);
1077 rv = recover_from_urb_loss(desc);
1078 mutex_unlock(&desc->wlock);
1079 mutex_unlock(&desc->rlock);
1083 static struct usb_driver wdm_driver = {
1086 .disconnect = wdm_disconnect,
1088 .suspend = wdm_suspend,
1089 .resume = wdm_resume,
1090 .reset_resume = wdm_resume,
1092 .pre_reset = wdm_pre_reset,
1093 .post_reset = wdm_post_reset,
1094 .id_table = wdm_ids,
1095 .supports_autosuspend = 1,
1096 .disable_hub_initiated_lpm = 1,
1099 module_usb_driver(wdm_driver);
1101 MODULE_AUTHOR(DRIVER_AUTHOR);
1102 MODULE_DESCRIPTION(DRIVER_DESC);
1103 MODULE_LICENSE("GPL");