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 clear_bit(WDM_RESPONDING, &desc->flags);
748 spin_unlock_irq(&desc->iuspin);
749 desc->manage_power(desc->intf, 0);
751 /* must avoid dev_printk here as desc->intf is invalid */
752 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
756 mutex_unlock(&wdm_mutex);
760 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
762 struct wdm_device *desc = file->private_data;
766 case IOCTL_WDM_MAX_COMMAND:
767 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
776 static const struct file_operations wdm_fops = {
777 .owner = THIS_MODULE,
783 .release = wdm_release,
785 .unlocked_ioctl = wdm_ioctl,
786 .compat_ioctl = wdm_ioctl,
787 .llseek = noop_llseek,
790 static struct usb_class_driver wdm_class = {
793 .minor_base = WDM_MINOR_BASE,
796 /* --- error handling --- */
797 static void wdm_rxwork(struct work_struct *work)
799 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
804 spin_lock_irqsave(&desc->iuspin, flags);
805 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
806 spin_unlock_irqrestore(&desc->iuspin, flags);
808 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
809 spin_unlock_irqrestore(&desc->iuspin, flags);
811 rv = usb_submit_urb(desc->response, GFP_KERNEL);
812 if (rv < 0 && rv != -EPERM) {
813 spin_lock_irqsave(&desc->iuspin, flags);
814 clear_bit(WDM_RESPONDING, &desc->flags);
815 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
816 schedule_work(&desc->rxwork);
817 spin_unlock_irqrestore(&desc->iuspin, flags);
822 /* --- hotplug --- */
824 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
825 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
828 struct wdm_device *desc;
830 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
833 INIT_LIST_HEAD(&desc->device_list);
834 mutex_init(&desc->rlock);
835 mutex_init(&desc->wlock);
836 spin_lock_init(&desc->iuspin);
837 init_waitqueue_head(&desc->wait);
838 desc->wMaxCommand = bufsize;
839 /* this will be expanded and needed in hardware endianness */
840 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
842 INIT_WORK(&desc->rxwork, wdm_rxwork);
845 if (!usb_endpoint_is_int_in(ep))
848 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
850 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
853 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
857 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
861 desc->response = usb_alloc_urb(0, GFP_KERNEL);
865 desc->command = usb_alloc_urb(0, GFP_KERNEL);
869 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
873 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
877 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
883 interface_to_usbdev(intf),
884 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
886 desc->wMaxPacketSize,
892 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
893 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
894 desc->irq->wValue = 0;
895 desc->irq->wIndex = desc->inum; /* already converted */
896 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
898 usb_fill_control_urb(
900 interface_to_usbdev(intf),
901 /* using common endpoint 0 */
902 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
903 (unsigned char *)desc->irq,
910 desc->manage_power = manage_power;
912 spin_lock(&wdm_device_list_lock);
913 list_add(&desc->device_list, &wdm_device_list);
914 spin_unlock(&wdm_device_list_lock);
916 rv = usb_register_dev(intf, &wdm_class);
920 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
924 spin_lock(&wdm_device_list_lock);
925 list_del(&desc->device_list);
926 spin_unlock(&wdm_device_list_lock);
931 static int wdm_manage_power(struct usb_interface *intf, int on)
933 /* need autopm_get/put here to ensure the usbcore sees the new value */
934 int rv = usb_autopm_get_interface(intf);
936 intf->needs_remote_wakeup = on;
938 usb_autopm_put_interface(intf);
942 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
945 struct usb_host_interface *iface;
946 struct usb_endpoint_descriptor *ep;
947 struct usb_cdc_parsed_header hdr;
948 u8 *buffer = intf->altsetting->extra;
949 int buflen = intf->altsetting->extralen;
950 u16 maxcom = WDM_DEFAULT_BUFSIZE;
955 cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
957 if (hdr.usb_cdc_dmm_desc)
958 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
960 iface = intf->cur_altsetting;
961 if (iface->desc.bNumEndpoints != 1)
963 ep = &iface->endpoint[0].desc;
965 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
972 * usb_cdc_wdm_register - register a WDM subdriver
973 * @intf: usb interface the subdriver will associate with
974 * @ep: interrupt endpoint to monitor for notifications
975 * @bufsize: maximum message size to support for read/write
977 * Create WDM usb class character device and associate it with intf
978 * without binding, allowing another driver to manage the interface.
980 * The subdriver will manage the given interrupt endpoint exclusively
981 * and will issue control requests referring to the given intf. It
982 * will otherwise avoid interferring, and in particular not do
983 * usb_set_intfdata/usb_get_intfdata on intf.
985 * The return value is a pointer to the subdriver's struct usb_driver.
986 * The registering driver is responsible for calling this subdriver's
987 * disconnect, suspend, resume, pre_reset and post_reset methods from
990 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
991 struct usb_endpoint_descriptor *ep,
993 int (*manage_power)(struct usb_interface *, int))
997 rv = wdm_create(intf, ep, bufsize, manage_power);
1005 EXPORT_SYMBOL(usb_cdc_wdm_register);
1007 static void wdm_disconnect(struct usb_interface *intf)
1009 struct wdm_device *desc;
1010 unsigned long flags;
1012 usb_deregister_dev(intf, &wdm_class);
1013 desc = wdm_find_device(intf);
1014 mutex_lock(&wdm_mutex);
1016 /* the spinlock makes sure no new urbs are generated in the callbacks */
1017 spin_lock_irqsave(&desc->iuspin, flags);
1018 set_bit(WDM_DISCONNECTING, &desc->flags);
1019 set_bit(WDM_READ, &desc->flags);
1020 spin_unlock_irqrestore(&desc->iuspin, flags);
1021 wake_up_all(&desc->wait);
1022 mutex_lock(&desc->rlock);
1023 mutex_lock(&desc->wlock);
1025 cancel_work_sync(&desc->rxwork);
1026 mutex_unlock(&desc->wlock);
1027 mutex_unlock(&desc->rlock);
1029 /* the desc->intf pointer used as list key is now invalid */
1030 spin_lock(&wdm_device_list_lock);
1031 list_del(&desc->device_list);
1032 spin_unlock(&wdm_device_list_lock);
1037 dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
1038 mutex_unlock(&wdm_mutex);
1042 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
1044 struct wdm_device *desc = wdm_find_device(intf);
1047 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1049 /* if this is an autosuspend the caller does the locking */
1050 if (!PMSG_IS_AUTO(message)) {
1051 mutex_lock(&desc->rlock);
1052 mutex_lock(&desc->wlock);
1054 spin_lock_irq(&desc->iuspin);
1056 if (PMSG_IS_AUTO(message) &&
1057 (test_bit(WDM_IN_USE, &desc->flags)
1058 || test_bit(WDM_RESPONDING, &desc->flags))) {
1059 spin_unlock_irq(&desc->iuspin);
1063 set_bit(WDM_SUSPENDING, &desc->flags);
1064 spin_unlock_irq(&desc->iuspin);
1065 /* callback submits work - order is essential */
1067 cancel_work_sync(&desc->rxwork);
1069 if (!PMSG_IS_AUTO(message)) {
1070 mutex_unlock(&desc->wlock);
1071 mutex_unlock(&desc->rlock);
1078 static int recover_from_urb_loss(struct wdm_device *desc)
1083 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1085 dev_err(&desc->intf->dev,
1086 "Error resume submitting int urb - %d\n", rv);
1092 static int wdm_resume(struct usb_interface *intf)
1094 struct wdm_device *desc = wdm_find_device(intf);
1097 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1099 clear_bit(WDM_SUSPENDING, &desc->flags);
1100 rv = recover_from_urb_loss(desc);
1106 static int wdm_pre_reset(struct usb_interface *intf)
1108 struct wdm_device *desc = wdm_find_device(intf);
1111 * we notify everybody using poll of
1112 * an exceptional situation
1113 * must be done before recovery lest a spontaneous
1114 * message from the device is lost
1116 spin_lock_irq(&desc->iuspin);
1117 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1118 set_bit(WDM_READ, &desc->flags); /* unblock read */
1119 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1120 desc->rerr = -EINTR;
1121 spin_unlock_irq(&desc->iuspin);
1122 wake_up_all(&desc->wait);
1123 mutex_lock(&desc->rlock);
1124 mutex_lock(&desc->wlock);
1126 cancel_work_sync(&desc->rxwork);
1130 static int wdm_post_reset(struct usb_interface *intf)
1132 struct wdm_device *desc = wdm_find_device(intf);
1135 clear_bit(WDM_OVERFLOW, &desc->flags);
1136 clear_bit(WDM_RESETTING, &desc->flags);
1137 rv = recover_from_urb_loss(desc);
1138 mutex_unlock(&desc->wlock);
1139 mutex_unlock(&desc->rlock);
1143 static struct usb_driver wdm_driver = {
1146 .disconnect = wdm_disconnect,
1148 .suspend = wdm_suspend,
1149 .resume = wdm_resume,
1150 .reset_resume = wdm_resume,
1152 .pre_reset = wdm_pre_reset,
1153 .post_reset = wdm_post_reset,
1154 .id_table = wdm_ids,
1155 .supports_autosuspend = 1,
1156 .disable_hub_initiated_lpm = 1,
1159 module_usb_driver(wdm_driver);
1161 MODULE_AUTHOR(DRIVER_AUTHOR);
1162 MODULE_DESCRIPTION(DRIVER_DESC);
1163 MODULE_LICENSE("GPL");