--- zzzz-none-000/linux-3.10.107/drivers/usb/class/cdc-wdm.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/usb/class/cdc-wdm.c 2021-02-04 17:41:59.000000000 +0000 @@ -101,6 +101,7 @@ struct work_struct rxwork; int werr; int rerr; + int resp_count; struct list_head device_list; int (*manage_power)(struct usb_interface *, int); @@ -253,6 +254,10 @@ "NOTIFY_NETWORK_CONNECTION %s network", dr->wValue ? "connected to" : "disconnected from"); goto exit; + case USB_CDC_NOTIFY_SPEED_CHANGE: + dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)", + urb->actual_length); + goto exit; default: clear_bit(WDM_POLL_RUNNING, &desc->flags); dev_err(&desc->intf->dev, @@ -264,9 +269,9 @@ } spin_lock(&desc->iuspin); - clear_bit(WDM_READ, &desc->flags); responding = test_and_set_bit(WDM_RESPONDING, &desc->flags); - if (!responding && !test_bit(WDM_DISCONNECTING, &desc->flags) + if (!desc->resp_count++ && !responding + && !test_bit(WDM_DISCONNECTING, &desc->flags) && !test_bit(WDM_SUSPENDING, &desc->flags)) { rv = usb_submit_urb(desc->response, GFP_ATOMIC); dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", @@ -336,7 +341,7 @@ desc->werr = 0; spin_unlock_irq(&desc->iuspin); if (we < 0) - return -EIO; + return usb_translate_errors(we); buf = kmalloc(count, GFP_KERNEL); if (!buf) { @@ -346,30 +351,25 @@ r = copy_from_user(buf, buffer, count); if (r > 0) { - kfree(buf); rv = -EFAULT; - goto outnl; + goto out_free_mem; } /* concurrent writes and disconnect */ r = mutex_lock_interruptible(&desc->wlock); rv = -ERESTARTSYS; - if (r) { - kfree(buf); - goto outnl; - } + if (r) + goto out_free_mem; if (test_bit(WDM_DISCONNECTING, &desc->flags)) { - kfree(buf); rv = -ENODEV; - goto outnp; + goto out_free_mem_lock; } r = usb_autopm_get_interface(desc->intf); if (r < 0) { - kfree(buf); rv = usb_translate_errors(r); - goto outnp; + goto out_free_mem_lock; } if (!(file->f_flags & O_NONBLOCK)) @@ -383,9 +383,8 @@ r = -EIO; if (r < 0) { - kfree(buf); rv = r; - goto out; + goto out_free_mem_pm; } req = desc->orq; @@ -412,21 +411,60 @@ rv = usb_submit_urb(desc->command, GFP_KERNEL); if (rv < 0) { - kfree(buf); desc->outbuf = NULL; clear_bit(WDM_IN_USE, &desc->flags); dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); rv = usb_translate_errors(rv); + goto out_free_mem_pm; } else { dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", le16_to_cpu(req->wIndex)); } -out: + usb_autopm_put_interface(desc->intf); -outnp: mutex_unlock(&desc->wlock); outnl: return rv < 0 ? rv : count; + +out_free_mem_pm: + usb_autopm_put_interface(desc->intf); +out_free_mem_lock: + mutex_unlock(&desc->wlock); +out_free_mem: + kfree(buf); + return rv; +} + +/* + * clear WDM_READ flag and possibly submit the read urb if resp_count + * is non-zero. + * + * Called with desc->iuspin locked + */ +static int clear_wdm_read_flag(struct wdm_device *desc) +{ + int rv = 0; + + clear_bit(WDM_READ, &desc->flags); + + /* submit read urb only if the device is waiting for it */ + if (!desc->resp_count || !--desc->resp_count) + goto out; + + set_bit(WDM_RESPONDING, &desc->flags); + spin_unlock_irq(&desc->iuspin); + rv = usb_submit_urb(desc->response, GFP_KERNEL); + spin_lock_irq(&desc->iuspin); + if (rv) { + dev_err(&desc->intf->dev, + "usb_submit_urb failed with result %d\n", rv); + + /* make sure the next notification trigger a submit */ + clear_bit(WDM_RESPONDING, &desc->flags); + desc->resp_count = 0; + } +out: + return rv; } static ssize_t wdm_read @@ -484,9 +522,9 @@ spin_lock_irq(&desc->iuspin); if (desc->rerr) { /* read completed, error happened */ + rv = usb_translate_errors(desc->rerr); desc->rerr = 0; spin_unlock_irq(&desc->iuspin); - rv = -EIO; goto err; } /* @@ -500,8 +538,10 @@ if (!desc->reslength) { /* zero length read */ dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__); - clear_bit(WDM_READ, &desc->flags); + rv = clear_wdm_read_flag(desc); spin_unlock_irq(&desc->iuspin); + if (rv < 0) + goto err; goto retry; } cntr = desc->length; @@ -524,10 +564,8 @@ desc->length -= cntr; /* in case we had outstanding data */ if (!desc->length) - clear_bit(WDM_READ, &desc->flags); - + clear_wdm_read_flag(desc); spin_unlock_irq(&desc->iuspin); - rv = cntr; err: @@ -637,6 +675,9 @@ if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); kill_urbs(desc); + spin_lock_irq(&desc->iuspin); + desc->resp_count = 0; + spin_unlock_irq(&desc->iuspin); desc->manage_power(desc->intf, 0); } else { /* must avoid dev_printk here as desc->intf is invalid */