xref: /openbmc/linux/drivers/usb/core/hub.c (revision 05f91689)
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 
42fa2a9566SLan Tianyu struct usb_port {
43ff823c79SLan Tianyu 	struct usb_device *child;
44fa2a9566SLan Tianyu 	struct device dev;
45fa2a9566SLan Tianyu 	struct dev_state *port_owner;
4605f91689SLan Tianyu 	enum usb_port_connect_type connect_type;
47fa2a9566SLan Tianyu };
48fa2a9566SLan Tianyu 
491bb5f66bSAlan Stern struct usb_hub {
501bb5f66bSAlan Stern 	struct device		*intfdev;	/* the "interface" device */
511bb5f66bSAlan Stern 	struct usb_device	*hdev;
52e8054854SAlan Stern 	struct kref		kref;
531bb5f66bSAlan Stern 	struct urb		*urb;		/* for interrupt polling pipe */
541bb5f66bSAlan Stern 
551bb5f66bSAlan Stern 	/* buffer for urb ... with extra space in case of babble */
561bb5f66bSAlan Stern 	char			(*buffer)[8];
571bb5f66bSAlan Stern 	union {
581bb5f66bSAlan Stern 		struct usb_hub_status	hub;
591bb5f66bSAlan Stern 		struct usb_port_status	port;
601bb5f66bSAlan Stern 	}			*status;	/* buffer for status reports */
61db90e7a1SAlan Stern 	struct mutex		status_mutex;	/* for the status buffer */
621bb5f66bSAlan Stern 
631bb5f66bSAlan Stern 	int			error;		/* last reported error */
641bb5f66bSAlan Stern 	int			nerrors;	/* track consecutive errors */
651bb5f66bSAlan Stern 
661bb5f66bSAlan Stern 	struct list_head	event_list;	/* hubs w/data or errs ready */
671bb5f66bSAlan Stern 	unsigned long		event_bits[1];	/* status change bitmask */
681bb5f66bSAlan Stern 	unsigned long		change_bits[1];	/* ports with logical connect
691bb5f66bSAlan Stern 							status change */
701bb5f66bSAlan Stern 	unsigned long		busy_bits[1];	/* ports being reset or
711bb5f66bSAlan Stern 							resumed */
72253e0572SAlan Stern 	unsigned long		removed_bits[1]; /* ports with a "removed"
73253e0572SAlan Stern 							device present */
744ee823b8SSarah Sharp 	unsigned long		wakeup_bits[1];	/* ports that have signaled
754ee823b8SSarah Sharp 							remote wakeup */
761bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
771bb5f66bSAlan Stern #error event_bits[] is too short!
781bb5f66bSAlan Stern #endif
791bb5f66bSAlan Stern 
801bb5f66bSAlan Stern 	struct usb_hub_descriptor *descriptor;	/* class descriptor */
811bb5f66bSAlan Stern 	struct usb_tt		tt;		/* Transaction Translator */
821bb5f66bSAlan Stern 
831bb5f66bSAlan Stern 	unsigned		mA_per_port;	/* current for each child */
841bb5f66bSAlan Stern 
851bb5f66bSAlan Stern 	unsigned		limited_power:1;
861bb5f66bSAlan Stern 	unsigned		quiescing:1;
87e8054854SAlan Stern 	unsigned		disconnected:1;
881bb5f66bSAlan Stern 
891bb5f66bSAlan Stern 	unsigned		has_indicators:1;
901bb5f66bSAlan Stern 	u8			indicator[USB_MAXCHILDREN];
916d5aefb8SDavid Howells 	struct delayed_work	leds;
928520f380SAlan Stern 	struct delayed_work	init_work;
93fa2a9566SLan Tianyu 	struct usb_port		**ports;
941bb5f66bSAlan Stern };
951bb5f66bSAlan Stern 
96dbe79bbeSJohn Youn static inline int hub_is_superspeed(struct usb_device *hdev)
97dbe79bbeSJohn Youn {
987bf01185SAman Deep 	return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
99dbe79bbeSJohn Youn }
1001bb5f66bSAlan Stern 
101fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members
1029ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
1031da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
1041da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* khubd's worklist and its lock */
1071da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
1081da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* Wakes up khubd */
1111da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
1121da177e4SLinus Torvalds 
1139c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
11690ab5ee9SRusty Russell static bool blinkenlights = 0;
1171da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1181da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /*
121fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
122fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
123fd7c519dSJaroslav Kysela  */
124fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
125fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
126fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
127b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
128b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
129b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
130fd7c519dSJaroslav Kysela 
131fd7c519dSJaroslav Kysela /*
1321da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1331da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1341da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1351da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1361da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1371da177e4SLinus Torvalds  * following module parameters.
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1401da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1411da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1421da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1431da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1441da177e4SLinus Torvalds  */
14590ab5ee9SRusty Russell static bool old_scheme_first = 0;
1461da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1471da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1481da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1491da177e4SLinus Torvalds 
15090ab5ee9SRusty Russell static bool use_both_schemes = 1;
1511da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1521da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1531da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1541da177e4SLinus Torvalds 		"first one fails");
1551da177e4SLinus Torvalds 
15632fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
15732fe0198SAlan Stern  * port reset on some companion controllers.
15832fe0198SAlan Stern  */
15932fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
16032fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
16132fe0198SAlan Stern 
162948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
163948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
164948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
165948fea37SAlan Stern 
166fa2a9566SLan Tianyu #define to_usb_port(_dev) \
167fa2a9566SLan Tianyu 	container_of(_dev, struct usb_port, dev)
1681da177e4SLinus Torvalds 
169742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
170742120c6SMing Lei 
171131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus)
1721da177e4SLinus Torvalds {
173131dec34SSarah Sharp 	if (hub_is_superspeed(hub->hdev))
174131dec34SSarah Sharp 		return "5.0 Gb/s";
175288ead45SAlan Stern 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1761da177e4SLinus Torvalds     		return "480 Mb/s";
177288ead45SAlan Stern 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1781da177e4SLinus Torvalds 		return "1.5 Mb/s";
1791da177e4SLinus Torvalds 	else
1801da177e4SLinus Torvalds 		return "12 Mb/s";
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
18425118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1851da177e4SLinus Torvalds {
186ff823c79SLan Tianyu 	if (!hdev || !hdev->actconfig || !hdev->maxchild)
18725118084SAlan Stern 		return NULL;
1881da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191d9b2099cSSarah Sharp static int usb_device_supports_lpm(struct usb_device *udev)
192d9b2099cSSarah Sharp {
193d9b2099cSSarah Sharp 	/* USB 2.1 (and greater) devices indicate LPM support through
194d9b2099cSSarah Sharp 	 * their USB 2.0 Extended Capabilities BOS descriptor.
195d9b2099cSSarah Sharp 	 */
196d9b2099cSSarah Sharp 	if (udev->speed == USB_SPEED_HIGH) {
197d9b2099cSSarah Sharp 		if (udev->bos->ext_cap &&
198d9b2099cSSarah Sharp 			(USB_LPM_SUPPORT &
199d9b2099cSSarah Sharp 			 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
200d9b2099cSSarah Sharp 			return 1;
201d9b2099cSSarah Sharp 		return 0;
202d9b2099cSSarah Sharp 	}
20351e0a012SSarah Sharp 
20451e0a012SSarah Sharp 	/* All USB 3.0 must support LPM, but we need their max exit latency
20551e0a012SSarah Sharp 	 * information from the SuperSpeed Extended Capabilities BOS descriptor.
20651e0a012SSarah Sharp 	 */
20751e0a012SSarah Sharp 	if (!udev->bos->ss_cap) {
20851e0a012SSarah Sharp 		dev_warn(&udev->dev, "No LPM exit latency info found.  "
20951e0a012SSarah Sharp 				"Power management will be impacted.\n");
210d9b2099cSSarah Sharp 		return 0;
211d9b2099cSSarah Sharp 	}
21251e0a012SSarah Sharp 	if (udev->parent->lpm_capable)
21351e0a012SSarah Sharp 		return 1;
21451e0a012SSarah Sharp 
21551e0a012SSarah Sharp 	dev_warn(&udev->dev, "Parent hub missing LPM exit latency info.  "
21651e0a012SSarah Sharp 			"Power management will be impacted.\n");
21751e0a012SSarah Sharp 	return 0;
21851e0a012SSarah Sharp }
21951e0a012SSarah Sharp 
22051e0a012SSarah Sharp /*
22151e0a012SSarah Sharp  * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
22251e0a012SSarah Sharp  * either U1 or U2.
22351e0a012SSarah Sharp  */
22451e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev,
22551e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params,
22651e0a012SSarah Sharp 		unsigned int udev_exit_latency,
22751e0a012SSarah Sharp 		struct usb_hub *hub,
22851e0a012SSarah Sharp 		struct usb3_lpm_parameters *hub_lpm_params,
22951e0a012SSarah Sharp 		unsigned int hub_exit_latency)
23051e0a012SSarah Sharp {
23151e0a012SSarah Sharp 	unsigned int total_mel;
23251e0a012SSarah Sharp 	unsigned int device_mel;
23351e0a012SSarah Sharp 	unsigned int hub_mel;
23451e0a012SSarah Sharp 
23551e0a012SSarah Sharp 	/*
23651e0a012SSarah Sharp 	 * Calculate the time it takes to transition all links from the roothub
23751e0a012SSarah Sharp 	 * to the parent hub into U0.  The parent hub must then decode the
23851e0a012SSarah Sharp 	 * packet (hub header decode latency) to figure out which port it was
23951e0a012SSarah Sharp 	 * bound for.
24051e0a012SSarah Sharp 	 *
24151e0a012SSarah Sharp 	 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
24251e0a012SSarah Sharp 	 * means 0.1us).  Multiply that by 100 to get nanoseconds.
24351e0a012SSarah Sharp 	 */
24451e0a012SSarah Sharp 	total_mel = hub_lpm_params->mel +
24551e0a012SSarah Sharp 		(hub->descriptor->u.ss.bHubHdrDecLat * 100);
24651e0a012SSarah Sharp 
24751e0a012SSarah Sharp 	/*
24851e0a012SSarah Sharp 	 * How long will it take to transition the downstream hub's port into
24951e0a012SSarah Sharp 	 * U0?  The greater of either the hub exit latency or the device exit
25051e0a012SSarah Sharp 	 * latency.
25151e0a012SSarah Sharp 	 *
25251e0a012SSarah Sharp 	 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
25351e0a012SSarah Sharp 	 * Multiply that by 1000 to get nanoseconds.
25451e0a012SSarah Sharp 	 */
25551e0a012SSarah Sharp 	device_mel = udev_exit_latency * 1000;
25651e0a012SSarah Sharp 	hub_mel = hub_exit_latency * 1000;
25751e0a012SSarah Sharp 	if (device_mel > hub_mel)
25851e0a012SSarah Sharp 		total_mel += device_mel;
25951e0a012SSarah Sharp 	else
26051e0a012SSarah Sharp 		total_mel += hub_mel;
26151e0a012SSarah Sharp 
26251e0a012SSarah Sharp 	udev_lpm_params->mel = total_mel;
26351e0a012SSarah Sharp }
26451e0a012SSarah Sharp 
26551e0a012SSarah Sharp /*
26651e0a012SSarah Sharp  * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
26751e0a012SSarah Sharp  * a transition from either U1 or U2.
26851e0a012SSarah Sharp  */
26951e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev,
27051e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params,
27151e0a012SSarah Sharp 		unsigned int udev_exit_latency,
27251e0a012SSarah Sharp 		struct usb_hub *hub,
27351e0a012SSarah Sharp 		struct usb3_lpm_parameters *hub_lpm_params,
27451e0a012SSarah Sharp 		unsigned int hub_exit_latency,
27551e0a012SSarah Sharp 		unsigned int port_to_port_exit_latency)
27651e0a012SSarah Sharp {
27751e0a012SSarah Sharp 	unsigned int first_link_pel;
27851e0a012SSarah Sharp 	unsigned int hub_pel;
27951e0a012SSarah Sharp 
28051e0a012SSarah Sharp 	/*
28151e0a012SSarah Sharp 	 * First, the device sends an LFPS to transition the link between the
28251e0a012SSarah Sharp 	 * device and the parent hub into U0.  The exit latency is the bigger of
28351e0a012SSarah Sharp 	 * the device exit latency or the hub exit latency.
28451e0a012SSarah Sharp 	 */
28551e0a012SSarah Sharp 	if (udev_exit_latency > hub_exit_latency)
28651e0a012SSarah Sharp 		first_link_pel = udev_exit_latency * 1000;
28751e0a012SSarah Sharp 	else
28851e0a012SSarah Sharp 		first_link_pel = hub_exit_latency * 1000;
28951e0a012SSarah Sharp 
29051e0a012SSarah Sharp 	/*
29151e0a012SSarah Sharp 	 * When the hub starts to receive the LFPS, there is a slight delay for
29251e0a012SSarah Sharp 	 * it to figure out that one of the ports is sending an LFPS.  Then it
29351e0a012SSarah Sharp 	 * will forward the LFPS to its upstream link.  The exit latency is the
29451e0a012SSarah Sharp 	 * delay, plus the PEL that we calculated for this hub.
29551e0a012SSarah Sharp 	 */
29651e0a012SSarah Sharp 	hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
29751e0a012SSarah Sharp 
29851e0a012SSarah Sharp 	/*
29951e0a012SSarah Sharp 	 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
30051e0a012SSarah Sharp 	 * is the greater of the two exit latencies.
30151e0a012SSarah Sharp 	 */
30251e0a012SSarah Sharp 	if (first_link_pel > hub_pel)
30351e0a012SSarah Sharp 		udev_lpm_params->pel = first_link_pel;
30451e0a012SSarah Sharp 	else
30551e0a012SSarah Sharp 		udev_lpm_params->pel = hub_pel;
30651e0a012SSarah Sharp }
30751e0a012SSarah Sharp 
30851e0a012SSarah Sharp /*
30951e0a012SSarah Sharp  * Set the System Exit Latency (SEL) to indicate the total worst-case time from
31051e0a012SSarah Sharp  * when a device initiates a transition to U0, until when it will receive the
31151e0a012SSarah Sharp  * first packet from the host controller.
31251e0a012SSarah Sharp  *
31351e0a012SSarah Sharp  * Section C.1.5.1 describes the four components to this:
31451e0a012SSarah Sharp  *  - t1: device PEL
31551e0a012SSarah Sharp  *  - t2: time for the ERDY to make it from the device to the host.
31651e0a012SSarah Sharp  *  - t3: a host-specific delay to process the ERDY.
31751e0a012SSarah Sharp  *  - t4: time for the packet to make it from the host to the device.
31851e0a012SSarah Sharp  *
31951e0a012SSarah Sharp  * t3 is specific to both the xHCI host and the platform the host is integrated
32051e0a012SSarah Sharp  * into.  The Intel HW folks have said it's negligible, FIXME if a different
32151e0a012SSarah Sharp  * vendor says otherwise.
32251e0a012SSarah Sharp  */
32351e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev,
32451e0a012SSarah Sharp 		struct usb3_lpm_parameters *udev_lpm_params)
32551e0a012SSarah Sharp {
32651e0a012SSarah Sharp 	struct usb_device *parent;
32751e0a012SSarah Sharp 	unsigned int num_hubs;
32851e0a012SSarah Sharp 	unsigned int total_sel;
32951e0a012SSarah Sharp 
33051e0a012SSarah Sharp 	/* t1 = device PEL */
33151e0a012SSarah Sharp 	total_sel = udev_lpm_params->pel;
33251e0a012SSarah Sharp 	/* How many external hubs are in between the device & the root port. */
33351e0a012SSarah Sharp 	for (parent = udev->parent, num_hubs = 0; parent->parent;
33451e0a012SSarah Sharp 			parent = parent->parent)
33551e0a012SSarah Sharp 		num_hubs++;
33651e0a012SSarah Sharp 	/* t2 = 2.1us + 250ns * (num_hubs - 1) */
33751e0a012SSarah Sharp 	if (num_hubs > 0)
33851e0a012SSarah Sharp 		total_sel += 2100 + 250 * (num_hubs - 1);
33951e0a012SSarah Sharp 
34051e0a012SSarah Sharp 	/* t4 = 250ns * num_hubs */
34151e0a012SSarah Sharp 	total_sel += 250 * num_hubs;
34251e0a012SSarah Sharp 
34351e0a012SSarah Sharp 	udev_lpm_params->sel = total_sel;
34451e0a012SSarah Sharp }
34551e0a012SSarah Sharp 
34651e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev)
34751e0a012SSarah Sharp {
34851e0a012SSarah Sharp 	struct usb_hub *hub;
34951e0a012SSarah Sharp 	unsigned int port_to_port_delay;
35051e0a012SSarah Sharp 	unsigned int udev_u1_del;
35151e0a012SSarah Sharp 	unsigned int udev_u2_del;
35251e0a012SSarah Sharp 	unsigned int hub_u1_del;
35351e0a012SSarah Sharp 	unsigned int hub_u2_del;
35451e0a012SSarah Sharp 
35551e0a012SSarah Sharp 	if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
35651e0a012SSarah Sharp 		return;
35751e0a012SSarah Sharp 
35851e0a012SSarah Sharp 	hub = hdev_to_hub(udev->parent);
35951e0a012SSarah Sharp 	/* It doesn't take time to transition the roothub into U0, since it
36051e0a012SSarah Sharp 	 * doesn't have an upstream link.
36151e0a012SSarah Sharp 	 */
36251e0a012SSarah Sharp 	if (!hub)
36351e0a012SSarah Sharp 		return;
36451e0a012SSarah Sharp 
36551e0a012SSarah Sharp 	udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
36651e0a012SSarah Sharp 	udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
36751e0a012SSarah Sharp 	hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
36851e0a012SSarah Sharp 	hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
36951e0a012SSarah Sharp 
37051e0a012SSarah Sharp 	usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
37151e0a012SSarah Sharp 			hub, &udev->parent->u1_params, hub_u1_del);
37251e0a012SSarah Sharp 
37351e0a012SSarah Sharp 	usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
37451e0a012SSarah Sharp 			hub, &udev->parent->u2_params, hub_u2_del);
37551e0a012SSarah Sharp 
37651e0a012SSarah Sharp 	/*
37751e0a012SSarah Sharp 	 * Appendix C, section C.2.2.2, says that there is a slight delay from
37851e0a012SSarah Sharp 	 * when the parent hub notices the downstream port is trying to
37951e0a012SSarah Sharp 	 * transition to U0 to when the hub initiates a U0 transition on its
38051e0a012SSarah Sharp 	 * upstream port.  The section says the delays are tPort2PortU1EL and
38151e0a012SSarah Sharp 	 * tPort2PortU2EL, but it doesn't define what they are.
38251e0a012SSarah Sharp 	 *
38351e0a012SSarah Sharp 	 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
38451e0a012SSarah Sharp 	 * about the same delays.  Use the maximum delay calculations from those
38551e0a012SSarah Sharp 	 * sections.  For U1, it's tHubPort2PortExitLat, which is 1us max.  For
38651e0a012SSarah Sharp 	 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat.  I
38751e0a012SSarah Sharp 	 * assume the device exit latencies they are talking about are the hub
38851e0a012SSarah Sharp 	 * exit latencies.
38951e0a012SSarah Sharp 	 *
39051e0a012SSarah Sharp 	 * What do we do if the U2 exit latency is less than the U1 exit
39151e0a012SSarah Sharp 	 * latency?  It's possible, although not likely...
39251e0a012SSarah Sharp 	 */
39351e0a012SSarah Sharp 	port_to_port_delay = 1;
39451e0a012SSarah Sharp 
39551e0a012SSarah Sharp 	usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
39651e0a012SSarah Sharp 			hub, &udev->parent->u1_params, hub_u1_del,
39751e0a012SSarah Sharp 			port_to_port_delay);
39851e0a012SSarah Sharp 
39951e0a012SSarah Sharp 	if (hub_u2_del > hub_u1_del)
40051e0a012SSarah Sharp 		port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
40151e0a012SSarah Sharp 	else
40251e0a012SSarah Sharp 		port_to_port_delay = 1 + hub_u1_del;
40351e0a012SSarah Sharp 
40451e0a012SSarah Sharp 	usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
40551e0a012SSarah Sharp 			hub, &udev->parent->u2_params, hub_u2_del,
40651e0a012SSarah Sharp 			port_to_port_delay);
40751e0a012SSarah Sharp 
40851e0a012SSarah Sharp 	/* Now that we've got PEL, calculate SEL. */
40951e0a012SSarah Sharp 	usb_set_lpm_sel(udev, &udev->u1_params);
41051e0a012SSarah Sharp 	usb_set_lpm_sel(udev, &udev->u2_params);
41151e0a012SSarah Sharp }
412d9b2099cSSarah Sharp 
4131da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
414dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data)
4151da177e4SLinus Torvalds {
416dbe79bbeSJohn Youn 	int i, ret, size;
417dbe79bbeSJohn Youn 	unsigned dtype;
418dbe79bbeSJohn Youn 
419dbe79bbeSJohn Youn 	if (hub_is_superspeed(hdev)) {
420dbe79bbeSJohn Youn 		dtype = USB_DT_SS_HUB;
421dbe79bbeSJohn Youn 		size = USB_DT_SS_HUB_SIZE;
422dbe79bbeSJohn Youn 	} else {
423dbe79bbeSJohn Youn 		dtype = USB_DT_HUB;
424dbe79bbeSJohn Youn 		size = sizeof(struct usb_hub_descriptor);
425dbe79bbeSJohn Youn 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
4281da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
4291da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
430dbe79bbeSJohn Youn 			dtype << 8, 0, data, size,
4311da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
4321da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
4331da177e4SLinus Torvalds 			return ret;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 	return -EINVAL;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*
4391da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4441da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
4491da177e4SLinus Torvalds  */
4501da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4531da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
4541da177e4SLinus Torvalds 		NULL, 0, 1000);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4631da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
4641da177e4SLinus Torvalds 		NULL, 0, 1000);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
4681da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
4691da177e4SLinus Torvalds  * for info about using port indicators
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds static void set_port_led(
4721da177e4SLinus Torvalds 	struct usb_hub *hub,
4731da177e4SLinus Torvalds 	int port1,
4741da177e4SLinus Torvalds 	int selector
4751da177e4SLinus Torvalds )
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
4781da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
4791da177e4SLinus Torvalds 	if (status < 0)
4801da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
4811da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
4821da177e4SLinus Torvalds 			port1,
4831da177e4SLinus Torvalds 			({ char *s; switch (selector) {
4841da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
4851da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
4861da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
4871da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
4881da177e4SLinus Torvalds 			default: s = "??"; break;
4891da177e4SLinus Torvalds 			}; s; }),
4901da177e4SLinus Torvalds 			status);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
4941da177e4SLinus Torvalds 
495c4028958SDavid Howells static void led_work (struct work_struct *work)
4961da177e4SLinus Torvalds {
497c4028958SDavid Howells 	struct usb_hub		*hub =
498c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
4991da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
5001da177e4SLinus Torvalds 	unsigned		i;
5011da177e4SLinus Torvalds 	unsigned		changed = 0;
5021da177e4SLinus Torvalds 	int			cursor = -1;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
5051da177e4SLinus Torvalds 		return;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
5081da177e4SLinus Torvalds 		unsigned	selector, mode;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
5131da177e4SLinus Torvalds 		/* cycle marker */
5141da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
5151da177e4SLinus Torvalds 			cursor = i;
5161da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
5171da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
5181da177e4SLinus Torvalds 			break;
5191da177e4SLinus Torvalds 		/* blinking green = sw attention */
5201da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
5211da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
5221da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
5231da177e4SLinus Torvalds 			break;
5241da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
5251da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
5261da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
5271da177e4SLinus Torvalds 			break;
5281da177e4SLinus Torvalds 		/* blinking amber = hw attention */
5291da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
5301da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
5311da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
5321da177e4SLinus Torvalds 			break;
5331da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
5341da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
5351da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
5361da177e4SLinus Torvalds 			break;
5371da177e4SLinus Torvalds 		/* blink green/amber = reserved */
5381da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
5391da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
5401da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
5411da177e4SLinus Torvalds 			break;
5421da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
5431da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
5441da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
5451da177e4SLinus Torvalds 			break;
5461da177e4SLinus Torvalds 		default:
5471da177e4SLinus Torvalds 			continue;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
5501da177e4SLinus Torvalds 			changed = 1;
5511da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
5521da177e4SLinus Torvalds 		hub->indicator[i] = mode;
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
5551da177e4SLinus Torvalds 		cursor++;
5561da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
5571da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
5581da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
5591da177e4SLinus Torvalds 		changed++;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 	if (changed)
5621da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
5661da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
5671da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /*
5701da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
5711da177e4SLinus Torvalds  */
5721da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
5731da177e4SLinus Torvalds 		struct usb_hub_status *data)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
5761da177e4SLinus Torvalds 
5773824c1ddSLibor Pechacek 	for (i = 0; i < USB_STS_RETRIES &&
5783824c1ddSLibor Pechacek 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
5791da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
5801da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
5811da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 	return status;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /*
5871da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
5881da177e4SLinus Torvalds  */
5891da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
5901da177e4SLinus Torvalds 		struct usb_port_status *data)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
5931da177e4SLinus Torvalds 
5943824c1ddSLibor Pechacek 	for (i = 0; i < USB_STS_RETRIES &&
5953824c1ddSLibor Pechacek 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
5961da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
5971da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
5981da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 	return status;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6033eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
6043eb14915SAlan Stern 		u16 *status, u16 *change)
6053eb14915SAlan Stern {
6063eb14915SAlan Stern 	int ret;
6073eb14915SAlan Stern 
6083eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
6093eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
6103eb14915SAlan Stern 	if (ret < 4) {
6113eb14915SAlan Stern 		dev_err(hub->intfdev,
6123eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
6133eb14915SAlan Stern 		if (ret >= 0)
6143eb14915SAlan Stern 			ret = -EIO;
6153eb14915SAlan Stern 	} else {
6163eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
6173eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
618dbe79bbeSJohn Youn 
6193eb14915SAlan Stern 		ret = 0;
6203eb14915SAlan Stern 	}
6213eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
6223eb14915SAlan Stern 	return ret;
6233eb14915SAlan Stern }
6243eb14915SAlan Stern 
6251da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	unsigned long	flags;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
6302e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
6311da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
6328e4ceb38SAlan Stern 
6338e4ceb38SAlan Stern 		/* Suppress autosuspend until khubd runs */
6348e4ceb38SAlan Stern 		usb_autopm_get_interface_no_resume(
6358e4ceb38SAlan Stern 				to_usb_interface(hub->intfdev));
6361da177e4SLinus Torvalds 		wake_up(&khubd_wait);
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
6421da177e4SLinus Torvalds {
64325118084SAlan Stern 	struct usb_hub *hub = hdev_to_hub(hdev);
64425118084SAlan Stern 
64525118084SAlan Stern 	if (hub)
64625118084SAlan Stern 		kick_khubd(hub);
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6494ee823b8SSarah Sharp /*
6504ee823b8SSarah Sharp  * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
6514ee823b8SSarah Sharp  * Notification, which indicates it had initiated remote wakeup.
6524ee823b8SSarah Sharp  *
6534ee823b8SSarah Sharp  * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
6544ee823b8SSarah Sharp  * device initiates resume, so the USB core will not receive notice of the
6554ee823b8SSarah Sharp  * resume through the normal hub interrupt URB.
6564ee823b8SSarah Sharp  */
6574ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev,
6584ee823b8SSarah Sharp 		unsigned int portnum)
6594ee823b8SSarah Sharp {
6604ee823b8SSarah Sharp 	struct usb_hub *hub;
6614ee823b8SSarah Sharp 
6624ee823b8SSarah Sharp 	if (!hdev)
6634ee823b8SSarah Sharp 		return;
6644ee823b8SSarah Sharp 
6654ee823b8SSarah Sharp 	hub = hdev_to_hub(hdev);
6664ee823b8SSarah Sharp 	if (hub) {
6674ee823b8SSarah Sharp 		set_bit(portnum, hub->wakeup_bits);
6684ee823b8SSarah Sharp 		kick_khubd(hub);
6694ee823b8SSarah Sharp 	}
6704ee823b8SSarah Sharp }
6714ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
6747d12e780SDavid Howells static void hub_irq(struct urb *urb)
6751da177e4SLinus Torvalds {
676ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
677e015268dSAlan Stern 	int status = urb->status;
67871d2718fSRoel Kluin 	unsigned i;
6791da177e4SLinus Torvalds 	unsigned long bits;
6801da177e4SLinus Torvalds 
681e015268dSAlan Stern 	switch (status) {
6821da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
6831da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
6841da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
6851da177e4SLinus Torvalds 		return;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	default:		/* presumably an error */
6881da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
689e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
6901da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
6911da177e4SLinus Torvalds 			goto resubmit;
692e015268dSAlan Stern 		hub->error = status;
6931da177e4SLinus Torvalds 		/* FALL THROUGH */
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	/* let khubd handle things */
6961da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
6971da177e4SLinus Torvalds 		bits = 0;
6981da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
6991da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
7001da177e4SLinus Torvalds 					<< (i*8);
7011da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
7021da177e4SLinus Torvalds 		break;
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	hub->nerrors = 0;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
7081da177e4SLinus Torvalds 	kick_khubd(hub);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds resubmit:
7111da177e4SLinus Torvalds 	if (hub->quiescing)
7121da177e4SLinus Torvalds 		return;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
7151da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
7161da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
7201da177e4SLinus Torvalds static inline int
7211da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
7221da177e4SLinus Torvalds {
723c2f6595fSAlan Stern 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
7241da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
7251da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds /*
7291da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
7301da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
7311da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
7321da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
7331da177e4SLinus Torvalds  */
734cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
7351da177e4SLinus Torvalds {
736c4028958SDavid Howells 	struct usb_hub		*hub =
737cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
7381da177e4SLinus Torvalds 	unsigned long		flags;
73955e5fdfaSMark Lord 	int			limit = 100;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
74255e5fdfaSMark Lord 	while (--limit && !list_empty (&hub->tt.clear_list)) {
743d0f830d3SH Hartley Sweeten 		struct list_head	*next;
7441da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
7451da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
746cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
7471da177e4SLinus Torvalds 		int			status;
7481da177e4SLinus Torvalds 
749d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
750d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
7511da177e4SLinus Torvalds 		list_del (&clear->clear_list);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
7541da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
7551da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
7561da177e4SLinus Torvalds 		if (status)
7571da177e4SLinus Torvalds 			dev_err (&hdev->dev,
7581da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
7591da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
760cb88a1b8SAlan Stern 
761cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
762cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
763cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
764cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
765cb88a1b8SAlan Stern 
7661da177e4SLinus Torvalds 		kfree(clear);
767cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /**
773cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
774cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
7751da177e4SLinus Torvalds  *
7761da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
7771da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
7781da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
7791da177e4SLinus Torvalds  * interrupt context.
7801da177e4SLinus Torvalds  *
7811da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
7821da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
7831da177e4SLinus Torvalds  */
784cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
7851da177e4SLinus Torvalds {
786cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
787cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
7881da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
7891da177e4SLinus Torvalds 	unsigned long		flags;
7901da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
7931da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
7941da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
7951da177e4SLinus Torvalds 	 */
79654e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
7971da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
7981da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
799cb88a1b8SAlan Stern 		return -ENOMEM;
8001da177e4SLinus Torvalds 	}
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
8031da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
8041da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
8051da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
8061da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
8071da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
8081da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
8091da177e4SLinus Torvalds 	if (usb_pipein (pipe))
8101da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
8111da177e4SLinus Torvalds 
812cb88a1b8SAlan Stern 	/* info for completion callback */
813cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
814cb88a1b8SAlan Stern 	clear->ep = urb->ep;
815cb88a1b8SAlan Stern 
8161da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
8171da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
8181da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
819cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
8201da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
821cb88a1b8SAlan Stern 	return 0;
8221da177e4SLinus Torvalds }
823cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
8241da177e4SLinus Torvalds 
8258520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
8268520f380SAlan Stern  * needs to delay.
8278520f380SAlan Stern  */
8288520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
8291da177e4SLinus Torvalds {
8301da177e4SLinus Torvalds 	int port1;
831b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
8328520f380SAlan Stern 	unsigned delay;
8334489a571SAlan Stern 	u16 wHubCharacteristics =
8344489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
8351da177e4SLinus Torvalds 
8364489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
8374489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
8384489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
8394489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
8404489a571SAlan Stern 	 * unless we send these messages to the hub.
8414489a571SAlan Stern 	 */
8424489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
8431da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
8444489a571SAlan Stern 	else
8454489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
8464489a571SAlan Stern 				"non-switchable hub\n");
8471da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
8484489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
8491da177e4SLinus Torvalds 
850b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
8518520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
8528520f380SAlan Stern 	if (do_delay)
8538520f380SAlan Stern 		msleep(delay);
8548520f380SAlan Stern 	return delay;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
8581da177e4SLinus Torvalds 		u16 *status, u16 *change)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	int ret;
8611da177e4SLinus Torvalds 
862db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
8631da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
8641da177e4SLinus Torvalds 	if (ret < 0)
8651da177e4SLinus Torvalds 		dev_err (hub->intfdev,
866441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
8671da177e4SLinus Torvalds 	else {
8681da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
8691da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
8701da177e4SLinus Torvalds 		ret = 0;
8711da177e4SLinus Torvalds 	}
872db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
8731da177e4SLinus Torvalds 	return ret;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8768b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
8778b28c752SAlan Stern {
8788b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
8790458d5b4SAlan Stern 	int ret = 0;
8808b28c752SAlan Stern 
881ff823c79SLan Tianyu 	if (hub->ports[port1 - 1]->child && set_state)
882ff823c79SLan Tianyu 		usb_set_device_state(hub->ports[port1 - 1]->child,
8838b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
884dbe79bbeSJohn Youn 	if (!hub->error && !hub_is_superspeed(hub->hdev))
8858b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
8868b28c752SAlan Stern 	if (ret)
8878b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
8888b28c752SAlan Stern 				port1, ret);
8898b28c752SAlan Stern 	return ret;
8908b28c752SAlan Stern }
8918b28c752SAlan Stern 
8920458d5b4SAlan Stern /*
8936d42fcdbSJustin P. Mattock  * Disable a port and mark a logical connect-change event, so that some
8940458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
8950458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
8960458d5b4SAlan Stern  */
8970458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
8987d069b7dSAlan Stern {
8990458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
9000458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
9010458d5b4SAlan Stern 
9020458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
9030458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
9040458d5b4SAlan Stern 	 *  - SRP saves power that way
9050458d5b4SAlan Stern 	 *  - ... new call, TBD ...
9060458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
9070458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
9080458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
9090458d5b4SAlan Stern 	 */
9100458d5b4SAlan Stern 
9110458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
9120458d5b4SAlan Stern  	kick_khubd(hub);
9130458d5b4SAlan Stern }
9140458d5b4SAlan Stern 
915253e0572SAlan Stern /**
916253e0572SAlan Stern  * usb_remove_device - disable a device's port on its parent hub
917253e0572SAlan Stern  * @udev: device to be disabled and removed
918253e0572SAlan Stern  * Context: @udev locked, must be able to sleep.
919253e0572SAlan Stern  *
920253e0572SAlan Stern  * After @udev's port has been disabled, khubd is notified and it will
921253e0572SAlan Stern  * see that the device has been disconnected.  When the device is
922253e0572SAlan Stern  * physically unplugged and something is plugged in, the events will
923253e0572SAlan Stern  * be received and processed normally.
924253e0572SAlan Stern  */
925253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
926253e0572SAlan Stern {
927253e0572SAlan Stern 	struct usb_hub *hub;
928253e0572SAlan Stern 	struct usb_interface *intf;
929253e0572SAlan Stern 
930253e0572SAlan Stern 	if (!udev->parent)	/* Can't remove a root hub */
931253e0572SAlan Stern 		return -EINVAL;
932253e0572SAlan Stern 	hub = hdev_to_hub(udev->parent);
933253e0572SAlan Stern 	intf = to_usb_interface(hub->intfdev);
934253e0572SAlan Stern 
935253e0572SAlan Stern 	usb_autopm_get_interface(intf);
936253e0572SAlan Stern 	set_bit(udev->portnum, hub->removed_bits);
937253e0572SAlan Stern 	hub_port_logical_disconnect(hub, udev->portnum);
938253e0572SAlan Stern 	usb_autopm_put_interface(intf);
939253e0572SAlan Stern 	return 0;
940253e0572SAlan Stern }
941253e0572SAlan Stern 
9426ee0b270SAlan Stern enum hub_activation_type {
9438e4ceb38SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
9448520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
9456ee0b270SAlan Stern };
9465e6effaeSAlan Stern 
9478520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
9488520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
9498520f380SAlan Stern 
950f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
9515e6effaeSAlan Stern {
9525e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
953653a39d1SSarah Sharp 	struct usb_hcd *hcd;
954653a39d1SSarah Sharp 	int ret;
9555e6effaeSAlan Stern 	int port1;
956f2835219SAlan Stern 	int status;
957948fea37SAlan Stern 	bool need_debounce_delay = false;
9588520f380SAlan Stern 	unsigned delay;
9598520f380SAlan Stern 
9608520f380SAlan Stern 	/* Continue a partial initialization */
9618520f380SAlan Stern 	if (type == HUB_INIT2)
9628520f380SAlan Stern 		goto init2;
9638520f380SAlan Stern 	if (type == HUB_INIT3)
9648520f380SAlan Stern 		goto init3;
9655e6effaeSAlan Stern 
966a45aa3b3SElric Fu 	/* The superspeed hub except for root hub has to use Hub Depth
967a45aa3b3SElric Fu 	 * value as an offset into the route string to locate the bits
968a45aa3b3SElric Fu 	 * it uses to determine the downstream port number. So hub driver
969a45aa3b3SElric Fu 	 * should send a set hub depth request to superspeed hub after
970a45aa3b3SElric Fu 	 * the superspeed hub is set configuration in initialization or
971a45aa3b3SElric Fu 	 * reset procedure.
972a45aa3b3SElric Fu 	 *
973a45aa3b3SElric Fu 	 * After a resume, port power should still be on.
974f2835219SAlan Stern 	 * For any other type of activation, turn it on.
975f2835219SAlan Stern 	 */
9768520f380SAlan Stern 	if (type != HUB_RESUME) {
977a45aa3b3SElric Fu 		if (hdev->parent && hub_is_superspeed(hdev)) {
978a45aa3b3SElric Fu 			ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
979a45aa3b3SElric Fu 					HUB_SET_DEPTH, USB_RT_HUB,
980a45aa3b3SElric Fu 					hdev->level - 1, 0, NULL, 0,
981a45aa3b3SElric Fu 					USB_CTRL_SET_TIMEOUT);
982a45aa3b3SElric Fu 			if (ret < 0)
983a45aa3b3SElric Fu 				dev_err(hub->intfdev,
984a45aa3b3SElric Fu 						"set hub depth failed\n");
985a45aa3b3SElric Fu 		}
9868520f380SAlan Stern 
9878520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
9888520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
9898520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
9908520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
9918520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
9928520f380SAlan Stern 		 * rather than as a module).  It adds up.
9938520f380SAlan Stern 		 *
9948520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
9958520f380SAlan Stern 		 * because for those activation types the ports have to be
9968520f380SAlan Stern 		 * operational when we return.  In theory this could be done
9978520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
9988520f380SAlan Stern 		 */
9998520f380SAlan Stern 		if (type == HUB_INIT) {
10008520f380SAlan Stern 			delay = hub_power_on(hub, false);
10018520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
10028520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
10038520f380SAlan Stern 					msecs_to_jiffies(delay));
100461fbeba1SAlan Stern 
100561fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
10068e4ceb38SAlan Stern 			usb_autopm_get_interface_no_resume(
10078e4ceb38SAlan Stern 					to_usb_interface(hub->intfdev));
10088520f380SAlan Stern 			return;		/* Continues at init2: below */
1009653a39d1SSarah Sharp 		} else if (type == HUB_RESET_RESUME) {
1010653a39d1SSarah Sharp 			/* The internal host controller state for the hub device
1011653a39d1SSarah Sharp 			 * may be gone after a host power loss on system resume.
1012653a39d1SSarah Sharp 			 * Update the device's info so the HW knows it's a hub.
1013653a39d1SSarah Sharp 			 */
1014653a39d1SSarah Sharp 			hcd = bus_to_hcd(hdev->bus);
1015653a39d1SSarah Sharp 			if (hcd->driver->update_hub_device) {
1016653a39d1SSarah Sharp 				ret = hcd->driver->update_hub_device(hcd, hdev,
1017653a39d1SSarah Sharp 						&hub->tt, GFP_NOIO);
1018653a39d1SSarah Sharp 				if (ret < 0) {
1019653a39d1SSarah Sharp 					dev_err(hub->intfdev, "Host not "
1020653a39d1SSarah Sharp 							"accepting hub info "
1021653a39d1SSarah Sharp 							"update.\n");
1022653a39d1SSarah Sharp 					dev_err(hub->intfdev, "LS/FS devices "
1023653a39d1SSarah Sharp 							"and hubs may not work "
1024653a39d1SSarah Sharp 							"under this hub\n.");
1025653a39d1SSarah Sharp 				}
1026653a39d1SSarah Sharp 			}
1027653a39d1SSarah Sharp 			hub_power_on(hub, true);
10288520f380SAlan Stern 		} else {
10298520f380SAlan Stern 			hub_power_on(hub, true);
10308520f380SAlan Stern 		}
10318520f380SAlan Stern 	}
10328520f380SAlan Stern  init2:
1033f2835219SAlan Stern 
10346ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
10356ee0b270SAlan Stern 	 * which ports need attention.
10365e6effaeSAlan Stern 	 */
10375e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1038ff823c79SLan Tianyu 		struct usb_device *udev = hub->ports[port1 - 1]->child;
10395e6effaeSAlan Stern 		u16 portstatus, portchange;
10405e6effaeSAlan Stern 
10416ee0b270SAlan Stern 		portstatus = portchange = 0;
10426ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
10436ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
10446ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
10456ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
10466ee0b270SAlan Stern 					port1, portstatus, portchange);
10475e6effaeSAlan Stern 
10486ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
10496ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
10506ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
10516ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
10525e6effaeSAlan Stern 		 */
10536ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
10546ee0b270SAlan Stern 				type != HUB_RESUME ||
10556ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
10566ee0b270SAlan Stern 				!udev ||
10576ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
10589f0a6cd3SAndiry Xu 			/*
10599f0a6cd3SAndiry Xu 			 * USB3 protocol ports will automatically transition
10609f0a6cd3SAndiry Xu 			 * to Enabled state when detect an USB3.0 device attach.
10619f0a6cd3SAndiry Xu 			 * Do not disable USB3 protocol ports.
10629f0a6cd3SAndiry Xu 			 */
1063131dec34SSarah Sharp 			if (!hub_is_superspeed(hdev)) {
10649f0a6cd3SAndiry Xu 				clear_port_feature(hdev, port1,
10659f0a6cd3SAndiry Xu 						   USB_PORT_FEAT_ENABLE);
10666ee0b270SAlan Stern 				portstatus &= ~USB_PORT_STAT_ENABLE;
106785f0ff46SSarah Sharp 			} else {
106885f0ff46SSarah Sharp 				/* Pretend that power was lost for USB3 devs */
106985f0ff46SSarah Sharp 				portstatus &= ~USB_PORT_STAT_ENABLE;
10706ee0b270SAlan Stern 			}
10719f0a6cd3SAndiry Xu 		}
10726ee0b270SAlan Stern 
1073948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
1074948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
1075948fea37SAlan Stern 			need_debounce_delay = true;
1076948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
1077948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
1078948fea37SAlan Stern 		}
1079948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
1080948fea37SAlan Stern 			need_debounce_delay = true;
1081948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
1082948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
1083948fea37SAlan Stern 		}
108479c3dd81SDon Zickus 		if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
108579c3dd81SDon Zickus 				hub_is_superspeed(hub->hdev)) {
108679c3dd81SDon Zickus 			need_debounce_delay = true;
108779c3dd81SDon Zickus 			clear_port_feature(hub->hdev, port1,
108879c3dd81SDon Zickus 					USB_PORT_FEAT_C_BH_PORT_RESET);
108979c3dd81SDon Zickus 		}
1090253e0572SAlan Stern 		/* We can forget about a "removed" device when there's a
1091253e0572SAlan Stern 		 * physical disconnect or the connect status changes.
1092253e0572SAlan Stern 		 */
1093253e0572SAlan Stern 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1094253e0572SAlan Stern 				(portchange & USB_PORT_STAT_C_CONNECTION))
1095253e0572SAlan Stern 			clear_bit(port1, hub->removed_bits);
1096253e0572SAlan Stern 
10976ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
10986ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
10996ee0b270SAlan Stern 			 * check for a new connection
11006ee0b270SAlan Stern 			 */
11016ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
11026ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
11036ee0b270SAlan Stern 
11046ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
110572937e1eSSarah Sharp 			bool port_resumed = (portstatus &
110672937e1eSSarah Sharp 					USB_PORT_STAT_LINK_STATE) ==
110772937e1eSSarah Sharp 				USB_SS_PORT_LS_U0;
11086ee0b270SAlan Stern 			/* The power session apparently survived the resume.
11096ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
11106ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
111172937e1eSSarah Sharp 			 * take care of it.  Look at the port link state
111272937e1eSSarah Sharp 			 * for USB 3.0 hubs, since they don't have a suspend
111372937e1eSSarah Sharp 			 * change bit, and they don't set the port link change
111472937e1eSSarah Sharp 			 * bit on device-initiated resume.
11156ee0b270SAlan Stern 			 */
111672937e1eSSarah Sharp 			if (portchange || (hub_is_superspeed(hub->hdev) &&
111772937e1eSSarah Sharp 						port_resumed))
11186ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
11196ee0b270SAlan Stern 
11206ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
11216ee0b270SAlan Stern #ifdef CONFIG_PM
11225e6effaeSAlan Stern 			udev->reset_resume = 1;
11236ee0b270SAlan Stern #endif
11248808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
11258808f00cSAlan Stern 
11266ee0b270SAlan Stern 		} else {
11276ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
11286ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
11296ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
11305e6effaeSAlan Stern 		}
11315e6effaeSAlan Stern 	}
11325e6effaeSAlan Stern 
1133948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
1134948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
1135948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
1136948fea37SAlan Stern 	 * one at a time later on.
1137948fea37SAlan Stern 	 *
1138948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
1139948fea37SAlan Stern 	 * will see them later and handle them normally.
1140948fea37SAlan Stern 	 */
11418520f380SAlan Stern 	if (need_debounce_delay) {
11428520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
1143f2835219SAlan Stern 
11448520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
11458520f380SAlan Stern 		if (type == HUB_INIT2) {
11468520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
11478520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
11488520f380SAlan Stern 					msecs_to_jiffies(delay));
11498520f380SAlan Stern 			return;		/* Continues at init3: below */
11508520f380SAlan Stern 		} else {
11518520f380SAlan Stern 			msleep(delay);
11528520f380SAlan Stern 		}
11538520f380SAlan Stern 	}
11548520f380SAlan Stern  init3:
1155f2835219SAlan Stern 	hub->quiescing = 0;
1156f2835219SAlan Stern 
1157f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
1158f2835219SAlan Stern 	if (status < 0)
1159f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
1160f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
1161f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1162f2835219SAlan Stern 
1163f2835219SAlan Stern 	/* Scan all ports that need attention */
1164f2835219SAlan Stern 	kick_khubd(hub);
11658e4ceb38SAlan Stern 
11668e4ceb38SAlan Stern 	/* Allow autosuspend if it was suppressed */
11678e4ceb38SAlan Stern 	if (type <= HUB_INIT3)
11688e4ceb38SAlan Stern 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
11695e6effaeSAlan Stern }
11705e6effaeSAlan Stern 
11718520f380SAlan Stern /* Implement the continuations for the delays above */
11728520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
11738520f380SAlan Stern {
11748520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
11758520f380SAlan Stern 
11768520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
11778520f380SAlan Stern }
11788520f380SAlan Stern 
11798520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
11808520f380SAlan Stern {
11818520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
11828520f380SAlan Stern 
11838520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
11848520f380SAlan Stern }
11858520f380SAlan Stern 
11864330354fSAlan Stern enum hub_quiescing_type {
11874330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
11884330354fSAlan Stern };
11894330354fSAlan Stern 
11904330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
11914330354fSAlan Stern {
11924330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
11934330354fSAlan Stern 	int i;
11944330354fSAlan Stern 
11958520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
11968520f380SAlan Stern 
11974330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
11984330354fSAlan Stern 	hub->quiescing = 1;
11994330354fSAlan Stern 
12004330354fSAlan Stern 	if (type != HUB_SUSPEND) {
12014330354fSAlan Stern 		/* Disconnect all the children */
12024330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
1203ff823c79SLan Tianyu 			if (hub->ports[i]->child)
1204ff823c79SLan Tianyu 				usb_disconnect(&hub->ports[i]->child);
12054330354fSAlan Stern 		}
12064330354fSAlan Stern 	}
12074330354fSAlan Stern 
12084330354fSAlan Stern 	/* Stop khubd and related activity */
12094330354fSAlan Stern 	usb_kill_urb(hub->urb);
12104330354fSAlan Stern 	if (hub->has_indicators)
12114330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
12124330354fSAlan Stern 	if (hub->tt.hub)
1213cb88a1b8SAlan Stern 		cancel_work_sync(&hub->tt.clear_work);
12144330354fSAlan Stern }
12154330354fSAlan Stern 
12163eb14915SAlan Stern /* caller has locked the hub device */
12173eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
12183eb14915SAlan Stern {
12193eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
12203eb14915SAlan Stern 
12214330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
1222f07600cfSAlan Stern 	return 0;
12237d069b7dSAlan Stern }
12247d069b7dSAlan Stern 
12257d069b7dSAlan Stern /* caller has locked the hub device */
1226f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
12277d069b7dSAlan Stern {
12287de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
12297de18d8bSAlan Stern 
1230f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
1231f07600cfSAlan Stern 	return 0;
12327d069b7dSAlan Stern }
12337d069b7dSAlan Stern 
1234fa2a9566SLan Tianyu static void usb_port_device_release(struct device *dev)
1235fa2a9566SLan Tianyu {
1236fa2a9566SLan Tianyu 	struct usb_port *port_dev = to_usb_port(dev);
1237fa2a9566SLan Tianyu 
1238fa2a9566SLan Tianyu 	kfree(port_dev);
1239fa2a9566SLan Tianyu }
1240fa2a9566SLan Tianyu 
1241fa2a9566SLan Tianyu static void usb_hub_remove_port_device(struct usb_hub *hub,
1242fa2a9566SLan Tianyu 				       int port1)
1243fa2a9566SLan Tianyu {
1244fa2a9566SLan Tianyu 	device_unregister(&hub->ports[port1 - 1]->dev);
1245fa2a9566SLan Tianyu }
1246fa2a9566SLan Tianyu 
1247fa2a9566SLan Tianyu struct device_type usb_port_device_type = {
1248fa2a9566SLan Tianyu 	.name =		"usb_port",
1249fa2a9566SLan Tianyu 	.release =	usb_port_device_release,
1250fa2a9566SLan Tianyu };
1251fa2a9566SLan Tianyu 
1252fa2a9566SLan Tianyu static int usb_hub_create_port_device(struct usb_hub *hub,
1253fa2a9566SLan Tianyu 				      int port1)
1254fa2a9566SLan Tianyu {
1255fa2a9566SLan Tianyu 	struct usb_port *port_dev = NULL;
1256fa2a9566SLan Tianyu 	int retval;
1257fa2a9566SLan Tianyu 
1258fa2a9566SLan Tianyu 	port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1259fa2a9566SLan Tianyu 	if (!port_dev) {
1260fa2a9566SLan Tianyu 		retval = -ENOMEM;
1261fa2a9566SLan Tianyu 		goto exit;
1262fa2a9566SLan Tianyu 	}
1263fa2a9566SLan Tianyu 
1264fa2a9566SLan Tianyu 	hub->ports[port1 - 1] = port_dev;
1265fa2a9566SLan Tianyu 	port_dev->dev.parent = hub->intfdev;
1266fa2a9566SLan Tianyu 	port_dev->dev.type = &usb_port_device_type;
1267fa2a9566SLan Tianyu 	dev_set_name(&port_dev->dev, "port%d", port1);
1268fa2a9566SLan Tianyu 
1269fa2a9566SLan Tianyu 	retval = device_register(&port_dev->dev);
1270fa2a9566SLan Tianyu 	if (retval)
1271fa2a9566SLan Tianyu 		goto error_register;
1272fa2a9566SLan Tianyu 	return 0;
1273fa2a9566SLan Tianyu 
1274fa2a9566SLan Tianyu error_register:
1275fa2a9566SLan Tianyu 	put_device(&port_dev->dev);
1276fa2a9566SLan Tianyu exit:
1277fa2a9566SLan Tianyu 	return retval;
1278fa2a9566SLan Tianyu }
1279fa2a9566SLan Tianyu 
12801da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
12811da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
12821da177e4SLinus Torvalds {
1283b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
12841da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
12851da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
12861da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
128774ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
12881da177e4SLinus Torvalds 	unsigned int pipe;
1289fa2a9566SLan Tianyu 	int maxp, ret, i;
12907cbe5dcaSAlan Stern 	char *message = "out of memory";
12911da177e4SLinus Torvalds 
1292d697cddaSAlan Stern 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
12931da177e4SLinus Torvalds 	if (!hub->buffer) {
12941da177e4SLinus Torvalds 		ret = -ENOMEM;
12951da177e4SLinus Torvalds 		goto fail;
12961da177e4SLinus Torvalds 	}
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
12991da177e4SLinus Torvalds 	if (!hub->status) {
13001da177e4SLinus Torvalds 		ret = -ENOMEM;
13011da177e4SLinus Torvalds 		goto fail;
13021da177e4SLinus Torvalds 	}
1303db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
13061da177e4SLinus Torvalds 	if (!hub->descriptor) {
13071da177e4SLinus Torvalds 		ret = -ENOMEM;
13081da177e4SLinus Torvalds 		goto fail;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
13121da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
13131da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
13141da177e4SLinus Torvalds 	 */
1315dbe79bbeSJohn Youn 	ret = get_hub_descriptor(hdev, hub->descriptor);
13161da177e4SLinus Torvalds 	if (ret < 0) {
13171da177e4SLinus Torvalds 		message = "can't read hub descriptor";
13181da177e4SLinus Torvalds 		goto fail;
13191da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
13201da177e4SLinus Torvalds 		message = "hub has too many ports!";
13211da177e4SLinus Torvalds 		ret = -ENODEV;
13221da177e4SLinus Torvalds 		goto fail;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
13261da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
13271da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
13281da177e4SLinus Torvalds 
1329fa2a9566SLan Tianyu 	hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1330336c5c31SLan Tianyu 			     GFP_KERNEL);
1331ff823c79SLan Tianyu 	if (!hub->ports) {
13327cbe5dcaSAlan Stern 		ret = -ENOMEM;
13337cbe5dcaSAlan Stern 		goto fail;
13347cbe5dcaSAlan Stern 	}
13357cbe5dcaSAlan Stern 
133674ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
13371da177e4SLinus Torvalds 
1338dbe79bbeSJohn Youn 	/* FIXME for USB 3.0, skip for now */
1339dbe79bbeSJohn Youn 	if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1340dbe79bbeSJohn Youn 			!(hub_is_superspeed(hdev))) {
13411da177e4SLinus Torvalds 		int	i;
13421da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
1345dbe79bbeSJohn Youn 			portstr[i] = hub->descriptor->u.hs.DeviceRemovable
13461da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
13471da177e4SLinus Torvalds 				? 'F' : 'R';
13481da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
13491da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
13501da177e4SLinus Torvalds 	} else
13511da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
13521da177e4SLinus Torvalds 
135374ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
13547bf01185SAman Deep 	case HUB_CHAR_COMMON_LPSM:
13551da177e4SLinus Torvalds 		dev_dbg(hub_dev, "ganged power switching\n");
13561da177e4SLinus Torvalds 		break;
13577bf01185SAman Deep 	case HUB_CHAR_INDV_PORT_LPSM:
13581da177e4SLinus Torvalds 		dev_dbg(hub_dev, "individual port power switching\n");
13591da177e4SLinus Torvalds 		break;
13607bf01185SAman Deep 	case HUB_CHAR_NO_LPSM:
13617bf01185SAman Deep 	case HUB_CHAR_LPSM:
13621da177e4SLinus Torvalds 		dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
13631da177e4SLinus Torvalds 		break;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
136674ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
13677bf01185SAman Deep 	case HUB_CHAR_COMMON_OCPM:
13681da177e4SLinus Torvalds 		dev_dbg(hub_dev, "global over-current protection\n");
13691da177e4SLinus Torvalds 		break;
13707bf01185SAman Deep 	case HUB_CHAR_INDV_PORT_OCPM:
13711da177e4SLinus Torvalds 		dev_dbg(hub_dev, "individual port over-current protection\n");
13721da177e4SLinus Torvalds 		break;
13737bf01185SAman Deep 	case HUB_CHAR_NO_OCPM:
13747bf01185SAman Deep 	case HUB_CHAR_OCPM:
13751da177e4SLinus Torvalds 		dev_dbg(hub_dev, "no over-current protection\n");
13761da177e4SLinus Torvalds 		break;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
13801da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
1381cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
13821da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
13837bf01185SAman Deep 	case USB_HUB_PR_FS:
13841da177e4SLinus Torvalds 		break;
13857bf01185SAman Deep 	case USB_HUB_PR_HS_SINGLE_TT:
13861da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Single TT\n");
13871da177e4SLinus Torvalds 		hub->tt.hub = hdev;
13881da177e4SLinus Torvalds 		break;
13897bf01185SAman Deep 	case USB_HUB_PR_HS_MULTI_TT:
13901da177e4SLinus Torvalds 		ret = usb_set_interface(hdev, 0, 1);
13911da177e4SLinus Torvalds 		if (ret == 0) {
13921da177e4SLinus Torvalds 			dev_dbg(hub_dev, "TT per port\n");
13931da177e4SLinus Torvalds 			hub->tt.multi = 1;
13941da177e4SLinus Torvalds 		} else
13951da177e4SLinus Torvalds 			dev_err(hub_dev, "Using single TT (err %d)\n",
13961da177e4SLinus Torvalds 				ret);
13971da177e4SLinus Torvalds 		hub->tt.hub = hdev;
13981da177e4SLinus Torvalds 		break;
13997bf01185SAman Deep 	case USB_HUB_PR_SS:
1400d2e9b4d6SSarah Sharp 		/* USB 3.0 hubs don't have a TT */
1401d2e9b4d6SSarah Sharp 		break;
14021da177e4SLinus Torvalds 	default:
14031da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
14041da177e4SLinus Torvalds 			hdev->descriptor.bDeviceProtocol);
14051da177e4SLinus Torvalds 		break;
14061da177e4SLinus Torvalds 	}
14071da177e4SLinus Torvalds 
1408e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
140974ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1410e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
1411e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
1412e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
1413e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
1414e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
1415e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
1416e09711aeSdavid-b@pacbell.net 			}
14171da177e4SLinus Torvalds 			break;
1418e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
1419e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
1420e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1421e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1422e09711aeSdavid-b@pacbell.net 				16, hub->tt.think_time);
14231da177e4SLinus Torvalds 			break;
1424e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
1425e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
1426e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1427e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1428e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
14291da177e4SLinus Torvalds 			break;
1430e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
1431e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
1432e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1433e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1434e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
14351da177e4SLinus Torvalds 			break;
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
143974ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
14401da177e4SLinus Torvalds 		hub->has_indicators = 1;
14411da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
14451da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
14481da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
14491da177e4SLinus Torvalds 	 */
14501da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
145155c52718SAlan Stern 	if (ret < 2) {
14521da177e4SLinus Torvalds 		message = "can't get hub status";
14531da177e4SLinus Torvalds 		goto fail;
14541da177e4SLinus Torvalds 	}
14557d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
14567d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
145755c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
145855c52718SAlan Stern 			hub->mA_per_port = 500;
145955c52718SAlan Stern 		else {
146055c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
146155c52718SAlan Stern 			hub->limited_power = 1;
146255c52718SAlan Stern 		}
14637d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
14641da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
14651da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
146655c52718SAlan Stern 		hub->limited_power = 1;
146755c52718SAlan Stern 		if (hdev->maxchild > 0) {
146855c52718SAlan Stern 			int remaining = hdev->bus_mA -
146955c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
14701da177e4SLinus Torvalds 
147155c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
147255c52718SAlan Stern 				dev_warn(hub_dev,
147355c52718SAlan Stern 					"insufficient power available "
147455c52718SAlan Stern 					"to use all downstream ports\n");
147555c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
147655c52718SAlan Stern 		}
147755c52718SAlan Stern 	} else {	/* Self-powered external hub */
147855c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
147955c52718SAlan Stern 		 * provide less current per port? */
148055c52718SAlan Stern 		hub->mA_per_port = 500;
148155c52718SAlan Stern 	}
148255c52718SAlan Stern 	if (hub->mA_per_port < 500)
148355c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
148455c52718SAlan Stern 				hub->mA_per_port);
14851da177e4SLinus Torvalds 
1486b356b7c7SSarah Sharp 	/* Update the HCD's internal representation of this hub before khubd
1487b356b7c7SSarah Sharp 	 * starts getting port status changes for devices under the hub.
1488b356b7c7SSarah Sharp 	 */
1489b356b7c7SSarah Sharp 	hcd = bus_to_hcd(hdev->bus);
1490b356b7c7SSarah Sharp 	if (hcd->driver->update_hub_device) {
1491b356b7c7SSarah Sharp 		ret = hcd->driver->update_hub_device(hcd, hdev,
1492b356b7c7SSarah Sharp 				&hub->tt, GFP_KERNEL);
1493b356b7c7SSarah Sharp 		if (ret < 0) {
1494b356b7c7SSarah Sharp 			message = "can't update HCD hub info";
1495b356b7c7SSarah Sharp 			goto fail;
1496b356b7c7SSarah Sharp 		}
1497b356b7c7SSarah Sharp 	}
1498b356b7c7SSarah Sharp 
14991da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
15001da177e4SLinus Torvalds 	if (ret < 0) {
15011da177e4SLinus Torvalds 		message = "can't get hub status";
15021da177e4SLinus Torvalds 		goto fail;
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
15061da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
15071da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
15081da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
15091da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
15101da177e4SLinus Torvalds 
151174ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
15121da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
15131da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
15141da177e4SLinus Torvalds 
151588fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
151688fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
151788fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
151888fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
151988fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
152088fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
15211da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
15221da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
15251da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
15281da177e4SLinus Torvalds 	if (!hub->urb) {
15291da177e4SLinus Torvalds 		ret = -ENOMEM;
15301da177e4SLinus Torvalds 		goto fail;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
15341da177e4SLinus Torvalds 		hub, endpoint->bInterval);
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
15371da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
15381da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
15391da177e4SLinus Torvalds 
1540fa2a9566SLan Tianyu 	for (i = 0; i < hdev->maxchild; i++)
1541fa2a9566SLan Tianyu 		if (usb_hub_create_port_device(hub, i + 1) < 0)
1542fa2a9566SLan Tianyu 			dev_err(hub->intfdev,
1543fa2a9566SLan Tianyu 				"couldn't create port%d device.\n", i + 1);
1544fa2a9566SLan Tianyu 
1545f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
15461da177e4SLinus Torvalds 	return 0;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds fail:
15491da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
15501da177e4SLinus Torvalds 			message, ret);
15511da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
15521da177e4SLinus Torvalds 	return ret;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
1555e8054854SAlan Stern static void hub_release(struct kref *kref)
1556e8054854SAlan Stern {
1557e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1558e8054854SAlan Stern 
1559e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1560e8054854SAlan Stern 	kfree(hub);
1561e8054854SAlan Stern }
1562e8054854SAlan Stern 
15631da177e4SLinus Torvalds static unsigned highspeed_hubs;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
1568fa286188SGreg Kroah-Hartman 	struct usb_device *hdev = interface_to_usbdev(intf);
1569fa2a9566SLan Tianyu 	int i;
1570fa2a9566SLan Tianyu 
1571fa2a9566SLan Tianyu 	for (i = 0; i < hdev->maxchild; i++)
1572fa2a9566SLan Tianyu 		usb_hub_remove_port_device(hub, i + 1);
1573e8054854SAlan Stern 
1574e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1575e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
15768e4ceb38SAlan Stern 	if (!list_empty(&hub->event_list)) {
1577e8054854SAlan Stern 		list_del_init(&hub->event_list);
15788e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
15798e4ceb38SAlan Stern 	}
1580e8054854SAlan Stern 	hub->disconnected = 1;
1581e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
15821da177e4SLinus Torvalds 
15837de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
15847de18d8bSAlan Stern 	hub->error = 0;
15854330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
15867de18d8bSAlan Stern 
15878b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
15887cbe5dcaSAlan Stern 	hub->hdev->maxchild = 0;
15891da177e4SLinus Torvalds 
1590e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
15911da177e4SLinus Torvalds 		highspeed_hubs--;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
1594fa2a9566SLan Tianyu 	kfree(hub->ports);
15951da177e4SLinus Torvalds 	kfree(hub->descriptor);
15961da177e4SLinus Torvalds 	kfree(hub->status);
1597d697cddaSAlan Stern 	kfree(hub->buffer);
15981da177e4SLinus Torvalds 
1599e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds 	struct usb_host_interface *desc;
16051da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
16061da177e4SLinus Torvalds 	struct usb_device *hdev;
16071da177e4SLinus Torvalds 	struct usb_hub *hub;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
16101da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
16111da177e4SLinus Torvalds 
16122839f5bcSSarah Sharp 	/* Hubs have proper suspend/resume support. */
1613088f7fecSAlan Stern 	usb_enable_autosuspend(hdev);
1614088f7fecSAlan Stern 
161538f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1616b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1617b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
161838f3ad5eSFelipe Balbi 		return -E2BIG;
161938f3ad5eSFelipe Balbi 	}
162038f3ad5eSFelipe Balbi 
162189ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
162289ccbdc9SDavid Brownell 	if (hdev->parent) {
162389ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
162489ccbdc9SDavid Brownell 		return -ENODEV;
162589ccbdc9SDavid Brownell 	}
162689ccbdc9SDavid Brownell #endif
162789ccbdc9SDavid Brownell 
16281da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
16291da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
16301da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
16311da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
16321da177e4SLinus Torvalds descriptor_error:
16331da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
16341da177e4SLinus Torvalds 		return -EIO;
16351da177e4SLinus Torvalds 	}
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
16381da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
16391da177e4SLinus Torvalds 		goto descriptor_error;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
16421da177e4SLinus Torvalds 
1643fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1644fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
16451da177e4SLinus Torvalds 		goto descriptor_error;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	/* We found a hub */
16481da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
16491da177e4SLinus Torvalds 
16500a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
16511da177e4SLinus Torvalds 	if (!hub) {
16521da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
16531da177e4SLinus Torvalds 		return -ENOMEM;
16541da177e4SLinus Torvalds 	}
16551da177e4SLinus Torvalds 
1656e8054854SAlan Stern 	kref_init(&hub->kref);
16571da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
16581da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
16591da177e4SLinus Torvalds 	hub->hdev = hdev;
1660c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
16618520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1662e8054854SAlan Stern 	usb_get_intf(intf);
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
166540f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
16681da177e4SLinus Torvalds 		highspeed_hubs++;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
16711da177e4SLinus Torvalds 		return 0;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	hub_disconnect (intf);
16741da177e4SLinus Torvalds 	return -ENODEV;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds static int
16781da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
16791da177e4SLinus Torvalds {
16801da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
1681ff823c79SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
16841da177e4SLinus Torvalds 	switch (code) {
16851da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
16861da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
16871da177e4SLinus Torvalds 		int i;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
16901da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
16911da177e4SLinus Torvalds 			info->nports = 0;
16921da177e4SLinus Torvalds 		else {
16931da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
16941da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
1695ff823c79SLan Tianyu 				if (hub->ports[i]->child == NULL)
16961da177e4SLinus Torvalds 					info->port[i] = 0;
16971da177e4SLinus Torvalds 				else
16981da177e4SLinus Torvalds 					info->port[i] =
1699ff823c79SLan Tianyu 						hub->ports[i]->child->devnum;
17001da177e4SLinus Torvalds 			}
17011da177e4SLinus Torvalds 		}
17021da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 		return info->nports + 1;
17051da177e4SLinus Torvalds 		}
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	default:
17081da177e4SLinus Torvalds 		return -ENOSYS;
17091da177e4SLinus Torvalds 	}
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds 
17127cbe5dcaSAlan Stern /*
17137cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
17147cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
17157cbe5dcaSAlan Stern  */
17167cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
1717336c5c31SLan Tianyu 		struct dev_state ***ppowner)
17187cbe5dcaSAlan Stern {
17197cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
17207cbe5dcaSAlan Stern 		return -ENODEV;
17217cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
17227cbe5dcaSAlan Stern 		return -EINVAL;
17237cbe5dcaSAlan Stern 
17247cbe5dcaSAlan Stern 	/* This assumes that devices not managed by the hub driver
17257cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
17267cbe5dcaSAlan Stern 	 */
1727fa2a9566SLan Tianyu 	*ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
17287cbe5dcaSAlan Stern 	return 0;
17297cbe5dcaSAlan Stern }
17307cbe5dcaSAlan Stern 
17317cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
1732336c5c31SLan Tianyu int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1733336c5c31SLan Tianyu 		       struct dev_state *owner)
17347cbe5dcaSAlan Stern {
17357cbe5dcaSAlan Stern 	int rc;
1736336c5c31SLan Tianyu 	struct dev_state **powner;
17377cbe5dcaSAlan Stern 
17387cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
17397cbe5dcaSAlan Stern 	if (rc)
17407cbe5dcaSAlan Stern 		return rc;
17417cbe5dcaSAlan Stern 	if (*powner)
17427cbe5dcaSAlan Stern 		return -EBUSY;
17437cbe5dcaSAlan Stern 	*powner = owner;
17447cbe5dcaSAlan Stern 	return rc;
17457cbe5dcaSAlan Stern }
17467cbe5dcaSAlan Stern 
1747336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1748336c5c31SLan Tianyu 			 struct dev_state *owner)
17497cbe5dcaSAlan Stern {
17507cbe5dcaSAlan Stern 	int rc;
1751336c5c31SLan Tianyu 	struct dev_state **powner;
17527cbe5dcaSAlan Stern 
17537cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
17547cbe5dcaSAlan Stern 	if (rc)
17557cbe5dcaSAlan Stern 		return rc;
17567cbe5dcaSAlan Stern 	if (*powner != owner)
17577cbe5dcaSAlan Stern 		return -ENOENT;
17587cbe5dcaSAlan Stern 	*powner = NULL;
17597cbe5dcaSAlan Stern 	return rc;
17607cbe5dcaSAlan Stern }
17617cbe5dcaSAlan Stern 
1762336c5c31SLan Tianyu void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
17637cbe5dcaSAlan Stern {
1764fa2a9566SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
17657cbe5dcaSAlan Stern 	int n;
17667cbe5dcaSAlan Stern 
1767fa2a9566SLan Tianyu 	for (n = 0; n < hdev->maxchild; n++) {
1768fa2a9566SLan Tianyu 		if (hub->ports[n]->port_owner == owner)
1769fa2a9566SLan Tianyu 			hub->ports[n]->port_owner = NULL;
17707cbe5dcaSAlan Stern 	}
1771fa2a9566SLan Tianyu 
17727cbe5dcaSAlan Stern }
17737cbe5dcaSAlan Stern 
17747cbe5dcaSAlan Stern /* The caller must hold udev's lock */
17757cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
17767cbe5dcaSAlan Stern {
17777cbe5dcaSAlan Stern 	struct usb_hub *hub;
17787cbe5dcaSAlan Stern 
17797cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
17807cbe5dcaSAlan Stern 		return false;
17817cbe5dcaSAlan Stern 	hub = hdev_to_hub(udev->parent);
1782fa2a9566SLan Tianyu 	return !!hub->ports[udev->portnum - 1]->port_owner;
17837cbe5dcaSAlan Stern }
17847cbe5dcaSAlan Stern 
17851da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
17861da177e4SLinus Torvalds {
1787ff823c79SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(udev);
17881da177e4SLinus Torvalds 	int i;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
1791ff823c79SLan Tianyu 		if (hub->ports[i]->child)
1792ff823c79SLan Tianyu 			recursively_mark_NOTATTACHED(hub->ports[i]->child);
17931da177e4SLinus Torvalds 	}
17949bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_SUSPENDED)
179515123006SSarah Sharp 		udev->active_duration -= jiffies;
17961da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds /**
18001da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
18011da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
18021da177e4SLinus Torvalds  * @new_state: new state value to be stored
18031da177e4SLinus Torvalds  *
18041da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
18051da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
18061da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
18071da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
18081da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
18091da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
18101da177e4SLinus Torvalds  * device_state_lock spinlock.
18111da177e4SLinus Torvalds  *
18121da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
18131da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
18141da177e4SLinus Torvalds  *
18151da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
18161da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
18171da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
18181da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
18191da177e4SLinus Torvalds  */
18201da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
18211da177e4SLinus Torvalds 		enum usb_device_state new_state)
18221da177e4SLinus Torvalds {
18231da177e4SLinus Torvalds 	unsigned long flags;
18244681b171SRafael J. Wysocki 	int wakeup = -1;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
18271da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
18281da177e4SLinus Torvalds 		;	/* do nothing */
1829b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1830fb669cc0SDavid Brownell 
1831fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1832fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1833fb669cc0SDavid Brownell 		 */
1834fb669cc0SDavid Brownell 		if (udev->parent) {
1835645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1836645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1837645daaabSAlan Stern 				;	/* No change to wakeup settings */
1838645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
18394681b171SRafael J. Wysocki 				wakeup = udev->actconfig->desc.bmAttributes
18404681b171SRafael J. Wysocki 					 & USB_CONFIG_ATT_WAKEUP;
1841645daaabSAlan Stern 			else
18424681b171SRafael J. Wysocki 				wakeup = 0;
1843fb669cc0SDavid Brownell 		}
184415123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
184515123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
184615123006SSarah Sharp 			udev->active_duration -= jiffies;
184715123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
184815123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
184915123006SSarah Sharp 			udev->active_duration += jiffies;
1850645daaabSAlan Stern 		udev->state = new_state;
1851b94dc6b5SDavid Brownell 	} else
18521da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
18531da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
18544681b171SRafael J. Wysocki 	if (wakeup >= 0)
18554681b171SRafael J. Wysocki 		device_set_wakeup_capable(&udev->dev, wakeup);
18561da177e4SLinus Torvalds }
18576da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
18581da177e4SLinus Torvalds 
18598af548dcSInaky Perez-Gonzalez /*
18603b29b68bSAlan Stern  * Choose a device number.
18613b29b68bSAlan Stern  *
18623b29b68bSAlan Stern  * Device numbers are used as filenames in usbfs.  On USB-1.1 and
18633b29b68bSAlan Stern  * USB-2.0 buses they are also used as device addresses, however on
18643b29b68bSAlan Stern  * USB-3.0 buses the address is assigned by the controller hardware
18653b29b68bSAlan Stern  * and it usually is not the same as the device number.
18663b29b68bSAlan Stern  *
18678af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
18688af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
18698af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
18708af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
18718af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
18728af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
18738af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
18748af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
18758af548dcSInaky Perez-Gonzalez  * [X | 0x80].
18768af548dcSInaky Perez-Gonzalez  *
18778af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
18788af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
18798af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
18808af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
18818af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
1882c6515272SSarah Sharp  *
1883c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
1884c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
1885c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
1886c6515272SSarah Sharp  * command to the hardware.
18878af548dcSInaky Perez-Gonzalez  */
18883b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int		devnum;
18911da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
18948af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
18958af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
18968af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
18978af548dcSInaky Perez-Gonzalez 	} else {
18988af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
18998af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
19001da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
19011da177e4SLinus Torvalds 					    bus->devnum_next);
19021da177e4SLinus Torvalds 		if (devnum >= 128)
19038af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
19048af548dcSInaky Perez-Gonzalez 						    128, 1);
19051da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
19068af548dcSInaky Perez-Gonzalez 	}
19071da177e4SLinus Torvalds 	if (devnum < 128) {
19081da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
19091da177e4SLinus Torvalds 		udev->devnum = devnum;
19101da177e4SLinus Torvalds 	}
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
19133b29b68bSAlan Stern static void release_devnum(struct usb_device *udev)
19141da177e4SLinus Torvalds {
19151da177e4SLinus Torvalds 	if (udev->devnum > 0) {
19161da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
19171da177e4SLinus Torvalds 		udev->devnum = -1;
19181da177e4SLinus Torvalds 	}
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19213b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum)
19224953d141SDavid Vrabel {
19234953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
19244953d141SDavid Vrabel 	if (!udev->wusb)
19254953d141SDavid Vrabel 		udev->devnum = devnum;
19264953d141SDavid Vrabel }
19274953d141SDavid Vrabel 
1928f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev)
1929f7410cedSHerbert Xu {
1930f7410cedSHerbert Xu 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1931f7410cedSHerbert Xu 
1932f7410cedSHerbert Xu 	/* Root hubs aren't real devices, so don't free HCD resources */
1933f7410cedSHerbert Xu 	if (hcd->driver->free_dev && udev->parent)
1934f7410cedSHerbert Xu 		hcd->driver->free_dev(hcd, udev);
1935f7410cedSHerbert Xu }
1936f7410cedSHerbert Xu 
19371da177e4SLinus Torvalds /**
19381da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
19391da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
19401da177e4SLinus Torvalds  * Context: !in_interrupt ()
19411da177e4SLinus Torvalds  *
19421da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
19431da177e4SLinus Torvalds  *
19441da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
19451da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
19461da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
19471da177e4SLinus Torvalds  *
19481da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
19491da177e4SLinus Torvalds  * controllers) should ever call this.
19501da177e4SLinus Torvalds  *
19511da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
19521da177e4SLinus Torvalds  */
19531da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
1956ff823c79SLan Tianyu 	struct usb_hub		*hub = hdev_to_hub(udev);
19571da177e4SLinus Torvalds 	int			i;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
19601da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
196125985edcSLucas De Marchi 	 * this quiesces everything except pending urbs.
19621da177e4SLinus Torvalds 	 */
19631da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
19643b29b68bSAlan Stern 	dev_info(&udev->dev, "USB disconnect, device number %d\n",
19653b29b68bSAlan Stern 			udev->devnum);
19661da177e4SLinus Torvalds 
19679ad3d6ccSAlan Stern 	usb_lock_device(udev);
19689ad3d6ccSAlan Stern 
19691da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
19708816230eSHuajun Li 	for (i = 0; i < udev->maxchild; i++) {
1971ff823c79SLan Tianyu 		if (hub->ports[i]->child)
1972ff823c79SLan Tianyu 			usb_disconnect(&hub->ports[i]->child);
19731da177e4SLinus Torvalds 	}
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
19761da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
19771da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
19781da177e4SLinus Torvalds 	 */
1979782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
19801da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
1981cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
19821da177e4SLinus Torvalds 
19833b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
1984782da727SAlan Stern 	usb_unlock_device(udev);
19853099e75aSGreg Kroah-Hartman 
1986782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
19873b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
19883b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1989782da727SAlan Stern 	 */
1990782da727SAlan Stern 	device_del(&udev->dev);
1991782da727SAlan Stern 
1992782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
19931da177e4SLinus Torvalds 	 * (or root_hub) pointer.
19941da177e4SLinus Torvalds 	 */
19953b29b68bSAlan Stern 	release_devnum(udev);
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
19981da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
19991da177e4SLinus Torvalds 	*pdev = NULL;
20001da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
20011da177e4SLinus Torvalds 
2002f7410cedSHerbert Xu 	hub_free_dev(udev);
2003f7410cedSHerbert Xu 
2004782da727SAlan Stern 	put_device(&udev->dev);
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
2007f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
20081da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
20091da177e4SLinus Torvalds {
20101da177e4SLinus Torvalds 	if (!string)
20111da177e4SLinus Torvalds 		return;
20121da177e4SLinus Torvalds 	dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
2015f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
2016f2a383e4SGreg Kroah-Hartman {
2017f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2018f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
2019f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
2020b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
2021b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2022f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
2023f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
2024f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
2025f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
2026f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
2027f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
2028f2a383e4SGreg Kroah-Hartman }
20291da177e4SLinus Torvalds #else
2030f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
20311da177e4SLinus Torvalds #endif
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
20341da177e4SLinus Torvalds #include "otg_whitelist.h"
20351da177e4SLinus Torvalds #endif
20361da177e4SLinus Torvalds 
20373ede760fSOliver Neukum /**
20388d8558d1SAlan Stern  * usb_enumerate_device_otg - FIXME (usbcore-internal)
20393ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
20403ede760fSOliver Neukum  *
20418d8558d1SAlan Stern  * Finish enumeration for On-The-Go devices
20423ede760fSOliver Neukum  */
20438d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
20441da177e4SLinus Torvalds {
2045d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
20481da177e4SLinus Torvalds 	/*
20491da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
20501da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
20511da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
20521da177e4SLinus Torvalds 	 */
20531da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
20541da177e4SLinus Torvalds 			&& udev->config
20551da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
20562eb5052eSFelipe Balbi 		struct usb_otg_descriptor	*desc = NULL;
20571da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
20601da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
20611da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
20621da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
20631da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
206412c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 				dev_info(&udev->dev,
20671da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
20681da177e4SLinus Torvalds 					(port1 == bus->otg_port)
20691da177e4SLinus Torvalds 						? "" : "non-");
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
20721da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
20731da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
20741da177e4SLinus Torvalds 				err = usb_control_msg(udev,
20751da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
20761da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
20771da177e4SLinus Torvalds 					bus->b_hnp_enable
20781da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
20791da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
20801da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
20811da177e4SLinus Torvalds 				if (err < 0) {
20821da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
20831da177e4SLinus Torvalds 					 * customize to match your product.
20841da177e4SLinus Torvalds 					 */
20851da177e4SLinus Torvalds 					dev_info(&udev->dev,
2086b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
20871da177e4SLinus Torvalds 						err);
20881da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
20891da177e4SLinus Torvalds 				}
20901da177e4SLinus Torvalds 			}
20911da177e4SLinus Torvalds 		}
20921da177e4SLinus Torvalds 	}
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
20971da177e4SLinus Torvalds 		 * (Includes HNP test device.)
20981da177e4SLinus Torvalds 		 */
20991da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2100634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
21011da177e4SLinus Torvalds 			if (err < 0)
21021da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
21031da177e4SLinus Torvalds 		}
2104ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
21051da177e4SLinus Torvalds 		goto fail;
21061da177e4SLinus Torvalds 	}
2107d9d16e8aSInaky Perez-Gonzalez fail:
21081da177e4SLinus Torvalds #endif
2109d9d16e8aSInaky Perez-Gonzalez 	return err;
2110d9d16e8aSInaky Perez-Gonzalez }
21111da177e4SLinus Torvalds 
2112d9d16e8aSInaky Perez-Gonzalez 
2113d9d16e8aSInaky Perez-Gonzalez /**
21148d8558d1SAlan Stern  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2115d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
2116d9d16e8aSInaky Perez-Gonzalez  *
2117d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
2118d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
2119d9d16e8aSInaky Perez-Gonzalez  * environment.
2120d9d16e8aSInaky Perez-Gonzalez  *
2121d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
2122d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
2123d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
2124d9d16e8aSInaky Perez-Gonzalez  */
21258d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
2126d9d16e8aSInaky Perez-Gonzalez {
2127d9d16e8aSInaky Perez-Gonzalez 	int err;
2128d9d16e8aSInaky Perez-Gonzalez 
2129d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
2130d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
2131d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
2132d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
2133d9d16e8aSInaky Perez-Gonzalez 				err);
213480da2e0dSLaurent Pinchart 			return err;
2135d9d16e8aSInaky Perez-Gonzalez 		}
2136d9d16e8aSInaky Perez-Gonzalez 	}
2137d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
2138d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2139d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2140d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2141d9d16e8aSInaky Perez-Gonzalez 	}
2142d9d16e8aSInaky Perez-Gonzalez 	else {
2143d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
2144d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2145d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
2146d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
2147d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2148d9d16e8aSInaky Perez-Gonzalez 	}
21498d8558d1SAlan Stern 	err = usb_enumerate_device_otg(udev);
215080da2e0dSLaurent Pinchart 	if (err < 0)
2151d9d16e8aSInaky Perez-Gonzalez 		return err;
215280da2e0dSLaurent Pinchart 
215380da2e0dSLaurent Pinchart 	usb_detect_interface_quirks(udev);
215480da2e0dSLaurent Pinchart 
215580da2e0dSLaurent Pinchart 	return 0;
2156d9d16e8aSInaky Perez-Gonzalez }
2157d9d16e8aSInaky Perez-Gonzalez 
2158d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev)
2159d35e70d5SMatthew Garrett {
2160d35e70d5SMatthew Garrett 	struct usb_device *hdev = udev->parent;
2161d35e70d5SMatthew Garrett 	struct usb_hub *hub;
2162d35e70d5SMatthew Garrett 	u8 port = udev->portnum;
2163d35e70d5SMatthew Garrett 	u16 wHubCharacteristics;
2164d35e70d5SMatthew Garrett 	bool removable = true;
2165d35e70d5SMatthew Garrett 
2166d35e70d5SMatthew Garrett 	if (!hdev)
2167d35e70d5SMatthew Garrett 		return;
2168d35e70d5SMatthew Garrett 
2169d35e70d5SMatthew Garrett 	hub = hdev_to_hub(udev->parent);
2170d35e70d5SMatthew Garrett 
2171d35e70d5SMatthew Garrett 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2172d35e70d5SMatthew Garrett 
2173d35e70d5SMatthew Garrett 	if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2174d35e70d5SMatthew Garrett 		return;
2175d35e70d5SMatthew Garrett 
2176d35e70d5SMatthew Garrett 	if (hub_is_superspeed(hdev)) {
2177d35e70d5SMatthew Garrett 		if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2178d35e70d5SMatthew Garrett 			removable = false;
2179d35e70d5SMatthew Garrett 	} else {
2180d35e70d5SMatthew Garrett 		if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2181d35e70d5SMatthew Garrett 			removable = false;
2182d35e70d5SMatthew Garrett 	}
2183d35e70d5SMatthew Garrett 
2184d35e70d5SMatthew Garrett 	if (removable)
2185d35e70d5SMatthew Garrett 		udev->removable = USB_DEVICE_REMOVABLE;
2186d35e70d5SMatthew Garrett 	else
2187d35e70d5SMatthew Garrett 		udev->removable = USB_DEVICE_FIXED;
2188d35e70d5SMatthew Garrett }
2189d9d16e8aSInaky Perez-Gonzalez 
2190d9d16e8aSInaky Perez-Gonzalez /**
2191d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
2192d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
2193d9d16e8aSInaky Perez-Gonzalez  *
21948d8558d1SAlan Stern  * This is called with devices which have been detected but not fully
21958d8558d1SAlan Stern  * enumerated.  The device descriptor is available, but not descriptors
2196d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
2197d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
2198d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
2199d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
2200d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
2201d9d16e8aSInaky Perez-Gonzalez  *
2202d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
2203d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
2204d9d16e8aSInaky Perez-Gonzalez  * errno value.
2205d9d16e8aSInaky Perez-Gonzalez  *
2206d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
2207d9d16e8aSInaky Perez-Gonzalez  *
2208d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
2209d9d16e8aSInaky Perez-Gonzalez  */
2210d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
2211d9d16e8aSInaky Perez-Gonzalez {
2212d9d16e8aSInaky Perez-Gonzalez 	int err;
2213d9d16e8aSInaky Perez-Gonzalez 
221416985408SDan Streetman 	if (udev->parent) {
221516985408SDan Streetman 		/* Initialize non-root-hub device wakeup to disabled;
221616985408SDan Streetman 		 * device (un)configuration controls wakeup capable
221716985408SDan Streetman 		 * sysfs power/wakeup controls wakeup enabled/disabled
221816985408SDan Streetman 		 */
221916985408SDan Streetman 		device_init_wakeup(&udev->dev, 0);
222016985408SDan Streetman 	}
222116985408SDan Streetman 
22229bbdf1e0SAlan Stern 	/* Tell the runtime-PM framework the device is active */
22239bbdf1e0SAlan Stern 	pm_runtime_set_active(&udev->dev);
2224c08512c7SAlan Stern 	pm_runtime_get_noresume(&udev->dev);
2225fcc4a01eSAlan Stern 	pm_runtime_use_autosuspend(&udev->dev);
22269bbdf1e0SAlan Stern 	pm_runtime_enable(&udev->dev);
22279bbdf1e0SAlan Stern 
2228c08512c7SAlan Stern 	/* By default, forbid autosuspend for all devices.  It will be
2229c08512c7SAlan Stern 	 * allowed for hubs during binding.
2230c08512c7SAlan Stern 	 */
2231c08512c7SAlan Stern 	usb_disable_autosuspend(udev);
2232c08512c7SAlan Stern 
22338d8558d1SAlan Stern 	err = usb_enumerate_device(udev);	/* Read descriptors */
2234d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
2235d9d16e8aSInaky Perez-Gonzalez 		goto fail;
2236c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2237c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
2238c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
22399f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
22409f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
22419f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
22429f8b17e6SKay Sievers 
22436cd13201SAlan Stern 	/* Tell the world! */
22446cd13201SAlan Stern 	announce_device(udev);
2245195af2ccSAlan Stern 
2246b04b3156STheodore Ts'o 	if (udev->serial)
2247b04b3156STheodore Ts'o 		add_device_randomness(udev->serial, strlen(udev->serial));
2248b04b3156STheodore Ts'o 	if (udev->product)
2249b04b3156STheodore Ts'o 		add_device_randomness(udev->product, strlen(udev->product));
2250b04b3156STheodore Ts'o 	if (udev->manufacturer)
2251b04b3156STheodore Ts'o 		add_device_randomness(udev->manufacturer,
2252b04b3156STheodore Ts'o 				      strlen(udev->manufacturer));
2253b04b3156STheodore Ts'o 
2254927bc916SRafael J. Wysocki 	device_enable_async_suspend(&udev->dev);
2255d35e70d5SMatthew Garrett 
2256d35e70d5SMatthew Garrett 	/*
2257d35e70d5SMatthew Garrett 	 * check whether the hub marks this port as non-removable. Do it
2258d35e70d5SMatthew Garrett 	 * now so that platform-specific data can override it in
2259d35e70d5SMatthew Garrett 	 * device_add()
2260d35e70d5SMatthew Garrett 	 */
2261d35e70d5SMatthew Garrett 	if (udev->parent)
2262d35e70d5SMatthew Garrett 		set_usb_port_removable(udev);
2263d35e70d5SMatthew Garrett 
2264782da727SAlan Stern 	/* Register the device.  The device driver is responsible
22653b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
22663b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
2267782da727SAlan Stern 	 */
22681da177e4SLinus Torvalds 	err = device_add(&udev->dev);
22691da177e4SLinus Torvalds 	if (err) {
22701da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
22711da177e4SLinus Torvalds 		goto fail;
22721da177e4SLinus Torvalds 	}
22739ad3d6ccSAlan Stern 
22743b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2275c08512c7SAlan Stern 	usb_mark_last_busy(udev);
2276c08512c7SAlan Stern 	pm_runtime_put_sync_autosuspend(&udev->dev);
2277c066475eSGreg Kroah-Hartman 	return err;
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds fail:
22801da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
22819bbdf1e0SAlan Stern 	pm_runtime_disable(&udev->dev);
22829bbdf1e0SAlan Stern 	pm_runtime_set_suspended(&udev->dev);
2283d9d16e8aSInaky Perez-Gonzalez 	return err;
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
228693993a0aSInaky Perez-Gonzalez 
228793993a0aSInaky Perez-Gonzalez /**
2288fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2289fd39c86bSRandy Dunlap  * @usb_dev: USB device
2290fd39c86bSRandy Dunlap  *
2291fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
2292fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
229393993a0aSInaky Perez-Gonzalez  *
229493993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
229593993a0aSInaky Perez-Gonzalez  * defer its call.
229693993a0aSInaky Perez-Gonzalez  */
229793993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
229893993a0aSInaky Perez-Gonzalez {
229993993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
230093993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
230193993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
2302da307123SAlan Stern 
230393993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
230493993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
2305da307123SAlan Stern 
2306da307123SAlan Stern 	kfree(usb_dev->product);
230793993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2308da307123SAlan Stern 	kfree(usb_dev->manufacturer);
230993993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2310da307123SAlan Stern 	kfree(usb_dev->serial);
231193993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2312da307123SAlan Stern 
2313da307123SAlan Stern 	usb_destroy_configuration(usb_dev);
231493993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
2315da307123SAlan Stern 
231693993a0aSInaky Perez-Gonzalez out_unauthorized:
231793993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
231893993a0aSInaky Perez-Gonzalez 	return 0;
231993993a0aSInaky Perez-Gonzalez }
232093993a0aSInaky Perez-Gonzalez 
232193993a0aSInaky Perez-Gonzalez 
232293993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
232393993a0aSInaky Perez-Gonzalez {
232493993a0aSInaky Perez-Gonzalez 	int result = 0, c;
2325da307123SAlan Stern 
232693993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
232793993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
232893993a0aSInaky Perez-Gonzalez 		goto out_authorized;
2329da307123SAlan Stern 
233093993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
233193993a0aSInaky Perez-Gonzalez 	if (result < 0) {
233293993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
233393993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
233493993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
233593993a0aSInaky Perez-Gonzalez 	}
233693993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
233793993a0aSInaky Perez-Gonzalez 	if (result < 0) {
233893993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
233993993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
234093993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
234193993a0aSInaky Perez-Gonzalez 	}
2342da307123SAlan Stern 
2343da307123SAlan Stern 	kfree(usb_dev->product);
2344da307123SAlan Stern 	usb_dev->product = NULL;
2345da307123SAlan Stern 	kfree(usb_dev->manufacturer);
2346da307123SAlan Stern 	usb_dev->manufacturer = NULL;
2347da307123SAlan Stern 	kfree(usb_dev->serial);
2348da307123SAlan Stern 	usb_dev->serial = NULL;
2349da307123SAlan Stern 
235093993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
23518d8558d1SAlan Stern 	result = usb_enumerate_device(usb_dev);
235293993a0aSInaky Perez-Gonzalez 	if (result < 0)
23538d8558d1SAlan Stern 		goto error_enumerate;
235493993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
235593993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
235693993a0aSInaky Perez-Gonzalez 	 */
2357b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
235893993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
235993993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
236093993a0aSInaky Perez-Gonzalez 		if (result) {
236193993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
236293993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
236393993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
236493993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
236593993a0aSInaky Perez-Gonzalez 		}
236693993a0aSInaky Perez-Gonzalez 	}
236793993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
2368da307123SAlan Stern 
23698d8558d1SAlan Stern error_enumerate:
237093993a0aSInaky Perez-Gonzalez error_device_descriptor:
2371da307123SAlan Stern 	usb_autosuspend_device(usb_dev);
237293993a0aSInaky Perez-Gonzalez error_autoresume:
237393993a0aSInaky Perez-Gonzalez out_authorized:
237493993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
237593993a0aSInaky Perez-Gonzalez 	return result;
237693993a0aSInaky Perez-Gonzalez }
237793993a0aSInaky Perez-Gonzalez 
237893993a0aSInaky Perez-Gonzalez 
23790165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
23800165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
23810165de09SInaky Perez-Gonzalez {
23820165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
23830165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
23840165de09SInaky Perez-Gonzalez 		return 0;
23850165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
23860165de09SInaky Perez-Gonzalez 	return hcd->wireless;
23870165de09SInaky Perez-Gonzalez }
23880165de09SInaky Perez-Gonzalez 
23890165de09SInaky Perez-Gonzalez 
23901da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
23911da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
23921da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
23931da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
239490ab5ee9SRusty Russell #define USE_NEW_SCHEME(i)	((i) / 2 == (int)old_scheme_first)
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
23971da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
239875d7cf72SAndiry Xu #define HUB_BH_RESET_TIME	50
23991da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
24001da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
24011da177e4SLinus Torvalds 
240210d674a8SSarah Sharp static int hub_port_reset(struct usb_hub *hub, int port1,
240310d674a8SSarah Sharp 			struct usb_device *udev, unsigned int delay, bool warm);
240410d674a8SSarah Sharp 
24058bea2bd3SStanislaw Ledwon /* Is a USB 3.0 port in the Inactive or Complinance Mode state?
24068bea2bd3SStanislaw Ledwon  * Port worm reset is required to recover
24078bea2bd3SStanislaw Ledwon  */
24088bea2bd3SStanislaw Ledwon static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
240910d674a8SSarah Sharp {
241010d674a8SSarah Sharp 	return hub_is_superspeed(hub->hdev) &&
24118bea2bd3SStanislaw Ledwon 		(((portstatus & USB_PORT_STAT_LINK_STATE) ==
24128bea2bd3SStanislaw Ledwon 		  USB_SS_PORT_LS_SS_INACTIVE) ||
24138bea2bd3SStanislaw Ledwon 		 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
24148bea2bd3SStanislaw Ledwon 		  USB_SS_PORT_LS_COMP_MOD)) ;
241510d674a8SSarah Sharp }
241610d674a8SSarah Sharp 
24171da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
241875d7cf72SAndiry Xu 			struct usb_device *udev, unsigned int delay, bool warm)
24191da177e4SLinus Torvalds {
24201da177e4SLinus Torvalds 	int delay_time, ret;
24211da177e4SLinus Torvalds 	u16 portstatus;
24221da177e4SLinus Torvalds 	u16 portchange;
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	for (delay_time = 0;
24251da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
24261da177e4SLinus Torvalds 			delay_time += delay) {
24271da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
24281da177e4SLinus Torvalds 		msleep(delay);
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 		/* read and decode port status */
24311da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
24321da177e4SLinus Torvalds 		if (ret < 0)
24331da177e4SLinus Torvalds 			return ret;
24341da177e4SLinus Torvalds 
243575d7cf72SAndiry Xu 		/*
243675d7cf72SAndiry Xu 		 * Some buggy devices require a warm reset to be issued even
243775d7cf72SAndiry Xu 		 * when the port appears not to be connected.
243875d7cf72SAndiry Xu 		 */
243975d7cf72SAndiry Xu 		if (!warm) {
244010d674a8SSarah Sharp 			/*
244110d674a8SSarah Sharp 			 * Some buggy devices can cause an NEC host controller
244210d674a8SSarah Sharp 			 * to transition to the "Error" state after a hot port
244310d674a8SSarah Sharp 			 * reset.  This will show up as the port state in
244410d674a8SSarah Sharp 			 * "Inactive", and the port may also report a
244510d674a8SSarah Sharp 			 * disconnect.  Forcing a warm port reset seems to make
244610d674a8SSarah Sharp 			 * the device work.
244710d674a8SSarah Sharp 			 *
244810d674a8SSarah Sharp 			 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
244910d674a8SSarah Sharp 			 */
24508bea2bd3SStanislaw Ledwon 			if (hub_port_warm_reset_required(hub, portstatus)) {
245110d674a8SSarah Sharp 				int ret;
245210d674a8SSarah Sharp 
245310d674a8SSarah Sharp 				if ((portchange & USB_PORT_STAT_C_CONNECTION))
245410d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
245510d674a8SSarah Sharp 							USB_PORT_FEAT_C_CONNECTION);
245610d674a8SSarah Sharp 				if (portchange & USB_PORT_STAT_C_LINK_STATE)
245710d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
245810d674a8SSarah Sharp 							USB_PORT_FEAT_C_PORT_LINK_STATE);
245910d674a8SSarah Sharp 				if (portchange & USB_PORT_STAT_C_RESET)
246010d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
246110d674a8SSarah Sharp 							USB_PORT_FEAT_C_RESET);
246210d674a8SSarah Sharp 				dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
246310d674a8SSarah Sharp 						port1);
246410d674a8SSarah Sharp 				ret = hub_port_reset(hub, port1,
246510d674a8SSarah Sharp 						udev, HUB_BH_RESET_TIME,
246610d674a8SSarah Sharp 						true);
246710d674a8SSarah Sharp 				if ((portchange & USB_PORT_STAT_C_CONNECTION))
246810d674a8SSarah Sharp 					clear_port_feature(hub->hdev, port1,
246910d674a8SSarah Sharp 							USB_PORT_FEAT_C_CONNECTION);
247010d674a8SSarah Sharp 				return ret;
247110d674a8SSarah Sharp 			}
24721da177e4SLinus Torvalds 			/* Device went away? */
24731da177e4SLinus Torvalds 			if (!(portstatus & USB_PORT_STAT_CONNECTION))
24741da177e4SLinus Torvalds 				return -ENOTCONN;
24751da177e4SLinus Torvalds 
2476dd4dd19eSAlan Stern 			/* bomb out completely if the connection bounced */
24771da177e4SLinus Torvalds 			if ((portchange & USB_PORT_STAT_C_CONNECTION))
2478dd4dd19eSAlan Stern 				return -ENOTCONN;
24791da177e4SLinus Torvalds 
248075d7cf72SAndiry Xu 			/* if we`ve finished resetting, then break out of
248175d7cf72SAndiry Xu 			 * the loop
248275d7cf72SAndiry Xu 			 */
24831da177e4SLinus Torvalds 			if (!(portstatus & USB_PORT_STAT_RESET) &&
24841da177e4SLinus Torvalds 			    (portstatus & USB_PORT_STAT_ENABLE)) {
24850165de09SInaky Perez-Gonzalez 				if (hub_is_wusb(hub))
2486551cdbbeSGreg Kroah-Hartman 					udev->speed = USB_SPEED_WIRELESS;
2487131dec34SSarah Sharp 				else if (hub_is_superspeed(hub->hdev))
2488809cd1cbSSarah Sharp 					udev->speed = USB_SPEED_SUPER;
24890165de09SInaky Perez-Gonzalez 				else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
24901da177e4SLinus Torvalds 					udev->speed = USB_SPEED_HIGH;
24911da177e4SLinus Torvalds 				else if (portstatus & USB_PORT_STAT_LOW_SPEED)
24921da177e4SLinus Torvalds 					udev->speed = USB_SPEED_LOW;
24931da177e4SLinus Torvalds 				else
24941da177e4SLinus Torvalds 					udev->speed = USB_SPEED_FULL;
24951da177e4SLinus Torvalds 				return 0;
24961da177e4SLinus Torvalds 			}
249775d7cf72SAndiry Xu 		} else {
249875d7cf72SAndiry Xu 			if (portchange & USB_PORT_STAT_C_BH_RESET)
249975d7cf72SAndiry Xu 				return 0;
250075d7cf72SAndiry Xu 		}
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
25031da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
25041da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
250775d7cf72SAndiry Xu 			"port %d not %sreset yet, waiting %dms\n",
250875d7cf72SAndiry Xu 			port1, warm ? "warm " : "", delay);
25091da177e4SLinus Torvalds 	}
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	return -EBUSY;
25121da177e4SLinus Torvalds }
25131da177e4SLinus Torvalds 
251475d7cf72SAndiry Xu static void hub_port_finish_reset(struct usb_hub *hub, int port1,
251575d7cf72SAndiry Xu 			struct usb_device *udev, int *status, bool warm)
25161da177e4SLinus Torvalds {
251775d7cf72SAndiry Xu 	switch (*status) {
25181da177e4SLinus Torvalds 	case 0:
251975d7cf72SAndiry Xu 		if (!warm) {
252075d7cf72SAndiry Xu 			struct usb_hcd *hcd;
2521b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
2522b789696aSDavid Brownell 			msleep(10 + 40);
25233b29b68bSAlan Stern 			update_devnum(udev, 0);
252475d7cf72SAndiry Xu 			hcd = bus_to_hcd(udev->bus);
2525a5f0efabSSarah Sharp 			if (hcd->driver->reset_device) {
252675d7cf72SAndiry Xu 				*status = hcd->driver->reset_device(hcd, udev);
252775d7cf72SAndiry Xu 				if (*status < 0) {
2528a5f0efabSSarah Sharp 					dev_err(&udev->dev, "Cannot reset "
2529a5f0efabSSarah Sharp 							"HCD device state\n");
2530a5f0efabSSarah Sharp 					break;
2531a5f0efabSSarah Sharp 				}
2532a5f0efabSSarah Sharp 			}
253375d7cf72SAndiry Xu 		}
25341da177e4SLinus Torvalds 		/* FALL THROUGH */
25351da177e4SLinus Torvalds 	case -ENOTCONN:
25361da177e4SLinus Torvalds 	case -ENODEV:
25371da177e4SLinus Torvalds 		clear_port_feature(hub->hdev,
25381da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
25391da177e4SLinus Torvalds 		/* FIXME need disconnect() for NOTATTACHED device */
254075d7cf72SAndiry Xu 		if (warm) {
254175d7cf72SAndiry Xu 			clear_port_feature(hub->hdev, port1,
254275d7cf72SAndiry Xu 					USB_PORT_FEAT_C_BH_PORT_RESET);
254375d7cf72SAndiry Xu 			clear_port_feature(hub->hdev, port1,
254475d7cf72SAndiry Xu 					USB_PORT_FEAT_C_PORT_LINK_STATE);
254575d7cf72SAndiry Xu 		} else {
254675d7cf72SAndiry Xu 			usb_set_device_state(udev, *status
25471da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
25481da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
254975d7cf72SAndiry Xu 		}
255075d7cf72SAndiry Xu 		break;
255175d7cf72SAndiry Xu 	}
255275d7cf72SAndiry Xu }
255375d7cf72SAndiry Xu 
255475d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
255575d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1,
255675d7cf72SAndiry Xu 			struct usb_device *udev, unsigned int delay, bool warm)
255775d7cf72SAndiry Xu {
255875d7cf72SAndiry Xu 	int i, status;
255975d7cf72SAndiry Xu 
256075d7cf72SAndiry Xu 	if (!warm) {
256175d7cf72SAndiry Xu 		/* Block EHCI CF initialization during the port reset.
256275d7cf72SAndiry Xu 		 * Some companion controllers don't like it when they mix.
256375d7cf72SAndiry Xu 		 */
256475d7cf72SAndiry Xu 		down_read(&ehci_cf_port_reset_rwsem);
256575d7cf72SAndiry Xu 	} else {
256675d7cf72SAndiry Xu 		if (!hub_is_superspeed(hub->hdev)) {
256775d7cf72SAndiry Xu 			dev_err(hub->intfdev, "only USB3 hub support "
256875d7cf72SAndiry Xu 						"warm reset\n");
256975d7cf72SAndiry Xu 			return -EINVAL;
257075d7cf72SAndiry Xu 		}
257175d7cf72SAndiry Xu 	}
257275d7cf72SAndiry Xu 
257375d7cf72SAndiry Xu 	/* Reset the port */
257475d7cf72SAndiry Xu 	for (i = 0; i < PORT_RESET_TRIES; i++) {
257575d7cf72SAndiry Xu 		status = set_port_feature(hub->hdev, port1, (warm ?
257675d7cf72SAndiry Xu 					USB_PORT_FEAT_BH_PORT_RESET :
257775d7cf72SAndiry Xu 					USB_PORT_FEAT_RESET));
257875d7cf72SAndiry Xu 		if (status) {
257975d7cf72SAndiry Xu 			dev_err(hub->intfdev,
258075d7cf72SAndiry Xu 					"cannot %sreset port %d (err = %d)\n",
258175d7cf72SAndiry Xu 					warm ? "warm " : "", port1, status);
258275d7cf72SAndiry Xu 		} else {
258375d7cf72SAndiry Xu 			status = hub_port_wait_reset(hub, port1, udev, delay,
258475d7cf72SAndiry Xu 								warm);
258575d7cf72SAndiry Xu 			if (status && status != -ENOTCONN)
258675d7cf72SAndiry Xu 				dev_dbg(hub->intfdev,
258775d7cf72SAndiry Xu 						"port_wait_reset: err = %d\n",
258875d7cf72SAndiry Xu 						status);
258975d7cf72SAndiry Xu 		}
259075d7cf72SAndiry Xu 
259175d7cf72SAndiry Xu 		/* return on disconnect or reset */
259275d7cf72SAndiry Xu 		if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
259375d7cf72SAndiry Xu 			hub_port_finish_reset(hub, port1, udev, &status, warm);
259432fe0198SAlan Stern 			goto done;
25951da177e4SLinus Torvalds 		}
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
259875d7cf72SAndiry Xu 			"port %d not enabled, trying %sreset again...\n",
259975d7cf72SAndiry Xu 			port1, warm ? "warm " : "");
26001da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
26011da177e4SLinus Torvalds 	}
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	dev_err (hub->intfdev,
26041da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
26051da177e4SLinus Torvalds 		port1);
26061da177e4SLinus Torvalds 
260732fe0198SAlan Stern done:
260875d7cf72SAndiry Xu 	if (!warm)
260932fe0198SAlan Stern 		up_read(&ehci_cf_port_reset_rwsem);
261075d7cf72SAndiry Xu 
26111da177e4SLinus Torvalds 	return status;
26121da177e4SLinus Torvalds }
26131da177e4SLinus Torvalds 
26140ed9a57eSAndiry Xu /* Check if a port is power on */
26150ed9a57eSAndiry Xu static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
26160ed9a57eSAndiry Xu {
26170ed9a57eSAndiry Xu 	int ret = 0;
26180ed9a57eSAndiry Xu 
26190ed9a57eSAndiry Xu 	if (hub_is_superspeed(hub->hdev)) {
26200ed9a57eSAndiry Xu 		if (portstatus & USB_SS_PORT_STAT_POWER)
26210ed9a57eSAndiry Xu 			ret = 1;
26220ed9a57eSAndiry Xu 	} else {
26230ed9a57eSAndiry Xu 		if (portstatus & USB_PORT_STAT_POWER)
26240ed9a57eSAndiry Xu 			ret = 1;
26250ed9a57eSAndiry Xu 	}
26260ed9a57eSAndiry Xu 
26270ed9a57eSAndiry Xu 	return ret;
26280ed9a57eSAndiry Xu }
26290ed9a57eSAndiry Xu 
2630d388dab7SAlan Stern #ifdef	CONFIG_PM
26311da177e4SLinus Torvalds 
26320ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
26330ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
26340ed9a57eSAndiry Xu {
26350ed9a57eSAndiry Xu 	int ret = 0;
26360ed9a57eSAndiry Xu 
26370ed9a57eSAndiry Xu 	if (hub_is_superspeed(hub->hdev)) {
26380ed9a57eSAndiry Xu 		if ((portstatus & USB_PORT_STAT_LINK_STATE)
26390ed9a57eSAndiry Xu 				== USB_SS_PORT_LS_U3)
26400ed9a57eSAndiry Xu 			ret = 1;
26410ed9a57eSAndiry Xu 	} else {
26420ed9a57eSAndiry Xu 		if (portstatus & USB_PORT_STAT_SUSPEND)
26430ed9a57eSAndiry Xu 			ret = 1;
26440ed9a57eSAndiry Xu 	}
26450ed9a57eSAndiry Xu 
26460ed9a57eSAndiry Xu 	return ret;
26470ed9a57eSAndiry Xu }
2648b01b03f3SAlan Stern 
2649b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2650b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2651b01b03f3SAlan Stern  */
2652b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2653b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2654b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2655b01b03f3SAlan Stern {
2656b01b03f3SAlan Stern 	/* Is the device still present? */
26570ed9a57eSAndiry Xu 	if (status || port_is_suspended(hub, portstatus) ||
26580ed9a57eSAndiry Xu 			!port_is_power_on(hub, portstatus) ||
26590ed9a57eSAndiry Xu 			!(portstatus & USB_PORT_STAT_CONNECTION)) {
2660b01b03f3SAlan Stern 		if (status >= 0)
2661b01b03f3SAlan Stern 			status = -ENODEV;
2662b01b03f3SAlan Stern 	}
2663b01b03f3SAlan Stern 
266486c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
266586c57edfSAlan Stern 	 * so try a reset-resume instead.
266686c57edfSAlan Stern 	 */
266786c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
266886c57edfSAlan Stern 		if (udev->persist_enabled)
266986c57edfSAlan Stern 			udev->reset_resume = 1;
267086c57edfSAlan Stern 		else
2671b01b03f3SAlan Stern 			status = -ENODEV;
267286c57edfSAlan Stern 	}
2673b01b03f3SAlan Stern 
2674b01b03f3SAlan Stern 	if (status) {
2675b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
2676b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
2677b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
2678b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2679b01b03f3SAlan Stern 
2680b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2681b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2682b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2683b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2684b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2685b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2686b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2687b01b03f3SAlan Stern 	}
2688b01b03f3SAlan Stern 
2689b01b03f3SAlan Stern 	return status;
2690b01b03f3SAlan Stern }
2691b01b03f3SAlan Stern 
2692f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
2693f74631e3SSarah Sharp {
2694f74631e3SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2695f74631e3SSarah Sharp 
2696f74631e3SSarah Sharp 	/* Check if the roothub and device supports LTM. */
2697f74631e3SSarah Sharp 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2698f74631e3SSarah Sharp 			!usb_device_supports_ltm(udev))
2699f74631e3SSarah Sharp 		return 0;
2700f74631e3SSarah Sharp 
2701f74631e3SSarah Sharp 	/* Clear Feature LTM Enable can only be sent if the device is
2702f74631e3SSarah Sharp 	 * configured.
2703f74631e3SSarah Sharp 	 */
2704f74631e3SSarah Sharp 	if (!udev->actconfig)
2705f74631e3SSarah Sharp 		return 0;
2706f74631e3SSarah Sharp 
2707f74631e3SSarah Sharp 	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2708f74631e3SSarah Sharp 			USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2709f74631e3SSarah Sharp 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2710f74631e3SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
2711f74631e3SSarah Sharp }
2712f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
2713f74631e3SSarah Sharp 
2714f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev)
2715f74631e3SSarah Sharp {
2716f74631e3SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2717f74631e3SSarah Sharp 
2718f74631e3SSarah Sharp 	/* Check if the roothub and device supports LTM. */
2719f74631e3SSarah Sharp 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2720f74631e3SSarah Sharp 			!usb_device_supports_ltm(udev))
2721f74631e3SSarah Sharp 		return;
2722f74631e3SSarah Sharp 
2723f74631e3SSarah Sharp 	/* Set Feature LTM Enable can only be sent if the device is
2724f74631e3SSarah Sharp 	 * configured.
2725f74631e3SSarah Sharp 	 */
2726f74631e3SSarah Sharp 	if (!udev->actconfig)
2727f74631e3SSarah Sharp 		return;
2728f74631e3SSarah Sharp 
2729f74631e3SSarah Sharp 	usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2730f74631e3SSarah Sharp 			USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2731f74631e3SSarah Sharp 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2732f74631e3SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
2733f74631e3SSarah Sharp }
2734f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
2735f74631e3SSarah Sharp 
27361da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds /*
2739140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
2740624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
27415edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
27421da177e4SLinus Torvalds  *
27431da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
27441da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
27451da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
2746140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
27471da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
27481da177e4SLinus Torvalds  *
2749390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
2750390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
2751390a8c34SDavid Brownell  *
2752624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
2753624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
2754624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
2755624d6c07SAlan Stern  *
2756624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
2757624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
2758624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
2759624d6c07SAlan Stern  * some cases, this wakes the USB host.
2760624d6c07SAlan Stern  *
27611da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
27621da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
27631da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
27641da177e4SLinus Torvalds  *
27654956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
27664956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
27674956eccdSAlan Stern  * State transitions include:
27684956eccdSAlan Stern  *
27694956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
27704956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
27714956eccdSAlan Stern  *
27724956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
27734956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
27744956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
27754956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
27764956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
27774956eccdSAlan Stern  *
27784956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
27794956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
27804956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
27814956eccdSAlan Stern  *
27821da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
27831da177e4SLinus Torvalds  */
278465bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
27851da177e4SLinus Torvalds {
2786624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2787624d6c07SAlan Stern 	int		port1 = udev->portnum;
2788624d6c07SAlan Stern 	int		status;
27894956eccdSAlan Stern 
2790624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
2791624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
2792624d6c07SAlan Stern 	 *
2793624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2794624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2795624d6c07SAlan Stern 	 */
2796624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2797623bef9eSSarah Sharp 		if (!hub_is_superspeed(hub->hdev)) {
2798624d6c07SAlan Stern 			status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2799624d6c07SAlan Stern 					USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2800624d6c07SAlan Stern 					USB_DEVICE_REMOTE_WAKEUP, 0,
2801624d6c07SAlan Stern 					NULL, 0,
2802624d6c07SAlan Stern 					USB_CTRL_SET_TIMEOUT);
2803623bef9eSSarah Sharp 		} else {
2804623bef9eSSarah Sharp 			/* Assume there's only one function on the USB 3.0
2805623bef9eSSarah Sharp 			 * device and enable remote wake for the first
2806623bef9eSSarah Sharp 			 * interface. FIXME if the interface association
2807623bef9eSSarah Sharp 			 * descriptor shows there's more than one function.
2808623bef9eSSarah Sharp 			 */
2809623bef9eSSarah Sharp 			status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2810623bef9eSSarah Sharp 					USB_REQ_SET_FEATURE,
2811623bef9eSSarah Sharp 					USB_RECIP_INTERFACE,
2812623bef9eSSarah Sharp 					USB_INTRF_FUNC_SUSPEND,
28133b9b6acdSSarah Sharp 					USB_INTRF_FUNC_SUSPEND_RW |
28143b9b6acdSSarah Sharp 					USB_INTRF_FUNC_SUSPEND_LP,
2815623bef9eSSarah Sharp 					NULL, 0,
2816623bef9eSSarah Sharp 					USB_CTRL_SET_TIMEOUT);
2817623bef9eSSarah Sharp 		}
28180c487206SOliver Neukum 		if (status) {
2819624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2820624d6c07SAlan Stern 					status);
28210c487206SOliver Neukum 			/* bail if autosuspend is requested */
28225b1b0b81SAlan Stern 			if (PMSG_IS_AUTO(msg))
28230c487206SOliver Neukum 				return status;
28240c487206SOliver Neukum 		}
2825624d6c07SAlan Stern 	}
2826624d6c07SAlan Stern 
282765580b43SAndiry Xu 	/* disable USB2 hardware LPM */
282865580b43SAndiry Xu 	if (udev->usb2_hw_lpm_enabled == 1)
282965580b43SAndiry Xu 		usb_set_usb2_hardware_lpm(udev, 0);
283065580b43SAndiry Xu 
2831f74631e3SSarah Sharp 	if (usb_disable_ltm(udev)) {
2832f74631e3SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2833f74631e3SSarah Sharp 				__func__);
2834f74631e3SSarah Sharp 		return -ENOMEM;
2835f74631e3SSarah Sharp 	}
28368306095fSSarah Sharp 	if (usb_unlocked_disable_lpm(udev)) {
28378306095fSSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
28388306095fSSarah Sharp 				__func__);
28398306095fSSarah Sharp 		return -ENOMEM;
28408306095fSSarah Sharp 	}
28418306095fSSarah Sharp 
2842624d6c07SAlan Stern 	/* see 7.1.7.6 */
2843a7114230SAndiry Xu 	if (hub_is_superspeed(hub->hdev))
2844a7114230SAndiry Xu 		status = set_port_feature(hub->hdev,
2845a7114230SAndiry Xu 				port1 | (USB_SS_PORT_LS_U3 << 3),
2846a7114230SAndiry Xu 				USB_PORT_FEAT_LINK_STATE);
2847a8f08d86SAndiry Xu 	else
2848a8f08d86SAndiry Xu 		status = set_port_feature(hub->hdev, port1,
2849a8f08d86SAndiry Xu 						USB_PORT_FEAT_SUSPEND);
2850624d6c07SAlan Stern 	if (status) {
2851624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2852624d6c07SAlan Stern 				port1, status);
2853624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
28540c487206SOliver Neukum 		if (udev->do_remote_wakeup)
2855624d6c07SAlan Stern 			(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2856624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2857624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2858624d6c07SAlan Stern 				NULL, 0,
2859624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2860cbb33004SAlan Stern 
2861c3e751e4SAndiry Xu 		/* Try to enable USB2 hardware LPM again */
2862c3e751e4SAndiry Xu 		if (udev->usb2_hw_lpm_capable == 1)
2863c3e751e4SAndiry Xu 			usb_set_usb2_hardware_lpm(udev, 1);
2864c3e751e4SAndiry Xu 
2865f74631e3SSarah Sharp 		/* Try to enable USB3 LTM and LPM again */
2866f74631e3SSarah Sharp 		usb_enable_ltm(udev);
28678306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
28688306095fSSarah Sharp 
2869cbb33004SAlan Stern 		/* System sleep transitions should never fail */
28705b1b0b81SAlan Stern 		if (!PMSG_IS_AUTO(msg))
2871cbb33004SAlan Stern 			status = 0;
2872624d6c07SAlan Stern 	} else {
2873624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
287430b1a7a3SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
287530b1a7a3SAlan Stern 				(PMSG_IS_AUTO(msg) ? "auto-" : ""),
287630b1a7a3SAlan Stern 				udev->do_remote_wakeup);
2877624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2878624d6c07SAlan Stern 		msleep(10);
2879624d6c07SAlan Stern 	}
2880c08512c7SAlan Stern 	usb_mark_last_busy(hub->hdev);
28814956eccdSAlan Stern 	return status;
28821da177e4SLinus Torvalds }
2883f3f3253dSDavid Brownell 
28841da177e4SLinus Torvalds /*
2885390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2886390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
288754515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
28881da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
28891da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
28901da177e4SLinus Torvalds  * in the tree that's rooted at this device.
289154515fe5SAlan Stern  *
289254515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
289354515fe5SAlan Stern  * status check is done.
28941da177e4SLinus Torvalds  */
2895140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
28961da177e4SLinus Torvalds {
289754515fe5SAlan Stern 	int	status = 0;
28981da177e4SLinus Torvalds 	u16	devstatus;
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2901b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2902b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
29051da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
29061da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
29071da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
29081da177e4SLinus Torvalds 	 */
29091da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
29101da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
29111da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
29121da177e4SLinus Torvalds 
291354515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
291454515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
291554515fe5SAlan Stern 	 * operation is carried out here, after the port has been
291654515fe5SAlan Stern 	 * resumed.
291754515fe5SAlan Stern 	 */
291854515fe5SAlan Stern 	if (udev->reset_resume)
291986c57edfSAlan Stern  retry_reset_resume:
2920742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
292154515fe5SAlan Stern 
29221da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
29231da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
29241da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
29251da177e4SLinus Torvalds 	 */
292654515fe5SAlan Stern 	if (status == 0) {
292746dede46SAlan Stern 		devstatus = 0;
29281da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2929b40b7a90SAlan Stern 		if (status >= 0)
293046dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
293186c57edfSAlan Stern 
293286c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
293386c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
293486c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
293586c57edfSAlan Stern 			udev->reset_resume = 1;
293686c57edfSAlan Stern 			goto retry_reset_resume;
293786c57edfSAlan Stern 		}
293854515fe5SAlan Stern 	}
2939b40b7a90SAlan Stern 
2940624d6c07SAlan Stern 	if (status) {
2941624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
29421da177e4SLinus Torvalds 				status);
2943624d6c07SAlan Stern 	} else if (udev->actconfig) {
29441da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2945686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
29461da177e4SLinus Torvalds 			status = usb_control_msg(udev,
29471da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
29481da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
29491da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
29501da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
29511da177e4SLinus Torvalds 					NULL, 0,
29521da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2953a8e7c565SAlan Stern 			if (status)
2954b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2955b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
2956b9cef6c3SWu Fengguang 					status);
29574bf0ba86SAlan Stern 		}
29581da177e4SLinus Torvalds 		status = 0;
29591da177e4SLinus Torvalds 	}
29601da177e4SLinus Torvalds 	return status;
29611da177e4SLinus Torvalds }
29621da177e4SLinus Torvalds 
2963624d6c07SAlan Stern /*
2964624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
2965624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
2966624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
2967624d6c07SAlan Stern  *
2968624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
2969624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
2970624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
2971624d6c07SAlan Stern  * the host and the device is the same as it was when the device
2972624d6c07SAlan Stern  * suspended.
2973624d6c07SAlan Stern  *
2974feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
2975feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
297654515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
297754515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
297854515fe5SAlan Stern  *
297954515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
298054515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
298154515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
298254515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
298354515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
298454515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
298554515fe5SAlan Stern  * made to appear as if it had not disconnected.
298654515fe5SAlan Stern  *
2987742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
2988feccc30dSAlan Stern  * every effort to insure that the same device is present after the
298954515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
299054515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
299154515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
299254515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
299354515fe5SAlan Stern  * new card is corrupted and the system crashes.
299454515fe5SAlan Stern  *
2995624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
2996624d6c07SAlan Stern  */
299765bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
29981da177e4SLinus Torvalds {
2999624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
3000624d6c07SAlan Stern 	int		port1 = udev->portnum;
30011da177e4SLinus Torvalds 	int		status;
3002d25450c6SAlan Stern 	u16		portchange, portstatus;
3003d25450c6SAlan Stern 
3004d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
3005d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
30060ed9a57eSAndiry Xu 	if (status == 0 && !port_is_suspended(hub, portstatus))
3007d25450c6SAlan Stern 		goto SuspendCleared;
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
30101da177e4SLinus Torvalds 
3011d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
3012d5cbad4bSAlan Stern 
30131da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
3014a7114230SAndiry Xu 	if (hub_is_superspeed(hub->hdev))
3015a7114230SAndiry Xu 		status = set_port_feature(hub->hdev,
3016a7114230SAndiry Xu 				port1 | (USB_SS_PORT_LS_U0 << 3),
3017a7114230SAndiry Xu 				USB_PORT_FEAT_LINK_STATE);
3018a7114230SAndiry Xu 	else
30191da177e4SLinus Torvalds 		status = clear_port_feature(hub->hdev,
30201da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_SUSPEND);
30211da177e4SLinus Torvalds 	if (status) {
3022624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
30231da177e4SLinus Torvalds 				port1, status);
30241da177e4SLinus Torvalds 	} else {
30251da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
3026645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
30275b1b0b81SAlan Stern 				(PMSG_IS_AUTO(msg) ? "auto-" : ""));
30281da177e4SLinus Torvalds 		msleep(25);
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
30311da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
30321da177e4SLinus Torvalds 		 * sequence.
30331da177e4SLinus Torvalds 		 */
3034d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
303554515fe5SAlan Stern 
30361da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
30371da177e4SLinus Torvalds 		msleep(10);
30381da177e4SLinus Torvalds 	}
3039b01b03f3SAlan Stern 
3040b01b03f3SAlan Stern  SuspendCleared:
3041b01b03f3SAlan Stern 	if (status == 0) {
3042a7114230SAndiry Xu 		if (hub_is_superspeed(hub->hdev)) {
3043a7114230SAndiry Xu 			if (portchange & USB_PORT_STAT_C_LINK_STATE)
3044a7114230SAndiry Xu 				clear_port_feature(hub->hdev, port1,
3045a7114230SAndiry Xu 					USB_PORT_FEAT_C_PORT_LINK_STATE);
3046a7114230SAndiry Xu 		} else {
3047b01b03f3SAlan Stern 			if (portchange & USB_PORT_STAT_C_SUSPEND)
3048b01b03f3SAlan Stern 				clear_port_feature(hub->hdev, port1,
3049b01b03f3SAlan Stern 						USB_PORT_FEAT_C_SUSPEND);
30501da177e4SLinus Torvalds 		}
3051a7114230SAndiry Xu 	}
30521da177e4SLinus Torvalds 
3053d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
3054d5cbad4bSAlan Stern 
3055b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
3056b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
305754515fe5SAlan Stern 	if (status == 0)
305854515fe5SAlan Stern 		status = finish_port_resume(udev);
305954515fe5SAlan Stern 	if (status < 0) {
306054515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
306154515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
306265580b43SAndiry Xu 	} else  {
306365580b43SAndiry Xu 		/* Try to enable USB2 hardware LPM */
306465580b43SAndiry Xu 		if (udev->usb2_hw_lpm_capable == 1)
306565580b43SAndiry Xu 			usb_set_usb2_hardware_lpm(udev, 1);
30668306095fSSarah Sharp 
3067f74631e3SSarah Sharp 		/* Try to enable USB3 LTM and LPM */
3068f74631e3SSarah Sharp 		usb_enable_ltm(udev);
30698306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
307054515fe5SAlan Stern 	}
307165580b43SAndiry Xu 
30721da177e4SLinus Torvalds 	return status;
30731da177e4SLinus Torvalds }
30741da177e4SLinus Torvalds 
30758808f00cSAlan Stern /* caller has locked udev */
30760534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev)
30771da177e4SLinus Torvalds {
30781da177e4SLinus Torvalds 	int	status = 0;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
3081645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
30829bbdf1e0SAlan Stern 		status = usb_autoresume_device(udev);
30839bbdf1e0SAlan Stern 		if (status == 0) {
30849bbdf1e0SAlan Stern 			/* Let the drivers do their thing, then... */
30859bbdf1e0SAlan Stern 			usb_autosuspend_device(udev);
30869bbdf1e0SAlan Stern 		}
3087d25450c6SAlan Stern 	}
30881da177e4SLinus Torvalds 	return status;
30891da177e4SLinus Torvalds }
30901da177e4SLinus Torvalds 
3091d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
3092d388dab7SAlan Stern 
3093d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3094d388dab7SAlan Stern 
309565bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3096d388dab7SAlan Stern {
3097d388dab7SAlan Stern 	return 0;
3098d388dab7SAlan Stern }
3099d388dab7SAlan Stern 
3100b01b03f3SAlan Stern /* However we may need to do a reset-resume */
3101b01b03f3SAlan Stern 
310265bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3103d388dab7SAlan Stern {
3104b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
3105b01b03f3SAlan Stern 	int		port1 = udev->portnum;
3106b01b03f3SAlan Stern 	int		status;
3107b01b03f3SAlan Stern 	u16		portchange, portstatus;
310854515fe5SAlan Stern 
3109b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
3110b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
3111b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
3112b01b03f3SAlan Stern 
3113b01b03f3SAlan Stern 	if (status) {
3114b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3115b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
3116b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
311754515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
3118742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
311954515fe5SAlan Stern 	}
312054515fe5SAlan Stern 	return status;
3121d388dab7SAlan Stern }
3122d388dab7SAlan Stern 
3123d388dab7SAlan Stern #endif
3124d388dab7SAlan Stern 
3125db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
31261da177e4SLinus Torvalds {
31271da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
31281da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
31291da177e4SLinus Torvalds 	unsigned		port1;
31304296c70aSSarah Sharp 	int			status;
31311da177e4SLinus Torvalds 
3132cbb33004SAlan Stern 	/* Warn if children aren't already suspended */
31331da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
31341da177e4SLinus Torvalds 		struct usb_device	*udev;
31351da177e4SLinus Torvalds 
3136ff823c79SLan Tianyu 		udev = hub->ports[port1 - 1]->child;
31376840d255SAlan Stern 		if (udev && udev->can_submit) {
3138cbb33004SAlan Stern 			dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
31395b1b0b81SAlan Stern 			if (PMSG_IS_AUTO(msg))
3140c9f89fa4SDavid Brownell 				return -EBUSY;
3141c9f89fa4SDavid Brownell 		}
31421da177e4SLinus Torvalds 	}
31434296c70aSSarah Sharp 	if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
31444296c70aSSarah Sharp 		/* Enable hub to send remote wakeup for all ports. */
31454296c70aSSarah Sharp 		for (port1 = 1; port1 <= hdev->maxchild; port1++) {
31464296c70aSSarah Sharp 			status = set_port_feature(hdev,
31474296c70aSSarah Sharp 					port1 |
31484296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
31494296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
31504296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
31514296c70aSSarah Sharp 					USB_PORT_FEAT_REMOTE_WAKE_MASK);
31524296c70aSSarah Sharp 		}
31534296c70aSSarah Sharp 	}
31541da177e4SLinus Torvalds 
3155441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
315640f122f3SAlan Stern 
3157c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
31584330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
3159b6f6436dSAlan Stern 	return 0;
31601da177e4SLinus Torvalds }
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
31631da177e4SLinus Torvalds {
31641da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
31651da177e4SLinus Torvalds 
31665e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
3167f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
31681da177e4SLinus Torvalds 	return 0;
31691da177e4SLinus Torvalds }
31701da177e4SLinus Torvalds 
3171b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
3172f07600cfSAlan Stern {
3173b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
3174f07600cfSAlan Stern 
31755e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
3176f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
3177f07600cfSAlan Stern 	return 0;
3178f07600cfSAlan Stern }
3179f07600cfSAlan Stern 
318054515fe5SAlan Stern /**
318154515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
318254515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
318354515fe5SAlan Stern  *
318454515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
318554515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
3186feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
3187feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
3188feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3189feccc30dSAlan Stern  * the others will be disconnected.
319054515fe5SAlan Stern  */
319154515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
319254515fe5SAlan Stern {
319354515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
319454515fe5SAlan Stern 	rhdev->reset_resume = 1;
319554515fe5SAlan Stern }
319654515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
319754515fe5SAlan Stern 
31981ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[]  = {
31991ea7e0e8SSarah Sharp 	"U0",
32001ea7e0e8SSarah Sharp 	"U1",
32011ea7e0e8SSarah Sharp 	"U2",
32021ea7e0e8SSarah Sharp 	"U3",
32031ea7e0e8SSarah Sharp };
32041ea7e0e8SSarah Sharp 
32051ea7e0e8SSarah Sharp /*
32061ea7e0e8SSarah Sharp  * Send a Set SEL control transfer to the device, prior to enabling
32071ea7e0e8SSarah Sharp  * device-initiated U1 or U2.  This lets the device know the exit latencies from
32081ea7e0e8SSarah Sharp  * the time the device initiates a U1 or U2 exit, to the time it will receive a
32091ea7e0e8SSarah Sharp  * packet from the host.
32101ea7e0e8SSarah Sharp  *
32111ea7e0e8SSarah Sharp  * This function will fail if the SEL or PEL values for udev are greater than
32121ea7e0e8SSarah Sharp  * the maximum allowed values for the link state to be enabled.
32131ea7e0e8SSarah Sharp  */
32141ea7e0e8SSarah Sharp static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
32151ea7e0e8SSarah Sharp {
32161ea7e0e8SSarah Sharp 	struct usb_set_sel_req *sel_values;
32171ea7e0e8SSarah Sharp 	unsigned long long u1_sel;
32181ea7e0e8SSarah Sharp 	unsigned long long u1_pel;
32191ea7e0e8SSarah Sharp 	unsigned long long u2_sel;
32201ea7e0e8SSarah Sharp 	unsigned long long u2_pel;
32211ea7e0e8SSarah Sharp 	int ret;
32221ea7e0e8SSarah Sharp 
32231ea7e0e8SSarah Sharp 	/* Convert SEL and PEL stored in ns to us */
32241ea7e0e8SSarah Sharp 	u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
32251ea7e0e8SSarah Sharp 	u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
32261ea7e0e8SSarah Sharp 	u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
32271ea7e0e8SSarah Sharp 	u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
32281ea7e0e8SSarah Sharp 
32291ea7e0e8SSarah Sharp 	/*
32301ea7e0e8SSarah Sharp 	 * Make sure that the calculated SEL and PEL values for the link
32311ea7e0e8SSarah Sharp 	 * state we're enabling aren't bigger than the max SEL/PEL
32321ea7e0e8SSarah Sharp 	 * value that will fit in the SET SEL control transfer.
32331ea7e0e8SSarah Sharp 	 * Otherwise the device would get an incorrect idea of the exit
32341ea7e0e8SSarah Sharp 	 * latency for the link state, and could start a device-initiated
32351ea7e0e8SSarah Sharp 	 * U1/U2 when the exit latencies are too high.
32361ea7e0e8SSarah Sharp 	 */
32371ea7e0e8SSarah Sharp 	if ((state == USB3_LPM_U1 &&
32381ea7e0e8SSarah Sharp 				(u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
32391ea7e0e8SSarah Sharp 				 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
32401ea7e0e8SSarah Sharp 			(state == USB3_LPM_U2 &&
32411ea7e0e8SSarah Sharp 			 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
32421ea7e0e8SSarah Sharp 			  u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
32431ea7e0e8SSarah Sharp 		dev_dbg(&udev->dev, "Device-initiated %s disabled due "
32441ea7e0e8SSarah Sharp 				"to long SEL %llu ms or PEL %llu ms\n",
32451ea7e0e8SSarah Sharp 				usb3_lpm_names[state], u1_sel, u1_pel);
32461ea7e0e8SSarah Sharp 		return -EINVAL;
32471ea7e0e8SSarah Sharp 	}
32481ea7e0e8SSarah Sharp 
32491ea7e0e8SSarah Sharp 	/*
32501ea7e0e8SSarah Sharp 	 * If we're enabling device-initiated LPM for one link state,
32511ea7e0e8SSarah Sharp 	 * but the other link state has a too high SEL or PEL value,
32521ea7e0e8SSarah Sharp 	 * just set those values to the max in the Set SEL request.
32531ea7e0e8SSarah Sharp 	 */
32541ea7e0e8SSarah Sharp 	if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
32551ea7e0e8SSarah Sharp 		u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
32561ea7e0e8SSarah Sharp 
32571ea7e0e8SSarah Sharp 	if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
32581ea7e0e8SSarah Sharp 		u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
32591ea7e0e8SSarah Sharp 
32601ea7e0e8SSarah Sharp 	if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
32611ea7e0e8SSarah Sharp 		u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
32621ea7e0e8SSarah Sharp 
32631ea7e0e8SSarah Sharp 	if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
32641ea7e0e8SSarah Sharp 		u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
32651ea7e0e8SSarah Sharp 
32661ea7e0e8SSarah Sharp 	/*
32671ea7e0e8SSarah Sharp 	 * usb_enable_lpm() can be called as part of a failed device reset,
32681ea7e0e8SSarah Sharp 	 * which may be initiated by an error path of a mass storage driver.
32691ea7e0e8SSarah Sharp 	 * Therefore, use GFP_NOIO.
32701ea7e0e8SSarah Sharp 	 */
32711ea7e0e8SSarah Sharp 	sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
32721ea7e0e8SSarah Sharp 	if (!sel_values)
32731ea7e0e8SSarah Sharp 		return -ENOMEM;
32741ea7e0e8SSarah Sharp 
32751ea7e0e8SSarah Sharp 	sel_values->u1_sel = u1_sel;
32761ea7e0e8SSarah Sharp 	sel_values->u1_pel = u1_pel;
32771ea7e0e8SSarah Sharp 	sel_values->u2_sel = cpu_to_le16(u2_sel);
32781ea7e0e8SSarah Sharp 	sel_values->u2_pel = cpu_to_le16(u2_pel);
32791ea7e0e8SSarah Sharp 
32801ea7e0e8SSarah Sharp 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
32811ea7e0e8SSarah Sharp 			USB_REQ_SET_SEL,
32821ea7e0e8SSarah Sharp 			USB_RECIP_DEVICE,
32831ea7e0e8SSarah Sharp 			0, 0,
32841ea7e0e8SSarah Sharp 			sel_values, sizeof *(sel_values),
32851ea7e0e8SSarah Sharp 			USB_CTRL_SET_TIMEOUT);
32861ea7e0e8SSarah Sharp 	kfree(sel_values);
32871ea7e0e8SSarah Sharp 	return ret;
32881ea7e0e8SSarah Sharp }
32891ea7e0e8SSarah Sharp 
32901ea7e0e8SSarah Sharp /*
32911ea7e0e8SSarah Sharp  * Enable or disable device-initiated U1 or U2 transitions.
32921ea7e0e8SSarah Sharp  */
32931ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev,
32941ea7e0e8SSarah Sharp 		enum usb3_link_state state, bool enable)
32951ea7e0e8SSarah Sharp {
32961ea7e0e8SSarah Sharp 	int ret;
32971ea7e0e8SSarah Sharp 	int feature;
32981ea7e0e8SSarah Sharp 
32991ea7e0e8SSarah Sharp 	switch (state) {
33001ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
33011ea7e0e8SSarah Sharp 		feature = USB_DEVICE_U1_ENABLE;
33021ea7e0e8SSarah Sharp 		break;
33031ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
33041ea7e0e8SSarah Sharp 		feature = USB_DEVICE_U2_ENABLE;
33051ea7e0e8SSarah Sharp 		break;
33061ea7e0e8SSarah Sharp 	default:
33071ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
33081ea7e0e8SSarah Sharp 				__func__, enable ? "enable" : "disable");
33091ea7e0e8SSarah Sharp 		return -EINVAL;
33101ea7e0e8SSarah Sharp 	}
33111ea7e0e8SSarah Sharp 
33121ea7e0e8SSarah Sharp 	if (udev->state != USB_STATE_CONFIGURED) {
33131ea7e0e8SSarah Sharp 		dev_dbg(&udev->dev, "%s: Can't %s %s state "
33141ea7e0e8SSarah Sharp 				"for unconfigured device.\n",
33151ea7e0e8SSarah Sharp 				__func__, enable ? "enable" : "disable",
33161ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
33171ea7e0e8SSarah Sharp 		return 0;
33181ea7e0e8SSarah Sharp 	}
33191ea7e0e8SSarah Sharp 
33201ea7e0e8SSarah Sharp 	if (enable) {
33211ea7e0e8SSarah Sharp 		/*
33221ea7e0e8SSarah Sharp 		 * First, let the device know about the exit latencies
33231ea7e0e8SSarah Sharp 		 * associated with the link state we're about to enable.
33241ea7e0e8SSarah Sharp 		 */
33251ea7e0e8SSarah Sharp 		ret = usb_req_set_sel(udev, state);
33261ea7e0e8SSarah Sharp 		if (ret < 0) {
33271ea7e0e8SSarah Sharp 			dev_warn(&udev->dev, "Set SEL for device-initiated "
33281ea7e0e8SSarah Sharp 					"%s failed.\n", usb3_lpm_names[state]);
33291ea7e0e8SSarah Sharp 			return -EBUSY;
33301ea7e0e8SSarah Sharp 		}
33311ea7e0e8SSarah Sharp 		/*
33321ea7e0e8SSarah Sharp 		 * Now send the control transfer to enable device-initiated LPM
33331ea7e0e8SSarah Sharp 		 * for either U1 or U2.
33341ea7e0e8SSarah Sharp 		 */
33351ea7e0e8SSarah Sharp 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33361ea7e0e8SSarah Sharp 				USB_REQ_SET_FEATURE,
33371ea7e0e8SSarah Sharp 				USB_RECIP_DEVICE,
33381ea7e0e8SSarah Sharp 				feature,
33391ea7e0e8SSarah Sharp 				0, NULL, 0,
33401ea7e0e8SSarah Sharp 				USB_CTRL_SET_TIMEOUT);
33411ea7e0e8SSarah Sharp 	} else {
33421ea7e0e8SSarah Sharp 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
33431ea7e0e8SSarah Sharp 				USB_REQ_CLEAR_FEATURE,
33441ea7e0e8SSarah Sharp 				USB_RECIP_DEVICE,
33451ea7e0e8SSarah Sharp 				feature,
33461ea7e0e8SSarah Sharp 				0, NULL, 0,
33471ea7e0e8SSarah Sharp 				USB_CTRL_SET_TIMEOUT);
33481ea7e0e8SSarah Sharp 	}
33491ea7e0e8SSarah Sharp 	if (ret < 0) {
33501ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
33511ea7e0e8SSarah Sharp 				enable ? "Enable" : "Disable",
33521ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
33531ea7e0e8SSarah Sharp 		return -EBUSY;
33541ea7e0e8SSarah Sharp 	}
33551ea7e0e8SSarah Sharp 	return 0;
33561ea7e0e8SSarah Sharp }
33571ea7e0e8SSarah Sharp 
33581ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev,
33591ea7e0e8SSarah Sharp 		enum usb3_link_state state, int timeout)
33601ea7e0e8SSarah Sharp {
33611ea7e0e8SSarah Sharp 	int ret;
33621ea7e0e8SSarah Sharp 	int feature;
33631ea7e0e8SSarah Sharp 
33641ea7e0e8SSarah Sharp 	switch (state) {
33651ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
33661ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U1_TIMEOUT;
33671ea7e0e8SSarah Sharp 		break;
33681ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
33691ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U2_TIMEOUT;
33701ea7e0e8SSarah Sharp 		break;
33711ea7e0e8SSarah Sharp 	default:
33721ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
33731ea7e0e8SSarah Sharp 				__func__);
33741ea7e0e8SSarah Sharp 		return -EINVAL;
33751ea7e0e8SSarah Sharp 	}
33761ea7e0e8SSarah Sharp 
33771ea7e0e8SSarah Sharp 	if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
33781ea7e0e8SSarah Sharp 			timeout != USB3_LPM_DEVICE_INITIATED) {
33791ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
33801ea7e0e8SSarah Sharp 				"which is a reserved value.\n",
33811ea7e0e8SSarah Sharp 				usb3_lpm_names[state], timeout);
33821ea7e0e8SSarah Sharp 		return -EINVAL;
33831ea7e0e8SSarah Sharp 	}
33841ea7e0e8SSarah Sharp 
33851ea7e0e8SSarah Sharp 	ret = set_port_feature(udev->parent,
33861ea7e0e8SSarah Sharp 			USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
33871ea7e0e8SSarah Sharp 			feature);
33881ea7e0e8SSarah Sharp 	if (ret < 0) {
33891ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
33901ea7e0e8SSarah Sharp 				"error code %i\n", usb3_lpm_names[state],
33911ea7e0e8SSarah Sharp 				timeout, ret);
33921ea7e0e8SSarah Sharp 		return -EBUSY;
33931ea7e0e8SSarah Sharp 	}
33941ea7e0e8SSarah Sharp 	if (state == USB3_LPM_U1)
33951ea7e0e8SSarah Sharp 		udev->u1_params.timeout = timeout;
33961ea7e0e8SSarah Sharp 	else
33971ea7e0e8SSarah Sharp 		udev->u2_params.timeout = timeout;
33981ea7e0e8SSarah Sharp 	return 0;
33991ea7e0e8SSarah Sharp }
34001ea7e0e8SSarah Sharp 
34011ea7e0e8SSarah Sharp /*
34021ea7e0e8SSarah Sharp  * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
34031ea7e0e8SSarah Sharp  * U1/U2 entry.
34041ea7e0e8SSarah Sharp  *
34051ea7e0e8SSarah Sharp  * We will attempt to enable U1 or U2, but there are no guarantees that the
34061ea7e0e8SSarah Sharp  * control transfers to set the hub timeout or enable device-initiated U1/U2
34071ea7e0e8SSarah Sharp  * will be successful.
34081ea7e0e8SSarah Sharp  *
34091ea7e0e8SSarah Sharp  * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
34101ea7e0e8SSarah Sharp  * driver know about it.  If that call fails, it should be harmless, and just
34111ea7e0e8SSarah Sharp  * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
34121ea7e0e8SSarah Sharp  */
34131ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
34141ea7e0e8SSarah Sharp 		enum usb3_link_state state)
34151ea7e0e8SSarah Sharp {
34161ea7e0e8SSarah Sharp 	int timeout;
34171ea7e0e8SSarah Sharp 
34181ea7e0e8SSarah Sharp 	/* We allow the host controller to set the U1/U2 timeout internally
34191ea7e0e8SSarah Sharp 	 * first, so that it can change its schedule to account for the
34201ea7e0e8SSarah Sharp 	 * additional latency to send data to a device in a lower power
34211ea7e0e8SSarah Sharp 	 * link state.
34221ea7e0e8SSarah Sharp 	 */
34231ea7e0e8SSarah Sharp 	timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
34241ea7e0e8SSarah Sharp 
34251ea7e0e8SSarah Sharp 	/* xHCI host controller doesn't want to enable this LPM state. */
34261ea7e0e8SSarah Sharp 	if (timeout == 0)
34271ea7e0e8SSarah Sharp 		return;
34281ea7e0e8SSarah Sharp 
34291ea7e0e8SSarah Sharp 	if (timeout < 0) {
34301ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Could not enable %s link state, "
34311ea7e0e8SSarah Sharp 				"xHCI error %i.\n", usb3_lpm_names[state],
34321ea7e0e8SSarah Sharp 				timeout);
34331ea7e0e8SSarah Sharp 		return;
34341ea7e0e8SSarah Sharp 	}
34351ea7e0e8SSarah Sharp 
34361ea7e0e8SSarah Sharp 	if (usb_set_lpm_timeout(udev, state, timeout))
34371ea7e0e8SSarah Sharp 		/* If we can't set the parent hub U1/U2 timeout,
34381ea7e0e8SSarah Sharp 		 * device-initiated LPM won't be allowed either, so let the xHCI
34391ea7e0e8SSarah Sharp 		 * host know that this link state won't be enabled.
34401ea7e0e8SSarah Sharp 		 */
34411ea7e0e8SSarah Sharp 		hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
34421ea7e0e8SSarah Sharp 
34431ea7e0e8SSarah Sharp 	/* Only a configured device will accept the Set Feature U1/U2_ENABLE */
34441ea7e0e8SSarah Sharp 	else if (udev->actconfig)
34451ea7e0e8SSarah Sharp 		usb_set_device_initiated_lpm(udev, state, true);
34461ea7e0e8SSarah Sharp 
34471ea7e0e8SSarah Sharp }
34481ea7e0e8SSarah Sharp 
34491ea7e0e8SSarah Sharp /*
34501ea7e0e8SSarah Sharp  * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
34511ea7e0e8SSarah Sharp  * U1/U2 entry.
34521ea7e0e8SSarah Sharp  *
34531ea7e0e8SSarah Sharp  * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
34541ea7e0e8SSarah Sharp  * If zero is returned, the parent will not allow the link to go into U1/U2.
34551ea7e0e8SSarah Sharp  *
34561ea7e0e8SSarah Sharp  * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
34571ea7e0e8SSarah Sharp  * it won't have an effect on the bus link state because the parent hub will
34581ea7e0e8SSarah Sharp  * still disallow device-initiated U1/U2 entry.
34591ea7e0e8SSarah Sharp  *
34601ea7e0e8SSarah Sharp  * If zero is returned, the xHCI host controller may still think U1/U2 entry is
34611ea7e0e8SSarah Sharp  * possible.  The result will be slightly more bus bandwidth will be taken up
34621ea7e0e8SSarah Sharp  * (to account for U1/U2 exit latency), but it should be harmless.
34631ea7e0e8SSarah Sharp  */
34641ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
34651ea7e0e8SSarah Sharp 		enum usb3_link_state state)
34661ea7e0e8SSarah Sharp {
34671ea7e0e8SSarah Sharp 	int feature;
34681ea7e0e8SSarah Sharp 
34691ea7e0e8SSarah Sharp 	switch (state) {
34701ea7e0e8SSarah Sharp 	case USB3_LPM_U1:
34711ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U1_TIMEOUT;
34721ea7e0e8SSarah Sharp 		break;
34731ea7e0e8SSarah Sharp 	case USB3_LPM_U2:
34741ea7e0e8SSarah Sharp 		feature = USB_PORT_FEAT_U2_TIMEOUT;
34751ea7e0e8SSarah Sharp 		break;
34761ea7e0e8SSarah Sharp 	default:
34771ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
34781ea7e0e8SSarah Sharp 				__func__);
34791ea7e0e8SSarah Sharp 		return -EINVAL;
34801ea7e0e8SSarah Sharp 	}
34811ea7e0e8SSarah Sharp 
34821ea7e0e8SSarah Sharp 	if (usb_set_lpm_timeout(udev, state, 0))
34831ea7e0e8SSarah Sharp 		return -EBUSY;
34841ea7e0e8SSarah Sharp 
34851ea7e0e8SSarah Sharp 	usb_set_device_initiated_lpm(udev, state, false);
34861ea7e0e8SSarah Sharp 
34871ea7e0e8SSarah Sharp 	if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
34881ea7e0e8SSarah Sharp 		dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
34891ea7e0e8SSarah Sharp 				"bus schedule bandwidth may be impacted.\n",
34901ea7e0e8SSarah Sharp 				usb3_lpm_names[state]);
34911ea7e0e8SSarah Sharp 	return 0;
34921ea7e0e8SSarah Sharp }
34931ea7e0e8SSarah Sharp 
34941ea7e0e8SSarah Sharp /*
34951ea7e0e8SSarah Sharp  * Disable hub-initiated and device-initiated U1 and U2 entry.
34961ea7e0e8SSarah Sharp  * Caller must own the bandwidth_mutex.
34971ea7e0e8SSarah Sharp  *
34981ea7e0e8SSarah Sharp  * This will call usb_enable_lpm() on failure, which will decrement
34991ea7e0e8SSarah Sharp  * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
35001ea7e0e8SSarah Sharp  */
35011ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
35021ea7e0e8SSarah Sharp {
35031ea7e0e8SSarah Sharp 	struct usb_hcd *hcd;
35041ea7e0e8SSarah Sharp 
35051ea7e0e8SSarah Sharp 	if (!udev || !udev->parent ||
35061ea7e0e8SSarah Sharp 			udev->speed != USB_SPEED_SUPER ||
35071ea7e0e8SSarah Sharp 			!udev->lpm_capable)
35081ea7e0e8SSarah Sharp 		return 0;
35091ea7e0e8SSarah Sharp 
35101ea7e0e8SSarah Sharp 	hcd = bus_to_hcd(udev->bus);
35111ea7e0e8SSarah Sharp 	if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
35121ea7e0e8SSarah Sharp 		return 0;
35131ea7e0e8SSarah Sharp 
35141ea7e0e8SSarah Sharp 	udev->lpm_disable_count++;
351555558c33SDan Carpenter 	if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
35161ea7e0e8SSarah Sharp 		return 0;
35171ea7e0e8SSarah Sharp 
35181ea7e0e8SSarah Sharp 	/* If LPM is enabled, attempt to disable it. */
35191ea7e0e8SSarah Sharp 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
35201ea7e0e8SSarah Sharp 		goto enable_lpm;
35211ea7e0e8SSarah Sharp 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
35221ea7e0e8SSarah Sharp 		goto enable_lpm;
35231ea7e0e8SSarah Sharp 
35241ea7e0e8SSarah Sharp 	return 0;
35251ea7e0e8SSarah Sharp 
35261ea7e0e8SSarah Sharp enable_lpm:
35271ea7e0e8SSarah Sharp 	usb_enable_lpm(udev);
35281ea7e0e8SSarah Sharp 	return -EBUSY;
35291ea7e0e8SSarah Sharp }
35301ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
35311ea7e0e8SSarah Sharp 
35321ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
35331ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
35341ea7e0e8SSarah Sharp {
35351ea7e0e8SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
35361ea7e0e8SSarah Sharp 	int ret;
35371ea7e0e8SSarah Sharp 
35381ea7e0e8SSarah Sharp 	if (!hcd)
35391ea7e0e8SSarah Sharp 		return -EINVAL;
35401ea7e0e8SSarah Sharp 
35411ea7e0e8SSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
35421ea7e0e8SSarah Sharp 	ret = usb_disable_lpm(udev);
35431ea7e0e8SSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
35441ea7e0e8SSarah Sharp 
35451ea7e0e8SSarah Sharp 	return ret;
35461ea7e0e8SSarah Sharp }
35471ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
35481ea7e0e8SSarah Sharp 
35491ea7e0e8SSarah Sharp /*
35501ea7e0e8SSarah Sharp  * Attempt to enable device-initiated and hub-initiated U1 and U2 entry.  The
35511ea7e0e8SSarah Sharp  * xHCI host policy may prevent U1 or U2 from being enabled.
35521ea7e0e8SSarah Sharp  *
35531ea7e0e8SSarah Sharp  * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
35541ea7e0e8SSarah Sharp  * until the lpm_disable_count drops to zero.  Caller must own the
35551ea7e0e8SSarah Sharp  * bandwidth_mutex.
35561ea7e0e8SSarah Sharp  */
35571ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev)
35581ea7e0e8SSarah Sharp {
35591ea7e0e8SSarah Sharp 	struct usb_hcd *hcd;
35601ea7e0e8SSarah Sharp 
35611ea7e0e8SSarah Sharp 	if (!udev || !udev->parent ||
35621ea7e0e8SSarah Sharp 			udev->speed != USB_SPEED_SUPER ||
35631ea7e0e8SSarah Sharp 			!udev->lpm_capable)
35641ea7e0e8SSarah Sharp 		return;
35651ea7e0e8SSarah Sharp 
35661ea7e0e8SSarah Sharp 	udev->lpm_disable_count--;
35671ea7e0e8SSarah Sharp 	hcd = bus_to_hcd(udev->bus);
35681ea7e0e8SSarah Sharp 	/* Double check that we can both enable and disable LPM.
35691ea7e0e8SSarah Sharp 	 * Device must be configured to accept set feature U1/U2 timeout.
35701ea7e0e8SSarah Sharp 	 */
35711ea7e0e8SSarah Sharp 	if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
35721ea7e0e8SSarah Sharp 			!hcd->driver->disable_usb3_lpm_timeout)
35731ea7e0e8SSarah Sharp 		return;
35741ea7e0e8SSarah Sharp 
35751ea7e0e8SSarah Sharp 	if (udev->lpm_disable_count > 0)
35761ea7e0e8SSarah Sharp 		return;
35771ea7e0e8SSarah Sharp 
35781ea7e0e8SSarah Sharp 	usb_enable_link_state(hcd, udev, USB3_LPM_U1);
35791ea7e0e8SSarah Sharp 	usb_enable_link_state(hcd, udev, USB3_LPM_U2);
35801ea7e0e8SSarah Sharp }
35811ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
35821ea7e0e8SSarah Sharp 
35831ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
35841ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev)
35851ea7e0e8SSarah Sharp {
35861ea7e0e8SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
35871ea7e0e8SSarah Sharp 
35881ea7e0e8SSarah Sharp 	if (!hcd)
35891ea7e0e8SSarah Sharp 		return;
35901ea7e0e8SSarah Sharp 
35911ea7e0e8SSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
35921ea7e0e8SSarah Sharp 	usb_enable_lpm(udev);
35931ea7e0e8SSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
35941ea7e0e8SSarah Sharp }
35951ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
35961ea7e0e8SSarah Sharp 
35971ea7e0e8SSarah Sharp 
3598d388dab7SAlan Stern #else	/* CONFIG_PM */
3599d388dab7SAlan Stern 
3600511366daSAndrew Morton #define hub_suspend		NULL
3601511366daSAndrew Morton #define hub_resume		NULL
3602f07600cfSAlan Stern #define hub_reset_resume	NULL
36031ea7e0e8SSarah Sharp 
36041ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev)
36051ea7e0e8SSarah Sharp {
36061ea7e0e8SSarah Sharp 	return 0;
36071ea7e0e8SSarah Sharp }
3608e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm);
36091ea7e0e8SSarah Sharp 
36101ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { }
3611e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm);
36121ea7e0e8SSarah Sharp 
36131ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev)
36141ea7e0e8SSarah Sharp {
36151ea7e0e8SSarah Sharp 	return 0;
36161ea7e0e8SSarah Sharp }
3617e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
36181ea7e0e8SSarah Sharp 
36191ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { }
3620e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3621f74631e3SSarah Sharp 
3622f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev)
3623f74631e3SSarah Sharp {
3624f74631e3SSarah Sharp 	return 0;
3625f74631e3SSarah Sharp }
3626f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm);
3627f74631e3SSarah Sharp 
3628f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) { }
3629f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm);
3630d388dab7SAlan Stern #endif
3631d388dab7SAlan Stern 
36321da177e4SLinus Torvalds 
36331da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
36341da177e4SLinus Torvalds  *
36351da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
36361da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
36371da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
36381da177e4SLinus Torvalds  *
36391da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
36401da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
36411da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
36421da177e4SLinus Torvalds  *
36431da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
36441da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
36451da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
36461da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
36471da177e4SLinus Torvalds  */
36481da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
36491da177e4SLinus Torvalds {
36501da177e4SLinus Torvalds 	int ret;
36511da177e4SLinus Torvalds 	int total_time, stable_time = 0;
36521da177e4SLinus Torvalds 	u16 portchange, portstatus;
36531da177e4SLinus Torvalds 	unsigned connection = 0xffff;
36541da177e4SLinus Torvalds 
36551da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
36561da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
36571da177e4SLinus Torvalds 		if (ret < 0)
36581da177e4SLinus Torvalds 			return ret;
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
36611da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
36621da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
36631da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
36641da177e4SLinus Torvalds 				break;
36651da177e4SLinus Torvalds 		} else {
36661da177e4SLinus Torvalds 			stable_time = 0;
36671da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
36681da177e4SLinus Torvalds 		}
36691da177e4SLinus Torvalds 
36701da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
36711da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
36721da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
36731da177e4SLinus Torvalds 		}
36741da177e4SLinus Torvalds 
36751da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
36761da177e4SLinus Torvalds 			break;
36771da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
36781da177e4SLinus Torvalds 	}
36791da177e4SLinus Torvalds 
36801da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
36811da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
36821da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
36851da177e4SLinus Torvalds 		return -ETIMEDOUT;
36861da177e4SLinus Torvalds 	return portstatus;
36871da177e4SLinus Torvalds }
36881da177e4SLinus Torvalds 
3689fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
36901da177e4SLinus Torvalds {
3691ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3692ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
36932caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
36941da177e4SLinus Torvalds }
3695fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
36961da177e4SLinus Torvalds 
36971da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
36981da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
36991da177e4SLinus Torvalds 
37004326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
37011da177e4SLinus Torvalds {
37021da177e4SLinus Torvalds 	int retval;
3703c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
37041da177e4SLinus Torvalds 
3705c6515272SSarah Sharp 	/*
3706c6515272SSarah Sharp 	 * The host controller will choose the device address,
3707c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
3708c6515272SSarah Sharp 	 */
3709c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
37101da177e4SLinus Torvalds 		return -EINVAL;
37111da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
37121da177e4SLinus Torvalds 		return 0;
37131da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
37141da177e4SLinus Torvalds 		return -EINVAL;
3715c8d4af8eSAndiry Xu 	if (hcd->driver->address_device)
3716c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
3717c8d4af8eSAndiry Xu 	else
37181da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
37194326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
37201da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
37211da177e4SLinus Torvalds 	if (retval == 0) {
37223b29b68bSAlan Stern 		update_devnum(udev, devnum);
37234953d141SDavid Vrabel 		/* Device now using proper address. */
37241da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
3725fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
37261da177e4SLinus Torvalds 	}
37271da177e4SLinus Torvalds 	return retval;
37281da177e4SLinus Torvalds }
37291da177e4SLinus Torvalds 
37301da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
37311da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
37321da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
37331da177e4SLinus Torvalds  *
37341da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
3735742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
37361da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
37371da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
37381da177e4SLinus Torvalds  */
37391da177e4SLinus Torvalds static int
37401da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
37411da177e4SLinus Torvalds 		int retry_counter)
37421da177e4SLinus Torvalds {
37434186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
37441da177e4SLinus Torvalds 
37451da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
3746e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
37471da177e4SLinus Torvalds 	int			i, j, retval;
37481da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
37491da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
3750e538dfdaSMichal Nazarewicz 	const char		*speed;
37514326ed0bSAlan Stern 	int			devnum = udev->devnum;
37521da177e4SLinus Torvalds 
37531da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
37541da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
37551da177e4SLinus Torvalds 	 */
37561da177e4SLinus Torvalds 	if (!hdev->parent) {
37571da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
37581da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
37591da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
37601da177e4SLinus Torvalds 	}
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
37631da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
37641da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
37651da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
37661da177e4SLinus Torvalds 
37674186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
37681da177e4SLinus Torvalds 
37691da177e4SLinus Torvalds 	/* Reset the device; full speed may morph to high speed */
3770e7b77172SSarah Sharp 	/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
377175d7cf72SAndiry Xu 	retval = hub_port_reset(hub, port1, udev, delay, false);
37721da177e4SLinus Torvalds 	if (retval < 0)		/* error or disconnect */
37731da177e4SLinus Torvalds 		goto fail;
37741da177e4SLinus Torvalds 	/* success, speed is known */
377507194ab7SLuben Tuikov 
37761da177e4SLinus Torvalds 	retval = -ENODEV;
37771da177e4SLinus Torvalds 
37781da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
37791da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
37801da177e4SLinus Torvalds 		goto fail;
37811da177e4SLinus Torvalds 	}
37821da177e4SLinus Torvalds 	oldspeed = udev->speed;
37831da177e4SLinus Torvalds 
37841da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
37851da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
37865bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
37875bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
37881da177e4SLinus Torvalds 	 */
37891da177e4SLinus Torvalds 	switch (udev->speed) {
37906b403b02SSarah Sharp 	case USB_SPEED_SUPER:
3791551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:	/* fixed at 512 */
3792551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
37935bb6e0aeSInaky Perez-Gonzalez 		break;
37941da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
3795551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
37961da177e4SLinus Torvalds 		break;
37971da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
37981da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
37991da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
38001da177e4SLinus Torvalds 		 * then correct our initial guess.
38011da177e4SLinus Torvalds 		 */
3802551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
38031da177e4SLinus Torvalds 		break;
38041da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
3805551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
38061da177e4SLinus Torvalds 		break;
38071da177e4SLinus Torvalds 	default:
38081da177e4SLinus Torvalds 		goto fail;
38091da177e4SLinus Torvalds 	}
38101da177e4SLinus Torvalds 
3811e538dfdaSMichal Nazarewicz 	if (udev->speed == USB_SPEED_WIRELESS)
3812e538dfdaSMichal Nazarewicz 		speed = "variable speed Wireless";
3813e538dfdaSMichal Nazarewicz 	else
3814e538dfdaSMichal Nazarewicz 		speed = usb_speed_string(udev->speed);
3815e538dfdaSMichal Nazarewicz 
3816c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
381783a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
3818e538dfdaSMichal Nazarewicz 				"%s %s USB device number %d using %s\n",
3819e538dfdaSMichal Nazarewicz 				(udev->config) ? "reset" : "new", speed,
38203b29b68bSAlan Stern 				devnum, udev->bus->controller->driver->name);
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
38231da177e4SLinus Torvalds 	if (hdev->tt) {
38241da177e4SLinus Torvalds 		udev->tt = hdev->tt;
38251da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
38261da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
38271da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
3828d199c96dSAlan Stern 		if (!hub->tt.hub) {
3829d199c96dSAlan Stern 			dev_err(&udev->dev, "parent hub has no TT\n");
3830d199c96dSAlan Stern 			retval = -EINVAL;
3831d199c96dSAlan Stern 			goto fail;
3832d199c96dSAlan Stern 		}
38331da177e4SLinus Torvalds 		udev->tt = &hub->tt;
38341da177e4SLinus Torvalds 		udev->ttport = port1;
38351da177e4SLinus Torvalds 	}
38361da177e4SLinus Torvalds 
38371da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
38381da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
38391da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
38401da177e4SLinus Torvalds 	 * Change it cautiously.
38411da177e4SLinus Torvalds 	 *
38421da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
38431da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
38441da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
38451da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
38461da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
38471da177e4SLinus Torvalds 	 * value.
38481da177e4SLinus Torvalds 	 */
38491da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
3850c6515272SSarah Sharp 		if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
38511da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
38521da177e4SLinus Torvalds 			int r = 0;
38531da177e4SLinus Torvalds 
38541da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
38551da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
38561da177e4SLinus Torvalds 			if (!buf) {
38571da177e4SLinus Torvalds 				retval = -ENOMEM;
38581da177e4SLinus Torvalds 				continue;
38591da177e4SLinus Torvalds 			}
38601da177e4SLinus Torvalds 
3861b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
3862b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
3863b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
38641da177e4SLinus Torvalds 			 */
38651da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
38661da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
38671da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
38681da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
38691da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
38701da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
3871fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
38721da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
38735bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
38741da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
38751da177e4SLinus Torvalds 							USB_DT_DEVICE) {
38761da177e4SLinus Torvalds 						r = 0;
38771da177e4SLinus Torvalds 						break;
38781da177e4SLinus Torvalds 					}
38791da177e4SLinus Torvalds 					/* FALL THROUGH */
38801da177e4SLinus Torvalds 				default:
38811da177e4SLinus Torvalds 					if (r == 0)
38821da177e4SLinus Torvalds 						r = -EPROTO;
38831da177e4SLinus Torvalds 					break;
38841da177e4SLinus Torvalds 				}
38851da177e4SLinus Torvalds 				if (r == 0)
38861da177e4SLinus Torvalds 					break;
38871da177e4SLinus Torvalds 			}
38881da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
38891da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
38901da177e4SLinus Torvalds 			kfree(buf);
38911da177e4SLinus Torvalds 
389275d7cf72SAndiry Xu 			retval = hub_port_reset(hub, port1, udev, delay, false);
38931da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
38941da177e4SLinus Torvalds 				goto fail;
38951da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
38961da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
38971da177e4SLinus Torvalds 					"device reset changed speed!\n");
38981da177e4SLinus Torvalds 				retval = -ENODEV;
38991da177e4SLinus Torvalds 				goto fail;
39001da177e4SLinus Torvalds 			}
39011da177e4SLinus Torvalds 			if (r) {
3902b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
3903b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
3904b9cef6c3SWu Fengguang 					r);
39051da177e4SLinus Torvalds 				retval = -EMSGSIZE;
39061da177e4SLinus Torvalds 				continue;
39071da177e4SLinus Torvalds 			}
39081da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
39091da177e4SLinus Torvalds 		}
39101da177e4SLinus Torvalds 
39116c529cdcSInaky Perez-Gonzalez  		/*
39126c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
39136c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
39146c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
39156c529cdcSInaky Perez-Gonzalez  		 */
39166c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
39171da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
39184326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
39191da177e4SLinus Torvalds 				if (retval >= 0)
39201da177e4SLinus Torvalds 					break;
39211da177e4SLinus Torvalds 				msleep(200);
39221da177e4SLinus Torvalds 			}
39231da177e4SLinus Torvalds 			if (retval < 0) {
39241da177e4SLinus Torvalds 				dev_err(&udev->dev,
39251da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
39264326ed0bSAlan Stern 					devnum, retval);
39271da177e4SLinus Torvalds 				goto fail;
39281da177e4SLinus Torvalds 			}
3929c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
3930c6515272SSarah Sharp 				devnum = udev->devnum;
3931c6515272SSarah Sharp 				dev_info(&udev->dev,
39323b29b68bSAlan Stern 						"%s SuperSpeed USB device number %d using %s\n",
3933c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
39343b29b68bSAlan Stern 						devnum, udev->bus->controller->driver->name);
3935c6515272SSarah Sharp 			}
39361da177e4SLinus Torvalds 
39371da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
39381da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
39391da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
39401da177e4SLinus Torvalds 			 */
39411da177e4SLinus Torvalds 			msleep(10);
3942c6515272SSarah Sharp 			if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
39431da177e4SLinus Torvalds 				break;
39446c529cdcSInaky Perez-Gonzalez   		}
39451da177e4SLinus Torvalds 
39461da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
39471da177e4SLinus Torvalds 		if (retval < 8) {
3948b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
3949b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
3950b9cef6c3SWu Fengguang 					retval);
39511da177e4SLinus Torvalds 			if (retval >= 0)
39521da177e4SLinus Torvalds 				retval = -EMSGSIZE;
39531da177e4SLinus Torvalds 		} else {
39541da177e4SLinus Torvalds 			retval = 0;
39551da177e4SLinus Torvalds 			break;
39561da177e4SLinus Torvalds 		}
39571da177e4SLinus Torvalds 	}
39581da177e4SLinus Torvalds 	if (retval)
39591da177e4SLinus Torvalds 		goto fail;
39601da177e4SLinus Torvalds 
3961d8aec3dbSElric Fu 	/*
3962d8aec3dbSElric Fu 	 * Some superspeed devices have finished the link training process
3963d8aec3dbSElric Fu 	 * and attached to a superspeed hub port, but the device descriptor
3964d8aec3dbSElric Fu 	 * got from those devices show they aren't superspeed devices. Warm
3965d8aec3dbSElric Fu 	 * reset the port attached by the devices can fix them.
3966d8aec3dbSElric Fu 	 */
3967d8aec3dbSElric Fu 	if ((udev->speed == USB_SPEED_SUPER) &&
3968d8aec3dbSElric Fu 			(le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3969d8aec3dbSElric Fu 		dev_err(&udev->dev, "got a wrong device descriptor, "
3970d8aec3dbSElric Fu 				"warm reset device\n");
3971d8aec3dbSElric Fu 		hub_port_reset(hub, port1, udev,
3972d8aec3dbSElric Fu 				HUB_BH_RESET_TIME, true);
3973d8aec3dbSElric Fu 		retval = -EINVAL;
3974d8aec3dbSElric Fu 		goto fail;
3975d8aec3dbSElric Fu 	}
3976d8aec3dbSElric Fu 
39776b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
39786b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
39796b403b02SSarah Sharp 		i = 512;
39806b403b02SSarah Sharp 	else
39816b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
398229cc8897SKuninori Morimoto 	if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
398356626a72SAlan Stern 		if (udev->speed == USB_SPEED_LOW ||
39841da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
398556626a72SAlan Stern 			dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
39861da177e4SLinus Torvalds 			retval = -EMSGSIZE;
39871da177e4SLinus Torvalds 			goto fail;
39881da177e4SLinus Torvalds 		}
398956626a72SAlan Stern 		if (udev->speed == USB_SPEED_FULL)
39901da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
399156626a72SAlan Stern 		else
399256626a72SAlan Stern 			dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
39931da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
3994fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
39951da177e4SLinus Torvalds 	}
39961da177e4SLinus Torvalds 
39971da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
39981da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
3999b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4000b9cef6c3SWu Fengguang 			retval);
40011da177e4SLinus Torvalds 		if (retval >= 0)
40021da177e4SLinus Torvalds 			retval = -ENOMSG;
40031da177e4SLinus Torvalds 		goto fail;
40041da177e4SLinus Torvalds 	}
40051da177e4SLinus Torvalds 
40061ff4df56SAndiry Xu 	if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
40071ff4df56SAndiry Xu 		retval = usb_get_bos_descriptor(udev);
400851e0a012SSarah Sharp 		if (!retval) {
4009d9b2099cSSarah Sharp 			udev->lpm_capable = usb_device_supports_lpm(udev);
401051e0a012SSarah Sharp 			usb_set_lpm_parameters(udev);
401151e0a012SSarah Sharp 		}
40121ff4df56SAndiry Xu 	}
40133148bf04SAndiry Xu 
40141da177e4SLinus Torvalds 	retval = 0;
401548f24970SAlek Du 	/* notify HCD that we have a device connected and addressed */
401648f24970SAlek Du 	if (hcd->driver->update_device)
401748f24970SAlek Du 		hcd->driver->update_device(hcd, udev);
40181da177e4SLinus Torvalds fail:
40194326ed0bSAlan Stern 	if (retval) {
40201da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
40213b29b68bSAlan Stern 		update_devnum(udev, devnum);	/* for disconnect processing */
40224326ed0bSAlan Stern 	}
40234186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
40241da177e4SLinus Torvalds 	return retval;
40251da177e4SLinus Torvalds }
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds static void
40281da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
40291da177e4SLinus Torvalds {
40301da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
40311da177e4SLinus Torvalds 	int				status;
40321da177e4SLinus Torvalds 
4033e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
40341da177e4SLinus Torvalds 	if (qual == NULL)
40351da177e4SLinus Torvalds 		return;
40361da177e4SLinus Torvalds 
40371da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
40381da177e4SLinus Torvalds 			qual, sizeof *qual);
40391da177e4SLinus Torvalds 	if (status == sizeof *qual) {
40401da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
40411da177e4SLinus Torvalds 			"connect to a high speed hub\n");
40421da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
40431da177e4SLinus Torvalds 		if (hub->has_indicators) {
40441da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
4045c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
40461da177e4SLinus Torvalds 		}
40471da177e4SLinus Torvalds 	}
40481da177e4SLinus Torvalds 	kfree(qual);
40491da177e4SLinus Torvalds }
40501da177e4SLinus Torvalds 
40511da177e4SLinus Torvalds static unsigned
40521da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
40531da177e4SLinus Torvalds {
40541da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
40551da177e4SLinus Torvalds 	int remaining;
405655c52718SAlan Stern 	int port1;
40571da177e4SLinus Torvalds 
405855c52718SAlan Stern 	if (!hub->limited_power)
40591da177e4SLinus Torvalds 		return 0;
40601da177e4SLinus Torvalds 
406155c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
406255c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4063ff823c79SLan Tianyu 		struct usb_device	*udev = hub->ports[port1 - 1]->child;
406455c52718SAlan Stern 		int			delta;
40651da177e4SLinus Torvalds 
40661da177e4SLinus Torvalds 		if (!udev)
40671da177e4SLinus Torvalds 			continue;
40681da177e4SLinus Torvalds 
406955c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
407055c52718SAlan Stern 		 * or 8mA for OTG ports */
40711da177e4SLinus Torvalds 		if (udev->actconfig)
407255c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
407355c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
407455c52718SAlan Stern 			delta = 100;
40751da177e4SLinus Torvalds 		else
407655c52718SAlan Stern 			delta = 8;
407755c52718SAlan Stern 		if (delta > hub->mA_per_port)
4078b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
4079b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
408055c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
40811da177e4SLinus Torvalds 		remaining -= delta;
40821da177e4SLinus Torvalds 	}
40831da177e4SLinus Torvalds 	if (remaining < 0) {
408455c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
408555c52718SAlan Stern 			- remaining);
40861da177e4SLinus Torvalds 		remaining = 0;
40871da177e4SLinus Torvalds 	}
40881da177e4SLinus Torvalds 	return remaining;
40891da177e4SLinus Torvalds }
40901da177e4SLinus Torvalds 
40911da177e4SLinus Torvalds /* Handle physical or logical connection change events.
40921da177e4SLinus Torvalds  * This routine is called when:
40931da177e4SLinus Torvalds  * 	a port connection-change occurs;
40941da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
4095742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
40961da177e4SLinus Torvalds  *		a firmware download)
40971da177e4SLinus Torvalds  * caller already locked the hub
40981da177e4SLinus Torvalds  */
40991da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
41001da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
41011da177e4SLinus Torvalds {
41021da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
41031da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
410490da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
410524618b0cSAlan Stern 	unsigned wHubCharacteristics =
410624618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
41078808f00cSAlan Stern 	struct usb_device *udev;
41081da177e4SLinus Torvalds 	int status, i;
41091da177e4SLinus Torvalds 
41101da177e4SLinus Torvalds 	dev_dbg (hub_dev,
41111da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
4112131dec34SSarah Sharp 		port1, portstatus, portchange, portspeed(hub, portstatus));
41131da177e4SLinus Torvalds 
41141da177e4SLinus Torvalds 	if (hub->has_indicators) {
41151da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
41161da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
41171da177e4SLinus Torvalds 	}
41181da177e4SLinus Torvalds 
41191da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
41201da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
41211da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
412224618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
412324618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
41241da177e4SLinus Torvalds #endif
41251da177e4SLinus Torvalds 
41268808f00cSAlan Stern 	/* Try to resuscitate an existing device */
4127ff823c79SLan Tianyu 	udev = hub->ports[port1 - 1]->child;
41288808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
41298808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
41308808f00cSAlan Stern 		usb_lock_device(udev);
41318808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
41328808f00cSAlan Stern 			status = 0;		/* Nothing to do */
41338808f00cSAlan Stern 
41348808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
41355257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
41365257d97aSAlan Stern 				udev->persist_enabled) {
41378808f00cSAlan Stern 			/* For a suspended device, treat this as a
41388808f00cSAlan Stern 			 * remote wakeup event.
41398808f00cSAlan Stern 			 */
41400534d468SAlan Stern 			status = usb_remote_wakeup(udev);
41418808f00cSAlan Stern #endif
41428808f00cSAlan Stern 
41438808f00cSAlan Stern 		} else {
41445257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
41458808f00cSAlan Stern 		}
41468808f00cSAlan Stern 		usb_unlock_device(udev);
41478808f00cSAlan Stern 
41488808f00cSAlan Stern 		if (status == 0) {
41498808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
41508808f00cSAlan Stern 			return;
41518808f00cSAlan Stern 		}
41528808f00cSAlan Stern 	}
41538808f00cSAlan Stern 
415424618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
41558808f00cSAlan Stern 	if (udev)
4156ff823c79SLan Tianyu 		usb_disconnect(&hub->ports[port1 - 1]->child);
415724618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
415824618b0cSAlan Stern 
4159253e0572SAlan Stern 	/* We can forget about a "removed" device when there's a physical
4160253e0572SAlan Stern 	 * disconnect or the connect status changes.
4161253e0572SAlan Stern 	 */
4162253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4163253e0572SAlan Stern 			(portchange & USB_PORT_STAT_C_CONNECTION))
4164253e0572SAlan Stern 		clear_bit(port1, hub->removed_bits);
4165253e0572SAlan Stern 
41665257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
41675257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
41685257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
41695257d97aSAlan Stern 		if (status < 0) {
41705257d97aSAlan Stern 			if (printk_ratelimit())
41715257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
41725257d97aSAlan Stern 						"port %d disabled\n", port1);
41735257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
41745257d97aSAlan Stern 		} else {
41755257d97aSAlan Stern 			portstatus = status;
41765257d97aSAlan Stern 		}
41775257d97aSAlan Stern 	}
41785257d97aSAlan Stern 
4179925aa46bSRichard Zhao 	if (hcd->phy && !hdev->parent) {
4180925aa46bSRichard Zhao 		if (portstatus & USB_PORT_STAT_CONNECTION)
4181925aa46bSRichard Zhao 			usb_phy_notify_connect(hcd->phy, port1);
4182925aa46bSRichard Zhao 		else
4183925aa46bSRichard Zhao 			usb_phy_notify_disconnect(hcd->phy, port1);
4184925aa46bSRichard Zhao 	}
4185925aa46bSRichard Zhao 
4186253e0572SAlan Stern 	/* Return now if debouncing failed or nothing is connected or
4187253e0572SAlan Stern 	 * the device was "removed".
4188253e0572SAlan Stern 	 */
4189253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4190253e0572SAlan Stern 			test_bit(port1, hub->removed_bits)) {
41911da177e4SLinus Torvalds 
41921da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
419374ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
41940ed9a57eSAndiry Xu 				&& !port_is_power_on(hub, portstatus))
41951da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
41961da177e4SLinus Torvalds 
41971da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
41981da177e4SLinus Torvalds   			goto done;
41991da177e4SLinus Torvalds 		return;
42001da177e4SLinus Torvalds 	}
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
42031da177e4SLinus Torvalds 
42041da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
42051da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
42061da177e4SLinus Torvalds 		 */
42071da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
42081da177e4SLinus Torvalds 		if (!udev) {
42091da177e4SLinus Torvalds 			dev_err (hub_dev,
42101da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
42111da177e4SLinus Torvalds 				port1);
42121da177e4SLinus Torvalds 			goto done;
42131da177e4SLinus Torvalds 		}
42141da177e4SLinus Torvalds 
42151da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
421655c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
4217b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
42188af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
42191da177e4SLinus Torvalds 
4220131dec34SSarah Sharp 		/* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4221131dec34SSarah Sharp 		if (hub_is_superspeed(hub->hdev))
4222e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
4223e7b77172SSarah Sharp 		else
4224e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
4225e7b77172SSarah Sharp 
42263b29b68bSAlan Stern 		choose_devnum(udev);
4227c6515272SSarah Sharp 		if (udev->devnum <= 0) {
4228c6515272SSarah Sharp 			status = -ENOTCONN;	/* Don't retry */
4229c6515272SSarah Sharp 			goto loop;
4230c6515272SSarah Sharp 		}
4231c6515272SSarah Sharp 
4232e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
42331da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
42341da177e4SLinus Torvalds 		if (status < 0)
42351da177e4SLinus Torvalds 			goto loop;
42361da177e4SLinus Torvalds 
423793362a87SPhil Dibowitz 		usb_detect_quirks(udev);
423893362a87SPhil Dibowitz 		if (udev->quirks & USB_QUIRK_DELAY_INIT)
423993362a87SPhil Dibowitz 			msleep(1000);
424093362a87SPhil Dibowitz 
42411da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
42421da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
42431da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
42441da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
42451da177e4SLinus Torvalds 		 * on the parent.
42461da177e4SLinus Torvalds 		 */
42471da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
424855c52718SAlan Stern 				&& udev->bus_mA <= 100) {
42491da177e4SLinus Torvalds 			u16	devstat;
42501da177e4SLinus Torvalds 
42511da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
42521da177e4SLinus Torvalds 					&devstat);
425355c52718SAlan Stern 			if (status < 2) {
42541da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
42551da177e4SLinus Torvalds 				goto loop_disable;
42561da177e4SLinus Torvalds 			}
425755c52718SAlan Stern 			le16_to_cpus(&devstat);
42581da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
42591da177e4SLinus Torvalds 				dev_err(&udev->dev,
42601da177e4SLinus Torvalds 					"can't connect bus-powered hub "
42611da177e4SLinus Torvalds 					"to this port\n");
42621da177e4SLinus Torvalds 				if (hub->has_indicators) {
42631da177e4SLinus Torvalds 					hub->indicator[port1-1] =
42641da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
4265c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
42661da177e4SLinus Torvalds 				}
42671da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
42681da177e4SLinus Torvalds 				goto loop_disable;
42691da177e4SLinus Torvalds 			}
42701da177e4SLinus Torvalds 		}
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds 		/* check for devices running slower than they could */
42731da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
42741da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
42751da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
42761da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
42771da177e4SLinus Torvalds 
4278fa286188SGreg Kroah-Hartman 		/* Store the parent's children[] pointer.  At this point
42791da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
42801da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
42811da177e4SLinus Torvalds 		 */
42821da177e4SLinus Torvalds 		status = 0;
42831da177e4SLinus Torvalds 
42841da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
42851da177e4SLinus Torvalds 		 * been disconnected; we would race with the
42861da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
42871da177e4SLinus Torvalds 		 */
42881da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
42891da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
42901da177e4SLinus Torvalds 			status = -ENOTCONN;
42911da177e4SLinus Torvalds 		else
4292ff823c79SLan Tianyu 			hub->ports[port1 - 1]->child = udev;
42931da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
42941da177e4SLinus Torvalds 
42951da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
42961da177e4SLinus Torvalds 		if (!status) {
42971da177e4SLinus Torvalds 			status = usb_new_device(udev);
42981da177e4SLinus Torvalds 			if (status) {
42991da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
4300ff823c79SLan Tianyu 				hub->ports[port1 - 1]->child = NULL;
43011da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
43021da177e4SLinus Torvalds 			}
43031da177e4SLinus Torvalds 		}
43041da177e4SLinus Torvalds 
43051da177e4SLinus Torvalds 		if (status)
43061da177e4SLinus Torvalds 			goto loop_disable;
43071da177e4SLinus Torvalds 
43081da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
43091da177e4SLinus Torvalds 		if (status)
431055c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
43111da177e4SLinus Torvalds 
43121da177e4SLinus Torvalds 		return;
43131da177e4SLinus Torvalds 
43141da177e4SLinus Torvalds loop_disable:
43151da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
43161da177e4SLinus Torvalds loop:
4317fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
43183b29b68bSAlan Stern 		release_devnum(udev);
4319f7410cedSHerbert Xu 		hub_free_dev(udev);
43201da177e4SLinus Torvalds 		usb_put_dev(udev);
4321ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
43221da177e4SLinus Torvalds 			break;
43231da177e4SLinus Torvalds 	}
43243a31155cSAlan Stern 	if (hub->hdev->parent ||
43253a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
43263a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
43273a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
43283a31155cSAlan Stern 				port1);
43291da177e4SLinus Torvalds 
43301da177e4SLinus Torvalds done:
43311da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
433290da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
433390da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
43341da177e4SLinus Torvalds }
43351da177e4SLinus Torvalds 
4336714b07beSSarah Sharp /* Returns 1 if there was a remote wakeup and a connect status change. */
4337714b07beSSarah Sharp static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
433872937e1eSSarah Sharp 		u16 portstatus, u16 portchange)
4339714b07beSSarah Sharp {
4340714b07beSSarah Sharp 	struct usb_device *hdev;
4341714b07beSSarah Sharp 	struct usb_device *udev;
4342714b07beSSarah Sharp 	int connect_change = 0;
4343714b07beSSarah Sharp 	int ret;
4344714b07beSSarah Sharp 
4345714b07beSSarah Sharp 	hdev = hub->hdev;
4346ff823c79SLan Tianyu 	udev = hub->ports[port - 1]->child;
43474ee823b8SSarah Sharp 	if (!hub_is_superspeed(hdev)) {
4348714b07beSSarah Sharp 		if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4349714b07beSSarah Sharp 			return 0;
4350714b07beSSarah Sharp 		clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
43514ee823b8SSarah Sharp 	} else {
43524ee823b8SSarah Sharp 		if (!udev || udev->state != USB_STATE_SUSPENDED ||
435372937e1eSSarah Sharp 				 (portstatus & USB_PORT_STAT_LINK_STATE) !=
435472937e1eSSarah Sharp 				 USB_SS_PORT_LS_U0)
43554ee823b8SSarah Sharp 			return 0;
43564ee823b8SSarah Sharp 	}
4357714b07beSSarah Sharp 
4358714b07beSSarah Sharp 	if (udev) {
4359714b07beSSarah Sharp 		/* TRSMRCY = 10 msec */
4360714b07beSSarah Sharp 		msleep(10);
4361714b07beSSarah Sharp 
4362714b07beSSarah Sharp 		usb_lock_device(udev);
4363714b07beSSarah Sharp 		ret = usb_remote_wakeup(udev);
4364714b07beSSarah Sharp 		usb_unlock_device(udev);
4365714b07beSSarah Sharp 		if (ret < 0)
4366714b07beSSarah Sharp 			connect_change = 1;
4367714b07beSSarah Sharp 	} else {
4368714b07beSSarah Sharp 		ret = -ENODEV;
4369714b07beSSarah Sharp 		hub_port_disable(hub, port, 1);
4370714b07beSSarah Sharp 	}
4371714b07beSSarah Sharp 	dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4372714b07beSSarah Sharp 			port, ret);
4373714b07beSSarah Sharp 	return connect_change;
4374714b07beSSarah Sharp }
4375714b07beSSarah Sharp 
43761da177e4SLinus Torvalds static void hub_events(void)
43771da177e4SLinus Torvalds {
43781da177e4SLinus Torvalds 	struct list_head *tmp;
43791da177e4SLinus Torvalds 	struct usb_device *hdev;
43801da177e4SLinus Torvalds 	struct usb_interface *intf;
43811da177e4SLinus Torvalds 	struct usb_hub *hub;
43821da177e4SLinus Torvalds 	struct device *hub_dev;
43831da177e4SLinus Torvalds 	u16 hubstatus;
43841da177e4SLinus Torvalds 	u16 hubchange;
43851da177e4SLinus Torvalds 	u16 portstatus;
43861da177e4SLinus Torvalds 	u16 portchange;
43871da177e4SLinus Torvalds 	int i, ret;
43884ee823b8SSarah Sharp 	int connect_change, wakeup_change;
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds 	/*
43911da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
43921da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
43931da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
43941da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
43951da177e4SLinus Torvalds 	 */
43961da177e4SLinus Torvalds 	while (1) {
43971da177e4SLinus Torvalds 
43981da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
43991da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
44001da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
44011da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
44021da177e4SLinus Torvalds 			break;
44031da177e4SLinus Torvalds 		}
44041da177e4SLinus Torvalds 
44051da177e4SLinus Torvalds 		tmp = hub_event_list.next;
44061da177e4SLinus Torvalds 		list_del_init(tmp);
44071da177e4SLinus Torvalds 
44081da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
4409e8054854SAlan Stern 		kref_get(&hub->kref);
4410e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
44111da177e4SLinus Torvalds 
4412e8054854SAlan Stern 		hdev = hub->hdev;
4413e8054854SAlan Stern 		hub_dev = hub->intfdev;
4414e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
441540f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
44161da177e4SLinus Torvalds 				hdev->state, hub->descriptor
44171da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
44181da177e4SLinus Torvalds 					: 0,
44191da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
44201da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
442140f122f3SAlan Stern 				(u16) hub->event_bits[0]);
44221da177e4SLinus Torvalds 
44231da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
44241da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
442506b84e8aSAlan Stern 		usb_lock_device(hdev);
4426e8054854SAlan Stern 		if (unlikely(hub->disconnected))
44279bbdf1e0SAlan Stern 			goto loop_disconnected;
44281da177e4SLinus Torvalds 
44291da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
44301da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
44317de18d8bSAlan Stern 			hub->error = -ENODEV;
44324330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
44331da177e4SLinus Torvalds 			goto loop;
44341da177e4SLinus Torvalds 		}
44351da177e4SLinus Torvalds 
443640f122f3SAlan Stern 		/* Autoresume */
443740f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
443840f122f3SAlan Stern 		if (ret) {
443940f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
44401da177e4SLinus Torvalds 			goto loop;
444140f122f3SAlan Stern 		}
444240f122f3SAlan Stern 
444340f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
444440f122f3SAlan Stern 		if (hub->quiescing)
444540f122f3SAlan Stern 			goto loop_autopm;
44461da177e4SLinus Torvalds 
44471da177e4SLinus Torvalds 		if (hub->error) {
44481da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
44491da177e4SLinus Torvalds 				hub->error);
44501da177e4SLinus Torvalds 
4451742120c6SMing Lei 			ret = usb_reset_device(hdev);
44521da177e4SLinus Torvalds 			if (ret) {
44531da177e4SLinus Torvalds 				dev_dbg (hub_dev,
44541da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
445540f122f3SAlan Stern 				goto loop_autopm;
44561da177e4SLinus Torvalds 			}
44571da177e4SLinus Torvalds 
44581da177e4SLinus Torvalds 			hub->nerrors = 0;
44591da177e4SLinus Torvalds 			hub->error = 0;
44601da177e4SLinus Torvalds 		}
44611da177e4SLinus Torvalds 
44621da177e4SLinus Torvalds 		/* deal with port status changes */
44631da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
44641da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
44651da177e4SLinus Torvalds 				continue;
44661da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
446772937e1eSSarah Sharp 			wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
44681da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
44694ee823b8SSarah Sharp 					!connect_change && !wakeup_change)
44701da177e4SLinus Torvalds 				continue;
44711da177e4SLinus Torvalds 
44721da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
44731da177e4SLinus Torvalds 					&portstatus, &portchange);
44741da177e4SLinus Torvalds 			if (ret < 0)
44751da177e4SLinus Torvalds 				continue;
44761da177e4SLinus Torvalds 
44771da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
44781da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
44791da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
44801da177e4SLinus Torvalds 				connect_change = 1;
44811da177e4SLinus Torvalds 			}
44821da177e4SLinus Torvalds 
44831da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
44841da177e4SLinus Torvalds 				if (!connect_change)
44851da177e4SLinus Torvalds 					dev_dbg (hub_dev,
44861da177e4SLinus Torvalds 						"port %d enable change, "
44871da177e4SLinus Torvalds 						"status %08x\n",
44881da177e4SLinus Torvalds 						i, portstatus);
44891da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
44901da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
44911da177e4SLinus Torvalds 
44921da177e4SLinus Torvalds 				/*
44931da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
44941da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
44951da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
44961da177e4SLinus Torvalds 				 * Works at least with mouse driver.
44971da177e4SLinus Torvalds 				 */
44981da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
44991da177e4SLinus Torvalds 				    && !connect_change
4500ff823c79SLan Tianyu 				    && hub->ports[i - 1]->child) {
45011da177e4SLinus Torvalds 					dev_err (hub_dev,
45021da177e4SLinus Torvalds 					    "port %i "
45031da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
45041da177e4SLinus Torvalds 					    "re-enabling...\n",
45051da177e4SLinus Torvalds 						i);
45061da177e4SLinus Torvalds 					connect_change = 1;
45071da177e4SLinus Torvalds 				}
45081da177e4SLinus Torvalds 			}
45091da177e4SLinus Torvalds 
451072937e1eSSarah Sharp 			if (hub_handle_remote_wakeup(hub, i,
451172937e1eSSarah Sharp 						portstatus, portchange))
45121da177e4SLinus Torvalds 				connect_change = 1;
45131da177e4SLinus Torvalds 
45141da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4515752d57a8SPaul Bolle 				u16 status = 0;
4516752d57a8SPaul Bolle 				u16 unused;
4517752d57a8SPaul Bolle 
4518752d57a8SPaul Bolle 				dev_dbg(hub_dev, "over-current change on port "
4519752d57a8SPaul Bolle 					"%d\n", i);
45201da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
45211da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
4522752d57a8SPaul Bolle 				msleep(100);	/* Cool down */
45238520f380SAlan Stern 				hub_power_on(hub, true);
4524752d57a8SPaul Bolle 				hub_port_status(hub, i, &status, &unused);
4525752d57a8SPaul Bolle 				if (status & USB_PORT_STAT_OVERCURRENT)
4526752d57a8SPaul Bolle 					dev_err(hub_dev, "over-current "
4527752d57a8SPaul Bolle 						"condition on port %d\n", i);
45281da177e4SLinus Torvalds 			}
45291da177e4SLinus Torvalds 
45301da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
45311da177e4SLinus Torvalds 				dev_dbg (hub_dev,
45321da177e4SLinus Torvalds 					"reset change on port %d\n",
45331da177e4SLinus Torvalds 					i);
45341da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
45351da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
45361da177e4SLinus Torvalds 			}
4537c7061574SSarah Sharp 			if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4538c7061574SSarah Sharp 					hub_is_superspeed(hub->hdev)) {
4539c7061574SSarah Sharp 				dev_dbg(hub_dev,
4540c7061574SSarah Sharp 					"warm reset change on port %d\n",
4541c7061574SSarah Sharp 					i);
4542c7061574SSarah Sharp 				clear_port_feature(hdev, i,
4543c7061574SSarah Sharp 					USB_PORT_FEAT_C_BH_PORT_RESET);
4544c7061574SSarah Sharp 			}
4545dbe79bbeSJohn Youn 			if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4546dbe79bbeSJohn Youn 				clear_port_feature(hub->hdev, i,
4547dbe79bbeSJohn Youn 						USB_PORT_FEAT_C_PORT_LINK_STATE);
4548dbe79bbeSJohn Youn 			}
4549dbe79bbeSJohn Youn 			if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4550dbe79bbeSJohn Youn 				dev_warn(hub_dev,
4551dbe79bbeSJohn Youn 					"config error on port %d\n",
4552dbe79bbeSJohn Youn 					i);
4553dbe79bbeSJohn Youn 				clear_port_feature(hub->hdev, i,
4554dbe79bbeSJohn Youn 						USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4555dbe79bbeSJohn Youn 			}
45561da177e4SLinus Torvalds 
45575e467f6eSAndiry Xu 			/* Warm reset a USB3 protocol port if it's in
45585e467f6eSAndiry Xu 			 * SS.Inactive state.
45595e467f6eSAndiry Xu 			 */
45608bea2bd3SStanislaw Ledwon 			if (hub_port_warm_reset_required(hub, portstatus)) {
45615e467f6eSAndiry Xu 				dev_dbg(hub_dev, "warm reset port %d\n", i);
456275d7cf72SAndiry Xu 				hub_port_reset(hub, i, NULL,
456375d7cf72SAndiry Xu 						HUB_BH_RESET_TIME, true);
45645e467f6eSAndiry Xu 			}
45655e467f6eSAndiry Xu 
45661da177e4SLinus Torvalds 			if (connect_change)
45671da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
45681da177e4SLinus Torvalds 						portstatus, portchange);
45691da177e4SLinus Torvalds 		} /* end for i */
45701da177e4SLinus Torvalds 
45711da177e4SLinus Torvalds 		/* deal with hub status changes */
45721da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
45731da177e4SLinus Torvalds 			;	/* do nothing */
45741da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
45751da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
45761da177e4SLinus Torvalds 		else {
45771da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
45781da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
45791da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
458055c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
458155c52718SAlan Stern 					/* FIXME: Is this always true? */
458255c52718SAlan Stern 					hub->limited_power = 1;
4583403fae78Sjidong xiao 				else
4584403fae78Sjidong xiao 					hub->limited_power = 0;
45851da177e4SLinus Torvalds 			}
45861da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
4587752d57a8SPaul Bolle 				u16 status = 0;
4588752d57a8SPaul Bolle 				u16 unused;
4589752d57a8SPaul Bolle 
4590752d57a8SPaul Bolle 				dev_dbg(hub_dev, "over-current change\n");
45911da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
4592752d57a8SPaul Bolle 				msleep(500);	/* Cool down */
45938520f380SAlan Stern                         	hub_power_on(hub, true);
4594752d57a8SPaul Bolle 				hub_hub_status(hub, &status, &unused);
4595752d57a8SPaul Bolle 				if (status & HUB_STATUS_OVERCURRENT)
4596752d57a8SPaul Bolle 					dev_err(hub_dev, "over-current "
4597752d57a8SPaul Bolle 						"condition\n");
45981da177e4SLinus Torvalds 			}
45991da177e4SLinus Torvalds 		}
46001da177e4SLinus Torvalds 
460140f122f3SAlan Stern  loop_autopm:
46028e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface() above */
46038e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
46041da177e4SLinus Torvalds  loop:
46058e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface_no_resume() in
46068e4ceb38SAlan Stern 		 * kick_khubd() and allow autosuspend.
46078e4ceb38SAlan Stern 		 */
46088e4ceb38SAlan Stern 		usb_autopm_put_interface(intf);
46099bbdf1e0SAlan Stern  loop_disconnected:
46101da177e4SLinus Torvalds 		usb_unlock_device(hdev);
4611e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
46121da177e4SLinus Torvalds 
46131da177e4SLinus Torvalds         } /* end while (1) */
46141da177e4SLinus Torvalds }
46151da177e4SLinus Torvalds 
46161da177e4SLinus Torvalds static int hub_thread(void *__unused)
46171da177e4SLinus Torvalds {
46183bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
46193bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
46203bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
46213bb1af52SAlan Stern 	 * the companion full-speed controller.
46223bb1af52SAlan Stern 	 */
462383144186SRafael J. Wysocki 	set_freezable();
46243bb1af52SAlan Stern 
46251da177e4SLinus Torvalds 	do {
46261da177e4SLinus Torvalds 		hub_events();
4627e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
46289c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
46299c8d6178Sakpm@osdl.org 				kthread_should_stop());
46309c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
46311da177e4SLinus Torvalds 
46321da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
46339c8d6178Sakpm@osdl.org 	return 0;
46341da177e4SLinus Torvalds }
46351da177e4SLinus Torvalds 
46361e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = {
46371da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
46381da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
46391da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
46401da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
46411da177e4SLinus Torvalds     { }						/* Terminating entry */
46421da177e4SLinus Torvalds };
46431da177e4SLinus Torvalds 
46441da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
46451da177e4SLinus Torvalds 
46461da177e4SLinus Torvalds static struct usb_driver hub_driver = {
46471da177e4SLinus Torvalds 	.name =		"hub",
46481da177e4SLinus Torvalds 	.probe =	hub_probe,
46491da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
46501da177e4SLinus Torvalds 	.suspend =	hub_suspend,
46511da177e4SLinus Torvalds 	.resume =	hub_resume,
4652f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
46537de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
46547de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
4655c532b29aSAndi Kleen 	.unlocked_ioctl = hub_ioctl,
46561da177e4SLinus Torvalds 	.id_table =	hub_id_table,
465740f122f3SAlan Stern 	.supports_autosuspend =	1,
46581da177e4SLinus Torvalds };
46591da177e4SLinus Torvalds 
46601da177e4SLinus Torvalds int usb_hub_init(void)
46611da177e4SLinus Torvalds {
46621da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
46631da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
46641da177e4SLinus Torvalds 			usbcore_name);
46651da177e4SLinus Torvalds 		return -1;
46661da177e4SLinus Torvalds 	}
46671da177e4SLinus Torvalds 
46689c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
46699c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
46701da177e4SLinus Torvalds 		return 0;
46711da177e4SLinus Torvalds 
46721da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
46731da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
46741da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
46751da177e4SLinus Torvalds 
46761da177e4SLinus Torvalds 	return -1;
46771da177e4SLinus Torvalds }
46781da177e4SLinus Torvalds 
46791da177e4SLinus Torvalds void usb_hub_cleanup(void)
46801da177e4SLinus Torvalds {
46819c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
46821da177e4SLinus Torvalds 
46831da177e4SLinus Torvalds 	/*
46841da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
46851da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
46861da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
46871da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
46881da177e4SLinus Torvalds 	 * individual hub resources. -greg
46891da177e4SLinus Torvalds 	 */
46901da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
46911da177e4SLinus Torvalds } /* usb_hub_cleanup() */
46921da177e4SLinus Torvalds 
4693eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
4694eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
46951da177e4SLinus Torvalds {
4696eb764c4bSAlan Stern 	int		changed = 0;
46971da177e4SLinus Torvalds 	unsigned	index;
4698eb764c4bSAlan Stern 	unsigned	serial_len = 0;
4699eb764c4bSAlan Stern 	unsigned	len;
4700eb764c4bSAlan Stern 	unsigned	old_length;
4701eb764c4bSAlan Stern 	int		length;
4702eb764c4bSAlan Stern 	char		*buf;
47031da177e4SLinus Torvalds 
4704eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
4705eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
4706eb764c4bSAlan Stern 		return 1;
4707eb764c4bSAlan Stern 
4708eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
4709eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
4710eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
4711eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
4712eb764c4bSAlan Stern 	 * different flash card of the same brand).
4713eb764c4bSAlan Stern 	 */
4714eb764c4bSAlan Stern 	if (udev->serial)
4715eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
4716eb764c4bSAlan Stern 
4717eb764c4bSAlan Stern 	len = serial_len;
47181da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4719eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4720eb764c4bSAlan Stern 		len = max(len, old_length);
47211da177e4SLinus Torvalds 	}
4722eb764c4bSAlan Stern 
47230cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
47241da177e4SLinus Torvalds 	if (buf == NULL) {
47251da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
47261da177e4SLinus Torvalds 		/* assume the worst */
47271da177e4SLinus Torvalds 		return 1;
47281da177e4SLinus Torvalds 	}
47291da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4730eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
47311da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
47321da177e4SLinus Torvalds 				old_length);
4733eb764c4bSAlan Stern 		if (length != old_length) {
47341da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
47351da177e4SLinus Torvalds 					index, length);
4736eb764c4bSAlan Stern 			changed = 1;
47371da177e4SLinus Torvalds 			break;
47381da177e4SLinus Torvalds 		}
47391da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
47401da177e4SLinus Torvalds 				!= 0) {
47411da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
4742eb764c4bSAlan Stern 				index,
4743eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
4744eb764c4bSAlan Stern 					bConfigurationValue);
4745eb764c4bSAlan Stern 			changed = 1;
47461da177e4SLinus Torvalds 			break;
47471da177e4SLinus Torvalds 		}
47481da177e4SLinus Torvalds 	}
4749eb764c4bSAlan Stern 
4750eb764c4bSAlan Stern 	if (!changed && serial_len) {
4751eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
4752eb764c4bSAlan Stern 				buf, serial_len);
4753eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
4754eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
4755eb764c4bSAlan Stern 					length);
4756eb764c4bSAlan Stern 			changed = 1;
4757eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
4758eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
4759eb764c4bSAlan Stern 			changed = 1;
4760eb764c4bSAlan Stern 		}
4761eb764c4bSAlan Stern 	}
4762eb764c4bSAlan Stern 
47631da177e4SLinus Torvalds 	kfree(buf);
4764eb764c4bSAlan Stern 	return changed;
47651da177e4SLinus Torvalds }
47661da177e4SLinus Torvalds 
47671da177e4SLinus Torvalds /**
4768742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
47691da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
47701da177e4SLinus Torvalds  *
477179efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
477279efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
4773742120c6SMing Lei  * Use usb_reset_device() instead.
47741da177e4SLinus Torvalds  *
47751da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
47761da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
47771da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
47781da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
47791da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
47801da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
47811da177e4SLinus Torvalds  * re-enumerated and probed all over again.
47821da177e4SLinus Torvalds  *
47831da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
47841da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
47851da177e4SLinus Torvalds  * if the reset wasn't even attempted.
47861da177e4SLinus Torvalds  *
47871da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
47881da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
47891da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
47901da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
47916bc6cff5SAlan Stern  *
47926bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
47936bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
47946bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
47956bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
47961da177e4SLinus Torvalds  */
4797742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
47981da177e4SLinus Torvalds {
47991da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
48001da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
48013f0479e0SSarah Sharp 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
48021da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
480312c3da34SAlan Stern 	int 				i, ret = 0;
480412c3da34SAlan Stern 	int				port1 = udev->portnum;
48051da177e4SLinus Torvalds 
48061da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
48071da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
48081da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
48091da177e4SLinus Torvalds 				udev->state);
48101da177e4SLinus Torvalds 		return -EINVAL;
48111da177e4SLinus Torvalds 	}
48121da177e4SLinus Torvalds 
48131da177e4SLinus Torvalds 	if (!parent_hdev) {
48144bec9917SWolfram Sang 		/* this requires hcd-specific logic; see ohci_restart() */
4815441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
48161da177e4SLinus Torvalds 		return -EISDIR;
48171da177e4SLinus Torvalds 	}
48181da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
48191da177e4SLinus Torvalds 
4820f74631e3SSarah Sharp 	/* Disable LPM and LTM while we reset the device and reinstall the alt
4821f74631e3SSarah Sharp 	 * settings.  Device-initiated LPM settings, and system exit latency
4822f74631e3SSarah Sharp 	 * settings are cleared when the device is reset, so we have to set
4823f74631e3SSarah Sharp 	 * them up again.
48246d1d0513SSarah Sharp 	 */
48256d1d0513SSarah Sharp 	ret = usb_unlocked_disable_lpm(udev);
48266d1d0513SSarah Sharp 	if (ret) {
48276d1d0513SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
48286d1d0513SSarah Sharp 		goto re_enumerate;
48296d1d0513SSarah Sharp 	}
4830f74631e3SSarah Sharp 	ret = usb_disable_ltm(udev);
4831f74631e3SSarah Sharp 	if (ret) {
4832f74631e3SSarah Sharp 		dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4833f74631e3SSarah Sharp 				__func__);
4834f74631e3SSarah Sharp 		goto re_enumerate;
4835f74631e3SSarah Sharp 	}
48366d1d0513SSarah Sharp 
48371da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
48381da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
48391da177e4SLinus Torvalds 
48401da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
48411da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
4842fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
48431da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
4844dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
48451da177e4SLinus Torvalds 			break;
48461da177e4SLinus Torvalds 	}
48471da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
4848d5cbad4bSAlan Stern 
48491da177e4SLinus Torvalds 	if (ret < 0)
48501da177e4SLinus Torvalds 		goto re_enumerate;
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
4853eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
48541da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
48551da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
48561da177e4SLinus Torvalds 		goto re_enumerate;
48571da177e4SLinus Torvalds   	}
48581da177e4SLinus Torvalds 
48594fe0387aSAlan Stern 	/* Restore the device's previous configuration */
48601da177e4SLinus Torvalds 	if (!udev->actconfig)
48611da177e4SLinus Torvalds 		goto done;
48623f0479e0SSarah Sharp 
4863d673bfcbSSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
48643f0479e0SSarah Sharp 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
48653f0479e0SSarah Sharp 	if (ret < 0) {
48663f0479e0SSarah Sharp 		dev_warn(&udev->dev,
48673f0479e0SSarah Sharp 				"Busted HC?  Not enough HCD resources for "
48683f0479e0SSarah Sharp 				"old configuration.\n");
4869d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
48703f0479e0SSarah Sharp 		goto re_enumerate;
48713f0479e0SSarah Sharp 	}
48721da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
48731da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
48741da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
48751da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
48761da177e4SLinus Torvalds 	if (ret < 0) {
48771da177e4SLinus Torvalds 		dev_err(&udev->dev,
48781da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
48791da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
4880d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
48811da177e4SLinus Torvalds 		goto re_enumerate;
48821da177e4SLinus Torvalds   	}
4883d673bfcbSSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
48841da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
48851da177e4SLinus Torvalds 
48864fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
48874fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
48884fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
48894fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
48904fe0387aSAlan Stern 	 * endpoint state.
48914fe0387aSAlan Stern 	 */
48921da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
48933f0479e0SSarah Sharp 		struct usb_host_config *config = udev->actconfig;
48943f0479e0SSarah Sharp 		struct usb_interface *intf = config->interface[i];
48951da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
48961da177e4SLinus Torvalds 
48971da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
48984fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
48994fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
49004fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
49014fe0387aSAlan Stern 			ret = 0;
49024fe0387aSAlan Stern 		} else {
490304a723eaSSarah Sharp 			/* Let the bandwidth allocation function know that this
490404a723eaSSarah Sharp 			 * device has been reset, and it will have to use
490504a723eaSSarah Sharp 			 * alternate setting 0 as the current alternate setting.
49063f0479e0SSarah Sharp 			 */
490704a723eaSSarah Sharp 			intf->resetting_device = 1;
49081da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
49091da177e4SLinus Torvalds 					desc->bAlternateSetting);
491004a723eaSSarah Sharp 			intf->resetting_device = 0;
49114fe0387aSAlan Stern 		}
49121da177e4SLinus Torvalds 		if (ret < 0) {
49131da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
49141da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
49151da177e4SLinus Torvalds 				desc->bInterfaceNumber,
49161da177e4SLinus Torvalds 				desc->bAlternateSetting,
49171da177e4SLinus Torvalds 				ret);
49181da177e4SLinus Torvalds 			goto re_enumerate;
49191da177e4SLinus Torvalds 		}
49201da177e4SLinus Torvalds 	}
49211da177e4SLinus Torvalds 
49221da177e4SLinus Torvalds done:
4923f74631e3SSarah Sharp 	/* Now that the alt settings are re-installed, enable LTM and LPM. */
492478d9a487SAlan Stern 	usb_unlocked_enable_lpm(udev);
4925f74631e3SSarah Sharp 	usb_enable_ltm(udev);
49261da177e4SLinus Torvalds 	return 0;
49271da177e4SLinus Torvalds 
49281da177e4SLinus Torvalds re_enumerate:
49296d1d0513SSarah Sharp 	/* LPM state doesn't matter when we're about to destroy the device. */
49301da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
49311da177e4SLinus Torvalds 	return -ENODEV;
49321da177e4SLinus Torvalds }
49331da177e4SLinus Torvalds 
49341da177e4SLinus Torvalds /**
49351da177e4SLinus Torvalds  * usb_reset_device - warn interface drivers and perform a USB port reset
49361da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
49371da177e4SLinus Torvalds  *
493879efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
493979efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
4940742120c6SMing Lei  * the reset is over (using their post_reset method).
494179efa097SAlan Stern  *
494279efa097SAlan Stern  * Return value is the same as for usb_reset_and_verify_device().
494379efa097SAlan Stern  *
494479efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
494579efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
494679efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
4947742120c6SMing Lei  * the device using usb_lock_device_for_reset().
494879efa097SAlan Stern  *
494979efa097SAlan Stern  * If an interface is currently being probed or disconnected, we assume
495079efa097SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
495179efa097SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
495279efa097SAlan Stern  * we attempt to unbind it and rebind afterward.
495379efa097SAlan Stern  */
4954742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
495579efa097SAlan Stern {
495679efa097SAlan Stern 	int ret;
4957852c4b43SAlan Stern 	int i;
495879efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
495979efa097SAlan Stern 
496079efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
496179efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
496279efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
496379efa097SAlan Stern 				udev->state);
496479efa097SAlan Stern 		return -EINVAL;
496579efa097SAlan Stern 	}
496679efa097SAlan Stern 
4967645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
496894fcda1fSAlan Stern 	usb_autoresume_device(udev);
4969645daaabSAlan Stern 
497079efa097SAlan Stern 	if (config) {
4971852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
4972852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
497379efa097SAlan Stern 			struct usb_driver *drv;
497478d9a487SAlan Stern 			int unbind = 0;
497579efa097SAlan Stern 
4976852c4b43SAlan Stern 			if (cintf->dev.driver) {
497779efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
497878d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
497978d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
498078d9a487SAlan Stern 				else if (cintf->condition ==
498178d9a487SAlan Stern 						USB_INTERFACE_BOUND)
498278d9a487SAlan Stern 					unbind = 1;
498378d9a487SAlan Stern 				if (unbind)
498478d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
498579efa097SAlan Stern 			}
498679efa097SAlan Stern 		}
498779efa097SAlan Stern 	}
498879efa097SAlan Stern 
4989742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
499079efa097SAlan Stern 
499179efa097SAlan Stern 	if (config) {
4992852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
4993852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
499479efa097SAlan Stern 			struct usb_driver *drv;
499578d9a487SAlan Stern 			int rebind = cintf->needs_binding;
499679efa097SAlan Stern 
499778d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
499879efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
499979efa097SAlan Stern 				if (drv->post_reset)
500078d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
500178d9a487SAlan Stern 				else if (cintf->condition ==
500278d9a487SAlan Stern 						USB_INTERFACE_BOUND)
500378d9a487SAlan Stern 					rebind = 1;
500479efa097SAlan Stern 			}
50056c640945SAlan Stern 			if (ret == 0 && rebind)
500678d9a487SAlan Stern 				usb_rebind_intf(cintf);
500779efa097SAlan Stern 		}
500879efa097SAlan Stern 	}
500979efa097SAlan Stern 
501094fcda1fSAlan Stern 	usb_autosuspend_device(udev);
501179efa097SAlan Stern 	return ret;
501279efa097SAlan Stern }
5013742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
5014dc023dceSInaky Perez-Gonzalez 
5015dc023dceSInaky Perez-Gonzalez 
5016dc023dceSInaky Perez-Gonzalez /**
5017dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
5018dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
5019dc023dceSInaky Perez-Gonzalez  *
5020dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
5021dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
5022dc023dceSInaky Perez-Gonzalez  *
5023dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
5024dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
5025dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
5026dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
5027dc023dceSInaky Perez-Gonzalez  *
5028dc023dceSInaky Perez-Gonzalez  * Corner cases:
5029dc023dceSInaky Perez-Gonzalez  *
5030dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
5031dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
5032dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
5033dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
5034dc023dceSInaky Perez-Gonzalez  *
5035dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
5036dc023dceSInaky Perez-Gonzalez  *   be cancelled.
5037dc023dceSInaky Perez-Gonzalez  *
5038dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
5039dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
5040dc023dceSInaky Perez-Gonzalez  *   cancelled.
5041dc023dceSInaky Perez-Gonzalez  *
5042dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
5043dc023dceSInaky Perez-Gonzalez  * does its own.
5044dc023dceSInaky Perez-Gonzalez  *
5045dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
5046dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
5047dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
5048dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
5049dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
5050dc023dceSInaky Perez-Gonzalez  *     are referenced.
5051dc023dceSInaky Perez-Gonzalez  */
5052dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
5053dc023dceSInaky Perez-Gonzalez {
5054dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
5055dc023dceSInaky Perez-Gonzalez }
5056dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
5057ff823c79SLan Tianyu 
5058ff823c79SLan Tianyu /**
5059ff823c79SLan Tianyu  * usb_hub_find_child - Get the pointer of child device
5060ff823c79SLan Tianyu  * attached to the port which is specified by @port1.
5061ff823c79SLan Tianyu  * @hdev: USB device belonging to the usb hub
5062ff823c79SLan Tianyu  * @port1: port num to indicate which port the child device
5063ff823c79SLan Tianyu  *	is attached to.
5064ff823c79SLan Tianyu  *
5065ff823c79SLan Tianyu  * USB drivers call this function to get hub's child device
5066ff823c79SLan Tianyu  * pointer.
5067ff823c79SLan Tianyu  *
5068ff823c79SLan Tianyu  * Return NULL if input param is invalid and
5069ff823c79SLan Tianyu  * child's usb_device pointer if non-NULL.
5070ff823c79SLan Tianyu  */
5071ff823c79SLan Tianyu struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5072ff823c79SLan Tianyu 		int port1)
5073ff823c79SLan Tianyu {
5074ff823c79SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
5075ff823c79SLan Tianyu 
5076ff823c79SLan Tianyu 	if (port1 < 1 || port1 > hdev->maxchild)
5077ff823c79SLan Tianyu 		return NULL;
5078ff823c79SLan Tianyu 	return hub->ports[port1 - 1]->child;
5079ff823c79SLan Tianyu }
5080ff823c79SLan Tianyu EXPORT_SYMBOL_GPL(usb_hub_find_child);
5081d5575424SLan Tianyu 
508205f91689SLan Tianyu /**
508305f91689SLan Tianyu  * usb_set_hub_port_connect_type - set hub port connect type.
508405f91689SLan Tianyu  * @hdev: USB device belonging to the usb hub
508505f91689SLan Tianyu  * @port1: port num of the port
508605f91689SLan Tianyu  * @type: connect type of the port
508705f91689SLan Tianyu  */
508805f91689SLan Tianyu void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
508905f91689SLan Tianyu 	enum usb_port_connect_type type)
509005f91689SLan Tianyu {
509105f91689SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
509205f91689SLan Tianyu 
509305f91689SLan Tianyu 	hub->ports[port1 - 1]->connect_type = type;
509405f91689SLan Tianyu }
509505f91689SLan Tianyu 
509605f91689SLan Tianyu /**
509705f91689SLan Tianyu  * usb_get_hub_port_connect_type - Get the port's connect type
509805f91689SLan Tianyu  * @hdev: USB device belonging to the usb hub
509905f91689SLan Tianyu  * @port1: port num of the port
510005f91689SLan Tianyu  *
510105f91689SLan Tianyu  * Return connect type of the port and if input params are
510205f91689SLan Tianyu  * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
510305f91689SLan Tianyu  */
510405f91689SLan Tianyu enum usb_port_connect_type
510505f91689SLan Tianyu usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
510605f91689SLan Tianyu {
510705f91689SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
510805f91689SLan Tianyu 
510905f91689SLan Tianyu 	return hub->ports[port1 - 1]->connect_type;
511005f91689SLan Tianyu }
511105f91689SLan Tianyu 
5112d5575424SLan Tianyu #ifdef CONFIG_ACPI
5113d5575424SLan Tianyu /**
5114d5575424SLan Tianyu  * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5115d5575424SLan Tianyu  * @hdev: USB device belonging to the usb hub
5116d5575424SLan Tianyu  * @port1: port num of the port
5117d5575424SLan Tianyu  *
5118d5575424SLan Tianyu  * Return port's acpi handle if successful, NULL if params are
5119d5575424SLan Tianyu  * invaild.
5120d5575424SLan Tianyu  */
5121d5575424SLan Tianyu acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5122d5575424SLan Tianyu 	int port1)
5123d5575424SLan Tianyu {
5124d5575424SLan Tianyu 	struct usb_hub *hub = hdev_to_hub(hdev);
5125d5575424SLan Tianyu 
5126d5575424SLan Tianyu 	return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5127d5575424SLan Tianyu }
5128d5575424SLan Tianyu #endif
5129