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>
32 #define DRIVER_VERSION "v0.03"
33 #define DRIVER_AUTHOR "Oliver Neukum"
34 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
36 static const struct usb_device_id wdm_ids[] = {
38 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
39 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
40 .bInterfaceClass = USB_CLASS_COMM,
41 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
46 MODULE_DEVICE_TABLE (usb, wdm_ids);
48 #define WDM_MINOR_BASE 176
52 #define WDM_DISCONNECTING 2
55 #define WDM_INT_STALL 5
56 #define WDM_POLL_RUNNING 6
57 #define WDM_RESPONDING 7
58 #define WDM_SUSPENDING 8
59 #define WDM_RESETTING 9
60 #define WDM_OVERFLOW 10
64 /* we cannot wait forever at flush() */
65 #define WDM_FLUSH_TIMEOUT (30 * HZ)
67 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
68 #define WDM_DEFAULT_BUFSIZE 256
70 static DEFINE_MUTEX(wdm_mutex);
71 static DEFINE_SPINLOCK(wdm_device_list_lock);
72 static LIST_HEAD(wdm_device_list);
74 /* --- method tables --- */
77 u8 *inbuf; /* buffer for response */
78 u8 *outbuf; /* buffer for command */
79 u8 *sbuf; /* buffer for status */
80 u8 *ubuf; /* buffer for copy to user space */
85 struct usb_interface *intf;
86 struct usb_ctrlrequest *orq;
87 struct usb_ctrlrequest *irq;
103 wait_queue_head_t wait;
104 struct work_struct rxwork;
109 struct list_head device_list;
110 int (*manage_power)(struct usb_interface *, int);
113 static struct usb_driver wdm_driver;
115 /* return intfdata if we own the interface, else look up intf in the list */
116 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
118 struct wdm_device *desc;
120 spin_lock(&wdm_device_list_lock);
121 list_for_each_entry(desc, &wdm_device_list, device_list)
122 if (desc->intf == intf)
126 spin_unlock(&wdm_device_list_lock);
131 static struct wdm_device *wdm_find_device_by_minor(int minor)
133 struct wdm_device *desc;
135 spin_lock(&wdm_device_list_lock);
136 list_for_each_entry(desc, &wdm_device_list, device_list)
137 if (desc->intf->minor == minor)
141 spin_unlock(&wdm_device_list_lock);
146 /* --- callbacks --- */
147 static void wdm_out_callback(struct urb *urb)
149 struct wdm_device *desc;
151 spin_lock(&desc->iuspin);
152 desc->werr = urb->status;
153 spin_unlock(&desc->iuspin);
156 clear_bit(WDM_IN_USE, &desc->flags);
157 wake_up_all(&desc->wait);
160 static void wdm_in_callback(struct urb *urb)
162 struct wdm_device *desc = urb->context;
163 int status = urb->status;
164 int length = urb->actual_length;
166 spin_lock(&desc->iuspin);
167 clear_bit(WDM_RESPONDING, &desc->flags);
172 dev_dbg(&desc->intf->dev,
173 "nonzero urb status received: -ENOENT");
176 dev_dbg(&desc->intf->dev,
177 "nonzero urb status received: -ECONNRESET");
180 dev_dbg(&desc->intf->dev,
181 "nonzero urb status received: -ESHUTDOWN");
184 dev_err(&desc->intf->dev,
185 "nonzero urb status received: -EPIPE\n");
188 dev_err(&desc->intf->dev,
189 "Unexpected error %d\n", status);
195 if (length + desc->length > desc->wMaxCommand) {
196 /* The buffer would overflow */
197 set_bit(WDM_OVERFLOW, &desc->flags);
199 /* we may already be in overflow */
200 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
201 memmove(desc->ubuf + desc->length, desc->inbuf, length);
202 desc->length += length;
203 desc->reslength = length;
207 wake_up(&desc->wait);
209 set_bit(WDM_READ, &desc->flags);
210 spin_unlock(&desc->iuspin);
213 static void wdm_int_callback(struct urb *urb)
217 int status = urb->status;
218 struct wdm_device *desc;
219 struct usb_cdc_notification *dr;
222 dr = (struct usb_cdc_notification *)desc->sbuf;
231 set_bit(WDM_INT_STALL, &desc->flags);
232 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
233 goto sw; /* halt is cleared in work */
235 dev_err(&desc->intf->dev,
236 "nonzero urb status received: %d\n", status);
241 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
242 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
247 switch (dr->bNotificationType) {
248 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
249 dev_dbg(&desc->intf->dev,
250 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
251 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
254 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
256 dev_dbg(&desc->intf->dev,
257 "NOTIFY_NETWORK_CONNECTION %s network",
258 dr->wValue ? "connected to" : "disconnected from");
260 case USB_CDC_NOTIFY_SPEED_CHANGE:
261 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)",
265 clear_bit(WDM_POLL_RUNNING, &desc->flags);
266 dev_err(&desc->intf->dev,
267 "unknown notification %d received: index %d len %d\n",
268 dr->bNotificationType,
269 le16_to_cpu(dr->wIndex),
270 le16_to_cpu(dr->wLength));
274 spin_lock(&desc->iuspin);
275 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
276 if (!desc->resp_count++ && !responding
277 && !test_bit(WDM_DISCONNECTING, &desc->flags)
278 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
279 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
280 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
283 spin_unlock(&desc->iuspin);
285 clear_bit(WDM_RESPONDING, &desc->flags);
290 rv = schedule_work(&desc->rxwork);
292 dev_err(&desc->intf->dev,
293 "Cannot schedule work\n");
297 rv = usb_submit_urb(urb, GFP_ATOMIC);
299 dev_err(&desc->intf->dev,
300 "%s - usb_submit_urb failed with result %d\n",
305 static void kill_urbs(struct wdm_device *desc)
307 /* the order here is essential */
308 usb_kill_urb(desc->command);
309 usb_kill_urb(desc->validity);
310 usb_kill_urb(desc->response);
313 static void free_urbs(struct wdm_device *desc)
315 usb_free_urb(desc->validity);
316 usb_free_urb(desc->response);
317 usb_free_urb(desc->command);
320 static void cleanup(struct wdm_device *desc)
331 static ssize_t wdm_write
332 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
335 int rv = -EMSGSIZE, r, we;
336 struct wdm_device *desc = file->private_data;
337 struct usb_ctrlrequest *req;
339 if (count > desc->wMaxCommand)
340 count = desc->wMaxCommand;
342 spin_lock_irq(&desc->iuspin);
345 spin_unlock_irq(&desc->iuspin);
347 return usb_translate_errors(we);
349 buf = kmalloc(count, GFP_KERNEL);
355 r = copy_from_user(buf, buffer, count);
361 /* concurrent writes and disconnect */
362 r = mutex_lock_interruptible(&desc->wlock);
367 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
369 goto out_free_mem_lock;
372 r = usb_autopm_get_interface(desc->intf);
374 rv = usb_translate_errors(r);
375 goto out_free_mem_lock;
378 if (!(file->f_flags & O_NONBLOCK))
379 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
382 if (test_bit(WDM_IN_USE, &desc->flags))
385 if (test_bit(WDM_RESETTING, &desc->flags))
388 if (test_bit(WDM_DISCONNECTING, &desc->flags))
393 goto out_free_mem_pm;
397 usb_fill_control_urb(
399 interface_to_usbdev(desc->intf),
400 /* using common endpoint 0 */
401 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
402 (unsigned char *)req,
409 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
410 USB_RECIP_INTERFACE);
411 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
413 req->wIndex = desc->inum; /* already converted */
414 req->wLength = cpu_to_le16(count);
415 set_bit(WDM_IN_USE, &desc->flags);
418 rv = usb_submit_urb(desc->command, GFP_KERNEL);
421 clear_bit(WDM_IN_USE, &desc->flags);
422 wake_up_all(&desc->wait); /* for wdm_wait_for_response() */
423 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
424 rv = usb_translate_errors(rv);
425 goto out_free_mem_pm;
427 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
428 le16_to_cpu(req->wIndex));
431 usb_autopm_put_interface(desc->intf);
432 mutex_unlock(&desc->wlock);
434 return rv < 0 ? rv : count;
437 usb_autopm_put_interface(desc->intf);
439 mutex_unlock(&desc->wlock);
446 * clear WDM_READ flag and possibly submit the read urb if resp_count
449 * Called with desc->iuspin locked
451 static int clear_wdm_read_flag(struct wdm_device *desc)
455 clear_bit(WDM_READ, &desc->flags);
457 /* submit read urb only if the device is waiting for it */
458 if (!desc->resp_count || !--desc->resp_count)
461 set_bit(WDM_RESPONDING, &desc->flags);
462 spin_unlock_irq(&desc->iuspin);
463 rv = usb_submit_urb(desc->response, GFP_KERNEL);
464 spin_lock_irq(&desc->iuspin);
466 dev_err(&desc->intf->dev,
467 "usb_submit_urb failed with result %d\n", rv);
469 /* make sure the next notification trigger a submit */
470 clear_bit(WDM_RESPONDING, &desc->flags);
471 desc->resp_count = 0;
477 static ssize_t wdm_read
478 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
482 struct wdm_device *desc = file->private_data;
485 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
489 cntr = ACCESS_ONCE(desc->length);
493 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
497 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
498 clear_bit(WDM_OVERFLOW, &desc->flags);
503 if (file->f_flags & O_NONBLOCK) {
504 if (!test_bit(WDM_READ, &desc->flags)) {
505 rv = cntr ? cntr : -EAGAIN;
510 rv = wait_event_interruptible(desc->wait,
511 test_bit(WDM_READ, &desc->flags));
514 /* may have happened while we slept */
515 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
519 if (test_bit(WDM_RESETTING, &desc->flags)) {
523 usb_mark_last_busy(interface_to_usbdev(desc->intf));
529 spin_lock_irq(&desc->iuspin);
531 if (desc->rerr) { /* read completed, error happened */
532 rv = usb_translate_errors(desc->rerr);
534 spin_unlock_irq(&desc->iuspin);
538 * recheck whether we've lost the race
539 * against the completion handler
541 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
542 spin_unlock_irq(&desc->iuspin);
546 if (!desc->reslength) { /* zero length read */
547 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
548 rv = clear_wdm_read_flag(desc);
549 spin_unlock_irq(&desc->iuspin);
555 spin_unlock_irq(&desc->iuspin);
560 rv = copy_to_user(buffer, desc->ubuf, cntr);
566 spin_lock_irq(&desc->iuspin);
568 for (i = 0; i < desc->length - cntr; i++)
569 desc->ubuf[i] = desc->ubuf[i + cntr];
571 desc->length -= cntr;
572 /* in case we had outstanding data */
574 clear_wdm_read_flag(desc);
575 spin_unlock_irq(&desc->iuspin);
579 mutex_unlock(&desc->rlock);
583 static int wdm_wait_for_response(struct file *file, long timeout)
585 struct wdm_device *desc = file->private_data;
586 long rv; /* Use long here because (int) MAX_SCHEDULE_TIMEOUT < 0. */
589 * Needs both flags. We cannot do with one because resetting it would
590 * cause a race with write() yet we need to signal a disconnect.
592 rv = wait_event_interruptible_timeout(desc->wait,
593 !test_bit(WDM_IN_USE, &desc->flags) ||
594 test_bit(WDM_DISCONNECTING, &desc->flags),
598 * To report the correct error. This is best effort.
599 * We are inevitably racing with the hardware.
601 if (test_bit(WDM_DISCONNECTING, &desc->flags))
608 spin_lock_irq(&desc->iuspin);
611 spin_unlock_irq(&desc->iuspin);
613 return usb_translate_errors(rv);
618 * You need to send a signal when you react to malicious or defective hardware.
619 * Also, don't abort when fsync() returned -EINVAL, for older kernels which do
620 * not implement wdm_flush() will return -EINVAL.
622 static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
624 return wdm_wait_for_response(file, MAX_SCHEDULE_TIMEOUT);
628 * Same with wdm_fsync(), except it uses finite timeout in order to react to
629 * malicious or defective hardware which ceased communication after close() was
630 * implicitly called due to process termination.
632 static int wdm_flush(struct file *file, fl_owner_t id)
634 return wdm_wait_for_response(file, WDM_FLUSH_TIMEOUT);
637 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
639 struct wdm_device *desc = file->private_data;
641 unsigned int mask = 0;
643 spin_lock_irqsave(&desc->iuspin, flags);
644 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
645 mask = POLLHUP | POLLERR;
646 spin_unlock_irqrestore(&desc->iuspin, flags);
649 if (test_bit(WDM_READ, &desc->flags))
650 mask = POLLIN | POLLRDNORM;
651 if (desc->rerr || desc->werr)
653 if (!test_bit(WDM_IN_USE, &desc->flags))
654 mask |= POLLOUT | POLLWRNORM;
655 spin_unlock_irqrestore(&desc->iuspin, flags);
657 poll_wait(file, &desc->wait, wait);
663 static int wdm_open(struct inode *inode, struct file *file)
665 int minor = iminor(inode);
667 struct usb_interface *intf;
668 struct wdm_device *desc;
670 mutex_lock(&wdm_mutex);
671 desc = wdm_find_device_by_minor(minor);
676 if (test_bit(WDM_DISCONNECTING, &desc->flags))
678 file->private_data = desc;
680 rv = usb_autopm_get_interface(desc->intf);
682 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
686 /* using write lock to protect desc->count */
687 mutex_lock(&desc->wlock);
688 if (!desc->count++) {
691 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
694 dev_err(&desc->intf->dev,
695 "Error submitting int urb - %d\n", rv);
696 rv = usb_translate_errors(rv);
701 mutex_unlock(&desc->wlock);
702 if (desc->count == 1)
703 desc->manage_power(intf, 1);
704 usb_autopm_put_interface(desc->intf);
706 mutex_unlock(&wdm_mutex);
710 static int wdm_release(struct inode *inode, struct file *file)
712 struct wdm_device *desc = file->private_data;
714 mutex_lock(&wdm_mutex);
716 /* using write lock to protect desc->count */
717 mutex_lock(&desc->wlock);
719 mutex_unlock(&desc->wlock);
722 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
723 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
725 spin_lock_irq(&desc->iuspin);
726 desc->resp_count = 0;
727 spin_unlock_irq(&desc->iuspin);
728 desc->manage_power(desc->intf, 0);
730 /* must avoid dev_printk here as desc->intf is invalid */
731 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
735 mutex_unlock(&wdm_mutex);
739 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
741 struct wdm_device *desc = file->private_data;
745 case IOCTL_WDM_MAX_COMMAND:
746 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
755 static const struct file_operations wdm_fops = {
756 .owner = THIS_MODULE,
762 .release = wdm_release,
764 .unlocked_ioctl = wdm_ioctl,
765 .compat_ioctl = wdm_ioctl,
766 .llseek = noop_llseek,
769 static struct usb_class_driver wdm_class = {
772 .minor_base = WDM_MINOR_BASE,
775 /* --- error handling --- */
776 static void wdm_rxwork(struct work_struct *work)
778 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
783 spin_lock_irqsave(&desc->iuspin, flags);
784 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
785 spin_unlock_irqrestore(&desc->iuspin, flags);
787 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
788 spin_unlock_irqrestore(&desc->iuspin, flags);
790 rv = usb_submit_urb(desc->response, GFP_KERNEL);
791 if (rv < 0 && rv != -EPERM) {
792 spin_lock_irqsave(&desc->iuspin, flags);
793 clear_bit(WDM_RESPONDING, &desc->flags);
794 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
795 schedule_work(&desc->rxwork);
796 spin_unlock_irqrestore(&desc->iuspin, flags);
801 /* --- hotplug --- */
803 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
804 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
807 struct wdm_device *desc;
809 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
812 INIT_LIST_HEAD(&desc->device_list);
813 mutex_init(&desc->rlock);
814 mutex_init(&desc->wlock);
815 spin_lock_init(&desc->iuspin);
816 init_waitqueue_head(&desc->wait);
817 desc->wMaxCommand = bufsize;
818 /* this will be expanded and needed in hardware endianness */
819 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
821 INIT_WORK(&desc->rxwork, wdm_rxwork);
824 if (!usb_endpoint_is_int_in(ep))
827 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
829 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
832 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
836 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
840 desc->response = usb_alloc_urb(0, GFP_KERNEL);
844 desc->command = usb_alloc_urb(0, GFP_KERNEL);
848 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
852 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
856 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
862 interface_to_usbdev(intf),
863 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
865 desc->wMaxPacketSize,
871 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
872 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
873 desc->irq->wValue = 0;
874 desc->irq->wIndex = desc->inum; /* already converted */
875 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
877 usb_fill_control_urb(
879 interface_to_usbdev(intf),
880 /* using common endpoint 0 */
881 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
882 (unsigned char *)desc->irq,
889 desc->manage_power = manage_power;
891 spin_lock(&wdm_device_list_lock);
892 list_add(&desc->device_list, &wdm_device_list);
893 spin_unlock(&wdm_device_list_lock);
895 rv = usb_register_dev(intf, &wdm_class);
899 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
903 spin_lock(&wdm_device_list_lock);
904 list_del(&desc->device_list);
905 spin_unlock(&wdm_device_list_lock);
910 static int wdm_manage_power(struct usb_interface *intf, int on)
912 /* need autopm_get/put here to ensure the usbcore sees the new value */
913 int rv = usb_autopm_get_interface(intf);
915 intf->needs_remote_wakeup = on;
917 usb_autopm_put_interface(intf);
921 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
924 struct usb_host_interface *iface;
925 struct usb_endpoint_descriptor *ep;
926 struct usb_cdc_dmm_desc *dmhd;
927 u8 *buffer = intf->altsetting->extra;
928 int buflen = intf->altsetting->extralen;
929 u16 maxcom = WDM_DEFAULT_BUFSIZE;
934 if (buffer[1] != USB_DT_CS_INTERFACE) {
935 dev_err(&intf->dev, "skipping garbage\n");
940 case USB_CDC_HEADER_TYPE:
942 case USB_CDC_DMM_TYPE:
943 dmhd = (struct usb_cdc_dmm_desc *)buffer;
944 maxcom = le16_to_cpu(dmhd->wMaxCommand);
946 "Finding maximum buffer length: %d", maxcom);
950 "Ignoring extra header, type %d, length %d\n",
951 buffer[2], buffer[0]);
959 iface = intf->cur_altsetting;
960 if (iface->desc.bNumEndpoints != 1)
962 ep = &iface->endpoint[0].desc;
964 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
971 * usb_cdc_wdm_register - register a WDM subdriver
972 * @intf: usb interface the subdriver will associate with
973 * @ep: interrupt endpoint to monitor for notifications
974 * @bufsize: maximum message size to support for read/write
976 * Create WDM usb class character device and associate it with intf
977 * without binding, allowing another driver to manage the interface.
979 * The subdriver will manage the given interrupt endpoint exclusively
980 * and will issue control requests referring to the given intf. It
981 * will otherwise avoid interferring, and in particular not do
982 * usb_set_intfdata/usb_get_intfdata on intf.
984 * The return value is a pointer to the subdriver's struct usb_driver.
985 * The registering driver is responsible for calling this subdriver's
986 * disconnect, suspend, resume, pre_reset and post_reset methods from
989 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
990 struct usb_endpoint_descriptor *ep,
992 int (*manage_power)(struct usb_interface *, int))
996 rv = wdm_create(intf, ep, bufsize, manage_power);
1004 EXPORT_SYMBOL(usb_cdc_wdm_register);
1006 static void wdm_disconnect(struct usb_interface *intf)
1008 struct wdm_device *desc;
1009 unsigned long flags;
1011 usb_deregister_dev(intf, &wdm_class);
1012 desc = wdm_find_device(intf);
1013 mutex_lock(&wdm_mutex);
1015 /* the spinlock makes sure no new urbs are generated in the callbacks */
1016 spin_lock_irqsave(&desc->iuspin, flags);
1017 set_bit(WDM_DISCONNECTING, &desc->flags);
1018 set_bit(WDM_READ, &desc->flags);
1019 spin_unlock_irqrestore(&desc->iuspin, flags);
1020 wake_up_all(&desc->wait);
1021 mutex_lock(&desc->rlock);
1022 mutex_lock(&desc->wlock);
1024 cancel_work_sync(&desc->rxwork);
1025 mutex_unlock(&desc->wlock);
1026 mutex_unlock(&desc->rlock);
1028 /* the desc->intf pointer used as list key is now invalid */
1029 spin_lock(&wdm_device_list_lock);
1030 list_del(&desc->device_list);
1031 spin_unlock(&wdm_device_list_lock);
1036 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
1037 mutex_unlock(&wdm_mutex);
1041 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
1043 struct wdm_device *desc = wdm_find_device(intf);
1046 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1048 /* if this is an autosuspend the caller does the locking */
1049 if (!PMSG_IS_AUTO(message)) {
1050 mutex_lock(&desc->rlock);
1051 mutex_lock(&desc->wlock);
1053 spin_lock_irq(&desc->iuspin);
1055 if (PMSG_IS_AUTO(message) &&
1056 (test_bit(WDM_IN_USE, &desc->flags)
1057 || test_bit(WDM_RESPONDING, &desc->flags))) {
1058 spin_unlock_irq(&desc->iuspin);
1062 set_bit(WDM_SUSPENDING, &desc->flags);
1063 spin_unlock_irq(&desc->iuspin);
1064 /* callback submits work - order is essential */
1066 cancel_work_sync(&desc->rxwork);
1068 if (!PMSG_IS_AUTO(message)) {
1069 mutex_unlock(&desc->wlock);
1070 mutex_unlock(&desc->rlock);
1077 static int recover_from_urb_loss(struct wdm_device *desc)
1082 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1084 dev_err(&desc->intf->dev,
1085 "Error resume submitting int urb - %d\n", rv);
1091 static int wdm_resume(struct usb_interface *intf)
1093 struct wdm_device *desc = wdm_find_device(intf);
1096 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1098 clear_bit(WDM_SUSPENDING, &desc->flags);
1099 rv = recover_from_urb_loss(desc);
1105 static int wdm_pre_reset(struct usb_interface *intf)
1107 struct wdm_device *desc = wdm_find_device(intf);
1110 * we notify everybody using poll of
1111 * an exceptional situation
1112 * must be done before recovery lest a spontaneous
1113 * message from the device is lost
1115 spin_lock_irq(&desc->iuspin);
1116 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1117 set_bit(WDM_READ, &desc->flags); /* unblock read */
1118 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1119 desc->rerr = -EINTR;
1120 spin_unlock_irq(&desc->iuspin);
1121 wake_up_all(&desc->wait);
1122 mutex_lock(&desc->rlock);
1123 mutex_lock(&desc->wlock);
1125 cancel_work_sync(&desc->rxwork);
1129 static int wdm_post_reset(struct usb_interface *intf)
1131 struct wdm_device *desc = wdm_find_device(intf);
1134 clear_bit(WDM_OVERFLOW, &desc->flags);
1135 clear_bit(WDM_RESETTING, &desc->flags);
1136 rv = recover_from_urb_loss(desc);
1137 mutex_unlock(&desc->wlock);
1138 mutex_unlock(&desc->rlock);
1142 static struct usb_driver wdm_driver = {
1145 .disconnect = wdm_disconnect,
1147 .suspend = wdm_suspend,
1148 .resume = wdm_resume,
1149 .reset_resume = wdm_resume,
1151 .pre_reset = wdm_pre_reset,
1152 .post_reset = wdm_post_reset,
1153 .id_table = wdm_ids,
1154 .supports_autosuspend = 1,
1155 .disable_hub_initiated_lpm = 1,
1158 module_usb_driver(wdm_driver);
1160 MODULE_AUTHOR(DRIVER_AUTHOR);
1161 MODULE_DESCRIPTION(DRIVER_DESC);
1162 MODULE_LICENSE("GPL");