Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * cdc-wdm.c
0004  *
0005  * This driver supports USB CDC WCM Device Management.
0006  *
0007  * Copyright (c) 2007-2009 Oliver Neukum
0008  *
0009  * Some code taken from cdc-acm.c
0010  *
0011  * Released under the GPLv2.
0012  *
0013  * Many thanks to Carl Nordbeck
0014  */
0015 #include <linux/kernel.h>
0016 #include <linux/errno.h>
0017 #include <linux/ioctl.h>
0018 #include <linux/slab.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/uaccess.h>
0022 #include <linux/bitops.h>
0023 #include <linux/poll.h>
0024 #include <linux/skbuff.h>
0025 #include <linux/usb.h>
0026 #include <linux/usb/cdc.h>
0027 #include <linux/wwan.h>
0028 #include <asm/byteorder.h>
0029 #include <asm/unaligned.h>
0030 #include <linux/usb/cdc-wdm.h>
0031 
0032 #define DRIVER_AUTHOR "Oliver Neukum"
0033 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
0034 
0035 static const struct usb_device_id wdm_ids[] = {
0036     {
0037         .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
0038                  USB_DEVICE_ID_MATCH_INT_SUBCLASS,
0039         .bInterfaceClass = USB_CLASS_COMM,
0040         .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
0041     },
0042     { }
0043 };
0044 
0045 MODULE_DEVICE_TABLE (usb, wdm_ids);
0046 
0047 #define WDM_MINOR_BASE  176
0048 
0049 
0050 #define WDM_IN_USE      1
0051 #define WDM_DISCONNECTING   2
0052 #define WDM_RESULT      3
0053 #define WDM_READ        4
0054 #define WDM_INT_STALL       5
0055 #define WDM_POLL_RUNNING    6
0056 #define WDM_RESPONDING      7
0057 #define WDM_SUSPENDING      8
0058 #define WDM_RESETTING       9
0059 #define WDM_OVERFLOW        10
0060 #define WDM_WWAN_IN_USE     11
0061 
0062 #define WDM_MAX         16
0063 
0064 /* we cannot wait forever at flush() */
0065 #define WDM_FLUSH_TIMEOUT   (30 * HZ)
0066 
0067 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
0068 #define WDM_DEFAULT_BUFSIZE 256
0069 
0070 static DEFINE_MUTEX(wdm_mutex);
0071 static DEFINE_SPINLOCK(wdm_device_list_lock);
0072 static LIST_HEAD(wdm_device_list);
0073 
0074 /* --- method tables --- */
0075 
0076 struct wdm_device {
0077     u8          *inbuf; /* buffer for response */
0078     u8          *outbuf; /* buffer for command */
0079     u8          *sbuf; /* buffer for status */
0080     u8          *ubuf; /* buffer for copy to user space */
0081 
0082     struct urb      *command;
0083     struct urb      *response;
0084     struct urb      *validity;
0085     struct usb_interface    *intf;
0086     struct usb_ctrlrequest  *orq;
0087     struct usb_ctrlrequest  *irq;
0088     spinlock_t      iuspin;
0089 
0090     unsigned long       flags;
0091     u16         bufsize;
0092     u16         wMaxCommand;
0093     u16         wMaxPacketSize;
0094     __le16          inum;
0095     int         reslength;
0096     int         length;
0097     int         read;
0098     int         count;
0099     dma_addr_t      shandle;
0100     dma_addr_t      ihandle;
0101     struct mutex        wlock;
0102     struct mutex        rlock;
0103     wait_queue_head_t   wait;
0104     struct work_struct  rxwork;
0105     struct work_struct  service_outs_intr;
0106     int         werr;
0107     int         rerr;
0108     int                     resp_count;
0109 
0110     struct list_head    device_list;
0111     int         (*manage_power)(struct usb_interface *, int);
0112 
0113     enum wwan_port_type wwanp_type;
0114     struct wwan_port    *wwanp;
0115 };
0116 
0117 static struct usb_driver wdm_driver;
0118 
0119 /* return intfdata if we own the interface, else look up intf in the list */
0120 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
0121 {
0122     struct wdm_device *desc;
0123 
0124     spin_lock(&wdm_device_list_lock);
0125     list_for_each_entry(desc, &wdm_device_list, device_list)
0126         if (desc->intf == intf)
0127             goto found;
0128     desc = NULL;
0129 found:
0130     spin_unlock(&wdm_device_list_lock);
0131 
0132     return desc;
0133 }
0134 
0135 static struct wdm_device *wdm_find_device_by_minor(int minor)
0136 {
0137     struct wdm_device *desc;
0138 
0139     spin_lock(&wdm_device_list_lock);
0140     list_for_each_entry(desc, &wdm_device_list, device_list)
0141         if (desc->intf->minor == minor)
0142             goto found;
0143     desc = NULL;
0144 found:
0145     spin_unlock(&wdm_device_list_lock);
0146 
0147     return desc;
0148 }
0149 
0150 /* --- callbacks --- */
0151 static void wdm_out_callback(struct urb *urb)
0152 {
0153     struct wdm_device *desc;
0154     unsigned long flags;
0155 
0156     desc = urb->context;
0157     spin_lock_irqsave(&desc->iuspin, flags);
0158     desc->werr = urb->status;
0159     spin_unlock_irqrestore(&desc->iuspin, flags);
0160     kfree(desc->outbuf);
0161     desc->outbuf = NULL;
0162     clear_bit(WDM_IN_USE, &desc->flags);
0163     wake_up_all(&desc->wait);
0164 }
0165 
0166 static void wdm_wwan_rx(struct wdm_device *desc, int length);
0167 
0168 static void wdm_in_callback(struct urb *urb)
0169 {
0170     unsigned long flags;
0171     struct wdm_device *desc = urb->context;
0172     int status = urb->status;
0173     int length = urb->actual_length;
0174 
0175     spin_lock_irqsave(&desc->iuspin, flags);
0176     clear_bit(WDM_RESPONDING, &desc->flags);
0177 
0178     if (status) {
0179         switch (status) {
0180         case -ENOENT:
0181             dev_dbg(&desc->intf->dev,
0182                 "nonzero urb status received: -ENOENT\n");
0183             goto skip_error;
0184         case -ECONNRESET:
0185             dev_dbg(&desc->intf->dev,
0186                 "nonzero urb status received: -ECONNRESET\n");
0187             goto skip_error;
0188         case -ESHUTDOWN:
0189             dev_dbg(&desc->intf->dev,
0190                 "nonzero urb status received: -ESHUTDOWN\n");
0191             goto skip_error;
0192         case -EPIPE:
0193             dev_err(&desc->intf->dev,
0194                 "nonzero urb status received: -EPIPE\n");
0195             break;
0196         default:
0197             dev_err(&desc->intf->dev,
0198                 "Unexpected error %d\n", status);
0199             break;
0200         }
0201     }
0202 
0203     if (test_bit(WDM_WWAN_IN_USE, &desc->flags)) {
0204         wdm_wwan_rx(desc, length);
0205         goto out;
0206     }
0207 
0208     /*
0209      * only set a new error if there is no previous error.
0210      * Errors are only cleared during read/open
0211      * Avoid propagating -EPIPE (stall) to userspace since it is
0212      * better handled as an empty read
0213      */
0214     if (desc->rerr == 0 && status != -EPIPE)
0215         desc->rerr = status;
0216 
0217     if (length + desc->length > desc->wMaxCommand) {
0218         /* The buffer would overflow */
0219         set_bit(WDM_OVERFLOW, &desc->flags);
0220     } else {
0221         /* we may already be in overflow */
0222         if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
0223             memmove(desc->ubuf + desc->length, desc->inbuf, length);
0224             desc->length += length;
0225             desc->reslength = length;
0226         }
0227     }
0228 skip_error:
0229 
0230     if (desc->rerr) {
0231         /*
0232          * Since there was an error, userspace may decide to not read
0233          * any data after poll'ing.
0234          * We should respond to further attempts from the device to send
0235          * data, so that we can get unstuck.
0236          */
0237         schedule_work(&desc->service_outs_intr);
0238     } else {
0239         set_bit(WDM_READ, &desc->flags);
0240         wake_up(&desc->wait);
0241     }
0242 out:
0243     spin_unlock_irqrestore(&desc->iuspin, flags);
0244 }
0245 
0246 static void wdm_int_callback(struct urb *urb)
0247 {
0248     unsigned long flags;
0249     int rv = 0;
0250     int responding;
0251     int status = urb->status;
0252     struct wdm_device *desc;
0253     struct usb_cdc_notification *dr;
0254 
0255     desc = urb->context;
0256     dr = (struct usb_cdc_notification *)desc->sbuf;
0257 
0258     if (status) {
0259         switch (status) {
0260         case -ESHUTDOWN:
0261         case -ENOENT:
0262         case -ECONNRESET:
0263             return; /* unplug */
0264         case -EPIPE:
0265             set_bit(WDM_INT_STALL, &desc->flags);
0266             dev_err(&desc->intf->dev, "Stall on int endpoint\n");
0267             goto sw; /* halt is cleared in work */
0268         default:
0269             dev_err(&desc->intf->dev,
0270                 "nonzero urb status received: %d\n", status);
0271             break;
0272         }
0273     }
0274 
0275     if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
0276         dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
0277             urb->actual_length);
0278         goto exit;
0279     }
0280 
0281     switch (dr->bNotificationType) {
0282     case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
0283         dev_dbg(&desc->intf->dev,
0284             "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
0285             le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
0286         break;
0287 
0288     case USB_CDC_NOTIFY_NETWORK_CONNECTION:
0289 
0290         dev_dbg(&desc->intf->dev,
0291             "NOTIFY_NETWORK_CONNECTION %s network\n",
0292             dr->wValue ? "connected to" : "disconnected from");
0293         goto exit;
0294     case USB_CDC_NOTIFY_SPEED_CHANGE:
0295         dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
0296             urb->actual_length);
0297         goto exit;
0298     default:
0299         clear_bit(WDM_POLL_RUNNING, &desc->flags);
0300         dev_err(&desc->intf->dev,
0301             "unknown notification %d received: index %d len %d\n",
0302             dr->bNotificationType,
0303             le16_to_cpu(dr->wIndex),
0304             le16_to_cpu(dr->wLength));
0305         goto exit;
0306     }
0307 
0308     spin_lock_irqsave(&desc->iuspin, flags);
0309     responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
0310     if (!desc->resp_count++ && !responding
0311         && !test_bit(WDM_DISCONNECTING, &desc->flags)
0312         && !test_bit(WDM_SUSPENDING, &desc->flags)) {
0313         rv = usb_submit_urb(desc->response, GFP_ATOMIC);
0314         dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
0315     }
0316     spin_unlock_irqrestore(&desc->iuspin, flags);
0317     if (rv < 0) {
0318         clear_bit(WDM_RESPONDING, &desc->flags);
0319         if (rv == -EPERM)
0320             return;
0321         if (rv == -ENOMEM) {
0322 sw:
0323             rv = schedule_work(&desc->rxwork);
0324             if (rv)
0325                 dev_err(&desc->intf->dev,
0326                     "Cannot schedule work\n");
0327         }
0328     }
0329 exit:
0330     rv = usb_submit_urb(urb, GFP_ATOMIC);
0331     if (rv)
0332         dev_err(&desc->intf->dev,
0333             "%s - usb_submit_urb failed with result %d\n",
0334             __func__, rv);
0335 
0336 }
0337 
0338 static void poison_urbs(struct wdm_device *desc)
0339 {
0340     /* the order here is essential */
0341     usb_poison_urb(desc->command);
0342     usb_poison_urb(desc->validity);
0343     usb_poison_urb(desc->response);
0344 }
0345 
0346 static void unpoison_urbs(struct wdm_device *desc)
0347 {
0348     /*
0349      *  the order here is not essential
0350      *  it is symmetrical just to be nice
0351      */
0352     usb_unpoison_urb(desc->response);
0353     usb_unpoison_urb(desc->validity);
0354     usb_unpoison_urb(desc->command);
0355 }
0356 
0357 static void free_urbs(struct wdm_device *desc)
0358 {
0359     usb_free_urb(desc->validity);
0360     usb_free_urb(desc->response);
0361     usb_free_urb(desc->command);
0362 }
0363 
0364 static void cleanup(struct wdm_device *desc)
0365 {
0366     kfree(desc->sbuf);
0367     kfree(desc->inbuf);
0368     kfree(desc->orq);
0369     kfree(desc->irq);
0370     kfree(desc->ubuf);
0371     free_urbs(desc);
0372     kfree(desc);
0373 }
0374 
0375 static ssize_t wdm_write
0376 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
0377 {
0378     u8 *buf;
0379     int rv = -EMSGSIZE, r, we;
0380     struct wdm_device *desc = file->private_data;
0381     struct usb_ctrlrequest *req;
0382 
0383     if (count > desc->wMaxCommand)
0384         count = desc->wMaxCommand;
0385 
0386     spin_lock_irq(&desc->iuspin);
0387     we = desc->werr;
0388     desc->werr = 0;
0389     spin_unlock_irq(&desc->iuspin);
0390     if (we < 0)
0391         return usb_translate_errors(we);
0392 
0393     buf = memdup_user(buffer, count);
0394     if (IS_ERR(buf))
0395         return PTR_ERR(buf);
0396 
0397     /* concurrent writes and disconnect */
0398     r = mutex_lock_interruptible(&desc->wlock);
0399     rv = -ERESTARTSYS;
0400     if (r)
0401         goto out_free_mem;
0402 
0403     if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
0404         rv = -ENODEV;
0405         goto out_free_mem_lock;
0406     }
0407 
0408     r = usb_autopm_get_interface(desc->intf);
0409     if (r < 0) {
0410         rv = usb_translate_errors(r);
0411         goto out_free_mem_lock;
0412     }
0413 
0414     if (!(file->f_flags & O_NONBLOCK))
0415         r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
0416                                 &desc->flags));
0417     else
0418         if (test_bit(WDM_IN_USE, &desc->flags))
0419             r = -EAGAIN;
0420 
0421     if (test_bit(WDM_RESETTING, &desc->flags))
0422         r = -EIO;
0423 
0424     if (test_bit(WDM_DISCONNECTING, &desc->flags))
0425         r = -ENODEV;
0426 
0427     if (r < 0) {
0428         rv = r;
0429         goto out_free_mem_pm;
0430     }
0431 
0432     req = desc->orq;
0433     usb_fill_control_urb(
0434         desc->command,
0435         interface_to_usbdev(desc->intf),
0436         /* using common endpoint 0 */
0437         usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
0438         (unsigned char *)req,
0439         buf,
0440         count,
0441         wdm_out_callback,
0442         desc
0443     );
0444 
0445     req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
0446                  USB_RECIP_INTERFACE);
0447     req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
0448     req->wValue = 0;
0449     req->wIndex = desc->inum; /* already converted */
0450     req->wLength = cpu_to_le16(count);
0451     set_bit(WDM_IN_USE, &desc->flags);
0452     desc->outbuf = buf;
0453 
0454     rv = usb_submit_urb(desc->command, GFP_KERNEL);
0455     if (rv < 0) {
0456         desc->outbuf = NULL;
0457         clear_bit(WDM_IN_USE, &desc->flags);
0458         wake_up_all(&desc->wait); /* for wdm_wait_for_response() */
0459         dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
0460         rv = usb_translate_errors(rv);
0461         goto out_free_mem_pm;
0462     } else {
0463         dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
0464             le16_to_cpu(req->wIndex));
0465     }
0466 
0467     usb_autopm_put_interface(desc->intf);
0468     mutex_unlock(&desc->wlock);
0469     return count;
0470 
0471 out_free_mem_pm:
0472     usb_autopm_put_interface(desc->intf);
0473 out_free_mem_lock:
0474     mutex_unlock(&desc->wlock);
0475 out_free_mem:
0476     kfree(buf);
0477     return rv;
0478 }
0479 
0480 /*
0481  * Submit the read urb if resp_count is non-zero.
0482  *
0483  * Called with desc->iuspin locked
0484  */
0485 static int service_outstanding_interrupt(struct wdm_device *desc)
0486 {
0487     int rv = 0;
0488 
0489     /* submit read urb only if the device is waiting for it */
0490     if (!desc->resp_count || !--desc->resp_count)
0491         goto out;
0492 
0493     if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
0494         rv = -ENODEV;
0495         goto out;
0496     }
0497     if (test_bit(WDM_RESETTING, &desc->flags)) {
0498         rv = -EIO;
0499         goto out;
0500     }
0501 
0502     set_bit(WDM_RESPONDING, &desc->flags);
0503     spin_unlock_irq(&desc->iuspin);
0504     rv = usb_submit_urb(desc->response, GFP_KERNEL);
0505     spin_lock_irq(&desc->iuspin);
0506     if (rv) {
0507         if (!test_bit(WDM_DISCONNECTING, &desc->flags))
0508             dev_err(&desc->intf->dev,
0509                 "usb_submit_urb failed with result %d\n", rv);
0510 
0511         /* make sure the next notification trigger a submit */
0512         clear_bit(WDM_RESPONDING, &desc->flags);
0513         desc->resp_count = 0;
0514     }
0515 out:
0516     return rv;
0517 }
0518 
0519 static ssize_t wdm_read
0520 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
0521 {
0522     int rv, cntr;
0523     int i = 0;
0524     struct wdm_device *desc = file->private_data;
0525 
0526 
0527     rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
0528     if (rv < 0)
0529         return -ERESTARTSYS;
0530 
0531     cntr = READ_ONCE(desc->length);
0532     if (cntr == 0) {
0533         desc->read = 0;
0534 retry:
0535         if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
0536             rv = -ENODEV;
0537             goto err;
0538         }
0539         if (test_bit(WDM_OVERFLOW, &desc->flags)) {
0540             clear_bit(WDM_OVERFLOW, &desc->flags);
0541             rv = -ENOBUFS;
0542             goto err;
0543         }
0544         i++;
0545         if (file->f_flags & O_NONBLOCK) {
0546             if (!test_bit(WDM_READ, &desc->flags)) {
0547                 rv = -EAGAIN;
0548                 goto err;
0549             }
0550             rv = 0;
0551         } else {
0552             rv = wait_event_interruptible(desc->wait,
0553                 test_bit(WDM_READ, &desc->flags));
0554         }
0555 
0556         /* may have happened while we slept */
0557         if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
0558             rv = -ENODEV;
0559             goto err;
0560         }
0561         if (test_bit(WDM_RESETTING, &desc->flags)) {
0562             rv = -EIO;
0563             goto err;
0564         }
0565         usb_mark_last_busy(interface_to_usbdev(desc->intf));
0566         if (rv < 0) {
0567             rv = -ERESTARTSYS;
0568             goto err;
0569         }
0570 
0571         spin_lock_irq(&desc->iuspin);
0572 
0573         if (desc->rerr) { /* read completed, error happened */
0574             rv = usb_translate_errors(desc->rerr);
0575             desc->rerr = 0;
0576             spin_unlock_irq(&desc->iuspin);
0577             goto err;
0578         }
0579         /*
0580          * recheck whether we've lost the race
0581          * against the completion handler
0582          */
0583         if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
0584             spin_unlock_irq(&desc->iuspin);
0585             goto retry;
0586         }
0587 
0588         if (!desc->reslength) { /* zero length read */
0589             dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
0590             clear_bit(WDM_READ, &desc->flags);
0591             rv = service_outstanding_interrupt(desc);
0592             spin_unlock_irq(&desc->iuspin);
0593             if (rv < 0)
0594                 goto err;
0595             goto retry;
0596         }
0597         cntr = desc->length;
0598         spin_unlock_irq(&desc->iuspin);
0599     }
0600 
0601     if (cntr > count)
0602         cntr = count;
0603     rv = copy_to_user(buffer, desc->ubuf, cntr);
0604     if (rv > 0) {
0605         rv = -EFAULT;
0606         goto err;
0607     }
0608 
0609     spin_lock_irq(&desc->iuspin);
0610 
0611     for (i = 0; i < desc->length - cntr; i++)
0612         desc->ubuf[i] = desc->ubuf[i + cntr];
0613 
0614     desc->length -= cntr;
0615     /* in case we had outstanding data */
0616     if (!desc->length) {
0617         clear_bit(WDM_READ, &desc->flags);
0618         service_outstanding_interrupt(desc);
0619     }
0620     spin_unlock_irq(&desc->iuspin);
0621     rv = cntr;
0622 
0623 err:
0624     mutex_unlock(&desc->rlock);
0625     return rv;
0626 }
0627 
0628 static int wdm_wait_for_response(struct file *file, long timeout)
0629 {
0630     struct wdm_device *desc = file->private_data;
0631     long rv; /* Use long here because (int) MAX_SCHEDULE_TIMEOUT < 0. */
0632 
0633     /*
0634      * Needs both flags. We cannot do with one because resetting it would
0635      * cause a race with write() yet we need to signal a disconnect.
0636      */
0637     rv = wait_event_interruptible_timeout(desc->wait,
0638                   !test_bit(WDM_IN_USE, &desc->flags) ||
0639                   test_bit(WDM_DISCONNECTING, &desc->flags),
0640                   timeout);
0641 
0642     /*
0643      * To report the correct error. This is best effort.
0644      * We are inevitably racing with the hardware.
0645      */
0646     if (test_bit(WDM_DISCONNECTING, &desc->flags))
0647         return -ENODEV;
0648     if (!rv)
0649         return -EIO;
0650     if (rv < 0)
0651         return -EINTR;
0652 
0653     spin_lock_irq(&desc->iuspin);
0654     rv = desc->werr;
0655     desc->werr = 0;
0656     spin_unlock_irq(&desc->iuspin);
0657 
0658     return usb_translate_errors(rv);
0659 
0660 }
0661 
0662 /*
0663  * You need to send a signal when you react to malicious or defective hardware.
0664  * Also, don't abort when fsync() returned -EINVAL, for older kernels which do
0665  * not implement wdm_flush() will return -EINVAL.
0666  */
0667 static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
0668 {
0669     return wdm_wait_for_response(file, MAX_SCHEDULE_TIMEOUT);
0670 }
0671 
0672 /*
0673  * Same with wdm_fsync(), except it uses finite timeout in order to react to
0674  * malicious or defective hardware which ceased communication after close() was
0675  * implicitly called due to process termination.
0676  */
0677 static int wdm_flush(struct file *file, fl_owner_t id)
0678 {
0679     return wdm_wait_for_response(file, WDM_FLUSH_TIMEOUT);
0680 }
0681 
0682 static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
0683 {
0684     struct wdm_device *desc = file->private_data;
0685     unsigned long flags;
0686     __poll_t mask = 0;
0687 
0688     spin_lock_irqsave(&desc->iuspin, flags);
0689     if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
0690         mask = EPOLLHUP | EPOLLERR;
0691         spin_unlock_irqrestore(&desc->iuspin, flags);
0692         goto desc_out;
0693     }
0694     if (test_bit(WDM_READ, &desc->flags))
0695         mask = EPOLLIN | EPOLLRDNORM;
0696     if (desc->rerr || desc->werr)
0697         mask |= EPOLLERR;
0698     if (!test_bit(WDM_IN_USE, &desc->flags))
0699         mask |= EPOLLOUT | EPOLLWRNORM;
0700     spin_unlock_irqrestore(&desc->iuspin, flags);
0701 
0702     poll_wait(file, &desc->wait, wait);
0703 
0704 desc_out:
0705     return mask;
0706 }
0707 
0708 static int wdm_open(struct inode *inode, struct file *file)
0709 {
0710     int minor = iminor(inode);
0711     int rv = -ENODEV;
0712     struct usb_interface *intf;
0713     struct wdm_device *desc;
0714 
0715     mutex_lock(&wdm_mutex);
0716     desc = wdm_find_device_by_minor(minor);
0717     if (!desc)
0718         goto out;
0719 
0720     intf = desc->intf;
0721     if (test_bit(WDM_DISCONNECTING, &desc->flags))
0722         goto out;
0723     file->private_data = desc;
0724 
0725     if (test_bit(WDM_WWAN_IN_USE, &desc->flags)) {
0726         rv = -EBUSY;
0727         goto out;
0728     }
0729 
0730     rv = usb_autopm_get_interface(desc->intf);
0731     if (rv < 0) {
0732         dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
0733         goto out;
0734     }
0735 
0736     /* using write lock to protect desc->count */
0737     mutex_lock(&desc->wlock);
0738     if (!desc->count++) {
0739         desc->werr = 0;
0740         desc->rerr = 0;
0741         rv = usb_submit_urb(desc->validity, GFP_KERNEL);
0742         if (rv < 0) {
0743             desc->count--;
0744             dev_err(&desc->intf->dev,
0745                 "Error submitting int urb - %d\n", rv);
0746             rv = usb_translate_errors(rv);
0747         }
0748     } else {
0749         rv = 0;
0750     }
0751     mutex_unlock(&desc->wlock);
0752     if (desc->count == 1)
0753         desc->manage_power(intf, 1);
0754     usb_autopm_put_interface(desc->intf);
0755 out:
0756     mutex_unlock(&wdm_mutex);
0757     return rv;
0758 }
0759 
0760 static int wdm_release(struct inode *inode, struct file *file)
0761 {
0762     struct wdm_device *desc = file->private_data;
0763 
0764     mutex_lock(&wdm_mutex);
0765 
0766     /* using write lock to protect desc->count */
0767     mutex_lock(&desc->wlock);
0768     desc->count--;
0769     mutex_unlock(&desc->wlock);
0770 
0771     if (!desc->count) {
0772         if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
0773             dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
0774             poison_urbs(desc);
0775             spin_lock_irq(&desc->iuspin);
0776             desc->resp_count = 0;
0777             clear_bit(WDM_RESPONDING, &desc->flags);
0778             spin_unlock_irq(&desc->iuspin);
0779             desc->manage_power(desc->intf, 0);
0780             unpoison_urbs(desc);
0781         } else {
0782             /* must avoid dev_printk here as desc->intf is invalid */
0783             pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
0784             cleanup(desc);
0785         }
0786     }
0787     mutex_unlock(&wdm_mutex);
0788     return 0;
0789 }
0790 
0791 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0792 {
0793     struct wdm_device *desc = file->private_data;
0794     int rv = 0;
0795 
0796     switch (cmd) {
0797     case IOCTL_WDM_MAX_COMMAND:
0798         if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
0799             rv = -EFAULT;
0800         break;
0801     default:
0802         rv = -ENOTTY;
0803     }
0804     return rv;
0805 }
0806 
0807 static const struct file_operations wdm_fops = {
0808     .owner =    THIS_MODULE,
0809     .read =     wdm_read,
0810     .write =    wdm_write,
0811     .fsync =    wdm_fsync,
0812     .open =     wdm_open,
0813     .flush =    wdm_flush,
0814     .release =  wdm_release,
0815     .poll =     wdm_poll,
0816     .unlocked_ioctl = wdm_ioctl,
0817     .compat_ioctl = compat_ptr_ioctl,
0818     .llseek =   noop_llseek,
0819 };
0820 
0821 static struct usb_class_driver wdm_class = {
0822     .name =     "cdc-wdm%d",
0823     .fops =     &wdm_fops,
0824     .minor_base =   WDM_MINOR_BASE,
0825 };
0826 
0827 /* --- WWAN framework integration --- */
0828 #ifdef CONFIG_WWAN
0829 static int wdm_wwan_port_start(struct wwan_port *port)
0830 {
0831     struct wdm_device *desc = wwan_port_get_drvdata(port);
0832 
0833     /* The interface is both exposed via the WWAN framework and as a
0834      * legacy usbmisc chardev. If chardev is already open, just fail
0835      * to prevent concurrent usage. Otherwise, switch to WWAN mode.
0836      */
0837     mutex_lock(&wdm_mutex);
0838     if (desc->count) {
0839         mutex_unlock(&wdm_mutex);
0840         return -EBUSY;
0841     }
0842     set_bit(WDM_WWAN_IN_USE, &desc->flags);
0843     mutex_unlock(&wdm_mutex);
0844 
0845     desc->manage_power(desc->intf, 1);
0846 
0847     /* tx is allowed */
0848     wwan_port_txon(port);
0849 
0850     /* Start getting events */
0851     return usb_submit_urb(desc->validity, GFP_KERNEL);
0852 }
0853 
0854 static void wdm_wwan_port_stop(struct wwan_port *port)
0855 {
0856     struct wdm_device *desc = wwan_port_get_drvdata(port);
0857 
0858     /* Stop all transfers and disable WWAN mode */
0859     poison_urbs(desc);
0860     desc->manage_power(desc->intf, 0);
0861     clear_bit(WDM_READ, &desc->flags);
0862     clear_bit(WDM_WWAN_IN_USE, &desc->flags);
0863     unpoison_urbs(desc);
0864 }
0865 
0866 static void wdm_wwan_port_tx_complete(struct urb *urb)
0867 {
0868     struct sk_buff *skb = urb->context;
0869     struct wdm_device *desc = skb_shinfo(skb)->destructor_arg;
0870 
0871     usb_autopm_put_interface(desc->intf);
0872     wwan_port_txon(desc->wwanp);
0873     kfree_skb(skb);
0874 }
0875 
0876 static int wdm_wwan_port_tx(struct wwan_port *port, struct sk_buff *skb)
0877 {
0878     struct wdm_device *desc = wwan_port_get_drvdata(port);
0879     struct usb_interface *intf = desc->intf;
0880     struct usb_ctrlrequest *req = desc->orq;
0881     int rv;
0882 
0883     rv = usb_autopm_get_interface(intf);
0884     if (rv)
0885         return rv;
0886 
0887     usb_fill_control_urb(
0888         desc->command,
0889         interface_to_usbdev(intf),
0890         usb_sndctrlpipe(interface_to_usbdev(intf), 0),
0891         (unsigned char *)req,
0892         skb->data,
0893         skb->len,
0894         wdm_wwan_port_tx_complete,
0895         skb
0896     );
0897 
0898     req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
0899     req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
0900     req->wValue = 0;
0901     req->wIndex = desc->inum;
0902     req->wLength = cpu_to_le16(skb->len);
0903 
0904     skb_shinfo(skb)->destructor_arg = desc;
0905 
0906     rv = usb_submit_urb(desc->command, GFP_KERNEL);
0907     if (rv)
0908         usb_autopm_put_interface(intf);
0909     else /* One transfer at a time, stop TX until URB completion */
0910         wwan_port_txoff(port);
0911 
0912     return rv;
0913 }
0914 
0915 static const struct wwan_port_ops wdm_wwan_port_ops = {
0916     .start = wdm_wwan_port_start,
0917     .stop = wdm_wwan_port_stop,
0918     .tx = wdm_wwan_port_tx,
0919 };
0920 
0921 static void wdm_wwan_init(struct wdm_device *desc)
0922 {
0923     struct usb_interface *intf = desc->intf;
0924     struct wwan_port *port;
0925 
0926     /* Only register to WWAN core if protocol/type is known */
0927     if (desc->wwanp_type == WWAN_PORT_UNKNOWN) {
0928         dev_info(&intf->dev, "Unknown control protocol\n");
0929         return;
0930     }
0931 
0932     port = wwan_create_port(&intf->dev, desc->wwanp_type, &wdm_wwan_port_ops, desc);
0933     if (IS_ERR(port)) {
0934         dev_err(&intf->dev, "%s: Unable to create WWAN port\n",
0935             dev_name(intf->usb_dev));
0936         return;
0937     }
0938 
0939     desc->wwanp = port;
0940 }
0941 
0942 static void wdm_wwan_deinit(struct wdm_device *desc)
0943 {
0944     if (!desc->wwanp)
0945         return;
0946 
0947     wwan_remove_port(desc->wwanp);
0948     desc->wwanp = NULL;
0949 }
0950 
0951 static void wdm_wwan_rx(struct wdm_device *desc, int length)
0952 {
0953     struct wwan_port *port = desc->wwanp;
0954     struct sk_buff *skb;
0955 
0956     /* Forward data to WWAN port */
0957     skb = alloc_skb(length, GFP_ATOMIC);
0958     if (!skb)
0959         return;
0960 
0961     memcpy(skb_put(skb, length), desc->inbuf, length);
0962     wwan_port_rx(port, skb);
0963 
0964     /* inbuf has been copied, it is safe to check for outstanding data */
0965     schedule_work(&desc->service_outs_intr);
0966 }
0967 #else /* CONFIG_WWAN */
0968 static void wdm_wwan_init(struct wdm_device *desc) {}
0969 static void wdm_wwan_deinit(struct wdm_device *desc) {}
0970 static void wdm_wwan_rx(struct wdm_device *desc, int length) {}
0971 #endif /* CONFIG_WWAN */
0972 
0973 /* --- error handling --- */
0974 static void wdm_rxwork(struct work_struct *work)
0975 {
0976     struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
0977     unsigned long flags;
0978     int rv = 0;
0979     int responding;
0980 
0981     spin_lock_irqsave(&desc->iuspin, flags);
0982     if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
0983         spin_unlock_irqrestore(&desc->iuspin, flags);
0984     } else {
0985         responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
0986         spin_unlock_irqrestore(&desc->iuspin, flags);
0987         if (!responding)
0988             rv = usb_submit_urb(desc->response, GFP_KERNEL);
0989         if (rv < 0 && rv != -EPERM) {
0990             spin_lock_irqsave(&desc->iuspin, flags);
0991             clear_bit(WDM_RESPONDING, &desc->flags);
0992             if (!test_bit(WDM_DISCONNECTING, &desc->flags))
0993                 schedule_work(&desc->rxwork);
0994             spin_unlock_irqrestore(&desc->iuspin, flags);
0995         }
0996     }
0997 }
0998 
0999 static void service_interrupt_work(struct work_struct *work)
1000 {
1001     struct wdm_device *desc;
1002 
1003     desc = container_of(work, struct wdm_device, service_outs_intr);
1004 
1005     spin_lock_irq(&desc->iuspin);
1006     service_outstanding_interrupt(desc);
1007     if (!desc->resp_count) {
1008         set_bit(WDM_READ, &desc->flags);
1009         wake_up(&desc->wait);
1010     }
1011     spin_unlock_irq(&desc->iuspin);
1012 }
1013 
1014 /* --- hotplug --- */
1015 
1016 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
1017               u16 bufsize, enum wwan_port_type type,
1018               int (*manage_power)(struct usb_interface *, int))
1019 {
1020     int rv = -ENOMEM;
1021     struct wdm_device *desc;
1022 
1023     desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
1024     if (!desc)
1025         goto out;
1026     INIT_LIST_HEAD(&desc->device_list);
1027     mutex_init(&desc->rlock);
1028     mutex_init(&desc->wlock);
1029     spin_lock_init(&desc->iuspin);
1030     init_waitqueue_head(&desc->wait);
1031     desc->wMaxCommand = bufsize;
1032     /* this will be expanded and needed in hardware endianness */
1033     desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
1034     desc->intf = intf;
1035     desc->wwanp_type = type;
1036     INIT_WORK(&desc->rxwork, wdm_rxwork);
1037     INIT_WORK(&desc->service_outs_intr, service_interrupt_work);
1038 
1039     if (!usb_endpoint_is_int_in(ep)) {
1040         rv = -EINVAL;
1041         goto err;
1042     }
1043 
1044     desc->wMaxPacketSize = usb_endpoint_maxp(ep);
1045 
1046     desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1047     if (!desc->orq)
1048         goto err;
1049     desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1050     if (!desc->irq)
1051         goto err;
1052 
1053     desc->validity = usb_alloc_urb(0, GFP_KERNEL);
1054     if (!desc->validity)
1055         goto err;
1056 
1057     desc->response = usb_alloc_urb(0, GFP_KERNEL);
1058     if (!desc->response)
1059         goto err;
1060 
1061     desc->command = usb_alloc_urb(0, GFP_KERNEL);
1062     if (!desc->command)
1063         goto err;
1064 
1065     desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
1066     if (!desc->ubuf)
1067         goto err;
1068 
1069     desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
1070     if (!desc->sbuf)
1071         goto err;
1072 
1073     desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
1074     if (!desc->inbuf)
1075         goto err;
1076 
1077     usb_fill_int_urb(
1078         desc->validity,
1079         interface_to_usbdev(intf),
1080         usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
1081         desc->sbuf,
1082         desc->wMaxPacketSize,
1083         wdm_int_callback,
1084         desc,
1085         ep->bInterval
1086     );
1087 
1088     desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
1089     desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1090     desc->irq->wValue = 0;
1091     desc->irq->wIndex = desc->inum; /* already converted */
1092     desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
1093 
1094     usb_fill_control_urb(
1095         desc->response,
1096         interface_to_usbdev(intf),
1097         /* using common endpoint 0 */
1098         usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
1099         (unsigned char *)desc->irq,
1100         desc->inbuf,
1101         desc->wMaxCommand,
1102         wdm_in_callback,
1103         desc
1104     );
1105 
1106     desc->manage_power = manage_power;
1107 
1108     spin_lock(&wdm_device_list_lock);
1109     list_add(&desc->device_list, &wdm_device_list);
1110     spin_unlock(&wdm_device_list_lock);
1111 
1112     rv = usb_register_dev(intf, &wdm_class);
1113     if (rv < 0)
1114         goto err;
1115     else
1116         dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
1117 
1118     wdm_wwan_init(desc);
1119 
1120 out:
1121     return rv;
1122 err:
1123     spin_lock(&wdm_device_list_lock);
1124     list_del(&desc->device_list);
1125     spin_unlock(&wdm_device_list_lock);
1126     cleanup(desc);
1127     return rv;
1128 }
1129 
1130 static int wdm_manage_power(struct usb_interface *intf, int on)
1131 {
1132     /* need autopm_get/put here to ensure the usbcore sees the new value */
1133     int rv = usb_autopm_get_interface(intf);
1134 
1135     intf->needs_remote_wakeup = on;
1136     if (!rv)
1137         usb_autopm_put_interface(intf);
1138     return 0;
1139 }
1140 
1141 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
1142 {
1143     int rv = -EINVAL;
1144     struct usb_host_interface *iface;
1145     struct usb_endpoint_descriptor *ep;
1146     struct usb_cdc_parsed_header hdr;
1147     u8 *buffer = intf->altsetting->extra;
1148     int buflen = intf->altsetting->extralen;
1149     u16 maxcom = WDM_DEFAULT_BUFSIZE;
1150 
1151     if (!buffer)
1152         goto err;
1153 
1154     cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
1155 
1156     if (hdr.usb_cdc_dmm_desc)
1157         maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
1158 
1159     iface = intf->cur_altsetting;
1160     if (iface->desc.bNumEndpoints != 1)
1161         goto err;
1162     ep = &iface->endpoint[0].desc;
1163 
1164     rv = wdm_create(intf, ep, maxcom, WWAN_PORT_UNKNOWN, &wdm_manage_power);
1165 
1166 err:
1167     return rv;
1168 }
1169 
1170 /**
1171  * usb_cdc_wdm_register - register a WDM subdriver
1172  * @intf: usb interface the subdriver will associate with
1173  * @ep: interrupt endpoint to monitor for notifications
1174  * @bufsize: maximum message size to support for read/write
1175  * @type: Type/protocol of the transported data (MBIM, QMI...)
1176  * @manage_power: call-back invoked during open and release to
1177  *                manage the device's power
1178  * Create WDM usb class character device and associate it with intf
1179  * without binding, allowing another driver to manage the interface.
1180  *
1181  * The subdriver will manage the given interrupt endpoint exclusively
1182  * and will issue control requests referring to the given intf. It
1183  * will otherwise avoid interferring, and in particular not do
1184  * usb_set_intfdata/usb_get_intfdata on intf.
1185  *
1186  * The return value is a pointer to the subdriver's struct usb_driver.
1187  * The registering driver is responsible for calling this subdriver's
1188  * disconnect, suspend, resume, pre_reset and post_reset methods from
1189  * its own.
1190  */
1191 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
1192                     struct usb_endpoint_descriptor *ep,
1193                     int bufsize, enum wwan_port_type type,
1194                     int (*manage_power)(struct usb_interface *, int))
1195 {
1196     int rv;
1197 
1198     rv = wdm_create(intf, ep, bufsize, type, manage_power);
1199     if (rv < 0)
1200         goto err;
1201 
1202     return &wdm_driver;
1203 err:
1204     return ERR_PTR(rv);
1205 }
1206 EXPORT_SYMBOL(usb_cdc_wdm_register);
1207 
1208 static void wdm_disconnect(struct usb_interface *intf)
1209 {
1210     struct wdm_device *desc;
1211     unsigned long flags;
1212 
1213     usb_deregister_dev(intf, &wdm_class);
1214     desc = wdm_find_device(intf);
1215     mutex_lock(&wdm_mutex);
1216 
1217     wdm_wwan_deinit(desc);
1218 
1219     /* the spinlock makes sure no new urbs are generated in the callbacks */
1220     spin_lock_irqsave(&desc->iuspin, flags);
1221     set_bit(WDM_DISCONNECTING, &desc->flags);
1222     set_bit(WDM_READ, &desc->flags);
1223     spin_unlock_irqrestore(&desc->iuspin, flags);
1224     wake_up_all(&desc->wait);
1225     mutex_lock(&desc->rlock);
1226     mutex_lock(&desc->wlock);
1227     poison_urbs(desc);
1228     cancel_work_sync(&desc->rxwork);
1229     cancel_work_sync(&desc->service_outs_intr);
1230     mutex_unlock(&desc->wlock);
1231     mutex_unlock(&desc->rlock);
1232 
1233     /* the desc->intf pointer used as list key is now invalid */
1234     spin_lock(&wdm_device_list_lock);
1235     list_del(&desc->device_list);
1236     spin_unlock(&wdm_device_list_lock);
1237 
1238     if (!desc->count)
1239         cleanup(desc);
1240     else
1241         dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
1242     mutex_unlock(&wdm_mutex);
1243 }
1244 
1245 #ifdef CONFIG_PM
1246 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
1247 {
1248     struct wdm_device *desc = wdm_find_device(intf);
1249     int rv = 0;
1250 
1251     dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1252 
1253     /* if this is an autosuspend the caller does the locking */
1254     if (!PMSG_IS_AUTO(message)) {
1255         mutex_lock(&desc->rlock);
1256         mutex_lock(&desc->wlock);
1257     }
1258     spin_lock_irq(&desc->iuspin);
1259 
1260     if (PMSG_IS_AUTO(message) &&
1261             (test_bit(WDM_IN_USE, &desc->flags)
1262             || test_bit(WDM_RESPONDING, &desc->flags))) {
1263         spin_unlock_irq(&desc->iuspin);
1264         rv = -EBUSY;
1265     } else {
1266 
1267         set_bit(WDM_SUSPENDING, &desc->flags);
1268         spin_unlock_irq(&desc->iuspin);
1269         /* callback submits work - order is essential */
1270         poison_urbs(desc);
1271         cancel_work_sync(&desc->rxwork);
1272         cancel_work_sync(&desc->service_outs_intr);
1273         unpoison_urbs(desc);
1274     }
1275     if (!PMSG_IS_AUTO(message)) {
1276         mutex_unlock(&desc->wlock);
1277         mutex_unlock(&desc->rlock);
1278     }
1279 
1280     return rv;
1281 }
1282 #endif
1283 
1284 static int recover_from_urb_loss(struct wdm_device *desc)
1285 {
1286     int rv = 0;
1287 
1288     if (desc->count) {
1289         rv = usb_submit_urb(desc->validity, GFP_NOIO);
1290         if (rv < 0)
1291             dev_err(&desc->intf->dev,
1292                 "Error resume submitting int urb - %d\n", rv);
1293     }
1294     return rv;
1295 }
1296 
1297 #ifdef CONFIG_PM
1298 static int wdm_resume(struct usb_interface *intf)
1299 {
1300     struct wdm_device *desc = wdm_find_device(intf);
1301     int rv;
1302 
1303     dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1304 
1305     clear_bit(WDM_SUSPENDING, &desc->flags);
1306     rv = recover_from_urb_loss(desc);
1307 
1308     return rv;
1309 }
1310 #endif
1311 
1312 static int wdm_pre_reset(struct usb_interface *intf)
1313 {
1314     struct wdm_device *desc = wdm_find_device(intf);
1315 
1316     /*
1317      * we notify everybody using poll of
1318      * an exceptional situation
1319      * must be done before recovery lest a spontaneous
1320      * message from the device is lost
1321      */
1322     spin_lock_irq(&desc->iuspin);
1323     set_bit(WDM_RESETTING, &desc->flags);   /* inform read/write */
1324     set_bit(WDM_READ, &desc->flags);    /* unblock read */
1325     clear_bit(WDM_IN_USE, &desc->flags);    /* unblock write */
1326     desc->rerr = -EINTR;
1327     spin_unlock_irq(&desc->iuspin);
1328     wake_up_all(&desc->wait);
1329     mutex_lock(&desc->rlock);
1330     mutex_lock(&desc->wlock);
1331     poison_urbs(desc);
1332     cancel_work_sync(&desc->rxwork);
1333     cancel_work_sync(&desc->service_outs_intr);
1334     return 0;
1335 }
1336 
1337 static int wdm_post_reset(struct usb_interface *intf)
1338 {
1339     struct wdm_device *desc = wdm_find_device(intf);
1340     int rv;
1341 
1342     unpoison_urbs(desc);
1343     clear_bit(WDM_OVERFLOW, &desc->flags);
1344     clear_bit(WDM_RESETTING, &desc->flags);
1345     rv = recover_from_urb_loss(desc);
1346     mutex_unlock(&desc->wlock);
1347     mutex_unlock(&desc->rlock);
1348     return rv;
1349 }
1350 
1351 static struct usb_driver wdm_driver = {
1352     .name =     "cdc_wdm",
1353     .probe =    wdm_probe,
1354     .disconnect =   wdm_disconnect,
1355 #ifdef CONFIG_PM
1356     .suspend =  wdm_suspend,
1357     .resume =   wdm_resume,
1358     .reset_resume = wdm_resume,
1359 #endif
1360     .pre_reset =    wdm_pre_reset,
1361     .post_reset =   wdm_post_reset,
1362     .id_table = wdm_ids,
1363     .supports_autosuspend = 1,
1364     .disable_hub_initiated_lpm = 1,
1365 };
1366 
1367 module_usb_driver(wdm_driver);
1368 
1369 MODULE_AUTHOR(DRIVER_AUTHOR);
1370 MODULE_DESCRIPTION(DRIVER_DESC);
1371 MODULE_LICENSE("GPL");