xref: /openbmc/linux/drivers/usb/core/hub.c (revision 01ed67dc)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * USB hub driver.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Linus Torvalds
51da177e4SLinus Torvalds  * (C) Copyright 1999 Johannes Erdfelt
61da177e4SLinus Torvalds  * (C) Copyright 1999 Gregory P. Smith
71da177e4SLinus Torvalds  * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/completion.h>
161da177e4SLinus Torvalds #include <linux/sched.h>
171da177e4SLinus Torvalds #include <linux/list.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/ioctl.h>
201da177e4SLinus Torvalds #include <linux/usb.h>
211da177e4SLinus Torvalds #include <linux/usbdevice_fs.h>
2227729aadSEric Lescouet #include <linux/usb/hcd.h>
23925aa46bSRichard Zhao #include <linux/usb/otg.h>
2493362a87SPhil Dibowitz #include <linux/usb/quirks.h>
2532a69589SPetr Mladek #include <linux/workqueue.h>
264186ecf8SArjan van de Ven #include <linux/mutex.h>
27b04b3156STheodore Ts'o #include <linux/random.h>
28ad493e5eSLan Tianyu #include <linux/pm_qos.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <asm/uaccess.h>
311da177e4SLinus Torvalds #include <asm/byteorder.h>
321da177e4SLinus Torvalds 
336e30d7cbSLan Tianyu #include "hub.h"
34026f3fcbSPeter Chen #include "otg_whitelist.h"
351da177e4SLinus Torvalds 
36e6f30deaSMing Lei #define USB_VENDOR_GENESYS_LOGIC		0x05e3
37e6f30deaSMing Lei #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND	0x01
38e6f30deaSMing Lei 
39fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members
409ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
411da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
421da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
431da177e4SLinus Torvalds 
4432a69589SPetr Mladek /* workqueue to process hub events */
4532a69589SPetr Mladek static struct workqueue_struct *hub_wq;
4632a69589SPetr Mladek static void hub_event(struct work_struct *work);
471da177e4SLinus Torvalds 
48d8521afeSDan Williams /* synchronize hub-port add/remove and peering operations */
49d8521afeSDan Williams DEFINE_MUTEX(usb_port_peer_mutex);
50d8521afeSDan Williams 
511da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
5290ab5ee9SRusty Russell static bool blinkenlights = 0;
531da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
541da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
57fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
58fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
59fd7c519dSJaroslav Kysela  */
60fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
61fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
62fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
63b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
64b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
65b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
66fd7c519dSJaroslav Kysela 
67fd7c519dSJaroslav Kysela /*
681da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
691da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
701da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
711da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
721da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
731da177e4SLinus Torvalds  * following module parameters.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
761da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
771da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
781da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
791da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
801da177e4SLinus Torvalds  */
8190ab5ee9SRusty Russell static bool old_scheme_first = 0;
821da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
831da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
841da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
851da177e4SLinus Torvalds 
8690ab5ee9SRusty Russell static bool use_both_schemes = 1;
871da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
881da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
891da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
901da177e4SLinus Torvalds 		"first one fails");
911da177e4SLinus Torvalds 
9232fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
9332fe0198SAlan Stern  * port reset on some companion controllers.
9432fe0198SAlan Stern  */
9532fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
9632fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
9732fe0198SAlan Stern 
98ad493e5eSLan Tianyu #define HUB_DEBOUNCE_TIMEOUT	2000
99948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
100948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
101948fea37SAlan Stern 
10232a69589SPetr Mladek static void hub_release(struct kref *kref);
103742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
104742120c6SMing Lei 
105131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus)
1061da177e4SLinus Torvalds {
107131dec34SSarah Sharp 	if (hub_is_superspeed(hub->hdev))
108131dec34SSarah Sharp 		return "5.0 Gb/s";
109288ead45SAlan Stern 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1101da177e4SLinus Torvalds 		return "480 Mb/s";
111288ead45SAlan Stern 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1121da177e4SLinus Torvalds 		return "1.5 Mb/s";
1131da177e4SLinus Torvalds 	else
1141da177e4SLinus Torvalds 		return "12 Mb/s";
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
118ad493e5eSLan Tianyu struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
1191da177e4SLinus Torvalds {
120ff823c79SLan Tianyu 	if (!hdev || !hdev->actconfig || !hdev->maxchild)
12125118084SAlan Stern 		return NULL;
1221da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
125140e3026SSarah Sharp static int usb_device_supports_lpm(struct usb_device *udev)
126d9b2099cSSarah Sharp {
127d9b2099cSSarah Sharp 	/* USB 2.1 (and greater) devices indicate LPM support through
128d9b2099cSSarah Sharp 	 * their USB 2.0 Extended Capabilities BOS descriptor.
129d9b2099cSSarah Sharp 	 */
130d9b2099cSSarah Sharp 	if (udev->speed == USB_SPEED_HIGH) {
131d9b2099cSSarah Sharp 		if (udev->bos->ext_cap &&
132d9b2099cSSarah Sharp 			(USB_LPM_SUPPORT &
133d9b2099cSSarah Sharp 			 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
134d9b2099cSSarah Sharp 			return 1;
135d9b2099cSSarah Sharp 		return 0;
136d9b2099cSSarah Sharp 	}
13751e0a012SSarah Sharp 
13825cd2882SSarah Sharp 	/*
13925cd2882SSarah Sharp 	 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
14025cd2882SSarah Sharp 	 * However, there are some that don't, and they set the U1/U2 exit
14125cd2882SSarah Sharp 	 * latencies to zero.
14251e0a012SSarah Sharp 	 */
14351e0a012SSarah Sharp 	if (!udev->bos->ss_cap) {
14425cd2882SSarah Sharp 		dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
145d9b2099cSSarah Sharp 		return 0;
146d9b2099cSSarah Sharp 	}
14751e0a012SSarah Sharp 
14825cd2882SSarah Sharp 	if (udev->bos->ss_cap->bU1devExitLat == 0 &&
14925cd2882SSarah Sharp 			udev->bos->ss_cap->bU2DevExitLat == 0) {
15025cd2882SSarah Sharp 		if (udev->parent)
15125cd2882SSarah Sharp 			dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
15225cd2882SSarah Sharp 		else
15325cd2882SSarah Sharp 			dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
15425cd2882SSarah Sharp 		return 0;
15525cd2882SSarah Sharp 	}
15625cd2882SSarah Sharp 
15725cd2882SSarah Sharp 	if (!udev->parent || udev->parent->lpm_capable)
15825cd2882SSarah Sharp 		return 1;
15951e0a012SSarah Sharp 	return 0;
16051e0a012SSarah Sharp }
16151e0a012SSarah Sharp 
16251e0a012SSarah Sharp /*
16351e0a012SSarah Sharp  * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
16451e0a012SSarah Sharp  * either U1 or U2.
16551e0a012SSarah Sharp  */
16651e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev,
16751e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params,
16851e0a012SSarah Sharp 		unsigned int udev_exit_latency,
16951e0a012SSarah Sharp 		struct usb_hub *hub,
17051e0a012SSarah Sharp 		struct usb3_lpm_parameters *hub_lpm_params,
17151e0a012SSarah Sharp 		unsigned int hub_exit_latency)
17251e0a012SSarah Sharp {
17351e0a012SSarah Sharp 	unsigned int total_mel;
17451e0a012SSarah Sharp 	unsigned int device_mel;
17551e0a012SSarah Sharp 	unsigned int hub_mel;
17651e0a012SSarah Sharp 
17751e0a012SSarah Sharp 	/*
17851e0a012SSarah Sharp 	 * Calculate the time it takes to transition all links from the roothub
17951e0a012SSarah Sharp 	 * to the parent hub into U0.  The parent hub must then decode the
18051e0a012SSarah Sharp 	 * packet (hub header decode latency) to figure out which port it was
18151e0a012SSarah Sharp 	 * bound for.
18251e0a012SSarah Sharp 	 *
18351e0a012SSarah Sharp 	 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
18451e0a012SSarah Sharp 	 * means 0.1us).  Multiply that by 100 to get nanoseconds.
18551e0a012SSarah Sharp 	 */
18651e0a012SSarah Sharp 	total_mel = hub_lpm_params->mel +
18751e0a012SSarah Sharp 		(hub->descriptor->u.ss.bHubHdrDecLat * 100);
18851e0a012SSarah Sharp 
18951e0a012SSarah Sharp 	/*
19051e0a012SSarah Sharp 	 * How long will it take to transition the downstream hub's port into
19151e0a012SSarah Sharp 	 * U0?  The greater of either the hub exit latency or the device exit
19251e0a012SSarah Sharp 	 * latency.
19351e0a012SSarah Sharp 	 *
19451e0a012SSarah Sharp 	 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
19551e0a012SSarah Sharp 	 * Multiply that by 1000 to get nanoseconds.
19651e0a012SSarah Sharp 	 */
19751e0a012SSarah Sharp 	device_mel = udev_exit_latency * 1000;
19851e0a012SSarah Sharp 	hub_mel = hub_exit_latency * 1000;
19951e0a012SSarah Sharp 	if (device_mel > hub_mel)
20051e0a012SSarah Sharp 		total_mel += device_mel;
20151e0a012SSarah Sharp 	else
20251e0a012SSarah Sharp 		total_mel += hub_mel;
20351e0a012SSarah Sharp 
20451e0a012SSarah Sharp 	udev_lpm_params->mel = total_mel;
20551e0a012SSarah Sharp }
20651e0a012SSarah Sharp 
20751e0a012SSarah Sharp /*
20851e0a012SSarah Sharp  * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
20951e0a012SSarah Sharp  * a transition from either U1 or U2.
21051e0a012SSarah Sharp  */
21151e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev,
21251e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params,
21351e0a012SSarah Sharp 		unsigned int udev_exit_latency,
21451e0a012SSarah Sharp 		struct usb_hub *hub,
21551e0a012SSarah Sharp 		struct usb3_lpm_parameters *hub_lpm_params,
21651e0a012SSarah Sharp 		unsigned int hub_exit_latency,
21751e0a012SSarah Sharp 		unsigned int port_to_port_exit_latency)
21851e0a012SSarah Sharp {
21951e0a012SSarah Sharp 	unsigned int first_link_pel;
22051e0a012SSarah Sharp 	unsigned int hub_pel;
22151e0a012SSarah Sharp 
22251e0a012SSarah Sharp 	/*
22351e0a012SSarah Sharp 	 * First, the device sends an LFPS to transition the link between the
22451e0a012SSarah Sharp 	 * device and the parent hub into U0.  The exit latency is the bigger of
22551e0a012SSarah Sharp 	 * the device exit latency or the hub exit latency.
22651e0a012SSarah Sharp 	 */
22751e0a012SSarah Sharp 	if (udev_exit_latency > hub_exit_latency)
22851e0a012SSarah Sharp 		first_link_pel = udev_exit_latency * 1000;
22951e0a012SSarah Sharp 	else
23051e0a012SSarah Sharp 		first_link_pel = hub_exit_latency * 1000;
23151e0a012SSarah Sharp 
23251e0a012SSarah Sharp 	/*
23351e0a012SSarah Sharp 	 * When the hub starts to receive the LFPS, there is a slight delay for
23451e0a012SSarah Sharp 	 * it to figure out that one of the ports is sending an LFPS.  Then it
23551e0a012SSarah Sharp 	 * will forward the LFPS to its upstream link.  The exit latency is the
23651e0a012SSarah Sharp 	 * delay, plus the PEL that we calculated for this hub.
23751e0a012SSarah Sharp 	 */
23851e0a012SSarah Sharp 	hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
23951e0a012SSarah Sharp 
24051e0a012SSarah Sharp 	/*
24151e0a012SSarah Sharp 	 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
24251e0a012SSarah Sharp 	 * is the greater of the two exit latencies.
24351e0a012SSarah Sharp 	 */
24451e0a012SSarah Sharp 	if (first_link_pel > hub_pel)
24551e0a012SSarah Sharp 		udev_lpm_params->pel = first_link_pel;
24651e0a012SSarah Sharp 	else
24751e0a012SSarah Sharp 		udev_lpm_params->pel = hub_pel;
24851e0a012SSarah Sharp }
24951e0a012SSarah Sharp 
25051e0a012SSarah Sharp /*
25151e0a012SSarah Sharp  * Set the System Exit Latency (SEL) to indicate the total worst-case time from
25251e0a012SSarah Sharp  * when a device initiates a transition to U0, until when it will receive the
25351e0a012SSarah Sharp  * first packet from the host controller.
25451e0a012SSarah Sharp  *
25551e0a012SSarah Sharp  * Section C.1.5.1 describes the four components to this:
25651e0a012SSarah Sharp  *  - t1: device PEL
25751e0a012SSarah Sharp  *  - t2: time for the ERDY to make it from the device to the host.
25851e0a012SSarah Sharp  *  - t3: a host-specific delay to process the ERDY.
25951e0a012SSarah Sharp  *  - t4: time for the packet to make it from the host to the device.
26051e0a012SSarah Sharp  *
26151e0a012SSarah Sharp  * t3 is specific to both the xHCI host and the platform the host is integrated
26251e0a012SSarah Sharp  * into.  The Intel HW folks have said it's negligible, FIXME if a different
26351e0a012SSarah Sharp  * vendor says otherwise.
26451e0a012SSarah Sharp  */
26551e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev,
26651e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params)
26751e0a012SSarah Sharp {
26851e0a012SSarah Sharp 	struct usb_device *parent;
26951e0a012SSarah Sharp 	unsigned int num_hubs;
27051e0a012SSarah Sharp 	unsigned int total_sel;
27151e0a012SSarah Sharp 
27251e0a012SSarah Sharp 	/* t1 = device PEL */
27351e0a012SSarah Sharp 	total_sel = udev_lpm_params->pel;
27451e0a012SSarah Sharp 	/* How many external hubs are in between the device & the root port. */
27551e0a012SSarah Sharp 	for (parent = udev->parent, num_hubs = 0; parent->parent;
27651e0a012SSarah Sharp 			parent = parent->parent)
27751e0a012SSarah Sharp 		num_hubs++;
27851e0a012SSarah Sharp 	/* t2 = 2.1us + 250ns * (num_hubs - 1) */
27951e0a012SSarah Sharp 	if (num_hubs > 0)
28051e0a012SSarah Sharp 		total_sel += 2100 + 250 * (num_hubs - 1);
28151e0a012SSarah Sharp 
28251e0a012SSarah Sharp 	/* t4 = 250ns * num_hubs */
28351e0a012SSarah Sharp 	total_sel += 250 * num_hubs;
28451e0a012SSarah Sharp 
28551e0a012SSarah Sharp 	udev_lpm_params->sel = total_sel;
28651e0a012SSarah Sharp }
28751e0a012SSarah Sharp 
28851e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev)
28951e0a012SSarah Sharp {
29051e0a012SSarah Sharp 	struct usb_hub *hub;
29151e0a012SSarah Sharp 	unsigned int port_to_port_delay;
29251e0a012SSarah Sharp 	unsigned int udev_u1_del;
29351e0a012SSarah Sharp 	unsigned int udev_u2_del;
29451e0a012SSarah Sharp 	unsigned int hub_u1_del;
29551e0a012SSarah Sharp 	unsigned int hub_u2_del;
29651e0a012SSarah Sharp 
29751e0a012SSarah Sharp 	if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
29851e0a012SSarah Sharp 		return;
29951e0a012SSarah Sharp 
300ad493e5eSLan Tianyu 	hub = usb_hub_to_struct_hub(udev->parent);
30151e0a012SSarah Sharp 	/* It doesn't take time to transition the roothub into U0, since it
30251e0a012SSarah Sharp 	 * doesn't have an upstream link.
30351e0a012SSarah Sharp 	 */
30451e0a012SSarah Sharp 	if (!hub)
30551e0a012SSarah Sharp 		return;
30651e0a012SSarah Sharp 
30751e0a012SSarah Sharp 	udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
3084d967995SXenia Ragiadakou 	udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
30951e0a012SSarah Sharp 	hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
3104d967995SXenia Ragiadakou 	hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
31151e0a012SSarah Sharp 
31251e0a012SSarah Sharp 	usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
31351e0a012SSarah Sharp 			hub, &udev->parent->u1_params, hub_u1_del);
31451e0a012SSarah Sharp 
31551e0a012SSarah Sharp 	usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
31651e0a012SSarah Sharp 			hub, &udev->parent->u2_params, hub_u2_del);
31751e0a012SSarah Sharp 
31851e0a012SSarah Sharp 	/*
31951e0a012SSarah Sharp 	 * Appendix C, section C.2.2.2, says that there is a slight delay from
32051e0a012SSarah Sharp 	 * when the parent hub notices the downstream port is trying to
32151e0a012SSarah Sharp 	 * transition to U0 to when the hub initiates a U0 transition on its
32251e0a012SSarah Sharp 	 * upstream port.  The section says the delays are tPort2PortU1EL and
32351e0a012SSarah Sharp 	 * tPort2PortU2EL, but it doesn't define what they are.
32451e0a012SSarah Sharp 	 *
32551e0a012SSarah Sharp 	 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
32651e0a012SSarah Sharp 	 * about the same delays.  Use the maximum delay calculations from those
32751e0a012SSarah Sharp 	 * sections.  For U1, it's tHubPort2PortExitLat, which is 1us max.  For
32851e0a012SSarah Sharp 	 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat.  I
32951e0a012SSarah Sharp 	 * assume the device exit latencies they are talking about are the hub
33051e0a012SSarah Sharp 	 * exit latencies.
33151e0a012SSarah Sharp 	 *
33251e0a012SSarah Sharp 	 * What do we do if the U2 exit latency is less than the U1 exit
33351e0a012SSarah Sharp 	 * latency?  It's possible, although not likely...
33451e0a012SSarah Sharp 	 */
33551e0a012SSarah Sharp 	port_to_port_delay = 1;
33651e0a012SSarah Sharp 
33751e0a012SSarah Sharp 	usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
33851e0a012SSarah Sharp 			hub, &udev->parent->u1_params, hub_u1_del,
33951e0a012SSarah Sharp 			port_to_port_delay);
34051e0a012SSarah Sharp 
34151e0a012SSarah Sharp 	if (hub_u2_del > hub_u1_del)
34251e0a012SSarah Sharp 		port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
34351e0a012SSarah Sharp 	else
34451e0a012SSarah Sharp 		port_to_port_delay = 1 + hub_u1_del;
34551e0a012SSarah Sharp 
34651e0a012SSarah Sharp 	usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
34751e0a012SSarah Sharp 			hub, &udev->parent->u2_params, hub_u2_del,
34851e0a012SSarah Sharp 			port_to_port_delay);
34951e0a012SSarah Sharp 
35051e0a012SSarah Sharp 	/* Now that we've got PEL, calculate SEL. */
35151e0a012SSarah Sharp 	usb_set_lpm_sel(udev, &udev->u1_params);
35251e0a012SSarah Sharp 	usb_set_lpm_sel(udev, &udev->u2_params);
35351e0a012SSarah Sharp }
354d9b2099cSSarah Sharp 
3551da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
356dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data)
3571da177e4SLinus Torvalds {
358dbe79bbeSJohn Youn 	int i, ret, size;
359dbe79bbeSJohn Youn 	unsigned dtype;
360dbe79bbeSJohn Youn 
361dbe79bbeSJohn Youn 	if (hub_is_superspeed(hdev)) {
362dbe79bbeSJohn Youn 		dtype = USB_DT_SS_HUB;
363dbe79bbeSJohn Youn 		size = USB_DT_SS_HUB_SIZE;
364dbe79bbeSJohn Youn 	} else {
365dbe79bbeSJohn Youn 		dtype = USB_DT_HUB;
366dbe79bbeSJohn Youn 		size = sizeof(struct usb_hub_descriptor);
367dbe79bbeSJohn Youn 	}
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
3701da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3711da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
372dbe79bbeSJohn Youn 			dtype << 8, 0, data, size,
3731da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
3741da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
3751da177e4SLinus Torvalds 			return ret;
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 	return -EINVAL;
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /*
3811da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
3861da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /*
3901da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
3911da177e4SLinus Torvalds  */
392ad493e5eSLan Tianyu int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
3951da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
3961da177e4SLinus Torvalds 		NULL, 0, 1000);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*
4001da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4051da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
4061da177e4SLinus Torvalds 		NULL, 0, 1000);
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
409d99f6b41SDan Williams static char *to_led_name(int selector)
410d99f6b41SDan Williams {
411d99f6b41SDan Williams 	switch (selector) {
412d99f6b41SDan Williams 	case HUB_LED_AMBER:
413d99f6b41SDan Williams 		return "amber";
414d99f6b41SDan Williams 	case HUB_LED_GREEN:
415d99f6b41SDan Williams 		return "green";
416d99f6b41SDan Williams 	case HUB_LED_OFF:
417d99f6b41SDan Williams 		return "off";
418d99f6b41SDan Williams 	case HUB_LED_AUTO:
419d99f6b41SDan Williams 		return "auto";
420d99f6b41SDan Williams 	default:
421d99f6b41SDan Williams 		return "??";
422d99f6b41SDan Williams 	}
423d99f6b41SDan Williams }
424d99f6b41SDan Williams 
4251da177e4SLinus Torvalds /*
4261da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
4271da177e4SLinus Torvalds  * for info about using port indicators
4281da177e4SLinus Torvalds  */
429d99f6b41SDan Williams static void set_port_led(struct usb_hub *hub, int port1, int selector)
4301da177e4SLinus Torvalds {
431d99f6b41SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
432d99f6b41SDan Williams 	int status;
433d99f6b41SDan Williams 
434d99f6b41SDan Williams 	status = set_port_feature(hub->hdev, (selector << 8) | port1,
4351da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
436d99f6b41SDan Williams 	dev_dbg(&port_dev->dev, "indicator %s status %d\n",
437d99f6b41SDan Williams 		to_led_name(selector), status);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
4411da177e4SLinus Torvalds 
442c4028958SDavid Howells static void led_work (struct work_struct *work)
4431da177e4SLinus Torvalds {
444c4028958SDavid Howells 	struct usb_hub		*hub =
445c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
4461da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
4471da177e4SLinus Torvalds 	unsigned		i;
4481da177e4SLinus Torvalds 	unsigned		changed = 0;
4491da177e4SLinus Torvalds 	int			cursor = -1;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
4521da177e4SLinus Torvalds 		return;
4531da177e4SLinus Torvalds 
4543bbc47d8SKrzysztof Mazur 	for (i = 0; i < hdev->maxchild; i++) {
4551da177e4SLinus Torvalds 		unsigned	selector, mode;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
4601da177e4SLinus Torvalds 		/* cycle marker */
4611da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
4621da177e4SLinus Torvalds 			cursor = i;
4631da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
4641da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
4651da177e4SLinus Torvalds 			break;
4661da177e4SLinus Torvalds 		/* blinking green = sw attention */
4671da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
4681da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
4691da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
4701da177e4SLinus Torvalds 			break;
4711da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
4721da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
4731da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
4741da177e4SLinus Torvalds 			break;
4751da177e4SLinus Torvalds 		/* blinking amber = hw attention */
4761da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
4771da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
4781da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
4791da177e4SLinus Torvalds 			break;
4801da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
4811da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
4821da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
4831da177e4SLinus Torvalds 			break;
4841da177e4SLinus Torvalds 		/* blink green/amber = reserved */
4851da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
4861da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
4871da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
4881da177e4SLinus Torvalds 			break;
4891da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
4901da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
4911da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
4921da177e4SLinus Torvalds 			break;
4931da177e4SLinus Torvalds 		default:
4941da177e4SLinus Torvalds 			continue;
4951da177e4SLinus Torvalds 		}
4961da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
4971da177e4SLinus Torvalds 			changed = 1;
4981da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
4991da177e4SLinus Torvalds 		hub->indicator[i] = mode;
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
5021da177e4SLinus Torvalds 		cursor++;
5033bbc47d8SKrzysztof Mazur 		cursor %= hdev->maxchild;
5041da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
5051da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
5061da177e4SLinus Torvalds 		changed++;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 	if (changed)
50922f6a0f0SShaibal Dutta 		queue_delayed_work(system_power_efficient_wq,
51022f6a0f0SShaibal Dutta 				&hub->leds, LED_CYCLE_PERIOD);
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
5141da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
5151da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /*
5181da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
5191da177e4SLinus Torvalds  */
5201da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
5211da177e4SLinus Torvalds 		struct usb_hub_status *data)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
5241da177e4SLinus Torvalds 
5253824c1ddSLibor Pechacek 	for (i = 0; i < USB_STS_RETRIES &&
5263824c1ddSLibor Pechacek 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
5271da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
5281da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
5291da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 	return status;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds /*
5351da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
5361da177e4SLinus Torvalds  */
5371da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
5381da177e4SLinus Torvalds 		struct usb_port_status *data)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
5411da177e4SLinus Torvalds 
5423824c1ddSLibor Pechacek 	for (i = 0; i < USB_STS_RETRIES &&
5433824c1ddSLibor Pechacek 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
5441da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
5451da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
5461da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 	return status;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5513eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
5523eb14915SAlan Stern 		u16 *status, u16 *change)
5533eb14915SAlan Stern {
5543eb14915SAlan Stern 	int ret;
5553eb14915SAlan Stern 
5563eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
5573eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
5583eb14915SAlan Stern 	if (ret < 4) {
559e9e88fb7SAlan Stern 		if (ret != -ENODEV)
5603eb14915SAlan Stern 			dev_err(hub->intfdev,
5613eb14915SAlan Stern 				"%s failed (err = %d)\n", __func__, ret);
5623eb14915SAlan Stern 		if (ret >= 0)
5633eb14915SAlan Stern 			ret = -EIO;
5643eb14915SAlan Stern 	} else {
5653eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
5663eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
567dbe79bbeSJohn Youn 
5683eb14915SAlan Stern 		ret = 0;
5693eb14915SAlan Stern 	}
5703eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
5713eb14915SAlan Stern 	return ret;
5723eb14915SAlan Stern }
5733eb14915SAlan Stern 
57432a69589SPetr Mladek static void kick_hub_wq(struct usb_hub *hub)
5751da177e4SLinus Torvalds {
57632a69589SPetr Mladek 	struct usb_interface *intf;
5771da177e4SLinus Torvalds 
57832a69589SPetr Mladek 	if (hub->disconnected || work_pending(&hub->events))
57932a69589SPetr Mladek 		return;
5808e4ceb38SAlan Stern 
58132a69589SPetr Mladek 	/*
58232a69589SPetr Mladek 	 * Suppress autosuspend until the event is proceed.
58332a69589SPetr Mladek 	 *
58432a69589SPetr Mladek 	 * Be careful and make sure that the symmetric operation is
58532a69589SPetr Mladek 	 * always called. We are here only when there is no pending
58632a69589SPetr Mladek 	 * work for this hub. Therefore put the interface either when
58732a69589SPetr Mladek 	 * the new work is called or when it is canceled.
58832a69589SPetr Mladek 	 */
58932a69589SPetr Mladek 	intf = to_usb_interface(hub->intfdev);
59032a69589SPetr Mladek 	usb_autopm_get_interface_no_resume(intf);
59132a69589SPetr Mladek 	kref_get(&hub->kref);
59232a69589SPetr Mladek 
59332a69589SPetr Mladek 	if (queue_work(hub_wq, &hub->events))
59432a69589SPetr Mladek 		return;
59532a69589SPetr Mladek 
59632a69589SPetr Mladek 	/* the work has already been scheduled */
59732a69589SPetr Mladek 	usb_autopm_put_interface_async(intf);
59832a69589SPetr Mladek 	kref_put(&hub->kref, hub_release);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
60159d48b3fSPetr Mladek void usb_kick_hub_wq(struct usb_device *hdev)
6021da177e4SLinus Torvalds {
603ad493e5eSLan Tianyu 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
60425118084SAlan Stern 
60525118084SAlan Stern 	if (hub)
60632a69589SPetr Mladek 		kick_hub_wq(hub);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6094ee823b8SSarah Sharp /*
6104ee823b8SSarah Sharp  * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
6114ee823b8SSarah Sharp  * Notification, which indicates it had initiated remote wakeup.
6124ee823b8SSarah Sharp  *
6134ee823b8SSarah Sharp  * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
6144ee823b8SSarah Sharp  * device initiates resume, so the USB core will not receive notice of the
6154ee823b8SSarah Sharp  * resume through the normal hub interrupt URB.
6164ee823b8SSarah Sharp  */
6174ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev,
6184ee823b8SSarah Sharp 		unsigned int portnum)
6194ee823b8SSarah Sharp {
6204ee823b8SSarah Sharp 	struct usb_hub *hub;
6214ee823b8SSarah Sharp 
6224ee823b8SSarah Sharp 	if (!hdev)
6234ee823b8SSarah Sharp 		return;
6244ee823b8SSarah Sharp 
625ad493e5eSLan Tianyu 	hub = usb_hub_to_struct_hub(hdev);
6264ee823b8SSarah Sharp 	if (hub) {
6274ee823b8SSarah Sharp 		set_bit(portnum, hub->wakeup_bits);
62832a69589SPetr Mladek 		kick_hub_wq(hub);
6294ee823b8SSarah Sharp 	}
6304ee823b8SSarah Sharp }
6314ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
6347d12e780SDavid Howells static void hub_irq(struct urb *urb)
6351da177e4SLinus Torvalds {
636ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
637e015268dSAlan Stern 	int status = urb->status;
63871d2718fSRoel Kluin 	unsigned i;
6391da177e4SLinus Torvalds 	unsigned long bits;
6401da177e4SLinus Torvalds 
641e015268dSAlan Stern 	switch (status) {
6421da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
6431da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
6441da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
6451da177e4SLinus Torvalds 		return;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	default:		/* presumably an error */
6481da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
649e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
6501da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
6511da177e4SLinus Torvalds 			goto resubmit;
652e015268dSAlan Stern 		hub->error = status;
6531da177e4SLinus Torvalds 		/* FALL THROUGH */
6541da177e4SLinus Torvalds 
65537ebb549SPetr Mladek 	/* let hub_wq handle things */
6561da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
6571da177e4SLinus Torvalds 		bits = 0;
6581da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
6591da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
6601da177e4SLinus Torvalds 					<< (i*8);
6611da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
6621da177e4SLinus Torvalds 		break;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	hub->nerrors = 0;
6661da177e4SLinus Torvalds 
66737ebb549SPetr Mladek 	/* Something happened, let hub_wq figure it out */
66832a69589SPetr Mladek 	kick_hub_wq(hub);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds resubmit:
6711da177e4SLinus Torvalds 	if (hub->quiescing)
6721da177e4SLinus Torvalds 		return;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
6751da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
6761da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
6801da177e4SLinus Torvalds static inline int
6811da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
6821da177e4SLinus Torvalds {
6832c7b871bSWilliam Gulland 	/* Need to clear both directions for control ep */
6842c7b871bSWilliam Gulland 	if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
6852c7b871bSWilliam Gulland 			USB_ENDPOINT_XFER_CONTROL) {
6862c7b871bSWilliam Gulland 		int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
6872c7b871bSWilliam Gulland 				HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
6882c7b871bSWilliam Gulland 				devinfo ^ 0x8000, tt, NULL, 0, 1000);
6892c7b871bSWilliam Gulland 		if (status)
6902c7b871bSWilliam Gulland 			return status;
6912c7b871bSWilliam Gulland 	}
692c2f6595fSAlan Stern 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
6931da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
6941da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds /*
69837ebb549SPetr Mladek  * enumeration blocks hub_wq for a long time. we use keventd instead, since
6991da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
7001da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
7011da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
7021da177e4SLinus Torvalds  */
703cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
7041da177e4SLinus Torvalds {
705c4028958SDavid Howells 	struct usb_hub		*hub =
706cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
7071da177e4SLinus Torvalds 	unsigned long		flags;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
7103b6054daSOctavian Purdila 	while (!list_empty(&hub->tt.clear_list)) {
711d0f830d3SH Hartley Sweeten 		struct list_head	*next;
7121da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
7131da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
714cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
7151da177e4SLinus Torvalds 		int			status;
7161da177e4SLinus Torvalds 
717d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
718d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
7191da177e4SLinus Torvalds 		list_del (&clear->clear_list);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
7221da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
7231da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
724e9e88fb7SAlan Stern 		if (status && status != -ENODEV)
7251da177e4SLinus Torvalds 			dev_err (&hdev->dev,
7261da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
7271da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
728cb88a1b8SAlan Stern 
729cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
730cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
731cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
732cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
733cb88a1b8SAlan Stern 
7341da177e4SLinus Torvalds 		kfree(clear);
735cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /**
741971fcd49SLan Tianyu  * usb_hub_set_port_power - control hub port's power state
74241341261SMathias Nyman  * @hdev: USB device belonging to the usb hub
74341341261SMathias Nyman  * @hub: target hub
744971fcd49SLan Tianyu  * @port1: port index
745971fcd49SLan Tianyu  * @set: expected status
746971fcd49SLan Tianyu  *
747971fcd49SLan Tianyu  * call this function to control port's power via setting or
748971fcd49SLan Tianyu  * clearing the port's PORT_POWER feature.
749626f090cSYacine Belkadi  *
750626f090cSYacine Belkadi  * Return: 0 if successful. A negative error code otherwise.
751971fcd49SLan Tianyu  */
75241341261SMathias Nyman int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
75341341261SMathias Nyman 			   int port1, bool set)
754971fcd49SLan Tianyu {
755971fcd49SLan Tianyu 	int ret;
756971fcd49SLan Tianyu 
757971fcd49SLan Tianyu 	if (set)
758971fcd49SLan Tianyu 		ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
759971fcd49SLan Tianyu 	else
760ad493e5eSLan Tianyu 		ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
761ad493e5eSLan Tianyu 
762d5c3834eSDan Williams 	if (ret)
763971fcd49SLan Tianyu 		return ret;
764d5c3834eSDan Williams 
765d5c3834eSDan Williams 	if (set)
766d5c3834eSDan Williams 		set_bit(port1, hub->power_bits);
767d5c3834eSDan Williams 	else
768d5c3834eSDan Williams 		clear_bit(port1, hub->power_bits);
769d5c3834eSDan Williams 	return 0;
770971fcd49SLan Tianyu }
771971fcd49SLan Tianyu 
772971fcd49SLan Tianyu /**
773cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
774cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
7751da177e4SLinus Torvalds  *
7761da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
7771da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
7781da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
7791da177e4SLinus Torvalds  * interrupt context.
7801da177e4SLinus Torvalds  *
7811da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
7821da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
783626f090cSYacine Belkadi  *
784626f090cSYacine Belkadi  * Return: 0 if successful. A negative error code otherwise.
7851da177e4SLinus Torvalds  */
786cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
7871da177e4SLinus Torvalds {
788cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
789cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
7901da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
7911da177e4SLinus Torvalds 	unsigned long		flags;
7921da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
7951da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
7961da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
7971da177e4SLinus Torvalds 	 */
79854e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
7991da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
8001da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
801cb88a1b8SAlan Stern 		return -ENOMEM;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
8051da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
8061da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
8071da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
8081da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
8091da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
8101da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
8111da177e4SLinus Torvalds 	if (usb_pipein (pipe))
8121da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
8131da177e4SLinus Torvalds 
814cb88a1b8SAlan Stern 	/* info for completion callback */
815cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
816cb88a1b8SAlan Stern 	clear->ep = urb->ep;
817cb88a1b8SAlan Stern 
8181da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
8191da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
8201da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
821cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
8221da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
823cb88a1b8SAlan Stern 	return 0;
8241da177e4SLinus Torvalds }
825cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
8261da177e4SLinus Torvalds 
8277ad3c470SDan Williams static void hub_power_on(struct usb_hub *hub, bool do_delay)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	int port1;
8301da177e4SLinus Torvalds 
8314489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
8324489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
8334489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
8344489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
8354489a571SAlan Stern 	 * unless we send these messages to the hub.
8364489a571SAlan Stern 	 */
8379262c19dSDan Williams 	if (hub_is_port_power_switchable(hub))
8381da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
8394489a571SAlan Stern 	else
8404489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
8414489a571SAlan Stern 				"non-switchable hub\n");
8423bbc47d8SKrzysztof Mazur 	for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
843d5c3834eSDan Williams 		if (test_bit(port1, hub->power_bits))
8444489a571SAlan Stern 			set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
845ad493e5eSLan Tianyu 		else
846ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
847ad493e5eSLan Tianyu 						USB_PORT_FEAT_POWER);
8488520f380SAlan Stern 	if (do_delay)
8497ad3c470SDan Williams 		msleep(hub_power_on_good_delay(hub));
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
8531da177e4SLinus Torvalds 		u16 *status, u16 *change)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	int ret;
8561da177e4SLinus Torvalds 
857db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
8581da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
859e9e88fb7SAlan Stern 	if (ret < 0) {
860e9e88fb7SAlan Stern 		if (ret != -ENODEV)
8611da177e4SLinus Torvalds 			dev_err(hub->intfdev,
862441b62c1SHarvey Harrison 				"%s failed (err = %d)\n", __func__, ret);
863e9e88fb7SAlan Stern 	} else {
8641da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
8651da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
8661da177e4SLinus Torvalds 		ret = 0;
8671da177e4SLinus Torvalds 	}
868db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
8691da177e4SLinus Torvalds 	return ret;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
87241e7e056SSarah Sharp static int hub_set_port_link_state(struct usb_hub *hub, int port1,
87341e7e056SSarah Sharp 			unsigned int link_status)
87441e7e056SSarah Sharp {
87541e7e056SSarah Sharp 	return set_port_feature(hub->hdev,
87641e7e056SSarah Sharp 			port1 | (link_status << 3),
87741e7e056SSarah Sharp 			USB_PORT_FEAT_LINK_STATE);
87841e7e056SSarah Sharp }
87941e7e056SSarah Sharp 
88041e7e056SSarah Sharp /*
88141e7e056SSarah Sharp  * If USB 3.0 ports are placed into the Disabled state, they will no longer
88241e7e056SSarah Sharp  * detect any device connects or disconnects.  This is generally not what the
88341e7e056SSarah Sharp  * USB core wants, since it expects a disabled port to produce a port status
88441e7e056SSarah Sharp  * change event when a new device connects.
88541e7e056SSarah Sharp  *
88641e7e056SSarah Sharp  * Instead, set the link state to Disabled, wait for the link to settle into
88741e7e056SSarah Sharp  * that state, clear any change bits, and then put the port into the RxDetect
88841e7e056SSarah Sharp  * state.
88941e7e056SSarah Sharp  */
89041e7e056SSarah Sharp static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
89141e7e056SSarah Sharp {
89241e7e056SSarah Sharp 	int ret;
89341e7e056SSarah Sharp 	int total_time;
89441e7e056SSarah Sharp 	u16 portchange, portstatus;
89541e7e056SSarah Sharp 
89641e7e056SSarah Sharp 	if (!hub_is_superspeed(hub->hdev))
89741e7e056SSarah Sharp 		return -EINVAL;
89841e7e056SSarah Sharp 
899bb86cf56SGavin Guo 	ret = hub_port_status(hub, port1, &portstatus, &portchange);
900bb86cf56SGavin Guo 	if (ret < 0)
901bb86cf56SGavin Guo 		return ret;
902bb86cf56SGavin Guo 
903bb86cf56SGavin Guo 	/*
904bb86cf56SGavin Guo 	 * USB controller Advanced Micro Devices, Inc. [AMD] FCH USB XHCI
905bb86cf56SGavin Guo 	 * Controller [1022:7814] will have spurious result making the following
906bb86cf56SGavin Guo 	 * usb 3.0 device hotplugging route to the 2.0 root hub and recognized
907bb86cf56SGavin Guo 	 * as high-speed device if we set the usb 3.0 port link state to
908bb86cf56SGavin Guo 	 * Disabled. Since it's already in USB_SS_PORT_LS_RX_DETECT state, we
909bb86cf56SGavin Guo 	 * check the state here to avoid the bug.
910bb86cf56SGavin Guo 	 */
911bb86cf56SGavin Guo 	if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
912bb86cf56SGavin Guo 				USB_SS_PORT_LS_RX_DETECT) {
913bb86cf56SGavin Guo 		dev_dbg(&hub->ports[port1 - 1]->dev,
914bb86cf56SGavin Guo 			 "Not disabling port; link state is RxDetect\n");
915bb86cf56SGavin Guo 		return ret;
916bb86cf56SGavin Guo 	}
917bb86cf56SGavin Guo 
91841e7e056SSarah Sharp 	ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
919e9e88fb7SAlan Stern 	if (ret)
92041e7e056SSarah Sharp 		return ret;
92141e7e056SSarah Sharp 
92241e7e056SSarah Sharp 	/* Wait for the link to enter the disabled state. */
92341e7e056SSarah Sharp 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
92441e7e056SSarah Sharp 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
92541e7e056SSarah Sharp 		if (ret < 0)
92641e7e056SSarah Sharp 			return ret;
92741e7e056SSarah Sharp 
92841e7e056SSarah Sharp 		if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
92941e7e056SSarah Sharp 				USB_SS_PORT_LS_SS_DISABLED)
93041e7e056SSarah Sharp 			break;
93141e7e056SSarah Sharp 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
93241e7e056SSarah Sharp 			break;
93341e7e056SSarah Sharp 		msleep(HUB_DEBOUNCE_STEP);
93441e7e056SSarah Sharp 	}
93541e7e056SSarah Sharp 	if (total_time >= HUB_DEBOUNCE_TIMEOUT)
936d99f6b41SDan Williams 		dev_warn(&hub->ports[port1 - 1]->dev,
937d99f6b41SDan Williams 				"Could not disable after %d ms\n", total_time);
93841e7e056SSarah Sharp 
93941e7e056SSarah Sharp 	return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
94041e7e056SSarah Sharp }
94141e7e056SSarah Sharp 
9428b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
9438b28c752SAlan Stern {
944d99f6b41SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
9458b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
9460458d5b4SAlan Stern 	int ret = 0;
9478b28c752SAlan Stern 
948d99f6b41SDan Williams 	if (port_dev->child && set_state)
949d99f6b41SDan Williams 		usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
95041e7e056SSarah Sharp 	if (!hub->error) {
95141e7e056SSarah Sharp 		if (hub_is_superspeed(hub->hdev))
95241e7e056SSarah Sharp 			ret = hub_usb3_port_disable(hub, port1);
95341e7e056SSarah Sharp 		else
954ad493e5eSLan Tianyu 			ret = usb_clear_port_feature(hdev, port1,
95541e7e056SSarah Sharp 					USB_PORT_FEAT_ENABLE);
95641e7e056SSarah Sharp 	}
957e9e88fb7SAlan Stern 	if (ret && ret != -ENODEV)
958d99f6b41SDan Williams 		dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
9598b28c752SAlan Stern 	return ret;
9608b28c752SAlan Stern }
9618b28c752SAlan Stern 
9620458d5b4SAlan Stern /*
9636d42fcdbSJustin P. Mattock  * Disable a port and mark a logical connect-change event, so that some
96437ebb549SPetr Mladek  * time later hub_wq will disconnect() any existing usb_device on the port
9650458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
9660458d5b4SAlan Stern  */
9670458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
9687d069b7dSAlan Stern {
969d99f6b41SDan Williams 	dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
9700458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
9710458d5b4SAlan Stern 
9720458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
9730458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
9740458d5b4SAlan Stern 	 *  - SRP saves power that way
9750458d5b4SAlan Stern 	 *  - ... new call, TBD ...
9760458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
97737ebb549SPetr Mladek 	 * hub_wq reactivates the port later (timer, SRP, etc).
9780458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
9790458d5b4SAlan Stern 	 */
9800458d5b4SAlan Stern 
9810458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
98232a69589SPetr Mladek 	kick_hub_wq(hub);
9830458d5b4SAlan Stern }
9840458d5b4SAlan Stern 
985253e0572SAlan Stern /**
986253e0572SAlan Stern  * usb_remove_device - disable a device's port on its parent hub
987253e0572SAlan Stern  * @udev: device to be disabled and removed
988253e0572SAlan Stern  * Context: @udev locked, must be able to sleep.
989253e0572SAlan Stern  *
99037ebb549SPetr Mladek  * After @udev's port has been disabled, hub_wq is notified and it will
991253e0572SAlan Stern  * see that the device has been disconnected.  When the device is
992253e0572SAlan Stern  * physically unplugged and something is plugged in, the events will
993253e0572SAlan Stern  * be received and processed normally.
994626f090cSYacine Belkadi  *
995626f090cSYacine Belkadi  * Return: 0 if successful. A negative error code otherwise.
996253e0572SAlan Stern  */
997253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
998253e0572SAlan Stern {
999253e0572SAlan Stern 	struct usb_hub *hub;
1000253e0572SAlan Stern 	struct usb_interface *intf;
1001253e0572SAlan Stern 
1002253e0572SAlan Stern 	if (!udev->parent)	/* Can't remove a root hub */
1003253e0572SAlan Stern 		return -EINVAL;
1004ad493e5eSLan Tianyu 	hub = usb_hub_to_struct_hub(udev->parent);
1005253e0572SAlan Stern 	intf = to_usb_interface(hub->intfdev);
1006253e0572SAlan Stern 
1007253e0572SAlan Stern 	usb_autopm_get_interface(intf);
1008253e0572SAlan Stern 	set_bit(udev->portnum, hub->removed_bits);
1009253e0572SAlan Stern 	hub_port_logical_disconnect(hub, udev->portnum);
1010253e0572SAlan Stern 	usb_autopm_put_interface(intf);
1011253e0572SAlan Stern 	return 0;
1012253e0572SAlan Stern }
1013253e0572SAlan Stern 
10146ee0b270SAlan Stern enum hub_activation_type {
10158e4ceb38SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
10168520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
10176ee0b270SAlan Stern };
10185e6effaeSAlan Stern 
10198520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
10208520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
10218520f380SAlan Stern 
1022f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
10235e6effaeSAlan Stern {
10245e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
1025653a39d1SSarah Sharp 	struct usb_hcd *hcd;
1026653a39d1SSarah Sharp 	int ret;
10275e6effaeSAlan Stern 	int port1;
1028f2835219SAlan Stern 	int status;
1029948fea37SAlan Stern 	bool need_debounce_delay = false;
10308520f380SAlan Stern 	unsigned delay;
10318520f380SAlan Stern 
10328520f380SAlan Stern 	/* Continue a partial initialization */
10338520f380SAlan Stern 	if (type == HUB_INIT2)
10348520f380SAlan Stern 		goto init2;
10358520f380SAlan Stern 	if (type == HUB_INIT3)
10368520f380SAlan Stern 		goto init3;
10375e6effaeSAlan Stern 
1038a45aa3b3SElric Fu 	/* The superspeed hub except for root hub has to use Hub Depth
1039a45aa3b3SElric Fu 	 * value as an offset into the route string to locate the bits
1040a45aa3b3SElric Fu 	 * it uses to determine the downstream port number. So hub driver
1041a45aa3b3SElric Fu 	 * should send a set hub depth request to superspeed hub after
1042a45aa3b3SElric Fu 	 * the superspeed hub is set configuration in initialization or
1043a45aa3b3SElric Fu 	 * reset procedure.
1044a45aa3b3SElric Fu 	 *
1045a45aa3b3SElric Fu 	 * After a resume, port power should still be on.
1046f2835219SAlan Stern 	 * For any other type of activation, turn it on.
1047f2835219SAlan Stern 	 */
10488520f380SAlan Stern 	if (type != HUB_RESUME) {
1049a45aa3b3SElric Fu 		if (hdev->parent && hub_is_superspeed(hdev)) {
1050a45aa3b3SElric Fu 			ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1051a45aa3b3SElric Fu 					HUB_SET_DEPTH, USB_RT_HUB,
1052a45aa3b3SElric Fu 					hdev->level - 1, 0, NULL, 0,
1053a45aa3b3SElric Fu 					USB_CTRL_SET_TIMEOUT);
1054a45aa3b3SElric Fu 			if (ret < 0)
1055a45aa3b3SElric Fu 				dev_err(hub->intfdev,
1056a45aa3b3SElric Fu 						"set hub depth failed\n");
1057a45aa3b3SElric Fu 		}
10588520f380SAlan Stern 
10598520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
10608520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
10618520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
10628520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
10638520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
10648520f380SAlan Stern 		 * rather than as a module).  It adds up.
10658520f380SAlan Stern 		 *
10668520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
10678520f380SAlan Stern 		 * because for those activation types the ports have to be
10688520f380SAlan Stern 		 * operational when we return.  In theory this could be done
10698520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
10708520f380SAlan Stern 		 */
10718520f380SAlan Stern 		if (type == HUB_INIT) {
10727ad3c470SDan Williams 			unsigned delay = hub_power_on_good_delay(hub);
10737ad3c470SDan Williams 
10747ad3c470SDan Williams 			hub_power_on(hub, false);
107577fa83cfSTejun Heo 			INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
107622f6a0f0SShaibal Dutta 			queue_delayed_work(system_power_efficient_wq,
107722f6a0f0SShaibal Dutta 					&hub->init_work,
10788520f380SAlan Stern 					msecs_to_jiffies(delay));
107961fbeba1SAlan Stern 
108061fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
10818e4ceb38SAlan Stern 			usb_autopm_get_interface_no_resume(
10828e4ceb38SAlan Stern 					to_usb_interface(hub->intfdev));
10838520f380SAlan Stern 			return;		/* Continues at init2: below */
1084653a39d1SSarah Sharp 		} else if (type == HUB_RESET_RESUME) {
1085653a39d1SSarah Sharp 			/* The internal host controller state for the hub device
1086653a39d1SSarah Sharp 			 * may be gone after a host power loss on system resume.
1087653a39d1SSarah Sharp 			 * Update the device's info so the HW knows it's a hub.
1088653a39d1SSarah Sharp 			 */
1089653a39d1SSarah Sharp 			hcd = bus_to_hcd(hdev->bus);
1090653a39d1SSarah Sharp 			if (hcd->driver->update_hub_device) {
1091653a39d1SSarah Sharp 				ret = hcd->driver->update_hub_device(hcd, hdev,
1092653a39d1SSarah Sharp 						&hub->tt, GFP_NOIO);
1093653a39d1SSarah Sharp 				if (ret < 0) {
1094653a39d1SSarah Sharp 					dev_err(hub->intfdev, "Host not "
1095653a39d1SSarah Sharp 							"accepting hub info "
1096653a39d1SSarah Sharp 							"update.\n");
1097653a39d1SSarah Sharp 					dev_err(hub->intfdev, "LS/FS devices "
1098653a39d1SSarah Sharp 							"and hubs may not work "
1099653a39d1SSarah Sharp 							"under this hub\n.");
1100653a39d1SSarah Sharp 				}
1101653a39d1SSarah Sharp 			}
1102653a39d1SSarah Sharp 			hub_power_on(hub, true);
11038520f380SAlan Stern 		} else {
11048520f380SAlan Stern 			hub_power_on(hub, true);
11058520f380SAlan Stern 		}
11068520f380SAlan Stern 	}
11078520f380SAlan Stern  init2:
1108f2835219SAlan Stern 
1109d99f6b41SDan Williams 	/*
111037ebb549SPetr Mladek 	 * Check each port and set hub->change_bits to let hub_wq know
11116ee0b270SAlan Stern 	 * which ports need attention.
11125e6effaeSAlan Stern 	 */
11135e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1114d99f6b41SDan Williams 		struct usb_port *port_dev = hub->ports[port1 - 1];
1115d99f6b41SDan Williams 		struct usb_device *udev = port_dev->child;
11165e6effaeSAlan Stern 		u16 portstatus, portchange;
11175e6effaeSAlan Stern 
11186ee0b270SAlan Stern 		portstatus = portchange = 0;
11196ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
11206ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1121d99f6b41SDan Williams 			dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1122d99f6b41SDan Williams 					portstatus, portchange);
11235e6effaeSAlan Stern 
1124d99f6b41SDan Williams 		/*
1125d99f6b41SDan Williams 		 * After anything other than HUB_RESUME (i.e., initialization
11266ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
11276ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
11286ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
11295e6effaeSAlan Stern 		 */
11306ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
11316ee0b270SAlan Stern 				type != HUB_RESUME ||
11326ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
11336ee0b270SAlan Stern 				!udev ||
11346ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
11359f0a6cd3SAndiry Xu 			/*
11369f0a6cd3SAndiry Xu 			 * USB3 protocol ports will automatically transition
11379f0a6cd3SAndiry Xu 			 * to Enabled state when detect an USB3.0 device attach.
1138fd1ac4cfSDan Williams 			 * Do not disable USB3 protocol ports, just pretend
1139fd1ac4cfSDan Williams 			 * power was lost
11409f0a6cd3SAndiry Xu 			 */
1141fd1ac4cfSDan Williams 			portstatus &= ~USB_PORT_STAT_ENABLE;
1142fd1ac4cfSDan Williams 			if (!hub_is_superspeed(hdev))
1143ad493e5eSLan Tianyu 				usb_clear_port_feature(hdev, port1,
11449f0a6cd3SAndiry Xu 						   USB_PORT_FEAT_ENABLE);
11459f0a6cd3SAndiry Xu 		}
11466ee0b270SAlan Stern 
1147948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
1148948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
1149948fea37SAlan Stern 			need_debounce_delay = true;
1150ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
1151948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
1152948fea37SAlan Stern 		}
1153948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
1154948fea37SAlan Stern 			need_debounce_delay = true;
1155ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
1156948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
1157948fea37SAlan Stern 		}
1158e92aee33SJulius Werner 		if (portchange & USB_PORT_STAT_C_RESET) {
1159e92aee33SJulius Werner 			need_debounce_delay = true;
1160e92aee33SJulius Werner 			usb_clear_port_feature(hub->hdev, port1,
1161e92aee33SJulius Werner 					USB_PORT_FEAT_C_RESET);
1162e92aee33SJulius Werner 		}
116379c3dd81SDon Zickus 		if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
116479c3dd81SDon Zickus 				hub_is_superspeed(hub->hdev)) {
116579c3dd81SDon Zickus 			need_debounce_delay = true;
1166ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
116779c3dd81SDon Zickus 					USB_PORT_FEAT_C_BH_PORT_RESET);
116879c3dd81SDon Zickus 		}
1169253e0572SAlan Stern 		/* We can forget about a "removed" device when there's a
1170253e0572SAlan Stern 		 * physical disconnect or the connect status changes.
1171253e0572SAlan Stern 		 */
1172253e0572SAlan Stern 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1173253e0572SAlan Stern 				(portchange & USB_PORT_STAT_C_CONNECTION))
1174253e0572SAlan Stern 			clear_bit(port1, hub->removed_bits);
1175253e0572SAlan Stern 
11766ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
117737ebb549SPetr Mladek 			/* Tell hub_wq to disconnect the device or
11786ee0b270SAlan Stern 			 * check for a new connection
11796ee0b270SAlan Stern 			 */
118008d1dec6SShen Guang 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
118108d1dec6SShen Guang 			    (portstatus & USB_PORT_STAT_OVERCURRENT))
11826ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
11836ee0b270SAlan Stern 
11846ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
118572937e1eSSarah Sharp 			bool port_resumed = (portstatus &
118672937e1eSSarah Sharp 					USB_PORT_STAT_LINK_STATE) ==
118772937e1eSSarah Sharp 				USB_SS_PORT_LS_U0;
11886ee0b270SAlan Stern 			/* The power session apparently survived the resume.
11896ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
119037ebb549SPetr Mladek 			 * (i.e., remote wakeup request), have hub_wq
119172937e1eSSarah Sharp 			 * take care of it.  Look at the port link state
119272937e1eSSarah Sharp 			 * for USB 3.0 hubs, since they don't have a suspend
119372937e1eSSarah Sharp 			 * change bit, and they don't set the port link change
119472937e1eSSarah Sharp 			 * bit on device-initiated resume.
11956ee0b270SAlan Stern 			 */
119672937e1eSSarah Sharp 			if (portchange || (hub_is_superspeed(hub->hdev) &&
119772937e1eSSarah Sharp 						port_resumed))
11986ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
11996ee0b270SAlan Stern 
12006ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
12016ee0b270SAlan Stern #ifdef CONFIG_PM
12025e6effaeSAlan Stern 			udev->reset_resume = 1;
12036ee0b270SAlan Stern #endif
1204ad493e5eSLan Tianyu 			/* Don't set the change_bits when the device
1205ad493e5eSLan Tianyu 			 * was powered off.
1206ad493e5eSLan Tianyu 			 */
1207d5c3834eSDan Williams 			if (test_bit(port1, hub->power_bits))
12088808f00cSAlan Stern 				set_bit(port1, hub->change_bits);
12098808f00cSAlan Stern 
12106ee0b270SAlan Stern 		} else {
121137ebb549SPetr Mladek 			/* The power session is gone; tell hub_wq */
12126ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
12136ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
12145e6effaeSAlan Stern 		}
12155e6effaeSAlan Stern 	}
12165e6effaeSAlan Stern 
1217948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
1218948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
121937ebb549SPetr Mladek 	 * ports all at once right now, instead of letting hub_wq do them
1220948fea37SAlan Stern 	 * one at a time later on.
1221948fea37SAlan Stern 	 *
122237ebb549SPetr Mladek 	 * If any port-status changes do occur during this delay, hub_wq
1223948fea37SAlan Stern 	 * will see them later and handle them normally.
1224948fea37SAlan Stern 	 */
12258520f380SAlan Stern 	if (need_debounce_delay) {
12268520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
1227f2835219SAlan Stern 
12288520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
12298520f380SAlan Stern 		if (type == HUB_INIT2) {
123077fa83cfSTejun Heo 			INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
123122f6a0f0SShaibal Dutta 			queue_delayed_work(system_power_efficient_wq,
123222f6a0f0SShaibal Dutta 					&hub->init_work,
12338520f380SAlan Stern 					msecs_to_jiffies(delay));
12348520f380SAlan Stern 			return;		/* Continues at init3: below */
12358520f380SAlan Stern 		} else {
12368520f380SAlan Stern 			msleep(delay);
12378520f380SAlan Stern 		}
12388520f380SAlan Stern 	}
12398520f380SAlan Stern  init3:
1240f2835219SAlan Stern 	hub->quiescing = 0;
1241f2835219SAlan Stern 
1242f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
1243f2835219SAlan Stern 	if (status < 0)
1244f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
1245f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
124622f6a0f0SShaibal Dutta 		queue_delayed_work(system_power_efficient_wq,
124722f6a0f0SShaibal Dutta 				&hub->leds, LED_CYCLE_PERIOD);
1248f2835219SAlan Stern 
1249f2835219SAlan Stern 	/* Scan all ports that need attention */
125032a69589SPetr Mladek 	kick_hub_wq(hub);
12518e4ceb38SAlan Stern 
12528e4ceb38SAlan Stern 	/* Allow autosuspend if it was suppressed */
12538e4ceb38SAlan Stern 	if (type <= HUB_INIT3)
12548e4ceb38SAlan Stern 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
12555e6effaeSAlan Stern }
12565e6effaeSAlan Stern 
12578520f380SAlan Stern /* Implement the continuations for the delays above */
12588520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
12598520f380SAlan Stern {
12608520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
12618520f380SAlan Stern 
12628520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
12638520f380SAlan Stern }
12648520f380SAlan Stern 
12658520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
12668520f380SAlan Stern {
12678520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
12688520f380SAlan Stern 
12698520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
12708520f380SAlan Stern }
12718520f380SAlan Stern 
12724330354fSAlan Stern enum hub_quiescing_type {
12734330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
12744330354fSAlan Stern };
12754330354fSAlan Stern 
12764330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
12774330354fSAlan Stern {
12784330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
12794330354fSAlan Stern 	int i;
12804330354fSAlan Stern 
12818520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
12828520f380SAlan Stern 
128337ebb549SPetr Mladek 	/* hub_wq and related activity won't re-trigger */
12844330354fSAlan Stern 	hub->quiescing = 1;
12854330354fSAlan Stern 
12864330354fSAlan Stern 	if (type != HUB_SUSPEND) {
12874330354fSAlan Stern 		/* Disconnect all the children */
12884330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
1289ff823c79SLan Tianyu 			if (hub->ports[i]->child)
1290ff823c79SLan Tianyu 				usb_disconnect(&hub->ports[i]->child);
12914330354fSAlan Stern 		}
12924330354fSAlan Stern 	}
12934330354fSAlan Stern 
129437ebb549SPetr Mladek 	/* Stop hub_wq and related activity */
12954330354fSAlan Stern 	usb_kill_urb(hub->urb);
12964330354fSAlan Stern 	if (hub->has_indicators)
12974330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
12984330354fSAlan Stern 	if (hub->tt.hub)
1299036546bfSOctavian Purdila 		flush_work(&hub->tt.clear_work);
13004330354fSAlan Stern }
13014330354fSAlan Stern 
1302600856c2SAlan Stern static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1303600856c2SAlan Stern {
1304600856c2SAlan Stern 	int i;
1305600856c2SAlan Stern 
1306600856c2SAlan Stern 	for (i = 0; i < hub->hdev->maxchild; ++i)
1307600856c2SAlan Stern 		pm_runtime_barrier(&hub->ports[i]->dev);
1308600856c2SAlan Stern }
1309600856c2SAlan Stern 
13103eb14915SAlan Stern /* caller has locked the hub device */
13113eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
13123eb14915SAlan Stern {
13133eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
13143eb14915SAlan Stern 
13154330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
1316600856c2SAlan Stern 	hub->in_reset = 1;
1317600856c2SAlan Stern 	hub_pm_barrier_for_all_ports(hub);
1318f07600cfSAlan Stern 	return 0;
13197d069b7dSAlan Stern }
13207d069b7dSAlan Stern 
13217d069b7dSAlan Stern /* caller has locked the hub device */
1322f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
13237d069b7dSAlan Stern {
13247de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
13257de18d8bSAlan Stern 
1326600856c2SAlan Stern 	hub->in_reset = 0;
1327600856c2SAlan Stern 	hub_pm_barrier_for_all_ports(hub);
1328f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
1329f07600cfSAlan Stern 	return 0;
13307d069b7dSAlan Stern }
13317d069b7dSAlan Stern 
13321da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
13331da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
13341da177e4SLinus Torvalds {
1335b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
13361da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
13371da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
13381da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
133974ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
13401da177e4SLinus Torvalds 	unsigned int pipe;
1341fa2a9566SLan Tianyu 	int maxp, ret, i;
13427cbe5dcaSAlan Stern 	char *message = "out of memory";
1343430ee58eSSebastian Andrzej Siewior 	unsigned unit_load;
1344430ee58eSSebastian Andrzej Siewior 	unsigned full_load;
1345d8521afeSDan Williams 	unsigned maxchild;
13461da177e4SLinus Torvalds 
1347d697cddaSAlan Stern 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
13481da177e4SLinus Torvalds 	if (!hub->buffer) {
13491da177e4SLinus Torvalds 		ret = -ENOMEM;
13501da177e4SLinus Torvalds 		goto fail;
13511da177e4SLinus Torvalds 	}
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
13541da177e4SLinus Torvalds 	if (!hub->status) {
13551da177e4SLinus Torvalds 		ret = -ENOMEM;
13561da177e4SLinus Torvalds 		goto fail;
13571da177e4SLinus Torvalds 	}
1358db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
13611da177e4SLinus Torvalds 	if (!hub->descriptor) {
13621da177e4SLinus Torvalds 		ret = -ENOMEM;
13631da177e4SLinus Torvalds 		goto fail;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
13671da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
13681da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
13691da177e4SLinus Torvalds 	 */
1370dbe79bbeSJohn Youn 	ret = get_hub_descriptor(hdev, hub->descriptor);
13711da177e4SLinus Torvalds 	if (ret < 0) {
13721da177e4SLinus Torvalds 		message = "can't read hub descriptor";
13731da177e4SLinus Torvalds 		goto fail;
13741da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
13751da177e4SLinus Torvalds 		message = "hub has too many ports!";
13761da177e4SLinus Torvalds 		ret = -ENODEV;
13771da177e4SLinus Torvalds 		goto fail;
1378769d7368SDavid Linares 	} else if (hub->descriptor->bNbrPorts == 0) {
1379769d7368SDavid Linares 		message = "hub doesn't have any ports!";
1380769d7368SDavid Linares 		ret = -ENODEV;
1381769d7368SDavid Linares 		goto fail;
13821da177e4SLinus Torvalds 	}
13831da177e4SLinus Torvalds 
1384d8521afeSDan Williams 	maxchild = hub->descriptor->bNbrPorts;
1385d8521afeSDan Williams 	dev_info(hub_dev, "%d port%s detected\n", maxchild,
1386d8521afeSDan Williams 			(maxchild == 1) ? "" : "s");
13871da177e4SLinus Torvalds 
1388d8521afeSDan Williams 	hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
1389ff823c79SLan Tianyu 	if (!hub->ports) {
13907cbe5dcaSAlan Stern 		ret = -ENOMEM;
13917cbe5dcaSAlan Stern 		goto fail;
13927cbe5dcaSAlan Stern 	}
13937cbe5dcaSAlan Stern 
139474ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1395430ee58eSSebastian Andrzej Siewior 	if (hub_is_superspeed(hdev)) {
1396430ee58eSSebastian Andrzej Siewior 		unit_load = 150;
1397430ee58eSSebastian Andrzej Siewior 		full_load = 900;
1398430ee58eSSebastian Andrzej Siewior 	} else {
1399430ee58eSSebastian Andrzej Siewior 		unit_load = 100;
1400430ee58eSSebastian Andrzej Siewior 		full_load = 500;
1401430ee58eSSebastian Andrzej Siewior 	}
14021da177e4SLinus Torvalds 
1403dbe79bbeSJohn Youn 	/* FIXME for USB 3.0, skip for now */
1404dbe79bbeSJohn Youn 	if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1405dbe79bbeSJohn Youn 			!(hub_is_superspeed(hdev))) {
14061da177e4SLinus Torvalds 		int	i;
14071da177e4SLinus Torvalds 		char	portstr[USB_MAXCHILDREN + 1];
14081da177e4SLinus Torvalds 
1409d8521afeSDan Williams 		for (i = 0; i < maxchild; i++)
1410dbe79bbeSJohn Youn 			portstr[i] = hub->descriptor->u.hs.DeviceRemovable
14111da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
14121da177e4SLinus Torvalds 				? 'F' : 'R';
1413d8521afeSDan Williams 		portstr[maxchild] = 0;
14141da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
14151da177e4SLinus Torvalds 	} else
14161da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
14171da177e4SLinus Torvalds 
141874ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
14197bf01185SAman Deep 	case HUB_CHAR_COMMON_LPSM:
14201da177e4SLinus Torvalds 		dev_dbg(hub_dev, "ganged power switching\n");
14211da177e4SLinus Torvalds 		break;
14227bf01185SAman Deep 	case HUB_CHAR_INDV_PORT_LPSM:
14231da177e4SLinus Torvalds 		dev_dbg(hub_dev, "individual port power switching\n");
14241da177e4SLinus Torvalds 		break;
14257bf01185SAman Deep 	case HUB_CHAR_NO_LPSM:
14267bf01185SAman Deep 	case HUB_CHAR_LPSM:
14271da177e4SLinus Torvalds 		dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
14281da177e4SLinus Torvalds 		break;
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 
143174ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
14327bf01185SAman Deep 	case HUB_CHAR_COMMON_OCPM:
14331da177e4SLinus Torvalds 		dev_dbg(hub_dev, "global over-current protection\n");
14341da177e4SLinus Torvalds 		break;
14357bf01185SAman Deep 	case HUB_CHAR_INDV_PORT_OCPM:
14361da177e4SLinus Torvalds 		dev_dbg(hub_dev, "individual port over-current protection\n");
14371da177e4SLinus Torvalds 		break;
14387bf01185SAman Deep 	case HUB_CHAR_NO_OCPM:
14397bf01185SAman Deep 	case HUB_CHAR_OCPM:
14401da177e4SLinus Torvalds 		dev_dbg(hub_dev, "no over-current protection\n");
14411da177e4SLinus Torvalds 		break;
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
14451da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
1446cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
14471da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
14487bf01185SAman Deep 	case USB_HUB_PR_FS:
14491da177e4SLinus Torvalds 		break;
14507bf01185SAman Deep 	case USB_HUB_PR_HS_SINGLE_TT:
14511da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Single TT\n");
14521da177e4SLinus Torvalds 		hub->tt.hub = hdev;
14531da177e4SLinus Torvalds 		break;
14547bf01185SAman Deep 	case USB_HUB_PR_HS_MULTI_TT:
14551da177e4SLinus Torvalds 		ret = usb_set_interface(hdev, 0, 1);
14561da177e4SLinus Torvalds 		if (ret == 0) {
14571da177e4SLinus Torvalds 			dev_dbg(hub_dev, "TT per port\n");
14581da177e4SLinus Torvalds 			hub->tt.multi = 1;
14591da177e4SLinus Torvalds 		} else
14601da177e4SLinus Torvalds 			dev_err(hub_dev, "Using single TT (err %d)\n",
14611da177e4SLinus Torvalds 				ret);
14621da177e4SLinus Torvalds 		hub->tt.hub = hdev;
14631da177e4SLinus Torvalds 		break;
14647bf01185SAman Deep 	case USB_HUB_PR_SS:
1465d2e9b4d6SSarah Sharp 		/* USB 3.0 hubs don't have a TT */
1466d2e9b4d6SSarah Sharp 		break;
14671da177e4SLinus Torvalds 	default:
14681da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
14691da177e4SLinus Torvalds 			hdev->descriptor.bDeviceProtocol);
14701da177e4SLinus Torvalds 		break;
14711da177e4SLinus Torvalds 	}
14721da177e4SLinus Torvalds 
1473e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
147474ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1475e09711aeSdavid-b@pacbell.net 	case HUB_TTTT_8_BITS:
1476e09711aeSdavid-b@pacbell.net 		if (hdev->descriptor.bDeviceProtocol != 0) {
1477e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666;
1478e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1479e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1480e09711aeSdavid-b@pacbell.net 				8, hub->tt.think_time);
1481e09711aeSdavid-b@pacbell.net 		}
14821da177e4SLinus Torvalds 		break;
1483e09711aeSdavid-b@pacbell.net 	case HUB_TTTT_16_BITS:
1484e09711aeSdavid-b@pacbell.net 		hub->tt.think_time = 666 * 2;
1485e09711aeSdavid-b@pacbell.net 		dev_dbg(hub_dev, "TT requires at most %d "
1486e09711aeSdavid-b@pacbell.net 				"FS bit times (%d ns)\n",
1487e09711aeSdavid-b@pacbell.net 			16, hub->tt.think_time);
14881da177e4SLinus Torvalds 		break;
1489e09711aeSdavid-b@pacbell.net 	case HUB_TTTT_24_BITS:
1490e09711aeSdavid-b@pacbell.net 		hub->tt.think_time = 666 * 3;
1491e09711aeSdavid-b@pacbell.net 		dev_dbg(hub_dev, "TT requires at most %d "
1492e09711aeSdavid-b@pacbell.net 				"FS bit times (%d ns)\n",
1493e09711aeSdavid-b@pacbell.net 			24, hub->tt.think_time);
14941da177e4SLinus Torvalds 		break;
1495e09711aeSdavid-b@pacbell.net 	case HUB_TTTT_32_BITS:
1496e09711aeSdavid-b@pacbell.net 		hub->tt.think_time = 666 * 4;
1497e09711aeSdavid-b@pacbell.net 		dev_dbg(hub_dev, "TT requires at most %d "
1498e09711aeSdavid-b@pacbell.net 				"FS bit times (%d ns)\n",
1499e09711aeSdavid-b@pacbell.net 			32, hub->tt.think_time);
15001da177e4SLinus Torvalds 		break;
15011da177e4SLinus Torvalds 	}
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
150474ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
15051da177e4SLinus Torvalds 		hub->has_indicators = 1;
15061da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
15071da177e4SLinus Torvalds 	}
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
15101da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
15131da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
15141da177e4SLinus Torvalds 	 */
15151da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
151615b7336eSAlan Stern 	if (ret) {
15171da177e4SLinus Torvalds 		message = "can't get hub status";
15181da177e4SLinus Torvalds 		goto fail;
15191da177e4SLinus Torvalds 	}
1520430ee58eSSebastian Andrzej Siewior 	hcd = bus_to_hcd(hdev->bus);
15217d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
1522430ee58eSSebastian Andrzej Siewior 		if (hcd->power_budget > 0)
1523430ee58eSSebastian Andrzej Siewior 			hdev->bus_mA = hcd->power_budget;
1524430ee58eSSebastian Andrzej Siewior 		else
1525d8521afeSDan Williams 			hdev->bus_mA = full_load * maxchild;
1526430ee58eSSebastian Andrzej Siewior 		if (hdev->bus_mA >= full_load)
1527430ee58eSSebastian Andrzej Siewior 			hub->mA_per_port = full_load;
152855c52718SAlan Stern 		else {
152955c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
153055c52718SAlan Stern 			hub->limited_power = 1;
153155c52718SAlan Stern 		}
15327d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
153355c52718SAlan Stern 		int remaining = hdev->bus_mA -
153455c52718SAlan Stern 			hub->descriptor->bHubContrCurrent;
15351da177e4SLinus Torvalds 
1536430ee58eSSebastian Andrzej Siewior 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1537430ee58eSSebastian Andrzej Siewior 			hub->descriptor->bHubContrCurrent);
1538430ee58eSSebastian Andrzej Siewior 		hub->limited_power = 1;
1539430ee58eSSebastian Andrzej Siewior 
1540d8521afeSDan Williams 		if (remaining < maxchild * unit_load)
154155c52718SAlan Stern 			dev_warn(hub_dev,
154255c52718SAlan Stern 					"insufficient power available "
154355c52718SAlan Stern 					"to use all downstream ports\n");
1544430ee58eSSebastian Andrzej Siewior 		hub->mA_per_port = unit_load;	/* 7.2.1 */
1545430ee58eSSebastian Andrzej Siewior 
154655c52718SAlan Stern 	} else {	/* Self-powered external hub */
154755c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
154855c52718SAlan Stern 		 * provide less current per port? */
1549430ee58eSSebastian Andrzej Siewior 		hub->mA_per_port = full_load;
155055c52718SAlan Stern 	}
1551430ee58eSSebastian Andrzej Siewior 	if (hub->mA_per_port < full_load)
155255c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
155355c52718SAlan Stern 				hub->mA_per_port);
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
15561da177e4SLinus Torvalds 	if (ret < 0) {
15571da177e4SLinus Torvalds 		message = "can't get hub status";
15581da177e4SLinus Torvalds 		goto fail;
15591da177e4SLinus Torvalds 	}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
15621da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
15631da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
15641da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
15651da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
15661da177e4SLinus Torvalds 
156774ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
15681da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
15691da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
15701da177e4SLinus Torvalds 
157188fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
157288fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
157388fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
157488fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
157588fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
157688fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
15771da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
15781da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
15811da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
15841da177e4SLinus Torvalds 	if (!hub->urb) {
15851da177e4SLinus Torvalds 		ret = -ENOMEM;
15861da177e4SLinus Torvalds 		goto fail;
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
15901da177e4SLinus Torvalds 		hub, endpoint->bInterval);
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
15931da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
15941da177e4SLinus Torvalds 		hub->indicator[0] = INDICATOR_CYCLE;
15951da177e4SLinus Torvalds 
1596d8521afeSDan Williams 	mutex_lock(&usb_port_peer_mutex);
1597d8521afeSDan Williams 	for (i = 0; i < maxchild; i++) {
1598e58547ebSKrzysztof Mazur 		ret = usb_hub_create_port_device(hub, i + 1);
1599e58547ebSKrzysztof Mazur 		if (ret < 0) {
1600fa2a9566SLan Tianyu 			dev_err(hub->intfdev,
1601fa2a9566SLan Tianyu 				"couldn't create port%d device.\n", i + 1);
1602d8521afeSDan Williams 			break;
1603d8521afeSDan Williams 		}
1604d8521afeSDan Williams 	}
1605e58547ebSKrzysztof Mazur 	hdev->maxchild = i;
1606e3d10505SDan Williams 	for (i = 0; i < hdev->maxchild; i++) {
1607e3d10505SDan Williams 		struct usb_port *port_dev = hub->ports[i];
1608e3d10505SDan Williams 
1609e3d10505SDan Williams 		pm_runtime_put(&port_dev->dev);
1610e3d10505SDan Williams 	}
1611e3d10505SDan Williams 
1612d8521afeSDan Williams 	mutex_unlock(&usb_port_peer_mutex);
1613d8521afeSDan Williams 	if (ret < 0)
1614d8521afeSDan Williams 		goto fail;
1615fa2a9566SLan Tianyu 
161637ebb549SPetr Mladek 	/* Update the HCD's internal representation of this hub before hub_wq
1617e3d95580SDan Williams 	 * starts getting port status changes for devices under the hub.
1618e3d95580SDan Williams 	 */
1619e3d95580SDan Williams 	if (hcd->driver->update_hub_device) {
1620e3d95580SDan Williams 		ret = hcd->driver->update_hub_device(hcd, hdev,
1621e3d95580SDan Williams 				&hub->tt, GFP_KERNEL);
1622e3d95580SDan Williams 		if (ret < 0) {
1623e3d95580SDan Williams 			message = "can't update HCD hub info";
1624e3d95580SDan Williams 			goto fail;
1625e3d95580SDan Williams 		}
1626e3d95580SDan Williams 	}
1627e3d95580SDan Williams 
1628d2123fd9SLan Tianyu 	usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1629d2123fd9SLan Tianyu 
1630f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
16311da177e4SLinus Torvalds 	return 0;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds fail:
16341da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
16351da177e4SLinus Torvalds 			message, ret);
16361da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
16371da177e4SLinus Torvalds 	return ret;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
1640e8054854SAlan Stern static void hub_release(struct kref *kref)
1641e8054854SAlan Stern {
1642e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1643e8054854SAlan Stern 
16445d14f323SPetr Mladek 	usb_put_dev(hub->hdev);
1645e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1646e8054854SAlan Stern 	kfree(hub);
1647e8054854SAlan Stern }
1648e8054854SAlan Stern 
16491da177e4SLinus Torvalds static unsigned highspeed_hubs;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
16521da177e4SLinus Torvalds {
16531da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
1654fa286188SGreg Kroah-Hartman 	struct usb_device *hdev = interface_to_usbdev(intf);
1655543d7784SAlan Stern 	int port1;
1656fa2a9566SLan Tianyu 
165732a69589SPetr Mladek 	/*
165832a69589SPetr Mladek 	 * Stop adding new hub events. We do not want to block here and thus
165932a69589SPetr Mladek 	 * will not try to remove any pending work item.
166032a69589SPetr Mladek 	 */
1661e8054854SAlan Stern 	hub->disconnected = 1;
16621da177e4SLinus Torvalds 
16637de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
16647de18d8bSAlan Stern 	hub->error = 0;
16654330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
16667de18d8bSAlan Stern 
1667d8521afeSDan Williams 	mutex_lock(&usb_port_peer_mutex);
1668d8521afeSDan Williams 
1669543d7784SAlan Stern 	/* Avoid races with recursively_mark_NOTATTACHED() */
1670543d7784SAlan Stern 	spin_lock_irq(&device_state_lock);
1671543d7784SAlan Stern 	port1 = hdev->maxchild;
1672543d7784SAlan Stern 	hdev->maxchild = 0;
16738b28c752SAlan Stern 	usb_set_intfdata(intf, NULL);
1674543d7784SAlan Stern 	spin_unlock_irq(&device_state_lock);
16751f2235b8SAlexander Shishkin 
1676543d7784SAlan Stern 	for (; port1 > 0; --port1)
1677543d7784SAlan Stern 		usb_hub_remove_port_device(hub, port1);
16781da177e4SLinus Torvalds 
1679d8521afeSDan Williams 	mutex_unlock(&usb_port_peer_mutex);
1680d8521afeSDan Williams 
1681e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
16821da177e4SLinus Torvalds 		highspeed_hubs--;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
1685fa2a9566SLan Tianyu 	kfree(hub->ports);
16861da177e4SLinus Torvalds 	kfree(hub->descriptor);
16871da177e4SLinus Torvalds 	kfree(hub->status);
1688d697cddaSAlan Stern 	kfree(hub->buffer);
16891da177e4SLinus Torvalds 
1690971fcd49SLan Tianyu 	pm_suspend_ignore_children(&intf->dev, false);
1691e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
16921da177e4SLinus Torvalds }
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
16951da177e4SLinus Torvalds {
16961da177e4SLinus Torvalds 	struct usb_host_interface *desc;
16971da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
16981da177e4SLinus Torvalds 	struct usb_device *hdev;
16991da177e4SLinus Torvalds 	struct usb_hub *hub;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
17021da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
17031da177e4SLinus Torvalds 
1704596d789aSMing Lei 	/*
1705596d789aSMing Lei 	 * Set default autosuspend delay as 0 to speedup bus suspend,
1706596d789aSMing Lei 	 * based on the below considerations:
1707596d789aSMing Lei 	 *
1708596d789aSMing Lei 	 * - Unlike other drivers, the hub driver does not rely on the
1709596d789aSMing Lei 	 *   autosuspend delay to provide enough time to handle a wakeup
1710596d789aSMing Lei 	 *   event, and the submitted status URB is just to check future
1711596d789aSMing Lei 	 *   change on hub downstream ports, so it is safe to do it.
1712596d789aSMing Lei 	 *
1713596d789aSMing Lei 	 * - The patch might cause one or more auto supend/resume for
1714596d789aSMing Lei 	 *   below very rare devices when they are plugged into hub
1715596d789aSMing Lei 	 *   first time:
1716596d789aSMing Lei 	 *
1717596d789aSMing Lei 	 *   	devices having trouble initializing, and disconnect
1718596d789aSMing Lei 	 *   	themselves from the bus and then reconnect a second
1719596d789aSMing Lei 	 *   	or so later
1720596d789aSMing Lei 	 *
1721596d789aSMing Lei 	 *   	devices just for downloading firmware, and disconnects
1722596d789aSMing Lei 	 *   	themselves after completing it
1723596d789aSMing Lei 	 *
1724596d789aSMing Lei 	 *   For these quite rare devices, their drivers may change the
1725596d789aSMing Lei 	 *   autosuspend delay of their parent hub in the probe() to one
1726596d789aSMing Lei 	 *   appropriate value to avoid the subtle problem if someone
1727596d789aSMing Lei 	 *   does care it.
1728596d789aSMing Lei 	 *
1729596d789aSMing Lei 	 * - The patch may cause one or more auto suspend/resume on
1730596d789aSMing Lei 	 *   hub during running 'lsusb', but it is probably too
1731596d789aSMing Lei 	 *   infrequent to worry about.
1732596d789aSMing Lei 	 *
1733596d789aSMing Lei 	 * - Change autosuspend delay of hub can avoid unnecessary auto
1734596d789aSMing Lei 	 *   suspend timer for hub, also may decrease power consumption
1735596d789aSMing Lei 	 *   of USB bus.
1736bdd405d2SRoger Quadros 	 *
1737bdd405d2SRoger Quadros 	 * - If user has indicated to prevent autosuspend by passing
1738bdd405d2SRoger Quadros 	 *   usbcore.autosuspend = -1 then keep autosuspend disabled.
1739596d789aSMing Lei 	 */
1740a9ef803dSGreg Kroah-Hartman #ifdef CONFIG_PM_RUNTIME
1741bdd405d2SRoger Quadros 	if (hdev->dev.power.autosuspend_delay >= 0)
1742596d789aSMing Lei 		pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1743a9ef803dSGreg Kroah-Hartman #endif
1744596d789aSMing Lei 
17458ef42dddSAlan Stern 	/*
17468ef42dddSAlan Stern 	 * Hubs have proper suspend/resume support, except for root hubs
17478ef42dddSAlan Stern 	 * where the controller driver doesn't have bus_suspend and
17488ef42dddSAlan Stern 	 * bus_resume methods.
17498ef42dddSAlan Stern 	 */
17508ef42dddSAlan Stern 	if (hdev->parent) {		/* normal device */
1751088f7fecSAlan Stern 		usb_enable_autosuspend(hdev);
17528ef42dddSAlan Stern 	} else {			/* root hub */
17538ef42dddSAlan Stern 		const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
17548ef42dddSAlan Stern 
17558ef42dddSAlan Stern 		if (drv->bus_suspend && drv->bus_resume)
17568ef42dddSAlan Stern 			usb_enable_autosuspend(hdev);
17578ef42dddSAlan Stern 	}
1758088f7fecSAlan Stern 
175938f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1760b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1761b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
176238f3ad5eSFelipe Balbi 		return -E2BIG;
176338f3ad5eSFelipe Balbi 	}
176438f3ad5eSFelipe Balbi 
176589ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
176689ccbdc9SDavid Brownell 	if (hdev->parent) {
176789ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
176889ccbdc9SDavid Brownell 		return -ENODEV;
176989ccbdc9SDavid Brownell 	}
177089ccbdc9SDavid Brownell #endif
177189ccbdc9SDavid Brownell 
17721da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
17731da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
17741da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
17751da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
17761da177e4SLinus Torvalds descriptor_error:
17771da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
17781da177e4SLinus Torvalds 		return -EIO;
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
17821da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
17831da177e4SLinus Torvalds 		goto descriptor_error;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
17861da177e4SLinus Torvalds 
1787fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1788fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
17891da177e4SLinus Torvalds 		goto descriptor_error;
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	/* We found a hub */
17921da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
17931da177e4SLinus Torvalds 
17940a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
17951da177e4SLinus Torvalds 	if (!hub) {
17961da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
17971da177e4SLinus Torvalds 		return -ENOMEM;
17981da177e4SLinus Torvalds 	}
17991da177e4SLinus Torvalds 
1800e8054854SAlan Stern 	kref_init(&hub->kref);
18011da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
18021da177e4SLinus Torvalds 	hub->hdev = hdev;
1803c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
18048520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
180532a69589SPetr Mladek 	INIT_WORK(&hub->events, hub_event);
1806e8054854SAlan Stern 	usb_get_intf(intf);
18075d14f323SPetr Mladek 	usb_get_dev(hdev);
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
181040f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
1811971fcd49SLan Tianyu 	pm_suspend_ignore_children(&intf->dev, true);
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
18141da177e4SLinus Torvalds 		highspeed_hubs++;
18151da177e4SLinus Torvalds 
1816e6f30deaSMing Lei 	if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1817e6f30deaSMing Lei 		hub->quirk_check_port_auto_suspend = 1;
1818e6f30deaSMing Lei 
18191da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
18201da177e4SLinus Torvalds 		return 0;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	hub_disconnect (intf);
18231da177e4SLinus Torvalds 	return -ENODEV;
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds static int
18271da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
1830ad493e5eSLan Tianyu 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
18331da177e4SLinus Torvalds 	switch (code) {
18341da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
18351da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
18361da177e4SLinus Torvalds 		int i;
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
18391da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
18401da177e4SLinus Torvalds 			info->nports = 0;
18411da177e4SLinus Torvalds 		else {
18421da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
18431da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
1844ff823c79SLan Tianyu 				if (hub->ports[i]->child == NULL)
18451da177e4SLinus Torvalds 					info->port[i] = 0;
18461da177e4SLinus Torvalds 				else
18471da177e4SLinus Torvalds 					info->port[i] =
1848ff823c79SLan Tianyu 						hub->ports[i]->child->devnum;
18491da177e4SLinus Torvalds 			}
18501da177e4SLinus Torvalds 		}
18511da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 		return info->nports + 1;
18541da177e4SLinus Torvalds 		}
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	default:
18571da177e4SLinus Torvalds 		return -ENOSYS;
18581da177e4SLinus Torvalds 	}
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18617cbe5dcaSAlan Stern /*
18627cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
18637cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
18647cbe5dcaSAlan Stern  */
18657cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
18669b6f0c4bSValentina Manea 		struct usb_dev_state ***ppowner)
18677cbe5dcaSAlan Stern {
186841341261SMathias Nyman 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
186941341261SMathias Nyman 
18707cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
18717cbe5dcaSAlan Stern 		return -ENODEV;
18727cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
18737cbe5dcaSAlan Stern 		return -EINVAL;
18747cbe5dcaSAlan Stern 
187541341261SMathias Nyman 	/* Devices not managed by the hub driver
18767cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
18777cbe5dcaSAlan Stern 	 */
187841341261SMathias Nyman 	*ppowner = &(hub->ports[port1 - 1]->port_owner);
18797cbe5dcaSAlan Stern 	return 0;
18807cbe5dcaSAlan Stern }
18817cbe5dcaSAlan Stern 
18827cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
1883336c5c31SLan Tianyu int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
18849b6f0c4bSValentina Manea 		       struct usb_dev_state *owner)
18857cbe5dcaSAlan Stern {
18867cbe5dcaSAlan Stern 	int rc;
18879b6f0c4bSValentina Manea 	struct usb_dev_state **powner;
18887cbe5dcaSAlan Stern 
18897cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
18907cbe5dcaSAlan Stern 	if (rc)
18917cbe5dcaSAlan Stern 		return rc;
18927cbe5dcaSAlan Stern 	if (*powner)
18937cbe5dcaSAlan Stern 		return -EBUSY;
18947cbe5dcaSAlan Stern 	*powner = owner;
18957cbe5dcaSAlan Stern 	return rc;
18967cbe5dcaSAlan Stern }
18976080cd0eSValentina Manea EXPORT_SYMBOL_GPL(usb_hub_claim_port);
18987cbe5dcaSAlan Stern 
1899336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
19009b6f0c4bSValentina Manea 			 struct usb_dev_state *owner)
19017cbe5dcaSAlan Stern {
19027cbe5dcaSAlan Stern 	int rc;
19039b6f0c4bSValentina Manea 	struct usb_dev_state **powner;
19047cbe5dcaSAlan Stern 
19057cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
19067cbe5dcaSAlan Stern 	if (rc)
19077cbe5dcaSAlan Stern 		return rc;
19087cbe5dcaSAlan Stern 	if (*powner != owner)
19097cbe5dcaSAlan Stern 		return -ENOENT;
19107cbe5dcaSAlan Stern 	*powner = NULL;
19117cbe5dcaSAlan Stern 	return rc;
19127cbe5dcaSAlan Stern }
19136080cd0eSValentina Manea EXPORT_SYMBOL_GPL(usb_hub_release_port);
19147cbe5dcaSAlan Stern 
19159b6f0c4bSValentina Manea void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
19167cbe5dcaSAlan Stern {
1917ad493e5eSLan Tianyu 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
19187cbe5dcaSAlan Stern 	int n;
19197cbe5dcaSAlan Stern 
1920fa2a9566SLan Tianyu 	for (n = 0; n < hdev->maxchild; n++) {
1921fa2a9566SLan Tianyu 		if (hub->ports[n]->port_owner == owner)
1922fa2a9566SLan Tianyu 			hub->ports[n]->port_owner = NULL;
19237cbe5dcaSAlan Stern 	}
1924fa2a9566SLan Tianyu 
19257cbe5dcaSAlan Stern }
19267cbe5dcaSAlan Stern 
19277cbe5dcaSAlan Stern /* The caller must hold udev's lock */
19287cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
19297cbe5dcaSAlan Stern {
19307cbe5dcaSAlan Stern 	struct usb_hub *hub;
19317cbe5dcaSAlan Stern 
19327cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
19337cbe5dcaSAlan Stern 		return false;
1934ad493e5eSLan Tianyu 	hub = usb_hub_to_struct_hub(udev->parent);
1935fa2a9566SLan Tianyu 	return !!hub->ports[udev->portnum - 1]->port_owner;
19367cbe5dcaSAlan Stern }
19377cbe5dcaSAlan Stern 
19381da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
19391da177e4SLinus Torvalds {
1940ad493e5eSLan Tianyu 	struct usb_hub *hub = usb_hub_to_struct_hub(udev);
19411da177e4SLinus Torvalds 	int i;
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
1944ff823c79SLan Tianyu 		if (hub->ports[i]->child)
1945ff823c79SLan Tianyu 			recursively_mark_NOTATTACHED(hub->ports[i]->child);
19461da177e4SLinus Torvalds 	}
19479bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_SUSPENDED)
194815123006SSarah Sharp 		udev->active_duration -= jiffies;
19491da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds /**
19531da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
19541da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
19551da177e4SLinus Torvalds  * @new_state: new state value to be stored
19561da177e4SLinus Torvalds  *
19571da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
19581da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
19591da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
19601da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
19611da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
19621da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
19631da177e4SLinus Torvalds  * device_state_lock spinlock.
19641da177e4SLinus Torvalds  *
19651da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
19661da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
19671da177e4SLinus Torvalds  *
19681da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
19691da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
19701da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
19711da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
19721da177e4SLinus Torvalds  */
19731da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
19741da177e4SLinus Torvalds 		enum usb_device_state new_state)
19751da177e4SLinus Torvalds {
19761da177e4SLinus Torvalds 	unsigned long flags;
19774681b171SRafael J. Wysocki 	int wakeup = -1;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
19801da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
19811da177e4SLinus Torvalds 		;	/* do nothing */
1982b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1983fb669cc0SDavid Brownell 
1984fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1985fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1986fb669cc0SDavid Brownell 		 */
1987fb669cc0SDavid Brownell 		if (udev->parent) {
1988645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1989645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1990645daaabSAlan Stern 				;	/* No change to wakeup settings */
1991645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
1992ddbe1fcaSLu Baolu 				wakeup = (udev->quirks &
1993ddbe1fcaSLu Baolu 					USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
1994ddbe1fcaSLu Baolu 					udev->actconfig->desc.bmAttributes &
1995ddbe1fcaSLu Baolu 					USB_CONFIG_ATT_WAKEUP;
1996645daaabSAlan Stern 			else
19974681b171SRafael J. Wysocki 				wakeup = 0;
1998fb669cc0SDavid Brownell 		}
199915123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
200015123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
200115123006SSarah Sharp 			udev->active_duration -= jiffies;
200215123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
200315123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
200415123006SSarah Sharp 			udev->active_duration += jiffies;
2005645daaabSAlan Stern 		udev->state = new_state;
2006b94dc6b5SDavid Brownell 	} else
20071da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
20081da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
20094681b171SRafael J. Wysocki 	if (wakeup >= 0)
20104681b171SRafael J. Wysocki 		device_set_wakeup_capable(&udev->dev, wakeup);
20111da177e4SLinus Torvalds }
20126da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
20131da177e4SLinus Torvalds 
20148af548dcSInaky Perez-Gonzalez /*
20153b29b68bSAlan Stern  * Choose a device number.
20163b29b68bSAlan Stern  *
20173b29b68bSAlan Stern  * Device numbers are used as filenames in usbfs.  On USB-1.1 and
20183b29b68bSAlan Stern  * USB-2.0 buses they are also used as device addresses, however on
20193b29b68bSAlan Stern  * USB-3.0 buses the address is assigned by the controller hardware
20203b29b68bSAlan Stern  * and it usually is not the same as the device number.
20213b29b68bSAlan Stern  *
20228af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
20238af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
20248af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
20258af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
20268af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
20278af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
20288af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
20298af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
20308af548dcSInaky Perez-Gonzalez  * [X | 0x80].
20318af548dcSInaky Perez-Gonzalez  *
20328af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
20338af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
20348af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
20358af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
20368af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
2037c6515272SSarah Sharp  *
2038c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
2039c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
2040c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
2041c6515272SSarah Sharp  * command to the hardware.
20428af548dcSInaky Perez-Gonzalez  */
20433b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev)
20441da177e4SLinus Torvalds {
20451da177e4SLinus Torvalds 	int		devnum;
20461da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
20471da177e4SLinus Torvalds 
2048638139ebSPetr Mladek 	/* be safe when more hub events are proceed in parallel */
2049638139ebSPetr Mladek 	mutex_lock(&bus->usb_address0_mutex);
20508af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
20518af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
20528af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
20538af548dcSInaky Perez-Gonzalez 	} else {
20548af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
20558af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
20561da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
20571da177e4SLinus Torvalds 					    bus->devnum_next);
20581da177e4SLinus Torvalds 		if (devnum >= 128)
20598af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
20608af548dcSInaky Perez-Gonzalez 						    128, 1);
20611da177e4SLinus Torvalds 		bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
20628af548dcSInaky Perez-Gonzalez 	}
20631da177e4SLinus Torvalds 	if (devnum < 128) {
20641da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
20651da177e4SLinus Torvalds 		udev->devnum = devnum;
20661da177e4SLinus Torvalds 	}
2067638139ebSPetr Mladek 	mutex_unlock(&bus->usb_address0_mutex);
20681da177e4SLinus Torvalds }
20691da177e4SLinus Torvalds 
20703b29b68bSAlan Stern static void release_devnum(struct usb_device *udev)
20711da177e4SLinus Torvalds {
20721da177e4SLinus Torvalds 	if (udev->devnum > 0) {
20731da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
20741da177e4SLinus Torvalds 		udev->devnum = -1;
20751da177e4SLinus Torvalds 	}
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
20783b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum)
20794953d141SDavid Vrabel {
20804953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
20814953d141SDavid Vrabel 	if (!udev->wusb)
20824953d141SDavid Vrabel 		udev->devnum = devnum;
20834953d141SDavid Vrabel }
20844953d141SDavid Vrabel 
2085f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev)
2086f7410cedSHerbert Xu {
2087f7410cedSHerbert Xu 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2088f7410cedSHerbert Xu 
2089f7410cedSHerbert Xu 	/* Root hubs aren't real devices, so don't free HCD resources */
2090f7410cedSHerbert Xu 	if (hcd->driver->free_dev && udev->parent)
2091f7410cedSHerbert Xu 		hcd->driver->free_dev(hcd, udev);
2092f7410cedSHerbert Xu }
2093f7410cedSHerbert Xu 
20947027df36SDan Williams static void hub_disconnect_children(struct usb_device *udev)
20957027df36SDan Williams {
20967027df36SDan Williams 	struct usb_hub *hub = usb_hub_to_struct_hub(udev);
20977027df36SDan Williams 	int i;
20987027df36SDan Williams 
20997027df36SDan Williams 	/* Free up all the children before we remove this device */
21007027df36SDan Williams 	for (i = 0; i < udev->maxchild; i++) {
21017027df36SDan Williams 		if (hub->ports[i]->child)
21027027df36SDan Williams 			usb_disconnect(&hub->ports[i]->child);
21037027df36SDan Williams 	}
21047027df36SDan Williams }
21057027df36SDan Williams 
21061da177e4SLinus Torvalds /**
21071da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
21081da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
21091da177e4SLinus Torvalds  * Context: !in_interrupt ()
21101da177e4SLinus Torvalds  *
21111da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
21121da177e4SLinus Torvalds  *
21131da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
2114db8f2aa3SBjorn Helgaas  * If *pdev is a root hub then the caller must hold the usb_bus_list_lock,
2115db8f2aa3SBjorn Helgaas  * which protects the set of root hubs as well as the list of buses.
21161da177e4SLinus Torvalds  *
21171da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
21181da177e4SLinus Torvalds  * controllers) should ever call this.
21191da177e4SLinus Torvalds  *
21201da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
21211da177e4SLinus Torvalds  */
21221da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
21231da177e4SLinus Torvalds {
21247027df36SDan Williams 	struct usb_port *port_dev = NULL;
21251da177e4SLinus Torvalds 	struct usb_device *udev = *pdev;
21265b1dc209SPeter Chen 	struct usb_hub *hub = NULL;
21275b1dc209SPeter Chen 	int port1 = 1;
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
21301da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
213125985edcSLucas De Marchi 	 * this quiesces everything except pending urbs.
21321da177e4SLinus Torvalds 	 */
21331da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
21343b29b68bSAlan Stern 	dev_info(&udev->dev, "USB disconnect, device number %d\n",
21353b29b68bSAlan Stern 			udev->devnum);
21361da177e4SLinus Torvalds 
21379ad3d6ccSAlan Stern 	usb_lock_device(udev);
21389ad3d6ccSAlan Stern 
21397027df36SDan Williams 	hub_disconnect_children(udev);
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
21421da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
21431da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
21441da177e4SLinus Torvalds 	 */
2145782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
21461da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
2147cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
21481da177e4SLinus Torvalds 
2149fde26380SLan Tianyu 	if (udev->parent) {
21507027df36SDan Williams 		port1 = udev->portnum;
21517027df36SDan Williams 		hub = usb_hub_to_struct_hub(udev->parent);
21527027df36SDan Williams 		port_dev = hub->ports[port1 - 1];
2153fde26380SLan Tianyu 
2154fde26380SLan Tianyu 		sysfs_remove_link(&udev->dev.kobj, "port");
2155fde26380SLan Tianyu 		sysfs_remove_link(&port_dev->dev.kobj, "device");
2156971fcd49SLan Tianyu 
21577027df36SDan Williams 		/*
21587027df36SDan Williams 		 * As usb_port_runtime_resume() de-references udev, make
21597027df36SDan Williams 		 * sure no resumes occur during removal
21607027df36SDan Williams 		 */
21617027df36SDan Williams 		if (!test_and_set_bit(port1, hub->child_usage_bits))
21627027df36SDan Williams 			pm_runtime_get_sync(&port_dev->dev);
2163fde26380SLan Tianyu 	}
2164fde26380SLan Tianyu 
21653b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
2166782da727SAlan Stern 	usb_unlock_device(udev);
21673099e75aSGreg Kroah-Hartman 
2168782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
21693b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
21703b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
2171782da727SAlan Stern 	 */
2172782da727SAlan Stern 	device_del(&udev->dev);
2173782da727SAlan Stern 
2174782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
21751da177e4SLinus Torvalds 	 * (or root_hub) pointer.
21761da177e4SLinus Torvalds 	 */
21773b29b68bSAlan Stern 	release_devnum(udev);
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
21801da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
21811da177e4SLinus Torvalds 	*pdev = NULL;
21821da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
21831da177e4SLinus Torvalds 
21847027df36SDan Williams 	if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
21857027df36SDan Williams 		pm_runtime_put(&port_dev->dev);
21867027df36SDan Williams 
2187f7410cedSHerbert Xu 	hub_free_dev(udev);
2188f7410cedSHerbert Xu 
2189782da727SAlan Stern 	put_device(&udev->dev);
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
2192f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
21931da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
21941da177e4SLinus Torvalds {
21951da177e4SLinus Torvalds 	if (!string)
21961da177e4SLinus Torvalds 		return;
2197f2ec522eSJoe Perches 	dev_info(&udev->dev, "%s: %s\n", id, string);
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
2200f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
2201f2a383e4SGreg Kroah-Hartman {
2202f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2203f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
2204f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
2205b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
2206b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2207f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
2208f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
2209f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
2210f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
2211f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
2212f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
2213f2a383e4SGreg Kroah-Hartman }
22141da177e4SLinus Torvalds #else
2215f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
22161da177e4SLinus Torvalds #endif
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 
22193ede760fSOliver Neukum /**
22208d8558d1SAlan Stern  * usb_enumerate_device_otg - FIXME (usbcore-internal)
22213ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
22223ede760fSOliver Neukum  *
22238d8558d1SAlan Stern  * Finish enumeration for On-The-Go devices
2224626f090cSYacine Belkadi  *
2225626f090cSYacine Belkadi  * Return: 0 if successful. A negative error code otherwise.
22263ede760fSOliver Neukum  */
22278d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
22281da177e4SLinus Torvalds {
2229d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
22321da177e4SLinus Torvalds 	/*
22331da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
22341da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
22351da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
22361da177e4SLinus Torvalds 	 */
22371da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
22381da177e4SLinus Torvalds 			&& udev->config
22391da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
22402eb5052eSFelipe Balbi 		struct usb_otg_descriptor	*desc = NULL;
22411da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
22441da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
22451da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
22461da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
22471da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
224812c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 				dev_info(&udev->dev,
22511da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
22521da177e4SLinus Torvalds 					(port1 == bus->otg_port)
22531da177e4SLinus Torvalds 						? "" : "non-");
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
22561da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
22571da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
22581da177e4SLinus Torvalds 				err = usb_control_msg(udev,
22591da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
22601da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
22611da177e4SLinus Torvalds 					bus->b_hnp_enable
22621da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
22631da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
22641da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
22651da177e4SLinus Torvalds 				if (err < 0) {
22661da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
22671da177e4SLinus Torvalds 					 * customize to match your product.
22681da177e4SLinus Torvalds 					 */
22691da177e4SLinus Torvalds 					dev_info(&udev->dev,
2270b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
22711da177e4SLinus Torvalds 						err);
22721da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
22731da177e4SLinus Torvalds 				}
22741da177e4SLinus Torvalds 			}
22751da177e4SLinus Torvalds 		}
22761da177e4SLinus Torvalds 	}
22771da177e4SLinus Torvalds #endif
2278d9d16e8aSInaky Perez-Gonzalez 	return err;
2279d9d16e8aSInaky Perez-Gonzalez }
22801da177e4SLinus Torvalds 
2281d9d16e8aSInaky Perez-Gonzalez 
2282d9d16e8aSInaky Perez-Gonzalez /**
22838d8558d1SAlan Stern  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2284d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
2285d9d16e8aSInaky Perez-Gonzalez  *
2286d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
2287d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
2288d9d16e8aSInaky Perez-Gonzalez  * environment.
2289d9d16e8aSInaky Perez-Gonzalez  *
2290d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
2291d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
2292d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
2293626f090cSYacine Belkadi  *
2294626f090cSYacine Belkadi  * Return: 0 if successful. A negative error code otherwise.
2295d9d16e8aSInaky Perez-Gonzalez  */
22968d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
2297d9d16e8aSInaky Perez-Gonzalez {
2298d9d16e8aSInaky Perez-Gonzalez 	int err;
2299026f3fcbSPeter Chen 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2300d9d16e8aSInaky Perez-Gonzalez 
2301d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
2302d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
2303d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
2304e9e88fb7SAlan Stern 			if (err != -ENODEV)
2305d9d16e8aSInaky Perez-Gonzalez 				dev_err(&udev->dev, "can't read configurations, error %d\n",
2306d9d16e8aSInaky Perez-Gonzalez 						err);
230780da2e0dSLaurent Pinchart 			return err;
2308d9d16e8aSInaky Perez-Gonzalez 		}
2309d9d16e8aSInaky Perez-Gonzalez 	}
231083e83ecbSThomas Pugliese 
2311d9d16e8aSInaky Perez-Gonzalez 	/* read the standard strings and cache them if present */
2312d9d16e8aSInaky Perez-Gonzalez 	udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2313d9d16e8aSInaky Perez-Gonzalez 	udev->manufacturer = usb_cache_string(udev,
2314d9d16e8aSInaky Perez-Gonzalez 					      udev->descriptor.iManufacturer);
2315d9d16e8aSInaky Perez-Gonzalez 	udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
231683e83ecbSThomas Pugliese 
23178d8558d1SAlan Stern 	err = usb_enumerate_device_otg(udev);
231880da2e0dSLaurent Pinchart 	if (err < 0)
2319d9d16e8aSInaky Perez-Gonzalez 		return err;
232080da2e0dSLaurent Pinchart 
2321026f3fcbSPeter Chen 	if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
2322e5a9d621SPeter Chen 		!is_targeted(udev)) {
2323026f3fcbSPeter Chen 		/* Maybe it can talk to us, though we can't talk to it.
2324026f3fcbSPeter Chen 		 * (Includes HNP test device.)
2325026f3fcbSPeter Chen 		 */
2326e5a9d621SPeter Chen 		if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2327e5a9d621SPeter Chen 			|| udev->bus->is_b_host)) {
2328026f3fcbSPeter Chen 			err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2329026f3fcbSPeter Chen 			if (err < 0)
2330026f3fcbSPeter Chen 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2331026f3fcbSPeter Chen 		}
2332026f3fcbSPeter Chen 		return -ENOTSUPP;
2333026f3fcbSPeter Chen 	}
2334026f3fcbSPeter Chen 
233580da2e0dSLaurent Pinchart 	usb_detect_interface_quirks(udev);
233680da2e0dSLaurent Pinchart 
233780da2e0dSLaurent Pinchart 	return 0;
2338d9d16e8aSInaky Perez-Gonzalez }
2339d9d16e8aSInaky Perez-Gonzalez 
2340d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev)
2341d35e70d5SMatthew Garrett {
2342d35e70d5SMatthew Garrett 	struct usb_device *hdev = udev->parent;
2343d35e70d5SMatthew Garrett 	struct usb_hub *hub;
2344d35e70d5SMatthew Garrett 	u8 port = udev->portnum;
2345d35e70d5SMatthew Garrett 	u16 wHubCharacteristics;
2346d35e70d5SMatthew Garrett 	bool removable = true;
2347d35e70d5SMatthew Garrett 
2348d35e70d5SMatthew Garrett 	if (!hdev)
2349d35e70d5SMatthew Garrett 		return;
2350d35e70d5SMatthew Garrett 
2351ad493e5eSLan Tianyu 	hub = usb_hub_to_struct_hub(udev->parent);
2352d35e70d5SMatthew Garrett 
2353d35e70d5SMatthew Garrett 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2354d35e70d5SMatthew Garrett 
2355d35e70d5SMatthew Garrett 	if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2356d35e70d5SMatthew Garrett 		return;
2357d35e70d5SMatthew Garrett 
2358d35e70d5SMatthew Garrett 	if (hub_is_superspeed(hdev)) {
2359ca3c1539SLan Tianyu 		if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2360ca3c1539SLan Tianyu 				& (1 << port))
2361d35e70d5SMatthew Garrett 			removable = false;
2362d35e70d5SMatthew Garrett 	} else {
2363d35e70d5SMatthew Garrett 		if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2364d35e70d5SMatthew Garrett 			removable = false;
2365d35e70d5SMatthew Garrett 	}
2366d35e70d5SMatthew Garrett 
2367d35e70d5SMatthew Garrett 	if (removable)
2368d35e70d5SMatthew Garrett 		udev->removable = USB_DEVICE_REMOVABLE;
2369d35e70d5SMatthew Garrett 	else
2370d35e70d5SMatthew Garrett 		udev->removable = USB_DEVICE_FIXED;
2371a4204ff0SDan Williams 
2372a4204ff0SDan Williams 	/*
2373a4204ff0SDan Williams 	 * Platform firmware may have populated an alternative value for
2374a4204ff0SDan Williams 	 * removable.  If the parent port has a known connect_type use
2375a4204ff0SDan Williams 	 * that instead.
2376a4204ff0SDan Williams 	 */
2377a4204ff0SDan Williams 	switch (hub->ports[udev->portnum - 1]->connect_type) {
2378a4204ff0SDan Williams 	case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2379a4204ff0SDan Williams 		udev->removable = USB_DEVICE_REMOVABLE;
2380a4204ff0SDan Williams 		break;
2381a4204ff0SDan Williams 	case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2382a4204ff0SDan Williams 		udev->removable = USB_DEVICE_FIXED;
2383a4204ff0SDan Williams 		break;
2384a4204ff0SDan Williams 	default: /* use what was set above */
2385a4204ff0SDan Williams 		break;
2386a4204ff0SDan Williams 	}
2387d35e70d5SMatthew Garrett }
2388d9d16e8aSInaky Perez-Gonzalez 
2389d9d16e8aSInaky Perez-Gonzalez /**
2390d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
2391d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
2392d9d16e8aSInaky Perez-Gonzalez  *
23938d8558d1SAlan Stern  * This is called with devices which have been detected but not fully
23948d8558d1SAlan Stern  * enumerated.  The device descriptor is available, but not descriptors
2395d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
2396d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
2397d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
2398d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
2399d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
2400d9d16e8aSInaky Perez-Gonzalez  *
2401d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
2402d9d16e8aSInaky Perez-Gonzalez  *
2403d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
2404626f090cSYacine Belkadi  *
2405626f090cSYacine Belkadi  * Return: Whether the device is configured properly or not. Zero if the
2406626f090cSYacine Belkadi  * interface was registered with the driver core; else a negative errno
2407626f090cSYacine Belkadi  * value.
2408626f090cSYacine Belkadi  *
2409d9d16e8aSInaky Perez-Gonzalez  */
2410d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
2411d9d16e8aSInaky Perez-Gonzalez {
2412d9d16e8aSInaky Perez-Gonzalez 	int err;
2413d9d16e8aSInaky Perez-Gonzalez 
241416985408SDan Streetman 	if (udev->parent) {
241516985408SDan Streetman 		/* Initialize non-root-hub device wakeup to disabled;
241616985408SDan Streetman 		 * device (un)configuration controls wakeup capable
241716985408SDan Streetman 		 * sysfs power/wakeup controls wakeup enabled/disabled
241816985408SDan Streetman 		 */
241916985408SDan Streetman 		device_init_wakeup(&udev->dev, 0);
242016985408SDan Streetman 	}
242116985408SDan Streetman 
24229bbdf1e0SAlan Stern 	/* Tell the runtime-PM framework the device is active */
24239bbdf1e0SAlan Stern 	pm_runtime_set_active(&udev->dev);
2424c08512c7SAlan Stern 	pm_runtime_get_noresume(&udev->dev);
2425fcc4a01eSAlan Stern 	pm_runtime_use_autosuspend(&udev->dev);
24269bbdf1e0SAlan Stern 	pm_runtime_enable(&udev->dev);
24279bbdf1e0SAlan Stern 
2428c08512c7SAlan Stern 	/* By default, forbid autosuspend for all devices.  It will be
2429c08512c7SAlan Stern 	 * allowed for hubs during binding.
2430c08512c7SAlan Stern 	 */
2431c08512c7SAlan Stern 	usb_disable_autosuspend(udev);
2432c08512c7SAlan Stern 
24338d8558d1SAlan Stern 	err = usb_enumerate_device(udev);	/* Read descriptors */
2434d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
2435d9d16e8aSInaky Perez-Gonzalez 		goto fail;
2436c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2437c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
2438c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
24399f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
24409f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
24419f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
24429f8b17e6SKay Sievers 
24436cd13201SAlan Stern 	/* Tell the world! */
24446cd13201SAlan Stern 	announce_device(udev);
2445195af2ccSAlan Stern 
2446b04b3156STheodore Ts'o 	if (udev->serial)
2447b04b3156STheodore Ts'o 		add_device_randomness(udev->serial, strlen(udev->serial));
2448b04b3156STheodore Ts'o 	if (udev->product)
2449b04b3156STheodore Ts'o 		add_device_randomness(udev->product, strlen(udev->product));
2450b04b3156STheodore Ts'o 	if (udev->manufacturer)
2451b04b3156STheodore Ts'o 		add_device_randomness(udev->manufacturer,
2452b04b3156STheodore Ts'o 				      strlen(udev->manufacturer));
2453b04b3156STheodore Ts'o 
2454927bc916SRafael J. Wysocki 	device_enable_async_suspend(&udev->dev);
2455d35e70d5SMatthew Garrett 
2456a4204ff0SDan Williams 	/* check whether the hub or firmware marks this port as non-removable */
2457d35e70d5SMatthew Garrett 	if (udev->parent)
2458d35e70d5SMatthew Garrett 		set_usb_port_removable(udev);
2459d35e70d5SMatthew Garrett 
2460782da727SAlan Stern 	/* Register the device.  The device driver is responsible
24613b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
24623b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
2463782da727SAlan Stern 	 */
24641da177e4SLinus Torvalds 	err = device_add(&udev->dev);
24651da177e4SLinus Torvalds 	if (err) {
24661da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
24671da177e4SLinus Torvalds 		goto fail;
24681da177e4SLinus Torvalds 	}
24699ad3d6ccSAlan Stern 
2470fde26380SLan Tianyu 	/* Create link files between child device and usb port device. */
2471fde26380SLan Tianyu 	if (udev->parent) {
2472ad493e5eSLan Tianyu 		struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2473d5c3834eSDan Williams 		int port1 = udev->portnum;
2474d5c3834eSDan Williams 		struct usb_port	*port_dev = hub->ports[port1 - 1];
2475fde26380SLan Tianyu 
2476fde26380SLan Tianyu 		err = sysfs_create_link(&udev->dev.kobj,
2477fde26380SLan Tianyu 				&port_dev->dev.kobj, "port");
2478fde26380SLan Tianyu 		if (err)
2479fde26380SLan Tianyu 			goto fail;
2480fde26380SLan Tianyu 
2481fde26380SLan Tianyu 		err = sysfs_create_link(&port_dev->dev.kobj,
2482fde26380SLan Tianyu 				&udev->dev.kobj, "device");
2483fde26380SLan Tianyu 		if (err) {
2484fde26380SLan Tianyu 			sysfs_remove_link(&udev->dev.kobj, "port");
2485fde26380SLan Tianyu 			goto fail;
2486fde26380SLan Tianyu 		}
2487971fcd49SLan Tianyu 
2488d5c3834eSDan Williams 		if (!test_and_set_bit(port1, hub->child_usage_bits))
2489971fcd49SLan Tianyu 			pm_runtime_get_sync(&port_dev->dev);
2490fde26380SLan Tianyu 	}
2491fde26380SLan Tianyu 
24923b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2493c08512c7SAlan Stern 	usb_mark_last_busy(udev);
2494c08512c7SAlan Stern 	pm_runtime_put_sync_autosuspend(&udev->dev);
2495c066475eSGreg Kroah-Hartman 	return err;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds fail:
24981da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
24999bbdf1e0SAlan Stern 	pm_runtime_disable(&udev->dev);
25009bbdf1e0SAlan Stern 	pm_runtime_set_suspended(&udev->dev);
2501d9d16e8aSInaky Perez-Gonzalez 	return err;
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds 
250493993a0aSInaky Perez-Gonzalez 
250593993a0aSInaky Perez-Gonzalez /**
2506fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2507fd39c86bSRandy Dunlap  * @usb_dev: USB device
2508fd39c86bSRandy Dunlap  *
2509fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
2510fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
251193993a0aSInaky Perez-Gonzalez  *
251293993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
251393993a0aSInaky Perez-Gonzalez  * defer its call.
2514626f090cSYacine Belkadi  *
2515626f090cSYacine Belkadi  * Return: 0.
251693993a0aSInaky Perez-Gonzalez  */
251793993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
251893993a0aSInaky Perez-Gonzalez {
251993993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
252093993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
252193993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
2522da307123SAlan Stern 
252393993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
252493993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
2525da307123SAlan Stern 
252693993a0aSInaky Perez-Gonzalez out_unauthorized:
252793993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
252893993a0aSInaky Perez-Gonzalez 	return 0;
252993993a0aSInaky Perez-Gonzalez }
253093993a0aSInaky Perez-Gonzalez 
253193993a0aSInaky Perez-Gonzalez 
253293993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
253393993a0aSInaky Perez-Gonzalez {
253493993a0aSInaky Perez-Gonzalez 	int result = 0, c;
2535da307123SAlan Stern 
253693993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
253793993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
253893993a0aSInaky Perez-Gonzalez 		goto out_authorized;
2539da307123SAlan Stern 
254093993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
254193993a0aSInaky Perez-Gonzalez 	if (result < 0) {
254293993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
254393993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
254493993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
254593993a0aSInaky Perez-Gonzalez 	}
254693993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
254793993a0aSInaky Perez-Gonzalez 	if (result < 0) {
254893993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
254993993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
255093993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
255193993a0aSInaky Perez-Gonzalez 	}
2552da307123SAlan Stern 
255393993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
255493993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
255593993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
255693993a0aSInaky Perez-Gonzalez 	 */
2557b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
255893993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
255993993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
256093993a0aSInaky Perez-Gonzalez 		if (result) {
256193993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
256293993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
256393993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
256493993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
256593993a0aSInaky Perez-Gonzalez 		}
256693993a0aSInaky Perez-Gonzalez 	}
256793993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
2568da307123SAlan Stern 
256993993a0aSInaky Perez-Gonzalez error_device_descriptor:
2570da307123SAlan Stern 	usb_autosuspend_device(usb_dev);
257193993a0aSInaky Perez-Gonzalez error_autoresume:
257293993a0aSInaky Perez-Gonzalez out_authorized:
2573781b2137SMatthias Beyer 	usb_unlock_device(usb_dev);	/* complements locktree */
257493993a0aSInaky Perez-Gonzalez 	return result;
257593993a0aSInaky Perez-Gonzalez }
257693993a0aSInaky Perez-Gonzalez 
257793993a0aSInaky Perez-Gonzalez 
25780165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
25790165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
25800165de09SInaky Perez-Gonzalez {
25810165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
25820165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
25830165de09SInaky Perez-Gonzalez 		return 0;
25840165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
25850165de09SInaky Perez-Gonzalez 	return hcd->wireless;
25860165de09SInaky Perez-Gonzalez }
25870165de09SInaky Perez-Gonzalez 
25880165de09SInaky Perez-Gonzalez 
25891da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
25901da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
25911da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
25921da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
259390ab5ee9SRusty Russell #define USE_NEW_SCHEME(i)	((i) / 2 == (int)old_scheme_first)
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
25961da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
259775d7cf72SAndiry Xu #define HUB_BH_RESET_TIME	50
25981da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
259977c7f072SSarah Sharp #define HUB_RESET_TIMEOUT	800
26001da177e4SLinus Torvalds 
260148fc7dbdSDan Williams /*
260248fc7dbdSDan Williams  * "New scheme" enumeration causes an extra state transition to be
260348fc7dbdSDan Williams  * exposed to an xhci host and causes USB3 devices to receive control
260448fc7dbdSDan Williams  * commands in the default state.  This has been seen to cause
260548fc7dbdSDan Williams  * enumeration failures, so disable this enumeration scheme for USB3
260648fc7dbdSDan Williams  * devices.
260748fc7dbdSDan Williams  */
260848fc7dbdSDan Williams static bool use_new_scheme(struct usb_device *udev, int retry)
260948fc7dbdSDan Williams {
261048fc7dbdSDan Williams 	if (udev->speed == USB_SPEED_SUPER)
261148fc7dbdSDan Williams 		return false;
261248fc7dbdSDan Williams 
261348fc7dbdSDan Williams 	return USE_NEW_SCHEME(retry);
261448fc7dbdSDan Williams }
261548fc7dbdSDan Williams 
261610d674a8SSarah Sharp static int hub_port_reset(struct usb_hub *hub, int port1,
261710d674a8SSarah Sharp 			struct usb_device *udev, unsigned int delay, bool warm);
261810d674a8SSarah Sharp 
2619025d4430SRahul Bedarkar /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
26208bea2bd3SStanislaw Ledwon  * Port worm reset is required to recover
26218bea2bd3SStanislaw Ledwon  */
26223cd12f91SDan Williams static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
26233cd12f91SDan Williams 		u16 portstatus)
262410d674a8SSarah Sharp {
26253cd12f91SDan Williams 	u16 link_state;
26263cd12f91SDan Williams 
26273cd12f91SDan Williams 	if (!hub_is_superspeed(hub->hdev))
26283cd12f91SDan Williams 		return false;
26293cd12f91SDan Williams 
26303cd12f91SDan Williams 	if (test_bit(port1, hub->warm_reset_bits))
26313cd12f91SDan Williams 		return true;
26323cd12f91SDan Williams 
26333cd12f91SDan Williams 	link_state = portstatus & USB_PORT_STAT_LINK_STATE;
26343cd12f91SDan Williams 	return link_state == USB_SS_PORT_LS_SS_INACTIVE
26353cd12f91SDan Williams 		|| link_state == USB_SS_PORT_LS_COMP_MOD;
263610d674a8SSarah Sharp }
263710d674a8SSarah Sharp 
26381da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
263975d7cf72SAndiry Xu 			struct usb_device *udev, unsigned int delay, bool warm)
26401da177e4SLinus Torvalds {
26411da177e4SLinus Torvalds 	int delay_time, ret;
26421da177e4SLinus Torvalds 	u16 portstatus;
26431da177e4SLinus Torvalds 	u16 portchange;
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds 	for (delay_time = 0;
26461da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
26471da177e4SLinus Torvalds 			delay_time += delay) {
26481da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
26491da177e4SLinus Torvalds 		msleep(delay);
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 		/* read and decode port status */
26521da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
26531da177e4SLinus Torvalds 		if (ret < 0)
26541da177e4SLinus Torvalds 			return ret;
26551da177e4SLinus Torvalds 
26564f43447eSSarah Sharp 		/* The port state is unknown until the reset completes. */
2657470f0be8SSarah Sharp 		if (!(portstatus & USB_PORT_STAT_RESET))
2658470f0be8SSarah Sharp 			break;
2659470f0be8SSarah Sharp 
2660470f0be8SSarah Sharp 		/* switch to the long delay after two short delay failures */
2661470f0be8SSarah Sharp 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2662470f0be8SSarah Sharp 			delay = HUB_LONG_RESET_TIME;
2663470f0be8SSarah Sharp 
2664d99f6b41SDan Williams 		dev_dbg(&hub->ports[port1 - 1]->dev,
2665d99f6b41SDan Williams 				"not %sreset yet, waiting %dms\n",
2666d99f6b41SDan Williams 				warm ? "warm " : "", delay);
2667470f0be8SSarah Sharp 	}
2668470f0be8SSarah Sharp 
26694f43447eSSarah Sharp 	if ((portstatus & USB_PORT_STAT_RESET))
2670470f0be8SSarah Sharp 		return -EBUSY;
26714f43447eSSarah Sharp 
26723cd12f91SDan Williams 	if (hub_port_warm_reset_required(hub, port1, portstatus))
2673a24a6078SSarah Sharp 		return -ENOTCONN;
267410d674a8SSarah Sharp 
26751da177e4SLinus Torvalds 	/* Device went away? */
26761da177e4SLinus Torvalds 	if (!(portstatus & USB_PORT_STAT_CONNECTION))
26771da177e4SLinus Torvalds 		return -ENOTCONN;
26781da177e4SLinus Torvalds 
2679a24a6078SSarah Sharp 	/* bomb out completely if the connection bounced.  A USB 3.0
2680a24a6078SSarah Sharp 	 * connection may bounce if multiple warm resets were issued,
2681a24a6078SSarah Sharp 	 * but the device may have successfully re-connected. Ignore it.
2682a24a6078SSarah Sharp 	 */
2683a24a6078SSarah Sharp 	if (!hub_is_superspeed(hub->hdev) &&
2684a24a6078SSarah Sharp 			(portchange & USB_PORT_STAT_C_CONNECTION))
2685dd4dd19eSAlan Stern 		return -ENOTCONN;
26861da177e4SLinus Torvalds 
2687470f0be8SSarah Sharp 	if (!(portstatus & USB_PORT_STAT_ENABLE))
2688470f0be8SSarah Sharp 		return -EBUSY;
2689470f0be8SSarah Sharp 
26902d4fa940SSarah Sharp 	if (!udev)
26912d4fa940SSarah Sharp 		return 0;
26922d4fa940SSarah Sharp 
26930165de09SInaky Perez-Gonzalez 	if (hub_is_wusb(hub))
2694551cdbbeSGreg Kroah-Hartman 		udev->speed = USB_SPEED_WIRELESS;
2695131dec34SSarah Sharp 	else if (hub_is_superspeed(hub->hdev))
2696809cd1cbSSarah Sharp 		udev->speed = USB_SPEED_SUPER;
26970165de09SInaky Perez-Gonzalez 	else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
26981da177e4SLinus Torvalds 		udev->speed = USB_SPEED_HIGH;
26991da177e4SLinus Torvalds 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
27001da177e4SLinus Torvalds 		udev->speed = USB_SPEED_LOW;
27011da177e4SLinus Torvalds 	else
27021da177e4SLinus Torvalds 		udev->speed = USB_SPEED_FULL;
27031da177e4SLinus Torvalds 	return 0;
27041da177e4SLinus Torvalds }
27051da177e4SLinus Torvalds 
270675d7cf72SAndiry Xu static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2707a24a6078SSarah Sharp 			struct usb_device *udev, int *status)
27081da177e4SLinus Torvalds {
270975d7cf72SAndiry Xu 	switch (*status) {
27101da177e4SLinus Torvalds 	case 0:
2711b789696aSDavid Brownell 		/* TRSTRCY = 10 ms; plus some extra */
2712b789696aSDavid Brownell 		msleep(10 + 40);
27132d4fa940SSarah Sharp 		if (udev) {
2714a24a6078SSarah Sharp 			struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2715a24a6078SSarah Sharp 
27163b29b68bSAlan Stern 			update_devnum(udev, 0);
2717a24a6078SSarah Sharp 			/* The xHC may think the device is already reset,
2718a24a6078SSarah Sharp 			 * so ignore the status.
27198b8132bcSSarah Sharp 			 */
27208b8132bcSSarah Sharp 			if (hcd->driver->reset_device)
27218b8132bcSSarah Sharp 				hcd->driver->reset_device(hcd, udev);
272275d7cf72SAndiry Xu 		}
27231da177e4SLinus Torvalds 		/* FALL THROUGH */
27241da177e4SLinus Torvalds 	case -ENOTCONN:
27251da177e4SLinus Torvalds 	case -ENODEV:
2726ad493e5eSLan Tianyu 		usb_clear_port_feature(hub->hdev,
27271da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
27281c7439c6SSarah Sharp 		if (hub_is_superspeed(hub->hdev)) {
2729ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
273075d7cf72SAndiry Xu 					USB_PORT_FEAT_C_BH_PORT_RESET);
2731ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
273275d7cf72SAndiry Xu 					USB_PORT_FEAT_C_PORT_LINK_STATE);
2733ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
2734a24a6078SSarah Sharp 					USB_PORT_FEAT_C_CONNECTION);
27351c7439c6SSarah Sharp 		}
2736a24a6078SSarah Sharp 		if (udev)
273775d7cf72SAndiry Xu 			usb_set_device_state(udev, *status
27381da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
27391da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
274075d7cf72SAndiry Xu 		break;
274175d7cf72SAndiry Xu 	}
274275d7cf72SAndiry Xu }
274375d7cf72SAndiry Xu 
274475d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
274575d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1,
274675d7cf72SAndiry Xu 			struct usb_device *udev, unsigned int delay, bool warm)
274775d7cf72SAndiry Xu {
274875d7cf72SAndiry Xu 	int i, status;
2749a24a6078SSarah Sharp 	u16 portchange, portstatus;
2750d99f6b41SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
275175d7cf72SAndiry Xu 
275275d7cf72SAndiry Xu 	if (!hub_is_superspeed(hub->hdev)) {
27530fe51aa5SSarah Sharp 		if (warm) {
275475d7cf72SAndiry Xu 			dev_err(hub->intfdev, "only USB3 hub support "
275575d7cf72SAndiry Xu 						"warm reset\n");
275675d7cf72SAndiry Xu 			return -EINVAL;
275775d7cf72SAndiry Xu 		}
27580fe51aa5SSarah Sharp 		/* Block EHCI CF initialization during the port reset.
27590fe51aa5SSarah Sharp 		 * Some companion controllers don't like it when they mix.
27600fe51aa5SSarah Sharp 		 */
27610fe51aa5SSarah Sharp 		down_read(&ehci_cf_port_reset_rwsem);
2762d3b9d7a9SSarah Sharp 	} else if (!warm) {
2763d3b9d7a9SSarah Sharp 		/*
2764d3b9d7a9SSarah Sharp 		 * If the caller hasn't explicitly requested a warm reset,
2765d3b9d7a9SSarah Sharp 		 * double check and see if one is needed.
2766d3b9d7a9SSarah Sharp 		 */
2767d3b9d7a9SSarah Sharp 		status = hub_port_status(hub, port1,
2768d3b9d7a9SSarah Sharp 					&portstatus, &portchange);
2769d3b9d7a9SSarah Sharp 		if (status < 0)
2770d3b9d7a9SSarah Sharp 			goto done;
2771d3b9d7a9SSarah Sharp 
27723cd12f91SDan Williams 		if (hub_port_warm_reset_required(hub, port1, portstatus))
2773d3b9d7a9SSarah Sharp 			warm = true;
277475d7cf72SAndiry Xu 	}
27753cd12f91SDan Williams 	clear_bit(port1, hub->warm_reset_bits);
277675d7cf72SAndiry Xu 
277775d7cf72SAndiry Xu 	/* Reset the port */
277875d7cf72SAndiry Xu 	for (i = 0; i < PORT_RESET_TRIES; i++) {
277975d7cf72SAndiry Xu 		status = set_port_feature(hub->hdev, port1, (warm ?
278075d7cf72SAndiry Xu 					USB_PORT_FEAT_BH_PORT_RESET :
278175d7cf72SAndiry Xu 					USB_PORT_FEAT_RESET));
2782e9e88fb7SAlan Stern 		if (status == -ENODEV) {
2783e9e88fb7SAlan Stern 			;	/* The hub is gone */
2784e9e88fb7SAlan Stern 		} else if (status) {
2785d99f6b41SDan Williams 			dev_err(&port_dev->dev,
2786d99f6b41SDan Williams 					"cannot %sreset (err = %d)\n",
2787d99f6b41SDan Williams 					warm ? "warm " : "", status);
278875d7cf72SAndiry Xu 		} else {
278975d7cf72SAndiry Xu 			status = hub_port_wait_reset(hub, port1, udev, delay,
279075d7cf72SAndiry Xu 								warm);
2791e9e88fb7SAlan Stern 			if (status && status != -ENOTCONN && status != -ENODEV)
279275d7cf72SAndiry Xu 				dev_dbg(hub->intfdev,
279375d7cf72SAndiry Xu 						"port_wait_reset: err = %d\n",
279475d7cf72SAndiry Xu 						status);
279575d7cf72SAndiry Xu 		}
279675d7cf72SAndiry Xu 
2797a24a6078SSarah Sharp 		/* Check for disconnect or reset */
279875d7cf72SAndiry Xu 		if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2799a24a6078SSarah Sharp 			hub_port_finish_reset(hub, port1, udev, &status);
2800a24a6078SSarah Sharp 
2801a24a6078SSarah Sharp 			if (!hub_is_superspeed(hub->hdev))
280232fe0198SAlan Stern 				goto done;
2803a24a6078SSarah Sharp 
2804a24a6078SSarah Sharp 			/*
2805a24a6078SSarah Sharp 			 * If a USB 3.0 device migrates from reset to an error
2806a24a6078SSarah Sharp 			 * state, re-issue the warm reset.
2807a24a6078SSarah Sharp 			 */
2808a24a6078SSarah Sharp 			if (hub_port_status(hub, port1,
2809a24a6078SSarah Sharp 					&portstatus, &portchange) < 0)
2810a24a6078SSarah Sharp 				goto done;
2811a24a6078SSarah Sharp 
28123cd12f91SDan Williams 			if (!hub_port_warm_reset_required(hub, port1,
28133cd12f91SDan Williams 					portstatus))
2814a24a6078SSarah Sharp 				goto done;
2815a24a6078SSarah Sharp 
2816a24a6078SSarah Sharp 			/*
2817a24a6078SSarah Sharp 			 * If the port is in SS.Inactive or Compliance Mode, the
2818a24a6078SSarah Sharp 			 * hot or warm reset failed.  Try another warm reset.
2819a24a6078SSarah Sharp 			 */
2820a24a6078SSarah Sharp 			if (!warm) {
2821d99f6b41SDan Williams 				dev_dbg(&port_dev->dev,
2822d99f6b41SDan Williams 						"hot reset failed, warm reset\n");
2823a24a6078SSarah Sharp 				warm = true;
2824a24a6078SSarah Sharp 			}
28251da177e4SLinus Torvalds 		}
28261da177e4SLinus Torvalds 
2827d99f6b41SDan Williams 		dev_dbg(&port_dev->dev,
2828d99f6b41SDan Williams 				"not enabled, trying %sreset again...\n",
2829d99f6b41SDan Williams 				warm ? "warm " : "");
28301da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
28311da177e4SLinus Torvalds 	}
28321da177e4SLinus Torvalds 
2833d99f6b41SDan Williams 	dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
28341da177e4SLinus Torvalds 
283532fe0198SAlan Stern done:
28360fe51aa5SSarah Sharp 	if (!hub_is_superspeed(hub->hdev))
283732fe0198SAlan Stern 		up_read(&ehci_cf_port_reset_rwsem);
283875d7cf72SAndiry Xu 
28391da177e4SLinus Torvalds 	return status;
28401da177e4SLinus Torvalds }
28411da177e4SLinus Torvalds 
28420ed9a57eSAndiry Xu /* Check if a port is power on */
28430ed9a57eSAndiry Xu static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
28440ed9a57eSAndiry Xu {
28450ed9a57eSAndiry Xu 	int ret = 0;
28460ed9a57eSAndiry Xu 
28470ed9a57eSAndiry Xu 	if (hub_is_superspeed(hub->hdev)) {
28480ed9a57eSAndiry Xu 		if (portstatus & USB_SS_PORT_STAT_POWER)
28490ed9a57eSAndiry Xu 			ret = 1;
28500ed9a57eSAndiry Xu 	} else {
28510ed9a57eSAndiry Xu 		if (portstatus & USB_PORT_STAT_POWER)
28520ed9a57eSAndiry Xu 			ret = 1;
28530ed9a57eSAndiry Xu 	}
28540ed9a57eSAndiry Xu 
28550ed9a57eSAndiry Xu 	return ret;
28560ed9a57eSAndiry Xu }
28570ed9a57eSAndiry Xu 
28585c79a1e3SDan Williams static void usb_lock_port(struct usb_port *port_dev)
28595c79a1e3SDan Williams 		__acquires(&port_dev->status_lock)
28605c79a1e3SDan Williams {
28615c79a1e3SDan Williams 	mutex_lock(&port_dev->status_lock);
28625c79a1e3SDan Williams 	__acquire(&port_dev->status_lock);
28635c79a1e3SDan Williams }
28645c79a1e3SDan Williams 
28655c79a1e3SDan Williams static void usb_unlock_port(struct usb_port *port_dev)
28665c79a1e3SDan Williams 		__releases(&port_dev->status_lock)
28675c79a1e3SDan Williams {
28685c79a1e3SDan Williams 	mutex_unlock(&port_dev->status_lock);
28695c79a1e3SDan Williams 	__release(&port_dev->status_lock);
28705c79a1e3SDan Williams }
28715c79a1e3SDan Williams 
2872d388dab7SAlan Stern #ifdef	CONFIG_PM
28731da177e4SLinus Torvalds 
28740ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
28750ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
28760ed9a57eSAndiry Xu {
28770ed9a57eSAndiry Xu 	int ret = 0;
28780ed9a57eSAndiry Xu 
28790ed9a57eSAndiry Xu 	if (hub_is_superspeed(hub->hdev)) {
28800ed9a57eSAndiry Xu 		if ((portstatus & USB_PORT_STAT_LINK_STATE)
28810ed9a57eSAndiry Xu 				== USB_SS_PORT_LS_U3)
28820ed9a57eSAndiry Xu 			ret = 1;
28830ed9a57eSAndiry Xu 	} else {
28840ed9a57eSAndiry Xu 		if (portstatus & USB_PORT_STAT_SUSPEND)
28850ed9a57eSAndiry Xu 			ret = 1;
28860ed9a57eSAndiry Xu 	}
28870ed9a57eSAndiry Xu 
28880ed9a57eSAndiry Xu 	return ret;
28890ed9a57eSAndiry Xu }
2890b01b03f3SAlan Stern 
2891b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2892b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2893b01b03f3SAlan Stern  */
2894b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2895b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2896b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2897b01b03f3SAlan Stern {
2898d99f6b41SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
2899d99f6b41SDan Williams 
29003cd12f91SDan Williams 	/* Is a warm reset needed to recover the connection? */
29013cd12f91SDan Williams 	if (status == 0 && udev->reset_resume
29023cd12f91SDan Williams 		&& hub_port_warm_reset_required(hub, port1, portstatus)) {
29033cd12f91SDan Williams 		/* pass */;
29043cd12f91SDan Williams 	}
2905b01b03f3SAlan Stern 	/* Is the device still present? */
29063cd12f91SDan Williams 	else if (status || port_is_suspended(hub, portstatus) ||
29070ed9a57eSAndiry Xu 			!port_is_power_on(hub, portstatus) ||
29080ed9a57eSAndiry Xu 			!(portstatus & USB_PORT_STAT_CONNECTION)) {
2909b01b03f3SAlan Stern 		if (status >= 0)
2910b01b03f3SAlan Stern 			status = -ENODEV;
2911b01b03f3SAlan Stern 	}
2912b01b03f3SAlan Stern 
291386c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
291486c57edfSAlan Stern 	 * so try a reset-resume instead.
291586c57edfSAlan Stern 	 */
291686c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
291786c57edfSAlan Stern 		if (udev->persist_enabled)
291886c57edfSAlan Stern 			udev->reset_resume = 1;
291986c57edfSAlan Stern 		else
2920b01b03f3SAlan Stern 			status = -ENODEV;
292186c57edfSAlan Stern 	}
2922b01b03f3SAlan Stern 
2923b01b03f3SAlan Stern 	if (status) {
2924d99f6b41SDan Williams 		dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2925d99f6b41SDan Williams 				portchange, portstatus, status);
2926b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2927b01b03f3SAlan Stern 
2928b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2929b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2930ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
2931b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2932b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2933ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
2934b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2935b01b03f3SAlan Stern 	}
2936b01b03f3SAlan Stern 
2937b01b03f3SAlan Stern 	return status;
2938b01b03f3SAlan Stern }
2939b01b03f3SAlan Stern 
2940f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
2941f74631e3SSarah Sharp {
2942f74631e3SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2943f74631e3SSarah Sharp 
2944f74631e3SSarah Sharp 	/* Check if the roothub and device supports LTM. */
2945f74631e3SSarah Sharp 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2946f74631e3SSarah Sharp 			!usb_device_supports_ltm(udev))
2947f74631e3SSarah Sharp 		return 0;
2948f74631e3SSarah Sharp 
2949f74631e3SSarah Sharp 	/* Clear Feature LTM Enable can only be sent if the device is
2950f74631e3SSarah Sharp 	 * configured.
2951f74631e3SSarah Sharp 	 */
2952f74631e3SSarah Sharp 	if (!udev->actconfig)
2953f74631e3SSarah Sharp 		return 0;
2954f74631e3SSarah Sharp 
2955f74631e3SSarah Sharp 	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2956f74631e3SSarah Sharp 			USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2957f74631e3SSarah Sharp 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2958f74631e3SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
2959f74631e3SSarah Sharp }
2960f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
2961f74631e3SSarah Sharp 
2962f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev)
2963f74631e3SSarah Sharp {
2964f74631e3SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2965f74631e3SSarah Sharp 
2966f74631e3SSarah Sharp 	/* Check if the roothub and device supports LTM. */
2967f74631e3SSarah Sharp 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2968f74631e3SSarah Sharp 			!usb_device_supports_ltm(udev))
2969f74631e3SSarah Sharp 		return;
2970f74631e3SSarah Sharp 
2971f74631e3SSarah Sharp 	/* Set Feature LTM Enable can only be sent if the device is
2972f74631e3SSarah Sharp 	 * configured.
2973f74631e3SSarah Sharp 	 */
2974f74631e3SSarah Sharp 	if (!udev->actconfig)
2975f74631e3SSarah Sharp 		return;
2976f74631e3SSarah Sharp 
2977f74631e3SSarah Sharp 	usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2978f74631e3SSarah Sharp 			USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2979f74631e3SSarah Sharp 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2980f74631e3SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
2981f74631e3SSarah Sharp }
2982f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
2983f74631e3SSarah Sharp 
298454a3ac0cSLan Tianyu /*
298528e86165SAlan Stern  * usb_enable_remote_wakeup - enable remote wakeup for a device
298654a3ac0cSLan Tianyu  * @udev: target device
298754a3ac0cSLan Tianyu  *
298828e86165SAlan Stern  * For USB-2 devices: Set the device's remote wakeup feature.
298928e86165SAlan Stern  *
299028e86165SAlan Stern  * For USB-3 devices: Assume there's only one function on the device and
299128e86165SAlan Stern  * enable remote wake for the first interface.  FIXME if the interface
299228e86165SAlan Stern  * association descriptor shows there's more than one function.
299354a3ac0cSLan Tianyu  */
299428e86165SAlan Stern static int usb_enable_remote_wakeup(struct usb_device *udev)
299554a3ac0cSLan Tianyu {
299628e86165SAlan Stern 	if (udev->speed < USB_SPEED_SUPER)
299728e86165SAlan Stern 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
299828e86165SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
299928e86165SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
300028e86165SAlan Stern 				USB_CTRL_SET_TIMEOUT);
300128e86165SAlan Stern 	else
300228e86165SAlan Stern 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
300328e86165SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
300428e86165SAlan Stern 				USB_INTRF_FUNC_SUSPEND,
300528e86165SAlan Stern 				USB_INTRF_FUNC_SUSPEND_RW |
300628e86165SAlan Stern 					USB_INTRF_FUNC_SUSPEND_LP,
300728e86165SAlan Stern 				NULL, 0, USB_CTRL_SET_TIMEOUT);
300828e86165SAlan Stern }
300928e86165SAlan Stern 
301028e86165SAlan Stern /*
301128e86165SAlan Stern  * usb_disable_remote_wakeup - disable remote wakeup for a device
301228e86165SAlan Stern  * @udev: target device
301328e86165SAlan Stern  *
301428e86165SAlan Stern  * For USB-2 devices: Clear the device's remote wakeup feature.
301528e86165SAlan Stern  *
301628e86165SAlan Stern  * For USB-3 devices: Assume there's only one function on the device and
301728e86165SAlan Stern  * disable remote wake for the first interface.  FIXME if the interface
301828e86165SAlan Stern  * association descriptor shows there's more than one function.
301928e86165SAlan Stern  */
302028e86165SAlan Stern static int usb_disable_remote_wakeup(struct usb_device *udev)
302128e86165SAlan Stern {
302228e86165SAlan Stern 	if (udev->speed < USB_SPEED_SUPER)
302328e86165SAlan Stern 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
302428e86165SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
302528e86165SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
302628e86165SAlan Stern 				USB_CTRL_SET_TIMEOUT);
302728e86165SAlan Stern 	else
302854a3ac0cSLan Tianyu 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
302954a3ac0cSLan Tianyu 				USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
303054a3ac0cSLan Tianyu 				USB_INTRF_FUNC_SUSPEND,	0, NULL, 0,
303154a3ac0cSLan Tianyu 				USB_CTRL_SET_TIMEOUT);
303254a3ac0cSLan Tianyu }
30331da177e4SLinus Torvalds 
3034e583d9dbSAlan Stern /* Count of wakeup-enabled devices at or below udev */
3035e583d9dbSAlan Stern static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3036e583d9dbSAlan Stern {
3037e583d9dbSAlan Stern 	struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3038e583d9dbSAlan Stern 
3039e583d9dbSAlan Stern 	return udev->do_remote_wakeup +
3040e583d9dbSAlan Stern 			(hub ? hub->wakeup_enabled_descendants : 0);
3041e583d9dbSAlan Stern }
3042e583d9dbSAlan Stern 
30431da177e4SLinus Torvalds /*
3044140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
3045624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
30465edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
30471da177e4SLinus Torvalds  *
30481da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
30491da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
30501da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
3051140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
30521da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
30531da177e4SLinus Torvalds  *
3054390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
3055390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
3056390a8c34SDavid Brownell  *
3057624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
3058624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
3059624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
3060624d6c07SAlan Stern  *
3061624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
3062624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
3063624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
3064624d6c07SAlan Stern  * some cases, this wakes the USB host.
3065624d6c07SAlan Stern  *
30661da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
30671da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
30681da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
30691da177e4SLinus Torvalds  *
30704956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
30714956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
30724956eccdSAlan Stern  * State transitions include:
30734956eccdSAlan Stern  *
30744956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
30754956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
30764956eccdSAlan Stern  *
30774956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
30784956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
30794956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
308037ebb549SPetr Mladek  * Linux (2.6) currently has NO mechanisms to initiate that:  no hub_wq
30814956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
30824956eccdSAlan Stern  *
3083e583d9dbSAlan Stern  * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3084e583d9dbSAlan Stern  * suspended until their bus goes into global suspend (i.e., the root
30850aa2832dSAlan Stern  * hub is suspended).  Nevertheless, we change @udev->state to
30860aa2832dSAlan Stern  * USB_STATE_SUSPENDED as this is the device's "logical" state.  The actual
30870aa2832dSAlan Stern  * upstream port setting is stored in @udev->port_is_suspended.
30884956eccdSAlan Stern  *
30891da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
30901da177e4SLinus Torvalds  */
309165bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
30921da177e4SLinus Torvalds {
3093ad493e5eSLan Tianyu 	struct usb_hub	*hub = usb_hub_to_struct_hub(udev->parent);
3094ad493e5eSLan Tianyu 	struct usb_port *port_dev = hub->ports[udev->portnum - 1];
3095624d6c07SAlan Stern 	int		port1 = udev->portnum;
3096624d6c07SAlan Stern 	int		status;
30970aa2832dSAlan Stern 	bool		really_suspend = true;
30984956eccdSAlan Stern 
30995c79a1e3SDan Williams 	usb_lock_port(port_dev);
31005c79a1e3SDan Williams 
3101624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
3102624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
3103624d6c07SAlan Stern 	 *
3104624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
3105624d6c07SAlan Stern 	 * we don't explicitly enable it here.
3106624d6c07SAlan Stern 	 */
3107624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
310828e86165SAlan Stern 		status = usb_enable_remote_wakeup(udev);
31090c487206SOliver Neukum 		if (status) {
3110624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3111624d6c07SAlan Stern 					status);
31120c487206SOliver Neukum 			/* bail if autosuspend is requested */
31135b1b0b81SAlan Stern 			if (PMSG_IS_AUTO(msg))
31144fae6f0fSAlan Stern 				goto err_wakeup;
31150c487206SOliver Neukum 		}
3116624d6c07SAlan Stern 	}
3117624d6c07SAlan Stern 
311865580b43SAndiry Xu 	/* disable USB2 hardware LPM */
311965580b43SAndiry Xu 	if (udev->usb2_hw_lpm_enabled == 1)
312065580b43SAndiry Xu 		usb_set_usb2_hardware_lpm(udev, 0);
312165580b43SAndiry Xu 
3122f74631e3SSarah Sharp 	if (usb_disable_ltm(udev)) {
31234fae6f0fSAlan Stern 		dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
31244fae6f0fSAlan Stern 		status = -ENOMEM;
31254fae6f0fSAlan Stern 		if (PMSG_IS_AUTO(msg))
31264fae6f0fSAlan Stern 			goto err_ltm;
3127f74631e3SSarah Sharp 	}
31288306095fSSarah Sharp 	if (usb_unlocked_disable_lpm(udev)) {
31294fae6f0fSAlan Stern 		dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
31304fae6f0fSAlan Stern 		status = -ENOMEM;
31314fae6f0fSAlan Stern 		if (PMSG_IS_AUTO(msg))
31324fae6f0fSAlan Stern 			goto err_lpm3;
31338306095fSSarah Sharp 	}
31348306095fSSarah Sharp 
3135624d6c07SAlan Stern 	/* see 7.1.7.6 */
3136a7114230SAndiry Xu 	if (hub_is_superspeed(hub->hdev))
3137c2f60db7SSarah Sharp 		status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
3138e583d9dbSAlan Stern 
31390aa2832dSAlan Stern 	/*
31400aa2832dSAlan Stern 	 * For system suspend, we do not need to enable the suspend feature
31410aa2832dSAlan Stern 	 * on individual USB-2 ports.  The devices will automatically go
31420aa2832dSAlan Stern 	 * into suspend a few ms after the root hub stops sending packets.
31430aa2832dSAlan Stern 	 * The USB 2.0 spec calls this "global suspend".
3144e583d9dbSAlan Stern 	 *
3145e583d9dbSAlan Stern 	 * However, many USB hubs have a bug: They don't relay wakeup requests
3146e583d9dbSAlan Stern 	 * from a downstream port if the port's suspend feature isn't on.
3147e583d9dbSAlan Stern 	 * Therefore we will turn on the suspend feature if udev or any of its
3148e583d9dbSAlan Stern 	 * descendants is enabled for remote wakeup.
31490aa2832dSAlan Stern 	 */
3150e583d9dbSAlan Stern 	else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3151e583d9dbSAlan Stern 		status = set_port_feature(hub->hdev, port1,
3152e583d9dbSAlan Stern 				USB_PORT_FEAT_SUSPEND);
31530aa2832dSAlan Stern 	else {
31540aa2832dSAlan Stern 		really_suspend = false;
31550aa2832dSAlan Stern 		status = 0;
31560aa2832dSAlan Stern 	}
3157624d6c07SAlan Stern 	if (status) {
3158d99f6b41SDan Williams 		dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
3159cbb33004SAlan Stern 
31604fae6f0fSAlan Stern 		/* Try to enable USB3 LPM and LTM again */
31614fae6f0fSAlan Stern 		usb_unlocked_enable_lpm(udev);
31624fae6f0fSAlan Stern  err_lpm3:
31634fae6f0fSAlan Stern 		usb_enable_ltm(udev);
31644fae6f0fSAlan Stern  err_ltm:
3165c3e751e4SAndiry Xu 		/* Try to enable USB2 hardware LPM again */
3166c3e751e4SAndiry Xu 		if (udev->usb2_hw_lpm_capable == 1)
3167c3e751e4SAndiry Xu 			usb_set_usb2_hardware_lpm(udev, 1);
3168c3e751e4SAndiry Xu 
31694fae6f0fSAlan Stern 		if (udev->do_remote_wakeup)
31704fae6f0fSAlan Stern 			(void) usb_disable_remote_wakeup(udev);
31714fae6f0fSAlan Stern  err_wakeup:
31728306095fSSarah Sharp 
3173cbb33004SAlan Stern 		/* System sleep transitions should never fail */
31745b1b0b81SAlan Stern 		if (!PMSG_IS_AUTO(msg))
3175cbb33004SAlan Stern 			status = 0;
3176624d6c07SAlan Stern 	} else {
317730b1a7a3SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
317830b1a7a3SAlan Stern 				(PMSG_IS_AUTO(msg) ? "auto-" : ""),
317930b1a7a3SAlan Stern 				udev->do_remote_wakeup);
31800aa2832dSAlan Stern 		if (really_suspend) {
3181bfd1e910SAlan Stern 			udev->port_is_suspended = 1;
3182e583d9dbSAlan Stern 
3183e583d9dbSAlan Stern 			/* device has up to 10 msec to fully suspend */
3184624d6c07SAlan Stern 			msleep(10);
3185624d6c07SAlan Stern 		}
3186e583d9dbSAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
31870aa2832dSAlan Stern 	}
3188ad493e5eSLan Tianyu 
3189d5c3834eSDan Williams 	if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3190d5c3834eSDan Williams 			&& test_and_clear_bit(port1, hub->child_usage_bits))
3191ad493e5eSLan Tianyu 		pm_runtime_put_sync(&port_dev->dev);
3192ad493e5eSLan Tianyu 
3193c08512c7SAlan Stern 	usb_mark_last_busy(hub->hdev);
31945c79a1e3SDan Williams 
31955c79a1e3SDan Williams 	usb_unlock_port(port_dev);
31964956eccdSAlan Stern 	return status;
31971da177e4SLinus Torvalds }
3198f3f3253dSDavid Brownell 
31991da177e4SLinus Torvalds /*
3200390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
3201390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
320254515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
32031da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
32041da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
32051da177e4SLinus Torvalds  * in the tree that's rooted at this device.
320654515fe5SAlan Stern  *
320754515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
320854515fe5SAlan Stern  * status check is done.
32091da177e4SLinus Torvalds  */
3210140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
32111da177e4SLinus Torvalds {
321254515fe5SAlan Stern 	int	status = 0;
321307e72b95SOliver Neukum 	u16	devstatus = 0;
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds 	/* caller owns the udev device lock */
3216b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
3217b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
32201da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
32211da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
322237ebb549SPetr Mladek 	 * during many timeouts, but hub_wq can't suspend until later.
32231da177e4SLinus Torvalds 	 */
32241da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
32251da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
32261da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
32271da177e4SLinus Torvalds 
322854515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
322954515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
323054515fe5SAlan Stern 	 * operation is carried out here, after the port has been
323154515fe5SAlan Stern 	 * resumed.
323254515fe5SAlan Stern 	 */
32331d10255cSAlan Stern 	if (udev->reset_resume) {
32341d10255cSAlan Stern 		/*
32351d10255cSAlan Stern 		 * If the device morphs or switches modes when it is reset,
32361d10255cSAlan Stern 		 * we don't want to perform a reset-resume.  We'll fail the
32371d10255cSAlan Stern 		 * resume, which will cause a logical disconnect, and then
32381d10255cSAlan Stern 		 * the device will be rediscovered.
32391d10255cSAlan Stern 		 */
324086c57edfSAlan Stern  retry_reset_resume:
32411d10255cSAlan Stern 		if (udev->quirks & USB_QUIRK_RESET)
32421d10255cSAlan Stern 			status = -ENODEV;
32431d10255cSAlan Stern 		else
3244742120c6SMing Lei 			status = usb_reset_and_verify_device(udev);
32451d10255cSAlan Stern 	}
324654515fe5SAlan Stern 
32471da177e4SLinus Torvalds 	/* 10.5.4.5 says be sure devices in the tree are still there.
32481da177e4SLinus Torvalds 	 * For now let's assume the device didn't go crazy on resume,
32491da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
32501da177e4SLinus Torvalds 	 */
325154515fe5SAlan Stern 	if (status == 0) {
325246dede46SAlan Stern 		devstatus = 0;
32531da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
325486c57edfSAlan Stern 
325586c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
325686c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
325786c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
325886c57edfSAlan Stern 			udev->reset_resume = 1;
325986c57edfSAlan Stern 			goto retry_reset_resume;
326086c57edfSAlan Stern 		}
326154515fe5SAlan Stern 	}
3262b40b7a90SAlan Stern 
3263624d6c07SAlan Stern 	if (status) {
3264624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
32651da177e4SLinus Torvalds 				status);
326607e72b95SOliver Neukum 	/*
326707e72b95SOliver Neukum 	 * There are a few quirky devices which violate the standard
326807e72b95SOliver Neukum 	 * by claiming to have remote wakeup enabled after a reset,
326907e72b95SOliver Neukum 	 * which crash if the feature is cleared, hence check for
327007e72b95SOliver Neukum 	 * udev->reset_resume
327107e72b95SOliver Neukum 	 */
327207e72b95SOliver Neukum 	} else if (udev->actconfig && !udev->reset_resume) {
327328e86165SAlan Stern 		if (udev->speed < USB_SPEED_SUPER) {
327454a3ac0cSLan Tianyu 			if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
327528e86165SAlan Stern 				status = usb_disable_remote_wakeup(udev);
327654a3ac0cSLan Tianyu 		} else {
327754a3ac0cSLan Tianyu 			status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
327854a3ac0cSLan Tianyu 					&devstatus);
327954a3ac0cSLan Tianyu 			if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
328054a3ac0cSLan Tianyu 					| USB_INTRF_STAT_FUNC_RW))
328128e86165SAlan Stern 				status = usb_disable_remote_wakeup(udev);
328254a3ac0cSLan Tianyu 		}
328354a3ac0cSLan Tianyu 
3284a8e7c565SAlan Stern 		if (status)
3285b9cef6c3SWu Fengguang 			dev_dbg(&udev->dev,
3286b9cef6c3SWu Fengguang 				"disable remote wakeup, status %d\n",
3287b9cef6c3SWu Fengguang 				status);
32881da177e4SLinus Torvalds 		status = 0;
32891da177e4SLinus Torvalds 	}
32901da177e4SLinus Torvalds 	return status;
32911da177e4SLinus Torvalds }
32921da177e4SLinus Torvalds 
3293624d6c07SAlan Stern /*
3294a40178b2SPratyush Anand  * There are some SS USB devices which take longer time for link training.
3295a40178b2SPratyush Anand  * XHCI specs 4.19.4 says that when Link training is successful, port
3296a40178b2SPratyush Anand  * sets CSC bit to 1. So if SW reads port status before successful link
3297a40178b2SPratyush Anand  * training, then it will not find device to be present.
3298a40178b2SPratyush Anand  * USB Analyzer log with such buggy devices show that in some cases
3299a40178b2SPratyush Anand  * device switch on the RX termination after long delay of host enabling
3300a40178b2SPratyush Anand  * the VBUS. In few other cases it has been seen that device fails to
3301a40178b2SPratyush Anand  * negotiate link training in first attempt. It has been
3302a40178b2SPratyush Anand  * reported till now that few devices take as long as 2000 ms to train
3303a40178b2SPratyush Anand  * the link after host enabling its VBUS and termination. Following
3304a40178b2SPratyush Anand  * routine implements a 2000 ms timeout for link training. If in a case
3305a40178b2SPratyush Anand  * link trains before timeout, loop will exit earlier.
3306a40178b2SPratyush Anand  *
3307a40178b2SPratyush Anand  * FIXME: If a device was connected before suspend, but was removed
3308a40178b2SPratyush Anand  * while system was asleep, then the loop in the following routine will
3309a40178b2SPratyush Anand  * only exit at timeout.
3310a40178b2SPratyush Anand  *
3311a40178b2SPratyush Anand  * This routine should only be called when persist is enabled for a SS
3312a40178b2SPratyush Anand  * device.
3313a40178b2SPratyush Anand  */
3314a40178b2SPratyush Anand static int wait_for_ss_port_enable(struct usb_device *udev,
3315a40178b2SPratyush Anand 		struct usb_hub *hub, int *port1,
3316a40178b2SPratyush Anand 		u16 *portchange, u16 *portstatus)
3317a40178b2SPratyush Anand {
3318a40178b2SPratyush Anand 	int status = 0, delay_ms = 0;
3319a40178b2SPratyush Anand 
3320a40178b2SPratyush Anand 	while (delay_ms < 2000) {
3321a40178b2SPratyush Anand 		if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3322a40178b2SPratyush Anand 			break;
3323a40178b2SPratyush Anand 		msleep(20);
3324a40178b2SPratyush Anand 		delay_ms += 20;
3325a40178b2SPratyush Anand 		status = hub_port_status(hub, *port1, portstatus, portchange);
3326a40178b2SPratyush Anand 	}
3327a40178b2SPratyush Anand 	return status;
3328a40178b2SPratyush Anand }
3329a40178b2SPratyush Anand 
3330a40178b2SPratyush Anand /*
3331624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
3332624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
3333624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
3334624d6c07SAlan Stern  *
3335624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
3336624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
3337624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
3338624d6c07SAlan Stern  * the host and the device is the same as it was when the device
3339624d6c07SAlan Stern  * suspended.
3340624d6c07SAlan Stern  *
3341feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
3342feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
334354515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
334454515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
334554515fe5SAlan Stern  *
334654515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
334754515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
334854515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
334954515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
335054515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
335154515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
335254515fe5SAlan Stern  * made to appear as if it had not disconnected.
335354515fe5SAlan Stern  *
3354742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
3355feccc30dSAlan Stern  * every effort to insure that the same device is present after the
335654515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
335754515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
335854515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
335954515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
336054515fe5SAlan Stern  * new card is corrupted and the system crashes.
336154515fe5SAlan Stern  *
3362624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
3363624d6c07SAlan Stern  */
336465bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
33651da177e4SLinus Torvalds {
3366ad493e5eSLan Tianyu 	struct usb_hub	*hub = usb_hub_to_struct_hub(udev->parent);
3367ad493e5eSLan Tianyu 	struct usb_port *port_dev = hub->ports[udev->portnum  - 1];
3368624d6c07SAlan Stern 	int		port1 = udev->portnum;
33691da177e4SLinus Torvalds 	int		status;
3370d25450c6SAlan Stern 	u16		portchange, portstatus;
3371d25450c6SAlan Stern 
3372d5c3834eSDan Williams 	if (!test_and_set_bit(port1, hub->child_usage_bits)) {
3373ad493e5eSLan Tianyu 		status = pm_runtime_get_sync(&port_dev->dev);
3374ad493e5eSLan Tianyu 		if (status < 0) {
3375ad493e5eSLan Tianyu 			dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3376ad493e5eSLan Tianyu 					status);
3377ad493e5eSLan Tianyu 			return status;
3378ad493e5eSLan Tianyu 		}
3379ad493e5eSLan Tianyu 	}
3380ad493e5eSLan Tianyu 
33815c79a1e3SDan Williams 	usb_lock_port(port_dev);
33825c79a1e3SDan Williams 
3383d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
3384d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
33850ed9a57eSAndiry Xu 	if (status == 0 && !port_is_suspended(hub, portstatus))
3386d25450c6SAlan Stern 		goto SuspendCleared;
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
3389a7114230SAndiry Xu 	if (hub_is_superspeed(hub->hdev))
3390c2f60db7SSarah Sharp 		status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
3391a7114230SAndiry Xu 	else
3392ad493e5eSLan Tianyu 		status = usb_clear_port_feature(hub->hdev,
33931da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_SUSPEND);
33941da177e4SLinus Torvalds 	if (status) {
3395d99f6b41SDan Williams 		dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
33961da177e4SLinus Torvalds 	} else {
33971da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
3398645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
33995b1b0b81SAlan Stern 				(PMSG_IS_AUTO(msg) ? "auto-" : ""));
34001da177e4SLinus Torvalds 		msleep(25);
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
34031da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
34041da177e4SLinus Torvalds 		 * sequence.
34051da177e4SLinus Torvalds 		 */
3406d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
340754515fe5SAlan Stern 
34081da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
34091da177e4SLinus Torvalds 		msleep(10);
34101da177e4SLinus Torvalds 	}
3411b01b03f3SAlan Stern 
3412b01b03f3SAlan Stern  SuspendCleared:
3413b01b03f3SAlan Stern 	if (status == 0) {
3414bfd1e910SAlan Stern 		udev->port_is_suspended = 0;
3415a7114230SAndiry Xu 		if (hub_is_superspeed(hub->hdev)) {
3416a7114230SAndiry Xu 			if (portchange & USB_PORT_STAT_C_LINK_STATE)
3417ad493e5eSLan Tianyu 				usb_clear_port_feature(hub->hdev, port1,
3418a7114230SAndiry Xu 					USB_PORT_FEAT_C_PORT_LINK_STATE);
3419a7114230SAndiry Xu 		} else {
3420b01b03f3SAlan Stern 			if (portchange & USB_PORT_STAT_C_SUSPEND)
3421ad493e5eSLan Tianyu 				usb_clear_port_feature(hub->hdev, port1,
3422b01b03f3SAlan Stern 						USB_PORT_FEAT_C_SUSPEND);
34231da177e4SLinus Torvalds 		}
3424a7114230SAndiry Xu 	}
34251da177e4SLinus Torvalds 
3426a40178b2SPratyush Anand 	if (udev->persist_enabled && hub_is_superspeed(hub->hdev))
3427a40178b2SPratyush Anand 		status = wait_for_ss_port_enable(udev, hub, &port1, &portchange,
3428a40178b2SPratyush Anand 				&portstatus);
3429a40178b2SPratyush Anand 
3430b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
3431b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
343254515fe5SAlan Stern 	if (status == 0)
343354515fe5SAlan Stern 		status = finish_port_resume(udev);
343454515fe5SAlan Stern 	if (status < 0) {
343554515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
343654515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
343765580b43SAndiry Xu 	} else  {
343865580b43SAndiry Xu 		/* Try to enable USB2 hardware LPM */
343965580b43SAndiry Xu 		if (udev->usb2_hw_lpm_capable == 1)
344065580b43SAndiry Xu 			usb_set_usb2_hardware_lpm(udev, 1);
34418306095fSSarah Sharp 
3442f74631e3SSarah Sharp 		/* Try to enable USB3 LTM and LPM */
3443f74631e3SSarah Sharp 		usb_enable_ltm(udev);
34448306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
344554515fe5SAlan Stern 	}
344665580b43SAndiry Xu 
34475c79a1e3SDan Williams 	usb_unlock_port(port_dev);
34485c79a1e3SDan Williams 
34491da177e4SLinus Torvalds 	return status;
34501da177e4SLinus Torvalds }
34511da177e4SLinus Torvalds 
345284ebc102SAlan Stern #ifdef	CONFIG_PM_RUNTIME
345384ebc102SAlan Stern 
34540534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev)
34551da177e4SLinus Torvalds {
34561da177e4SLinus Torvalds 	int	status = 0;
34571da177e4SLinus Torvalds 
34585c79a1e3SDan Williams 	usb_lock_device(udev);
34591da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
3460645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
34619bbdf1e0SAlan Stern 		status = usb_autoresume_device(udev);
34629bbdf1e0SAlan Stern 		if (status == 0) {
34639bbdf1e0SAlan Stern 			/* Let the drivers do their thing, then... */
34649bbdf1e0SAlan Stern 			usb_autosuspend_device(udev);
34659bbdf1e0SAlan Stern 		}
3466d25450c6SAlan Stern 	}
34675c79a1e3SDan Williams 	usb_unlock_device(udev);
34681da177e4SLinus Torvalds 	return status;
34691da177e4SLinus Torvalds }
34701da177e4SLinus Torvalds 
34717e73be22SDan Williams /* Returns 1 if there was a remote wakeup and a connect status change. */
34727e73be22SDan Williams static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
34737e73be22SDan Williams 		u16 portstatus, u16 portchange)
34747e73be22SDan Williams 		__must_hold(&port_dev->status_lock)
34757e73be22SDan Williams {
34767e73be22SDan Williams 	struct usb_port *port_dev = hub->ports[port - 1];
34777e73be22SDan Williams 	struct usb_device *hdev;
34787e73be22SDan Williams 	struct usb_device *udev;
34797e73be22SDan Williams 	int connect_change = 0;
34807e73be22SDan Williams 	int ret;
34817e73be22SDan Williams 
34827e73be22SDan Williams 	hdev = hub->hdev;
34837e73be22SDan Williams 	udev = port_dev->child;
34847e73be22SDan Williams 	if (!hub_is_superspeed(hdev)) {
34857e73be22SDan Williams 		if (!(portchange & USB_PORT_STAT_C_SUSPEND))
34867e73be22SDan Williams 			return 0;
34877e73be22SDan Williams 		usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
34887e73be22SDan Williams 	} else {
34897e73be22SDan Williams 		if (!udev || udev->state != USB_STATE_SUSPENDED ||
34907e73be22SDan Williams 				 (portstatus & USB_PORT_STAT_LINK_STATE) !=
34917e73be22SDan Williams 				 USB_SS_PORT_LS_U0)
34927e73be22SDan Williams 			return 0;
34937e73be22SDan Williams 	}
34947e73be22SDan Williams 
34957e73be22SDan Williams 	if (udev) {
34967e73be22SDan Williams 		/* TRSMRCY = 10 msec */
34977e73be22SDan Williams 		msleep(10);
34987e73be22SDan Williams 
34997e73be22SDan Williams 		usb_unlock_port(port_dev);
35007e73be22SDan Williams 		ret = usb_remote_wakeup(udev);
35017e73be22SDan Williams 		usb_lock_port(port_dev);
35027e73be22SDan Williams 		if (ret < 0)
35037e73be22SDan Williams 			connect_change = 1;
35047e73be22SDan Williams 	} else {
35057e73be22SDan Williams 		ret = -ENODEV;
35067e73be22SDan Williams 		hub_port_disable(hub, port, 1);
35077e73be22SDan Williams 	}
35087e73be22SDan Williams 	dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
35097e73be22SDan Williams 	return connect_change;
35107e73be22SDan Williams }
35117e73be22SDan Williams 
35127e73be22SDan Williams #else
35137e73be22SDan Williams 
35147e73be22SDan Williams static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
35157e73be22SDan Williams 		u16 portstatus, u16 portchange)
35167e73be22SDan Williams {
35177e73be22SDan Williams 	return 0;
35187e73be22SDan Williams }
35197e73be22SDan Williams 
3520d388dab7SAlan Stern #endif
3521d388dab7SAlan Stern 
3522e6f30deaSMing Lei static int check_ports_changed(struct usb_hub *hub)
3523e6f30deaSMing Lei {
3524e6f30deaSMing Lei 	int port1;
3525e6f30deaSMing Lei 
3526e6f30deaSMing Lei 	for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3527e6f30deaSMing Lei 		u16 portstatus, portchange;
3528e6f30deaSMing Lei 		int status;
3529e6f30deaSMing Lei 
3530e6f30deaSMing Lei 		status = hub_port_status(hub, port1, &portstatus, &portchange);
3531e6f30deaSMing Lei 		if (!status && portchange)
3532e6f30deaSMing Lei 			return 1;
3533e6f30deaSMing Lei 	}
3534e6f30deaSMing Lei 	return 0;
3535e6f30deaSMing Lei }
3536e6f30deaSMing Lei 
3537db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
35381da177e4SLinus Torvalds {
35391da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
35401da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
35411da177e4SLinus Torvalds 	unsigned		port1;
35424296c70aSSarah Sharp 	int			status;
35431da177e4SLinus Torvalds 
3544e583d9dbSAlan Stern 	/*
3545e583d9dbSAlan Stern 	 * Warn if children aren't already suspended.
3546e583d9dbSAlan Stern 	 * Also, add up the number of wakeup-enabled descendants.
3547e583d9dbSAlan Stern 	 */
3548e583d9dbSAlan Stern 	hub->wakeup_enabled_descendants = 0;
35491da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3550d99f6b41SDan Williams 		struct usb_port *port_dev = hub->ports[port1 - 1];
3551d99f6b41SDan Williams 		struct usb_device *udev = port_dev->child;
35521da177e4SLinus Torvalds 
35536840d255SAlan Stern 		if (udev && udev->can_submit) {
3554b658b8f5SDan Williams 			dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3555b658b8f5SDan Williams 					dev_name(&udev->dev));
35565b1b0b81SAlan Stern 			if (PMSG_IS_AUTO(msg))
3557c9f89fa4SDavid Brownell 				return -EBUSY;
3558c9f89fa4SDavid Brownell 		}
3559e583d9dbSAlan Stern 		if (udev)
3560e583d9dbSAlan Stern 			hub->wakeup_enabled_descendants +=
3561e583d9dbSAlan Stern 					wakeup_enabled_descendants(udev);
35621da177e4SLinus Torvalds 	}
3563e6f30deaSMing Lei 
3564e6f30deaSMing Lei 	if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3565e6f30deaSMing Lei 		/* check if there are changes pending on hub ports */
3566e6f30deaSMing Lei 		if (check_ports_changed(hub)) {
3567e6f30deaSMing Lei 			if (PMSG_IS_AUTO(msg))
3568e6f30deaSMing Lei 				return -EBUSY;
3569e6f30deaSMing Lei 			pm_wakeup_event(&hdev->dev, 2000);
3570e6f30deaSMing Lei 		}
3571e6f30deaSMing Lei 	}
3572e6f30deaSMing Lei 
35734296c70aSSarah Sharp 	if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
35744296c70aSSarah Sharp 		/* Enable hub to send remote wakeup for all ports. */
35754296c70aSSarah Sharp 		for (port1 = 1; port1 <= hdev->maxchild; port1++) {
35764296c70aSSarah Sharp 			status = set_port_feature(hdev,
35774296c70aSSarah Sharp 					port1 |
35784296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
35794296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
35804296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
35814296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_MASK);
35824296c70aSSarah Sharp 		}
35834296c70aSSarah Sharp 	}
35841da177e4SLinus Torvalds 
3585441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
358640f122f3SAlan Stern 
358737ebb549SPetr Mladek 	/* stop hub_wq and related activity */
35884330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
3589b6f6436dSAlan Stern 	return 0;
35901da177e4SLinus Torvalds }
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
35931da177e4SLinus Torvalds {
35941da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
35951da177e4SLinus Torvalds 
35965e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
3597f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
35981da177e4SLinus Torvalds 	return 0;
35991da177e4SLinus Torvalds }
36001da177e4SLinus Torvalds 
3601b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
3602f07600cfSAlan Stern {
3603b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
3604f07600cfSAlan Stern 
36055e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
3606f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
3607f07600cfSAlan Stern 	return 0;
3608f07600cfSAlan Stern }
3609f07600cfSAlan Stern 
361054515fe5SAlan Stern /**
361154515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
361254515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
361354515fe5SAlan Stern  *
361454515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
361554515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
3616feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
3617feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
3618feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3619feccc30dSAlan Stern  * the others will be disconnected.
362054515fe5SAlan Stern  */
362154515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
362254515fe5SAlan Stern {
362354515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
362454515fe5SAlan Stern 	rhdev->reset_resume = 1;
362554515fe5SAlan Stern }
362654515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
362754515fe5SAlan Stern 
36281ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[]  = {
36291ea7e0e8SSarah Sharp 	"U0",
36301ea7e0e8SSarah Sharp 	"U1",
36311ea7e0e8SSarah Sharp 	"U2",
36321ea7e0e8SSarah Sharp 	"U3",
36331ea7e0e8SSarah Sharp };
36341ea7e0e8SSarah Sharp 
36351ea7e0e8SSarah Sharp /*
36361ea7e0e8SSarah Sharp  * Send a Set SEL control transfer to the device, prior to enabling
36371ea7e0e8SSarah Sharp  * device-initiated U1 or U2.  This lets the device know the exit latencies from
36381ea7e0e8SSarah Sharp  * the time the device initiates a U1 or U2 exit, to the time it will receive a
36391ea7e0e8SSarah Sharp  * packet from the host.
36401ea7e0e8SSarah Sharp  *
36411ea7e0e8SSarah Sharp  * This function will fail if the SEL or PEL values for udev are greater than
36421ea7e0e8SSarah Sharp  * the maximum allowed values for the link state to be enabled.
36431ea7e0e8SSarah Sharp  */
36441ea7e0e8SSarah Sharp static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
36451ea7e0e8SSarah Sharp {
36461ea7e0e8SSarah Sharp 	struct usb_set_sel_req *sel_values;
36471ea7e0e8SSarah Sharp 	unsigned long long u1_sel;
36481ea7e0e8SSarah Sharp 	unsigned long long u1_pel;
36491ea7e0e8SSarah Sharp 	unsigned long long u2_sel;
36501ea7e0e8SSarah Sharp 	unsigned long long u2_pel;
36511ea7e0e8SSarah Sharp 	int ret;
36521ea7e0e8SSarah Sharp 
365338d7f688SXenia Ragiadakou 	if (udev->state != USB_STATE_CONFIGURED)
365438d7f688SXenia Ragiadakou 		return 0;
365538d7f688SXenia Ragiadakou 
36561ea7e0e8SSarah Sharp 	/* Convert SEL and PEL stored in ns to us */
36571ea7e0e8SSarah Sharp 	u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
36581ea7e0e8SSarah Sharp 	u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
36591ea7e0e8SSarah Sharp 	u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
36601ea7e0e8SSarah Sharp 	u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
36611ea7e0e8SSarah Sharp 
36621ea7e0e8SSarah Sharp 	/*
36631ea7e0e8SSarah Sharp 	 * Make sure that the calculated SEL and PEL values for the link
36641ea7e0e8SSarah Sharp 	 * state we're enabling aren't bigger than the max SEL/PEL
36651ea7e0e8SSarah Sharp 	 * value that will fit in the SET SEL control transfer.
36661ea7e0e8SSarah Sharp 	 * Otherwise the device would get an incorrect idea of the exit
36671ea7e0e8SSarah Sharp 	 * latency for the link state, and could start a device-initiated
36681ea7e0e8SSarah Sharp 	 * U1/U2 when the exit latencies are too high.
36691ea7e0e8SSarah Sharp 	 */
36701ea7e0e8SSarah Sharp 	if ((state == USB3_LPM_U1 &&
36711ea7e0e8SSarah Sharp 				(u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
36721ea7e0e8SSarah Sharp 				 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
36731ea7e0e8SSarah Sharp 			(state == USB3_LPM_U2 &&
36741ea7e0e8SSarah Sharp 			 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
36751ea7e0e8SSarah Sharp 			  u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
36761510a1a2SSarah Sharp 		dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
36771ea7e0e8SSarah Sharp 				usb3_lpm_names[state], u1_sel, u1_pel);
36781ea7e0e8SSarah Sharp 		return -EINVAL;
36791ea7e0e8SSarah Sharp 	}
36801ea7e0e8SSarah Sharp 
36811ea7e0e8SSarah Sharp 	/*
36821ea7e0e8SSarah Sharp 	 * If we're enabling device-initiated LPM for one link state,
36831ea7e0e8SSarah Sharp 	 * but the other link state has a too high SEL or PEL value,
36841ea7e0e8SSarah Sharp 	 * just set those values to the max in the Set SEL request.
36851ea7e0e8SSarah Sharp 	 */
36861ea7e0e8SSarah Sharp 	if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
36871ea7e0e8SSarah Sharp 		u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
36881ea7e0e8SSarah Sharp 
36891ea7e0e8SSarah Sharp 	if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
36901ea7e0e8SSarah Sharp 		u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
36911ea7e0e8SSarah Sharp 
36921ea7e0e8SSarah Sharp 	if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
36931ea7e0e8SSarah Sharp 		u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
36941ea7e0e8SSarah Sharp 
36951ea7e0e8SSarah Sharp 	if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
36961ea7e0e8SSarah Sharp 		u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
36971ea7e0e8SSarah Sharp 
36981ea7e0e8SSarah Sharp 	/*
36991ea7e0e8SSarah Sharp 	 * usb_enable_lpm() can be called as part of a failed device reset,
37001ea7e0e8SSarah Sharp 	 * which may be initiated by an error path of a mass storage driver.
37011ea7e0e8SSarah Sharp 	 * Therefore, use GFP_NOIO.
37021ea7e0e8SSarah Sharp 	 */
37031ea7e0e8SSarah Sharp 	sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
37041ea7e0e8SSarah Sharp 	if (!sel_values)
37051ea7e0e8SSarah Sharp 		return -ENOMEM;
37061ea7e0e8SSarah Sharp 
37071ea7e0e8SSarah Sharp 	sel_values->u1_sel = u1_sel;
37081ea7e0e8SSarah Sharp 	sel_values->u1_pel = u1_pel;
37091ea7e0e8SSarah Sharp 	sel_values->u2_sel = cpu_to_le16(u2_sel);
37101ea7e0e8SSarah Sharp 	sel_values->u2_pel = cpu_to_le16(u2_pel);
37111ea7e0e8SSarah Sharp 
37121ea7e0e8SSarah Sharp 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
37131ea7e0e8SSarah Sharp 			USB_REQ_SET_SEL,
37141ea7e0e8SSarah Sharp 			USB_RECIP_DEVICE,
37151ea7e0e8SSarah Sharp 			0, 0,
37161ea7e0e8SSarah Sharp 			sel_values, sizeof *(sel_values),
37171ea7e0e8SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
37181ea7e0e8SSarah Sharp 	kfree(sel_values);
37191ea7e0e8SSarah Sharp 	return ret;
37201ea7e0e8SSarah Sharp }
37211ea7e0e8SSarah Sharp 
37221ea7e0e8SSarah Sharp /*
37231ea7e0e8SSarah Sharp  * Enable or disable device-initiated U1 or U2 transitions.
37241ea7e0e8SSarah Sharp  */
37251ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev,
37261ea7e0e8SSarah Sharp 		enum usb3_link_state state, bool enable)
37271ea7e0e8SSarah Sharp {
37281ea7e0e8SSarah Sharp 	int ret;
37291ea7e0e8SSarah Sharp 	int feature;
37301ea7e0e8SSarah Sharp 
37311ea7e0e8SSarah Sharp 	switch (state) {
37321ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
37331ea7e0e8SSarah Sharp 		feature = USB_DEVICE_U1_ENABLE;
37341ea7e0e8SSarah Sharp 		break;
37351ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
37361ea7e0e8SSarah Sharp 		feature = USB_DEVICE_U2_ENABLE;
37371ea7e0e8SSarah Sharp 		break;
37381ea7e0e8SSarah Sharp 	default:
37391ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
37401ea7e0e8SSarah Sharp 				__func__, enable ? "enable" : "disable");
37411ea7e0e8SSarah Sharp 		return -EINVAL;
37421ea7e0e8SSarah Sharp 	}
37431ea7e0e8SSarah Sharp 
37441ea7e0e8SSarah Sharp 	if (udev->state != USB_STATE_CONFIGURED) {
37451ea7e0e8SSarah Sharp 		dev_dbg(&udev->dev, "%s: Can't %s %s state "
37461ea7e0e8SSarah Sharp 				"for unconfigured device.\n",
37471ea7e0e8SSarah Sharp 				__func__, enable ? "enable" : "disable",
37481ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
37491ea7e0e8SSarah Sharp 		return 0;
37501ea7e0e8SSarah Sharp 	}
37511ea7e0e8SSarah Sharp 
37521ea7e0e8SSarah Sharp 	if (enable) {
37531ea7e0e8SSarah Sharp 		/*
37541ea7e0e8SSarah Sharp 		 * Now send the control transfer to enable device-initiated LPM
37551ea7e0e8SSarah Sharp 		 * for either U1 or U2.
37561ea7e0e8SSarah Sharp 		 */
37571ea7e0e8SSarah Sharp 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
37581ea7e0e8SSarah Sharp 				USB_REQ_SET_FEATURE,
37591ea7e0e8SSarah Sharp 				USB_RECIP_DEVICE,
37601ea7e0e8SSarah Sharp 				feature,
37611ea7e0e8SSarah Sharp 				0, NULL, 0,
37621ea7e0e8SSarah Sharp 				USB_CTRL_SET_TIMEOUT);
37631ea7e0e8SSarah Sharp 	} else {
37641ea7e0e8SSarah Sharp 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
37651ea7e0e8SSarah Sharp 				USB_REQ_CLEAR_FEATURE,
37661ea7e0e8SSarah Sharp 				USB_RECIP_DEVICE,
37671ea7e0e8SSarah Sharp 				feature,
37681ea7e0e8SSarah Sharp 				0, NULL, 0,
37691ea7e0e8SSarah Sharp 				USB_CTRL_SET_TIMEOUT);
37701ea7e0e8SSarah Sharp 	}
37711ea7e0e8SSarah Sharp 	if (ret < 0) {
37721ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
37731ea7e0e8SSarah Sharp 				enable ? "Enable" : "Disable",
37741ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
37751ea7e0e8SSarah Sharp 		return -EBUSY;
37761ea7e0e8SSarah Sharp 	}
37771ea7e0e8SSarah Sharp 	return 0;
37781ea7e0e8SSarah Sharp }
37791ea7e0e8SSarah Sharp 
37801ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev,
37811ea7e0e8SSarah Sharp 		enum usb3_link_state state, int timeout)
37821ea7e0e8SSarah Sharp {
37831ea7e0e8SSarah Sharp 	int ret;
37841ea7e0e8SSarah Sharp 	int feature;
37851ea7e0e8SSarah Sharp 
37861ea7e0e8SSarah Sharp 	switch (state) {
37871ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
37881ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U1_TIMEOUT;
37891ea7e0e8SSarah Sharp 		break;
37901ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
37911ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U2_TIMEOUT;
37921ea7e0e8SSarah Sharp 		break;
37931ea7e0e8SSarah Sharp 	default:
37941ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
37951ea7e0e8SSarah Sharp 				__func__);
37961ea7e0e8SSarah Sharp 		return -EINVAL;
37971ea7e0e8SSarah Sharp 	}
37981ea7e0e8SSarah Sharp 
37991ea7e0e8SSarah Sharp 	if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
38001ea7e0e8SSarah Sharp 			timeout != USB3_LPM_DEVICE_INITIATED) {
38011ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
38021ea7e0e8SSarah Sharp 				"which is a reserved value.\n",
38031ea7e0e8SSarah Sharp 				usb3_lpm_names[state], timeout);
38041ea7e0e8SSarah Sharp 		return -EINVAL;
38051ea7e0e8SSarah Sharp 	}
38061ea7e0e8SSarah Sharp 
38071ea7e0e8SSarah Sharp 	ret = set_port_feature(udev->parent,
38081ea7e0e8SSarah Sharp 			USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
38091ea7e0e8SSarah Sharp 			feature);
38101ea7e0e8SSarah Sharp 	if (ret < 0) {
38111ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
38121ea7e0e8SSarah Sharp 				"error code %i\n", usb3_lpm_names[state],
38131ea7e0e8SSarah Sharp 				timeout, ret);
38141ea7e0e8SSarah Sharp 		return -EBUSY;
38151ea7e0e8SSarah Sharp 	}
38161ea7e0e8SSarah Sharp 	if (state == USB3_LPM_U1)
38171ea7e0e8SSarah Sharp 		udev->u1_params.timeout = timeout;
38181ea7e0e8SSarah Sharp 	else
38191ea7e0e8SSarah Sharp 		udev->u2_params.timeout = timeout;
38201ea7e0e8SSarah Sharp 	return 0;
38211ea7e0e8SSarah Sharp }
38221ea7e0e8SSarah Sharp 
38231ea7e0e8SSarah Sharp /*
38241ea7e0e8SSarah Sharp  * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
38251ea7e0e8SSarah Sharp  * U1/U2 entry.
38261ea7e0e8SSarah Sharp  *
38271ea7e0e8SSarah Sharp  * We will attempt to enable U1 or U2, but there are no guarantees that the
38281ea7e0e8SSarah Sharp  * control transfers to set the hub timeout or enable device-initiated U1/U2
38291ea7e0e8SSarah Sharp  * will be successful.
38301ea7e0e8SSarah Sharp  *
38311ea7e0e8SSarah Sharp  * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
38321ea7e0e8SSarah Sharp  * driver know about it.  If that call fails, it should be harmless, and just
38331ea7e0e8SSarah Sharp  * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
38341ea7e0e8SSarah Sharp  */
38351ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
38361ea7e0e8SSarah Sharp 		enum usb3_link_state state)
38371ea7e0e8SSarah Sharp {
383865a95b75SSarah Sharp 	int timeout, ret;
3839ae8963adSSarah Sharp 	__u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3840ae8963adSSarah Sharp 	__le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3841ae8963adSSarah Sharp 
3842ae8963adSSarah Sharp 	/* If the device says it doesn't have *any* exit latency to come out of
3843ae8963adSSarah Sharp 	 * U1 or U2, it's probably lying.  Assume it doesn't implement that link
3844ae8963adSSarah Sharp 	 * state.
3845ae8963adSSarah Sharp 	 */
3846ae8963adSSarah Sharp 	if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3847ae8963adSSarah Sharp 			(state == USB3_LPM_U2 && u2_mel == 0))
3848ae8963adSSarah Sharp 		return;
38491ea7e0e8SSarah Sharp 
385065a95b75SSarah Sharp 	/*
385165a95b75SSarah Sharp 	 * First, let the device know about the exit latencies
385265a95b75SSarah Sharp 	 * associated with the link state we're about to enable.
385365a95b75SSarah Sharp 	 */
385465a95b75SSarah Sharp 	ret = usb_req_set_sel(udev, state);
385565a95b75SSarah Sharp 	if (ret < 0) {
385665a95b75SSarah Sharp 		dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
385765a95b75SSarah Sharp 				usb3_lpm_names[state]);
385865a95b75SSarah Sharp 		return;
385965a95b75SSarah Sharp 	}
386065a95b75SSarah Sharp 
38611ea7e0e8SSarah Sharp 	/* We allow the host controller to set the U1/U2 timeout internally
38621ea7e0e8SSarah Sharp 	 * first, so that it can change its schedule to account for the
38631ea7e0e8SSarah Sharp 	 * additional latency to send data to a device in a lower power
38641ea7e0e8SSarah Sharp 	 * link state.
38651ea7e0e8SSarah Sharp 	 */
38661ea7e0e8SSarah Sharp 	timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
38671ea7e0e8SSarah Sharp 
38681ea7e0e8SSarah Sharp 	/* xHCI host controller doesn't want to enable this LPM state. */
38691ea7e0e8SSarah Sharp 	if (timeout == 0)
38701ea7e0e8SSarah Sharp 		return;
38711ea7e0e8SSarah Sharp 
38721ea7e0e8SSarah Sharp 	if (timeout < 0) {
38731ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Could not enable %s link state, "
38741ea7e0e8SSarah Sharp 				"xHCI error %i.\n", usb3_lpm_names[state],
38751ea7e0e8SSarah Sharp 				timeout);
38761ea7e0e8SSarah Sharp 		return;
38771ea7e0e8SSarah Sharp 	}
38781ea7e0e8SSarah Sharp 
38791ea7e0e8SSarah Sharp 	if (usb_set_lpm_timeout(udev, state, timeout))
38801ea7e0e8SSarah Sharp 		/* If we can't set the parent hub U1/U2 timeout,
38811ea7e0e8SSarah Sharp 		 * device-initiated LPM won't be allowed either, so let the xHCI
38821ea7e0e8SSarah Sharp 		 * host know that this link state won't be enabled.
38831ea7e0e8SSarah Sharp 		 */
38841ea7e0e8SSarah Sharp 		hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
38851ea7e0e8SSarah Sharp 
38861ea7e0e8SSarah Sharp 	/* Only a configured device will accept the Set Feature U1/U2_ENABLE */
38871ea7e0e8SSarah Sharp 	else if (udev->actconfig)
38881ea7e0e8SSarah Sharp 		usb_set_device_initiated_lpm(udev, state, true);
38891ea7e0e8SSarah Sharp 
38901ea7e0e8SSarah Sharp }
38911ea7e0e8SSarah Sharp 
38921ea7e0e8SSarah Sharp /*
38931ea7e0e8SSarah Sharp  * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
38941ea7e0e8SSarah Sharp  * U1/U2 entry.
38951ea7e0e8SSarah Sharp  *
38961ea7e0e8SSarah Sharp  * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
38971ea7e0e8SSarah Sharp  * If zero is returned, the parent will not allow the link to go into U1/U2.
38981ea7e0e8SSarah Sharp  *
38991ea7e0e8SSarah Sharp  * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
39001ea7e0e8SSarah Sharp  * it won't have an effect on the bus link state because the parent hub will
39011ea7e0e8SSarah Sharp  * still disallow device-initiated U1/U2 entry.
39021ea7e0e8SSarah Sharp  *
39031ea7e0e8SSarah Sharp  * If zero is returned, the xHCI host controller may still think U1/U2 entry is
39041ea7e0e8SSarah Sharp  * possible.  The result will be slightly more bus bandwidth will be taken up
39051ea7e0e8SSarah Sharp  * (to account for U1/U2 exit latency), but it should be harmless.
39061ea7e0e8SSarah Sharp  */
39071ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
39081ea7e0e8SSarah Sharp 		enum usb3_link_state state)
39091ea7e0e8SSarah Sharp {
39101ea7e0e8SSarah Sharp 	int feature;
39111ea7e0e8SSarah Sharp 
39121ea7e0e8SSarah Sharp 	switch (state) {
39131ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
39141ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U1_TIMEOUT;
39151ea7e0e8SSarah Sharp 		break;
39161ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
39171ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U2_TIMEOUT;
39181ea7e0e8SSarah Sharp 		break;
39191ea7e0e8SSarah Sharp 	default:
39201ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
39211ea7e0e8SSarah Sharp 				__func__);
39221ea7e0e8SSarah Sharp 		return -EINVAL;
39231ea7e0e8SSarah Sharp 	}
39241ea7e0e8SSarah Sharp 
39251ea7e0e8SSarah Sharp 	if (usb_set_lpm_timeout(udev, state, 0))
39261ea7e0e8SSarah Sharp 		return -EBUSY;
39271ea7e0e8SSarah Sharp 
39281ea7e0e8SSarah Sharp 	usb_set_device_initiated_lpm(udev, state, false);
39291ea7e0e8SSarah Sharp 
39301ea7e0e8SSarah Sharp 	if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
39311ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
39321ea7e0e8SSarah Sharp 				"bus schedule bandwidth may be impacted.\n",
39331ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
39341ea7e0e8SSarah Sharp 	return 0;
39351ea7e0e8SSarah Sharp }
39361ea7e0e8SSarah Sharp 
39371ea7e0e8SSarah Sharp /*
39381ea7e0e8SSarah Sharp  * Disable hub-initiated and device-initiated U1 and U2 entry.
39391ea7e0e8SSarah Sharp  * Caller must own the bandwidth_mutex.
39401ea7e0e8SSarah Sharp  *
39411ea7e0e8SSarah Sharp  * This will call usb_enable_lpm() on failure, which will decrement
39421ea7e0e8SSarah Sharp  * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
39431ea7e0e8SSarah Sharp  */
39441ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
39451ea7e0e8SSarah Sharp {
39461ea7e0e8SSarah Sharp 	struct usb_hcd *hcd;
39471ea7e0e8SSarah Sharp 
39481ea7e0e8SSarah Sharp 	if (!udev || !udev->parent ||
39491ea7e0e8SSarah Sharp 			udev->speed != USB_SPEED_SUPER ||
395051df62ffSPratyush Anand 			!udev->lpm_capable ||
395151df62ffSPratyush Anand 			udev->state < USB_STATE_DEFAULT)
39521ea7e0e8SSarah Sharp 		return 0;
39531ea7e0e8SSarah Sharp 
39541ea7e0e8SSarah Sharp 	hcd = bus_to_hcd(udev->bus);
39551ea7e0e8SSarah Sharp 	if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
39561ea7e0e8SSarah Sharp 		return 0;
39571ea7e0e8SSarah Sharp 
39581ea7e0e8SSarah Sharp 	udev->lpm_disable_count++;
395955558c33SDan Carpenter 	if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
39601ea7e0e8SSarah Sharp 		return 0;
39611ea7e0e8SSarah Sharp 
39621ea7e0e8SSarah Sharp 	/* If LPM is enabled, attempt to disable it. */
39631ea7e0e8SSarah Sharp 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
39641ea7e0e8SSarah Sharp 		goto enable_lpm;
39651ea7e0e8SSarah Sharp 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
39661ea7e0e8SSarah Sharp 		goto enable_lpm;
39671ea7e0e8SSarah Sharp 
39681ea7e0e8SSarah Sharp 	return 0;
39691ea7e0e8SSarah Sharp 
39701ea7e0e8SSarah Sharp enable_lpm:
39711ea7e0e8SSarah Sharp 	usb_enable_lpm(udev);
39721ea7e0e8SSarah Sharp 	return -EBUSY;
39731ea7e0e8SSarah Sharp }
39741ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
39751ea7e0e8SSarah Sharp 
39761ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
39771ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
39781ea7e0e8SSarah Sharp {
39791ea7e0e8SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
39801ea7e0e8SSarah Sharp 	int ret;
39811ea7e0e8SSarah Sharp 
39821ea7e0e8SSarah Sharp 	if (!hcd)
39831ea7e0e8SSarah Sharp 		return -EINVAL;
39841ea7e0e8SSarah Sharp 
39851ea7e0e8SSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
39861ea7e0e8SSarah Sharp 	ret = usb_disable_lpm(udev);
39871ea7e0e8SSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
39881ea7e0e8SSarah Sharp 
39891ea7e0e8SSarah Sharp 	return ret;
39901ea7e0e8SSarah Sharp }
39911ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
39921ea7e0e8SSarah Sharp 
39931ea7e0e8SSarah Sharp /*
39941ea7e0e8SSarah Sharp  * Attempt to enable device-initiated and hub-initiated U1 and U2 entry.  The
39951ea7e0e8SSarah Sharp  * xHCI host policy may prevent U1 or U2 from being enabled.
39961ea7e0e8SSarah Sharp  *
39971ea7e0e8SSarah Sharp  * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
39981ea7e0e8SSarah Sharp  * until the lpm_disable_count drops to zero.  Caller must own the
39991ea7e0e8SSarah Sharp  * bandwidth_mutex.
40001ea7e0e8SSarah Sharp  */
40011ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev)
40021ea7e0e8SSarah Sharp {
40031ea7e0e8SSarah Sharp 	struct usb_hcd *hcd;
40041ea7e0e8SSarah Sharp 
40051ea7e0e8SSarah Sharp 	if (!udev || !udev->parent ||
40061ea7e0e8SSarah Sharp 			udev->speed != USB_SPEED_SUPER ||
400751df62ffSPratyush Anand 			!udev->lpm_capable ||
400851df62ffSPratyush Anand 			udev->state < USB_STATE_DEFAULT)
40091ea7e0e8SSarah Sharp 		return;
40101ea7e0e8SSarah Sharp 
40111ea7e0e8SSarah Sharp 	udev->lpm_disable_count--;
40121ea7e0e8SSarah Sharp 	hcd = bus_to_hcd(udev->bus);
40131ea7e0e8SSarah Sharp 	/* Double check that we can both enable and disable LPM.
40141ea7e0e8SSarah Sharp 	 * Device must be configured to accept set feature U1/U2 timeout.
40151ea7e0e8SSarah Sharp 	 */
40161ea7e0e8SSarah Sharp 	if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
40171ea7e0e8SSarah Sharp 			!hcd->driver->disable_usb3_lpm_timeout)
40181ea7e0e8SSarah Sharp 		return;
40191ea7e0e8SSarah Sharp 
40201ea7e0e8SSarah Sharp 	if (udev->lpm_disable_count > 0)
40211ea7e0e8SSarah Sharp 		return;
40221ea7e0e8SSarah Sharp 
40231ea7e0e8SSarah Sharp 	usb_enable_link_state(hcd, udev, USB3_LPM_U1);
40241ea7e0e8SSarah Sharp 	usb_enable_link_state(hcd, udev, USB3_LPM_U2);
40251ea7e0e8SSarah Sharp }
40261ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
40271ea7e0e8SSarah Sharp 
40281ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
40291ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev)
40301ea7e0e8SSarah Sharp {
40311ea7e0e8SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
40321ea7e0e8SSarah Sharp 
40331ea7e0e8SSarah Sharp 	if (!hcd)
40341ea7e0e8SSarah Sharp 		return;
40351ea7e0e8SSarah Sharp 
40361ea7e0e8SSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
40371ea7e0e8SSarah Sharp 	usb_enable_lpm(udev);
40381ea7e0e8SSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
40391ea7e0e8SSarah Sharp }
40401ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
40411ea7e0e8SSarah Sharp 
40421ea7e0e8SSarah Sharp 
4043d388dab7SAlan Stern #else	/* CONFIG_PM */
4044d388dab7SAlan Stern 
4045511366daSAndrew Morton #define hub_suspend		NULL
4046511366daSAndrew Morton #define hub_resume		NULL
4047f07600cfSAlan Stern #define hub_reset_resume	NULL
40481ea7e0e8SSarah Sharp 
40491ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
40501ea7e0e8SSarah Sharp {
40511ea7e0e8SSarah Sharp 	return 0;
40521ea7e0e8SSarah Sharp }
4053e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
40541ea7e0e8SSarah Sharp 
40551ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { }
4056e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
40571ea7e0e8SSarah Sharp 
40581ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
40591ea7e0e8SSarah Sharp {
40601ea7e0e8SSarah Sharp 	return 0;
40611ea7e0e8SSarah Sharp }
4062e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
40631ea7e0e8SSarah Sharp 
40641ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { }
4065e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4066f74631e3SSarah Sharp 
4067f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
4068f74631e3SSarah Sharp {
4069f74631e3SSarah Sharp 	return 0;
4070f74631e3SSarah Sharp }
4071f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
4072f74631e3SSarah Sharp 
4073f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) { }
4074f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
4075c4b51a43SAlan Stern 
40764a95b1fcSStephen Rothwell static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
40774a95b1fcSStephen Rothwell 		u16 portstatus, u16 portchange)
40784a95b1fcSStephen Rothwell {
40794a95b1fcSStephen Rothwell 	return 0;
40804a95b1fcSStephen Rothwell }
40814a95b1fcSStephen Rothwell 
4082c4b51a43SAlan Stern #endif	/* CONFIG_PM */
4083d388dab7SAlan Stern 
40841da177e4SLinus Torvalds 
40851da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
40861da177e4SLinus Torvalds  *
40871da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
40881da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
40891da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
40901da177e4SLinus Torvalds  *
40911da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
40921da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
40931da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
40941da177e4SLinus Torvalds  *
40951da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
40961da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
40971da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
40981da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
40991da177e4SLinus Torvalds  */
4100ad493e5eSLan Tianyu int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
41011da177e4SLinus Torvalds {
41021da177e4SLinus Torvalds 	int ret;
41031da177e4SLinus Torvalds 	u16 portchange, portstatus;
41041da177e4SLinus Torvalds 	unsigned connection = 0xffff;
4105d99f6b41SDan Williams 	int total_time, stable_time = 0;
4106d99f6b41SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
41071da177e4SLinus Torvalds 
41081da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
41091da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
41101da177e4SLinus Torvalds 		if (ret < 0)
41111da177e4SLinus Torvalds 			return ret;
41121da177e4SLinus Torvalds 
41131da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
41141da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
4115ad493e5eSLan Tianyu 			if (!must_be_connected ||
4116ad493e5eSLan Tianyu 			     (connection == USB_PORT_STAT_CONNECTION))
41171da177e4SLinus Torvalds 				stable_time += HUB_DEBOUNCE_STEP;
41181da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
41191da177e4SLinus Torvalds 				break;
41201da177e4SLinus Torvalds 		} else {
41211da177e4SLinus Torvalds 			stable_time = 0;
41221da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
41231da177e4SLinus Torvalds 		}
41241da177e4SLinus Torvalds 
41251da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
4126ad493e5eSLan Tianyu 			usb_clear_port_feature(hub->hdev, port1,
41271da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
41281da177e4SLinus Torvalds 		}
41291da177e4SLinus Torvalds 
41301da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
41311da177e4SLinus Torvalds 			break;
41321da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
41331da177e4SLinus Torvalds 	}
41341da177e4SLinus Torvalds 
4135d99f6b41SDan Williams 	dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4136d99f6b41SDan Williams 			total_time, stable_time, portstatus);
41371da177e4SLinus Torvalds 
41381da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
41391da177e4SLinus Torvalds 		return -ETIMEDOUT;
41401da177e4SLinus Torvalds 	return portstatus;
41411da177e4SLinus Torvalds }
41421da177e4SLinus Torvalds 
4143fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
41441da177e4SLinus Torvalds {
4145ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4146ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
41472caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
41481da177e4SLinus Torvalds }
4149fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
41501da177e4SLinus Torvalds 
41511da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
41521da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
41531da177e4SLinus Torvalds 
41544326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
41551da177e4SLinus Torvalds {
41561da177e4SLinus Torvalds 	int retval;
4157c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
41581da177e4SLinus Torvalds 
4159c6515272SSarah Sharp 	/*
4160c6515272SSarah Sharp 	 * The host controller will choose the device address,
4161c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
4162c6515272SSarah Sharp 	 */
4163c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
41641da177e4SLinus Torvalds 		return -EINVAL;
41651da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
41661da177e4SLinus Torvalds 		return 0;
41671da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
41681da177e4SLinus Torvalds 		return -EINVAL;
4169c8d4af8eSAndiry Xu 	if (hcd->driver->address_device)
4170c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
4171c8d4af8eSAndiry Xu 	else
41721da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
41734326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
41741da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
41751da177e4SLinus Torvalds 	if (retval == 0) {
41763b29b68bSAlan Stern 		update_devnum(udev, devnum);
41774953d141SDavid Vrabel 		/* Device now using proper address. */
41781da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
4179fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
41801da177e4SLinus Torvalds 	}
41811da177e4SLinus Torvalds 	return retval;
41821da177e4SLinus Torvalds }
41831da177e4SLinus Torvalds 
4184890dae88SMathias Nyman /*
4185890dae88SMathias Nyman  * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4186890dae88SMathias Nyman  * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4187890dae88SMathias Nyman  * enabled.
4188890dae88SMathias Nyman  *
4189890dae88SMathias Nyman  * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4190890dae88SMathias Nyman  * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4191890dae88SMathias Nyman  * support bit in the BOS descriptor.
4192890dae88SMathias Nyman  */
4193890dae88SMathias Nyman static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4194890dae88SMathias Nyman {
4195d99f6b41SDan Williams 	struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4196d99f6b41SDan Williams 	int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
4197890dae88SMathias Nyman 
4198890dae88SMathias Nyman 	if (!udev->usb2_hw_lpm_capable)
4199890dae88SMathias Nyman 		return;
4200890dae88SMathias Nyman 
4201d99f6b41SDan Williams 	if (hub)
4202d99f6b41SDan Williams 		connect_type = hub->ports[udev->portnum - 1]->connect_type;
4203890dae88SMathias Nyman 
420423c05820SBjørn Mork 	if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
4205890dae88SMathias Nyman 			connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4206890dae88SMathias Nyman 		udev->usb2_hw_lpm_allowed = 1;
4207890dae88SMathias Nyman 		usb_set_usb2_hardware_lpm(udev, 1);
4208890dae88SMathias Nyman 	}
4209890dae88SMathias Nyman }
4210890dae88SMathias Nyman 
421148fc7dbdSDan Williams static int hub_enable_device(struct usb_device *udev)
421248fc7dbdSDan Williams {
421348fc7dbdSDan Williams 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
421448fc7dbdSDan Williams 
421548fc7dbdSDan Williams 	if (!hcd->driver->enable_device)
421648fc7dbdSDan Williams 		return 0;
421748fc7dbdSDan Williams 	if (udev->state == USB_STATE_ADDRESS)
421848fc7dbdSDan Williams 		return 0;
421948fc7dbdSDan Williams 	if (udev->state != USB_STATE_DEFAULT)
422048fc7dbdSDan Williams 		return -EINVAL;
422148fc7dbdSDan Williams 
422248fc7dbdSDan Williams 	return hcd->driver->enable_device(hcd, udev);
422348fc7dbdSDan Williams }
422448fc7dbdSDan Williams 
42251da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
42261da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
42271da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
42281da177e4SLinus Torvalds  *
42291da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
42305c79a1e3SDan Williams  * usb_reset_and_verify_device), the caller must own the device lock and
42315c79a1e3SDan Williams  * the port lock.  For a newly detected device that is not accessible
42325c79a1e3SDan Williams  * through any global pointers, it's not necessary to lock the device,
42335c79a1e3SDan Williams  * but it is still necessary to lock the port.
42341da177e4SLinus Torvalds  */
42351da177e4SLinus Torvalds static int
42361da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
42371da177e4SLinus Torvalds 		int retry_counter)
42381da177e4SLinus Torvalds {
42391da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
4240e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
42411da177e4SLinus Torvalds 	int			i, j, retval;
42421da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
42431da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
4244e538dfdaSMichal Nazarewicz 	const char		*speed;
42454326ed0bSAlan Stern 	int			devnum = udev->devnum;
42461da177e4SLinus Torvalds 
42471da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
42481da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
42491da177e4SLinus Torvalds 	 */
42501da177e4SLinus Torvalds 	if (!hdev->parent) {
42511da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
42521da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
42531da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
42541da177e4SLinus Torvalds 	}
42551da177e4SLinus Torvalds 
42561da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
42571da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
42581da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
42591da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
42601da177e4SLinus Torvalds 
42616fecd4f2STodd E Brandt 	mutex_lock(&hdev->bus->usb_address0_mutex);
42621da177e4SLinus Torvalds 
42631da177e4SLinus Torvalds 	/* Reset the device; full speed may morph to high speed */
4264e7b77172SSarah Sharp 	/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
426575d7cf72SAndiry Xu 	retval = hub_port_reset(hub, port1, udev, delay, false);
42661da177e4SLinus Torvalds 	if (retval < 0)		/* error or disconnect */
42671da177e4SLinus Torvalds 		goto fail;
42681da177e4SLinus Torvalds 	/* success, speed is known */
426907194ab7SLuben Tuikov 
42701da177e4SLinus Torvalds 	retval = -ENODEV;
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
42731da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
42741da177e4SLinus Torvalds 		goto fail;
42751da177e4SLinus Torvalds 	}
42761da177e4SLinus Torvalds 	oldspeed = udev->speed;
42771da177e4SLinus Torvalds 
42781da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
42791da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
42805bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
42815bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
42821da177e4SLinus Torvalds 	 */
42831da177e4SLinus Torvalds 	switch (udev->speed) {
42846b403b02SSarah Sharp 	case USB_SPEED_SUPER:
4285551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:	/* fixed at 512 */
4286551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
42875bb6e0aeSInaky Perez-Gonzalez 		break;
42881da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
4289551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
42901da177e4SLinus Torvalds 		break;
42911da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
42921da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
42931da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
42941da177e4SLinus Torvalds 		 * then correct our initial guess.
42951da177e4SLinus Torvalds 		 */
4296551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
42971da177e4SLinus Torvalds 		break;
42981da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
4299551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
43001da177e4SLinus Torvalds 		break;
43011da177e4SLinus Torvalds 	default:
43021da177e4SLinus Torvalds 		goto fail;
43031da177e4SLinus Torvalds 	}
43041da177e4SLinus Torvalds 
4305e538dfdaSMichal Nazarewicz 	if (udev->speed == USB_SPEED_WIRELESS)
4306e538dfdaSMichal Nazarewicz 		speed = "variable speed Wireless";
4307e538dfdaSMichal Nazarewicz 	else
4308e538dfdaSMichal Nazarewicz 		speed = usb_speed_string(udev->speed);
4309e538dfdaSMichal Nazarewicz 
4310c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
431183a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
4312e538dfdaSMichal Nazarewicz 				"%s %s USB device number %d using %s\n",
4313e538dfdaSMichal Nazarewicz 				(udev->config) ? "reset" : "new", speed,
43143b29b68bSAlan Stern 				devnum, udev->bus->controller->driver->name);
43151da177e4SLinus Torvalds 
43161da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
43171da177e4SLinus Torvalds 	if (hdev->tt) {
43181da177e4SLinus Torvalds 		udev->tt = hdev->tt;
43191da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
43201da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
43211da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
4322d199c96dSAlan Stern 		if (!hub->tt.hub) {
4323d199c96dSAlan Stern 			dev_err(&udev->dev, "parent hub has no TT\n");
4324d199c96dSAlan Stern 			retval = -EINVAL;
4325d199c96dSAlan Stern 			goto fail;
4326d199c96dSAlan Stern 		}
43271da177e4SLinus Torvalds 		udev->tt = &hub->tt;
43281da177e4SLinus Torvalds 		udev->ttport = port1;
43291da177e4SLinus Torvalds 	}
43301da177e4SLinus Torvalds 
43311da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
43321da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
43331da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
43341da177e4SLinus Torvalds 	 * Change it cautiously.
43351da177e4SLinus Torvalds 	 *
433648fc7dbdSDan Williams 	 * NOTE:  If use_new_scheme() is true we will start by issuing
43371da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
43381da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
43391da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
43401da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
43411da177e4SLinus Torvalds 	 * value.
43421da177e4SLinus Torvalds 	 */
43431da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
434448fc7dbdSDan Williams 		bool did_new_scheme = false;
434548fc7dbdSDan Williams 
434648fc7dbdSDan Williams 		if (use_new_scheme(udev, retry_counter)) {
43471da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
43481da177e4SLinus Torvalds 			int r = 0;
43491da177e4SLinus Torvalds 
435048fc7dbdSDan Williams 			did_new_scheme = true;
435148fc7dbdSDan Williams 			retval = hub_enable_device(udev);
4352938569ebSOliver Neukum 			if (retval < 0) {
4353938569ebSOliver Neukum 				dev_err(&udev->dev,
4354938569ebSOliver Neukum 					"hub failed to enable device, error %d\n",
4355938569ebSOliver Neukum 					retval);
435648fc7dbdSDan Williams 				goto fail;
4357938569ebSOliver Neukum 			}
435848fc7dbdSDan Williams 
43591da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
43601da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
43611da177e4SLinus Torvalds 			if (!buf) {
43621da177e4SLinus Torvalds 				retval = -ENOMEM;
43631da177e4SLinus Torvalds 				continue;
43641da177e4SLinus Torvalds 			}
43651da177e4SLinus Torvalds 
4366b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
4367b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
4368b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
43691da177e4SLinus Torvalds 			 */
43701da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
43711da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
43721da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
43731da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
43741da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
43751da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
4376fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
43771da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
43785bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
43791da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
43801da177e4SLinus Torvalds 							USB_DT_DEVICE) {
43811da177e4SLinus Torvalds 						r = 0;
43821da177e4SLinus Torvalds 						break;
43831da177e4SLinus Torvalds 					}
43841da177e4SLinus Torvalds 					/* FALL THROUGH */
43851da177e4SLinus Torvalds 				default:
43861da177e4SLinus Torvalds 					if (r == 0)
43871da177e4SLinus Torvalds 						r = -EPROTO;
43881da177e4SLinus Torvalds 					break;
43891da177e4SLinus Torvalds 				}
43901da177e4SLinus Torvalds 				if (r == 0)
43911da177e4SLinus Torvalds 					break;
43921da177e4SLinus Torvalds 			}
43931da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
43941da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
43951da177e4SLinus Torvalds 			kfree(buf);
43961da177e4SLinus Torvalds 
439775d7cf72SAndiry Xu 			retval = hub_port_reset(hub, port1, udev, delay, false);
43981da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
43991da177e4SLinus Torvalds 				goto fail;
44001da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
44011da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
44021da177e4SLinus Torvalds 					"device reset changed speed!\n");
44031da177e4SLinus Torvalds 				retval = -ENODEV;
44041da177e4SLinus Torvalds 				goto fail;
44051da177e4SLinus Torvalds 			}
44061da177e4SLinus Torvalds 			if (r) {
4407e9e88fb7SAlan Stern 				if (r != -ENODEV)
4408e9e88fb7SAlan Stern 					dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4409b9cef6c3SWu Fengguang 							r);
44101da177e4SLinus Torvalds 				retval = -EMSGSIZE;
44111da177e4SLinus Torvalds 				continue;
44121da177e4SLinus Torvalds 			}
44131da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
44141da177e4SLinus Torvalds 		}
44151da177e4SLinus Torvalds 
44166c529cdcSInaky Perez-Gonzalez 		/*
44176c529cdcSInaky Perez-Gonzalez 		 * If device is WUSB, we already assigned an
44186c529cdcSInaky Perez-Gonzalez 		 * unauthorized address in the Connect Ack sequence;
44196c529cdcSInaky Perez-Gonzalez 		 * authorization will assign the final address.
44206c529cdcSInaky Perez-Gonzalez 		 */
44216c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
44221da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
44234326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
44241da177e4SLinus Torvalds 				if (retval >= 0)
44251da177e4SLinus Torvalds 					break;
44261da177e4SLinus Torvalds 				msleep(200);
44271da177e4SLinus Torvalds 			}
44281da177e4SLinus Torvalds 			if (retval < 0) {
4429e9e88fb7SAlan Stern 				if (retval != -ENODEV)
4430e9e88fb7SAlan Stern 					dev_err(&udev->dev, "device not accepting address %d, error %d\n",
44314326ed0bSAlan Stern 							devnum, retval);
44321da177e4SLinus Torvalds 				goto fail;
44331da177e4SLinus Torvalds 			}
4434c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
4435c6515272SSarah Sharp 				devnum = udev->devnum;
4436c6515272SSarah Sharp 				dev_info(&udev->dev,
44373b29b68bSAlan Stern 						"%s SuperSpeed USB device number %d using %s\n",
4438c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
44393b29b68bSAlan Stern 						devnum, udev->bus->controller->driver->name);
4440c6515272SSarah Sharp 			}
44411da177e4SLinus Torvalds 
44421da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
44431da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
44441da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
44451da177e4SLinus Torvalds 			 */
44461da177e4SLinus Torvalds 			msleep(10);
444748fc7dbdSDan Williams 			/* use_new_scheme() checks the speed which may have
444848fc7dbdSDan Williams 			 * changed since the initial look so we cache the result
444948fc7dbdSDan Williams 			 * in did_new_scheme
445048fc7dbdSDan Williams 			 */
445148fc7dbdSDan Williams 			if (did_new_scheme)
44521da177e4SLinus Torvalds 				break;
44536c529cdcSInaky Perez-Gonzalez 		}
44541da177e4SLinus Torvalds 
44551da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
44561da177e4SLinus Torvalds 		if (retval < 8) {
4457e9e88fb7SAlan Stern 			if (retval != -ENODEV)
4458b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
4459b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
4460b9cef6c3SWu Fengguang 					retval);
44611da177e4SLinus Torvalds 			if (retval >= 0)
44621da177e4SLinus Torvalds 				retval = -EMSGSIZE;
44631da177e4SLinus Torvalds 		} else {
44641da177e4SLinus Torvalds 			retval = 0;
44651da177e4SLinus Torvalds 			break;
44661da177e4SLinus Torvalds 		}
44671da177e4SLinus Torvalds 	}
44681da177e4SLinus Torvalds 	if (retval)
44691da177e4SLinus Torvalds 		goto fail;
44701da177e4SLinus Torvalds 
4471d8aec3dbSElric Fu 	/*
4472d8aec3dbSElric Fu 	 * Some superspeed devices have finished the link training process
4473d8aec3dbSElric Fu 	 * and attached to a superspeed hub port, but the device descriptor
4474d8aec3dbSElric Fu 	 * got from those devices show they aren't superspeed devices. Warm
4475d8aec3dbSElric Fu 	 * reset the port attached by the devices can fix them.
4476d8aec3dbSElric Fu 	 */
4477d8aec3dbSElric Fu 	if ((udev->speed == USB_SPEED_SUPER) &&
4478d8aec3dbSElric Fu 			(le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4479d8aec3dbSElric Fu 		dev_err(&udev->dev, "got a wrong device descriptor, "
4480d8aec3dbSElric Fu 				"warm reset device\n");
4481d8aec3dbSElric Fu 		hub_port_reset(hub, port1, udev,
4482d8aec3dbSElric Fu 				HUB_BH_RESET_TIME, true);
4483d8aec3dbSElric Fu 		retval = -EINVAL;
4484d8aec3dbSElric Fu 		goto fail;
4485d8aec3dbSElric Fu 	}
4486d8aec3dbSElric Fu 
44876b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
44886b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
44896b403b02SSarah Sharp 		i = 512;
44906b403b02SSarah Sharp 	else
44916b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
449229cc8897SKuninori Morimoto 	if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
449356626a72SAlan Stern 		if (udev->speed == USB_SPEED_LOW ||
44941da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
449556626a72SAlan Stern 			dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
44961da177e4SLinus Torvalds 			retval = -EMSGSIZE;
44971da177e4SLinus Torvalds 			goto fail;
44981da177e4SLinus Torvalds 		}
449956626a72SAlan Stern 		if (udev->speed == USB_SPEED_FULL)
45001da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
450156626a72SAlan Stern 		else
450256626a72SAlan Stern 			dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
45031da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
4504fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
45051da177e4SLinus Torvalds 	}
45061da177e4SLinus Torvalds 
45071da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
45081da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
4509e9e88fb7SAlan Stern 		if (retval != -ENODEV)
4510b9cef6c3SWu Fengguang 			dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4511b9cef6c3SWu Fengguang 					retval);
45121da177e4SLinus Torvalds 		if (retval >= 0)
45131da177e4SLinus Torvalds 			retval = -ENOMSG;
45141da177e4SLinus Torvalds 		goto fail;
45151da177e4SLinus Torvalds 	}
45161da177e4SLinus Torvalds 
45171ff4df56SAndiry Xu 	if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
45181ff4df56SAndiry Xu 		retval = usb_get_bos_descriptor(udev);
451951e0a012SSarah Sharp 		if (!retval) {
4520d9b2099cSSarah Sharp 			udev->lpm_capable = usb_device_supports_lpm(udev);
452151e0a012SSarah Sharp 			usb_set_lpm_parameters(udev);
452251e0a012SSarah Sharp 		}
45231ff4df56SAndiry Xu 	}
45243148bf04SAndiry Xu 
45251da177e4SLinus Torvalds 	retval = 0;
452648f24970SAlek Du 	/* notify HCD that we have a device connected and addressed */
452748f24970SAlek Du 	if (hcd->driver->update_device)
452848f24970SAlek Du 		hcd->driver->update_device(hcd, udev);
4529890dae88SMathias Nyman 	hub_set_initial_usb2_lpm_policy(udev);
45301da177e4SLinus Torvalds fail:
45314326ed0bSAlan Stern 	if (retval) {
45321da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
45333b29b68bSAlan Stern 		update_devnum(udev, devnum);	/* for disconnect processing */
45344326ed0bSAlan Stern 	}
45356fecd4f2STodd E Brandt 	mutex_unlock(&hdev->bus->usb_address0_mutex);
45361da177e4SLinus Torvalds 	return retval;
45371da177e4SLinus Torvalds }
45381da177e4SLinus Torvalds 
45391da177e4SLinus Torvalds static void
45401da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
45411da177e4SLinus Torvalds {
45421da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
45431da177e4SLinus Torvalds 	int				status;
45441da177e4SLinus Torvalds 
45452a159389SJohan Hovold 	if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
45462a159389SJohan Hovold 		return;
45472a159389SJohan Hovold 
4548e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
45491da177e4SLinus Torvalds 	if (qual == NULL)
45501da177e4SLinus Torvalds 		return;
45511da177e4SLinus Torvalds 
45521da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
45531da177e4SLinus Torvalds 			qual, sizeof *qual);
45541da177e4SLinus Torvalds 	if (status == sizeof *qual) {
45551da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
45561da177e4SLinus Torvalds 			"connect to a high speed hub\n");
45571da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
45581da177e4SLinus Torvalds 		if (hub->has_indicators) {
45591da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
456022f6a0f0SShaibal Dutta 			queue_delayed_work(system_power_efficient_wq,
456122f6a0f0SShaibal Dutta 					&hub->leds, 0);
45621da177e4SLinus Torvalds 		}
45631da177e4SLinus Torvalds 	}
45641da177e4SLinus Torvalds 	kfree(qual);
45651da177e4SLinus Torvalds }
45661da177e4SLinus Torvalds 
45671da177e4SLinus Torvalds static unsigned
45681da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
45691da177e4SLinus Torvalds {
45701da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
45711da177e4SLinus Torvalds 	int remaining;
457255c52718SAlan Stern 	int port1;
45731da177e4SLinus Torvalds 
457455c52718SAlan Stern 	if (!hub->limited_power)
45751da177e4SLinus Torvalds 		return 0;
45761da177e4SLinus Torvalds 
457755c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
457855c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4579d99f6b41SDan Williams 		struct usb_port *port_dev = hub->ports[port1 - 1];
4580d99f6b41SDan Williams 		struct usb_device *udev = port_dev->child;
4581430ee58eSSebastian Andrzej Siewior 		unsigned unit_load;
4582d99f6b41SDan Williams 		int delta;
45831da177e4SLinus Torvalds 
45841da177e4SLinus Torvalds 		if (!udev)
45851da177e4SLinus Torvalds 			continue;
4586430ee58eSSebastian Andrzej Siewior 		if (hub_is_superspeed(udev))
4587430ee58eSSebastian Andrzej Siewior 			unit_load = 150;
4588430ee58eSSebastian Andrzej Siewior 		else
4589430ee58eSSebastian Andrzej Siewior 			unit_load = 100;
45901da177e4SLinus Torvalds 
4591430ee58eSSebastian Andrzej Siewior 		/*
4592430ee58eSSebastian Andrzej Siewior 		 * Unconfigured devices may not use more than one unit load,
4593430ee58eSSebastian Andrzej Siewior 		 * or 8mA for OTG ports
4594430ee58eSSebastian Andrzej Siewior 		 */
45951da177e4SLinus Torvalds 		if (udev->actconfig)
45968d8479dbSSebastian Andrzej Siewior 			delta = usb_get_max_power(udev, udev->actconfig);
459755c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
4598430ee58eSSebastian Andrzej Siewior 			delta = unit_load;
45991da177e4SLinus Torvalds 		else
460055c52718SAlan Stern 			delta = 8;
460155c52718SAlan Stern 		if (delta > hub->mA_per_port)
4602d99f6b41SDan Williams 			dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4603d99f6b41SDan Williams 					delta, hub->mA_per_port);
46041da177e4SLinus Torvalds 		remaining -= delta;
46051da177e4SLinus Torvalds 	}
46061da177e4SLinus Torvalds 	if (remaining < 0) {
460755c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
460855c52718SAlan Stern 			-remaining);
46091da177e4SLinus Torvalds 		remaining = 0;
46101da177e4SLinus Torvalds 	}
46111da177e4SLinus Torvalds 	return remaining;
46121da177e4SLinus Torvalds }
46131da177e4SLinus Torvalds 
4614af376a46SDan Williams static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4615af376a46SDan Williams 		u16 portchange)
46161da177e4SLinus Torvalds {
46171da177e4SLinus Torvalds 	int status, i;
4618430ee58eSSebastian Andrzej Siewior 	unsigned unit_load;
46191da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
46201da177e4SLinus Torvalds 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4621d99f6b41SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
4622af376a46SDan Williams 	struct usb_device *udev = port_dev->child;
46235ee0f803SOliver Neukum 	static int unreliable_port = -1;
46248808f00cSAlan Stern 
462524618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
4626b76baa81SPeter Chen 	if (udev) {
46273d46e73dSAntoine Tenart 		if (hcd->usb_phy && !hdev->parent &&
4628b76baa81SPeter Chen 				!(portstatus & USB_PORT_STAT_CONNECTION))
46293d46e73dSAntoine Tenart 			usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
4630d99f6b41SDan Williams 		usb_disconnect(&port_dev->child);
4631b76baa81SPeter Chen 	}
463224618b0cSAlan Stern 
4633253e0572SAlan Stern 	/* We can forget about a "removed" device when there's a physical
4634253e0572SAlan Stern 	 * disconnect or the connect status changes.
4635253e0572SAlan Stern 	 */
4636253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4637253e0572SAlan Stern 			(portchange & USB_PORT_STAT_C_CONNECTION))
4638253e0572SAlan Stern 		clear_bit(port1, hub->removed_bits);
4639253e0572SAlan Stern 
46405257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
46415257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
4642ad493e5eSLan Tianyu 		status = hub_port_debounce_be_stable(hub, port1);
46435257d97aSAlan Stern 		if (status < 0) {
46445ee0f803SOliver Neukum 			if (status != -ENODEV &&
46455ee0f803SOliver Neukum 				port1 != unreliable_port &&
46465ee0f803SOliver Neukum 				printk_ratelimit())
4647dd5f5006STakashi Iwai 				dev_err(&port_dev->dev, "connect-debounce failed\n");
46485257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
46495ee0f803SOliver Neukum 			unreliable_port = port1;
46505257d97aSAlan Stern 		} else {
46515257d97aSAlan Stern 			portstatus = status;
46525257d97aSAlan Stern 		}
46535257d97aSAlan Stern 	}
46545257d97aSAlan Stern 
4655253e0572SAlan Stern 	/* Return now if debouncing failed or nothing is connected or
4656253e0572SAlan Stern 	 * the device was "removed".
4657253e0572SAlan Stern 	 */
4658253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4659253e0572SAlan Stern 			test_bit(port1, hub->removed_bits)) {
46601da177e4SLinus Torvalds 
46611da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
46629262c19dSDan Williams 		if (hub_is_port_power_switchable(hub)
46630ed9a57eSAndiry Xu 				&& !port_is_power_on(hub, portstatus))
46641da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
46651da177e4SLinus Torvalds 
46661da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
46671da177e4SLinus Torvalds 			goto done;
46681da177e4SLinus Torvalds 		return;
46691da177e4SLinus Torvalds 	}
4670430ee58eSSebastian Andrzej Siewior 	if (hub_is_superspeed(hub->hdev))
4671430ee58eSSebastian Andrzej Siewior 		unit_load = 150;
4672430ee58eSSebastian Andrzej Siewior 	else
4673430ee58eSSebastian Andrzej Siewior 		unit_load = 100;
46741da177e4SLinus Torvalds 
4675e9e88fb7SAlan Stern 	status = 0;
46761da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
46771da177e4SLinus Torvalds 
46781da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
46791da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
46801da177e4SLinus Torvalds 		 */
46811da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
46821da177e4SLinus Torvalds 		if (!udev) {
4683d99f6b41SDan Williams 			dev_err(&port_dev->dev,
4684d99f6b41SDan Williams 					"couldn't allocate usb_device\n");
46851da177e4SLinus Torvalds 			goto done;
46861da177e4SLinus Torvalds 		}
46871da177e4SLinus Torvalds 
46881da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
468955c52718SAlan Stern 		udev->bus_mA = hub->mA_per_port;
4690b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
46918af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
46921da177e4SLinus Torvalds 
4693131dec34SSarah Sharp 		/* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4694131dec34SSarah Sharp 		if (hub_is_superspeed(hub->hdev))
4695e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
4696e7b77172SSarah Sharp 		else
4697e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
4698e7b77172SSarah Sharp 
46993b29b68bSAlan Stern 		choose_devnum(udev);
4700c6515272SSarah Sharp 		if (udev->devnum <= 0) {
4701c6515272SSarah Sharp 			status = -ENOTCONN;	/* Don't retry */
4702c6515272SSarah Sharp 			goto loop;
4703c6515272SSarah Sharp 		}
4704c6515272SSarah Sharp 
4705e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
47065c79a1e3SDan Williams 		usb_lock_port(port_dev);
47071da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
47085c79a1e3SDan Williams 		usb_unlock_port(port_dev);
47091da177e4SLinus Torvalds 		if (status < 0)
47101da177e4SLinus Torvalds 			goto loop;
47111da177e4SLinus Torvalds 
471293362a87SPhil Dibowitz 		usb_detect_quirks(udev);
471393362a87SPhil Dibowitz 		if (udev->quirks & USB_QUIRK_DELAY_INIT)
471493362a87SPhil Dibowitz 			msleep(1000);
471593362a87SPhil Dibowitz 
47161da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
47171da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
47181da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
47191da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
47201da177e4SLinus Torvalds 		 * on the parent.
47211da177e4SLinus Torvalds 		 */
47221da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
4723430ee58eSSebastian Andrzej Siewior 				&& udev->bus_mA <= unit_load) {
47241da177e4SLinus Torvalds 			u16	devstat;
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
47271da177e4SLinus Torvalds 					&devstat);
472815b7336eSAlan Stern 			if (status) {
47291da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
47301da177e4SLinus Torvalds 				goto loop_disable;
47311da177e4SLinus Torvalds 			}
47321da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
47331da177e4SLinus Torvalds 				dev_err(&udev->dev,
47341da177e4SLinus Torvalds 					"can't connect bus-powered hub "
47351da177e4SLinus Torvalds 					"to this port\n");
47361da177e4SLinus Torvalds 				if (hub->has_indicators) {
47371da177e4SLinus Torvalds 					hub->indicator[port1-1] =
47381da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
473922f6a0f0SShaibal Dutta 					queue_delayed_work(
474022f6a0f0SShaibal Dutta 						system_power_efficient_wq,
474122f6a0f0SShaibal Dutta 						&hub->leds, 0);
47421da177e4SLinus Torvalds 				}
47431da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
47441da177e4SLinus Torvalds 				goto loop_disable;
47451da177e4SLinus Torvalds 			}
47461da177e4SLinus Torvalds 		}
47471da177e4SLinus Torvalds 
47481da177e4SLinus Torvalds 		/* check for devices running slower than they could */
47491da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
47501da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
47511da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
47521da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
47531da177e4SLinus Torvalds 
4754fa286188SGreg Kroah-Hartman 		/* Store the parent's children[] pointer.  At this point
47551da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
47561da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
47571da177e4SLinus Torvalds 		 */
47581da177e4SLinus Torvalds 		status = 0;
47591da177e4SLinus Torvalds 
4760d8521afeSDan Williams 		mutex_lock(&usb_port_peer_mutex);
4761d8521afeSDan Williams 
47621da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
47631da177e4SLinus Torvalds 		 * been disconnected; we would race with the
47641da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
47651da177e4SLinus Torvalds 		 */
47661da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
47671da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
47681da177e4SLinus Torvalds 			status = -ENOTCONN;
47691da177e4SLinus Torvalds 		else
4770d99f6b41SDan Williams 			port_dev->child = udev;
47711da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
4772d8521afeSDan Williams 		mutex_unlock(&usb_port_peer_mutex);
47731da177e4SLinus Torvalds 
47741da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
47751da177e4SLinus Torvalds 		if (!status) {
47761da177e4SLinus Torvalds 			status = usb_new_device(udev);
47771da177e4SLinus Torvalds 			if (status) {
4778d8521afeSDan Williams 				mutex_lock(&usb_port_peer_mutex);
47791da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
4780d99f6b41SDan Williams 				port_dev->child = NULL;
47811da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
4782d8521afeSDan Williams 				mutex_unlock(&usb_port_peer_mutex);
478301ed67dcSTony Zheng 			} else {
478401ed67dcSTony Zheng 				if (hcd->usb_phy && !hdev->parent)
478501ed67dcSTony Zheng 					usb_phy_notify_connect(hcd->usb_phy,
478601ed67dcSTony Zheng 							udev->speed);
47871da177e4SLinus Torvalds 			}
47881da177e4SLinus Torvalds 		}
47891da177e4SLinus Torvalds 
47901da177e4SLinus Torvalds 		if (status)
47911da177e4SLinus Torvalds 			goto loop_disable;
47921da177e4SLinus Torvalds 
47931da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
47941da177e4SLinus Torvalds 		if (status)
4795d99f6b41SDan Williams 			dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
47961da177e4SLinus Torvalds 
47971da177e4SLinus Torvalds 		return;
47981da177e4SLinus Torvalds 
47991da177e4SLinus Torvalds loop_disable:
48001da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
48011da177e4SLinus Torvalds loop:
4802fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
48033b29b68bSAlan Stern 		release_devnum(udev);
4804f7410cedSHerbert Xu 		hub_free_dev(udev);
48051da177e4SLinus Torvalds 		usb_put_dev(udev);
4806ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
48071da177e4SLinus Torvalds 			break;
48081da177e4SLinus Torvalds 	}
48093a31155cSAlan Stern 	if (hub->hdev->parent ||
48103a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
4811e9e88fb7SAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1)) {
4812e9e88fb7SAlan Stern 		if (status != -ENOTCONN && status != -ENODEV)
4813d99f6b41SDan Williams 			dev_err(&port_dev->dev,
4814d99f6b41SDan Williams 					"unable to enumerate USB device\n");
4815e9e88fb7SAlan Stern 	}
48161da177e4SLinus Torvalds 
48171da177e4SLinus Torvalds done:
48181da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
481990da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
482090da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
4821af376a46SDan Williams 
48221da177e4SLinus Torvalds }
48231da177e4SLinus Torvalds 
4824af376a46SDan Williams /* Handle physical or logical connection change events.
4825af376a46SDan Williams  * This routine is called when:
4826af376a46SDan Williams  *	a port connection-change occurs;
4827af376a46SDan Williams  *	a port enable-change occurs (often caused by EMI);
4828af376a46SDan Williams  *	usb_reset_and_verify_device() encounters changed descriptors (as from
4829af376a46SDan Williams  *		a firmware download)
4830af376a46SDan Williams  * caller already locked the hub
4831af376a46SDan Williams  */
4832af376a46SDan Williams static void hub_port_connect_change(struct usb_hub *hub, int port1,
483372937e1eSSarah Sharp 					u16 portstatus, u16 portchange)
48345c79a1e3SDan Williams 		__must_hold(&port_dev->status_lock)
4835714b07beSSarah Sharp {
4836af376a46SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
4837af376a46SDan Williams 	struct usb_device *udev = port_dev->child;
4838af376a46SDan Williams 	int status = -ENODEV;
4839714b07beSSarah Sharp 
4840af376a46SDan Williams 	dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4841af376a46SDan Williams 			portchange, portspeed(hub, portstatus));
4842af376a46SDan Williams 
4843af376a46SDan Williams 	if (hub->has_indicators) {
4844af376a46SDan Williams 		set_port_led(hub, port1, HUB_LED_AUTO);
4845af376a46SDan Williams 		hub->indicator[port1-1] = INDICATOR_AUTO;
48464ee823b8SSarah Sharp 	}
4847714b07beSSarah Sharp 
4848af376a46SDan Williams #ifdef	CONFIG_USB_OTG
4849af376a46SDan Williams 	/* during HNP, don't repeat the debounce */
4850af376a46SDan Williams 	if (hub->hdev->bus->is_b_host)
4851af376a46SDan Williams 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4852af376a46SDan Williams 				USB_PORT_STAT_C_ENABLE);
4853af376a46SDan Williams #endif
4854714b07beSSarah Sharp 
4855af376a46SDan Williams 	/* Try to resuscitate an existing device */
4856af376a46SDan Williams 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4857af376a46SDan Williams 			udev->state != USB_STATE_NOTATTACHED) {
4858af376a46SDan Williams 		if (portstatus & USB_PORT_STAT_ENABLE) {
4859af376a46SDan Williams 			status = 0;		/* Nothing to do */
4860af376a46SDan Williams #ifdef CONFIG_PM_RUNTIME
4861af376a46SDan Williams 		} else if (udev->state == USB_STATE_SUSPENDED &&
4862af376a46SDan Williams 				udev->persist_enabled) {
4863af376a46SDan Williams 			/* For a suspended device, treat this as a
4864af376a46SDan Williams 			 * remote wakeup event.
4865af376a46SDan Williams 			 */
48665c79a1e3SDan Williams 			usb_unlock_port(port_dev);
4867af376a46SDan Williams 			status = usb_remote_wakeup(udev);
48685c79a1e3SDan Williams 			usb_lock_port(port_dev);
4869af376a46SDan Williams #endif
4870af376a46SDan Williams 		} else {
4871af376a46SDan Williams 			/* Don't resuscitate */;
4872af376a46SDan Williams 		}
4873af376a46SDan Williams 	}
4874af376a46SDan Williams 	clear_bit(port1, hub->change_bits);
4875af376a46SDan Williams 
48765c79a1e3SDan Williams 	/* successfully revalidated the connection */
4877af376a46SDan Williams 	if (status == 0)
4878af376a46SDan Williams 		return;
4879af376a46SDan Williams 
48805c79a1e3SDan Williams 	usb_unlock_port(port_dev);
4881af376a46SDan Williams 	hub_port_connect(hub, port1, portstatus, portchange);
48825c79a1e3SDan Williams 	usb_lock_port(port_dev);
48831da177e4SLinus Torvalds }
48841da177e4SLinus Torvalds 
4885af376a46SDan Williams static void port_event(struct usb_hub *hub, int port1)
48865c79a1e3SDan Williams 		__must_hold(&port_dev->status_lock)
4887af376a46SDan Williams {
4888af376a46SDan Williams 	int connect_change, reset_device = 0;
4889af376a46SDan Williams 	struct usb_port *port_dev = hub->ports[port1 - 1];
4890af376a46SDan Williams 	struct usb_device *udev = port_dev->child;
4891af376a46SDan Williams 	struct usb_device *hdev = hub->hdev;
4892af376a46SDan Williams 	u16 portstatus, portchange;
4893af376a46SDan Williams 
4894af376a46SDan Williams 	connect_change = test_bit(port1, hub->change_bits);
4895af376a46SDan Williams 	clear_bit(port1, hub->event_bits);
4896af376a46SDan Williams 	clear_bit(port1, hub->wakeup_bits);
4897af376a46SDan Williams 
4898af376a46SDan Williams 	if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
4899af376a46SDan Williams 		return;
4900af376a46SDan Williams 
4901af376a46SDan Williams 	if (portchange & USB_PORT_STAT_C_CONNECTION) {
4902af376a46SDan Williams 		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
4903714b07beSSarah Sharp 		connect_change = 1;
4904714b07beSSarah Sharp 	}
4905af376a46SDan Williams 
4906af376a46SDan Williams 	if (portchange & USB_PORT_STAT_C_ENABLE) {
4907af376a46SDan Williams 		if (!connect_change)
4908af376a46SDan Williams 			dev_dbg(&port_dev->dev, "enable change, status %08x\n",
4909af376a46SDan Williams 					portstatus);
4910af376a46SDan Williams 		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
4911af376a46SDan Williams 
4912af376a46SDan Williams 		/*
4913af376a46SDan Williams 		 * EM interference sometimes causes badly shielded USB devices
4914af376a46SDan Williams 		 * to be shutdown by the hub, this hack enables them again.
4915af376a46SDan Williams 		 * Works at least with mouse driver.
4916af376a46SDan Williams 		 */
4917af376a46SDan Williams 		if (!(portstatus & USB_PORT_STAT_ENABLE)
4918af376a46SDan Williams 		    && !connect_change && udev) {
4919af376a46SDan Williams 			dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
4920af376a46SDan Williams 			connect_change = 1;
4921714b07beSSarah Sharp 		}
4922af376a46SDan Williams 	}
4923af376a46SDan Williams 
4924af376a46SDan Williams 	if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4925af376a46SDan Williams 		u16 status = 0, unused;
4926af376a46SDan Williams 
4927af376a46SDan Williams 		dev_dbg(&port_dev->dev, "over-current change\n");
4928af376a46SDan Williams 		usb_clear_port_feature(hdev, port1,
4929af376a46SDan Williams 				USB_PORT_FEAT_C_OVER_CURRENT);
4930af376a46SDan Williams 		msleep(100);	/* Cool down */
4931af376a46SDan Williams 		hub_power_on(hub, true);
4932af376a46SDan Williams 		hub_port_status(hub, port1, &status, &unused);
4933af376a46SDan Williams 		if (status & USB_PORT_STAT_OVERCURRENT)
4934af376a46SDan Williams 			dev_err(&port_dev->dev, "over-current condition\n");
4935af376a46SDan Williams 	}
4936af376a46SDan Williams 
4937af376a46SDan Williams 	if (portchange & USB_PORT_STAT_C_RESET) {
4938af376a46SDan Williams 		dev_dbg(&port_dev->dev, "reset change\n");
4939af376a46SDan Williams 		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
4940af376a46SDan Williams 	}
4941af376a46SDan Williams 	if ((portchange & USB_PORT_STAT_C_BH_RESET)
4942af376a46SDan Williams 	    && hub_is_superspeed(hdev)) {
4943af376a46SDan Williams 		dev_dbg(&port_dev->dev, "warm reset change\n");
4944af376a46SDan Williams 		usb_clear_port_feature(hdev, port1,
4945af376a46SDan Williams 				USB_PORT_FEAT_C_BH_PORT_RESET);
4946af376a46SDan Williams 	}
4947af376a46SDan Williams 	if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4948af376a46SDan Williams 		dev_dbg(&port_dev->dev, "link state change\n");
4949af376a46SDan Williams 		usb_clear_port_feature(hdev, port1,
4950af376a46SDan Williams 				USB_PORT_FEAT_C_PORT_LINK_STATE);
4951af376a46SDan Williams 	}
4952af376a46SDan Williams 	if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4953af376a46SDan Williams 		dev_warn(&port_dev->dev, "config error\n");
4954af376a46SDan Williams 		usb_clear_port_feature(hdev, port1,
4955af376a46SDan Williams 				USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4956af376a46SDan Williams 	}
4957af376a46SDan Williams 
4958097a155fSDan Williams 	/* skip port actions that require the port to be powered on */
4959097a155fSDan Williams 	if (!pm_runtime_active(&port_dev->dev))
4960097a155fSDan Williams 		return;
4961097a155fSDan Williams 
4962af376a46SDan Williams 	if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
4963af376a46SDan Williams 		connect_change = 1;
4964af376a46SDan Williams 
4965af376a46SDan Williams 	/*
4966af376a46SDan Williams 	 * Warm reset a USB3 protocol port if it's in
4967af376a46SDan Williams 	 * SS.Inactive state.
4968af376a46SDan Williams 	 */
49693cd12f91SDan Williams 	if (hub_port_warm_reset_required(hub, port1, portstatus)) {
4970af376a46SDan Williams 		dev_dbg(&port_dev->dev, "do warm reset\n");
4971af376a46SDan Williams 		if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
4972af376a46SDan Williams 				|| udev->state == USB_STATE_NOTATTACHED) {
4973af376a46SDan Williams 			if (hub_port_reset(hub, port1, NULL,
4974af376a46SDan Williams 					HUB_BH_RESET_TIME, true) < 0)
4975af376a46SDan Williams 				hub_port_disable(hub, port1, 1);
4976af376a46SDan Williams 		} else
4977af376a46SDan Williams 			reset_device = 1;
4978af376a46SDan Williams 	}
4979af376a46SDan Williams 
4980af376a46SDan Williams 	/*
4981af376a46SDan Williams 	 * On disconnect USB3 protocol ports transit from U0 to
4982af376a46SDan Williams 	 * SS.Inactive to Rx.Detect. If this happens a warm-
4983af376a46SDan Williams 	 * reset is not needed, but a (re)connect may happen
498437ebb549SPetr Mladek 	 * before hub_wq runs and sees the disconnect, and the
4985af376a46SDan Williams 	 * device may be an unknown state.
4986af376a46SDan Williams 	 *
498737ebb549SPetr Mladek 	 * If the port went through SS.Inactive without hub_wq
4988af376a46SDan Williams 	 * seeing it the C_LINK_STATE change flag will be set,
4989af376a46SDan Williams 	 * and we reset the dev to put it in a known state.
4990af376a46SDan Williams 	 */
4991af376a46SDan Williams 	if (reset_device || (udev && hub_is_superspeed(hub->hdev)
4992af376a46SDan Williams 				&& (portchange & USB_PORT_STAT_C_LINK_STATE)
4993af376a46SDan Williams 				&& (portstatus & USB_PORT_STAT_CONNECTION))) {
49945c79a1e3SDan Williams 		usb_unlock_port(port_dev);
4995af376a46SDan Williams 		usb_lock_device(udev);
4996af376a46SDan Williams 		usb_reset_device(udev);
4997af376a46SDan Williams 		usb_unlock_device(udev);
49985c79a1e3SDan Williams 		usb_lock_port(port_dev);
4999af376a46SDan Williams 		connect_change = 0;
5000af376a46SDan Williams 	}
5001af376a46SDan Williams 
5002af376a46SDan Williams 	if (connect_change)
5003af376a46SDan Williams 		hub_port_connect_change(hub, port1, portstatus, portchange);
5004af376a46SDan Williams }
5005af376a46SDan Williams 
500632a69589SPetr Mladek static void hub_event(struct work_struct *work)
50071da177e4SLinus Torvalds {
50081da177e4SLinus Torvalds 	struct usb_device *hdev;
50091da177e4SLinus Torvalds 	struct usb_interface *intf;
50101da177e4SLinus Torvalds 	struct usb_hub *hub;
50111da177e4SLinus Torvalds 	struct device *hub_dev;
50121da177e4SLinus Torvalds 	u16 hubstatus;
50131da177e4SLinus Torvalds 	u16 hubchange;
50141da177e4SLinus Torvalds 	int i, ret;
50151da177e4SLinus Torvalds 
501632a69589SPetr Mladek 	hub = container_of(work, struct usb_hub, events);
50175d14f323SPetr Mladek 	hdev = hub->hdev;
5018e8054854SAlan Stern 	hub_dev = hub->intfdev;
5019e8054854SAlan Stern 	intf = to_usb_interface(hub_dev);
502032a69589SPetr Mladek 
502140f122f3SAlan Stern 	dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
50223bbc47d8SKrzysztof Mazur 			hdev->state, hdev->maxchild,
50231da177e4SLinus Torvalds 			/* NOTE: expects max 15 ports... */
50241da177e4SLinus Torvalds 			(u16) hub->change_bits[0],
502540f122f3SAlan Stern 			(u16) hub->event_bits[0]);
50261da177e4SLinus Torvalds 
50271da177e4SLinus Torvalds 	/* Lock the device, then check to see if we were
50281da177e4SLinus Torvalds 	 * disconnected while waiting for the lock to succeed. */
502906b84e8aSAlan Stern 	usb_lock_device(hdev);
5030e8054854SAlan Stern 	if (unlikely(hub->disconnected))
503132a69589SPetr Mladek 		goto out_hdev_lock;
50321da177e4SLinus Torvalds 
50331da177e4SLinus Torvalds 	/* If the hub has died, clean up after it */
50341da177e4SLinus Torvalds 	if (hdev->state == USB_STATE_NOTATTACHED) {
50357de18d8bSAlan Stern 		hub->error = -ENODEV;
50364330354fSAlan Stern 		hub_quiesce(hub, HUB_DISCONNECT);
503732a69589SPetr Mladek 		goto out_hdev_lock;
50381da177e4SLinus Torvalds 	}
50391da177e4SLinus Torvalds 
504040f122f3SAlan Stern 	/* Autoresume */
504140f122f3SAlan Stern 	ret = usb_autopm_get_interface(intf);
504240f122f3SAlan Stern 	if (ret) {
504340f122f3SAlan Stern 		dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
504432a69589SPetr Mladek 		goto out_hdev_lock;
504540f122f3SAlan Stern 	}
504640f122f3SAlan Stern 
504740f122f3SAlan Stern 	/* If this is an inactive hub, do nothing */
504840f122f3SAlan Stern 	if (hub->quiescing)
5049eb6e2924SPetr Mladek 		goto out_autopm;
50501da177e4SLinus Torvalds 
50511da177e4SLinus Torvalds 	if (hub->error) {
5052eb6e2924SPetr Mladek 		dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
50531da177e4SLinus Torvalds 
5054742120c6SMing Lei 		ret = usb_reset_device(hdev);
50551da177e4SLinus Torvalds 		if (ret) {
5056eb6e2924SPetr Mladek 			dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5057eb6e2924SPetr Mladek 			goto out_autopm;
50581da177e4SLinus Torvalds 		}
50591da177e4SLinus Torvalds 
50601da177e4SLinus Torvalds 		hub->nerrors = 0;
50611da177e4SLinus Torvalds 		hub->error = 0;
50621da177e4SLinus Torvalds 	}
50631da177e4SLinus Torvalds 
50641da177e4SLinus Torvalds 	/* deal with port status changes */
50653bbc47d8SKrzysztof Mazur 	for (i = 1; i <= hdev->maxchild; i++) {
5066097a155fSDan Williams 		struct usb_port *port_dev = hub->ports[i - 1];
5067a82b76f7SHans de Goede 
50685c79a1e3SDan Williams 		if (test_bit(i, hub->event_bits)
5069af376a46SDan Williams 				|| test_bit(i, hub->change_bits)
50705c79a1e3SDan Williams 				|| test_bit(i, hub->wakeup_bits)) {
50711da177e4SLinus Torvalds 			/*
5072097a155fSDan Williams 			 * The get_noresume and barrier ensure that if
5073097a155fSDan Williams 			 * the port was in the process of resuming, we
5074097a155fSDan Williams 			 * flush that work and keep the port active for
5075097a155fSDan Williams 			 * the duration of the port_event().  However,
5076097a155fSDan Williams 			 * if the port is runtime pm suspended
5077097a155fSDan Williams 			 * (powered-off), we leave it in that state, run
5078097a155fSDan Williams 			 * an abbreviated port_event(), and move on.
50791da177e4SLinus Torvalds 			 */
5080097a155fSDan Williams 			pm_runtime_get_noresume(&port_dev->dev);
5081097a155fSDan Williams 			pm_runtime_barrier(&port_dev->dev);
50825c79a1e3SDan Williams 			usb_lock_port(port_dev);
5083af376a46SDan Williams 			port_event(hub, i);
50845c79a1e3SDan Williams 			usb_unlock_port(port_dev);
5085097a155fSDan Williams 			pm_runtime_put_sync(&port_dev->dev);
50861da177e4SLinus Torvalds 		}
50871da177e4SLinus Torvalds 	}
50881da177e4SLinus Torvalds 
50891da177e4SLinus Torvalds 	/* deal with hub status changes */
50901da177e4SLinus Torvalds 	if (test_and_clear_bit(0, hub->event_bits) == 0)
50911da177e4SLinus Torvalds 		;	/* do nothing */
50921da177e4SLinus Torvalds 	else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
50931da177e4SLinus Torvalds 		dev_err(hub_dev, "get_hub_status failed\n");
50941da177e4SLinus Torvalds 	else {
50951da177e4SLinus Torvalds 		if (hubchange & HUB_CHANGE_LOCAL_POWER) {
50961da177e4SLinus Torvalds 			dev_dbg(hub_dev, "power change\n");
50971da177e4SLinus Torvalds 			clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
509855c52718SAlan Stern 			if (hubstatus & HUB_STATUS_LOCAL_POWER)
509955c52718SAlan Stern 				/* FIXME: Is this always true? */
510055c52718SAlan Stern 				hub->limited_power = 1;
5101403fae78Sjidong xiao 			else
5102403fae78Sjidong xiao 				hub->limited_power = 0;
51031da177e4SLinus Torvalds 		}
51041da177e4SLinus Torvalds 		if (hubchange & HUB_CHANGE_OVERCURRENT) {
5105752d57a8SPaul Bolle 			u16 status = 0;
5106752d57a8SPaul Bolle 			u16 unused;
5107752d57a8SPaul Bolle 
5108752d57a8SPaul Bolle 			dev_dbg(hub_dev, "over-current change\n");
51091da177e4SLinus Torvalds 			clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5110752d57a8SPaul Bolle 			msleep(500);	/* Cool down */
51118520f380SAlan Stern 			hub_power_on(hub, true);
5112752d57a8SPaul Bolle 			hub_hub_status(hub, &status, &unused);
5113752d57a8SPaul Bolle 			if (status & HUB_STATUS_OVERCURRENT)
5114eb6e2924SPetr Mladek 				dev_err(hub_dev, "over-current condition\n");
51151da177e4SLinus Torvalds 		}
51161da177e4SLinus Torvalds 	}
51171da177e4SLinus Torvalds 
5118eb6e2924SPetr Mladek out_autopm:
51198e4ceb38SAlan Stern 	/* Balance the usb_autopm_get_interface() above */
51208e4ceb38SAlan Stern 	usb_autopm_put_interface_no_suspend(intf);
512132a69589SPetr Mladek out_hdev_lock:
51221da177e4SLinus Torvalds 	usb_unlock_device(hdev);
512332a69589SPetr Mladek 
512432a69589SPetr Mladek 	/* Balance the stuff in kick_hub_wq() and allow autosuspend */
512532a69589SPetr Mladek 	usb_autopm_put_interface(intf);
5126e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
51271da177e4SLinus Torvalds }
51281da177e4SLinus Torvalds 
51291e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = {
5130e6f30deaSMing Lei     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5131e6f30deaSMing Lei 			| USB_DEVICE_ID_MATCH_INT_CLASS,
5132e6f30deaSMing Lei       .idVendor = USB_VENDOR_GENESYS_LOGIC,
5133e6f30deaSMing Lei       .bInterfaceClass = USB_CLASS_HUB,
5134e6f30deaSMing Lei       .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
51351da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
51361da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
51371da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
51381da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
51391da177e4SLinus Torvalds     { }						/* Terminating entry */
51401da177e4SLinus Torvalds };
51411da177e4SLinus Torvalds 
51421da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
51431da177e4SLinus Torvalds 
51441da177e4SLinus Torvalds static struct usb_driver hub_driver = {
51451da177e4SLinus Torvalds 	.name =		"hub",
51461da177e4SLinus Torvalds 	.probe =	hub_probe,
51471da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
51481da177e4SLinus Torvalds 	.suspend =	hub_suspend,
51491da177e4SLinus Torvalds 	.resume =	hub_resume,
5150f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
51517de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
51527de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
5153c532b29aSAndi Kleen 	.unlocked_ioctl = hub_ioctl,
51541da177e4SLinus Torvalds 	.id_table =	hub_id_table,
515540f122f3SAlan Stern 	.supports_autosuspend =	1,
51561da177e4SLinus Torvalds };
51571da177e4SLinus Torvalds 
51581da177e4SLinus Torvalds int usb_hub_init(void)
51591da177e4SLinus Torvalds {
51601da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
51611da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
51621da177e4SLinus Torvalds 			usbcore_name);
51631da177e4SLinus Torvalds 		return -1;
51641da177e4SLinus Torvalds 	}
51651da177e4SLinus Torvalds 
516632a69589SPetr Mladek 	/*
516732a69589SPetr Mladek 	 * The workqueue needs to be freezable to avoid interfering with
516832a69589SPetr Mladek 	 * USB-PERSIST port handover. Otherwise it might see that a full-speed
516932a69589SPetr Mladek 	 * device was gone before the EHCI controller had handed its port
517032a69589SPetr Mladek 	 * over to the companion full-speed controller.
517132a69589SPetr Mladek 	 */
5172638139ebSPetr Mladek 	hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
517332a69589SPetr Mladek 	if (hub_wq)
51741da177e4SLinus Torvalds 		return 0;
51751da177e4SLinus Torvalds 
51761da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
51771da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
517832a69589SPetr Mladek 	pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
51791da177e4SLinus Torvalds 
51801da177e4SLinus Torvalds 	return -1;
51811da177e4SLinus Torvalds }
51821da177e4SLinus Torvalds 
51831da177e4SLinus Torvalds void usb_hub_cleanup(void)
51841da177e4SLinus Torvalds {
518532a69589SPetr Mladek 	destroy_workqueue(hub_wq);
51861da177e4SLinus Torvalds 
51871da177e4SLinus Torvalds 	/*
51881da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
51891da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
51901da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
51911da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
51921da177e4SLinus Torvalds 	 * individual hub resources. -greg
51931da177e4SLinus Torvalds 	 */
51941da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
51951da177e4SLinus Torvalds } /* usb_hub_cleanup() */
51961da177e4SLinus Torvalds 
5197eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
5198e3376d6cSXenia Ragiadakou 		struct usb_device_descriptor *old_device_descriptor,
5199e3376d6cSXenia Ragiadakou 		struct usb_host_bos *old_bos)
52001da177e4SLinus Torvalds {
5201eb764c4bSAlan Stern 	int		changed = 0;
52021da177e4SLinus Torvalds 	unsigned	index;
5203eb764c4bSAlan Stern 	unsigned	serial_len = 0;
5204eb764c4bSAlan Stern 	unsigned	len;
5205eb764c4bSAlan Stern 	unsigned	old_length;
5206eb764c4bSAlan Stern 	int		length;
5207eb764c4bSAlan Stern 	char		*buf;
52081da177e4SLinus Torvalds 
5209eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
5210eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
5211eb764c4bSAlan Stern 		return 1;
5212eb764c4bSAlan Stern 
5213e3376d6cSXenia Ragiadakou 	if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5214e3376d6cSXenia Ragiadakou 		return 1;
5215e3376d6cSXenia Ragiadakou 	if (udev->bos) {
5216b9a10481SXenia Ragiadakou 		len = le16_to_cpu(udev->bos->desc->wTotalLength);
5217b9a10481SXenia Ragiadakou 		if (len != le16_to_cpu(old_bos->desc->wTotalLength))
5218e3376d6cSXenia Ragiadakou 			return 1;
5219b9a10481SXenia Ragiadakou 		if (memcmp(udev->bos->desc, old_bos->desc, len))
5220e3376d6cSXenia Ragiadakou 			return 1;
5221e3376d6cSXenia Ragiadakou 	}
5222e3376d6cSXenia Ragiadakou 
5223eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
5224eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
5225eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
5226eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
5227eb764c4bSAlan Stern 	 * different flash card of the same brand).
5228eb764c4bSAlan Stern 	 */
5229eb764c4bSAlan Stern 	if (udev->serial)
5230eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
5231eb764c4bSAlan Stern 
5232eb764c4bSAlan Stern 	len = serial_len;
52331da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5234eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5235eb764c4bSAlan Stern 		len = max(len, old_length);
52361da177e4SLinus Torvalds 	}
5237eb764c4bSAlan Stern 
52380cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
52391da177e4SLinus Torvalds 	if (buf == NULL) {
52401da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
52411da177e4SLinus Torvalds 		/* assume the worst */
52421da177e4SLinus Torvalds 		return 1;
52431da177e4SLinus Torvalds 	}
52441da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5245eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
52461da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
52471da177e4SLinus Torvalds 				old_length);
5248eb764c4bSAlan Stern 		if (length != old_length) {
52491da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
52501da177e4SLinus Torvalds 					index, length);
5251eb764c4bSAlan Stern 			changed = 1;
52521da177e4SLinus Torvalds 			break;
52531da177e4SLinus Torvalds 		}
52541da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
52551da177e4SLinus Torvalds 				!= 0) {
52561da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
5257eb764c4bSAlan Stern 				index,
5258eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
5259eb764c4bSAlan Stern 					bConfigurationValue);
5260eb764c4bSAlan Stern 			changed = 1;
52611da177e4SLinus Torvalds 			break;
52621da177e4SLinus Torvalds 		}
52631da177e4SLinus Torvalds 	}
5264eb764c4bSAlan Stern 
5265eb764c4bSAlan Stern 	if (!changed && serial_len) {
5266eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
5267eb764c4bSAlan Stern 				buf, serial_len);
5268eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
5269eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
5270eb764c4bSAlan Stern 					length);
5271eb764c4bSAlan Stern 			changed = 1;
5272eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
5273eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
5274eb764c4bSAlan Stern 			changed = 1;
5275eb764c4bSAlan Stern 		}
5276eb764c4bSAlan Stern 	}
5277eb764c4bSAlan Stern 
52781da177e4SLinus Torvalds 	kfree(buf);
5279eb764c4bSAlan Stern 	return changed;
52801da177e4SLinus Torvalds }
52811da177e4SLinus Torvalds 
52821da177e4SLinus Torvalds /**
5283742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
52841da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
52851da177e4SLinus Torvalds  *
528679efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
528779efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
5288742120c6SMing Lei  * Use usb_reset_device() instead.
52891da177e4SLinus Torvalds  *
52901da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
52911da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
52921da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
52931da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
529437ebb549SPetr Mladek  * telling hub_wq to pretend the device has been disconnected and then
52951da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
52961da177e4SLinus Torvalds  * re-enumerated and probed all over again.
52971da177e4SLinus Torvalds  *
5298626f090cSYacine Belkadi  * Return: 0 if the reset succeeded, -ENODEV if the device has been
52991da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
53001da177e4SLinus Torvalds  * if the reset wasn't even attempted.
53011da177e4SLinus Torvalds  *
5302626f090cSYacine Belkadi  * Note:
53035c79a1e3SDan Williams  * The caller must own the device lock and the port lock, the latter is
53045c79a1e3SDan Williams  * taken by usb_reset_device().  For example, it's safe to use
53055c79a1e3SDan Williams  * usb_reset_device() from a driver probe() routine after downloading
53065c79a1e3SDan Williams  * new firmware.  For calls that might not occur during probe(), drivers
53075c79a1e3SDan Williams  * should lock the device using usb_lock_device_for_reset().
53086bc6cff5SAlan Stern  *
53096bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
53106bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
53116bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
53125c79a1e3SDan Williams  * usb_autopm_resume_device(), thereby preventing any unwanted
53135c79a1e3SDan Williams  * autoresume.  The autoresume handler is expected to have already
53145c79a1e3SDan Williams  * acquired the port lock before calling this routine.
53151da177e4SLinus Torvalds  */
5316742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
53171da177e4SLinus Torvalds {
53181da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
53191da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
53203f0479e0SSarah Sharp 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
53211da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
5322e3376d6cSXenia Ragiadakou 	struct usb_host_bos		*bos;
53237a7b562dSHans de Goede 	int				i, j, ret = 0;
532412c3da34SAlan Stern 	int				port1 = udev->portnum;
53251da177e4SLinus Torvalds 
53261da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
53271da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
53281da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
53291da177e4SLinus Torvalds 				udev->state);
53301da177e4SLinus Torvalds 		return -EINVAL;
53311da177e4SLinus Torvalds 	}
53321da177e4SLinus Torvalds 
53335c79a1e3SDan Williams 	if (!parent_hdev)
53341da177e4SLinus Torvalds 		return -EISDIR;
53355c79a1e3SDan Williams 
5336ad493e5eSLan Tianyu 	parent_hub = usb_hub_to_struct_hub(parent_hdev);
53371da177e4SLinus Torvalds 
5338dcc01c08SSarah Sharp 	/* Disable USB2 hardware LPM.
5339dcc01c08SSarah Sharp 	 * It will be re-enabled by the enumeration process.
5340dcc01c08SSarah Sharp 	 */
5341dcc01c08SSarah Sharp 	if (udev->usb2_hw_lpm_enabled == 1)
5342dcc01c08SSarah Sharp 		usb_set_usb2_hardware_lpm(udev, 0);
5343dcc01c08SSarah Sharp 
5344e3376d6cSXenia Ragiadakou 	bos = udev->bos;
5345e3376d6cSXenia Ragiadakou 	udev->bos = NULL;
5346e3376d6cSXenia Ragiadakou 
5347f74631e3SSarah Sharp 	/* Disable LPM and LTM while we reset the device and reinstall the alt
5348f74631e3SSarah Sharp 	 * settings.  Device-initiated LPM settings, and system exit latency
5349f74631e3SSarah Sharp 	 * settings are cleared when the device is reset, so we have to set
5350f74631e3SSarah Sharp 	 * them up again.
53516d1d0513SSarah Sharp 	 */
53526d1d0513SSarah Sharp 	ret = usb_unlocked_disable_lpm(udev);
53536d1d0513SSarah Sharp 	if (ret) {
53546d1d0513SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
53556d1d0513SSarah Sharp 		goto re_enumerate;
53566d1d0513SSarah Sharp 	}
5357f74631e3SSarah Sharp 	ret = usb_disable_ltm(udev);
5358f74631e3SSarah Sharp 	if (ret) {
5359f74631e3SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5360f74631e3SSarah Sharp 				__func__);
5361f74631e3SSarah Sharp 		goto re_enumerate;
5362f74631e3SSarah Sharp 	}
53636d1d0513SSarah Sharp 
53641da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
53651da177e4SLinus Torvalds 
53661da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
53671da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
5368fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
53691da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
5370dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
53711da177e4SLinus Torvalds 			break;
53721da177e4SLinus Torvalds 	}
5373d5cbad4bSAlan Stern 
53741da177e4SLinus Torvalds 	if (ret < 0)
53751da177e4SLinus Torvalds 		goto re_enumerate;
53761da177e4SLinus Torvalds 
53771da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
5378e3376d6cSXenia Ragiadakou 	if (descriptors_changed(udev, &descriptor, bos)) {
53791da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
53801da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
53811da177e4SLinus Torvalds 		goto re_enumerate;
53821da177e4SLinus Torvalds 	}
53831da177e4SLinus Torvalds 
53844fe0387aSAlan Stern 	/* Restore the device's previous configuration */
53851da177e4SLinus Torvalds 	if (!udev->actconfig)
53861da177e4SLinus Torvalds 		goto done;
53873f0479e0SSarah Sharp 
5388d673bfcbSSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
53893f0479e0SSarah Sharp 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
53903f0479e0SSarah Sharp 	if (ret < 0) {
53913f0479e0SSarah Sharp 		dev_warn(&udev->dev,
53923f0479e0SSarah Sharp 				"Busted HC?  Not enough HCD resources for "
53933f0479e0SSarah Sharp 				"old configuration.\n");
5394d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
53953f0479e0SSarah Sharp 		goto re_enumerate;
53963f0479e0SSarah Sharp 	}
53971da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
53981da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
53991da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
54001da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
54011da177e4SLinus Torvalds 	if (ret < 0) {
54021da177e4SLinus Torvalds 		dev_err(&udev->dev,
54031da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
54041da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
5405d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
54061da177e4SLinus Torvalds 		goto re_enumerate;
54071da177e4SLinus Torvalds 	}
5408d673bfcbSSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
54091da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
54101da177e4SLinus Torvalds 
54114fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
54124fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
54134fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
54144fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
54154fe0387aSAlan Stern 	 * endpoint state.
54164fe0387aSAlan Stern 	 */
54171da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
54183f0479e0SSarah Sharp 		struct usb_host_config *config = udev->actconfig;
54193f0479e0SSarah Sharp 		struct usb_interface *intf = config->interface[i];
54201da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
54211da177e4SLinus Torvalds 
54221da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
54234fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
54244fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
54254fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
54264fe0387aSAlan Stern 			ret = 0;
54274fe0387aSAlan Stern 		} else {
542804a723eaSSarah Sharp 			/* Let the bandwidth allocation function know that this
542904a723eaSSarah Sharp 			 * device has been reset, and it will have to use
543004a723eaSSarah Sharp 			 * alternate setting 0 as the current alternate setting.
54313f0479e0SSarah Sharp 			 */
543204a723eaSSarah Sharp 			intf->resetting_device = 1;
54331da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
54341da177e4SLinus Torvalds 					desc->bAlternateSetting);
543504a723eaSSarah Sharp 			intf->resetting_device = 0;
54364fe0387aSAlan Stern 		}
54371da177e4SLinus Torvalds 		if (ret < 0) {
54381da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
54391da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
54401da177e4SLinus Torvalds 				desc->bInterfaceNumber,
54411da177e4SLinus Torvalds 				desc->bAlternateSetting,
54421da177e4SLinus Torvalds 				ret);
54431da177e4SLinus Torvalds 			goto re_enumerate;
54441da177e4SLinus Torvalds 		}
54457a7b562dSHans de Goede 		/* Resetting also frees any allocated streams */
54467a7b562dSHans de Goede 		for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
54477a7b562dSHans de Goede 			intf->cur_altsetting->endpoint[j].streams = 0;
54481da177e4SLinus Torvalds 	}
54491da177e4SLinus Torvalds 
54501da177e4SLinus Torvalds done:
5451f74631e3SSarah Sharp 	/* Now that the alt settings are re-installed, enable LTM and LPM. */
5452de68bab4SSarah Sharp 	usb_set_usb2_hardware_lpm(udev, 1);
545378d9a487SAlan Stern 	usb_unlocked_enable_lpm(udev);
5454f74631e3SSarah Sharp 	usb_enable_ltm(udev);
5455e3376d6cSXenia Ragiadakou 	usb_release_bos_descriptor(udev);
5456e3376d6cSXenia Ragiadakou 	udev->bos = bos;
54571da177e4SLinus Torvalds 	return 0;
54581da177e4SLinus Torvalds 
54591da177e4SLinus Torvalds re_enumerate:
54606d1d0513SSarah Sharp 	/* LPM state doesn't matter when we're about to destroy the device. */
54611da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
5462e3376d6cSXenia Ragiadakou 	usb_release_bos_descriptor(udev);
5463e3376d6cSXenia Ragiadakou 	udev->bos = bos;
54641da177e4SLinus Torvalds 	return -ENODEV;
54651da177e4SLinus Torvalds }
54661da177e4SLinus Torvalds 
54671da177e4SLinus Torvalds /**
54681da177e4SLinus Torvalds  * usb_reset_device - warn interface drivers and perform a USB port reset
54691da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
54701da177e4SLinus Torvalds  *
547179efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
547279efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
5473742120c6SMing Lei  * the reset is over (using their post_reset method).
547479efa097SAlan Stern  *
5475626f090cSYacine Belkadi  * Return: The same as for usb_reset_and_verify_device().
547679efa097SAlan Stern  *
5477626f090cSYacine Belkadi  * Note:
547879efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
547979efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
548079efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
5481742120c6SMing Lei  * the device using usb_lock_device_for_reset().
548279efa097SAlan Stern  *
548379efa097SAlan Stern  * If an interface is currently being probed or disconnected, we assume
548479efa097SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
548579efa097SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
548679efa097SAlan Stern  * we attempt to unbind it and rebind afterward.
548779efa097SAlan Stern  */
5488742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
548979efa097SAlan Stern {
549079efa097SAlan Stern 	int ret;
5491852c4b43SAlan Stern 	int i;
54924d769defSMing Lei 	unsigned int noio_flag;
54935c79a1e3SDan Williams 	struct usb_port *port_dev;
549479efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
54955c79a1e3SDan Williams 	struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
549679efa097SAlan Stern 
549779efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
549879efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
549979efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
550079efa097SAlan Stern 				udev->state);
550179efa097SAlan Stern 		return -EINVAL;
550279efa097SAlan Stern 	}
550379efa097SAlan Stern 
55045c79a1e3SDan Williams 	if (!udev->parent) {
55055c79a1e3SDan Williams 		/* this requires hcd-specific logic; see ohci_restart() */
55065c79a1e3SDan Williams 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
55075c79a1e3SDan Williams 		return -EISDIR;
55085c79a1e3SDan Williams 	}
55095c79a1e3SDan Williams 
55105c79a1e3SDan Williams 	port_dev = hub->ports[udev->portnum - 1];
55115c79a1e3SDan Williams 
55124d769defSMing Lei 	/*
55134d769defSMing Lei 	 * Don't allocate memory with GFP_KERNEL in current
55144d769defSMing Lei 	 * context to avoid possible deadlock if usb mass
55154d769defSMing Lei 	 * storage interface or usbnet interface(iSCSI case)
55164d769defSMing Lei 	 * is included in current configuration. The easist
55174d769defSMing Lei 	 * approach is to do it for every device reset,
55184d769defSMing Lei 	 * because the device 'memalloc_noio' flag may have
55194d769defSMing Lei 	 * not been set before reseting the usb device.
55204d769defSMing Lei 	 */
55214d769defSMing Lei 	noio_flag = memalloc_noio_save();
55224d769defSMing Lei 
5523645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
552494fcda1fSAlan Stern 	usb_autoresume_device(udev);
5525645daaabSAlan Stern 
552679efa097SAlan Stern 	if (config) {
5527852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
5528852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
552979efa097SAlan Stern 			struct usb_driver *drv;
553078d9a487SAlan Stern 			int unbind = 0;
553179efa097SAlan Stern 
5532852c4b43SAlan Stern 			if (cintf->dev.driver) {
553379efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
553478d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
553578d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
553678d9a487SAlan Stern 				else if (cintf->condition ==
553778d9a487SAlan Stern 						USB_INTERFACE_BOUND)
553878d9a487SAlan Stern 					unbind = 1;
553978d9a487SAlan Stern 				if (unbind)
554078d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
554179efa097SAlan Stern 			}
554279efa097SAlan Stern 		}
554379efa097SAlan Stern 	}
554479efa097SAlan Stern 
55455c79a1e3SDan Williams 	usb_lock_port(port_dev);
5546742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
55475c79a1e3SDan Williams 	usb_unlock_port(port_dev);
554879efa097SAlan Stern 
554979efa097SAlan Stern 	if (config) {
5550852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
5551852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
555279efa097SAlan Stern 			struct usb_driver *drv;
555378d9a487SAlan Stern 			int rebind = cintf->needs_binding;
555479efa097SAlan Stern 
555578d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
555679efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
555779efa097SAlan Stern 				if (drv->post_reset)
555878d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
555978d9a487SAlan Stern 				else if (cintf->condition ==
556078d9a487SAlan Stern 						USB_INTERFACE_BOUND)
556178d9a487SAlan Stern 					rebind = 1;
55626aec044cSAlan Stern 				if (rebind)
55636aec044cSAlan Stern 					cintf->needs_binding = 1;
556479efa097SAlan Stern 			}
556579efa097SAlan Stern 		}
55666aec044cSAlan Stern 		usb_unbind_and_rebind_marked_interfaces(udev);
556779efa097SAlan Stern 	}
556879efa097SAlan Stern 
556994fcda1fSAlan Stern 	usb_autosuspend_device(udev);
55704d769defSMing Lei 	memalloc_noio_restore(noio_flag);
557179efa097SAlan Stern 	return ret;
557279efa097SAlan Stern }
5573742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
5574dc023dceSInaky Perez-Gonzalez 
5575dc023dceSInaky Perez-Gonzalez 
5576dc023dceSInaky Perez-Gonzalez /**
5577dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
5578dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
5579dc023dceSInaky Perez-Gonzalez  *
5580dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
5581dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
5582dc023dceSInaky Perez-Gonzalez  *
5583dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
5584dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
5585dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
5586dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
5587dc023dceSInaky Perez-Gonzalez  *
5588dc023dceSInaky Perez-Gonzalez  * Corner cases:
5589dc023dceSInaky Perez-Gonzalez  *
5590dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
5591dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
5592dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
5593dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
5594dc023dceSInaky Perez-Gonzalez  *
5595dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
5596dc023dceSInaky Perez-Gonzalez  *   be cancelled.
5597dc023dceSInaky Perez-Gonzalez  *
5598dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
5599dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
5600dc023dceSInaky Perez-Gonzalez  *   cancelled.
5601dc023dceSInaky Perez-Gonzalez  *
5602dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
5603dc023dceSInaky Perez-Gonzalez  * does its own.
5604dc023dceSInaky Perez-Gonzalez  *
5605dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
5606dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
5607dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
5608dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
5609dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
5610dc023dceSInaky Perez-Gonzalez  *     are referenced.
5611dc023dceSInaky Perez-Gonzalez  */
5612dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
5613dc023dceSInaky Perez-Gonzalez {
5614dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
5615dc023dceSInaky Perez-Gonzalez }
5616dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
5617ff823c79SLan Tianyu 
5618ff823c79SLan Tianyu /**
5619ff823c79SLan Tianyu  * usb_hub_find_child - Get the pointer of child device
5620ff823c79SLan Tianyu  * attached to the port which is specified by @port1.
5621ff823c79SLan Tianyu  * @hdev: USB device belonging to the usb hub
5622ff823c79SLan Tianyu  * @port1: port num to indicate which port the child device
5623ff823c79SLan Tianyu  *	is attached to.
5624ff823c79SLan Tianyu  *
5625ff823c79SLan Tianyu  * USB drivers call this function to get hub's child device
5626ff823c79SLan Tianyu  * pointer.
5627ff823c79SLan Tianyu  *
5628626f090cSYacine Belkadi  * Return: %NULL if input param is invalid and
5629ff823c79SLan Tianyu  * child's usb_device pointer if non-NULL.
5630ff823c79SLan Tianyu  */
5631ff823c79SLan Tianyu struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5632ff823c79SLan Tianyu 		int port1)
5633ff823c79SLan Tianyu {
5634ad493e5eSLan Tianyu 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
5635ff823c79SLan Tianyu 
5636ff823c79SLan Tianyu 	if (port1 < 1 || port1 > hdev->maxchild)
5637ff823c79SLan Tianyu 		return NULL;
5638ff823c79SLan Tianyu 	return hub->ports[port1 - 1]->child;
5639ff823c79SLan Tianyu }
5640ff823c79SLan Tianyu EXPORT_SYMBOL_GPL(usb_hub_find_child);
5641d5575424SLan Tianyu 
5642d2123fd9SLan Tianyu void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5643d2123fd9SLan Tianyu 		struct usb_hub_descriptor *desc)
5644d2123fd9SLan Tianyu {
5645d99f6b41SDan Williams 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
5646d2123fd9SLan Tianyu 	enum usb_port_connect_type connect_type;
5647d2123fd9SLan Tianyu 	int i;
5648d2123fd9SLan Tianyu 
5649d99f6b41SDan Williams 	if (!hub)
5650d99f6b41SDan Williams 		return;
5651d99f6b41SDan Williams 
5652d2123fd9SLan Tianyu 	if (!hub_is_superspeed(hdev)) {
5653d2123fd9SLan Tianyu 		for (i = 1; i <= hdev->maxchild; i++) {
5654d99f6b41SDan Williams 			struct usb_port *port_dev = hub->ports[i - 1];
5655d2123fd9SLan Tianyu 
5656d99f6b41SDan Williams 			connect_type = port_dev->connect_type;
5657d2123fd9SLan Tianyu 			if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5658d2123fd9SLan Tianyu 				u8 mask = 1 << (i%8);
5659d2123fd9SLan Tianyu 
5660d2123fd9SLan Tianyu 				if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
5661d99f6b41SDan Williams 					dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
5662d2123fd9SLan Tianyu 					desc->u.hs.DeviceRemovable[i/8]	|= mask;
5663d2123fd9SLan Tianyu 				}
5664d2123fd9SLan Tianyu 			}
5665d2123fd9SLan Tianyu 		}
5666d2123fd9SLan Tianyu 	} else {
5667d2123fd9SLan Tianyu 		u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5668d2123fd9SLan Tianyu 
5669d2123fd9SLan Tianyu 		for (i = 1; i <= hdev->maxchild; i++) {
5670d99f6b41SDan Williams 			struct usb_port *port_dev = hub->ports[i - 1];
5671d2123fd9SLan Tianyu 
5672d99f6b41SDan Williams 			connect_type = port_dev->connect_type;
5673d2123fd9SLan Tianyu 			if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5674d2123fd9SLan Tianyu 				u16 mask = 1 << i;
5675d2123fd9SLan Tianyu 
5676d2123fd9SLan Tianyu 				if (!(port_removable & mask)) {
5677d99f6b41SDan Williams 					dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
5678d2123fd9SLan Tianyu 					port_removable |= mask;
5679d2123fd9SLan Tianyu 				}
5680d2123fd9SLan Tianyu 			}
5681d2123fd9SLan Tianyu 		}
5682d2123fd9SLan Tianyu 
5683d2123fd9SLan Tianyu 		desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5684d2123fd9SLan Tianyu 	}
5685d2123fd9SLan Tianyu }
5686d2123fd9SLan Tianyu 
5687d5575424SLan Tianyu #ifdef CONFIG_ACPI
5688d5575424SLan Tianyu /**
5689d5575424SLan Tianyu  * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5690d5575424SLan Tianyu  * @hdev: USB device belonging to the usb hub
5691d5575424SLan Tianyu  * @port1: port num of the port
5692d5575424SLan Tianyu  *
5693626f090cSYacine Belkadi  * Return: Port's acpi handle if successful, %NULL if params are
5694626f090cSYacine Belkadi  * invalid.
5695d5575424SLan Tianyu  */
5696d5575424SLan Tianyu acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5697d5575424SLan Tianyu 	int port1)
5698d5575424SLan Tianyu {
5699ad493e5eSLan Tianyu 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
5700d5575424SLan Tianyu 
570141341261SMathias Nyman 	if (!hub)
570241341261SMathias Nyman 		return NULL;
570341341261SMathias Nyman 
57043a83f992SRafael J. Wysocki 	return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5705d5575424SLan Tianyu }
5706d5575424SLan Tianyu #endif
5707