1aa1f3bb5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * USB hub driver.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds
61da177e4SLinus Torvalds * (C) Copyright 1999 Johannes Erdfelt
71da177e4SLinus Torvalds * (C) Copyright 1999 Gregory P. Smith
81da177e4SLinus Torvalds * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
91da177e4SLinus Torvalds *
10b65fba3dSGreg Kroah-Hartman * Released under the GPLv2 only.
111da177e4SLinus Torvalds */
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds #include <linux/kernel.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/moduleparam.h>
171da177e4SLinus Torvalds #include <linux/completion.h>
185b3cc15aSIngo Molnar #include <linux/sched/mm.h>
191da177e4SLinus Torvalds #include <linux/list.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
2195d23dc2SAndrey Konovalov #include <linux/kcov.h>
221da177e4SLinus Torvalds #include <linux/ioctl.h>
231da177e4SLinus Torvalds #include <linux/usb.h>
241da177e4SLinus Torvalds #include <linux/usbdevice_fs.h>
2527729aadSEric Lescouet #include <linux/usb/hcd.h>
263a6bf4a0SMatthias Kaehlcke #include <linux/usb/onboard_hub.h>
27925aa46bSRichard Zhao #include <linux/usb/otg.h>
2893362a87SPhil Dibowitz #include <linux/usb/quirks.h>
2932a69589SPetr Mladek #include <linux/workqueue.h>
304186ecf8SArjan van de Ven #include <linux/mutex.h>
31b04b3156STheodore Ts'o #include <linux/random.h>
32ad493e5eSLan Tianyu #include <linux/pm_qos.h>
33201af55dSJon Flatley #include <linux/kobject.h>
341da177e4SLinus Torvalds
350299809bSThinh Nguyen #include <linux/bitfield.h>
367c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
371da177e4SLinus Torvalds #include <asm/byteorder.h>
381da177e4SLinus Torvalds
396e30d7cbSLan Tianyu #include "hub.h"
40f8f02d5cSGreg Kroah-Hartman #include "otg_productlist.h"
411da177e4SLinus Torvalds
42e6f30deaSMing Lei #define USB_VENDOR_GENESYS_LOGIC 0x05e3
431208f9e1SHardik Gajjar #define USB_VENDOR_SMSC 0x0424
4476e1ef1dSEugeniu Rosca #define USB_PRODUCT_USB5534B 0x5534
45a7d8d1c7SAndrew Lunn #define USB_VENDOR_CYPRESS 0x04b4
46a7d8d1c7SAndrew Lunn #define USB_PRODUCT_CY7C65632 0x6570
477171b0e2SFlavio Suligoi #define USB_VENDOR_TEXAS_INSTRUMENTS 0x0451
487171b0e2SFlavio Suligoi #define USB_PRODUCT_TUSB8041_USB3 0x8140
497171b0e2SFlavio Suligoi #define USB_PRODUCT_TUSB8041_USB2 0x8142
50915d900fSHardik Gajjar #define USB_VENDOR_MICROCHIP 0x0424
51915d900fSHardik Gajjar #define USB_PRODUCT_USB4913 0x4913
52915d900fSHardik Gajjar #define USB_PRODUCT_USB4914 0x4914
53915d900fSHardik Gajjar #define USB_PRODUCT_USB4915 0x4915
54cd72da00SHardik Gajjar #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND BIT(0)
55cd72da00SHardik Gajjar #define HUB_QUIRK_DISABLE_AUTOSUSPEND BIT(1)
56915d900fSHardik Gajjar #define HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL BIT(2)
57e6f30deaSMing Lei
58886ee36eSFelipe Balbi #define USB_TP_TRANSMISSION_DELAY 40 /* ns */
59886ee36eSFelipe Balbi #define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */
601bf2761cSMathias Nyman #define USB_PING_RESPONSE_TIME 400 /* ns */
61915d900fSHardik Gajjar #define USB_REDUCE_FRAME_INTR_BINTERVAL 9
62886ee36eSFelipe Balbi
633adcbec4SHardik Gajjar /*
643adcbec4SHardik Gajjar * The SET_ADDRESS request timeout will be 500 ms when
653adcbec4SHardik Gajjar * USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT quirk flag is set.
663adcbec4SHardik Gajjar */
673adcbec4SHardik Gajjar #define USB_SHORT_SET_ADDRESS_REQ_TIMEOUT 500 /* ms */
683adcbec4SHardik Gajjar
69fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members
709ad3d6ccSAlan Stern * Note: Both are also protected by ->dev.sem, except that ->state can
711da177e4SLinus Torvalds * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
721da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
731da177e4SLinus Torvalds
7432a69589SPetr Mladek /* workqueue to process hub events */
7532a69589SPetr Mladek static struct workqueue_struct *hub_wq;
7632a69589SPetr Mladek static void hub_event(struct work_struct *work);
771da177e4SLinus Torvalds
78d8521afeSDan Williams /* synchronize hub-port add/remove and peering operations */
79d8521afeSDan Williams DEFINE_MUTEX(usb_port_peer_mutex);
80d8521afeSDan Williams
811da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
82a44007a4SSaurabh Sengar static bool blinkenlights;
831da177e4SLinus Torvalds module_param(blinkenlights, bool, S_IRUGO);
841da177e4SLinus Torvalds MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds /*
87fd7c519dSJaroslav Kysela * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
88fd7c519dSJaroslav Kysela * 10 seconds to send reply for the initial 64-byte descriptor request.
89fd7c519dSJaroslav Kysela */
90fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
91fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
92fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
93b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
94b9cef6c3SWu Fengguang "initial 64-byte descriptor request timeout in milliseconds "
95b9cef6c3SWu Fengguang "(default 5000 - 5.0 seconds)");
96fd7c519dSJaroslav Kysela
97fd7c519dSJaroslav Kysela /*
981da177e4SLinus Torvalds * As of 2.6.10 we introduce a new USB device initialization scheme which
991da177e4SLinus Torvalds * closely resembles the way Windows works. Hopefully it will be compatible
1001da177e4SLinus Torvalds * with a wider range of devices than the old scheme. However some previously
1011da177e4SLinus Torvalds * working devices may start giving rise to "device not accepting address"
1021da177e4SLinus Torvalds * errors; if that happens the user can try the old scheme by adjusting the
1031da177e4SLinus Torvalds * following module parameters.
1041da177e4SLinus Torvalds *
1051da177e4SLinus Torvalds * For maximum flexibility there are two boolean parameters to control the
1061da177e4SLinus Torvalds * hub driver's behavior. On the first initialization attempt, if the
1071da177e4SLinus Torvalds * "old_scheme_first" parameter is set then the old scheme will be used,
1081da177e4SLinus Torvalds * otherwise the new scheme is used. If that fails and "use_both_schemes"
1091da177e4SLinus Torvalds * is set, then the driver will make another attempt, using the other scheme.
1101da177e4SLinus Torvalds */
111a44007a4SSaurabh Sengar static bool old_scheme_first;
1121da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1131da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1141da177e4SLinus Torvalds "start with the old device initialization scheme");
1151da177e4SLinus Torvalds
116b9cf2cb5SJason Yan static bool use_both_schemes = true;
1171da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1181da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1191da177e4SLinus Torvalds "try the other device initialization scheme if the "
1201da177e4SLinus Torvalds "first one fails");
1211da177e4SLinus Torvalds
12232fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization. This interferes with
12332fe0198SAlan Stern * port reset on some companion controllers.
12432fe0198SAlan Stern */
12532fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
12632fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
12732fe0198SAlan Stern
128ad493e5eSLan Tianyu #define HUB_DEBOUNCE_TIMEOUT 2000
129948fea37SAlan Stern #define HUB_DEBOUNCE_STEP 25
130948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE 100
131948fea37SAlan Stern
132742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
1333bc02bceSGeert Uytterhoeven static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
1344fdc1790SJan-Marek Glogowski static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
1354fdc1790SJan-Marek Glogowski u16 portstatus);
136742120c6SMing Lei
portspeed(struct usb_hub * hub,int portstatus)137131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus)
1381da177e4SLinus Torvalds {
139d64aab0cSOliver Neukum if (hub_is_superspeedplus(hub->hdev))
140d64aab0cSOliver Neukum return "10.0 Gb/s";
141131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev))
142131dec34SSarah Sharp return "5.0 Gb/s";
143288ead45SAlan Stern if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1441da177e4SLinus Torvalds return "480 Mb/s";
145288ead45SAlan Stern else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1461da177e4SLinus Torvalds return "1.5 Mb/s";
1471da177e4SLinus Torvalds else
1481da177e4SLinus Torvalds return "12 Mb/s";
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
usb_hub_to_struct_hub(struct usb_device * hdev)152ad493e5eSLan Tianyu struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
1531da177e4SLinus Torvalds {
154ff823c79SLan Tianyu if (!hdev || !hdev->actconfig || !hdev->maxchild)
15525118084SAlan Stern return NULL;
1561da177e4SLinus Torvalds return usb_get_intfdata(hdev->actconfig->interface[0]);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds
usb_device_supports_lpm(struct usb_device * udev)1592d2a3167SLu Baolu int usb_device_supports_lpm(struct usb_device *udev)
160d9b2099cSSarah Sharp {
161ad87e032SAlan Stern /* Some devices have trouble with LPM */
162ad87e032SAlan Stern if (udev->quirks & USB_QUIRK_NO_LPM)
163ad87e032SAlan Stern return 0;
164ad87e032SAlan Stern
165f74a7afcSRicardo Cañuelo /* Skip if the device BOS descriptor couldn't be read */
166f74a7afcSRicardo Cañuelo if (!udev->bos)
167f74a7afcSRicardo Cañuelo return 0;
168f74a7afcSRicardo Cañuelo
169d9b2099cSSarah Sharp /* USB 2.1 (and greater) devices indicate LPM support through
170d9b2099cSSarah Sharp * their USB 2.0 Extended Capabilities BOS descriptor.
171d9b2099cSSarah Sharp */
172a8425292SRupesh Tatiya if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
173d9b2099cSSarah Sharp if (udev->bos->ext_cap &&
174d9b2099cSSarah Sharp (USB_LPM_SUPPORT &
175d9b2099cSSarah Sharp le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
176d9b2099cSSarah Sharp return 1;
177d9b2099cSSarah Sharp return 0;
178d9b2099cSSarah Sharp }
17951e0a012SSarah Sharp
18025cd2882SSarah Sharp /*
18125cd2882SSarah Sharp * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
18225cd2882SSarah Sharp * However, there are some that don't, and they set the U1/U2 exit
18325cd2882SSarah Sharp * latencies to zero.
18451e0a012SSarah Sharp */
18551e0a012SSarah Sharp if (!udev->bos->ss_cap) {
18625cd2882SSarah Sharp dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
187d9b2099cSSarah Sharp return 0;
188d9b2099cSSarah Sharp }
18951e0a012SSarah Sharp
19025cd2882SSarah Sharp if (udev->bos->ss_cap->bU1devExitLat == 0 &&
19125cd2882SSarah Sharp udev->bos->ss_cap->bU2DevExitLat == 0) {
19225cd2882SSarah Sharp if (udev->parent)
19325cd2882SSarah Sharp dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
19425cd2882SSarah Sharp else
19525cd2882SSarah Sharp dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
19625cd2882SSarah Sharp return 0;
19725cd2882SSarah Sharp }
19825cd2882SSarah Sharp
19925cd2882SSarah Sharp if (!udev->parent || udev->parent->lpm_capable)
20025cd2882SSarah Sharp return 1;
20151e0a012SSarah Sharp return 0;
20251e0a012SSarah Sharp }
20351e0a012SSarah Sharp
20451e0a012SSarah Sharp /*
2051bf2761cSMathias Nyman * Set the Maximum Exit Latency (MEL) for the host to wakup up the path from
2061bf2761cSMathias Nyman * U1/U2, send a PING to the device and receive a PING_RESPONSE.
2071bf2761cSMathias Nyman * See USB 3.1 section C.1.5.2
20851e0a012SSarah Sharp */
usb_set_lpm_mel(struct usb_device * udev,struct usb3_lpm_parameters * udev_lpm_params,unsigned int udev_exit_latency,struct usb_hub * hub,struct usb3_lpm_parameters * hub_lpm_params,unsigned int hub_exit_latency)20951e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev,
21051e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params,
21151e0a012SSarah Sharp unsigned int udev_exit_latency,
21251e0a012SSarah Sharp struct usb_hub *hub,
21351e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params,
21451e0a012SSarah Sharp unsigned int hub_exit_latency)
21551e0a012SSarah Sharp {
21651e0a012SSarah Sharp unsigned int total_mel;
21751e0a012SSarah Sharp
21851e0a012SSarah Sharp /*
2191bf2761cSMathias Nyman * tMEL1. time to transition path from host to device into U0.
2201bf2761cSMathias Nyman * MEL for parent already contains the delay up to parent, so only add
2211bf2761cSMathias Nyman * the exit latency for the last link (pick the slower exit latency),
2221bf2761cSMathias Nyman * and the hub header decode latency. See USB 3.1 section C 2.2.1
2231bf2761cSMathias Nyman * Store MEL in nanoseconds
22451e0a012SSarah Sharp */
22551e0a012SSarah Sharp total_mel = hub_lpm_params->mel +
2261bf2761cSMathias Nyman max(udev_exit_latency, hub_exit_latency) * 1000 +
2271bf2761cSMathias Nyman hub->descriptor->u.ss.bHubHdrDecLat * 100;
22851e0a012SSarah Sharp
22951e0a012SSarah Sharp /*
2301bf2761cSMathias Nyman * tMEL2. Time to submit PING packet. Sum of tTPTransmissionDelay for
2311bf2761cSMathias Nyman * each link + wHubDelay for each hub. Add only for last link.
2321bf2761cSMathias Nyman * tMEL4, the time for PING_RESPONSE to traverse upstream is similar.
2331bf2761cSMathias Nyman * Multiply by 2 to include it as well.
23451e0a012SSarah Sharp */
2351bf2761cSMathias Nyman total_mel += (__le16_to_cpu(hub->descriptor->u.ss.wHubDelay) +
2361bf2761cSMathias Nyman USB_TP_TRANSMISSION_DELAY) * 2;
2371bf2761cSMathias Nyman
2381bf2761cSMathias Nyman /*
2391bf2761cSMathias Nyman * tMEL3, tPingResponse. Time taken by device to generate PING_RESPONSE
2401bf2761cSMathias Nyman * after receiving PING. Also add 2100ns as stated in USB 3.1 C 1.5.2.4
2411bf2761cSMathias Nyman * to cover the delay if the PING_RESPONSE is queued behind a Max Packet
2421bf2761cSMathias Nyman * Size DP.
2431bf2761cSMathias Nyman * Note these delays should be added only once for the entire path, so
2441bf2761cSMathias Nyman * add them to the MEL of the device connected to the roothub.
2451bf2761cSMathias Nyman */
2461bf2761cSMathias Nyman if (!hub->hdev->parent)
2471bf2761cSMathias Nyman total_mel += USB_PING_RESPONSE_TIME + 2100;
24851e0a012SSarah Sharp
24951e0a012SSarah Sharp udev_lpm_params->mel = total_mel;
25051e0a012SSarah Sharp }
25151e0a012SSarah Sharp
25251e0a012SSarah Sharp /*
25351e0a012SSarah Sharp * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
25451e0a012SSarah Sharp * a transition from either U1 or U2.
25551e0a012SSarah Sharp */
usb_set_lpm_pel(struct usb_device * udev,struct usb3_lpm_parameters * udev_lpm_params,unsigned int udev_exit_latency,struct usb_hub * hub,struct usb3_lpm_parameters * hub_lpm_params,unsigned int hub_exit_latency,unsigned int port_to_port_exit_latency)25651e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev,
25751e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params,
25851e0a012SSarah Sharp unsigned int udev_exit_latency,
25951e0a012SSarah Sharp struct usb_hub *hub,
26051e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params,
26151e0a012SSarah Sharp unsigned int hub_exit_latency,
26251e0a012SSarah Sharp unsigned int port_to_port_exit_latency)
26351e0a012SSarah Sharp {
26451e0a012SSarah Sharp unsigned int first_link_pel;
26551e0a012SSarah Sharp unsigned int hub_pel;
26651e0a012SSarah Sharp
26751e0a012SSarah Sharp /*
26851e0a012SSarah Sharp * First, the device sends an LFPS to transition the link between the
26951e0a012SSarah Sharp * device and the parent hub into U0. The exit latency is the bigger of
27051e0a012SSarah Sharp * the device exit latency or the hub exit latency.
27151e0a012SSarah Sharp */
27251e0a012SSarah Sharp if (udev_exit_latency > hub_exit_latency)
27351e0a012SSarah Sharp first_link_pel = udev_exit_latency * 1000;
27451e0a012SSarah Sharp else
27551e0a012SSarah Sharp first_link_pel = hub_exit_latency * 1000;
27651e0a012SSarah Sharp
27751e0a012SSarah Sharp /*
27851e0a012SSarah Sharp * When the hub starts to receive the LFPS, there is a slight delay for
27951e0a012SSarah Sharp * it to figure out that one of the ports is sending an LFPS. Then it
28051e0a012SSarah Sharp * will forward the LFPS to its upstream link. The exit latency is the
28151e0a012SSarah Sharp * delay, plus the PEL that we calculated for this hub.
28251e0a012SSarah Sharp */
28351e0a012SSarah Sharp hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
28451e0a012SSarah Sharp
28551e0a012SSarah Sharp /*
28651e0a012SSarah Sharp * According to figure C-7 in the USB 3.0 spec, the PEL for this device
28751e0a012SSarah Sharp * is the greater of the two exit latencies.
28851e0a012SSarah Sharp */
28951e0a012SSarah Sharp if (first_link_pel > hub_pel)
29051e0a012SSarah Sharp udev_lpm_params->pel = first_link_pel;
29151e0a012SSarah Sharp else
29251e0a012SSarah Sharp udev_lpm_params->pel = hub_pel;
29351e0a012SSarah Sharp }
29451e0a012SSarah Sharp
29551e0a012SSarah Sharp /*
29651e0a012SSarah Sharp * Set the System Exit Latency (SEL) to indicate the total worst-case time from
29751e0a012SSarah Sharp * when a device initiates a transition to U0, until when it will receive the
29851e0a012SSarah Sharp * first packet from the host controller.
29951e0a012SSarah Sharp *
30051e0a012SSarah Sharp * Section C.1.5.1 describes the four components to this:
30151e0a012SSarah Sharp * - t1: device PEL
30251e0a012SSarah Sharp * - t2: time for the ERDY to make it from the device to the host.
30351e0a012SSarah Sharp * - t3: a host-specific delay to process the ERDY.
30451e0a012SSarah Sharp * - t4: time for the packet to make it from the host to the device.
30551e0a012SSarah Sharp *
30651e0a012SSarah Sharp * t3 is specific to both the xHCI host and the platform the host is integrated
30751e0a012SSarah Sharp * into. The Intel HW folks have said it's negligible, FIXME if a different
30851e0a012SSarah Sharp * vendor says otherwise.
30951e0a012SSarah Sharp */
usb_set_lpm_sel(struct usb_device * udev,struct usb3_lpm_parameters * udev_lpm_params)31051e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev,
31151e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params)
31251e0a012SSarah Sharp {
31351e0a012SSarah Sharp struct usb_device *parent;
31451e0a012SSarah Sharp unsigned int num_hubs;
31551e0a012SSarah Sharp unsigned int total_sel;
31651e0a012SSarah Sharp
31751e0a012SSarah Sharp /* t1 = device PEL */
31851e0a012SSarah Sharp total_sel = udev_lpm_params->pel;
31951e0a012SSarah Sharp /* How many external hubs are in between the device & the root port. */
32051e0a012SSarah Sharp for (parent = udev->parent, num_hubs = 0; parent->parent;
32151e0a012SSarah Sharp parent = parent->parent)
32251e0a012SSarah Sharp num_hubs++;
32351e0a012SSarah Sharp /* t2 = 2.1us + 250ns * (num_hubs - 1) */
32451e0a012SSarah Sharp if (num_hubs > 0)
32551e0a012SSarah Sharp total_sel += 2100 + 250 * (num_hubs - 1);
32651e0a012SSarah Sharp
32751e0a012SSarah Sharp /* t4 = 250ns * num_hubs */
32851e0a012SSarah Sharp total_sel += 250 * num_hubs;
32951e0a012SSarah Sharp
33051e0a012SSarah Sharp udev_lpm_params->sel = total_sel;
33151e0a012SSarah Sharp }
33251e0a012SSarah Sharp
usb_set_lpm_parameters(struct usb_device * udev)33351e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev)
33451e0a012SSarah Sharp {
33551e0a012SSarah Sharp struct usb_hub *hub;
33651e0a012SSarah Sharp unsigned int port_to_port_delay;
33751e0a012SSarah Sharp unsigned int udev_u1_del;
33851e0a012SSarah Sharp unsigned int udev_u2_del;
33951e0a012SSarah Sharp unsigned int hub_u1_del;
34051e0a012SSarah Sharp unsigned int hub_u2_del;
34151e0a012SSarah Sharp
3428a1b2725SMathias Nyman if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
34351e0a012SSarah Sharp return;
34451e0a012SSarah Sharp
345f74a7afcSRicardo Cañuelo /* Skip if the device BOS descriptor couldn't be read */
346f74a7afcSRicardo Cañuelo if (!udev->bos)
347f74a7afcSRicardo Cañuelo return;
348f74a7afcSRicardo Cañuelo
349ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent);
35051e0a012SSarah Sharp /* It doesn't take time to transition the roothub into U0, since it
35151e0a012SSarah Sharp * doesn't have an upstream link.
35251e0a012SSarah Sharp */
35351e0a012SSarah Sharp if (!hub)
35451e0a012SSarah Sharp return;
35551e0a012SSarah Sharp
35651e0a012SSarah Sharp udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
3574d967995SXenia Ragiadakou udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
35851e0a012SSarah Sharp hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
3594d967995SXenia Ragiadakou hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
36051e0a012SSarah Sharp
36151e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
36251e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del);
36351e0a012SSarah Sharp
36451e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
36551e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del);
36651e0a012SSarah Sharp
36751e0a012SSarah Sharp /*
36851e0a012SSarah Sharp * Appendix C, section C.2.2.2, says that there is a slight delay from
36951e0a012SSarah Sharp * when the parent hub notices the downstream port is trying to
37051e0a012SSarah Sharp * transition to U0 to when the hub initiates a U0 transition on its
37151e0a012SSarah Sharp * upstream port. The section says the delays are tPort2PortU1EL and
37251e0a012SSarah Sharp * tPort2PortU2EL, but it doesn't define what they are.
37351e0a012SSarah Sharp *
37451e0a012SSarah Sharp * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
37551e0a012SSarah Sharp * about the same delays. Use the maximum delay calculations from those
37651e0a012SSarah Sharp * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
37751e0a012SSarah Sharp * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
37851e0a012SSarah Sharp * assume the device exit latencies they are talking about are the hub
37951e0a012SSarah Sharp * exit latencies.
38051e0a012SSarah Sharp *
38151e0a012SSarah Sharp * What do we do if the U2 exit latency is less than the U1 exit
38251e0a012SSarah Sharp * latency? It's possible, although not likely...
38351e0a012SSarah Sharp */
38451e0a012SSarah Sharp port_to_port_delay = 1;
38551e0a012SSarah Sharp
38651e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
38751e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del,
38851e0a012SSarah Sharp port_to_port_delay);
38951e0a012SSarah Sharp
39051e0a012SSarah Sharp if (hub_u2_del > hub_u1_del)
39151e0a012SSarah Sharp port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
39251e0a012SSarah Sharp else
39351e0a012SSarah Sharp port_to_port_delay = 1 + hub_u1_del;
39451e0a012SSarah Sharp
39551e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
39651e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del,
39751e0a012SSarah Sharp port_to_port_delay);
39851e0a012SSarah Sharp
39951e0a012SSarah Sharp /* Now that we've got PEL, calculate SEL. */
40051e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u1_params);
40151e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u2_params);
40251e0a012SSarah Sharp }
403d9b2099cSSarah Sharp
4041da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
get_hub_descriptor(struct usb_device * hdev,struct usb_hub_descriptor * desc)405bec444cdSJohan Hovold static int get_hub_descriptor(struct usb_device *hdev,
406bec444cdSJohan Hovold struct usb_hub_descriptor *desc)
4071da177e4SLinus Torvalds {
408dbe79bbeSJohn Youn int i, ret, size;
409dbe79bbeSJohn Youn unsigned dtype;
410dbe79bbeSJohn Youn
411dbe79bbeSJohn Youn if (hub_is_superspeed(hdev)) {
412dbe79bbeSJohn Youn dtype = USB_DT_SS_HUB;
413dbe79bbeSJohn Youn size = USB_DT_SS_HUB_SIZE;
414dbe79bbeSJohn Youn } else {
415dbe79bbeSJohn Youn dtype = USB_DT_HUB;
416dbe79bbeSJohn Youn size = sizeof(struct usb_hub_descriptor);
417dbe79bbeSJohn Youn }
4181da177e4SLinus Torvalds
4191da177e4SLinus Torvalds for (i = 0; i < 3; i++) {
4201da177e4SLinus Torvalds ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
4211da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
422bec444cdSJohan Hovold dtype << 8, 0, desc, size,
4231da177e4SLinus Torvalds USB_CTRL_GET_TIMEOUT);
4242c25a2c8SJohan Hovold if (hub_is_superspeed(hdev)) {
4252c25a2c8SJohan Hovold if (ret == size)
4261da177e4SLinus Torvalds return ret;
427bec444cdSJohan Hovold } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) {
428bec444cdSJohan Hovold /* Make sure we have the DeviceRemovable field. */
429bec444cdSJohan Hovold size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1;
430bec444cdSJohan Hovold if (ret < size)
431bec444cdSJohan Hovold return -EMSGSIZE;
4322c25a2c8SJohan Hovold return ret;
4332c25a2c8SJohan Hovold }
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds return -EINVAL;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds
4381da177e4SLinus Torvalds /*
4391da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.1
4401da177e4SLinus Torvalds */
clear_hub_feature(struct usb_device * hdev,int feature)4411da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
4421da177e4SLinus Torvalds {
4431afe33a7SOliver Neukum return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4441afe33a7SOliver Neukum USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.2
4491da177e4SLinus Torvalds */
usb_clear_port_feature(struct usb_device * hdev,int port1,int feature)450ad493e5eSLan Tianyu int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
4511da177e4SLinus Torvalds {
4521afe33a7SOliver Neukum return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4531afe33a7SOliver Neukum USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
4541afe33a7SOliver Neukum NULL, 0, 1000);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.13
4591da177e4SLinus Torvalds */
set_port_feature(struct usb_device * hdev,int port1,int feature)4601da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
4611da177e4SLinus Torvalds {
4621afe33a7SOliver Neukum return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4631afe33a7SOliver Neukum USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
4641afe33a7SOliver Neukum NULL, 0, 1000);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds
to_led_name(int selector)467d99f6b41SDan Williams static char *to_led_name(int selector)
468d99f6b41SDan Williams {
469d99f6b41SDan Williams switch (selector) {
470d99f6b41SDan Williams case HUB_LED_AMBER:
471d99f6b41SDan Williams return "amber";
472d99f6b41SDan Williams case HUB_LED_GREEN:
473d99f6b41SDan Williams return "green";
474d99f6b41SDan Williams case HUB_LED_OFF:
475d99f6b41SDan Williams return "off";
476d99f6b41SDan Williams case HUB_LED_AUTO:
477d99f6b41SDan Williams return "auto";
478d99f6b41SDan Williams default:
479d99f6b41SDan Williams return "??";
480d99f6b41SDan Williams }
481d99f6b41SDan Williams }
482d99f6b41SDan Williams
4831da177e4SLinus Torvalds /*
4841da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
4851da177e4SLinus Torvalds * for info about using port indicators
4861da177e4SLinus Torvalds */
set_port_led(struct usb_hub * hub,int port1,int selector)487d99f6b41SDan Williams static void set_port_led(struct usb_hub *hub, int port1, int selector)
4881da177e4SLinus Torvalds {
489d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
490d99f6b41SDan Williams int status;
491d99f6b41SDan Williams
492d99f6b41SDan Williams status = set_port_feature(hub->hdev, (selector << 8) | port1,
4931da177e4SLinus Torvalds USB_PORT_FEAT_INDICATOR);
494d99f6b41SDan Williams dev_dbg(&port_dev->dev, "indicator %s status %d\n",
495d99f6b41SDan Williams to_led_name(selector), status);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds
4981da177e4SLinus Torvalds #define LED_CYCLE_PERIOD ((2*HZ)/3)
4991da177e4SLinus Torvalds
led_work(struct work_struct * work)500c4028958SDavid Howells static void led_work(struct work_struct *work)
5011da177e4SLinus Torvalds {
502c4028958SDavid Howells struct usb_hub *hub =
503c4028958SDavid Howells container_of(work, struct usb_hub, leds.work);
5041da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
5051da177e4SLinus Torvalds unsigned i;
5061da177e4SLinus Torvalds unsigned changed = 0;
5071da177e4SLinus Torvalds int cursor = -1;
5081da177e4SLinus Torvalds
5091da177e4SLinus Torvalds if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
5101da177e4SLinus Torvalds return;
5111da177e4SLinus Torvalds
5123bbc47d8SKrzysztof Mazur for (i = 0; i < hdev->maxchild; i++) {
5131da177e4SLinus Torvalds unsigned selector, mode;
5141da177e4SLinus Torvalds
5151da177e4SLinus Torvalds /* 30%-50% duty cycle */
5161da177e4SLinus Torvalds
5171da177e4SLinus Torvalds switch (hub->indicator[i]) {
5181da177e4SLinus Torvalds /* cycle marker */
5191da177e4SLinus Torvalds case INDICATOR_CYCLE:
5201da177e4SLinus Torvalds cursor = i;
5211da177e4SLinus Torvalds selector = HUB_LED_AUTO;
5221da177e4SLinus Torvalds mode = INDICATOR_AUTO;
5231da177e4SLinus Torvalds break;
5241da177e4SLinus Torvalds /* blinking green = sw attention */
5251da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK:
5261da177e4SLinus Torvalds selector = HUB_LED_GREEN;
5271da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK_OFF;
5281da177e4SLinus Torvalds break;
5291da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK_OFF:
5301da177e4SLinus Torvalds selector = HUB_LED_OFF;
5311da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK;
5321da177e4SLinus Torvalds break;
5331da177e4SLinus Torvalds /* blinking amber = hw attention */
5341da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK:
5351da177e4SLinus Torvalds selector = HUB_LED_AMBER;
5361da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK_OFF;
5371da177e4SLinus Torvalds break;
5381da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK_OFF:
5391da177e4SLinus Torvalds selector = HUB_LED_OFF;
5401da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK;
5411da177e4SLinus Torvalds break;
5421da177e4SLinus Torvalds /* blink green/amber = reserved */
5431da177e4SLinus Torvalds case INDICATOR_ALT_BLINK:
5441da177e4SLinus Torvalds selector = HUB_LED_GREEN;
5451da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK_OFF;
5461da177e4SLinus Torvalds break;
5471da177e4SLinus Torvalds case INDICATOR_ALT_BLINK_OFF:
5481da177e4SLinus Torvalds selector = HUB_LED_AMBER;
5491da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK;
5501da177e4SLinus Torvalds break;
5511da177e4SLinus Torvalds default:
5521da177e4SLinus Torvalds continue;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds if (selector != HUB_LED_AUTO)
5551da177e4SLinus Torvalds changed = 1;
5561da177e4SLinus Torvalds set_port_led(hub, i + 1, selector);
5571da177e4SLinus Torvalds hub->indicator[i] = mode;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds if (!changed && blinkenlights) {
5601da177e4SLinus Torvalds cursor++;
5613bbc47d8SKrzysztof Mazur cursor %= hdev->maxchild;
5621da177e4SLinus Torvalds set_port_led(hub, cursor + 1, HUB_LED_GREEN);
5631da177e4SLinus Torvalds hub->indicator[cursor] = INDICATOR_CYCLE;
5641da177e4SLinus Torvalds changed++;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds if (changed)
56722f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq,
56822f6a0f0SShaibal Dutta &hub->leds, LED_CYCLE_PERIOD);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds
5711da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
5721da177e4SLinus Torvalds #define USB_STS_TIMEOUT 1000
5731da177e4SLinus Torvalds #define USB_STS_RETRIES 5
5741da177e4SLinus Torvalds
5751da177e4SLinus Torvalds /*
5761da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.6
5771da177e4SLinus Torvalds */
get_hub_status(struct usb_device * hdev,struct usb_hub_status * data)5781da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
5791da177e4SLinus Torvalds struct usb_hub_status *data)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds int i, status = -ETIMEDOUT;
5821da177e4SLinus Torvalds
5833824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES &&
5843824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) {
5851da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
5861da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
5871da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT);
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds return status;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds
5921da177e4SLinus Torvalds /*
5931da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7
5940cdd49a1SMathias Nyman * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
5951da177e4SLinus Torvalds */
get_port_status(struct usb_device * hdev,int port1,void * data,u16 value,u16 length)5961da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
5970cdd49a1SMathias Nyman void *data, u16 value, u16 length)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds int i, status = -ETIMEDOUT;
6001da177e4SLinus Torvalds
6013824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES &&
6023824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) {
6031da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
6040cdd49a1SMathias Nyman USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
6050cdd49a1SMathias Nyman port1, data, length, USB_STS_TIMEOUT);
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds return status;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds
hub_ext_port_status(struct usb_hub * hub,int port1,int type,u16 * status,u16 * change,u32 * ext_status)6100cdd49a1SMathias Nyman static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
6110cdd49a1SMathias Nyman u16 *status, u16 *change, u32 *ext_status)
6123eb14915SAlan Stern {
6133eb14915SAlan Stern int ret;
6140cdd49a1SMathias Nyman int len = 4;
6150cdd49a1SMathias Nyman
6160cdd49a1SMathias Nyman if (type != HUB_PORT_STATUS)
6170cdd49a1SMathias Nyman len = 8;
6183eb14915SAlan Stern
6193eb14915SAlan Stern mutex_lock(&hub->status_mutex);
6200cdd49a1SMathias Nyman ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
6210cdd49a1SMathias Nyman if (ret < len) {
622e9e88fb7SAlan Stern if (ret != -ENODEV)
6233eb14915SAlan Stern dev_err(hub->intfdev,
6243eb14915SAlan Stern "%s failed (err = %d)\n", __func__, ret);
6253eb14915SAlan Stern if (ret >= 0)
6263eb14915SAlan Stern ret = -EIO;
6273eb14915SAlan Stern } else {
6283eb14915SAlan Stern *status = le16_to_cpu(hub->status->port.wPortStatus);
6293eb14915SAlan Stern *change = le16_to_cpu(hub->status->port.wPortChange);
6300cdd49a1SMathias Nyman if (type != HUB_PORT_STATUS && ext_status)
6310cdd49a1SMathias Nyman *ext_status = le32_to_cpu(
6320cdd49a1SMathias Nyman hub->status->port.dwExtPortStatus);
6333eb14915SAlan Stern ret = 0;
6343eb14915SAlan Stern }
6353eb14915SAlan Stern mutex_unlock(&hub->status_mutex);
6363eb14915SAlan Stern return ret;
6373eb14915SAlan Stern }
6383eb14915SAlan Stern
usb_hub_port_status(struct usb_hub * hub,int port1,u16 * status,u16 * change)639f061f43dSMichael Grzeschik int usb_hub_port_status(struct usb_hub *hub, int port1,
6400cdd49a1SMathias Nyman u16 *status, u16 *change)
6410cdd49a1SMathias Nyman {
6420cdd49a1SMathias Nyman return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
6430cdd49a1SMathias Nyman status, change, NULL);
6440cdd49a1SMathias Nyman }
6450cdd49a1SMathias Nyman
hub_resubmit_irq_urb(struct usb_hub * hub)6468eb58994SNicolas Saenz Julienne static void hub_resubmit_irq_urb(struct usb_hub *hub)
6478eb58994SNicolas Saenz Julienne {
6488eb58994SNicolas Saenz Julienne unsigned long flags;
6498eb58994SNicolas Saenz Julienne int status;
6508eb58994SNicolas Saenz Julienne
6518eb58994SNicolas Saenz Julienne spin_lock_irqsave(&hub->irq_urb_lock, flags);
6528eb58994SNicolas Saenz Julienne
6538eb58994SNicolas Saenz Julienne if (hub->quiescing) {
6548eb58994SNicolas Saenz Julienne spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
6558eb58994SNicolas Saenz Julienne return;
6568eb58994SNicolas Saenz Julienne }
6578eb58994SNicolas Saenz Julienne
6588eb58994SNicolas Saenz Julienne status = usb_submit_urb(hub->urb, GFP_ATOMIC);
6598eb58994SNicolas Saenz Julienne if (status && status != -ENODEV && status != -EPERM &&
6608eb58994SNicolas Saenz Julienne status != -ESHUTDOWN) {
6618eb58994SNicolas Saenz Julienne dev_err(hub->intfdev, "resubmit --> %d\n", status);
6628eb58994SNicolas Saenz Julienne mod_timer(&hub->irq_urb_retry, jiffies + HZ);
6638eb58994SNicolas Saenz Julienne }
6648eb58994SNicolas Saenz Julienne
6658eb58994SNicolas Saenz Julienne spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
6668eb58994SNicolas Saenz Julienne }
6678eb58994SNicolas Saenz Julienne
hub_retry_irq_urb(struct timer_list * t)6688eb58994SNicolas Saenz Julienne static void hub_retry_irq_urb(struct timer_list *t)
6698eb58994SNicolas Saenz Julienne {
6708eb58994SNicolas Saenz Julienne struct usb_hub *hub = from_timer(hub, t, irq_urb_retry);
6718eb58994SNicolas Saenz Julienne
6728eb58994SNicolas Saenz Julienne hub_resubmit_irq_urb(hub);
6738eb58994SNicolas Saenz Julienne }
6748eb58994SNicolas Saenz Julienne
6758eb58994SNicolas Saenz Julienne
kick_hub_wq(struct usb_hub * hub)67632a69589SPetr Mladek static void kick_hub_wq(struct usb_hub *hub)
6771da177e4SLinus Torvalds {
67832a69589SPetr Mladek struct usb_interface *intf;
6791da177e4SLinus Torvalds
68032a69589SPetr Mladek if (hub->disconnected || work_pending(&hub->events))
68132a69589SPetr Mladek return;
6828e4ceb38SAlan Stern
68332a69589SPetr Mladek /*
68432a69589SPetr Mladek * Suppress autosuspend until the event is proceed.
68532a69589SPetr Mladek *
68632a69589SPetr Mladek * Be careful and make sure that the symmetric operation is
68732a69589SPetr Mladek * always called. We are here only when there is no pending
68832a69589SPetr Mladek * work for this hub. Therefore put the interface either when
68932a69589SPetr Mladek * the new work is called or when it is canceled.
69032a69589SPetr Mladek */
69132a69589SPetr Mladek intf = to_usb_interface(hub->intfdev);
69232a69589SPetr Mladek usb_autopm_get_interface_no_resume(intf);
6938dbc001bSAlan Stern hub_get(hub);
69432a69589SPetr Mladek
69532a69589SPetr Mladek if (queue_work(hub_wq, &hub->events))
69632a69589SPetr Mladek return;
69732a69589SPetr Mladek
69832a69589SPetr Mladek /* the work has already been scheduled */
69932a69589SPetr Mladek usb_autopm_put_interface_async(intf);
7008dbc001bSAlan Stern hub_put(hub);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds
usb_kick_hub_wq(struct usb_device * hdev)70359d48b3fSPetr Mladek void usb_kick_hub_wq(struct usb_device *hdev)
7041da177e4SLinus Torvalds {
705ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
70625118084SAlan Stern
70725118084SAlan Stern if (hub)
70832a69589SPetr Mladek kick_hub_wq(hub);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds
7114ee823b8SSarah Sharp /*
7124ee823b8SSarah Sharp * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
7134ee823b8SSarah Sharp * Notification, which indicates it had initiated remote wakeup.
7144ee823b8SSarah Sharp *
7154ee823b8SSarah Sharp * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
7164ee823b8SSarah Sharp * device initiates resume, so the USB core will not receive notice of the
7174ee823b8SSarah Sharp * resume through the normal hub interrupt URB.
7184ee823b8SSarah Sharp */
usb_wakeup_notification(struct usb_device * hdev,unsigned int portnum)7194ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev,
7204ee823b8SSarah Sharp unsigned int portnum)
7214ee823b8SSarah Sharp {
7224ee823b8SSarah Sharp struct usb_hub *hub;
72383a62c51SRavi Chandra Sadineni struct usb_port *port_dev;
7244ee823b8SSarah Sharp
7254ee823b8SSarah Sharp if (!hdev)
7264ee823b8SSarah Sharp return;
7274ee823b8SSarah Sharp
728ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(hdev);
7294ee823b8SSarah Sharp if (hub) {
73083a62c51SRavi Chandra Sadineni port_dev = hub->ports[portnum - 1];
73183a62c51SRavi Chandra Sadineni if (port_dev && port_dev->child)
73283a62c51SRavi Chandra Sadineni pm_wakeup_event(&port_dev->child->dev, 0);
73383a62c51SRavi Chandra Sadineni
7344ee823b8SSarah Sharp set_bit(portnum, hub->wakeup_bits);
73532a69589SPetr Mladek kick_hub_wq(hub);
7364ee823b8SSarah Sharp }
7374ee823b8SSarah Sharp }
7384ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification);
7391da177e4SLinus Torvalds
7401da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
hub_irq(struct urb * urb)7417d12e780SDavid Howells static void hub_irq(struct urb *urb)
7421da177e4SLinus Torvalds {
743ec17cf1cSTobias Klauser struct usb_hub *hub = urb->context;
744e015268dSAlan Stern int status = urb->status;
74571d2718fSRoel Kluin unsigned i;
7461da177e4SLinus Torvalds unsigned long bits;
7471da177e4SLinus Torvalds
748e015268dSAlan Stern switch (status) {
7491da177e4SLinus Torvalds case -ENOENT: /* synchronous unlink */
7501da177e4SLinus Torvalds case -ECONNRESET: /* async unlink */
7511da177e4SLinus Torvalds case -ESHUTDOWN: /* hardware going away */
7521da177e4SLinus Torvalds return;
7531da177e4SLinus Torvalds
7541da177e4SLinus Torvalds default: /* presumably an error */
7551da177e4SLinus Torvalds /* Cause a hub reset after 10 consecutive errors */
756e015268dSAlan Stern dev_dbg(hub->intfdev, "transfer --> %d\n", status);
7571da177e4SLinus Torvalds if ((++hub->nerrors < 10) || hub->error)
7581da177e4SLinus Torvalds goto resubmit;
759e015268dSAlan Stern hub->error = status;
760df561f66SGustavo A. R. Silva fallthrough;
7611da177e4SLinus Torvalds
76237ebb549SPetr Mladek /* let hub_wq handle things */
7631da177e4SLinus Torvalds case 0: /* we got data: port status changed */
7641da177e4SLinus Torvalds bits = 0;
7651da177e4SLinus Torvalds for (i = 0; i < urb->actual_length; ++i)
7661da177e4SLinus Torvalds bits |= ((unsigned long) ((*hub->buffer)[i]))
7671da177e4SLinus Torvalds << (i*8);
7681da177e4SLinus Torvalds hub->event_bits[0] = bits;
7691da177e4SLinus Torvalds break;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds
7721da177e4SLinus Torvalds hub->nerrors = 0;
7731da177e4SLinus Torvalds
77437ebb549SPetr Mladek /* Something happened, let hub_wq figure it out */
77532a69589SPetr Mladek kick_hub_wq(hub);
7761da177e4SLinus Torvalds
7771da177e4SLinus Torvalds resubmit:
7788eb58994SNicolas Saenz Julienne hub_resubmit_irq_urb(hub);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds
7811da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
7821da177e4SLinus Torvalds static inline int
hub_clear_tt_buffer(struct usb_device * hdev,u16 devinfo,u16 tt)7831da177e4SLinus Torvalds hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
7841da177e4SLinus Torvalds {
7852c7b871bSWilliam Gulland /* Need to clear both directions for control ep */
7862c7b871bSWilliam Gulland if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
7872c7b871bSWilliam Gulland USB_ENDPOINT_XFER_CONTROL) {
7881afe33a7SOliver Neukum int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
7892c7b871bSWilliam Gulland HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
7902c7b871bSWilliam Gulland devinfo ^ 0x8000, tt, NULL, 0, 1000);
7912c7b871bSWilliam Gulland if (status)
7922c7b871bSWilliam Gulland return status;
7932c7b871bSWilliam Gulland }
7941afe33a7SOliver Neukum return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
7951afe33a7SOliver Neukum HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
7961afe33a7SOliver Neukum tt, NULL, 0, 1000);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds
7991da177e4SLinus Torvalds /*
80037ebb549SPetr Mladek * enumeration blocks hub_wq for a long time. we use keventd instead, since
8011da177e4SLinus Torvalds * long blocking there is the exception, not the rule. accordingly, HCDs
8021da177e4SLinus Torvalds * talking to TTs must queue control transfers (not just bulk and iso), so
8031da177e4SLinus Torvalds * both can talk to the same hub concurrently.
8041da177e4SLinus Torvalds */
hub_tt_work(struct work_struct * work)805cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
8061da177e4SLinus Torvalds {
807c4028958SDavid Howells struct usb_hub *hub =
808cb88a1b8SAlan Stern container_of(work, struct usb_hub, tt.clear_work);
8091da177e4SLinus Torvalds unsigned long flags;
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds spin_lock_irqsave(&hub->tt.lock, flags);
8123b6054daSOctavian Purdila while (!list_empty(&hub->tt.clear_list)) {
813d0f830d3SH Hartley Sweeten struct list_head *next;
8141da177e4SLinus Torvalds struct usb_tt_clear *clear;
8151da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
816cb88a1b8SAlan Stern const struct hc_driver *drv;
8171da177e4SLinus Torvalds int status;
8181da177e4SLinus Torvalds
819d0f830d3SH Hartley Sweeten next = hub->tt.clear_list.next;
820d0f830d3SH Hartley Sweeten clear = list_entry(next, struct usb_tt_clear, clear_list);
8211da177e4SLinus Torvalds list_del(&clear->clear_list);
8221da177e4SLinus Torvalds
8231da177e4SLinus Torvalds /* drop lock so HCD can concurrently report other TT errors */
8241da177e4SLinus Torvalds spin_unlock_irqrestore(&hub->tt.lock, flags);
8251da177e4SLinus Torvalds status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
826e9e88fb7SAlan Stern if (status && status != -ENODEV)
8271da177e4SLinus Torvalds dev_err(&hdev->dev,
8281da177e4SLinus Torvalds "clear tt %d (%04x) error %d\n",
8291da177e4SLinus Torvalds clear->tt, clear->devinfo, status);
830cb88a1b8SAlan Stern
831cb88a1b8SAlan Stern /* Tell the HCD, even if the operation failed */
832cb88a1b8SAlan Stern drv = clear->hcd->driver;
833cb88a1b8SAlan Stern if (drv->clear_tt_buffer_complete)
834cb88a1b8SAlan Stern (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
835cb88a1b8SAlan Stern
8361da177e4SLinus Torvalds kfree(clear);
837cb88a1b8SAlan Stern spin_lock_irqsave(&hub->tt.lock, flags);
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds spin_unlock_irqrestore(&hub->tt.lock, flags);
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds
8421da177e4SLinus Torvalds /**
843971fcd49SLan Tianyu * usb_hub_set_port_power - control hub port's power state
84441341261SMathias Nyman * @hdev: USB device belonging to the usb hub
84541341261SMathias Nyman * @hub: target hub
846971fcd49SLan Tianyu * @port1: port index
847971fcd49SLan Tianyu * @set: expected status
848971fcd49SLan Tianyu *
849971fcd49SLan Tianyu * call this function to control port's power via setting or
850971fcd49SLan Tianyu * clearing the port's PORT_POWER feature.
851626f090cSYacine Belkadi *
852626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise.
853971fcd49SLan Tianyu */
usb_hub_set_port_power(struct usb_device * hdev,struct usb_hub * hub,int port1,bool set)85441341261SMathias Nyman int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
85541341261SMathias Nyman int port1, bool set)
856971fcd49SLan Tianyu {
857971fcd49SLan Tianyu int ret;
858971fcd49SLan Tianyu
859971fcd49SLan Tianyu if (set)
860971fcd49SLan Tianyu ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
861971fcd49SLan Tianyu else
862ad493e5eSLan Tianyu ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
863ad493e5eSLan Tianyu
864d5c3834eSDan Williams if (ret)
865971fcd49SLan Tianyu return ret;
866d5c3834eSDan Williams
867d5c3834eSDan Williams if (set)
868d5c3834eSDan Williams set_bit(port1, hub->power_bits);
869d5c3834eSDan Williams else
870d5c3834eSDan Williams clear_bit(port1, hub->power_bits);
871d5c3834eSDan Williams return 0;
872971fcd49SLan Tianyu }
873971fcd49SLan Tianyu
874971fcd49SLan Tianyu /**
875cb88a1b8SAlan Stern * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
876cb88a1b8SAlan Stern * @urb: an URB associated with the failed or incomplete split transaction
8771da177e4SLinus Torvalds *
8781da177e4SLinus Torvalds * High speed HCDs use this to tell the hub driver that some split control or
8791da177e4SLinus Torvalds * bulk transaction failed in a way that requires clearing internal state of
8801da177e4SLinus Torvalds * a transaction translator. This is normally detected (and reported) from
8811da177e4SLinus Torvalds * interrupt context.
8821da177e4SLinus Torvalds *
8831da177e4SLinus Torvalds * It may not be possible for that hub to handle additional full (or low)
8841da177e4SLinus Torvalds * speed transactions until that state is fully cleared out.
885626f090cSYacine Belkadi *
886626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise.
8871da177e4SLinus Torvalds */
usb_hub_clear_tt_buffer(struct urb * urb)888cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
8891da177e4SLinus Torvalds {
890cb88a1b8SAlan Stern struct usb_device *udev = urb->dev;
891cb88a1b8SAlan Stern int pipe = urb->pipe;
8921da177e4SLinus Torvalds struct usb_tt *tt = udev->tt;
8931da177e4SLinus Torvalds unsigned long flags;
8941da177e4SLinus Torvalds struct usb_tt_clear *clear;
8951da177e4SLinus Torvalds
8961da177e4SLinus Torvalds /* we've got to cope with an arbitrary number of pending TT clears,
8971da177e4SLinus Torvalds * since each TT has "at least two" buffers that can need it (and
8981da177e4SLinus Torvalds * there can be many TTs per hub). even if they're uncommon.
8991da177e4SLinus Torvalds */
900d544d273SGreg Kroah-Hartman clear = kmalloc(sizeof *clear, GFP_ATOMIC);
901d544d273SGreg Kroah-Hartman if (clear == NULL) {
9021da177e4SLinus Torvalds dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
9031da177e4SLinus Torvalds /* FIXME recover somehow ... RESET_TT? */
904cb88a1b8SAlan Stern return -ENOMEM;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds
9071da177e4SLinus Torvalds /* info that CLEAR_TT_BUFFER needs */
9081da177e4SLinus Torvalds clear->tt = tt->multi ? udev->ttport : 1;
9091da177e4SLinus Torvalds clear->devinfo = usb_pipeendpoint (pipe);
9104998f1efSJim Lin clear->devinfo |= ((u16)udev->devaddr) << 4;
9111da177e4SLinus Torvalds clear->devinfo |= usb_pipecontrol(pipe)
9121da177e4SLinus Torvalds ? (USB_ENDPOINT_XFER_CONTROL << 11)
9131da177e4SLinus Torvalds : (USB_ENDPOINT_XFER_BULK << 11);
9141da177e4SLinus Torvalds if (usb_pipein(pipe))
9151da177e4SLinus Torvalds clear->devinfo |= 1 << 15;
9161da177e4SLinus Torvalds
917cb88a1b8SAlan Stern /* info for completion callback */
918cb88a1b8SAlan Stern clear->hcd = bus_to_hcd(udev->bus);
919cb88a1b8SAlan Stern clear->ep = urb->ep;
920cb88a1b8SAlan Stern
9211da177e4SLinus Torvalds /* tell keventd to clear state for this TT */
9221da177e4SLinus Torvalds spin_lock_irqsave(&tt->lock, flags);
9231da177e4SLinus Torvalds list_add_tail(&clear->clear_list, &tt->clear_list);
924cb88a1b8SAlan Stern schedule_work(&tt->clear_work);
9251da177e4SLinus Torvalds spin_unlock_irqrestore(&tt->lock, flags);
926cb88a1b8SAlan Stern return 0;
9271da177e4SLinus Torvalds }
928cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
9291da177e4SLinus Torvalds
hub_power_on(struct usb_hub * hub,bool do_delay)9307ad3c470SDan Williams static void hub_power_on(struct usb_hub *hub, bool do_delay)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds int port1;
9331da177e4SLinus Torvalds
9344489a571SAlan Stern /* Enable power on each port. Some hubs have reserved values
9354489a571SAlan Stern * of LPSM (> 2) in their descriptors, even though they are
9364489a571SAlan Stern * USB 2.0 hubs. Some hubs do not implement port-power switching
9374489a571SAlan Stern * but only emulate it. In all cases, the ports won't work
9384489a571SAlan Stern * unless we send these messages to the hub.
9394489a571SAlan Stern */
9409262c19dSDan Williams if (hub_is_port_power_switchable(hub))
9411da177e4SLinus Torvalds dev_dbg(hub->intfdev, "enabling power on all ports\n");
9424489a571SAlan Stern else
9434489a571SAlan Stern dev_dbg(hub->intfdev, "trying to enable port power on "
9444489a571SAlan Stern "non-switchable hub\n");
9453bbc47d8SKrzysztof Mazur for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
946d5c3834eSDan Williams if (test_bit(port1, hub->power_bits))
9474489a571SAlan Stern set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
948ad493e5eSLan Tianyu else
949ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
950ad493e5eSLan Tianyu USB_PORT_FEAT_POWER);
9518520f380SAlan Stern if (do_delay)
9527ad3c470SDan Williams msleep(hub_power_on_good_delay(hub));
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds
hub_hub_status(struct usb_hub * hub,u16 * status,u16 * change)9551da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
9561da177e4SLinus Torvalds u16 *status, u16 *change)
9571da177e4SLinus Torvalds {
9581da177e4SLinus Torvalds int ret;
9591da177e4SLinus Torvalds
960db90e7a1SAlan Stern mutex_lock(&hub->status_mutex);
9611da177e4SLinus Torvalds ret = get_hub_status(hub->hdev, &hub->status->hub);
962e9e88fb7SAlan Stern if (ret < 0) {
963e9e88fb7SAlan Stern if (ret != -ENODEV)
9641da177e4SLinus Torvalds dev_err(hub->intfdev,
965441b62c1SHarvey Harrison "%s failed (err = %d)\n", __func__, ret);
966e9e88fb7SAlan Stern } else {
9671da177e4SLinus Torvalds *status = le16_to_cpu(hub->status->hub.wHubStatus);
9681da177e4SLinus Torvalds *change = le16_to_cpu(hub->status->hub.wHubChange);
9691da177e4SLinus Torvalds ret = 0;
9701da177e4SLinus Torvalds }
971db90e7a1SAlan Stern mutex_unlock(&hub->status_mutex);
9721da177e4SLinus Torvalds return ret;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds
hub_set_port_link_state(struct usb_hub * hub,int port1,unsigned int link_status)97541e7e056SSarah Sharp static int hub_set_port_link_state(struct usb_hub *hub, int port1,
97641e7e056SSarah Sharp unsigned int link_status)
97741e7e056SSarah Sharp {
97841e7e056SSarah Sharp return set_port_feature(hub->hdev,
97941e7e056SSarah Sharp port1 | (link_status << 3),
98041e7e056SSarah Sharp USB_PORT_FEAT_LINK_STATE);
98141e7e056SSarah Sharp }
98241e7e056SSarah Sharp
98341e7e056SSarah Sharp /*
9846d42fcdbSJustin P. Mattock * Disable a port and mark a logical connect-change event, so that some
98537ebb549SPetr Mladek * time later hub_wq will disconnect() any existing usb_device on the port
9860458d5b4SAlan Stern * and will re-enumerate if there actually is a device attached.
9870458d5b4SAlan Stern */
hub_port_logical_disconnect(struct usb_hub * hub,int port1)9880458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
9897d069b7dSAlan Stern {
990d99f6b41SDan Williams dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
9910458d5b4SAlan Stern hub_port_disable(hub, port1, 1);
9920458d5b4SAlan Stern
9930458d5b4SAlan Stern /* FIXME let caller ask to power down the port:
9940458d5b4SAlan Stern * - some devices won't enumerate without a VBUS power cycle
9950458d5b4SAlan Stern * - SRP saves power that way
9960458d5b4SAlan Stern * - ... new call, TBD ...
9970458d5b4SAlan Stern * That's easy if this hub can switch power per-port, and
99837ebb549SPetr Mladek * hub_wq reactivates the port later (timer, SRP, etc).
9990458d5b4SAlan Stern * Powerdown must be optional, because of reset/DFU.
10000458d5b4SAlan Stern */
10010458d5b4SAlan Stern
10020458d5b4SAlan Stern set_bit(port1, hub->change_bits);
100332a69589SPetr Mladek kick_hub_wq(hub);
10040458d5b4SAlan Stern }
10050458d5b4SAlan Stern
1006253e0572SAlan Stern /**
1007253e0572SAlan Stern * usb_remove_device - disable a device's port on its parent hub
1008253e0572SAlan Stern * @udev: device to be disabled and removed
1009253e0572SAlan Stern * Context: @udev locked, must be able to sleep.
1010253e0572SAlan Stern *
101137ebb549SPetr Mladek * After @udev's port has been disabled, hub_wq is notified and it will
1012253e0572SAlan Stern * see that the device has been disconnected. When the device is
1013253e0572SAlan Stern * physically unplugged and something is plugged in, the events will
1014253e0572SAlan Stern * be received and processed normally.
1015626f090cSYacine Belkadi *
1016626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise.
1017253e0572SAlan Stern */
usb_remove_device(struct usb_device * udev)1018253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
1019253e0572SAlan Stern {
1020253e0572SAlan Stern struct usb_hub *hub;
1021253e0572SAlan Stern struct usb_interface *intf;
102260e3f6e4SEugeniu Rosca int ret;
1023253e0572SAlan Stern
1024253e0572SAlan Stern if (!udev->parent) /* Can't remove a root hub */
1025253e0572SAlan Stern return -EINVAL;
1026ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent);
1027253e0572SAlan Stern intf = to_usb_interface(hub->intfdev);
1028253e0572SAlan Stern
102960e3f6e4SEugeniu Rosca ret = usb_autopm_get_interface(intf);
103060e3f6e4SEugeniu Rosca if (ret < 0)
103160e3f6e4SEugeniu Rosca return ret;
103260e3f6e4SEugeniu Rosca
1033253e0572SAlan Stern set_bit(udev->portnum, hub->removed_bits);
1034253e0572SAlan Stern hub_port_logical_disconnect(hub, udev->portnum);
1035253e0572SAlan Stern usb_autopm_put_interface(intf);
1036253e0572SAlan Stern return 0;
1037253e0572SAlan Stern }
1038253e0572SAlan Stern
10396ee0b270SAlan Stern enum hub_activation_type {
10408e4ceb38SAlan Stern HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
10418520f380SAlan Stern HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
10426ee0b270SAlan Stern };
10435e6effaeSAlan Stern
10448520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
10458520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
10468520f380SAlan Stern
hub_activate(struct usb_hub * hub,enum hub_activation_type type)1047f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
10485e6effaeSAlan Stern {
10495e6effaeSAlan Stern struct usb_device *hdev = hub->hdev;
1050653a39d1SSarah Sharp struct usb_hcd *hcd;
1051653a39d1SSarah Sharp int ret;
10525e6effaeSAlan Stern int port1;
1053f2835219SAlan Stern int status;
1054948fea37SAlan Stern bool need_debounce_delay = false;
10558520f380SAlan Stern unsigned delay;
10568520f380SAlan Stern
10578520f380SAlan Stern /* Continue a partial initialization */
1058e50293efSAlan Stern if (type == HUB_INIT2 || type == HUB_INIT3) {
105907d316a2SAlan Stern device_lock(&hdev->dev);
1060e50293efSAlan Stern
1061e50293efSAlan Stern /* Was the hub disconnected while we were waiting? */
1062ca5cbc8bSAlan Stern if (hub->disconnected)
1063ca5cbc8bSAlan Stern goto disconnected;
10648520f380SAlan Stern if (type == HUB_INIT2)
10658520f380SAlan Stern goto init2;
10668520f380SAlan Stern goto init3;
1067e50293efSAlan Stern }
10688dbc001bSAlan Stern hub_get(hub);
10695e6effaeSAlan Stern
1070a45aa3b3SElric Fu /* The superspeed hub except for root hub has to use Hub Depth
1071a45aa3b3SElric Fu * value as an offset into the route string to locate the bits
1072a45aa3b3SElric Fu * it uses to determine the downstream port number. So hub driver
1073a45aa3b3SElric Fu * should send a set hub depth request to superspeed hub after
1074a45aa3b3SElric Fu * the superspeed hub is set configuration in initialization or
1075a45aa3b3SElric Fu * reset procedure.
1076a45aa3b3SElric Fu *
1077a45aa3b3SElric Fu * After a resume, port power should still be on.
1078f2835219SAlan Stern * For any other type of activation, turn it on.
1079f2835219SAlan Stern */
10808520f380SAlan Stern if (type != HUB_RESUME) {
1081a45aa3b3SElric Fu if (hdev->parent && hub_is_superspeed(hdev)) {
10821afe33a7SOliver Neukum ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
10831afe33a7SOliver Neukum HUB_SET_DEPTH, USB_RT_HUB,
1084a45aa3b3SElric Fu hdev->level - 1, 0, NULL, 0,
1085a45aa3b3SElric Fu USB_CTRL_SET_TIMEOUT);
10861afe33a7SOliver Neukum if (ret < 0)
1087a45aa3b3SElric Fu dev_err(hub->intfdev,
1088a45aa3b3SElric Fu "set hub depth failed\n");
1089a45aa3b3SElric Fu }
10908520f380SAlan Stern
10918520f380SAlan Stern /* Speed up system boot by using a delayed_work for the
10928520f380SAlan Stern * hub's initial power-up delays. This is pretty awkward
10938520f380SAlan Stern * and the implementation looks like a home-brewed sort of
10948520f380SAlan Stern * setjmp/longjmp, but it saves at least 100 ms for each
10958520f380SAlan Stern * root hub (assuming usbcore is compiled into the kernel
10968520f380SAlan Stern * rather than as a module). It adds up.
10978520f380SAlan Stern *
10988520f380SAlan Stern * This can't be done for HUB_RESUME or HUB_RESET_RESUME
10998520f380SAlan Stern * because for those activation types the ports have to be
11008520f380SAlan Stern * operational when we return. In theory this could be done
11018520f380SAlan Stern * for HUB_POST_RESET, but it's easier not to.
11028520f380SAlan Stern */
11038520f380SAlan Stern if (type == HUB_INIT) {
110417a364e2SKris Borer delay = hub_power_on_good_delay(hub);
11057ad3c470SDan Williams
11067ad3c470SDan Williams hub_power_on(hub, false);
110777fa83cfSTejun Heo INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
110822f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq,
110922f6a0f0SShaibal Dutta &hub->init_work,
11108520f380SAlan Stern msecs_to_jiffies(delay));
111161fbeba1SAlan Stern
111261fbeba1SAlan Stern /* Suppress autosuspend until init is done */
11138e4ceb38SAlan Stern usb_autopm_get_interface_no_resume(
11148e4ceb38SAlan Stern to_usb_interface(hub->intfdev));
11158520f380SAlan Stern return; /* Continues at init2: below */
1116653a39d1SSarah Sharp } else if (type == HUB_RESET_RESUME) {
1117653a39d1SSarah Sharp /* The internal host controller state for the hub device
1118653a39d1SSarah Sharp * may be gone after a host power loss on system resume.
1119653a39d1SSarah Sharp * Update the device's info so the HW knows it's a hub.
1120653a39d1SSarah Sharp */
1121653a39d1SSarah Sharp hcd = bus_to_hcd(hdev->bus);
1122653a39d1SSarah Sharp if (hcd->driver->update_hub_device) {
1123653a39d1SSarah Sharp ret = hcd->driver->update_hub_device(hcd, hdev,
1124653a39d1SSarah Sharp &hub->tt, GFP_NOIO);
1125653a39d1SSarah Sharp if (ret < 0) {
11261ccc417eSJoe Perches dev_err(hub->intfdev,
11271ccc417eSJoe Perches "Host not accepting hub info update\n");
11281ccc417eSJoe Perches dev_err(hub->intfdev,
11291ccc417eSJoe Perches "LS/FS devices and hubs may not work under this hub\n");
1130653a39d1SSarah Sharp }
1131653a39d1SSarah Sharp }
1132653a39d1SSarah Sharp hub_power_on(hub, true);
11338520f380SAlan Stern } else {
11348520f380SAlan Stern hub_power_on(hub, true);
11358520f380SAlan Stern }
113600558586SKai-Heng Feng /* Give some time on remote wakeup to let links to transit to U0 */
113700558586SKai-Heng Feng } else if (hub_is_superspeed(hub->hdev))
113800558586SKai-Heng Feng msleep(20);
113900558586SKai-Heng Feng
11408520f380SAlan Stern init2:
1141f2835219SAlan Stern
1142d99f6b41SDan Williams /*
114337ebb549SPetr Mladek * Check each port and set hub->change_bits to let hub_wq know
11446ee0b270SAlan Stern * which ports need attention.
11455e6effaeSAlan Stern */
11465e6effaeSAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1147d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
1148d99f6b41SDan Williams struct usb_device *udev = port_dev->child;
11495e6effaeSAlan Stern u16 portstatus, portchange;
11505e6effaeSAlan Stern
11516ee0b270SAlan Stern portstatus = portchange = 0;
1152f061f43dSMichael Grzeschik status = usb_hub_port_status(hub, port1, &portstatus, &portchange);
1153245b2eecSGuenter Roeck if (status)
1154245b2eecSGuenter Roeck goto abort;
1155245b2eecSGuenter Roeck
11566ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1157d99f6b41SDan Williams dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1158d99f6b41SDan Williams portstatus, portchange);
11595e6effaeSAlan Stern
1160d99f6b41SDan Williams /*
1161d99f6b41SDan Williams * After anything other than HUB_RESUME (i.e., initialization
11626ee0b270SAlan Stern * or any sort of reset), every port should be disabled.
11636ee0b270SAlan Stern * Unconnected ports should likewise be disabled (paranoia),
11646ee0b270SAlan Stern * and so should ports for which we have no usb_device.
11655e6effaeSAlan Stern */
11666ee0b270SAlan Stern if ((portstatus & USB_PORT_STAT_ENABLE) && (
11676ee0b270SAlan Stern type != HUB_RESUME ||
11686ee0b270SAlan Stern !(portstatus & USB_PORT_STAT_CONNECTION) ||
11696ee0b270SAlan Stern !udev ||
11706ee0b270SAlan Stern udev->state == USB_STATE_NOTATTACHED)) {
11719f0a6cd3SAndiry Xu /*
11729f0a6cd3SAndiry Xu * USB3 protocol ports will automatically transition
11739f0a6cd3SAndiry Xu * to Enabled state when detect an USB3.0 device attach.
1174fd1ac4cfSDan Williams * Do not disable USB3 protocol ports, just pretend
1175fd1ac4cfSDan Williams * power was lost
11769f0a6cd3SAndiry Xu */
1177fd1ac4cfSDan Williams portstatus &= ~USB_PORT_STAT_ENABLE;
1178fd1ac4cfSDan Williams if (!hub_is_superspeed(hdev))
1179ad493e5eSLan Tianyu usb_clear_port_feature(hdev, port1,
11809f0a6cd3SAndiry Xu USB_PORT_FEAT_ENABLE);
11819f0a6cd3SAndiry Xu }
11826ee0b270SAlan Stern
11834fdc1790SJan-Marek Glogowski /* Make sure a warm-reset request is handled by port_event */
11844fdc1790SJan-Marek Glogowski if (type == HUB_RESUME &&
11854fdc1790SJan-Marek Glogowski hub_port_warm_reset_required(hub, port1, portstatus))
11864fdc1790SJan-Marek Glogowski set_bit(port1, hub->event_bits);
11874fdc1790SJan-Marek Glogowski
1188e8610894SMathias Nyman /*
1189e8610894SMathias Nyman * Add debounce if USB3 link is in polling/link training state.
1190e8610894SMathias Nyman * Link will automatically transition to Enabled state after
1191e8610894SMathias Nyman * link training completes.
1192e8610894SMathias Nyman */
1193e8610894SMathias Nyman if (hub_is_superspeed(hdev) &&
1194e8610894SMathias Nyman ((portstatus & USB_PORT_STAT_LINK_STATE) ==
1195e8610894SMathias Nyman USB_SS_PORT_LS_POLLING))
1196e8610894SMathias Nyman need_debounce_delay = true;
1197e8610894SMathias Nyman
1198948fea37SAlan Stern /* Clear status-change flags; we'll debounce later */
1199948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) {
1200948fea37SAlan Stern need_debounce_delay = true;
1201ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
1202948fea37SAlan Stern USB_PORT_FEAT_C_CONNECTION);
1203948fea37SAlan Stern }
1204948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) {
1205948fea37SAlan Stern need_debounce_delay = true;
1206ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
1207948fea37SAlan Stern USB_PORT_FEAT_C_ENABLE);
1208948fea37SAlan Stern }
1209e92aee33SJulius Werner if (portchange & USB_PORT_STAT_C_RESET) {
1210e92aee33SJulius Werner need_debounce_delay = true;
1211e92aee33SJulius Werner usb_clear_port_feature(hub->hdev, port1,
1212e92aee33SJulius Werner USB_PORT_FEAT_C_RESET);
1213e92aee33SJulius Werner }
121479c3dd81SDon Zickus if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
121579c3dd81SDon Zickus hub_is_superspeed(hub->hdev)) {
121679c3dd81SDon Zickus need_debounce_delay = true;
1217ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
121879c3dd81SDon Zickus USB_PORT_FEAT_C_BH_PORT_RESET);
121979c3dd81SDon Zickus }
1220253e0572SAlan Stern /* We can forget about a "removed" device when there's a
1221253e0572SAlan Stern * physical disconnect or the connect status changes.
1222253e0572SAlan Stern */
1223253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1224253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION))
1225253e0572SAlan Stern clear_bit(port1, hub->removed_bits);
1226253e0572SAlan Stern
12276ee0b270SAlan Stern if (!udev || udev->state == USB_STATE_NOTATTACHED) {
122837ebb549SPetr Mladek /* Tell hub_wq to disconnect the device or
1229249a32b7SBin Liu * check for a new connection or over current condition.
1230249a32b7SBin Liu * Based on USB2.0 Spec Section 11.12.5,
1231249a32b7SBin Liu * C_PORT_OVER_CURRENT could be set while
1232249a32b7SBin Liu * PORT_OVER_CURRENT is not. So check for any of them.
12336ee0b270SAlan Stern */
123408d1dec6SShen Guang if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
12359c06ac4cSKeiya Nobuta (portchange & USB_PORT_STAT_C_CONNECTION) ||
1236249a32b7SBin Liu (portstatus & USB_PORT_STAT_OVERCURRENT) ||
1237249a32b7SBin Liu (portchange & USB_PORT_STAT_C_OVERCURRENT))
12386ee0b270SAlan Stern set_bit(port1, hub->change_bits);
12396ee0b270SAlan Stern
12406ee0b270SAlan Stern } else if (portstatus & USB_PORT_STAT_ENABLE) {
124172937e1eSSarah Sharp bool port_resumed = (portstatus &
124272937e1eSSarah Sharp USB_PORT_STAT_LINK_STATE) ==
124372937e1eSSarah Sharp USB_SS_PORT_LS_U0;
12446ee0b270SAlan Stern /* The power session apparently survived the resume.
12456ee0b270SAlan Stern * If there was an overcurrent or suspend change
124637ebb549SPetr Mladek * (i.e., remote wakeup request), have hub_wq
124772937e1eSSarah Sharp * take care of it. Look at the port link state
124872937e1eSSarah Sharp * for USB 3.0 hubs, since they don't have a suspend
124972937e1eSSarah Sharp * change bit, and they don't set the port link change
125072937e1eSSarah Sharp * bit on device-initiated resume.
12516ee0b270SAlan Stern */
125272937e1eSSarah Sharp if (portchange || (hub_is_superspeed(hub->hdev) &&
125372937e1eSSarah Sharp port_resumed))
12540f663729SAlan Stern set_bit(port1, hub->event_bits);
12556ee0b270SAlan Stern
12566ee0b270SAlan Stern } else if (udev->persist_enabled) {
12576ee0b270SAlan Stern #ifdef CONFIG_PM
12585e6effaeSAlan Stern udev->reset_resume = 1;
12596ee0b270SAlan Stern #endif
12609f952e26SAlan Stern /* Don't set the change_bits when the device
12619f952e26SAlan Stern * was powered off.
12629f952e26SAlan Stern */
12639f952e26SAlan Stern if (test_bit(port1, hub->power_bits))
12649f952e26SAlan Stern set_bit(port1, hub->change_bits);
12658808f00cSAlan Stern
12666ee0b270SAlan Stern } else {
126737ebb549SPetr Mladek /* The power session is gone; tell hub_wq */
12686ee0b270SAlan Stern usb_set_device_state(udev, USB_STATE_NOTATTACHED);
12696ee0b270SAlan Stern set_bit(port1, hub->change_bits);
12705e6effaeSAlan Stern }
12715e6effaeSAlan Stern }
12725e6effaeSAlan Stern
1273948fea37SAlan Stern /* If no port-status-change flags were set, we don't need any
1274948fea37SAlan Stern * debouncing. If flags were set we can try to debounce the
127537ebb549SPetr Mladek * ports all at once right now, instead of letting hub_wq do them
1276948fea37SAlan Stern * one at a time later on.
1277948fea37SAlan Stern *
127837ebb549SPetr Mladek * If any port-status changes do occur during this delay, hub_wq
1279948fea37SAlan Stern * will see them later and handle them normally.
1280948fea37SAlan Stern */
12818520f380SAlan Stern if (need_debounce_delay) {
12828520f380SAlan Stern delay = HUB_DEBOUNCE_STABLE;
1283f2835219SAlan Stern
12848520f380SAlan Stern /* Don't do a long sleep inside a workqueue routine */
12858520f380SAlan Stern if (type == HUB_INIT2) {
128677fa83cfSTejun Heo INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
128722f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq,
128822f6a0f0SShaibal Dutta &hub->init_work,
12898520f380SAlan Stern msecs_to_jiffies(delay));
129007d316a2SAlan Stern device_unlock(&hdev->dev);
12918520f380SAlan Stern return; /* Continues at init3: below */
12928520f380SAlan Stern } else {
12938520f380SAlan Stern msleep(delay);
12948520f380SAlan Stern }
12958520f380SAlan Stern }
12968520f380SAlan Stern init3:
1297f2835219SAlan Stern hub->quiescing = 0;
1298f2835219SAlan Stern
1299f2835219SAlan Stern status = usb_submit_urb(hub->urb, GFP_NOIO);
1300f2835219SAlan Stern if (status < 0)
1301f2835219SAlan Stern dev_err(hub->intfdev, "activate --> %d\n", status);
1302f2835219SAlan Stern if (hub->has_indicators && blinkenlights)
130322f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq,
130422f6a0f0SShaibal Dutta &hub->leds, LED_CYCLE_PERIOD);
1305f2835219SAlan Stern
1306f2835219SAlan Stern /* Scan all ports that need attention */
130732a69589SPetr Mladek kick_hub_wq(hub);
1308245b2eecSGuenter Roeck abort:
1309ca5cbc8bSAlan Stern if (type == HUB_INIT2 || type == HUB_INIT3) {
13108e4ceb38SAlan Stern /* Allow autosuspend if it was suppressed */
1311ca5cbc8bSAlan Stern disconnected:
13128e4ceb38SAlan Stern usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
131307d316a2SAlan Stern device_unlock(&hdev->dev);
1314ca5cbc8bSAlan Stern }
1315e50293efSAlan Stern
13168dbc001bSAlan Stern hub_put(hub);
13175e6effaeSAlan Stern }
13185e6effaeSAlan Stern
13198520f380SAlan Stern /* Implement the continuations for the delays above */
hub_init_func2(struct work_struct * ws)13208520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
13218520f380SAlan Stern {
13228520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
13238520f380SAlan Stern
13248520f380SAlan Stern hub_activate(hub, HUB_INIT2);
13258520f380SAlan Stern }
13268520f380SAlan Stern
hub_init_func3(struct work_struct * ws)13278520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
13288520f380SAlan Stern {
13298520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
13308520f380SAlan Stern
13318520f380SAlan Stern hub_activate(hub, HUB_INIT3);
13328520f380SAlan Stern }
13338520f380SAlan Stern
13344330354fSAlan Stern enum hub_quiescing_type {
13354330354fSAlan Stern HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
13364330354fSAlan Stern };
13374330354fSAlan Stern
hub_quiesce(struct usb_hub * hub,enum hub_quiescing_type type)13384330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
13394330354fSAlan Stern {
13404330354fSAlan Stern struct usb_device *hdev = hub->hdev;
13418eb58994SNicolas Saenz Julienne unsigned long flags;
13424330354fSAlan Stern int i;
13434330354fSAlan Stern
134437ebb549SPetr Mladek /* hub_wq and related activity won't re-trigger */
13458eb58994SNicolas Saenz Julienne spin_lock_irqsave(&hub->irq_urb_lock, flags);
13464330354fSAlan Stern hub->quiescing = 1;
13478eb58994SNicolas Saenz Julienne spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
13484330354fSAlan Stern
13494330354fSAlan Stern if (type != HUB_SUSPEND) {
13504330354fSAlan Stern /* Disconnect all the children */
13514330354fSAlan Stern for (i = 0; i < hdev->maxchild; ++i) {
1352ff823c79SLan Tianyu if (hub->ports[i]->child)
1353ff823c79SLan Tianyu usb_disconnect(&hub->ports[i]->child);
13544330354fSAlan Stern }
13554330354fSAlan Stern }
13564330354fSAlan Stern
135737ebb549SPetr Mladek /* Stop hub_wq and related activity */
13588eb58994SNicolas Saenz Julienne del_timer_sync(&hub->irq_urb_retry);
13594330354fSAlan Stern usb_kill_urb(hub->urb);
13604330354fSAlan Stern if (hub->has_indicators)
13614330354fSAlan Stern cancel_delayed_work_sync(&hub->leds);
13624330354fSAlan Stern if (hub->tt.hub)
1363036546bfSOctavian Purdila flush_work(&hub->tt.clear_work);
13644330354fSAlan Stern }
13654330354fSAlan Stern
hub_pm_barrier_for_all_ports(struct usb_hub * hub)1366600856c2SAlan Stern static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1367600856c2SAlan Stern {
1368600856c2SAlan Stern int i;
1369600856c2SAlan Stern
1370600856c2SAlan Stern for (i = 0; i < hub->hdev->maxchild; ++i)
1371600856c2SAlan Stern pm_runtime_barrier(&hub->ports[i]->dev);
1372600856c2SAlan Stern }
1373600856c2SAlan Stern
13743eb14915SAlan Stern /* caller has locked the hub device */
hub_pre_reset(struct usb_interface * intf)13753eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
13763eb14915SAlan Stern {
13773eb14915SAlan Stern struct usb_hub *hub = usb_get_intfdata(intf);
13783eb14915SAlan Stern
13794330354fSAlan Stern hub_quiesce(hub, HUB_PRE_RESET);
1380600856c2SAlan Stern hub->in_reset = 1;
1381600856c2SAlan Stern hub_pm_barrier_for_all_ports(hub);
1382f07600cfSAlan Stern return 0;
13837d069b7dSAlan Stern }
13847d069b7dSAlan Stern
13857d069b7dSAlan Stern /* caller has locked the hub device */
hub_post_reset(struct usb_interface * intf)1386f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
13877d069b7dSAlan Stern {
13887de18d8bSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf);
13897de18d8bSAlan Stern
1390600856c2SAlan Stern hub->in_reset = 0;
1391600856c2SAlan Stern hub_pm_barrier_for_all_ports(hub);
1392f2835219SAlan Stern hub_activate(hub, HUB_POST_RESET);
1393f07600cfSAlan Stern return 0;
13947d069b7dSAlan Stern }
13957d069b7dSAlan Stern
hub_configure(struct usb_hub * hub,struct usb_endpoint_descriptor * endpoint)13961da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
13971da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint)
13981da177e4SLinus Torvalds {
1399b356b7c7SSarah Sharp struct usb_hcd *hcd;
14001da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
14011da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev;
14021da177e4SLinus Torvalds u16 hubstatus, hubchange;
140374ad9bd2SGreg Kroah-Hartman u16 wHubCharacteristics;
14041da177e4SLinus Torvalds unsigned int pipe;
1405fa2a9566SLan Tianyu int maxp, ret, i;
14067cbe5dcaSAlan Stern char *message = "out of memory";
1407430ee58eSSebastian Andrzej Siewior unsigned unit_load;
1408430ee58eSSebastian Andrzej Siewior unsigned full_load;
1409d8521afeSDan Williams unsigned maxchild;
14101da177e4SLinus Torvalds
1411d697cddaSAlan Stern hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
14121da177e4SLinus Torvalds if (!hub->buffer) {
14131da177e4SLinus Torvalds ret = -ENOMEM;
14141da177e4SLinus Torvalds goto fail;
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds
14171da177e4SLinus Torvalds hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
14181da177e4SLinus Torvalds if (!hub->status) {
14191da177e4SLinus Torvalds ret = -ENOMEM;
14201da177e4SLinus Torvalds goto fail;
14211da177e4SLinus Torvalds }
1422db90e7a1SAlan Stern mutex_init(&hub->status_mutex);
14231da177e4SLinus Torvalds
1424bec444cdSJohan Hovold hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL);
14251da177e4SLinus Torvalds if (!hub->descriptor) {
14261da177e4SLinus Torvalds ret = -ENOMEM;
14271da177e4SLinus Torvalds goto fail;
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds
14301da177e4SLinus Torvalds /* Request the entire hub descriptor.
14311da177e4SLinus Torvalds * hub->descriptor can handle USB_MAXCHILDREN ports,
14322c25a2c8SJohan Hovold * but a (non-SS) hub can/will return fewer bytes here.
14331da177e4SLinus Torvalds */
1434dbe79bbeSJohn Youn ret = get_hub_descriptor(hdev, hub->descriptor);
14351da177e4SLinus Torvalds if (ret < 0) {
14361da177e4SLinus Torvalds message = "can't read hub descriptor";
14371da177e4SLinus Torvalds goto fail;
143893491cedSJohan Hovold }
143993491cedSJohan Hovold
144093491cedSJohan Hovold maxchild = USB_MAXCHILDREN;
144193491cedSJohan Hovold if (hub_is_superspeed(hdev))
144293491cedSJohan Hovold maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS);
144393491cedSJohan Hovold
144493491cedSJohan Hovold if (hub->descriptor->bNbrPorts > maxchild) {
14451da177e4SLinus Torvalds message = "hub has too many ports!";
14461da177e4SLinus Torvalds ret = -ENODEV;
14471da177e4SLinus Torvalds goto fail;
1448769d7368SDavid Linares } else if (hub->descriptor->bNbrPorts == 0) {
1449769d7368SDavid Linares message = "hub doesn't have any ports!";
1450769d7368SDavid Linares ret = -ENODEV;
1451769d7368SDavid Linares goto fail;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds
1454886ee36eSFelipe Balbi /*
1455886ee36eSFelipe Balbi * Accumulate wHubDelay + 40ns for every hub in the tree of devices.
1456886ee36eSFelipe Balbi * The resulting value will be used for SetIsochDelay() request.
1457886ee36eSFelipe Balbi */
1458886ee36eSFelipe Balbi if (hub_is_superspeed(hdev) || hub_is_superspeedplus(hdev)) {
1459886ee36eSFelipe Balbi u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay);
1460886ee36eSFelipe Balbi
1461886ee36eSFelipe Balbi if (hdev->parent)
1462886ee36eSFelipe Balbi delay += hdev->parent->hub_delay;
1463886ee36eSFelipe Balbi
1464886ee36eSFelipe Balbi delay += USB_TP_TRANSMISSION_DELAY;
1465886ee36eSFelipe Balbi hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX);
1466886ee36eSFelipe Balbi }
1467886ee36eSFelipe Balbi
1468d8521afeSDan Williams maxchild = hub->descriptor->bNbrPorts;
1469d8521afeSDan Williams dev_info(hub_dev, "%d port%s detected\n", maxchild,
1470d8521afeSDan Williams (maxchild == 1) ? "" : "s");
14711da177e4SLinus Torvalds
14726396bb22SKees Cook hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL);
1473ff823c79SLan Tianyu if (!hub->ports) {
14747cbe5dcaSAlan Stern ret = -ENOMEM;
14757cbe5dcaSAlan Stern goto fail;
14767cbe5dcaSAlan Stern }
14777cbe5dcaSAlan Stern
147874ad9bd2SGreg Kroah-Hartman wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1479430ee58eSSebastian Andrzej Siewior if (hub_is_superspeed(hdev)) {
1480430ee58eSSebastian Andrzej Siewior unit_load = 150;
1481430ee58eSSebastian Andrzej Siewior full_load = 900;
1482430ee58eSSebastian Andrzej Siewior } else {
1483430ee58eSSebastian Andrzej Siewior unit_load = 100;
1484430ee58eSSebastian Andrzej Siewior full_load = 500;
1485430ee58eSSebastian Andrzej Siewior }
14861da177e4SLinus Torvalds
1487dbe79bbeSJohn Youn /* FIXME for USB 3.0, skip for now */
1488dbe79bbeSJohn Youn if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1489dbe79bbeSJohn Youn !(hub_is_superspeed(hdev))) {
14901da177e4SLinus Torvalds char portstr[USB_MAXCHILDREN + 1];
14911da177e4SLinus Torvalds
1492d8521afeSDan Williams for (i = 0; i < maxchild; i++)
1493dbe79bbeSJohn Youn portstr[i] = hub->descriptor->u.hs.DeviceRemovable
14941da177e4SLinus Torvalds [((i + 1) / 8)] & (1 << ((i + 1) % 8))
14951da177e4SLinus Torvalds ? 'F' : 'R';
1496d8521afeSDan Williams portstr[maxchild] = 0;
14971da177e4SLinus Torvalds dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
14981da177e4SLinus Torvalds } else
14991da177e4SLinus Torvalds dev_dbg(hub_dev, "standalone hub\n");
15001da177e4SLinus Torvalds
150174ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_LPSM) {
15027bf01185SAman Deep case HUB_CHAR_COMMON_LPSM:
15031da177e4SLinus Torvalds dev_dbg(hub_dev, "ganged power switching\n");
15041da177e4SLinus Torvalds break;
15057bf01185SAman Deep case HUB_CHAR_INDV_PORT_LPSM:
15061da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port power switching\n");
15071da177e4SLinus Torvalds break;
15087bf01185SAman Deep case HUB_CHAR_NO_LPSM:
15097bf01185SAman Deep case HUB_CHAR_LPSM:
15101da177e4SLinus Torvalds dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
15111da177e4SLinus Torvalds break;
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds
151474ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_OCPM) {
15157bf01185SAman Deep case HUB_CHAR_COMMON_OCPM:
15161da177e4SLinus Torvalds dev_dbg(hub_dev, "global over-current protection\n");
15171da177e4SLinus Torvalds break;
15187bf01185SAman Deep case HUB_CHAR_INDV_PORT_OCPM:
15191da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port over-current protection\n");
15201da177e4SLinus Torvalds break;
15217bf01185SAman Deep case HUB_CHAR_NO_OCPM:
15227bf01185SAman Deep case HUB_CHAR_OCPM:
15231da177e4SLinus Torvalds dev_dbg(hub_dev, "no over-current protection\n");
15241da177e4SLinus Torvalds break;
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds
15271da177e4SLinus Torvalds spin_lock_init(&hub->tt.lock);
15281da177e4SLinus Torvalds INIT_LIST_HEAD(&hub->tt.clear_list);
1529cb88a1b8SAlan Stern INIT_WORK(&hub->tt.clear_work, hub_tt_work);
15301da177e4SLinus Torvalds switch (hdev->descriptor.bDeviceProtocol) {
15317bf01185SAman Deep case USB_HUB_PR_FS:
15321da177e4SLinus Torvalds break;
15337bf01185SAman Deep case USB_HUB_PR_HS_SINGLE_TT:
15341da177e4SLinus Torvalds dev_dbg(hub_dev, "Single TT\n");
15351da177e4SLinus Torvalds hub->tt.hub = hdev;
15361da177e4SLinus Torvalds break;
15377bf01185SAman Deep case USB_HUB_PR_HS_MULTI_TT:
15381da177e4SLinus Torvalds ret = usb_set_interface(hdev, 0, 1);
15391da177e4SLinus Torvalds if (ret == 0) {
15401da177e4SLinus Torvalds dev_dbg(hub_dev, "TT per port\n");
15411da177e4SLinus Torvalds hub->tt.multi = 1;
15421da177e4SLinus Torvalds } else
15431da177e4SLinus Torvalds dev_err(hub_dev, "Using single TT (err %d)\n",
15441da177e4SLinus Torvalds ret);
15451da177e4SLinus Torvalds hub->tt.hub = hdev;
15461da177e4SLinus Torvalds break;
15477bf01185SAman Deep case USB_HUB_PR_SS:
1548d2e9b4d6SSarah Sharp /* USB 3.0 hubs don't have a TT */
1549d2e9b4d6SSarah Sharp break;
15501da177e4SLinus Torvalds default:
15511da177e4SLinus Torvalds dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
15521da177e4SLinus Torvalds hdev->descriptor.bDeviceProtocol);
15531da177e4SLinus Torvalds break;
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds
1556e09711aeSdavid-b@pacbell.net /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
155774ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1558e09711aeSdavid-b@pacbell.net case HUB_TTTT_8_BITS:
1559e09711aeSdavid-b@pacbell.net if (hdev->descriptor.bDeviceProtocol != 0) {
1560e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666;
1561e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d "
1562e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n",
1563e09711aeSdavid-b@pacbell.net 8, hub->tt.think_time);
1564e09711aeSdavid-b@pacbell.net }
15651da177e4SLinus Torvalds break;
1566e09711aeSdavid-b@pacbell.net case HUB_TTTT_16_BITS:
1567e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 2;
1568e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d "
1569e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n",
1570e09711aeSdavid-b@pacbell.net 16, hub->tt.think_time);
15711da177e4SLinus Torvalds break;
1572e09711aeSdavid-b@pacbell.net case HUB_TTTT_24_BITS:
1573e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 3;
1574e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d "
1575e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n",
1576e09711aeSdavid-b@pacbell.net 24, hub->tt.think_time);
15771da177e4SLinus Torvalds break;
1578e09711aeSdavid-b@pacbell.net case HUB_TTTT_32_BITS:
1579e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 4;
1580e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d "
1581e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n",
1582e09711aeSdavid-b@pacbell.net 32, hub->tt.think_time);
15831da177e4SLinus Torvalds break;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds
15861da177e4SLinus Torvalds /* probe() zeroes hub->indicator[] */
158774ad9bd2SGreg Kroah-Hartman if (wHubCharacteristics & HUB_CHAR_PORTIND) {
15881da177e4SLinus Torvalds hub->has_indicators = 1;
15891da177e4SLinus Torvalds dev_dbg(hub_dev, "Port indicators are supported\n");
15901da177e4SLinus Torvalds }
15911da177e4SLinus Torvalds
15921da177e4SLinus Torvalds dev_dbg(hub_dev, "power on to power good time: %dms\n",
15931da177e4SLinus Torvalds hub->descriptor->bPwrOn2PwrGood * 2);
15941da177e4SLinus Torvalds
15951da177e4SLinus Torvalds /* power budgeting mostly matters with bus-powered hubs,
15961da177e4SLinus Torvalds * and battery-powered root hubs (may provide just 8 mA).
15971da177e4SLinus Torvalds */
1598d9e1e148SFelipe Balbi ret = usb_get_std_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
159915b7336eSAlan Stern if (ret) {
16001da177e4SLinus Torvalds message = "can't get hub status";
16011da177e4SLinus Torvalds goto fail;
16021da177e4SLinus Torvalds }
1603430ee58eSSebastian Andrzej Siewior hcd = bus_to_hcd(hdev->bus);
16047d35b929SAlan Stern if (hdev == hdev->bus->root_hub) {
1605430ee58eSSebastian Andrzej Siewior if (hcd->power_budget > 0)
1606430ee58eSSebastian Andrzej Siewior hdev->bus_mA = hcd->power_budget;
1607430ee58eSSebastian Andrzej Siewior else
1608d8521afeSDan Williams hdev->bus_mA = full_load * maxchild;
1609430ee58eSSebastian Andrzej Siewior if (hdev->bus_mA >= full_load)
1610430ee58eSSebastian Andrzej Siewior hub->mA_per_port = full_load;
161155c52718SAlan Stern else {
161255c52718SAlan Stern hub->mA_per_port = hdev->bus_mA;
161355c52718SAlan Stern hub->limited_power = 1;
161455c52718SAlan Stern }
16157d35b929SAlan Stern } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
161655c52718SAlan Stern int remaining = hdev->bus_mA -
161755c52718SAlan Stern hub->descriptor->bHubContrCurrent;
16181da177e4SLinus Torvalds
1619430ee58eSSebastian Andrzej Siewior dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1620430ee58eSSebastian Andrzej Siewior hub->descriptor->bHubContrCurrent);
1621430ee58eSSebastian Andrzej Siewior hub->limited_power = 1;
1622430ee58eSSebastian Andrzej Siewior
1623d8521afeSDan Williams if (remaining < maxchild * unit_load)
162455c52718SAlan Stern dev_warn(hub_dev,
162555c52718SAlan Stern "insufficient power available "
162655c52718SAlan Stern "to use all downstream ports\n");
1627430ee58eSSebastian Andrzej Siewior hub->mA_per_port = unit_load; /* 7.2.1 */
1628430ee58eSSebastian Andrzej Siewior
162955c52718SAlan Stern } else { /* Self-powered external hub */
163055c52718SAlan Stern /* FIXME: What about battery-powered external hubs that
163155c52718SAlan Stern * provide less current per port? */
1632430ee58eSSebastian Andrzej Siewior hub->mA_per_port = full_load;
163355c52718SAlan Stern }
1634430ee58eSSebastian Andrzej Siewior if (hub->mA_per_port < full_load)
163555c52718SAlan Stern dev_dbg(hub_dev, "%umA bus power budget for each child\n",
163655c52718SAlan Stern hub->mA_per_port);
16371da177e4SLinus Torvalds
16381da177e4SLinus Torvalds ret = hub_hub_status(hub, &hubstatus, &hubchange);
16391da177e4SLinus Torvalds if (ret < 0) {
16401da177e4SLinus Torvalds message = "can't get hub status";
16411da177e4SLinus Torvalds goto fail;
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds
16441da177e4SLinus Torvalds /* local power status reports aren't always correct */
16451da177e4SLinus Torvalds if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
16461da177e4SLinus Torvalds dev_dbg(hub_dev, "local power source is %s\n",
16471da177e4SLinus Torvalds (hubstatus & HUB_STATUS_LOCAL_POWER)
16481da177e4SLinus Torvalds ? "lost (inactive)" : "good");
16491da177e4SLinus Torvalds
165074ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
16511da177e4SLinus Torvalds dev_dbg(hub_dev, "%sover-current condition exists\n",
16521da177e4SLinus Torvalds (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
16531da177e4SLinus Torvalds
165488fafff9Sinaky@linux.intel.com /* set up the interrupt endpoint
165588fafff9Sinaky@linux.intel.com * We use the EP's maxpacket size instead of (PORTS+1+7)/8
165688fafff9Sinaky@linux.intel.com * bytes as USB2.0[11.12.3] says because some hubs are known
165788fafff9Sinaky@linux.intel.com * to send more data (and thus cause overflow). For root hubs,
165888fafff9Sinaky@linux.intel.com * maxpktsize is defined in hcd.c's fake endpoint descriptors
165988fafff9Sinaky@linux.intel.com * to be big enough for at least USB_MAXCHILDREN ports. */
16601da177e4SLinus Torvalds pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1661dcd2e49bSVincent Mailhol maxp = usb_maxpacket(hdev, pipe);
16621da177e4SLinus Torvalds
16631da177e4SLinus Torvalds if (maxp > sizeof(*hub->buffer))
16641da177e4SLinus Torvalds maxp = sizeof(*hub->buffer);
16651da177e4SLinus Torvalds
16661da177e4SLinus Torvalds hub->urb = usb_alloc_urb(0, GFP_KERNEL);
16671da177e4SLinus Torvalds if (!hub->urb) {
16681da177e4SLinus Torvalds ret = -ENOMEM;
16691da177e4SLinus Torvalds goto fail;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds
16721da177e4SLinus Torvalds usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
16731da177e4SLinus Torvalds hub, endpoint->bInterval);
16741da177e4SLinus Torvalds
16751da177e4SLinus Torvalds /* maybe cycle the hub leds */
16761da177e4SLinus Torvalds if (hub->has_indicators && blinkenlights)
16771da177e4SLinus Torvalds hub->indicator[0] = INDICATOR_CYCLE;
16781da177e4SLinus Torvalds
1679d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex);
1680d8521afeSDan Williams for (i = 0; i < maxchild; i++) {
1681e58547ebSKrzysztof Mazur ret = usb_hub_create_port_device(hub, i + 1);
1682e58547ebSKrzysztof Mazur if (ret < 0) {
1683fa2a9566SLan Tianyu dev_err(hub->intfdev,
1684fa2a9566SLan Tianyu "couldn't create port%d device.\n", i + 1);
1685d8521afeSDan Williams break;
1686d8521afeSDan Williams }
1687d8521afeSDan Williams }
1688e58547ebSKrzysztof Mazur hdev->maxchild = i;
1689e3d10505SDan Williams for (i = 0; i < hdev->maxchild; i++) {
1690e3d10505SDan Williams struct usb_port *port_dev = hub->ports[i];
1691e3d10505SDan Williams
1692e3d10505SDan Williams pm_runtime_put(&port_dev->dev);
1693e3d10505SDan Williams }
1694e3d10505SDan Williams
1695d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex);
1696d8521afeSDan Williams if (ret < 0)
1697d8521afeSDan Williams goto fail;
1698fa2a9566SLan Tianyu
169937ebb549SPetr Mladek /* Update the HCD's internal representation of this hub before hub_wq
1700e3d95580SDan Williams * starts getting port status changes for devices under the hub.
1701e3d95580SDan Williams */
1702e3d95580SDan Williams if (hcd->driver->update_hub_device) {
1703e3d95580SDan Williams ret = hcd->driver->update_hub_device(hcd, hdev,
1704e3d95580SDan Williams &hub->tt, GFP_KERNEL);
1705e3d95580SDan Williams if (ret < 0) {
1706e3d95580SDan Williams message = "can't update HCD hub info";
1707e3d95580SDan Williams goto fail;
1708e3d95580SDan Williams }
1709e3d95580SDan Williams }
1710e3d95580SDan Williams
1711d2123fd9SLan Tianyu usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1712d2123fd9SLan Tianyu
1713f2835219SAlan Stern hub_activate(hub, HUB_INIT);
17141da177e4SLinus Torvalds return 0;
17151da177e4SLinus Torvalds
17161da177e4SLinus Torvalds fail:
17171da177e4SLinus Torvalds dev_err(hub_dev, "config failed, %s (err %d)\n",
17181da177e4SLinus Torvalds message, ret);
17191da177e4SLinus Torvalds /* hub_disconnect() frees urb and descriptor */
17201da177e4SLinus Torvalds return ret;
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds
hub_release(struct kref * kref)1723e8054854SAlan Stern static void hub_release(struct kref *kref)
1724e8054854SAlan Stern {
1725e8054854SAlan Stern struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1726e8054854SAlan Stern
17275d14f323SPetr Mladek usb_put_dev(hub->hdev);
1728e8054854SAlan Stern usb_put_intf(to_usb_interface(hub->intfdev));
1729e8054854SAlan Stern kfree(hub);
1730e8054854SAlan Stern }
1731e8054854SAlan Stern
hub_get(struct usb_hub * hub)17328dbc001bSAlan Stern void hub_get(struct usb_hub *hub)
17338dbc001bSAlan Stern {
17348dbc001bSAlan Stern kref_get(&hub->kref);
17358dbc001bSAlan Stern }
17368dbc001bSAlan Stern
hub_put(struct usb_hub * hub)17378dbc001bSAlan Stern void hub_put(struct usb_hub *hub)
17388dbc001bSAlan Stern {
17398dbc001bSAlan Stern kref_put(&hub->kref, hub_release);
17408dbc001bSAlan Stern }
17418dbc001bSAlan Stern
17421da177e4SLinus Torvalds static unsigned highspeed_hubs;
17431da177e4SLinus Torvalds
hub_disconnect(struct usb_interface * intf)17441da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
17451da177e4SLinus Torvalds {
17461da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf);
1747fa286188SGreg Kroah-Hartman struct usb_device *hdev = interface_to_usbdev(intf);
1748543d7784SAlan Stern int port1;
1749fa2a9566SLan Tianyu
175032a69589SPetr Mladek /*
175132a69589SPetr Mladek * Stop adding new hub events. We do not want to block here and thus
175232a69589SPetr Mladek * will not try to remove any pending work item.
175332a69589SPetr Mladek */
1754e8054854SAlan Stern hub->disconnected = 1;
17551da177e4SLinus Torvalds
17567de18d8bSAlan Stern /* Disconnect all children and quiesce the hub */
17577de18d8bSAlan Stern hub->error = 0;
17584330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT);
17597de18d8bSAlan Stern
1760d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex);
1761d8521afeSDan Williams
1762543d7784SAlan Stern /* Avoid races with recursively_mark_NOTATTACHED() */
1763543d7784SAlan Stern spin_lock_irq(&device_state_lock);
1764543d7784SAlan Stern port1 = hdev->maxchild;
1765543d7784SAlan Stern hdev->maxchild = 0;
17668b28c752SAlan Stern usb_set_intfdata(intf, NULL);
1767543d7784SAlan Stern spin_unlock_irq(&device_state_lock);
17681f2235b8SAlexander Shishkin
1769543d7784SAlan Stern for (; port1 > 0; --port1)
1770543d7784SAlan Stern usb_hub_remove_port_device(hub, port1);
17711da177e4SLinus Torvalds
1772d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex);
1773d8521afeSDan Williams
1774e8054854SAlan Stern if (hub->hdev->speed == USB_SPEED_HIGH)
17751da177e4SLinus Torvalds highspeed_hubs--;
17761da177e4SLinus Torvalds
17771da177e4SLinus Torvalds usb_free_urb(hub->urb);
1778fa2a9566SLan Tianyu kfree(hub->ports);
17791da177e4SLinus Torvalds kfree(hub->descriptor);
17801da177e4SLinus Torvalds kfree(hub->status);
1781d697cddaSAlan Stern kfree(hub->buffer);
17821da177e4SLinus Torvalds
1783971fcd49SLan Tianyu pm_suspend_ignore_children(&intf->dev, false);
17841208f9e1SHardik Gajjar
17851208f9e1SHardik Gajjar if (hub->quirk_disable_autosuspend)
17861208f9e1SHardik Gajjar usb_autopm_put_interface(intf);
17871208f9e1SHardik Gajjar
17883a6bf4a0SMatthias Kaehlcke onboard_hub_destroy_pdevs(&hub->onboard_hub_devs);
17893a6bf4a0SMatthias Kaehlcke
17908dbc001bSAlan Stern hub_put(hub);
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds
hub_descriptor_is_sane(struct usb_host_interface * desc)1793c94dc34fSEugene Korenevsky static bool hub_descriptor_is_sane(struct usb_host_interface *desc)
1794c94dc34fSEugene Korenevsky {
1795c94dc34fSEugene Korenevsky /* Some hubs have a subclass of 1, which AFAICT according to the */
1796c94dc34fSEugene Korenevsky /* specs is not defined, but it works */
1797c94dc34fSEugene Korenevsky if (desc->desc.bInterfaceSubClass != 0 &&
1798c94dc34fSEugene Korenevsky desc->desc.bInterfaceSubClass != 1)
1799c94dc34fSEugene Korenevsky return false;
1800c94dc34fSEugene Korenevsky
1801c94dc34fSEugene Korenevsky /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1802c94dc34fSEugene Korenevsky if (desc->desc.bNumEndpoints != 1)
1803c94dc34fSEugene Korenevsky return false;
1804c94dc34fSEugene Korenevsky
1805c94dc34fSEugene Korenevsky /* If the first endpoint is not interrupt IN, we'd better punt! */
1806c94dc34fSEugene Korenevsky if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc))
1807c94dc34fSEugene Korenevsky return false;
1808c94dc34fSEugene Korenevsky
1809c94dc34fSEugene Korenevsky return true;
1810c94dc34fSEugene Korenevsky }
1811c94dc34fSEugene Korenevsky
hub_probe(struct usb_interface * intf,const struct usb_device_id * id)18121da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
18131da177e4SLinus Torvalds {
18141da177e4SLinus Torvalds struct usb_host_interface *desc;
18151da177e4SLinus Torvalds struct usb_device *hdev;
18161da177e4SLinus Torvalds struct usb_hub *hub;
18171da177e4SLinus Torvalds
18181da177e4SLinus Torvalds desc = intf->cur_altsetting;
18191da177e4SLinus Torvalds hdev = interface_to_usbdev(intf);
18201da177e4SLinus Torvalds
1821596d789aSMing Lei /*
1822596d789aSMing Lei * Set default autosuspend delay as 0 to speedup bus suspend,
1823596d789aSMing Lei * based on the below considerations:
1824596d789aSMing Lei *
1825596d789aSMing Lei * - Unlike other drivers, the hub driver does not rely on the
1826596d789aSMing Lei * autosuspend delay to provide enough time to handle a wakeup
1827596d789aSMing Lei * event, and the submitted status URB is just to check future
1828596d789aSMing Lei * change on hub downstream ports, so it is safe to do it.
1829596d789aSMing Lei *
1830596d789aSMing Lei * - The patch might cause one or more auto supend/resume for
1831596d789aSMing Lei * below very rare devices when they are plugged into hub
1832596d789aSMing Lei * first time:
1833596d789aSMing Lei *
1834596d789aSMing Lei * devices having trouble initializing, and disconnect
1835596d789aSMing Lei * themselves from the bus and then reconnect a second
1836596d789aSMing Lei * or so later
1837596d789aSMing Lei *
1838596d789aSMing Lei * devices just for downloading firmware, and disconnects
1839596d789aSMing Lei * themselves after completing it
1840596d789aSMing Lei *
1841596d789aSMing Lei * For these quite rare devices, their drivers may change the
1842596d789aSMing Lei * autosuspend delay of their parent hub in the probe() to one
1843596d789aSMing Lei * appropriate value to avoid the subtle problem if someone
1844596d789aSMing Lei * does care it.
1845596d789aSMing Lei *
1846596d789aSMing Lei * - The patch may cause one or more auto suspend/resume on
1847596d789aSMing Lei * hub during running 'lsusb', but it is probably too
1848596d789aSMing Lei * infrequent to worry about.
1849596d789aSMing Lei *
1850596d789aSMing Lei * - Change autosuspend delay of hub can avoid unnecessary auto
1851596d789aSMing Lei * suspend timer for hub, also may decrease power consumption
1852596d789aSMing Lei * of USB bus.
1853bdd405d2SRoger Quadros *
1854bdd405d2SRoger Quadros * - If user has indicated to prevent autosuspend by passing
1855bdd405d2SRoger Quadros * usbcore.autosuspend = -1 then keep autosuspend disabled.
1856596d789aSMing Lei */
1857ceb6c9c8SRafael J. Wysocki #ifdef CONFIG_PM
1858bdd405d2SRoger Quadros if (hdev->dev.power.autosuspend_delay >= 0)
1859596d789aSMing Lei pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1860a9ef803dSGreg Kroah-Hartman #endif
1861596d789aSMing Lei
18628ef42dddSAlan Stern /*
18638ef42dddSAlan Stern * Hubs have proper suspend/resume support, except for root hubs
18648ef42dddSAlan Stern * where the controller driver doesn't have bus_suspend and
18658ef42dddSAlan Stern * bus_resume methods.
18668ef42dddSAlan Stern */
18678ef42dddSAlan Stern if (hdev->parent) { /* normal device */
1868088f7fecSAlan Stern usb_enable_autosuspend(hdev);
18698ef42dddSAlan Stern } else { /* root hub */
18708ef42dddSAlan Stern const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
18718ef42dddSAlan Stern
18728ef42dddSAlan Stern if (drv->bus_suspend && drv->bus_resume)
18738ef42dddSAlan Stern usb_enable_autosuspend(hdev);
18748ef42dddSAlan Stern }
1875088f7fecSAlan Stern
187638f3ad5eSFelipe Balbi if (hdev->level == MAX_TOPO_LEVEL) {
1877b9cef6c3SWu Fengguang dev_err(&intf->dev,
1878b9cef6c3SWu Fengguang "Unsupported bus topology: hub nested too deep\n");
187938f3ad5eSFelipe Balbi return -E2BIG;
188038f3ad5eSFelipe Balbi }
188138f3ad5eSFelipe Balbi
18829af54301SGreg Kroah-Hartman #ifdef CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB
188389ccbdc9SDavid Brownell if (hdev->parent) {
188489ccbdc9SDavid Brownell dev_warn(&intf->dev, "ignoring external hub\n");
188589ccbdc9SDavid Brownell return -ENODEV;
188689ccbdc9SDavid Brownell }
188789ccbdc9SDavid Brownell #endif
188889ccbdc9SDavid Brownell
1889c94dc34fSEugene Korenevsky if (!hub_descriptor_is_sane(desc)) {
18901da177e4SLinus Torvalds dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
18911da177e4SLinus Torvalds return -EIO;
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds
18941da177e4SLinus Torvalds /* We found a hub */
18951da177e4SLinus Torvalds dev_info(&intf->dev, "USB hub found\n");
18961da177e4SLinus Torvalds
18970a1ef3b5SAlan Stern hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1898b74e7062SWolfram Sang if (!hub)
18991da177e4SLinus Torvalds return -ENOMEM;
19001da177e4SLinus Torvalds
1901e8054854SAlan Stern kref_init(&hub->kref);
19021da177e4SLinus Torvalds hub->intfdev = &intf->dev;
19031da177e4SLinus Torvalds hub->hdev = hdev;
1904c4028958SDavid Howells INIT_DELAYED_WORK(&hub->leds, led_work);
19058520f380SAlan Stern INIT_DELAYED_WORK(&hub->init_work, NULL);
190632a69589SPetr Mladek INIT_WORK(&hub->events, hub_event);
19073a6bf4a0SMatthias Kaehlcke INIT_LIST_HEAD(&hub->onboard_hub_devs);
19088eb58994SNicolas Saenz Julienne spin_lock_init(&hub->irq_urb_lock);
19098eb58994SNicolas Saenz Julienne timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0);
1910e8054854SAlan Stern usb_get_intf(intf);
19115d14f323SPetr Mladek usb_get_dev(hdev);
19121da177e4SLinus Torvalds
19131da177e4SLinus Torvalds usb_set_intfdata(intf, hub);
191440f122f3SAlan Stern intf->needs_remote_wakeup = 1;
1915971fcd49SLan Tianyu pm_suspend_ignore_children(&intf->dev, true);
19161da177e4SLinus Torvalds
19171da177e4SLinus Torvalds if (hdev->speed == USB_SPEED_HIGH)
19181da177e4SLinus Torvalds highspeed_hubs++;
19191da177e4SLinus Torvalds
1920e6f30deaSMing Lei if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1921e6f30deaSMing Lei hub->quirk_check_port_auto_suspend = 1;
1922e6f30deaSMing Lei
19231208f9e1SHardik Gajjar if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) {
19241208f9e1SHardik Gajjar hub->quirk_disable_autosuspend = 1;
192563d6d7edSEugeniu Rosca usb_autopm_get_interface_no_resume(intf);
19261208f9e1SHardik Gajjar }
19271208f9e1SHardik Gajjar
1928915d900fSHardik Gajjar if ((id->driver_info & HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL) &&
1929915d900fSHardik Gajjar desc->endpoint[0].desc.bInterval > USB_REDUCE_FRAME_INTR_BINTERVAL) {
1930915d900fSHardik Gajjar desc->endpoint[0].desc.bInterval =
1931915d900fSHardik Gajjar USB_REDUCE_FRAME_INTR_BINTERVAL;
1932915d900fSHardik Gajjar /* Tell the HCD about the interrupt ep's new bInterval */
1933915d900fSHardik Gajjar usb_set_interface(hdev, 0, 0);
1934915d900fSHardik Gajjar }
1935915d900fSHardik Gajjar
19363a6bf4a0SMatthias Kaehlcke if (hub_configure(hub, &desc->endpoint[0].desc) >= 0) {
19373a6bf4a0SMatthias Kaehlcke onboard_hub_create_pdevs(hdev, &hub->onboard_hub_devs);
19383a6bf4a0SMatthias Kaehlcke
19391da177e4SLinus Torvalds return 0;
19403a6bf4a0SMatthias Kaehlcke }
19411da177e4SLinus Torvalds
19421da177e4SLinus Torvalds hub_disconnect(intf);
19431da177e4SLinus Torvalds return -ENODEV;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds
19461da177e4SLinus Torvalds static int
hub_ioctl(struct usb_interface * intf,unsigned int code,void * user_data)19471da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
19481da177e4SLinus Torvalds {
19491da177e4SLinus Torvalds struct usb_device *hdev = interface_to_usbdev(intf);
1950ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
19511da177e4SLinus Torvalds
19521da177e4SLinus Torvalds /* assert ifno == 0 (part of hub spec) */
19531da177e4SLinus Torvalds switch (code) {
19541da177e4SLinus Torvalds case USBDEVFS_HUB_PORTINFO: {
19551da177e4SLinus Torvalds struct usbdevfs_hub_portinfo *info = user_data;
19561da177e4SLinus Torvalds int i;
19571da177e4SLinus Torvalds
19581da177e4SLinus Torvalds spin_lock_irq(&device_state_lock);
19591da177e4SLinus Torvalds if (hdev->devnum <= 0)
19601da177e4SLinus Torvalds info->nports = 0;
19611da177e4SLinus Torvalds else {
19621da177e4SLinus Torvalds info->nports = hdev->maxchild;
19631da177e4SLinus Torvalds for (i = 0; i < info->nports; i++) {
1964ff823c79SLan Tianyu if (hub->ports[i]->child == NULL)
19651da177e4SLinus Torvalds info->port[i] = 0;
19661da177e4SLinus Torvalds else
19671da177e4SLinus Torvalds info->port[i] =
1968ff823c79SLan Tianyu hub->ports[i]->child->devnum;
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock);
19721da177e4SLinus Torvalds
19731da177e4SLinus Torvalds return info->nports + 1;
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds
19761da177e4SLinus Torvalds default:
19771da177e4SLinus Torvalds return -ENOSYS;
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds }
19801da177e4SLinus Torvalds
19817cbe5dcaSAlan Stern /*
19827cbe5dcaSAlan Stern * Allow user programs to claim ports on a hub. When a device is attached
19837cbe5dcaSAlan Stern * to one of these "claimed" ports, the program will "own" the device.
19847cbe5dcaSAlan Stern */
find_port_owner(struct usb_device * hdev,unsigned port1,struct usb_dev_state *** ppowner)19857cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
19869b6f0c4bSValentina Manea struct usb_dev_state ***ppowner)
19877cbe5dcaSAlan Stern {
198841341261SMathias Nyman struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
198941341261SMathias Nyman
19907cbe5dcaSAlan Stern if (hdev->state == USB_STATE_NOTATTACHED)
19917cbe5dcaSAlan Stern return -ENODEV;
19927cbe5dcaSAlan Stern if (port1 == 0 || port1 > hdev->maxchild)
19937cbe5dcaSAlan Stern return -EINVAL;
19947cbe5dcaSAlan Stern
199541341261SMathias Nyman /* Devices not managed by the hub driver
19967cbe5dcaSAlan Stern * will always have maxchild equal to 0.
19977cbe5dcaSAlan Stern */
199841341261SMathias Nyman *ppowner = &(hub->ports[port1 - 1]->port_owner);
19997cbe5dcaSAlan Stern return 0;
20007cbe5dcaSAlan Stern }
20017cbe5dcaSAlan Stern
20027cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
usb_hub_claim_port(struct usb_device * hdev,unsigned port1,struct usb_dev_state * owner)2003336c5c31SLan Tianyu int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
20049b6f0c4bSValentina Manea struct usb_dev_state *owner)
20057cbe5dcaSAlan Stern {
20067cbe5dcaSAlan Stern int rc;
20079b6f0c4bSValentina Manea struct usb_dev_state **powner;
20087cbe5dcaSAlan Stern
20097cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner);
20107cbe5dcaSAlan Stern if (rc)
20117cbe5dcaSAlan Stern return rc;
20127cbe5dcaSAlan Stern if (*powner)
20137cbe5dcaSAlan Stern return -EBUSY;
20147cbe5dcaSAlan Stern *powner = owner;
20157cbe5dcaSAlan Stern return rc;
20167cbe5dcaSAlan Stern }
20176080cd0eSValentina Manea EXPORT_SYMBOL_GPL(usb_hub_claim_port);
20187cbe5dcaSAlan Stern
usb_hub_release_port(struct usb_device * hdev,unsigned port1,struct usb_dev_state * owner)2019336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
20209b6f0c4bSValentina Manea struct usb_dev_state *owner)
20217cbe5dcaSAlan Stern {
20227cbe5dcaSAlan Stern int rc;
20239b6f0c4bSValentina Manea struct usb_dev_state **powner;
20247cbe5dcaSAlan Stern
20257cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner);
20267cbe5dcaSAlan Stern if (rc)
20277cbe5dcaSAlan Stern return rc;
20287cbe5dcaSAlan Stern if (*powner != owner)
20297cbe5dcaSAlan Stern return -ENOENT;
20307cbe5dcaSAlan Stern *powner = NULL;
20317cbe5dcaSAlan Stern return rc;
20327cbe5dcaSAlan Stern }
20336080cd0eSValentina Manea EXPORT_SYMBOL_GPL(usb_hub_release_port);
20347cbe5dcaSAlan Stern
usb_hub_release_all_ports(struct usb_device * hdev,struct usb_dev_state * owner)20359b6f0c4bSValentina Manea void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
20367cbe5dcaSAlan Stern {
2037ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
20387cbe5dcaSAlan Stern int n;
20397cbe5dcaSAlan Stern
2040fa2a9566SLan Tianyu for (n = 0; n < hdev->maxchild; n++) {
2041fa2a9566SLan Tianyu if (hub->ports[n]->port_owner == owner)
2042fa2a9566SLan Tianyu hub->ports[n]->port_owner = NULL;
20437cbe5dcaSAlan Stern }
2044fa2a9566SLan Tianyu
20457cbe5dcaSAlan Stern }
20467cbe5dcaSAlan Stern
20477cbe5dcaSAlan Stern /* The caller must hold udev's lock */
usb_device_is_owned(struct usb_device * udev)20487cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
20497cbe5dcaSAlan Stern {
20507cbe5dcaSAlan Stern struct usb_hub *hub;
20517cbe5dcaSAlan Stern
20527cbe5dcaSAlan Stern if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
20537cbe5dcaSAlan Stern return false;
2054ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent);
2055fa2a9566SLan Tianyu return !!hub->ports[udev->portnum - 1]->port_owner;
20567cbe5dcaSAlan Stern }
20577cbe5dcaSAlan Stern
update_port_device_state(struct usb_device * udev)205883cb2604SRoy Luo static void update_port_device_state(struct usb_device *udev)
205983cb2604SRoy Luo {
206083cb2604SRoy Luo struct usb_hub *hub;
206183cb2604SRoy Luo struct usb_port *port_dev;
206283cb2604SRoy Luo
206383cb2604SRoy Luo if (udev->parent) {
206483cb2604SRoy Luo hub = usb_hub_to_struct_hub(udev->parent);
2065ed85777cSUdipto Goswami
2066ed85777cSUdipto Goswami /*
2067ed85777cSUdipto Goswami * The Link Layer Validation System Driver (lvstest)
2068ed85777cSUdipto Goswami * has a test step to unbind the hub before running the
2069ed85777cSUdipto Goswami * rest of the procedure. This triggers hub_disconnect
2070ed85777cSUdipto Goswami * which will set the hub's maxchild to 0, further
2071ed85777cSUdipto Goswami * resulting in usb_hub_to_struct_hub returning NULL.
2072ed85777cSUdipto Goswami */
2073ed85777cSUdipto Goswami if (hub) {
207483cb2604SRoy Luo port_dev = hub->ports[udev->portnum - 1];
207583cb2604SRoy Luo WRITE_ONCE(port_dev->state, udev->state);
207683cb2604SRoy Luo sysfs_notify_dirent(port_dev->state_kn);
207783cb2604SRoy Luo }
207883cb2604SRoy Luo }
2079ed85777cSUdipto Goswami }
208083cb2604SRoy Luo
recursively_mark_NOTATTACHED(struct usb_device * udev)20811da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
20821da177e4SLinus Torvalds {
2083ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev);
20841da177e4SLinus Torvalds int i;
20851da177e4SLinus Torvalds
20861da177e4SLinus Torvalds for (i = 0; i < udev->maxchild; ++i) {
2087ff823c79SLan Tianyu if (hub->ports[i]->child)
2088ff823c79SLan Tianyu recursively_mark_NOTATTACHED(hub->ports[i]->child);
20891da177e4SLinus Torvalds }
20909bbdf1e0SAlan Stern if (udev->state == USB_STATE_SUSPENDED)
209115123006SSarah Sharp udev->active_duration -= jiffies;
20921da177e4SLinus Torvalds udev->state = USB_STATE_NOTATTACHED;
209383cb2604SRoy Luo update_port_device_state(udev);
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds
20961da177e4SLinus Torvalds /**
20971da177e4SLinus Torvalds * usb_set_device_state - change a device's current state (usbcore, hcds)
20981da177e4SLinus Torvalds * @udev: pointer to device whose state should be changed
20991da177e4SLinus Torvalds * @new_state: new state value to be stored
21001da177e4SLinus Torvalds *
21011da177e4SLinus Torvalds * udev->state is _not_ fully protected by the device lock. Although
21021da177e4SLinus Torvalds * most transitions are made only while holding the lock, the state can
21031da177e4SLinus Torvalds * can change to USB_STATE_NOTATTACHED at almost any time. This
21041da177e4SLinus Torvalds * is so that devices can be marked as disconnected as soon as possible,
21051da177e4SLinus Torvalds * without having to wait for any semaphores to be released. As a result,
21061da177e4SLinus Torvalds * all changes to any device's state must be protected by the
21071da177e4SLinus Torvalds * device_state_lock spinlock.
21081da177e4SLinus Torvalds *
21091da177e4SLinus Torvalds * Once a device has been added to the device tree, all changes to its state
21101da177e4SLinus Torvalds * should be made using this routine. The state should _not_ be set directly.
21111da177e4SLinus Torvalds *
21121da177e4SLinus Torvalds * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
21131da177e4SLinus Torvalds * Otherwise udev->state is set to new_state, and if new_state is
21141da177e4SLinus Torvalds * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
21151da177e4SLinus Torvalds * to USB_STATE_NOTATTACHED.
21161da177e4SLinus Torvalds */
usb_set_device_state(struct usb_device * udev,enum usb_device_state new_state)21171da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
21181da177e4SLinus Torvalds enum usb_device_state new_state)
21191da177e4SLinus Torvalds {
21201da177e4SLinus Torvalds unsigned long flags;
21214681b171SRafael J. Wysocki int wakeup = -1;
21221da177e4SLinus Torvalds
21231da177e4SLinus Torvalds spin_lock_irqsave(&device_state_lock, flags);
21241da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED)
21251da177e4SLinus Torvalds ; /* do nothing */
2126b94dc6b5SDavid Brownell else if (new_state != USB_STATE_NOTATTACHED) {
2127fb669cc0SDavid Brownell
2128fb669cc0SDavid Brownell /* root hub wakeup capabilities are managed out-of-band
2129fb669cc0SDavid Brownell * and may involve silicon errata ... ignore them here.
2130fb669cc0SDavid Brownell */
2131fb669cc0SDavid Brownell if (udev->parent) {
2132645daaabSAlan Stern if (udev->state == USB_STATE_SUSPENDED
2133645daaabSAlan Stern || new_state == USB_STATE_SUSPENDED)
2134645daaabSAlan Stern ; /* No change to wakeup settings */
2135645daaabSAlan Stern else if (new_state == USB_STATE_CONFIGURED)
2136ddbe1fcaSLu Baolu wakeup = (udev->quirks &
2137ddbe1fcaSLu Baolu USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
2138ddbe1fcaSLu Baolu udev->actconfig->desc.bmAttributes &
2139ddbe1fcaSLu Baolu USB_CONFIG_ATT_WAKEUP;
2140645daaabSAlan Stern else
21414681b171SRafael J. Wysocki wakeup = 0;
2142fb669cc0SDavid Brownell }
214315123006SSarah Sharp if (udev->state == USB_STATE_SUSPENDED &&
214415123006SSarah Sharp new_state != USB_STATE_SUSPENDED)
214515123006SSarah Sharp udev->active_duration -= jiffies;
214615123006SSarah Sharp else if (new_state == USB_STATE_SUSPENDED &&
214715123006SSarah Sharp udev->state != USB_STATE_SUSPENDED)
214815123006SSarah Sharp udev->active_duration += jiffies;
2149645daaabSAlan Stern udev->state = new_state;
215083cb2604SRoy Luo update_port_device_state(udev);
2151b94dc6b5SDavid Brownell } else
21521da177e4SLinus Torvalds recursively_mark_NOTATTACHED(udev);
21531da177e4SLinus Torvalds spin_unlock_irqrestore(&device_state_lock, flags);
21544681b171SRafael J. Wysocki if (wakeup >= 0)
21554681b171SRafael J. Wysocki device_set_wakeup_capable(&udev->dev, wakeup);
21561da177e4SLinus Torvalds }
21576da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
21581da177e4SLinus Torvalds
21598af548dcSInaky Perez-Gonzalez /*
21603b29b68bSAlan Stern * Choose a device number.
21613b29b68bSAlan Stern *
21623b29b68bSAlan Stern * Device numbers are used as filenames in usbfs. On USB-1.1 and
21633b29b68bSAlan Stern * USB-2.0 buses they are also used as device addresses, however on
21643b29b68bSAlan Stern * USB-3.0 buses the address is assigned by the controller hardware
21653b29b68bSAlan Stern * and it usually is not the same as the device number.
21663b29b68bSAlan Stern *
2167c6515272SSarah Sharp * Devices connected under xHCI are not as simple. The host controller
2168c6515272SSarah Sharp * supports virtualization, so the hardware assigns device addresses and
2169c6515272SSarah Sharp * the HCD must setup data structures before issuing a set address
2170c6515272SSarah Sharp * command to the hardware.
21718af548dcSInaky Perez-Gonzalez */
choose_devnum(struct usb_device * udev)21723b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds int devnum;
21751da177e4SLinus Torvalds struct usb_bus *bus = udev->bus;
21761da177e4SLinus Torvalds
2177638139ebSPetr Mladek /* be safe when more hub events are proceed in parallel */
2178feb26ac3SChris Bainbridge mutex_lock(&bus->devnum_next_mutex);
21791e4c5742SAlan Stern
21801e4c5742SAlan Stern /* Try to allocate the next devnum beginning at bus->devnum_next. */
21811da177e4SLinus Torvalds devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
21821da177e4SLinus Torvalds bus->devnum_next);
21831da177e4SLinus Torvalds if (devnum >= 128)
21841e4c5742SAlan Stern devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1);
21851da177e4SLinus Torvalds bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
21861da177e4SLinus Torvalds if (devnum < 128) {
21871da177e4SLinus Torvalds set_bit(devnum, bus->devmap.devicemap);
21881da177e4SLinus Torvalds udev->devnum = devnum;
21891da177e4SLinus Torvalds }
2190feb26ac3SChris Bainbridge mutex_unlock(&bus->devnum_next_mutex);
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds
release_devnum(struct usb_device * udev)21933b29b68bSAlan Stern static void release_devnum(struct usb_device *udev)
21941da177e4SLinus Torvalds {
21951da177e4SLinus Torvalds if (udev->devnum > 0) {
21961da177e4SLinus Torvalds clear_bit(udev->devnum, udev->bus->devmap.devicemap);
21971da177e4SLinus Torvalds udev->devnum = -1;
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds
update_devnum(struct usb_device * udev,int devnum)22013b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum)
22024953d141SDavid Vrabel {
22034953d141SDavid Vrabel udev->devnum = devnum;
22044998f1efSJim Lin if (!udev->devaddr)
22054998f1efSJim Lin udev->devaddr = (u8)devnum;
22064953d141SDavid Vrabel }
22074953d141SDavid Vrabel
hub_free_dev(struct usb_device * udev)2208f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev)
2209f7410cedSHerbert Xu {
2210f7410cedSHerbert Xu struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2211f7410cedSHerbert Xu
2212f7410cedSHerbert Xu /* Root hubs aren't real devices, so don't free HCD resources */
2213f7410cedSHerbert Xu if (hcd->driver->free_dev && udev->parent)
2214f7410cedSHerbert Xu hcd->driver->free_dev(hcd, udev);
2215f7410cedSHerbert Xu }
2216f7410cedSHerbert Xu
hub_disconnect_children(struct usb_device * udev)22177027df36SDan Williams static void hub_disconnect_children(struct usb_device *udev)
22187027df36SDan Williams {
22197027df36SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(udev);
22207027df36SDan Williams int i;
22217027df36SDan Williams
22227027df36SDan Williams /* Free up all the children before we remove this device */
22237027df36SDan Williams for (i = 0; i < udev->maxchild; i++) {
22247027df36SDan Williams if (hub->ports[i]->child)
22257027df36SDan Williams usb_disconnect(&hub->ports[i]->child);
22267027df36SDan Williams }
22277027df36SDan Williams }
22287027df36SDan Williams
22291da177e4SLinus Torvalds /**
22301da177e4SLinus Torvalds * usb_disconnect - disconnect a device (usbcore-internal)
22311da177e4SLinus Torvalds * @pdev: pointer to device being disconnected
223241631d36SAhmed S. Darwish *
223341631d36SAhmed S. Darwish * Context: task context, might sleep
22341da177e4SLinus Torvalds *
22351da177e4SLinus Torvalds * Something got disconnected. Get rid of it and all of its children.
22361da177e4SLinus Torvalds *
22371da177e4SLinus Torvalds * If *pdev is a normal device then the parent hub must already be locked.
2238a4b5d606SHeiner Kallweit * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
2239db8f2aa3SBjorn Helgaas * which protects the set of root hubs as well as the list of buses.
22401da177e4SLinus Torvalds *
22411da177e4SLinus Torvalds * Only hub drivers (including virtual root hub drivers for host
22421da177e4SLinus Torvalds * controllers) should ever call this.
22431da177e4SLinus Torvalds *
22441da177e4SLinus Torvalds * This call is synchronous, and may not be used in an interrupt context.
22451da177e4SLinus Torvalds */
usb_disconnect(struct usb_device ** pdev)22461da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
22471da177e4SLinus Torvalds {
22487027df36SDan Williams struct usb_port *port_dev = NULL;
22491da177e4SLinus Torvalds struct usb_device *udev = *pdev;
22505b1dc209SPeter Chen struct usb_hub *hub = NULL;
22515b1dc209SPeter Chen int port1 = 1;
22521da177e4SLinus Torvalds
22531da177e4SLinus Torvalds /* mark the device as inactive, so any further urb submissions for
22541da177e4SLinus Torvalds * this device (and any of its children) will fail immediately.
225525985edcSLucas De Marchi * this quiesces everything except pending urbs.
22561da177e4SLinus Torvalds */
22571da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED);
22583b29b68bSAlan Stern dev_info(&udev->dev, "USB disconnect, device number %d\n",
22593b29b68bSAlan Stern udev->devnum);
22601da177e4SLinus Torvalds
2261f5cccf49SGuenter Roeck /*
2262f5cccf49SGuenter Roeck * Ensure that the pm runtime code knows that the USB device
2263f5cccf49SGuenter Roeck * is in the process of being disconnected.
2264f5cccf49SGuenter Roeck */
2265f5cccf49SGuenter Roeck pm_runtime_barrier(&udev->dev);
2266f5cccf49SGuenter Roeck
22679ad3d6ccSAlan Stern usb_lock_device(udev);
22689ad3d6ccSAlan Stern
22697027df36SDan Williams hub_disconnect_children(udev);
22701da177e4SLinus Torvalds
22711da177e4SLinus Torvalds /* deallocate hcd/hardware state ... nuking all pending urbs and
22721da177e4SLinus Torvalds * cleaning up all state associated with the current configuration
22731da177e4SLinus Torvalds * so that the hardware is now fully quiesced.
22741da177e4SLinus Torvalds */
2275782da727SAlan Stern dev_dbg(&udev->dev, "unregistering device\n");
22761da177e4SLinus Torvalds usb_disable_device(udev, 0);
2277cde217a5SAlan Stern usb_hcd_synchronize_unlinks(udev);
22781da177e4SLinus Torvalds
2279fde26380SLan Tianyu if (udev->parent) {
22807027df36SDan Williams port1 = udev->portnum;
22817027df36SDan Williams hub = usb_hub_to_struct_hub(udev->parent);
22827027df36SDan Williams port_dev = hub->ports[port1 - 1];
2283fde26380SLan Tianyu
2284fde26380SLan Tianyu sysfs_remove_link(&udev->dev.kobj, "port");
2285fde26380SLan Tianyu sysfs_remove_link(&port_dev->dev.kobj, "device");
2286971fcd49SLan Tianyu
22877027df36SDan Williams /*
22887027df36SDan Williams * As usb_port_runtime_resume() de-references udev, make
22897027df36SDan Williams * sure no resumes occur during removal
22907027df36SDan Williams */
22917027df36SDan Williams if (!test_and_set_bit(port1, hub->child_usage_bits))
22927027df36SDan Williams pm_runtime_get_sync(&port_dev->dev);
2293fde26380SLan Tianyu }
2294fde26380SLan Tianyu
22953b23dd6fSAlan Stern usb_remove_ep_devs(&udev->ep0);
2296782da727SAlan Stern usb_unlock_device(udev);
22973099e75aSGreg Kroah-Hartman
2298782da727SAlan Stern /* Unregister the device. The device driver is responsible
22993b23dd6fSAlan Stern * for de-configuring the device and invoking the remove-device
23003b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others).
2301782da727SAlan Stern */
2302782da727SAlan Stern device_del(&udev->dev);
2303782da727SAlan Stern
2304782da727SAlan Stern /* Free the device number and delete the parent's children[]
23051da177e4SLinus Torvalds * (or root_hub) pointer.
23061da177e4SLinus Torvalds */
23073b29b68bSAlan Stern release_devnum(udev);
23081da177e4SLinus Torvalds
23091da177e4SLinus Torvalds /* Avoid races with recursively_mark_NOTATTACHED() */
23101da177e4SLinus Torvalds spin_lock_irq(&device_state_lock);
23111da177e4SLinus Torvalds *pdev = NULL;
23121da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock);
23131da177e4SLinus Torvalds
23147027df36SDan Williams if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
23157027df36SDan Williams pm_runtime_put(&port_dev->dev);
23167027df36SDan Williams
2317f7410cedSHerbert Xu hub_free_dev(udev);
2318f7410cedSHerbert Xu
2319782da727SAlan Stern put_device(&udev->dev);
23201da177e4SLinus Torvalds }
23211da177e4SLinus Torvalds
2322f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
show_string(struct usb_device * udev,char * id,char * string)23231da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
23241da177e4SLinus Torvalds {
23251da177e4SLinus Torvalds if (!string)
23261da177e4SLinus Torvalds return;
2327f2ec522eSJoe Perches dev_info(&udev->dev, "%s: %s\n", id, string);
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds
announce_device(struct usb_device * udev)2330f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
2331f2a383e4SGreg Kroah-Hartman {
233273c6d3b2SBenson Leung u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
233373c6d3b2SBenson Leung
233473c6d3b2SBenson Leung dev_info(&udev->dev,
233573c6d3b2SBenson Leung "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n",
2336f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idVendor),
233773c6d3b2SBenson Leung le16_to_cpu(udev->descriptor.idProduct),
233873c6d3b2SBenson Leung bcdDevice >> 8, bcdDevice & 0xff);
2339b9cef6c3SWu Fengguang dev_info(&udev->dev,
2340b9cef6c3SWu Fengguang "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2341f2a383e4SGreg Kroah-Hartman udev->descriptor.iManufacturer,
2342f2a383e4SGreg Kroah-Hartman udev->descriptor.iProduct,
2343f2a383e4SGreg Kroah-Hartman udev->descriptor.iSerialNumber);
2344f2a383e4SGreg Kroah-Hartman show_string(udev, "Product", udev->product);
2345f2a383e4SGreg Kroah-Hartman show_string(udev, "Manufacturer", udev->manufacturer);
2346f2a383e4SGreg Kroah-Hartman show_string(udev, "SerialNumber", udev->serial);
2347f2a383e4SGreg Kroah-Hartman }
23481da177e4SLinus Torvalds #else
announce_device(struct usb_device * udev)2349f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
23501da177e4SLinus Torvalds #endif
23511da177e4SLinus Torvalds
23521da177e4SLinus Torvalds
23533ede760fSOliver Neukum /**
23548d8558d1SAlan Stern * usb_enumerate_device_otg - FIXME (usbcore-internal)
23553ede760fSOliver Neukum * @udev: newly addressed device (in ADDRESS state)
23563ede760fSOliver Neukum *
23578d8558d1SAlan Stern * Finish enumeration for On-The-Go devices
2358626f090cSYacine Belkadi *
2359626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise.
23603ede760fSOliver Neukum */
usb_enumerate_device_otg(struct usb_device * udev)23618d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
23621da177e4SLinus Torvalds {
2363d9d16e8aSInaky Perez-Gonzalez int err = 0;
23641da177e4SLinus Torvalds
23651da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG
23661da177e4SLinus Torvalds /*
23671da177e4SLinus Torvalds * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
23681da177e4SLinus Torvalds * to wake us after we've powered off VBUS; and HNP, switching roles
23691da177e4SLinus Torvalds * "host" to "peripheral". The OTG descriptor helps figure this out.
23701da177e4SLinus Torvalds */
23711da177e4SLinus Torvalds if (!udev->bus->is_b_host
23721da177e4SLinus Torvalds && udev->config
23731da177e4SLinus Torvalds && udev->parent == udev->bus->root_hub) {
23742eb5052eSFelipe Balbi struct usb_otg_descriptor *desc = NULL;
23751da177e4SLinus Torvalds struct usb_bus *bus = udev->bus;
237612c3da34SAlan Stern unsigned port1 = udev->portnum;
23771da177e4SLinus Torvalds
23787d2d641cSLi Jun /* descriptor may appear anywhere in config */
23797d2d641cSLi Jun err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
23807d2d641cSLi Jun le16_to_cpu(udev->config[0].desc.wTotalLength),
2381704620afSMathias Payer USB_DT_OTG, (void **) &desc, sizeof(*desc));
23827d2d641cSLi Jun if (err || !(desc->bmAttributes & USB_OTG_HNP))
23837d2d641cSLi Jun return 0;
23847d2d641cSLi Jun
23857d2d641cSLi Jun dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
23867d2d641cSLi Jun (port1 == bus->otg_port) ? "" : "non-");
23871da177e4SLinus Torvalds
23881da177e4SLinus Torvalds /* enable HNP before suspend, it's simpler */
23897d2d641cSLi Jun if (port1 == bus->otg_port) {
23901da177e4SLinus Torvalds bus->b_hnp_enable = 1;
23911afe33a7SOliver Neukum err = usb_control_msg(udev,
23921afe33a7SOliver Neukum usb_sndctrlpipe(udev, 0),
23931afe33a7SOliver Neukum USB_REQ_SET_FEATURE, 0,
23941afe33a7SOliver Neukum USB_DEVICE_B_HNP_ENABLE,
23951afe33a7SOliver Neukum 0, NULL, 0,
23961afe33a7SOliver Neukum USB_CTRL_SET_TIMEOUT);
23971afe33a7SOliver Neukum if (err < 0) {
23987d2d641cSLi Jun /*
23997d2d641cSLi Jun * OTG MESSAGE: report errors here,
24001da177e4SLinus Torvalds * customize to match your product.
24011da177e4SLinus Torvalds */
24027d2d641cSLi Jun dev_err(&udev->dev, "can't set HNP mode: %d\n",
24031da177e4SLinus Torvalds err);
24041da177e4SLinus Torvalds bus->b_hnp_enable = 0;
24051da177e4SLinus Torvalds }
24067d2d641cSLi Jun } else if (desc->bLength == sizeof
24077d2d641cSLi Jun (struct usb_otg_descriptor)) {
2408eafeda9eSOliver Neukum /*
2409eafeda9eSOliver Neukum * We are operating on a legacy OTP device
2410eafeda9eSOliver Neukum * These should be told that they are operating
2411eafeda9eSOliver Neukum * on the wrong port if we have another port that does
2412eafeda9eSOliver Neukum * support HNP
2413eafeda9eSOliver Neukum */
2414eafeda9eSOliver Neukum if (bus->otg_port != 0) {
24157d2d641cSLi Jun /* Set a_alt_hnp_support for legacy otg device */
24161afe33a7SOliver Neukum err = usb_control_msg(udev,
24171afe33a7SOliver Neukum usb_sndctrlpipe(udev, 0),
24181afe33a7SOliver Neukum USB_REQ_SET_FEATURE, 0,
24191afe33a7SOliver Neukum USB_DEVICE_A_ALT_HNP_SUPPORT,
24201afe33a7SOliver Neukum 0, NULL, 0,
24211afe33a7SOliver Neukum USB_CTRL_SET_TIMEOUT);
24221afe33a7SOliver Neukum if (err < 0)
24237d2d641cSLi Jun dev_err(&udev->dev,
24247d2d641cSLi Jun "set a_alt_hnp_support failed: %d\n",
24257d2d641cSLi Jun err);
24261da177e4SLinus Torvalds }
24271da177e4SLinus Torvalds }
2428eafeda9eSOliver Neukum }
24291da177e4SLinus Torvalds #endif
2430d9d16e8aSInaky Perez-Gonzalez return err;
2431d9d16e8aSInaky Perez-Gonzalez }
24321da177e4SLinus Torvalds
2433d9d16e8aSInaky Perez-Gonzalez
2434d9d16e8aSInaky Perez-Gonzalez /**
24358d8558d1SAlan Stern * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2436d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state)
2437d9d16e8aSInaky Perez-Gonzalez *
243845bf39f8SAlan Stern * This is only called by usb_new_device() -- all comments that apply there
243945bf39f8SAlan Stern * apply here wrt to environment.
2440d9d16e8aSInaky Perez-Gonzalez *
2441d9d16e8aSInaky Perez-Gonzalez * If the device is WUSB and not authorized, we don't attempt to read
2442d9d16e8aSInaky Perez-Gonzalez * the string descriptors, as they will be errored out by the device
2443d9d16e8aSInaky Perez-Gonzalez * until it has been authorized.
2444626f090cSYacine Belkadi *
2445626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise.
2446d9d16e8aSInaky Perez-Gonzalez */
usb_enumerate_device(struct usb_device * udev)24478d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
2448d9d16e8aSInaky Perez-Gonzalez {
2449d9d16e8aSInaky Perez-Gonzalez int err;
2450026f3fcbSPeter Chen struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2451d9d16e8aSInaky Perez-Gonzalez
2452d9d16e8aSInaky Perez-Gonzalez if (udev->config == NULL) {
2453d9d16e8aSInaky Perez-Gonzalez err = usb_get_configuration(udev);
2454d9d16e8aSInaky Perez-Gonzalez if (err < 0) {
2455e9e88fb7SAlan Stern if (err != -ENODEV)
2456d9d16e8aSInaky Perez-Gonzalez dev_err(&udev->dev, "can't read configurations, error %d\n",
2457d9d16e8aSInaky Perez-Gonzalez err);
245880da2e0dSLaurent Pinchart return err;
2459d9d16e8aSInaky Perez-Gonzalez }
2460d9d16e8aSInaky Perez-Gonzalez }
246183e83ecbSThomas Pugliese
2462d9d16e8aSInaky Perez-Gonzalez /* read the standard strings and cache them if present */
2463d9d16e8aSInaky Perez-Gonzalez udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2464d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = usb_cache_string(udev,
2465d9d16e8aSInaky Perez-Gonzalez udev->descriptor.iManufacturer);
2466d9d16e8aSInaky Perez-Gonzalez udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
246783e83ecbSThomas Pugliese
24688d8558d1SAlan Stern err = usb_enumerate_device_otg(udev);
246980da2e0dSLaurent Pinchart if (err < 0)
2470d9d16e8aSInaky Perez-Gonzalez return err;
247180da2e0dSLaurent Pinchart
2472f8f02d5cSGreg Kroah-Hartman if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support &&
2473e5a9d621SPeter Chen !is_targeted(udev)) {
2474026f3fcbSPeter Chen /* Maybe it can talk to us, though we can't talk to it.
2475026f3fcbSPeter Chen * (Includes HNP test device.)
2476026f3fcbSPeter Chen */
2477e5a9d621SPeter Chen if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2478e5a9d621SPeter Chen || udev->bus->is_b_host)) {
2479026f3fcbSPeter Chen err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2480026f3fcbSPeter Chen if (err < 0)
2481026f3fcbSPeter Chen dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2482026f3fcbSPeter Chen }
2483026f3fcbSPeter Chen return -ENOTSUPP;
2484026f3fcbSPeter Chen }
2485026f3fcbSPeter Chen
248680da2e0dSLaurent Pinchart usb_detect_interface_quirks(udev);
248780da2e0dSLaurent Pinchart
248880da2e0dSLaurent Pinchart return 0;
2489d9d16e8aSInaky Perez-Gonzalez }
2490d9d16e8aSInaky Perez-Gonzalez
set_usb_port_removable(struct usb_device * udev)2491d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev)
2492d35e70d5SMatthew Garrett {
2493d35e70d5SMatthew Garrett struct usb_device *hdev = udev->parent;
2494d35e70d5SMatthew Garrett struct usb_hub *hub;
2495d35e70d5SMatthew Garrett u8 port = udev->portnum;
2496d35e70d5SMatthew Garrett u16 wHubCharacteristics;
2497d35e70d5SMatthew Garrett bool removable = true;
2498d35e70d5SMatthew Garrett
249970f400d4SRajat Jain dev_set_removable(&udev->dev, DEVICE_REMOVABLE_UNKNOWN);
250070f400d4SRajat Jain
2501d35e70d5SMatthew Garrett if (!hdev)
2502d35e70d5SMatthew Garrett return;
2503d35e70d5SMatthew Garrett
2504ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent);
2505d35e70d5SMatthew Garrett
250692bfbf71SMatthew Garrett /*
250792bfbf71SMatthew Garrett * If the platform firmware has provided information about a port,
250892bfbf71SMatthew Garrett * use that to determine whether it's removable.
250992bfbf71SMatthew Garrett */
251092bfbf71SMatthew Garrett switch (hub->ports[udev->portnum - 1]->connect_type) {
251192bfbf71SMatthew Garrett case USB_PORT_CONNECT_TYPE_HOT_PLUG:
251270f400d4SRajat Jain dev_set_removable(&udev->dev, DEVICE_REMOVABLE);
251392bfbf71SMatthew Garrett return;
251492bfbf71SMatthew Garrett case USB_PORT_CONNECT_TYPE_HARD_WIRED:
251558339c2eSMatthew Garrett case USB_PORT_NOT_USED:
251670f400d4SRajat Jain dev_set_removable(&udev->dev, DEVICE_FIXED);
251792bfbf71SMatthew Garrett return;
251858339c2eSMatthew Garrett default:
251958339c2eSMatthew Garrett break;
252092bfbf71SMatthew Garrett }
252192bfbf71SMatthew Garrett
252292bfbf71SMatthew Garrett /*
252392bfbf71SMatthew Garrett * Otherwise, check whether the hub knows whether a port is removable
252492bfbf71SMatthew Garrett * or not
252592bfbf71SMatthew Garrett */
2526d35e70d5SMatthew Garrett wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2527d35e70d5SMatthew Garrett
2528d35e70d5SMatthew Garrett if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2529d35e70d5SMatthew Garrett return;
2530d35e70d5SMatthew Garrett
2531d35e70d5SMatthew Garrett if (hub_is_superspeed(hdev)) {
2532ca3c1539SLan Tianyu if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2533ca3c1539SLan Tianyu & (1 << port))
2534d35e70d5SMatthew Garrett removable = false;
2535d35e70d5SMatthew Garrett } else {
2536d35e70d5SMatthew Garrett if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2537d35e70d5SMatthew Garrett removable = false;
2538d35e70d5SMatthew Garrett }
2539d35e70d5SMatthew Garrett
2540d35e70d5SMatthew Garrett if (removable)
254170f400d4SRajat Jain dev_set_removable(&udev->dev, DEVICE_REMOVABLE);
2542d35e70d5SMatthew Garrett else
254370f400d4SRajat Jain dev_set_removable(&udev->dev, DEVICE_FIXED);
2544a4204ff0SDan Williams
2545d35e70d5SMatthew Garrett }
2546d9d16e8aSInaky Perez-Gonzalez
2547d9d16e8aSInaky Perez-Gonzalez /**
2548d9d16e8aSInaky Perez-Gonzalez * usb_new_device - perform initial device setup (usbcore-internal)
2549d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state)
2550d9d16e8aSInaky Perez-Gonzalez *
25518d8558d1SAlan Stern * This is called with devices which have been detected but not fully
25528d8558d1SAlan Stern * enumerated. The device descriptor is available, but not descriptors
2553d9d16e8aSInaky Perez-Gonzalez * for any device configuration. The caller must have locked either
2554d9d16e8aSInaky Perez-Gonzalez * the parent hub (if udev is a normal device) or else the
2555a4b5d606SHeiner Kallweit * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
2556d9d16e8aSInaky Perez-Gonzalez * udev has already been installed, but udev is not yet visible through
2557d9d16e8aSInaky Perez-Gonzalez * sysfs or other filesystem code.
2558d9d16e8aSInaky Perez-Gonzalez *
2559d9d16e8aSInaky Perez-Gonzalez * This call is synchronous, and may not be used in an interrupt context.
2560d9d16e8aSInaky Perez-Gonzalez *
2561d9d16e8aSInaky Perez-Gonzalez * Only the hub driver or root-hub registrar should ever call this.
2562626f090cSYacine Belkadi *
2563626f090cSYacine Belkadi * Return: Whether the device is configured properly or not. Zero if the
2564626f090cSYacine Belkadi * interface was registered with the driver core; else a negative errno
2565626f090cSYacine Belkadi * value.
2566626f090cSYacine Belkadi *
2567d9d16e8aSInaky Perez-Gonzalez */
usb_new_device(struct usb_device * udev)2568d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
2569d9d16e8aSInaky Perez-Gonzalez {
2570d9d16e8aSInaky Perez-Gonzalez int err;
2571d9d16e8aSInaky Perez-Gonzalez
257216985408SDan Streetman if (udev->parent) {
257316985408SDan Streetman /* Initialize non-root-hub device wakeup to disabled;
257416985408SDan Streetman * device (un)configuration controls wakeup capable
257516985408SDan Streetman * sysfs power/wakeup controls wakeup enabled/disabled
257616985408SDan Streetman */
257716985408SDan Streetman device_init_wakeup(&udev->dev, 0);
257816985408SDan Streetman }
257916985408SDan Streetman
25809bbdf1e0SAlan Stern /* Tell the runtime-PM framework the device is active */
25819bbdf1e0SAlan Stern pm_runtime_set_active(&udev->dev);
2582c08512c7SAlan Stern pm_runtime_get_noresume(&udev->dev);
2583fcc4a01eSAlan Stern pm_runtime_use_autosuspend(&udev->dev);
25849bbdf1e0SAlan Stern pm_runtime_enable(&udev->dev);
25859bbdf1e0SAlan Stern
2586c08512c7SAlan Stern /* By default, forbid autosuspend for all devices. It will be
2587c08512c7SAlan Stern * allowed for hubs during binding.
2588c08512c7SAlan Stern */
2589c08512c7SAlan Stern usb_disable_autosuspend(udev);
2590c08512c7SAlan Stern
25918d8558d1SAlan Stern err = usb_enumerate_device(udev); /* Read descriptors */
2592d9d16e8aSInaky Perez-Gonzalez if (err < 0)
2593d9d16e8aSInaky Perez-Gonzalez goto fail;
2594c6515272SSarah Sharp dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2595c6515272SSarah Sharp udev->devnum, udev->bus->busnum,
2596c6515272SSarah Sharp (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
25979f8b17e6SKay Sievers /* export the usbdev device-node for libusb */
25989f8b17e6SKay Sievers udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
25999f8b17e6SKay Sievers (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
26009f8b17e6SKay Sievers
26016cd13201SAlan Stern /* Tell the world! */
26026cd13201SAlan Stern announce_device(udev);
2603195af2ccSAlan Stern
2604b04b3156STheodore Ts'o if (udev->serial)
2605b04b3156STheodore Ts'o add_device_randomness(udev->serial, strlen(udev->serial));
2606b04b3156STheodore Ts'o if (udev->product)
2607b04b3156STheodore Ts'o add_device_randomness(udev->product, strlen(udev->product));
2608b04b3156STheodore Ts'o if (udev->manufacturer)
2609b04b3156STheodore Ts'o add_device_randomness(udev->manufacturer,
2610b04b3156STheodore Ts'o strlen(udev->manufacturer));
2611b04b3156STheodore Ts'o
2612927bc916SRafael J. Wysocki device_enable_async_suspend(&udev->dev);
2613d35e70d5SMatthew Garrett
2614a4204ff0SDan Williams /* check whether the hub or firmware marks this port as non-removable */
2615d35e70d5SMatthew Garrett set_usb_port_removable(udev);
2616d35e70d5SMatthew Garrett
2617782da727SAlan Stern /* Register the device. The device driver is responsible
26183b23dd6fSAlan Stern * for configuring the device and invoking the add-device
26193b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others).
2620782da727SAlan Stern */
26211da177e4SLinus Torvalds err = device_add(&udev->dev);
26221da177e4SLinus Torvalds if (err) {
26231da177e4SLinus Torvalds dev_err(&udev->dev, "can't device_add, error %d\n", err);
26241da177e4SLinus Torvalds goto fail;
26251da177e4SLinus Torvalds }
26269ad3d6ccSAlan Stern
2627fde26380SLan Tianyu /* Create link files between child device and usb port device. */
2628fde26380SLan Tianyu if (udev->parent) {
2629ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2630d5c3834eSDan Williams int port1 = udev->portnum;
2631d5c3834eSDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
2632fde26380SLan Tianyu
2633fde26380SLan Tianyu err = sysfs_create_link(&udev->dev.kobj,
2634fde26380SLan Tianyu &port_dev->dev.kobj, "port");
2635fde26380SLan Tianyu if (err)
2636fde26380SLan Tianyu goto fail;
2637fde26380SLan Tianyu
2638fde26380SLan Tianyu err = sysfs_create_link(&port_dev->dev.kobj,
2639fde26380SLan Tianyu &udev->dev.kobj, "device");
2640fde26380SLan Tianyu if (err) {
2641fde26380SLan Tianyu sysfs_remove_link(&udev->dev.kobj, "port");
2642fde26380SLan Tianyu goto fail;
2643fde26380SLan Tianyu }
2644971fcd49SLan Tianyu
2645d5c3834eSDan Williams if (!test_and_set_bit(port1, hub->child_usage_bits))
2646971fcd49SLan Tianyu pm_runtime_get_sync(&port_dev->dev);
2647fde26380SLan Tianyu }
2648fde26380SLan Tianyu
26493b23dd6fSAlan Stern (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2650c08512c7SAlan Stern usb_mark_last_busy(udev);
2651c08512c7SAlan Stern pm_runtime_put_sync_autosuspend(&udev->dev);
2652c066475eSGreg Kroah-Hartman return err;
26531da177e4SLinus Torvalds
26541da177e4SLinus Torvalds fail:
26551da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED);
26569bbdf1e0SAlan Stern pm_runtime_disable(&udev->dev);
26579bbdf1e0SAlan Stern pm_runtime_set_suspended(&udev->dev);
2658d9d16e8aSInaky Perez-Gonzalez return err;
26591da177e4SLinus Torvalds }
26601da177e4SLinus Torvalds
266193993a0aSInaky Perez-Gonzalez
266293993a0aSInaky Perez-Gonzalez /**
2663fd39c86bSRandy Dunlap * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2664fd39c86bSRandy Dunlap * @usb_dev: USB device
2665fd39c86bSRandy Dunlap *
2666fd39c86bSRandy Dunlap * Move the USB device to a very basic state where interfaces are disabled
2667fd39c86bSRandy Dunlap * and the device is in fact unconfigured and unusable.
266893993a0aSInaky Perez-Gonzalez *
266993993a0aSInaky Perez-Gonzalez * We share a lock (that we have) with device_del(), so we need to
267093993a0aSInaky Perez-Gonzalez * defer its call.
2671626f090cSYacine Belkadi *
2672626f090cSYacine Belkadi * Return: 0.
267393993a0aSInaky Perez-Gonzalez */
usb_deauthorize_device(struct usb_device * usb_dev)267493993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
267593993a0aSInaky Perez-Gonzalez {
267693993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev);
267793993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 0)
267893993a0aSInaky Perez-Gonzalez goto out_unauthorized;
2679da307123SAlan Stern
268093993a0aSInaky Perez-Gonzalez usb_dev->authorized = 0;
268193993a0aSInaky Perez-Gonzalez usb_set_configuration(usb_dev, -1);
2682da307123SAlan Stern
268393993a0aSInaky Perez-Gonzalez out_unauthorized:
268493993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev);
268593993a0aSInaky Perez-Gonzalez return 0;
268693993a0aSInaky Perez-Gonzalez }
268793993a0aSInaky Perez-Gonzalez
268893993a0aSInaky Perez-Gonzalez
usb_authorize_device(struct usb_device * usb_dev)268993993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
269093993a0aSInaky Perez-Gonzalez {
269193993a0aSInaky Perez-Gonzalez int result = 0, c;
2692da307123SAlan Stern
269393993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev);
269493993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 1)
269593993a0aSInaky Perez-Gonzalez goto out_authorized;
2696da307123SAlan Stern
269793993a0aSInaky Perez-Gonzalez result = usb_autoresume_device(usb_dev);
269893993a0aSInaky Perez-Gonzalez if (result < 0) {
269993993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev,
270093993a0aSInaky Perez-Gonzalez "can't autoresume for authorization: %d\n", result);
270193993a0aSInaky Perez-Gonzalez goto error_autoresume;
270293993a0aSInaky Perez-Gonzalez }
2703e50a322eSJosef Gajdusek
270493993a0aSInaky Perez-Gonzalez usb_dev->authorized = 1;
270593993a0aSInaky Perez-Gonzalez /* Choose and set the configuration. This registers the interfaces
270693993a0aSInaky Perez-Gonzalez * with the driver core and lets interface drivers bind to them.
270793993a0aSInaky Perez-Gonzalez */
2708b5ea060fSGreg Kroah-Hartman c = usb_choose_configuration(usb_dev);
270993993a0aSInaky Perez-Gonzalez if (c >= 0) {
271093993a0aSInaky Perez-Gonzalez result = usb_set_configuration(usb_dev, c);
271193993a0aSInaky Perez-Gonzalez if (result) {
271293993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev,
271393993a0aSInaky Perez-Gonzalez "can't set config #%d, error %d\n", c, result);
271493993a0aSInaky Perez-Gonzalez /* This need not be fatal. The user can try to
271593993a0aSInaky Perez-Gonzalez * set other configurations. */
271693993a0aSInaky Perez-Gonzalez }
271793993a0aSInaky Perez-Gonzalez }
271893993a0aSInaky Perez-Gonzalez dev_info(&usb_dev->dev, "authorized to connect\n");
2719da307123SAlan Stern
2720da307123SAlan Stern usb_autosuspend_device(usb_dev);
272193993a0aSInaky Perez-Gonzalez error_autoresume:
272293993a0aSInaky Perez-Gonzalez out_authorized:
2723781b2137SMatthias Beyer usb_unlock_device(usb_dev); /* complements locktree */
272493993a0aSInaky Perez-Gonzalez return result;
272593993a0aSInaky Perez-Gonzalez }
272693993a0aSInaky Perez-Gonzalez
27270299809bSThinh Nguyen /**
27280299809bSThinh Nguyen * get_port_ssp_rate - Match the extended port status to SSP rate
27290299809bSThinh Nguyen * @hdev: The hub device
27300299809bSThinh Nguyen * @ext_portstatus: extended port status
27310299809bSThinh Nguyen *
27320299809bSThinh Nguyen * Match the extended port status speed id to the SuperSpeed Plus sublink speed
27330299809bSThinh Nguyen * capability attributes. Base on the number of connected lanes and speed,
27340299809bSThinh Nguyen * return the corresponding enum usb_ssp_rate.
27350299809bSThinh Nguyen */
get_port_ssp_rate(struct usb_device * hdev,u32 ext_portstatus)27360299809bSThinh Nguyen static enum usb_ssp_rate get_port_ssp_rate(struct usb_device *hdev,
27370299809bSThinh Nguyen u32 ext_portstatus)
27380299809bSThinh Nguyen {
2739f74a7afcSRicardo Cañuelo struct usb_ssp_cap_descriptor *ssp_cap;
27400299809bSThinh Nguyen u32 attr;
27410299809bSThinh Nguyen u8 speed_id;
27420299809bSThinh Nguyen u8 ssac;
27430299809bSThinh Nguyen u8 lanes;
27440299809bSThinh Nguyen int i;
27450299809bSThinh Nguyen
2746f74a7afcSRicardo Cañuelo if (!hdev->bos)
2747f74a7afcSRicardo Cañuelo goto out;
2748f74a7afcSRicardo Cañuelo
2749f74a7afcSRicardo Cañuelo ssp_cap = hdev->bos->ssp_cap;
27500299809bSThinh Nguyen if (!ssp_cap)
27510299809bSThinh Nguyen goto out;
27520299809bSThinh Nguyen
27530299809bSThinh Nguyen speed_id = ext_portstatus & USB_EXT_PORT_STAT_RX_SPEED_ID;
27540299809bSThinh Nguyen lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1;
27550299809bSThinh Nguyen
27560299809bSThinh Nguyen ssac = le32_to_cpu(ssp_cap->bmAttributes) &
27570299809bSThinh Nguyen USB_SSP_SUBLINK_SPEED_ATTRIBS;
27580299809bSThinh Nguyen
27590299809bSThinh Nguyen for (i = 0; i <= ssac; i++) {
27600299809bSThinh Nguyen u8 ssid;
27610299809bSThinh Nguyen
27620299809bSThinh Nguyen attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
27630299809bSThinh Nguyen ssid = FIELD_GET(USB_SSP_SUBLINK_SPEED_SSID, attr);
27640299809bSThinh Nguyen if (speed_id == ssid) {
27650299809bSThinh Nguyen u16 mantissa;
27660299809bSThinh Nguyen u8 lse;
27670299809bSThinh Nguyen u8 type;
27680299809bSThinh Nguyen
27690299809bSThinh Nguyen /*
27700299809bSThinh Nguyen * Note: currently asymmetric lane types are only
27710299809bSThinh Nguyen * applicable for SSIC operate in SuperSpeed protocol
27720299809bSThinh Nguyen */
27730299809bSThinh Nguyen type = FIELD_GET(USB_SSP_SUBLINK_SPEED_ST, attr);
27740299809bSThinh Nguyen if (type == USB_SSP_SUBLINK_SPEED_ST_ASYM_RX ||
27750299809bSThinh Nguyen type == USB_SSP_SUBLINK_SPEED_ST_ASYM_TX)
27760299809bSThinh Nguyen goto out;
27770299809bSThinh Nguyen
27780299809bSThinh Nguyen if (FIELD_GET(USB_SSP_SUBLINK_SPEED_LP, attr) !=
27790299809bSThinh Nguyen USB_SSP_SUBLINK_SPEED_LP_SSP)
27800299809bSThinh Nguyen goto out;
27810299809bSThinh Nguyen
27820299809bSThinh Nguyen lse = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSE, attr);
27830299809bSThinh Nguyen mantissa = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSM, attr);
27840299809bSThinh Nguyen
27850299809bSThinh Nguyen /* Convert to Gbps */
27860299809bSThinh Nguyen for (; lse < USB_SSP_SUBLINK_SPEED_LSE_GBPS; lse++)
27870299809bSThinh Nguyen mantissa /= 1000;
27880299809bSThinh Nguyen
27890299809bSThinh Nguyen if (mantissa >= 10 && lanes == 1)
27900299809bSThinh Nguyen return USB_SSP_GEN_2x1;
27910299809bSThinh Nguyen
27920299809bSThinh Nguyen if (mantissa >= 10 && lanes == 2)
27930299809bSThinh Nguyen return USB_SSP_GEN_2x2;
27940299809bSThinh Nguyen
27950299809bSThinh Nguyen if (mantissa >= 5 && lanes == 2)
27960299809bSThinh Nguyen return USB_SSP_GEN_1x2;
27970299809bSThinh Nguyen
27980299809bSThinh Nguyen goto out;
27990299809bSThinh Nguyen }
28000299809bSThinh Nguyen }
28010299809bSThinh Nguyen
28020299809bSThinh Nguyen out:
28030299809bSThinh Nguyen return USB_SSP_GEN_UNKNOWN;
28040299809bSThinh Nguyen }
28050299809bSThinh Nguyen
2806fb6f076dSAlan Stern #ifdef CONFIG_USB_FEW_INIT_RETRIES
2807fb6f076dSAlan Stern #define PORT_RESET_TRIES 2
2808fb6f076dSAlan Stern #define SET_ADDRESS_TRIES 1
2809fb6f076dSAlan Stern #define GET_DESCRIPTOR_TRIES 1
2810fb6f076dSAlan Stern #define GET_MAXPACKET0_TRIES 1
2811fb6f076dSAlan Stern #define PORT_INIT_TRIES 4
2812fb6f076dSAlan Stern
2813fb6f076dSAlan Stern #else
28141da177e4SLinus Torvalds #define PORT_RESET_TRIES 5
28151da177e4SLinus Torvalds #define SET_ADDRESS_TRIES 2
28161da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES 2
2817fb6f076dSAlan Stern #define GET_MAXPACKET0_TRIES 3
281819502e69SAlan Stern #define PORT_INIT_TRIES 4
2819fb6f076dSAlan Stern #endif /* CONFIG_USB_FEW_INIT_RETRIES */
28201da177e4SLinus Torvalds
2821f59f93cdSMathias Nyman #define DETECT_DISCONNECT_TRIES 5
2822f59f93cdSMathias Nyman
282374072baeSMathias Nyman #define HUB_ROOT_RESET_TIME 60 /* times are in msec */
28241da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME 10
282575d7cf72SAndiry Xu #define HUB_BH_RESET_TIME 50
28261da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME 200
282777c7f072SSarah Sharp #define HUB_RESET_TIMEOUT 800
28281da177e4SLinus Torvalds
use_new_scheme(struct usb_device * udev,int retry,struct usb_port * port_dev)282919502e69SAlan Stern static bool use_new_scheme(struct usb_device *udev, int retry,
283019502e69SAlan Stern struct usb_port *port_dev)
283119502e69SAlan Stern {
283219502e69SAlan Stern int old_scheme_first_port =
283319502e69SAlan Stern (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) ||
283419502e69SAlan Stern old_scheme_first;
283519502e69SAlan Stern
283648fc7dbdSDan Williams /*
283748fc7dbdSDan Williams * "New scheme" enumeration causes an extra state transition to be
283848fc7dbdSDan Williams * exposed to an xhci host and causes USB3 devices to receive control
283948fc7dbdSDan Williams * commands in the default state. This has been seen to cause
284048fc7dbdSDan Williams * enumeration failures, so disable this enumeration scheme for USB3
284148fc7dbdSDan Williams * devices.
284248fc7dbdSDan Williams */
28438a1b2725SMathias Nyman if (udev->speed >= USB_SPEED_SUPER)
284448fc7dbdSDan Williams return false;
284548fc7dbdSDan Williams
284619502e69SAlan Stern /*
284719502e69SAlan Stern * If use_both_schemes is set, use the first scheme (whichever
284819502e69SAlan Stern * it is) for the larger half of the retries, then use the other
284919502e69SAlan Stern * scheme. Otherwise, use the first scheme for all the retries.
285019502e69SAlan Stern */
285119502e69SAlan Stern if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2)
285219502e69SAlan Stern return old_scheme_first_port; /* Second half */
285319502e69SAlan Stern return !old_scheme_first_port; /* First half or all */
285448fc7dbdSDan Williams }
285548fc7dbdSDan Williams
2856025d4430SRahul Bedarkar /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
2857d46a6024SHarry Pan * Port warm reset is required to recover
28588bea2bd3SStanislaw Ledwon */
hub_port_warm_reset_required(struct usb_hub * hub,int port1,u16 portstatus)28593cd12f91SDan Williams static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
28603cd12f91SDan Williams u16 portstatus)
286110d674a8SSarah Sharp {
28623cd12f91SDan Williams u16 link_state;
28633cd12f91SDan Williams
28643cd12f91SDan Williams if (!hub_is_superspeed(hub->hdev))
28653cd12f91SDan Williams return false;
28663cd12f91SDan Williams
28673cd12f91SDan Williams if (test_bit(port1, hub->warm_reset_bits))
28683cd12f91SDan Williams return true;
28693cd12f91SDan Williams
28703cd12f91SDan Williams link_state = portstatus & USB_PORT_STAT_LINK_STATE;
28713cd12f91SDan Williams return link_state == USB_SS_PORT_LS_SS_INACTIVE
28723cd12f91SDan Williams || link_state == USB_SS_PORT_LS_COMP_MOD;
287310d674a8SSarah Sharp }
287410d674a8SSarah Sharp
hub_port_wait_reset(struct usb_hub * hub,int port1,struct usb_device * udev,unsigned int delay,bool warm)28751da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
287675d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm)
28771da177e4SLinus Torvalds {
28781da177e4SLinus Torvalds int delay_time, ret;
28791da177e4SLinus Torvalds u16 portstatus;
28801da177e4SLinus Torvalds u16 portchange;
28810cdd49a1SMathias Nyman u32 ext_portstatus = 0;
28821da177e4SLinus Torvalds
28831da177e4SLinus Torvalds for (delay_time = 0;
28841da177e4SLinus Torvalds delay_time < HUB_RESET_TIMEOUT;
28851da177e4SLinus Torvalds delay_time += delay) {
28861da177e4SLinus Torvalds /* wait to give the device a chance to reset */
28871da177e4SLinus Torvalds msleep(delay);
28881da177e4SLinus Torvalds
28891da177e4SLinus Torvalds /* read and decode port status */
28900cdd49a1SMathias Nyman if (hub_is_superspeedplus(hub->hdev))
28910cdd49a1SMathias Nyman ret = hub_ext_port_status(hub, port1,
28920cdd49a1SMathias Nyman HUB_EXT_PORT_STATUS,
28930cdd49a1SMathias Nyman &portstatus, &portchange,
28940cdd49a1SMathias Nyman &ext_portstatus);
28950cdd49a1SMathias Nyman else
2896f061f43dSMichael Grzeschik ret = usb_hub_port_status(hub, port1, &portstatus,
28970cdd49a1SMathias Nyman &portchange);
28981da177e4SLinus Torvalds if (ret < 0)
28991da177e4SLinus Torvalds return ret;
29001da177e4SLinus Torvalds
290122547c4cSGuenter Roeck /*
290222547c4cSGuenter Roeck * The port state is unknown until the reset completes.
290322547c4cSGuenter Roeck *
290422547c4cSGuenter Roeck * On top of that, some chips may require additional time
290522547c4cSGuenter Roeck * to re-establish a connection after the reset is complete,
290622547c4cSGuenter Roeck * so also wait for the connection to be re-established.
290722547c4cSGuenter Roeck */
290822547c4cSGuenter Roeck if (!(portstatus & USB_PORT_STAT_RESET) &&
290922547c4cSGuenter Roeck (portstatus & USB_PORT_STAT_CONNECTION))
2910470f0be8SSarah Sharp break;
2911470f0be8SSarah Sharp
2912470f0be8SSarah Sharp /* switch to the long delay after two short delay failures */
2913470f0be8SSarah Sharp if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2914470f0be8SSarah Sharp delay = HUB_LONG_RESET_TIME;
2915470f0be8SSarah Sharp
2916d99f6b41SDan Williams dev_dbg(&hub->ports[port1 - 1]->dev,
2917d99f6b41SDan Williams "not %sreset yet, waiting %dms\n",
2918d99f6b41SDan Williams warm ? "warm " : "", delay);
2919470f0be8SSarah Sharp }
2920470f0be8SSarah Sharp
29214f43447eSSarah Sharp if ((portstatus & USB_PORT_STAT_RESET))
2922470f0be8SSarah Sharp return -EBUSY;
29234f43447eSSarah Sharp
29243cd12f91SDan Williams if (hub_port_warm_reset_required(hub, port1, portstatus))
2925a24a6078SSarah Sharp return -ENOTCONN;
292610d674a8SSarah Sharp
29271da177e4SLinus Torvalds /* Device went away? */
29281da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_CONNECTION))
29291da177e4SLinus Torvalds return -ENOTCONN;
29301da177e4SLinus Torvalds
29311ac7db63SMathias Nyman /* Retry if connect change is set but status is still connected.
29321ac7db63SMathias Nyman * A USB 3.0 connection may bounce if multiple warm resets were issued,
2933a24a6078SSarah Sharp * but the device may have successfully re-connected. Ignore it.
2934a24a6078SSarah Sharp */
2935a24a6078SSarah Sharp if (!hub_is_superspeed(hub->hdev) &&
29361ac7db63SMathias Nyman (portchange & USB_PORT_STAT_C_CONNECTION)) {
29371ac7db63SMathias Nyman usb_clear_port_feature(hub->hdev, port1,
29381ac7db63SMathias Nyman USB_PORT_FEAT_C_CONNECTION);
29391ac7db63SMathias Nyman return -EAGAIN;
29401ac7db63SMathias Nyman }
29411da177e4SLinus Torvalds
2942470f0be8SSarah Sharp if (!(portstatus & USB_PORT_STAT_ENABLE))
2943470f0be8SSarah Sharp return -EBUSY;
2944470f0be8SSarah Sharp
29452d4fa940SSarah Sharp if (!udev)
29462d4fa940SSarah Sharp return 0;
29472d4fa940SSarah Sharp
2948013eedb8SMathias Nyman if (hub_is_superspeedplus(hub->hdev)) {
2949013eedb8SMathias Nyman /* extended portstatus Rx and Tx lane count are zero based */
2950013eedb8SMathias Nyman udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1;
2951013eedb8SMathias Nyman udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1;
29520299809bSThinh Nguyen udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus);
2953013eedb8SMathias Nyman } else {
2954013eedb8SMathias Nyman udev->rx_lanes = 1;
2955013eedb8SMathias Nyman udev->tx_lanes = 1;
29560299809bSThinh Nguyen udev->ssp_rate = USB_SSP_GEN_UNKNOWN;
2957013eedb8SMathias Nyman }
29581e4c5742SAlan Stern if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN)
29590cdd49a1SMathias Nyman udev->speed = USB_SPEED_SUPER_PLUS;
2960131dec34SSarah Sharp else if (hub_is_superspeed(hub->hdev))
2961809cd1cbSSarah Sharp udev->speed = USB_SPEED_SUPER;
29620165de09SInaky Perez-Gonzalez else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
29631da177e4SLinus Torvalds udev->speed = USB_SPEED_HIGH;
29641da177e4SLinus Torvalds else if (portstatus & USB_PORT_STAT_LOW_SPEED)
29651da177e4SLinus Torvalds udev->speed = USB_SPEED_LOW;
29661da177e4SLinus Torvalds else
29671da177e4SLinus Torvalds udev->speed = USB_SPEED_FULL;
29681da177e4SLinus Torvalds return 0;
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds
297175d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
hub_port_reset(struct usb_hub * hub,int port1,struct usb_device * udev,unsigned int delay,bool warm)297275d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1,
297375d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm)
297475d7cf72SAndiry Xu {
297575d7cf72SAndiry Xu int i, status;
2976a24a6078SSarah Sharp u16 portchange, portstatus;
2977d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
2978781f0766SKai-Heng Feng int reset_recovery_time;
297975d7cf72SAndiry Xu
298075d7cf72SAndiry Xu if (!hub_is_superspeed(hub->hdev)) {
29810fe51aa5SSarah Sharp if (warm) {
298275d7cf72SAndiry Xu dev_err(hub->intfdev, "only USB3 hub support "
298375d7cf72SAndiry Xu "warm reset\n");
298475d7cf72SAndiry Xu return -EINVAL;
298575d7cf72SAndiry Xu }
29860fe51aa5SSarah Sharp /* Block EHCI CF initialization during the port reset.
29870fe51aa5SSarah Sharp * Some companion controllers don't like it when they mix.
29880fe51aa5SSarah Sharp */
29890fe51aa5SSarah Sharp down_read(&ehci_cf_port_reset_rwsem);
2990d3b9d7a9SSarah Sharp } else if (!warm) {
2991d3b9d7a9SSarah Sharp /*
2992d3b9d7a9SSarah Sharp * If the caller hasn't explicitly requested a warm reset,
2993d3b9d7a9SSarah Sharp * double check and see if one is needed.
2994d3b9d7a9SSarah Sharp */
2995f061f43dSMichael Grzeschik if (usb_hub_port_status(hub, port1, &portstatus,
2996f061f43dSMichael Grzeschik &portchange) == 0)
2997fb6d1f7dSRobert Schlabbach if (hub_port_warm_reset_required(hub, port1,
2998fb6d1f7dSRobert Schlabbach portstatus))
2999d3b9d7a9SSarah Sharp warm = true;
300075d7cf72SAndiry Xu }
30013cd12f91SDan Williams clear_bit(port1, hub->warm_reset_bits);
300275d7cf72SAndiry Xu
300375d7cf72SAndiry Xu /* Reset the port */
300475d7cf72SAndiry Xu for (i = 0; i < PORT_RESET_TRIES; i++) {
300575d7cf72SAndiry Xu status = set_port_feature(hub->hdev, port1, (warm ?
300675d7cf72SAndiry Xu USB_PORT_FEAT_BH_PORT_RESET :
300775d7cf72SAndiry Xu USB_PORT_FEAT_RESET));
3008e9e88fb7SAlan Stern if (status == -ENODEV) {
3009e9e88fb7SAlan Stern ; /* The hub is gone */
3010e9e88fb7SAlan Stern } else if (status) {
3011d99f6b41SDan Williams dev_err(&port_dev->dev,
3012d99f6b41SDan Williams "cannot %sreset (err = %d)\n",
3013d99f6b41SDan Williams warm ? "warm " : "", status);
301475d7cf72SAndiry Xu } else {
301575d7cf72SAndiry Xu status = hub_port_wait_reset(hub, port1, udev, delay,
301675d7cf72SAndiry Xu warm);
3017e9e88fb7SAlan Stern if (status && status != -ENOTCONN && status != -ENODEV)
301875d7cf72SAndiry Xu dev_dbg(hub->intfdev,
301975d7cf72SAndiry Xu "port_wait_reset: err = %d\n",
302075d7cf72SAndiry Xu status);
302175d7cf72SAndiry Xu }
302275d7cf72SAndiry Xu
3023c8968611SKai-Heng Feng /*
3024c8968611SKai-Heng Feng * Check for disconnect or reset, and bail out after several
3025c8968611SKai-Heng Feng * reset attempts to avoid warm reset loop.
3026c8968611SKai-Heng Feng */
3027c8968611SKai-Heng Feng if (status == 0 || status == -ENOTCONN || status == -ENODEV ||
3028c8968611SKai-Heng Feng (status == -EBUSY && i == PORT_RESET_TRIES - 1)) {
3029fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1,
3030fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_RESET);
3031a24a6078SSarah Sharp
3032a24a6078SSarah Sharp if (!hub_is_superspeed(hub->hdev))
303332fe0198SAlan Stern goto done;
3034a24a6078SSarah Sharp
3035fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1,
3036fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_BH_PORT_RESET);
3037fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1,
3038fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_PORT_LINK_STATE);
303922454b79SDennis Wassenberg
304022454b79SDennis Wassenberg if (udev)
3041fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1,
3042fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_CONNECTION);
3043fb6d1f7dSRobert Schlabbach
3044a24a6078SSarah Sharp /*
3045a24a6078SSarah Sharp * If a USB 3.0 device migrates from reset to an error
3046a24a6078SSarah Sharp * state, re-issue the warm reset.
3047a24a6078SSarah Sharp */
3048f061f43dSMichael Grzeschik if (usb_hub_port_status(hub, port1,
3049a24a6078SSarah Sharp &portstatus, &portchange) < 0)
3050a24a6078SSarah Sharp goto done;
3051a24a6078SSarah Sharp
30523cd12f91SDan Williams if (!hub_port_warm_reset_required(hub, port1,
30533cd12f91SDan Williams portstatus))
3054a24a6078SSarah Sharp goto done;
3055a24a6078SSarah Sharp
3056a24a6078SSarah Sharp /*
3057a24a6078SSarah Sharp * If the port is in SS.Inactive or Compliance Mode, the
3058a24a6078SSarah Sharp * hot or warm reset failed. Try another warm reset.
3059a24a6078SSarah Sharp */
3060a24a6078SSarah Sharp if (!warm) {
3061d99f6b41SDan Williams dev_dbg(&port_dev->dev,
3062d99f6b41SDan Williams "hot reset failed, warm reset\n");
3063a24a6078SSarah Sharp warm = true;
3064a24a6078SSarah Sharp }
30651da177e4SLinus Torvalds }
30661da177e4SLinus Torvalds
3067d99f6b41SDan Williams dev_dbg(&port_dev->dev,
3068d99f6b41SDan Williams "not enabled, trying %sreset again...\n",
3069d99f6b41SDan Williams warm ? "warm " : "");
30701da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME;
30711da177e4SLinus Torvalds }
30721da177e4SLinus Torvalds
3073d99f6b41SDan Williams dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
30741da177e4SLinus Torvalds
307532fe0198SAlan Stern done:
3076fb6d1f7dSRobert Schlabbach if (status == 0) {
3077aa071a92SNicolas Boichat if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM)
3078aa071a92SNicolas Boichat usleep_range(10000, 12000);
3079781f0766SKai-Heng Feng else {
3080781f0766SKai-Heng Feng /* TRSTRCY = 10 ms; plus some extra */
3081781f0766SKai-Heng Feng reset_recovery_time = 10 + 40;
3082781f0766SKai-Heng Feng
3083781f0766SKai-Heng Feng /* Hub needs extra delay after resetting its port. */
3084781f0766SKai-Heng Feng if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET)
3085781f0766SKai-Heng Feng reset_recovery_time += 100;
3086781f0766SKai-Heng Feng
3087781f0766SKai-Heng Feng msleep(reset_recovery_time);
3088781f0766SKai-Heng Feng }
3089aa071a92SNicolas Boichat
3090fb6d1f7dSRobert Schlabbach if (udev) {
3091fb6d1f7dSRobert Schlabbach struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3092fb6d1f7dSRobert Schlabbach
3093fb6d1f7dSRobert Schlabbach update_devnum(udev, 0);
3094fb6d1f7dSRobert Schlabbach /* The xHC may think the device is already reset,
3095fb6d1f7dSRobert Schlabbach * so ignore the status.
3096fb6d1f7dSRobert Schlabbach */
3097fb6d1f7dSRobert Schlabbach if (hcd->driver->reset_device)
3098fb6d1f7dSRobert Schlabbach hcd->driver->reset_device(hcd, udev);
3099fb6d1f7dSRobert Schlabbach
3100fb6d1f7dSRobert Schlabbach usb_set_device_state(udev, USB_STATE_DEFAULT);
3101fb6d1f7dSRobert Schlabbach }
3102fb6d1f7dSRobert Schlabbach } else {
3103fb6d1f7dSRobert Schlabbach if (udev)
3104fb6d1f7dSRobert Schlabbach usb_set_device_state(udev, USB_STATE_NOTATTACHED);
3105fb6d1f7dSRobert Schlabbach }
3106fb6d1f7dSRobert Schlabbach
31070fe51aa5SSarah Sharp if (!hub_is_superspeed(hub->hdev))
310832fe0198SAlan Stern up_read(&ehci_cf_port_reset_rwsem);
310975d7cf72SAndiry Xu
31101da177e4SLinus Torvalds return status;
31111da177e4SLinus Torvalds }
31121da177e4SLinus Torvalds
3113430d57f5SRay Chi /*
3114430d57f5SRay Chi * hub_port_stop_enumerate - stop USB enumeration or ignore port events
3115430d57f5SRay Chi * @hub: target hub
3116430d57f5SRay Chi * @port1: port num of the port
3117430d57f5SRay Chi * @retries: port retries number of hub_port_init()
3118430d57f5SRay Chi *
3119430d57f5SRay Chi * Return:
3120430d57f5SRay Chi * true: ignore port actions/events or give up connection attempts.
3121430d57f5SRay Chi * false: keep original behavior.
3122430d57f5SRay Chi *
3123430d57f5SRay Chi * This function will be based on retries to check whether the port which is
3124430d57f5SRay Chi * marked with early_stop attribute would stop enumeration or ignore events.
3125430d57f5SRay Chi *
3126430d57f5SRay Chi * Note:
3127430d57f5SRay Chi * This function didn't change anything if early_stop is not set, and it will
3128430d57f5SRay Chi * prevent all connection attempts when early_stop is set and the attempts of
3129430d57f5SRay Chi * the port are more than 1.
3130430d57f5SRay Chi */
hub_port_stop_enumerate(struct usb_hub * hub,int port1,int retries)3131430d57f5SRay Chi static bool hub_port_stop_enumerate(struct usb_hub *hub, int port1, int retries)
3132430d57f5SRay Chi {
3133430d57f5SRay Chi struct usb_port *port_dev = hub->ports[port1 - 1];
3134430d57f5SRay Chi
3135430d57f5SRay Chi if (port_dev->early_stop) {
3136430d57f5SRay Chi if (port_dev->ignore_event)
3137430d57f5SRay Chi return true;
3138430d57f5SRay Chi
3139430d57f5SRay Chi /*
3140430d57f5SRay Chi * We want unsuccessful attempts to fail quickly.
3141430d57f5SRay Chi * Since some devices may need one failure during
3142430d57f5SRay Chi * port initialization, we allow two tries but no
3143430d57f5SRay Chi * more.
3144430d57f5SRay Chi */
3145430d57f5SRay Chi if (retries < 2)
3146430d57f5SRay Chi return false;
3147430d57f5SRay Chi
3148430d57f5SRay Chi port_dev->ignore_event = 1;
3149430d57f5SRay Chi } else
3150430d57f5SRay Chi port_dev->ignore_event = 0;
3151430d57f5SRay Chi
3152430d57f5SRay Chi return port_dev->ignore_event;
3153430d57f5SRay Chi }
3154430d57f5SRay Chi
31550ed9a57eSAndiry Xu /* Check if a port is power on */
usb_port_is_power_on(struct usb_hub * hub,unsigned int portstatus)3156f061f43dSMichael Grzeschik int usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus)
31570ed9a57eSAndiry Xu {
31580ed9a57eSAndiry Xu int ret = 0;
31590ed9a57eSAndiry Xu
31600ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) {
31610ed9a57eSAndiry Xu if (portstatus & USB_SS_PORT_STAT_POWER)
31620ed9a57eSAndiry Xu ret = 1;
31630ed9a57eSAndiry Xu } else {
31640ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_POWER)
31650ed9a57eSAndiry Xu ret = 1;
31660ed9a57eSAndiry Xu }
31670ed9a57eSAndiry Xu
31680ed9a57eSAndiry Xu return ret;
31690ed9a57eSAndiry Xu }
31700ed9a57eSAndiry Xu
usb_lock_port(struct usb_port * port_dev)31715c79a1e3SDan Williams static void usb_lock_port(struct usb_port *port_dev)
31725c79a1e3SDan Williams __acquires(&port_dev->status_lock)
31735c79a1e3SDan Williams {
31745c79a1e3SDan Williams mutex_lock(&port_dev->status_lock);
31755c79a1e3SDan Williams __acquire(&port_dev->status_lock);
31765c79a1e3SDan Williams }
31775c79a1e3SDan Williams
usb_unlock_port(struct usb_port * port_dev)31785c79a1e3SDan Williams static void usb_unlock_port(struct usb_port *port_dev)
31795c79a1e3SDan Williams __releases(&port_dev->status_lock)
31805c79a1e3SDan Williams {
31815c79a1e3SDan Williams mutex_unlock(&port_dev->status_lock);
31825c79a1e3SDan Williams __release(&port_dev->status_lock);
31835c79a1e3SDan Williams }
31845c79a1e3SDan Williams
3185d388dab7SAlan Stern #ifdef CONFIG_PM
31861da177e4SLinus Torvalds
31870ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
port_is_suspended(struct usb_hub * hub,unsigned portstatus)31880ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
31890ed9a57eSAndiry Xu {
31900ed9a57eSAndiry Xu int ret = 0;
31910ed9a57eSAndiry Xu
31920ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) {
31930ed9a57eSAndiry Xu if ((portstatus & USB_PORT_STAT_LINK_STATE)
31940ed9a57eSAndiry Xu == USB_SS_PORT_LS_U3)
31950ed9a57eSAndiry Xu ret = 1;
31960ed9a57eSAndiry Xu } else {
31970ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_SUSPEND)
31980ed9a57eSAndiry Xu ret = 1;
31990ed9a57eSAndiry Xu }
32000ed9a57eSAndiry Xu
32010ed9a57eSAndiry Xu return ret;
32020ed9a57eSAndiry Xu }
3203b01b03f3SAlan Stern
3204b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
3205b01b03f3SAlan Stern * is ready for a reset-resume, or should be disconnected.
3206b01b03f3SAlan Stern */
check_port_resume_type(struct usb_device * udev,struct usb_hub * hub,int port1,int status,u16 portchange,u16 portstatus)3207b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
3208b01b03f3SAlan Stern struct usb_hub *hub, int port1,
32097fa40910SJulius Werner int status, u16 portchange, u16 portstatus)
3210b01b03f3SAlan Stern {
3211d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
32127fa40910SJulius Werner int retries = 3;
3213d99f6b41SDan Williams
32147fa40910SJulius Werner retry:
32153cd12f91SDan Williams /* Is a warm reset needed to recover the connection? */
32163cd12f91SDan Williams if (status == 0 && udev->reset_resume
32173cd12f91SDan Williams && hub_port_warm_reset_required(hub, port1, portstatus)) {
32183cd12f91SDan Williams /* pass */;
32193cd12f91SDan Williams }
3220b01b03f3SAlan Stern /* Is the device still present? */
32213cd12f91SDan Williams else if (status || port_is_suspended(hub, portstatus) ||
3222f061f43dSMichael Grzeschik !usb_port_is_power_on(hub, portstatus)) {
3223b01b03f3SAlan Stern if (status >= 0)
3224b01b03f3SAlan Stern status = -ENODEV;
32257fa40910SJulius Werner } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
32267fa40910SJulius Werner if (retries--) {
32277fa40910SJulius Werner usleep_range(200, 300);
3228f061f43dSMichael Grzeschik status = usb_hub_port_status(hub, port1, &portstatus,
32297fa40910SJulius Werner &portchange);
32307fa40910SJulius Werner goto retry;
32317fa40910SJulius Werner }
32327fa40910SJulius Werner status = -ENODEV;
3233b01b03f3SAlan Stern }
3234b01b03f3SAlan Stern
323586c57edfSAlan Stern /* Can't do a normal resume if the port isn't enabled,
323686c57edfSAlan Stern * so try a reset-resume instead.
323786c57edfSAlan Stern */
323886c57edfSAlan Stern else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
323986c57edfSAlan Stern if (udev->persist_enabled)
324086c57edfSAlan Stern udev->reset_resume = 1;
324186c57edfSAlan Stern else
3242b01b03f3SAlan Stern status = -ENODEV;
324386c57edfSAlan Stern }
3244b01b03f3SAlan Stern
3245b01b03f3SAlan Stern if (status) {
3246d99f6b41SDan Williams dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
3247d99f6b41SDan Williams portchange, portstatus, status);
3248b01b03f3SAlan Stern } else if (udev->reset_resume) {
3249b01b03f3SAlan Stern
3250b01b03f3SAlan Stern /* Late port handoff can set status-change bits */
3251b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION)
3252ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
3253b01b03f3SAlan Stern USB_PORT_FEAT_C_CONNECTION);
3254b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE)
3255ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
3256b01b03f3SAlan Stern USB_PORT_FEAT_C_ENABLE);
32579f952e26SAlan Stern
32589f952e26SAlan Stern /*
32599f952e26SAlan Stern * Whatever made this reset-resume necessary may have
32609f952e26SAlan Stern * turned on the port1 bit in hub->change_bits. But after
32619f952e26SAlan Stern * a successful reset-resume we want the bit to be clear;
32629f952e26SAlan Stern * if it was on it would indicate that something happened
32639f952e26SAlan Stern * following the reset-resume.
32649f952e26SAlan Stern */
32659f952e26SAlan Stern clear_bit(port1, hub->change_bits);
3266b01b03f3SAlan Stern }
3267b01b03f3SAlan Stern
3268b01b03f3SAlan Stern return status;
3269b01b03f3SAlan Stern }
3270b01b03f3SAlan Stern
usb_disable_ltm(struct usb_device * udev)3271f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
3272f74631e3SSarah Sharp {
3273f74631e3SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3274f74631e3SSarah Sharp
3275f74631e3SSarah Sharp /* Check if the roothub and device supports LTM. */
3276f74631e3SSarah Sharp if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3277f74631e3SSarah Sharp !usb_device_supports_ltm(udev))
3278f74631e3SSarah Sharp return 0;
3279f74631e3SSarah Sharp
3280f74631e3SSarah Sharp /* Clear Feature LTM Enable can only be sent if the device is
3281f74631e3SSarah Sharp * configured.
3282f74631e3SSarah Sharp */
3283f74631e3SSarah Sharp if (!udev->actconfig)
3284f74631e3SSarah Sharp return 0;
3285f74631e3SSarah Sharp
32861afe33a7SOliver Neukum return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
32871afe33a7SOliver Neukum USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
32881afe33a7SOliver Neukum USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
32891afe33a7SOliver Neukum USB_CTRL_SET_TIMEOUT);
3290f74631e3SSarah Sharp }
3291f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
3292f74631e3SSarah Sharp
usb_enable_ltm(struct usb_device * udev)3293f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev)
3294f74631e3SSarah Sharp {
3295f74631e3SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3296f74631e3SSarah Sharp
3297f74631e3SSarah Sharp /* Check if the roothub and device supports LTM. */
3298f74631e3SSarah Sharp if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3299f74631e3SSarah Sharp !usb_device_supports_ltm(udev))
3300f74631e3SSarah Sharp return;
3301f74631e3SSarah Sharp
3302f74631e3SSarah Sharp /* Set Feature LTM Enable can only be sent if the device is
3303f74631e3SSarah Sharp * configured.
3304f74631e3SSarah Sharp */
3305f74631e3SSarah Sharp if (!udev->actconfig)
3306f74631e3SSarah Sharp return;
3307f74631e3SSarah Sharp
33081afe33a7SOliver Neukum usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33091afe33a7SOliver Neukum USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
33101afe33a7SOliver Neukum USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
33111afe33a7SOliver Neukum USB_CTRL_SET_TIMEOUT);
3312f74631e3SSarah Sharp }
3313f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
3314f74631e3SSarah Sharp
331554a3ac0cSLan Tianyu /*
331628e86165SAlan Stern * usb_enable_remote_wakeup - enable remote wakeup for a device
331754a3ac0cSLan Tianyu * @udev: target device
331854a3ac0cSLan Tianyu *
331928e86165SAlan Stern * For USB-2 devices: Set the device's remote wakeup feature.
332028e86165SAlan Stern *
332128e86165SAlan Stern * For USB-3 devices: Assume there's only one function on the device and
332228e86165SAlan Stern * enable remote wake for the first interface. FIXME if the interface
332328e86165SAlan Stern * association descriptor shows there's more than one function.
332454a3ac0cSLan Tianyu */
usb_enable_remote_wakeup(struct usb_device * udev)332528e86165SAlan Stern static int usb_enable_remote_wakeup(struct usb_device *udev)
332654a3ac0cSLan Tianyu {
332728e86165SAlan Stern if (udev->speed < USB_SPEED_SUPER)
33281afe33a7SOliver Neukum return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33291afe33a7SOliver Neukum USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
33301afe33a7SOliver Neukum USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
33311afe33a7SOliver Neukum USB_CTRL_SET_TIMEOUT);
333228e86165SAlan Stern else
33331afe33a7SOliver Neukum return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33341afe33a7SOliver Neukum USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
333528e86165SAlan Stern USB_INTRF_FUNC_SUSPEND,
33361afe33a7SOliver Neukum USB_INTRF_FUNC_SUSPEND_RW |
33371afe33a7SOliver Neukum USB_INTRF_FUNC_SUSPEND_LP,
333828e86165SAlan Stern NULL, 0, USB_CTRL_SET_TIMEOUT);
333928e86165SAlan Stern }
334028e86165SAlan Stern
334128e86165SAlan Stern /*
334228e86165SAlan Stern * usb_disable_remote_wakeup - disable remote wakeup for a device
334328e86165SAlan Stern * @udev: target device
334428e86165SAlan Stern *
334528e86165SAlan Stern * For USB-2 devices: Clear the device's remote wakeup feature.
334628e86165SAlan Stern *
334728e86165SAlan Stern * For USB-3 devices: Assume there's only one function on the device and
334828e86165SAlan Stern * disable remote wake for the first interface. FIXME if the interface
334928e86165SAlan Stern * association descriptor shows there's more than one function.
335028e86165SAlan Stern */
usb_disable_remote_wakeup(struct usb_device * udev)335128e86165SAlan Stern static int usb_disable_remote_wakeup(struct usb_device *udev)
335228e86165SAlan Stern {
335328e86165SAlan Stern if (udev->speed < USB_SPEED_SUPER)
33541afe33a7SOliver Neukum return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33551afe33a7SOliver Neukum USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
335628e86165SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
335728e86165SAlan Stern USB_CTRL_SET_TIMEOUT);
335828e86165SAlan Stern else
33591afe33a7SOliver Neukum return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33601afe33a7SOliver Neukum USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
336154a3ac0cSLan Tianyu USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
336254a3ac0cSLan Tianyu USB_CTRL_SET_TIMEOUT);
336354a3ac0cSLan Tianyu }
33641da177e4SLinus Torvalds
3365e583d9dbSAlan Stern /* Count of wakeup-enabled devices at or below udev */
usb_wakeup_enabled_descendants(struct usb_device * udev)33667a6127e3SDouglas Anderson unsigned usb_wakeup_enabled_descendants(struct usb_device *udev)
3367e583d9dbSAlan Stern {
3368e583d9dbSAlan Stern struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3369e583d9dbSAlan Stern
3370e583d9dbSAlan Stern return udev->do_remote_wakeup +
3371e583d9dbSAlan Stern (hub ? hub->wakeup_enabled_descendants : 0);
3372e583d9dbSAlan Stern }
33737a6127e3SDouglas Anderson EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants);
3374e583d9dbSAlan Stern
33751da177e4SLinus Torvalds /*
3376140d8f68SAlan Stern * usb_port_suspend - suspend a usb device's upstream port
3377624d6c07SAlan Stern * @udev: device that's no longer in active use, not a root hub
33785edbfb7cSDavid Brownell * Context: must be able to sleep; device not locked; pm locks held
33791da177e4SLinus Torvalds *
33801da177e4SLinus Torvalds * Suspends a USB device that isn't in active use, conserving power.
33811da177e4SLinus Torvalds * Devices may wake out of a suspend, if anything important happens,
33821da177e4SLinus Torvalds * using the remote wakeup mechanism. They may also be taken out of
3383140d8f68SAlan Stern * suspend by the host, using usb_port_resume(). It's also routine
33841da177e4SLinus Torvalds * to disconnect devices while they are suspended.
33851da177e4SLinus Torvalds *
3386390a8c34SDavid Brownell * This only affects the USB hardware for a device; its interfaces
3387390a8c34SDavid Brownell * (and, for hubs, child devices) must already have been suspended.
3388390a8c34SDavid Brownell *
3389624d6c07SAlan Stern * Selective port suspend reduces power; most suspended devices draw
3390624d6c07SAlan Stern * less than 500 uA. It's also used in OTG, along with remote wakeup.
3391624d6c07SAlan Stern * All devices below the suspended port are also suspended.
3392624d6c07SAlan Stern *
3393624d6c07SAlan Stern * Devices leave suspend state when the host wakes them up. Some devices
3394624d6c07SAlan Stern * also support "remote wakeup", where the device can activate the USB
3395624d6c07SAlan Stern * tree above them to deliver data, such as a keypress or packet. In
3396624d6c07SAlan Stern * some cases, this wakes the USB host.
3397624d6c07SAlan Stern *
33981da177e4SLinus Torvalds * Suspending OTG devices may trigger HNP, if that's been enabled
33991da177e4SLinus Torvalds * between a pair of dual-role devices. That will change roles, such
34001da177e4SLinus Torvalds * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
34011da177e4SLinus Torvalds *
34024956eccdSAlan Stern * Devices on USB hub ports have only one "suspend" state, corresponding
34034956eccdSAlan Stern * to ACPI D2, "may cause the device to lose some context".
34044956eccdSAlan Stern * State transitions include:
34054956eccdSAlan Stern *
34064956eccdSAlan Stern * - suspend, resume ... when the VBUS power link stays live
34074956eccdSAlan Stern * - suspend, disconnect ... VBUS lost
34084956eccdSAlan Stern *
34094956eccdSAlan Stern * Once VBUS drop breaks the circuit, the port it's using has to go through
34104956eccdSAlan Stern * normal re-enumeration procedures, starting with enabling VBUS power.
34114956eccdSAlan Stern * Other than re-initializing the hub (plug/unplug, except for root hubs),
341237ebb549SPetr Mladek * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
34134956eccdSAlan Stern * timer, no SRP, no requests through sysfs.
34144956eccdSAlan Stern *
3415e583d9dbSAlan Stern * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3416e583d9dbSAlan Stern * suspended until their bus goes into global suspend (i.e., the root
34170aa2832dSAlan Stern * hub is suspended). Nevertheless, we change @udev->state to
34180aa2832dSAlan Stern * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
34190aa2832dSAlan Stern * upstream port setting is stored in @udev->port_is_suspended.
34204956eccdSAlan Stern *
34211da177e4SLinus Torvalds * Returns 0 on success, else negative errno.
34221da177e4SLinus Torvalds */
usb_port_suspend(struct usb_device * udev,pm_message_t msg)342365bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
34241da177e4SLinus Torvalds {
3425ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3426ad493e5eSLan Tianyu struct usb_port *port_dev = hub->ports[udev->portnum - 1];
3427624d6c07SAlan Stern int port1 = udev->portnum;
3428624d6c07SAlan Stern int status;
34290aa2832dSAlan Stern bool really_suspend = true;
34304956eccdSAlan Stern
34315c79a1e3SDan Williams usb_lock_port(port_dev);
34325c79a1e3SDan Williams
3433624d6c07SAlan Stern /* enable remote wakeup when appropriate; this lets the device
3434624d6c07SAlan Stern * wake up the upstream hub (including maybe the root hub).
3435624d6c07SAlan Stern *
3436624d6c07SAlan Stern * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3437624d6c07SAlan Stern * we don't explicitly enable it here.
3438624d6c07SAlan Stern */
3439624d6c07SAlan Stern if (udev->do_remote_wakeup) {
344028e86165SAlan Stern status = usb_enable_remote_wakeup(udev);
34410c487206SOliver Neukum if (status) {
3442624d6c07SAlan Stern dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3443624d6c07SAlan Stern status);
34440c487206SOliver Neukum /* bail if autosuspend is requested */
34455b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg))
34464fae6f0fSAlan Stern goto err_wakeup;
34470c487206SOliver Neukum }
3448624d6c07SAlan Stern }
3449624d6c07SAlan Stern
345065580b43SAndiry Xu /* disable USB2 hardware LPM */
34517529b257SKai-Heng Feng usb_disable_usb2_hardware_lpm(udev);
345265580b43SAndiry Xu
3453f74631e3SSarah Sharp if (usb_disable_ltm(udev)) {
34541ccc417eSJoe Perches dev_err(&udev->dev, "Failed to disable LTM before suspend\n");
34554fae6f0fSAlan Stern status = -ENOMEM;
34564fae6f0fSAlan Stern if (PMSG_IS_AUTO(msg))
34574fae6f0fSAlan Stern goto err_ltm;
3458f74631e3SSarah Sharp }
34598306095fSSarah Sharp
3460624d6c07SAlan Stern /* see 7.1.7.6 */
3461a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev))
3462c2f60db7SSarah Sharp status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
3463e583d9dbSAlan Stern
34640aa2832dSAlan Stern /*
34650aa2832dSAlan Stern * For system suspend, we do not need to enable the suspend feature
34660aa2832dSAlan Stern * on individual USB-2 ports. The devices will automatically go
34670aa2832dSAlan Stern * into suspend a few ms after the root hub stops sending packets.
34680aa2832dSAlan Stern * The USB 2.0 spec calls this "global suspend".
3469e583d9dbSAlan Stern *
3470e583d9dbSAlan Stern * However, many USB hubs have a bug: They don't relay wakeup requests
3471e583d9dbSAlan Stern * from a downstream port if the port's suspend feature isn't on.
3472e583d9dbSAlan Stern * Therefore we will turn on the suspend feature if udev or any of its
3473e583d9dbSAlan Stern * descendants is enabled for remote wakeup.
34740aa2832dSAlan Stern */
34757a6127e3SDouglas Anderson else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0)
3476e583d9dbSAlan Stern status = set_port_feature(hub->hdev, port1,
3477e583d9dbSAlan Stern USB_PORT_FEAT_SUSPEND);
34780aa2832dSAlan Stern else {
34790aa2832dSAlan Stern really_suspend = false;
34800aa2832dSAlan Stern status = 0;
34810aa2832dSAlan Stern }
3482624d6c07SAlan Stern if (status) {
348371424523SChris Chiu /* Check if the port has been suspended for the timeout case
348471424523SChris Chiu * to prevent the suspended port from incorrect handling.
348571424523SChris Chiu */
348671424523SChris Chiu if (status == -ETIMEDOUT) {
348771424523SChris Chiu int ret;
348871424523SChris Chiu u16 portstatus, portchange;
348971424523SChris Chiu
349071424523SChris Chiu portstatus = portchange = 0;
3491f061f43dSMichael Grzeschik ret = usb_hub_port_status(hub, port1, &portstatus,
349271424523SChris Chiu &portchange);
349371424523SChris Chiu
349471424523SChris Chiu dev_dbg(&port_dev->dev,
349571424523SChris Chiu "suspend timeout, status %04x\n", portstatus);
349671424523SChris Chiu
349771424523SChris Chiu if (ret == 0 && port_is_suspended(hub, portstatus)) {
349871424523SChris Chiu status = 0;
349971424523SChris Chiu goto suspend_done;
350071424523SChris Chiu }
350171424523SChris Chiu }
350271424523SChris Chiu
3503d99f6b41SDan Williams dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
3504cbb33004SAlan Stern
3505d590c231SMathias Nyman /* Try to enable USB3 LTM again */
35064fae6f0fSAlan Stern usb_enable_ltm(udev);
35074fae6f0fSAlan Stern err_ltm:
3508c3e751e4SAndiry Xu /* Try to enable USB2 hardware LPM again */
35097529b257SKai-Heng Feng usb_enable_usb2_hardware_lpm(udev);
3510c3e751e4SAndiry Xu
35114fae6f0fSAlan Stern if (udev->do_remote_wakeup)
35124fae6f0fSAlan Stern (void) usb_disable_remote_wakeup(udev);
35134fae6f0fSAlan Stern err_wakeup:
35148306095fSSarah Sharp
3515cbb33004SAlan Stern /* System sleep transitions should never fail */
35165b1b0b81SAlan Stern if (!PMSG_IS_AUTO(msg))
3517cbb33004SAlan Stern status = 0;
3518624d6c07SAlan Stern } else {
351971424523SChris Chiu suspend_done:
352030b1a7a3SAlan Stern dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
352130b1a7a3SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : ""),
352230b1a7a3SAlan Stern udev->do_remote_wakeup);
35230aa2832dSAlan Stern if (really_suspend) {
3524bfd1e910SAlan Stern udev->port_is_suspended = 1;
3525e583d9dbSAlan Stern
3526e583d9dbSAlan Stern /* device has up to 10 msec to fully suspend */
3527624d6c07SAlan Stern msleep(10);
3528624d6c07SAlan Stern }
3529e583d9dbSAlan Stern usb_set_device_state(udev, USB_STATE_SUSPENDED);
35300aa2832dSAlan Stern }
3531ad493e5eSLan Tianyu
3532d5c3834eSDan Williams if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3533d5c3834eSDan Williams && test_and_clear_bit(port1, hub->child_usage_bits))
3534ad493e5eSLan Tianyu pm_runtime_put_sync(&port_dev->dev);
3535ad493e5eSLan Tianyu
3536c08512c7SAlan Stern usb_mark_last_busy(hub->hdev);
35375c79a1e3SDan Williams
35385c79a1e3SDan Williams usb_unlock_port(port_dev);
35394956eccdSAlan Stern return status;
35401da177e4SLinus Torvalds }
3541f3f3253dSDavid Brownell
35421da177e4SLinus Torvalds /*
3543390a8c34SDavid Brownell * If the USB "suspend" state is in use (rather than "global suspend"),
3544390a8c34SDavid Brownell * many devices will be individually taken out of suspend state using
354554515fe5SAlan Stern * special "resume" signaling. This routine kicks in shortly after
35461da177e4SLinus Torvalds * hardware resume signaling is finished, either because of selective
35471da177e4SLinus Torvalds * resume (by host) or remote wakeup (by device) ... now see what changed
35481da177e4SLinus Torvalds * in the tree that's rooted at this device.
354954515fe5SAlan Stern *
355054515fe5SAlan Stern * If @udev->reset_resume is set then the device is reset before the
355154515fe5SAlan Stern * status check is done.
35521da177e4SLinus Torvalds */
finish_port_resume(struct usb_device * udev)3553140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
35541da177e4SLinus Torvalds {
355554515fe5SAlan Stern int status = 0;
355607e72b95SOliver Neukum u16 devstatus = 0;
35571da177e4SLinus Torvalds
35581da177e4SLinus Torvalds /* caller owns the udev device lock */
3559b9cef6c3SWu Fengguang dev_dbg(&udev->dev, "%s\n",
3560b9cef6c3SWu Fengguang udev->reset_resume ? "finish reset-resume" : "finish resume");
35611da177e4SLinus Torvalds
35621da177e4SLinus Torvalds /* usb ch9 identifies four variants of SUSPENDED, based on what
35631da177e4SLinus Torvalds * state the device resumes to. Linux currently won't see the
35641da177e4SLinus Torvalds * first two on the host side; they'd be inside hub_port_init()
356537ebb549SPetr Mladek * during many timeouts, but hub_wq can't suspend until later.
35661da177e4SLinus Torvalds */
35671da177e4SLinus Torvalds usb_set_device_state(udev, udev->actconfig
35681da177e4SLinus Torvalds ? USB_STATE_CONFIGURED
35691da177e4SLinus Torvalds : USB_STATE_ADDRESS);
35701da177e4SLinus Torvalds
357154515fe5SAlan Stern /* 10.5.4.5 says not to reset a suspended port if the attached
357254515fe5SAlan Stern * device is enabled for remote wakeup. Hence the reset
357354515fe5SAlan Stern * operation is carried out here, after the port has been
357454515fe5SAlan Stern * resumed.
357554515fe5SAlan Stern */
35761d10255cSAlan Stern if (udev->reset_resume) {
35771d10255cSAlan Stern /*
35781d10255cSAlan Stern * If the device morphs or switches modes when it is reset,
35791d10255cSAlan Stern * we don't want to perform a reset-resume. We'll fail the
35801d10255cSAlan Stern * resume, which will cause a logical disconnect, and then
35811d10255cSAlan Stern * the device will be rediscovered.
35821d10255cSAlan Stern */
358386c57edfSAlan Stern retry_reset_resume:
35841d10255cSAlan Stern if (udev->quirks & USB_QUIRK_RESET)
35851d10255cSAlan Stern status = -ENODEV;
35861d10255cSAlan Stern else
3587742120c6SMing Lei status = usb_reset_and_verify_device(udev);
35881d10255cSAlan Stern }
358954515fe5SAlan Stern
35901da177e4SLinus Torvalds /* 10.5.4.5 says be sure devices in the tree are still there.
35911da177e4SLinus Torvalds * For now let's assume the device didn't go crazy on resume,
35921da177e4SLinus Torvalds * and device drivers will know about any resume quirks.
35931da177e4SLinus Torvalds */
359454515fe5SAlan Stern if (status == 0) {
359546dede46SAlan Stern devstatus = 0;
3596d9e1e148SFelipe Balbi status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
359786c57edfSAlan Stern
359886c57edfSAlan Stern /* If a normal resume failed, try doing a reset-resume */
359986c57edfSAlan Stern if (status && !udev->reset_resume && udev->persist_enabled) {
360086c57edfSAlan Stern dev_dbg(&udev->dev, "retry with reset-resume\n");
360186c57edfSAlan Stern udev->reset_resume = 1;
360286c57edfSAlan Stern goto retry_reset_resume;
360386c57edfSAlan Stern }
360454515fe5SAlan Stern }
3605b40b7a90SAlan Stern
3606624d6c07SAlan Stern if (status) {
3607624d6c07SAlan Stern dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
36081da177e4SLinus Torvalds status);
360907e72b95SOliver Neukum /*
361007e72b95SOliver Neukum * There are a few quirky devices which violate the standard
361107e72b95SOliver Neukum * by claiming to have remote wakeup enabled after a reset,
361207e72b95SOliver Neukum * which crash if the feature is cleared, hence check for
361307e72b95SOliver Neukum * udev->reset_resume
361407e72b95SOliver Neukum */
361507e72b95SOliver Neukum } else if (udev->actconfig && !udev->reset_resume) {
361628e86165SAlan Stern if (udev->speed < USB_SPEED_SUPER) {
361754a3ac0cSLan Tianyu if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
361828e86165SAlan Stern status = usb_disable_remote_wakeup(udev);
361954a3ac0cSLan Tianyu } else {
3620d9e1e148SFelipe Balbi status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0,
362154a3ac0cSLan Tianyu &devstatus);
362254a3ac0cSLan Tianyu if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
362354a3ac0cSLan Tianyu | USB_INTRF_STAT_FUNC_RW))
362428e86165SAlan Stern status = usb_disable_remote_wakeup(udev);
362554a3ac0cSLan Tianyu }
362654a3ac0cSLan Tianyu
3627a8e7c565SAlan Stern if (status)
3628b9cef6c3SWu Fengguang dev_dbg(&udev->dev,
3629b9cef6c3SWu Fengguang "disable remote wakeup, status %d\n",
3630b9cef6c3SWu Fengguang status);
36311da177e4SLinus Torvalds status = 0;
36321da177e4SLinus Torvalds }
36331da177e4SLinus Torvalds return status;
36341da177e4SLinus Torvalds }
36351da177e4SLinus Torvalds
3636624d6c07SAlan Stern /*
3637a40178b2SPratyush Anand * There are some SS USB devices which take longer time for link training.
3638a40178b2SPratyush Anand * XHCI specs 4.19.4 says that when Link training is successful, port
36396b82b122SAl Cooper * sets CCS bit to 1. So if SW reads port status before successful link
3640a40178b2SPratyush Anand * training, then it will not find device to be present.
3641a40178b2SPratyush Anand * USB Analyzer log with such buggy devices show that in some cases
3642a40178b2SPratyush Anand * device switch on the RX termination after long delay of host enabling
3643a40178b2SPratyush Anand * the VBUS. In few other cases it has been seen that device fails to
3644a40178b2SPratyush Anand * negotiate link training in first attempt. It has been
3645a40178b2SPratyush Anand * reported till now that few devices take as long as 2000 ms to train
3646a40178b2SPratyush Anand * the link after host enabling its VBUS and termination. Following
3647a40178b2SPratyush Anand * routine implements a 2000 ms timeout for link training. If in a case
3648a40178b2SPratyush Anand * link trains before timeout, loop will exit earlier.
3649a40178b2SPratyush Anand *
36506b82b122SAl Cooper * There are also some 2.0 hard drive based devices and 3.0 thumb
36516b82b122SAl Cooper * drives that, when plugged into a 2.0 only port, take a long
36526b82b122SAl Cooper * time to set CCS after VBUS enable.
36536b82b122SAl Cooper *
3654a40178b2SPratyush Anand * FIXME: If a device was connected before suspend, but was removed
3655a40178b2SPratyush Anand * while system was asleep, then the loop in the following routine will
3656a40178b2SPratyush Anand * only exit at timeout.
3657a40178b2SPratyush Anand *
36586b82b122SAl Cooper * This routine should only be called when persist is enabled.
3659a40178b2SPratyush Anand */
wait_for_connected(struct usb_device * udev,struct usb_hub * hub,int port1,u16 * portchange,u16 * portstatus)36606b82b122SAl Cooper static int wait_for_connected(struct usb_device *udev,
3661b8f1ba99SDan Carpenter struct usb_hub *hub, int port1,
3662a40178b2SPratyush Anand u16 *portchange, u16 *portstatus)
3663a40178b2SPratyush Anand {
3664a40178b2SPratyush Anand int status = 0, delay_ms = 0;
3665a40178b2SPratyush Anand
3666a40178b2SPratyush Anand while (delay_ms < 2000) {
3667a40178b2SPratyush Anand if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3668a40178b2SPratyush Anand break;
3669f061f43dSMichael Grzeschik if (!usb_port_is_power_on(hub, *portstatus)) {
36705d111f51SDominik Bozek status = -ENODEV;
36715d111f51SDominik Bozek break;
36725d111f51SDominik Bozek }
3673a40178b2SPratyush Anand msleep(20);
3674a40178b2SPratyush Anand delay_ms += 20;
3675f061f43dSMichael Grzeschik status = usb_hub_port_status(hub, port1, portstatus, portchange);
3676a40178b2SPratyush Anand }
36776b82b122SAl Cooper dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
3678a40178b2SPratyush Anand return status;
3679a40178b2SPratyush Anand }
3680a40178b2SPratyush Anand
3681a40178b2SPratyush Anand /*
3682624d6c07SAlan Stern * usb_port_resume - re-activate a suspended usb device's upstream port
3683624d6c07SAlan Stern * @udev: device to re-activate, not a root hub
3684624d6c07SAlan Stern * Context: must be able to sleep; device not locked; pm locks held
3685624d6c07SAlan Stern *
3686624d6c07SAlan Stern * This will re-activate the suspended device, increasing power usage
3687624d6c07SAlan Stern * while letting drivers communicate again with its endpoints.
3688624d6c07SAlan Stern * USB resume explicitly guarantees that the power session between
3689624d6c07SAlan Stern * the host and the device is the same as it was when the device
3690624d6c07SAlan Stern * suspended.
3691624d6c07SAlan Stern *
3692feccc30dSAlan Stern * If @udev->reset_resume is set then this routine won't check that the
3693feccc30dSAlan Stern * port is still enabled. Furthermore, finish_port_resume() above will
369454515fe5SAlan Stern * reset @udev. The end result is that a broken power session can be
369554515fe5SAlan Stern * recovered and @udev will appear to persist across a loss of VBUS power.
369654515fe5SAlan Stern *
369754515fe5SAlan Stern * For example, if a host controller doesn't maintain VBUS suspend current
369854515fe5SAlan Stern * during a system sleep or is reset when the system wakes up, all the USB
369954515fe5SAlan Stern * power sessions below it will be broken. This is especially troublesome
370054515fe5SAlan Stern * for mass-storage devices containing mounted filesystems, since the
370154515fe5SAlan Stern * device will appear to have disconnected and all the memory mappings
370254515fe5SAlan Stern * to it will be lost. Using the USB_PERSIST facility, the device can be
370354515fe5SAlan Stern * made to appear as if it had not disconnected.
370454515fe5SAlan Stern *
3705742120c6SMing Lei * This facility can be dangerous. Although usb_reset_and_verify_device() makes
3706feccc30dSAlan Stern * every effort to insure that the same device is present after the
370754515fe5SAlan Stern * reset as before, it cannot provide a 100% guarantee. Furthermore it's
370854515fe5SAlan Stern * quite possible for a device to remain unaltered but its media to be
370954515fe5SAlan Stern * changed. If the user replaces a flash memory card while the system is
371054515fe5SAlan Stern * asleep, he will have only himself to blame when the filesystem on the
371154515fe5SAlan Stern * new card is corrupted and the system crashes.
371254515fe5SAlan Stern *
3713624d6c07SAlan Stern * Returns 0 on success, else negative errno.
3714624d6c07SAlan Stern */
usb_port_resume(struct usb_device * udev,pm_message_t msg)371565bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
37161da177e4SLinus Torvalds {
3717ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3718ad493e5eSLan Tianyu struct usb_port *port_dev = hub->ports[udev->portnum - 1];
3719624d6c07SAlan Stern int port1 = udev->portnum;
37201da177e4SLinus Torvalds int status;
3721d25450c6SAlan Stern u16 portchange, portstatus;
3722d25450c6SAlan Stern
3723d5c3834eSDan Williams if (!test_and_set_bit(port1, hub->child_usage_bits)) {
3724025f97d1SBixuan Cui status = pm_runtime_resume_and_get(&port_dev->dev);
3725ad493e5eSLan Tianyu if (status < 0) {
3726ad493e5eSLan Tianyu dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3727ad493e5eSLan Tianyu status);
3728ad493e5eSLan Tianyu return status;
3729ad493e5eSLan Tianyu }
3730ad493e5eSLan Tianyu }
3731ad493e5eSLan Tianyu
37325c79a1e3SDan Williams usb_lock_port(port_dev);
37335c79a1e3SDan Williams
3734d25450c6SAlan Stern /* Skip the initial Clear-Suspend step for a remote wakeup */
3735f061f43dSMichael Grzeschik status = usb_hub_port_status(hub, port1, &portstatus, &portchange);
373683a62c51SRavi Chandra Sadineni if (status == 0 && !port_is_suspended(hub, portstatus)) {
373783a62c51SRavi Chandra Sadineni if (portchange & USB_PORT_STAT_C_SUSPEND)
373883a62c51SRavi Chandra Sadineni pm_wakeup_event(&udev->dev, 0);
3739d25450c6SAlan Stern goto SuspendCleared;
374083a62c51SRavi Chandra Sadineni }
37411da177e4SLinus Torvalds
37421da177e4SLinus Torvalds /* see 7.1.7.7; affects power usage, but not budgeting */
3743a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev))
3744c2f60db7SSarah Sharp status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
3745a7114230SAndiry Xu else
3746ad493e5eSLan Tianyu status = usb_clear_port_feature(hub->hdev,
37471da177e4SLinus Torvalds port1, USB_PORT_FEAT_SUSPEND);
37481da177e4SLinus Torvalds if (status) {
3749d99f6b41SDan Williams dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
37501da177e4SLinus Torvalds } else {
3751bbc78c07SFelipe Balbi /* drive resume for USB_RESUME_TIMEOUT msec */
3752645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n",
37535b1b0b81SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : ""));
3754bbc78c07SFelipe Balbi msleep(USB_RESUME_TIMEOUT);
37551da177e4SLinus Torvalds
37561da177e4SLinus Torvalds /* Virtual root hubs can trigger on GET_PORT_STATUS to
37571da177e4SLinus Torvalds * stop resume signaling. Then finish the resume
37581da177e4SLinus Torvalds * sequence.
37591da177e4SLinus Torvalds */
3760f061f43dSMichael Grzeschik status = usb_hub_port_status(hub, port1, &portstatus, &portchange);
37611da177e4SLinus Torvalds }
3762b01b03f3SAlan Stern
3763b01b03f3SAlan Stern SuspendCleared:
3764b01b03f3SAlan Stern if (status == 0) {
3765bfd1e910SAlan Stern udev->port_is_suspended = 0;
3766a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) {
3767a7114230SAndiry Xu if (portchange & USB_PORT_STAT_C_LINK_STATE)
3768ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
3769a7114230SAndiry Xu USB_PORT_FEAT_C_PORT_LINK_STATE);
3770a7114230SAndiry Xu } else {
3771b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_SUSPEND)
3772ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
3773b01b03f3SAlan Stern USB_PORT_FEAT_C_SUSPEND);
37741da177e4SLinus Torvalds }
3775975f94c7SChunfeng Yun
3776975f94c7SChunfeng Yun /* TRSMRCY = 10 msec */
3777975f94c7SChunfeng Yun msleep(10);
3778a7114230SAndiry Xu }
37791da177e4SLinus Torvalds
37806b82b122SAl Cooper if (udev->persist_enabled)
3781b8f1ba99SDan Carpenter status = wait_for_connected(udev, hub, port1, &portchange,
3782a40178b2SPratyush Anand &portstatus);
3783a40178b2SPratyush Anand
3784b01b03f3SAlan Stern status = check_port_resume_type(udev,
3785b01b03f3SAlan Stern hub, port1, status, portchange, portstatus);
378654515fe5SAlan Stern if (status == 0)
378754515fe5SAlan Stern status = finish_port_resume(udev);
378854515fe5SAlan Stern if (status < 0) {
378954515fe5SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status);
379054515fe5SAlan Stern hub_port_logical_disconnect(hub, port1);
379165580b43SAndiry Xu } else {
379265580b43SAndiry Xu /* Try to enable USB2 hardware LPM */
37937529b257SKai-Heng Feng usb_enable_usb2_hardware_lpm(udev);
37948306095fSSarah Sharp
3795d590c231SMathias Nyman /* Try to enable USB3 LTM */
3796f74631e3SSarah Sharp usb_enable_ltm(udev);
379754515fe5SAlan Stern }
379865580b43SAndiry Xu
37995c79a1e3SDan Williams usb_unlock_port(port_dev);
38005c79a1e3SDan Williams
38011da177e4SLinus Torvalds return status;
38021da177e4SLinus Torvalds }
38031da177e4SLinus Torvalds
usb_remote_wakeup(struct usb_device * udev)38040534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev)
38051da177e4SLinus Torvalds {
38061da177e4SLinus Torvalds int status = 0;
38071da177e4SLinus Torvalds
38085c79a1e3SDan Williams usb_lock_device(udev);
38091da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED) {
3810645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
38119bbdf1e0SAlan Stern status = usb_autoresume_device(udev);
38129bbdf1e0SAlan Stern if (status == 0) {
38139bbdf1e0SAlan Stern /* Let the drivers do their thing, then... */
38149bbdf1e0SAlan Stern usb_autosuspend_device(udev);
38159bbdf1e0SAlan Stern }
3816d25450c6SAlan Stern }
38175c79a1e3SDan Williams usb_unlock_device(udev);
38181da177e4SLinus Torvalds return status;
38191da177e4SLinus Torvalds }
38201da177e4SLinus Torvalds
38217e73be22SDan Williams /* Returns 1 if there was a remote wakeup and a connect status change. */
hub_handle_remote_wakeup(struct usb_hub * hub,unsigned int port,u16 portstatus,u16 portchange)38227e73be22SDan Williams static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
38237e73be22SDan Williams u16 portstatus, u16 portchange)
38247e73be22SDan Williams __must_hold(&port_dev->status_lock)
38257e73be22SDan Williams {
38267e73be22SDan Williams struct usb_port *port_dev = hub->ports[port - 1];
38277e73be22SDan Williams struct usb_device *hdev;
38287e73be22SDan Williams struct usb_device *udev;
38297e73be22SDan Williams int connect_change = 0;
3830e244c469SLee, Chiasheng u16 link_state;
38317e73be22SDan Williams int ret;
38327e73be22SDan Williams
38337e73be22SDan Williams hdev = hub->hdev;
38347e73be22SDan Williams udev = port_dev->child;
38357e73be22SDan Williams if (!hub_is_superspeed(hdev)) {
38367e73be22SDan Williams if (!(portchange & USB_PORT_STAT_C_SUSPEND))
38377e73be22SDan Williams return 0;
38387e73be22SDan Williams usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
38397e73be22SDan Williams } else {
3840e244c469SLee, Chiasheng link_state = portstatus & USB_PORT_STAT_LINK_STATE;
38417e73be22SDan Williams if (!udev || udev->state != USB_STATE_SUSPENDED ||
3842e244c469SLee, Chiasheng (link_state != USB_SS_PORT_LS_U0 &&
3843e244c469SLee, Chiasheng link_state != USB_SS_PORT_LS_U1 &&
3844e244c469SLee, Chiasheng link_state != USB_SS_PORT_LS_U2))
38457e73be22SDan Williams return 0;
38467e73be22SDan Williams }
38477e73be22SDan Williams
38487e73be22SDan Williams if (udev) {
38497e73be22SDan Williams /* TRSMRCY = 10 msec */
38507e73be22SDan Williams msleep(10);
38517e73be22SDan Williams
38527e73be22SDan Williams usb_unlock_port(port_dev);
38537e73be22SDan Williams ret = usb_remote_wakeup(udev);
38547e73be22SDan Williams usb_lock_port(port_dev);
38557e73be22SDan Williams if (ret < 0)
38567e73be22SDan Williams connect_change = 1;
38577e73be22SDan Williams } else {
38587e73be22SDan Williams ret = -ENODEV;
38597e73be22SDan Williams hub_port_disable(hub, port, 1);
38607e73be22SDan Williams }
38617e73be22SDan Williams dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
38627e73be22SDan Williams return connect_change;
38637e73be22SDan Williams }
38647e73be22SDan Williams
check_ports_changed(struct usb_hub * hub)3865e6f30deaSMing Lei static int check_ports_changed(struct usb_hub *hub)
3866e6f30deaSMing Lei {
3867e6f30deaSMing Lei int port1;
3868e6f30deaSMing Lei
3869e6f30deaSMing Lei for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3870e6f30deaSMing Lei u16 portstatus, portchange;
3871e6f30deaSMing Lei int status;
3872e6f30deaSMing Lei
3873f061f43dSMichael Grzeschik status = usb_hub_port_status(hub, port1, &portstatus, &portchange);
3874e6f30deaSMing Lei if (!status && portchange)
3875e6f30deaSMing Lei return 1;
3876e6f30deaSMing Lei }
3877e6f30deaSMing Lei return 0;
3878e6f30deaSMing Lei }
3879e6f30deaSMing Lei
hub_suspend(struct usb_interface * intf,pm_message_t msg)3880db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
38811da177e4SLinus Torvalds {
38821da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf);
38831da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
38841da177e4SLinus Torvalds unsigned port1;
38851da177e4SLinus Torvalds
3886e583d9dbSAlan Stern /*
3887e583d9dbSAlan Stern * Warn if children aren't already suspended.
3888e583d9dbSAlan Stern * Also, add up the number of wakeup-enabled descendants.
3889e583d9dbSAlan Stern */
3890e583d9dbSAlan Stern hub->wakeup_enabled_descendants = 0;
38911da177e4SLinus Torvalds for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3892d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
3893d99f6b41SDan Williams struct usb_device *udev = port_dev->child;
38941da177e4SLinus Torvalds
38956840d255SAlan Stern if (udev && udev->can_submit) {
3896b658b8f5SDan Williams dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3897b658b8f5SDan Williams dev_name(&udev->dev));
38985b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg))
3899c9f89fa4SDavid Brownell return -EBUSY;
3900c9f89fa4SDavid Brownell }
3901e583d9dbSAlan Stern if (udev)
3902e583d9dbSAlan Stern hub->wakeup_enabled_descendants +=
39037a6127e3SDouglas Anderson usb_wakeup_enabled_descendants(udev);
39041da177e4SLinus Torvalds }
3905e6f30deaSMing Lei
3906e6f30deaSMing Lei if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3907e6f30deaSMing Lei /* check if there are changes pending on hub ports */
3908e6f30deaSMing Lei if (check_ports_changed(hub)) {
3909e6f30deaSMing Lei if (PMSG_IS_AUTO(msg))
3910e6f30deaSMing Lei return -EBUSY;
3911e6f30deaSMing Lei pm_wakeup_event(&hdev->dev, 2000);
3912e6f30deaSMing Lei }
3913e6f30deaSMing Lei }
3914e6f30deaSMing Lei
39154296c70aSSarah Sharp if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
39164296c70aSSarah Sharp /* Enable hub to send remote wakeup for all ports. */
39174296c70aSSarah Sharp for (port1 = 1; port1 <= hdev->maxchild; port1++) {
39183bee346bSMathieu Malaterre set_port_feature(hdev,
39194296c70aSSarah Sharp port1 |
39204296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
39214296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
39224296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
39234296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_MASK);
39244296c70aSSarah Sharp }
39254296c70aSSarah Sharp }
39261da177e4SLinus Torvalds
3927441b62c1SHarvey Harrison dev_dbg(&intf->dev, "%s\n", __func__);
392840f122f3SAlan Stern
392937ebb549SPetr Mladek /* stop hub_wq and related activity */
39304330354fSAlan Stern hub_quiesce(hub, HUB_SUSPEND);
3931b6f6436dSAlan Stern return 0;
39321da177e4SLinus Torvalds }
39331da177e4SLinus Torvalds
3934379cacc5SAlan Stern /* Report wakeup requests from the ports of a resuming root hub */
report_wakeup_requests(struct usb_hub * hub)3935379cacc5SAlan Stern static void report_wakeup_requests(struct usb_hub *hub)
3936379cacc5SAlan Stern {
3937379cacc5SAlan Stern struct usb_device *hdev = hub->hdev;
3938379cacc5SAlan Stern struct usb_device *udev;
3939379cacc5SAlan Stern struct usb_hcd *hcd;
3940379cacc5SAlan Stern unsigned long resuming_ports;
3941379cacc5SAlan Stern int i;
3942379cacc5SAlan Stern
3943379cacc5SAlan Stern if (hdev->parent)
3944379cacc5SAlan Stern return; /* Not a root hub */
3945379cacc5SAlan Stern
3946379cacc5SAlan Stern hcd = bus_to_hcd(hdev->bus);
3947379cacc5SAlan Stern if (hcd->driver->get_resuming_ports) {
3948379cacc5SAlan Stern
3949379cacc5SAlan Stern /*
3950379cacc5SAlan Stern * The get_resuming_ports() method returns a bitmap (origin 0)
3951379cacc5SAlan Stern * of ports which have started wakeup signaling but have not
3952379cacc5SAlan Stern * yet finished resuming. During system resume we will
3953379cacc5SAlan Stern * resume all the enabled ports, regardless of any wakeup
3954379cacc5SAlan Stern * signals, which means the wakeup requests would be lost.
3955379cacc5SAlan Stern * To prevent this, report them to the PM core here.
3956379cacc5SAlan Stern */
3957379cacc5SAlan Stern resuming_ports = hcd->driver->get_resuming_ports(hcd);
3958379cacc5SAlan Stern for (i = 0; i < hdev->maxchild; ++i) {
3959379cacc5SAlan Stern if (test_bit(i, &resuming_ports)) {
3960379cacc5SAlan Stern udev = hub->ports[i]->child;
3961379cacc5SAlan Stern if (udev)
3962379cacc5SAlan Stern pm_wakeup_event(&udev->dev, 0);
3963379cacc5SAlan Stern }
3964379cacc5SAlan Stern }
3965379cacc5SAlan Stern }
3966379cacc5SAlan Stern }
3967379cacc5SAlan Stern
hub_resume(struct usb_interface * intf)39681da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
39691da177e4SLinus Torvalds {
39701da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf);
39711da177e4SLinus Torvalds
39725e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__);
3973f2835219SAlan Stern hub_activate(hub, HUB_RESUME);
3974379cacc5SAlan Stern
3975379cacc5SAlan Stern /*
3976379cacc5SAlan Stern * This should be called only for system resume, not runtime resume.
3977379cacc5SAlan Stern * We can't tell the difference here, so some wakeup requests will be
3978379cacc5SAlan Stern * reported at the wrong time or more than once. This shouldn't
3979379cacc5SAlan Stern * matter much, so long as they do get reported.
3980379cacc5SAlan Stern */
3981379cacc5SAlan Stern report_wakeup_requests(hub);
39821da177e4SLinus Torvalds return 0;
39831da177e4SLinus Torvalds }
39841da177e4SLinus Torvalds
hub_reset_resume(struct usb_interface * intf)3985b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
3986f07600cfSAlan Stern {
3987b41a60ecSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf);
3988f07600cfSAlan Stern
39895e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__);
3990f2835219SAlan Stern hub_activate(hub, HUB_RESET_RESUME);
3991f07600cfSAlan Stern return 0;
3992f07600cfSAlan Stern }
3993f07600cfSAlan Stern
399454515fe5SAlan Stern /**
399554515fe5SAlan Stern * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
399654515fe5SAlan Stern * @rhdev: struct usb_device for the root hub
399754515fe5SAlan Stern *
399854515fe5SAlan Stern * The USB host controller driver calls this function when its root hub
399954515fe5SAlan Stern * is resumed and Vbus power has been interrupted or the controller
4000feccc30dSAlan Stern * has been reset. The routine marks @rhdev as having lost power.
4001feccc30dSAlan Stern * When the hub driver is resumed it will take notice and carry out
4002feccc30dSAlan Stern * power-session recovery for all the "USB-PERSIST"-enabled child devices;
4003feccc30dSAlan Stern * the others will be disconnected.
400454515fe5SAlan Stern */
usb_root_hub_lost_power(struct usb_device * rhdev)400554515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
400654515fe5SAlan Stern {
40070442d7b0STomeu Vizoso dev_notice(&rhdev->dev, "root hub lost power or was reset\n");
400854515fe5SAlan Stern rhdev->reset_resume = 1;
400954515fe5SAlan Stern }
401054515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
401154515fe5SAlan Stern
40121ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[] = {
40131ea7e0e8SSarah Sharp "U0",
40141ea7e0e8SSarah Sharp "U1",
40151ea7e0e8SSarah Sharp "U2",
40161ea7e0e8SSarah Sharp "U3",
40171ea7e0e8SSarah Sharp };
40181ea7e0e8SSarah Sharp
40191ea7e0e8SSarah Sharp /*
40201ea7e0e8SSarah Sharp * Send a Set SEL control transfer to the device, prior to enabling
40211ea7e0e8SSarah Sharp * device-initiated U1 or U2. This lets the device know the exit latencies from
40221ea7e0e8SSarah Sharp * the time the device initiates a U1 or U2 exit, to the time it will receive a
40231ea7e0e8SSarah Sharp * packet from the host.
40241ea7e0e8SSarah Sharp *
40251ea7e0e8SSarah Sharp * This function will fail if the SEL or PEL values for udev are greater than
40261ea7e0e8SSarah Sharp * the maximum allowed values for the link state to be enabled.
40271ea7e0e8SSarah Sharp */
usb_req_set_sel(struct usb_device * udev)4028e146caf3SMathias Nyman static int usb_req_set_sel(struct usb_device *udev)
40291ea7e0e8SSarah Sharp {
40301ea7e0e8SSarah Sharp struct usb_set_sel_req *sel_values;
40311ea7e0e8SSarah Sharp unsigned long long u1_sel;
40321ea7e0e8SSarah Sharp unsigned long long u1_pel;
40331ea7e0e8SSarah Sharp unsigned long long u2_sel;
40341ea7e0e8SSarah Sharp unsigned long long u2_pel;
40351ea7e0e8SSarah Sharp int ret;
40361ea7e0e8SSarah Sharp
4037e146caf3SMathias Nyman if (!udev->parent || udev->speed < USB_SPEED_SUPER || !udev->lpm_capable)
403838d7f688SXenia Ragiadakou return 0;
403938d7f688SXenia Ragiadakou
40401ea7e0e8SSarah Sharp /* Convert SEL and PEL stored in ns to us */
40411ea7e0e8SSarah Sharp u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
40421ea7e0e8SSarah Sharp u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
40431ea7e0e8SSarah Sharp u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
40441ea7e0e8SSarah Sharp u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
40451ea7e0e8SSarah Sharp
40461ea7e0e8SSarah Sharp /*
40471ea7e0e8SSarah Sharp * Make sure that the calculated SEL and PEL values for the link
40481ea7e0e8SSarah Sharp * state we're enabling aren't bigger than the max SEL/PEL
40491ea7e0e8SSarah Sharp * value that will fit in the SET SEL control transfer.
40501ea7e0e8SSarah Sharp * Otherwise the device would get an incorrect idea of the exit
40511ea7e0e8SSarah Sharp * latency for the link state, and could start a device-initiated
40521ea7e0e8SSarah Sharp * U1/U2 when the exit latencies are too high.
40531ea7e0e8SSarah Sharp */
4054e146caf3SMathias Nyman if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
4055e146caf3SMathias Nyman u1_pel > USB3_LPM_MAX_U1_SEL_PEL ||
4056e146caf3SMathias Nyman u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
4057e146caf3SMathias Nyman u2_pel > USB3_LPM_MAX_U2_SEL_PEL) {
4058e146caf3SMathias Nyman dev_dbg(&udev->dev, "Device-initiated U1/U2 disabled due to long SEL or PEL\n");
40591ea7e0e8SSarah Sharp return -EINVAL;
40601ea7e0e8SSarah Sharp }
40611ea7e0e8SSarah Sharp
40621ea7e0e8SSarah Sharp /*
40631ea7e0e8SSarah Sharp * usb_enable_lpm() can be called as part of a failed device reset,
40641ea7e0e8SSarah Sharp * which may be initiated by an error path of a mass storage driver.
40651ea7e0e8SSarah Sharp * Therefore, use GFP_NOIO.
40661ea7e0e8SSarah Sharp */
40671ea7e0e8SSarah Sharp sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
40681ea7e0e8SSarah Sharp if (!sel_values)
40691ea7e0e8SSarah Sharp return -ENOMEM;
40701ea7e0e8SSarah Sharp
40711ea7e0e8SSarah Sharp sel_values->u1_sel = u1_sel;
40721ea7e0e8SSarah Sharp sel_values->u1_pel = u1_pel;
40731ea7e0e8SSarah Sharp sel_values->u2_sel = cpu_to_le16(u2_sel);
40741ea7e0e8SSarah Sharp sel_values->u2_pel = cpu_to_le16(u2_pel);
40751ea7e0e8SSarah Sharp
40761ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
40771ea7e0e8SSarah Sharp USB_REQ_SET_SEL,
40781ea7e0e8SSarah Sharp USB_RECIP_DEVICE,
40791ea7e0e8SSarah Sharp 0, 0,
40801ea7e0e8SSarah Sharp sel_values, sizeof *(sel_values),
40811ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT);
40821ea7e0e8SSarah Sharp kfree(sel_values);
4083e146caf3SMathias Nyman
4084e146caf3SMathias Nyman if (ret > 0)
4085e146caf3SMathias Nyman udev->lpm_devinit_allow = 1;
4086e146caf3SMathias Nyman
40871ea7e0e8SSarah Sharp return ret;
40881ea7e0e8SSarah Sharp }
40891ea7e0e8SSarah Sharp
40901ea7e0e8SSarah Sharp /*
40911ea7e0e8SSarah Sharp * Enable or disable device-initiated U1 or U2 transitions.
40921ea7e0e8SSarah Sharp */
usb_set_device_initiated_lpm(struct usb_device * udev,enum usb3_link_state state,bool enable)40931ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev,
40941ea7e0e8SSarah Sharp enum usb3_link_state state, bool enable)
40951ea7e0e8SSarah Sharp {
40961ea7e0e8SSarah Sharp int ret;
40971ea7e0e8SSarah Sharp int feature;
40981ea7e0e8SSarah Sharp
40991ea7e0e8SSarah Sharp switch (state) {
41001ea7e0e8SSarah Sharp case USB3_LPM_U1:
41011ea7e0e8SSarah Sharp feature = USB_DEVICE_U1_ENABLE;
41021ea7e0e8SSarah Sharp break;
41031ea7e0e8SSarah Sharp case USB3_LPM_U2:
41041ea7e0e8SSarah Sharp feature = USB_DEVICE_U2_ENABLE;
41051ea7e0e8SSarah Sharp break;
41061ea7e0e8SSarah Sharp default:
41071ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
41081ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable");
41091ea7e0e8SSarah Sharp return -EINVAL;
41101ea7e0e8SSarah Sharp }
41111ea7e0e8SSarah Sharp
41121ea7e0e8SSarah Sharp if (udev->state != USB_STATE_CONFIGURED) {
41131ea7e0e8SSarah Sharp dev_dbg(&udev->dev, "%s: Can't %s %s state "
41141ea7e0e8SSarah Sharp "for unconfigured device.\n",
41151ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable",
41161ea7e0e8SSarah Sharp usb3_lpm_names[state]);
41171ea7e0e8SSarah Sharp return 0;
41181ea7e0e8SSarah Sharp }
41191ea7e0e8SSarah Sharp
41201ea7e0e8SSarah Sharp if (enable) {
41211ea7e0e8SSarah Sharp /*
41221ea7e0e8SSarah Sharp * Now send the control transfer to enable device-initiated LPM
41231ea7e0e8SSarah Sharp * for either U1 or U2.
41241ea7e0e8SSarah Sharp */
41251ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
41261ea7e0e8SSarah Sharp USB_REQ_SET_FEATURE,
41271ea7e0e8SSarah Sharp USB_RECIP_DEVICE,
41281ea7e0e8SSarah Sharp feature,
41291ea7e0e8SSarah Sharp 0, NULL, 0,
41301ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT);
41311ea7e0e8SSarah Sharp } else {
41321ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
41331ea7e0e8SSarah Sharp USB_REQ_CLEAR_FEATURE,
41341ea7e0e8SSarah Sharp USB_RECIP_DEVICE,
41351ea7e0e8SSarah Sharp feature,
41361ea7e0e8SSarah Sharp 0, NULL, 0,
41371ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT);
41381ea7e0e8SSarah Sharp }
41391ea7e0e8SSarah Sharp if (ret < 0) {
41401ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
41411ea7e0e8SSarah Sharp enable ? "Enable" : "Disable",
41421ea7e0e8SSarah Sharp usb3_lpm_names[state]);
41431ea7e0e8SSarah Sharp return -EBUSY;
41441ea7e0e8SSarah Sharp }
41451ea7e0e8SSarah Sharp return 0;
41461ea7e0e8SSarah Sharp }
41471ea7e0e8SSarah Sharp
usb_set_lpm_timeout(struct usb_device * udev,enum usb3_link_state state,int timeout)41481ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev,
41491ea7e0e8SSarah Sharp enum usb3_link_state state, int timeout)
41501ea7e0e8SSarah Sharp {
41511ea7e0e8SSarah Sharp int ret;
41521ea7e0e8SSarah Sharp int feature;
41531ea7e0e8SSarah Sharp
41541ea7e0e8SSarah Sharp switch (state) {
41551ea7e0e8SSarah Sharp case USB3_LPM_U1:
41561ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U1_TIMEOUT;
41571ea7e0e8SSarah Sharp break;
41581ea7e0e8SSarah Sharp case USB3_LPM_U2:
41591ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U2_TIMEOUT;
41601ea7e0e8SSarah Sharp break;
41611ea7e0e8SSarah Sharp default:
41621ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
41631ea7e0e8SSarah Sharp __func__);
41641ea7e0e8SSarah Sharp return -EINVAL;
41651ea7e0e8SSarah Sharp }
41661ea7e0e8SSarah Sharp
41671ea7e0e8SSarah Sharp if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
41681ea7e0e8SSarah Sharp timeout != USB3_LPM_DEVICE_INITIATED) {
41691ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
41701ea7e0e8SSarah Sharp "which is a reserved value.\n",
41711ea7e0e8SSarah Sharp usb3_lpm_names[state], timeout);
41721ea7e0e8SSarah Sharp return -EINVAL;
41731ea7e0e8SSarah Sharp }
41741ea7e0e8SSarah Sharp
41751ea7e0e8SSarah Sharp ret = set_port_feature(udev->parent,
41761ea7e0e8SSarah Sharp USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
41771ea7e0e8SSarah Sharp feature);
41781ea7e0e8SSarah Sharp if (ret < 0) {
41791ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
41801ea7e0e8SSarah Sharp "error code %i\n", usb3_lpm_names[state],
41811ea7e0e8SSarah Sharp timeout, ret);
41821ea7e0e8SSarah Sharp return -EBUSY;
41831ea7e0e8SSarah Sharp }
41841ea7e0e8SSarah Sharp if (state == USB3_LPM_U1)
41851ea7e0e8SSarah Sharp udev->u1_params.timeout = timeout;
41861ea7e0e8SSarah Sharp else
41871ea7e0e8SSarah Sharp udev->u2_params.timeout = timeout;
41881ea7e0e8SSarah Sharp return 0;
41891ea7e0e8SSarah Sharp }
41901ea7e0e8SSarah Sharp
41911ea7e0e8SSarah Sharp /*
41921b7f56fbSMathias Nyman * Don't allow device intiated U1/U2 if the system exit latency + one bus
41931b7f56fbSMathias Nyman * interval is greater than the minimum service interval of any active
41941b7f56fbSMathias Nyman * periodic endpoint. See USB 3.2 section 9.4.9
41951b7f56fbSMathias Nyman */
usb_device_may_initiate_lpm(struct usb_device * udev,enum usb3_link_state state)41961b7f56fbSMathias Nyman static bool usb_device_may_initiate_lpm(struct usb_device *udev,
41971b7f56fbSMathias Nyman enum usb3_link_state state)
41981b7f56fbSMathias Nyman {
41991b7f56fbSMathias Nyman unsigned int sel; /* us */
42001b7f56fbSMathias Nyman int i, j;
42011b7f56fbSMathias Nyman
4202e146caf3SMathias Nyman if (!udev->lpm_devinit_allow)
4203e146caf3SMathias Nyman return false;
4204e146caf3SMathias Nyman
42051b7f56fbSMathias Nyman if (state == USB3_LPM_U1)
42061b7f56fbSMathias Nyman sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
42071b7f56fbSMathias Nyman else if (state == USB3_LPM_U2)
42081b7f56fbSMathias Nyman sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
42091b7f56fbSMathias Nyman else
42101b7f56fbSMathias Nyman return false;
42111b7f56fbSMathias Nyman
42121b7f56fbSMathias Nyman for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
42131b7f56fbSMathias Nyman struct usb_interface *intf;
42141b7f56fbSMathias Nyman struct usb_endpoint_descriptor *desc;
42151b7f56fbSMathias Nyman unsigned int interval;
42161b7f56fbSMathias Nyman
42171b7f56fbSMathias Nyman intf = udev->actconfig->interface[i];
42181b7f56fbSMathias Nyman if (!intf)
42191b7f56fbSMathias Nyman continue;
42201b7f56fbSMathias Nyman
42211b7f56fbSMathias Nyman for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) {
42221b7f56fbSMathias Nyman desc = &intf->cur_altsetting->endpoint[j].desc;
42231b7f56fbSMathias Nyman
42241b7f56fbSMathias Nyman if (usb_endpoint_xfer_int(desc) ||
42251b7f56fbSMathias Nyman usb_endpoint_xfer_isoc(desc)) {
42261b7f56fbSMathias Nyman interval = (1 << (desc->bInterval - 1)) * 125;
42271b7f56fbSMathias Nyman if (sel + 125 > interval)
42281b7f56fbSMathias Nyman return false;
42291b7f56fbSMathias Nyman }
42301b7f56fbSMathias Nyman }
42311b7f56fbSMathias Nyman }
42321b7f56fbSMathias Nyman return true;
42331b7f56fbSMathias Nyman }
42341b7f56fbSMathias Nyman
42351b7f56fbSMathias Nyman /*
42361ea7e0e8SSarah Sharp * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
42371ea7e0e8SSarah Sharp * U1/U2 entry.
42381ea7e0e8SSarah Sharp *
42391ea7e0e8SSarah Sharp * We will attempt to enable U1 or U2, but there are no guarantees that the
42401ea7e0e8SSarah Sharp * control transfers to set the hub timeout or enable device-initiated U1/U2
42411ea7e0e8SSarah Sharp * will be successful.
42421ea7e0e8SSarah Sharp *
424356175929SThinh Nguyen * If the control transfer to enable device-initiated U1/U2 entry fails, then
424456175929SThinh Nguyen * hub-initiated U1/U2 will be disabled.
424556175929SThinh Nguyen *
42461ea7e0e8SSarah Sharp * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
42471ea7e0e8SSarah Sharp * driver know about it. If that call fails, it should be harmless, and just
42481ea7e0e8SSarah Sharp * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
42491ea7e0e8SSarah Sharp */
usb_enable_link_state(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)42501ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
42511ea7e0e8SSarah Sharp enum usb3_link_state state)
42521ea7e0e8SSarah Sharp {
4253e146caf3SMathias Nyman int timeout;
4254f74a7afcSRicardo Cañuelo __u8 u1_mel;
4255f74a7afcSRicardo Cañuelo __le16 u2_mel;
4256f74a7afcSRicardo Cañuelo
4257f74a7afcSRicardo Cañuelo /* Skip if the device BOS descriptor couldn't be read */
4258f74a7afcSRicardo Cañuelo if (!udev->bos)
4259f74a7afcSRicardo Cañuelo return;
4260f74a7afcSRicardo Cañuelo
4261f74a7afcSRicardo Cañuelo u1_mel = udev->bos->ss_cap->bU1devExitLat;
4262f74a7afcSRicardo Cañuelo u2_mel = udev->bos->ss_cap->bU2DevExitLat;
4263ae8963adSSarah Sharp
4264ae8963adSSarah Sharp /* If the device says it doesn't have *any* exit latency to come out of
4265ae8963adSSarah Sharp * U1 or U2, it's probably lying. Assume it doesn't implement that link
4266ae8963adSSarah Sharp * state.
4267ae8963adSSarah Sharp */
4268ae8963adSSarah Sharp if ((state == USB3_LPM_U1 && u1_mel == 0) ||
4269ae8963adSSarah Sharp (state == USB3_LPM_U2 && u2_mel == 0))
4270ae8963adSSarah Sharp return;
42711ea7e0e8SSarah Sharp
42721ea7e0e8SSarah Sharp /* We allow the host controller to set the U1/U2 timeout internally
42731ea7e0e8SSarah Sharp * first, so that it can change its schedule to account for the
42741ea7e0e8SSarah Sharp * additional latency to send data to a device in a lower power
42751ea7e0e8SSarah Sharp * link state.
42761ea7e0e8SSarah Sharp */
42771ea7e0e8SSarah Sharp timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
42781ea7e0e8SSarah Sharp
42791ea7e0e8SSarah Sharp /* xHCI host controller doesn't want to enable this LPM state. */
42801ea7e0e8SSarah Sharp if (timeout == 0)
42811ea7e0e8SSarah Sharp return;
42821ea7e0e8SSarah Sharp
42831ea7e0e8SSarah Sharp if (timeout < 0) {
42841ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not enable %s link state, "
42851ea7e0e8SSarah Sharp "xHCI error %i.\n", usb3_lpm_names[state],
42861ea7e0e8SSarah Sharp timeout);
42871ea7e0e8SSarah Sharp return;
42881ea7e0e8SSarah Sharp }
42891ea7e0e8SSarah Sharp
4290bf5ce5bfSLu Baolu if (usb_set_lpm_timeout(udev, state, timeout)) {
42911ea7e0e8SSarah Sharp /* If we can't set the parent hub U1/U2 timeout,
42921ea7e0e8SSarah Sharp * device-initiated LPM won't be allowed either, so let the xHCI
42931ea7e0e8SSarah Sharp * host know that this link state won't be enabled.
42941ea7e0e8SSarah Sharp */
42951ea7e0e8SSarah Sharp hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
429656175929SThinh Nguyen return;
429756175929SThinh Nguyen }
429856175929SThinh Nguyen
4299bf5ce5bfSLu Baolu /* Only a configured device will accept the Set Feature
4300bf5ce5bfSLu Baolu * U1/U2_ENABLE
4301bf5ce5bfSLu Baolu */
430256175929SThinh Nguyen if (udev->actconfig &&
43031b7f56fbSMathias Nyman usb_device_may_initiate_lpm(udev, state)) {
43041b7f56fbSMathias Nyman if (usb_set_device_initiated_lpm(udev, state, true)) {
43051b7f56fbSMathias Nyman /*
43061b7f56fbSMathias Nyman * Request to enable device initiated U1/U2 failed,
43071b7f56fbSMathias Nyman * better to turn off lpm in this case.
43081b7f56fbSMathias Nyman */
43091b7f56fbSMathias Nyman usb_set_lpm_timeout(udev, state, 0);
43101b7f56fbSMathias Nyman hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
43111b7f56fbSMathias Nyman return;
43121b7f56fbSMathias Nyman }
43131b7f56fbSMathias Nyman }
43141b7f56fbSMathias Nyman
4315bf5ce5bfSLu Baolu if (state == USB3_LPM_U1)
4316bf5ce5bfSLu Baolu udev->usb3_lpm_u1_enabled = 1;
4317bf5ce5bfSLu Baolu else if (state == USB3_LPM_U2)
4318bf5ce5bfSLu Baolu udev->usb3_lpm_u2_enabled = 1;
4319bf5ce5bfSLu Baolu }
43201ea7e0e8SSarah Sharp /*
43211ea7e0e8SSarah Sharp * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
43221ea7e0e8SSarah Sharp * U1/U2 entry.
43231ea7e0e8SSarah Sharp *
43241ea7e0e8SSarah Sharp * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
43251ea7e0e8SSarah Sharp * If zero is returned, the parent will not allow the link to go into U1/U2.
43261ea7e0e8SSarah Sharp *
43271ea7e0e8SSarah Sharp * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
43281ea7e0e8SSarah Sharp * it won't have an effect on the bus link state because the parent hub will
43291ea7e0e8SSarah Sharp * still disallow device-initiated U1/U2 entry.
43301ea7e0e8SSarah Sharp *
43311ea7e0e8SSarah Sharp * If zero is returned, the xHCI host controller may still think U1/U2 entry is
43321ea7e0e8SSarah Sharp * possible. The result will be slightly more bus bandwidth will be taken up
43331ea7e0e8SSarah Sharp * (to account for U1/U2 exit latency), but it should be harmless.
43341ea7e0e8SSarah Sharp */
usb_disable_link_state(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)43351ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
43361ea7e0e8SSarah Sharp enum usb3_link_state state)
43371ea7e0e8SSarah Sharp {
43381ea7e0e8SSarah Sharp switch (state) {
43391ea7e0e8SSarah Sharp case USB3_LPM_U1:
43401ea7e0e8SSarah Sharp case USB3_LPM_U2:
43411ea7e0e8SSarah Sharp break;
43421ea7e0e8SSarah Sharp default:
43431ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
43441ea7e0e8SSarah Sharp __func__);
43451ea7e0e8SSarah Sharp return -EINVAL;
43461ea7e0e8SSarah Sharp }
43471ea7e0e8SSarah Sharp
43481ea7e0e8SSarah Sharp if (usb_set_lpm_timeout(udev, state, 0))
43491ea7e0e8SSarah Sharp return -EBUSY;
43501ea7e0e8SSarah Sharp
43511ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, false);
43521ea7e0e8SSarah Sharp
43531ea7e0e8SSarah Sharp if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
43541ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
43551ea7e0e8SSarah Sharp "bus schedule bandwidth may be impacted.\n",
43561ea7e0e8SSarah Sharp usb3_lpm_names[state]);
4357bf5ce5bfSLu Baolu
4358bf5ce5bfSLu Baolu /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
4359bf5ce5bfSLu Baolu * is disabled. Hub will disallows link to enter U1/U2 as well,
4360bf5ce5bfSLu Baolu * even device is initiating LPM. Hence LPM is disabled if hub LPM
4361bf5ce5bfSLu Baolu * timeout set to 0, no matter device-initiated LPM is disabled or
4362bf5ce5bfSLu Baolu * not.
4363bf5ce5bfSLu Baolu */
4364bf5ce5bfSLu Baolu if (state == USB3_LPM_U1)
4365bf5ce5bfSLu Baolu udev->usb3_lpm_u1_enabled = 0;
4366bf5ce5bfSLu Baolu else if (state == USB3_LPM_U2)
4367bf5ce5bfSLu Baolu udev->usb3_lpm_u2_enabled = 0;
4368bf5ce5bfSLu Baolu
43691ea7e0e8SSarah Sharp return 0;
43701ea7e0e8SSarah Sharp }
43711ea7e0e8SSarah Sharp
43721ea7e0e8SSarah Sharp /*
43731ea7e0e8SSarah Sharp * Disable hub-initiated and device-initiated U1 and U2 entry.
43741ea7e0e8SSarah Sharp * Caller must own the bandwidth_mutex.
43751ea7e0e8SSarah Sharp *
43761ea7e0e8SSarah Sharp * This will call usb_enable_lpm() on failure, which will decrement
43771ea7e0e8SSarah Sharp * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
43781ea7e0e8SSarah Sharp */
usb_disable_lpm(struct usb_device * udev)43791ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
43801ea7e0e8SSarah Sharp {
43811ea7e0e8SSarah Sharp struct usb_hcd *hcd;
43821ea7e0e8SSarah Sharp
43831ea7e0e8SSarah Sharp if (!udev || !udev->parent ||
43848a1b2725SMathias Nyman udev->speed < USB_SPEED_SUPER ||
438551df62ffSPratyush Anand !udev->lpm_capable ||
4386fea3af5eSThinh Nguyen udev->state < USB_STATE_CONFIGURED)
43871ea7e0e8SSarah Sharp return 0;
43881ea7e0e8SSarah Sharp
43891ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus);
43901ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
43911ea7e0e8SSarah Sharp return 0;
43921ea7e0e8SSarah Sharp
43931ea7e0e8SSarah Sharp udev->lpm_disable_count++;
439455558c33SDan Carpenter if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
43951ea7e0e8SSarah Sharp return 0;
43961ea7e0e8SSarah Sharp
43971ea7e0e8SSarah Sharp /* If LPM is enabled, attempt to disable it. */
43981ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
43991ea7e0e8SSarah Sharp goto enable_lpm;
44001ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
44011ea7e0e8SSarah Sharp goto enable_lpm;
44021ea7e0e8SSarah Sharp
44031ea7e0e8SSarah Sharp return 0;
44041ea7e0e8SSarah Sharp
44051ea7e0e8SSarah Sharp enable_lpm:
44061ea7e0e8SSarah Sharp usb_enable_lpm(udev);
44071ea7e0e8SSarah Sharp return -EBUSY;
44081ea7e0e8SSarah Sharp }
44091ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
44101ea7e0e8SSarah Sharp
44111ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
usb_unlocked_disable_lpm(struct usb_device * udev)44121ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
44131ea7e0e8SSarah Sharp {
44141ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus);
44151ea7e0e8SSarah Sharp int ret;
44161ea7e0e8SSarah Sharp
44171ea7e0e8SSarah Sharp if (!hcd)
44181ea7e0e8SSarah Sharp return -EINVAL;
44191ea7e0e8SSarah Sharp
44201ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex);
44211ea7e0e8SSarah Sharp ret = usb_disable_lpm(udev);
44221ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex);
44231ea7e0e8SSarah Sharp
44241ea7e0e8SSarah Sharp return ret;
44251ea7e0e8SSarah Sharp }
44261ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
44271ea7e0e8SSarah Sharp
44281ea7e0e8SSarah Sharp /*
44291ea7e0e8SSarah Sharp * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
44301ea7e0e8SSarah Sharp * xHCI host policy may prevent U1 or U2 from being enabled.
44311ea7e0e8SSarah Sharp *
44321ea7e0e8SSarah Sharp * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
44331ea7e0e8SSarah Sharp * until the lpm_disable_count drops to zero. Caller must own the
44341ea7e0e8SSarah Sharp * bandwidth_mutex.
44351ea7e0e8SSarah Sharp */
usb_enable_lpm(struct usb_device * udev)44361ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev)
44371ea7e0e8SSarah Sharp {
44381ea7e0e8SSarah Sharp struct usb_hcd *hcd;
4439513072d9SLu Baolu struct usb_hub *hub;
4440513072d9SLu Baolu struct usb_port *port_dev;
44411ea7e0e8SSarah Sharp
44421ea7e0e8SSarah Sharp if (!udev || !udev->parent ||
44438a1b2725SMathias Nyman udev->speed < USB_SPEED_SUPER ||
444451df62ffSPratyush Anand !udev->lpm_capable ||
4445fea3af5eSThinh Nguyen udev->state < USB_STATE_CONFIGURED)
44461ea7e0e8SSarah Sharp return;
44471ea7e0e8SSarah Sharp
44481ea7e0e8SSarah Sharp udev->lpm_disable_count--;
44491ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus);
44501ea7e0e8SSarah Sharp /* Double check that we can both enable and disable LPM.
44511ea7e0e8SSarah Sharp * Device must be configured to accept set feature U1/U2 timeout.
44521ea7e0e8SSarah Sharp */
44531ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
44541ea7e0e8SSarah Sharp !hcd->driver->disable_usb3_lpm_timeout)
44551ea7e0e8SSarah Sharp return;
44561ea7e0e8SSarah Sharp
44571ea7e0e8SSarah Sharp if (udev->lpm_disable_count > 0)
44581ea7e0e8SSarah Sharp return;
44591ea7e0e8SSarah Sharp
4460513072d9SLu Baolu hub = usb_hub_to_struct_hub(udev->parent);
4461513072d9SLu Baolu if (!hub)
4462513072d9SLu Baolu return;
4463513072d9SLu Baolu
4464513072d9SLu Baolu port_dev = hub->ports[udev->portnum - 1];
4465513072d9SLu Baolu
4466513072d9SLu Baolu if (port_dev->usb3_lpm_u1_permit)
44671ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4468513072d9SLu Baolu
4469513072d9SLu Baolu if (port_dev->usb3_lpm_u2_permit)
44701ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U2);
44711ea7e0e8SSarah Sharp }
44721ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
44731ea7e0e8SSarah Sharp
44741ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
usb_unlocked_enable_lpm(struct usb_device * udev)44751ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev)
44761ea7e0e8SSarah Sharp {
44771ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus);
44781ea7e0e8SSarah Sharp
44791ea7e0e8SSarah Sharp if (!hcd)
44801ea7e0e8SSarah Sharp return;
44811ea7e0e8SSarah Sharp
44821ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex);
44831ea7e0e8SSarah Sharp usb_enable_lpm(udev);
44841ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex);
44851ea7e0e8SSarah Sharp }
44861ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
44871ea7e0e8SSarah Sharp
448837be6676SMathias Nyman /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
hub_usb3_port_prepare_disable(struct usb_hub * hub,struct usb_port * port_dev)448937be6676SMathias Nyman static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
449037be6676SMathias Nyman struct usb_port *port_dev)
449137be6676SMathias Nyman {
449237be6676SMathias Nyman struct usb_device *udev = port_dev->child;
449337be6676SMathias Nyman int ret;
449437be6676SMathias Nyman
449537be6676SMathias Nyman if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
449637be6676SMathias Nyman ret = hub_set_port_link_state(hub, port_dev->portnum,
449737be6676SMathias Nyman USB_SS_PORT_LS_U0);
449837be6676SMathias Nyman if (!ret) {
449937be6676SMathias Nyman msleep(USB_RESUME_TIMEOUT);
450037be6676SMathias Nyman ret = usb_disable_remote_wakeup(udev);
450137be6676SMathias Nyman }
450237be6676SMathias Nyman if (ret)
450337be6676SMathias Nyman dev_warn(&udev->dev,
450437be6676SMathias Nyman "Port disable: can't disable remote wake\n");
450537be6676SMathias Nyman udev->do_remote_wakeup = 0;
450637be6676SMathias Nyman }
450737be6676SMathias Nyman }
45081ea7e0e8SSarah Sharp
4509d388dab7SAlan Stern #else /* CONFIG_PM */
4510d388dab7SAlan Stern
4511511366daSAndrew Morton #define hub_suspend NULL
4512511366daSAndrew Morton #define hub_resume NULL
4513f07600cfSAlan Stern #define hub_reset_resume NULL
45141ea7e0e8SSarah Sharp
hub_usb3_port_prepare_disable(struct usb_hub * hub,struct usb_port * port_dev)451537be6676SMathias Nyman static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
451637be6676SMathias Nyman struct usb_port *port_dev) { }
451737be6676SMathias Nyman
usb_disable_lpm(struct usb_device * udev)45181ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
45191ea7e0e8SSarah Sharp {
45201ea7e0e8SSarah Sharp return 0;
45211ea7e0e8SSarah Sharp }
4522e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
45231ea7e0e8SSarah Sharp
usb_enable_lpm(struct usb_device * udev)45241ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { }
4525e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
45261ea7e0e8SSarah Sharp
usb_unlocked_disable_lpm(struct usb_device * udev)45271ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
45281ea7e0e8SSarah Sharp {
45291ea7e0e8SSarah Sharp return 0;
45301ea7e0e8SSarah Sharp }
4531e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
45321ea7e0e8SSarah Sharp
usb_unlocked_enable_lpm(struct usb_device * udev)45331ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { }
4534e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4535f74631e3SSarah Sharp
usb_disable_ltm(struct usb_device * udev)4536f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
4537f74631e3SSarah Sharp {
4538f74631e3SSarah Sharp return 0;
4539f74631e3SSarah Sharp }
4540f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
4541f74631e3SSarah Sharp
usb_enable_ltm(struct usb_device * udev)4542f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) { }
4543f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
4544c4b51a43SAlan Stern
hub_handle_remote_wakeup(struct usb_hub * hub,unsigned int port,u16 portstatus,u16 portchange)45454a95b1fcSStephen Rothwell static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
45464a95b1fcSStephen Rothwell u16 portstatus, u16 portchange)
45474a95b1fcSStephen Rothwell {
45484a95b1fcSStephen Rothwell return 0;
45494a95b1fcSStephen Rothwell }
45504a95b1fcSStephen Rothwell
usb_req_set_sel(struct usb_device * udev)4551e146caf3SMathias Nyman static int usb_req_set_sel(struct usb_device *udev)
4552e146caf3SMathias Nyman {
4553e146caf3SMathias Nyman return 0;
4554e146caf3SMathias Nyman }
4555e146caf3SMathias Nyman
4556c4b51a43SAlan Stern #endif /* CONFIG_PM */
4557d388dab7SAlan Stern
45583bc02bceSGeert Uytterhoeven /*
45593bc02bceSGeert Uytterhoeven * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
45603bc02bceSGeert Uytterhoeven * a connection with a plugged-in cable but will signal the host when the cable
45613bc02bceSGeert Uytterhoeven * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
45623bc02bceSGeert Uytterhoeven */
hub_port_disable(struct usb_hub * hub,int port1,int set_state)45633bc02bceSGeert Uytterhoeven static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
45643bc02bceSGeert Uytterhoeven {
45653bc02bceSGeert Uytterhoeven struct usb_port *port_dev = hub->ports[port1 - 1];
45663bc02bceSGeert Uytterhoeven struct usb_device *hdev = hub->hdev;
45673bc02bceSGeert Uytterhoeven int ret = 0;
45683bc02bceSGeert Uytterhoeven
45693bc02bceSGeert Uytterhoeven if (!hub->error) {
45703bc02bceSGeert Uytterhoeven if (hub_is_superspeed(hub->hdev)) {
45713bc02bceSGeert Uytterhoeven hub_usb3_port_prepare_disable(hub, port_dev);
45723bc02bceSGeert Uytterhoeven ret = hub_set_port_link_state(hub, port_dev->portnum,
45733bc02bceSGeert Uytterhoeven USB_SS_PORT_LS_U3);
45743bc02bceSGeert Uytterhoeven } else {
45753bc02bceSGeert Uytterhoeven ret = usb_clear_port_feature(hdev, port1,
45763bc02bceSGeert Uytterhoeven USB_PORT_FEAT_ENABLE);
45773bc02bceSGeert Uytterhoeven }
45783bc02bceSGeert Uytterhoeven }
45793bc02bceSGeert Uytterhoeven if (port_dev->child && set_state)
45803bc02bceSGeert Uytterhoeven usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
45813bc02bceSGeert Uytterhoeven if (ret && ret != -ENODEV)
45823bc02bceSGeert Uytterhoeven dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
45833bc02bceSGeert Uytterhoeven return ret;
45843bc02bceSGeert Uytterhoeven }
45853bc02bceSGeert Uytterhoeven
45868dd8d2c9SDaniel Drake /*
45878dd8d2c9SDaniel Drake * usb_port_disable - disable a usb device's upstream port
45888dd8d2c9SDaniel Drake * @udev: device to disable
45898dd8d2c9SDaniel Drake * Context: @udev locked, must be able to sleep.
45908dd8d2c9SDaniel Drake *
45918dd8d2c9SDaniel Drake * Disables a USB device that isn't in active use.
45928dd8d2c9SDaniel Drake */
usb_port_disable(struct usb_device * udev)45938dd8d2c9SDaniel Drake int usb_port_disable(struct usb_device *udev)
45948dd8d2c9SDaniel Drake {
45958dd8d2c9SDaniel Drake struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
45968dd8d2c9SDaniel Drake
45978dd8d2c9SDaniel Drake return hub_port_disable(hub, udev->portnum, 0);
45988dd8d2c9SDaniel Drake }
45991da177e4SLinus Torvalds
46001da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
46011da177e4SLinus Torvalds *
46021da177e4SLinus Torvalds * Between connect detection and reset signaling there must be a delay
46031da177e4SLinus Torvalds * of 100ms at least for debounce and power-settling. The corresponding
46041da177e4SLinus Torvalds * timer shall restart whenever the downstream port detects a disconnect.
46051da177e4SLinus Torvalds *
46061da177e4SLinus Torvalds * Apparently there are some bluetooth and irda-dongles and a number of
46071da177e4SLinus Torvalds * low-speed devices for which this debounce period may last over a second.
46081da177e4SLinus Torvalds * Not covered by the spec - but easy to deal with.
46091da177e4SLinus Torvalds *
46101da177e4SLinus Torvalds * This implementation uses a 1500ms total debounce timeout; if the
46111da177e4SLinus Torvalds * connection isn't stable by then it returns -ETIMEDOUT. It checks
46121da177e4SLinus Torvalds * every 25ms for transient disconnects. When the port status has been
46131da177e4SLinus Torvalds * unchanged for 100ms it returns the port status.
46141da177e4SLinus Torvalds */
hub_port_debounce(struct usb_hub * hub,int port1,bool must_be_connected)4615ad493e5eSLan Tianyu int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
46161da177e4SLinus Torvalds {
46171da177e4SLinus Torvalds int ret;
46181da177e4SLinus Torvalds u16 portchange, portstatus;
46191da177e4SLinus Torvalds unsigned connection = 0xffff;
4620d99f6b41SDan Williams int total_time, stable_time = 0;
4621d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
46221da177e4SLinus Torvalds
46231da177e4SLinus Torvalds for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4624f061f43dSMichael Grzeschik ret = usb_hub_port_status(hub, port1, &portstatus, &portchange);
46251da177e4SLinus Torvalds if (ret < 0)
46261da177e4SLinus Torvalds return ret;
46271da177e4SLinus Torvalds
46281da177e4SLinus Torvalds if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
46291da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
4630ad493e5eSLan Tianyu if (!must_be_connected ||
4631ad493e5eSLan Tianyu (connection == USB_PORT_STAT_CONNECTION))
46321da177e4SLinus Torvalds stable_time += HUB_DEBOUNCE_STEP;
46331da177e4SLinus Torvalds if (stable_time >= HUB_DEBOUNCE_STABLE)
46341da177e4SLinus Torvalds break;
46351da177e4SLinus Torvalds } else {
46361da177e4SLinus Torvalds stable_time = 0;
46371da177e4SLinus Torvalds connection = portstatus & USB_PORT_STAT_CONNECTION;
46381da177e4SLinus Torvalds }
46391da177e4SLinus Torvalds
46401da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) {
4641ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1,
46421da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION);
46431da177e4SLinus Torvalds }
46441da177e4SLinus Torvalds
46451da177e4SLinus Torvalds if (total_time >= HUB_DEBOUNCE_TIMEOUT)
46461da177e4SLinus Torvalds break;
46471da177e4SLinus Torvalds msleep(HUB_DEBOUNCE_STEP);
46481da177e4SLinus Torvalds }
46491da177e4SLinus Torvalds
4650d99f6b41SDan Williams dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4651d99f6b41SDan Williams total_time, stable_time, portstatus);
46521da177e4SLinus Torvalds
46531da177e4SLinus Torvalds if (stable_time < HUB_DEBOUNCE_STABLE)
46541da177e4SLinus Torvalds return -ETIMEDOUT;
46551da177e4SLinus Torvalds return portstatus;
46561da177e4SLinus Torvalds }
46571da177e4SLinus Torvalds
usb_ep0_reinit(struct usb_device * udev)4658fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
46591da177e4SLinus Torvalds {
4660ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4661ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
46622caf7fcdSAlan Stern usb_enable_endpoint(udev, &udev->ep0, true);
46631da177e4SLinus Torvalds }
4664fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
46651da177e4SLinus Torvalds
46661da177e4SLinus Torvalds #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
46671da177e4SLinus Torvalds #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
46681da177e4SLinus Torvalds
hub_set_address(struct usb_device * udev,int devnum)46694326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
46701da177e4SLinus Torvalds {
46711da177e4SLinus Torvalds int retval;
46723adcbec4SHardik Gajjar unsigned int timeout_ms = USB_CTRL_SET_TIMEOUT;
4673c6515272SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus);
46743adcbec4SHardik Gajjar struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
46753adcbec4SHardik Gajjar
46763adcbec4SHardik Gajjar if (hub->hdev->quirks & USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT)
46773adcbec4SHardik Gajjar timeout_ms = USB_SHORT_SET_ADDRESS_REQ_TIMEOUT;
46781da177e4SLinus Torvalds
4679c6515272SSarah Sharp /*
4680c6515272SSarah Sharp * The host controller will choose the device address,
4681c6515272SSarah Sharp * instead of the core having chosen it earlier
4682c6515272SSarah Sharp */
4683c6515272SSarah Sharp if (!hcd->driver->address_device && devnum <= 1)
46841da177e4SLinus Torvalds return -EINVAL;
46851da177e4SLinus Torvalds if (udev->state == USB_STATE_ADDRESS)
46861da177e4SLinus Torvalds return 0;
46871da177e4SLinus Torvalds if (udev->state != USB_STATE_DEFAULT)
46881da177e4SLinus Torvalds return -EINVAL;
4689c8d4af8eSAndiry Xu if (hcd->driver->address_device)
46903adcbec4SHardik Gajjar retval = hcd->driver->address_device(hcd, udev, timeout_ms);
4691c8d4af8eSAndiry Xu else
46921da177e4SLinus Torvalds retval = usb_control_msg(udev, usb_sndaddr0pipe(),
46934326ed0bSAlan Stern USB_REQ_SET_ADDRESS, 0, devnum, 0,
46943adcbec4SHardik Gajjar NULL, 0, timeout_ms);
46951da177e4SLinus Torvalds if (retval == 0) {
46963b29b68bSAlan Stern update_devnum(udev, devnum);
46974953d141SDavid Vrabel /* Device now using proper address. */
46981da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_ADDRESS);
4699fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev);
47001da177e4SLinus Torvalds }
47011da177e4SLinus Torvalds return retval;
47021da177e4SLinus Torvalds }
47031da177e4SLinus Torvalds
4704890dae88SMathias Nyman /*
4705890dae88SMathias Nyman * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4706890dae88SMathias Nyman * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4707890dae88SMathias Nyman * enabled.
4708890dae88SMathias Nyman *
4709890dae88SMathias Nyman * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4710890dae88SMathias Nyman * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4711890dae88SMathias Nyman * support bit in the BOS descriptor.
4712890dae88SMathias Nyman */
hub_set_initial_usb2_lpm_policy(struct usb_device * udev)4713890dae88SMathias Nyman static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4714890dae88SMathias Nyman {
4715d99f6b41SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4716d99f6b41SDan Williams int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
4717890dae88SMathias Nyman
47187b2db29fSGuenter Roeck if (!udev->usb2_hw_lpm_capable || !udev->bos)
4719890dae88SMathias Nyman return;
4720890dae88SMathias Nyman
4721d99f6b41SDan Williams if (hub)
4722d99f6b41SDan Williams connect_type = hub->ports[udev->portnum - 1]->connect_type;
4723890dae88SMathias Nyman
472423c05820SBjørn Mork if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
4725890dae88SMathias Nyman connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4726890dae88SMathias Nyman udev->usb2_hw_lpm_allowed = 1;
47277529b257SKai-Heng Feng usb_enable_usb2_hardware_lpm(udev);
4728890dae88SMathias Nyman }
4729890dae88SMathias Nyman }
4730890dae88SMathias Nyman
hub_enable_device(struct usb_device * udev)473148fc7dbdSDan Williams static int hub_enable_device(struct usb_device *udev)
473248fc7dbdSDan Williams {
473348fc7dbdSDan Williams struct usb_hcd *hcd = bus_to_hcd(udev->bus);
473448fc7dbdSDan Williams
473548fc7dbdSDan Williams if (!hcd->driver->enable_device)
473648fc7dbdSDan Williams return 0;
473748fc7dbdSDan Williams if (udev->state == USB_STATE_ADDRESS)
473848fc7dbdSDan Williams return 0;
473948fc7dbdSDan Williams if (udev->state != USB_STATE_DEFAULT)
474048fc7dbdSDan Williams return -EINVAL;
474148fc7dbdSDan Williams
474248fc7dbdSDan Williams return hcd->driver->enable_device(hcd, udev);
474348fc7dbdSDan Williams }
474448fc7dbdSDan Williams
474585d07c55SAlan Stern /*
474685d07c55SAlan Stern * Get the bMaxPacketSize0 value during initialization by reading the
474785d07c55SAlan Stern * device's device descriptor. Since we don't already know this value,
474885d07c55SAlan Stern * the transfer is unsafe and it ignores I/O errors, only testing for
474985d07c55SAlan Stern * reasonable received values.
475085d07c55SAlan Stern *
475185d07c55SAlan Stern * For "old scheme" initialization, size will be 8 so we read just the
475285d07c55SAlan Stern * start of the device descriptor, which should work okay regardless of
475385d07c55SAlan Stern * the actual bMaxPacketSize0 value. For "new scheme" initialization,
475485d07c55SAlan Stern * size will be 64 (and buf will point to a sufficiently large buffer),
475585d07c55SAlan Stern * which might not be kosher according to the USB spec but it's what
475685d07c55SAlan Stern * Windows does and what many devices expect.
475785d07c55SAlan Stern *
475885d07c55SAlan Stern * Returns: bMaxPacketSize0 or a negative error code.
475985d07c55SAlan Stern */
get_bMaxPacketSize0(struct usb_device * udev,struct usb_device_descriptor * buf,int size,bool first_time)476085d07c55SAlan Stern static int get_bMaxPacketSize0(struct usb_device *udev,
476185d07c55SAlan Stern struct usb_device_descriptor *buf, int size, bool first_time)
476285d07c55SAlan Stern {
476385d07c55SAlan Stern int i, rc;
476485d07c55SAlan Stern
476585d07c55SAlan Stern /*
476685d07c55SAlan Stern * Retry on all errors; some devices are flakey.
476785d07c55SAlan Stern * 255 is for WUSB devices, we actually need to use
476885d07c55SAlan Stern * 512 (WUSB1.0[4.8.1]).
476985d07c55SAlan Stern */
477085d07c55SAlan Stern for (i = 0; i < GET_MAXPACKET0_TRIES; ++i) {
477185d07c55SAlan Stern /* Start with invalid values in case the transfer fails */
477285d07c55SAlan Stern buf->bDescriptorType = buf->bMaxPacketSize0 = 0;
477385d07c55SAlan Stern rc = usb_control_msg(udev, usb_rcvaddr0pipe(),
477485d07c55SAlan Stern USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
477585d07c55SAlan Stern USB_DT_DEVICE << 8, 0,
477685d07c55SAlan Stern buf, size,
477785d07c55SAlan Stern initial_descriptor_timeout);
477885d07c55SAlan Stern switch (buf->bMaxPacketSize0) {
477959cf4457SAlan Stern case 8: case 16: case 32: case 64: case 9:
478085d07c55SAlan Stern if (buf->bDescriptorType == USB_DT_DEVICE) {
478185d07c55SAlan Stern rc = buf->bMaxPacketSize0;
478285d07c55SAlan Stern break;
478385d07c55SAlan Stern }
478485d07c55SAlan Stern fallthrough;
478585d07c55SAlan Stern default:
478685d07c55SAlan Stern if (rc >= 0)
478785d07c55SAlan Stern rc = -EPROTO;
478885d07c55SAlan Stern break;
478985d07c55SAlan Stern }
479085d07c55SAlan Stern
479185d07c55SAlan Stern /*
479285d07c55SAlan Stern * Some devices time out if they are powered on
479385d07c55SAlan Stern * when already connected. They need a second
479485d07c55SAlan Stern * reset, so return early. But only on the first
479585d07c55SAlan Stern * attempt, lest we get into a time-out/reset loop.
479685d07c55SAlan Stern */
479785d07c55SAlan Stern if (rc > 0 || (rc == -ETIMEDOUT && first_time &&
479885d07c55SAlan Stern udev->speed > USB_SPEED_FULL))
479985d07c55SAlan Stern break;
480085d07c55SAlan Stern }
480185d07c55SAlan Stern return rc;
480285d07c55SAlan Stern }
480385d07c55SAlan Stern
480485d07c55SAlan Stern #define GET_DESCRIPTOR_BUFSIZE 64
480585d07c55SAlan Stern
48061da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
48071da177e4SLinus Torvalds * Device connection must be stable, no more debouncing needed.
48081da177e4SLinus Torvalds * Returns device in USB_STATE_ADDRESS, except on error.
48091da177e4SLinus Torvalds *
48101da177e4SLinus Torvalds * If this is called for an already-existing device (as part of
48115c79a1e3SDan Williams * usb_reset_and_verify_device), the caller must own the device lock and
48125c79a1e3SDan Williams * the port lock. For a newly detected device that is not accessible
48135c79a1e3SDan Williams * through any global pointers, it's not necessary to lock the device,
48145c79a1e3SDan Williams * but it is still necessary to lock the port.
4815ff33299eSAlan Stern *
4816ff33299eSAlan Stern * For a newly detected device, @dev_descr must be NULL. The device
4817ff33299eSAlan Stern * descriptor retrieved from the device will then be stored in
4818ff33299eSAlan Stern * @udev->descriptor. For an already existing device, @dev_descr
4819ff33299eSAlan Stern * must be non-NULL. The device descriptor will be stored there,
4820ff33299eSAlan Stern * not in @udev->descriptor, because descriptors for registered
4821ff33299eSAlan Stern * devices are meant to be immutable.
48221da177e4SLinus Torvalds */
48231da177e4SLinus Torvalds static int
hub_port_init(struct usb_hub * hub,struct usb_device * udev,int port1,int retry_counter,struct usb_device_descriptor * dev_descr)48241da177e4SLinus Torvalds hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
4825ff33299eSAlan Stern int retry_counter, struct usb_device_descriptor *dev_descr)
48261da177e4SLinus Torvalds {
48271da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
4828e7b77172SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
482925244227SNicolas Boichat struct usb_port *port_dev = hub->ports[port1 - 1];
48300d5ce778SOliver Neukum int retries, operations, retval, i;
48311da177e4SLinus Torvalds unsigned delay = HUB_SHORT_RESET_TIME;
48321da177e4SLinus Torvalds enum usb_device_speed oldspeed = udev->speed;
4833e538dfdaSMichal Nazarewicz const char *speed;
48344326ed0bSAlan Stern int devnum = udev->devnum;
48351bb90cf0SPeter Chen const char *driver_name;
483619502e69SAlan Stern bool do_new_scheme;
4837ff33299eSAlan Stern const bool initial = !dev_descr;
483885d07c55SAlan Stern int maxp0;
4839de28e469SAlan Stern struct usb_device_descriptor *buf, *descr;
484085d07c55SAlan Stern
484185d07c55SAlan Stern buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
484285d07c55SAlan Stern if (!buf)
484385d07c55SAlan Stern return -ENOMEM;
48441da177e4SLinus Torvalds
48451da177e4SLinus Torvalds /* root hub ports have a slightly longer reset period
48461da177e4SLinus Torvalds * (from USB 2.0 spec, section 7.1.7.5)
48471da177e4SLinus Torvalds */
48481da177e4SLinus Torvalds if (!hdev->parent) {
48491da177e4SLinus Torvalds delay = HUB_ROOT_RESET_TIME;
48501da177e4SLinus Torvalds if (port1 == hdev->bus->otg_port)
48511da177e4SLinus Torvalds hdev->bus->b_hnp_enable = 0;
48521da177e4SLinus Torvalds }
48531da177e4SLinus Torvalds
48541da177e4SLinus Torvalds /* Some low speed devices have problems with the quick delay, so */
48551da177e4SLinus Torvalds /* be a bit pessimistic with those devices. RHbug #23670 */
48561da177e4SLinus Torvalds if (oldspeed == USB_SPEED_LOW)
48571da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME;
48581da177e4SLinus Torvalds
48591da177e4SLinus Torvalds /* Reset the device; full speed may morph to high speed */
4860e7b77172SSarah Sharp /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
486175d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false);
48621da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */
48631da177e4SLinus Torvalds goto fail;
48641da177e4SLinus Torvalds /* success, speed is known */
486507194ab7SLuben Tuikov
48661da177e4SLinus Torvalds retval = -ENODEV;
48671da177e4SLinus Torvalds
48688a1b2725SMathias Nyman /* Don't allow speed changes at reset, except usb 3.0 to faster */
48698a1b2725SMathias Nyman if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
48708a1b2725SMathias Nyman !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
48711da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset changed speed!\n");
48721da177e4SLinus Torvalds goto fail;
48731da177e4SLinus Torvalds }
48741da177e4SLinus Torvalds oldspeed = udev->speed;
48751da177e4SLinus Torvalds
4876ff33299eSAlan Stern if (initial) {
48771da177e4SLinus Torvalds /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
48781da177e4SLinus Torvalds * it's fixed size except for full speed devices.
48791da177e4SLinus Torvalds */
48801da177e4SLinus Torvalds switch (udev->speed) {
48818a1b2725SMathias Nyman case USB_SPEED_SUPER_PLUS:
48826b403b02SSarah Sharp case USB_SPEED_SUPER:
4883551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
48845bb6e0aeSInaky Perez-Gonzalez break;
48851da177e4SLinus Torvalds case USB_SPEED_HIGH: /* fixed at 64 */
4886551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
48871da177e4SLinus Torvalds break;
48881da177e4SLinus Torvalds case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
48891da177e4SLinus Torvalds /* to determine the ep0 maxpacket size, try to read
48901da177e4SLinus Torvalds * the device descriptor to get bMaxPacketSize0 and
48911da177e4SLinus Torvalds * then correct our initial guess.
48921da177e4SLinus Torvalds */
4893551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
48941da177e4SLinus Torvalds break;
48951da177e4SLinus Torvalds case USB_SPEED_LOW: /* fixed at 8 */
4896551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
48971da177e4SLinus Torvalds break;
48981da177e4SLinus Torvalds default:
48991da177e4SLinus Torvalds goto fail;
49001da177e4SLinus Torvalds }
4901ff33299eSAlan Stern }
49021da177e4SLinus Torvalds
4903e538dfdaSMichal Nazarewicz speed = usb_speed_string(udev->speed);
4904e538dfdaSMichal Nazarewicz
49051bb90cf0SPeter Chen /*
49061bb90cf0SPeter Chen * The controller driver may be NULL if the controller device
49071bb90cf0SPeter Chen * is the middle device between platform device and roothub.
49081bb90cf0SPeter Chen * This middle device may not need a device driver due to
49091bb90cf0SPeter Chen * all hardware control can be at platform device driver, this
49101bb90cf0SPeter Chen * platform device is usually a dual-role USB controller device.
49111bb90cf0SPeter Chen */
49121bb90cf0SPeter Chen if (udev->bus->controller->driver)
49131bb90cf0SPeter Chen driver_name = udev->bus->controller->driver->name;
49141bb90cf0SPeter Chen else
49151bb90cf0SPeter Chen driver_name = udev->bus->sysdev->driver->name;
49161bb90cf0SPeter Chen
49178a1b2725SMathias Nyman if (udev->speed < USB_SPEED_SUPER)
491883a07196SInaky Perez-Gonzalez dev_info(&udev->dev,
4919e538dfdaSMichal Nazarewicz "%s %s USB device number %d using %s\n",
4920ff33299eSAlan Stern (initial ? "new" : "reset"), speed,
49211bb90cf0SPeter Chen devnum, driver_name);
49221da177e4SLinus Torvalds
4923ff33299eSAlan Stern if (initial) {
49241da177e4SLinus Torvalds /* Set up TT records, if needed */
49251da177e4SLinus Torvalds if (hdev->tt) {
49261da177e4SLinus Torvalds udev->tt = hdev->tt;
49271da177e4SLinus Torvalds udev->ttport = hdev->ttport;
49281da177e4SLinus Torvalds } else if (udev->speed != USB_SPEED_HIGH
49291da177e4SLinus Torvalds && hdev->speed == USB_SPEED_HIGH) {
4930d199c96dSAlan Stern if (!hub->tt.hub) {
4931d199c96dSAlan Stern dev_err(&udev->dev, "parent hub has no TT\n");
4932d199c96dSAlan Stern retval = -EINVAL;
4933d199c96dSAlan Stern goto fail;
4934d199c96dSAlan Stern }
49351da177e4SLinus Torvalds udev->tt = &hub->tt;
49361da177e4SLinus Torvalds udev->ttport = port1;
49371da177e4SLinus Torvalds }
4938ff33299eSAlan Stern }
49391da177e4SLinus Torvalds
49401da177e4SLinus Torvalds /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
49411da177e4SLinus Torvalds * Because device hardware and firmware is sometimes buggy in
49421da177e4SLinus Torvalds * this area, and this is how Linux has done it for ages.
49431da177e4SLinus Torvalds * Change it cautiously.
49441da177e4SLinus Torvalds *
494548fc7dbdSDan Williams * NOTE: If use_new_scheme() is true we will start by issuing
49461da177e4SLinus Torvalds * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
49471da177e4SLinus Torvalds * so it may help with some non-standards-compliant devices.
49481da177e4SLinus Torvalds * Otherwise we start with SET_ADDRESS and then try to read the
49491da177e4SLinus Torvalds * first 8 bytes of the device descriptor to get the ep0 maxpacket
49501da177e4SLinus Torvalds * value.
49511da177e4SLinus Torvalds */
495219502e69SAlan Stern do_new_scheme = use_new_scheme(udev, retry_counter, port_dev);
495348fc7dbdSDan Williams
495419502e69SAlan Stern for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
4955430d57f5SRay Chi if (hub_port_stop_enumerate(hub, port1, retries)) {
4956430d57f5SRay Chi retval = -ENODEV;
4957430d57f5SRay Chi break;
4958430d57f5SRay Chi }
4959430d57f5SRay Chi
496019502e69SAlan Stern if (do_new_scheme) {
496148fc7dbdSDan Williams retval = hub_enable_device(udev);
4962938569ebSOliver Neukum if (retval < 0) {
4963938569ebSOliver Neukum dev_err(&udev->dev,
4964938569ebSOliver Neukum "hub failed to enable device, error %d\n",
4965938569ebSOliver Neukum retval);
496648fc7dbdSDan Williams goto fail;
4967938569ebSOliver Neukum }
496848fc7dbdSDan Williams
496985d07c55SAlan Stern maxp0 = get_bMaxPacketSize0(udev, buf,
497085d07c55SAlan Stern GET_DESCRIPTOR_BUFSIZE, retries == 0);
4971ff33299eSAlan Stern if (maxp0 > 0 && !initial &&
4972ff33299eSAlan Stern maxp0 != udev->descriptor.bMaxPacketSize0) {
4973ff33299eSAlan Stern dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n");
4974ff33299eSAlan Stern retval = -ENODEV;
4975ff33299eSAlan Stern goto fail;
4976ff33299eSAlan Stern }
49771da177e4SLinus Torvalds
497875d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false);
49791da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */
49801da177e4SLinus Torvalds goto fail;
49811da177e4SLinus Torvalds if (oldspeed != udev->speed) {
49821da177e4SLinus Torvalds dev_dbg(&udev->dev,
49831da177e4SLinus Torvalds "device reset changed speed!\n");
49841da177e4SLinus Torvalds retval = -ENODEV;
49851da177e4SLinus Torvalds goto fail;
49861da177e4SLinus Torvalds }
498785d07c55SAlan Stern if (maxp0 < 0) {
498885d07c55SAlan Stern if (maxp0 != -ENODEV)
4989e9e88fb7SAlan Stern dev_err(&udev->dev, "device descriptor read/64, error %d\n",
499085d07c55SAlan Stern maxp0);
499185d07c55SAlan Stern retval = maxp0;
49921da177e4SLinus Torvalds continue;
49931da177e4SLinus Torvalds }
49941da177e4SLinus Torvalds }
49951da177e4SLinus Torvalds
49960d5ce778SOliver Neukum for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
49974326ed0bSAlan Stern retval = hub_set_address(udev, devnum);
49981da177e4SLinus Torvalds if (retval >= 0)
49991da177e4SLinus Torvalds break;
50001da177e4SLinus Torvalds msleep(200);
50011da177e4SLinus Torvalds }
50021da177e4SLinus Torvalds if (retval < 0) {
5003e9e88fb7SAlan Stern if (retval != -ENODEV)
5004e9e88fb7SAlan Stern dev_err(&udev->dev, "device not accepting address %d, error %d\n",
50054326ed0bSAlan Stern devnum, retval);
50061da177e4SLinus Torvalds goto fail;
50071da177e4SLinus Torvalds }
50088a1b2725SMathias Nyman if (udev->speed >= USB_SPEED_SUPER) {
5009c6515272SSarah Sharp devnum = udev->devnum;
5010c6515272SSarah Sharp dev_info(&udev->dev,
501145455e4dSMathias Nyman "%s SuperSpeed%s%s USB device number %d using %s\n",
5012c6515272SSarah Sharp (udev->config) ? "reset" : "new",
501345455e4dSMathias Nyman (udev->speed == USB_SPEED_SUPER_PLUS) ?
50142d0e82c9SThinh Nguyen " Plus" : "",
50152d0e82c9SThinh Nguyen (udev->ssp_rate == USB_SSP_GEN_2x2) ?
50162d0e82c9SThinh Nguyen " Gen 2x2" :
50172d0e82c9SThinh Nguyen (udev->ssp_rate == USB_SSP_GEN_2x1) ?
50182d0e82c9SThinh Nguyen " Gen 2x1" :
50192d0e82c9SThinh Nguyen (udev->ssp_rate == USB_SSP_GEN_1x2) ?
50202d0e82c9SThinh Nguyen " Gen 1x2" : "",
50211bb90cf0SPeter Chen devnum, driver_name);
5022c6515272SSarah Sharp }
50231da177e4SLinus Torvalds
50241e4c5742SAlan Stern /*
50251e4c5742SAlan Stern * cope with hardware quirkiness:
50261da177e4SLinus Torvalds * - let SET_ADDRESS settle, some device hardware wants it
50271da177e4SLinus Torvalds * - read ep0 maxpacket even for high and low speed,
50281da177e4SLinus Torvalds */
50291da177e4SLinus Torvalds msleep(10);
50301e4c5742SAlan Stern
503119502e69SAlan Stern if (do_new_scheme)
50321da177e4SLinus Torvalds break;
50331da177e4SLinus Torvalds
503485d07c55SAlan Stern maxp0 = get_bMaxPacketSize0(udev, buf, 8, retries == 0);
503585d07c55SAlan Stern if (maxp0 < 0) {
503685d07c55SAlan Stern retval = maxp0;
5037e9e88fb7SAlan Stern if (retval != -ENODEV)
5038b9cef6c3SWu Fengguang dev_err(&udev->dev,
5039b9cef6c3SWu Fengguang "device descriptor read/8, error %d\n",
5040b9cef6c3SWu Fengguang retval);
50411da177e4SLinus Torvalds } else {
5042886ee36eSFelipe Balbi u32 delay;
5043886ee36eSFelipe Balbi
5044ff33299eSAlan Stern if (!initial && maxp0 != udev->descriptor.bMaxPacketSize0) {
5045ff33299eSAlan Stern dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n");
5046ff33299eSAlan Stern retval = -ENODEV;
5047ff33299eSAlan Stern goto fail;
5048ff33299eSAlan Stern }
5049ff33299eSAlan Stern
5050886ee36eSFelipe Balbi delay = udev->parent->hub_delay;
5051886ee36eSFelipe Balbi udev->hub_delay = min_t(u32, delay,
5052886ee36eSFelipe Balbi USB_TP_TRANSMISSION_DELAY_MAX);
5053886ee36eSFelipe Balbi retval = usb_set_isoch_delay(udev);
5054886ee36eSFelipe Balbi if (retval) {
5055886ee36eSFelipe Balbi dev_dbg(&udev->dev,
5056886ee36eSFelipe Balbi "Failed set isoch delay, error %d\n",
5057886ee36eSFelipe Balbi retval);
5058886ee36eSFelipe Balbi retval = 0;
5059886ee36eSFelipe Balbi }
50601da177e4SLinus Torvalds break;
50611da177e4SLinus Torvalds }
50621da177e4SLinus Torvalds }
50631da177e4SLinus Torvalds if (retval)
50641da177e4SLinus Torvalds goto fail;
50651da177e4SLinus Torvalds
506659cf4457SAlan Stern /*
506759cf4457SAlan Stern * Check the ep0 maxpacket guess and correct it if necessary.
506859cf4457SAlan Stern * maxp0 is the value stored in the device descriptor;
506959cf4457SAlan Stern * i is the value it encodes (logarithmic for SuperSpeed or greater).
507059cf4457SAlan Stern */
507185d07c55SAlan Stern i = maxp0;
507259cf4457SAlan Stern if (udev->speed >= USB_SPEED_SUPER) {
507359cf4457SAlan Stern if (maxp0 <= 16)
507459cf4457SAlan Stern i = 1 << maxp0;
507559cf4457SAlan Stern else
507659cf4457SAlan Stern i = 0; /* Invalid */
50771da177e4SLinus Torvalds }
507859cf4457SAlan Stern if (usb_endpoint_maxp(&udev->ep0.desc) == i) {
507959cf4457SAlan Stern ; /* Initial ep0 maxpacket guess is right */
5080480c3abbSAlan Stern } else if (((udev->speed == USB_SPEED_FULL ||
508159cf4457SAlan Stern udev->speed == USB_SPEED_HIGH) &&
5082480c3abbSAlan Stern (i == 8 || i == 16 || i == 32 || i == 64)) ||
5083480c3abbSAlan Stern (udev->speed >= USB_SPEED_SUPER && i > 0)) {
508459cf4457SAlan Stern /* Initial guess is wrong; use the descriptor's value */
508556626a72SAlan Stern if (udev->speed == USB_SPEED_FULL)
50861da177e4SLinus Torvalds dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
508756626a72SAlan Stern else
508856626a72SAlan Stern dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
50891da177e4SLinus Torvalds udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
5090fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev);
509159cf4457SAlan Stern } else {
509259cf4457SAlan Stern /* Initial guess is wrong and descriptor's value is invalid */
509359cf4457SAlan Stern dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", maxp0);
509459cf4457SAlan Stern retval = -EMSGSIZE;
509559cf4457SAlan Stern goto fail;
50961da177e4SLinus Torvalds }
50971da177e4SLinus Torvalds
5098de28e469SAlan Stern descr = usb_get_device_descriptor(udev);
5099de28e469SAlan Stern if (IS_ERR(descr)) {
5100de28e469SAlan Stern retval = PTR_ERR(descr);
5101e9e88fb7SAlan Stern if (retval != -ENODEV)
5102b9cef6c3SWu Fengguang dev_err(&udev->dev, "device descriptor read/all, error %d\n",
5103b9cef6c3SWu Fengguang retval);
51041da177e4SLinus Torvalds goto fail;
51051da177e4SLinus Torvalds }
5106ff33299eSAlan Stern if (initial)
5107de28e469SAlan Stern udev->descriptor = *descr;
5108ff33299eSAlan Stern else
5109ff33299eSAlan Stern *dev_descr = *descr;
5110de28e469SAlan Stern kfree(descr);
51111da177e4SLinus Torvalds
511285d07c55SAlan Stern /*
511385d07c55SAlan Stern * Some superspeed devices have finished the link training process
511485d07c55SAlan Stern * and attached to a superspeed hub port, but the device descriptor
511585d07c55SAlan Stern * got from those devices show they aren't superspeed devices. Warm
511685d07c55SAlan Stern * reset the port attached by the devices can fix them.
511785d07c55SAlan Stern */
511885d07c55SAlan Stern if ((udev->speed >= USB_SPEED_SUPER) &&
511985d07c55SAlan Stern (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
512085d07c55SAlan Stern dev_err(&udev->dev, "got a wrong device descriptor, warm reset device\n");
512185d07c55SAlan Stern hub_port_reset(hub, port1, udev, HUB_BH_RESET_TIME, true);
512285d07c55SAlan Stern retval = -EINVAL;
512385d07c55SAlan Stern goto fail;
512485d07c55SAlan Stern }
512585d07c55SAlan Stern
5126ad87e032SAlan Stern usb_detect_quirks(udev);
5127ad87e032SAlan Stern
51281e4c5742SAlan Stern if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
51291ff4df56SAndiry Xu retval = usb_get_bos_descriptor(udev);
513051e0a012SSarah Sharp if (!retval) {
5131d9b2099cSSarah Sharp udev->lpm_capable = usb_device_supports_lpm(udev);
5132cd36facfSMathias Nyman udev->lpm_disable_count = 1;
513351e0a012SSarah Sharp usb_set_lpm_parameters(udev);
5134e146caf3SMathias Nyman usb_req_set_sel(udev);
513551e0a012SSarah Sharp }
51361ff4df56SAndiry Xu }
51373148bf04SAndiry Xu
51381da177e4SLinus Torvalds retval = 0;
513948f24970SAlek Du /* notify HCD that we have a device connected and addressed */
514048f24970SAlek Du if (hcd->driver->update_device)
514148f24970SAlek Du hcd->driver->update_device(hcd, udev);
5142890dae88SMathias Nyman hub_set_initial_usb2_lpm_policy(udev);
51431da177e4SLinus Torvalds fail:
51444326ed0bSAlan Stern if (retval) {
51451da177e4SLinus Torvalds hub_port_disable(hub, port1, 0);
51463b29b68bSAlan Stern update_devnum(udev, devnum); /* for disconnect processing */
51474326ed0bSAlan Stern }
514885d07c55SAlan Stern kfree(buf);
51491da177e4SLinus Torvalds return retval;
51501da177e4SLinus Torvalds }
51511da177e4SLinus Torvalds
51521da177e4SLinus Torvalds static void
check_highspeed(struct usb_hub * hub,struct usb_device * udev,int port1)51531da177e4SLinus Torvalds check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
51541da177e4SLinus Torvalds {
51551da177e4SLinus Torvalds struct usb_qualifier_descriptor *qual;
51561da177e4SLinus Torvalds int status;
51571da177e4SLinus Torvalds
51582a159389SJohan Hovold if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
51592a159389SJohan Hovold return;
51602a159389SJohan Hovold
5161e94b1766SChristoph Lameter qual = kmalloc(sizeof *qual, GFP_KERNEL);
51621da177e4SLinus Torvalds if (qual == NULL)
51631da177e4SLinus Torvalds return;
51641da177e4SLinus Torvalds
51651da177e4SLinus Torvalds status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
51661da177e4SLinus Torvalds qual, sizeof *qual);
51671da177e4SLinus Torvalds if (status == sizeof *qual) {
51681da177e4SLinus Torvalds dev_info(&udev->dev, "not running at top speed; "
51691da177e4SLinus Torvalds "connect to a high speed hub\n");
51701da177e4SLinus Torvalds /* hub LEDs are probably harder to miss than syslog */
51711da177e4SLinus Torvalds if (hub->has_indicators) {
51721da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
517322f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq,
517422f6a0f0SShaibal Dutta &hub->leds, 0);
51751da177e4SLinus Torvalds }
51761da177e4SLinus Torvalds }
51771da177e4SLinus Torvalds kfree(qual);
51781da177e4SLinus Torvalds }
51791da177e4SLinus Torvalds
51801da177e4SLinus Torvalds static unsigned
hub_power_remaining(struct usb_hub * hub)51811da177e4SLinus Torvalds hub_power_remaining(struct usb_hub *hub)
51821da177e4SLinus Torvalds {
51831da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
51841da177e4SLinus Torvalds int remaining;
518555c52718SAlan Stern int port1;
51861da177e4SLinus Torvalds
518755c52718SAlan Stern if (!hub->limited_power)
51881da177e4SLinus Torvalds return 0;
51891da177e4SLinus Torvalds
519055c52718SAlan Stern remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
519155c52718SAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
5192d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
5193d99f6b41SDan Williams struct usb_device *udev = port_dev->child;
5194430ee58eSSebastian Andrzej Siewior unsigned unit_load;
5195d99f6b41SDan Williams int delta;
51961da177e4SLinus Torvalds
51971da177e4SLinus Torvalds if (!udev)
51981da177e4SLinus Torvalds continue;
5199430ee58eSSebastian Andrzej Siewior if (hub_is_superspeed(udev))
5200430ee58eSSebastian Andrzej Siewior unit_load = 150;
5201430ee58eSSebastian Andrzej Siewior else
5202430ee58eSSebastian Andrzej Siewior unit_load = 100;
52031da177e4SLinus Torvalds
5204430ee58eSSebastian Andrzej Siewior /*
5205430ee58eSSebastian Andrzej Siewior * Unconfigured devices may not use more than one unit load,
5206430ee58eSSebastian Andrzej Siewior * or 8mA for OTG ports
5207430ee58eSSebastian Andrzej Siewior */
52081da177e4SLinus Torvalds if (udev->actconfig)
52098d8479dbSSebastian Andrzej Siewior delta = usb_get_max_power(udev, udev->actconfig);
521055c52718SAlan Stern else if (port1 != udev->bus->otg_port || hdev->parent)
5211430ee58eSSebastian Andrzej Siewior delta = unit_load;
52121da177e4SLinus Torvalds else
521355c52718SAlan Stern delta = 8;
521455c52718SAlan Stern if (delta > hub->mA_per_port)
5215d99f6b41SDan Williams dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
5216d99f6b41SDan Williams delta, hub->mA_per_port);
52171da177e4SLinus Torvalds remaining -= delta;
52181da177e4SLinus Torvalds }
52191da177e4SLinus Torvalds if (remaining < 0) {
522055c52718SAlan Stern dev_warn(hub->intfdev, "%dmA over power budget!\n",
522155c52718SAlan Stern -remaining);
52221da177e4SLinus Torvalds remaining = 0;
52231da177e4SLinus Torvalds }
52241da177e4SLinus Torvalds return remaining;
52251da177e4SLinus Torvalds }
52261da177e4SLinus Torvalds
5227a4f55d8bSDavid Heinzelmann
descriptors_changed(struct usb_device * udev,struct usb_device_descriptor * new_device_descriptor,struct usb_host_bos * old_bos)5228a4f55d8bSDavid Heinzelmann static int descriptors_changed(struct usb_device *udev,
5229de28e469SAlan Stern struct usb_device_descriptor *new_device_descriptor,
5230a4f55d8bSDavid Heinzelmann struct usb_host_bos *old_bos)
5231a4f55d8bSDavid Heinzelmann {
5232a4f55d8bSDavid Heinzelmann int changed = 0;
5233a4f55d8bSDavid Heinzelmann unsigned index;
5234a4f55d8bSDavid Heinzelmann unsigned serial_len = 0;
5235a4f55d8bSDavid Heinzelmann unsigned len;
5236a4f55d8bSDavid Heinzelmann unsigned old_length;
5237a4f55d8bSDavid Heinzelmann int length;
5238a4f55d8bSDavid Heinzelmann char *buf;
5239a4f55d8bSDavid Heinzelmann
5240de28e469SAlan Stern if (memcmp(&udev->descriptor, new_device_descriptor,
5241de28e469SAlan Stern sizeof(*new_device_descriptor)) != 0)
5242a4f55d8bSDavid Heinzelmann return 1;
5243a4f55d8bSDavid Heinzelmann
5244a4f55d8bSDavid Heinzelmann if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5245a4f55d8bSDavid Heinzelmann return 1;
5246a4f55d8bSDavid Heinzelmann if (udev->bos) {
5247a4f55d8bSDavid Heinzelmann len = le16_to_cpu(udev->bos->desc->wTotalLength);
5248a4f55d8bSDavid Heinzelmann if (len != le16_to_cpu(old_bos->desc->wTotalLength))
5249a4f55d8bSDavid Heinzelmann return 1;
5250a4f55d8bSDavid Heinzelmann if (memcmp(udev->bos->desc, old_bos->desc, len))
5251a4f55d8bSDavid Heinzelmann return 1;
5252a4f55d8bSDavid Heinzelmann }
5253a4f55d8bSDavid Heinzelmann
5254a4f55d8bSDavid Heinzelmann /* Since the idVendor, idProduct, and bcdDevice values in the
5255a4f55d8bSDavid Heinzelmann * device descriptor haven't changed, we will assume the
5256a4f55d8bSDavid Heinzelmann * Manufacturer and Product strings haven't changed either.
5257a4f55d8bSDavid Heinzelmann * But the SerialNumber string could be different (e.g., a
5258a4f55d8bSDavid Heinzelmann * different flash card of the same brand).
5259a4f55d8bSDavid Heinzelmann */
5260a4f55d8bSDavid Heinzelmann if (udev->serial)
5261a4f55d8bSDavid Heinzelmann serial_len = strlen(udev->serial) + 1;
5262a4f55d8bSDavid Heinzelmann
5263a4f55d8bSDavid Heinzelmann len = serial_len;
5264a4f55d8bSDavid Heinzelmann for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5265a4f55d8bSDavid Heinzelmann old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5266a4f55d8bSDavid Heinzelmann len = max(len, old_length);
5267a4f55d8bSDavid Heinzelmann }
5268a4f55d8bSDavid Heinzelmann
5269a4f55d8bSDavid Heinzelmann buf = kmalloc(len, GFP_NOIO);
5270a4f55d8bSDavid Heinzelmann if (!buf)
5271a4f55d8bSDavid Heinzelmann /* assume the worst */
5272a4f55d8bSDavid Heinzelmann return 1;
5273a4f55d8bSDavid Heinzelmann
5274a4f55d8bSDavid Heinzelmann for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5275a4f55d8bSDavid Heinzelmann old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5276a4f55d8bSDavid Heinzelmann length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5277a4f55d8bSDavid Heinzelmann old_length);
5278a4f55d8bSDavid Heinzelmann if (length != old_length) {
5279a4f55d8bSDavid Heinzelmann dev_dbg(&udev->dev, "config index %d, error %d\n",
5280a4f55d8bSDavid Heinzelmann index, length);
5281a4f55d8bSDavid Heinzelmann changed = 1;
5282a4f55d8bSDavid Heinzelmann break;
5283a4f55d8bSDavid Heinzelmann }
5284a4f55d8bSDavid Heinzelmann if (memcmp(buf, udev->rawdescriptors[index], old_length)
5285a4f55d8bSDavid Heinzelmann != 0) {
5286a4f55d8bSDavid Heinzelmann dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
5287a4f55d8bSDavid Heinzelmann index,
5288a4f55d8bSDavid Heinzelmann ((struct usb_config_descriptor *) buf)->
5289a4f55d8bSDavid Heinzelmann bConfigurationValue);
5290a4f55d8bSDavid Heinzelmann changed = 1;
5291a4f55d8bSDavid Heinzelmann break;
5292a4f55d8bSDavid Heinzelmann }
5293a4f55d8bSDavid Heinzelmann }
5294a4f55d8bSDavid Heinzelmann
5295a4f55d8bSDavid Heinzelmann if (!changed && serial_len) {
5296a4f55d8bSDavid Heinzelmann length = usb_string(udev, udev->descriptor.iSerialNumber,
5297a4f55d8bSDavid Heinzelmann buf, serial_len);
5298a4f55d8bSDavid Heinzelmann if (length + 1 != serial_len) {
5299a4f55d8bSDavid Heinzelmann dev_dbg(&udev->dev, "serial string error %d\n",
5300a4f55d8bSDavid Heinzelmann length);
5301a4f55d8bSDavid Heinzelmann changed = 1;
5302a4f55d8bSDavid Heinzelmann } else if (memcmp(buf, udev->serial, length) != 0) {
5303a4f55d8bSDavid Heinzelmann dev_dbg(&udev->dev, "serial string changed\n");
5304a4f55d8bSDavid Heinzelmann changed = 1;
5305a4f55d8bSDavid Heinzelmann }
5306a4f55d8bSDavid Heinzelmann }
5307a4f55d8bSDavid Heinzelmann
5308a4f55d8bSDavid Heinzelmann kfree(buf);
5309a4f55d8bSDavid Heinzelmann return changed;
5310a4f55d8bSDavid Heinzelmann }
5311a4f55d8bSDavid Heinzelmann
hub_port_connect(struct usb_hub * hub,int port1,u16 portstatus,u16 portchange)5312af376a46SDan Williams static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
5313af376a46SDan Williams u16 portchange)
53141da177e4SLinus Torvalds {
531594c43b98SAlan Stern int status = -ENODEV;
531694c43b98SAlan Stern int i;
5317430ee58eSSebastian Andrzej Siewior unsigned unit_load;
53181da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev;
53191da177e4SLinus Torvalds struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
5320d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
5321af376a46SDan Williams struct usb_device *udev = port_dev->child;
53225ee0f803SOliver Neukum static int unreliable_port = -1;
53236cca13deSMathias Nyman bool retry_locked;
53248808f00cSAlan Stern
532524618b0cSAlan Stern /* Disconnect any existing devices under this port */
5326b76baa81SPeter Chen if (udev) {
5327b2108f1eSPeter Chen if (hcd->usb_phy && !hdev->parent)
53283d46e73dSAntoine Tenart usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
5329d99f6b41SDan Williams usb_disconnect(&port_dev->child);
5330b76baa81SPeter Chen }
533124618b0cSAlan Stern
5332253e0572SAlan Stern /* We can forget about a "removed" device when there's a physical
5333253e0572SAlan Stern * disconnect or the connect status changes.
5334253e0572SAlan Stern */
5335253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
5336253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION))
5337253e0572SAlan Stern clear_bit(port1, hub->removed_bits);
5338253e0572SAlan Stern
53395257d97aSAlan Stern if (portchange & (USB_PORT_STAT_C_CONNECTION |
53405257d97aSAlan Stern USB_PORT_STAT_C_ENABLE)) {
5341ad493e5eSLan Tianyu status = hub_port_debounce_be_stable(hub, port1);
53425257d97aSAlan Stern if (status < 0) {
53435ee0f803SOliver Neukum if (status != -ENODEV &&
53445ee0f803SOliver Neukum port1 != unreliable_port &&
53455ee0f803SOliver Neukum printk_ratelimit())
5346dd5f5006STakashi Iwai dev_err(&port_dev->dev, "connect-debounce failed\n");
53475257d97aSAlan Stern portstatus &= ~USB_PORT_STAT_CONNECTION;
53485ee0f803SOliver Neukum unreliable_port = port1;
53495257d97aSAlan Stern } else {
53505257d97aSAlan Stern portstatus = status;
53515257d97aSAlan Stern }
53525257d97aSAlan Stern }
53535257d97aSAlan Stern
5354253e0572SAlan Stern /* Return now if debouncing failed or nothing is connected or
5355253e0572SAlan Stern * the device was "removed".
5356253e0572SAlan Stern */
5357253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
5358253e0572SAlan Stern test_bit(port1, hub->removed_bits)) {
53591da177e4SLinus Torvalds
5360fbaecff0SDeepak Das /*
5361fbaecff0SDeepak Das * maybe switch power back on (e.g. root hub was reset)
5362fbaecff0SDeepak Das * but only if the port isn't owned by someone else.
5363fbaecff0SDeepak Das */
53649262c19dSDan Williams if (hub_is_port_power_switchable(hub)
5365f061f43dSMichael Grzeschik && !usb_port_is_power_on(hub, portstatus)
5366fbaecff0SDeepak Das && !port_dev->port_owner)
53671da177e4SLinus Torvalds set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
53681da177e4SLinus Torvalds
53691da177e4SLinus Torvalds if (portstatus & USB_PORT_STAT_ENABLE)
53701da177e4SLinus Torvalds goto done;
53711da177e4SLinus Torvalds return;
53721da177e4SLinus Torvalds }
5373430ee58eSSebastian Andrzej Siewior if (hub_is_superspeed(hub->hdev))
5374430ee58eSSebastian Andrzej Siewior unit_load = 150;
5375430ee58eSSebastian Andrzej Siewior else
5376430ee58eSSebastian Andrzej Siewior unit_load = 100;
53771da177e4SLinus Torvalds
5378e9e88fb7SAlan Stern status = 0;
53796ae6dc22SMathias Nyman
538019502e69SAlan Stern for (i = 0; i < PORT_INIT_TRIES; i++) {
5381430d57f5SRay Chi if (hub_port_stop_enumerate(hub, port1, i)) {
5382430d57f5SRay Chi status = -ENODEV;
5383430d57f5SRay Chi break;
5384430d57f5SRay Chi }
5385430d57f5SRay Chi
53866cca13deSMathias Nyman usb_lock_port(port_dev);
53876cca13deSMathias Nyman mutex_lock(hcd->address0_mutex);
53886cca13deSMathias Nyman retry_locked = true;
53891da177e4SLinus Torvalds /* reallocate for each attempt, since references
53901da177e4SLinus Torvalds * to the previous one can escape in various ways
53911da177e4SLinus Torvalds */
53921da177e4SLinus Torvalds udev = usb_alloc_dev(hdev, hdev->bus, port1);
53931da177e4SLinus Torvalds if (!udev) {
5394d99f6b41SDan Williams dev_err(&port_dev->dev,
5395d99f6b41SDan Williams "couldn't allocate usb_device\n");
53966cca13deSMathias Nyman mutex_unlock(hcd->address0_mutex);
53976cca13deSMathias Nyman usb_unlock_port(port_dev);
53981da177e4SLinus Torvalds goto done;
53991da177e4SLinus Torvalds }
54001da177e4SLinus Torvalds
54011da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_POWERED);
540255c52718SAlan Stern udev->bus_mA = hub->mA_per_port;
5403b6956ffaSAlan Stern udev->level = hdev->level + 1;
54041da177e4SLinus Torvalds
54058a1b2725SMathias Nyman /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
5406131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev))
5407e7b77172SSarah Sharp udev->speed = USB_SPEED_SUPER;
5408e7b77172SSarah Sharp else
5409e7b77172SSarah Sharp udev->speed = USB_SPEED_UNKNOWN;
5410e7b77172SSarah Sharp
54113b29b68bSAlan Stern choose_devnum(udev);
5412c6515272SSarah Sharp if (udev->devnum <= 0) {
5413c6515272SSarah Sharp status = -ENOTCONN; /* Don't retry */
5414c6515272SSarah Sharp goto loop;
5415c6515272SSarah Sharp }
5416c6515272SSarah Sharp
5417e7b77172SSarah Sharp /* reset (non-USB 3.0 devices) and get descriptor */
5418ff33299eSAlan Stern status = hub_port_init(hub, udev, port1, i, NULL);
54191da177e4SLinus Torvalds if (status < 0)
54201da177e4SLinus Torvalds goto loop;
54211da177e4SLinus Torvalds
54226ae6dc22SMathias Nyman mutex_unlock(hcd->address0_mutex);
54236cca13deSMathias Nyman usb_unlock_port(port_dev);
54246cca13deSMathias Nyman retry_locked = false;
54256ae6dc22SMathias Nyman
542693362a87SPhil Dibowitz if (udev->quirks & USB_QUIRK_DELAY_INIT)
5427b2a542bbSDmitry Fleytman msleep(2000);
542893362a87SPhil Dibowitz
54291da177e4SLinus Torvalds /* consecutive bus-powered hubs aren't reliable; they can
54301da177e4SLinus Torvalds * violate the voltage drop budget. if the new child has
54311da177e4SLinus Torvalds * a "powered" LED, users should notice we didn't enable it
54321da177e4SLinus Torvalds * (without reading syslog), even without per-port LEDs
54331da177e4SLinus Torvalds * on the parent.
54341da177e4SLinus Torvalds */
54351da177e4SLinus Torvalds if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
5436430ee58eSSebastian Andrzej Siewior && udev->bus_mA <= unit_load) {
54371da177e4SLinus Torvalds u16 devstat;
54381da177e4SLinus Torvalds
5439d9e1e148SFelipe Balbi status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
54401da177e4SLinus Torvalds &devstat);
544115b7336eSAlan Stern if (status) {
54421da177e4SLinus Torvalds dev_dbg(&udev->dev, "get status %d ?\n", status);
54431da177e4SLinus Torvalds goto loop_disable;
54441da177e4SLinus Torvalds }
54451da177e4SLinus Torvalds if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
54461da177e4SLinus Torvalds dev_err(&udev->dev,
54471da177e4SLinus Torvalds "can't connect bus-powered hub "
54481da177e4SLinus Torvalds "to this port\n");
54491da177e4SLinus Torvalds if (hub->has_indicators) {
54501da177e4SLinus Torvalds hub->indicator[port1-1] =
54511da177e4SLinus Torvalds INDICATOR_AMBER_BLINK;
545222f6a0f0SShaibal Dutta queue_delayed_work(
545322f6a0f0SShaibal Dutta system_power_efficient_wq,
545422f6a0f0SShaibal Dutta &hub->leds, 0);
54551da177e4SLinus Torvalds }
54561da177e4SLinus Torvalds status = -ENOTCONN; /* Don't retry */
54571da177e4SLinus Torvalds goto loop_disable;
54581da177e4SLinus Torvalds }
54591da177e4SLinus Torvalds }
54601da177e4SLinus Torvalds
54611da177e4SLinus Torvalds /* check for devices running slower than they could */
54621da177e4SLinus Torvalds if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
54631da177e4SLinus Torvalds && udev->speed == USB_SPEED_FULL
54641da177e4SLinus Torvalds && highspeed_hubs != 0)
54651da177e4SLinus Torvalds check_highspeed(hub, udev, port1);
54661da177e4SLinus Torvalds
5467fa286188SGreg Kroah-Hartman /* Store the parent's children[] pointer. At this point
54681da177e4SLinus Torvalds * udev becomes globally accessible, although presumably
54691da177e4SLinus Torvalds * no one will look at it until hdev is unlocked.
54701da177e4SLinus Torvalds */
54711da177e4SLinus Torvalds status = 0;
54721da177e4SLinus Torvalds
5473d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex);
5474d8521afeSDan Williams
54751da177e4SLinus Torvalds /* We mustn't add new devices if the parent hub has
54761da177e4SLinus Torvalds * been disconnected; we would race with the
54771da177e4SLinus Torvalds * recursively_mark_NOTATTACHED() routine.
54781da177e4SLinus Torvalds */
54791da177e4SLinus Torvalds spin_lock_irq(&device_state_lock);
54801da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED)
54811da177e4SLinus Torvalds status = -ENOTCONN;
54821da177e4SLinus Torvalds else
5483d99f6b41SDan Williams port_dev->child = udev;
54841da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock);
5485d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex);
54861da177e4SLinus Torvalds
54871da177e4SLinus Torvalds /* Run it through the hoops (find a driver, etc) */
54881da177e4SLinus Torvalds if (!status) {
54891da177e4SLinus Torvalds status = usb_new_device(udev);
54901da177e4SLinus Torvalds if (status) {
5491d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex);
54921da177e4SLinus Torvalds spin_lock_irq(&device_state_lock);
5493d99f6b41SDan Williams port_dev->child = NULL;
54941da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock);
5495d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex);
549601ed67dcSTony Zheng } else {
549701ed67dcSTony Zheng if (hcd->usb_phy && !hdev->parent)
549801ed67dcSTony Zheng usb_phy_notify_connect(hcd->usb_phy,
549901ed67dcSTony Zheng udev->speed);
55001da177e4SLinus Torvalds }
55011da177e4SLinus Torvalds }
55021da177e4SLinus Torvalds
55031da177e4SLinus Torvalds if (status)
55041da177e4SLinus Torvalds goto loop_disable;
55051da177e4SLinus Torvalds
55061da177e4SLinus Torvalds status = hub_power_remaining(hub);
55071da177e4SLinus Torvalds if (status)
5508d99f6b41SDan Williams dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
55091da177e4SLinus Torvalds
55101da177e4SLinus Torvalds return;
55111da177e4SLinus Torvalds
55121da177e4SLinus Torvalds loop_disable:
55131da177e4SLinus Torvalds hub_port_disable(hub, port1, 1);
55141da177e4SLinus Torvalds loop:
5515fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev);
55163b29b68bSAlan Stern release_devnum(udev);
5517f7410cedSHerbert Xu hub_free_dev(udev);
55186cca13deSMathias Nyman if (retry_locked) {
55196cca13deSMathias Nyman mutex_unlock(hcd->address0_mutex);
55206cca13deSMathias Nyman usb_unlock_port(port_dev);
55216cca13deSMathias Nyman }
55221da177e4SLinus Torvalds usb_put_dev(udev);
5523ffcdc18dSVikram Pandita if ((status == -ENOTCONN) || (status == -ENOTSUPP))
55241da177e4SLinus Torvalds break;
5525973593a9SMike Looijmans
5526973593a9SMike Looijmans /* When halfway through our retry count, power-cycle the port */
552719502e69SAlan Stern if (i == (PORT_INIT_TRIES - 1) / 2) {
5528973593a9SMike Looijmans dev_info(&port_dev->dev, "attempt power cycle\n");
5529973593a9SMike Looijmans usb_hub_set_port_power(hdev, hub, port1, false);
5530973593a9SMike Looijmans msleep(2 * hub_power_on_good_delay(hub));
5531973593a9SMike Looijmans usb_hub_set_port_power(hdev, hub, port1, true);
5532973593a9SMike Looijmans msleep(hub_power_on_good_delay(hub));
5533973593a9SMike Looijmans }
55341da177e4SLinus Torvalds }
55353a31155cSAlan Stern if (hub->hdev->parent ||
55363a31155cSAlan Stern !hcd->driver->port_handed_over ||
5537e9e88fb7SAlan Stern !(hcd->driver->port_handed_over)(hcd, port1)) {
5538e9e88fb7SAlan Stern if (status != -ENOTCONN && status != -ENODEV)
5539d99f6b41SDan Williams dev_err(&port_dev->dev,
5540d99f6b41SDan Williams "unable to enumerate USB device\n");
5541e9e88fb7SAlan Stern }
55421da177e4SLinus Torvalds
55431da177e4SLinus Torvalds done:
55441da177e4SLinus Torvalds hub_port_disable(hub, port1, 1);
554594c43b98SAlan Stern if (hcd->driver->relinquish_port && !hub->hdev->parent) {
554694c43b98SAlan Stern if (status != -ENOTCONN && status != -ENODEV)
554790da096eSBalaji Rao hcd->driver->relinquish_port(hcd, port1);
554894c43b98SAlan Stern }
55491da177e4SLinus Torvalds }
55501da177e4SLinus Torvalds
5551af376a46SDan Williams /* Handle physical or logical connection change events.
5552af376a46SDan Williams * This routine is called when:
5553af376a46SDan Williams * a port connection-change occurs;
5554af376a46SDan Williams * a port enable-change occurs (often caused by EMI);
5555af376a46SDan Williams * usb_reset_and_verify_device() encounters changed descriptors (as from
5556af376a46SDan Williams * a firmware download)
5557af376a46SDan Williams * caller already locked the hub
5558af376a46SDan Williams */
hub_port_connect_change(struct usb_hub * hub,int port1,u16 portstatus,u16 portchange)5559af376a46SDan Williams static void hub_port_connect_change(struct usb_hub *hub, int port1,
556072937e1eSSarah Sharp u16 portstatus, u16 portchange)
55615c79a1e3SDan Williams __must_hold(&port_dev->status_lock)
5562714b07beSSarah Sharp {
5563af376a46SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
5564af376a46SDan Williams struct usb_device *udev = port_dev->child;
5565de28e469SAlan Stern struct usb_device_descriptor *descr;
5566af376a46SDan Williams int status = -ENODEV;
5567714b07beSSarah Sharp
5568af376a46SDan Williams dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
5569af376a46SDan Williams portchange, portspeed(hub, portstatus));
5570af376a46SDan Williams
5571af376a46SDan Williams if (hub->has_indicators) {
5572af376a46SDan Williams set_port_led(hub, port1, HUB_LED_AUTO);
5573af376a46SDan Williams hub->indicator[port1-1] = INDICATOR_AUTO;
55744ee823b8SSarah Sharp }
5575714b07beSSarah Sharp
5576af376a46SDan Williams #ifdef CONFIG_USB_OTG
5577af376a46SDan Williams /* during HNP, don't repeat the debounce */
5578af376a46SDan Williams if (hub->hdev->bus->is_b_host)
5579af376a46SDan Williams portchange &= ~(USB_PORT_STAT_C_CONNECTION |
5580af376a46SDan Williams USB_PORT_STAT_C_ENABLE);
5581af376a46SDan Williams #endif
5582714b07beSSarah Sharp
5583af376a46SDan Williams /* Try to resuscitate an existing device */
5584af376a46SDan Williams if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
5585af376a46SDan Williams udev->state != USB_STATE_NOTATTACHED) {
5586af376a46SDan Williams if (portstatus & USB_PORT_STAT_ENABLE) {
5587a4f55d8bSDavid Heinzelmann /*
5588a4f55d8bSDavid Heinzelmann * USB-3 connections are initialized automatically by
5589a4f55d8bSDavid Heinzelmann * the hostcontroller hardware. Therefore check for
5590a4f55d8bSDavid Heinzelmann * changed device descriptors before resuscitating the
5591a4f55d8bSDavid Heinzelmann * device.
5592a4f55d8bSDavid Heinzelmann */
5593de28e469SAlan Stern descr = usb_get_device_descriptor(udev);
5594de28e469SAlan Stern if (IS_ERR(descr)) {
5595a4f55d8bSDavid Heinzelmann dev_dbg(&udev->dev,
5596de28e469SAlan Stern "can't read device descriptor %ld\n",
5597de28e469SAlan Stern PTR_ERR(descr));
5598a4f55d8bSDavid Heinzelmann } else {
5599de28e469SAlan Stern if (descriptors_changed(udev, descr,
5600a4f55d8bSDavid Heinzelmann udev->bos)) {
5601a4f55d8bSDavid Heinzelmann dev_dbg(&udev->dev,
5602a4f55d8bSDavid Heinzelmann "device descriptor has changed\n");
5603a4f55d8bSDavid Heinzelmann } else {
5604af376a46SDan Williams status = 0; /* Nothing to do */
5605a4f55d8bSDavid Heinzelmann }
5606de28e469SAlan Stern kfree(descr);
5607a4f55d8bSDavid Heinzelmann }
5608ceb6c9c8SRafael J. Wysocki #ifdef CONFIG_PM
5609af376a46SDan Williams } else if (udev->state == USB_STATE_SUSPENDED &&
5610af376a46SDan Williams udev->persist_enabled) {
5611af376a46SDan Williams /* For a suspended device, treat this as a
5612af376a46SDan Williams * remote wakeup event.
5613af376a46SDan Williams */
56145c79a1e3SDan Williams usb_unlock_port(port_dev);
5615af376a46SDan Williams status = usb_remote_wakeup(udev);
56165c79a1e3SDan Williams usb_lock_port(port_dev);
5617af376a46SDan Williams #endif
5618af376a46SDan Williams } else {
5619af376a46SDan Williams /* Don't resuscitate */;
5620af376a46SDan Williams }
5621af376a46SDan Williams }
5622af376a46SDan Williams clear_bit(port1, hub->change_bits);
5623af376a46SDan Williams
56245c79a1e3SDan Williams /* successfully revalidated the connection */
5625af376a46SDan Williams if (status == 0)
5626af376a46SDan Williams return;
5627af376a46SDan Williams
56285c79a1e3SDan Williams usb_unlock_port(port_dev);
5629af376a46SDan Williams hub_port_connect(hub, port1, portstatus, portchange);
56305c79a1e3SDan Williams usb_lock_port(port_dev);
56311da177e4SLinus Torvalds }
56321da177e4SLinus Torvalds
5633201af55dSJon Flatley /* Handle notifying userspace about hub over-current events */
port_over_current_notify(struct usb_port * port_dev)5634201af55dSJon Flatley static void port_over_current_notify(struct usb_port *port_dev)
5635201af55dSJon Flatley {
56366c5ba739SBhuvanesh Surachari char *envp[3] = { NULL, NULL, NULL };
5637201af55dSJon Flatley struct device *hub_dev;
5638201af55dSJon Flatley char *port_dev_path;
5639201af55dSJon Flatley
5640201af55dSJon Flatley sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count");
5641201af55dSJon Flatley
5642201af55dSJon Flatley hub_dev = port_dev->dev.parent;
5643201af55dSJon Flatley
5644201af55dSJon Flatley if (!hub_dev)
5645201af55dSJon Flatley return;
5646201af55dSJon Flatley
5647201af55dSJon Flatley port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL);
5648201af55dSJon Flatley if (!port_dev_path)
5649201af55dSJon Flatley return;
5650201af55dSJon Flatley
5651201af55dSJon Flatley envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path);
5652201af55dSJon Flatley if (!envp[0])
56536c5ba739SBhuvanesh Surachari goto exit;
5654201af55dSJon Flatley
5655201af55dSJon Flatley envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u",
5656201af55dSJon Flatley port_dev->over_current_count);
5657201af55dSJon Flatley if (!envp[1])
5658201af55dSJon Flatley goto exit;
5659201af55dSJon Flatley
5660201af55dSJon Flatley kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp);
5661201af55dSJon Flatley
5662201af55dSJon Flatley exit:
56636c5ba739SBhuvanesh Surachari kfree(envp[1]);
5664201af55dSJon Flatley kfree(envp[0]);
5665bf7f547eSColin Ian King kfree(port_dev_path);
5666201af55dSJon Flatley }
5667201af55dSJon Flatley
port_event(struct usb_hub * hub,int port1)5668af376a46SDan Williams static void port_event(struct usb_hub *hub, int port1)
56695c79a1e3SDan Williams __must_hold(&port_dev->status_lock)
5670af376a46SDan Williams {
56717671bd1eSZhuang Jin Can int connect_change;
5672af376a46SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1];
5673af376a46SDan Williams struct usb_device *udev = port_dev->child;
5674af376a46SDan Williams struct usb_device *hdev = hub->hdev;
5675af376a46SDan Williams u16 portstatus, portchange;
5676f59f93cdSMathias Nyman int i = 0;
5677af376a46SDan Williams
5678af376a46SDan Williams connect_change = test_bit(port1, hub->change_bits);
5679af376a46SDan Williams clear_bit(port1, hub->event_bits);
5680af376a46SDan Williams clear_bit(port1, hub->wakeup_bits);
5681af376a46SDan Williams
5682f061f43dSMichael Grzeschik if (usb_hub_port_status(hub, port1, &portstatus, &portchange) < 0)
5683af376a46SDan Williams return;
5684af376a46SDan Williams
5685af376a46SDan Williams if (portchange & USB_PORT_STAT_C_CONNECTION) {
5686af376a46SDan Williams usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
5687714b07beSSarah Sharp connect_change = 1;
5688714b07beSSarah Sharp }
5689af376a46SDan Williams
5690af376a46SDan Williams if (portchange & USB_PORT_STAT_C_ENABLE) {
5691af376a46SDan Williams if (!connect_change)
5692af376a46SDan Williams dev_dbg(&port_dev->dev, "enable change, status %08x\n",
5693af376a46SDan Williams portstatus);
5694af376a46SDan Williams usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
5695af376a46SDan Williams
5696af376a46SDan Williams /*
5697af376a46SDan Williams * EM interference sometimes causes badly shielded USB devices
5698af376a46SDan Williams * to be shutdown by the hub, this hack enables them again.
5699af376a46SDan Williams * Works at least with mouse driver.
5700af376a46SDan Williams */
5701af376a46SDan Williams if (!(portstatus & USB_PORT_STAT_ENABLE)
5702af376a46SDan Williams && !connect_change && udev) {
5703af376a46SDan Williams dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
5704af376a46SDan Williams connect_change = 1;
5705714b07beSSarah Sharp }
5706af376a46SDan Williams }
5707af376a46SDan Williams
5708af376a46SDan Williams if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
5709af376a46SDan Williams u16 status = 0, unused;
57101cbd53c8SRichard Leitner port_dev->over_current_count++;
5711201af55dSJon Flatley port_over_current_notify(port_dev);
5712af376a46SDan Williams
57131cbd53c8SRichard Leitner dev_dbg(&port_dev->dev, "over-current change #%u\n",
57141cbd53c8SRichard Leitner port_dev->over_current_count);
5715af376a46SDan Williams usb_clear_port_feature(hdev, port1,
5716af376a46SDan Williams USB_PORT_FEAT_C_OVER_CURRENT);
5717af376a46SDan Williams msleep(100); /* Cool down */
5718af376a46SDan Williams hub_power_on(hub, true);
5719f061f43dSMichael Grzeschik usb_hub_port_status(hub, port1, &status, &unused);
5720af376a46SDan Williams if (status & USB_PORT_STAT_OVERCURRENT)
5721af376a46SDan Williams dev_err(&port_dev->dev, "over-current condition\n");
5722af376a46SDan Williams }
5723af376a46SDan Williams
5724af376a46SDan Williams if (portchange & USB_PORT_STAT_C_RESET) {
5725af376a46SDan Williams dev_dbg(&port_dev->dev, "reset change\n");
5726af376a46SDan Williams usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
5727af376a46SDan Williams }
5728af376a46SDan Williams if ((portchange & USB_PORT_STAT_C_BH_RESET)
5729af376a46SDan Williams && hub_is_superspeed(hdev)) {
5730af376a46SDan Williams dev_dbg(&port_dev->dev, "warm reset change\n");
5731af376a46SDan Williams usb_clear_port_feature(hdev, port1,
5732af376a46SDan Williams USB_PORT_FEAT_C_BH_PORT_RESET);
5733af376a46SDan Williams }
5734af376a46SDan Williams if (portchange & USB_PORT_STAT_C_LINK_STATE) {
5735af376a46SDan Williams dev_dbg(&port_dev->dev, "link state change\n");
5736af376a46SDan Williams usb_clear_port_feature(hdev, port1,
5737af376a46SDan Williams USB_PORT_FEAT_C_PORT_LINK_STATE);
5738af376a46SDan Williams }
5739af376a46SDan Williams if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
5740af376a46SDan Williams dev_warn(&port_dev->dev, "config error\n");
5741af376a46SDan Williams usb_clear_port_feature(hdev, port1,
5742af376a46SDan Williams USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
5743af376a46SDan Williams }
5744af376a46SDan Williams
5745097a155fSDan Williams /* skip port actions that require the port to be powered on */
5746097a155fSDan Williams if (!pm_runtime_active(&port_dev->dev))
5747097a155fSDan Williams return;
5748097a155fSDan Williams
5749430d57f5SRay Chi /* skip port actions if ignore_event and early_stop are true */
5750430d57f5SRay Chi if (port_dev->ignore_event && port_dev->early_stop)
5751430d57f5SRay Chi return;
5752430d57f5SRay Chi
5753af376a46SDan Williams if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
5754af376a46SDan Williams connect_change = 1;
5755af376a46SDan Williams
5756af376a46SDan Williams /*
5757f59f93cdSMathias Nyman * Avoid trying to recover a USB3 SS.Inactive port with a warm reset if
5758f59f93cdSMathias Nyman * the device was disconnected. A 12ms disconnect detect timer in
5759f59f93cdSMathias Nyman * SS.Inactive state transitions the port to RxDetect automatically.
5760f59f93cdSMathias Nyman * SS.Inactive link error state is common during device disconnect.
5761af376a46SDan Williams */
5762f59f93cdSMathias Nyman while (hub_port_warm_reset_required(hub, port1, portstatus)) {
5763f59f93cdSMathias Nyman if ((i++ < DETECT_DISCONNECT_TRIES) && udev) {
5764f59f93cdSMathias Nyman u16 unused;
5765f59f93cdSMathias Nyman
5766f59f93cdSMathias Nyman msleep(20);
5767f061f43dSMichael Grzeschik usb_hub_port_status(hub, port1, &portstatus, &unused);
5768f59f93cdSMathias Nyman dev_dbg(&port_dev->dev, "Wait for inactive link disconnect detect\n");
5769f59f93cdSMathias Nyman continue;
5770f59f93cdSMathias Nyman } else if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
5771af376a46SDan Williams || udev->state == USB_STATE_NOTATTACHED) {
5772f59f93cdSMathias Nyman dev_dbg(&port_dev->dev, "do warm reset, port only\n");
5773af376a46SDan Williams if (hub_port_reset(hub, port1, NULL,
5774af376a46SDan Williams HUB_BH_RESET_TIME, true) < 0)
5775af376a46SDan Williams hub_port_disable(hub, port1, 1);
57767671bd1eSZhuang Jin Can } else {
5777f59f93cdSMathias Nyman dev_dbg(&port_dev->dev, "do warm reset, full device\n");
57785c79a1e3SDan Williams usb_unlock_port(port_dev);
5779af376a46SDan Williams usb_lock_device(udev);
5780af376a46SDan Williams usb_reset_device(udev);
5781af376a46SDan Williams usb_unlock_device(udev);
57825c79a1e3SDan Williams usb_lock_port(port_dev);
5783af376a46SDan Williams connect_change = 0;
5784af376a46SDan Williams }
5785f59f93cdSMathias Nyman break;
57867671bd1eSZhuang Jin Can }
5787af376a46SDan Williams
5788af376a46SDan Williams if (connect_change)
5789af376a46SDan Williams hub_port_connect_change(hub, port1, portstatus, portchange);
5790af376a46SDan Williams }
5791af376a46SDan Williams
hub_event(struct work_struct * work)579232a69589SPetr Mladek static void hub_event(struct work_struct *work)
57931da177e4SLinus Torvalds {
57941da177e4SLinus Torvalds struct usb_device *hdev;
57951da177e4SLinus Torvalds struct usb_interface *intf;
57961da177e4SLinus Torvalds struct usb_hub *hub;
57971da177e4SLinus Torvalds struct device *hub_dev;
57981da177e4SLinus Torvalds u16 hubstatus;
57991da177e4SLinus Torvalds u16 hubchange;
58001da177e4SLinus Torvalds int i, ret;
58011da177e4SLinus Torvalds
580232a69589SPetr Mladek hub = container_of(work, struct usb_hub, events);
58035d14f323SPetr Mladek hdev = hub->hdev;
5804e8054854SAlan Stern hub_dev = hub->intfdev;
5805e8054854SAlan Stern intf = to_usb_interface(hub_dev);
580632a69589SPetr Mladek
580795d23dc2SAndrey Konovalov kcov_remote_start_usb((u64)hdev->bus->busnum);
580895d23dc2SAndrey Konovalov
580940f122f3SAlan Stern dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
58103bbc47d8SKrzysztof Mazur hdev->state, hdev->maxchild,
58111da177e4SLinus Torvalds /* NOTE: expects max 15 ports... */
58121da177e4SLinus Torvalds (u16) hub->change_bits[0],
581340f122f3SAlan Stern (u16) hub->event_bits[0]);
58141da177e4SLinus Torvalds
58151da177e4SLinus Torvalds /* Lock the device, then check to see if we were
58161da177e4SLinus Torvalds * disconnected while waiting for the lock to succeed. */
581706b84e8aSAlan Stern usb_lock_device(hdev);
5818e8054854SAlan Stern if (unlikely(hub->disconnected))
581932a69589SPetr Mladek goto out_hdev_lock;
58201da177e4SLinus Torvalds
58211da177e4SLinus Torvalds /* If the hub has died, clean up after it */
58221da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) {
58237de18d8bSAlan Stern hub->error = -ENODEV;
58244330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT);
582532a69589SPetr Mladek goto out_hdev_lock;
58261da177e4SLinus Torvalds }
58271da177e4SLinus Torvalds
582840f122f3SAlan Stern /* Autoresume */
582940f122f3SAlan Stern ret = usb_autopm_get_interface(intf);
583040f122f3SAlan Stern if (ret) {
583140f122f3SAlan Stern dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
583232a69589SPetr Mladek goto out_hdev_lock;
583340f122f3SAlan Stern }
583440f122f3SAlan Stern
583540f122f3SAlan Stern /* If this is an inactive hub, do nothing */
583640f122f3SAlan Stern if (hub->quiescing)
5837eb6e2924SPetr Mladek goto out_autopm;
58381da177e4SLinus Torvalds
58391da177e4SLinus Torvalds if (hub->error) {
5840eb6e2924SPetr Mladek dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
58411da177e4SLinus Torvalds
5842742120c6SMing Lei ret = usb_reset_device(hdev);
58431da177e4SLinus Torvalds if (ret) {
5844eb6e2924SPetr Mladek dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5845eb6e2924SPetr Mladek goto out_autopm;
58461da177e4SLinus Torvalds }
58471da177e4SLinus Torvalds
58481da177e4SLinus Torvalds hub->nerrors = 0;
58491da177e4SLinus Torvalds hub->error = 0;
58501da177e4SLinus Torvalds }
58511da177e4SLinus Torvalds
58521da177e4SLinus Torvalds /* deal with port status changes */
58533bbc47d8SKrzysztof Mazur for (i = 1; i <= hdev->maxchild; i++) {
5854097a155fSDan Williams struct usb_port *port_dev = hub->ports[i - 1];
5855a82b76f7SHans de Goede
58565c79a1e3SDan Williams if (test_bit(i, hub->event_bits)
5857af376a46SDan Williams || test_bit(i, hub->change_bits)
58585c79a1e3SDan Williams || test_bit(i, hub->wakeup_bits)) {
58591da177e4SLinus Torvalds /*
5860097a155fSDan Williams * The get_noresume and barrier ensure that if
5861097a155fSDan Williams * the port was in the process of resuming, we
5862097a155fSDan Williams * flush that work and keep the port active for
5863097a155fSDan Williams * the duration of the port_event(). However,
5864097a155fSDan Williams * if the port is runtime pm suspended
5865097a155fSDan Williams * (powered-off), we leave it in that state, run
5866097a155fSDan Williams * an abbreviated port_event(), and move on.
58671da177e4SLinus Torvalds */
5868097a155fSDan Williams pm_runtime_get_noresume(&port_dev->dev);
5869097a155fSDan Williams pm_runtime_barrier(&port_dev->dev);
58705c79a1e3SDan Williams usb_lock_port(port_dev);
5871af376a46SDan Williams port_event(hub, i);
58725c79a1e3SDan Williams usb_unlock_port(port_dev);
5873097a155fSDan Williams pm_runtime_put_sync(&port_dev->dev);
58741da177e4SLinus Torvalds }
58751da177e4SLinus Torvalds }
58761da177e4SLinus Torvalds
58771da177e4SLinus Torvalds /* deal with hub status changes */
58781da177e4SLinus Torvalds if (test_and_clear_bit(0, hub->event_bits) == 0)
58791da177e4SLinus Torvalds ; /* do nothing */
58801da177e4SLinus Torvalds else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
58811da177e4SLinus Torvalds dev_err(hub_dev, "get_hub_status failed\n");
58821da177e4SLinus Torvalds else {
58831da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_LOCAL_POWER) {
58841da177e4SLinus Torvalds dev_dbg(hub_dev, "power change\n");
58851da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
588655c52718SAlan Stern if (hubstatus & HUB_STATUS_LOCAL_POWER)
588755c52718SAlan Stern /* FIXME: Is this always true? */
588855c52718SAlan Stern hub->limited_power = 1;
5889403fae78Sjidong xiao else
5890403fae78Sjidong xiao hub->limited_power = 0;
58911da177e4SLinus Torvalds }
58921da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_OVERCURRENT) {
5893752d57a8SPaul Bolle u16 status = 0;
5894752d57a8SPaul Bolle u16 unused;
5895752d57a8SPaul Bolle
5896752d57a8SPaul Bolle dev_dbg(hub_dev, "over-current change\n");
58971da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5898752d57a8SPaul Bolle msleep(500); /* Cool down */
58998520f380SAlan Stern hub_power_on(hub, true);
5900752d57a8SPaul Bolle hub_hub_status(hub, &status, &unused);
5901752d57a8SPaul Bolle if (status & HUB_STATUS_OVERCURRENT)
5902eb6e2924SPetr Mladek dev_err(hub_dev, "over-current condition\n");
59031da177e4SLinus Torvalds }
59041da177e4SLinus Torvalds }
59051da177e4SLinus Torvalds
5906eb6e2924SPetr Mladek out_autopm:
59078e4ceb38SAlan Stern /* Balance the usb_autopm_get_interface() above */
59088e4ceb38SAlan Stern usb_autopm_put_interface_no_suspend(intf);
590932a69589SPetr Mladek out_hdev_lock:
59101da177e4SLinus Torvalds usb_unlock_device(hdev);
591132a69589SPetr Mladek
591232a69589SPetr Mladek /* Balance the stuff in kick_hub_wq() and allow autosuspend */
591332a69589SPetr Mladek usb_autopm_put_interface(intf);
59148dbc001bSAlan Stern hub_put(hub);
591595d23dc2SAndrey Konovalov
591695d23dc2SAndrey Konovalov kcov_remote_stop();
59171da177e4SLinus Torvalds }
59181da177e4SLinus Torvalds
59191e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = {
592076e1ef1dSEugeniu Rosca { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
592176e1ef1dSEugeniu Rosca | USB_DEVICE_ID_MATCH_PRODUCT
592276e1ef1dSEugeniu Rosca | USB_DEVICE_ID_MATCH_INT_CLASS,
59231208f9e1SHardik Gajjar .idVendor = USB_VENDOR_SMSC,
592476e1ef1dSEugeniu Rosca .idProduct = USB_PRODUCT_USB5534B,
59251208f9e1SHardik Gajjar .bInterfaceClass = USB_CLASS_HUB,
59261208f9e1SHardik Gajjar .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5927e6f30deaSMing Lei { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5928a7d8d1c7SAndrew Lunn | USB_DEVICE_ID_MATCH_PRODUCT,
5929a7d8d1c7SAndrew Lunn .idVendor = USB_VENDOR_CYPRESS,
5930a7d8d1c7SAndrew Lunn .idProduct = USB_PRODUCT_CY7C65632,
5931a7d8d1c7SAndrew Lunn .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5932a7d8d1c7SAndrew Lunn { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5933e6f30deaSMing Lei | USB_DEVICE_ID_MATCH_INT_CLASS,
5934e6f30deaSMing Lei .idVendor = USB_VENDOR_GENESYS_LOGIC,
5935e6f30deaSMing Lei .bInterfaceClass = USB_CLASS_HUB,
5936e6f30deaSMing Lei .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
59377171b0e2SFlavio Suligoi { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
59387171b0e2SFlavio Suligoi | USB_DEVICE_ID_MATCH_PRODUCT,
59397171b0e2SFlavio Suligoi .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS,
59407171b0e2SFlavio Suligoi .idProduct = USB_PRODUCT_TUSB8041_USB2,
59417171b0e2SFlavio Suligoi .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
59427171b0e2SFlavio Suligoi { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
59437171b0e2SFlavio Suligoi | USB_DEVICE_ID_MATCH_PRODUCT,
59447171b0e2SFlavio Suligoi .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS,
59457171b0e2SFlavio Suligoi .idProduct = USB_PRODUCT_TUSB8041_USB3,
59467171b0e2SFlavio Suligoi .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5947915d900fSHardik Gajjar { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5948915d900fSHardik Gajjar | USB_DEVICE_ID_MATCH_PRODUCT,
5949915d900fSHardik Gajjar .idVendor = USB_VENDOR_MICROCHIP,
5950915d900fSHardik Gajjar .idProduct = USB_PRODUCT_USB4913,
5951915d900fSHardik Gajjar .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL},
5952915d900fSHardik Gajjar { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5953915d900fSHardik Gajjar | USB_DEVICE_ID_MATCH_PRODUCT,
5954915d900fSHardik Gajjar .idVendor = USB_VENDOR_MICROCHIP,
5955915d900fSHardik Gajjar .idProduct = USB_PRODUCT_USB4914,
5956915d900fSHardik Gajjar .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL},
5957915d900fSHardik Gajjar { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5958915d900fSHardik Gajjar | USB_DEVICE_ID_MATCH_PRODUCT,
5959915d900fSHardik Gajjar .idVendor = USB_VENDOR_MICROCHIP,
5960915d900fSHardik Gajjar .idProduct = USB_PRODUCT_USB4915,
5961915d900fSHardik Gajjar .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL},
59621da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
59631da177e4SLinus Torvalds .bDeviceClass = USB_CLASS_HUB},
59641da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
59651da177e4SLinus Torvalds .bInterfaceClass = USB_CLASS_HUB},
59661da177e4SLinus Torvalds { } /* Terminating entry */
59671da177e4SLinus Torvalds };
59681da177e4SLinus Torvalds
59691da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, hub_id_table);
59701da177e4SLinus Torvalds
59711da177e4SLinus Torvalds static struct usb_driver hub_driver = {
59721da177e4SLinus Torvalds .name = "hub",
59731da177e4SLinus Torvalds .probe = hub_probe,
59741da177e4SLinus Torvalds .disconnect = hub_disconnect,
59751da177e4SLinus Torvalds .suspend = hub_suspend,
59761da177e4SLinus Torvalds .resume = hub_resume,
5977f07600cfSAlan Stern .reset_resume = hub_reset_resume,
59787de18d8bSAlan Stern .pre_reset = hub_pre_reset,
59797de18d8bSAlan Stern .post_reset = hub_post_reset,
5980c532b29aSAndi Kleen .unlocked_ioctl = hub_ioctl,
59811da177e4SLinus Torvalds .id_table = hub_id_table,
598240f122f3SAlan Stern .supports_autosuspend = 1,
59831da177e4SLinus Torvalds };
59841da177e4SLinus Torvalds
usb_hub_init(void)59851da177e4SLinus Torvalds int usb_hub_init(void)
59861da177e4SLinus Torvalds {
59871da177e4SLinus Torvalds if (usb_register(&hub_driver) < 0) {
59881da177e4SLinus Torvalds printk(KERN_ERR "%s: can't register hub driver\n",
59891da177e4SLinus Torvalds usbcore_name);
59901da177e4SLinus Torvalds return -1;
59911da177e4SLinus Torvalds }
59921da177e4SLinus Torvalds
599332a69589SPetr Mladek /*
599432a69589SPetr Mladek * The workqueue needs to be freezable to avoid interfering with
599532a69589SPetr Mladek * USB-PERSIST port handover. Otherwise it might see that a full-speed
599632a69589SPetr Mladek * device was gone before the EHCI controller had handed its port
599732a69589SPetr Mladek * over to the companion full-speed controller.
599832a69589SPetr Mladek */
5999638139ebSPetr Mladek hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
600032a69589SPetr Mladek if (hub_wq)
60011da177e4SLinus Torvalds return 0;
60021da177e4SLinus Torvalds
60031da177e4SLinus Torvalds /* Fall through if kernel_thread failed */
60041da177e4SLinus Torvalds usb_deregister(&hub_driver);
600532a69589SPetr Mladek pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
60061da177e4SLinus Torvalds
60071da177e4SLinus Torvalds return -1;
60081da177e4SLinus Torvalds }
60091da177e4SLinus Torvalds
usb_hub_cleanup(void)60101da177e4SLinus Torvalds void usb_hub_cleanup(void)
60111da177e4SLinus Torvalds {
601232a69589SPetr Mladek destroy_workqueue(hub_wq);
60131da177e4SLinus Torvalds
60141da177e4SLinus Torvalds /*
60151da177e4SLinus Torvalds * Hub resources are freed for us by usb_deregister. It calls
60161da177e4SLinus Torvalds * usb_driver_purge on every device which in turn calls that
60171da177e4SLinus Torvalds * devices disconnect function if it is using this driver.
60181da177e4SLinus Torvalds * The hub_disconnect function takes care of releasing the
60191da177e4SLinus Torvalds * individual hub resources. -greg
60201da177e4SLinus Torvalds */
60211da177e4SLinus Torvalds usb_deregister(&hub_driver);
60221da177e4SLinus Torvalds } /* usb_hub_cleanup() */
60231da177e4SLinus Torvalds
60241da177e4SLinus Torvalds /**
6025742120c6SMing Lei * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
60261da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
60271da177e4SLinus Torvalds *
602879efa097SAlan Stern * WARNING - don't use this routine to reset a composite device
602979efa097SAlan Stern * (one with multiple interfaces owned by separate drivers)!
6030742120c6SMing Lei * Use usb_reset_device() instead.
60311da177e4SLinus Torvalds *
60321da177e4SLinus Torvalds * Do a port reset, reassign the device's address, and establish its
60331da177e4SLinus Torvalds * former operating configuration. If the reset fails, or the device's
60341da177e4SLinus Torvalds * descriptors change from their values before the reset, or the original
60351da177e4SLinus Torvalds * configuration and altsettings cannot be restored, a flag will be set
603637ebb549SPetr Mladek * telling hub_wq to pretend the device has been disconnected and then
60371da177e4SLinus Torvalds * re-connected. All drivers will be unbound, and the device will be
60381da177e4SLinus Torvalds * re-enumerated and probed all over again.
60391da177e4SLinus Torvalds *
6040626f090cSYacine Belkadi * Return: 0 if the reset succeeded, -ENODEV if the device has been
60411da177e4SLinus Torvalds * flagged for logical disconnection, or some other negative error code
60421da177e4SLinus Torvalds * if the reset wasn't even attempted.
60431da177e4SLinus Torvalds *
6044626f090cSYacine Belkadi * Note:
60455c79a1e3SDan Williams * The caller must own the device lock and the port lock, the latter is
60465c79a1e3SDan Williams * taken by usb_reset_device(). For example, it's safe to use
60475c79a1e3SDan Williams * usb_reset_device() from a driver probe() routine after downloading
60485c79a1e3SDan Williams * new firmware. For calls that might not occur during probe(), drivers
60495c79a1e3SDan Williams * should lock the device using usb_lock_device_for_reset().
60506bc6cff5SAlan Stern *
60516bc6cff5SAlan Stern * Locking exception: This routine may also be called from within an
60526bc6cff5SAlan Stern * autoresume handler. Such usage won't conflict with other tasks
60536bc6cff5SAlan Stern * holding the device lock because these tasks should always call
60545c79a1e3SDan Williams * usb_autopm_resume_device(), thereby preventing any unwanted
60555c79a1e3SDan Williams * autoresume. The autoresume handler is expected to have already
60565c79a1e3SDan Williams * acquired the port lock before calling this routine.
60571da177e4SLinus Torvalds */
usb_reset_and_verify_device(struct usb_device * udev)6058742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
60591da177e4SLinus Torvalds {
60601da177e4SLinus Torvalds struct usb_device *parent_hdev = udev->parent;
60611da177e4SLinus Torvalds struct usb_hub *parent_hub;
60623f0479e0SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus);
6063ff33299eSAlan Stern struct usb_device_descriptor descriptor;
6064e3376d6cSXenia Ragiadakou struct usb_host_bos *bos;
60657a7b562dSHans de Goede int i, j, ret = 0;
606612c3da34SAlan Stern int port1 = udev->portnum;
60671da177e4SLinus Torvalds
60681da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED ||
60691da177e4SLinus Torvalds udev->state == USB_STATE_SUSPENDED) {
60701da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
60711da177e4SLinus Torvalds udev->state);
60721da177e4SLinus Torvalds return -EINVAL;
60731da177e4SLinus Torvalds }
60741da177e4SLinus Torvalds
60755c79a1e3SDan Williams if (!parent_hdev)
60761da177e4SLinus Torvalds return -EISDIR;
60775c79a1e3SDan Williams
6078ad493e5eSLan Tianyu parent_hub = usb_hub_to_struct_hub(parent_hdev);
60791da177e4SLinus Torvalds
6080dcc01c08SSarah Sharp /* Disable USB2 hardware LPM.
6081dcc01c08SSarah Sharp * It will be re-enabled by the enumeration process.
6082dcc01c08SSarah Sharp */
60837529b257SKai-Heng Feng usb_disable_usb2_hardware_lpm(udev);
6084dcc01c08SSarah Sharp
6085464ad8c4SHans Yang bos = udev->bos;
6086e5bdfd50SGreg Kroah-Hartman udev->bos = NULL;
6087464ad8c4SHans Yang
60886ae6dc22SMathias Nyman mutex_lock(hcd->address0_mutex);
60896ae6dc22SMathias Nyman
609019502e69SAlan Stern for (i = 0; i < PORT_INIT_TRIES; ++i) {
6091430d57f5SRay Chi if (hub_port_stop_enumerate(parent_hub, port1, i)) {
6092430d57f5SRay Chi ret = -ENODEV;
6093430d57f5SRay Chi break;
6094430d57f5SRay Chi }
60951da177e4SLinus Torvalds
60961da177e4SLinus Torvalds /* ep0 maxpacket size may change; let the HCD know about it.
60971da177e4SLinus Torvalds * Other endpoints will be handled by re-enumeration. */
6098fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev);
6099ff33299eSAlan Stern ret = hub_port_init(parent_hub, udev, port1, i, &descriptor);
6100dd4dd19eSAlan Stern if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
61011da177e4SLinus Torvalds break;
61021da177e4SLinus Torvalds }
61036ae6dc22SMathias Nyman mutex_unlock(hcd->address0_mutex);
6104d5cbad4bSAlan Stern
61051da177e4SLinus Torvalds if (ret < 0)
61061da177e4SLinus Torvalds goto re_enumerate;
61071da177e4SLinus Torvalds
61081da177e4SLinus Torvalds /* Device might have changed firmware (DFU or similar) */
6109e3376d6cSXenia Ragiadakou if (descriptors_changed(udev, &descriptor, bos)) {
61101da177e4SLinus Torvalds dev_info(&udev->dev, "device firmware changed\n");
61111da177e4SLinus Torvalds goto re_enumerate;
61121da177e4SLinus Torvalds }
61131da177e4SLinus Torvalds
61144fe0387aSAlan Stern /* Restore the device's previous configuration */
61151da177e4SLinus Torvalds if (!udev->actconfig)
61161da177e4SLinus Torvalds goto done;
61173f0479e0SSarah Sharp
6118d673bfcbSSarah Sharp mutex_lock(hcd->bandwidth_mutex);
61193f0479e0SSarah Sharp ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
61203f0479e0SSarah Sharp if (ret < 0) {
61213f0479e0SSarah Sharp dev_warn(&udev->dev,
61223f0479e0SSarah Sharp "Busted HC? Not enough HCD resources for "
61233f0479e0SSarah Sharp "old configuration.\n");
6124d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex);
61253f0479e0SSarah Sharp goto re_enumerate;
61263f0479e0SSarah Sharp }
61271da177e4SLinus Torvalds ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
61281da177e4SLinus Torvalds USB_REQ_SET_CONFIGURATION, 0,
61291da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, 0,
61301da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT);
61311da177e4SLinus Torvalds if (ret < 0) {
61321da177e4SLinus Torvalds dev_err(&udev->dev,
61331da177e4SLinus Torvalds "can't restore configuration #%d (error=%d)\n",
61341da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, ret);
6135d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex);
61361da177e4SLinus Torvalds goto re_enumerate;
61371da177e4SLinus Torvalds }
6138d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex);
61391da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_CONFIGURED);
61401da177e4SLinus Torvalds
61414fe0387aSAlan Stern /* Put interfaces back into the same altsettings as before.
61424fe0387aSAlan Stern * Don't bother to send the Set-Interface request for interfaces
61434fe0387aSAlan Stern * that were already in altsetting 0; besides being unnecessary,
61444fe0387aSAlan Stern * many devices can't handle it. Instead just reset the host-side
61454fe0387aSAlan Stern * endpoint state.
61464fe0387aSAlan Stern */
61471da177e4SLinus Torvalds for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
61483f0479e0SSarah Sharp struct usb_host_config *config = udev->actconfig;
61493f0479e0SSarah Sharp struct usb_interface *intf = config->interface[i];
61501da177e4SLinus Torvalds struct usb_interface_descriptor *desc;
61511da177e4SLinus Torvalds
61521da177e4SLinus Torvalds desc = &intf->cur_altsetting->desc;
61534fe0387aSAlan Stern if (desc->bAlternateSetting == 0) {
61544fe0387aSAlan Stern usb_disable_interface(udev, intf, true);
61554fe0387aSAlan Stern usb_enable_interface(udev, intf, true);
61564fe0387aSAlan Stern ret = 0;
61574fe0387aSAlan Stern } else {
615804a723eaSSarah Sharp /* Let the bandwidth allocation function know that this
615904a723eaSSarah Sharp * device has been reset, and it will have to use
616004a723eaSSarah Sharp * alternate setting 0 as the current alternate setting.
61613f0479e0SSarah Sharp */
616204a723eaSSarah Sharp intf->resetting_device = 1;
61631da177e4SLinus Torvalds ret = usb_set_interface(udev, desc->bInterfaceNumber,
61641da177e4SLinus Torvalds desc->bAlternateSetting);
616504a723eaSSarah Sharp intf->resetting_device = 0;
61664fe0387aSAlan Stern }
61671da177e4SLinus Torvalds if (ret < 0) {
61681da177e4SLinus Torvalds dev_err(&udev->dev, "failed to restore interface %d "
61691da177e4SLinus Torvalds "altsetting %d (error=%d)\n",
61701da177e4SLinus Torvalds desc->bInterfaceNumber,
61711da177e4SLinus Torvalds desc->bAlternateSetting,
61721da177e4SLinus Torvalds ret);
61731da177e4SLinus Torvalds goto re_enumerate;
61741da177e4SLinus Torvalds }
61757a7b562dSHans de Goede /* Resetting also frees any allocated streams */
61767a7b562dSHans de Goede for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
61777a7b562dSHans de Goede intf->cur_altsetting->endpoint[j].streams = 0;
61781da177e4SLinus Torvalds }
61791da177e4SLinus Torvalds
61801da177e4SLinus Torvalds done:
6181f74631e3SSarah Sharp /* Now that the alt settings are re-installed, enable LTM and LPM. */
61827529b257SKai-Heng Feng usb_enable_usb2_hardware_lpm(udev);
618378d9a487SAlan Stern usb_unlocked_enable_lpm(udev);
6184f74631e3SSarah Sharp usb_enable_ltm(udev);
6185e3376d6cSXenia Ragiadakou usb_release_bos_descriptor(udev);
6186e3376d6cSXenia Ragiadakou udev->bos = bos;
61871da177e4SLinus Torvalds return 0;
61881da177e4SLinus Torvalds
61891da177e4SLinus Torvalds re_enumerate:
6190e3376d6cSXenia Ragiadakou usb_release_bos_descriptor(udev);
6191e3376d6cSXenia Ragiadakou udev->bos = bos;
6192464ad8c4SHans Yang hub_port_logical_disconnect(parent_hub, port1);
61931da177e4SLinus Torvalds return -ENODEV;
61941da177e4SLinus Torvalds }
61951da177e4SLinus Torvalds
61961da177e4SLinus Torvalds /**
61971da177e4SLinus Torvalds * usb_reset_device - warn interface drivers and perform a USB port reset
6198e76b3bf7SKai-Heng Feng * @udev: device to reset (not in NOTATTACHED state)
61991da177e4SLinus Torvalds *
620079efa097SAlan Stern * Warns all drivers bound to registered interfaces (using their pre_reset
620179efa097SAlan Stern * method), performs the port reset, and then lets the drivers know that
6202742120c6SMing Lei * the reset is over (using their post_reset method).
620379efa097SAlan Stern *
6204626f090cSYacine Belkadi * Return: The same as for usb_reset_and_verify_device().
62059c6d7788SAlan Stern * However, if a reset is already in progress (for instance, if a
6206766a96dcSAlan Stern * driver doesn't have pre_reset() or post_reset() callbacks, and while
62079c6d7788SAlan Stern * being unbound or re-bound during the ongoing reset its disconnect()
62089c6d7788SAlan Stern * or probe() routine tries to perform a second, nested reset), the
62099c6d7788SAlan Stern * routine returns -EINPROGRESS.
621079efa097SAlan Stern *
6211626f090cSYacine Belkadi * Note:
621279efa097SAlan Stern * The caller must own the device lock. For example, it's safe to use
621379efa097SAlan Stern * this from a driver probe() routine after downloading new firmware.
621479efa097SAlan Stern * For calls that might not occur during probe(), drivers should lock
6215742120c6SMing Lei * the device using usb_lock_device_for_reset().
621679efa097SAlan Stern *
621779efa097SAlan Stern * If an interface is currently being probed or disconnected, we assume
621879efa097SAlan Stern * its driver knows how to handle resets. For all other interfaces,
621979efa097SAlan Stern * if the driver doesn't have pre_reset and post_reset methods then
622079efa097SAlan Stern * we attempt to unbind it and rebind afterward.
622179efa097SAlan Stern */
usb_reset_device(struct usb_device * udev)6222742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
622379efa097SAlan Stern {
622479efa097SAlan Stern int ret;
6225852c4b43SAlan Stern int i;
62264d769defSMing Lei unsigned int noio_flag;
62275c79a1e3SDan Williams struct usb_port *port_dev;
622879efa097SAlan Stern struct usb_host_config *config = udev->actconfig;
62295c79a1e3SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
623079efa097SAlan Stern
6231e76b3bf7SKai-Heng Feng if (udev->state == USB_STATE_NOTATTACHED) {
623279efa097SAlan Stern dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
623379efa097SAlan Stern udev->state);
623479efa097SAlan Stern return -EINVAL;
623579efa097SAlan Stern }
623679efa097SAlan Stern
62375c79a1e3SDan Williams if (!udev->parent) {
62385c79a1e3SDan Williams /* this requires hcd-specific logic; see ohci_restart() */
62395c79a1e3SDan Williams dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
62405c79a1e3SDan Williams return -EISDIR;
62415c79a1e3SDan Williams }
62425c79a1e3SDan Williams
62439c6d7788SAlan Stern if (udev->reset_in_progress)
62449c6d7788SAlan Stern return -EINPROGRESS;
62459c6d7788SAlan Stern udev->reset_in_progress = 1;
62469c6d7788SAlan Stern
62475c79a1e3SDan Williams port_dev = hub->ports[udev->portnum - 1];
62485c79a1e3SDan Williams
62494d769defSMing Lei /*
62504d769defSMing Lei * Don't allocate memory with GFP_KERNEL in current
62514d769defSMing Lei * context to avoid possible deadlock if usb mass
62524d769defSMing Lei * storage interface or usbnet interface(iSCSI case)
62534d769defSMing Lei * is included in current configuration. The easist
62544d769defSMing Lei * approach is to do it for every device reset,
62554d769defSMing Lei * because the device 'memalloc_noio' flag may have
62564d769defSMing Lei * not been set before reseting the usb device.
62574d769defSMing Lei */
62584d769defSMing Lei noio_flag = memalloc_noio_save();
62594d769defSMing Lei
6260645daaabSAlan Stern /* Prevent autosuspend during the reset */
626194fcda1fSAlan Stern usb_autoresume_device(udev);
6262645daaabSAlan Stern
626379efa097SAlan Stern if (config) {
6264852c4b43SAlan Stern for (i = 0; i < config->desc.bNumInterfaces; ++i) {
6265852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i];
626679efa097SAlan Stern struct usb_driver *drv;
626778d9a487SAlan Stern int unbind = 0;
626879efa097SAlan Stern
6269852c4b43SAlan Stern if (cintf->dev.driver) {
627079efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver);
627178d9a487SAlan Stern if (drv->pre_reset && drv->post_reset)
627278d9a487SAlan Stern unbind = (drv->pre_reset)(cintf);
627378d9a487SAlan Stern else if (cintf->condition ==
627478d9a487SAlan Stern USB_INTERFACE_BOUND)
627578d9a487SAlan Stern unbind = 1;
627678d9a487SAlan Stern if (unbind)
627778d9a487SAlan Stern usb_forced_unbind_intf(cintf);
627879efa097SAlan Stern }
627979efa097SAlan Stern }
628079efa097SAlan Stern }
628179efa097SAlan Stern
62825c79a1e3SDan Williams usb_lock_port(port_dev);
6283742120c6SMing Lei ret = usb_reset_and_verify_device(udev);
62845c79a1e3SDan Williams usb_unlock_port(port_dev);
628579efa097SAlan Stern
628679efa097SAlan Stern if (config) {
6287852c4b43SAlan Stern for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
6288852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i];
628979efa097SAlan Stern struct usb_driver *drv;
629078d9a487SAlan Stern int rebind = cintf->needs_binding;
629179efa097SAlan Stern
629278d9a487SAlan Stern if (!rebind && cintf->dev.driver) {
629379efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver);
629479efa097SAlan Stern if (drv->post_reset)
629578d9a487SAlan Stern rebind = (drv->post_reset)(cintf);
629678d9a487SAlan Stern else if (cintf->condition ==
629778d9a487SAlan Stern USB_INTERFACE_BOUND)
629878d9a487SAlan Stern rebind = 1;
62996aec044cSAlan Stern if (rebind)
63006aec044cSAlan Stern cintf->needs_binding = 1;
630179efa097SAlan Stern }
630279efa097SAlan Stern }
6303381419faSAlan Stern
6304381419faSAlan Stern /* If the reset failed, hub_wq will unbind drivers later */
6305381419faSAlan Stern if (ret == 0)
63066aec044cSAlan Stern usb_unbind_and_rebind_marked_interfaces(udev);
630779efa097SAlan Stern }
630879efa097SAlan Stern
630994fcda1fSAlan Stern usb_autosuspend_device(udev);
63104d769defSMing Lei memalloc_noio_restore(noio_flag);
63119c6d7788SAlan Stern udev->reset_in_progress = 0;
631279efa097SAlan Stern return ret;
631379efa097SAlan Stern }
6314742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
6315dc023dceSInaky Perez-Gonzalez
6316dc023dceSInaky Perez-Gonzalez
6317dc023dceSInaky Perez-Gonzalez /**
6318dc023dceSInaky Perez-Gonzalez * usb_queue_reset_device - Reset a USB device from an atomic context
6319dc023dceSInaky Perez-Gonzalez * @iface: USB interface belonging to the device to reset
6320dc023dceSInaky Perez-Gonzalez *
6321dc023dceSInaky Perez-Gonzalez * This function can be used to reset a USB device from an atomic
6322dc023dceSInaky Perez-Gonzalez * context, where usb_reset_device() won't work (as it blocks).
6323dc023dceSInaky Perez-Gonzalez *
6324dc023dceSInaky Perez-Gonzalez * Doing a reset via this method is functionally equivalent to calling
6325dc023dceSInaky Perez-Gonzalez * usb_reset_device(), except for the fact that it is delayed to a
6326dc023dceSInaky Perez-Gonzalez * workqueue. This means that any drivers bound to other interfaces
6327dc023dceSInaky Perez-Gonzalez * might be unbound, as well as users from usbfs in user space.
6328dc023dceSInaky Perez-Gonzalez *
6329dc023dceSInaky Perez-Gonzalez * Corner cases:
6330dc023dceSInaky Perez-Gonzalez *
6331dc023dceSInaky Perez-Gonzalez * - Scheduling two resets at the same time from two different drivers
6332dc023dceSInaky Perez-Gonzalez * attached to two different interfaces of the same device is
6333dc023dceSInaky Perez-Gonzalez * possible; depending on how the driver attached to each interface
6334dc023dceSInaky Perez-Gonzalez * handles ->pre_reset(), the second reset might happen or not.
6335dc023dceSInaky Perez-Gonzalez *
6336524134d4SAlan Stern * - If the reset is delayed so long that the interface is unbound from
6337524134d4SAlan Stern * its driver, the reset will be skipped.
6338dc023dceSInaky Perez-Gonzalez *
6339524134d4SAlan Stern * - This function can be called during .probe(). It can also be called
6340524134d4SAlan Stern * during .disconnect(), but doing so is pointless because the reset
6341524134d4SAlan Stern * will not occur. If you really want to reset the device during
6342524134d4SAlan Stern * .disconnect(), call usb_reset_device() directly -- but watch out
6343524134d4SAlan Stern * for nested unbinding issues!
6344dc023dceSInaky Perez-Gonzalez */
usb_queue_reset_device(struct usb_interface * iface)6345dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
6346dc023dceSInaky Perez-Gonzalez {
6347524134d4SAlan Stern if (schedule_work(&iface->reset_ws))
6348524134d4SAlan Stern usb_get_intf(iface);
6349dc023dceSInaky Perez-Gonzalez }
6350dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
6351ff823c79SLan Tianyu
6352ff823c79SLan Tianyu /**
6353ff823c79SLan Tianyu * usb_hub_find_child - Get the pointer of child device
6354ff823c79SLan Tianyu * attached to the port which is specified by @port1.
6355ff823c79SLan Tianyu * @hdev: USB device belonging to the usb hub
6356ff823c79SLan Tianyu * @port1: port num to indicate which port the child device
6357ff823c79SLan Tianyu * is attached to.
6358ff823c79SLan Tianyu *
6359ff823c79SLan Tianyu * USB drivers call this function to get hub's child device
6360ff823c79SLan Tianyu * pointer.
6361ff823c79SLan Tianyu *
6362626f090cSYacine Belkadi * Return: %NULL if input param is invalid and
6363ff823c79SLan Tianyu * child's usb_device pointer if non-NULL.
6364ff823c79SLan Tianyu */
usb_hub_find_child(struct usb_device * hdev,int port1)6365ff823c79SLan Tianyu struct usb_device *usb_hub_find_child(struct usb_device *hdev,
6366ff823c79SLan Tianyu int port1)
6367ff823c79SLan Tianyu {
6368ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6369ff823c79SLan Tianyu
6370ff823c79SLan Tianyu if (port1 < 1 || port1 > hdev->maxchild)
6371ff823c79SLan Tianyu return NULL;
6372ff823c79SLan Tianyu return hub->ports[port1 - 1]->child;
6373ff823c79SLan Tianyu }
6374ff823c79SLan Tianyu EXPORT_SYMBOL_GPL(usb_hub_find_child);
6375d5575424SLan Tianyu
usb_hub_adjust_deviceremovable(struct usb_device * hdev,struct usb_hub_descriptor * desc)6376d2123fd9SLan Tianyu void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
6377d2123fd9SLan Tianyu struct usb_hub_descriptor *desc)
6378d2123fd9SLan Tianyu {
6379d99f6b41SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6380d2123fd9SLan Tianyu enum usb_port_connect_type connect_type;
6381d2123fd9SLan Tianyu int i;
6382d2123fd9SLan Tianyu
6383d99f6b41SDan Williams if (!hub)
6384d99f6b41SDan Williams return;
6385d99f6b41SDan Williams
6386d2123fd9SLan Tianyu if (!hub_is_superspeed(hdev)) {
6387d2123fd9SLan Tianyu for (i = 1; i <= hdev->maxchild; i++) {
6388d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[i - 1];
6389d2123fd9SLan Tianyu
6390d99f6b41SDan Williams connect_type = port_dev->connect_type;
6391d2123fd9SLan Tianyu if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
6392d2123fd9SLan Tianyu u8 mask = 1 << (i%8);
6393d2123fd9SLan Tianyu
6394d2123fd9SLan Tianyu if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
6395d99f6b41SDan Williams dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
6396d2123fd9SLan Tianyu desc->u.hs.DeviceRemovable[i/8] |= mask;
6397d2123fd9SLan Tianyu }
6398d2123fd9SLan Tianyu }
6399d2123fd9SLan Tianyu }
6400d2123fd9SLan Tianyu } else {
6401d2123fd9SLan Tianyu u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
6402d2123fd9SLan Tianyu
6403d2123fd9SLan Tianyu for (i = 1; i <= hdev->maxchild; i++) {
6404d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[i - 1];
6405d2123fd9SLan Tianyu
6406d99f6b41SDan Williams connect_type = port_dev->connect_type;
6407d2123fd9SLan Tianyu if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
6408d2123fd9SLan Tianyu u16 mask = 1 << i;
6409d2123fd9SLan Tianyu
6410d2123fd9SLan Tianyu if (!(port_removable & mask)) {
6411d99f6b41SDan Williams dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
6412d2123fd9SLan Tianyu port_removable |= mask;
6413d2123fd9SLan Tianyu }
6414d2123fd9SLan Tianyu }
6415d2123fd9SLan Tianyu }
6416d2123fd9SLan Tianyu
6417d2123fd9SLan Tianyu desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
6418d2123fd9SLan Tianyu }
6419d2123fd9SLan Tianyu }
6420d2123fd9SLan Tianyu
6421d5575424SLan Tianyu #ifdef CONFIG_ACPI
6422d5575424SLan Tianyu /**
6423d5575424SLan Tianyu * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
6424d5575424SLan Tianyu * @hdev: USB device belonging to the usb hub
6425d5575424SLan Tianyu * @port1: port num of the port
6426d5575424SLan Tianyu *
6427626f090cSYacine Belkadi * Return: Port's acpi handle if successful, %NULL if params are
6428626f090cSYacine Belkadi * invalid.
6429d5575424SLan Tianyu */
usb_get_hub_port_acpi_handle(struct usb_device * hdev,int port1)6430d5575424SLan Tianyu acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
6431d5575424SLan Tianyu int port1)
6432d5575424SLan Tianyu {
6433ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6434d5575424SLan Tianyu
643541341261SMathias Nyman if (!hub)
643641341261SMathias Nyman return NULL;
643741341261SMathias Nyman
64383a83f992SRafael J. Wysocki return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
6439d5575424SLan Tianyu }
6440d5575424SLan Tianyu #endif
6441