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
61 #define WDM_DRAIN_ON_OPEN 11
65 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
66 #define WDM_DEFAULT_BUFSIZE 256
68 static DEFINE_MUTEX(wdm_mutex);
69 static DEFINE_SPINLOCK(wdm_device_list_lock);
70 static LIST_HEAD(wdm_device_list);
72 /* --- method tables --- */
75 u8 *inbuf; /* buffer for response */
76 u8 *outbuf; /* buffer for command */
77 u8 *sbuf; /* buffer for status */
78 u8 *ubuf; /* buffer for copy to user space */
83 struct usb_interface *intf;
84 struct usb_ctrlrequest *orq;
85 struct usb_ctrlrequest *irq;
101 wait_queue_head_t wait;
102 struct work_struct rxwork;
107 struct list_head device_list;
108 int (*manage_power)(struct usb_interface *, int);
111 static struct usb_driver wdm_driver;
113 /* return intfdata if we own the interface, else look up intf in the list */
114 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
116 struct wdm_device *desc;
118 spin_lock(&wdm_device_list_lock);
119 list_for_each_entry(desc, &wdm_device_list, device_list)
120 if (desc->intf == intf)
124 spin_unlock(&wdm_device_list_lock);
129 static struct wdm_device *wdm_find_device_by_minor(int minor)
131 struct wdm_device *desc;
133 spin_lock(&wdm_device_list_lock);
134 list_for_each_entry(desc, &wdm_device_list, device_list)
135 if (desc->intf->minor == minor)
139 spin_unlock(&wdm_device_list_lock);
144 /* --- callbacks --- */
145 static void wdm_out_callback(struct urb *urb)
147 struct wdm_device *desc;
149 spin_lock(&desc->iuspin);
150 desc->werr = urb->status;
151 spin_unlock(&desc->iuspin);
154 clear_bit(WDM_IN_USE, &desc->flags);
155 wake_up(&desc->wait);
158 static void wdm_in_callback(struct urb *urb)
160 struct wdm_device *desc = urb->context;
161 int status = urb->status;
162 int length = urb->actual_length;
164 spin_lock(&desc->iuspin);
165 clear_bit(WDM_RESPONDING, &desc->flags);
170 dev_dbg(&desc->intf->dev,
171 "nonzero urb status received: -ENOENT");
174 dev_dbg(&desc->intf->dev,
175 "nonzero urb status received: -ECONNRESET");
178 dev_dbg(&desc->intf->dev,
179 "nonzero urb status received: -ESHUTDOWN");
182 dev_dbg(&desc->intf->dev,
183 "nonzero urb status received: -EPIPE\n");
186 dev_err(&desc->intf->dev,
187 "Unexpected error %d\n", status);
193 if (length + desc->length > desc->wMaxCommand) {
194 /* The buffer would overflow */
195 set_bit(WDM_OVERFLOW, &desc->flags);
197 /* we may already be in overflow */
198 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
199 memmove(desc->ubuf + desc->length, desc->inbuf, length);
200 desc->length += length;
201 desc->reslength = length;
206 * Handling devices with the WDM_DRAIN_ON_OPEN flag set:
207 * If desc->resp_count is unset, then the urb was submitted
208 * without a prior notification. If the device returned any
209 * data, then this implies that it had messages queued without
210 * notifying us. Continue reading until that queue is flushed.
212 if (!desc->resp_count) {
214 /* do not propagate the expected -EPIPE */
218 dev_dbg(&desc->intf->dev, "got %d bytes without notification\n", length);
219 set_bit(WDM_RESPONDING, &desc->flags);
220 usb_submit_urb(desc->response, GFP_ATOMIC);
224 wake_up(&desc->wait);
226 set_bit(WDM_READ, &desc->flags);
228 spin_unlock(&desc->iuspin);
231 static void wdm_int_callback(struct urb *urb)
235 int status = urb->status;
236 struct wdm_device *desc;
237 struct usb_cdc_notification *dr;
240 dr = (struct usb_cdc_notification *)desc->sbuf;
249 set_bit(WDM_INT_STALL, &desc->flags);
250 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
251 goto sw; /* halt is cleared in work */
253 dev_err(&desc->intf->dev,
254 "nonzero urb status received: %d\n", status);
259 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
260 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
265 switch (dr->bNotificationType) {
266 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
267 dev_dbg(&desc->intf->dev,
268 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
269 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
272 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
274 dev_dbg(&desc->intf->dev,
275 "NOTIFY_NETWORK_CONNECTION %s network",
276 dr->wValue ? "connected to" : "disconnected from");
278 case USB_CDC_NOTIFY_SPEED_CHANGE:
279 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)",
283 clear_bit(WDM_POLL_RUNNING, &desc->flags);
284 dev_err(&desc->intf->dev,
285 "unknown notification %d received: index %d len %d\n",
286 dr->bNotificationType,
287 le16_to_cpu(dr->wIndex),
288 le16_to_cpu(dr->wLength));
292 spin_lock(&desc->iuspin);
293 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
294 if (!desc->resp_count++ && !responding
295 && !test_bit(WDM_DISCONNECTING, &desc->flags)
296 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
297 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
298 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
301 spin_unlock(&desc->iuspin);
303 clear_bit(WDM_RESPONDING, &desc->flags);
308 rv = schedule_work(&desc->rxwork);
310 dev_err(&desc->intf->dev,
311 "Cannot schedule work\n");
315 rv = usb_submit_urb(urb, GFP_ATOMIC);
317 dev_err(&desc->intf->dev,
318 "%s - usb_submit_urb failed with result %d\n",
323 static void kill_urbs(struct wdm_device *desc)
325 /* the order here is essential */
326 usb_kill_urb(desc->command);
327 usb_kill_urb(desc->validity);
328 usb_kill_urb(desc->response);
331 static void free_urbs(struct wdm_device *desc)
333 usb_free_urb(desc->validity);
334 usb_free_urb(desc->response);
335 usb_free_urb(desc->command);
338 static void cleanup(struct wdm_device *desc)
349 static ssize_t wdm_write
350 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
353 int rv = -EMSGSIZE, r, we;
354 struct wdm_device *desc = file->private_data;
355 struct usb_ctrlrequest *req;
357 if (count > desc->wMaxCommand)
358 count = desc->wMaxCommand;
360 spin_lock_irq(&desc->iuspin);
363 spin_unlock_irq(&desc->iuspin);
365 return usb_translate_errors(we);
367 buf = kmalloc(count, GFP_KERNEL);
373 r = copy_from_user(buf, buffer, count);
379 /* concurrent writes and disconnect */
380 r = mutex_lock_interruptible(&desc->wlock);
385 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
387 goto out_free_mem_lock;
390 r = usb_autopm_get_interface(desc->intf);
392 rv = usb_translate_errors(r);
393 goto out_free_mem_lock;
396 if (!(file->f_flags & O_NONBLOCK))
397 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
400 if (test_bit(WDM_IN_USE, &desc->flags))
403 if (test_bit(WDM_RESETTING, &desc->flags))
408 goto out_free_mem_pm;
412 usb_fill_control_urb(
414 interface_to_usbdev(desc->intf),
415 /* using common endpoint 0 */
416 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
417 (unsigned char *)req,
424 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
425 USB_RECIP_INTERFACE);
426 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
428 req->wIndex = desc->inum; /* already converted */
429 req->wLength = cpu_to_le16(count);
430 set_bit(WDM_IN_USE, &desc->flags);
433 rv = usb_submit_urb(desc->command, GFP_KERNEL);
436 clear_bit(WDM_IN_USE, &desc->flags);
437 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
438 rv = usb_translate_errors(rv);
439 goto out_free_mem_pm;
441 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
442 le16_to_cpu(req->wIndex));
445 usb_autopm_put_interface(desc->intf);
446 mutex_unlock(&desc->wlock);
448 return rv < 0 ? rv : count;
451 usb_autopm_put_interface(desc->intf);
453 mutex_unlock(&desc->wlock);
460 * clear WDM_READ flag and possibly submit the read urb if resp_count
463 * Called with desc->iuspin locked
465 static int clear_wdm_read_flag(struct wdm_device *desc)
469 clear_bit(WDM_READ, &desc->flags);
471 /* submit read urb only if the device is waiting for it */
472 if (!desc->resp_count || !--desc->resp_count)
475 set_bit(WDM_RESPONDING, &desc->flags);
476 spin_unlock_irq(&desc->iuspin);
477 rv = usb_submit_urb(desc->response, GFP_KERNEL);
478 spin_lock_irq(&desc->iuspin);
480 dev_err(&desc->intf->dev,
481 "usb_submit_urb failed with result %d\n", rv);
483 /* make sure the next notification trigger a submit */
484 clear_bit(WDM_RESPONDING, &desc->flags);
485 desc->resp_count = 0;
491 static ssize_t wdm_read
492 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
496 struct wdm_device *desc = file->private_data;
499 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
503 cntr = ACCESS_ONCE(desc->length);
507 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
511 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
512 clear_bit(WDM_OVERFLOW, &desc->flags);
517 if (file->f_flags & O_NONBLOCK) {
518 if (!test_bit(WDM_READ, &desc->flags)) {
519 rv = cntr ? cntr : -EAGAIN;
524 rv = wait_event_interruptible(desc->wait,
525 test_bit(WDM_READ, &desc->flags));
528 /* may have happened while we slept */
529 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
533 if (test_bit(WDM_RESETTING, &desc->flags)) {
537 usb_mark_last_busy(interface_to_usbdev(desc->intf));
543 spin_lock_irq(&desc->iuspin);
545 if (desc->rerr) { /* read completed, error happened */
546 rv = usb_translate_errors(desc->rerr);
548 spin_unlock_irq(&desc->iuspin);
552 * recheck whether we've lost the race
553 * against the completion handler
555 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
556 spin_unlock_irq(&desc->iuspin);
560 if (!desc->reslength) { /* zero length read */
561 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
562 rv = clear_wdm_read_flag(desc);
563 spin_unlock_irq(&desc->iuspin);
569 spin_unlock_irq(&desc->iuspin);
574 rv = copy_to_user(buffer, desc->ubuf, cntr);
580 spin_lock_irq(&desc->iuspin);
582 for (i = 0; i < desc->length - cntr; i++)
583 desc->ubuf[i] = desc->ubuf[i + cntr];
585 desc->length -= cntr;
586 /* in case we had outstanding data */
588 clear_wdm_read_flag(desc);
589 spin_unlock_irq(&desc->iuspin);
593 mutex_unlock(&desc->rlock);
597 static int wdm_flush(struct file *file, fl_owner_t id)
599 struct wdm_device *desc = file->private_data;
601 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
603 /* cannot dereference desc->intf if WDM_DISCONNECTING */
604 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
605 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
608 return usb_translate_errors(desc->werr);
611 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
613 struct wdm_device *desc = file->private_data;
615 unsigned int mask = 0;
617 spin_lock_irqsave(&desc->iuspin, flags);
618 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
619 mask = POLLHUP | POLLERR;
620 spin_unlock_irqrestore(&desc->iuspin, flags);
623 if (test_bit(WDM_READ, &desc->flags))
624 mask = POLLIN | POLLRDNORM;
625 if (desc->rerr || desc->werr)
627 if (!test_bit(WDM_IN_USE, &desc->flags))
628 mask |= POLLOUT | POLLWRNORM;
629 spin_unlock_irqrestore(&desc->iuspin, flags);
631 poll_wait(file, &desc->wait, wait);
637 static int wdm_open(struct inode *inode, struct file *file)
639 int minor = iminor(inode);
641 struct usb_interface *intf;
642 struct wdm_device *desc;
644 mutex_lock(&wdm_mutex);
645 desc = wdm_find_device_by_minor(minor);
650 if (test_bit(WDM_DISCONNECTING, &desc->flags))
652 file->private_data = desc;
654 rv = usb_autopm_get_interface(desc->intf);
656 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
660 /* using write lock to protect desc->count */
661 mutex_lock(&desc->wlock);
662 if (!desc->count++) {
665 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
668 dev_err(&desc->intf->dev,
669 "Error submitting int urb - %d\n", rv);
670 rv = usb_translate_errors(rv);
671 } else if (test_bit(WDM_DRAIN_ON_OPEN, &desc->flags)) {
673 * Some devices keep pending messages queued
674 * without resending notifications. We must
675 * flush the message queue before we can
676 * assume a one-to-one relationship between
677 * notifications and messages in the queue
679 dev_dbg(&desc->intf->dev, "draining queued data\n");
680 set_bit(WDM_RESPONDING, &desc->flags);
681 rv = usb_submit_urb(desc->response, GFP_KERNEL);
686 mutex_unlock(&desc->wlock);
687 if (desc->count == 1)
688 desc->manage_power(intf, 1);
689 usb_autopm_put_interface(desc->intf);
691 mutex_unlock(&wdm_mutex);
695 static int wdm_release(struct inode *inode, struct file *file)
697 struct wdm_device *desc = file->private_data;
699 mutex_lock(&wdm_mutex);
701 /* using write lock to protect desc->count */
702 mutex_lock(&desc->wlock);
704 mutex_unlock(&desc->wlock);
707 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
708 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
710 spin_lock_irq(&desc->iuspin);
711 desc->resp_count = 0;
712 spin_unlock_irq(&desc->iuspin);
713 desc->manage_power(desc->intf, 0);
715 /* must avoid dev_printk here as desc->intf is invalid */
716 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
720 mutex_unlock(&wdm_mutex);
724 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
726 struct wdm_device *desc = file->private_data;
730 case IOCTL_WDM_MAX_COMMAND:
731 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
740 static const struct file_operations wdm_fops = {
741 .owner = THIS_MODULE,
746 .release = wdm_release,
748 .unlocked_ioctl = wdm_ioctl,
749 .compat_ioctl = wdm_ioctl,
750 .llseek = noop_llseek,
753 static struct usb_class_driver wdm_class = {
756 .minor_base = WDM_MINOR_BASE,
759 /* --- error handling --- */
760 static void wdm_rxwork(struct work_struct *work)
762 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
767 spin_lock_irqsave(&desc->iuspin, flags);
768 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
769 spin_unlock_irqrestore(&desc->iuspin, flags);
771 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
772 spin_unlock_irqrestore(&desc->iuspin, flags);
774 rv = usb_submit_urb(desc->response, GFP_KERNEL);
775 if (rv < 0 && rv != -EPERM) {
776 spin_lock_irqsave(&desc->iuspin, flags);
777 clear_bit(WDM_RESPONDING, &desc->flags);
778 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
779 schedule_work(&desc->rxwork);
780 spin_unlock_irqrestore(&desc->iuspin, flags);
785 /* --- hotplug --- */
787 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
788 u16 bufsize, int (*manage_power)(struct usb_interface *, int),
792 struct wdm_device *desc;
794 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
797 INIT_LIST_HEAD(&desc->device_list);
798 mutex_init(&desc->rlock);
799 mutex_init(&desc->wlock);
800 spin_lock_init(&desc->iuspin);
801 init_waitqueue_head(&desc->wait);
802 desc->wMaxCommand = bufsize;
803 /* this will be expanded and needed in hardware endianness */
804 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
806 INIT_WORK(&desc->rxwork, wdm_rxwork);
809 if (!usb_endpoint_is_int_in(ep))
812 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
814 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
817 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
821 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
825 desc->response = usb_alloc_urb(0, GFP_KERNEL);
829 desc->command = usb_alloc_urb(0, GFP_KERNEL);
833 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
837 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
841 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
847 interface_to_usbdev(intf),
848 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
850 desc->wMaxPacketSize,
856 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
857 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
858 desc->irq->wValue = 0;
859 desc->irq->wIndex = desc->inum; /* already converted */
860 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
862 usb_fill_control_urb(
864 interface_to_usbdev(intf),
865 /* using common endpoint 0 */
866 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
867 (unsigned char *)desc->irq,
874 desc->manage_power = manage_power;
877 * "drain_on_open" enables a hack to work around a firmware
878 * issue observed on network functions, in particular MBIM
881 * Quoting section 7 of the CDC-WMC r1.1 specification:
883 * "The firmware shall interpret GetEncapsulatedResponse as a
884 * request to read response bytes. The firmware shall send
885 * the next wLength bytes from the response. The firmware
886 * shall allow the host to retrieve data using any number of
887 * GetEncapsulatedResponse requests. The firmware shall
888 * return a zero- length reply if there are no data bytes
891 * The firmware shall send ResponseAvailable notifications
892 * periodically, using any appropriate algorithm, to inform
893 * the host that there is data available in the reply
894 * buffer. The firmware is allowed to send ResponseAvailable
895 * notifications even if there is no data available, but
896 * this will obviously reduce overall performance."
898 * These requirements, although they make equally sense, are
899 * often not implemented by network functions. Some firmwares
900 * will queue data indefinitely, without ever resending a
901 * notification. The result is that the driver and firmware
902 * loses "syncronization" if the driver ever fails to respond
903 * to a single notification, something which easily can happen
904 * on release(). When this happens, the driver will appear to
905 * never receive notifications for the most current data. Each
906 * notification will only cause a single read, which returns
907 * the oldest data in the firmware's queue.
909 * The "drain_on_open" hack resolves the situation by draining
910 * data from the firmware until none is returned, without a
911 * prior notification.
913 * This will inevitably race with the firmware, risking that
914 * we read data from the device before handling the associated
915 * notification. To make things worse, some of the devices
916 * needing the hack do not implement the "return zero if no
917 * data is available" requirement either. Instead they return
918 * an error on the subsequent read in this case. This means
919 * that "winning" the race can cause an unexpected EIO to
922 * "winning" the race is more likely on resume() than on
923 * open(), and the unexpected error is more harmful in the
924 * middle of an open session. The hack is therefore only
925 * applied on open(), and not on resume() where it logically
926 * would be equally necessary. So we define open() as the only
927 * driver <-> device "syncronization point". Should we happen
928 * to lose a notification after open(), then syncronization
929 * will be lost until release()
931 * The hack should not be enabled for CDC WDM devices
932 * conforming to the CDC-WMC r1.1 specification. This is
933 * ensured by setting drain_on_open to false in wdm_probe().
936 set_bit(WDM_DRAIN_ON_OPEN, &desc->flags);
938 spin_lock(&wdm_device_list_lock);
939 list_add(&desc->device_list, &wdm_device_list);
940 spin_unlock(&wdm_device_list_lock);
942 rv = usb_register_dev(intf, &wdm_class);
946 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
950 spin_lock(&wdm_device_list_lock);
951 list_del(&desc->device_list);
952 spin_unlock(&wdm_device_list_lock);
957 static int wdm_manage_power(struct usb_interface *intf, int on)
959 /* need autopm_get/put here to ensure the usbcore sees the new value */
960 int rv = usb_autopm_get_interface(intf);
962 intf->needs_remote_wakeup = on;
964 usb_autopm_put_interface(intf);
968 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
971 struct usb_host_interface *iface;
972 struct usb_endpoint_descriptor *ep;
973 struct usb_cdc_parsed_header hdr;
974 u8 *buffer = intf->altsetting->extra;
975 int buflen = intf->altsetting->extralen;
976 u16 maxcom = WDM_DEFAULT_BUFSIZE;
981 cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
983 if (hdr.usb_cdc_dmm_desc)
984 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
986 iface = intf->cur_altsetting;
987 if (iface->desc.bNumEndpoints != 1)
989 ep = &iface->endpoint[0].desc;
991 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power, false);
998 * usb_cdc_wdm_register - register a WDM subdriver
999 * @intf: usb interface the subdriver will associate with
1000 * @ep: interrupt endpoint to monitor for notifications
1001 * @bufsize: maximum message size to support for read/write
1003 * Create WDM usb class character device and associate it with intf
1004 * without binding, allowing another driver to manage the interface.
1006 * The subdriver will manage the given interrupt endpoint exclusively
1007 * and will issue control requests referring to the given intf. It
1008 * will otherwise avoid interferring, and in particular not do
1009 * usb_set_intfdata/usb_get_intfdata on intf.
1011 * The return value is a pointer to the subdriver's struct usb_driver.
1012 * The registering driver is responsible for calling this subdriver's
1013 * disconnect, suspend, resume, pre_reset and post_reset methods from
1016 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
1017 struct usb_endpoint_descriptor *ep,
1019 int (*manage_power)(struct usb_interface *, int))
1023 rv = wdm_create(intf, ep, bufsize, manage_power, true);
1031 EXPORT_SYMBOL(usb_cdc_wdm_register);
1033 static void wdm_disconnect(struct usb_interface *intf)
1035 struct wdm_device *desc;
1036 unsigned long flags;
1038 usb_deregister_dev(intf, &wdm_class);
1039 desc = wdm_find_device(intf);
1040 mutex_lock(&wdm_mutex);
1042 /* the spinlock makes sure no new urbs are generated in the callbacks */
1043 spin_lock_irqsave(&desc->iuspin, flags);
1044 set_bit(WDM_DISCONNECTING, &desc->flags);
1045 set_bit(WDM_READ, &desc->flags);
1046 /* to terminate pending flushes */
1047 clear_bit(WDM_IN_USE, &desc->flags);
1048 spin_unlock_irqrestore(&desc->iuspin, flags);
1049 wake_up_all(&desc->wait);
1050 mutex_lock(&desc->rlock);
1051 mutex_lock(&desc->wlock);
1053 cancel_work_sync(&desc->rxwork);
1054 mutex_unlock(&desc->wlock);
1055 mutex_unlock(&desc->rlock);
1057 /* the desc->intf pointer used as list key is now invalid */
1058 spin_lock(&wdm_device_list_lock);
1059 list_del(&desc->device_list);
1060 spin_unlock(&wdm_device_list_lock);
1065 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
1066 mutex_unlock(&wdm_mutex);
1070 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
1072 struct wdm_device *desc = wdm_find_device(intf);
1075 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1077 /* if this is an autosuspend the caller does the locking */
1078 if (!PMSG_IS_AUTO(message)) {
1079 mutex_lock(&desc->rlock);
1080 mutex_lock(&desc->wlock);
1082 spin_lock_irq(&desc->iuspin);
1084 if (PMSG_IS_AUTO(message) &&
1085 (test_bit(WDM_IN_USE, &desc->flags)
1086 || test_bit(WDM_RESPONDING, &desc->flags))) {
1087 spin_unlock_irq(&desc->iuspin);
1091 set_bit(WDM_SUSPENDING, &desc->flags);
1092 spin_unlock_irq(&desc->iuspin);
1093 /* callback submits work - order is essential */
1095 cancel_work_sync(&desc->rxwork);
1097 if (!PMSG_IS_AUTO(message)) {
1098 mutex_unlock(&desc->wlock);
1099 mutex_unlock(&desc->rlock);
1106 static int recover_from_urb_loss(struct wdm_device *desc)
1111 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1113 dev_err(&desc->intf->dev,
1114 "Error resume submitting int urb - %d\n", rv);
1120 static int wdm_resume(struct usb_interface *intf)
1122 struct wdm_device *desc = wdm_find_device(intf);
1125 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1127 clear_bit(WDM_SUSPENDING, &desc->flags);
1128 rv = recover_from_urb_loss(desc);
1134 static int wdm_pre_reset(struct usb_interface *intf)
1136 struct wdm_device *desc = wdm_find_device(intf);
1139 * we notify everybody using poll of
1140 * an exceptional situation
1141 * must be done before recovery lest a spontaneous
1142 * message from the device is lost
1144 spin_lock_irq(&desc->iuspin);
1145 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1146 set_bit(WDM_READ, &desc->flags); /* unblock read */
1147 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1148 desc->rerr = -EINTR;
1149 spin_unlock_irq(&desc->iuspin);
1150 wake_up_all(&desc->wait);
1151 mutex_lock(&desc->rlock);
1152 mutex_lock(&desc->wlock);
1154 cancel_work_sync(&desc->rxwork);
1158 static int wdm_post_reset(struct usb_interface *intf)
1160 struct wdm_device *desc = wdm_find_device(intf);
1163 clear_bit(WDM_OVERFLOW, &desc->flags);
1164 clear_bit(WDM_RESETTING, &desc->flags);
1165 rv = recover_from_urb_loss(desc);
1166 mutex_unlock(&desc->wlock);
1167 mutex_unlock(&desc->rlock);
1171 static struct usb_driver wdm_driver = {
1174 .disconnect = wdm_disconnect,
1176 .suspend = wdm_suspend,
1177 .resume = wdm_resume,
1178 .reset_resume = wdm_resume,
1180 .pre_reset = wdm_pre_reset,
1181 .post_reset = wdm_post_reset,
1182 .id_table = wdm_ids,
1183 .supports_autosuspend = 1,
1184 .disable_hub_initiated_lpm = 1,
1187 module_usb_driver(wdm_driver);
1189 MODULE_AUTHOR(DRIVER_AUTHOR);
1190 MODULE_DESCRIPTION(DRIVER_DESC);
1191 MODULE_LICENSE("GPL");