1 // SPDX-License-Identifier: GPL-2.0
5 * This driver supports USB CDC WCM Device Management.
7 * Copyright (c) 2007-2009 Oliver Neukum
9 * Some code taken from cdc-acm.c
11 * Released under the GPLv2.
13 * Many thanks to Carl Nordbeck
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/ioctl.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/uaccess.h>
22 #include <linux/bitops.h>
23 #include <linux/poll.h>
24 #include <linux/usb.h>
25 #include <linux/usb/cdc.h>
26 #include <asm/byteorder.h>
27 #include <asm/unaligned.h>
28 #include <linux/usb/cdc-wdm.h>
30 #define DRIVER_AUTHOR "Oliver Neukum"
31 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
33 static const struct usb_device_id wdm_ids[] = {
35 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
36 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
37 .bInterfaceClass = USB_CLASS_COMM,
38 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
43 MODULE_DEVICE_TABLE (usb, wdm_ids);
45 #define WDM_MINOR_BASE 176
49 #define WDM_DISCONNECTING 2
52 #define WDM_INT_STALL 5
53 #define WDM_POLL_RUNNING 6
54 #define WDM_RESPONDING 7
55 #define WDM_SUSPENDING 8
56 #define WDM_RESETTING 9
57 #define WDM_OVERFLOW 10
61 /* we cannot wait forever at flush() */
62 #define WDM_FLUSH_TIMEOUT (30 * HZ)
64 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
65 #define WDM_DEFAULT_BUFSIZE 256
67 static DEFINE_MUTEX(wdm_mutex);
68 static DEFINE_SPINLOCK(wdm_device_list_lock);
69 static LIST_HEAD(wdm_device_list);
71 /* --- method tables --- */
74 u8 *inbuf; /* buffer for response */
75 u8 *outbuf; /* buffer for command */
76 u8 *sbuf; /* buffer for status */
77 u8 *ubuf; /* buffer for copy to user space */
82 struct usb_interface *intf;
83 struct usb_ctrlrequest *orq;
84 struct usb_ctrlrequest *irq;
100 wait_queue_head_t wait;
101 struct work_struct rxwork;
102 struct work_struct service_outs_intr;
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;
151 spin_lock_irqsave(&desc->iuspin, flags);
152 desc->werr = urb->status;
153 spin_unlock_irqrestore(&desc->iuspin, flags);
156 clear_bit(WDM_IN_USE, &desc->flags);
157 wake_up_all(&desc->wait);
160 static void wdm_in_callback(struct urb *urb)
163 struct wdm_device *desc = urb->context;
164 int status = urb->status;
165 int length = urb->actual_length;
167 spin_lock_irqsave(&desc->iuspin, flags);
168 clear_bit(WDM_RESPONDING, &desc->flags);
173 dev_dbg(&desc->intf->dev,
174 "nonzero urb status received: -ENOENT\n");
177 dev_dbg(&desc->intf->dev,
178 "nonzero urb status received: -ECONNRESET\n");
181 dev_dbg(&desc->intf->dev,
182 "nonzero urb status received: -ESHUTDOWN\n");
185 dev_err(&desc->intf->dev,
186 "nonzero urb status received: -EPIPE\n");
189 dev_err(&desc->intf->dev,
190 "Unexpected error %d\n", status);
196 * only set a new error if there is no previous error.
197 * Errors are only cleared during read/open
198 * Avoid propagating -EPIPE (stall) to userspace since it is
199 * better handled as an empty read
201 if (desc->rerr == 0 && status != -EPIPE)
204 if (length + desc->length > desc->wMaxCommand) {
205 /* The buffer would overflow */
206 set_bit(WDM_OVERFLOW, &desc->flags);
208 /* we may already be in overflow */
209 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
210 memmove(desc->ubuf + desc->length, desc->inbuf, length);
211 desc->length += length;
212 desc->reslength = length;
219 * Since there was an error, userspace may decide to not read
220 * any data after poll'ing.
221 * We should respond to further attempts from the device to send
222 * data, so that we can get unstuck.
224 schedule_work(&desc->service_outs_intr);
226 set_bit(WDM_READ, &desc->flags);
227 wake_up(&desc->wait);
229 spin_unlock_irqrestore(&desc->iuspin, flags);
232 static void wdm_int_callback(struct urb *urb)
237 int status = urb->status;
238 struct wdm_device *desc;
239 struct usb_cdc_notification *dr;
242 dr = (struct usb_cdc_notification *)desc->sbuf;
251 set_bit(WDM_INT_STALL, &desc->flags);
252 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
253 goto sw; /* halt is cleared in work */
255 dev_err(&desc->intf->dev,
256 "nonzero urb status received: %d\n", status);
261 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
262 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
267 switch (dr->bNotificationType) {
268 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
269 dev_dbg(&desc->intf->dev,
270 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
271 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
274 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
276 dev_dbg(&desc->intf->dev,
277 "NOTIFY_NETWORK_CONNECTION %s network\n",
278 dr->wValue ? "connected to" : "disconnected from");
280 case USB_CDC_NOTIFY_SPEED_CHANGE:
281 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
285 clear_bit(WDM_POLL_RUNNING, &desc->flags);
286 dev_err(&desc->intf->dev,
287 "unknown notification %d received: index %d len %d\n",
288 dr->bNotificationType,
289 le16_to_cpu(dr->wIndex),
290 le16_to_cpu(dr->wLength));
294 spin_lock_irqsave(&desc->iuspin, flags);
295 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
296 if (!desc->resp_count++ && !responding
297 && !test_bit(WDM_DISCONNECTING, &desc->flags)
298 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
299 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
300 dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
302 spin_unlock_irqrestore(&desc->iuspin, flags);
304 clear_bit(WDM_RESPONDING, &desc->flags);
309 rv = schedule_work(&desc->rxwork);
311 dev_err(&desc->intf->dev,
312 "Cannot schedule work\n");
316 rv = usb_submit_urb(urb, GFP_ATOMIC);
318 dev_err(&desc->intf->dev,
319 "%s - usb_submit_urb failed with result %d\n",
324 static void poison_urbs(struct wdm_device *desc)
326 /* the order here is essential */
327 usb_poison_urb(desc->command);
328 usb_poison_urb(desc->validity);
329 usb_poison_urb(desc->response);
332 static void unpoison_urbs(struct wdm_device *desc)
335 * the order here is not essential
336 * it is symmetrical just to be nice
338 usb_unpoison_urb(desc->response);
339 usb_unpoison_urb(desc->validity);
340 usb_unpoison_urb(desc->command);
343 static void free_urbs(struct wdm_device *desc)
345 usb_free_urb(desc->validity);
346 usb_free_urb(desc->response);
347 usb_free_urb(desc->command);
350 static void cleanup(struct wdm_device *desc)
361 static ssize_t wdm_write
362 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
365 int rv = -EMSGSIZE, r, we;
366 struct wdm_device *desc = file->private_data;
367 struct usb_ctrlrequest *req;
369 if (count > desc->wMaxCommand)
370 count = desc->wMaxCommand;
372 spin_lock_irq(&desc->iuspin);
375 spin_unlock_irq(&desc->iuspin);
377 return usb_translate_errors(we);
379 buf = memdup_user(buffer, count);
383 /* concurrent writes and disconnect */
384 r = mutex_lock_interruptible(&desc->wlock);
389 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
391 goto out_free_mem_lock;
394 r = usb_autopm_get_interface(desc->intf);
396 rv = usb_translate_errors(r);
397 goto out_free_mem_lock;
400 if (!(file->f_flags & O_NONBLOCK))
401 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
404 if (test_bit(WDM_IN_USE, &desc->flags))
407 if (test_bit(WDM_RESETTING, &desc->flags))
410 if (test_bit(WDM_DISCONNECTING, &desc->flags))
415 goto out_free_mem_pm;
419 usb_fill_control_urb(
421 interface_to_usbdev(desc->intf),
422 /* using common endpoint 0 */
423 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
424 (unsigned char *)req,
431 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
432 USB_RECIP_INTERFACE);
433 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
435 req->wIndex = desc->inum; /* already converted */
436 req->wLength = cpu_to_le16(count);
437 set_bit(WDM_IN_USE, &desc->flags);
440 rv = usb_submit_urb(desc->command, GFP_KERNEL);
443 clear_bit(WDM_IN_USE, &desc->flags);
444 wake_up_all(&desc->wait); /* for wdm_wait_for_response() */
445 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
446 rv = usb_translate_errors(rv);
447 goto out_free_mem_pm;
449 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
450 le16_to_cpu(req->wIndex));
453 usb_autopm_put_interface(desc->intf);
454 mutex_unlock(&desc->wlock);
458 usb_autopm_put_interface(desc->intf);
460 mutex_unlock(&desc->wlock);
467 * Submit the read urb if resp_count is non-zero.
469 * Called with desc->iuspin locked
471 static int service_outstanding_interrupt(struct wdm_device *desc)
475 /* submit read urb only if the device is waiting for it */
476 if (!desc->resp_count || !--desc->resp_count)
479 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
483 if (test_bit(WDM_RESETTING, &desc->flags)) {
488 set_bit(WDM_RESPONDING, &desc->flags);
489 spin_unlock_irq(&desc->iuspin);
490 rv = usb_submit_urb(desc->response, GFP_KERNEL);
491 spin_lock_irq(&desc->iuspin);
493 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
494 dev_err(&desc->intf->dev,
495 "usb_submit_urb failed with result %d\n", rv);
497 /* make sure the next notification trigger a submit */
498 clear_bit(WDM_RESPONDING, &desc->flags);
499 desc->resp_count = 0;
505 static ssize_t wdm_read
506 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
510 struct wdm_device *desc = file->private_data;
513 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
517 cntr = READ_ONCE(desc->length);
521 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
525 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
526 clear_bit(WDM_OVERFLOW, &desc->flags);
531 if (file->f_flags & O_NONBLOCK) {
532 if (!test_bit(WDM_READ, &desc->flags)) {
538 rv = wait_event_interruptible(desc->wait,
539 test_bit(WDM_READ, &desc->flags));
542 /* may have happened while we slept */
543 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
547 if (test_bit(WDM_RESETTING, &desc->flags)) {
551 usb_mark_last_busy(interface_to_usbdev(desc->intf));
557 spin_lock_irq(&desc->iuspin);
559 if (desc->rerr) { /* read completed, error happened */
560 rv = usb_translate_errors(desc->rerr);
562 spin_unlock_irq(&desc->iuspin);
566 * recheck whether we've lost the race
567 * against the completion handler
569 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
570 spin_unlock_irq(&desc->iuspin);
574 if (!desc->reslength) { /* zero length read */
575 dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
576 clear_bit(WDM_READ, &desc->flags);
577 rv = service_outstanding_interrupt(desc);
578 spin_unlock_irq(&desc->iuspin);
584 spin_unlock_irq(&desc->iuspin);
589 rv = copy_to_user(buffer, desc->ubuf, cntr);
595 spin_lock_irq(&desc->iuspin);
597 for (i = 0; i < desc->length - cntr; i++)
598 desc->ubuf[i] = desc->ubuf[i + cntr];
600 desc->length -= cntr;
601 /* in case we had outstanding data */
603 clear_bit(WDM_READ, &desc->flags);
604 service_outstanding_interrupt(desc);
606 spin_unlock_irq(&desc->iuspin);
610 mutex_unlock(&desc->rlock);
614 static int wdm_wait_for_response(struct file *file, long timeout)
616 struct wdm_device *desc = file->private_data;
617 long rv; /* Use long here because (int) MAX_SCHEDULE_TIMEOUT < 0. */
620 * Needs both flags. We cannot do with one because resetting it would
621 * cause a race with write() yet we need to signal a disconnect.
623 rv = wait_event_interruptible_timeout(desc->wait,
624 !test_bit(WDM_IN_USE, &desc->flags) ||
625 test_bit(WDM_DISCONNECTING, &desc->flags),
629 * To report the correct error. This is best effort.
630 * We are inevitably racing with the hardware.
632 if (test_bit(WDM_DISCONNECTING, &desc->flags))
639 spin_lock_irq(&desc->iuspin);
642 spin_unlock_irq(&desc->iuspin);
644 return usb_translate_errors(rv);
649 * You need to send a signal when you react to malicious or defective hardware.
650 * Also, don't abort when fsync() returned -EINVAL, for older kernels which do
651 * not implement wdm_flush() will return -EINVAL.
653 static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
655 return wdm_wait_for_response(file, MAX_SCHEDULE_TIMEOUT);
659 * Same with wdm_fsync(), except it uses finite timeout in order to react to
660 * malicious or defective hardware which ceased communication after close() was
661 * implicitly called due to process termination.
663 static int wdm_flush(struct file *file, fl_owner_t id)
665 return wdm_wait_for_response(file, WDM_FLUSH_TIMEOUT);
668 static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
670 struct wdm_device *desc = file->private_data;
674 spin_lock_irqsave(&desc->iuspin, flags);
675 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
676 mask = EPOLLHUP | EPOLLERR;
677 spin_unlock_irqrestore(&desc->iuspin, flags);
680 if (test_bit(WDM_READ, &desc->flags))
681 mask = EPOLLIN | EPOLLRDNORM;
682 if (desc->rerr || desc->werr)
684 if (!test_bit(WDM_IN_USE, &desc->flags))
685 mask |= EPOLLOUT | EPOLLWRNORM;
686 spin_unlock_irqrestore(&desc->iuspin, flags);
688 poll_wait(file, &desc->wait, wait);
694 static int wdm_open(struct inode *inode, struct file *file)
696 int minor = iminor(inode);
698 struct usb_interface *intf;
699 struct wdm_device *desc;
701 mutex_lock(&wdm_mutex);
702 desc = wdm_find_device_by_minor(minor);
707 if (test_bit(WDM_DISCONNECTING, &desc->flags))
709 file->private_data = desc;
711 rv = usb_autopm_get_interface(desc->intf);
713 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
717 /* using write lock to protect desc->count */
718 mutex_lock(&desc->wlock);
719 if (!desc->count++) {
722 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
725 dev_err(&desc->intf->dev,
726 "Error submitting int urb - %d\n", rv);
727 rv = usb_translate_errors(rv);
732 mutex_unlock(&desc->wlock);
733 if (desc->count == 1)
734 desc->manage_power(intf, 1);
735 usb_autopm_put_interface(desc->intf);
737 mutex_unlock(&wdm_mutex);
741 static int wdm_release(struct inode *inode, struct file *file)
743 struct wdm_device *desc = file->private_data;
745 mutex_lock(&wdm_mutex);
747 /* using write lock to protect desc->count */
748 mutex_lock(&desc->wlock);
750 mutex_unlock(&desc->wlock);
753 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
754 dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
756 spin_lock_irq(&desc->iuspin);
757 desc->resp_count = 0;
758 clear_bit(WDM_RESPONDING, &desc->flags);
759 spin_unlock_irq(&desc->iuspin);
760 desc->manage_power(desc->intf, 0);
763 /* must avoid dev_printk here as desc->intf is invalid */
764 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
768 mutex_unlock(&wdm_mutex);
772 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
774 struct wdm_device *desc = file->private_data;
778 case IOCTL_WDM_MAX_COMMAND:
779 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
788 static const struct file_operations wdm_fops = {
789 .owner = THIS_MODULE,
795 .release = wdm_release,
797 .unlocked_ioctl = wdm_ioctl,
798 .compat_ioctl = wdm_ioctl,
799 .llseek = noop_llseek,
802 static struct usb_class_driver wdm_class = {
805 .minor_base = WDM_MINOR_BASE,
808 /* --- error handling --- */
809 static void wdm_rxwork(struct work_struct *work)
811 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
816 spin_lock_irqsave(&desc->iuspin, flags);
817 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
818 spin_unlock_irqrestore(&desc->iuspin, flags);
820 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
821 spin_unlock_irqrestore(&desc->iuspin, flags);
823 rv = usb_submit_urb(desc->response, GFP_KERNEL);
824 if (rv < 0 && rv != -EPERM) {
825 spin_lock_irqsave(&desc->iuspin, flags);
826 clear_bit(WDM_RESPONDING, &desc->flags);
827 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
828 schedule_work(&desc->rxwork);
829 spin_unlock_irqrestore(&desc->iuspin, flags);
834 static void service_interrupt_work(struct work_struct *work)
836 struct wdm_device *desc;
838 desc = container_of(work, struct wdm_device, service_outs_intr);
840 spin_lock_irq(&desc->iuspin);
841 service_outstanding_interrupt(desc);
842 if (!desc->resp_count) {
843 set_bit(WDM_READ, &desc->flags);
844 wake_up(&desc->wait);
846 spin_unlock_irq(&desc->iuspin);
849 /* --- hotplug --- */
851 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
852 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
855 struct wdm_device *desc;
857 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
860 INIT_LIST_HEAD(&desc->device_list);
861 mutex_init(&desc->rlock);
862 mutex_init(&desc->wlock);
863 spin_lock_init(&desc->iuspin);
864 init_waitqueue_head(&desc->wait);
865 desc->wMaxCommand = bufsize;
866 /* this will be expanded and needed in hardware endianness */
867 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
869 INIT_WORK(&desc->rxwork, wdm_rxwork);
870 INIT_WORK(&desc->service_outs_intr, service_interrupt_work);
873 if (!usb_endpoint_is_int_in(ep))
876 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
878 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
881 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
885 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
889 desc->response = usb_alloc_urb(0, GFP_KERNEL);
893 desc->command = usb_alloc_urb(0, GFP_KERNEL);
897 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
901 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
905 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
911 interface_to_usbdev(intf),
912 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
914 desc->wMaxPacketSize,
920 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
921 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
922 desc->irq->wValue = 0;
923 desc->irq->wIndex = desc->inum; /* already converted */
924 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
926 usb_fill_control_urb(
928 interface_to_usbdev(intf),
929 /* using common endpoint 0 */
930 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
931 (unsigned char *)desc->irq,
938 desc->manage_power = manage_power;
940 spin_lock(&wdm_device_list_lock);
941 list_add(&desc->device_list, &wdm_device_list);
942 spin_unlock(&wdm_device_list_lock);
944 rv = usb_register_dev(intf, &wdm_class);
948 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
952 spin_lock(&wdm_device_list_lock);
953 list_del(&desc->device_list);
954 spin_unlock(&wdm_device_list_lock);
959 static int wdm_manage_power(struct usb_interface *intf, int on)
961 /* need autopm_get/put here to ensure the usbcore sees the new value */
962 int rv = usb_autopm_get_interface(intf);
964 intf->needs_remote_wakeup = on;
966 usb_autopm_put_interface(intf);
970 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
973 struct usb_host_interface *iface;
974 struct usb_endpoint_descriptor *ep;
975 struct usb_cdc_parsed_header hdr;
976 u8 *buffer = intf->altsetting->extra;
977 int buflen = intf->altsetting->extralen;
978 u16 maxcom = WDM_DEFAULT_BUFSIZE;
983 cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
985 if (hdr.usb_cdc_dmm_desc)
986 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
988 iface = intf->cur_altsetting;
989 if (iface->desc.bNumEndpoints != 1)
991 ep = &iface->endpoint[0].desc;
993 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
1000 * usb_cdc_wdm_register - register a WDM subdriver
1001 * @intf: usb interface the subdriver will associate with
1002 * @ep: interrupt endpoint to monitor for notifications
1003 * @bufsize: maximum message size to support for read/write
1005 * Create WDM usb class character device and associate it with intf
1006 * without binding, allowing another driver to manage the interface.
1008 * The subdriver will manage the given interrupt endpoint exclusively
1009 * and will issue control requests referring to the given intf. It
1010 * will otherwise avoid interferring, and in particular not do
1011 * usb_set_intfdata/usb_get_intfdata on intf.
1013 * The return value is a pointer to the subdriver's struct usb_driver.
1014 * The registering driver is responsible for calling this subdriver's
1015 * disconnect, suspend, resume, pre_reset and post_reset methods from
1018 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
1019 struct usb_endpoint_descriptor *ep,
1021 int (*manage_power)(struct usb_interface *, int))
1025 rv = wdm_create(intf, ep, bufsize, manage_power);
1033 EXPORT_SYMBOL(usb_cdc_wdm_register);
1035 static void wdm_disconnect(struct usb_interface *intf)
1037 struct wdm_device *desc;
1038 unsigned long flags;
1040 usb_deregister_dev(intf, &wdm_class);
1041 desc = wdm_find_device(intf);
1042 mutex_lock(&wdm_mutex);
1044 /* the spinlock makes sure no new urbs are generated in the callbacks */
1045 spin_lock_irqsave(&desc->iuspin, flags);
1046 set_bit(WDM_DISCONNECTING, &desc->flags);
1047 set_bit(WDM_READ, &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 cancel_work_sync(&desc->service_outs_intr);
1055 mutex_unlock(&desc->wlock);
1056 mutex_unlock(&desc->rlock);
1058 /* the desc->intf pointer used as list key is now invalid */
1059 spin_lock(&wdm_device_list_lock);
1060 list_del(&desc->device_list);
1061 spin_unlock(&wdm_device_list_lock);
1066 dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
1067 mutex_unlock(&wdm_mutex);
1071 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
1073 struct wdm_device *desc = wdm_find_device(intf);
1076 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1078 /* if this is an autosuspend the caller does the locking */
1079 if (!PMSG_IS_AUTO(message)) {
1080 mutex_lock(&desc->rlock);
1081 mutex_lock(&desc->wlock);
1083 spin_lock_irq(&desc->iuspin);
1085 if (PMSG_IS_AUTO(message) &&
1086 (test_bit(WDM_IN_USE, &desc->flags)
1087 || test_bit(WDM_RESPONDING, &desc->flags))) {
1088 spin_unlock_irq(&desc->iuspin);
1092 set_bit(WDM_SUSPENDING, &desc->flags);
1093 spin_unlock_irq(&desc->iuspin);
1094 /* callback submits work - order is essential */
1096 cancel_work_sync(&desc->rxwork);
1097 cancel_work_sync(&desc->service_outs_intr);
1098 unpoison_urbs(desc);
1100 if (!PMSG_IS_AUTO(message)) {
1101 mutex_unlock(&desc->wlock);
1102 mutex_unlock(&desc->rlock);
1109 static int recover_from_urb_loss(struct wdm_device *desc)
1114 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1116 dev_err(&desc->intf->dev,
1117 "Error resume submitting int urb - %d\n", rv);
1123 static int wdm_resume(struct usb_interface *intf)
1125 struct wdm_device *desc = wdm_find_device(intf);
1128 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1130 clear_bit(WDM_SUSPENDING, &desc->flags);
1131 rv = recover_from_urb_loss(desc);
1137 static int wdm_pre_reset(struct usb_interface *intf)
1139 struct wdm_device *desc = wdm_find_device(intf);
1142 * we notify everybody using poll of
1143 * an exceptional situation
1144 * must be done before recovery lest a spontaneous
1145 * message from the device is lost
1147 spin_lock_irq(&desc->iuspin);
1148 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1149 set_bit(WDM_READ, &desc->flags); /* unblock read */
1150 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1151 desc->rerr = -EINTR;
1152 spin_unlock_irq(&desc->iuspin);
1153 wake_up_all(&desc->wait);
1154 mutex_lock(&desc->rlock);
1155 mutex_lock(&desc->wlock);
1157 cancel_work_sync(&desc->rxwork);
1158 cancel_work_sync(&desc->service_outs_intr);
1162 static int wdm_post_reset(struct usb_interface *intf)
1164 struct wdm_device *desc = wdm_find_device(intf);
1167 unpoison_urbs(desc);
1168 clear_bit(WDM_OVERFLOW, &desc->flags);
1169 clear_bit(WDM_RESETTING, &desc->flags);
1170 rv = recover_from_urb_loss(desc);
1171 mutex_unlock(&desc->wlock);
1172 mutex_unlock(&desc->rlock);
1176 static struct usb_driver wdm_driver = {
1179 .disconnect = wdm_disconnect,
1181 .suspend = wdm_suspend,
1182 .resume = wdm_resume,
1183 .reset_resume = wdm_resume,
1185 .pre_reset = wdm_pre_reset,
1186 .post_reset = wdm_post_reset,
1187 .id_table = wdm_ids,
1188 .supports_autosuspend = 1,
1189 .disable_hub_initiated_lpm = 1,
1192 module_usb_driver(wdm_driver);
1194 MODULE_AUTHOR(DRIVER_AUTHOR);
1195 MODULE_DESCRIPTION(DRIVER_DESC);
1196 MODULE_LICENSE("GPL");