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 /* forward declaration */
161 static int service_outstanding_interrupt(struct wdm_device *desc);
163 static void wdm_in_callback(struct urb *urb)
165 struct wdm_device *desc = urb->context;
166 int status = urb->status;
167 int length = urb->actual_length;
169 spin_lock(&desc->iuspin);
170 clear_bit(WDM_RESPONDING, &desc->flags);
175 dev_dbg(&desc->intf->dev,
176 "nonzero urb status received: -ENOENT\n");
179 dev_dbg(&desc->intf->dev,
180 "nonzero urb status received: -ECONNRESET\n");
183 dev_dbg(&desc->intf->dev,
184 "nonzero urb status received: -ESHUTDOWN\n");
187 dev_err(&desc->intf->dev,
188 "nonzero urb status received: -EPIPE\n");
191 dev_err(&desc->intf->dev,
192 "Unexpected error %d\n", status);
198 * only set a new error if there is no previous error.
199 * Errors are only cleared during read/open
200 * Avoid propagating -EPIPE (stall) to userspace since it is
201 * better handled as an empty read
203 if (desc->rerr == 0 && status != -EPIPE)
206 if (length + desc->length > desc->wMaxCommand) {
207 /* The buffer would overflow */
208 set_bit(WDM_OVERFLOW, &desc->flags);
210 /* we may already be in overflow */
211 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
212 memmove(desc->ubuf + desc->length, desc->inbuf, length);
213 desc->length += length;
214 desc->reslength = length;
218 set_bit(WDM_READ, &desc->flags);
219 wake_up(&desc->wait);
223 * Since there was an error, userspace may decide to not read
224 * any data after poll'ing.
225 * We should respond to further attempts from the device to send
226 * data, so that we can get unstuck.
228 service_outstanding_interrupt(desc);
231 spin_unlock(&desc->iuspin);
234 static void wdm_int_callback(struct urb *urb)
238 int status = urb->status;
239 struct wdm_device *desc;
240 struct usb_cdc_notification *dr;
243 dr = (struct usb_cdc_notification *)desc->sbuf;
252 set_bit(WDM_INT_STALL, &desc->flags);
253 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
254 goto sw; /* halt is cleared in work */
256 dev_err(&desc->intf->dev,
257 "nonzero urb status received: %d\n", status);
262 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
263 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
268 switch (dr->bNotificationType) {
269 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
270 dev_dbg(&desc->intf->dev,
271 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
272 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
275 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
277 dev_dbg(&desc->intf->dev,
278 "NOTIFY_NETWORK_CONNECTION %s network\n",
279 dr->wValue ? "connected to" : "disconnected from");
281 case USB_CDC_NOTIFY_SPEED_CHANGE:
282 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
286 clear_bit(WDM_POLL_RUNNING, &desc->flags);
287 dev_err(&desc->intf->dev,
288 "unknown notification %d received: index %d len %d\n",
289 dr->bNotificationType,
290 le16_to_cpu(dr->wIndex),
291 le16_to_cpu(dr->wLength));
295 spin_lock(&desc->iuspin);
296 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
297 if (!desc->resp_count++ && !responding
298 && !test_bit(WDM_DISCONNECTING, &desc->flags)
299 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
300 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
301 dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
303 spin_unlock(&desc->iuspin);
305 clear_bit(WDM_RESPONDING, &desc->flags);
310 rv = schedule_work(&desc->rxwork);
312 dev_err(&desc->intf->dev,
313 "Cannot schedule work\n");
317 rv = usb_submit_urb(urb, GFP_ATOMIC);
319 dev_err(&desc->intf->dev,
320 "%s - usb_submit_urb failed with result %d\n",
325 static void kill_urbs(struct wdm_device *desc)
327 /* the order here is essential */
328 usb_kill_urb(desc->command);
329 usb_kill_urb(desc->validity);
330 usb_kill_urb(desc->response);
333 static void free_urbs(struct wdm_device *desc)
335 usb_free_urb(desc->validity);
336 usb_free_urb(desc->response);
337 usb_free_urb(desc->command);
340 static void cleanup(struct wdm_device *desc)
351 static ssize_t wdm_write
352 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
355 int rv = -EMSGSIZE, r, we;
356 struct wdm_device *desc = file->private_data;
357 struct usb_ctrlrequest *req;
359 if (count > desc->wMaxCommand)
360 count = desc->wMaxCommand;
362 spin_lock_irq(&desc->iuspin);
365 spin_unlock_irq(&desc->iuspin);
367 return usb_translate_errors(we);
369 buf = kmalloc(count, GFP_KERNEL);
375 r = copy_from_user(buf, buffer, count);
381 /* concurrent writes and disconnect */
382 r = mutex_lock_interruptible(&desc->wlock);
387 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
389 goto out_free_mem_lock;
392 r = usb_autopm_get_interface(desc->intf);
394 rv = usb_translate_errors(r);
395 goto out_free_mem_lock;
398 if (!(file->f_flags & O_NONBLOCK))
399 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
402 if (test_bit(WDM_IN_USE, &desc->flags))
405 if (test_bit(WDM_RESETTING, &desc->flags))
408 if (test_bit(WDM_DISCONNECTING, &desc->flags))
413 goto out_free_mem_pm;
417 usb_fill_control_urb(
419 interface_to_usbdev(desc->intf),
420 /* using common endpoint 0 */
421 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
422 (unsigned char *)req,
429 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
430 USB_RECIP_INTERFACE);
431 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
433 req->wIndex = desc->inum; /* already converted */
434 req->wLength = cpu_to_le16(count);
435 set_bit(WDM_IN_USE, &desc->flags);
438 rv = usb_submit_urb(desc->command, GFP_KERNEL);
441 clear_bit(WDM_IN_USE, &desc->flags);
442 wake_up_all(&desc->wait); /* for wdm_wait_for_response() */
443 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
444 rv = usb_translate_errors(rv);
445 goto out_free_mem_pm;
447 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
448 le16_to_cpu(req->wIndex));
451 usb_autopm_put_interface(desc->intf);
452 mutex_unlock(&desc->wlock);
454 return rv < 0 ? rv : count;
457 usb_autopm_put_interface(desc->intf);
459 mutex_unlock(&desc->wlock);
466 * Submit the read urb if resp_count is non-zero.
468 * Called with desc->iuspin locked
470 static int service_outstanding_interrupt(struct wdm_device *desc)
474 /* submit read urb only if the device is waiting for it */
475 if (!desc->resp_count || !--desc->resp_count)
478 set_bit(WDM_RESPONDING, &desc->flags);
479 spin_unlock_irq(&desc->iuspin);
480 rv = usb_submit_urb(desc->response, GFP_KERNEL);
481 spin_lock_irq(&desc->iuspin);
483 dev_err(&desc->intf->dev,
484 "usb_submit_urb failed with result %d\n", rv);
486 /* make sure the next notification trigger a submit */
487 clear_bit(WDM_RESPONDING, &desc->flags);
488 desc->resp_count = 0;
494 static ssize_t wdm_read
495 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
499 struct wdm_device *desc = file->private_data;
502 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
506 cntr = ACCESS_ONCE(desc->length);
510 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
514 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
515 clear_bit(WDM_OVERFLOW, &desc->flags);
520 if (file->f_flags & O_NONBLOCK) {
521 if (!test_bit(WDM_READ, &desc->flags)) {
522 rv = cntr ? cntr : -EAGAIN;
527 rv = wait_event_interruptible(desc->wait,
528 test_bit(WDM_READ, &desc->flags));
531 /* may have happened while we slept */
532 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
536 if (test_bit(WDM_RESETTING, &desc->flags)) {
540 usb_mark_last_busy(interface_to_usbdev(desc->intf));
546 spin_lock_irq(&desc->iuspin);
548 if (desc->rerr) { /* read completed, error happened */
549 rv = usb_translate_errors(desc->rerr);
551 spin_unlock_irq(&desc->iuspin);
555 * recheck whether we've lost the race
556 * against the completion handler
558 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
559 spin_unlock_irq(&desc->iuspin);
563 if (!desc->reslength) { /* zero length read */
564 dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
565 clear_bit(WDM_READ, &desc->flags);
566 rv = service_outstanding_interrupt(desc);
567 spin_unlock_irq(&desc->iuspin);
573 spin_unlock_irq(&desc->iuspin);
578 rv = copy_to_user(buffer, desc->ubuf, cntr);
584 spin_lock_irq(&desc->iuspin);
586 for (i = 0; i < desc->length - cntr; i++)
587 desc->ubuf[i] = desc->ubuf[i + cntr];
589 desc->length -= cntr;
590 /* in case we had outstanding data */
592 clear_bit(WDM_READ, &desc->flags);
593 service_outstanding_interrupt(desc);
595 spin_unlock_irq(&desc->iuspin);
599 mutex_unlock(&desc->rlock);
603 static int wdm_wait_for_response(struct file *file, long timeout)
605 struct wdm_device *desc = file->private_data;
606 long rv; /* Use long here because (int) MAX_SCHEDULE_TIMEOUT < 0. */
609 * Needs both flags. We cannot do with one because resetting it would
610 * cause a race with write() yet we need to signal a disconnect.
612 rv = wait_event_interruptible_timeout(desc->wait,
613 !test_bit(WDM_IN_USE, &desc->flags) ||
614 test_bit(WDM_DISCONNECTING, &desc->flags),
618 * To report the correct error. This is best effort.
619 * We are inevitably racing with the hardware.
621 if (test_bit(WDM_DISCONNECTING, &desc->flags))
628 spin_lock_irq(&desc->iuspin);
631 spin_unlock_irq(&desc->iuspin);
633 return usb_translate_errors(rv);
638 * You need to send a signal when you react to malicious or defective hardware.
639 * Also, don't abort when fsync() returned -EINVAL, for older kernels which do
640 * not implement wdm_flush() will return -EINVAL.
642 static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
644 return wdm_wait_for_response(file, MAX_SCHEDULE_TIMEOUT);
648 * Same with wdm_fsync(), except it uses finite timeout in order to react to
649 * malicious or defective hardware which ceased communication after close() was
650 * implicitly called due to process termination.
652 static int wdm_flush(struct file *file, fl_owner_t id)
654 return wdm_wait_for_response(file, WDM_FLUSH_TIMEOUT);
657 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
659 struct wdm_device *desc = file->private_data;
661 unsigned int mask = 0;
663 spin_lock_irqsave(&desc->iuspin, flags);
664 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
665 mask = POLLHUP | POLLERR;
666 spin_unlock_irqrestore(&desc->iuspin, flags);
669 if (test_bit(WDM_READ, &desc->flags))
670 mask = POLLIN | POLLRDNORM;
671 if (desc->rerr || desc->werr)
673 if (!test_bit(WDM_IN_USE, &desc->flags))
674 mask |= POLLOUT | POLLWRNORM;
675 spin_unlock_irqrestore(&desc->iuspin, flags);
677 poll_wait(file, &desc->wait, wait);
683 static int wdm_open(struct inode *inode, struct file *file)
685 int minor = iminor(inode);
687 struct usb_interface *intf;
688 struct wdm_device *desc;
690 mutex_lock(&wdm_mutex);
691 desc = wdm_find_device_by_minor(minor);
696 if (test_bit(WDM_DISCONNECTING, &desc->flags))
698 file->private_data = desc;
700 rv = usb_autopm_get_interface(desc->intf);
702 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
706 /* using write lock to protect desc->count */
707 mutex_lock(&desc->wlock);
708 if (!desc->count++) {
711 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
714 dev_err(&desc->intf->dev,
715 "Error submitting int urb - %d\n", rv);
716 rv = usb_translate_errors(rv);
721 mutex_unlock(&desc->wlock);
722 if (desc->count == 1)
723 desc->manage_power(intf, 1);
724 usb_autopm_put_interface(desc->intf);
726 mutex_unlock(&wdm_mutex);
730 static int wdm_release(struct inode *inode, struct file *file)
732 struct wdm_device *desc = file->private_data;
734 mutex_lock(&wdm_mutex);
736 /* using write lock to protect desc->count */
737 mutex_lock(&desc->wlock);
739 mutex_unlock(&desc->wlock);
742 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
743 dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
745 spin_lock_irq(&desc->iuspin);
746 desc->resp_count = 0;
747 spin_unlock_irq(&desc->iuspin);
748 desc->manage_power(desc->intf, 0);
750 /* must avoid dev_printk here as desc->intf is invalid */
751 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
755 mutex_unlock(&wdm_mutex);
759 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
761 struct wdm_device *desc = file->private_data;
765 case IOCTL_WDM_MAX_COMMAND:
766 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
775 static const struct file_operations wdm_fops = {
776 .owner = THIS_MODULE,
782 .release = wdm_release,
784 .unlocked_ioctl = wdm_ioctl,
785 .compat_ioctl = wdm_ioctl,
786 .llseek = noop_llseek,
789 static struct usb_class_driver wdm_class = {
792 .minor_base = WDM_MINOR_BASE,
795 /* --- error handling --- */
796 static void wdm_rxwork(struct work_struct *work)
798 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
803 spin_lock_irqsave(&desc->iuspin, flags);
804 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
805 spin_unlock_irqrestore(&desc->iuspin, flags);
807 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
808 spin_unlock_irqrestore(&desc->iuspin, flags);
810 rv = usb_submit_urb(desc->response, GFP_KERNEL);
811 if (rv < 0 && rv != -EPERM) {
812 spin_lock_irqsave(&desc->iuspin, flags);
813 clear_bit(WDM_RESPONDING, &desc->flags);
814 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
815 schedule_work(&desc->rxwork);
816 spin_unlock_irqrestore(&desc->iuspin, flags);
821 /* --- hotplug --- */
823 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
824 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
827 struct wdm_device *desc;
829 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
832 INIT_LIST_HEAD(&desc->device_list);
833 mutex_init(&desc->rlock);
834 mutex_init(&desc->wlock);
835 spin_lock_init(&desc->iuspin);
836 init_waitqueue_head(&desc->wait);
837 desc->wMaxCommand = bufsize;
838 /* this will be expanded and needed in hardware endianness */
839 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
841 INIT_WORK(&desc->rxwork, wdm_rxwork);
844 if (!usb_endpoint_is_int_in(ep))
847 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
849 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
852 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
856 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
860 desc->response = usb_alloc_urb(0, GFP_KERNEL);
864 desc->command = usb_alloc_urb(0, GFP_KERNEL);
868 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
872 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
876 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
882 interface_to_usbdev(intf),
883 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
885 desc->wMaxPacketSize,
891 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
892 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
893 desc->irq->wValue = 0;
894 desc->irq->wIndex = desc->inum; /* already converted */
895 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
897 usb_fill_control_urb(
899 interface_to_usbdev(intf),
900 /* using common endpoint 0 */
901 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
902 (unsigned char *)desc->irq,
909 desc->manage_power = manage_power;
911 spin_lock(&wdm_device_list_lock);
912 list_add(&desc->device_list, &wdm_device_list);
913 spin_unlock(&wdm_device_list_lock);
915 rv = usb_register_dev(intf, &wdm_class);
919 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
923 spin_lock(&wdm_device_list_lock);
924 list_del(&desc->device_list);
925 spin_unlock(&wdm_device_list_lock);
930 static int wdm_manage_power(struct usb_interface *intf, int on)
932 /* need autopm_get/put here to ensure the usbcore sees the new value */
933 int rv = usb_autopm_get_interface(intf);
935 intf->needs_remote_wakeup = on;
937 usb_autopm_put_interface(intf);
941 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
944 struct usb_host_interface *iface;
945 struct usb_endpoint_descriptor *ep;
946 struct usb_cdc_parsed_header hdr;
947 u8 *buffer = intf->altsetting->extra;
948 int buflen = intf->altsetting->extralen;
949 u16 maxcom = WDM_DEFAULT_BUFSIZE;
954 cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
956 if (hdr.usb_cdc_dmm_desc)
957 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
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, "%d open files - postponing cleanup\n", 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");