xref: /openbmc/linux/drivers/usb/core/hub.c (revision 1c7439c6)
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>
259c8d6178Sakpm@osdl.org #include <linux/kthread.h>
264186ecf8SArjan van de Ven #include <linux/mutex.h>
277dfb7103SNigel Cunningham #include <linux/freezer.h>
28b04b3156STheodore Ts'o #include <linux/random.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <asm/uaccess.h>
311da177e4SLinus Torvalds #include <asm/byteorder.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include "usb.h"
341da177e4SLinus Torvalds 
35f2a383e4SGreg Kroah-Hartman /* if we are in debug mode, always announce new devices */
36f2a383e4SGreg Kroah-Hartman #ifdef DEBUG
37f2a383e4SGreg Kroah-Hartman #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38f2a383e4SGreg Kroah-Hartman #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
39f2a383e4SGreg Kroah-Hartman #endif
40f2a383e4SGreg Kroah-Hartman #endif
41f2a383e4SGreg Kroah-Hartman 
42e6f30deaSMing Lei #define USB_VENDOR_GENESYS_LOGIC		0x05e3
43e6f30deaSMing Lei #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND	0x01
44e6f30deaSMing Lei 
45fa2a9566SLan Tianyu struct usb_port {
46ff823c79SLan Tianyu 	struct usb_device *child;
47fa2a9566SLan Tianyu 	struct device dev;
48fa2a9566SLan Tianyu 	struct dev_state *port_owner;
4905f91689SLan Tianyu 	enum usb_port_connect_type connect_type;
50fa2a9566SLan Tianyu };
51fa2a9566SLan Tianyu 
521bb5f66bSAlan Stern struct usb_hub {
531bb5f66bSAlan Stern 	struct device		*intfdev;	/* the "interface" device */
541bb5f66bSAlan Stern 	struct usb_device	*hdev;
55e8054854SAlan Stern 	struct kref		kref;
561bb5f66bSAlan Stern 	struct urb		*urb;		/* for interrupt polling pipe */
571bb5f66bSAlan Stern 
581bb5f66bSAlan Stern 	/* buffer for urb ... with extra space in case of babble */
591bb5f66bSAlan Stern 	char			(*buffer)[8];
601bb5f66bSAlan Stern 	union {
611bb5f66bSAlan Stern 		struct usb_hub_status	hub;
621bb5f66bSAlan Stern 		struct usb_port_status	port;
631bb5f66bSAlan Stern 	}			*status;	/* buffer for status reports */
64db90e7a1SAlan Stern 	struct mutex		status_mutex;	/* for the status buffer */
651bb5f66bSAlan Stern 
661bb5f66bSAlan Stern 	int			error;		/* last reported error */
671bb5f66bSAlan Stern 	int			nerrors;	/* track consecutive errors */
681bb5f66bSAlan Stern 
691bb5f66bSAlan Stern 	struct list_head	event_list;	/* hubs w/data or errs ready */
701bb5f66bSAlan Stern 	unsigned long		event_bits[1];	/* status change bitmask */
711bb5f66bSAlan Stern 	unsigned long		change_bits[1];	/* ports with logical connect
721bb5f66bSAlan Stern 							status change */
731bb5f66bSAlan Stern 	unsigned long		busy_bits[1];	/* ports being reset or
741bb5f66bSAlan Stern 							resumed */
75253e0572SAlan Stern 	unsigned long		removed_bits[1]; /* ports with a "removed"
76253e0572SAlan Stern 							device present */
774ee823b8SSarah Sharp 	unsigned long		wakeup_bits[1];	/* ports that have signaled
784ee823b8SSarah Sharp 							remote wakeup */
791bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
801bb5f66bSAlan Stern #error event_bits[] is too short!
811bb5f66bSAlan Stern #endif
821bb5f66bSAlan Stern 
831bb5f66bSAlan Stern 	struct usb_hub_descriptor *descriptor;	/* class descriptor */
841bb5f66bSAlan Stern 	struct usb_tt		tt;		/* Transaction Translator */
851bb5f66bSAlan Stern 
861bb5f66bSAlan Stern 	unsigned		mA_per_port;	/* current for each child */
871bb5f66bSAlan Stern 
881bb5f66bSAlan Stern 	unsigned		limited_power:1;
891bb5f66bSAlan Stern 	unsigned		quiescing:1;
90e8054854SAlan Stern 	unsigned		disconnected:1;
911bb5f66bSAlan Stern 
92e6f30deaSMing Lei 	unsigned		quirk_check_port_auto_suspend:1;
93e6f30deaSMing Lei 
941bb5f66bSAlan Stern 	unsigned		has_indicators:1;
951bb5f66bSAlan Stern 	u8			indicator[USB_MAXCHILDREN];
966d5aefb8SDavid Howells 	struct delayed_work	leds;
978520f380SAlan Stern 	struct delayed_work	init_work;
98fa2a9566SLan Tianyu 	struct usb_port		**ports;
991bb5f66bSAlan Stern };
1001bb5f66bSAlan Stern 
101dbe79bbeSJohn Youn static inline int hub_is_superspeed(struct usb_device *hdev)
102dbe79bbeSJohn Youn {
1037bf01185SAman Deep 	return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
104dbe79bbeSJohn Youn }
1051bb5f66bSAlan Stern 
106fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members
1079ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
1081da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
1091da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* khubd's worklist and its lock */
1121da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
1131da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* Wakes up khubd */
1161da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
1171da177e4SLinus Torvalds 
1189c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
12190ab5ee9SRusty Russell static bool blinkenlights = 0;
1221da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1231da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
126fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
127fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
128fd7c519dSJaroslav Kysela  */
129fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
130fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
131fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
132b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
133b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
134b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
135fd7c519dSJaroslav Kysela 
136fd7c519dSJaroslav Kysela /*
1371da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1381da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1391da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1401da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1411da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1421da177e4SLinus Torvalds  * following module parameters.
1431da177e4SLinus Torvalds  *
1441da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1451da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1461da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1471da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1481da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1491da177e4SLinus Torvalds  */
15090ab5ee9SRusty Russell static bool old_scheme_first = 0;
1511da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1521da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1531da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1541da177e4SLinus Torvalds 
15590ab5ee9SRusty Russell static bool use_both_schemes = 1;
1561da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1571da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1581da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1591da177e4SLinus Torvalds 		"first one fails");
1601da177e4SLinus Torvalds 
16132fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
16232fe0198SAlan Stern  * port reset on some companion controllers.
16332fe0198SAlan Stern  */
16432fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
16532fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
16632fe0198SAlan Stern 
167948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
168948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
169948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
170948fea37SAlan Stern 
171fa2a9566SLan Tianyu #define to_usb_port(_dev) \
172fa2a9566SLan Tianyu 	container_of(_dev, struct usb_port, dev)
1731da177e4SLinus Torvalds 
174742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
175742120c6SMing Lei 
176131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus)
1771da177e4SLinus Torvalds {
178131dec34SSarah Sharp 	if (hub_is_superspeed(hub->hdev))
179131dec34SSarah Sharp 		return "5.0 Gb/s";
180288ead45SAlan Stern 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1811da177e4SLinus Torvalds     		return "480 Mb/s";
182288ead45SAlan Stern 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1831da177e4SLinus Torvalds 		return "1.5 Mb/s";
1841da177e4SLinus Torvalds 	else
1851da177e4SLinus Torvalds 		return "12 Mb/s";
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
18925118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1901da177e4SLinus Torvalds {
191ff823c79SLan Tianyu 	if (!hdev || !hdev->actconfig || !hdev->maxchild)
19225118084SAlan Stern 		return NULL;
1931da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
196d9b2099cSSarah Sharp static int usb_device_supports_lpm(struct usb_device *udev)
197d9b2099cSSarah Sharp {
198d9b2099cSSarah Sharp 	/* USB 2.1 (and greater) devices indicate LPM support through
199d9b2099cSSarah Sharp 	 * their USB 2.0 Extended Capabilities BOS descriptor.
200d9b2099cSSarah Sharp 	 */
201d9b2099cSSarah Sharp 	if (udev->speed == USB_SPEED_HIGH) {
202d9b2099cSSarah Sharp 		if (udev->bos->ext_cap &&
203d9b2099cSSarah Sharp 			(USB_LPM_SUPPORT &
204d9b2099cSSarah Sharp 			 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
205d9b2099cSSarah Sharp 			return 1;
206d9b2099cSSarah Sharp 		return 0;
207d9b2099cSSarah Sharp 	}
20851e0a012SSarah Sharp 
20951e0a012SSarah Sharp 	/* All USB 3.0 must support LPM, but we need their max exit latency
21051e0a012SSarah Sharp 	 * information from the SuperSpeed Extended Capabilities BOS descriptor.
21151e0a012SSarah Sharp 	 */
21251e0a012SSarah Sharp 	if (!udev->bos->ss_cap) {
21351e0a012SSarah Sharp 		dev_warn(&udev->dev, "No LPM exit latency info found.  "
21451e0a012SSarah Sharp 				"Power management will be impacted.\n");
215d9b2099cSSarah Sharp 		return 0;
216d9b2099cSSarah Sharp 	}
21751e0a012SSarah Sharp 	if (udev->parent->lpm_capable)
21851e0a012SSarah Sharp 		return 1;
21951e0a012SSarah Sharp 
22051e0a012SSarah Sharp 	dev_warn(&udev->dev, "Parent hub missing LPM exit latency info.  "
22151e0a012SSarah Sharp 			"Power management will be impacted.\n");
22251e0a012SSarah Sharp 	return 0;
22351e0a012SSarah Sharp }
22451e0a012SSarah Sharp 
22551e0a012SSarah Sharp /*
22651e0a012SSarah Sharp  * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
22751e0a012SSarah Sharp  * either U1 or U2.
22851e0a012SSarah Sharp  */
22951e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev,
23051e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params,
23151e0a012SSarah Sharp 		unsigned int udev_exit_latency,
23251e0a012SSarah Sharp 		struct usb_hub *hub,
23351e0a012SSarah Sharp 		struct usb3_lpm_parameters *hub_lpm_params,
23451e0a012SSarah Sharp 		unsigned int hub_exit_latency)
23551e0a012SSarah Sharp {
23651e0a012SSarah Sharp 	unsigned int total_mel;
23751e0a012SSarah Sharp 	unsigned int device_mel;
23851e0a012SSarah Sharp 	unsigned int hub_mel;
23951e0a012SSarah Sharp 
24051e0a012SSarah Sharp 	/*
24151e0a012SSarah Sharp 	 * Calculate the time it takes to transition all links from the roothub
24251e0a012SSarah Sharp 	 * to the parent hub into U0.  The parent hub must then decode the
24351e0a012SSarah Sharp 	 * packet (hub header decode latency) to figure out which port it was
24451e0a012SSarah Sharp 	 * bound for.
24551e0a012SSarah Sharp 	 *
24651e0a012SSarah Sharp 	 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
24751e0a012SSarah Sharp 	 * means 0.1us).  Multiply that by 100 to get nanoseconds.
24851e0a012SSarah Sharp 	 */
24951e0a012SSarah Sharp 	total_mel = hub_lpm_params->mel +
25051e0a012SSarah Sharp 		(hub->descriptor->u.ss.bHubHdrDecLat * 100);
25151e0a012SSarah Sharp 
25251e0a012SSarah Sharp 	/*
25351e0a012SSarah Sharp 	 * How long will it take to transition the downstream hub's port into
25451e0a012SSarah Sharp 	 * U0?  The greater of either the hub exit latency or the device exit
25551e0a012SSarah Sharp 	 * latency.
25651e0a012SSarah Sharp 	 *
25751e0a012SSarah Sharp 	 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
25851e0a012SSarah Sharp 	 * Multiply that by 1000 to get nanoseconds.
25951e0a012SSarah Sharp 	 */
26051e0a012SSarah Sharp 	device_mel = udev_exit_latency * 1000;
26151e0a012SSarah Sharp 	hub_mel = hub_exit_latency * 1000;
26251e0a012SSarah Sharp 	if (device_mel > hub_mel)
26351e0a012SSarah Sharp 		total_mel += device_mel;
26451e0a012SSarah Sharp 	else
26551e0a012SSarah Sharp 		total_mel += hub_mel;
26651e0a012SSarah Sharp 
26751e0a012SSarah Sharp 	udev_lpm_params->mel = total_mel;
26851e0a012SSarah Sharp }
26951e0a012SSarah Sharp 
27051e0a012SSarah Sharp /*
27151e0a012SSarah Sharp  * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
27251e0a012SSarah Sharp  * a transition from either U1 or U2.
27351e0a012SSarah Sharp  */
27451e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev,
27551e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params,
27651e0a012SSarah Sharp 		unsigned int udev_exit_latency,
27751e0a012SSarah Sharp 		struct usb_hub *hub,
27851e0a012SSarah Sharp 		struct usb3_lpm_parameters *hub_lpm_params,
27951e0a012SSarah Sharp 		unsigned int hub_exit_latency,
28051e0a012SSarah Sharp 		unsigned int port_to_port_exit_latency)
28151e0a012SSarah Sharp {
28251e0a012SSarah Sharp 	unsigned int first_link_pel;
28351e0a012SSarah Sharp 	unsigned int hub_pel;
28451e0a012SSarah Sharp 
28551e0a012SSarah Sharp 	/*
28651e0a012SSarah Sharp 	 * First, the device sends an LFPS to transition the link between the
28751e0a012SSarah Sharp 	 * device and the parent hub into U0.  The exit latency is the bigger of
28851e0a012SSarah Sharp 	 * the device exit latency or the hub exit latency.
28951e0a012SSarah Sharp 	 */
29051e0a012SSarah Sharp 	if (udev_exit_latency > hub_exit_latency)
29151e0a012SSarah Sharp 		first_link_pel = udev_exit_latency * 1000;
29251e0a012SSarah Sharp 	else
29351e0a012SSarah Sharp 		first_link_pel = hub_exit_latency * 1000;
29451e0a012SSarah Sharp 
29551e0a012SSarah Sharp 	/*
29651e0a012SSarah Sharp 	 * When the hub starts to receive the LFPS, there is a slight delay for
29751e0a012SSarah Sharp 	 * it to figure out that one of the ports is sending an LFPS.  Then it
29851e0a012SSarah Sharp 	 * will forward the LFPS to its upstream link.  The exit latency is the
29951e0a012SSarah Sharp 	 * delay, plus the PEL that we calculated for this hub.
30051e0a012SSarah Sharp 	 */
30151e0a012SSarah Sharp 	hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
30251e0a012SSarah Sharp 
30351e0a012SSarah Sharp 	/*
30451e0a012SSarah Sharp 	 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
30551e0a012SSarah Sharp 	 * is the greater of the two exit latencies.
30651e0a012SSarah Sharp 	 */
30751e0a012SSarah Sharp 	if (first_link_pel > hub_pel)
30851e0a012SSarah Sharp 		udev_lpm_params->pel = first_link_pel;
30951e0a012SSarah Sharp 	else
31051e0a012SSarah Sharp 		udev_lpm_params->pel = hub_pel;
31151e0a012SSarah Sharp }
31251e0a012SSarah Sharp 
31351e0a012SSarah Sharp /*
31451e0a012SSarah Sharp  * Set the System Exit Latency (SEL) to indicate the total worst-case time from
31551e0a012SSarah Sharp  * when a device initiates a transition to U0, until when it will receive the
31651e0a012SSarah Sharp  * first packet from the host controller.
31751e0a012SSarah Sharp  *
31851e0a012SSarah Sharp  * Section C.1.5.1 describes the four components to this:
31951e0a012SSarah Sharp  *  - t1: device PEL
32051e0a012SSarah Sharp  *  - t2: time for the ERDY to make it from the device to the host.
32151e0a012SSarah Sharp  *  - t3: a host-specific delay to process the ERDY.
32251e0a012SSarah Sharp  *  - t4: time for the packet to make it from the host to the device.
32351e0a012SSarah Sharp  *
32451e0a012SSarah Sharp  * t3 is specific to both the xHCI host and the platform the host is integrated
32551e0a012SSarah Sharp  * into.  The Intel HW folks have said it's negligible, FIXME if a different
32651e0a012SSarah Sharp  * vendor says otherwise.
32751e0a012SSarah Sharp  */
32851e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev,
32951e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params)
33051e0a012SSarah Sharp {
33151e0a012SSarah Sharp 	struct usb_device *parent;
33251e0a012SSarah Sharp 	unsigned int num_hubs;
33351e0a012SSarah Sharp 	unsigned int total_sel;
33451e0a012SSarah Sharp 
33551e0a012SSarah Sharp 	/* t1 = device PEL */
33651e0a012SSarah Sharp 	total_sel = udev_lpm_params->pel;
33751e0a012SSarah Sharp 	/* How many external hubs are in between the device & the root port. */
33851e0a012SSarah Sharp 	for (parent = udev->parent, num_hubs = 0; parent->parent;
33951e0a012SSarah Sharp 			parent = parent->parent)
34051e0a012SSarah Sharp 		num_hubs++;
34151e0a012SSarah Sharp 	/* t2 = 2.1us + 250ns * (num_hubs - 1) */
34251e0a012SSarah Sharp 	if (num_hubs > 0)
34351e0a012SSarah Sharp 		total_sel += 2100 + 250 * (num_hubs - 1);
34451e0a012SSarah Sharp 
34551e0a012SSarah Sharp 	/* t4 = 250ns * num_hubs */
34651e0a012SSarah Sharp 	total_sel += 250 * num_hubs;
34751e0a012SSarah Sharp 
34851e0a012SSarah Sharp 	udev_lpm_params->sel = total_sel;
34951e0a012SSarah Sharp }
35051e0a012SSarah Sharp 
35151e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev)
35251e0a012SSarah Sharp {
35351e0a012SSarah Sharp 	struct usb_hub *hub;
35451e0a012SSarah Sharp 	unsigned int port_to_port_delay;
35551e0a012SSarah Sharp 	unsigned int udev_u1_del;
35651e0a012SSarah Sharp 	unsigned int udev_u2_del;
35751e0a012SSarah Sharp 	unsigned int hub_u1_del;
35851e0a012SSarah Sharp 	unsigned int hub_u2_del;
35951e0a012SSarah Sharp 
36051e0a012SSarah Sharp 	if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
36151e0a012SSarah Sharp 		return;
36251e0a012SSarah Sharp 
36351e0a012SSarah Sharp 	hub = hdev_to_hub(udev->parent);
36451e0a012SSarah Sharp 	/* It doesn't take time to transition the roothub into U0, since it
36551e0a012SSarah Sharp 	 * doesn't have an upstream link.
36651e0a012SSarah Sharp 	 */
36751e0a012SSarah Sharp 	if (!hub)
36851e0a012SSarah Sharp 		return;
36951e0a012SSarah Sharp 
37051e0a012SSarah Sharp 	udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
37151e0a012SSarah Sharp 	udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
37251e0a012SSarah Sharp 	hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
37351e0a012SSarah Sharp 	hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
37451e0a012SSarah Sharp 
37551e0a012SSarah Sharp 	usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
37651e0a012SSarah Sharp 			hub, &udev->parent->u1_params, hub_u1_del);
37751e0a012SSarah Sharp 
37851e0a012SSarah Sharp 	usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
37951e0a012SSarah Sharp 			hub, &udev->parent->u2_params, hub_u2_del);
38051e0a012SSarah Sharp 
38151e0a012SSarah Sharp 	/*
38251e0a012SSarah Sharp 	 * Appendix C, section C.2.2.2, says that there is a slight delay from
38351e0a012SSarah Sharp 	 * when the parent hub notices the downstream port is trying to
38451e0a012SSarah Sharp 	 * transition to U0 to when the hub initiates a U0 transition on its
38551e0a012SSarah Sharp 	 * upstream port.  The section says the delays are tPort2PortU1EL and
38651e0a012SSarah Sharp 	 * tPort2PortU2EL, but it doesn't define what they are.
38751e0a012SSarah Sharp 	 *
38851e0a012SSarah Sharp 	 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
38951e0a012SSarah Sharp 	 * about the same delays.  Use the maximum delay calculations from those
39051e0a012SSarah Sharp 	 * sections.  For U1, it's tHubPort2PortExitLat, which is 1us max.  For
39151e0a012SSarah Sharp 	 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat.  I
39251e0a012SSarah Sharp 	 * assume the device exit latencies they are talking about are the hub
39351e0a012SSarah Sharp 	 * exit latencies.
39451e0a012SSarah Sharp 	 *
39551e0a012SSarah Sharp 	 * What do we do if the U2 exit latency is less than the U1 exit
39651e0a012SSarah Sharp 	 * latency?  It's possible, although not likely...
39751e0a012SSarah Sharp 	 */
39851e0a012SSarah Sharp 	port_to_port_delay = 1;
39951e0a012SSarah Sharp 
40051e0a012SSarah Sharp 	usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
40151e0a012SSarah Sharp 			hub, &udev->parent->u1_params, hub_u1_del,
40251e0a012SSarah Sharp 			port_to_port_delay);
40351e0a012SSarah Sharp 
40451e0a012SSarah Sharp 	if (hub_u2_del > hub_u1_del)
40551e0a012SSarah Sharp 		port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
40651e0a012SSarah Sharp 	else
40751e0a012SSarah Sharp 		port_to_port_delay = 1 + hub_u1_del;
40851e0a012SSarah Sharp 
40951e0a012SSarah Sharp 	usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
41051e0a012SSarah Sharp 			hub, &udev->parent->u2_params, hub_u2_del,
41151e0a012SSarah Sharp 			port_to_port_delay);
41251e0a012SSarah Sharp 
41351e0a012SSarah Sharp 	/* Now that we've got PEL, calculate SEL. */
41451e0a012SSarah Sharp 	usb_set_lpm_sel(udev, &udev->u1_params);
41551e0a012SSarah Sharp 	usb_set_lpm_sel(udev, &udev->u2_params);
41651e0a012SSarah Sharp }
417d9b2099cSSarah Sharp 
4181da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
419dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data)
4201da177e4SLinus Torvalds {
421dbe79bbeSJohn Youn 	int i, ret, size;
422dbe79bbeSJohn Youn 	unsigned dtype;
423dbe79bbeSJohn Youn 
424dbe79bbeSJohn Youn 	if (hub_is_superspeed(hdev)) {
425dbe79bbeSJohn Youn 		dtype = USB_DT_SS_HUB;
426dbe79bbeSJohn Youn 		size = USB_DT_SS_HUB_SIZE;
427dbe79bbeSJohn Youn 	} else {
428dbe79bbeSJohn Youn 		dtype = USB_DT_HUB;
429dbe79bbeSJohn Youn 		size = sizeof(struct usb_hub_descriptor);
430dbe79bbeSJohn Youn 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
4331da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
4341da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
435dbe79bbeSJohn Youn 			dtype << 8, 0, data, size,
4361da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
4371da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
4381da177e4SLinus Torvalds 			return ret;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 	return -EINVAL;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /*
4441da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
4451da177e4SLinus Torvalds  */
4461da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4491da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
4541da177e4SLinus Torvalds  */
4551da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4581da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
4591da177e4SLinus Torvalds 		NULL, 0, 1000);
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /*
4631da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4681da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
4691da177e4SLinus Torvalds 		NULL, 0, 1000);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /*
4731da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
4741da177e4SLinus Torvalds  * for info about using port indicators
4751da177e4SLinus Torvalds  */
4761da177e4SLinus Torvalds static void set_port_led(
4771da177e4SLinus Torvalds 	struct usb_hub *hub,
4781da177e4SLinus Torvalds 	int port1,
4791da177e4SLinus Torvalds 	int selector
4801da177e4SLinus Torvalds )
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
4831da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
4841da177e4SLinus Torvalds 	if (status < 0)
4851da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
4861da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
4871da177e4SLinus Torvalds 			port1,
4881da177e4SLinus Torvalds 			({ char *s; switch (selector) {
4891da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
4901da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
4911da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
4921da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
4931da177e4SLinus Torvalds 			default: s = "??"; break;
4941da177e4SLinus Torvalds 			}; s; }),
4951da177e4SLinus Torvalds 			status);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
4991da177e4SLinus Torvalds 
500c4028958SDavid Howells static void led_work (struct work_struct *work)
5011da177e4SLinus Torvalds {
502c4028958SDavid Howells 	struct usb_hub		*hub =
503c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
5041da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
5051da177e4SLinus Torvalds 	unsigned		i;
5061da177e4SLinus Torvalds 	unsigned		changed = 0;
5071da177e4SLinus Torvalds 	int			cursor = -1;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
5101da177e4SLinus Torvalds 		return;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
5131da177e4SLinus Torvalds 		unsigned	selector, mode;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
5181da177e4SLinus Torvalds 		/* cycle marker */
5191da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
5201da177e4SLinus Torvalds 			cursor = i;
5211da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
5221da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
5231da177e4SLinus Torvalds 			break;
5241da177e4SLinus Torvalds 		/* blinking green = sw attention */
5251da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
5261da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
5271da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
5281da177e4SLinus Torvalds 			break;
5291da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
5301da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
5311da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
5321da177e4SLinus Torvalds 			break;
5331da177e4SLinus Torvalds 		/* blinking amber = hw attention */
5341da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
5351da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
5361da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
5371da177e4SLinus Torvalds 			break;
5381da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
5391da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
5401da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
5411da177e4SLinus Torvalds 			break;
5421da177e4SLinus Torvalds 		/* blink green/amber = reserved */
5431da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
5441da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
5451da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
5461da177e4SLinus Torvalds 			break;
5471da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
5481da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
5491da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
5501da177e4SLinus Torvalds 			break;
5511da177e4SLinus Torvalds 		default:
5521da177e4SLinus Torvalds 			continue;
5531da177e4SLinus Torvalds 		}
5541da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
5551da177e4SLinus Torvalds 			changed = 1;
5561da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
5571da177e4SLinus Torvalds 		hub->indicator[i] = mode;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
5601da177e4SLinus Torvalds 		cursor++;
5611da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
5621da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
5631da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
5641da177e4SLinus Torvalds 		changed++;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	if (changed)
5671da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
5711da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
5721da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /*
5751da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
5781da177e4SLinus Torvalds 		struct usb_hub_status *data)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
5811da177e4SLinus Torvalds 
5823824c1ddSLibor Pechacek 	for (i = 0; i < USB_STS_RETRIES &&
5833824c1ddSLibor Pechacek 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
5841da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
5851da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
5861da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 	return status;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds /*
5921da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
5931da177e4SLinus Torvalds  */
5941da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
5951da177e4SLinus Torvalds 		struct usb_port_status *data)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
5981da177e4SLinus Torvalds 
5993824c1ddSLibor Pechacek 	for (i = 0; i < USB_STS_RETRIES &&
6003824c1ddSLibor Pechacek 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
6011da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
6021da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
6031da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 	return status;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6083eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
6093eb14915SAlan Stern 		u16 *status, u16 *change)
6103eb14915SAlan Stern {
6113eb14915SAlan Stern 	int ret;
6123eb14915SAlan Stern 
6133eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
6143eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
6153eb14915SAlan Stern 	if (ret < 4) {
6163eb14915SAlan Stern 		dev_err(hub->intfdev,
6173eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
6183eb14915SAlan Stern 		if (ret >= 0)
6193eb14915SAlan Stern 			ret = -EIO;
6203eb14915SAlan Stern 	} else {
6213eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
6223eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
623dbe79bbeSJohn Youn 
6243eb14915SAlan Stern 		ret = 0;
6253eb14915SAlan Stern 	}
6263eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
6273eb14915SAlan Stern 	return ret;
6283eb14915SAlan Stern }
6293eb14915SAlan Stern 
6301da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	unsigned long	flags;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
6352e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
6361da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
6378e4ceb38SAlan Stern 
6388e4ceb38SAlan Stern 		/* Suppress autosuspend until khubd runs */
6398e4ceb38SAlan Stern 		usb_autopm_get_interface_no_resume(
6408e4ceb38SAlan Stern 				to_usb_interface(hub->intfdev));
6411da177e4SLinus Torvalds 		wake_up(&khubd_wait);
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
6471da177e4SLinus Torvalds {
64825118084SAlan Stern 	struct usb_hub *hub = hdev_to_hub(hdev);
64925118084SAlan Stern 
65025118084SAlan Stern 	if (hub)
65125118084SAlan Stern 		kick_khubd(hub);
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6544ee823b8SSarah Sharp /*
6554ee823b8SSarah Sharp  * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
6564ee823b8SSarah Sharp  * Notification, which indicates it had initiated remote wakeup.
6574ee823b8SSarah Sharp  *
6584ee823b8SSarah Sharp  * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
6594ee823b8SSarah Sharp  * device initiates resume, so the USB core will not receive notice of the
6604ee823b8SSarah Sharp  * resume through the normal hub interrupt URB.
6614ee823b8SSarah Sharp  */
6624ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev,
6634ee823b8SSarah Sharp 		unsigned int portnum)
6644ee823b8SSarah Sharp {
6654ee823b8SSarah Sharp 	struct usb_hub *hub;
6664ee823b8SSarah Sharp 
6674ee823b8SSarah Sharp 	if (!hdev)
6684ee823b8SSarah Sharp 		return;
6694ee823b8SSarah Sharp 
6704ee823b8SSarah Sharp 	hub = hdev_to_hub(hdev);
6714ee823b8SSarah Sharp 	if (hub) {
6724ee823b8SSarah Sharp 		set_bit(portnum, hub->wakeup_bits);
6734ee823b8SSarah Sharp 		kick_khubd(hub);
6744ee823b8SSarah Sharp 	}
6754ee823b8SSarah Sharp }
6764ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
6797d12e780SDavid Howells static void hub_irq(struct urb *urb)
6801da177e4SLinus Torvalds {
681ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
682e015268dSAlan Stern 	int status = urb->status;
68371d2718fSRoel Kluin 	unsigned i;
6841da177e4SLinus Torvalds 	unsigned long bits;
6851da177e4SLinus Torvalds 
686e015268dSAlan Stern 	switch (status) {
6871da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
6881da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
6891da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
6901da177e4SLinus Torvalds 		return;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	default:		/* presumably an error */
6931da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
694e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
6951da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
6961da177e4SLinus Torvalds 			goto resubmit;
697e015268dSAlan Stern 		hub->error = status;
6981da177e4SLinus Torvalds 		/* FALL THROUGH */
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	/* let khubd handle things */
7011da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
7021da177e4SLinus Torvalds 		bits = 0;
7031da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
7041da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
7051da177e4SLinus Torvalds 					<< (i*8);
7061da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
7071da177e4SLinus Torvalds 		break;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	hub->nerrors = 0;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
7131da177e4SLinus Torvalds 	kick_khubd(hub);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds resubmit:
7161da177e4SLinus Torvalds 	if (hub->quiescing)
7171da177e4SLinus Torvalds 		return;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
7201da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
7211da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
7251da177e4SLinus Torvalds static inline int
7261da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
7271da177e4SLinus Torvalds {
728c2f6595fSAlan Stern 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
7291da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
7301da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds /*
7341da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
7351da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
7361da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
7371da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
7381da177e4SLinus Torvalds  */
739cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
7401da177e4SLinus Torvalds {
741c4028958SDavid Howells 	struct usb_hub		*hub =
742cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
7431da177e4SLinus Torvalds 	unsigned long		flags;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
7463b6054daSOctavian Purdila 	while (!list_empty(&hub->tt.clear_list)) {
747d0f830d3SH Hartley Sweeten 		struct list_head	*next;
7481da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
7491da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
750cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
7511da177e4SLinus Torvalds 		int			status;
7521da177e4SLinus Torvalds 
753d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
754d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
7551da177e4SLinus Torvalds 		list_del (&clear->clear_list);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
7581da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
7591da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
7601da177e4SLinus Torvalds 		if (status)
7611da177e4SLinus Torvalds 			dev_err (&hdev->dev,
7621da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
7631da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
764cb88a1b8SAlan Stern 
765cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
766cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
767cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
768cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
769cb88a1b8SAlan Stern 
7701da177e4SLinus Torvalds 		kfree(clear);
771cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds /**
777cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
778cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
7791da177e4SLinus Torvalds  *
7801da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
7811da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
7821da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
7831da177e4SLinus Torvalds  * interrupt context.
7841da177e4SLinus Torvalds  *
7851da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
7861da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
7871da177e4SLinus Torvalds  */
788cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
7891da177e4SLinus Torvalds {
790cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
791cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
7921da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
7931da177e4SLinus Torvalds 	unsigned long		flags;
7941da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
7971da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
7981da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
7991da177e4SLinus Torvalds 	 */
80054e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
8011da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
8021da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
803cb88a1b8SAlan Stern 		return -ENOMEM;
8041da177e4SLinus Torvalds 	}
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
8071da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
8081da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
8091da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
8101da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
8111da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
8121da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
8131da177e4SLinus Torvalds 	if (usb_pipein (pipe))
8141da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
8151da177e4SLinus Torvalds 
816cb88a1b8SAlan Stern 	/* info for completion callback */
817cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
818cb88a1b8SAlan Stern 	clear->ep = urb->ep;
819cb88a1b8SAlan Stern 
8201da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
8211da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
8221da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
823cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
8241da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
825cb88a1b8SAlan Stern 	return 0;
8261da177e4SLinus Torvalds }
827cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
8281da177e4SLinus Torvalds 
8298520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
8308520f380SAlan Stern  * needs to delay.
8318520f380SAlan Stern  */
8328520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	int port1;
835b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
8368520f380SAlan Stern 	unsigned delay;
8374489a571SAlan Stern 	u16 wHubCharacteristics =
8384489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
8391da177e4SLinus Torvalds 
8404489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
8414489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
8424489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
8434489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
8444489a571SAlan Stern 	 * unless we send these messages to the hub.
8454489a571SAlan Stern 	 */
8464489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
8471da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
8484489a571SAlan Stern 	else
8494489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
8504489a571SAlan Stern 				"non-switchable hub\n");
8511da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
8524489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
8531da177e4SLinus Torvalds 
854b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
8558520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
8568520f380SAlan Stern 	if (do_delay)
8578520f380SAlan Stern 		msleep(delay);
8588520f380SAlan Stern 	return delay;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
8621da177e4SLinus Torvalds 		u16 *status, u16 *change)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	int ret;
8651da177e4SLinus Torvalds 
866db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
8671da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
8681da177e4SLinus Torvalds 	if (ret < 0)
8691da177e4SLinus Torvalds 		dev_err (hub->intfdev,
870441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
8711da177e4SLinus Torvalds 	else {
8721da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
8731da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
8741da177e4SLinus Torvalds 		ret = 0;
8751da177e4SLinus Torvalds 	}
876db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
8771da177e4SLinus Torvalds 	return ret;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8808b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
8818b28c752SAlan Stern {
8828b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
8830458d5b4SAlan Stern 	int ret = 0;
8848b28c752SAlan Stern 
885ff823c79SLan Tianyu 	if (hub->ports[port1 - 1]->child && set_state)
886ff823c79SLan Tianyu 		usb_set_device_state(hub->ports[port1 - 1]->child,
8878b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
888dbe79bbeSJohn Youn 	if (!hub->error && !hub_is_superspeed(hub->hdev))
8898b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
8908b28c752SAlan Stern 	if (ret)
8918b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
8928b28c752SAlan Stern 				port1, ret);
8938b28c752SAlan Stern 	return ret;
8948b28c752SAlan Stern }
8958b28c752SAlan Stern 
8960458d5b4SAlan Stern /*
8976d42fcdbSJustin P. Mattock  * Disable a port and mark a logical connect-change event, so that some
8980458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
8990458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
9000458d5b4SAlan Stern  */
9010458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
9027d069b7dSAlan Stern {
9030458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
9040458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
9050458d5b4SAlan Stern 
9060458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
9070458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
9080458d5b4SAlan Stern 	 *  - SRP saves power that way
9090458d5b4SAlan Stern 	 *  - ... new call, TBD ...
9100458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
9110458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
9120458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
9130458d5b4SAlan Stern 	 */
9140458d5b4SAlan Stern 
9150458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
9160458d5b4SAlan Stern  	kick_khubd(hub);
9170458d5b4SAlan Stern }
9180458d5b4SAlan Stern 
919253e0572SAlan Stern /**
920253e0572SAlan Stern  * usb_remove_device - disable a device's port on its parent hub
921253e0572SAlan Stern  * @udev: device to be disabled and removed
922253e0572SAlan Stern  * Context: @udev locked, must be able to sleep.
923253e0572SAlan Stern  *
924253e0572SAlan Stern  * After @udev's port has been disabled, khubd is notified and it will
925253e0572SAlan Stern  * see that the device has been disconnected.  When the device is
926253e0572SAlan Stern  * physically unplugged and something is plugged in, the events will
927253e0572SAlan Stern  * be received and processed normally.
928253e0572SAlan Stern  */
929253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
930253e0572SAlan Stern {
931253e0572SAlan Stern 	struct usb_hub *hub;
932253e0572SAlan Stern 	struct usb_interface *intf;
933253e0572SAlan Stern 
934253e0572SAlan Stern 	if (!udev->parent)	/* Can't remove a root hub */
935253e0572SAlan Stern 		return -EINVAL;
936253e0572SAlan Stern 	hub = hdev_to_hub(udev->parent);
937253e0572SAlan Stern 	intf = to_usb_interface(hub->intfdev);
938253e0572SAlan Stern 
939253e0572SAlan Stern 	usb_autopm_get_interface(intf);
940253e0572SAlan Stern 	set_bit(udev->portnum, hub->removed_bits);
941253e0572SAlan Stern 	hub_port_logical_disconnect(hub, udev->portnum);
942253e0572SAlan Stern 	usb_autopm_put_interface(intf);
943253e0572SAlan Stern 	return 0;
944253e0572SAlan Stern }
945253e0572SAlan Stern 
9466ee0b270SAlan Stern enum hub_activation_type {
9478e4ceb38SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
9488520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
9496ee0b270SAlan Stern };
9505e6effaeSAlan Stern 
9518520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
9528520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
9538520f380SAlan Stern 
954f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
9555e6effaeSAlan Stern {
9565e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
957653a39d1SSarah Sharp 	struct usb_hcd *hcd;
958653a39d1SSarah Sharp 	int ret;
9595e6effaeSAlan Stern 	int port1;
960f2835219SAlan Stern 	int status;
961948fea37SAlan Stern 	bool need_debounce_delay = false;
9628520f380SAlan Stern 	unsigned delay;
9638520f380SAlan Stern 
9648520f380SAlan Stern 	/* Continue a partial initialization */
9658520f380SAlan Stern 	if (type == HUB_INIT2)
9668520f380SAlan Stern 		goto init2;
9678520f380SAlan Stern 	if (type == HUB_INIT3)
9688520f380SAlan Stern 		goto init3;
9695e6effaeSAlan Stern 
970a45aa3b3SElric Fu 	/* The superspeed hub except for root hub has to use Hub Depth
971a45aa3b3SElric Fu 	 * value as an offset into the route string to locate the bits
972a45aa3b3SElric Fu 	 * it uses to determine the downstream port number. So hub driver
973a45aa3b3SElric Fu 	 * should send a set hub depth request to superspeed hub after
974a45aa3b3SElric Fu 	 * the superspeed hub is set configuration in initialization or
975a45aa3b3SElric Fu 	 * reset procedure.
976a45aa3b3SElric Fu 	 *
977a45aa3b3SElric Fu 	 * After a resume, port power should still be on.
978f2835219SAlan Stern 	 * For any other type of activation, turn it on.
979f2835219SAlan Stern 	 */
9808520f380SAlan Stern 	if (type != HUB_RESUME) {
981a45aa3b3SElric Fu 		if (hdev->parent && hub_is_superspeed(hdev)) {
982a45aa3b3SElric Fu 			ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
983a45aa3b3SElric Fu 					HUB_SET_DEPTH, USB_RT_HUB,
984a45aa3b3SElric Fu 					hdev->level - 1, 0, NULL, 0,
985a45aa3b3SElric Fu 					USB_CTRL_SET_TIMEOUT);
986a45aa3b3SElric Fu 			if (ret < 0)
987a45aa3b3SElric Fu 				dev_err(hub->intfdev,
988a45aa3b3SElric Fu 						"set hub depth failed\n");
989a45aa3b3SElric Fu 		}
9908520f380SAlan Stern 
9918520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
9928520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
9938520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
9948520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
9958520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
9968520f380SAlan Stern 		 * rather than as a module).  It adds up.
9978520f380SAlan Stern 		 *
9988520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
9998520f380SAlan Stern 		 * because for those activation types the ports have to be
10008520f380SAlan Stern 		 * operational when we return.  In theory this could be done
10018520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
10028520f380SAlan Stern 		 */
10038520f380SAlan Stern 		if (type == HUB_INIT) {
10048520f380SAlan Stern 			delay = hub_power_on(hub, false);
10058520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
10068520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
10078520f380SAlan Stern 					msecs_to_jiffies(delay));
100861fbeba1SAlan Stern 
100961fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
10108e4ceb38SAlan Stern 			usb_autopm_get_interface_no_resume(
10118e4ceb38SAlan Stern 					to_usb_interface(hub->intfdev));
10128520f380SAlan Stern 			return;		/* Continues at init2: below */
1013653a39d1SSarah Sharp 		} else if (type == HUB_RESET_RESUME) {
1014653a39d1SSarah Sharp 			/* The internal host controller state for the hub device
1015653a39d1SSarah Sharp 			 * may be gone after a host power loss on system resume.
1016653a39d1SSarah Sharp 			 * Update the device's info so the HW knows it's a hub.
1017653a39d1SSarah Sharp 			 */
1018653a39d1SSarah Sharp 			hcd = bus_to_hcd(hdev->bus);
1019653a39d1SSarah Sharp 			if (hcd->driver->update_hub_device) {
1020653a39d1SSarah Sharp 				ret = hcd->driver->update_hub_device(hcd, hdev,
1021653a39d1SSarah Sharp 						&hub->tt, GFP_NOIO);
1022653a39d1SSarah Sharp 				if (ret < 0) {
1023653a39d1SSarah Sharp 					dev_err(hub->intfdev, "Host not "
1024653a39d1SSarah Sharp 							"accepting hub info "
1025653a39d1SSarah Sharp 							"update.\n");
1026653a39d1SSarah Sharp 					dev_err(hub->intfdev, "LS/FS devices "
1027653a39d1SSarah Sharp 							"and hubs may not work "
1028653a39d1SSarah Sharp 							"under this hub\n.");
1029653a39d1SSarah Sharp 				}
1030653a39d1SSarah Sharp 			}
1031653a39d1SSarah Sharp 			hub_power_on(hub, true);
10328520f380SAlan Stern 		} else {
10338520f380SAlan Stern 			hub_power_on(hub, true);
10348520f380SAlan Stern 		}
10358520f380SAlan Stern 	}
10368520f380SAlan Stern  init2:
1037f2835219SAlan Stern 
10386ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
10396ee0b270SAlan Stern 	 * which ports need attention.
10405e6effaeSAlan Stern 	 */
10415e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1042ff823c79SLan Tianyu 		struct usb_device *udev = hub->ports[port1 - 1]->child;
10435e6effaeSAlan Stern 		u16 portstatus, portchange;
10445e6effaeSAlan Stern 
10456ee0b270SAlan Stern 		portstatus = portchange = 0;
10466ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
10476ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
10486ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
10496ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
10506ee0b270SAlan Stern 					port1, portstatus, portchange);
10515e6effaeSAlan Stern 
10526ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
10536ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
10546ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
10556ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
10565e6effaeSAlan Stern 		 */
10576ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
10586ee0b270SAlan Stern 				type != HUB_RESUME ||
10596ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
10606ee0b270SAlan Stern 				!udev ||
10616ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
10629f0a6cd3SAndiry Xu 			/*
10639f0a6cd3SAndiry Xu 			 * USB3 protocol ports will automatically transition
10649f0a6cd3SAndiry Xu 			 * to Enabled state when detect an USB3.0 device attach.
10659f0a6cd3SAndiry Xu 			 * Do not disable USB3 protocol ports.
10669f0a6cd3SAndiry Xu 			 */
1067131dec34SSarah Sharp 			if (!hub_is_superspeed(hdev)) {
10689f0a6cd3SAndiry Xu 				clear_port_feature(hdev, port1,
10699f0a6cd3SAndiry Xu 						   USB_PORT_FEAT_ENABLE);
10706ee0b270SAlan Stern 				portstatus &= ~USB_PORT_STAT_ENABLE;
107185f0ff46SSarah Sharp 			} else {
107285f0ff46SSarah Sharp 				/* Pretend that power was lost for USB3 devs */
107385f0ff46SSarah Sharp 				portstatus &= ~USB_PORT_STAT_ENABLE;
10746ee0b270SAlan Stern 			}
10759f0a6cd3SAndiry Xu 		}
10766ee0b270SAlan Stern 
1077948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
1078948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
1079948fea37SAlan Stern 			need_debounce_delay = true;
1080948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
1081948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
1082948fea37SAlan Stern 		}
1083948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
1084948fea37SAlan Stern 			need_debounce_delay = true;
1085948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
1086948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
1087948fea37SAlan Stern 		}
108879c3dd81SDon Zickus 		if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
108979c3dd81SDon Zickus 				hub_is_superspeed(hub->hdev)) {
109079c3dd81SDon Zickus 			need_debounce_delay = true;
109179c3dd81SDon Zickus 			clear_port_feature(hub->hdev, port1,
109279c3dd81SDon Zickus 					USB_PORT_FEAT_C_BH_PORT_RESET);
109379c3dd81SDon Zickus 		}
1094253e0572SAlan Stern 		/* We can forget about a "removed" device when there's a
1095253e0572SAlan Stern 		 * physical disconnect or the connect status changes.
1096253e0572SAlan Stern 		 */
1097253e0572SAlan Stern 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1098253e0572SAlan Stern 				(portchange & USB_PORT_STAT_C_CONNECTION))
1099253e0572SAlan Stern 			clear_bit(port1, hub->removed_bits);
1100253e0572SAlan Stern 
11016ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
11026ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
11036ee0b270SAlan Stern 			 * check for a new connection
11046ee0b270SAlan Stern 			 */
11056ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
11066ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
11076ee0b270SAlan Stern 
11086ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
110972937e1eSSarah Sharp 			bool port_resumed = (portstatus &
111072937e1eSSarah Sharp 					USB_PORT_STAT_LINK_STATE) ==
111172937e1eSSarah Sharp 				USB_SS_PORT_LS_U0;
11126ee0b270SAlan Stern 			/* The power session apparently survived the resume.
11136ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
11146ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
111572937e1eSSarah Sharp 			 * take care of it.  Look at the port link state
111672937e1eSSarah Sharp 			 * for USB 3.0 hubs, since they don't have a suspend
111772937e1eSSarah Sharp 			 * change bit, and they don't set the port link change
111872937e1eSSarah Sharp 			 * bit on device-initiated resume.
11196ee0b270SAlan Stern 			 */
112072937e1eSSarah Sharp 			if (portchange || (hub_is_superspeed(hub->hdev) &&
112172937e1eSSarah Sharp 						port_resumed))
11226ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
11236ee0b270SAlan Stern 
11246ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
11256ee0b270SAlan Stern #ifdef CONFIG_PM
11265e6effaeSAlan Stern 			udev->reset_resume = 1;
11276ee0b270SAlan Stern #endif
11288808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
11298808f00cSAlan Stern 
11306ee0b270SAlan Stern 		} else {
11316ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
11326ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
11336ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
11345e6effaeSAlan Stern 		}
11355e6effaeSAlan Stern 	}
11365e6effaeSAlan Stern 
1137948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
1138948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
1139948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
1140948fea37SAlan Stern 	 * one at a time later on.
1141948fea37SAlan Stern 	 *
1142948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
1143948fea37SAlan Stern 	 * will see them later and handle them normally.
1144948fea37SAlan Stern 	 */
11458520f380SAlan Stern 	if (need_debounce_delay) {
11468520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
1147f2835219SAlan Stern 
11488520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
11498520f380SAlan Stern 		if (type == HUB_INIT2) {
11508520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
11518520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
11528520f380SAlan Stern 					msecs_to_jiffies(delay));
11538520f380SAlan Stern 			return;		/* Continues at init3: below */
11548520f380SAlan Stern 		} else {
11558520f380SAlan Stern 			msleep(delay);
11568520f380SAlan Stern 		}
11578520f380SAlan Stern 	}
11588520f380SAlan Stern  init3:
1159f2835219SAlan Stern 	hub->quiescing = 0;
1160f2835219SAlan Stern 
1161f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
1162f2835219SAlan Stern 	if (status < 0)
1163f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
1164f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
1165f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1166f2835219SAlan Stern 
1167f2835219SAlan Stern 	/* Scan all ports that need attention */
1168f2835219SAlan Stern 	kick_khubd(hub);
11698e4ceb38SAlan Stern 
11708e4ceb38SAlan Stern 	/* Allow autosuspend if it was suppressed */
11718e4ceb38SAlan Stern 	if (type <= HUB_INIT3)
11728e4ceb38SAlan Stern 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
11735e6effaeSAlan Stern }
11745e6effaeSAlan Stern 
11758520f380SAlan Stern /* Implement the continuations for the delays above */
11768520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
11778520f380SAlan Stern {
11788520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
11798520f380SAlan Stern 
11808520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
11818520f380SAlan Stern }
11828520f380SAlan Stern 
11838520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
11848520f380SAlan Stern {
11858520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
11868520f380SAlan Stern 
11878520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
11888520f380SAlan Stern }
11898520f380SAlan Stern 
11904330354fSAlan Stern enum hub_quiescing_type {
11914330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
11924330354fSAlan Stern };
11934330354fSAlan Stern 
11944330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
11954330354fSAlan Stern {
11964330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
11974330354fSAlan Stern 	int i;
11984330354fSAlan Stern 
11998520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
12008520f380SAlan Stern 
12014330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
12024330354fSAlan Stern 	hub->quiescing = 1;
12034330354fSAlan Stern 
12044330354fSAlan Stern 	if (type != HUB_SUSPEND) {
12054330354fSAlan Stern 		/* Disconnect all the children */
12064330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
1207ff823c79SLan Tianyu 			if (hub->ports[i]->child)
1208ff823c79SLan Tianyu 				usb_disconnect(&hub->ports[i]->child);
12094330354fSAlan Stern 		}
12104330354fSAlan Stern 	}
12114330354fSAlan Stern 
12124330354fSAlan Stern 	/* Stop khubd and related activity */
12134330354fSAlan Stern 	usb_kill_urb(hub->urb);
12144330354fSAlan Stern 	if (hub->has_indicators)
12154330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
12164330354fSAlan Stern 	if (hub->tt.hub)
1217036546bfSOctavian Purdila 		flush_work(&hub->tt.clear_work);
12184330354fSAlan Stern }
12194330354fSAlan Stern 
12203eb14915SAlan Stern /* caller has locked the hub device */
12213eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
12223eb14915SAlan Stern {
12233eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
12243eb14915SAlan Stern 
12254330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
1226f07600cfSAlan Stern 	return 0;
12277d069b7dSAlan Stern }
12287d069b7dSAlan Stern 
12297d069b7dSAlan Stern /* caller has locked the hub device */
1230f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
12317d069b7dSAlan Stern {
12327de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
12337de18d8bSAlan Stern 
1234f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
1235f07600cfSAlan Stern 	return 0;
12367d069b7dSAlan Stern }
12377d069b7dSAlan Stern 
1238fa2a9566SLan Tianyu static void usb_port_device_release(struct device *dev)
1239fa2a9566SLan Tianyu {
1240fa2a9566SLan Tianyu 	struct usb_port *port_dev = to_usb_port(dev);
1241fa2a9566SLan Tianyu 
1242fa2a9566SLan Tianyu 	kfree(port_dev);
1243fa2a9566SLan Tianyu }
1244fa2a9566SLan Tianyu 
1245fa2a9566SLan Tianyu static void usb_hub_remove_port_device(struct usb_hub *hub,
1246fa2a9566SLan Tianyu 				       int port1)
1247fa2a9566SLan Tianyu {
1248fa2a9566SLan Tianyu 	device_unregister(&hub->ports[port1 - 1]->dev);
1249fa2a9566SLan Tianyu }
1250fa2a9566SLan Tianyu 
1251fa2a9566SLan Tianyu struct device_type usb_port_device_type = {
1252fa2a9566SLan Tianyu 	.name =		"usb_port",
1253fa2a9566SLan Tianyu 	.release =	usb_port_device_release,
1254fa2a9566SLan Tianyu };
1255fa2a9566SLan Tianyu 
1256fa2a9566SLan Tianyu static int usb_hub_create_port_device(struct usb_hub *hub,
1257fa2a9566SLan Tianyu 				      int port1)
1258fa2a9566SLan Tianyu {
1259fa2a9566SLan Tianyu 	struct usb_port *port_dev = NULL;
1260fa2a9566SLan Tianyu 	int retval;
1261fa2a9566SLan Tianyu 
1262fa2a9566SLan Tianyu 	port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1263fa2a9566SLan Tianyu 	if (!port_dev) {
1264fa2a9566SLan Tianyu 		retval = -ENOMEM;
1265fa2a9566SLan Tianyu 		goto exit;
1266fa2a9566SLan Tianyu 	}
1267fa2a9566SLan Tianyu 
1268fa2a9566SLan Tianyu 	hub->ports[port1 - 1] = port_dev;
1269fa2a9566SLan Tianyu 	port_dev->dev.parent = hub->intfdev;
1270fa2a9566SLan Tianyu 	port_dev->dev.type = &usb_port_device_type;
1271fa2a9566SLan Tianyu 	dev_set_name(&port_dev->dev, "port%d", port1);
1272fa2a9566SLan Tianyu 
1273fa2a9566SLan Tianyu 	retval = device_register(&port_dev->dev);
1274fa2a9566SLan Tianyu 	if (retval)
1275fa2a9566SLan Tianyu 		goto error_register;
1276fa2a9566SLan Tianyu 	return 0;
1277fa2a9566SLan Tianyu 
1278fa2a9566SLan Tianyu error_register:
1279fa2a9566SLan Tianyu 	put_device(&port_dev->dev);
1280fa2a9566SLan Tianyu exit:
1281fa2a9566SLan Tianyu 	return retval;
1282fa2a9566SLan Tianyu }
1283fa2a9566SLan Tianyu 
12841da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
12851da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
12861da177e4SLinus Torvalds {
1287b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
12881da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
12891da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
12901da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
129174ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
12921da177e4SLinus Torvalds 	unsigned int pipe;
1293fa2a9566SLan Tianyu 	int maxp, ret, i;
12947cbe5dcaSAlan Stern 	char *message = "out of memory";
12951da177e4SLinus Torvalds 
1296d697cddaSAlan Stern 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
12971da177e4SLinus Torvalds 	if (!hub->buffer) {
12981da177e4SLinus Torvalds 		ret = -ENOMEM;
12991da177e4SLinus Torvalds 		goto fail;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
13031da177e4SLinus Torvalds 	if (!hub->status) {
13041da177e4SLinus Torvalds 		ret = -ENOMEM;
13051da177e4SLinus Torvalds 		goto fail;
13061da177e4SLinus Torvalds 	}
1307db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
13101da177e4SLinus Torvalds 	if (!hub->descriptor) {
13111da177e4SLinus Torvalds 		ret = -ENOMEM;
13121da177e4SLinus Torvalds 		goto fail;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
13161da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
13171da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
13181da177e4SLinus Torvalds 	 */
1319dbe79bbeSJohn Youn 	ret = get_hub_descriptor(hdev, hub->descriptor);
13201da177e4SLinus Torvalds 	if (ret < 0) {
13211da177e4SLinus Torvalds 		message = "can't read hub descriptor";
13221da177e4SLinus Torvalds 		goto fail;
13231da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
13241da177e4SLinus Torvalds 		message = "hub has too many ports!";
13251da177e4SLinus Torvalds 		ret = -ENODEV;
13261da177e4SLinus Torvalds 		goto fail;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
13301da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
13311da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
13321da177e4SLinus Torvalds 
1333fa2a9566SLan Tianyu 	hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1334336c5c31SLan Tianyu 			     GFP_KERNEL);
1335ff823c79SLan Tianyu 	if (!hub->ports) {
13367cbe5dcaSAlan Stern 		ret = -ENOMEM;
13377cbe5dcaSAlan Stern 		goto fail;
13387cbe5dcaSAlan Stern 	}
13397cbe5dcaSAlan Stern 
134074ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
13411da177e4SLinus Torvalds 
1342dbe79bbeSJohn Youn 	/* FIXME for USB 3.0, skip for now */
1343dbe79bbeSJohn Youn 	if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1344dbe79bbeSJohn Youn 			!(hub_is_superspeed(hdev))) {
13451da177e4SLinus Torvalds 		int	i;
13461da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
1349dbe79bbeSJohn Youn 			portstr[i] = hub->descriptor->u.hs.DeviceRemovable
13501da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
13511da177e4SLinus Torvalds 				? 'F' : 'R';
13521da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
13531da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
13541da177e4SLinus Torvalds 	} else
13551da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
13561da177e4SLinus Torvalds 
135774ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
13587bf01185SAman Deep 	case HUB_CHAR_COMMON_LPSM:
13591da177e4SLinus Torvalds 		dev_dbg(hub_dev, "ganged power switching\n");
13601da177e4SLinus Torvalds 		break;
13617bf01185SAman Deep 	case HUB_CHAR_INDV_PORT_LPSM:
13621da177e4SLinus Torvalds 		dev_dbg(hub_dev, "individual port power switching\n");
13631da177e4SLinus Torvalds 		break;
13647bf01185SAman Deep 	case HUB_CHAR_NO_LPSM:
13657bf01185SAman Deep 	case HUB_CHAR_LPSM:
13661da177e4SLinus Torvalds 		dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
13671da177e4SLinus Torvalds 		break;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
137074ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
13717bf01185SAman Deep 	case HUB_CHAR_COMMON_OCPM:
13721da177e4SLinus Torvalds 		dev_dbg(hub_dev, "global over-current protection\n");
13731da177e4SLinus Torvalds 		break;
13747bf01185SAman Deep 	case HUB_CHAR_INDV_PORT_OCPM:
13751da177e4SLinus Torvalds 		dev_dbg(hub_dev, "individual port over-current protection\n");
13761da177e4SLinus Torvalds 		break;
13777bf01185SAman Deep 	case HUB_CHAR_NO_OCPM:
13787bf01185SAman Deep 	case HUB_CHAR_OCPM:
13791da177e4SLinus Torvalds 		dev_dbg(hub_dev, "no over-current protection\n");
13801da177e4SLinus Torvalds 		break;
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
13841da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
1385cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
13861da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
13877bf01185SAman Deep 	case USB_HUB_PR_FS:
13881da177e4SLinus Torvalds 		break;
13897bf01185SAman Deep 	case USB_HUB_PR_HS_SINGLE_TT:
13901da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Single TT\n");
13911da177e4SLinus Torvalds 		hub->tt.hub = hdev;
13921da177e4SLinus Torvalds 		break;
13937bf01185SAman Deep 	case USB_HUB_PR_HS_MULTI_TT:
13941da177e4SLinus Torvalds 		ret = usb_set_interface(hdev, 0, 1);
13951da177e4SLinus Torvalds 		if (ret == 0) {
13961da177e4SLinus Torvalds 			dev_dbg(hub_dev, "TT per port\n");
13971da177e4SLinus Torvalds 			hub->tt.multi = 1;
13981da177e4SLinus Torvalds 		} else
13991da177e4SLinus Torvalds 			dev_err(hub_dev, "Using single TT (err %d)\n",
14001da177e4SLinus Torvalds 				ret);
14011da177e4SLinus Torvalds 		hub->tt.hub = hdev;
14021da177e4SLinus Torvalds 		break;
14037bf01185SAman Deep 	case USB_HUB_PR_SS:
1404d2e9b4d6SSarah Sharp 		/* USB 3.0 hubs don't have a TT */
1405d2e9b4d6SSarah Sharp 		break;
14061da177e4SLinus Torvalds 	default:
14071da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
14081da177e4SLinus Torvalds 			hdev->descriptor.bDeviceProtocol);
14091da177e4SLinus Torvalds 		break;
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 
1412e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
141374ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1414e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
1415e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
1416e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
1417e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
1418e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
1419e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
1420e09711aeSdavid-b@pacbell.net 			}
14211da177e4SLinus Torvalds 			break;
1422e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
1423e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
1424e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1425e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1426e09711aeSdavid-b@pacbell.net 				16, hub->tt.think_time);
14271da177e4SLinus Torvalds 			break;
1428e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
1429e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
1430e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1431e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1432e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
14331da177e4SLinus Torvalds 			break;
1434e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
1435e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
1436e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1437e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1438e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
14391da177e4SLinus Torvalds 			break;
14401da177e4SLinus Torvalds 	}
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
144374ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
14441da177e4SLinus Torvalds 		hub->has_indicators = 1;
14451da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
14491da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
14521da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
14531da177e4SLinus Torvalds 	 */
14541da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
145555c52718SAlan Stern 	if (ret < 2) {
14561da177e4SLinus Torvalds 		message = "can't get hub status";
14571da177e4SLinus Torvalds 		goto fail;
14581da177e4SLinus Torvalds 	}
14597d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
14607d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
146155c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
146255c52718SAlan Stern 			hub->mA_per_port = 500;
146355c52718SAlan Stern 		else {
146455c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
146555c52718SAlan Stern 			hub->limited_power = 1;
146655c52718SAlan Stern 		}
14677d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
14681da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
14691da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
147055c52718SAlan Stern 		hub->limited_power = 1;
147155c52718SAlan Stern 		if (hdev->maxchild > 0) {
147255c52718SAlan Stern 			int remaining = hdev->bus_mA -
147355c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
14741da177e4SLinus Torvalds 
147555c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
147655c52718SAlan Stern 				dev_warn(hub_dev,
147755c52718SAlan Stern 					"insufficient power available "
147855c52718SAlan Stern 					"to use all downstream ports\n");
147955c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
148055c52718SAlan Stern 		}
148155c52718SAlan Stern 	} else {	/* Self-powered external hub */
148255c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
148355c52718SAlan Stern 		 * provide less current per port? */
148455c52718SAlan Stern 		hub->mA_per_port = 500;
148555c52718SAlan Stern 	}
148655c52718SAlan Stern 	if (hub->mA_per_port < 500)
148755c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
148855c52718SAlan Stern 				hub->mA_per_port);
14891da177e4SLinus Torvalds 
1490b356b7c7SSarah Sharp 	/* Update the HCD's internal representation of this hub before khubd
1491b356b7c7SSarah Sharp 	 * starts getting port status changes for devices under the hub.
1492b356b7c7SSarah Sharp 	 */
1493b356b7c7SSarah Sharp 	hcd = bus_to_hcd(hdev->bus);
1494b356b7c7SSarah Sharp 	if (hcd->driver->update_hub_device) {
1495b356b7c7SSarah Sharp 		ret = hcd->driver->update_hub_device(hcd, hdev,
1496b356b7c7SSarah Sharp 				&hub->tt, GFP_KERNEL);
1497b356b7c7SSarah Sharp 		if (ret < 0) {
1498b356b7c7SSarah Sharp 			message = "can't update HCD hub info";
1499b356b7c7SSarah Sharp 			goto fail;
1500b356b7c7SSarah Sharp 		}
1501b356b7c7SSarah Sharp 	}
1502b356b7c7SSarah Sharp 
15031da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
15041da177e4SLinus Torvalds 	if (ret < 0) {
15051da177e4SLinus Torvalds 		message = "can't get hub status";
15061da177e4SLinus Torvalds 		goto fail;
15071da177e4SLinus Torvalds 	}
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
15101da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
15111da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
15121da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
15131da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
15141da177e4SLinus Torvalds 
151574ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
15161da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
15171da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
15181da177e4SLinus Torvalds 
151988fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
152088fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
152188fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
152288fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
152388fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
152488fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
15251da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
15261da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
15291da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
15321da177e4SLinus Torvalds 	if (!hub->urb) {
15331da177e4SLinus Torvalds 		ret = -ENOMEM;
15341da177e4SLinus Torvalds 		goto fail;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
15381da177e4SLinus Torvalds 		hub, endpoint->bInterval);
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
15411da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
15421da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
15431da177e4SLinus Torvalds 
1544fa2a9566SLan Tianyu 	for (i = 0; i < hdev->maxchild; i++)
1545fa2a9566SLan Tianyu 		if (usb_hub_create_port_device(hub, i + 1) < 0)
1546fa2a9566SLan Tianyu 			dev_err(hub->intfdev,
1547fa2a9566SLan Tianyu 				"couldn't create port%d device.\n", i + 1);
1548fa2a9566SLan Tianyu 
1549f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
15501da177e4SLinus Torvalds 	return 0;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds fail:
15531da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
15541da177e4SLinus Torvalds 			message, ret);
15551da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
15561da177e4SLinus Torvalds 	return ret;
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
1559e8054854SAlan Stern static void hub_release(struct kref *kref)
1560e8054854SAlan Stern {
1561e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1562e8054854SAlan Stern 
1563e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1564e8054854SAlan Stern 	kfree(hub);
1565e8054854SAlan Stern }
1566e8054854SAlan Stern 
15671da177e4SLinus Torvalds static unsigned highspeed_hubs;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
15701da177e4SLinus Torvalds {
15711da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
1572fa286188SGreg Kroah-Hartman 	struct usb_device *hdev = interface_to_usbdev(intf);
1573fa2a9566SLan Tianyu 	int i;
1574fa2a9566SLan Tianyu 
1575e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1576e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
15778e4ceb38SAlan Stern 	if (!list_empty(&hub->event_list)) {
1578e8054854SAlan Stern 		list_del_init(&hub->event_list);
15798e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
15808e4ceb38SAlan Stern 	}
1581e8054854SAlan Stern 	hub->disconnected = 1;
1582e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
15831da177e4SLinus Torvalds 
15847de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
15857de18d8bSAlan Stern 	hub->error = 0;
15864330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
15877de18d8bSAlan Stern 
15888b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
15891f2235b8SAlexander Shishkin 
15901f2235b8SAlexander Shishkin 	for (i = 0; i < hdev->maxchild; i++)
15911f2235b8SAlexander Shishkin 		usb_hub_remove_port_device(hub, i + 1);
15927cbe5dcaSAlan Stern 	hub->hdev->maxchild = 0;
15931da177e4SLinus Torvalds 
1594e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
15951da177e4SLinus Torvalds 		highspeed_hubs--;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
1598fa2a9566SLan Tianyu 	kfree(hub->ports);
15991da177e4SLinus Torvalds 	kfree(hub->descriptor);
16001da177e4SLinus Torvalds 	kfree(hub->status);
1601d697cddaSAlan Stern 	kfree(hub->buffer);
16021da177e4SLinus Torvalds 
1603e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
16071da177e4SLinus Torvalds {
16081da177e4SLinus Torvalds 	struct usb_host_interface *desc;
16091da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
16101da177e4SLinus Torvalds 	struct usb_device *hdev;
16111da177e4SLinus Torvalds 	struct usb_hub *hub;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
16141da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
16151da177e4SLinus Torvalds 
1616596d789aSMing Lei 	/*
1617596d789aSMing Lei 	 * Set default autosuspend delay as 0 to speedup bus suspend,
1618596d789aSMing Lei 	 * based on the below considerations:
1619596d789aSMing Lei 	 *
1620596d789aSMing Lei 	 * - Unlike other drivers, the hub driver does not rely on the
1621596d789aSMing Lei 	 *   autosuspend delay to provide enough time to handle a wakeup
1622596d789aSMing Lei 	 *   event, and the submitted status URB is just to check future
1623596d789aSMing Lei 	 *   change on hub downstream ports, so it is safe to do it.
1624596d789aSMing Lei 	 *
1625596d789aSMing Lei 	 * - The patch might cause one or more auto supend/resume for
1626596d789aSMing Lei 	 *   below very rare devices when they are plugged into hub
1627596d789aSMing Lei 	 *   first time:
1628596d789aSMing Lei 	 *
1629596d789aSMing Lei 	 *   	devices having trouble initializing, and disconnect
1630596d789aSMing Lei 	 *   	themselves from the bus and then reconnect a second
1631596d789aSMing Lei 	 *   	or so later
1632596d789aSMing Lei 	 *
1633596d789aSMing Lei 	 *   	devices just for downloading firmware, and disconnects
1634596d789aSMing Lei 	 *   	themselves after completing it
1635596d789aSMing Lei 	 *
1636596d789aSMing Lei 	 *   For these quite rare devices, their drivers may change the
1637596d789aSMing Lei 	 *   autosuspend delay of their parent hub in the probe() to one
1638596d789aSMing Lei 	 *   appropriate value to avoid the subtle problem if someone
1639596d789aSMing Lei 	 *   does care it.
1640596d789aSMing Lei 	 *
1641596d789aSMing Lei 	 * - The patch may cause one or more auto suspend/resume on
1642596d789aSMing Lei 	 *   hub during running 'lsusb', but it is probably too
1643596d789aSMing Lei 	 *   infrequent to worry about.
1644596d789aSMing Lei 	 *
1645596d789aSMing Lei 	 * - Change autosuspend delay of hub can avoid unnecessary auto
1646596d789aSMing Lei 	 *   suspend timer for hub, also may decrease power consumption
1647596d789aSMing Lei 	 *   of USB bus.
1648596d789aSMing Lei 	 */
1649596d789aSMing Lei 	pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1650596d789aSMing Lei 
16512839f5bcSSarah Sharp 	/* Hubs have proper suspend/resume support. */
1652088f7fecSAlan Stern 	usb_enable_autosuspend(hdev);
1653088f7fecSAlan Stern 
165438f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1655b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1656b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
165738f3ad5eSFelipe Balbi 		return -E2BIG;
165838f3ad5eSFelipe Balbi 	}
165938f3ad5eSFelipe Balbi 
166089ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
166189ccbdc9SDavid Brownell 	if (hdev->parent) {
166289ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
166389ccbdc9SDavid Brownell 		return -ENODEV;
166489ccbdc9SDavid Brownell 	}
166589ccbdc9SDavid Brownell #endif
166689ccbdc9SDavid Brownell 
16671da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
16681da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
16691da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
16701da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
16711da177e4SLinus Torvalds descriptor_error:
16721da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
16731da177e4SLinus Torvalds 		return -EIO;
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
16771da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
16781da177e4SLinus Torvalds 		goto descriptor_error;
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
16811da177e4SLinus Torvalds 
1682fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1683fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
16841da177e4SLinus Torvalds 		goto descriptor_error;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	/* We found a hub */
16871da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
16881da177e4SLinus Torvalds 
16890a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
16901da177e4SLinus Torvalds 	if (!hub) {
16911da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
16921da177e4SLinus Torvalds 		return -ENOMEM;
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds 
1695e8054854SAlan Stern 	kref_init(&hub->kref);
16961da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
16971da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
16981da177e4SLinus Torvalds 	hub->hdev = hdev;
1699c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
17008520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1701e8054854SAlan Stern 	usb_get_intf(intf);
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
170440f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
17071da177e4SLinus Torvalds 		highspeed_hubs++;
17081da177e4SLinus Torvalds 
1709e6f30deaSMing Lei 	if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1710e6f30deaSMing Lei 		hub->quirk_check_port_auto_suspend = 1;
1711e6f30deaSMing Lei 
17121da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
17131da177e4SLinus Torvalds 		return 0;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	hub_disconnect (intf);
17161da177e4SLinus Torvalds 	return -ENODEV;
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds static int
17201da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
17211da177e4SLinus Torvalds {
17221da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
1723ff823c79SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
17261da177e4SLinus Torvalds 	switch (code) {
17271da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
17281da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
17291da177e4SLinus Torvalds 		int i;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
17321da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
17331da177e4SLinus Torvalds 			info->nports = 0;
17341da177e4SLinus Torvalds 		else {
17351da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
17361da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
1737ff823c79SLan Tianyu 				if (hub->ports[i]->child == NULL)
17381da177e4SLinus Torvalds 					info->port[i] = 0;
17391da177e4SLinus Torvalds 				else
17401da177e4SLinus Torvalds 					info->port[i] =
1741ff823c79SLan Tianyu 						hub->ports[i]->child->devnum;
17421da177e4SLinus Torvalds 			}
17431da177e4SLinus Torvalds 		}
17441da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 		return info->nports + 1;
17471da177e4SLinus Torvalds 		}
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	default:
17501da177e4SLinus Torvalds 		return -ENOSYS;
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17547cbe5dcaSAlan Stern /*
17557cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
17567cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
17577cbe5dcaSAlan Stern  */
17587cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
1759336c5c31SLan Tianyu 		struct dev_state ***ppowner)
17607cbe5dcaSAlan Stern {
17617cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
17627cbe5dcaSAlan Stern 		return -ENODEV;
17637cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
17647cbe5dcaSAlan Stern 		return -EINVAL;
17657cbe5dcaSAlan Stern 
17667cbe5dcaSAlan Stern 	/* This assumes that devices not managed by the hub driver
17677cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
17687cbe5dcaSAlan Stern 	 */
1769fa2a9566SLan Tianyu 	*ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
17707cbe5dcaSAlan Stern 	return 0;
17717cbe5dcaSAlan Stern }
17727cbe5dcaSAlan Stern 
17737cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
1774336c5c31SLan Tianyu int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1775336c5c31SLan Tianyu 		       struct dev_state *owner)
17767cbe5dcaSAlan Stern {
17777cbe5dcaSAlan Stern 	int rc;
1778336c5c31SLan Tianyu 	struct dev_state **powner;
17797cbe5dcaSAlan Stern 
17807cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
17817cbe5dcaSAlan Stern 	if (rc)
17827cbe5dcaSAlan Stern 		return rc;
17837cbe5dcaSAlan Stern 	if (*powner)
17847cbe5dcaSAlan Stern 		return -EBUSY;
17857cbe5dcaSAlan Stern 	*powner = owner;
17867cbe5dcaSAlan Stern 	return rc;
17877cbe5dcaSAlan Stern }
17887cbe5dcaSAlan Stern 
1789336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1790336c5c31SLan Tianyu 			 struct dev_state *owner)
17917cbe5dcaSAlan Stern {
17927cbe5dcaSAlan Stern 	int rc;
1793336c5c31SLan Tianyu 	struct dev_state **powner;
17947cbe5dcaSAlan Stern 
17957cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
17967cbe5dcaSAlan Stern 	if (rc)
17977cbe5dcaSAlan Stern 		return rc;
17987cbe5dcaSAlan Stern 	if (*powner != owner)
17997cbe5dcaSAlan Stern 		return -ENOENT;
18007cbe5dcaSAlan Stern 	*powner = NULL;
18017cbe5dcaSAlan Stern 	return rc;
18027cbe5dcaSAlan Stern }
18037cbe5dcaSAlan Stern 
1804336c5c31SLan Tianyu void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
18057cbe5dcaSAlan Stern {
1806fa2a9566SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
18077cbe5dcaSAlan Stern 	int n;
18087cbe5dcaSAlan Stern 
1809fa2a9566SLan Tianyu 	for (n = 0; n < hdev->maxchild; n++) {
1810fa2a9566SLan Tianyu 		if (hub->ports[n]->port_owner == owner)
1811fa2a9566SLan Tianyu 			hub->ports[n]->port_owner = NULL;
18127cbe5dcaSAlan Stern 	}
1813fa2a9566SLan Tianyu 
18147cbe5dcaSAlan Stern }
18157cbe5dcaSAlan Stern 
18167cbe5dcaSAlan Stern /* The caller must hold udev's lock */
18177cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
18187cbe5dcaSAlan Stern {
18197cbe5dcaSAlan Stern 	struct usb_hub *hub;
18207cbe5dcaSAlan Stern 
18217cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
18227cbe5dcaSAlan Stern 		return false;
18237cbe5dcaSAlan Stern 	hub = hdev_to_hub(udev->parent);
1824fa2a9566SLan Tianyu 	return !!hub->ports[udev->portnum - 1]->port_owner;
18257cbe5dcaSAlan Stern }
18267cbe5dcaSAlan Stern 
18271da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
18281da177e4SLinus Torvalds {
1829ff823c79SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(udev);
18301da177e4SLinus Torvalds 	int i;
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
1833ff823c79SLan Tianyu 		if (hub->ports[i]->child)
1834ff823c79SLan Tianyu 			recursively_mark_NOTATTACHED(hub->ports[i]->child);
18351da177e4SLinus Torvalds 	}
18369bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_SUSPENDED)
183715123006SSarah Sharp 		udev->active_duration -= jiffies;
18381da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
18391da177e4SLinus Torvalds }
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds /**
18421da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
18431da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
18441da177e4SLinus Torvalds  * @new_state: new state value to be stored
18451da177e4SLinus Torvalds  *
18461da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
18471da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
18481da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
18491da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
18501da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
18511da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
18521da177e4SLinus Torvalds  * device_state_lock spinlock.
18531da177e4SLinus Torvalds  *
18541da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
18551da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
18561da177e4SLinus Torvalds  *
18571da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
18581da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
18591da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
18601da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
18611da177e4SLinus Torvalds  */
18621da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
18631da177e4SLinus Torvalds 		enum usb_device_state new_state)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	unsigned long flags;
18664681b171SRafael J. Wysocki 	int wakeup = -1;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
18691da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
18701da177e4SLinus Torvalds 		;	/* do nothing */
1871b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1872fb669cc0SDavid Brownell 
1873fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1874fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1875fb669cc0SDavid Brownell 		 */
1876fb669cc0SDavid Brownell 		if (udev->parent) {
1877645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1878645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1879645daaabSAlan Stern 				;	/* No change to wakeup settings */
1880645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
18814681b171SRafael J. Wysocki 				wakeup = udev->actconfig->desc.bmAttributes
18824681b171SRafael J. Wysocki 					 & USB_CONFIG_ATT_WAKEUP;
1883645daaabSAlan Stern 			else
18844681b171SRafael J. Wysocki 				wakeup = 0;
1885fb669cc0SDavid Brownell 		}
188615123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
188715123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
188815123006SSarah Sharp 			udev->active_duration -= jiffies;
188915123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
189015123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
189115123006SSarah Sharp 			udev->active_duration += jiffies;
1892645daaabSAlan Stern 		udev->state = new_state;
1893b94dc6b5SDavid Brownell 	} else
18941da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
18951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
18964681b171SRafael J. Wysocki 	if (wakeup >= 0)
18974681b171SRafael J. Wysocki 		device_set_wakeup_capable(&udev->dev, wakeup);
18981da177e4SLinus Torvalds }
18996da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
19001da177e4SLinus Torvalds 
19018af548dcSInaky Perez-Gonzalez /*
19023b29b68bSAlan Stern  * Choose a device number.
19033b29b68bSAlan Stern  *
19043b29b68bSAlan Stern  * Device numbers are used as filenames in usbfs.  On USB-1.1 and
19053b29b68bSAlan Stern  * USB-2.0 buses they are also used as device addresses, however on
19063b29b68bSAlan Stern  * USB-3.0 buses the address is assigned by the controller hardware
19073b29b68bSAlan Stern  * and it usually is not the same as the device number.
19083b29b68bSAlan Stern  *
19098af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
19108af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
19118af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
19128af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
19138af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
19148af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
19158af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
19168af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
19178af548dcSInaky Perez-Gonzalez  * [X | 0x80].
19188af548dcSInaky Perez-Gonzalez  *
19198af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
19208af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
19218af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
19228af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
19238af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
1924c6515272SSarah Sharp  *
1925c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
1926c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
1927c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
1928c6515272SSarah Sharp  * command to the hardware.
19298af548dcSInaky Perez-Gonzalez  */
19303b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev)
19311da177e4SLinus Torvalds {
19321da177e4SLinus Torvalds 	int		devnum;
19331da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
19368af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
19378af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
19388af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
19398af548dcSInaky Perez-Gonzalez 	} else {
19408af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
19418af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
19421da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
19431da177e4SLinus Torvalds 					    bus->devnum_next);
19441da177e4SLinus Torvalds 		if (devnum >= 128)
19458af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
19468af548dcSInaky Perez-Gonzalez 						    128, 1);
19471da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
19488af548dcSInaky Perez-Gonzalez 	}
19491da177e4SLinus Torvalds 	if (devnum < 128) {
19501da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
19511da177e4SLinus Torvalds 		udev->devnum = devnum;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds }
19541da177e4SLinus Torvalds 
19553b29b68bSAlan Stern static void release_devnum(struct usb_device *udev)
19561da177e4SLinus Torvalds {
19571da177e4SLinus Torvalds 	if (udev->devnum > 0) {
19581da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
19591da177e4SLinus Torvalds 		udev->devnum = -1;
19601da177e4SLinus Torvalds 	}
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
19633b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum)
19644953d141SDavid Vrabel {
19654953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
19664953d141SDavid Vrabel 	if (!udev->wusb)
19674953d141SDavid Vrabel 		udev->devnum = devnum;
19684953d141SDavid Vrabel }
19694953d141SDavid Vrabel 
1970f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev)
1971f7410cedSHerbert Xu {
1972f7410cedSHerbert Xu 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1973f7410cedSHerbert Xu 
1974f7410cedSHerbert Xu 	/* Root hubs aren't real devices, so don't free HCD resources */
1975f7410cedSHerbert Xu 	if (hcd->driver->free_dev && udev->parent)
1976f7410cedSHerbert Xu 		hcd->driver->free_dev(hcd, udev);
1977f7410cedSHerbert Xu }
1978f7410cedSHerbert Xu 
19791da177e4SLinus Torvalds /**
19801da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
19811da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
19821da177e4SLinus Torvalds  * Context: !in_interrupt ()
19831da177e4SLinus Torvalds  *
19841da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
19851da177e4SLinus Torvalds  *
19861da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
19871da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
19881da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
19891da177e4SLinus Torvalds  *
19901da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
19911da177e4SLinus Torvalds  * controllers) should ever call this.
19921da177e4SLinus Torvalds  *
19931da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
19941da177e4SLinus Torvalds  */
19951da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
19961da177e4SLinus Torvalds {
19971da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
1998ff823c79SLan Tianyu 	struct usb_hub		*hub = hdev_to_hub(udev);
19991da177e4SLinus Torvalds 	int			i;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
20021da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
200325985edcSLucas De Marchi 	 * this quiesces everything except pending urbs.
20041da177e4SLinus Torvalds 	 */
20051da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
20063b29b68bSAlan Stern 	dev_info(&udev->dev, "USB disconnect, device number %d\n",
20073b29b68bSAlan Stern 			udev->devnum);
20081da177e4SLinus Torvalds 
20099ad3d6ccSAlan Stern 	usb_lock_device(udev);
20109ad3d6ccSAlan Stern 
20111da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
20128816230eSHuajun Li 	for (i = 0; i < udev->maxchild; i++) {
2013ff823c79SLan Tianyu 		if (hub->ports[i]->child)
2014ff823c79SLan Tianyu 			usb_disconnect(&hub->ports[i]->child);
20151da177e4SLinus Torvalds 	}
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
20181da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
20191da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
20201da177e4SLinus Torvalds 	 */
2021782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
20221da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
2023cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
20241da177e4SLinus Torvalds 
20253b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
2026782da727SAlan Stern 	usb_unlock_device(udev);
20273099e75aSGreg Kroah-Hartman 
2028782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
20293b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
20303b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
2031782da727SAlan Stern 	 */
2032782da727SAlan Stern 	device_del(&udev->dev);
2033782da727SAlan Stern 
2034782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
20351da177e4SLinus Torvalds 	 * (or root_hub) pointer.
20361da177e4SLinus Torvalds 	 */
20373b29b68bSAlan Stern 	release_devnum(udev);
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
20401da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
20411da177e4SLinus Torvalds 	*pdev = NULL;
20421da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
20431da177e4SLinus Torvalds 
2044f7410cedSHerbert Xu 	hub_free_dev(udev);
2045f7410cedSHerbert Xu 
2046782da727SAlan Stern 	put_device(&udev->dev);
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
2049f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
20501da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
20511da177e4SLinus Torvalds {
20521da177e4SLinus Torvalds 	if (!string)
20531da177e4SLinus Torvalds 		return;
2054f2ec522eSJoe Perches 	dev_info(&udev->dev, "%s: %s\n", id, string);
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
2057f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
2058f2a383e4SGreg Kroah-Hartman {
2059f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2060f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
2061f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
2062b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
2063b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2064f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
2065f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
2066f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
2067f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
2068f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
2069f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
2070f2a383e4SGreg Kroah-Hartman }
20711da177e4SLinus Torvalds #else
2072f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
20731da177e4SLinus Torvalds #endif
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
20761da177e4SLinus Torvalds #include "otg_whitelist.h"
20771da177e4SLinus Torvalds #endif
20781da177e4SLinus Torvalds 
20793ede760fSOliver Neukum /**
20808d8558d1SAlan Stern  * usb_enumerate_device_otg - FIXME (usbcore-internal)
20813ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
20823ede760fSOliver Neukum  *
20838d8558d1SAlan Stern  * Finish enumeration for On-The-Go devices
20843ede760fSOliver Neukum  */
20858d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
20861da177e4SLinus Torvalds {
2087d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
20901da177e4SLinus Torvalds 	/*
20911da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
20921da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
20931da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
20941da177e4SLinus Torvalds 	 */
20951da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
20961da177e4SLinus Torvalds 			&& udev->config
20971da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
20982eb5052eSFelipe Balbi 		struct usb_otg_descriptor	*desc = NULL;
20991da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
21021da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
21031da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
21041da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
21051da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
210612c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 				dev_info(&udev->dev,
21091da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
21101da177e4SLinus Torvalds 					(port1 == bus->otg_port)
21111da177e4SLinus Torvalds 						? "" : "non-");
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
21141da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
21151da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
21161da177e4SLinus Torvalds 				err = usb_control_msg(udev,
21171da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
21181da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
21191da177e4SLinus Torvalds 					bus->b_hnp_enable
21201da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
21211da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
21221da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
21231da177e4SLinus Torvalds 				if (err < 0) {
21241da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
21251da177e4SLinus Torvalds 					 * customize to match your product.
21261da177e4SLinus Torvalds 					 */
21271da177e4SLinus Torvalds 					dev_info(&udev->dev,
2128b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
21291da177e4SLinus Torvalds 						err);
21301da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
21311da177e4SLinus Torvalds 				}
21321da177e4SLinus Torvalds 			}
21331da177e4SLinus Torvalds 		}
21341da177e4SLinus Torvalds 	}
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
21391da177e4SLinus Torvalds 		 * (Includes HNP test device.)
21401da177e4SLinus Torvalds 		 */
21411da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2142634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
21431da177e4SLinus Torvalds 			if (err < 0)
21441da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
21451da177e4SLinus Torvalds 		}
2146ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
21471da177e4SLinus Torvalds 		goto fail;
21481da177e4SLinus Torvalds 	}
2149d9d16e8aSInaky Perez-Gonzalez fail:
21501da177e4SLinus Torvalds #endif
2151d9d16e8aSInaky Perez-Gonzalez 	return err;
2152d9d16e8aSInaky Perez-Gonzalez }
21531da177e4SLinus Torvalds 
2154d9d16e8aSInaky Perez-Gonzalez 
2155d9d16e8aSInaky Perez-Gonzalez /**
21568d8558d1SAlan Stern  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2157d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
2158d9d16e8aSInaky Perez-Gonzalez  *
2159d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
2160d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
2161d9d16e8aSInaky Perez-Gonzalez  * environment.
2162d9d16e8aSInaky Perez-Gonzalez  *
2163d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
2164d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
2165d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
2166d9d16e8aSInaky Perez-Gonzalez  */
21678d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
2168d9d16e8aSInaky Perez-Gonzalez {
2169d9d16e8aSInaky Perez-Gonzalez 	int err;
2170d9d16e8aSInaky Perez-Gonzalez 
2171d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
2172d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
2173d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
2174d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
2175d9d16e8aSInaky Perez-Gonzalez 				err);
217680da2e0dSLaurent Pinchart 			return err;
2177d9d16e8aSInaky Perez-Gonzalez 		}
2178d9d16e8aSInaky Perez-Gonzalez 	}
2179d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
2180d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2181d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2182d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2183d9d16e8aSInaky Perez-Gonzalez 	}
2184d9d16e8aSInaky Perez-Gonzalez 	else {
2185d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
2186d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2187d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
2188d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
2189d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2190d9d16e8aSInaky Perez-Gonzalez 	}
21918d8558d1SAlan Stern 	err = usb_enumerate_device_otg(udev);
219280da2e0dSLaurent Pinchart 	if (err < 0)
2193d9d16e8aSInaky Perez-Gonzalez 		return err;
219480da2e0dSLaurent Pinchart 
219580da2e0dSLaurent Pinchart 	usb_detect_interface_quirks(udev);
219680da2e0dSLaurent Pinchart 
219780da2e0dSLaurent Pinchart 	return 0;
2198d9d16e8aSInaky Perez-Gonzalez }
2199d9d16e8aSInaky Perez-Gonzalez 
2200d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev)
2201d35e70d5SMatthew Garrett {
2202d35e70d5SMatthew Garrett 	struct usb_device *hdev = udev->parent;
2203d35e70d5SMatthew Garrett 	struct usb_hub *hub;
2204d35e70d5SMatthew Garrett 	u8 port = udev->portnum;
2205d35e70d5SMatthew Garrett 	u16 wHubCharacteristics;
2206d35e70d5SMatthew Garrett 	bool removable = true;
2207d35e70d5SMatthew Garrett 
2208d35e70d5SMatthew Garrett 	if (!hdev)
2209d35e70d5SMatthew Garrett 		return;
2210d35e70d5SMatthew Garrett 
2211d35e70d5SMatthew Garrett 	hub = hdev_to_hub(udev->parent);
2212d35e70d5SMatthew Garrett 
2213d35e70d5SMatthew Garrett 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2214d35e70d5SMatthew Garrett 
2215d35e70d5SMatthew Garrett 	if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2216d35e70d5SMatthew Garrett 		return;
2217d35e70d5SMatthew Garrett 
2218d35e70d5SMatthew Garrett 	if (hub_is_superspeed(hdev)) {
2219ca3c1539SLan Tianyu 		if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2220ca3c1539SLan Tianyu 				& (1 << port))
2221d35e70d5SMatthew Garrett 			removable = false;
2222d35e70d5SMatthew Garrett 	} else {
2223d35e70d5SMatthew Garrett 		if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2224d35e70d5SMatthew Garrett 			removable = false;
2225d35e70d5SMatthew Garrett 	}
2226d35e70d5SMatthew Garrett 
2227d35e70d5SMatthew Garrett 	if (removable)
2228d35e70d5SMatthew Garrett 		udev->removable = USB_DEVICE_REMOVABLE;
2229d35e70d5SMatthew Garrett 	else
2230d35e70d5SMatthew Garrett 		udev->removable = USB_DEVICE_FIXED;
2231d35e70d5SMatthew Garrett }
2232d9d16e8aSInaky Perez-Gonzalez 
2233d9d16e8aSInaky Perez-Gonzalez /**
2234d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
2235d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
2236d9d16e8aSInaky Perez-Gonzalez  *
22378d8558d1SAlan Stern  * This is called with devices which have been detected but not fully
22388d8558d1SAlan Stern  * enumerated.  The device descriptor is available, but not descriptors
2239d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
2240d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
2241d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
2242d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
2243d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
2244d9d16e8aSInaky Perez-Gonzalez  *
2245d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
2246d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
2247d9d16e8aSInaky Perez-Gonzalez  * errno value.
2248d9d16e8aSInaky Perez-Gonzalez  *
2249d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
2250d9d16e8aSInaky Perez-Gonzalez  *
2251d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
2252d9d16e8aSInaky Perez-Gonzalez  */
2253d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
2254d9d16e8aSInaky Perez-Gonzalez {
2255d9d16e8aSInaky Perez-Gonzalez 	int err;
2256d9d16e8aSInaky Perez-Gonzalez 
225716985408SDan Streetman 	if (udev->parent) {
225816985408SDan Streetman 		/* Initialize non-root-hub device wakeup to disabled;
225916985408SDan Streetman 		 * device (un)configuration controls wakeup capable
226016985408SDan Streetman 		 * sysfs power/wakeup controls wakeup enabled/disabled
226116985408SDan Streetman 		 */
226216985408SDan Streetman 		device_init_wakeup(&udev->dev, 0);
226316985408SDan Streetman 	}
226416985408SDan Streetman 
22659bbdf1e0SAlan Stern 	/* Tell the runtime-PM framework the device is active */
22669bbdf1e0SAlan Stern 	pm_runtime_set_active(&udev->dev);
2267c08512c7SAlan Stern 	pm_runtime_get_noresume(&udev->dev);
2268fcc4a01eSAlan Stern 	pm_runtime_use_autosuspend(&udev->dev);
22699bbdf1e0SAlan Stern 	pm_runtime_enable(&udev->dev);
22709bbdf1e0SAlan Stern 
2271c08512c7SAlan Stern 	/* By default, forbid autosuspend for all devices.  It will be
2272c08512c7SAlan Stern 	 * allowed for hubs during binding.
2273c08512c7SAlan Stern 	 */
2274c08512c7SAlan Stern 	usb_disable_autosuspend(udev);
2275c08512c7SAlan Stern 
22768d8558d1SAlan Stern 	err = usb_enumerate_device(udev);	/* Read descriptors */
2277d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
2278d9d16e8aSInaky Perez-Gonzalez 		goto fail;
2279c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2280c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
2281c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
22829f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
22839f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
22849f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
22859f8b17e6SKay Sievers 
22866cd13201SAlan Stern 	/* Tell the world! */
22876cd13201SAlan Stern 	announce_device(udev);
2288195af2ccSAlan Stern 
2289b04b3156STheodore Ts'o 	if (udev->serial)
2290b04b3156STheodore Ts'o 		add_device_randomness(udev->serial, strlen(udev->serial));
2291b04b3156STheodore Ts'o 	if (udev->product)
2292b04b3156STheodore Ts'o 		add_device_randomness(udev->product, strlen(udev->product));
2293b04b3156STheodore Ts'o 	if (udev->manufacturer)
2294b04b3156STheodore Ts'o 		add_device_randomness(udev->manufacturer,
2295b04b3156STheodore Ts'o 				      strlen(udev->manufacturer));
2296b04b3156STheodore Ts'o 
2297927bc916SRafael J. Wysocki 	device_enable_async_suspend(&udev->dev);
2298d35e70d5SMatthew Garrett 
2299d35e70d5SMatthew Garrett 	/*
2300d35e70d5SMatthew Garrett 	 * check whether the hub marks this port as non-removable. Do it
2301d35e70d5SMatthew Garrett 	 * now so that platform-specific data can override it in
2302d35e70d5SMatthew Garrett 	 * device_add()
2303d35e70d5SMatthew Garrett 	 */
2304d35e70d5SMatthew Garrett 	if (udev->parent)
2305d35e70d5SMatthew Garrett 		set_usb_port_removable(udev);
2306d35e70d5SMatthew Garrett 
2307782da727SAlan Stern 	/* Register the device.  The device driver is responsible
23083b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
23093b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
2310782da727SAlan Stern 	 */
23111da177e4SLinus Torvalds 	err = device_add(&udev->dev);
23121da177e4SLinus Torvalds 	if (err) {
23131da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
23141da177e4SLinus Torvalds 		goto fail;
23151da177e4SLinus Torvalds 	}
23169ad3d6ccSAlan Stern 
23173b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2318c08512c7SAlan Stern 	usb_mark_last_busy(udev);
2319c08512c7SAlan Stern 	pm_runtime_put_sync_autosuspend(&udev->dev);
2320c066475eSGreg Kroah-Hartman 	return err;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds fail:
23231da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
23249bbdf1e0SAlan Stern 	pm_runtime_disable(&udev->dev);
23259bbdf1e0SAlan Stern 	pm_runtime_set_suspended(&udev->dev);
2326d9d16e8aSInaky Perez-Gonzalez 	return err;
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds 
232993993a0aSInaky Perez-Gonzalez 
233093993a0aSInaky Perez-Gonzalez /**
2331fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2332fd39c86bSRandy Dunlap  * @usb_dev: USB device
2333fd39c86bSRandy Dunlap  *
2334fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
2335fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
233693993a0aSInaky Perez-Gonzalez  *
233793993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
233893993a0aSInaky Perez-Gonzalez  * defer its call.
233993993a0aSInaky Perez-Gonzalez  */
234093993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
234193993a0aSInaky Perez-Gonzalez {
234293993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
234393993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
234493993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
2345da307123SAlan Stern 
234693993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
234793993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
2348da307123SAlan Stern 
2349da307123SAlan Stern 	kfree(usb_dev->product);
235093993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2351da307123SAlan Stern 	kfree(usb_dev->manufacturer);
235293993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2353da307123SAlan Stern 	kfree(usb_dev->serial);
235493993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2355da307123SAlan Stern 
2356da307123SAlan Stern 	usb_destroy_configuration(usb_dev);
235793993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
2358da307123SAlan Stern 
235993993a0aSInaky Perez-Gonzalez out_unauthorized:
236093993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
236193993a0aSInaky Perez-Gonzalez 	return 0;
236293993a0aSInaky Perez-Gonzalez }
236393993a0aSInaky Perez-Gonzalez 
236493993a0aSInaky Perez-Gonzalez 
236593993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
236693993a0aSInaky Perez-Gonzalez {
236793993a0aSInaky Perez-Gonzalez 	int result = 0, c;
2368da307123SAlan Stern 
236993993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
237093993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
237193993a0aSInaky Perez-Gonzalez 		goto out_authorized;
2372da307123SAlan Stern 
237393993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
237493993a0aSInaky Perez-Gonzalez 	if (result < 0) {
237593993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
237693993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
237793993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
237893993a0aSInaky Perez-Gonzalez 	}
237993993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
238093993a0aSInaky Perez-Gonzalez 	if (result < 0) {
238193993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
238293993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
238393993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
238493993a0aSInaky Perez-Gonzalez 	}
2385da307123SAlan Stern 
2386da307123SAlan Stern 	kfree(usb_dev->product);
2387da307123SAlan Stern 	usb_dev->product = NULL;
2388da307123SAlan Stern 	kfree(usb_dev->manufacturer);
2389da307123SAlan Stern 	usb_dev->manufacturer = NULL;
2390da307123SAlan Stern 	kfree(usb_dev->serial);
2391da307123SAlan Stern 	usb_dev->serial = NULL;
2392da307123SAlan Stern 
239393993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
23948d8558d1SAlan Stern 	result = usb_enumerate_device(usb_dev);
239593993a0aSInaky Perez-Gonzalez 	if (result < 0)
23968d8558d1SAlan Stern 		goto error_enumerate;
239793993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
239893993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
239993993a0aSInaky Perez-Gonzalez 	 */
2400b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
240193993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
240293993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
240393993a0aSInaky Perez-Gonzalez 		if (result) {
240493993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
240593993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
240693993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
240793993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
240893993a0aSInaky Perez-Gonzalez 		}
240993993a0aSInaky Perez-Gonzalez 	}
241093993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
2411da307123SAlan Stern 
24128d8558d1SAlan Stern error_enumerate:
241393993a0aSInaky Perez-Gonzalez error_device_descriptor:
2414da307123SAlan Stern 	usb_autosuspend_device(usb_dev);
241593993a0aSInaky Perez-Gonzalez error_autoresume:
241693993a0aSInaky Perez-Gonzalez out_authorized:
241793993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
241893993a0aSInaky Perez-Gonzalez 	return result;
241993993a0aSInaky Perez-Gonzalez }
242093993a0aSInaky Perez-Gonzalez 
242193993a0aSInaky Perez-Gonzalez 
24220165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
24230165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
24240165de09SInaky Perez-Gonzalez {
24250165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
24260165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
24270165de09SInaky Perez-Gonzalez 		return 0;
24280165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
24290165de09SInaky Perez-Gonzalez 	return hcd->wireless;
24300165de09SInaky Perez-Gonzalez }
24310165de09SInaky Perez-Gonzalez 
24320165de09SInaky Perez-Gonzalez 
24331da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
24341da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
24351da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
24361da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
243790ab5ee9SRusty Russell #define USE_NEW_SCHEME(i)	((i) / 2 == (int)old_scheme_first)
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
24401da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
244175d7cf72SAndiry Xu #define HUB_BH_RESET_TIME	50
24421da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
24431da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
24441da177e4SLinus Torvalds 
244510d674a8SSarah Sharp static int hub_port_reset(struct usb_hub *hub, int port1,
244610d674a8SSarah Sharp 			struct usb_device *udev, unsigned int delay, bool warm);
244710d674a8SSarah Sharp 
24488bea2bd3SStanislaw Ledwon /* Is a USB 3.0 port in the Inactive or Complinance Mode state?
24498bea2bd3SStanislaw Ledwon  * Port worm reset is required to recover
24508bea2bd3SStanislaw Ledwon  */
24518bea2bd3SStanislaw Ledwon static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
245210d674a8SSarah Sharp {
245310d674a8SSarah Sharp 	return hub_is_superspeed(hub->hdev) &&
24548bea2bd3SStanislaw Ledwon 		(((portstatus & USB_PORT_STAT_LINK_STATE) ==
24558bea2bd3SStanislaw Ledwon 		  USB_SS_PORT_LS_SS_INACTIVE) ||
24568bea2bd3SStanislaw Ledwon 		 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
24578bea2bd3SStanislaw Ledwon 		  USB_SS_PORT_LS_COMP_MOD)) ;
245810d674a8SSarah Sharp }
245910d674a8SSarah Sharp 
24601da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
246175d7cf72SAndiry Xu 			struct usb_device *udev, unsigned int delay, bool warm)
24621da177e4SLinus Torvalds {
24631da177e4SLinus Torvalds 	int delay_time, ret;
24641da177e4SLinus Torvalds 	u16 portstatus;
24651da177e4SLinus Torvalds 	u16 portchange;
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 	for (delay_time = 0;
24681da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
24691da177e4SLinus Torvalds 			delay_time += delay) {
24701da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
24711da177e4SLinus Torvalds 		msleep(delay);
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 		/* read and decode port status */
24741da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
24751da177e4SLinus Torvalds 		if (ret < 0)
24761da177e4SLinus Torvalds 			return ret;
24771da177e4SLinus Torvalds 
247875d7cf72SAndiry Xu 		/*
247975d7cf72SAndiry Xu 		 * Some buggy devices require a warm reset to be issued even
248075d7cf72SAndiry Xu 		 * when the port appears not to be connected.
248175d7cf72SAndiry Xu 		 */
248275d7cf72SAndiry Xu 		if (!warm) {
248310d674a8SSarah Sharp 			/*
248410d674a8SSarah Sharp 			 * Some buggy devices can cause an NEC host controller
248510d674a8SSarah Sharp 			 * to transition to the "Error" state after a hot port
248610d674a8SSarah Sharp 			 * reset.  This will show up as the port state in
248710d674a8SSarah Sharp 			 * "Inactive", and the port may also report a
248810d674a8SSarah Sharp 			 * disconnect.  Forcing a warm port reset seems to make
248910d674a8SSarah Sharp 			 * the device work.
249010d674a8SSarah Sharp 			 *
249110d674a8SSarah Sharp 			 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
249210d674a8SSarah Sharp 			 */
24938bea2bd3SStanislaw Ledwon 			if (hub_port_warm_reset_required(hub, portstatus)) {
249410d674a8SSarah Sharp 				int ret;
249510d674a8SSarah Sharp 
249610d674a8SSarah Sharp 				if ((portchange & USB_PORT_STAT_C_CONNECTION))
249710d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
249810d674a8SSarah Sharp 							USB_PORT_FEAT_C_CONNECTION);
249910d674a8SSarah Sharp 				if (portchange & USB_PORT_STAT_C_LINK_STATE)
250010d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
250110d674a8SSarah Sharp 							USB_PORT_FEAT_C_PORT_LINK_STATE);
250210d674a8SSarah Sharp 				if (portchange & USB_PORT_STAT_C_RESET)
250310d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
250410d674a8SSarah Sharp 							USB_PORT_FEAT_C_RESET);
250510d674a8SSarah Sharp 				dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
250610d674a8SSarah Sharp 						port1);
250710d674a8SSarah Sharp 				ret = hub_port_reset(hub, port1,
250810d674a8SSarah Sharp 						udev, HUB_BH_RESET_TIME,
250910d674a8SSarah Sharp 						true);
251010d674a8SSarah Sharp 				if ((portchange & USB_PORT_STAT_C_CONNECTION))
251110d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
251210d674a8SSarah Sharp 							USB_PORT_FEAT_C_CONNECTION);
251310d674a8SSarah Sharp 				return ret;
251410d674a8SSarah Sharp 			}
25151da177e4SLinus Torvalds 			/* Device went away? */
25161da177e4SLinus Torvalds 			if (!(portstatus & USB_PORT_STAT_CONNECTION))
25171da177e4SLinus Torvalds 				return -ENOTCONN;
25181da177e4SLinus Torvalds 
2519dd4dd19eSAlan Stern 			/* bomb out completely if the connection bounced */
25201da177e4SLinus Torvalds 			if ((portchange & USB_PORT_STAT_C_CONNECTION))
2521dd4dd19eSAlan Stern 				return -ENOTCONN;
25221da177e4SLinus Torvalds 
252375d7cf72SAndiry Xu 			/* if we`ve finished resetting, then break out of
252475d7cf72SAndiry Xu 			 * the loop
252575d7cf72SAndiry Xu 			 */
25261da177e4SLinus Torvalds 			if (!(portstatus & USB_PORT_STAT_RESET) &&
25271da177e4SLinus Torvalds 			    (portstatus & USB_PORT_STAT_ENABLE)) {
25280165de09SInaky Perez-Gonzalez 				if (hub_is_wusb(hub))
2529551cdbbeSGreg Kroah-Hartman 					udev->speed = USB_SPEED_WIRELESS;
2530131dec34SSarah Sharp 				else if (hub_is_superspeed(hub->hdev))
2531809cd1cbSSarah Sharp 					udev->speed = USB_SPEED_SUPER;
25320165de09SInaky Perez-Gonzalez 				else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
25331da177e4SLinus Torvalds 					udev->speed = USB_SPEED_HIGH;
25341da177e4SLinus Torvalds 				else if (portstatus & USB_PORT_STAT_LOW_SPEED)
25351da177e4SLinus Torvalds 					udev->speed = USB_SPEED_LOW;
25361da177e4SLinus Torvalds 				else
25371da177e4SLinus Torvalds 					udev->speed = USB_SPEED_FULL;
25381da177e4SLinus Torvalds 				return 0;
25391da177e4SLinus Torvalds 			}
254075d7cf72SAndiry Xu 		} else {
254175d7cf72SAndiry Xu 			if (portchange & USB_PORT_STAT_C_BH_RESET)
254275d7cf72SAndiry Xu 				return 0;
254375d7cf72SAndiry Xu 		}
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
25461da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
25471da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
255075d7cf72SAndiry Xu 			"port %d not %sreset yet, waiting %dms\n",
255175d7cf72SAndiry Xu 			port1, warm ? "warm " : "", delay);
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	return -EBUSY;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
255775d7cf72SAndiry Xu static void hub_port_finish_reset(struct usb_hub *hub, int port1,
255875d7cf72SAndiry Xu 			struct usb_device *udev, int *status, bool warm)
25591da177e4SLinus Torvalds {
256075d7cf72SAndiry Xu 	switch (*status) {
25611da177e4SLinus Torvalds 	case 0:
256275d7cf72SAndiry Xu 		if (!warm) {
256375d7cf72SAndiry Xu 			struct usb_hcd *hcd;
2564b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
2565b789696aSDavid Brownell 			msleep(10 + 40);
25663b29b68bSAlan Stern 			update_devnum(udev, 0);
256775d7cf72SAndiry Xu 			hcd = bus_to_hcd(udev->bus);
2568a5f0efabSSarah Sharp 			if (hcd->driver->reset_device) {
256975d7cf72SAndiry Xu 				*status = hcd->driver->reset_device(hcd, udev);
257075d7cf72SAndiry Xu 				if (*status < 0) {
2571a5f0efabSSarah Sharp 					dev_err(&udev->dev, "Cannot reset "
2572a5f0efabSSarah Sharp 							"HCD device state\n");
2573a5f0efabSSarah Sharp 					break;
2574a5f0efabSSarah Sharp 				}
2575a5f0efabSSarah Sharp 			}
257675d7cf72SAndiry Xu 		}
25771da177e4SLinus Torvalds 		/* FALL THROUGH */
25781da177e4SLinus Torvalds 	case -ENOTCONN:
25791da177e4SLinus Torvalds 	case -ENODEV:
25801da177e4SLinus Torvalds 		clear_port_feature(hub->hdev,
25811da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
25821da177e4SLinus Torvalds 		/* FIXME need disconnect() for NOTATTACHED device */
25831c7439c6SSarah Sharp 		if (hub_is_superspeed(hub->hdev)) {
258475d7cf72SAndiry Xu 			clear_port_feature(hub->hdev, port1,
258575d7cf72SAndiry Xu 					USB_PORT_FEAT_C_BH_PORT_RESET);
258675d7cf72SAndiry Xu 			clear_port_feature(hub->hdev, port1,
258775d7cf72SAndiry Xu 					USB_PORT_FEAT_C_PORT_LINK_STATE);
25881c7439c6SSarah Sharp 		}
25891c7439c6SSarah Sharp 		if (!warm)
259075d7cf72SAndiry Xu 			usb_set_device_state(udev, *status
25911da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
25921da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
259375d7cf72SAndiry Xu 		break;
259475d7cf72SAndiry Xu 	}
259575d7cf72SAndiry Xu }
259675d7cf72SAndiry Xu 
259775d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
259875d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1,
259975d7cf72SAndiry Xu 			struct usb_device *udev, unsigned int delay, bool warm)
260075d7cf72SAndiry Xu {
260175d7cf72SAndiry Xu 	int i, status;
260275d7cf72SAndiry Xu 
260375d7cf72SAndiry Xu 	if (!warm) {
260475d7cf72SAndiry Xu 		/* Block EHCI CF initialization during the port reset.
260575d7cf72SAndiry Xu 		 * Some companion controllers don't like it when they mix.
260675d7cf72SAndiry Xu 		 */
260775d7cf72SAndiry Xu 		down_read(&ehci_cf_port_reset_rwsem);
260875d7cf72SAndiry Xu 	} else {
260975d7cf72SAndiry Xu 		if (!hub_is_superspeed(hub->hdev)) {
261075d7cf72SAndiry Xu 			dev_err(hub->intfdev, "only USB3 hub support "
261175d7cf72SAndiry Xu 						"warm reset\n");
261275d7cf72SAndiry Xu 			return -EINVAL;
261375d7cf72SAndiry Xu 		}
261475d7cf72SAndiry Xu 	}
261575d7cf72SAndiry Xu 
261675d7cf72SAndiry Xu 	/* Reset the port */
261775d7cf72SAndiry Xu 	for (i = 0; i < PORT_RESET_TRIES; i++) {
261875d7cf72SAndiry Xu 		status = set_port_feature(hub->hdev, port1, (warm ?
261975d7cf72SAndiry Xu 					USB_PORT_FEAT_BH_PORT_RESET :
262075d7cf72SAndiry Xu 					USB_PORT_FEAT_RESET));
262175d7cf72SAndiry Xu 		if (status) {
262275d7cf72SAndiry Xu 			dev_err(hub->intfdev,
262375d7cf72SAndiry Xu 					"cannot %sreset port %d (err = %d)\n",
262475d7cf72SAndiry Xu 					warm ? "warm " : "", port1, status);
262575d7cf72SAndiry Xu 		} else {
262675d7cf72SAndiry Xu 			status = hub_port_wait_reset(hub, port1, udev, delay,
262775d7cf72SAndiry Xu 								warm);
262875d7cf72SAndiry Xu 			if (status && status != -ENOTCONN)
262975d7cf72SAndiry Xu 				dev_dbg(hub->intfdev,
263075d7cf72SAndiry Xu 						"port_wait_reset: err = %d\n",
263175d7cf72SAndiry Xu 						status);
263275d7cf72SAndiry Xu 		}
263375d7cf72SAndiry Xu 
263475d7cf72SAndiry Xu 		/* return on disconnect or reset */
263575d7cf72SAndiry Xu 		if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
263675d7cf72SAndiry Xu 			hub_port_finish_reset(hub, port1, udev, &status, warm);
263732fe0198SAlan Stern 			goto done;
26381da177e4SLinus Torvalds 		}
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
264175d7cf72SAndiry Xu 			"port %d not enabled, trying %sreset again...\n",
264275d7cf72SAndiry Xu 			port1, warm ? "warm " : "");
26431da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
26441da177e4SLinus Torvalds 	}
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds 	dev_err (hub->intfdev,
26471da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
26481da177e4SLinus Torvalds 		port1);
26491da177e4SLinus Torvalds 
265032fe0198SAlan Stern done:
265175d7cf72SAndiry Xu 	if (!warm)
265232fe0198SAlan Stern 		up_read(&ehci_cf_port_reset_rwsem);
265375d7cf72SAndiry Xu 
26541da177e4SLinus Torvalds 	return status;
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
26570ed9a57eSAndiry Xu /* Check if a port is power on */
26580ed9a57eSAndiry Xu static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
26590ed9a57eSAndiry Xu {
26600ed9a57eSAndiry Xu 	int ret = 0;
26610ed9a57eSAndiry Xu 
26620ed9a57eSAndiry Xu 	if (hub_is_superspeed(hub->hdev)) {
26630ed9a57eSAndiry Xu 		if (portstatus & USB_SS_PORT_STAT_POWER)
26640ed9a57eSAndiry Xu 			ret = 1;
26650ed9a57eSAndiry Xu 	} else {
26660ed9a57eSAndiry Xu 		if (portstatus & USB_PORT_STAT_POWER)
26670ed9a57eSAndiry Xu 			ret = 1;
26680ed9a57eSAndiry Xu 	}
26690ed9a57eSAndiry Xu 
26700ed9a57eSAndiry Xu 	return ret;
26710ed9a57eSAndiry Xu }
26720ed9a57eSAndiry Xu 
2673d388dab7SAlan Stern #ifdef	CONFIG_PM
26741da177e4SLinus Torvalds 
26750ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
26760ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
26770ed9a57eSAndiry Xu {
26780ed9a57eSAndiry Xu 	int ret = 0;
26790ed9a57eSAndiry Xu 
26800ed9a57eSAndiry Xu 	if (hub_is_superspeed(hub->hdev)) {
26810ed9a57eSAndiry Xu 		if ((portstatus & USB_PORT_STAT_LINK_STATE)
26820ed9a57eSAndiry Xu 				== USB_SS_PORT_LS_U3)
26830ed9a57eSAndiry Xu 			ret = 1;
26840ed9a57eSAndiry Xu 	} else {
26850ed9a57eSAndiry Xu 		if (portstatus & USB_PORT_STAT_SUSPEND)
26860ed9a57eSAndiry Xu 			ret = 1;
26870ed9a57eSAndiry Xu 	}
26880ed9a57eSAndiry Xu 
26890ed9a57eSAndiry Xu 	return ret;
26900ed9a57eSAndiry Xu }
2691b01b03f3SAlan Stern 
2692b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2693b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2694b01b03f3SAlan Stern  */
2695b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2696b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2697b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2698b01b03f3SAlan Stern {
2699b01b03f3SAlan Stern 	/* Is the device still present? */
27000ed9a57eSAndiry Xu 	if (status || port_is_suspended(hub, portstatus) ||
27010ed9a57eSAndiry Xu 			!port_is_power_on(hub, portstatus) ||
27020ed9a57eSAndiry Xu 			!(portstatus & USB_PORT_STAT_CONNECTION)) {
2703b01b03f3SAlan Stern 		if (status >= 0)
2704b01b03f3SAlan Stern 			status = -ENODEV;
2705b01b03f3SAlan Stern 	}
2706b01b03f3SAlan Stern 
270786c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
270886c57edfSAlan Stern 	 * so try a reset-resume instead.
270986c57edfSAlan Stern 	 */
271086c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
271186c57edfSAlan Stern 		if (udev->persist_enabled)
271286c57edfSAlan Stern 			udev->reset_resume = 1;
271386c57edfSAlan Stern 		else
2714b01b03f3SAlan Stern 			status = -ENODEV;
271586c57edfSAlan Stern 	}
2716b01b03f3SAlan Stern 
2717b01b03f3SAlan Stern 	if (status) {
2718b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
2719b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
2720b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
2721b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2722b01b03f3SAlan Stern 
2723b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2724b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2725b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2726b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2727b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2728b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2729b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2730b01b03f3SAlan Stern 	}
2731b01b03f3SAlan Stern 
2732b01b03f3SAlan Stern 	return status;
2733b01b03f3SAlan Stern }
2734b01b03f3SAlan Stern 
2735f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
2736f74631e3SSarah Sharp {
2737f74631e3SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2738f74631e3SSarah Sharp 
2739f74631e3SSarah Sharp 	/* Check if the roothub and device supports LTM. */
2740f74631e3SSarah Sharp 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2741f74631e3SSarah Sharp 			!usb_device_supports_ltm(udev))
2742f74631e3SSarah Sharp 		return 0;
2743f74631e3SSarah Sharp 
2744f74631e3SSarah Sharp 	/* Clear Feature LTM Enable can only be sent if the device is
2745f74631e3SSarah Sharp 	 * configured.
2746f74631e3SSarah Sharp 	 */
2747f74631e3SSarah Sharp 	if (!udev->actconfig)
2748f74631e3SSarah Sharp 		return 0;
2749f74631e3SSarah Sharp 
2750f74631e3SSarah Sharp 	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2751f74631e3SSarah Sharp 			USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2752f74631e3SSarah Sharp 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2753f74631e3SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
2754f74631e3SSarah Sharp }
2755f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
2756f74631e3SSarah Sharp 
2757f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev)
2758f74631e3SSarah Sharp {
2759f74631e3SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2760f74631e3SSarah Sharp 
2761f74631e3SSarah Sharp 	/* Check if the roothub and device supports LTM. */
2762f74631e3SSarah Sharp 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2763f74631e3SSarah Sharp 			!usb_device_supports_ltm(udev))
2764f74631e3SSarah Sharp 		return;
2765f74631e3SSarah Sharp 
2766f74631e3SSarah Sharp 	/* Set Feature LTM Enable can only be sent if the device is
2767f74631e3SSarah Sharp 	 * configured.
2768f74631e3SSarah Sharp 	 */
2769f74631e3SSarah Sharp 	if (!udev->actconfig)
2770f74631e3SSarah Sharp 		return;
2771f74631e3SSarah Sharp 
2772f74631e3SSarah Sharp 	usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2773f74631e3SSarah Sharp 			USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2774f74631e3SSarah Sharp 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2775f74631e3SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
2776f74631e3SSarah Sharp }
2777f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
2778f74631e3SSarah Sharp 
27791da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds /*
2782140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
2783624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
27845edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
27851da177e4SLinus Torvalds  *
27861da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
27871da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
27881da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
2789140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
27901da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
27911da177e4SLinus Torvalds  *
2792390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
2793390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
2794390a8c34SDavid Brownell  *
2795624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
2796624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
2797624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
2798624d6c07SAlan Stern  *
2799624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
2800624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
2801624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
2802624d6c07SAlan Stern  * some cases, this wakes the USB host.
2803624d6c07SAlan Stern  *
28041da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
28051da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
28061da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
28071da177e4SLinus Torvalds  *
28084956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
28094956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
28104956eccdSAlan Stern  * State transitions include:
28114956eccdSAlan Stern  *
28124956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
28134956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
28144956eccdSAlan Stern  *
28154956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
28164956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
28174956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
28184956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
28194956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
28204956eccdSAlan Stern  *
28214956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
28224956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
28234956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
28244956eccdSAlan Stern  *
28251da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
28261da177e4SLinus Torvalds  */
282765bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
28281da177e4SLinus Torvalds {
2829624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2830624d6c07SAlan Stern 	int		port1 = udev->portnum;
2831624d6c07SAlan Stern 	int		status;
28324956eccdSAlan Stern 
2833624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
2834624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
2835624d6c07SAlan Stern 	 *
2836624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2837624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2838624d6c07SAlan Stern 	 */
2839624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2840623bef9eSSarah Sharp 		if (!hub_is_superspeed(hub->hdev)) {
2841624d6c07SAlan Stern 			status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2842624d6c07SAlan Stern 					USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2843624d6c07SAlan Stern 					USB_DEVICE_REMOTE_WAKEUP, 0,
2844624d6c07SAlan Stern 					NULL, 0,
2845624d6c07SAlan Stern 					USB_CTRL_SET_TIMEOUT);
2846623bef9eSSarah Sharp 		} else {
2847623bef9eSSarah Sharp 			/* Assume there's only one function on the USB 3.0
2848623bef9eSSarah Sharp 			 * device and enable remote wake for the first
2849623bef9eSSarah Sharp 			 * interface. FIXME if the interface association
2850623bef9eSSarah Sharp 			 * descriptor shows there's more than one function.
2851623bef9eSSarah Sharp 			 */
2852623bef9eSSarah Sharp 			status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2853623bef9eSSarah Sharp 					USB_REQ_SET_FEATURE,
2854623bef9eSSarah Sharp 					USB_RECIP_INTERFACE,
2855623bef9eSSarah Sharp 					USB_INTRF_FUNC_SUSPEND,
28563b9b6acdSSarah Sharp 					USB_INTRF_FUNC_SUSPEND_RW |
28573b9b6acdSSarah Sharp 					USB_INTRF_FUNC_SUSPEND_LP,
2858623bef9eSSarah Sharp 					NULL, 0,
2859623bef9eSSarah Sharp 					USB_CTRL_SET_TIMEOUT);
2860623bef9eSSarah Sharp 		}
28610c487206SOliver Neukum 		if (status) {
2862624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2863624d6c07SAlan Stern 					status);
28640c487206SOliver Neukum 			/* bail if autosuspend is requested */
28655b1b0b81SAlan Stern 			if (PMSG_IS_AUTO(msg))
28660c487206SOliver Neukum 				return status;
28670c487206SOliver Neukum 		}
2868624d6c07SAlan Stern 	}
2869624d6c07SAlan Stern 
287065580b43SAndiry Xu 	/* disable USB2 hardware LPM */
287165580b43SAndiry Xu 	if (udev->usb2_hw_lpm_enabled == 1)
287265580b43SAndiry Xu 		usb_set_usb2_hardware_lpm(udev, 0);
287365580b43SAndiry Xu 
2874f74631e3SSarah Sharp 	if (usb_disable_ltm(udev)) {
2875f74631e3SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2876f74631e3SSarah Sharp 				__func__);
2877f74631e3SSarah Sharp 		return -ENOMEM;
2878f74631e3SSarah Sharp 	}
28798306095fSSarah Sharp 	if (usb_unlocked_disable_lpm(udev)) {
28808306095fSSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
28818306095fSSarah Sharp 				__func__);
28828306095fSSarah Sharp 		return -ENOMEM;
28838306095fSSarah Sharp 	}
28848306095fSSarah Sharp 
2885624d6c07SAlan Stern 	/* see 7.1.7.6 */
2886a7114230SAndiry Xu 	if (hub_is_superspeed(hub->hdev))
2887a7114230SAndiry Xu 		status = set_port_feature(hub->hdev,
2888a7114230SAndiry Xu 				port1 | (USB_SS_PORT_LS_U3 << 3),
2889a7114230SAndiry Xu 				USB_PORT_FEAT_LINK_STATE);
2890a8f08d86SAndiry Xu 	else
2891a8f08d86SAndiry Xu 		status = set_port_feature(hub->hdev, port1,
2892a8f08d86SAndiry Xu 						USB_PORT_FEAT_SUSPEND);
2893624d6c07SAlan Stern 	if (status) {
2894624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2895624d6c07SAlan Stern 				port1, status);
2896624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
28970c487206SOliver Neukum 		if (udev->do_remote_wakeup)
2898624d6c07SAlan Stern 			(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2899624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2900624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2901624d6c07SAlan Stern 				NULL, 0,
2902624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2903cbb33004SAlan Stern 
2904c3e751e4SAndiry Xu 		/* Try to enable USB2 hardware LPM again */
2905c3e751e4SAndiry Xu 		if (udev->usb2_hw_lpm_capable == 1)
2906c3e751e4SAndiry Xu 			usb_set_usb2_hardware_lpm(udev, 1);
2907c3e751e4SAndiry Xu 
2908f74631e3SSarah Sharp 		/* Try to enable USB3 LTM and LPM again */
2909f74631e3SSarah Sharp 		usb_enable_ltm(udev);
29108306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
29118306095fSSarah Sharp 
2912cbb33004SAlan Stern 		/* System sleep transitions should never fail */
29135b1b0b81SAlan Stern 		if (!PMSG_IS_AUTO(msg))
2914cbb33004SAlan Stern 			status = 0;
2915624d6c07SAlan Stern 	} else {
2916624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
291730b1a7a3SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
291830b1a7a3SAlan Stern 				(PMSG_IS_AUTO(msg) ? "auto-" : ""),
291930b1a7a3SAlan Stern 				udev->do_remote_wakeup);
2920624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2921bfd1e910SAlan Stern 		udev->port_is_suspended = 1;
2922624d6c07SAlan Stern 		msleep(10);
2923624d6c07SAlan Stern 	}
2924c08512c7SAlan Stern 	usb_mark_last_busy(hub->hdev);
29254956eccdSAlan Stern 	return status;
29261da177e4SLinus Torvalds }
2927f3f3253dSDavid Brownell 
29281da177e4SLinus Torvalds /*
2929390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2930390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
293154515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
29321da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
29331da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
29341da177e4SLinus Torvalds  * in the tree that's rooted at this device.
293554515fe5SAlan Stern  *
293654515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
293754515fe5SAlan Stern  * status check is done.
29381da177e4SLinus Torvalds  */
2939140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
29401da177e4SLinus Torvalds {
294154515fe5SAlan Stern 	int	status = 0;
29421da177e4SLinus Torvalds 	u16	devstatus;
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2945b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2946b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
29491da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
29501da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
29511da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
29521da177e4SLinus Torvalds 	 */
29531da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
29541da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
29551da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
29561da177e4SLinus Torvalds 
295754515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
295854515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
295954515fe5SAlan Stern 	 * operation is carried out here, after the port has been
296054515fe5SAlan Stern 	 * resumed.
296154515fe5SAlan Stern 	 */
296254515fe5SAlan Stern 	if (udev->reset_resume)
296386c57edfSAlan Stern  retry_reset_resume:
2964742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
296554515fe5SAlan Stern 
29661da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
29671da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
29681da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
29691da177e4SLinus Torvalds 	 */
297054515fe5SAlan Stern 	if (status == 0) {
297146dede46SAlan Stern 		devstatus = 0;
29721da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2973b40b7a90SAlan Stern 		if (status >= 0)
297446dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
297586c57edfSAlan Stern 
297686c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
297786c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
297886c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
297986c57edfSAlan Stern 			udev->reset_resume = 1;
298086c57edfSAlan Stern 			goto retry_reset_resume;
298186c57edfSAlan Stern 		}
298254515fe5SAlan Stern 	}
2983b40b7a90SAlan Stern 
2984624d6c07SAlan Stern 	if (status) {
2985624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
29861da177e4SLinus Torvalds 				status);
2987624d6c07SAlan Stern 	} else if (udev->actconfig) {
29881da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2989686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
29901da177e4SLinus Torvalds 			status = usb_control_msg(udev,
29911da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
29921da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
29931da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
29941da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
29951da177e4SLinus Torvalds 					NULL, 0,
29961da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2997a8e7c565SAlan Stern 			if (status)
2998b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2999b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
3000b9cef6c3SWu Fengguang 					status);
30014bf0ba86SAlan Stern 		}
30021da177e4SLinus Torvalds 		status = 0;
30031da177e4SLinus Torvalds 	}
30041da177e4SLinus Torvalds 	return status;
30051da177e4SLinus Torvalds }
30061da177e4SLinus Torvalds 
3007624d6c07SAlan Stern /*
3008624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
3009624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
3010624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
3011624d6c07SAlan Stern  *
3012624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
3013624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
3014624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
3015624d6c07SAlan Stern  * the host and the device is the same as it was when the device
3016624d6c07SAlan Stern  * suspended.
3017624d6c07SAlan Stern  *
3018feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
3019feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
302054515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
302154515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
302254515fe5SAlan Stern  *
302354515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
302454515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
302554515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
302654515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
302754515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
302854515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
302954515fe5SAlan Stern  * made to appear as if it had not disconnected.
303054515fe5SAlan Stern  *
3031742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
3032feccc30dSAlan Stern  * every effort to insure that the same device is present after the
303354515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
303454515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
303554515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
303654515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
303754515fe5SAlan Stern  * new card is corrupted and the system crashes.
303854515fe5SAlan Stern  *
3039624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
3040624d6c07SAlan Stern  */
304165bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
30421da177e4SLinus Torvalds {
3043624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
3044624d6c07SAlan Stern 	int		port1 = udev->portnum;
30451da177e4SLinus Torvalds 	int		status;
3046d25450c6SAlan Stern 	u16		portchange, portstatus;
3047d25450c6SAlan Stern 
3048d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
3049d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
30500ed9a57eSAndiry Xu 	if (status == 0 && !port_is_suspended(hub, portstatus))
3051d25450c6SAlan Stern 		goto SuspendCleared;
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
30541da177e4SLinus Torvalds 
3055d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
3056d5cbad4bSAlan Stern 
30571da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
3058a7114230SAndiry Xu 	if (hub_is_superspeed(hub->hdev))
3059a7114230SAndiry Xu 		status = set_port_feature(hub->hdev,
3060a7114230SAndiry Xu 				port1 | (USB_SS_PORT_LS_U0 << 3),
3061a7114230SAndiry Xu 				USB_PORT_FEAT_LINK_STATE);
3062a7114230SAndiry Xu 	else
30631da177e4SLinus Torvalds 		status = clear_port_feature(hub->hdev,
30641da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_SUSPEND);
30651da177e4SLinus Torvalds 	if (status) {
3066624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
30671da177e4SLinus Torvalds 				port1, status);
30681da177e4SLinus Torvalds 	} else {
30691da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
3070645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
30715b1b0b81SAlan Stern 				(PMSG_IS_AUTO(msg) ? "auto-" : ""));
30721da177e4SLinus Torvalds 		msleep(25);
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
30751da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
30761da177e4SLinus Torvalds 		 * sequence.
30771da177e4SLinus Torvalds 		 */
3078d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
307954515fe5SAlan Stern 
30801da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
30811da177e4SLinus Torvalds 		msleep(10);
30821da177e4SLinus Torvalds 	}
3083b01b03f3SAlan Stern 
3084b01b03f3SAlan Stern  SuspendCleared:
3085b01b03f3SAlan Stern 	if (status == 0) {
3086bfd1e910SAlan Stern 		udev->port_is_suspended = 0;
3087a7114230SAndiry Xu 		if (hub_is_superspeed(hub->hdev)) {
3088a7114230SAndiry Xu 			if (portchange & USB_PORT_STAT_C_LINK_STATE)
3089a7114230SAndiry Xu 				clear_port_feature(hub->hdev, port1,
3090a7114230SAndiry Xu 					USB_PORT_FEAT_C_PORT_LINK_STATE);
3091a7114230SAndiry Xu 		} else {
3092b01b03f3SAlan Stern 			if (portchange & USB_PORT_STAT_C_SUSPEND)
3093b01b03f3SAlan Stern 				clear_port_feature(hub->hdev, port1,
3094b01b03f3SAlan Stern 						USB_PORT_FEAT_C_SUSPEND);
30951da177e4SLinus Torvalds 		}
3096a7114230SAndiry Xu 	}
30971da177e4SLinus Torvalds 
3098d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
3099d5cbad4bSAlan Stern 
3100b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
3101b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
310254515fe5SAlan Stern 	if (status == 0)
310354515fe5SAlan Stern 		status = finish_port_resume(udev);
310454515fe5SAlan Stern 	if (status < 0) {
310554515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
310654515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
310765580b43SAndiry Xu 	} else  {
310865580b43SAndiry Xu 		/* Try to enable USB2 hardware LPM */
310965580b43SAndiry Xu 		if (udev->usb2_hw_lpm_capable == 1)
311065580b43SAndiry Xu 			usb_set_usb2_hardware_lpm(udev, 1);
31118306095fSSarah Sharp 
3112f74631e3SSarah Sharp 		/* Try to enable USB3 LTM and LPM */
3113f74631e3SSarah Sharp 		usb_enable_ltm(udev);
31148306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
311554515fe5SAlan Stern 	}
311665580b43SAndiry Xu 
31171da177e4SLinus Torvalds 	return status;
31181da177e4SLinus Torvalds }
31191da177e4SLinus Torvalds 
31208808f00cSAlan Stern /* caller has locked udev */
31210534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev)
31221da177e4SLinus Torvalds {
31231da177e4SLinus Torvalds 	int	status = 0;
31241da177e4SLinus Torvalds 
31251da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
3126645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
31279bbdf1e0SAlan Stern 		status = usb_autoresume_device(udev);
31289bbdf1e0SAlan Stern 		if (status == 0) {
31299bbdf1e0SAlan Stern 			/* Let the drivers do their thing, then... */
31309bbdf1e0SAlan Stern 			usb_autosuspend_device(udev);
31319bbdf1e0SAlan Stern 		}
3132d25450c6SAlan Stern 	}
31331da177e4SLinus Torvalds 	return status;
31341da177e4SLinus Torvalds }
31351da177e4SLinus Torvalds 
3136d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
3137d388dab7SAlan Stern 
3138d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3139d388dab7SAlan Stern 
314065bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3141d388dab7SAlan Stern {
3142d388dab7SAlan Stern 	return 0;
3143d388dab7SAlan Stern }
3144d388dab7SAlan Stern 
3145b01b03f3SAlan Stern /* However we may need to do a reset-resume */
3146b01b03f3SAlan Stern 
314765bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3148d388dab7SAlan Stern {
3149b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
3150b01b03f3SAlan Stern 	int		port1 = udev->portnum;
3151b01b03f3SAlan Stern 	int		status;
3152b01b03f3SAlan Stern 	u16		portchange, portstatus;
315354515fe5SAlan Stern 
3154b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
3155b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
3156b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
3157b01b03f3SAlan Stern 
3158b01b03f3SAlan Stern 	if (status) {
3159b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3160b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
3161b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
316254515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
3163742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
316454515fe5SAlan Stern 	}
316554515fe5SAlan Stern 	return status;
3166d388dab7SAlan Stern }
3167d388dab7SAlan Stern 
3168d388dab7SAlan Stern #endif
3169d388dab7SAlan Stern 
3170e6f30deaSMing Lei static int check_ports_changed(struct usb_hub *hub)
3171e6f30deaSMing Lei {
3172e6f30deaSMing Lei 	int port1;
3173e6f30deaSMing Lei 
3174e6f30deaSMing Lei 	for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3175e6f30deaSMing Lei 		u16 portstatus, portchange;
3176e6f30deaSMing Lei 		int status;
3177e6f30deaSMing Lei 
3178e6f30deaSMing Lei 		status = hub_port_status(hub, port1, &portstatus, &portchange);
3179e6f30deaSMing Lei 		if (!status && portchange)
3180e6f30deaSMing Lei 			return 1;
3181e6f30deaSMing Lei 	}
3182e6f30deaSMing Lei 	return 0;
3183e6f30deaSMing Lei }
3184e6f30deaSMing Lei 
3185db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
31861da177e4SLinus Torvalds {
31871da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
31881da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
31891da177e4SLinus Torvalds 	unsigned		port1;
31904296c70aSSarah Sharp 	int			status;
31911da177e4SLinus Torvalds 
3192cbb33004SAlan Stern 	/* Warn if children aren't already suspended */
31931da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
31941da177e4SLinus Torvalds 		struct usb_device	*udev;
31951da177e4SLinus Torvalds 
3196ff823c79SLan Tianyu 		udev = hub->ports[port1 - 1]->child;
31976840d255SAlan Stern 		if (udev && udev->can_submit) {
3198cbb33004SAlan Stern 			dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
31995b1b0b81SAlan Stern 			if (PMSG_IS_AUTO(msg))
3200c9f89fa4SDavid Brownell 				return -EBUSY;
3201c9f89fa4SDavid Brownell 		}
32021da177e4SLinus Torvalds 	}
3203e6f30deaSMing Lei 
3204e6f30deaSMing Lei 	if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3205e6f30deaSMing Lei 		/* check if there are changes pending on hub ports */
3206e6f30deaSMing Lei 		if (check_ports_changed(hub)) {
3207e6f30deaSMing Lei 			if (PMSG_IS_AUTO(msg))
3208e6f30deaSMing Lei 				return -EBUSY;
3209e6f30deaSMing Lei 			pm_wakeup_event(&hdev->dev, 2000);
3210e6f30deaSMing Lei 		}
3211e6f30deaSMing Lei 	}
3212e6f30deaSMing Lei 
32134296c70aSSarah Sharp 	if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
32144296c70aSSarah Sharp 		/* Enable hub to send remote wakeup for all ports. */
32154296c70aSSarah Sharp 		for (port1 = 1; port1 <= hdev->maxchild; port1++) {
32164296c70aSSarah Sharp 			status = set_port_feature(hdev,
32174296c70aSSarah Sharp 					port1 |
32184296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
32194296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
32204296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
32214296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_MASK);
32224296c70aSSarah Sharp 		}
32234296c70aSSarah Sharp 	}
32241da177e4SLinus Torvalds 
3225441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
322640f122f3SAlan Stern 
3227c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
32284330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
3229b6f6436dSAlan Stern 	return 0;
32301da177e4SLinus Torvalds }
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
32331da177e4SLinus Torvalds {
32341da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
32351da177e4SLinus Torvalds 
32365e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
3237f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
32381da177e4SLinus Torvalds 	return 0;
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
3241b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
3242f07600cfSAlan Stern {
3243b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
3244f07600cfSAlan Stern 
32455e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
3246f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
3247f07600cfSAlan Stern 	return 0;
3248f07600cfSAlan Stern }
3249f07600cfSAlan Stern 
325054515fe5SAlan Stern /**
325154515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
325254515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
325354515fe5SAlan Stern  *
325454515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
325554515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
3256feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
3257feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
3258feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3259feccc30dSAlan Stern  * the others will be disconnected.
326054515fe5SAlan Stern  */
326154515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
326254515fe5SAlan Stern {
326354515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
326454515fe5SAlan Stern 	rhdev->reset_resume = 1;
326554515fe5SAlan Stern }
326654515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
326754515fe5SAlan Stern 
32681ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[]  = {
32691ea7e0e8SSarah Sharp 	"U0",
32701ea7e0e8SSarah Sharp 	"U1",
32711ea7e0e8SSarah Sharp 	"U2",
32721ea7e0e8SSarah Sharp 	"U3",
32731ea7e0e8SSarah Sharp };
32741ea7e0e8SSarah Sharp 
32751ea7e0e8SSarah Sharp /*
32761ea7e0e8SSarah Sharp  * Send a Set SEL control transfer to the device, prior to enabling
32771ea7e0e8SSarah Sharp  * device-initiated U1 or U2.  This lets the device know the exit latencies from
32781ea7e0e8SSarah Sharp  * the time the device initiates a U1 or U2 exit, to the time it will receive a
32791ea7e0e8SSarah Sharp  * packet from the host.
32801ea7e0e8SSarah Sharp  *
32811ea7e0e8SSarah Sharp  * This function will fail if the SEL or PEL values for udev are greater than
32821ea7e0e8SSarah Sharp  * the maximum allowed values for the link state to be enabled.
32831ea7e0e8SSarah Sharp  */
32841ea7e0e8SSarah Sharp static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
32851ea7e0e8SSarah Sharp {
32861ea7e0e8SSarah Sharp 	struct usb_set_sel_req *sel_values;
32871ea7e0e8SSarah Sharp 	unsigned long long u1_sel;
32881ea7e0e8SSarah Sharp 	unsigned long long u1_pel;
32891ea7e0e8SSarah Sharp 	unsigned long long u2_sel;
32901ea7e0e8SSarah Sharp 	unsigned long long u2_pel;
32911ea7e0e8SSarah Sharp 	int ret;
32921ea7e0e8SSarah Sharp 
32931ea7e0e8SSarah Sharp 	/* Convert SEL and PEL stored in ns to us */
32941ea7e0e8SSarah Sharp 	u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
32951ea7e0e8SSarah Sharp 	u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
32961ea7e0e8SSarah Sharp 	u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
32971ea7e0e8SSarah Sharp 	u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
32981ea7e0e8SSarah Sharp 
32991ea7e0e8SSarah Sharp 	/*
33001ea7e0e8SSarah Sharp 	 * Make sure that the calculated SEL and PEL values for the link
33011ea7e0e8SSarah Sharp 	 * state we're enabling aren't bigger than the max SEL/PEL
33021ea7e0e8SSarah Sharp 	 * value that will fit in the SET SEL control transfer.
33031ea7e0e8SSarah Sharp 	 * Otherwise the device would get an incorrect idea of the exit
33041ea7e0e8SSarah Sharp 	 * latency for the link state, and could start a device-initiated
33051ea7e0e8SSarah Sharp 	 * U1/U2 when the exit latencies are too high.
33061ea7e0e8SSarah Sharp 	 */
33071ea7e0e8SSarah Sharp 	if ((state == USB3_LPM_U1 &&
33081ea7e0e8SSarah Sharp 				(u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
33091ea7e0e8SSarah Sharp 				 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
33101ea7e0e8SSarah Sharp 			(state == USB3_LPM_U2 &&
33111ea7e0e8SSarah Sharp 			 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
33121ea7e0e8SSarah Sharp 			  u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
33131510a1a2SSarah Sharp 		dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
33141ea7e0e8SSarah Sharp 				usb3_lpm_names[state], u1_sel, u1_pel);
33151ea7e0e8SSarah Sharp 		return -EINVAL;
33161ea7e0e8SSarah Sharp 	}
33171ea7e0e8SSarah Sharp 
33181ea7e0e8SSarah Sharp 	/*
33191ea7e0e8SSarah Sharp 	 * If we're enabling device-initiated LPM for one link state,
33201ea7e0e8SSarah Sharp 	 * but the other link state has a too high SEL or PEL value,
33211ea7e0e8SSarah Sharp 	 * just set those values to the max in the Set SEL request.
33221ea7e0e8SSarah Sharp 	 */
33231ea7e0e8SSarah Sharp 	if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
33241ea7e0e8SSarah Sharp 		u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
33251ea7e0e8SSarah Sharp 
33261ea7e0e8SSarah Sharp 	if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
33271ea7e0e8SSarah Sharp 		u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
33281ea7e0e8SSarah Sharp 
33291ea7e0e8SSarah Sharp 	if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
33301ea7e0e8SSarah Sharp 		u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
33311ea7e0e8SSarah Sharp 
33321ea7e0e8SSarah Sharp 	if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
33331ea7e0e8SSarah Sharp 		u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
33341ea7e0e8SSarah Sharp 
33351ea7e0e8SSarah Sharp 	/*
33361ea7e0e8SSarah Sharp 	 * usb_enable_lpm() can be called as part of a failed device reset,
33371ea7e0e8SSarah Sharp 	 * which may be initiated by an error path of a mass storage driver.
33381ea7e0e8SSarah Sharp 	 * Therefore, use GFP_NOIO.
33391ea7e0e8SSarah Sharp 	 */
33401ea7e0e8SSarah Sharp 	sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
33411ea7e0e8SSarah Sharp 	if (!sel_values)
33421ea7e0e8SSarah Sharp 		return -ENOMEM;
33431ea7e0e8SSarah Sharp 
33441ea7e0e8SSarah Sharp 	sel_values->u1_sel = u1_sel;
33451ea7e0e8SSarah Sharp 	sel_values->u1_pel = u1_pel;
33461ea7e0e8SSarah Sharp 	sel_values->u2_sel = cpu_to_le16(u2_sel);
33471ea7e0e8SSarah Sharp 	sel_values->u2_pel = cpu_to_le16(u2_pel);
33481ea7e0e8SSarah Sharp 
33491ea7e0e8SSarah Sharp 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33501ea7e0e8SSarah Sharp 			USB_REQ_SET_SEL,
33511ea7e0e8SSarah Sharp 			USB_RECIP_DEVICE,
33521ea7e0e8SSarah Sharp 			0, 0,
33531ea7e0e8SSarah Sharp 			sel_values, sizeof *(sel_values),
33541ea7e0e8SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
33551ea7e0e8SSarah Sharp 	kfree(sel_values);
33561ea7e0e8SSarah Sharp 	return ret;
33571ea7e0e8SSarah Sharp }
33581ea7e0e8SSarah Sharp 
33591ea7e0e8SSarah Sharp /*
33601ea7e0e8SSarah Sharp  * Enable or disable device-initiated U1 or U2 transitions.
33611ea7e0e8SSarah Sharp  */
33621ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev,
33631ea7e0e8SSarah Sharp 		enum usb3_link_state state, bool enable)
33641ea7e0e8SSarah Sharp {
33651ea7e0e8SSarah Sharp 	int ret;
33661ea7e0e8SSarah Sharp 	int feature;
33671ea7e0e8SSarah Sharp 
33681ea7e0e8SSarah Sharp 	switch (state) {
33691ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
33701ea7e0e8SSarah Sharp 		feature = USB_DEVICE_U1_ENABLE;
33711ea7e0e8SSarah Sharp 		break;
33721ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
33731ea7e0e8SSarah Sharp 		feature = USB_DEVICE_U2_ENABLE;
33741ea7e0e8SSarah Sharp 		break;
33751ea7e0e8SSarah Sharp 	default:
33761ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
33771ea7e0e8SSarah Sharp 				__func__, enable ? "enable" : "disable");
33781ea7e0e8SSarah Sharp 		return -EINVAL;
33791ea7e0e8SSarah Sharp 	}
33801ea7e0e8SSarah Sharp 
33811ea7e0e8SSarah Sharp 	if (udev->state != USB_STATE_CONFIGURED) {
33821ea7e0e8SSarah Sharp 		dev_dbg(&udev->dev, "%s: Can't %s %s state "
33831ea7e0e8SSarah Sharp 				"for unconfigured device.\n",
33841ea7e0e8SSarah Sharp 				__func__, enable ? "enable" : "disable",
33851ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
33861ea7e0e8SSarah Sharp 		return 0;
33871ea7e0e8SSarah Sharp 	}
33881ea7e0e8SSarah Sharp 
33891ea7e0e8SSarah Sharp 	if (enable) {
33901ea7e0e8SSarah Sharp 		/*
33911ea7e0e8SSarah Sharp 		 * Now send the control transfer to enable device-initiated LPM
33921ea7e0e8SSarah Sharp 		 * for either U1 or U2.
33931ea7e0e8SSarah Sharp 		 */
33941ea7e0e8SSarah Sharp 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33951ea7e0e8SSarah Sharp 				USB_REQ_SET_FEATURE,
33961ea7e0e8SSarah Sharp 				USB_RECIP_DEVICE,
33971ea7e0e8SSarah Sharp 				feature,
33981ea7e0e8SSarah Sharp 				0, NULL, 0,
33991ea7e0e8SSarah Sharp 				USB_CTRL_SET_TIMEOUT);
34001ea7e0e8SSarah Sharp 	} else {
34011ea7e0e8SSarah Sharp 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
34021ea7e0e8SSarah Sharp 				USB_REQ_CLEAR_FEATURE,
34031ea7e0e8SSarah Sharp 				USB_RECIP_DEVICE,
34041ea7e0e8SSarah Sharp 				feature,
34051ea7e0e8SSarah Sharp 				0, NULL, 0,
34061ea7e0e8SSarah Sharp 				USB_CTRL_SET_TIMEOUT);
34071ea7e0e8SSarah Sharp 	}
34081ea7e0e8SSarah Sharp 	if (ret < 0) {
34091ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
34101ea7e0e8SSarah Sharp 				enable ? "Enable" : "Disable",
34111ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
34121ea7e0e8SSarah Sharp 		return -EBUSY;
34131ea7e0e8SSarah Sharp 	}
34141ea7e0e8SSarah Sharp 	return 0;
34151ea7e0e8SSarah Sharp }
34161ea7e0e8SSarah Sharp 
34171ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev,
34181ea7e0e8SSarah Sharp 		enum usb3_link_state state, int timeout)
34191ea7e0e8SSarah Sharp {
34201ea7e0e8SSarah Sharp 	int ret;
34211ea7e0e8SSarah Sharp 	int feature;
34221ea7e0e8SSarah Sharp 
34231ea7e0e8SSarah Sharp 	switch (state) {
34241ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
34251ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U1_TIMEOUT;
34261ea7e0e8SSarah Sharp 		break;
34271ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
34281ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U2_TIMEOUT;
34291ea7e0e8SSarah Sharp 		break;
34301ea7e0e8SSarah Sharp 	default:
34311ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
34321ea7e0e8SSarah Sharp 				__func__);
34331ea7e0e8SSarah Sharp 		return -EINVAL;
34341ea7e0e8SSarah Sharp 	}
34351ea7e0e8SSarah Sharp 
34361ea7e0e8SSarah Sharp 	if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
34371ea7e0e8SSarah Sharp 			timeout != USB3_LPM_DEVICE_INITIATED) {
34381ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
34391ea7e0e8SSarah Sharp 				"which is a reserved value.\n",
34401ea7e0e8SSarah Sharp 				usb3_lpm_names[state], timeout);
34411ea7e0e8SSarah Sharp 		return -EINVAL;
34421ea7e0e8SSarah Sharp 	}
34431ea7e0e8SSarah Sharp 
34441ea7e0e8SSarah Sharp 	ret = set_port_feature(udev->parent,
34451ea7e0e8SSarah Sharp 			USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
34461ea7e0e8SSarah Sharp 			feature);
34471ea7e0e8SSarah Sharp 	if (ret < 0) {
34481ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
34491ea7e0e8SSarah Sharp 				"error code %i\n", usb3_lpm_names[state],
34501ea7e0e8SSarah Sharp 				timeout, ret);
34511ea7e0e8SSarah Sharp 		return -EBUSY;
34521ea7e0e8SSarah Sharp 	}
34531ea7e0e8SSarah Sharp 	if (state == USB3_LPM_U1)
34541ea7e0e8SSarah Sharp 		udev->u1_params.timeout = timeout;
34551ea7e0e8SSarah Sharp 	else
34561ea7e0e8SSarah Sharp 		udev->u2_params.timeout = timeout;
34571ea7e0e8SSarah Sharp 	return 0;
34581ea7e0e8SSarah Sharp }
34591ea7e0e8SSarah Sharp 
34601ea7e0e8SSarah Sharp /*
34611ea7e0e8SSarah Sharp  * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
34621ea7e0e8SSarah Sharp  * U1/U2 entry.
34631ea7e0e8SSarah Sharp  *
34641ea7e0e8SSarah Sharp  * We will attempt to enable U1 or U2, but there are no guarantees that the
34651ea7e0e8SSarah Sharp  * control transfers to set the hub timeout or enable device-initiated U1/U2
34661ea7e0e8SSarah Sharp  * will be successful.
34671ea7e0e8SSarah Sharp  *
34681ea7e0e8SSarah Sharp  * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
34691ea7e0e8SSarah Sharp  * driver know about it.  If that call fails, it should be harmless, and just
34701ea7e0e8SSarah Sharp  * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
34711ea7e0e8SSarah Sharp  */
34721ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
34731ea7e0e8SSarah Sharp 		enum usb3_link_state state)
34741ea7e0e8SSarah Sharp {
347565a95b75SSarah Sharp 	int timeout, ret;
3476ae8963adSSarah Sharp 	__u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3477ae8963adSSarah Sharp 	__le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3478ae8963adSSarah Sharp 
3479ae8963adSSarah Sharp 	/* If the device says it doesn't have *any* exit latency to come out of
3480ae8963adSSarah Sharp 	 * U1 or U2, it's probably lying.  Assume it doesn't implement that link
3481ae8963adSSarah Sharp 	 * state.
3482ae8963adSSarah Sharp 	 */
3483ae8963adSSarah Sharp 	if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3484ae8963adSSarah Sharp 			(state == USB3_LPM_U2 && u2_mel == 0))
3485ae8963adSSarah Sharp 		return;
34861ea7e0e8SSarah Sharp 
348765a95b75SSarah Sharp 	/*
348865a95b75SSarah Sharp 	 * First, let the device know about the exit latencies
348965a95b75SSarah Sharp 	 * associated with the link state we're about to enable.
349065a95b75SSarah Sharp 	 */
349165a95b75SSarah Sharp 	ret = usb_req_set_sel(udev, state);
349265a95b75SSarah Sharp 	if (ret < 0) {
349365a95b75SSarah Sharp 		dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
349465a95b75SSarah Sharp 				usb3_lpm_names[state]);
349565a95b75SSarah Sharp 		return;
349665a95b75SSarah Sharp 	}
349765a95b75SSarah Sharp 
34981ea7e0e8SSarah Sharp 	/* We allow the host controller to set the U1/U2 timeout internally
34991ea7e0e8SSarah Sharp 	 * first, so that it can change its schedule to account for the
35001ea7e0e8SSarah Sharp 	 * additional latency to send data to a device in a lower power
35011ea7e0e8SSarah Sharp 	 * link state.
35021ea7e0e8SSarah Sharp 	 */
35031ea7e0e8SSarah Sharp 	timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
35041ea7e0e8SSarah Sharp 
35051ea7e0e8SSarah Sharp 	/* xHCI host controller doesn't want to enable this LPM state. */
35061ea7e0e8SSarah Sharp 	if (timeout == 0)
35071ea7e0e8SSarah Sharp 		return;
35081ea7e0e8SSarah Sharp 
35091ea7e0e8SSarah Sharp 	if (timeout < 0) {
35101ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Could not enable %s link state, "
35111ea7e0e8SSarah Sharp 				"xHCI error %i.\n", usb3_lpm_names[state],
35121ea7e0e8SSarah Sharp 				timeout);
35131ea7e0e8SSarah Sharp 		return;
35141ea7e0e8SSarah Sharp 	}
35151ea7e0e8SSarah Sharp 
35161ea7e0e8SSarah Sharp 	if (usb_set_lpm_timeout(udev, state, timeout))
35171ea7e0e8SSarah Sharp 		/* If we can't set the parent hub U1/U2 timeout,
35181ea7e0e8SSarah Sharp 		 * device-initiated LPM won't be allowed either, so let the xHCI
35191ea7e0e8SSarah Sharp 		 * host know that this link state won't be enabled.
35201ea7e0e8SSarah Sharp 		 */
35211ea7e0e8SSarah Sharp 		hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
35221ea7e0e8SSarah Sharp 
35231ea7e0e8SSarah Sharp 	/* Only a configured device will accept the Set Feature U1/U2_ENABLE */
35241ea7e0e8SSarah Sharp 	else if (udev->actconfig)
35251ea7e0e8SSarah Sharp 		usb_set_device_initiated_lpm(udev, state, true);
35261ea7e0e8SSarah Sharp 
35271ea7e0e8SSarah Sharp }
35281ea7e0e8SSarah Sharp 
35291ea7e0e8SSarah Sharp /*
35301ea7e0e8SSarah Sharp  * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
35311ea7e0e8SSarah Sharp  * U1/U2 entry.
35321ea7e0e8SSarah Sharp  *
35331ea7e0e8SSarah Sharp  * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
35341ea7e0e8SSarah Sharp  * If zero is returned, the parent will not allow the link to go into U1/U2.
35351ea7e0e8SSarah Sharp  *
35361ea7e0e8SSarah Sharp  * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
35371ea7e0e8SSarah Sharp  * it won't have an effect on the bus link state because the parent hub will
35381ea7e0e8SSarah Sharp  * still disallow device-initiated U1/U2 entry.
35391ea7e0e8SSarah Sharp  *
35401ea7e0e8SSarah Sharp  * If zero is returned, the xHCI host controller may still think U1/U2 entry is
35411ea7e0e8SSarah Sharp  * possible.  The result will be slightly more bus bandwidth will be taken up
35421ea7e0e8SSarah Sharp  * (to account for U1/U2 exit latency), but it should be harmless.
35431ea7e0e8SSarah Sharp  */
35441ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
35451ea7e0e8SSarah Sharp 		enum usb3_link_state state)
35461ea7e0e8SSarah Sharp {
35471ea7e0e8SSarah Sharp 	int feature;
35481ea7e0e8SSarah Sharp 
35491ea7e0e8SSarah Sharp 	switch (state) {
35501ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
35511ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U1_TIMEOUT;
35521ea7e0e8SSarah Sharp 		break;
35531ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
35541ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U2_TIMEOUT;
35551ea7e0e8SSarah Sharp 		break;
35561ea7e0e8SSarah Sharp 	default:
35571ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
35581ea7e0e8SSarah Sharp 				__func__);
35591ea7e0e8SSarah Sharp 		return -EINVAL;
35601ea7e0e8SSarah Sharp 	}
35611ea7e0e8SSarah Sharp 
35621ea7e0e8SSarah Sharp 	if (usb_set_lpm_timeout(udev, state, 0))
35631ea7e0e8SSarah Sharp 		return -EBUSY;
35641ea7e0e8SSarah Sharp 
35651ea7e0e8SSarah Sharp 	usb_set_device_initiated_lpm(udev, state, false);
35661ea7e0e8SSarah Sharp 
35671ea7e0e8SSarah Sharp 	if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
35681ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
35691ea7e0e8SSarah Sharp 				"bus schedule bandwidth may be impacted.\n",
35701ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
35711ea7e0e8SSarah Sharp 	return 0;
35721ea7e0e8SSarah Sharp }
35731ea7e0e8SSarah Sharp 
35741ea7e0e8SSarah Sharp /*
35751ea7e0e8SSarah Sharp  * Disable hub-initiated and device-initiated U1 and U2 entry.
35761ea7e0e8SSarah Sharp  * Caller must own the bandwidth_mutex.
35771ea7e0e8SSarah Sharp  *
35781ea7e0e8SSarah Sharp  * This will call usb_enable_lpm() on failure, which will decrement
35791ea7e0e8SSarah Sharp  * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
35801ea7e0e8SSarah Sharp  */
35811ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
35821ea7e0e8SSarah Sharp {
35831ea7e0e8SSarah Sharp 	struct usb_hcd *hcd;
35841ea7e0e8SSarah Sharp 
35851ea7e0e8SSarah Sharp 	if (!udev || !udev->parent ||
35861ea7e0e8SSarah Sharp 			udev->speed != USB_SPEED_SUPER ||
35871ea7e0e8SSarah Sharp 			!udev->lpm_capable)
35881ea7e0e8SSarah Sharp 		return 0;
35891ea7e0e8SSarah Sharp 
35901ea7e0e8SSarah Sharp 	hcd = bus_to_hcd(udev->bus);
35911ea7e0e8SSarah Sharp 	if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
35921ea7e0e8SSarah Sharp 		return 0;
35931ea7e0e8SSarah Sharp 
35941ea7e0e8SSarah Sharp 	udev->lpm_disable_count++;
359555558c33SDan Carpenter 	if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
35961ea7e0e8SSarah Sharp 		return 0;
35971ea7e0e8SSarah Sharp 
35981ea7e0e8SSarah Sharp 	/* If LPM is enabled, attempt to disable it. */
35991ea7e0e8SSarah Sharp 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
36001ea7e0e8SSarah Sharp 		goto enable_lpm;
36011ea7e0e8SSarah Sharp 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
36021ea7e0e8SSarah Sharp 		goto enable_lpm;
36031ea7e0e8SSarah Sharp 
36041ea7e0e8SSarah Sharp 	return 0;
36051ea7e0e8SSarah Sharp 
36061ea7e0e8SSarah Sharp enable_lpm:
36071ea7e0e8SSarah Sharp 	usb_enable_lpm(udev);
36081ea7e0e8SSarah Sharp 	return -EBUSY;
36091ea7e0e8SSarah Sharp }
36101ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
36111ea7e0e8SSarah Sharp 
36121ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
36131ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
36141ea7e0e8SSarah Sharp {
36151ea7e0e8SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
36161ea7e0e8SSarah Sharp 	int ret;
36171ea7e0e8SSarah Sharp 
36181ea7e0e8SSarah Sharp 	if (!hcd)
36191ea7e0e8SSarah Sharp 		return -EINVAL;
36201ea7e0e8SSarah Sharp 
36211ea7e0e8SSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
36221ea7e0e8SSarah Sharp 	ret = usb_disable_lpm(udev);
36231ea7e0e8SSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
36241ea7e0e8SSarah Sharp 
36251ea7e0e8SSarah Sharp 	return ret;
36261ea7e0e8SSarah Sharp }
36271ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
36281ea7e0e8SSarah Sharp 
36291ea7e0e8SSarah Sharp /*
36301ea7e0e8SSarah Sharp  * Attempt to enable device-initiated and hub-initiated U1 and U2 entry.  The
36311ea7e0e8SSarah Sharp  * xHCI host policy may prevent U1 or U2 from being enabled.
36321ea7e0e8SSarah Sharp  *
36331ea7e0e8SSarah Sharp  * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
36341ea7e0e8SSarah Sharp  * until the lpm_disable_count drops to zero.  Caller must own the
36351ea7e0e8SSarah Sharp  * bandwidth_mutex.
36361ea7e0e8SSarah Sharp  */
36371ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev)
36381ea7e0e8SSarah Sharp {
36391ea7e0e8SSarah Sharp 	struct usb_hcd *hcd;
36401ea7e0e8SSarah Sharp 
36411ea7e0e8SSarah Sharp 	if (!udev || !udev->parent ||
36421ea7e0e8SSarah Sharp 			udev->speed != USB_SPEED_SUPER ||
36431ea7e0e8SSarah Sharp 			!udev->lpm_capable)
36441ea7e0e8SSarah Sharp 		return;
36451ea7e0e8SSarah Sharp 
36461ea7e0e8SSarah Sharp 	udev->lpm_disable_count--;
36471ea7e0e8SSarah Sharp 	hcd = bus_to_hcd(udev->bus);
36481ea7e0e8SSarah Sharp 	/* Double check that we can both enable and disable LPM.
36491ea7e0e8SSarah Sharp 	 * Device must be configured to accept set feature U1/U2 timeout.
36501ea7e0e8SSarah Sharp 	 */
36511ea7e0e8SSarah Sharp 	if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
36521ea7e0e8SSarah Sharp 			!hcd->driver->disable_usb3_lpm_timeout)
36531ea7e0e8SSarah Sharp 		return;
36541ea7e0e8SSarah Sharp 
36551ea7e0e8SSarah Sharp 	if (udev->lpm_disable_count > 0)
36561ea7e0e8SSarah Sharp 		return;
36571ea7e0e8SSarah Sharp 
36581ea7e0e8SSarah Sharp 	usb_enable_link_state(hcd, udev, USB3_LPM_U1);
36591ea7e0e8SSarah Sharp 	usb_enable_link_state(hcd, udev, USB3_LPM_U2);
36601ea7e0e8SSarah Sharp }
36611ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
36621ea7e0e8SSarah Sharp 
36631ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
36641ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev)
36651ea7e0e8SSarah Sharp {
36661ea7e0e8SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
36671ea7e0e8SSarah Sharp 
36681ea7e0e8SSarah Sharp 	if (!hcd)
36691ea7e0e8SSarah Sharp 		return;
36701ea7e0e8SSarah Sharp 
36711ea7e0e8SSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
36721ea7e0e8SSarah Sharp 	usb_enable_lpm(udev);
36731ea7e0e8SSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
36741ea7e0e8SSarah Sharp }
36751ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
36761ea7e0e8SSarah Sharp 
36771ea7e0e8SSarah Sharp 
3678d388dab7SAlan Stern #else	/* CONFIG_PM */
3679d388dab7SAlan Stern 
3680511366daSAndrew Morton #define hub_suspend		NULL
3681511366daSAndrew Morton #define hub_resume		NULL
3682f07600cfSAlan Stern #define hub_reset_resume	NULL
36831ea7e0e8SSarah Sharp 
36841ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
36851ea7e0e8SSarah Sharp {
36861ea7e0e8SSarah Sharp 	return 0;
36871ea7e0e8SSarah Sharp }
3688e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
36891ea7e0e8SSarah Sharp 
36901ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { }
3691e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
36921ea7e0e8SSarah Sharp 
36931ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
36941ea7e0e8SSarah Sharp {
36951ea7e0e8SSarah Sharp 	return 0;
36961ea7e0e8SSarah Sharp }
3697e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
36981ea7e0e8SSarah Sharp 
36991ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { }
3700e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3701f74631e3SSarah Sharp 
3702f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
3703f74631e3SSarah Sharp {
3704f74631e3SSarah Sharp 	return 0;
3705f74631e3SSarah Sharp }
3706f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
3707f74631e3SSarah Sharp 
3708f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) { }
3709f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
3710d388dab7SAlan Stern #endif
3711d388dab7SAlan Stern 
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
37141da177e4SLinus Torvalds  *
37151da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
37161da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
37171da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
37181da177e4SLinus Torvalds  *
37191da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
37201da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
37211da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
37221da177e4SLinus Torvalds  *
37231da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
37241da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
37251da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
37261da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
37271da177e4SLinus Torvalds  */
37281da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
37291da177e4SLinus Torvalds {
37301da177e4SLinus Torvalds 	int ret;
37311da177e4SLinus Torvalds 	int total_time, stable_time = 0;
37321da177e4SLinus Torvalds 	u16 portchange, portstatus;
37331da177e4SLinus Torvalds 	unsigned connection = 0xffff;
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
37361da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
37371da177e4SLinus Torvalds 		if (ret < 0)
37381da177e4SLinus Torvalds 			return ret;
37391da177e4SLinus Torvalds 
37401da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
37411da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
37421da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
37431da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
37441da177e4SLinus Torvalds 				break;
37451da177e4SLinus Torvalds 		} else {
37461da177e4SLinus Torvalds 			stable_time = 0;
37471da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
37481da177e4SLinus Torvalds 		}
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
37511da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
37521da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
37531da177e4SLinus Torvalds 		}
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
37561da177e4SLinus Torvalds 			break;
37571da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
37581da177e4SLinus Torvalds 	}
37591da177e4SLinus Torvalds 
37601da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
37611da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
37621da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
37651da177e4SLinus Torvalds 		return -ETIMEDOUT;
37661da177e4SLinus Torvalds 	return portstatus;
37671da177e4SLinus Torvalds }
37681da177e4SLinus Torvalds 
3769fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
37701da177e4SLinus Torvalds {
3771ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3772ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
37732caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
37741da177e4SLinus Torvalds }
3775fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
37761da177e4SLinus Torvalds 
37771da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
37781da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
37791da177e4SLinus Torvalds 
37804326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
37811da177e4SLinus Torvalds {
37821da177e4SLinus Torvalds 	int retval;
3783c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
37841da177e4SLinus Torvalds 
3785c6515272SSarah Sharp 	/*
3786c6515272SSarah Sharp 	 * The host controller will choose the device address,
3787c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
3788c6515272SSarah Sharp 	 */
3789c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
37901da177e4SLinus Torvalds 		return -EINVAL;
37911da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
37921da177e4SLinus Torvalds 		return 0;
37931da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
37941da177e4SLinus Torvalds 		return -EINVAL;
3795c8d4af8eSAndiry Xu 	if (hcd->driver->address_device)
3796c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
3797c8d4af8eSAndiry Xu 	else
37981da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
37994326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
38001da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
38011da177e4SLinus Torvalds 	if (retval == 0) {
38023b29b68bSAlan Stern 		update_devnum(udev, devnum);
38034953d141SDavid Vrabel 		/* Device now using proper address. */
38041da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
3805fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
38061da177e4SLinus Torvalds 	}
38071da177e4SLinus Torvalds 	return retval;
38081da177e4SLinus Torvalds }
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
38111da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
38121da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
38131da177e4SLinus Torvalds  *
38141da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
3815742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
38161da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
38171da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
38181da177e4SLinus Torvalds  */
38191da177e4SLinus Torvalds static int
38201da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
38211da177e4SLinus Torvalds 		int retry_counter)
38221da177e4SLinus Torvalds {
38234186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
38241da177e4SLinus Torvalds 
38251da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
3826e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
38271da177e4SLinus Torvalds 	int			i, j, retval;
38281da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
38291da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
3830e538dfdaSMichal Nazarewicz 	const char		*speed;
38314326ed0bSAlan Stern 	int			devnum = udev->devnum;
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
38341da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
38351da177e4SLinus Torvalds 	 */
38361da177e4SLinus Torvalds 	if (!hdev->parent) {
38371da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
38381da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
38391da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
38401da177e4SLinus Torvalds 	}
38411da177e4SLinus Torvalds 
38421da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
38431da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
38441da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
38451da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
38461da177e4SLinus Torvalds 
38474186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 	/* Reset the device; full speed may morph to high speed */
3850e7b77172SSarah Sharp 	/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
385175d7cf72SAndiry Xu 	retval = hub_port_reset(hub, port1, udev, delay, false);
38521da177e4SLinus Torvalds 	if (retval < 0)		/* error or disconnect */
38531da177e4SLinus Torvalds 		goto fail;
38541da177e4SLinus Torvalds 	/* success, speed is known */
385507194ab7SLuben Tuikov 
38561da177e4SLinus Torvalds 	retval = -ENODEV;
38571da177e4SLinus Torvalds 
38581da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
38591da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
38601da177e4SLinus Torvalds 		goto fail;
38611da177e4SLinus Torvalds 	}
38621da177e4SLinus Torvalds 	oldspeed = udev->speed;
38631da177e4SLinus Torvalds 
38641da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
38651da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
38665bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
38675bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
38681da177e4SLinus Torvalds 	 */
38691da177e4SLinus Torvalds 	switch (udev->speed) {
38706b403b02SSarah Sharp 	case USB_SPEED_SUPER:
3871551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:	/* fixed at 512 */
3872551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
38735bb6e0aeSInaky Perez-Gonzalez 		break;
38741da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
3875551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
38761da177e4SLinus Torvalds 		break;
38771da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
38781da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
38791da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
38801da177e4SLinus Torvalds 		 * then correct our initial guess.
38811da177e4SLinus Torvalds 		 */
3882551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
38831da177e4SLinus Torvalds 		break;
38841da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
3885551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
38861da177e4SLinus Torvalds 		break;
38871da177e4SLinus Torvalds 	default:
38881da177e4SLinus Torvalds 		goto fail;
38891da177e4SLinus Torvalds 	}
38901da177e4SLinus Torvalds 
3891e538dfdaSMichal Nazarewicz 	if (udev->speed == USB_SPEED_WIRELESS)
3892e538dfdaSMichal Nazarewicz 		speed = "variable speed Wireless";
3893e538dfdaSMichal Nazarewicz 	else
3894e538dfdaSMichal Nazarewicz 		speed = usb_speed_string(udev->speed);
3895e538dfdaSMichal Nazarewicz 
3896c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
389783a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
3898e538dfdaSMichal Nazarewicz 				"%s %s USB device number %d using %s\n",
3899e538dfdaSMichal Nazarewicz 				(udev->config) ? "reset" : "new", speed,
39003b29b68bSAlan Stern 				devnum, udev->bus->controller->driver->name);
39011da177e4SLinus Torvalds 
39021da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
39031da177e4SLinus Torvalds 	if (hdev->tt) {
39041da177e4SLinus Torvalds 		udev->tt = hdev->tt;
39051da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
39061da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
39071da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
3908d199c96dSAlan Stern 		if (!hub->tt.hub) {
3909d199c96dSAlan Stern 			dev_err(&udev->dev, "parent hub has no TT\n");
3910d199c96dSAlan Stern 			retval = -EINVAL;
3911d199c96dSAlan Stern 			goto fail;
3912d199c96dSAlan Stern 		}
39131da177e4SLinus Torvalds 		udev->tt = &hub->tt;
39141da177e4SLinus Torvalds 		udev->ttport = port1;
39151da177e4SLinus Torvalds 	}
39161da177e4SLinus Torvalds 
39171da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
39181da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
39191da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
39201da177e4SLinus Torvalds 	 * Change it cautiously.
39211da177e4SLinus Torvalds 	 *
39221da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
39231da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
39241da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
39251da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
39261da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
39271da177e4SLinus Torvalds 	 * value.
39281da177e4SLinus Torvalds 	 */
39291da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
3930c6515272SSarah Sharp 		if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
39311da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
39321da177e4SLinus Torvalds 			int r = 0;
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
39351da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
39361da177e4SLinus Torvalds 			if (!buf) {
39371da177e4SLinus Torvalds 				retval = -ENOMEM;
39381da177e4SLinus Torvalds 				continue;
39391da177e4SLinus Torvalds 			}
39401da177e4SLinus Torvalds 
3941b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
3942b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
3943b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
39441da177e4SLinus Torvalds 			 */
39451da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
39461da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
39471da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
39481da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
39491da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
39501da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
3951fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
39521da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
39535bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
39541da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
39551da177e4SLinus Torvalds 							USB_DT_DEVICE) {
39561da177e4SLinus Torvalds 						r = 0;
39571da177e4SLinus Torvalds 						break;
39581da177e4SLinus Torvalds 					}
39591da177e4SLinus Torvalds 					/* FALL THROUGH */
39601da177e4SLinus Torvalds 				default:
39611da177e4SLinus Torvalds 					if (r == 0)
39621da177e4SLinus Torvalds 						r = -EPROTO;
39631da177e4SLinus Torvalds 					break;
39641da177e4SLinus Torvalds 				}
39651da177e4SLinus Torvalds 				if (r == 0)
39661da177e4SLinus Torvalds 					break;
39671da177e4SLinus Torvalds 			}
39681da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
39691da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
39701da177e4SLinus Torvalds 			kfree(buf);
39711da177e4SLinus Torvalds 
397275d7cf72SAndiry Xu 			retval = hub_port_reset(hub, port1, udev, delay, false);
39731da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
39741da177e4SLinus Torvalds 				goto fail;
39751da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
39761da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
39771da177e4SLinus Torvalds 					"device reset changed speed!\n");
39781da177e4SLinus Torvalds 				retval = -ENODEV;
39791da177e4SLinus Torvalds 				goto fail;
39801da177e4SLinus Torvalds 			}
39811da177e4SLinus Torvalds 			if (r) {
3982b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
3983b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
3984b9cef6c3SWu Fengguang 					r);
39851da177e4SLinus Torvalds 				retval = -EMSGSIZE;
39861da177e4SLinus Torvalds 				continue;
39871da177e4SLinus Torvalds 			}
39881da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
39891da177e4SLinus Torvalds 		}
39901da177e4SLinus Torvalds 
39916c529cdcSInaky Perez-Gonzalez  		/*
39926c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
39936c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
39946c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
39956c529cdcSInaky Perez-Gonzalez  		 */
39966c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
39971da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
39984326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
39991da177e4SLinus Torvalds 				if (retval >= 0)
40001da177e4SLinus Torvalds 					break;
40011da177e4SLinus Torvalds 				msleep(200);
40021da177e4SLinus Torvalds 			}
40031da177e4SLinus Torvalds 			if (retval < 0) {
40041da177e4SLinus Torvalds 				dev_err(&udev->dev,
40051da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
40064326ed0bSAlan Stern 					devnum, retval);
40071da177e4SLinus Torvalds 				goto fail;
40081da177e4SLinus Torvalds 			}
4009c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
4010c6515272SSarah Sharp 				devnum = udev->devnum;
4011c6515272SSarah Sharp 				dev_info(&udev->dev,
40123b29b68bSAlan Stern 						"%s SuperSpeed USB device number %d using %s\n",
4013c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
40143b29b68bSAlan Stern 						devnum, udev->bus->controller->driver->name);
4015c6515272SSarah Sharp 			}
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
40181da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
40191da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
40201da177e4SLinus Torvalds 			 */
40211da177e4SLinus Torvalds 			msleep(10);
4022c6515272SSarah Sharp 			if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
40231da177e4SLinus Torvalds 				break;
40246c529cdcSInaky Perez-Gonzalez   		}
40251da177e4SLinus Torvalds 
40261da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
40271da177e4SLinus Torvalds 		if (retval < 8) {
4028b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
4029b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
4030b9cef6c3SWu Fengguang 					retval);
40311da177e4SLinus Torvalds 			if (retval >= 0)
40321da177e4SLinus Torvalds 				retval = -EMSGSIZE;
40331da177e4SLinus Torvalds 		} else {
40341da177e4SLinus Torvalds 			retval = 0;
40351da177e4SLinus Torvalds 			break;
40361da177e4SLinus Torvalds 		}
40371da177e4SLinus Torvalds 	}
40381da177e4SLinus Torvalds 	if (retval)
40391da177e4SLinus Torvalds 		goto fail;
40401da177e4SLinus Torvalds 
4041b76baa81SPeter Chen 	if (hcd->phy && !hdev->parent)
4042ac96511bSPeter Chen 		usb_phy_notify_connect(hcd->phy, udev->speed);
4043b76baa81SPeter Chen 
4044d8aec3dbSElric Fu 	/*
4045d8aec3dbSElric Fu 	 * Some superspeed devices have finished the link training process
4046d8aec3dbSElric Fu 	 * and attached to a superspeed hub port, but the device descriptor
4047d8aec3dbSElric Fu 	 * got from those devices show they aren't superspeed devices. Warm
4048d8aec3dbSElric Fu 	 * reset the port attached by the devices can fix them.
4049d8aec3dbSElric Fu 	 */
4050d8aec3dbSElric Fu 	if ((udev->speed == USB_SPEED_SUPER) &&
4051d8aec3dbSElric Fu 			(le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4052d8aec3dbSElric Fu 		dev_err(&udev->dev, "got a wrong device descriptor, "
4053d8aec3dbSElric Fu 				"warm reset device\n");
4054d8aec3dbSElric Fu 		hub_port_reset(hub, port1, udev,
4055d8aec3dbSElric Fu 				HUB_BH_RESET_TIME, true);
4056d8aec3dbSElric Fu 		retval = -EINVAL;
4057d8aec3dbSElric Fu 		goto fail;
4058d8aec3dbSElric Fu 	}
4059d8aec3dbSElric Fu 
40606b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
40616b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
40626b403b02SSarah Sharp 		i = 512;
40636b403b02SSarah Sharp 	else
40646b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
406529cc8897SKuninori Morimoto 	if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
406656626a72SAlan Stern 		if (udev->speed == USB_SPEED_LOW ||
40671da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
406856626a72SAlan Stern 			dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
40691da177e4SLinus Torvalds 			retval = -EMSGSIZE;
40701da177e4SLinus Torvalds 			goto fail;
40711da177e4SLinus Torvalds 		}
407256626a72SAlan Stern 		if (udev->speed == USB_SPEED_FULL)
40731da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
407456626a72SAlan Stern 		else
407556626a72SAlan Stern 			dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
40761da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
4077fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
40781da177e4SLinus Torvalds 	}
40791da177e4SLinus Torvalds 
40801da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
40811da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
4082b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4083b9cef6c3SWu Fengguang 			retval);
40841da177e4SLinus Torvalds 		if (retval >= 0)
40851da177e4SLinus Torvalds 			retval = -ENOMSG;
40861da177e4SLinus Torvalds 		goto fail;
40871da177e4SLinus Torvalds 	}
40881da177e4SLinus Torvalds 
40891ff4df56SAndiry Xu 	if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
40901ff4df56SAndiry Xu 		retval = usb_get_bos_descriptor(udev);
409151e0a012SSarah Sharp 		if (!retval) {
4092d9b2099cSSarah Sharp 			udev->lpm_capable = usb_device_supports_lpm(udev);
409351e0a012SSarah Sharp 			usb_set_lpm_parameters(udev);
409451e0a012SSarah Sharp 		}
40951ff4df56SAndiry Xu 	}
40963148bf04SAndiry Xu 
40971da177e4SLinus Torvalds 	retval = 0;
409848f24970SAlek Du 	/* notify HCD that we have a device connected and addressed */
409948f24970SAlek Du 	if (hcd->driver->update_device)
410048f24970SAlek Du 		hcd->driver->update_device(hcd, udev);
41011da177e4SLinus Torvalds fail:
41024326ed0bSAlan Stern 	if (retval) {
41031da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
41043b29b68bSAlan Stern 		update_devnum(udev, devnum);	/* for disconnect processing */
41054326ed0bSAlan Stern 	}
41064186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
41071da177e4SLinus Torvalds 	return retval;
41081da177e4SLinus Torvalds }
41091da177e4SLinus Torvalds 
41101da177e4SLinus Torvalds static void
41111da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
41121da177e4SLinus Torvalds {
41131da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
41141da177e4SLinus Torvalds 	int				status;
41151da177e4SLinus Torvalds 
4116e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
41171da177e4SLinus Torvalds 	if (qual == NULL)
41181da177e4SLinus Torvalds 		return;
41191da177e4SLinus Torvalds 
41201da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
41211da177e4SLinus Torvalds 			qual, sizeof *qual);
41221da177e4SLinus Torvalds 	if (status == sizeof *qual) {
41231da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
41241da177e4SLinus Torvalds 			"connect to a high speed hub\n");
41251da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
41261da177e4SLinus Torvalds 		if (hub->has_indicators) {
41271da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
4128c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
41291da177e4SLinus Torvalds 		}
41301da177e4SLinus Torvalds 	}
41311da177e4SLinus Torvalds 	kfree(qual);
41321da177e4SLinus Torvalds }
41331da177e4SLinus Torvalds 
41341da177e4SLinus Torvalds static unsigned
41351da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
41361da177e4SLinus Torvalds {
41371da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
41381da177e4SLinus Torvalds 	int remaining;
413955c52718SAlan Stern 	int port1;
41401da177e4SLinus Torvalds 
414155c52718SAlan Stern 	if (!hub->limited_power)
41421da177e4SLinus Torvalds 		return 0;
41431da177e4SLinus Torvalds 
414455c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
414555c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4146ff823c79SLan Tianyu 		struct usb_device	*udev = hub->ports[port1 - 1]->child;
414755c52718SAlan Stern 		int			delta;
41481da177e4SLinus Torvalds 
41491da177e4SLinus Torvalds 		if (!udev)
41501da177e4SLinus Torvalds 			continue;
41511da177e4SLinus Torvalds 
415255c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
415355c52718SAlan Stern 		 * or 8mA for OTG ports */
41541da177e4SLinus Torvalds 		if (udev->actconfig)
415555c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
415655c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
415755c52718SAlan Stern 			delta = 100;
41581da177e4SLinus Torvalds 		else
415955c52718SAlan Stern 			delta = 8;
416055c52718SAlan Stern 		if (delta > hub->mA_per_port)
4161b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
4162b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
416355c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
41641da177e4SLinus Torvalds 		remaining -= delta;
41651da177e4SLinus Torvalds 	}
41661da177e4SLinus Torvalds 	if (remaining < 0) {
416755c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
416855c52718SAlan Stern 			- remaining);
41691da177e4SLinus Torvalds 		remaining = 0;
41701da177e4SLinus Torvalds 	}
41711da177e4SLinus Torvalds 	return remaining;
41721da177e4SLinus Torvalds }
41731da177e4SLinus Torvalds 
41741da177e4SLinus Torvalds /* Handle physical or logical connection change events.
41751da177e4SLinus Torvalds  * This routine is called when:
41761da177e4SLinus Torvalds  * 	a port connection-change occurs;
41771da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
4178742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
41791da177e4SLinus Torvalds  *		a firmware download)
41801da177e4SLinus Torvalds  * caller already locked the hub
41811da177e4SLinus Torvalds  */
41821da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
41831da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
41841da177e4SLinus Torvalds {
41851da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
41861da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
418790da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
418824618b0cSAlan Stern 	unsigned wHubCharacteristics =
418924618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
41908808f00cSAlan Stern 	struct usb_device *udev;
41911da177e4SLinus Torvalds 	int status, i;
41921da177e4SLinus Torvalds 
41931da177e4SLinus Torvalds 	dev_dbg (hub_dev,
41941da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
4195131dec34SSarah Sharp 		port1, portstatus, portchange, portspeed(hub, portstatus));
41961da177e4SLinus Torvalds 
41971da177e4SLinus Torvalds 	if (hub->has_indicators) {
41981da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
41991da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
42001da177e4SLinus Torvalds 	}
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
42031da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
42041da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
420524618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
420624618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
42071da177e4SLinus Torvalds #endif
42081da177e4SLinus Torvalds 
42098808f00cSAlan Stern 	/* Try to resuscitate an existing device */
4210ff823c79SLan Tianyu 	udev = hub->ports[port1 - 1]->child;
42118808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
42128808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
42138808f00cSAlan Stern 		usb_lock_device(udev);
42148808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
42158808f00cSAlan Stern 			status = 0;		/* Nothing to do */
42168808f00cSAlan Stern 
42178808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
42185257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
42195257d97aSAlan Stern 				udev->persist_enabled) {
42208808f00cSAlan Stern 			/* For a suspended device, treat this as a
42218808f00cSAlan Stern 			 * remote wakeup event.
42228808f00cSAlan Stern 			 */
42230534d468SAlan Stern 			status = usb_remote_wakeup(udev);
42248808f00cSAlan Stern #endif
42258808f00cSAlan Stern 
42268808f00cSAlan Stern 		} else {
42275257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
42288808f00cSAlan Stern 		}
42298808f00cSAlan Stern 		usb_unlock_device(udev);
42308808f00cSAlan Stern 
42318808f00cSAlan Stern 		if (status == 0) {
42328808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
42338808f00cSAlan Stern 			return;
42348808f00cSAlan Stern 		}
42358808f00cSAlan Stern 	}
42368808f00cSAlan Stern 
423724618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
4238b76baa81SPeter Chen 	if (udev) {
4239b76baa81SPeter Chen 		if (hcd->phy && !hdev->parent &&
4240b76baa81SPeter Chen 				!(portstatus & USB_PORT_STAT_CONNECTION))
4241ac96511bSPeter Chen 			usb_phy_notify_disconnect(hcd->phy, udev->speed);
4242ff823c79SLan Tianyu 		usb_disconnect(&hub->ports[port1 - 1]->child);
4243b76baa81SPeter Chen 	}
424424618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
424524618b0cSAlan Stern 
4246253e0572SAlan Stern 	/* We can forget about a "removed" device when there's a physical
4247253e0572SAlan Stern 	 * disconnect or the connect status changes.
4248253e0572SAlan Stern 	 */
4249253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4250253e0572SAlan Stern 			(portchange & USB_PORT_STAT_C_CONNECTION))
4251253e0572SAlan Stern 		clear_bit(port1, hub->removed_bits);
4252253e0572SAlan Stern 
42535257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
42545257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
42555257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
42565257d97aSAlan Stern 		if (status < 0) {
42575257d97aSAlan Stern 			if (printk_ratelimit())
42585257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
42595257d97aSAlan Stern 						"port %d disabled\n", port1);
42605257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
42615257d97aSAlan Stern 		} else {
42625257d97aSAlan Stern 			portstatus = status;
42635257d97aSAlan Stern 		}
42645257d97aSAlan Stern 	}
42655257d97aSAlan Stern 
4266253e0572SAlan Stern 	/* Return now if debouncing failed or nothing is connected or
4267253e0572SAlan Stern 	 * the device was "removed".
4268253e0572SAlan Stern 	 */
4269253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4270253e0572SAlan Stern 			test_bit(port1, hub->removed_bits)) {
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
427374ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
42740ed9a57eSAndiry Xu 				&& !port_is_power_on(hub, portstatus))
42751da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
42761da177e4SLinus Torvalds 
42771da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
42781da177e4SLinus Torvalds   			goto done;
42791da177e4SLinus Torvalds 		return;
42801da177e4SLinus Torvalds 	}
42811da177e4SLinus Torvalds 
42821da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
42831da177e4SLinus Torvalds 
42841da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
42851da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
42861da177e4SLinus Torvalds 		 */
42871da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
42881da177e4SLinus Torvalds 		if (!udev) {
42891da177e4SLinus Torvalds 			dev_err (hub_dev,
42901da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
42911da177e4SLinus Torvalds 				port1);
42921da177e4SLinus Torvalds 			goto done;
42931da177e4SLinus Torvalds 		}
42941da177e4SLinus Torvalds 
42951da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
429655c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
4297b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
42988af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
42991da177e4SLinus Torvalds 
4300131dec34SSarah Sharp 		/* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4301131dec34SSarah Sharp 		if (hub_is_superspeed(hub->hdev))
4302e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
4303e7b77172SSarah Sharp 		else
4304e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
4305e7b77172SSarah Sharp 
43063b29b68bSAlan Stern 		choose_devnum(udev);
4307c6515272SSarah Sharp 		if (udev->devnum <= 0) {
4308c6515272SSarah Sharp 			status = -ENOTCONN;	/* Don't retry */
4309c6515272SSarah Sharp 			goto loop;
4310c6515272SSarah Sharp 		}
4311c6515272SSarah Sharp 
4312e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
43131da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
43141da177e4SLinus Torvalds 		if (status < 0)
43151da177e4SLinus Torvalds 			goto loop;
43161da177e4SLinus Torvalds 
431793362a87SPhil Dibowitz 		usb_detect_quirks(udev);
431893362a87SPhil Dibowitz 		if (udev->quirks & USB_QUIRK_DELAY_INIT)
431993362a87SPhil Dibowitz 			msleep(1000);
432093362a87SPhil Dibowitz 
43211da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
43221da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
43231da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
43241da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
43251da177e4SLinus Torvalds 		 * on the parent.
43261da177e4SLinus Torvalds 		 */
43271da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
432855c52718SAlan Stern 				&& udev->bus_mA <= 100) {
43291da177e4SLinus Torvalds 			u16	devstat;
43301da177e4SLinus Torvalds 
43311da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
43321da177e4SLinus Torvalds 					&devstat);
433355c52718SAlan Stern 			if (status < 2) {
43341da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
43351da177e4SLinus Torvalds 				goto loop_disable;
43361da177e4SLinus Torvalds 			}
433755c52718SAlan Stern 			le16_to_cpus(&devstat);
43381da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
43391da177e4SLinus Torvalds 				dev_err(&udev->dev,
43401da177e4SLinus Torvalds 					"can't connect bus-powered hub "
43411da177e4SLinus Torvalds 					"to this port\n");
43421da177e4SLinus Torvalds 				if (hub->has_indicators) {
43431da177e4SLinus Torvalds 					hub->indicator[port1-1] =
43441da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
4345c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
43461da177e4SLinus Torvalds 				}
43471da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
43481da177e4SLinus Torvalds 				goto loop_disable;
43491da177e4SLinus Torvalds 			}
43501da177e4SLinus Torvalds 		}
43511da177e4SLinus Torvalds 
43521da177e4SLinus Torvalds 		/* check for devices running slower than they could */
43531da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
43541da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
43551da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
43561da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
43571da177e4SLinus Torvalds 
4358fa286188SGreg Kroah-Hartman 		/* Store the parent's children[] pointer.  At this point
43591da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
43601da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
43611da177e4SLinus Torvalds 		 */
43621da177e4SLinus Torvalds 		status = 0;
43631da177e4SLinus Torvalds 
43641da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
43651da177e4SLinus Torvalds 		 * been disconnected; we would race with the
43661da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
43671da177e4SLinus Torvalds 		 */
43681da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
43691da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
43701da177e4SLinus Torvalds 			status = -ENOTCONN;
43711da177e4SLinus Torvalds 		else
4372ff823c79SLan Tianyu 			hub->ports[port1 - 1]->child = udev;
43731da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
43741da177e4SLinus Torvalds 
43751da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
43761da177e4SLinus Torvalds 		if (!status) {
43771da177e4SLinus Torvalds 			status = usb_new_device(udev);
43781da177e4SLinus Torvalds 			if (status) {
43791da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
4380ff823c79SLan Tianyu 				hub->ports[port1 - 1]->child = NULL;
43811da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
43821da177e4SLinus Torvalds 			}
43831da177e4SLinus Torvalds 		}
43841da177e4SLinus Torvalds 
43851da177e4SLinus Torvalds 		if (status)
43861da177e4SLinus Torvalds 			goto loop_disable;
43871da177e4SLinus Torvalds 
43881da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
43891da177e4SLinus Torvalds 		if (status)
439055c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
43911da177e4SLinus Torvalds 
43921da177e4SLinus Torvalds 		return;
43931da177e4SLinus Torvalds 
43941da177e4SLinus Torvalds loop_disable:
43951da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
43961da177e4SLinus Torvalds loop:
4397fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
43983b29b68bSAlan Stern 		release_devnum(udev);
4399f7410cedSHerbert Xu 		hub_free_dev(udev);
44001da177e4SLinus Torvalds 		usb_put_dev(udev);
4401ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
44021da177e4SLinus Torvalds 			break;
44031da177e4SLinus Torvalds 	}
44043a31155cSAlan Stern 	if (hub->hdev->parent ||
44053a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
44063a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
44073a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
44083a31155cSAlan Stern 				port1);
44091da177e4SLinus Torvalds 
44101da177e4SLinus Torvalds done:
44111da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
441290da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
441390da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
44141da177e4SLinus Torvalds }
44151da177e4SLinus Torvalds 
4416714b07beSSarah Sharp /* Returns 1 if there was a remote wakeup and a connect status change. */
4417714b07beSSarah Sharp static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
441872937e1eSSarah Sharp 		u16 portstatus, u16 portchange)
4419714b07beSSarah Sharp {
4420714b07beSSarah Sharp 	struct usb_device *hdev;
4421714b07beSSarah Sharp 	struct usb_device *udev;
4422714b07beSSarah Sharp 	int connect_change = 0;
4423714b07beSSarah Sharp 	int ret;
4424714b07beSSarah Sharp 
4425714b07beSSarah Sharp 	hdev = hub->hdev;
4426ff823c79SLan Tianyu 	udev = hub->ports[port - 1]->child;
44274ee823b8SSarah Sharp 	if (!hub_is_superspeed(hdev)) {
4428714b07beSSarah Sharp 		if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4429714b07beSSarah Sharp 			return 0;
4430714b07beSSarah Sharp 		clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
44314ee823b8SSarah Sharp 	} else {
44324ee823b8SSarah Sharp 		if (!udev || udev->state != USB_STATE_SUSPENDED ||
443372937e1eSSarah Sharp 				 (portstatus & USB_PORT_STAT_LINK_STATE) !=
443472937e1eSSarah Sharp 				 USB_SS_PORT_LS_U0)
44354ee823b8SSarah Sharp 			return 0;
44364ee823b8SSarah Sharp 	}
4437714b07beSSarah Sharp 
4438714b07beSSarah Sharp 	if (udev) {
4439714b07beSSarah Sharp 		/* TRSMRCY = 10 msec */
4440714b07beSSarah Sharp 		msleep(10);
4441714b07beSSarah Sharp 
4442714b07beSSarah Sharp 		usb_lock_device(udev);
4443714b07beSSarah Sharp 		ret = usb_remote_wakeup(udev);
4444714b07beSSarah Sharp 		usb_unlock_device(udev);
4445714b07beSSarah Sharp 		if (ret < 0)
4446714b07beSSarah Sharp 			connect_change = 1;
4447714b07beSSarah Sharp 	} else {
4448714b07beSSarah Sharp 		ret = -ENODEV;
4449714b07beSSarah Sharp 		hub_port_disable(hub, port, 1);
4450714b07beSSarah Sharp 	}
4451714b07beSSarah Sharp 	dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4452714b07beSSarah Sharp 			port, ret);
4453714b07beSSarah Sharp 	return connect_change;
4454714b07beSSarah Sharp }
4455714b07beSSarah Sharp 
44561da177e4SLinus Torvalds static void hub_events(void)
44571da177e4SLinus Torvalds {
44581da177e4SLinus Torvalds 	struct list_head *tmp;
44591da177e4SLinus Torvalds 	struct usb_device *hdev;
44601da177e4SLinus Torvalds 	struct usb_interface *intf;
44611da177e4SLinus Torvalds 	struct usb_hub *hub;
44621da177e4SLinus Torvalds 	struct device *hub_dev;
44631da177e4SLinus Torvalds 	u16 hubstatus;
44641da177e4SLinus Torvalds 	u16 hubchange;
44651da177e4SLinus Torvalds 	u16 portstatus;
44661da177e4SLinus Torvalds 	u16 portchange;
44671da177e4SLinus Torvalds 	int i, ret;
44684ee823b8SSarah Sharp 	int connect_change, wakeup_change;
44691da177e4SLinus Torvalds 
44701da177e4SLinus Torvalds 	/*
44711da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
44721da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
44731da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
44741da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
44751da177e4SLinus Torvalds 	 */
44761da177e4SLinus Torvalds 	while (1) {
44771da177e4SLinus Torvalds 
44781da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
44791da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
44801da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
44811da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
44821da177e4SLinus Torvalds 			break;
44831da177e4SLinus Torvalds 		}
44841da177e4SLinus Torvalds 
44851da177e4SLinus Torvalds 		tmp = hub_event_list.next;
44861da177e4SLinus Torvalds 		list_del_init(tmp);
44871da177e4SLinus Torvalds 
44881da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
4489e8054854SAlan Stern 		kref_get(&hub->kref);
4490e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
44911da177e4SLinus Torvalds 
4492e8054854SAlan Stern 		hdev = hub->hdev;
4493e8054854SAlan Stern 		hub_dev = hub->intfdev;
4494e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
449540f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
44961da177e4SLinus Torvalds 				hdev->state, hub->descriptor
44971da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
44981da177e4SLinus Torvalds 					: 0,
44991da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
45001da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
450140f122f3SAlan Stern 				(u16) hub->event_bits[0]);
45021da177e4SLinus Torvalds 
45031da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
45041da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
450506b84e8aSAlan Stern 		usb_lock_device(hdev);
4506e8054854SAlan Stern 		if (unlikely(hub->disconnected))
45079bbdf1e0SAlan Stern 			goto loop_disconnected;
45081da177e4SLinus Torvalds 
45091da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
45101da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
45117de18d8bSAlan Stern 			hub->error = -ENODEV;
45124330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
45131da177e4SLinus Torvalds 			goto loop;
45141da177e4SLinus Torvalds 		}
45151da177e4SLinus Torvalds 
451640f122f3SAlan Stern 		/* Autoresume */
451740f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
451840f122f3SAlan Stern 		if (ret) {
451940f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
45201da177e4SLinus Torvalds 			goto loop;
452140f122f3SAlan Stern 		}
452240f122f3SAlan Stern 
452340f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
452440f122f3SAlan Stern 		if (hub->quiescing)
452540f122f3SAlan Stern 			goto loop_autopm;
45261da177e4SLinus Torvalds 
45271da177e4SLinus Torvalds 		if (hub->error) {
45281da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
45291da177e4SLinus Torvalds 				hub->error);
45301da177e4SLinus Torvalds 
4531742120c6SMing Lei 			ret = usb_reset_device(hdev);
45321da177e4SLinus Torvalds 			if (ret) {
45331da177e4SLinus Torvalds 				dev_dbg (hub_dev,
45341da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
453540f122f3SAlan Stern 				goto loop_autopm;
45361da177e4SLinus Torvalds 			}
45371da177e4SLinus Torvalds 
45381da177e4SLinus Torvalds 			hub->nerrors = 0;
45391da177e4SLinus Torvalds 			hub->error = 0;
45401da177e4SLinus Torvalds 		}
45411da177e4SLinus Torvalds 
45421da177e4SLinus Torvalds 		/* deal with port status changes */
45431da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
45441da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
45451da177e4SLinus Torvalds 				continue;
45461da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
454772937e1eSSarah Sharp 			wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
45481da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
45494ee823b8SSarah Sharp 					!connect_change && !wakeup_change)
45501da177e4SLinus Torvalds 				continue;
45511da177e4SLinus Torvalds 
45521da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
45531da177e4SLinus Torvalds 					&portstatus, &portchange);
45541da177e4SLinus Torvalds 			if (ret < 0)
45551da177e4SLinus Torvalds 				continue;
45561da177e4SLinus Torvalds 
45571da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
45581da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
45591da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
45601da177e4SLinus Torvalds 				connect_change = 1;
45611da177e4SLinus Torvalds 			}
45621da177e4SLinus Torvalds 
45631da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
45641da177e4SLinus Torvalds 				if (!connect_change)
45651da177e4SLinus Torvalds 					dev_dbg (hub_dev,
45661da177e4SLinus Torvalds 						"port %d enable change, "
45671da177e4SLinus Torvalds 						"status %08x\n",
45681da177e4SLinus Torvalds 						i, portstatus);
45691da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
45701da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
45711da177e4SLinus Torvalds 
45721da177e4SLinus Torvalds 				/*
45731da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
45741da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
45751da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
45761da177e4SLinus Torvalds 				 * Works at least with mouse driver.
45771da177e4SLinus Torvalds 				 */
45781da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
45791da177e4SLinus Torvalds 				    && !connect_change
4580ff823c79SLan Tianyu 				    && hub->ports[i - 1]->child) {
45811da177e4SLinus Torvalds 					dev_err (hub_dev,
45821da177e4SLinus Torvalds 					    "port %i "
45831da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
45841da177e4SLinus Torvalds 					    "re-enabling...\n",
45851da177e4SLinus Torvalds 						i);
45861da177e4SLinus Torvalds 					connect_change = 1;
45871da177e4SLinus Torvalds 				}
45881da177e4SLinus Torvalds 			}
45891da177e4SLinus Torvalds 
459072937e1eSSarah Sharp 			if (hub_handle_remote_wakeup(hub, i,
459172937e1eSSarah Sharp 						portstatus, portchange))
45921da177e4SLinus Torvalds 				connect_change = 1;
45931da177e4SLinus Torvalds 
45941da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4595752d57a8SPaul Bolle 				u16 status = 0;
4596752d57a8SPaul Bolle 				u16 unused;
4597752d57a8SPaul Bolle 
4598752d57a8SPaul Bolle 				dev_dbg(hub_dev, "over-current change on port "
4599752d57a8SPaul Bolle 					"%d\n", i);
46001da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
46011da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
4602752d57a8SPaul Bolle 				msleep(100);	/* Cool down */
46038520f380SAlan Stern 				hub_power_on(hub, true);
4604752d57a8SPaul Bolle 				hub_port_status(hub, i, &status, &unused);
4605752d57a8SPaul Bolle 				if (status & USB_PORT_STAT_OVERCURRENT)
4606752d57a8SPaul Bolle 					dev_err(hub_dev, "over-current "
4607752d57a8SPaul Bolle 						"condition on port %d\n", i);
46081da177e4SLinus Torvalds 			}
46091da177e4SLinus Torvalds 
46101da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
46111da177e4SLinus Torvalds 				dev_dbg (hub_dev,
46121da177e4SLinus Torvalds 					"reset change on port %d\n",
46131da177e4SLinus Torvalds 					i);
46141da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
46151da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
46161da177e4SLinus Torvalds 			}
4617c7061574SSarah Sharp 			if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4618c7061574SSarah Sharp 					hub_is_superspeed(hub->hdev)) {
4619c7061574SSarah Sharp 				dev_dbg(hub_dev,
4620c7061574SSarah Sharp 					"warm reset change on port %d\n",
4621c7061574SSarah Sharp 					i);
4622c7061574SSarah Sharp 				clear_port_feature(hdev, i,
4623c7061574SSarah Sharp 					USB_PORT_FEAT_C_BH_PORT_RESET);
4624c7061574SSarah Sharp 			}
4625dbe79bbeSJohn Youn 			if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4626dbe79bbeSJohn Youn 				clear_port_feature(hub->hdev, i,
4627dbe79bbeSJohn Youn 						USB_PORT_FEAT_C_PORT_LINK_STATE);
4628dbe79bbeSJohn Youn 			}
4629dbe79bbeSJohn Youn 			if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4630dbe79bbeSJohn Youn 				dev_warn(hub_dev,
4631dbe79bbeSJohn Youn 					"config error on port %d\n",
4632dbe79bbeSJohn Youn 					i);
4633dbe79bbeSJohn Youn 				clear_port_feature(hub->hdev, i,
4634dbe79bbeSJohn Youn 						USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4635dbe79bbeSJohn Youn 			}
46361da177e4SLinus Torvalds 
46375e467f6eSAndiry Xu 			/* Warm reset a USB3 protocol port if it's in
46385e467f6eSAndiry Xu 			 * SS.Inactive state.
46395e467f6eSAndiry Xu 			 */
46408bea2bd3SStanislaw Ledwon 			if (hub_port_warm_reset_required(hub, portstatus)) {
46415e467f6eSAndiry Xu 				dev_dbg(hub_dev, "warm reset port %d\n", i);
464275d7cf72SAndiry Xu 				hub_port_reset(hub, i, NULL,
464375d7cf72SAndiry Xu 						HUB_BH_RESET_TIME, true);
46445e467f6eSAndiry Xu 			}
46455e467f6eSAndiry Xu 
46461da177e4SLinus Torvalds 			if (connect_change)
46471da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
46481da177e4SLinus Torvalds 						portstatus, portchange);
46491da177e4SLinus Torvalds 		} /* end for i */
46501da177e4SLinus Torvalds 
46511da177e4SLinus Torvalds 		/* deal with hub status changes */
46521da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
46531da177e4SLinus Torvalds 			;	/* do nothing */
46541da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
46551da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
46561da177e4SLinus Torvalds 		else {
46571da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
46581da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
46591da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
466055c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
466155c52718SAlan Stern 					/* FIXME: Is this always true? */
466255c52718SAlan Stern 					hub->limited_power = 1;
4663403fae78Sjidong xiao 				else
4664403fae78Sjidong xiao 					hub->limited_power = 0;
46651da177e4SLinus Torvalds 			}
46661da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
4667752d57a8SPaul Bolle 				u16 status = 0;
4668752d57a8SPaul Bolle 				u16 unused;
4669752d57a8SPaul Bolle 
4670752d57a8SPaul Bolle 				dev_dbg(hub_dev, "over-current change\n");
46711da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
4672752d57a8SPaul Bolle 				msleep(500);	/* Cool down */
46738520f380SAlan Stern                         	hub_power_on(hub, true);
4674752d57a8SPaul Bolle 				hub_hub_status(hub, &status, &unused);
4675752d57a8SPaul Bolle 				if (status & HUB_STATUS_OVERCURRENT)
4676752d57a8SPaul Bolle 					dev_err(hub_dev, "over-current "
4677752d57a8SPaul Bolle 						"condition\n");
46781da177e4SLinus Torvalds 			}
46791da177e4SLinus Torvalds 		}
46801da177e4SLinus Torvalds 
468140f122f3SAlan Stern  loop_autopm:
46828e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface() above */
46838e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
46841da177e4SLinus Torvalds  loop:
46858e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface_no_resume() in
46868e4ceb38SAlan Stern 		 * kick_khubd() and allow autosuspend.
46878e4ceb38SAlan Stern 		 */
46888e4ceb38SAlan Stern 		usb_autopm_put_interface(intf);
46899bbdf1e0SAlan Stern  loop_disconnected:
46901da177e4SLinus Torvalds 		usb_unlock_device(hdev);
4691e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
46921da177e4SLinus Torvalds 
46931da177e4SLinus Torvalds         } /* end while (1) */
46941da177e4SLinus Torvalds }
46951da177e4SLinus Torvalds 
46961da177e4SLinus Torvalds static int hub_thread(void *__unused)
46971da177e4SLinus Torvalds {
46983bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
46993bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
47003bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
47013bb1af52SAlan Stern 	 * the companion full-speed controller.
47023bb1af52SAlan Stern 	 */
470383144186SRafael J. Wysocki 	set_freezable();
47043bb1af52SAlan Stern 
47051da177e4SLinus Torvalds 	do {
47061da177e4SLinus Torvalds 		hub_events();
4707e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
47089c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
47099c8d6178Sakpm@osdl.org 				kthread_should_stop());
47109c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
47111da177e4SLinus Torvalds 
47121da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
47139c8d6178Sakpm@osdl.org 	return 0;
47141da177e4SLinus Torvalds }
47151da177e4SLinus Torvalds 
47161e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = {
4717e6f30deaSMing Lei     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4718e6f30deaSMing Lei 	           | USB_DEVICE_ID_MATCH_INT_CLASS,
4719e6f30deaSMing Lei       .idVendor = USB_VENDOR_GENESYS_LOGIC,
4720e6f30deaSMing Lei       .bInterfaceClass = USB_CLASS_HUB,
4721e6f30deaSMing Lei       .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
47221da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
47231da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
47241da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
47251da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
47261da177e4SLinus Torvalds     { }						/* Terminating entry */
47271da177e4SLinus Torvalds };
47281da177e4SLinus Torvalds 
47291da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
47301da177e4SLinus Torvalds 
47311da177e4SLinus Torvalds static struct usb_driver hub_driver = {
47321da177e4SLinus Torvalds 	.name =		"hub",
47331da177e4SLinus Torvalds 	.probe =	hub_probe,
47341da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
47351da177e4SLinus Torvalds 	.suspend =	hub_suspend,
47361da177e4SLinus Torvalds 	.resume =	hub_resume,
4737f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
47387de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
47397de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
4740c532b29aSAndi Kleen 	.unlocked_ioctl = hub_ioctl,
47411da177e4SLinus Torvalds 	.id_table =	hub_id_table,
474240f122f3SAlan Stern 	.supports_autosuspend =	1,
47431da177e4SLinus Torvalds };
47441da177e4SLinus Torvalds 
47451da177e4SLinus Torvalds int usb_hub_init(void)
47461da177e4SLinus Torvalds {
47471da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
47481da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
47491da177e4SLinus Torvalds 			usbcore_name);
47501da177e4SLinus Torvalds 		return -1;
47511da177e4SLinus Torvalds 	}
47521da177e4SLinus Torvalds 
47539c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
47549c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
47551da177e4SLinus Torvalds 		return 0;
47561da177e4SLinus Torvalds 
47571da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
47581da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
47591da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
47601da177e4SLinus Torvalds 
47611da177e4SLinus Torvalds 	return -1;
47621da177e4SLinus Torvalds }
47631da177e4SLinus Torvalds 
47641da177e4SLinus Torvalds void usb_hub_cleanup(void)
47651da177e4SLinus Torvalds {
47669c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
47671da177e4SLinus Torvalds 
47681da177e4SLinus Torvalds 	/*
47691da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
47701da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
47711da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
47721da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
47731da177e4SLinus Torvalds 	 * individual hub resources. -greg
47741da177e4SLinus Torvalds 	 */
47751da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
47761da177e4SLinus Torvalds } /* usb_hub_cleanup() */
47771da177e4SLinus Torvalds 
4778eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
4779eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
47801da177e4SLinus Torvalds {
4781eb764c4bSAlan Stern 	int		changed = 0;
47821da177e4SLinus Torvalds 	unsigned	index;
4783eb764c4bSAlan Stern 	unsigned	serial_len = 0;
4784eb764c4bSAlan Stern 	unsigned	len;
4785eb764c4bSAlan Stern 	unsigned	old_length;
4786eb764c4bSAlan Stern 	int		length;
4787eb764c4bSAlan Stern 	char		*buf;
47881da177e4SLinus Torvalds 
4789eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
4790eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
4791eb764c4bSAlan Stern 		return 1;
4792eb764c4bSAlan Stern 
4793eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
4794eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
4795eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
4796eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
4797eb764c4bSAlan Stern 	 * different flash card of the same brand).
4798eb764c4bSAlan Stern 	 */
4799eb764c4bSAlan Stern 	if (udev->serial)
4800eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
4801eb764c4bSAlan Stern 
4802eb764c4bSAlan Stern 	len = serial_len;
48031da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4804eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4805eb764c4bSAlan Stern 		len = max(len, old_length);
48061da177e4SLinus Torvalds 	}
4807eb764c4bSAlan Stern 
48080cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
48091da177e4SLinus Torvalds 	if (buf == NULL) {
48101da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
48111da177e4SLinus Torvalds 		/* assume the worst */
48121da177e4SLinus Torvalds 		return 1;
48131da177e4SLinus Torvalds 	}
48141da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4815eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
48161da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
48171da177e4SLinus Torvalds 				old_length);
4818eb764c4bSAlan Stern 		if (length != old_length) {
48191da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
48201da177e4SLinus Torvalds 					index, length);
4821eb764c4bSAlan Stern 			changed = 1;
48221da177e4SLinus Torvalds 			break;
48231da177e4SLinus Torvalds 		}
48241da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
48251da177e4SLinus Torvalds 				!= 0) {
48261da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
4827eb764c4bSAlan Stern 				index,
4828eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
4829eb764c4bSAlan Stern 					bConfigurationValue);
4830eb764c4bSAlan Stern 			changed = 1;
48311da177e4SLinus Torvalds 			break;
48321da177e4SLinus Torvalds 		}
48331da177e4SLinus Torvalds 	}
4834eb764c4bSAlan Stern 
4835eb764c4bSAlan Stern 	if (!changed && serial_len) {
4836eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
4837eb764c4bSAlan Stern 				buf, serial_len);
4838eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
4839eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
4840eb764c4bSAlan Stern 					length);
4841eb764c4bSAlan Stern 			changed = 1;
4842eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
4843eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
4844eb764c4bSAlan Stern 			changed = 1;
4845eb764c4bSAlan Stern 		}
4846eb764c4bSAlan Stern 	}
4847eb764c4bSAlan Stern 
48481da177e4SLinus Torvalds 	kfree(buf);
4849eb764c4bSAlan Stern 	return changed;
48501da177e4SLinus Torvalds }
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds /**
4853742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
48541da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
48551da177e4SLinus Torvalds  *
485679efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
485779efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
4858742120c6SMing Lei  * Use usb_reset_device() instead.
48591da177e4SLinus Torvalds  *
48601da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
48611da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
48621da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
48631da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
48641da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
48651da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
48661da177e4SLinus Torvalds  * re-enumerated and probed all over again.
48671da177e4SLinus Torvalds  *
48681da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
48691da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
48701da177e4SLinus Torvalds  * if the reset wasn't even attempted.
48711da177e4SLinus Torvalds  *
48721da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
48731da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
48741da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
48751da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
48766bc6cff5SAlan Stern  *
48776bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
48786bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
48796bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
48806bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
48811da177e4SLinus Torvalds  */
4882742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
48831da177e4SLinus Torvalds {
48841da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
48851da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
48863f0479e0SSarah Sharp 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
48871da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
488812c3da34SAlan Stern 	int 				i, ret = 0;
488912c3da34SAlan Stern 	int				port1 = udev->portnum;
48901da177e4SLinus Torvalds 
48911da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
48921da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
48931da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
48941da177e4SLinus Torvalds 				udev->state);
48951da177e4SLinus Torvalds 		return -EINVAL;
48961da177e4SLinus Torvalds 	}
48971da177e4SLinus Torvalds 
48981da177e4SLinus Torvalds 	if (!parent_hdev) {
48994bec9917SWolfram Sang 		/* this requires hcd-specific logic; see ohci_restart() */
4900441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
49011da177e4SLinus Torvalds 		return -EISDIR;
49021da177e4SLinus Torvalds 	}
49031da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
49041da177e4SLinus Torvalds 
4905f74631e3SSarah Sharp 	/* Disable LPM and LTM while we reset the device and reinstall the alt
4906f74631e3SSarah Sharp 	 * settings.  Device-initiated LPM settings, and system exit latency
4907f74631e3SSarah Sharp 	 * settings are cleared when the device is reset, so we have to set
4908f74631e3SSarah Sharp 	 * them up again.
49096d1d0513SSarah Sharp 	 */
49106d1d0513SSarah Sharp 	ret = usb_unlocked_disable_lpm(udev);
49116d1d0513SSarah Sharp 	if (ret) {
49126d1d0513SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
49136d1d0513SSarah Sharp 		goto re_enumerate;
49146d1d0513SSarah Sharp 	}
4915f74631e3SSarah Sharp 	ret = usb_disable_ltm(udev);
4916f74631e3SSarah Sharp 	if (ret) {
4917f74631e3SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4918f74631e3SSarah Sharp 				__func__);
4919f74631e3SSarah Sharp 		goto re_enumerate;
4920f74631e3SSarah Sharp 	}
49216d1d0513SSarah Sharp 
49221da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
49231da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
49241da177e4SLinus Torvalds 
49251da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
49261da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
4927fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
49281da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
4929dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
49301da177e4SLinus Torvalds 			break;
49311da177e4SLinus Torvalds 	}
49321da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
4933d5cbad4bSAlan Stern 
49341da177e4SLinus Torvalds 	if (ret < 0)
49351da177e4SLinus Torvalds 		goto re_enumerate;
49361da177e4SLinus Torvalds 
49371da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
4938eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
49391da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
49401da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
49411da177e4SLinus Torvalds 		goto re_enumerate;
49421da177e4SLinus Torvalds   	}
49431da177e4SLinus Torvalds 
49444fe0387aSAlan Stern 	/* Restore the device's previous configuration */
49451da177e4SLinus Torvalds 	if (!udev->actconfig)
49461da177e4SLinus Torvalds 		goto done;
49473f0479e0SSarah Sharp 
4948d673bfcbSSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
49493f0479e0SSarah Sharp 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
49503f0479e0SSarah Sharp 	if (ret < 0) {
49513f0479e0SSarah Sharp 		dev_warn(&udev->dev,
49523f0479e0SSarah Sharp 				"Busted HC?  Not enough HCD resources for "
49533f0479e0SSarah Sharp 				"old configuration.\n");
4954d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
49553f0479e0SSarah Sharp 		goto re_enumerate;
49563f0479e0SSarah Sharp 	}
49571da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
49581da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
49591da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
49601da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
49611da177e4SLinus Torvalds 	if (ret < 0) {
49621da177e4SLinus Torvalds 		dev_err(&udev->dev,
49631da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
49641da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
4965d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
49661da177e4SLinus Torvalds 		goto re_enumerate;
49671da177e4SLinus Torvalds   	}
4968d673bfcbSSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
49691da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
49701da177e4SLinus Torvalds 
49714fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
49724fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
49734fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
49744fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
49754fe0387aSAlan Stern 	 * endpoint state.
49764fe0387aSAlan Stern 	 */
49771da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
49783f0479e0SSarah Sharp 		struct usb_host_config *config = udev->actconfig;
49793f0479e0SSarah Sharp 		struct usb_interface *intf = config->interface[i];
49801da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
49811da177e4SLinus Torvalds 
49821da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
49834fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
49844fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
49854fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
49864fe0387aSAlan Stern 			ret = 0;
49874fe0387aSAlan Stern 		} else {
498804a723eaSSarah Sharp 			/* Let the bandwidth allocation function know that this
498904a723eaSSarah Sharp 			 * device has been reset, and it will have to use
499004a723eaSSarah Sharp 			 * alternate setting 0 as the current alternate setting.
49913f0479e0SSarah Sharp 			 */
499204a723eaSSarah Sharp 			intf->resetting_device = 1;
49931da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
49941da177e4SLinus Torvalds 					desc->bAlternateSetting);
499504a723eaSSarah Sharp 			intf->resetting_device = 0;
49964fe0387aSAlan Stern 		}
49971da177e4SLinus Torvalds 		if (ret < 0) {
49981da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
49991da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
50001da177e4SLinus Torvalds 				desc->bInterfaceNumber,
50011da177e4SLinus Torvalds 				desc->bAlternateSetting,
50021da177e4SLinus Torvalds 				ret);
50031da177e4SLinus Torvalds 			goto re_enumerate;
50041da177e4SLinus Torvalds 		}
50051da177e4SLinus Torvalds 	}
50061da177e4SLinus Torvalds 
50071da177e4SLinus Torvalds done:
5008f74631e3SSarah Sharp 	/* Now that the alt settings are re-installed, enable LTM and LPM. */
500978d9a487SAlan Stern 	usb_unlocked_enable_lpm(udev);
5010f74631e3SSarah Sharp 	usb_enable_ltm(udev);
50111da177e4SLinus Torvalds 	return 0;
50121da177e4SLinus Torvalds 
50131da177e4SLinus Torvalds re_enumerate:
50146d1d0513SSarah Sharp 	/* LPM state doesn't matter when we're about to destroy the device. */
50151da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
50161da177e4SLinus Torvalds 	return -ENODEV;
50171da177e4SLinus Torvalds }
50181da177e4SLinus Torvalds 
50191da177e4SLinus Torvalds /**
50201da177e4SLinus Torvalds  * usb_reset_device - warn interface drivers and perform a USB port reset
50211da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
50221da177e4SLinus Torvalds  *
502379efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
502479efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
5025742120c6SMing Lei  * the reset is over (using their post_reset method).
502679efa097SAlan Stern  *
502779efa097SAlan Stern  * Return value is the same as for usb_reset_and_verify_device().
502879efa097SAlan Stern  *
502979efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
503079efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
503179efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
5032742120c6SMing Lei  * the device using usb_lock_device_for_reset().
503379efa097SAlan Stern  *
503479efa097SAlan Stern  * If an interface is currently being probed or disconnected, we assume
503579efa097SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
503679efa097SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
503779efa097SAlan Stern  * we attempt to unbind it and rebind afterward.
503879efa097SAlan Stern  */
5039742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
504079efa097SAlan Stern {
504179efa097SAlan Stern 	int ret;
5042852c4b43SAlan Stern 	int i;
504379efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
504479efa097SAlan Stern 
504579efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
504679efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
504779efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
504879efa097SAlan Stern 				udev->state);
504979efa097SAlan Stern 		return -EINVAL;
505079efa097SAlan Stern 	}
505179efa097SAlan Stern 
5052645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
505394fcda1fSAlan Stern 	usb_autoresume_device(udev);
5054645daaabSAlan Stern 
505579efa097SAlan Stern 	if (config) {
5056852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
5057852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
505879efa097SAlan Stern 			struct usb_driver *drv;
505978d9a487SAlan Stern 			int unbind = 0;
506079efa097SAlan Stern 
5061852c4b43SAlan Stern 			if (cintf->dev.driver) {
506279efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
506378d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
506478d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
506578d9a487SAlan Stern 				else if (cintf->condition ==
506678d9a487SAlan Stern 						USB_INTERFACE_BOUND)
506778d9a487SAlan Stern 					unbind = 1;
506878d9a487SAlan Stern 				if (unbind)
506978d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
507079efa097SAlan Stern 			}
507179efa097SAlan Stern 		}
507279efa097SAlan Stern 	}
507379efa097SAlan Stern 
5074742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
507579efa097SAlan Stern 
507679efa097SAlan Stern 	if (config) {
5077852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
5078852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
507979efa097SAlan Stern 			struct usb_driver *drv;
508078d9a487SAlan Stern 			int rebind = cintf->needs_binding;
508179efa097SAlan Stern 
508278d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
508379efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
508479efa097SAlan Stern 				if (drv->post_reset)
508578d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
508678d9a487SAlan Stern 				else if (cintf->condition ==
508778d9a487SAlan Stern 						USB_INTERFACE_BOUND)
508878d9a487SAlan Stern 					rebind = 1;
508979efa097SAlan Stern 			}
50906c640945SAlan Stern 			if (ret == 0 && rebind)
509178d9a487SAlan Stern 				usb_rebind_intf(cintf);
509279efa097SAlan Stern 		}
509379efa097SAlan Stern 	}
509479efa097SAlan Stern 
509594fcda1fSAlan Stern 	usb_autosuspend_device(udev);
509679efa097SAlan Stern 	return ret;
509779efa097SAlan Stern }
5098742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
5099dc023dceSInaky Perez-Gonzalez 
5100dc023dceSInaky Perez-Gonzalez 
5101dc023dceSInaky Perez-Gonzalez /**
5102dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
5103dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
5104dc023dceSInaky Perez-Gonzalez  *
5105dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
5106dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
5107dc023dceSInaky Perez-Gonzalez  *
5108dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
5109dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
5110dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
5111dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
5112dc023dceSInaky Perez-Gonzalez  *
5113dc023dceSInaky Perez-Gonzalez  * Corner cases:
5114dc023dceSInaky Perez-Gonzalez  *
5115dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
5116dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
5117dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
5118dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
5119dc023dceSInaky Perez-Gonzalez  *
5120dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
5121dc023dceSInaky Perez-Gonzalez  *   be cancelled.
5122dc023dceSInaky Perez-Gonzalez  *
5123dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
5124dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
5125dc023dceSInaky Perez-Gonzalez  *   cancelled.
5126dc023dceSInaky Perez-Gonzalez  *
5127dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
5128dc023dceSInaky Perez-Gonzalez  * does its own.
5129dc023dceSInaky Perez-Gonzalez  *
5130dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
5131dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
5132dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
5133dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
5134dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
5135dc023dceSInaky Perez-Gonzalez  *     are referenced.
5136dc023dceSInaky Perez-Gonzalez  */
5137dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
5138dc023dceSInaky Perez-Gonzalez {
5139dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
5140dc023dceSInaky Perez-Gonzalez }
5141dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
5142ff823c79SLan Tianyu 
5143ff823c79SLan Tianyu /**
5144ff823c79SLan Tianyu  * usb_hub_find_child - Get the pointer of child device
5145ff823c79SLan Tianyu  * attached to the port which is specified by @port1.
5146ff823c79SLan Tianyu  * @hdev: USB device belonging to the usb hub
5147ff823c79SLan Tianyu  * @port1: port num to indicate which port the child device
5148ff823c79SLan Tianyu  *	is attached to.
5149ff823c79SLan Tianyu  *
5150ff823c79SLan Tianyu  * USB drivers call this function to get hub's child device
5151ff823c79SLan Tianyu  * pointer.
5152ff823c79SLan Tianyu  *
5153ff823c79SLan Tianyu  * Return NULL if input param is invalid and
5154ff823c79SLan Tianyu  * child's usb_device pointer if non-NULL.
5155ff823c79SLan Tianyu  */
5156ff823c79SLan Tianyu struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5157ff823c79SLan Tianyu 		int port1)
5158ff823c79SLan Tianyu {
5159ff823c79SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
5160ff823c79SLan Tianyu 
5161ff823c79SLan Tianyu 	if (port1 < 1 || port1 > hdev->maxchild)
5162ff823c79SLan Tianyu 		return NULL;
5163ff823c79SLan Tianyu 	return hub->ports[port1 - 1]->child;
5164ff823c79SLan Tianyu }
5165ff823c79SLan Tianyu EXPORT_SYMBOL_GPL(usb_hub_find_child);
5166d5575424SLan Tianyu 
516705f91689SLan Tianyu /**
516805f91689SLan Tianyu  * usb_set_hub_port_connect_type - set hub port connect type.
516905f91689SLan Tianyu  * @hdev: USB device belonging to the usb hub
517005f91689SLan Tianyu  * @port1: port num of the port
517105f91689SLan Tianyu  * @type: connect type of the port
517205f91689SLan Tianyu  */
517305f91689SLan Tianyu void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
517405f91689SLan Tianyu 	enum usb_port_connect_type type)
517505f91689SLan Tianyu {
517605f91689SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
517705f91689SLan Tianyu 
517805f91689SLan Tianyu 	hub->ports[port1 - 1]->connect_type = type;
517905f91689SLan Tianyu }
518005f91689SLan Tianyu 
518105f91689SLan Tianyu /**
518205f91689SLan Tianyu  * usb_get_hub_port_connect_type - Get the port's connect type
518305f91689SLan Tianyu  * @hdev: USB device belonging to the usb hub
518405f91689SLan Tianyu  * @port1: port num of the port
518505f91689SLan Tianyu  *
518605f91689SLan Tianyu  * Return connect type of the port and if input params are
518705f91689SLan Tianyu  * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
518805f91689SLan Tianyu  */
518905f91689SLan Tianyu enum usb_port_connect_type
519005f91689SLan Tianyu usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
519105f91689SLan Tianyu {
519205f91689SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
519305f91689SLan Tianyu 
519405f91689SLan Tianyu 	return hub->ports[port1 - 1]->connect_type;
519505f91689SLan Tianyu }
519605f91689SLan Tianyu 
5197d5575424SLan Tianyu #ifdef CONFIG_ACPI
5198d5575424SLan Tianyu /**
5199d5575424SLan Tianyu  * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5200d5575424SLan Tianyu  * @hdev: USB device belonging to the usb hub
5201d5575424SLan Tianyu  * @port1: port num of the port
5202d5575424SLan Tianyu  *
5203d5575424SLan Tianyu  * Return port's acpi handle if successful, NULL if params are
5204d5575424SLan Tianyu  * invaild.
5205d5575424SLan Tianyu  */
5206d5575424SLan Tianyu acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5207d5575424SLan Tianyu 	int port1)
5208d5575424SLan Tianyu {
5209d5575424SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
5210d5575424SLan Tianyu 
5211d5575424SLan Tianyu 	return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5212d5575424SLan Tianyu }
5213d5575424SLan Tianyu #endif
5214