| .. | .. |
|---|
| 29 | 29 | #include <linux/mutex.h> |
|---|
| 30 | 30 | #include <linux/random.h> |
|---|
| 31 | 31 | #include <linux/pm_qos.h> |
|---|
| 32 | +#include <linux/kobject.h> |
|---|
| 32 | 33 | |
|---|
| 33 | 34 | #include <linux/uaccess.h> |
|---|
| 34 | 35 | #include <asm/byteorder.h> |
|---|
| 35 | 36 | |
|---|
| 36 | 37 | #include "hub.h" |
|---|
| 37 | | -#include "otg_whitelist.h" |
|---|
| 38 | +#include "otg_productlist.h" |
|---|
| 38 | 39 | |
|---|
| 39 | 40 | #define USB_VENDOR_GENESYS_LOGIC 0x05e3 |
|---|
| 40 | 41 | #define USB_VENDOR_SMSC 0x0424 |
|---|
| .. | .. |
|---|
| 95 | 96 | MODULE_PARM_DESC(old_scheme_first, |
|---|
| 96 | 97 | "start with the old device initialization scheme"); |
|---|
| 97 | 98 | |
|---|
| 98 | | -static bool use_both_schemes = 1; |
|---|
| 99 | +static bool use_both_schemes = true; |
|---|
| 99 | 100 | module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); |
|---|
| 100 | 101 | MODULE_PARM_DESC(use_both_schemes, |
|---|
| 101 | 102 | "try the other device initialization scheme if the " |
|---|
| .. | .. |
|---|
| 618 | 619 | status, change, NULL); |
|---|
| 619 | 620 | } |
|---|
| 620 | 621 | |
|---|
| 622 | +static void hub_resubmit_irq_urb(struct usb_hub *hub) |
|---|
| 623 | +{ |
|---|
| 624 | + unsigned long flags; |
|---|
| 625 | + int status; |
|---|
| 626 | + |
|---|
| 627 | + spin_lock_irqsave(&hub->irq_urb_lock, flags); |
|---|
| 628 | + |
|---|
| 629 | + if (hub->quiescing) { |
|---|
| 630 | + spin_unlock_irqrestore(&hub->irq_urb_lock, flags); |
|---|
| 631 | + return; |
|---|
| 632 | + } |
|---|
| 633 | + |
|---|
| 634 | + status = usb_submit_urb(hub->urb, GFP_ATOMIC); |
|---|
| 635 | + if (status && status != -ENODEV && status != -EPERM && |
|---|
| 636 | + status != -ESHUTDOWN) { |
|---|
| 637 | + dev_err(hub->intfdev, "resubmit --> %d\n", status); |
|---|
| 638 | + mod_timer(&hub->irq_urb_retry, jiffies + HZ); |
|---|
| 639 | + } |
|---|
| 640 | + |
|---|
| 641 | + spin_unlock_irqrestore(&hub->irq_urb_lock, flags); |
|---|
| 642 | +} |
|---|
| 643 | + |
|---|
| 644 | +static void hub_retry_irq_urb(struct timer_list *t) |
|---|
| 645 | +{ |
|---|
| 646 | + struct usb_hub *hub = from_timer(hub, t, irq_urb_retry); |
|---|
| 647 | + |
|---|
| 648 | + hub_resubmit_irq_urb(hub); |
|---|
| 649 | +} |
|---|
| 650 | + |
|---|
| 651 | + |
|---|
| 621 | 652 | static void kick_hub_wq(struct usb_hub *hub) |
|---|
| 622 | 653 | { |
|---|
| 623 | 654 | struct usb_interface *intf; |
|---|
| .. | .. |
|---|
| 702 | 733 | if ((++hub->nerrors < 10) || hub->error) |
|---|
| 703 | 734 | goto resubmit; |
|---|
| 704 | 735 | hub->error = status; |
|---|
| 705 | | - /* FALL THROUGH */ |
|---|
| 736 | + fallthrough; |
|---|
| 706 | 737 | |
|---|
| 707 | 738 | /* let hub_wq handle things */ |
|---|
| 708 | 739 | case 0: /* we got data: port status changed */ |
|---|
| .. | .. |
|---|
| 720 | 751 | kick_hub_wq(hub); |
|---|
| 721 | 752 | |
|---|
| 722 | 753 | resubmit: |
|---|
| 723 | | - if (hub->quiescing) |
|---|
| 724 | | - return; |
|---|
| 725 | | - |
|---|
| 726 | | - status = usb_submit_urb(hub->urb, GFP_ATOMIC); |
|---|
| 727 | | - if (status != 0 && status != -ENODEV && status != -EPERM) |
|---|
| 728 | | - dev_err(hub->intfdev, "resubmit --> %d\n", status); |
|---|
| 754 | + hub_resubmit_irq_urb(hub); |
|---|
| 729 | 755 | } |
|---|
| 730 | 756 | |
|---|
| 731 | 757 | /* USB 2.0 spec Section 11.24.2.3 */ |
|---|
| .. | .. |
|---|
| 857 | 883 | /* info that CLEAR_TT_BUFFER needs */ |
|---|
| 858 | 884 | clear->tt = tt->multi ? udev->ttport : 1; |
|---|
| 859 | 885 | clear->devinfo = usb_pipeendpoint (pipe); |
|---|
| 860 | | - clear->devinfo |= udev->devnum << 4; |
|---|
| 886 | + clear->devinfo |= ((u16)udev->devaddr) << 4; |
|---|
| 861 | 887 | clear->devinfo |= usb_pipecontrol(pipe) |
|---|
| 862 | 888 | ? (USB_ENDPOINT_XFER_CONTROL << 11) |
|---|
| 863 | 889 | : (USB_ENDPOINT_XFER_BULK << 11); |
|---|
| .. | .. |
|---|
| 1288 | 1314 | static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) |
|---|
| 1289 | 1315 | { |
|---|
| 1290 | 1316 | struct usb_device *hdev = hub->hdev; |
|---|
| 1317 | + unsigned long flags; |
|---|
| 1291 | 1318 | int i; |
|---|
| 1292 | 1319 | |
|---|
| 1293 | 1320 | /* hub_wq and related activity won't re-trigger */ |
|---|
| 1321 | + spin_lock_irqsave(&hub->irq_urb_lock, flags); |
|---|
| 1294 | 1322 | hub->quiescing = 1; |
|---|
| 1323 | + spin_unlock_irqrestore(&hub->irq_urb_lock, flags); |
|---|
| 1295 | 1324 | |
|---|
| 1296 | 1325 | if (type != HUB_SUSPEND) { |
|---|
| 1297 | 1326 | /* Disconnect all the children */ |
|---|
| .. | .. |
|---|
| 1302 | 1331 | } |
|---|
| 1303 | 1332 | |
|---|
| 1304 | 1333 | /* Stop hub_wq and related activity */ |
|---|
| 1334 | + del_timer_sync(&hub->irq_urb_retry); |
|---|
| 1305 | 1335 | usb_kill_urb(hub->urb); |
|---|
| 1306 | 1336 | if (hub->has_indicators) |
|---|
| 1307 | 1337 | cancel_delayed_work_sync(&hub->leds); |
|---|
| .. | .. |
|---|
| 1814 | 1844 | return -E2BIG; |
|---|
| 1815 | 1845 | } |
|---|
| 1816 | 1846 | |
|---|
| 1817 | | -#ifdef CONFIG_USB_OTG_BLACKLIST_HUB |
|---|
| 1847 | +#ifdef CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB |
|---|
| 1818 | 1848 | if (hdev->parent) { |
|---|
| 1819 | 1849 | dev_warn(&intf->dev, "ignoring external hub\n"); |
|---|
| 1820 | 1850 | return -ENODEV; |
|---|
| .. | .. |
|---|
| 1839 | 1869 | INIT_DELAYED_WORK(&hub->leds, led_work); |
|---|
| 1840 | 1870 | INIT_DELAYED_WORK(&hub->init_work, NULL); |
|---|
| 1841 | 1871 | INIT_WORK(&hub->events, hub_event); |
|---|
| 1872 | + spin_lock_init(&hub->irq_urb_lock); |
|---|
| 1873 | + timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0); |
|---|
| 1842 | 1874 | usb_get_intf(intf); |
|---|
| 1843 | 1875 | usb_get_dev(hdev); |
|---|
| 1844 | 1876 | |
|---|
| .. | .. |
|---|
| 2121 | 2153 | /* The address for a WUSB device is managed by wusbcore. */ |
|---|
| 2122 | 2154 | if (!udev->wusb) |
|---|
| 2123 | 2155 | udev->devnum = devnum; |
|---|
| 2156 | + if (!udev->devaddr) |
|---|
| 2157 | + udev->devaddr = (u8)devnum; |
|---|
| 2124 | 2158 | } |
|---|
| 2125 | 2159 | |
|---|
| 2126 | 2160 | static void hub_free_dev(struct usb_device *udev) |
|---|
| .. | .. |
|---|
| 2379 | 2413 | if (err < 0) |
|---|
| 2380 | 2414 | return err; |
|---|
| 2381 | 2415 | |
|---|
| 2382 | | - if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support && |
|---|
| 2416 | + if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support && |
|---|
| 2383 | 2417 | !is_targeted(udev)) { |
|---|
| 2384 | 2418 | /* Maybe it can talk to us, though we can't talk to it. |
|---|
| 2385 | 2419 | * (Includes HNP test device.) |
|---|
| .. | .. |
|---|
| 2681 | 2715 | } |
|---|
| 2682 | 2716 | |
|---|
| 2683 | 2717 | |
|---|
| 2718 | +#ifdef CONFIG_USB_FEW_INIT_RETRIES |
|---|
| 2719 | +#define PORT_RESET_TRIES 2 |
|---|
| 2720 | +#define SET_ADDRESS_TRIES 1 |
|---|
| 2721 | +#define GET_DESCRIPTOR_TRIES 1 |
|---|
| 2722 | +#define GET_MAXPACKET0_TRIES 1 |
|---|
| 2723 | +#define PORT_INIT_TRIES 4 |
|---|
| 2724 | + |
|---|
| 2725 | +#else |
|---|
| 2684 | 2726 | #define PORT_RESET_TRIES 5 |
|---|
| 2685 | 2727 | #define SET_ADDRESS_TRIES 2 |
|---|
| 2686 | 2728 | #define GET_DESCRIPTOR_TRIES 2 |
|---|
| 2687 | | -#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) |
|---|
| 2688 | | -#define USE_NEW_SCHEME(i, scheme) ((i) / 2 == (int)(scheme)) |
|---|
| 2729 | +#define GET_MAXPACKET0_TRIES 3 |
|---|
| 2730 | +#define PORT_INIT_TRIES 4 |
|---|
| 2731 | +#endif /* CONFIG_USB_FEW_INIT_RETRIES */ |
|---|
| 2689 | 2732 | |
|---|
| 2690 | 2733 | #define HUB_ROOT_RESET_TIME 60 /* times are in msec */ |
|---|
| 2691 | 2734 | #define HUB_SHORT_RESET_TIME 10 |
|---|
| .. | .. |
|---|
| 2693 | 2736 | #define HUB_LONG_RESET_TIME 200 |
|---|
| 2694 | 2737 | #define HUB_RESET_TIMEOUT 800 |
|---|
| 2695 | 2738 | |
|---|
| 2696 | | -/* |
|---|
| 2697 | | - * "New scheme" enumeration causes an extra state transition to be |
|---|
| 2698 | | - * exposed to an xhci host and causes USB3 devices to receive control |
|---|
| 2699 | | - * commands in the default state. This has been seen to cause |
|---|
| 2700 | | - * enumeration failures, so disable this enumeration scheme for USB3 |
|---|
| 2701 | | - * devices. |
|---|
| 2702 | | - */ |
|---|
| 2703 | 2739 | static bool use_new_scheme(struct usb_device *udev, int retry, |
|---|
| 2704 | 2740 | struct usb_port *port_dev) |
|---|
| 2705 | 2741 | { |
|---|
| 2706 | 2742 | int old_scheme_first_port = |
|---|
| 2707 | | - port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME; |
|---|
| 2743 | + (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || |
|---|
| 2744 | + old_scheme_first; |
|---|
| 2708 | 2745 | |
|---|
| 2746 | + /* |
|---|
| 2747 | + * "New scheme" enumeration causes an extra state transition to be |
|---|
| 2748 | + * exposed to an xhci host and causes USB3 devices to receive control |
|---|
| 2749 | + * commands in the default state. This has been seen to cause |
|---|
| 2750 | + * enumeration failures, so disable this enumeration scheme for USB3 |
|---|
| 2751 | + * devices. |
|---|
| 2752 | + */ |
|---|
| 2709 | 2753 | if (udev->speed >= USB_SPEED_SUPER) |
|---|
| 2710 | 2754 | return false; |
|---|
| 2711 | 2755 | |
|---|
| 2712 | | - return USE_NEW_SCHEME(retry, old_scheme_first_port || old_scheme_first); |
|---|
| 2756 | + /* |
|---|
| 2757 | + * If use_both_schemes is set, use the first scheme (whichever |
|---|
| 2758 | + * it is) for the larger half of the retries, then use the other |
|---|
| 2759 | + * scheme. Otherwise, use the first scheme for all the retries. |
|---|
| 2760 | + */ |
|---|
| 2761 | + if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2) |
|---|
| 2762 | + return old_scheme_first_port; /* Second half */ |
|---|
| 2763 | + return !old_scheme_first_port; /* First half or all */ |
|---|
| 2713 | 2764 | } |
|---|
| 2714 | 2765 | |
|---|
| 2715 | 2766 | /* Is a USB 3.0 port in the Inactive or Compliance Mode state? |
|---|
| 2716 | | - * Port worm reset is required to recover |
|---|
| 2767 | + * Port warm reset is required to recover |
|---|
| 2717 | 2768 | */ |
|---|
| 2718 | 2769 | static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, |
|---|
| 2719 | 2770 | u16 portstatus) |
|---|
| .. | .. |
|---|
| 3177 | 3228 | } |
|---|
| 3178 | 3229 | |
|---|
| 3179 | 3230 | /* Count of wakeup-enabled devices at or below udev */ |
|---|
| 3180 | | -static unsigned wakeup_enabled_descendants(struct usb_device *udev) |
|---|
| 3231 | +unsigned usb_wakeup_enabled_descendants(struct usb_device *udev) |
|---|
| 3181 | 3232 | { |
|---|
| 3182 | 3233 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); |
|---|
| 3183 | 3234 | |
|---|
| 3184 | 3235 | return udev->do_remote_wakeup + |
|---|
| 3185 | 3236 | (hub ? hub->wakeup_enabled_descendants : 0); |
|---|
| 3186 | 3237 | } |
|---|
| 3238 | +EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants); |
|---|
| 3187 | 3239 | |
|---|
| 3188 | 3240 | /* |
|---|
| 3189 | 3241 | * usb_port_suspend - suspend a usb device's upstream port |
|---|
| .. | .. |
|---|
| 3285 | 3337 | * Therefore we will turn on the suspend feature if udev or any of its |
|---|
| 3286 | 3338 | * descendants is enabled for remote wakeup. |
|---|
| 3287 | 3339 | */ |
|---|
| 3288 | | - else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0) |
|---|
| 3340 | + else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0) |
|---|
| 3289 | 3341 | status = set_port_feature(hub->hdev, port1, |
|---|
| 3290 | 3342 | USB_PORT_FEAT_SUSPEND); |
|---|
| 3291 | 3343 | else { |
|---|
| .. | .. |
|---|
| 3513 | 3565 | u16 portchange, portstatus; |
|---|
| 3514 | 3566 | |
|---|
| 3515 | 3567 | if (!test_and_set_bit(port1, hub->child_usage_bits)) { |
|---|
| 3516 | | - status = pm_runtime_get_sync(&port_dev->dev); |
|---|
| 3568 | + status = pm_runtime_resume_and_get(&port_dev->dev); |
|---|
| 3517 | 3569 | if (status < 0) { |
|---|
| 3518 | 3570 | dev_dbg(&udev->dev, "can't resume usb port, status %d\n", |
|---|
| 3519 | 3571 | status); |
|---|
| .. | .. |
|---|
| 3674 | 3726 | struct usb_hub *hub = usb_get_intfdata(intf); |
|---|
| 3675 | 3727 | struct usb_device *hdev = hub->hdev; |
|---|
| 3676 | 3728 | unsigned port1; |
|---|
| 3677 | | - int status; |
|---|
| 3678 | 3729 | |
|---|
| 3679 | 3730 | /* |
|---|
| 3680 | 3731 | * Warn if children aren't already suspended. |
|---|
| .. | .. |
|---|
| 3693 | 3744 | } |
|---|
| 3694 | 3745 | if (udev) |
|---|
| 3695 | 3746 | hub->wakeup_enabled_descendants += |
|---|
| 3696 | | - wakeup_enabled_descendants(udev); |
|---|
| 3747 | + usb_wakeup_enabled_descendants(udev); |
|---|
| 3697 | 3748 | } |
|---|
| 3698 | 3749 | |
|---|
| 3699 | 3750 | if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { |
|---|
| .. | .. |
|---|
| 3708 | 3759 | if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { |
|---|
| 3709 | 3760 | /* Enable hub to send remote wakeup for all ports. */ |
|---|
| 3710 | 3761 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
|---|
| 3711 | | - status = set_port_feature(hdev, |
|---|
| 3712 | | - port1 | |
|---|
| 3713 | | - USB_PORT_FEAT_REMOTE_WAKE_CONNECT | |
|---|
| 3714 | | - USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | |
|---|
| 3715 | | - USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, |
|---|
| 3716 | | - USB_PORT_FEAT_REMOTE_WAKE_MASK); |
|---|
| 3762 | + set_port_feature(hdev, |
|---|
| 3763 | + port1 | |
|---|
| 3764 | + USB_PORT_FEAT_REMOTE_WAKE_CONNECT | |
|---|
| 3765 | + USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | |
|---|
| 3766 | + USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, |
|---|
| 3767 | + USB_PORT_FEAT_REMOTE_WAKE_MASK); |
|---|
| 3717 | 3768 | } |
|---|
| 3718 | 3769 | } |
|---|
| 3719 | 3770 | |
|---|
| .. | .. |
|---|
| 4193 | 4244 | if (!udev || !udev->parent || |
|---|
| 4194 | 4245 | udev->speed < USB_SPEED_SUPER || |
|---|
| 4195 | 4246 | !udev->lpm_capable || |
|---|
| 4196 | | - udev->state < USB_STATE_DEFAULT) |
|---|
| 4247 | + udev->state < USB_STATE_CONFIGURED) |
|---|
| 4197 | 4248 | return 0; |
|---|
| 4198 | 4249 | |
|---|
| 4199 | 4250 | hcd = bus_to_hcd(udev->bus); |
|---|
| .. | .. |
|---|
| 4252 | 4303 | if (!udev || !udev->parent || |
|---|
| 4253 | 4304 | udev->speed < USB_SPEED_SUPER || |
|---|
| 4254 | 4305 | !udev->lpm_capable || |
|---|
| 4255 | | - udev->state < USB_STATE_DEFAULT) |
|---|
| 4306 | + udev->state < USB_STATE_CONFIGURED) |
|---|
| 4256 | 4307 | return; |
|---|
| 4257 | 4308 | |
|---|
| 4258 | 4309 | udev->lpm_disable_count--; |
|---|
| .. | .. |
|---|
| 4565 | 4616 | const char *speed; |
|---|
| 4566 | 4617 | int devnum = udev->devnum; |
|---|
| 4567 | 4618 | const char *driver_name; |
|---|
| 4619 | + bool do_new_scheme; |
|---|
| 4568 | 4620 | |
|---|
| 4569 | 4621 | /* root hub ports have a slightly longer reset period |
|---|
| 4570 | 4622 | * (from USB 2.0 spec, section 7.1.7.5) |
|---|
| .. | .. |
|---|
| 4675 | 4727 | * first 8 bytes of the device descriptor to get the ep0 maxpacket |
|---|
| 4676 | 4728 | * value. |
|---|
| 4677 | 4729 | */ |
|---|
| 4678 | | - for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { |
|---|
| 4679 | | - bool did_new_scheme = false; |
|---|
| 4730 | + do_new_scheme = use_new_scheme(udev, retry_counter, port_dev); |
|---|
| 4680 | 4731 | |
|---|
| 4681 | | - if (use_new_scheme(udev, retry_counter, port_dev)) { |
|---|
| 4732 | + for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { |
|---|
| 4733 | + if (do_new_scheme) { |
|---|
| 4682 | 4734 | struct usb_device_descriptor *buf; |
|---|
| 4683 | 4735 | int r = 0; |
|---|
| 4684 | 4736 | |
|---|
| 4685 | | - did_new_scheme = true; |
|---|
| 4686 | 4737 | retval = hub_enable_device(udev); |
|---|
| 4687 | 4738 | if (retval < 0) { |
|---|
| 4688 | 4739 | dev_err(&udev->dev, |
|---|
| .. | .. |
|---|
| 4702 | 4753 | * 255 is for WUSB devices, we actually need to use |
|---|
| 4703 | 4754 | * 512 (WUSB1.0[4.8.1]). |
|---|
| 4704 | 4755 | */ |
|---|
| 4705 | | - for (operations = 0; operations < 3; ++operations) { |
|---|
| 4756 | + for (operations = 0; operations < GET_MAXPACKET0_TRIES; |
|---|
| 4757 | + ++operations) { |
|---|
| 4706 | 4758 | buf->bMaxPacketSize0 = 0; |
|---|
| 4707 | 4759 | r = usb_control_msg(udev, usb_rcvaddr0pipe(), |
|---|
| 4708 | 4760 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
|---|
| .. | .. |
|---|
| 4716 | 4768 | r = 0; |
|---|
| 4717 | 4769 | break; |
|---|
| 4718 | 4770 | } |
|---|
| 4719 | | - /* FALL THROUGH */ |
|---|
| 4771 | + fallthrough; |
|---|
| 4720 | 4772 | default: |
|---|
| 4721 | 4773 | if (r == 0) |
|---|
| 4722 | 4774 | r = -EPROTO; |
|---|
| .. | .. |
|---|
| 4791 | 4843 | * - read ep0 maxpacket even for high and low speed, |
|---|
| 4792 | 4844 | */ |
|---|
| 4793 | 4845 | msleep(10); |
|---|
| 4794 | | - /* use_new_scheme() checks the speed which may have |
|---|
| 4795 | | - * changed since the initial look so we cache the result |
|---|
| 4796 | | - * in did_new_scheme |
|---|
| 4797 | | - */ |
|---|
| 4798 | | - if (did_new_scheme) |
|---|
| 4846 | + if (do_new_scheme) |
|---|
| 4799 | 4847 | break; |
|---|
| 4800 | 4848 | } |
|---|
| 4801 | 4849 | |
|---|
| .. | .. |
|---|
| 4972 | 5020 | return remaining; |
|---|
| 4973 | 5021 | } |
|---|
| 4974 | 5022 | |
|---|
| 5023 | + |
|---|
| 5024 | +static int descriptors_changed(struct usb_device *udev, |
|---|
| 5025 | + struct usb_device_descriptor *old_device_descriptor, |
|---|
| 5026 | + struct usb_host_bos *old_bos) |
|---|
| 5027 | +{ |
|---|
| 5028 | + int changed = 0; |
|---|
| 5029 | + unsigned index; |
|---|
| 5030 | + unsigned serial_len = 0; |
|---|
| 5031 | + unsigned len; |
|---|
| 5032 | + unsigned old_length; |
|---|
| 5033 | + int length; |
|---|
| 5034 | + char *buf; |
|---|
| 5035 | + |
|---|
| 5036 | + if (memcmp(&udev->descriptor, old_device_descriptor, |
|---|
| 5037 | + sizeof(*old_device_descriptor)) != 0) |
|---|
| 5038 | + return 1; |
|---|
| 5039 | + |
|---|
| 5040 | + if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) |
|---|
| 5041 | + return 1; |
|---|
| 5042 | + if (udev->bos) { |
|---|
| 5043 | + len = le16_to_cpu(udev->bos->desc->wTotalLength); |
|---|
| 5044 | + if (len != le16_to_cpu(old_bos->desc->wTotalLength)) |
|---|
| 5045 | + return 1; |
|---|
| 5046 | + if (memcmp(udev->bos->desc, old_bos->desc, len)) |
|---|
| 5047 | + return 1; |
|---|
| 5048 | + } |
|---|
| 5049 | + |
|---|
| 5050 | + /* Since the idVendor, idProduct, and bcdDevice values in the |
|---|
| 5051 | + * device descriptor haven't changed, we will assume the |
|---|
| 5052 | + * Manufacturer and Product strings haven't changed either. |
|---|
| 5053 | + * But the SerialNumber string could be different (e.g., a |
|---|
| 5054 | + * different flash card of the same brand). |
|---|
| 5055 | + */ |
|---|
| 5056 | + if (udev->serial) |
|---|
| 5057 | + serial_len = strlen(udev->serial) + 1; |
|---|
| 5058 | + |
|---|
| 5059 | + len = serial_len; |
|---|
| 5060 | + for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
|---|
| 5061 | + old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
|---|
| 5062 | + len = max(len, old_length); |
|---|
| 5063 | + } |
|---|
| 5064 | + |
|---|
| 5065 | + buf = kmalloc(len, GFP_NOIO); |
|---|
| 5066 | + if (!buf) |
|---|
| 5067 | + /* assume the worst */ |
|---|
| 5068 | + return 1; |
|---|
| 5069 | + |
|---|
| 5070 | + for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
|---|
| 5071 | + old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
|---|
| 5072 | + length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, |
|---|
| 5073 | + old_length); |
|---|
| 5074 | + if (length != old_length) { |
|---|
| 5075 | + dev_dbg(&udev->dev, "config index %d, error %d\n", |
|---|
| 5076 | + index, length); |
|---|
| 5077 | + changed = 1; |
|---|
| 5078 | + break; |
|---|
| 5079 | + } |
|---|
| 5080 | + if (memcmp(buf, udev->rawdescriptors[index], old_length) |
|---|
| 5081 | + != 0) { |
|---|
| 5082 | + dev_dbg(&udev->dev, "config index %d changed (#%d)\n", |
|---|
| 5083 | + index, |
|---|
| 5084 | + ((struct usb_config_descriptor *) buf)-> |
|---|
| 5085 | + bConfigurationValue); |
|---|
| 5086 | + changed = 1; |
|---|
| 5087 | + break; |
|---|
| 5088 | + } |
|---|
| 5089 | + } |
|---|
| 5090 | + |
|---|
| 5091 | + if (!changed && serial_len) { |
|---|
| 5092 | + length = usb_string(udev, udev->descriptor.iSerialNumber, |
|---|
| 5093 | + buf, serial_len); |
|---|
| 5094 | + if (length + 1 != serial_len) { |
|---|
| 5095 | + dev_dbg(&udev->dev, "serial string error %d\n", |
|---|
| 5096 | + length); |
|---|
| 5097 | + changed = 1; |
|---|
| 5098 | + } else if (memcmp(buf, udev->serial, length) != 0) { |
|---|
| 5099 | + dev_dbg(&udev->dev, "serial string changed\n"); |
|---|
| 5100 | + changed = 1; |
|---|
| 5101 | + } |
|---|
| 5102 | + } |
|---|
| 5103 | + |
|---|
| 5104 | + kfree(buf); |
|---|
| 5105 | + return changed; |
|---|
| 5106 | +} |
|---|
| 5107 | + |
|---|
| 4975 | 5108 | static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, |
|---|
| 4976 | 5109 | u16 portchange) |
|---|
| 4977 | 5110 | { |
|---|
| .. | .. |
|---|
| 5040 | 5173 | |
|---|
| 5041 | 5174 | status = 0; |
|---|
| 5042 | 5175 | |
|---|
| 5043 | | - for (i = 0; i < SET_CONFIG_TRIES; i++) { |
|---|
| 5176 | + for (i = 0; i < PORT_INIT_TRIES; i++) { |
|---|
| 5044 | 5177 | usb_lock_port(port_dev); |
|---|
| 5045 | 5178 | mutex_lock(hcd->address0_mutex); |
|---|
| 5046 | 5179 | retry_locked = true; |
|---|
| 5047 | | - |
|---|
| 5048 | 5180 | /* reallocate for each attempt, since references |
|---|
| 5049 | 5181 | * to the previous one can escape in various ways |
|---|
| 5050 | 5182 | */ |
|---|
| .. | .. |
|---|
| 5184 | 5316 | break; |
|---|
| 5185 | 5317 | |
|---|
| 5186 | 5318 | /* When halfway through our retry count, power-cycle the port */ |
|---|
| 5187 | | - if (i == (SET_CONFIG_TRIES / 2) - 1) { |
|---|
| 5319 | + if (i == (PORT_INIT_TRIES - 1) / 2) { |
|---|
| 5188 | 5320 | dev_info(&port_dev->dev, "attempt power cycle\n"); |
|---|
| 5189 | 5321 | usb_hub_set_port_power(hdev, hub, port1, false); |
|---|
| 5190 | 5322 | msleep(2 * hub_power_on_good_delay(hub)); |
|---|
| 5191 | | - set_bit(HCD_FLAG_POWER_ON, &hcd->flags); |
|---|
| 5192 | 5323 | usb_hub_set_port_power(hdev, hub, port1, true); |
|---|
| 5193 | 5324 | msleep(hub_power_on_good_delay(hub)); |
|---|
| 5194 | 5325 | } |
|---|
| .. | .. |
|---|
| 5205 | 5336 | hub_port_disable(hub, port1, 1); |
|---|
| 5206 | 5337 | if (hcd->driver->relinquish_port && !hub->hdev->parent) { |
|---|
| 5207 | 5338 | if ((status != -ENOTCONN && status != -ENODEV) || |
|---|
| 5208 | | - (status == -ENOTCONN && hcd->rk3288_relinquish_port_quirk)) |
|---|
| 5339 | + (status == -ENOTCONN && of_machine_is_compatible("rockchip,rk3288"))) |
|---|
| 5209 | 5340 | hcd->driver->relinquish_port(hcd, port1); |
|---|
| 5210 | 5341 | } |
|---|
| 5211 | 5342 | } |
|---|
| .. | .. |
|---|
| 5224 | 5355 | { |
|---|
| 5225 | 5356 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
|---|
| 5226 | 5357 | struct usb_device *udev = port_dev->child; |
|---|
| 5358 | + struct usb_device_descriptor descriptor; |
|---|
| 5227 | 5359 | int status = -ENODEV; |
|---|
| 5360 | + int retval; |
|---|
| 5228 | 5361 | |
|---|
| 5229 | 5362 | dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, |
|---|
| 5230 | 5363 | portchange, portspeed(hub, portstatus)); |
|---|
| .. | .. |
|---|
| 5245 | 5378 | if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && |
|---|
| 5246 | 5379 | udev->state != USB_STATE_NOTATTACHED) { |
|---|
| 5247 | 5380 | if (portstatus & USB_PORT_STAT_ENABLE) { |
|---|
| 5248 | | - status = 0; /* Nothing to do */ |
|---|
| 5381 | + /* |
|---|
| 5382 | + * USB-3 connections are initialized automatically by |
|---|
| 5383 | + * the hostcontroller hardware. Therefore check for |
|---|
| 5384 | + * changed device descriptors before resuscitating the |
|---|
| 5385 | + * device. |
|---|
| 5386 | + */ |
|---|
| 5387 | + descriptor = udev->descriptor; |
|---|
| 5388 | + retval = usb_get_device_descriptor(udev, |
|---|
| 5389 | + sizeof(udev->descriptor)); |
|---|
| 5390 | + if (retval < 0) { |
|---|
| 5391 | + dev_dbg(&udev->dev, |
|---|
| 5392 | + "can't read device descriptor %d\n", |
|---|
| 5393 | + retval); |
|---|
| 5394 | + } else { |
|---|
| 5395 | + if (descriptors_changed(udev, &descriptor, |
|---|
| 5396 | + udev->bos)) { |
|---|
| 5397 | + dev_dbg(&udev->dev, |
|---|
| 5398 | + "device descriptor has changed\n"); |
|---|
| 5399 | + /* for disconnect() calls */ |
|---|
| 5400 | + udev->descriptor = descriptor; |
|---|
| 5401 | + } else { |
|---|
| 5402 | + status = 0; /* Nothing to do */ |
|---|
| 5403 | + } |
|---|
| 5404 | + } |
|---|
| 5249 | 5405 | #ifdef CONFIG_PM |
|---|
| 5250 | 5406 | } else if (udev->state == USB_STATE_SUSPENDED && |
|---|
| 5251 | 5407 | udev->persist_enabled) { |
|---|
| .. | .. |
|---|
| 5269 | 5425 | usb_unlock_port(port_dev); |
|---|
| 5270 | 5426 | hub_port_connect(hub, port1, portstatus, portchange); |
|---|
| 5271 | 5427 | usb_lock_port(port_dev); |
|---|
| 5428 | +} |
|---|
| 5429 | + |
|---|
| 5430 | +/* Handle notifying userspace about hub over-current events */ |
|---|
| 5431 | +static void port_over_current_notify(struct usb_port *port_dev) |
|---|
| 5432 | +{ |
|---|
| 5433 | + char *envp[3]; |
|---|
| 5434 | + struct device *hub_dev; |
|---|
| 5435 | + char *port_dev_path; |
|---|
| 5436 | + |
|---|
| 5437 | + sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); |
|---|
| 5438 | + |
|---|
| 5439 | + hub_dev = port_dev->dev.parent; |
|---|
| 5440 | + |
|---|
| 5441 | + if (!hub_dev) |
|---|
| 5442 | + return; |
|---|
| 5443 | + |
|---|
| 5444 | + port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); |
|---|
| 5445 | + if (!port_dev_path) |
|---|
| 5446 | + return; |
|---|
| 5447 | + |
|---|
| 5448 | + envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path); |
|---|
| 5449 | + if (!envp[0]) |
|---|
| 5450 | + goto exit_path; |
|---|
| 5451 | + |
|---|
| 5452 | + envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u", |
|---|
| 5453 | + port_dev->over_current_count); |
|---|
| 5454 | + if (!envp[1]) |
|---|
| 5455 | + goto exit; |
|---|
| 5456 | + |
|---|
| 5457 | + envp[2] = NULL; |
|---|
| 5458 | + kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); |
|---|
| 5459 | + |
|---|
| 5460 | + kfree(envp[1]); |
|---|
| 5461 | +exit: |
|---|
| 5462 | + kfree(envp[0]); |
|---|
| 5463 | +exit_path: |
|---|
| 5464 | + kfree(port_dev_path); |
|---|
| 5272 | 5465 | } |
|---|
| 5273 | 5466 | |
|---|
| 5274 | 5467 | static void port_event(struct usb_hub *hub, int port1) |
|---|
| .. | .. |
|---|
| 5313 | 5506 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
|---|
| 5314 | 5507 | u16 status = 0, unused; |
|---|
| 5315 | 5508 | port_dev->over_current_count++; |
|---|
| 5509 | + port_over_current_notify(port_dev); |
|---|
| 5316 | 5510 | |
|---|
| 5317 | 5511 | dev_dbg(&port_dev->dev, "over-current change #%u\n", |
|---|
| 5318 | 5512 | port_dev->over_current_count); |
|---|
| .. | .. |
|---|
| 5367 | 5561 | } else { |
|---|
| 5368 | 5562 | usb_unlock_port(port_dev); |
|---|
| 5369 | 5563 | usb_lock_device(udev); |
|---|
| 5370 | | - |
|---|
| 5371 | | - /** |
|---|
| 5372 | | - * Some special SoCs (e.g. rk322xh) USB3 PHY lose the |
|---|
| 5373 | | - * ability to detect a disconnection when USB3 device |
|---|
| 5374 | | - * plug out, fortunately, it can detect port link state |
|---|
| 5375 | | - * change here, so we can do soft disconnect according |
|---|
| 5376 | | - * to the PLC here. |
|---|
| 5377 | | - * |
|---|
| 5378 | | - * And we only need to do the soft disconnect for root |
|---|
| 5379 | | - * hub. In addition, we just reuse the autosuspend quirk |
|---|
| 5380 | | - * but not add a new quirk for this issue. Because this |
|---|
| 5381 | | - * issue always occurs with autosuspend problem. |
|---|
| 5382 | | - */ |
|---|
| 5383 | | - if (!hub->hdev->parent && (hdev->quirks & |
|---|
| 5384 | | - USB_QUIRK_AUTO_SUSPEND)) |
|---|
| 5385 | | - usb_remove_device(udev); |
|---|
| 5386 | | - else |
|---|
| 5387 | | - usb_reset_device(udev); |
|---|
| 5564 | + usb_reset_device(udev); |
|---|
| 5388 | 5565 | usb_unlock_device(udev); |
|---|
| 5389 | 5566 | usb_lock_port(port_dev); |
|---|
| 5390 | 5567 | connect_change = 0; |
|---|
| .. | .. |
|---|
| 5602 | 5779 | usb_deregister(&hub_driver); |
|---|
| 5603 | 5780 | } /* usb_hub_cleanup() */ |
|---|
| 5604 | 5781 | |
|---|
| 5605 | | -static int descriptors_changed(struct usb_device *udev, |
|---|
| 5606 | | - struct usb_device_descriptor *old_device_descriptor, |
|---|
| 5607 | | - struct usb_host_bos *old_bos) |
|---|
| 5608 | | -{ |
|---|
| 5609 | | - int changed = 0; |
|---|
| 5610 | | - unsigned index; |
|---|
| 5611 | | - unsigned serial_len = 0; |
|---|
| 5612 | | - unsigned len; |
|---|
| 5613 | | - unsigned old_length; |
|---|
| 5614 | | - int length; |
|---|
| 5615 | | - char *buf; |
|---|
| 5616 | | - |
|---|
| 5617 | | - if (memcmp(&udev->descriptor, old_device_descriptor, |
|---|
| 5618 | | - sizeof(*old_device_descriptor)) != 0) |
|---|
| 5619 | | - return 1; |
|---|
| 5620 | | - |
|---|
| 5621 | | - if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) |
|---|
| 5622 | | - return 1; |
|---|
| 5623 | | - if (udev->bos) { |
|---|
| 5624 | | - len = le16_to_cpu(udev->bos->desc->wTotalLength); |
|---|
| 5625 | | - if (len != le16_to_cpu(old_bos->desc->wTotalLength)) |
|---|
| 5626 | | - return 1; |
|---|
| 5627 | | - if (memcmp(udev->bos->desc, old_bos->desc, len)) |
|---|
| 5628 | | - return 1; |
|---|
| 5629 | | - } |
|---|
| 5630 | | - |
|---|
| 5631 | | - /* Since the idVendor, idProduct, and bcdDevice values in the |
|---|
| 5632 | | - * device descriptor haven't changed, we will assume the |
|---|
| 5633 | | - * Manufacturer and Product strings haven't changed either. |
|---|
| 5634 | | - * But the SerialNumber string could be different (e.g., a |
|---|
| 5635 | | - * different flash card of the same brand). |
|---|
| 5636 | | - */ |
|---|
| 5637 | | - if (udev->serial) |
|---|
| 5638 | | - serial_len = strlen(udev->serial) + 1; |
|---|
| 5639 | | - |
|---|
| 5640 | | - len = serial_len; |
|---|
| 5641 | | - for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
|---|
| 5642 | | - old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
|---|
| 5643 | | - len = max(len, old_length); |
|---|
| 5644 | | - } |
|---|
| 5645 | | - |
|---|
| 5646 | | - buf = kmalloc(len, GFP_NOIO); |
|---|
| 5647 | | - if (!buf) |
|---|
| 5648 | | - /* assume the worst */ |
|---|
| 5649 | | - return 1; |
|---|
| 5650 | | - |
|---|
| 5651 | | - for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
|---|
| 5652 | | - old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
|---|
| 5653 | | - length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, |
|---|
| 5654 | | - old_length); |
|---|
| 5655 | | - if (length != old_length) { |
|---|
| 5656 | | - dev_dbg(&udev->dev, "config index %d, error %d\n", |
|---|
| 5657 | | - index, length); |
|---|
| 5658 | | - changed = 1; |
|---|
| 5659 | | - break; |
|---|
| 5660 | | - } |
|---|
| 5661 | | - if (memcmp(buf, udev->rawdescriptors[index], old_length) |
|---|
| 5662 | | - != 0) { |
|---|
| 5663 | | - dev_dbg(&udev->dev, "config index %d changed (#%d)\n", |
|---|
| 5664 | | - index, |
|---|
| 5665 | | - ((struct usb_config_descriptor *) buf)-> |
|---|
| 5666 | | - bConfigurationValue); |
|---|
| 5667 | | - changed = 1; |
|---|
| 5668 | | - break; |
|---|
| 5669 | | - } |
|---|
| 5670 | | - } |
|---|
| 5671 | | - |
|---|
| 5672 | | - if (!changed && serial_len) { |
|---|
| 5673 | | - length = usb_string(udev, udev->descriptor.iSerialNumber, |
|---|
| 5674 | | - buf, serial_len); |
|---|
| 5675 | | - if (length + 1 != serial_len) { |
|---|
| 5676 | | - dev_dbg(&udev->dev, "serial string error %d\n", |
|---|
| 5677 | | - length); |
|---|
| 5678 | | - changed = 1; |
|---|
| 5679 | | - } else if (memcmp(buf, udev->serial, length) != 0) { |
|---|
| 5680 | | - dev_dbg(&udev->dev, "serial string changed\n"); |
|---|
| 5681 | | - changed = 1; |
|---|
| 5682 | | - } |
|---|
| 5683 | | - } |
|---|
| 5684 | | - |
|---|
| 5685 | | - kfree(buf); |
|---|
| 5686 | | - return changed; |
|---|
| 5687 | | -} |
|---|
| 5688 | | - |
|---|
| 5689 | 5782 | /** |
|---|
| 5690 | 5783 | * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device |
|---|
| 5691 | 5784 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
|---|
| .. | .. |
|---|
| 5762 | 5855 | |
|---|
| 5763 | 5856 | mutex_lock(hcd->address0_mutex); |
|---|
| 5764 | 5857 | |
|---|
| 5765 | | - for (i = 0; i < SET_CONFIG_TRIES; ++i) { |
|---|
| 5858 | + for (i = 0; i < PORT_INIT_TRIES; ++i) { |
|---|
| 5766 | 5859 | |
|---|
| 5767 | 5860 | /* ep0 maxpacket size may change; let the HCD know about it. |
|---|
| 5768 | 5861 | * Other endpoints will be handled by re-enumeration. */ |
|---|