xref: /OK3568_Linux_fs/kernel/drivers/usb/core/hub.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * USB hub driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright 1999 Linus Torvalds
6*4882a593Smuzhiyun  * (C) Copyright 1999 Johannes Erdfelt
7*4882a593Smuzhiyun  * (C) Copyright 1999 Gregory P. Smith
8*4882a593Smuzhiyun  * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Released under the GPLv2 only.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/errno.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/moduleparam.h>
17*4882a593Smuzhiyun #include <linux/completion.h>
18*4882a593Smuzhiyun #include <linux/sched/mm.h>
19*4882a593Smuzhiyun #include <linux/list.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/kcov.h>
22*4882a593Smuzhiyun #include <linux/ioctl.h>
23*4882a593Smuzhiyun #include <linux/usb.h>
24*4882a593Smuzhiyun #include <linux/usbdevice_fs.h>
25*4882a593Smuzhiyun #include <linux/usb/hcd.h>
26*4882a593Smuzhiyun #include <linux/usb/otg.h>
27*4882a593Smuzhiyun #include <linux/usb/quirks.h>
28*4882a593Smuzhiyun #include <linux/workqueue.h>
29*4882a593Smuzhiyun #include <linux/mutex.h>
30*4882a593Smuzhiyun #include <linux/random.h>
31*4882a593Smuzhiyun #include <linux/pm_qos.h>
32*4882a593Smuzhiyun #include <linux/kobject.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <linux/uaccess.h>
35*4882a593Smuzhiyun #include <asm/byteorder.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include "hub.h"
38*4882a593Smuzhiyun #include "otg_productlist.h"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define USB_VENDOR_GENESYS_LOGIC		0x05e3
41*4882a593Smuzhiyun #define USB_VENDOR_SMSC				0x0424
42*4882a593Smuzhiyun #define USB_PRODUCT_USB5534B			0x5534
43*4882a593Smuzhiyun #define USB_VENDOR_CYPRESS			0x04b4
44*4882a593Smuzhiyun #define USB_PRODUCT_CY7C65632			0x6570
45*4882a593Smuzhiyun #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND	0x01
46*4882a593Smuzhiyun #define HUB_QUIRK_DISABLE_AUTOSUSPEND		0x02
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define USB_TP_TRANSMISSION_DELAY	40	/* ns */
49*4882a593Smuzhiyun #define USB_TP_TRANSMISSION_DELAY_MAX	65535	/* ns */
50*4882a593Smuzhiyun #define USB_PING_RESPONSE_TIME		400	/* ns */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /* Protect struct usb_device->state and ->children members
53*4882a593Smuzhiyun  * Note: Both are also protected by ->dev.sem, except that ->state can
54*4882a593Smuzhiyun  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
55*4882a593Smuzhiyun static DEFINE_SPINLOCK(device_state_lock);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /* workqueue to process hub events */
58*4882a593Smuzhiyun static struct workqueue_struct *hub_wq;
59*4882a593Smuzhiyun static void hub_event(struct work_struct *work);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* synchronize hub-port add/remove and peering operations */
62*4882a593Smuzhiyun DEFINE_MUTEX(usb_port_peer_mutex);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /* cycle leds on hubs that aren't blinking for attention */
65*4882a593Smuzhiyun static bool blinkenlights;
66*4882a593Smuzhiyun module_param(blinkenlights, bool, S_IRUGO);
67*4882a593Smuzhiyun MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
71*4882a593Smuzhiyun  * 10 seconds to send reply for the initial 64-byte descriptor request.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun /* define initial 64-byte descriptor request timeout in milliseconds */
74*4882a593Smuzhiyun static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
75*4882a593Smuzhiyun module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
76*4882a593Smuzhiyun MODULE_PARM_DESC(initial_descriptor_timeout,
77*4882a593Smuzhiyun 		"initial 64-byte descriptor request timeout in milliseconds "
78*4882a593Smuzhiyun 		"(default 5000 - 5.0 seconds)");
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun  * As of 2.6.10 we introduce a new USB device initialization scheme which
82*4882a593Smuzhiyun  * closely resembles the way Windows works.  Hopefully it will be compatible
83*4882a593Smuzhiyun  * with a wider range of devices than the old scheme.  However some previously
84*4882a593Smuzhiyun  * working devices may start giving rise to "device not accepting address"
85*4882a593Smuzhiyun  * errors; if that happens the user can try the old scheme by adjusting the
86*4882a593Smuzhiyun  * following module parameters.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * For maximum flexibility there are two boolean parameters to control the
89*4882a593Smuzhiyun  * hub driver's behavior.  On the first initialization attempt, if the
90*4882a593Smuzhiyun  * "old_scheme_first" parameter is set then the old scheme will be used,
91*4882a593Smuzhiyun  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
92*4882a593Smuzhiyun  * is set, then the driver will make another attempt, using the other scheme.
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun static bool old_scheme_first;
95*4882a593Smuzhiyun module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
96*4882a593Smuzhiyun MODULE_PARM_DESC(old_scheme_first,
97*4882a593Smuzhiyun 		 "start with the old device initialization scheme");
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static bool use_both_schemes = true;
100*4882a593Smuzhiyun module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
101*4882a593Smuzhiyun MODULE_PARM_DESC(use_both_schemes,
102*4882a593Smuzhiyun 		"try the other device initialization scheme if the "
103*4882a593Smuzhiyun 		"first one fails");
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* Mutual exclusion for EHCI CF initialization.  This interferes with
106*4882a593Smuzhiyun  * port reset on some companion controllers.
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
109*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun #define HUB_DEBOUNCE_TIMEOUT	2000
112*4882a593Smuzhiyun #define HUB_DEBOUNCE_STEP	  25
113*4882a593Smuzhiyun #define HUB_DEBOUNCE_STABLE	 100
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static void hub_release(struct kref *kref);
116*4882a593Smuzhiyun static int usb_reset_and_verify_device(struct usb_device *udev);
117*4882a593Smuzhiyun static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
118*4882a593Smuzhiyun static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
119*4882a593Smuzhiyun 		u16 portstatus);
120*4882a593Smuzhiyun 
portspeed(struct usb_hub * hub,int portstatus)121*4882a593Smuzhiyun static inline char *portspeed(struct usb_hub *hub, int portstatus)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	if (hub_is_superspeedplus(hub->hdev))
124*4882a593Smuzhiyun 		return "10.0 Gb/s";
125*4882a593Smuzhiyun 	if (hub_is_superspeed(hub->hdev))
126*4882a593Smuzhiyun 		return "5.0 Gb/s";
127*4882a593Smuzhiyun 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
128*4882a593Smuzhiyun 		return "480 Mb/s";
129*4882a593Smuzhiyun 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
130*4882a593Smuzhiyun 		return "1.5 Mb/s";
131*4882a593Smuzhiyun 	else
132*4882a593Smuzhiyun 		return "12 Mb/s";
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /* Note that hdev or one of its children must be locked! */
usb_hub_to_struct_hub(struct usb_device * hdev)136*4882a593Smuzhiyun struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	if (!hdev || !hdev->actconfig || !hdev->maxchild)
139*4882a593Smuzhiyun 		return NULL;
140*4882a593Smuzhiyun 	return usb_get_intfdata(hdev->actconfig->interface[0]);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
usb_device_supports_lpm(struct usb_device * udev)143*4882a593Smuzhiyun int usb_device_supports_lpm(struct usb_device *udev)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	/* Some devices have trouble with LPM */
146*4882a593Smuzhiyun 	if (udev->quirks & USB_QUIRK_NO_LPM)
147*4882a593Smuzhiyun 		return 0;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* USB 2.1 (and greater) devices indicate LPM support through
150*4882a593Smuzhiyun 	 * their USB 2.0 Extended Capabilities BOS descriptor.
151*4882a593Smuzhiyun 	 */
152*4882a593Smuzhiyun 	if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
153*4882a593Smuzhiyun 		if (udev->bos->ext_cap &&
154*4882a593Smuzhiyun 			(USB_LPM_SUPPORT &
155*4882a593Smuzhiyun 			 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
156*4882a593Smuzhiyun 			return 1;
157*4882a593Smuzhiyun 		return 0;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/*
161*4882a593Smuzhiyun 	 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
162*4882a593Smuzhiyun 	 * However, there are some that don't, and they set the U1/U2 exit
163*4882a593Smuzhiyun 	 * latencies to zero.
164*4882a593Smuzhiyun 	 */
165*4882a593Smuzhiyun 	if (!udev->bos->ss_cap) {
166*4882a593Smuzhiyun 		dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
167*4882a593Smuzhiyun 		return 0;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (udev->bos->ss_cap->bU1devExitLat == 0 &&
171*4882a593Smuzhiyun 			udev->bos->ss_cap->bU2DevExitLat == 0) {
172*4882a593Smuzhiyun 		if (udev->parent)
173*4882a593Smuzhiyun 			dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
174*4882a593Smuzhiyun 		else
175*4882a593Smuzhiyun 			dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
176*4882a593Smuzhiyun 		return 0;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (!udev->parent || udev->parent->lpm_capable)
180*4882a593Smuzhiyun 		return 1;
181*4882a593Smuzhiyun 	return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun  * Set the Maximum Exit Latency (MEL) for the host to wakup up the path from
186*4882a593Smuzhiyun  * U1/U2, send a PING to the device and receive a PING_RESPONSE.
187*4882a593Smuzhiyun  * See USB 3.1 section C.1.5.2
188*4882a593Smuzhiyun  */
usb_set_lpm_mel(struct usb_device * udev,struct usb3_lpm_parameters * udev_lpm_params,unsigned int udev_exit_latency,struct usb_hub * hub,struct usb3_lpm_parameters * hub_lpm_params,unsigned int hub_exit_latency)189*4882a593Smuzhiyun static void usb_set_lpm_mel(struct usb_device *udev,
190*4882a593Smuzhiyun 		struct usb3_lpm_parameters *udev_lpm_params,
191*4882a593Smuzhiyun 		unsigned int udev_exit_latency,
192*4882a593Smuzhiyun 		struct usb_hub *hub,
193*4882a593Smuzhiyun 		struct usb3_lpm_parameters *hub_lpm_params,
194*4882a593Smuzhiyun 		unsigned int hub_exit_latency)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	unsigned int total_mel;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/*
199*4882a593Smuzhiyun 	 * tMEL1. time to transition path from host to device into U0.
200*4882a593Smuzhiyun 	 * MEL for parent already contains the delay up to parent, so only add
201*4882a593Smuzhiyun 	 * the exit latency for the last link (pick the slower exit latency),
202*4882a593Smuzhiyun 	 * and the hub header decode latency. See USB 3.1 section C 2.2.1
203*4882a593Smuzhiyun 	 * Store MEL in nanoseconds
204*4882a593Smuzhiyun 	 */
205*4882a593Smuzhiyun 	total_mel = hub_lpm_params->mel +
206*4882a593Smuzhiyun 		max(udev_exit_latency, hub_exit_latency) * 1000 +
207*4882a593Smuzhiyun 		hub->descriptor->u.ss.bHubHdrDecLat * 100;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/*
210*4882a593Smuzhiyun 	 * tMEL2. Time to submit PING packet. Sum of tTPTransmissionDelay for
211*4882a593Smuzhiyun 	 * each link + wHubDelay for each hub. Add only for last link.
212*4882a593Smuzhiyun 	 * tMEL4, the time for PING_RESPONSE to traverse upstream is similar.
213*4882a593Smuzhiyun 	 * Multiply by 2 to include it as well.
214*4882a593Smuzhiyun 	 */
215*4882a593Smuzhiyun 	total_mel += (__le16_to_cpu(hub->descriptor->u.ss.wHubDelay) +
216*4882a593Smuzhiyun 		      USB_TP_TRANSMISSION_DELAY) * 2;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/*
219*4882a593Smuzhiyun 	 * tMEL3, tPingResponse. Time taken by device to generate PING_RESPONSE
220*4882a593Smuzhiyun 	 * after receiving PING. Also add 2100ns as stated in USB 3.1 C 1.5.2.4
221*4882a593Smuzhiyun 	 * to cover the delay if the PING_RESPONSE is queued behind a Max Packet
222*4882a593Smuzhiyun 	 * Size DP.
223*4882a593Smuzhiyun 	 * Note these delays should be added only once for the entire path, so
224*4882a593Smuzhiyun 	 * add them to the MEL of the device connected to the roothub.
225*4882a593Smuzhiyun 	 */
226*4882a593Smuzhiyun 	if (!hub->hdev->parent)
227*4882a593Smuzhiyun 		total_mel += USB_PING_RESPONSE_TIME + 2100;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	udev_lpm_params->mel = total_mel;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun /*
233*4882a593Smuzhiyun  * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
234*4882a593Smuzhiyun  * a transition from either U1 or U2.
235*4882a593Smuzhiyun  */
usb_set_lpm_pel(struct usb_device * udev,struct usb3_lpm_parameters * udev_lpm_params,unsigned int udev_exit_latency,struct usb_hub * hub,struct usb3_lpm_parameters * hub_lpm_params,unsigned int hub_exit_latency,unsigned int port_to_port_exit_latency)236*4882a593Smuzhiyun static void usb_set_lpm_pel(struct usb_device *udev,
237*4882a593Smuzhiyun 		struct usb3_lpm_parameters *udev_lpm_params,
238*4882a593Smuzhiyun 		unsigned int udev_exit_latency,
239*4882a593Smuzhiyun 		struct usb_hub *hub,
240*4882a593Smuzhiyun 		struct usb3_lpm_parameters *hub_lpm_params,
241*4882a593Smuzhiyun 		unsigned int hub_exit_latency,
242*4882a593Smuzhiyun 		unsigned int port_to_port_exit_latency)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	unsigned int first_link_pel;
245*4882a593Smuzhiyun 	unsigned int hub_pel;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/*
248*4882a593Smuzhiyun 	 * First, the device sends an LFPS to transition the link between the
249*4882a593Smuzhiyun 	 * device and the parent hub into U0.  The exit latency is the bigger of
250*4882a593Smuzhiyun 	 * the device exit latency or the hub exit latency.
251*4882a593Smuzhiyun 	 */
252*4882a593Smuzhiyun 	if (udev_exit_latency > hub_exit_latency)
253*4882a593Smuzhiyun 		first_link_pel = udev_exit_latency * 1000;
254*4882a593Smuzhiyun 	else
255*4882a593Smuzhiyun 		first_link_pel = hub_exit_latency * 1000;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/*
258*4882a593Smuzhiyun 	 * When the hub starts to receive the LFPS, there is a slight delay for
259*4882a593Smuzhiyun 	 * it to figure out that one of the ports is sending an LFPS.  Then it
260*4882a593Smuzhiyun 	 * will forward the LFPS to its upstream link.  The exit latency is the
261*4882a593Smuzhiyun 	 * delay, plus the PEL that we calculated for this hub.
262*4882a593Smuzhiyun 	 */
263*4882a593Smuzhiyun 	hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/*
266*4882a593Smuzhiyun 	 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
267*4882a593Smuzhiyun 	 * is the greater of the two exit latencies.
268*4882a593Smuzhiyun 	 */
269*4882a593Smuzhiyun 	if (first_link_pel > hub_pel)
270*4882a593Smuzhiyun 		udev_lpm_params->pel = first_link_pel;
271*4882a593Smuzhiyun 	else
272*4882a593Smuzhiyun 		udev_lpm_params->pel = hub_pel;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /*
276*4882a593Smuzhiyun  * Set the System Exit Latency (SEL) to indicate the total worst-case time from
277*4882a593Smuzhiyun  * when a device initiates a transition to U0, until when it will receive the
278*4882a593Smuzhiyun  * first packet from the host controller.
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  * Section C.1.5.1 describes the four components to this:
281*4882a593Smuzhiyun  *  - t1: device PEL
282*4882a593Smuzhiyun  *  - t2: time for the ERDY to make it from the device to the host.
283*4882a593Smuzhiyun  *  - t3: a host-specific delay to process the ERDY.
284*4882a593Smuzhiyun  *  - t4: time for the packet to make it from the host to the device.
285*4882a593Smuzhiyun  *
286*4882a593Smuzhiyun  * t3 is specific to both the xHCI host and the platform the host is integrated
287*4882a593Smuzhiyun  * into.  The Intel HW folks have said it's negligible, FIXME if a different
288*4882a593Smuzhiyun  * vendor says otherwise.
289*4882a593Smuzhiyun  */
usb_set_lpm_sel(struct usb_device * udev,struct usb3_lpm_parameters * udev_lpm_params)290*4882a593Smuzhiyun static void usb_set_lpm_sel(struct usb_device *udev,
291*4882a593Smuzhiyun 		struct usb3_lpm_parameters *udev_lpm_params)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct usb_device *parent;
294*4882a593Smuzhiyun 	unsigned int num_hubs;
295*4882a593Smuzhiyun 	unsigned int total_sel;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/* t1 = device PEL */
298*4882a593Smuzhiyun 	total_sel = udev_lpm_params->pel;
299*4882a593Smuzhiyun 	/* How many external hubs are in between the device & the root port. */
300*4882a593Smuzhiyun 	for (parent = udev->parent, num_hubs = 0; parent->parent;
301*4882a593Smuzhiyun 			parent = parent->parent)
302*4882a593Smuzhiyun 		num_hubs++;
303*4882a593Smuzhiyun 	/* t2 = 2.1us + 250ns * (num_hubs - 1) */
304*4882a593Smuzhiyun 	if (num_hubs > 0)
305*4882a593Smuzhiyun 		total_sel += 2100 + 250 * (num_hubs - 1);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	/* t4 = 250ns * num_hubs */
308*4882a593Smuzhiyun 	total_sel += 250 * num_hubs;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	udev_lpm_params->sel = total_sel;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
usb_set_lpm_parameters(struct usb_device * udev)313*4882a593Smuzhiyun static void usb_set_lpm_parameters(struct usb_device *udev)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	struct usb_hub *hub;
316*4882a593Smuzhiyun 	unsigned int port_to_port_delay;
317*4882a593Smuzhiyun 	unsigned int udev_u1_del;
318*4882a593Smuzhiyun 	unsigned int udev_u2_del;
319*4882a593Smuzhiyun 	unsigned int hub_u1_del;
320*4882a593Smuzhiyun 	unsigned int hub_u2_del;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
323*4882a593Smuzhiyun 		return;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	hub = usb_hub_to_struct_hub(udev->parent);
326*4882a593Smuzhiyun 	/* It doesn't take time to transition the roothub into U0, since it
327*4882a593Smuzhiyun 	 * doesn't have an upstream link.
328*4882a593Smuzhiyun 	 */
329*4882a593Smuzhiyun 	if (!hub)
330*4882a593Smuzhiyun 		return;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
333*4882a593Smuzhiyun 	udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
334*4882a593Smuzhiyun 	hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
335*4882a593Smuzhiyun 	hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
338*4882a593Smuzhiyun 			hub, &udev->parent->u1_params, hub_u1_del);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
341*4882a593Smuzhiyun 			hub, &udev->parent->u2_params, hub_u2_del);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	/*
344*4882a593Smuzhiyun 	 * Appendix C, section C.2.2.2, says that there is a slight delay from
345*4882a593Smuzhiyun 	 * when the parent hub notices the downstream port is trying to
346*4882a593Smuzhiyun 	 * transition to U0 to when the hub initiates a U0 transition on its
347*4882a593Smuzhiyun 	 * upstream port.  The section says the delays are tPort2PortU1EL and
348*4882a593Smuzhiyun 	 * tPort2PortU2EL, but it doesn't define what they are.
349*4882a593Smuzhiyun 	 *
350*4882a593Smuzhiyun 	 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
351*4882a593Smuzhiyun 	 * about the same delays.  Use the maximum delay calculations from those
352*4882a593Smuzhiyun 	 * sections.  For U1, it's tHubPort2PortExitLat, which is 1us max.  For
353*4882a593Smuzhiyun 	 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat.  I
354*4882a593Smuzhiyun 	 * assume the device exit latencies they are talking about are the hub
355*4882a593Smuzhiyun 	 * exit latencies.
356*4882a593Smuzhiyun 	 *
357*4882a593Smuzhiyun 	 * What do we do if the U2 exit latency is less than the U1 exit
358*4882a593Smuzhiyun 	 * latency?  It's possible, although not likely...
359*4882a593Smuzhiyun 	 */
360*4882a593Smuzhiyun 	port_to_port_delay = 1;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
363*4882a593Smuzhiyun 			hub, &udev->parent->u1_params, hub_u1_del,
364*4882a593Smuzhiyun 			port_to_port_delay);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (hub_u2_del > hub_u1_del)
367*4882a593Smuzhiyun 		port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
368*4882a593Smuzhiyun 	else
369*4882a593Smuzhiyun 		port_to_port_delay = 1 + hub_u1_del;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
372*4882a593Smuzhiyun 			hub, &udev->parent->u2_params, hub_u2_del,
373*4882a593Smuzhiyun 			port_to_port_delay);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/* Now that we've got PEL, calculate SEL. */
376*4882a593Smuzhiyun 	usb_set_lpm_sel(udev, &udev->u1_params);
377*4882a593Smuzhiyun 	usb_set_lpm_sel(udev, &udev->u2_params);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun /* USB 2.0 spec Section 11.24.4.5 */
get_hub_descriptor(struct usb_device * hdev,struct usb_hub_descriptor * desc)381*4882a593Smuzhiyun static int get_hub_descriptor(struct usb_device *hdev,
382*4882a593Smuzhiyun 		struct usb_hub_descriptor *desc)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	int i, ret, size;
385*4882a593Smuzhiyun 	unsigned dtype;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	if (hub_is_superspeed(hdev)) {
388*4882a593Smuzhiyun 		dtype = USB_DT_SS_HUB;
389*4882a593Smuzhiyun 		size = USB_DT_SS_HUB_SIZE;
390*4882a593Smuzhiyun 	} else {
391*4882a593Smuzhiyun 		dtype = USB_DT_HUB;
392*4882a593Smuzhiyun 		size = sizeof(struct usb_hub_descriptor);
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	for (i = 0; i < 3; i++) {
396*4882a593Smuzhiyun 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
397*4882a593Smuzhiyun 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
398*4882a593Smuzhiyun 			dtype << 8, 0, desc, size,
399*4882a593Smuzhiyun 			USB_CTRL_GET_TIMEOUT);
400*4882a593Smuzhiyun 		if (hub_is_superspeed(hdev)) {
401*4882a593Smuzhiyun 			if (ret == size)
402*4882a593Smuzhiyun 				return ret;
403*4882a593Smuzhiyun 		} else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) {
404*4882a593Smuzhiyun 			/* Make sure we have the DeviceRemovable field. */
405*4882a593Smuzhiyun 			size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1;
406*4882a593Smuzhiyun 			if (ret < size)
407*4882a593Smuzhiyun 				return -EMSGSIZE;
408*4882a593Smuzhiyun 			return ret;
409*4882a593Smuzhiyun 		}
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 	return -EINVAL;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /*
415*4882a593Smuzhiyun  * USB 2.0 spec Section 11.24.2.1
416*4882a593Smuzhiyun  */
clear_hub_feature(struct usb_device * hdev,int feature)417*4882a593Smuzhiyun static int clear_hub_feature(struct usb_device *hdev, int feature)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
420*4882a593Smuzhiyun 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun /*
424*4882a593Smuzhiyun  * USB 2.0 spec Section 11.24.2.2
425*4882a593Smuzhiyun  */
usb_clear_port_feature(struct usb_device * hdev,int port1,int feature)426*4882a593Smuzhiyun int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
429*4882a593Smuzhiyun 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
430*4882a593Smuzhiyun 		NULL, 0, 1000);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun  * USB 2.0 spec Section 11.24.2.13
435*4882a593Smuzhiyun  */
set_port_feature(struct usb_device * hdev,int port1,int feature)436*4882a593Smuzhiyun static int set_port_feature(struct usb_device *hdev, int port1, int feature)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
439*4882a593Smuzhiyun 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
440*4882a593Smuzhiyun 		NULL, 0, 1000);
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
to_led_name(int selector)443*4882a593Smuzhiyun static char *to_led_name(int selector)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	switch (selector) {
446*4882a593Smuzhiyun 	case HUB_LED_AMBER:
447*4882a593Smuzhiyun 		return "amber";
448*4882a593Smuzhiyun 	case HUB_LED_GREEN:
449*4882a593Smuzhiyun 		return "green";
450*4882a593Smuzhiyun 	case HUB_LED_OFF:
451*4882a593Smuzhiyun 		return "off";
452*4882a593Smuzhiyun 	case HUB_LED_AUTO:
453*4882a593Smuzhiyun 		return "auto";
454*4882a593Smuzhiyun 	default:
455*4882a593Smuzhiyun 		return "??";
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun /*
460*4882a593Smuzhiyun  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
461*4882a593Smuzhiyun  * for info about using port indicators
462*4882a593Smuzhiyun  */
set_port_led(struct usb_hub * hub,int port1,int selector)463*4882a593Smuzhiyun static void set_port_led(struct usb_hub *hub, int port1, int selector)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
466*4882a593Smuzhiyun 	int status;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	status = set_port_feature(hub->hdev, (selector << 8) | port1,
469*4882a593Smuzhiyun 			USB_PORT_FEAT_INDICATOR);
470*4882a593Smuzhiyun 	dev_dbg(&port_dev->dev, "indicator %s status %d\n",
471*4882a593Smuzhiyun 		to_led_name(selector), status);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun #define	LED_CYCLE_PERIOD	((2*HZ)/3)
475*4882a593Smuzhiyun 
led_work(struct work_struct * work)476*4882a593Smuzhiyun static void led_work(struct work_struct *work)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct usb_hub		*hub =
479*4882a593Smuzhiyun 		container_of(work, struct usb_hub, leds.work);
480*4882a593Smuzhiyun 	struct usb_device	*hdev = hub->hdev;
481*4882a593Smuzhiyun 	unsigned		i;
482*4882a593Smuzhiyun 	unsigned		changed = 0;
483*4882a593Smuzhiyun 	int			cursor = -1;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
486*4882a593Smuzhiyun 		return;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	for (i = 0; i < hdev->maxchild; i++) {
489*4882a593Smuzhiyun 		unsigned	selector, mode;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		/* 30%-50% duty cycle */
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 		switch (hub->indicator[i]) {
494*4882a593Smuzhiyun 		/* cycle marker */
495*4882a593Smuzhiyun 		case INDICATOR_CYCLE:
496*4882a593Smuzhiyun 			cursor = i;
497*4882a593Smuzhiyun 			selector = HUB_LED_AUTO;
498*4882a593Smuzhiyun 			mode = INDICATOR_AUTO;
499*4882a593Smuzhiyun 			break;
500*4882a593Smuzhiyun 		/* blinking green = sw attention */
501*4882a593Smuzhiyun 		case INDICATOR_GREEN_BLINK:
502*4882a593Smuzhiyun 			selector = HUB_LED_GREEN;
503*4882a593Smuzhiyun 			mode = INDICATOR_GREEN_BLINK_OFF;
504*4882a593Smuzhiyun 			break;
505*4882a593Smuzhiyun 		case INDICATOR_GREEN_BLINK_OFF:
506*4882a593Smuzhiyun 			selector = HUB_LED_OFF;
507*4882a593Smuzhiyun 			mode = INDICATOR_GREEN_BLINK;
508*4882a593Smuzhiyun 			break;
509*4882a593Smuzhiyun 		/* blinking amber = hw attention */
510*4882a593Smuzhiyun 		case INDICATOR_AMBER_BLINK:
511*4882a593Smuzhiyun 			selector = HUB_LED_AMBER;
512*4882a593Smuzhiyun 			mode = INDICATOR_AMBER_BLINK_OFF;
513*4882a593Smuzhiyun 			break;
514*4882a593Smuzhiyun 		case INDICATOR_AMBER_BLINK_OFF:
515*4882a593Smuzhiyun 			selector = HUB_LED_OFF;
516*4882a593Smuzhiyun 			mode = INDICATOR_AMBER_BLINK;
517*4882a593Smuzhiyun 			break;
518*4882a593Smuzhiyun 		/* blink green/amber = reserved */
519*4882a593Smuzhiyun 		case INDICATOR_ALT_BLINK:
520*4882a593Smuzhiyun 			selector = HUB_LED_GREEN;
521*4882a593Smuzhiyun 			mode = INDICATOR_ALT_BLINK_OFF;
522*4882a593Smuzhiyun 			break;
523*4882a593Smuzhiyun 		case INDICATOR_ALT_BLINK_OFF:
524*4882a593Smuzhiyun 			selector = HUB_LED_AMBER;
525*4882a593Smuzhiyun 			mode = INDICATOR_ALT_BLINK;
526*4882a593Smuzhiyun 			break;
527*4882a593Smuzhiyun 		default:
528*4882a593Smuzhiyun 			continue;
529*4882a593Smuzhiyun 		}
530*4882a593Smuzhiyun 		if (selector != HUB_LED_AUTO)
531*4882a593Smuzhiyun 			changed = 1;
532*4882a593Smuzhiyun 		set_port_led(hub, i + 1, selector);
533*4882a593Smuzhiyun 		hub->indicator[i] = mode;
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 	if (!changed && blinkenlights) {
536*4882a593Smuzhiyun 		cursor++;
537*4882a593Smuzhiyun 		cursor %= hdev->maxchild;
538*4882a593Smuzhiyun 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
539*4882a593Smuzhiyun 		hub->indicator[cursor] = INDICATOR_CYCLE;
540*4882a593Smuzhiyun 		changed++;
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun 	if (changed)
543*4882a593Smuzhiyun 		queue_delayed_work(system_power_efficient_wq,
544*4882a593Smuzhiyun 				&hub->leds, LED_CYCLE_PERIOD);
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun /* use a short timeout for hub/port status fetches */
548*4882a593Smuzhiyun #define	USB_STS_TIMEOUT		1000
549*4882a593Smuzhiyun #define	USB_STS_RETRIES		5
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun /*
552*4882a593Smuzhiyun  * USB 2.0 spec Section 11.24.2.6
553*4882a593Smuzhiyun  */
get_hub_status(struct usb_device * hdev,struct usb_hub_status * data)554*4882a593Smuzhiyun static int get_hub_status(struct usb_device *hdev,
555*4882a593Smuzhiyun 		struct usb_hub_status *data)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun 	int i, status = -ETIMEDOUT;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	for (i = 0; i < USB_STS_RETRIES &&
560*4882a593Smuzhiyun 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
561*4882a593Smuzhiyun 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
562*4882a593Smuzhiyun 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
563*4882a593Smuzhiyun 			data, sizeof(*data), USB_STS_TIMEOUT);
564*4882a593Smuzhiyun 	}
565*4882a593Smuzhiyun 	return status;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun /*
569*4882a593Smuzhiyun  * USB 2.0 spec Section 11.24.2.7
570*4882a593Smuzhiyun  * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
571*4882a593Smuzhiyun  */
get_port_status(struct usb_device * hdev,int port1,void * data,u16 value,u16 length)572*4882a593Smuzhiyun static int get_port_status(struct usb_device *hdev, int port1,
573*4882a593Smuzhiyun 			   void *data, u16 value, u16 length)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 	int i, status = -ETIMEDOUT;
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	for (i = 0; i < USB_STS_RETRIES &&
578*4882a593Smuzhiyun 			(status == -ETIMEDOUT || status == -EPIPE); i++) {
579*4882a593Smuzhiyun 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
580*4882a593Smuzhiyun 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
581*4882a593Smuzhiyun 			port1, data, length, USB_STS_TIMEOUT);
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 	return status;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
hub_ext_port_status(struct usb_hub * hub,int port1,int type,u16 * status,u16 * change,u32 * ext_status)586*4882a593Smuzhiyun static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
587*4882a593Smuzhiyun 			       u16 *status, u16 *change, u32 *ext_status)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	int ret;
590*4882a593Smuzhiyun 	int len = 4;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (type != HUB_PORT_STATUS)
593*4882a593Smuzhiyun 		len = 8;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	mutex_lock(&hub->status_mutex);
596*4882a593Smuzhiyun 	ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
597*4882a593Smuzhiyun 	if (ret < len) {
598*4882a593Smuzhiyun 		if (ret != -ENODEV)
599*4882a593Smuzhiyun 			dev_err(hub->intfdev,
600*4882a593Smuzhiyun 				"%s failed (err = %d)\n", __func__, ret);
601*4882a593Smuzhiyun 		if (ret >= 0)
602*4882a593Smuzhiyun 			ret = -EIO;
603*4882a593Smuzhiyun 	} else {
604*4882a593Smuzhiyun 		*status = le16_to_cpu(hub->status->port.wPortStatus);
605*4882a593Smuzhiyun 		*change = le16_to_cpu(hub->status->port.wPortChange);
606*4882a593Smuzhiyun 		if (type != HUB_PORT_STATUS && ext_status)
607*4882a593Smuzhiyun 			*ext_status = le32_to_cpu(
608*4882a593Smuzhiyun 				hub->status->port.dwExtPortStatus);
609*4882a593Smuzhiyun 		ret = 0;
610*4882a593Smuzhiyun 	}
611*4882a593Smuzhiyun 	mutex_unlock(&hub->status_mutex);
612*4882a593Smuzhiyun 	return ret;
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun 
hub_port_status(struct usb_hub * hub,int port1,u16 * status,u16 * change)615*4882a593Smuzhiyun static int hub_port_status(struct usb_hub *hub, int port1,
616*4882a593Smuzhiyun 		u16 *status, u16 *change)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
619*4882a593Smuzhiyun 				   status, change, NULL);
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
hub_resubmit_irq_urb(struct usb_hub * hub)622*4882a593Smuzhiyun static void hub_resubmit_irq_urb(struct usb_hub *hub)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	unsigned long flags;
625*4882a593Smuzhiyun 	int status;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	spin_lock_irqsave(&hub->irq_urb_lock, flags);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	if (hub->quiescing) {
630*4882a593Smuzhiyun 		spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
631*4882a593Smuzhiyun 		return;
632*4882a593Smuzhiyun 	}
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	status = usb_submit_urb(hub->urb, GFP_ATOMIC);
635*4882a593Smuzhiyun 	if (status && status != -ENODEV && status != -EPERM &&
636*4882a593Smuzhiyun 	    status != -ESHUTDOWN) {
637*4882a593Smuzhiyun 		dev_err(hub->intfdev, "resubmit --> %d\n", status);
638*4882a593Smuzhiyun 		mod_timer(&hub->irq_urb_retry, jiffies + HZ);
639*4882a593Smuzhiyun 	}
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun 
hub_retry_irq_urb(struct timer_list * t)644*4882a593Smuzhiyun static void hub_retry_irq_urb(struct timer_list *t)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	struct usb_hub *hub = from_timer(hub, t, irq_urb_retry);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	hub_resubmit_irq_urb(hub);
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 
kick_hub_wq(struct usb_hub * hub)652*4882a593Smuzhiyun static void kick_hub_wq(struct usb_hub *hub)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun 	struct usb_interface *intf;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (hub->disconnected || work_pending(&hub->events))
657*4882a593Smuzhiyun 		return;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	/*
660*4882a593Smuzhiyun 	 * Suppress autosuspend until the event is proceed.
661*4882a593Smuzhiyun 	 *
662*4882a593Smuzhiyun 	 * Be careful and make sure that the symmetric operation is
663*4882a593Smuzhiyun 	 * always called. We are here only when there is no pending
664*4882a593Smuzhiyun 	 * work for this hub. Therefore put the interface either when
665*4882a593Smuzhiyun 	 * the new work is called or when it is canceled.
666*4882a593Smuzhiyun 	 */
667*4882a593Smuzhiyun 	intf = to_usb_interface(hub->intfdev);
668*4882a593Smuzhiyun 	usb_autopm_get_interface_no_resume(intf);
669*4882a593Smuzhiyun 	kref_get(&hub->kref);
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	if (queue_work(hub_wq, &hub->events))
672*4882a593Smuzhiyun 		return;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	/* the work has already been scheduled */
675*4882a593Smuzhiyun 	usb_autopm_put_interface_async(intf);
676*4882a593Smuzhiyun 	kref_put(&hub->kref, hub_release);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
usb_kick_hub_wq(struct usb_device * hdev)679*4882a593Smuzhiyun void usb_kick_hub_wq(struct usb_device *hdev)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	if (hub)
684*4882a593Smuzhiyun 		kick_hub_wq(hub);
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun /*
688*4882a593Smuzhiyun  * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
689*4882a593Smuzhiyun  * Notification, which indicates it had initiated remote wakeup.
690*4882a593Smuzhiyun  *
691*4882a593Smuzhiyun  * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
692*4882a593Smuzhiyun  * device initiates resume, so the USB core will not receive notice of the
693*4882a593Smuzhiyun  * resume through the normal hub interrupt URB.
694*4882a593Smuzhiyun  */
usb_wakeup_notification(struct usb_device * hdev,unsigned int portnum)695*4882a593Smuzhiyun void usb_wakeup_notification(struct usb_device *hdev,
696*4882a593Smuzhiyun 		unsigned int portnum)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	struct usb_hub *hub;
699*4882a593Smuzhiyun 	struct usb_port *port_dev;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	if (!hdev)
702*4882a593Smuzhiyun 		return;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	hub = usb_hub_to_struct_hub(hdev);
705*4882a593Smuzhiyun 	if (hub) {
706*4882a593Smuzhiyun 		port_dev = hub->ports[portnum - 1];
707*4882a593Smuzhiyun 		if (port_dev && port_dev->child)
708*4882a593Smuzhiyun 			pm_wakeup_event(&port_dev->child->dev, 0);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 		set_bit(portnum, hub->wakeup_bits);
711*4882a593Smuzhiyun 		kick_hub_wq(hub);
712*4882a593Smuzhiyun 	}
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_wakeup_notification);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun /* completion function, fires on port status changes and various faults */
hub_irq(struct urb * urb)717*4882a593Smuzhiyun static void hub_irq(struct urb *urb)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	struct usb_hub *hub = urb->context;
720*4882a593Smuzhiyun 	int status = urb->status;
721*4882a593Smuzhiyun 	unsigned i;
722*4882a593Smuzhiyun 	unsigned long bits;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	switch (status) {
725*4882a593Smuzhiyun 	case -ENOENT:		/* synchronous unlink */
726*4882a593Smuzhiyun 	case -ECONNRESET:	/* async unlink */
727*4882a593Smuzhiyun 	case -ESHUTDOWN:	/* hardware going away */
728*4882a593Smuzhiyun 		return;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	default:		/* presumably an error */
731*4882a593Smuzhiyun 		/* Cause a hub reset after 10 consecutive errors */
732*4882a593Smuzhiyun 		dev_dbg(hub->intfdev, "transfer --> %d\n", status);
733*4882a593Smuzhiyun 		if ((++hub->nerrors < 10) || hub->error)
734*4882a593Smuzhiyun 			goto resubmit;
735*4882a593Smuzhiyun 		hub->error = status;
736*4882a593Smuzhiyun 		fallthrough;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	/* let hub_wq handle things */
739*4882a593Smuzhiyun 	case 0:			/* we got data:  port status changed */
740*4882a593Smuzhiyun 		bits = 0;
741*4882a593Smuzhiyun 		for (i = 0; i < urb->actual_length; ++i)
742*4882a593Smuzhiyun 			bits |= ((unsigned long) ((*hub->buffer)[i]))
743*4882a593Smuzhiyun 					<< (i*8);
744*4882a593Smuzhiyun 		hub->event_bits[0] = bits;
745*4882a593Smuzhiyun 		break;
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	hub->nerrors = 0;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/* Something happened, let hub_wq figure it out */
751*4882a593Smuzhiyun 	kick_hub_wq(hub);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun resubmit:
754*4882a593Smuzhiyun 	hub_resubmit_irq_urb(hub);
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun /* USB 2.0 spec Section 11.24.2.3 */
758*4882a593Smuzhiyun static inline int
hub_clear_tt_buffer(struct usb_device * hdev,u16 devinfo,u16 tt)759*4882a593Smuzhiyun hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun 	/* Need to clear both directions for control ep */
762*4882a593Smuzhiyun 	if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
763*4882a593Smuzhiyun 			USB_ENDPOINT_XFER_CONTROL) {
764*4882a593Smuzhiyun 		int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
765*4882a593Smuzhiyun 				HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
766*4882a593Smuzhiyun 				devinfo ^ 0x8000, tt, NULL, 0, 1000);
767*4882a593Smuzhiyun 		if (status)
768*4882a593Smuzhiyun 			return status;
769*4882a593Smuzhiyun 	}
770*4882a593Smuzhiyun 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
771*4882a593Smuzhiyun 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
772*4882a593Smuzhiyun 			       tt, NULL, 0, 1000);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun /*
776*4882a593Smuzhiyun  * enumeration blocks hub_wq for a long time. we use keventd instead, since
777*4882a593Smuzhiyun  * long blocking there is the exception, not the rule.  accordingly, HCDs
778*4882a593Smuzhiyun  * talking to TTs must queue control transfers (not just bulk and iso), so
779*4882a593Smuzhiyun  * both can talk to the same hub concurrently.
780*4882a593Smuzhiyun  */
hub_tt_work(struct work_struct * work)781*4882a593Smuzhiyun static void hub_tt_work(struct work_struct *work)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun 	struct usb_hub		*hub =
784*4882a593Smuzhiyun 		container_of(work, struct usb_hub, tt.clear_work);
785*4882a593Smuzhiyun 	unsigned long		flags;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	spin_lock_irqsave(&hub->tt.lock, flags);
788*4882a593Smuzhiyun 	while (!list_empty(&hub->tt.clear_list)) {
789*4882a593Smuzhiyun 		struct list_head	*next;
790*4882a593Smuzhiyun 		struct usb_tt_clear	*clear;
791*4882a593Smuzhiyun 		struct usb_device	*hdev = hub->hdev;
792*4882a593Smuzhiyun 		const struct hc_driver	*drv;
793*4882a593Smuzhiyun 		int			status;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 		next = hub->tt.clear_list.next;
796*4882a593Smuzhiyun 		clear = list_entry(next, struct usb_tt_clear, clear_list);
797*4882a593Smuzhiyun 		list_del(&clear->clear_list);
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 		/* drop lock so HCD can concurrently report other TT errors */
800*4882a593Smuzhiyun 		spin_unlock_irqrestore(&hub->tt.lock, flags);
801*4882a593Smuzhiyun 		status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
802*4882a593Smuzhiyun 		if (status && status != -ENODEV)
803*4882a593Smuzhiyun 			dev_err(&hdev->dev,
804*4882a593Smuzhiyun 				"clear tt %d (%04x) error %d\n",
805*4882a593Smuzhiyun 				clear->tt, clear->devinfo, status);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 		/* Tell the HCD, even if the operation failed */
808*4882a593Smuzhiyun 		drv = clear->hcd->driver;
809*4882a593Smuzhiyun 		if (drv->clear_tt_buffer_complete)
810*4882a593Smuzhiyun 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 		kfree(clear);
813*4882a593Smuzhiyun 		spin_lock_irqsave(&hub->tt.lock, flags);
814*4882a593Smuzhiyun 	}
815*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hub->tt.lock, flags);
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun /**
819*4882a593Smuzhiyun  * usb_hub_set_port_power - control hub port's power state
820*4882a593Smuzhiyun  * @hdev: USB device belonging to the usb hub
821*4882a593Smuzhiyun  * @hub: target hub
822*4882a593Smuzhiyun  * @port1: port index
823*4882a593Smuzhiyun  * @set: expected status
824*4882a593Smuzhiyun  *
825*4882a593Smuzhiyun  * call this function to control port's power via setting or
826*4882a593Smuzhiyun  * clearing the port's PORT_POWER feature.
827*4882a593Smuzhiyun  *
828*4882a593Smuzhiyun  * Return: 0 if successful. A negative error code otherwise.
829*4882a593Smuzhiyun  */
usb_hub_set_port_power(struct usb_device * hdev,struct usb_hub * hub,int port1,bool set)830*4882a593Smuzhiyun int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
831*4882a593Smuzhiyun 			   int port1, bool set)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	int ret;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	if (set)
836*4882a593Smuzhiyun 		ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
837*4882a593Smuzhiyun 	else
838*4882a593Smuzhiyun 		ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	if (ret)
841*4882a593Smuzhiyun 		return ret;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	if (set)
844*4882a593Smuzhiyun 		set_bit(port1, hub->power_bits);
845*4882a593Smuzhiyun 	else
846*4882a593Smuzhiyun 		clear_bit(port1, hub->power_bits);
847*4882a593Smuzhiyun 	return 0;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun /**
851*4882a593Smuzhiyun  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
852*4882a593Smuzhiyun  * @urb: an URB associated with the failed or incomplete split transaction
853*4882a593Smuzhiyun  *
854*4882a593Smuzhiyun  * High speed HCDs use this to tell the hub driver that some split control or
855*4882a593Smuzhiyun  * bulk transaction failed in a way that requires clearing internal state of
856*4882a593Smuzhiyun  * a transaction translator.  This is normally detected (and reported) from
857*4882a593Smuzhiyun  * interrupt context.
858*4882a593Smuzhiyun  *
859*4882a593Smuzhiyun  * It may not be possible for that hub to handle additional full (or low)
860*4882a593Smuzhiyun  * speed transactions until that state is fully cleared out.
861*4882a593Smuzhiyun  *
862*4882a593Smuzhiyun  * Return: 0 if successful. A negative error code otherwise.
863*4882a593Smuzhiyun  */
usb_hub_clear_tt_buffer(struct urb * urb)864*4882a593Smuzhiyun int usb_hub_clear_tt_buffer(struct urb *urb)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	struct usb_device	*udev = urb->dev;
867*4882a593Smuzhiyun 	int			pipe = urb->pipe;
868*4882a593Smuzhiyun 	struct usb_tt		*tt = udev->tt;
869*4882a593Smuzhiyun 	unsigned long		flags;
870*4882a593Smuzhiyun 	struct usb_tt_clear	*clear;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	/* we've got to cope with an arbitrary number of pending TT clears,
873*4882a593Smuzhiyun 	 * since each TT has "at least two" buffers that can need it (and
874*4882a593Smuzhiyun 	 * there can be many TTs per hub).  even if they're uncommon.
875*4882a593Smuzhiyun 	 */
876*4882a593Smuzhiyun 	clear = kmalloc(sizeof *clear, GFP_ATOMIC);
877*4882a593Smuzhiyun 	if (clear == NULL) {
878*4882a593Smuzhiyun 		dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
879*4882a593Smuzhiyun 		/* FIXME recover somehow ... RESET_TT? */
880*4882a593Smuzhiyun 		return -ENOMEM;
881*4882a593Smuzhiyun 	}
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	/* info that CLEAR_TT_BUFFER needs */
884*4882a593Smuzhiyun 	clear->tt = tt->multi ? udev->ttport : 1;
885*4882a593Smuzhiyun 	clear->devinfo = usb_pipeendpoint (pipe);
886*4882a593Smuzhiyun 	clear->devinfo |= ((u16)udev->devaddr) << 4;
887*4882a593Smuzhiyun 	clear->devinfo |= usb_pipecontrol(pipe)
888*4882a593Smuzhiyun 			? (USB_ENDPOINT_XFER_CONTROL << 11)
889*4882a593Smuzhiyun 			: (USB_ENDPOINT_XFER_BULK << 11);
890*4882a593Smuzhiyun 	if (usb_pipein(pipe))
891*4882a593Smuzhiyun 		clear->devinfo |= 1 << 15;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	/* info for completion callback */
894*4882a593Smuzhiyun 	clear->hcd = bus_to_hcd(udev->bus);
895*4882a593Smuzhiyun 	clear->ep = urb->ep;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	/* tell keventd to clear state for this TT */
898*4882a593Smuzhiyun 	spin_lock_irqsave(&tt->lock, flags);
899*4882a593Smuzhiyun 	list_add_tail(&clear->clear_list, &tt->clear_list);
900*4882a593Smuzhiyun 	schedule_work(&tt->clear_work);
901*4882a593Smuzhiyun 	spin_unlock_irqrestore(&tt->lock, flags);
902*4882a593Smuzhiyun 	return 0;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
905*4882a593Smuzhiyun 
hub_power_on(struct usb_hub * hub,bool do_delay)906*4882a593Smuzhiyun static void hub_power_on(struct usb_hub *hub, bool do_delay)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun 	int port1;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	/* Enable power on each port.  Some hubs have reserved values
911*4882a593Smuzhiyun 	 * of LPSM (> 2) in their descriptors, even though they are
912*4882a593Smuzhiyun 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
913*4882a593Smuzhiyun 	 * but only emulate it.  In all cases, the ports won't work
914*4882a593Smuzhiyun 	 * unless we send these messages to the hub.
915*4882a593Smuzhiyun 	 */
916*4882a593Smuzhiyun 	if (hub_is_port_power_switchable(hub))
917*4882a593Smuzhiyun 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
918*4882a593Smuzhiyun 	else
919*4882a593Smuzhiyun 		dev_dbg(hub->intfdev, "trying to enable port power on "
920*4882a593Smuzhiyun 				"non-switchable hub\n");
921*4882a593Smuzhiyun 	for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
922*4882a593Smuzhiyun 		if (test_bit(port1, hub->power_bits))
923*4882a593Smuzhiyun 			set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
924*4882a593Smuzhiyun 		else
925*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
926*4882a593Smuzhiyun 						USB_PORT_FEAT_POWER);
927*4882a593Smuzhiyun 	if (do_delay)
928*4882a593Smuzhiyun 		msleep(hub_power_on_good_delay(hub));
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun 
hub_hub_status(struct usb_hub * hub,u16 * status,u16 * change)931*4882a593Smuzhiyun static int hub_hub_status(struct usb_hub *hub,
932*4882a593Smuzhiyun 		u16 *status, u16 *change)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun 	int ret;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	mutex_lock(&hub->status_mutex);
937*4882a593Smuzhiyun 	ret = get_hub_status(hub->hdev, &hub->status->hub);
938*4882a593Smuzhiyun 	if (ret < 0) {
939*4882a593Smuzhiyun 		if (ret != -ENODEV)
940*4882a593Smuzhiyun 			dev_err(hub->intfdev,
941*4882a593Smuzhiyun 				"%s failed (err = %d)\n", __func__, ret);
942*4882a593Smuzhiyun 	} else {
943*4882a593Smuzhiyun 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
944*4882a593Smuzhiyun 		*change = le16_to_cpu(hub->status->hub.wHubChange);
945*4882a593Smuzhiyun 		ret = 0;
946*4882a593Smuzhiyun 	}
947*4882a593Smuzhiyun 	mutex_unlock(&hub->status_mutex);
948*4882a593Smuzhiyun 	return ret;
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun 
hub_set_port_link_state(struct usb_hub * hub,int port1,unsigned int link_status)951*4882a593Smuzhiyun static int hub_set_port_link_state(struct usb_hub *hub, int port1,
952*4882a593Smuzhiyun 			unsigned int link_status)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun 	return set_port_feature(hub->hdev,
955*4882a593Smuzhiyun 			port1 | (link_status << 3),
956*4882a593Smuzhiyun 			USB_PORT_FEAT_LINK_STATE);
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun /*
960*4882a593Smuzhiyun  * Disable a port and mark a logical connect-change event, so that some
961*4882a593Smuzhiyun  * time later hub_wq will disconnect() any existing usb_device on the port
962*4882a593Smuzhiyun  * and will re-enumerate if there actually is a device attached.
963*4882a593Smuzhiyun  */
hub_port_logical_disconnect(struct usb_hub * hub,int port1)964*4882a593Smuzhiyun static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
967*4882a593Smuzhiyun 	hub_port_disable(hub, port1, 1);
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	/* FIXME let caller ask to power down the port:
970*4882a593Smuzhiyun 	 *  - some devices won't enumerate without a VBUS power cycle
971*4882a593Smuzhiyun 	 *  - SRP saves power that way
972*4882a593Smuzhiyun 	 *  - ... new call, TBD ...
973*4882a593Smuzhiyun 	 * That's easy if this hub can switch power per-port, and
974*4882a593Smuzhiyun 	 * hub_wq reactivates the port later (timer, SRP, etc).
975*4882a593Smuzhiyun 	 * Powerdown must be optional, because of reset/DFU.
976*4882a593Smuzhiyun 	 */
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	set_bit(port1, hub->change_bits);
979*4882a593Smuzhiyun 	kick_hub_wq(hub);
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun /**
983*4882a593Smuzhiyun  * usb_remove_device - disable a device's port on its parent hub
984*4882a593Smuzhiyun  * @udev: device to be disabled and removed
985*4882a593Smuzhiyun  * Context: @udev locked, must be able to sleep.
986*4882a593Smuzhiyun  *
987*4882a593Smuzhiyun  * After @udev's port has been disabled, hub_wq is notified and it will
988*4882a593Smuzhiyun  * see that the device has been disconnected.  When the device is
989*4882a593Smuzhiyun  * physically unplugged and something is plugged in, the events will
990*4882a593Smuzhiyun  * be received and processed normally.
991*4882a593Smuzhiyun  *
992*4882a593Smuzhiyun  * Return: 0 if successful. A negative error code otherwise.
993*4882a593Smuzhiyun  */
usb_remove_device(struct usb_device * udev)994*4882a593Smuzhiyun int usb_remove_device(struct usb_device *udev)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun 	struct usb_hub *hub;
997*4882a593Smuzhiyun 	struct usb_interface *intf;
998*4882a593Smuzhiyun 	int ret;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	if (!udev->parent)	/* Can't remove a root hub */
1001*4882a593Smuzhiyun 		return -EINVAL;
1002*4882a593Smuzhiyun 	hub = usb_hub_to_struct_hub(udev->parent);
1003*4882a593Smuzhiyun 	intf = to_usb_interface(hub->intfdev);
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	ret = usb_autopm_get_interface(intf);
1006*4882a593Smuzhiyun 	if (ret < 0)
1007*4882a593Smuzhiyun 		return ret;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	set_bit(udev->portnum, hub->removed_bits);
1010*4882a593Smuzhiyun 	hub_port_logical_disconnect(hub, udev->portnum);
1011*4882a593Smuzhiyun 	usb_autopm_put_interface(intf);
1012*4882a593Smuzhiyun 	return 0;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun enum hub_activation_type {
1016*4882a593Smuzhiyun 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
1017*4882a593Smuzhiyun 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
1018*4882a593Smuzhiyun };
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun static void hub_init_func2(struct work_struct *ws);
1021*4882a593Smuzhiyun static void hub_init_func3(struct work_struct *ws);
1022*4882a593Smuzhiyun 
hub_activate(struct usb_hub * hub,enum hub_activation_type type)1023*4882a593Smuzhiyun static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1024*4882a593Smuzhiyun {
1025*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
1026*4882a593Smuzhiyun 	struct usb_hcd *hcd;
1027*4882a593Smuzhiyun 	int ret;
1028*4882a593Smuzhiyun 	int port1;
1029*4882a593Smuzhiyun 	int status;
1030*4882a593Smuzhiyun 	bool need_debounce_delay = false;
1031*4882a593Smuzhiyun 	unsigned delay;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	/* Continue a partial initialization */
1034*4882a593Smuzhiyun 	if (type == HUB_INIT2 || type == HUB_INIT3) {
1035*4882a593Smuzhiyun 		device_lock(&hdev->dev);
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 		/* Was the hub disconnected while we were waiting? */
1038*4882a593Smuzhiyun 		if (hub->disconnected)
1039*4882a593Smuzhiyun 			goto disconnected;
1040*4882a593Smuzhiyun 		if (type == HUB_INIT2)
1041*4882a593Smuzhiyun 			goto init2;
1042*4882a593Smuzhiyun 		goto init3;
1043*4882a593Smuzhiyun 	}
1044*4882a593Smuzhiyun 	kref_get(&hub->kref);
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	/* The superspeed hub except for root hub has to use Hub Depth
1047*4882a593Smuzhiyun 	 * value as an offset into the route string to locate the bits
1048*4882a593Smuzhiyun 	 * it uses to determine the downstream port number. So hub driver
1049*4882a593Smuzhiyun 	 * should send a set hub depth request to superspeed hub after
1050*4882a593Smuzhiyun 	 * the superspeed hub is set configuration in initialization or
1051*4882a593Smuzhiyun 	 * reset procedure.
1052*4882a593Smuzhiyun 	 *
1053*4882a593Smuzhiyun 	 * After a resume, port power should still be on.
1054*4882a593Smuzhiyun 	 * For any other type of activation, turn it on.
1055*4882a593Smuzhiyun 	 */
1056*4882a593Smuzhiyun 	if (type != HUB_RESUME) {
1057*4882a593Smuzhiyun 		if (hdev->parent && hub_is_superspeed(hdev)) {
1058*4882a593Smuzhiyun 			ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1059*4882a593Smuzhiyun 					HUB_SET_DEPTH, USB_RT_HUB,
1060*4882a593Smuzhiyun 					hdev->level - 1, 0, NULL, 0,
1061*4882a593Smuzhiyun 					USB_CTRL_SET_TIMEOUT);
1062*4882a593Smuzhiyun 			if (ret < 0)
1063*4882a593Smuzhiyun 				dev_err(hub->intfdev,
1064*4882a593Smuzhiyun 						"set hub depth failed\n");
1065*4882a593Smuzhiyun 		}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 		/* Speed up system boot by using a delayed_work for the
1068*4882a593Smuzhiyun 		 * hub's initial power-up delays.  This is pretty awkward
1069*4882a593Smuzhiyun 		 * and the implementation looks like a home-brewed sort of
1070*4882a593Smuzhiyun 		 * setjmp/longjmp, but it saves at least 100 ms for each
1071*4882a593Smuzhiyun 		 * root hub (assuming usbcore is compiled into the kernel
1072*4882a593Smuzhiyun 		 * rather than as a module).  It adds up.
1073*4882a593Smuzhiyun 		 *
1074*4882a593Smuzhiyun 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1075*4882a593Smuzhiyun 		 * because for those activation types the ports have to be
1076*4882a593Smuzhiyun 		 * operational when we return.  In theory this could be done
1077*4882a593Smuzhiyun 		 * for HUB_POST_RESET, but it's easier not to.
1078*4882a593Smuzhiyun 		 */
1079*4882a593Smuzhiyun 		if (type == HUB_INIT) {
1080*4882a593Smuzhiyun 			delay = hub_power_on_good_delay(hub);
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 			hub_power_on(hub, false);
1083*4882a593Smuzhiyun 			INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
1084*4882a593Smuzhiyun 			queue_delayed_work(system_power_efficient_wq,
1085*4882a593Smuzhiyun 					&hub->init_work,
1086*4882a593Smuzhiyun 					msecs_to_jiffies(delay));
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 			/* Suppress autosuspend until init is done */
1089*4882a593Smuzhiyun 			usb_autopm_get_interface_no_resume(
1090*4882a593Smuzhiyun 					to_usb_interface(hub->intfdev));
1091*4882a593Smuzhiyun 			return;		/* Continues at init2: below */
1092*4882a593Smuzhiyun 		} else if (type == HUB_RESET_RESUME) {
1093*4882a593Smuzhiyun 			/* The internal host controller state for the hub device
1094*4882a593Smuzhiyun 			 * may be gone after a host power loss on system resume.
1095*4882a593Smuzhiyun 			 * Update the device's info so the HW knows it's a hub.
1096*4882a593Smuzhiyun 			 */
1097*4882a593Smuzhiyun 			hcd = bus_to_hcd(hdev->bus);
1098*4882a593Smuzhiyun 			if (hcd->driver->update_hub_device) {
1099*4882a593Smuzhiyun 				ret = hcd->driver->update_hub_device(hcd, hdev,
1100*4882a593Smuzhiyun 						&hub->tt, GFP_NOIO);
1101*4882a593Smuzhiyun 				if (ret < 0) {
1102*4882a593Smuzhiyun 					dev_err(hub->intfdev,
1103*4882a593Smuzhiyun 						"Host not accepting hub info update\n");
1104*4882a593Smuzhiyun 					dev_err(hub->intfdev,
1105*4882a593Smuzhiyun 						"LS/FS devices and hubs may not work under this hub\n");
1106*4882a593Smuzhiyun 				}
1107*4882a593Smuzhiyun 			}
1108*4882a593Smuzhiyun 			hub_power_on(hub, true);
1109*4882a593Smuzhiyun 		} else {
1110*4882a593Smuzhiyun 			hub_power_on(hub, true);
1111*4882a593Smuzhiyun 		}
1112*4882a593Smuzhiyun 	/* Give some time on remote wakeup to let links to transit to U0 */
1113*4882a593Smuzhiyun 	} else if (hub_is_superspeed(hub->hdev))
1114*4882a593Smuzhiyun 		msleep(20);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun  init2:
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	/*
1119*4882a593Smuzhiyun 	 * Check each port and set hub->change_bits to let hub_wq know
1120*4882a593Smuzhiyun 	 * which ports need attention.
1121*4882a593Smuzhiyun 	 */
1122*4882a593Smuzhiyun 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1123*4882a593Smuzhiyun 		struct usb_port *port_dev = hub->ports[port1 - 1];
1124*4882a593Smuzhiyun 		struct usb_device *udev = port_dev->child;
1125*4882a593Smuzhiyun 		u16 portstatus, portchange;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 		portstatus = portchange = 0;
1128*4882a593Smuzhiyun 		status = hub_port_status(hub, port1, &portstatus, &portchange);
1129*4882a593Smuzhiyun 		if (status)
1130*4882a593Smuzhiyun 			goto abort;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1133*4882a593Smuzhiyun 			dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1134*4882a593Smuzhiyun 					portstatus, portchange);
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 		/*
1137*4882a593Smuzhiyun 		 * After anything other than HUB_RESUME (i.e., initialization
1138*4882a593Smuzhiyun 		 * or any sort of reset), every port should be disabled.
1139*4882a593Smuzhiyun 		 * Unconnected ports should likewise be disabled (paranoia),
1140*4882a593Smuzhiyun 		 * and so should ports for which we have no usb_device.
1141*4882a593Smuzhiyun 		 */
1142*4882a593Smuzhiyun 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
1143*4882a593Smuzhiyun 				type != HUB_RESUME ||
1144*4882a593Smuzhiyun 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
1145*4882a593Smuzhiyun 				!udev ||
1146*4882a593Smuzhiyun 				udev->state == USB_STATE_NOTATTACHED)) {
1147*4882a593Smuzhiyun 			/*
1148*4882a593Smuzhiyun 			 * USB3 protocol ports will automatically transition
1149*4882a593Smuzhiyun 			 * to Enabled state when detect an USB3.0 device attach.
1150*4882a593Smuzhiyun 			 * Do not disable USB3 protocol ports, just pretend
1151*4882a593Smuzhiyun 			 * power was lost
1152*4882a593Smuzhiyun 			 */
1153*4882a593Smuzhiyun 			portstatus &= ~USB_PORT_STAT_ENABLE;
1154*4882a593Smuzhiyun 			if (!hub_is_superspeed(hdev))
1155*4882a593Smuzhiyun 				usb_clear_port_feature(hdev, port1,
1156*4882a593Smuzhiyun 						   USB_PORT_FEAT_ENABLE);
1157*4882a593Smuzhiyun 		}
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 		/* Make sure a warm-reset request is handled by port_event */
1160*4882a593Smuzhiyun 		if (type == HUB_RESUME &&
1161*4882a593Smuzhiyun 		    hub_port_warm_reset_required(hub, port1, portstatus))
1162*4882a593Smuzhiyun 			set_bit(port1, hub->event_bits);
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 		/*
1165*4882a593Smuzhiyun 		 * Add debounce if USB3 link is in polling/link training state.
1166*4882a593Smuzhiyun 		 * Link will automatically transition to Enabled state after
1167*4882a593Smuzhiyun 		 * link training completes.
1168*4882a593Smuzhiyun 		 */
1169*4882a593Smuzhiyun 		if (hub_is_superspeed(hdev) &&
1170*4882a593Smuzhiyun 		    ((portstatus & USB_PORT_STAT_LINK_STATE) ==
1171*4882a593Smuzhiyun 						USB_SS_PORT_LS_POLLING))
1172*4882a593Smuzhiyun 			need_debounce_delay = true;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 		/* Clear status-change flags; we'll debounce later */
1175*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
1176*4882a593Smuzhiyun 			need_debounce_delay = true;
1177*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
1178*4882a593Smuzhiyun 					USB_PORT_FEAT_C_CONNECTION);
1179*4882a593Smuzhiyun 		}
1180*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_ENABLE) {
1181*4882a593Smuzhiyun 			need_debounce_delay = true;
1182*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
1183*4882a593Smuzhiyun 					USB_PORT_FEAT_C_ENABLE);
1184*4882a593Smuzhiyun 		}
1185*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_RESET) {
1186*4882a593Smuzhiyun 			need_debounce_delay = true;
1187*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
1188*4882a593Smuzhiyun 					USB_PORT_FEAT_C_RESET);
1189*4882a593Smuzhiyun 		}
1190*4882a593Smuzhiyun 		if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1191*4882a593Smuzhiyun 				hub_is_superspeed(hub->hdev)) {
1192*4882a593Smuzhiyun 			need_debounce_delay = true;
1193*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
1194*4882a593Smuzhiyun 					USB_PORT_FEAT_C_BH_PORT_RESET);
1195*4882a593Smuzhiyun 		}
1196*4882a593Smuzhiyun 		/* We can forget about a "removed" device when there's a
1197*4882a593Smuzhiyun 		 * physical disconnect or the connect status changes.
1198*4882a593Smuzhiyun 		 */
1199*4882a593Smuzhiyun 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1200*4882a593Smuzhiyun 				(portchange & USB_PORT_STAT_C_CONNECTION))
1201*4882a593Smuzhiyun 			clear_bit(port1, hub->removed_bits);
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1204*4882a593Smuzhiyun 			/* Tell hub_wq to disconnect the device or
1205*4882a593Smuzhiyun 			 * check for a new connection or over current condition.
1206*4882a593Smuzhiyun 			 * Based on USB2.0 Spec Section 11.12.5,
1207*4882a593Smuzhiyun 			 * C_PORT_OVER_CURRENT could be set while
1208*4882a593Smuzhiyun 			 * PORT_OVER_CURRENT is not. So check for any of them.
1209*4882a593Smuzhiyun 			 */
1210*4882a593Smuzhiyun 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
1211*4882a593Smuzhiyun 			    (portchange & USB_PORT_STAT_C_CONNECTION) ||
1212*4882a593Smuzhiyun 			    (portstatus & USB_PORT_STAT_OVERCURRENT) ||
1213*4882a593Smuzhiyun 			    (portchange & USB_PORT_STAT_C_OVERCURRENT))
1214*4882a593Smuzhiyun 				set_bit(port1, hub->change_bits);
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
1217*4882a593Smuzhiyun 			bool port_resumed = (portstatus &
1218*4882a593Smuzhiyun 					USB_PORT_STAT_LINK_STATE) ==
1219*4882a593Smuzhiyun 				USB_SS_PORT_LS_U0;
1220*4882a593Smuzhiyun 			/* The power session apparently survived the resume.
1221*4882a593Smuzhiyun 			 * If there was an overcurrent or suspend change
1222*4882a593Smuzhiyun 			 * (i.e., remote wakeup request), have hub_wq
1223*4882a593Smuzhiyun 			 * take care of it.  Look at the port link state
1224*4882a593Smuzhiyun 			 * for USB 3.0 hubs, since they don't have a suspend
1225*4882a593Smuzhiyun 			 * change bit, and they don't set the port link change
1226*4882a593Smuzhiyun 			 * bit on device-initiated resume.
1227*4882a593Smuzhiyun 			 */
1228*4882a593Smuzhiyun 			if (portchange || (hub_is_superspeed(hub->hdev) &&
1229*4882a593Smuzhiyun 						port_resumed))
1230*4882a593Smuzhiyun 				set_bit(port1, hub->event_bits);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 		} else if (udev->persist_enabled) {
1233*4882a593Smuzhiyun #ifdef CONFIG_PM
1234*4882a593Smuzhiyun 			udev->reset_resume = 1;
1235*4882a593Smuzhiyun #endif
1236*4882a593Smuzhiyun 			/* Don't set the change_bits when the device
1237*4882a593Smuzhiyun 			 * was powered off.
1238*4882a593Smuzhiyun 			 */
1239*4882a593Smuzhiyun 			if (test_bit(port1, hub->power_bits))
1240*4882a593Smuzhiyun 				set_bit(port1, hub->change_bits);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 		} else {
1243*4882a593Smuzhiyun 			/* The power session is gone; tell hub_wq */
1244*4882a593Smuzhiyun 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1245*4882a593Smuzhiyun 			set_bit(port1, hub->change_bits);
1246*4882a593Smuzhiyun 		}
1247*4882a593Smuzhiyun 	}
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	/* If no port-status-change flags were set, we don't need any
1250*4882a593Smuzhiyun 	 * debouncing.  If flags were set we can try to debounce the
1251*4882a593Smuzhiyun 	 * ports all at once right now, instead of letting hub_wq do them
1252*4882a593Smuzhiyun 	 * one at a time later on.
1253*4882a593Smuzhiyun 	 *
1254*4882a593Smuzhiyun 	 * If any port-status changes do occur during this delay, hub_wq
1255*4882a593Smuzhiyun 	 * will see them later and handle them normally.
1256*4882a593Smuzhiyun 	 */
1257*4882a593Smuzhiyun 	if (need_debounce_delay) {
1258*4882a593Smuzhiyun 		delay = HUB_DEBOUNCE_STABLE;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 		/* Don't do a long sleep inside a workqueue routine */
1261*4882a593Smuzhiyun 		if (type == HUB_INIT2) {
1262*4882a593Smuzhiyun 			INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
1263*4882a593Smuzhiyun 			queue_delayed_work(system_power_efficient_wq,
1264*4882a593Smuzhiyun 					&hub->init_work,
1265*4882a593Smuzhiyun 					msecs_to_jiffies(delay));
1266*4882a593Smuzhiyun 			device_unlock(&hdev->dev);
1267*4882a593Smuzhiyun 			return;		/* Continues at init3: below */
1268*4882a593Smuzhiyun 		} else {
1269*4882a593Smuzhiyun 			msleep(delay);
1270*4882a593Smuzhiyun 		}
1271*4882a593Smuzhiyun 	}
1272*4882a593Smuzhiyun  init3:
1273*4882a593Smuzhiyun 	hub->quiescing = 0;
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	status = usb_submit_urb(hub->urb, GFP_NOIO);
1276*4882a593Smuzhiyun 	if (status < 0)
1277*4882a593Smuzhiyun 		dev_err(hub->intfdev, "activate --> %d\n", status);
1278*4882a593Smuzhiyun 	if (hub->has_indicators && blinkenlights)
1279*4882a593Smuzhiyun 		queue_delayed_work(system_power_efficient_wq,
1280*4882a593Smuzhiyun 				&hub->leds, LED_CYCLE_PERIOD);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	/* Scan all ports that need attention */
1283*4882a593Smuzhiyun 	kick_hub_wq(hub);
1284*4882a593Smuzhiyun  abort:
1285*4882a593Smuzhiyun 	if (type == HUB_INIT2 || type == HUB_INIT3) {
1286*4882a593Smuzhiyun 		/* Allow autosuspend if it was suppressed */
1287*4882a593Smuzhiyun  disconnected:
1288*4882a593Smuzhiyun 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
1289*4882a593Smuzhiyun 		device_unlock(&hdev->dev);
1290*4882a593Smuzhiyun 	}
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	kref_put(&hub->kref, hub_release);
1293*4882a593Smuzhiyun }
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun /* Implement the continuations for the delays above */
hub_init_func2(struct work_struct * ws)1296*4882a593Smuzhiyun static void hub_init_func2(struct work_struct *ws)
1297*4882a593Smuzhiyun {
1298*4882a593Smuzhiyun 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	hub_activate(hub, HUB_INIT2);
1301*4882a593Smuzhiyun }
1302*4882a593Smuzhiyun 
hub_init_func3(struct work_struct * ws)1303*4882a593Smuzhiyun static void hub_init_func3(struct work_struct *ws)
1304*4882a593Smuzhiyun {
1305*4882a593Smuzhiyun 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	hub_activate(hub, HUB_INIT3);
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun enum hub_quiescing_type {
1311*4882a593Smuzhiyun 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1312*4882a593Smuzhiyun };
1313*4882a593Smuzhiyun 
hub_quiesce(struct usb_hub * hub,enum hub_quiescing_type type)1314*4882a593Smuzhiyun static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1315*4882a593Smuzhiyun {
1316*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
1317*4882a593Smuzhiyun 	unsigned long flags;
1318*4882a593Smuzhiyun 	int i;
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	/* hub_wq and related activity won't re-trigger */
1321*4882a593Smuzhiyun 	spin_lock_irqsave(&hub->irq_urb_lock, flags);
1322*4882a593Smuzhiyun 	hub->quiescing = 1;
1323*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	if (type != HUB_SUSPEND) {
1326*4882a593Smuzhiyun 		/* Disconnect all the children */
1327*4882a593Smuzhiyun 		for (i = 0; i < hdev->maxchild; ++i) {
1328*4882a593Smuzhiyun 			if (hub->ports[i]->child)
1329*4882a593Smuzhiyun 				usb_disconnect(&hub->ports[i]->child);
1330*4882a593Smuzhiyun 		}
1331*4882a593Smuzhiyun 	}
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	/* Stop hub_wq and related activity */
1334*4882a593Smuzhiyun 	del_timer_sync(&hub->irq_urb_retry);
1335*4882a593Smuzhiyun 	usb_kill_urb(hub->urb);
1336*4882a593Smuzhiyun 	if (hub->has_indicators)
1337*4882a593Smuzhiyun 		cancel_delayed_work_sync(&hub->leds);
1338*4882a593Smuzhiyun 	if (hub->tt.hub)
1339*4882a593Smuzhiyun 		flush_work(&hub->tt.clear_work);
1340*4882a593Smuzhiyun }
1341*4882a593Smuzhiyun 
hub_pm_barrier_for_all_ports(struct usb_hub * hub)1342*4882a593Smuzhiyun static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1343*4882a593Smuzhiyun {
1344*4882a593Smuzhiyun 	int i;
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	for (i = 0; i < hub->hdev->maxchild; ++i)
1347*4882a593Smuzhiyun 		pm_runtime_barrier(&hub->ports[i]->dev);
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun /* caller has locked the hub device */
hub_pre_reset(struct usb_interface * intf)1351*4882a593Smuzhiyun static int hub_pre_reset(struct usb_interface *intf)
1352*4882a593Smuzhiyun {
1353*4882a593Smuzhiyun 	struct usb_hub *hub = usb_get_intfdata(intf);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	hub_quiesce(hub, HUB_PRE_RESET);
1356*4882a593Smuzhiyun 	hub->in_reset = 1;
1357*4882a593Smuzhiyun 	hub_pm_barrier_for_all_ports(hub);
1358*4882a593Smuzhiyun 	return 0;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun /* caller has locked the hub device */
hub_post_reset(struct usb_interface * intf)1362*4882a593Smuzhiyun static int hub_post_reset(struct usb_interface *intf)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun 	struct usb_hub *hub = usb_get_intfdata(intf);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	hub->in_reset = 0;
1367*4882a593Smuzhiyun 	hub_pm_barrier_for_all_ports(hub);
1368*4882a593Smuzhiyun 	hub_activate(hub, HUB_POST_RESET);
1369*4882a593Smuzhiyun 	return 0;
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun 
hub_configure(struct usb_hub * hub,struct usb_endpoint_descriptor * endpoint)1372*4882a593Smuzhiyun static int hub_configure(struct usb_hub *hub,
1373*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *endpoint)
1374*4882a593Smuzhiyun {
1375*4882a593Smuzhiyun 	struct usb_hcd *hcd;
1376*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
1377*4882a593Smuzhiyun 	struct device *hub_dev = hub->intfdev;
1378*4882a593Smuzhiyun 	u16 hubstatus, hubchange;
1379*4882a593Smuzhiyun 	u16 wHubCharacteristics;
1380*4882a593Smuzhiyun 	unsigned int pipe;
1381*4882a593Smuzhiyun 	int maxp, ret, i;
1382*4882a593Smuzhiyun 	char *message = "out of memory";
1383*4882a593Smuzhiyun 	unsigned unit_load;
1384*4882a593Smuzhiyun 	unsigned full_load;
1385*4882a593Smuzhiyun 	unsigned maxchild;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1388*4882a593Smuzhiyun 	if (!hub->buffer) {
1389*4882a593Smuzhiyun 		ret = -ENOMEM;
1390*4882a593Smuzhiyun 		goto fail;
1391*4882a593Smuzhiyun 	}
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1394*4882a593Smuzhiyun 	if (!hub->status) {
1395*4882a593Smuzhiyun 		ret = -ENOMEM;
1396*4882a593Smuzhiyun 		goto fail;
1397*4882a593Smuzhiyun 	}
1398*4882a593Smuzhiyun 	mutex_init(&hub->status_mutex);
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1401*4882a593Smuzhiyun 	if (!hub->descriptor) {
1402*4882a593Smuzhiyun 		ret = -ENOMEM;
1403*4882a593Smuzhiyun 		goto fail;
1404*4882a593Smuzhiyun 	}
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	/* Request the entire hub descriptor.
1407*4882a593Smuzhiyun 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
1408*4882a593Smuzhiyun 	 * but a (non-SS) hub can/will return fewer bytes here.
1409*4882a593Smuzhiyun 	 */
1410*4882a593Smuzhiyun 	ret = get_hub_descriptor(hdev, hub->descriptor);
1411*4882a593Smuzhiyun 	if (ret < 0) {
1412*4882a593Smuzhiyun 		message = "can't read hub descriptor";
1413*4882a593Smuzhiyun 		goto fail;
1414*4882a593Smuzhiyun 	}
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	maxchild = USB_MAXCHILDREN;
1417*4882a593Smuzhiyun 	if (hub_is_superspeed(hdev))
1418*4882a593Smuzhiyun 		maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS);
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	if (hub->descriptor->bNbrPorts > maxchild) {
1421*4882a593Smuzhiyun 		message = "hub has too many ports!";
1422*4882a593Smuzhiyun 		ret = -ENODEV;
1423*4882a593Smuzhiyun 		goto fail;
1424*4882a593Smuzhiyun 	} else if (hub->descriptor->bNbrPorts == 0) {
1425*4882a593Smuzhiyun 		message = "hub doesn't have any ports!";
1426*4882a593Smuzhiyun 		ret = -ENODEV;
1427*4882a593Smuzhiyun 		goto fail;
1428*4882a593Smuzhiyun 	}
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	/*
1431*4882a593Smuzhiyun 	 * Accumulate wHubDelay + 40ns for every hub in the tree of devices.
1432*4882a593Smuzhiyun 	 * The resulting value will be used for SetIsochDelay() request.
1433*4882a593Smuzhiyun 	 */
1434*4882a593Smuzhiyun 	if (hub_is_superspeed(hdev) || hub_is_superspeedplus(hdev)) {
1435*4882a593Smuzhiyun 		u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay);
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 		if (hdev->parent)
1438*4882a593Smuzhiyun 			delay += hdev->parent->hub_delay;
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 		delay += USB_TP_TRANSMISSION_DELAY;
1441*4882a593Smuzhiyun 		hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX);
1442*4882a593Smuzhiyun 	}
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	maxchild = hub->descriptor->bNbrPorts;
1445*4882a593Smuzhiyun 	dev_info(hub_dev, "%d port%s detected\n", maxchild,
1446*4882a593Smuzhiyun 			(maxchild == 1) ? "" : "s");
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL);
1449*4882a593Smuzhiyun 	if (!hub->ports) {
1450*4882a593Smuzhiyun 		ret = -ENOMEM;
1451*4882a593Smuzhiyun 		goto fail;
1452*4882a593Smuzhiyun 	}
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1455*4882a593Smuzhiyun 	if (hub_is_superspeed(hdev)) {
1456*4882a593Smuzhiyun 		unit_load = 150;
1457*4882a593Smuzhiyun 		full_load = 900;
1458*4882a593Smuzhiyun 	} else {
1459*4882a593Smuzhiyun 		unit_load = 100;
1460*4882a593Smuzhiyun 		full_load = 500;
1461*4882a593Smuzhiyun 	}
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	/* FIXME for USB 3.0, skip for now */
1464*4882a593Smuzhiyun 	if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1465*4882a593Smuzhiyun 			!(hub_is_superspeed(hdev))) {
1466*4882a593Smuzhiyun 		char	portstr[USB_MAXCHILDREN + 1];
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 		for (i = 0; i < maxchild; i++)
1469*4882a593Smuzhiyun 			portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1470*4882a593Smuzhiyun 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1471*4882a593Smuzhiyun 				? 'F' : 'R';
1472*4882a593Smuzhiyun 		portstr[maxchild] = 0;
1473*4882a593Smuzhiyun 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1474*4882a593Smuzhiyun 	} else
1475*4882a593Smuzhiyun 		dev_dbg(hub_dev, "standalone hub\n");
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
1478*4882a593Smuzhiyun 	case HUB_CHAR_COMMON_LPSM:
1479*4882a593Smuzhiyun 		dev_dbg(hub_dev, "ganged power switching\n");
1480*4882a593Smuzhiyun 		break;
1481*4882a593Smuzhiyun 	case HUB_CHAR_INDV_PORT_LPSM:
1482*4882a593Smuzhiyun 		dev_dbg(hub_dev, "individual port power switching\n");
1483*4882a593Smuzhiyun 		break;
1484*4882a593Smuzhiyun 	case HUB_CHAR_NO_LPSM:
1485*4882a593Smuzhiyun 	case HUB_CHAR_LPSM:
1486*4882a593Smuzhiyun 		dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1487*4882a593Smuzhiyun 		break;
1488*4882a593Smuzhiyun 	}
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
1491*4882a593Smuzhiyun 	case HUB_CHAR_COMMON_OCPM:
1492*4882a593Smuzhiyun 		dev_dbg(hub_dev, "global over-current protection\n");
1493*4882a593Smuzhiyun 		break;
1494*4882a593Smuzhiyun 	case HUB_CHAR_INDV_PORT_OCPM:
1495*4882a593Smuzhiyun 		dev_dbg(hub_dev, "individual port over-current protection\n");
1496*4882a593Smuzhiyun 		break;
1497*4882a593Smuzhiyun 	case HUB_CHAR_NO_OCPM:
1498*4882a593Smuzhiyun 	case HUB_CHAR_OCPM:
1499*4882a593Smuzhiyun 		dev_dbg(hub_dev, "no over-current protection\n");
1500*4882a593Smuzhiyun 		break;
1501*4882a593Smuzhiyun 	}
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	spin_lock_init(&hub->tt.lock);
1504*4882a593Smuzhiyun 	INIT_LIST_HEAD(&hub->tt.clear_list);
1505*4882a593Smuzhiyun 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1506*4882a593Smuzhiyun 	switch (hdev->descriptor.bDeviceProtocol) {
1507*4882a593Smuzhiyun 	case USB_HUB_PR_FS:
1508*4882a593Smuzhiyun 		break;
1509*4882a593Smuzhiyun 	case USB_HUB_PR_HS_SINGLE_TT:
1510*4882a593Smuzhiyun 		dev_dbg(hub_dev, "Single TT\n");
1511*4882a593Smuzhiyun 		hub->tt.hub = hdev;
1512*4882a593Smuzhiyun 		break;
1513*4882a593Smuzhiyun 	case USB_HUB_PR_HS_MULTI_TT:
1514*4882a593Smuzhiyun 		ret = usb_set_interface(hdev, 0, 1);
1515*4882a593Smuzhiyun 		if (ret == 0) {
1516*4882a593Smuzhiyun 			dev_dbg(hub_dev, "TT per port\n");
1517*4882a593Smuzhiyun 			hub->tt.multi = 1;
1518*4882a593Smuzhiyun 		} else
1519*4882a593Smuzhiyun 			dev_err(hub_dev, "Using single TT (err %d)\n",
1520*4882a593Smuzhiyun 				ret);
1521*4882a593Smuzhiyun 		hub->tt.hub = hdev;
1522*4882a593Smuzhiyun 		break;
1523*4882a593Smuzhiyun 	case USB_HUB_PR_SS:
1524*4882a593Smuzhiyun 		/* USB 3.0 hubs don't have a TT */
1525*4882a593Smuzhiyun 		break;
1526*4882a593Smuzhiyun 	default:
1527*4882a593Smuzhiyun 		dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1528*4882a593Smuzhiyun 			hdev->descriptor.bDeviceProtocol);
1529*4882a593Smuzhiyun 		break;
1530*4882a593Smuzhiyun 	}
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
1533*4882a593Smuzhiyun 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1534*4882a593Smuzhiyun 	case HUB_TTTT_8_BITS:
1535*4882a593Smuzhiyun 		if (hdev->descriptor.bDeviceProtocol != 0) {
1536*4882a593Smuzhiyun 			hub->tt.think_time = 666;
1537*4882a593Smuzhiyun 			dev_dbg(hub_dev, "TT requires at most %d "
1538*4882a593Smuzhiyun 					"FS bit times (%d ns)\n",
1539*4882a593Smuzhiyun 				8, hub->tt.think_time);
1540*4882a593Smuzhiyun 		}
1541*4882a593Smuzhiyun 		break;
1542*4882a593Smuzhiyun 	case HUB_TTTT_16_BITS:
1543*4882a593Smuzhiyun 		hub->tt.think_time = 666 * 2;
1544*4882a593Smuzhiyun 		dev_dbg(hub_dev, "TT requires at most %d "
1545*4882a593Smuzhiyun 				"FS bit times (%d ns)\n",
1546*4882a593Smuzhiyun 			16, hub->tt.think_time);
1547*4882a593Smuzhiyun 		break;
1548*4882a593Smuzhiyun 	case HUB_TTTT_24_BITS:
1549*4882a593Smuzhiyun 		hub->tt.think_time = 666 * 3;
1550*4882a593Smuzhiyun 		dev_dbg(hub_dev, "TT requires at most %d "
1551*4882a593Smuzhiyun 				"FS bit times (%d ns)\n",
1552*4882a593Smuzhiyun 			24, hub->tt.think_time);
1553*4882a593Smuzhiyun 		break;
1554*4882a593Smuzhiyun 	case HUB_TTTT_32_BITS:
1555*4882a593Smuzhiyun 		hub->tt.think_time = 666 * 4;
1556*4882a593Smuzhiyun 		dev_dbg(hub_dev, "TT requires at most %d "
1557*4882a593Smuzhiyun 				"FS bit times (%d ns)\n",
1558*4882a593Smuzhiyun 			32, hub->tt.think_time);
1559*4882a593Smuzhiyun 		break;
1560*4882a593Smuzhiyun 	}
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	/* probe() zeroes hub->indicator[] */
1563*4882a593Smuzhiyun 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1564*4882a593Smuzhiyun 		hub->has_indicators = 1;
1565*4882a593Smuzhiyun 		dev_dbg(hub_dev, "Port indicators are supported\n");
1566*4882a593Smuzhiyun 	}
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
1569*4882a593Smuzhiyun 		hub->descriptor->bPwrOn2PwrGood * 2);
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun 	/* power budgeting mostly matters with bus-powered hubs,
1572*4882a593Smuzhiyun 	 * and battery-powered root hubs (may provide just 8 mA).
1573*4882a593Smuzhiyun 	 */
1574*4882a593Smuzhiyun 	ret = usb_get_std_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
1575*4882a593Smuzhiyun 	if (ret) {
1576*4882a593Smuzhiyun 		message = "can't get hub status";
1577*4882a593Smuzhiyun 		goto fail;
1578*4882a593Smuzhiyun 	}
1579*4882a593Smuzhiyun 	hcd = bus_to_hcd(hdev->bus);
1580*4882a593Smuzhiyun 	if (hdev == hdev->bus->root_hub) {
1581*4882a593Smuzhiyun 		if (hcd->power_budget > 0)
1582*4882a593Smuzhiyun 			hdev->bus_mA = hcd->power_budget;
1583*4882a593Smuzhiyun 		else
1584*4882a593Smuzhiyun 			hdev->bus_mA = full_load * maxchild;
1585*4882a593Smuzhiyun 		if (hdev->bus_mA >= full_load)
1586*4882a593Smuzhiyun 			hub->mA_per_port = full_load;
1587*4882a593Smuzhiyun 		else {
1588*4882a593Smuzhiyun 			hub->mA_per_port = hdev->bus_mA;
1589*4882a593Smuzhiyun 			hub->limited_power = 1;
1590*4882a593Smuzhiyun 		}
1591*4882a593Smuzhiyun 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1592*4882a593Smuzhiyun 		int remaining = hdev->bus_mA -
1593*4882a593Smuzhiyun 			hub->descriptor->bHubContrCurrent;
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1596*4882a593Smuzhiyun 			hub->descriptor->bHubContrCurrent);
1597*4882a593Smuzhiyun 		hub->limited_power = 1;
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 		if (remaining < maxchild * unit_load)
1600*4882a593Smuzhiyun 			dev_warn(hub_dev,
1601*4882a593Smuzhiyun 					"insufficient power available "
1602*4882a593Smuzhiyun 					"to use all downstream ports\n");
1603*4882a593Smuzhiyun 		hub->mA_per_port = unit_load;	/* 7.2.1 */
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	} else {	/* Self-powered external hub */
1606*4882a593Smuzhiyun 		/* FIXME: What about battery-powered external hubs that
1607*4882a593Smuzhiyun 		 * provide less current per port? */
1608*4882a593Smuzhiyun 		hub->mA_per_port = full_load;
1609*4882a593Smuzhiyun 	}
1610*4882a593Smuzhiyun 	if (hub->mA_per_port < full_load)
1611*4882a593Smuzhiyun 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1612*4882a593Smuzhiyun 				hub->mA_per_port);
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
1615*4882a593Smuzhiyun 	if (ret < 0) {
1616*4882a593Smuzhiyun 		message = "can't get hub status";
1617*4882a593Smuzhiyun 		goto fail;
1618*4882a593Smuzhiyun 	}
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 	/* local power status reports aren't always correct */
1621*4882a593Smuzhiyun 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1622*4882a593Smuzhiyun 		dev_dbg(hub_dev, "local power source is %s\n",
1623*4882a593Smuzhiyun 			(hubstatus & HUB_STATUS_LOCAL_POWER)
1624*4882a593Smuzhiyun 			? "lost (inactive)" : "good");
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1627*4882a593Smuzhiyun 		dev_dbg(hub_dev, "%sover-current condition exists\n",
1628*4882a593Smuzhiyun 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	/* set up the interrupt endpoint
1631*4882a593Smuzhiyun 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1632*4882a593Smuzhiyun 	 * bytes as USB2.0[11.12.3] says because some hubs are known
1633*4882a593Smuzhiyun 	 * to send more data (and thus cause overflow). For root hubs,
1634*4882a593Smuzhiyun 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1635*4882a593Smuzhiyun 	 * to be big enough for at least USB_MAXCHILDREN ports. */
1636*4882a593Smuzhiyun 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1637*4882a593Smuzhiyun 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	if (maxp > sizeof(*hub->buffer))
1640*4882a593Smuzhiyun 		maxp = sizeof(*hub->buffer);
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1643*4882a593Smuzhiyun 	if (!hub->urb) {
1644*4882a593Smuzhiyun 		ret = -ENOMEM;
1645*4882a593Smuzhiyun 		goto fail;
1646*4882a593Smuzhiyun 	}
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1649*4882a593Smuzhiyun 		hub, endpoint->bInterval);
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	/* maybe cycle the hub leds */
1652*4882a593Smuzhiyun 	if (hub->has_indicators && blinkenlights)
1653*4882a593Smuzhiyun 		hub->indicator[0] = INDICATOR_CYCLE;
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun 	mutex_lock(&usb_port_peer_mutex);
1656*4882a593Smuzhiyun 	for (i = 0; i < maxchild; i++) {
1657*4882a593Smuzhiyun 		ret = usb_hub_create_port_device(hub, i + 1);
1658*4882a593Smuzhiyun 		if (ret < 0) {
1659*4882a593Smuzhiyun 			dev_err(hub->intfdev,
1660*4882a593Smuzhiyun 				"couldn't create port%d device.\n", i + 1);
1661*4882a593Smuzhiyun 			break;
1662*4882a593Smuzhiyun 		}
1663*4882a593Smuzhiyun 	}
1664*4882a593Smuzhiyun 	hdev->maxchild = i;
1665*4882a593Smuzhiyun 	for (i = 0; i < hdev->maxchild; i++) {
1666*4882a593Smuzhiyun 		struct usb_port *port_dev = hub->ports[i];
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun 		pm_runtime_put(&port_dev->dev);
1669*4882a593Smuzhiyun 	}
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	mutex_unlock(&usb_port_peer_mutex);
1672*4882a593Smuzhiyun 	if (ret < 0)
1673*4882a593Smuzhiyun 		goto fail;
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun 	/* Update the HCD's internal representation of this hub before hub_wq
1676*4882a593Smuzhiyun 	 * starts getting port status changes for devices under the hub.
1677*4882a593Smuzhiyun 	 */
1678*4882a593Smuzhiyun 	if (hcd->driver->update_hub_device) {
1679*4882a593Smuzhiyun 		ret = hcd->driver->update_hub_device(hcd, hdev,
1680*4882a593Smuzhiyun 				&hub->tt, GFP_KERNEL);
1681*4882a593Smuzhiyun 		if (ret < 0) {
1682*4882a593Smuzhiyun 			message = "can't update HCD hub info";
1683*4882a593Smuzhiyun 			goto fail;
1684*4882a593Smuzhiyun 		}
1685*4882a593Smuzhiyun 	}
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun 	hub_activate(hub, HUB_INIT);
1690*4882a593Smuzhiyun 	return 0;
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun fail:
1693*4882a593Smuzhiyun 	dev_err(hub_dev, "config failed, %s (err %d)\n",
1694*4882a593Smuzhiyun 			message, ret);
1695*4882a593Smuzhiyun 	/* hub_disconnect() frees urb and descriptor */
1696*4882a593Smuzhiyun 	return ret;
1697*4882a593Smuzhiyun }
1698*4882a593Smuzhiyun 
hub_release(struct kref * kref)1699*4882a593Smuzhiyun static void hub_release(struct kref *kref)
1700*4882a593Smuzhiyun {
1701*4882a593Smuzhiyun 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	usb_put_dev(hub->hdev);
1704*4882a593Smuzhiyun 	usb_put_intf(to_usb_interface(hub->intfdev));
1705*4882a593Smuzhiyun 	kfree(hub);
1706*4882a593Smuzhiyun }
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun static unsigned highspeed_hubs;
1709*4882a593Smuzhiyun 
hub_disconnect(struct usb_interface * intf)1710*4882a593Smuzhiyun static void hub_disconnect(struct usb_interface *intf)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun 	struct usb_hub *hub = usb_get_intfdata(intf);
1713*4882a593Smuzhiyun 	struct usb_device *hdev = interface_to_usbdev(intf);
1714*4882a593Smuzhiyun 	int port1;
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	/*
1717*4882a593Smuzhiyun 	 * Stop adding new hub events. We do not want to block here and thus
1718*4882a593Smuzhiyun 	 * will not try to remove any pending work item.
1719*4882a593Smuzhiyun 	 */
1720*4882a593Smuzhiyun 	hub->disconnected = 1;
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 	/* Disconnect all children and quiesce the hub */
1723*4882a593Smuzhiyun 	hub->error = 0;
1724*4882a593Smuzhiyun 	hub_quiesce(hub, HUB_DISCONNECT);
1725*4882a593Smuzhiyun 
1726*4882a593Smuzhiyun 	mutex_lock(&usb_port_peer_mutex);
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	/* Avoid races with recursively_mark_NOTATTACHED() */
1729*4882a593Smuzhiyun 	spin_lock_irq(&device_state_lock);
1730*4882a593Smuzhiyun 	port1 = hdev->maxchild;
1731*4882a593Smuzhiyun 	hdev->maxchild = 0;
1732*4882a593Smuzhiyun 	usb_set_intfdata(intf, NULL);
1733*4882a593Smuzhiyun 	spin_unlock_irq(&device_state_lock);
1734*4882a593Smuzhiyun 
1735*4882a593Smuzhiyun 	for (; port1 > 0; --port1)
1736*4882a593Smuzhiyun 		usb_hub_remove_port_device(hub, port1);
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun 	mutex_unlock(&usb_port_peer_mutex);
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 	if (hub->hdev->speed == USB_SPEED_HIGH)
1741*4882a593Smuzhiyun 		highspeed_hubs--;
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	usb_free_urb(hub->urb);
1744*4882a593Smuzhiyun 	kfree(hub->ports);
1745*4882a593Smuzhiyun 	kfree(hub->descriptor);
1746*4882a593Smuzhiyun 	kfree(hub->status);
1747*4882a593Smuzhiyun 	kfree(hub->buffer);
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	pm_suspend_ignore_children(&intf->dev, false);
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 	if (hub->quirk_disable_autosuspend)
1752*4882a593Smuzhiyun 		usb_autopm_put_interface(intf);
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun 	kref_put(&hub->kref, hub_release);
1755*4882a593Smuzhiyun }
1756*4882a593Smuzhiyun 
hub_descriptor_is_sane(struct usb_host_interface * desc)1757*4882a593Smuzhiyun static bool hub_descriptor_is_sane(struct usb_host_interface *desc)
1758*4882a593Smuzhiyun {
1759*4882a593Smuzhiyun 	/* Some hubs have a subclass of 1, which AFAICT according to the */
1760*4882a593Smuzhiyun 	/*  specs is not defined, but it works */
1761*4882a593Smuzhiyun 	if (desc->desc.bInterfaceSubClass != 0 &&
1762*4882a593Smuzhiyun 	    desc->desc.bInterfaceSubClass != 1)
1763*4882a593Smuzhiyun 		return false;
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
1766*4882a593Smuzhiyun 	if (desc->desc.bNumEndpoints != 1)
1767*4882a593Smuzhiyun 		return false;
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 	/* If the first endpoint is not interrupt IN, we'd better punt! */
1770*4882a593Smuzhiyun 	if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc))
1771*4882a593Smuzhiyun 		return false;
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun         return true;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun 
hub_probe(struct usb_interface * intf,const struct usb_device_id * id)1776*4882a593Smuzhiyun static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1777*4882a593Smuzhiyun {
1778*4882a593Smuzhiyun 	struct usb_host_interface *desc;
1779*4882a593Smuzhiyun 	struct usb_device *hdev;
1780*4882a593Smuzhiyun 	struct usb_hub *hub;
1781*4882a593Smuzhiyun 
1782*4882a593Smuzhiyun 	desc = intf->cur_altsetting;
1783*4882a593Smuzhiyun 	hdev = interface_to_usbdev(intf);
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 	/*
1786*4882a593Smuzhiyun 	 * Set default autosuspend delay as 0 to speedup bus suspend,
1787*4882a593Smuzhiyun 	 * based on the below considerations:
1788*4882a593Smuzhiyun 	 *
1789*4882a593Smuzhiyun 	 * - Unlike other drivers, the hub driver does not rely on the
1790*4882a593Smuzhiyun 	 *   autosuspend delay to provide enough time to handle a wakeup
1791*4882a593Smuzhiyun 	 *   event, and the submitted status URB is just to check future
1792*4882a593Smuzhiyun 	 *   change on hub downstream ports, so it is safe to do it.
1793*4882a593Smuzhiyun 	 *
1794*4882a593Smuzhiyun 	 * - The patch might cause one or more auto supend/resume for
1795*4882a593Smuzhiyun 	 *   below very rare devices when they are plugged into hub
1796*4882a593Smuzhiyun 	 *   first time:
1797*4882a593Smuzhiyun 	 *
1798*4882a593Smuzhiyun 	 *   	devices having trouble initializing, and disconnect
1799*4882a593Smuzhiyun 	 *   	themselves from the bus and then reconnect a second
1800*4882a593Smuzhiyun 	 *   	or so later
1801*4882a593Smuzhiyun 	 *
1802*4882a593Smuzhiyun 	 *   	devices just for downloading firmware, and disconnects
1803*4882a593Smuzhiyun 	 *   	themselves after completing it
1804*4882a593Smuzhiyun 	 *
1805*4882a593Smuzhiyun 	 *   For these quite rare devices, their drivers may change the
1806*4882a593Smuzhiyun 	 *   autosuspend delay of their parent hub in the probe() to one
1807*4882a593Smuzhiyun 	 *   appropriate value to avoid the subtle problem if someone
1808*4882a593Smuzhiyun 	 *   does care it.
1809*4882a593Smuzhiyun 	 *
1810*4882a593Smuzhiyun 	 * - The patch may cause one or more auto suspend/resume on
1811*4882a593Smuzhiyun 	 *   hub during running 'lsusb', but it is probably too
1812*4882a593Smuzhiyun 	 *   infrequent to worry about.
1813*4882a593Smuzhiyun 	 *
1814*4882a593Smuzhiyun 	 * - Change autosuspend delay of hub can avoid unnecessary auto
1815*4882a593Smuzhiyun 	 *   suspend timer for hub, also may decrease power consumption
1816*4882a593Smuzhiyun 	 *   of USB bus.
1817*4882a593Smuzhiyun 	 *
1818*4882a593Smuzhiyun 	 * - If user has indicated to prevent autosuspend by passing
1819*4882a593Smuzhiyun 	 *   usbcore.autosuspend = -1 then keep autosuspend disabled.
1820*4882a593Smuzhiyun 	 */
1821*4882a593Smuzhiyun #ifdef CONFIG_PM
1822*4882a593Smuzhiyun 	if (hdev->dev.power.autosuspend_delay >= 0)
1823*4882a593Smuzhiyun 		pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1824*4882a593Smuzhiyun #endif
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	/*
1827*4882a593Smuzhiyun 	 * Hubs have proper suspend/resume support, except for root hubs
1828*4882a593Smuzhiyun 	 * where the controller driver doesn't have bus_suspend and
1829*4882a593Smuzhiyun 	 * bus_resume methods.
1830*4882a593Smuzhiyun 	 */
1831*4882a593Smuzhiyun 	if (hdev->parent) {		/* normal device */
1832*4882a593Smuzhiyun 		if (!(hdev->parent->quirks & USB_QUIRK_AUTO_SUSPEND))
1833*4882a593Smuzhiyun 			usb_enable_autosuspend(hdev);
1834*4882a593Smuzhiyun 	} else {			/* root hub */
1835*4882a593Smuzhiyun 		const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 		if (drv->bus_suspend && drv->bus_resume)
1838*4882a593Smuzhiyun 			usb_enable_autosuspend(hdev);
1839*4882a593Smuzhiyun 	}
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	if (hdev->level == MAX_TOPO_LEVEL) {
1842*4882a593Smuzhiyun 		dev_err(&intf->dev,
1843*4882a593Smuzhiyun 			"Unsupported bus topology: hub nested too deep\n");
1844*4882a593Smuzhiyun 		return -E2BIG;
1845*4882a593Smuzhiyun 	}
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun #ifdef	CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB
1848*4882a593Smuzhiyun 	if (hdev->parent) {
1849*4882a593Smuzhiyun 		dev_warn(&intf->dev, "ignoring external hub\n");
1850*4882a593Smuzhiyun 		return -ENODEV;
1851*4882a593Smuzhiyun 	}
1852*4882a593Smuzhiyun #endif
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun 	if (!hub_descriptor_is_sane(desc)) {
1855*4882a593Smuzhiyun 		dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
1856*4882a593Smuzhiyun 		return -EIO;
1857*4882a593Smuzhiyun 	}
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun 	/* We found a hub */
1860*4882a593Smuzhiyun 	dev_info(&intf->dev, "USB hub found\n");
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1863*4882a593Smuzhiyun 	if (!hub)
1864*4882a593Smuzhiyun 		return -ENOMEM;
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun 	kref_init(&hub->kref);
1867*4882a593Smuzhiyun 	hub->intfdev = &intf->dev;
1868*4882a593Smuzhiyun 	hub->hdev = hdev;
1869*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&hub->leds, led_work);
1870*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1871*4882a593Smuzhiyun 	INIT_WORK(&hub->events, hub_event);
1872*4882a593Smuzhiyun 	spin_lock_init(&hub->irq_urb_lock);
1873*4882a593Smuzhiyun 	timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0);
1874*4882a593Smuzhiyun 	usb_get_intf(intf);
1875*4882a593Smuzhiyun 	usb_get_dev(hdev);
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun 	usb_set_intfdata(intf, hub);
1878*4882a593Smuzhiyun 	intf->needs_remote_wakeup = 1;
1879*4882a593Smuzhiyun 	pm_suspend_ignore_children(&intf->dev, true);
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 	if (hdev->speed == USB_SPEED_HIGH)
1882*4882a593Smuzhiyun 		highspeed_hubs++;
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 	if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1885*4882a593Smuzhiyun 		hub->quirk_check_port_auto_suspend = 1;
1886*4882a593Smuzhiyun 
1887*4882a593Smuzhiyun 	if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) {
1888*4882a593Smuzhiyun 		hub->quirk_disable_autosuspend = 1;
1889*4882a593Smuzhiyun 		usb_autopm_get_interface_no_resume(intf);
1890*4882a593Smuzhiyun 	}
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun 	if (hub_configure(hub, &desc->endpoint[0].desc) >= 0)
1893*4882a593Smuzhiyun 		return 0;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	hub_disconnect(intf);
1896*4882a593Smuzhiyun 	return -ENODEV;
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun static int
hub_ioctl(struct usb_interface * intf,unsigned int code,void * user_data)1900*4882a593Smuzhiyun hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1901*4882a593Smuzhiyun {
1902*4882a593Smuzhiyun 	struct usb_device *hdev = interface_to_usbdev(intf);
1903*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	/* assert ifno == 0 (part of hub spec) */
1906*4882a593Smuzhiyun 	switch (code) {
1907*4882a593Smuzhiyun 	case USBDEVFS_HUB_PORTINFO: {
1908*4882a593Smuzhiyun 		struct usbdevfs_hub_portinfo *info = user_data;
1909*4882a593Smuzhiyun 		int i;
1910*4882a593Smuzhiyun 
1911*4882a593Smuzhiyun 		spin_lock_irq(&device_state_lock);
1912*4882a593Smuzhiyun 		if (hdev->devnum <= 0)
1913*4882a593Smuzhiyun 			info->nports = 0;
1914*4882a593Smuzhiyun 		else {
1915*4882a593Smuzhiyun 			info->nports = hdev->maxchild;
1916*4882a593Smuzhiyun 			for (i = 0; i < info->nports; i++) {
1917*4882a593Smuzhiyun 				if (hub->ports[i]->child == NULL)
1918*4882a593Smuzhiyun 					info->port[i] = 0;
1919*4882a593Smuzhiyun 				else
1920*4882a593Smuzhiyun 					info->port[i] =
1921*4882a593Smuzhiyun 						hub->ports[i]->child->devnum;
1922*4882a593Smuzhiyun 			}
1923*4882a593Smuzhiyun 		}
1924*4882a593Smuzhiyun 		spin_unlock_irq(&device_state_lock);
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 		return info->nports + 1;
1927*4882a593Smuzhiyun 		}
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	default:
1930*4882a593Smuzhiyun 		return -ENOSYS;
1931*4882a593Smuzhiyun 	}
1932*4882a593Smuzhiyun }
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun /*
1935*4882a593Smuzhiyun  * Allow user programs to claim ports on a hub.  When a device is attached
1936*4882a593Smuzhiyun  * to one of these "claimed" ports, the program will "own" the device.
1937*4882a593Smuzhiyun  */
find_port_owner(struct usb_device * hdev,unsigned port1,struct usb_dev_state *** ppowner)1938*4882a593Smuzhiyun static int find_port_owner(struct usb_device *hdev, unsigned port1,
1939*4882a593Smuzhiyun 		struct usb_dev_state ***ppowner)
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	if (hdev->state == USB_STATE_NOTATTACHED)
1944*4882a593Smuzhiyun 		return -ENODEV;
1945*4882a593Smuzhiyun 	if (port1 == 0 || port1 > hdev->maxchild)
1946*4882a593Smuzhiyun 		return -EINVAL;
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	/* Devices not managed by the hub driver
1949*4882a593Smuzhiyun 	 * will always have maxchild equal to 0.
1950*4882a593Smuzhiyun 	 */
1951*4882a593Smuzhiyun 	*ppowner = &(hub->ports[port1 - 1]->port_owner);
1952*4882a593Smuzhiyun 	return 0;
1953*4882a593Smuzhiyun }
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun /* In the following three functions, the caller must hold hdev's lock */
usb_hub_claim_port(struct usb_device * hdev,unsigned port1,struct usb_dev_state * owner)1956*4882a593Smuzhiyun int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1957*4882a593Smuzhiyun 		       struct usb_dev_state *owner)
1958*4882a593Smuzhiyun {
1959*4882a593Smuzhiyun 	int rc;
1960*4882a593Smuzhiyun 	struct usb_dev_state **powner;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	rc = find_port_owner(hdev, port1, &powner);
1963*4882a593Smuzhiyun 	if (rc)
1964*4882a593Smuzhiyun 		return rc;
1965*4882a593Smuzhiyun 	if (*powner)
1966*4882a593Smuzhiyun 		return -EBUSY;
1967*4882a593Smuzhiyun 	*powner = owner;
1968*4882a593Smuzhiyun 	return rc;
1969*4882a593Smuzhiyun }
1970*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hub_claim_port);
1971*4882a593Smuzhiyun 
usb_hub_release_port(struct usb_device * hdev,unsigned port1,struct usb_dev_state * owner)1972*4882a593Smuzhiyun int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1973*4882a593Smuzhiyun 			 struct usb_dev_state *owner)
1974*4882a593Smuzhiyun {
1975*4882a593Smuzhiyun 	int rc;
1976*4882a593Smuzhiyun 	struct usb_dev_state **powner;
1977*4882a593Smuzhiyun 
1978*4882a593Smuzhiyun 	rc = find_port_owner(hdev, port1, &powner);
1979*4882a593Smuzhiyun 	if (rc)
1980*4882a593Smuzhiyun 		return rc;
1981*4882a593Smuzhiyun 	if (*powner != owner)
1982*4882a593Smuzhiyun 		return -ENOENT;
1983*4882a593Smuzhiyun 	*powner = NULL;
1984*4882a593Smuzhiyun 	return rc;
1985*4882a593Smuzhiyun }
1986*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hub_release_port);
1987*4882a593Smuzhiyun 
usb_hub_release_all_ports(struct usb_device * hdev,struct usb_dev_state * owner)1988*4882a593Smuzhiyun void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
1989*4882a593Smuzhiyun {
1990*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1991*4882a593Smuzhiyun 	int n;
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	for (n = 0; n < hdev->maxchild; n++) {
1994*4882a593Smuzhiyun 		if (hub->ports[n]->port_owner == owner)
1995*4882a593Smuzhiyun 			hub->ports[n]->port_owner = NULL;
1996*4882a593Smuzhiyun 	}
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun }
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun /* The caller must hold udev's lock */
usb_device_is_owned(struct usb_device * udev)2001*4882a593Smuzhiyun bool usb_device_is_owned(struct usb_device *udev)
2002*4882a593Smuzhiyun {
2003*4882a593Smuzhiyun 	struct usb_hub *hub;
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
2006*4882a593Smuzhiyun 		return false;
2007*4882a593Smuzhiyun 	hub = usb_hub_to_struct_hub(udev->parent);
2008*4882a593Smuzhiyun 	return !!hub->ports[udev->portnum - 1]->port_owner;
2009*4882a593Smuzhiyun }
2010*4882a593Smuzhiyun 
recursively_mark_NOTATTACHED(struct usb_device * udev)2011*4882a593Smuzhiyun static void recursively_mark_NOTATTACHED(struct usb_device *udev)
2012*4882a593Smuzhiyun {
2013*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2014*4882a593Smuzhiyun 	int i;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	for (i = 0; i < udev->maxchild; ++i) {
2017*4882a593Smuzhiyun 		if (hub->ports[i]->child)
2018*4882a593Smuzhiyun 			recursively_mark_NOTATTACHED(hub->ports[i]->child);
2019*4882a593Smuzhiyun 	}
2020*4882a593Smuzhiyun 	if (udev->state == USB_STATE_SUSPENDED)
2021*4882a593Smuzhiyun 		udev->active_duration -= jiffies;
2022*4882a593Smuzhiyun 	udev->state = USB_STATE_NOTATTACHED;
2023*4882a593Smuzhiyun }
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun /**
2026*4882a593Smuzhiyun  * usb_set_device_state - change a device's current state (usbcore, hcds)
2027*4882a593Smuzhiyun  * @udev: pointer to device whose state should be changed
2028*4882a593Smuzhiyun  * @new_state: new state value to be stored
2029*4882a593Smuzhiyun  *
2030*4882a593Smuzhiyun  * udev->state is _not_ fully protected by the device lock.  Although
2031*4882a593Smuzhiyun  * most transitions are made only while holding the lock, the state can
2032*4882a593Smuzhiyun  * can change to USB_STATE_NOTATTACHED at almost any time.  This
2033*4882a593Smuzhiyun  * is so that devices can be marked as disconnected as soon as possible,
2034*4882a593Smuzhiyun  * without having to wait for any semaphores to be released.  As a result,
2035*4882a593Smuzhiyun  * all changes to any device's state must be protected by the
2036*4882a593Smuzhiyun  * device_state_lock spinlock.
2037*4882a593Smuzhiyun  *
2038*4882a593Smuzhiyun  * Once a device has been added to the device tree, all changes to its state
2039*4882a593Smuzhiyun  * should be made using this routine.  The state should _not_ be set directly.
2040*4882a593Smuzhiyun  *
2041*4882a593Smuzhiyun  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
2042*4882a593Smuzhiyun  * Otherwise udev->state is set to new_state, and if new_state is
2043*4882a593Smuzhiyun  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
2044*4882a593Smuzhiyun  * to USB_STATE_NOTATTACHED.
2045*4882a593Smuzhiyun  */
usb_set_device_state(struct usb_device * udev,enum usb_device_state new_state)2046*4882a593Smuzhiyun void usb_set_device_state(struct usb_device *udev,
2047*4882a593Smuzhiyun 		enum usb_device_state new_state)
2048*4882a593Smuzhiyun {
2049*4882a593Smuzhiyun 	unsigned long flags;
2050*4882a593Smuzhiyun 	int wakeup = -1;
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	spin_lock_irqsave(&device_state_lock, flags);
2053*4882a593Smuzhiyun 	if (udev->state == USB_STATE_NOTATTACHED)
2054*4882a593Smuzhiyun 		;	/* do nothing */
2055*4882a593Smuzhiyun 	else if (new_state != USB_STATE_NOTATTACHED) {
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun 		/* root hub wakeup capabilities are managed out-of-band
2058*4882a593Smuzhiyun 		 * and may involve silicon errata ... ignore them here.
2059*4882a593Smuzhiyun 		 */
2060*4882a593Smuzhiyun 		if (udev->parent) {
2061*4882a593Smuzhiyun 			if (udev->state == USB_STATE_SUSPENDED
2062*4882a593Smuzhiyun 					|| new_state == USB_STATE_SUSPENDED)
2063*4882a593Smuzhiyun 				;	/* No change to wakeup settings */
2064*4882a593Smuzhiyun 			else if (new_state == USB_STATE_CONFIGURED)
2065*4882a593Smuzhiyun 				wakeup = (udev->quirks &
2066*4882a593Smuzhiyun 					USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
2067*4882a593Smuzhiyun 					udev->actconfig->desc.bmAttributes &
2068*4882a593Smuzhiyun 					USB_CONFIG_ATT_WAKEUP;
2069*4882a593Smuzhiyun 			else
2070*4882a593Smuzhiyun 				wakeup = 0;
2071*4882a593Smuzhiyun 		}
2072*4882a593Smuzhiyun 		if (udev->state == USB_STATE_SUSPENDED &&
2073*4882a593Smuzhiyun 			new_state != USB_STATE_SUSPENDED)
2074*4882a593Smuzhiyun 			udev->active_duration -= jiffies;
2075*4882a593Smuzhiyun 		else if (new_state == USB_STATE_SUSPENDED &&
2076*4882a593Smuzhiyun 				udev->state != USB_STATE_SUSPENDED)
2077*4882a593Smuzhiyun 			udev->active_duration += jiffies;
2078*4882a593Smuzhiyun 		udev->state = new_state;
2079*4882a593Smuzhiyun 	} else
2080*4882a593Smuzhiyun 		recursively_mark_NOTATTACHED(udev);
2081*4882a593Smuzhiyun 	spin_unlock_irqrestore(&device_state_lock, flags);
2082*4882a593Smuzhiyun 	if (wakeup >= 0)
2083*4882a593Smuzhiyun 		device_set_wakeup_capable(&udev->dev, wakeup);
2084*4882a593Smuzhiyun }
2085*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_set_device_state);
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun /*
2088*4882a593Smuzhiyun  * Choose a device number.
2089*4882a593Smuzhiyun  *
2090*4882a593Smuzhiyun  * Device numbers are used as filenames in usbfs.  On USB-1.1 and
2091*4882a593Smuzhiyun  * USB-2.0 buses they are also used as device addresses, however on
2092*4882a593Smuzhiyun  * USB-3.0 buses the address is assigned by the controller hardware
2093*4882a593Smuzhiyun  * and it usually is not the same as the device number.
2094*4882a593Smuzhiyun  *
2095*4882a593Smuzhiyun  * WUSB devices are simple: they have no hubs behind, so the mapping
2096*4882a593Smuzhiyun  * device <-> virtual port number becomes 1:1. Why? to simplify the
2097*4882a593Smuzhiyun  * life of the device connection logic in
2098*4882a593Smuzhiyun  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2099*4882a593Smuzhiyun  * handshake we need to assign a temporary address in the unauthorized
2100*4882a593Smuzhiyun  * space. For simplicity we use the first virtual port number found to
2101*4882a593Smuzhiyun  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2102*4882a593Smuzhiyun  * and that becomes it's address [X < 128] or its unauthorized address
2103*4882a593Smuzhiyun  * [X | 0x80].
2104*4882a593Smuzhiyun  *
2105*4882a593Smuzhiyun  * We add 1 as an offset to the one-based USB-stack port number
2106*4882a593Smuzhiyun  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2107*4882a593Smuzhiyun  * 0 is reserved by USB for default address; (b) Linux's USB stack
2108*4882a593Smuzhiyun  * uses always #1 for the root hub of the controller. So USB stack's
2109*4882a593Smuzhiyun  * port #1, which is wusb virtual-port #0 has address #2.
2110*4882a593Smuzhiyun  *
2111*4882a593Smuzhiyun  * Devices connected under xHCI are not as simple.  The host controller
2112*4882a593Smuzhiyun  * supports virtualization, so the hardware assigns device addresses and
2113*4882a593Smuzhiyun  * the HCD must setup data structures before issuing a set address
2114*4882a593Smuzhiyun  * command to the hardware.
2115*4882a593Smuzhiyun  */
choose_devnum(struct usb_device * udev)2116*4882a593Smuzhiyun static void choose_devnum(struct usb_device *udev)
2117*4882a593Smuzhiyun {
2118*4882a593Smuzhiyun 	int		devnum;
2119*4882a593Smuzhiyun 	struct usb_bus	*bus = udev->bus;
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	/* be safe when more hub events are proceed in parallel */
2122*4882a593Smuzhiyun 	mutex_lock(&bus->devnum_next_mutex);
2123*4882a593Smuzhiyun 	if (udev->wusb) {
2124*4882a593Smuzhiyun 		devnum = udev->portnum + 1;
2125*4882a593Smuzhiyun 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2126*4882a593Smuzhiyun 	} else {
2127*4882a593Smuzhiyun 		/* Try to allocate the next devnum beginning at
2128*4882a593Smuzhiyun 		 * bus->devnum_next. */
2129*4882a593Smuzhiyun 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2130*4882a593Smuzhiyun 					    bus->devnum_next);
2131*4882a593Smuzhiyun 		if (devnum >= 128)
2132*4882a593Smuzhiyun 			devnum = find_next_zero_bit(bus->devmap.devicemap,
2133*4882a593Smuzhiyun 						    128, 1);
2134*4882a593Smuzhiyun 		bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
2135*4882a593Smuzhiyun 	}
2136*4882a593Smuzhiyun 	if (devnum < 128) {
2137*4882a593Smuzhiyun 		set_bit(devnum, bus->devmap.devicemap);
2138*4882a593Smuzhiyun 		udev->devnum = devnum;
2139*4882a593Smuzhiyun 	}
2140*4882a593Smuzhiyun 	mutex_unlock(&bus->devnum_next_mutex);
2141*4882a593Smuzhiyun }
2142*4882a593Smuzhiyun 
release_devnum(struct usb_device * udev)2143*4882a593Smuzhiyun static void release_devnum(struct usb_device *udev)
2144*4882a593Smuzhiyun {
2145*4882a593Smuzhiyun 	if (udev->devnum > 0) {
2146*4882a593Smuzhiyun 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2147*4882a593Smuzhiyun 		udev->devnum = -1;
2148*4882a593Smuzhiyun 	}
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun 
update_devnum(struct usb_device * udev,int devnum)2151*4882a593Smuzhiyun static void update_devnum(struct usb_device *udev, int devnum)
2152*4882a593Smuzhiyun {
2153*4882a593Smuzhiyun 	/* The address for a WUSB device is managed by wusbcore. */
2154*4882a593Smuzhiyun 	if (!udev->wusb)
2155*4882a593Smuzhiyun 		udev->devnum = devnum;
2156*4882a593Smuzhiyun 	if (!udev->devaddr)
2157*4882a593Smuzhiyun 		udev->devaddr = (u8)devnum;
2158*4882a593Smuzhiyun }
2159*4882a593Smuzhiyun 
hub_free_dev(struct usb_device * udev)2160*4882a593Smuzhiyun static void hub_free_dev(struct usb_device *udev)
2161*4882a593Smuzhiyun {
2162*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	/* Root hubs aren't real devices, so don't free HCD resources */
2165*4882a593Smuzhiyun 	if (hcd->driver->free_dev && udev->parent)
2166*4882a593Smuzhiyun 		hcd->driver->free_dev(hcd, udev);
2167*4882a593Smuzhiyun }
2168*4882a593Smuzhiyun 
hub_disconnect_children(struct usb_device * udev)2169*4882a593Smuzhiyun static void hub_disconnect_children(struct usb_device *udev)
2170*4882a593Smuzhiyun {
2171*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2172*4882a593Smuzhiyun 	int i;
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	/* Free up all the children before we remove this device */
2175*4882a593Smuzhiyun 	for (i = 0; i < udev->maxchild; i++) {
2176*4882a593Smuzhiyun 		if (hub->ports[i]->child)
2177*4882a593Smuzhiyun 			usb_disconnect(&hub->ports[i]->child);
2178*4882a593Smuzhiyun 	}
2179*4882a593Smuzhiyun }
2180*4882a593Smuzhiyun 
2181*4882a593Smuzhiyun /**
2182*4882a593Smuzhiyun  * usb_disconnect - disconnect a device (usbcore-internal)
2183*4882a593Smuzhiyun  * @pdev: pointer to device being disconnected
2184*4882a593Smuzhiyun  * Context: !in_interrupt ()
2185*4882a593Smuzhiyun  *
2186*4882a593Smuzhiyun  * Something got disconnected. Get rid of it and all of its children.
2187*4882a593Smuzhiyun  *
2188*4882a593Smuzhiyun  * If *pdev is a normal device then the parent hub must already be locked.
2189*4882a593Smuzhiyun  * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
2190*4882a593Smuzhiyun  * which protects the set of root hubs as well as the list of buses.
2191*4882a593Smuzhiyun  *
2192*4882a593Smuzhiyun  * Only hub drivers (including virtual root hub drivers for host
2193*4882a593Smuzhiyun  * controllers) should ever call this.
2194*4882a593Smuzhiyun  *
2195*4882a593Smuzhiyun  * This call is synchronous, and may not be used in an interrupt context.
2196*4882a593Smuzhiyun  */
usb_disconnect(struct usb_device ** pdev)2197*4882a593Smuzhiyun void usb_disconnect(struct usb_device **pdev)
2198*4882a593Smuzhiyun {
2199*4882a593Smuzhiyun 	struct usb_port *port_dev = NULL;
2200*4882a593Smuzhiyun 	struct usb_device *udev = *pdev;
2201*4882a593Smuzhiyun 	struct usb_hub *hub = NULL;
2202*4882a593Smuzhiyun 	int port1 = 1;
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 	/* mark the device as inactive, so any further urb submissions for
2205*4882a593Smuzhiyun 	 * this device (and any of its children) will fail immediately.
2206*4882a593Smuzhiyun 	 * this quiesces everything except pending urbs.
2207*4882a593Smuzhiyun 	 */
2208*4882a593Smuzhiyun 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2209*4882a593Smuzhiyun 	dev_info(&udev->dev, "USB disconnect, device number %d\n",
2210*4882a593Smuzhiyun 			udev->devnum);
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	/*
2213*4882a593Smuzhiyun 	 * Ensure that the pm runtime code knows that the USB device
2214*4882a593Smuzhiyun 	 * is in the process of being disconnected.
2215*4882a593Smuzhiyun 	 */
2216*4882a593Smuzhiyun 	pm_runtime_barrier(&udev->dev);
2217*4882a593Smuzhiyun 
2218*4882a593Smuzhiyun 	usb_lock_device(udev);
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun 	hub_disconnect_children(udev);
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun 	/* deallocate hcd/hardware state ... nuking all pending urbs and
2223*4882a593Smuzhiyun 	 * cleaning up all state associated with the current configuration
2224*4882a593Smuzhiyun 	 * so that the hardware is now fully quiesced.
2225*4882a593Smuzhiyun 	 */
2226*4882a593Smuzhiyun 	dev_dbg(&udev->dev, "unregistering device\n");
2227*4882a593Smuzhiyun 	usb_disable_device(udev, 0);
2228*4882a593Smuzhiyun 	usb_hcd_synchronize_unlinks(udev);
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	if (udev->parent) {
2231*4882a593Smuzhiyun 		port1 = udev->portnum;
2232*4882a593Smuzhiyun 		hub = usb_hub_to_struct_hub(udev->parent);
2233*4882a593Smuzhiyun 		port_dev = hub->ports[port1 - 1];
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 		sysfs_remove_link(&udev->dev.kobj, "port");
2236*4882a593Smuzhiyun 		sysfs_remove_link(&port_dev->dev.kobj, "device");
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 		/*
2239*4882a593Smuzhiyun 		 * As usb_port_runtime_resume() de-references udev, make
2240*4882a593Smuzhiyun 		 * sure no resumes occur during removal
2241*4882a593Smuzhiyun 		 */
2242*4882a593Smuzhiyun 		if (!test_and_set_bit(port1, hub->child_usage_bits))
2243*4882a593Smuzhiyun 			pm_runtime_get_sync(&port_dev->dev);
2244*4882a593Smuzhiyun 	}
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	usb_remove_ep_devs(&udev->ep0);
2247*4882a593Smuzhiyun 	usb_unlock_device(udev);
2248*4882a593Smuzhiyun 
2249*4882a593Smuzhiyun 	/* Unregister the device.  The device driver is responsible
2250*4882a593Smuzhiyun 	 * for de-configuring the device and invoking the remove-device
2251*4882a593Smuzhiyun 	 * notifier chain (used by usbfs and possibly others).
2252*4882a593Smuzhiyun 	 */
2253*4882a593Smuzhiyun 	device_del(&udev->dev);
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 	/* Free the device number and delete the parent's children[]
2256*4882a593Smuzhiyun 	 * (or root_hub) pointer.
2257*4882a593Smuzhiyun 	 */
2258*4882a593Smuzhiyun 	release_devnum(udev);
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 	/* Avoid races with recursively_mark_NOTATTACHED() */
2261*4882a593Smuzhiyun 	spin_lock_irq(&device_state_lock);
2262*4882a593Smuzhiyun 	*pdev = NULL;
2263*4882a593Smuzhiyun 	spin_unlock_irq(&device_state_lock);
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun 	if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2266*4882a593Smuzhiyun 		pm_runtime_put(&port_dev->dev);
2267*4882a593Smuzhiyun 
2268*4882a593Smuzhiyun 	hub_free_dev(udev);
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 	put_device(&udev->dev);
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun 
2273*4882a593Smuzhiyun #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
show_string(struct usb_device * udev,char * id,char * string)2274*4882a593Smuzhiyun static void show_string(struct usb_device *udev, char *id, char *string)
2275*4882a593Smuzhiyun {
2276*4882a593Smuzhiyun 	if (!string)
2277*4882a593Smuzhiyun 		return;
2278*4882a593Smuzhiyun 	dev_info(&udev->dev, "%s: %s\n", id, string);
2279*4882a593Smuzhiyun }
2280*4882a593Smuzhiyun 
announce_device(struct usb_device * udev)2281*4882a593Smuzhiyun static void announce_device(struct usb_device *udev)
2282*4882a593Smuzhiyun {
2283*4882a593Smuzhiyun 	u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2284*4882a593Smuzhiyun 
2285*4882a593Smuzhiyun 	dev_info(&udev->dev,
2286*4882a593Smuzhiyun 		"New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n",
2287*4882a593Smuzhiyun 		le16_to_cpu(udev->descriptor.idVendor),
2288*4882a593Smuzhiyun 		le16_to_cpu(udev->descriptor.idProduct),
2289*4882a593Smuzhiyun 		bcdDevice >> 8, bcdDevice & 0xff);
2290*4882a593Smuzhiyun 	dev_info(&udev->dev,
2291*4882a593Smuzhiyun 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2292*4882a593Smuzhiyun 		udev->descriptor.iManufacturer,
2293*4882a593Smuzhiyun 		udev->descriptor.iProduct,
2294*4882a593Smuzhiyun 		udev->descriptor.iSerialNumber);
2295*4882a593Smuzhiyun 	show_string(udev, "Product", udev->product);
2296*4882a593Smuzhiyun 	show_string(udev, "Manufacturer", udev->manufacturer);
2297*4882a593Smuzhiyun 	show_string(udev, "SerialNumber", udev->serial);
2298*4882a593Smuzhiyun }
2299*4882a593Smuzhiyun #else
announce_device(struct usb_device * udev)2300*4882a593Smuzhiyun static inline void announce_device(struct usb_device *udev) { }
2301*4882a593Smuzhiyun #endif
2302*4882a593Smuzhiyun 
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun /**
2305*4882a593Smuzhiyun  * usb_enumerate_device_otg - FIXME (usbcore-internal)
2306*4882a593Smuzhiyun  * @udev: newly addressed device (in ADDRESS state)
2307*4882a593Smuzhiyun  *
2308*4882a593Smuzhiyun  * Finish enumeration for On-The-Go devices
2309*4882a593Smuzhiyun  *
2310*4882a593Smuzhiyun  * Return: 0 if successful. A negative error code otherwise.
2311*4882a593Smuzhiyun  */
usb_enumerate_device_otg(struct usb_device * udev)2312*4882a593Smuzhiyun static int usb_enumerate_device_otg(struct usb_device *udev)
2313*4882a593Smuzhiyun {
2314*4882a593Smuzhiyun 	int err = 0;
2315*4882a593Smuzhiyun 
2316*4882a593Smuzhiyun #ifdef	CONFIG_USB_OTG
2317*4882a593Smuzhiyun 	/*
2318*4882a593Smuzhiyun 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2319*4882a593Smuzhiyun 	 * to wake us after we've powered off VBUS; and HNP, switching roles
2320*4882a593Smuzhiyun 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
2321*4882a593Smuzhiyun 	 */
2322*4882a593Smuzhiyun 	if (!udev->bus->is_b_host
2323*4882a593Smuzhiyun 			&& udev->config
2324*4882a593Smuzhiyun 			&& udev->parent == udev->bus->root_hub) {
2325*4882a593Smuzhiyun 		struct usb_otg_descriptor	*desc = NULL;
2326*4882a593Smuzhiyun 		struct usb_bus			*bus = udev->bus;
2327*4882a593Smuzhiyun 		unsigned			port1 = udev->portnum;
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun 		/* descriptor may appear anywhere in config */
2330*4882a593Smuzhiyun 		err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
2331*4882a593Smuzhiyun 				le16_to_cpu(udev->config[0].desc.wTotalLength),
2332*4882a593Smuzhiyun 				USB_DT_OTG, (void **) &desc, sizeof(*desc));
2333*4882a593Smuzhiyun 		if (err || !(desc->bmAttributes & USB_OTG_HNP))
2334*4882a593Smuzhiyun 			return 0;
2335*4882a593Smuzhiyun 
2336*4882a593Smuzhiyun 		dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
2337*4882a593Smuzhiyun 					(port1 == bus->otg_port) ? "" : "non-");
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun 		/* enable HNP before suspend, it's simpler */
2340*4882a593Smuzhiyun 		if (port1 == bus->otg_port) {
2341*4882a593Smuzhiyun 			bus->b_hnp_enable = 1;
2342*4882a593Smuzhiyun 			err = usb_control_msg(udev,
2343*4882a593Smuzhiyun 				usb_sndctrlpipe(udev, 0),
2344*4882a593Smuzhiyun 				USB_REQ_SET_FEATURE, 0,
2345*4882a593Smuzhiyun 				USB_DEVICE_B_HNP_ENABLE,
2346*4882a593Smuzhiyun 				0, NULL, 0,
2347*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
2348*4882a593Smuzhiyun 			if (err < 0) {
2349*4882a593Smuzhiyun 				/*
2350*4882a593Smuzhiyun 				 * OTG MESSAGE: report errors here,
2351*4882a593Smuzhiyun 				 * customize to match your product.
2352*4882a593Smuzhiyun 				 */
2353*4882a593Smuzhiyun 				dev_err(&udev->dev, "can't set HNP mode: %d\n",
2354*4882a593Smuzhiyun 									err);
2355*4882a593Smuzhiyun 				bus->b_hnp_enable = 0;
2356*4882a593Smuzhiyun 			}
2357*4882a593Smuzhiyun 		} else if (desc->bLength == sizeof
2358*4882a593Smuzhiyun 				(struct usb_otg_descriptor)) {
2359*4882a593Smuzhiyun 			/* Set a_alt_hnp_support for legacy otg device */
2360*4882a593Smuzhiyun 			err = usb_control_msg(udev,
2361*4882a593Smuzhiyun 				usb_sndctrlpipe(udev, 0),
2362*4882a593Smuzhiyun 				USB_REQ_SET_FEATURE, 0,
2363*4882a593Smuzhiyun 				USB_DEVICE_A_ALT_HNP_SUPPORT,
2364*4882a593Smuzhiyun 				0, NULL, 0,
2365*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
2366*4882a593Smuzhiyun 			if (err < 0)
2367*4882a593Smuzhiyun 				dev_err(&udev->dev,
2368*4882a593Smuzhiyun 					"set a_alt_hnp_support failed: %d\n",
2369*4882a593Smuzhiyun 					err);
2370*4882a593Smuzhiyun 		}
2371*4882a593Smuzhiyun 	}
2372*4882a593Smuzhiyun #endif
2373*4882a593Smuzhiyun 	return err;
2374*4882a593Smuzhiyun }
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun /**
2378*4882a593Smuzhiyun  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2379*4882a593Smuzhiyun  * @udev: newly addressed device (in ADDRESS state)
2380*4882a593Smuzhiyun  *
2381*4882a593Smuzhiyun  * This is only called by usb_new_device() and usb_authorize_device()
2382*4882a593Smuzhiyun  * and FIXME -- all comments that apply to them apply here wrt to
2383*4882a593Smuzhiyun  * environment.
2384*4882a593Smuzhiyun  *
2385*4882a593Smuzhiyun  * If the device is WUSB and not authorized, we don't attempt to read
2386*4882a593Smuzhiyun  * the string descriptors, as they will be errored out by the device
2387*4882a593Smuzhiyun  * until it has been authorized.
2388*4882a593Smuzhiyun  *
2389*4882a593Smuzhiyun  * Return: 0 if successful. A negative error code otherwise.
2390*4882a593Smuzhiyun  */
usb_enumerate_device(struct usb_device * udev)2391*4882a593Smuzhiyun static int usb_enumerate_device(struct usb_device *udev)
2392*4882a593Smuzhiyun {
2393*4882a593Smuzhiyun 	int err;
2394*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun 	if (udev->config == NULL) {
2397*4882a593Smuzhiyun 		err = usb_get_configuration(udev);
2398*4882a593Smuzhiyun 		if (err < 0) {
2399*4882a593Smuzhiyun 			if (err != -ENODEV)
2400*4882a593Smuzhiyun 				dev_err(&udev->dev, "can't read configurations, error %d\n",
2401*4882a593Smuzhiyun 						err);
2402*4882a593Smuzhiyun 			return err;
2403*4882a593Smuzhiyun 		}
2404*4882a593Smuzhiyun 	}
2405*4882a593Smuzhiyun 
2406*4882a593Smuzhiyun 	/* read the standard strings and cache them if present */
2407*4882a593Smuzhiyun 	udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2408*4882a593Smuzhiyun 	udev->manufacturer = usb_cache_string(udev,
2409*4882a593Smuzhiyun 					      udev->descriptor.iManufacturer);
2410*4882a593Smuzhiyun 	udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2411*4882a593Smuzhiyun 
2412*4882a593Smuzhiyun 	err = usb_enumerate_device_otg(udev);
2413*4882a593Smuzhiyun 	if (err < 0)
2414*4882a593Smuzhiyun 		return err;
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support &&
2417*4882a593Smuzhiyun 		!is_targeted(udev)) {
2418*4882a593Smuzhiyun 		/* Maybe it can talk to us, though we can't talk to it.
2419*4882a593Smuzhiyun 		 * (Includes HNP test device.)
2420*4882a593Smuzhiyun 		 */
2421*4882a593Smuzhiyun 		if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2422*4882a593Smuzhiyun 			|| udev->bus->is_b_host)) {
2423*4882a593Smuzhiyun 			err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2424*4882a593Smuzhiyun 			if (err < 0)
2425*4882a593Smuzhiyun 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2426*4882a593Smuzhiyun 		}
2427*4882a593Smuzhiyun 		return -ENOTSUPP;
2428*4882a593Smuzhiyun 	}
2429*4882a593Smuzhiyun 
2430*4882a593Smuzhiyun 	usb_detect_interface_quirks(udev);
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 	return 0;
2433*4882a593Smuzhiyun }
2434*4882a593Smuzhiyun 
set_usb_port_removable(struct usb_device * udev)2435*4882a593Smuzhiyun static void set_usb_port_removable(struct usb_device *udev)
2436*4882a593Smuzhiyun {
2437*4882a593Smuzhiyun 	struct usb_device *hdev = udev->parent;
2438*4882a593Smuzhiyun 	struct usb_hub *hub;
2439*4882a593Smuzhiyun 	u8 port = udev->portnum;
2440*4882a593Smuzhiyun 	u16 wHubCharacteristics;
2441*4882a593Smuzhiyun 	bool removable = true;
2442*4882a593Smuzhiyun 
2443*4882a593Smuzhiyun 	if (!hdev)
2444*4882a593Smuzhiyun 		return;
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun 	hub = usb_hub_to_struct_hub(udev->parent);
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun 	/*
2449*4882a593Smuzhiyun 	 * If the platform firmware has provided information about a port,
2450*4882a593Smuzhiyun 	 * use that to determine whether it's removable.
2451*4882a593Smuzhiyun 	 */
2452*4882a593Smuzhiyun 	switch (hub->ports[udev->portnum - 1]->connect_type) {
2453*4882a593Smuzhiyun 	case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2454*4882a593Smuzhiyun 		udev->removable = USB_DEVICE_REMOVABLE;
2455*4882a593Smuzhiyun 		return;
2456*4882a593Smuzhiyun 	case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2457*4882a593Smuzhiyun 	case USB_PORT_NOT_USED:
2458*4882a593Smuzhiyun 		udev->removable = USB_DEVICE_FIXED;
2459*4882a593Smuzhiyun 		return;
2460*4882a593Smuzhiyun 	default:
2461*4882a593Smuzhiyun 		break;
2462*4882a593Smuzhiyun 	}
2463*4882a593Smuzhiyun 
2464*4882a593Smuzhiyun 	/*
2465*4882a593Smuzhiyun 	 * Otherwise, check whether the hub knows whether a port is removable
2466*4882a593Smuzhiyun 	 * or not
2467*4882a593Smuzhiyun 	 */
2468*4882a593Smuzhiyun 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 	if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2471*4882a593Smuzhiyun 		return;
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 	if (hub_is_superspeed(hdev)) {
2474*4882a593Smuzhiyun 		if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2475*4882a593Smuzhiyun 				& (1 << port))
2476*4882a593Smuzhiyun 			removable = false;
2477*4882a593Smuzhiyun 	} else {
2478*4882a593Smuzhiyun 		if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2479*4882a593Smuzhiyun 			removable = false;
2480*4882a593Smuzhiyun 	}
2481*4882a593Smuzhiyun 
2482*4882a593Smuzhiyun 	if (removable)
2483*4882a593Smuzhiyun 		udev->removable = USB_DEVICE_REMOVABLE;
2484*4882a593Smuzhiyun 	else
2485*4882a593Smuzhiyun 		udev->removable = USB_DEVICE_FIXED;
2486*4882a593Smuzhiyun 
2487*4882a593Smuzhiyun }
2488*4882a593Smuzhiyun 
2489*4882a593Smuzhiyun /**
2490*4882a593Smuzhiyun  * usb_new_device - perform initial device setup (usbcore-internal)
2491*4882a593Smuzhiyun  * @udev: newly addressed device (in ADDRESS state)
2492*4882a593Smuzhiyun  *
2493*4882a593Smuzhiyun  * This is called with devices which have been detected but not fully
2494*4882a593Smuzhiyun  * enumerated.  The device descriptor is available, but not descriptors
2495*4882a593Smuzhiyun  * for any device configuration.  The caller must have locked either
2496*4882a593Smuzhiyun  * the parent hub (if udev is a normal device) or else the
2497*4882a593Smuzhiyun  * usb_bus_idr_lock (if udev is a root hub).  The parent's pointer to
2498*4882a593Smuzhiyun  * udev has already been installed, but udev is not yet visible through
2499*4882a593Smuzhiyun  * sysfs or other filesystem code.
2500*4882a593Smuzhiyun  *
2501*4882a593Smuzhiyun  * This call is synchronous, and may not be used in an interrupt context.
2502*4882a593Smuzhiyun  *
2503*4882a593Smuzhiyun  * Only the hub driver or root-hub registrar should ever call this.
2504*4882a593Smuzhiyun  *
2505*4882a593Smuzhiyun  * Return: Whether the device is configured properly or not. Zero if the
2506*4882a593Smuzhiyun  * interface was registered with the driver core; else a negative errno
2507*4882a593Smuzhiyun  * value.
2508*4882a593Smuzhiyun  *
2509*4882a593Smuzhiyun  */
usb_new_device(struct usb_device * udev)2510*4882a593Smuzhiyun int usb_new_device(struct usb_device *udev)
2511*4882a593Smuzhiyun {
2512*4882a593Smuzhiyun 	int err;
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 	if (udev->parent) {
2515*4882a593Smuzhiyun 		/* Initialize non-root-hub device wakeup to disabled;
2516*4882a593Smuzhiyun 		 * device (un)configuration controls wakeup capable
2517*4882a593Smuzhiyun 		 * sysfs power/wakeup controls wakeup enabled/disabled
2518*4882a593Smuzhiyun 		 */
2519*4882a593Smuzhiyun 		device_init_wakeup(&udev->dev, 0);
2520*4882a593Smuzhiyun 	}
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun 	/* Tell the runtime-PM framework the device is active */
2523*4882a593Smuzhiyun 	pm_runtime_set_active(&udev->dev);
2524*4882a593Smuzhiyun 	pm_runtime_get_noresume(&udev->dev);
2525*4882a593Smuzhiyun 	pm_runtime_use_autosuspend(&udev->dev);
2526*4882a593Smuzhiyun 	pm_runtime_enable(&udev->dev);
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun 	/* By default, forbid autosuspend for all devices.  It will be
2529*4882a593Smuzhiyun 	 * allowed for hubs during binding.
2530*4882a593Smuzhiyun 	 */
2531*4882a593Smuzhiyun 	usb_disable_autosuspend(udev);
2532*4882a593Smuzhiyun 
2533*4882a593Smuzhiyun 	err = usb_enumerate_device(udev);	/* Read descriptors */
2534*4882a593Smuzhiyun 	if (err < 0)
2535*4882a593Smuzhiyun 		goto fail;
2536*4882a593Smuzhiyun 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2537*4882a593Smuzhiyun 			udev->devnum, udev->bus->busnum,
2538*4882a593Smuzhiyun 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2539*4882a593Smuzhiyun 	/* export the usbdev device-node for libusb */
2540*4882a593Smuzhiyun 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2541*4882a593Smuzhiyun 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 	/* Tell the world! */
2544*4882a593Smuzhiyun 	announce_device(udev);
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	if (udev->serial)
2547*4882a593Smuzhiyun 		add_device_randomness(udev->serial, strlen(udev->serial));
2548*4882a593Smuzhiyun 	if (udev->product)
2549*4882a593Smuzhiyun 		add_device_randomness(udev->product, strlen(udev->product));
2550*4882a593Smuzhiyun 	if (udev->manufacturer)
2551*4882a593Smuzhiyun 		add_device_randomness(udev->manufacturer,
2552*4882a593Smuzhiyun 				      strlen(udev->manufacturer));
2553*4882a593Smuzhiyun 
2554*4882a593Smuzhiyun 	device_enable_async_suspend(&udev->dev);
2555*4882a593Smuzhiyun 
2556*4882a593Smuzhiyun 	/* check whether the hub or firmware marks this port as non-removable */
2557*4882a593Smuzhiyun 	if (udev->parent)
2558*4882a593Smuzhiyun 		set_usb_port_removable(udev);
2559*4882a593Smuzhiyun 
2560*4882a593Smuzhiyun 	/* Register the device.  The device driver is responsible
2561*4882a593Smuzhiyun 	 * for configuring the device and invoking the add-device
2562*4882a593Smuzhiyun 	 * notifier chain (used by usbfs and possibly others).
2563*4882a593Smuzhiyun 	 */
2564*4882a593Smuzhiyun 	err = device_add(&udev->dev);
2565*4882a593Smuzhiyun 	if (err) {
2566*4882a593Smuzhiyun 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
2567*4882a593Smuzhiyun 		goto fail;
2568*4882a593Smuzhiyun 	}
2569*4882a593Smuzhiyun 
2570*4882a593Smuzhiyun 	/* Create link files between child device and usb port device. */
2571*4882a593Smuzhiyun 	if (udev->parent) {
2572*4882a593Smuzhiyun 		struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2573*4882a593Smuzhiyun 		int port1 = udev->portnum;
2574*4882a593Smuzhiyun 		struct usb_port	*port_dev = hub->ports[port1 - 1];
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 		err = sysfs_create_link(&udev->dev.kobj,
2577*4882a593Smuzhiyun 				&port_dev->dev.kobj, "port");
2578*4882a593Smuzhiyun 		if (err)
2579*4882a593Smuzhiyun 			goto fail;
2580*4882a593Smuzhiyun 
2581*4882a593Smuzhiyun 		err = sysfs_create_link(&port_dev->dev.kobj,
2582*4882a593Smuzhiyun 				&udev->dev.kobj, "device");
2583*4882a593Smuzhiyun 		if (err) {
2584*4882a593Smuzhiyun 			sysfs_remove_link(&udev->dev.kobj, "port");
2585*4882a593Smuzhiyun 			goto fail;
2586*4882a593Smuzhiyun 		}
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 		if (!test_and_set_bit(port1, hub->child_usage_bits))
2589*4882a593Smuzhiyun 			pm_runtime_get_sync(&port_dev->dev);
2590*4882a593Smuzhiyun 	}
2591*4882a593Smuzhiyun 
2592*4882a593Smuzhiyun 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2593*4882a593Smuzhiyun 	usb_mark_last_busy(udev);
2594*4882a593Smuzhiyun 	pm_runtime_put_sync_autosuspend(&udev->dev);
2595*4882a593Smuzhiyun 	return err;
2596*4882a593Smuzhiyun 
2597*4882a593Smuzhiyun fail:
2598*4882a593Smuzhiyun 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2599*4882a593Smuzhiyun 	pm_runtime_disable(&udev->dev);
2600*4882a593Smuzhiyun 	pm_runtime_set_suspended(&udev->dev);
2601*4882a593Smuzhiyun 	return err;
2602*4882a593Smuzhiyun }
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 
2605*4882a593Smuzhiyun /**
2606*4882a593Smuzhiyun  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2607*4882a593Smuzhiyun  * @usb_dev: USB device
2608*4882a593Smuzhiyun  *
2609*4882a593Smuzhiyun  * Move the USB device to a very basic state where interfaces are disabled
2610*4882a593Smuzhiyun  * and the device is in fact unconfigured and unusable.
2611*4882a593Smuzhiyun  *
2612*4882a593Smuzhiyun  * We share a lock (that we have) with device_del(), so we need to
2613*4882a593Smuzhiyun  * defer its call.
2614*4882a593Smuzhiyun  *
2615*4882a593Smuzhiyun  * Return: 0.
2616*4882a593Smuzhiyun  */
usb_deauthorize_device(struct usb_device * usb_dev)2617*4882a593Smuzhiyun int usb_deauthorize_device(struct usb_device *usb_dev)
2618*4882a593Smuzhiyun {
2619*4882a593Smuzhiyun 	usb_lock_device(usb_dev);
2620*4882a593Smuzhiyun 	if (usb_dev->authorized == 0)
2621*4882a593Smuzhiyun 		goto out_unauthorized;
2622*4882a593Smuzhiyun 
2623*4882a593Smuzhiyun 	usb_dev->authorized = 0;
2624*4882a593Smuzhiyun 	usb_set_configuration(usb_dev, -1);
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun out_unauthorized:
2627*4882a593Smuzhiyun 	usb_unlock_device(usb_dev);
2628*4882a593Smuzhiyun 	return 0;
2629*4882a593Smuzhiyun }
2630*4882a593Smuzhiyun 
2631*4882a593Smuzhiyun 
usb_authorize_device(struct usb_device * usb_dev)2632*4882a593Smuzhiyun int usb_authorize_device(struct usb_device *usb_dev)
2633*4882a593Smuzhiyun {
2634*4882a593Smuzhiyun 	int result = 0, c;
2635*4882a593Smuzhiyun 
2636*4882a593Smuzhiyun 	usb_lock_device(usb_dev);
2637*4882a593Smuzhiyun 	if (usb_dev->authorized == 1)
2638*4882a593Smuzhiyun 		goto out_authorized;
2639*4882a593Smuzhiyun 
2640*4882a593Smuzhiyun 	result = usb_autoresume_device(usb_dev);
2641*4882a593Smuzhiyun 	if (result < 0) {
2642*4882a593Smuzhiyun 		dev_err(&usb_dev->dev,
2643*4882a593Smuzhiyun 			"can't autoresume for authorization: %d\n", result);
2644*4882a593Smuzhiyun 		goto error_autoresume;
2645*4882a593Smuzhiyun 	}
2646*4882a593Smuzhiyun 
2647*4882a593Smuzhiyun 	if (usb_dev->wusb) {
2648*4882a593Smuzhiyun 		result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2649*4882a593Smuzhiyun 		if (result < 0) {
2650*4882a593Smuzhiyun 			dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2651*4882a593Smuzhiyun 				"authorization: %d\n", result);
2652*4882a593Smuzhiyun 			goto error_device_descriptor;
2653*4882a593Smuzhiyun 		}
2654*4882a593Smuzhiyun 	}
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	usb_dev->authorized = 1;
2657*4882a593Smuzhiyun 	/* Choose and set the configuration.  This registers the interfaces
2658*4882a593Smuzhiyun 	 * with the driver core and lets interface drivers bind to them.
2659*4882a593Smuzhiyun 	 */
2660*4882a593Smuzhiyun 	c = usb_choose_configuration(usb_dev);
2661*4882a593Smuzhiyun 	if (c >= 0) {
2662*4882a593Smuzhiyun 		result = usb_set_configuration(usb_dev, c);
2663*4882a593Smuzhiyun 		if (result) {
2664*4882a593Smuzhiyun 			dev_err(&usb_dev->dev,
2665*4882a593Smuzhiyun 				"can't set config #%d, error %d\n", c, result);
2666*4882a593Smuzhiyun 			/* This need not be fatal.  The user can try to
2667*4882a593Smuzhiyun 			 * set other configurations. */
2668*4882a593Smuzhiyun 		}
2669*4882a593Smuzhiyun 	}
2670*4882a593Smuzhiyun 	dev_info(&usb_dev->dev, "authorized to connect\n");
2671*4882a593Smuzhiyun 
2672*4882a593Smuzhiyun error_device_descriptor:
2673*4882a593Smuzhiyun 	usb_autosuspend_device(usb_dev);
2674*4882a593Smuzhiyun error_autoresume:
2675*4882a593Smuzhiyun out_authorized:
2676*4882a593Smuzhiyun 	usb_unlock_device(usb_dev);	/* complements locktree */
2677*4882a593Smuzhiyun 	return result;
2678*4882a593Smuzhiyun }
2679*4882a593Smuzhiyun 
2680*4882a593Smuzhiyun /*
2681*4882a593Smuzhiyun  * Return 1 if port speed is SuperSpeedPlus, 0 otherwise
2682*4882a593Smuzhiyun  * check it from the link protocol field of the current speed ID attribute.
2683*4882a593Smuzhiyun  * current speed ID is got from ext port status request. Sublink speed attribute
2684*4882a593Smuzhiyun  * table is returned with the hub BOS SSP device capability descriptor
2685*4882a593Smuzhiyun  */
port_speed_is_ssp(struct usb_device * hdev,int speed_id)2686*4882a593Smuzhiyun static int port_speed_is_ssp(struct usb_device *hdev, int speed_id)
2687*4882a593Smuzhiyun {
2688*4882a593Smuzhiyun 	int ssa_count;
2689*4882a593Smuzhiyun 	u32 ss_attr;
2690*4882a593Smuzhiyun 	int i;
2691*4882a593Smuzhiyun 	struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap;
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun 	if (!ssp_cap)
2694*4882a593Smuzhiyun 		return 0;
2695*4882a593Smuzhiyun 
2696*4882a593Smuzhiyun 	ssa_count = le32_to_cpu(ssp_cap->bmAttributes) &
2697*4882a593Smuzhiyun 		USB_SSP_SUBLINK_SPEED_ATTRIBS;
2698*4882a593Smuzhiyun 
2699*4882a593Smuzhiyun 	for (i = 0; i <= ssa_count; i++) {
2700*4882a593Smuzhiyun 		ss_attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
2701*4882a593Smuzhiyun 		if (speed_id == (ss_attr & USB_SSP_SUBLINK_SPEED_SSID))
2702*4882a593Smuzhiyun 			return !!(ss_attr & USB_SSP_SUBLINK_SPEED_LP);
2703*4882a593Smuzhiyun 	}
2704*4882a593Smuzhiyun 	return 0;
2705*4882a593Smuzhiyun }
2706*4882a593Smuzhiyun 
2707*4882a593Smuzhiyun /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
hub_is_wusb(struct usb_hub * hub)2708*4882a593Smuzhiyun static unsigned hub_is_wusb(struct usb_hub *hub)
2709*4882a593Smuzhiyun {
2710*4882a593Smuzhiyun 	struct usb_hcd *hcd;
2711*4882a593Smuzhiyun 	if (hub->hdev->parent != NULL)  /* not a root hub? */
2712*4882a593Smuzhiyun 		return 0;
2713*4882a593Smuzhiyun 	hcd = bus_to_hcd(hub->hdev->bus);
2714*4882a593Smuzhiyun 	return hcd->wireless;
2715*4882a593Smuzhiyun }
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun #ifdef CONFIG_USB_FEW_INIT_RETRIES
2719*4882a593Smuzhiyun #define PORT_RESET_TRIES	2
2720*4882a593Smuzhiyun #define SET_ADDRESS_TRIES	1
2721*4882a593Smuzhiyun #define GET_DESCRIPTOR_TRIES	1
2722*4882a593Smuzhiyun #define GET_MAXPACKET0_TRIES	1
2723*4882a593Smuzhiyun #define PORT_INIT_TRIES		4
2724*4882a593Smuzhiyun 
2725*4882a593Smuzhiyun #else
2726*4882a593Smuzhiyun #define PORT_RESET_TRIES	5
2727*4882a593Smuzhiyun #define SET_ADDRESS_TRIES	2
2728*4882a593Smuzhiyun #define GET_DESCRIPTOR_TRIES	2
2729*4882a593Smuzhiyun #define GET_MAXPACKET0_TRIES	3
2730*4882a593Smuzhiyun #define PORT_INIT_TRIES		4
2731*4882a593Smuzhiyun #endif	/* CONFIG_USB_FEW_INIT_RETRIES */
2732*4882a593Smuzhiyun 
2733*4882a593Smuzhiyun #define HUB_ROOT_RESET_TIME	60	/* times are in msec */
2734*4882a593Smuzhiyun #define HUB_SHORT_RESET_TIME	10
2735*4882a593Smuzhiyun #define HUB_BH_RESET_TIME	50
2736*4882a593Smuzhiyun #define HUB_LONG_RESET_TIME	200
2737*4882a593Smuzhiyun #define HUB_RESET_TIMEOUT	800
2738*4882a593Smuzhiyun 
use_new_scheme(struct usb_device * udev,int retry,struct usb_port * port_dev)2739*4882a593Smuzhiyun static bool use_new_scheme(struct usb_device *udev, int retry,
2740*4882a593Smuzhiyun 			   struct usb_port *port_dev)
2741*4882a593Smuzhiyun {
2742*4882a593Smuzhiyun 	int old_scheme_first_port =
2743*4882a593Smuzhiyun 		(port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) ||
2744*4882a593Smuzhiyun 		old_scheme_first;
2745*4882a593Smuzhiyun 
2746*4882a593Smuzhiyun 	/*
2747*4882a593Smuzhiyun 	 * "New scheme" enumeration causes an extra state transition to be
2748*4882a593Smuzhiyun 	 * exposed to an xhci host and causes USB3 devices to receive control
2749*4882a593Smuzhiyun 	 * commands in the default state.  This has been seen to cause
2750*4882a593Smuzhiyun 	 * enumeration failures, so disable this enumeration scheme for USB3
2751*4882a593Smuzhiyun 	 * devices.
2752*4882a593Smuzhiyun 	 */
2753*4882a593Smuzhiyun 	if (udev->speed >= USB_SPEED_SUPER)
2754*4882a593Smuzhiyun 		return false;
2755*4882a593Smuzhiyun 
2756*4882a593Smuzhiyun 	/*
2757*4882a593Smuzhiyun 	 * If use_both_schemes is set, use the first scheme (whichever
2758*4882a593Smuzhiyun 	 * it is) for the larger half of the retries, then use the other
2759*4882a593Smuzhiyun 	 * scheme.  Otherwise, use the first scheme for all the retries.
2760*4882a593Smuzhiyun 	 */
2761*4882a593Smuzhiyun 	if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2)
2762*4882a593Smuzhiyun 		return old_scheme_first_port;	/* Second half */
2763*4882a593Smuzhiyun 	return !old_scheme_first_port;		/* First half or all */
2764*4882a593Smuzhiyun }
2765*4882a593Smuzhiyun 
2766*4882a593Smuzhiyun /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
2767*4882a593Smuzhiyun  * Port warm reset is required to recover
2768*4882a593Smuzhiyun  */
hub_port_warm_reset_required(struct usb_hub * hub,int port1,u16 portstatus)2769*4882a593Smuzhiyun static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2770*4882a593Smuzhiyun 		u16 portstatus)
2771*4882a593Smuzhiyun {
2772*4882a593Smuzhiyun 	u16 link_state;
2773*4882a593Smuzhiyun 
2774*4882a593Smuzhiyun 	if (!hub_is_superspeed(hub->hdev))
2775*4882a593Smuzhiyun 		return false;
2776*4882a593Smuzhiyun 
2777*4882a593Smuzhiyun 	if (test_bit(port1, hub->warm_reset_bits))
2778*4882a593Smuzhiyun 		return true;
2779*4882a593Smuzhiyun 
2780*4882a593Smuzhiyun 	link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2781*4882a593Smuzhiyun 	return link_state == USB_SS_PORT_LS_SS_INACTIVE
2782*4882a593Smuzhiyun 		|| link_state == USB_SS_PORT_LS_COMP_MOD;
2783*4882a593Smuzhiyun }
2784*4882a593Smuzhiyun 
hub_port_wait_reset(struct usb_hub * hub,int port1,struct usb_device * udev,unsigned int delay,bool warm)2785*4882a593Smuzhiyun static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2786*4882a593Smuzhiyun 			struct usb_device *udev, unsigned int delay, bool warm)
2787*4882a593Smuzhiyun {
2788*4882a593Smuzhiyun 	int delay_time, ret;
2789*4882a593Smuzhiyun 	u16 portstatus;
2790*4882a593Smuzhiyun 	u16 portchange;
2791*4882a593Smuzhiyun 	u32 ext_portstatus = 0;
2792*4882a593Smuzhiyun 
2793*4882a593Smuzhiyun 	for (delay_time = 0;
2794*4882a593Smuzhiyun 			delay_time < HUB_RESET_TIMEOUT;
2795*4882a593Smuzhiyun 			delay_time += delay) {
2796*4882a593Smuzhiyun 		/* wait to give the device a chance to reset */
2797*4882a593Smuzhiyun 		msleep(delay);
2798*4882a593Smuzhiyun 
2799*4882a593Smuzhiyun 		/* read and decode port status */
2800*4882a593Smuzhiyun 		if (hub_is_superspeedplus(hub->hdev))
2801*4882a593Smuzhiyun 			ret = hub_ext_port_status(hub, port1,
2802*4882a593Smuzhiyun 						  HUB_EXT_PORT_STATUS,
2803*4882a593Smuzhiyun 						  &portstatus, &portchange,
2804*4882a593Smuzhiyun 						  &ext_portstatus);
2805*4882a593Smuzhiyun 		else
2806*4882a593Smuzhiyun 			ret = hub_port_status(hub, port1, &portstatus,
2807*4882a593Smuzhiyun 					      &portchange);
2808*4882a593Smuzhiyun 		if (ret < 0)
2809*4882a593Smuzhiyun 			return ret;
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun 		/*
2812*4882a593Smuzhiyun 		 * The port state is unknown until the reset completes.
2813*4882a593Smuzhiyun 		 *
2814*4882a593Smuzhiyun 		 * On top of that, some chips may require additional time
2815*4882a593Smuzhiyun 		 * to re-establish a connection after the reset is complete,
2816*4882a593Smuzhiyun 		 * so also wait for the connection to be re-established.
2817*4882a593Smuzhiyun 		 */
2818*4882a593Smuzhiyun 		if (!(portstatus & USB_PORT_STAT_RESET) &&
2819*4882a593Smuzhiyun 		    (portstatus & USB_PORT_STAT_CONNECTION))
2820*4882a593Smuzhiyun 			break;
2821*4882a593Smuzhiyun 
2822*4882a593Smuzhiyun 		/* switch to the long delay after two short delay failures */
2823*4882a593Smuzhiyun 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2824*4882a593Smuzhiyun 			delay = HUB_LONG_RESET_TIME;
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun 		dev_dbg(&hub->ports[port1 - 1]->dev,
2827*4882a593Smuzhiyun 				"not %sreset yet, waiting %dms\n",
2828*4882a593Smuzhiyun 				warm ? "warm " : "", delay);
2829*4882a593Smuzhiyun 	}
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun 	if ((portstatus & USB_PORT_STAT_RESET))
2832*4882a593Smuzhiyun 		return -EBUSY;
2833*4882a593Smuzhiyun 
2834*4882a593Smuzhiyun 	if (hub_port_warm_reset_required(hub, port1, portstatus))
2835*4882a593Smuzhiyun 		return -ENOTCONN;
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 	/* Device went away? */
2838*4882a593Smuzhiyun 	if (!(portstatus & USB_PORT_STAT_CONNECTION))
2839*4882a593Smuzhiyun 		return -ENOTCONN;
2840*4882a593Smuzhiyun 
2841*4882a593Smuzhiyun 	/* Retry if connect change is set but status is still connected.
2842*4882a593Smuzhiyun 	 * A USB 3.0 connection may bounce if multiple warm resets were issued,
2843*4882a593Smuzhiyun 	 * but the device may have successfully re-connected. Ignore it.
2844*4882a593Smuzhiyun 	 */
2845*4882a593Smuzhiyun 	if (!hub_is_superspeed(hub->hdev) &&
2846*4882a593Smuzhiyun 	    (portchange & USB_PORT_STAT_C_CONNECTION)) {
2847*4882a593Smuzhiyun 		usb_clear_port_feature(hub->hdev, port1,
2848*4882a593Smuzhiyun 				       USB_PORT_FEAT_C_CONNECTION);
2849*4882a593Smuzhiyun 		return -EAGAIN;
2850*4882a593Smuzhiyun 	}
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun 	if (!(portstatus & USB_PORT_STAT_ENABLE))
2853*4882a593Smuzhiyun 		return -EBUSY;
2854*4882a593Smuzhiyun 
2855*4882a593Smuzhiyun 	if (!udev)
2856*4882a593Smuzhiyun 		return 0;
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun 	if (hub_is_superspeedplus(hub->hdev)) {
2859*4882a593Smuzhiyun 		/* extended portstatus Rx and Tx lane count are zero based */
2860*4882a593Smuzhiyun 		udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1;
2861*4882a593Smuzhiyun 		udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1;
2862*4882a593Smuzhiyun 	} else {
2863*4882a593Smuzhiyun 		udev->rx_lanes = 1;
2864*4882a593Smuzhiyun 		udev->tx_lanes = 1;
2865*4882a593Smuzhiyun 	}
2866*4882a593Smuzhiyun 	if (hub_is_wusb(hub))
2867*4882a593Smuzhiyun 		udev->speed = USB_SPEED_WIRELESS;
2868*4882a593Smuzhiyun 	else if (hub_is_superspeedplus(hub->hdev) &&
2869*4882a593Smuzhiyun 		 port_speed_is_ssp(hub->hdev, ext_portstatus &
2870*4882a593Smuzhiyun 				   USB_EXT_PORT_STAT_RX_SPEED_ID))
2871*4882a593Smuzhiyun 		udev->speed = USB_SPEED_SUPER_PLUS;
2872*4882a593Smuzhiyun 	else if (hub_is_superspeed(hub->hdev))
2873*4882a593Smuzhiyun 		udev->speed = USB_SPEED_SUPER;
2874*4882a593Smuzhiyun 	else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2875*4882a593Smuzhiyun 		udev->speed = USB_SPEED_HIGH;
2876*4882a593Smuzhiyun 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2877*4882a593Smuzhiyun 		udev->speed = USB_SPEED_LOW;
2878*4882a593Smuzhiyun 	else
2879*4882a593Smuzhiyun 		udev->speed = USB_SPEED_FULL;
2880*4882a593Smuzhiyun 	return 0;
2881*4882a593Smuzhiyun }
2882*4882a593Smuzhiyun 
2883*4882a593Smuzhiyun /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
hub_port_reset(struct usb_hub * hub,int port1,struct usb_device * udev,unsigned int delay,bool warm)2884*4882a593Smuzhiyun static int hub_port_reset(struct usb_hub *hub, int port1,
2885*4882a593Smuzhiyun 			struct usb_device *udev, unsigned int delay, bool warm)
2886*4882a593Smuzhiyun {
2887*4882a593Smuzhiyun 	int i, status;
2888*4882a593Smuzhiyun 	u16 portchange, portstatus;
2889*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
2890*4882a593Smuzhiyun 	int reset_recovery_time;
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	if (!hub_is_superspeed(hub->hdev)) {
2893*4882a593Smuzhiyun 		if (warm) {
2894*4882a593Smuzhiyun 			dev_err(hub->intfdev, "only USB3 hub support "
2895*4882a593Smuzhiyun 						"warm reset\n");
2896*4882a593Smuzhiyun 			return -EINVAL;
2897*4882a593Smuzhiyun 		}
2898*4882a593Smuzhiyun 		/* Block EHCI CF initialization during the port reset.
2899*4882a593Smuzhiyun 		 * Some companion controllers don't like it when they mix.
2900*4882a593Smuzhiyun 		 */
2901*4882a593Smuzhiyun 		down_read(&ehci_cf_port_reset_rwsem);
2902*4882a593Smuzhiyun 	} else if (!warm) {
2903*4882a593Smuzhiyun 		/*
2904*4882a593Smuzhiyun 		 * If the caller hasn't explicitly requested a warm reset,
2905*4882a593Smuzhiyun 		 * double check and see if one is needed.
2906*4882a593Smuzhiyun 		 */
2907*4882a593Smuzhiyun 		if (hub_port_status(hub, port1, &portstatus, &portchange) == 0)
2908*4882a593Smuzhiyun 			if (hub_port_warm_reset_required(hub, port1,
2909*4882a593Smuzhiyun 							portstatus))
2910*4882a593Smuzhiyun 				warm = true;
2911*4882a593Smuzhiyun 	}
2912*4882a593Smuzhiyun 	clear_bit(port1, hub->warm_reset_bits);
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun 	/* Reset the port */
2915*4882a593Smuzhiyun 	for (i = 0; i < PORT_RESET_TRIES; i++) {
2916*4882a593Smuzhiyun 		status = set_port_feature(hub->hdev, port1, (warm ?
2917*4882a593Smuzhiyun 					USB_PORT_FEAT_BH_PORT_RESET :
2918*4882a593Smuzhiyun 					USB_PORT_FEAT_RESET));
2919*4882a593Smuzhiyun 		if (status == -ENODEV) {
2920*4882a593Smuzhiyun 			;	/* The hub is gone */
2921*4882a593Smuzhiyun 		} else if (status) {
2922*4882a593Smuzhiyun 			dev_err(&port_dev->dev,
2923*4882a593Smuzhiyun 					"cannot %sreset (err = %d)\n",
2924*4882a593Smuzhiyun 					warm ? "warm " : "", status);
2925*4882a593Smuzhiyun 		} else {
2926*4882a593Smuzhiyun 			status = hub_port_wait_reset(hub, port1, udev, delay,
2927*4882a593Smuzhiyun 								warm);
2928*4882a593Smuzhiyun 			if (status && status != -ENOTCONN && status != -ENODEV)
2929*4882a593Smuzhiyun 				dev_dbg(hub->intfdev,
2930*4882a593Smuzhiyun 						"port_wait_reset: err = %d\n",
2931*4882a593Smuzhiyun 						status);
2932*4882a593Smuzhiyun 		}
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun 		/* Check for disconnect or reset */
2935*4882a593Smuzhiyun 		if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2936*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
2937*4882a593Smuzhiyun 					USB_PORT_FEAT_C_RESET);
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun 			if (!hub_is_superspeed(hub->hdev))
2940*4882a593Smuzhiyun 				goto done;
2941*4882a593Smuzhiyun 
2942*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
2943*4882a593Smuzhiyun 					USB_PORT_FEAT_C_BH_PORT_RESET);
2944*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
2945*4882a593Smuzhiyun 					USB_PORT_FEAT_C_PORT_LINK_STATE);
2946*4882a593Smuzhiyun 
2947*4882a593Smuzhiyun 			if (udev)
2948*4882a593Smuzhiyun 				usb_clear_port_feature(hub->hdev, port1,
2949*4882a593Smuzhiyun 					USB_PORT_FEAT_C_CONNECTION);
2950*4882a593Smuzhiyun 
2951*4882a593Smuzhiyun 			/*
2952*4882a593Smuzhiyun 			 * If a USB 3.0 device migrates from reset to an error
2953*4882a593Smuzhiyun 			 * state, re-issue the warm reset.
2954*4882a593Smuzhiyun 			 */
2955*4882a593Smuzhiyun 			if (hub_port_status(hub, port1,
2956*4882a593Smuzhiyun 					&portstatus, &portchange) < 0)
2957*4882a593Smuzhiyun 				goto done;
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun 			if (!hub_port_warm_reset_required(hub, port1,
2960*4882a593Smuzhiyun 					portstatus))
2961*4882a593Smuzhiyun 				goto done;
2962*4882a593Smuzhiyun 
2963*4882a593Smuzhiyun 			/*
2964*4882a593Smuzhiyun 			 * If the port is in SS.Inactive or Compliance Mode, the
2965*4882a593Smuzhiyun 			 * hot or warm reset failed.  Try another warm reset.
2966*4882a593Smuzhiyun 			 */
2967*4882a593Smuzhiyun 			if (!warm) {
2968*4882a593Smuzhiyun 				dev_dbg(&port_dev->dev,
2969*4882a593Smuzhiyun 						"hot reset failed, warm reset\n");
2970*4882a593Smuzhiyun 				warm = true;
2971*4882a593Smuzhiyun 			}
2972*4882a593Smuzhiyun 		}
2973*4882a593Smuzhiyun 
2974*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev,
2975*4882a593Smuzhiyun 				"not enabled, trying %sreset again...\n",
2976*4882a593Smuzhiyun 				warm ? "warm " : "");
2977*4882a593Smuzhiyun 		delay = HUB_LONG_RESET_TIME;
2978*4882a593Smuzhiyun 	}
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun 	dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
2981*4882a593Smuzhiyun 
2982*4882a593Smuzhiyun done:
2983*4882a593Smuzhiyun 	if (status == 0) {
2984*4882a593Smuzhiyun 		if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM)
2985*4882a593Smuzhiyun 			usleep_range(10000, 12000);
2986*4882a593Smuzhiyun 		else {
2987*4882a593Smuzhiyun 			/* TRSTRCY = 10 ms; plus some extra */
2988*4882a593Smuzhiyun 			reset_recovery_time = 10 + 40;
2989*4882a593Smuzhiyun 
2990*4882a593Smuzhiyun 			/* Hub needs extra delay after resetting its port. */
2991*4882a593Smuzhiyun 			if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET)
2992*4882a593Smuzhiyun 				reset_recovery_time += 100;
2993*4882a593Smuzhiyun 
2994*4882a593Smuzhiyun 			msleep(reset_recovery_time);
2995*4882a593Smuzhiyun 		}
2996*4882a593Smuzhiyun 
2997*4882a593Smuzhiyun 		if (udev) {
2998*4882a593Smuzhiyun 			struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2999*4882a593Smuzhiyun 
3000*4882a593Smuzhiyun 			update_devnum(udev, 0);
3001*4882a593Smuzhiyun 			/* The xHC may think the device is already reset,
3002*4882a593Smuzhiyun 			 * so ignore the status.
3003*4882a593Smuzhiyun 			 */
3004*4882a593Smuzhiyun 			if (hcd->driver->reset_device)
3005*4882a593Smuzhiyun 				hcd->driver->reset_device(hcd, udev);
3006*4882a593Smuzhiyun 
3007*4882a593Smuzhiyun 			usb_set_device_state(udev, USB_STATE_DEFAULT);
3008*4882a593Smuzhiyun 		}
3009*4882a593Smuzhiyun 	} else {
3010*4882a593Smuzhiyun 		if (udev)
3011*4882a593Smuzhiyun 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
3012*4882a593Smuzhiyun 	}
3013*4882a593Smuzhiyun 
3014*4882a593Smuzhiyun 	if (!hub_is_superspeed(hub->hdev))
3015*4882a593Smuzhiyun 		up_read(&ehci_cf_port_reset_rwsem);
3016*4882a593Smuzhiyun 
3017*4882a593Smuzhiyun 	return status;
3018*4882a593Smuzhiyun }
3019*4882a593Smuzhiyun 
3020*4882a593Smuzhiyun /* Check if a port is power on */
port_is_power_on(struct usb_hub * hub,unsigned portstatus)3021*4882a593Smuzhiyun static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
3022*4882a593Smuzhiyun {
3023*4882a593Smuzhiyun 	int ret = 0;
3024*4882a593Smuzhiyun 
3025*4882a593Smuzhiyun 	if (hub_is_superspeed(hub->hdev)) {
3026*4882a593Smuzhiyun 		if (portstatus & USB_SS_PORT_STAT_POWER)
3027*4882a593Smuzhiyun 			ret = 1;
3028*4882a593Smuzhiyun 	} else {
3029*4882a593Smuzhiyun 		if (portstatus & USB_PORT_STAT_POWER)
3030*4882a593Smuzhiyun 			ret = 1;
3031*4882a593Smuzhiyun 	}
3032*4882a593Smuzhiyun 
3033*4882a593Smuzhiyun 	return ret;
3034*4882a593Smuzhiyun }
3035*4882a593Smuzhiyun 
usb_lock_port(struct usb_port * port_dev)3036*4882a593Smuzhiyun static void usb_lock_port(struct usb_port *port_dev)
3037*4882a593Smuzhiyun 		__acquires(&port_dev->status_lock)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun 	mutex_lock(&port_dev->status_lock);
3040*4882a593Smuzhiyun 	__acquire(&port_dev->status_lock);
3041*4882a593Smuzhiyun }
3042*4882a593Smuzhiyun 
usb_unlock_port(struct usb_port * port_dev)3043*4882a593Smuzhiyun static void usb_unlock_port(struct usb_port *port_dev)
3044*4882a593Smuzhiyun 		__releases(&port_dev->status_lock)
3045*4882a593Smuzhiyun {
3046*4882a593Smuzhiyun 	mutex_unlock(&port_dev->status_lock);
3047*4882a593Smuzhiyun 	__release(&port_dev->status_lock);
3048*4882a593Smuzhiyun }
3049*4882a593Smuzhiyun 
3050*4882a593Smuzhiyun #ifdef	CONFIG_PM
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
port_is_suspended(struct usb_hub * hub,unsigned portstatus)3053*4882a593Smuzhiyun static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
3054*4882a593Smuzhiyun {
3055*4882a593Smuzhiyun 	int ret = 0;
3056*4882a593Smuzhiyun 
3057*4882a593Smuzhiyun 	if (hub_is_superspeed(hub->hdev)) {
3058*4882a593Smuzhiyun 		if ((portstatus & USB_PORT_STAT_LINK_STATE)
3059*4882a593Smuzhiyun 				== USB_SS_PORT_LS_U3)
3060*4882a593Smuzhiyun 			ret = 1;
3061*4882a593Smuzhiyun 	} else {
3062*4882a593Smuzhiyun 		if (portstatus & USB_PORT_STAT_SUSPEND)
3063*4882a593Smuzhiyun 			ret = 1;
3064*4882a593Smuzhiyun 	}
3065*4882a593Smuzhiyun 
3066*4882a593Smuzhiyun 	return ret;
3067*4882a593Smuzhiyun }
3068*4882a593Smuzhiyun 
3069*4882a593Smuzhiyun /* Determine whether the device on a port is ready for a normal resume,
3070*4882a593Smuzhiyun  * is ready for a reset-resume, or should be disconnected.
3071*4882a593Smuzhiyun  */
check_port_resume_type(struct usb_device * udev,struct usb_hub * hub,int port1,int status,u16 portchange,u16 portstatus)3072*4882a593Smuzhiyun static int check_port_resume_type(struct usb_device *udev,
3073*4882a593Smuzhiyun 		struct usb_hub *hub, int port1,
3074*4882a593Smuzhiyun 		int status, u16 portchange, u16 portstatus)
3075*4882a593Smuzhiyun {
3076*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
3077*4882a593Smuzhiyun 	int retries = 3;
3078*4882a593Smuzhiyun 
3079*4882a593Smuzhiyun  retry:
3080*4882a593Smuzhiyun 	/* Is a warm reset needed to recover the connection? */
3081*4882a593Smuzhiyun 	if (status == 0 && udev->reset_resume
3082*4882a593Smuzhiyun 		&& hub_port_warm_reset_required(hub, port1, portstatus)) {
3083*4882a593Smuzhiyun 		/* pass */;
3084*4882a593Smuzhiyun 	}
3085*4882a593Smuzhiyun 	/* Is the device still present? */
3086*4882a593Smuzhiyun 	else if (status || port_is_suspended(hub, portstatus) ||
3087*4882a593Smuzhiyun 			!port_is_power_on(hub, portstatus)) {
3088*4882a593Smuzhiyun 		if (status >= 0)
3089*4882a593Smuzhiyun 			status = -ENODEV;
3090*4882a593Smuzhiyun 	} else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
3091*4882a593Smuzhiyun 		if (retries--) {
3092*4882a593Smuzhiyun 			usleep_range(200, 300);
3093*4882a593Smuzhiyun 			status = hub_port_status(hub, port1, &portstatus,
3094*4882a593Smuzhiyun 							     &portchange);
3095*4882a593Smuzhiyun 			goto retry;
3096*4882a593Smuzhiyun 		}
3097*4882a593Smuzhiyun 		status = -ENODEV;
3098*4882a593Smuzhiyun 	}
3099*4882a593Smuzhiyun 
3100*4882a593Smuzhiyun 	/* Can't do a normal resume if the port isn't enabled,
3101*4882a593Smuzhiyun 	 * so try a reset-resume instead.
3102*4882a593Smuzhiyun 	 */
3103*4882a593Smuzhiyun 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
3104*4882a593Smuzhiyun 		if (udev->persist_enabled)
3105*4882a593Smuzhiyun 			udev->reset_resume = 1;
3106*4882a593Smuzhiyun 		else
3107*4882a593Smuzhiyun 			status = -ENODEV;
3108*4882a593Smuzhiyun 	}
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun 	if (status) {
3111*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
3112*4882a593Smuzhiyun 				portchange, portstatus, status);
3113*4882a593Smuzhiyun 	} else if (udev->reset_resume) {
3114*4882a593Smuzhiyun 
3115*4882a593Smuzhiyun 		/* Late port handoff can set status-change bits */
3116*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_CONNECTION)
3117*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
3118*4882a593Smuzhiyun 					USB_PORT_FEAT_C_CONNECTION);
3119*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_ENABLE)
3120*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
3121*4882a593Smuzhiyun 					USB_PORT_FEAT_C_ENABLE);
3122*4882a593Smuzhiyun 
3123*4882a593Smuzhiyun 		/*
3124*4882a593Smuzhiyun 		 * Whatever made this reset-resume necessary may have
3125*4882a593Smuzhiyun 		 * turned on the port1 bit in hub->change_bits.  But after
3126*4882a593Smuzhiyun 		 * a successful reset-resume we want the bit to be clear;
3127*4882a593Smuzhiyun 		 * if it was on it would indicate that something happened
3128*4882a593Smuzhiyun 		 * following the reset-resume.
3129*4882a593Smuzhiyun 		 */
3130*4882a593Smuzhiyun 		clear_bit(port1, hub->change_bits);
3131*4882a593Smuzhiyun 	}
3132*4882a593Smuzhiyun 
3133*4882a593Smuzhiyun 	return status;
3134*4882a593Smuzhiyun }
3135*4882a593Smuzhiyun 
usb_disable_ltm(struct usb_device * udev)3136*4882a593Smuzhiyun int usb_disable_ltm(struct usb_device *udev)
3137*4882a593Smuzhiyun {
3138*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3139*4882a593Smuzhiyun 
3140*4882a593Smuzhiyun 	/* Check if the roothub and device supports LTM. */
3141*4882a593Smuzhiyun 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3142*4882a593Smuzhiyun 			!usb_device_supports_ltm(udev))
3143*4882a593Smuzhiyun 		return 0;
3144*4882a593Smuzhiyun 
3145*4882a593Smuzhiyun 	/* Clear Feature LTM Enable can only be sent if the device is
3146*4882a593Smuzhiyun 	 * configured.
3147*4882a593Smuzhiyun 	 */
3148*4882a593Smuzhiyun 	if (!udev->actconfig)
3149*4882a593Smuzhiyun 		return 0;
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun 	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3152*4882a593Smuzhiyun 			USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3153*4882a593Smuzhiyun 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3154*4882a593Smuzhiyun 			USB_CTRL_SET_TIMEOUT);
3155*4882a593Smuzhiyun }
3156*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_disable_ltm);
3157*4882a593Smuzhiyun 
usb_enable_ltm(struct usb_device * udev)3158*4882a593Smuzhiyun void usb_enable_ltm(struct usb_device *udev)
3159*4882a593Smuzhiyun {
3160*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 	/* Check if the roothub and device supports LTM. */
3163*4882a593Smuzhiyun 	if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3164*4882a593Smuzhiyun 			!usb_device_supports_ltm(udev))
3165*4882a593Smuzhiyun 		return;
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 	/* Set Feature LTM Enable can only be sent if the device is
3168*4882a593Smuzhiyun 	 * configured.
3169*4882a593Smuzhiyun 	 */
3170*4882a593Smuzhiyun 	if (!udev->actconfig)
3171*4882a593Smuzhiyun 		return;
3172*4882a593Smuzhiyun 
3173*4882a593Smuzhiyun 	usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3174*4882a593Smuzhiyun 			USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3175*4882a593Smuzhiyun 			USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3176*4882a593Smuzhiyun 			USB_CTRL_SET_TIMEOUT);
3177*4882a593Smuzhiyun }
3178*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_enable_ltm);
3179*4882a593Smuzhiyun 
3180*4882a593Smuzhiyun /*
3181*4882a593Smuzhiyun  * usb_enable_remote_wakeup - enable remote wakeup for a device
3182*4882a593Smuzhiyun  * @udev: target device
3183*4882a593Smuzhiyun  *
3184*4882a593Smuzhiyun  * For USB-2 devices: Set the device's remote wakeup feature.
3185*4882a593Smuzhiyun  *
3186*4882a593Smuzhiyun  * For USB-3 devices: Assume there's only one function on the device and
3187*4882a593Smuzhiyun  * enable remote wake for the first interface.  FIXME if the interface
3188*4882a593Smuzhiyun  * association descriptor shows there's more than one function.
3189*4882a593Smuzhiyun  */
usb_enable_remote_wakeup(struct usb_device * udev)3190*4882a593Smuzhiyun static int usb_enable_remote_wakeup(struct usb_device *udev)
3191*4882a593Smuzhiyun {
3192*4882a593Smuzhiyun 	if (udev->speed < USB_SPEED_SUPER)
3193*4882a593Smuzhiyun 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3194*4882a593Smuzhiyun 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3195*4882a593Smuzhiyun 				USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3196*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
3197*4882a593Smuzhiyun 	else
3198*4882a593Smuzhiyun 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3199*4882a593Smuzhiyun 				USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3200*4882a593Smuzhiyun 				USB_INTRF_FUNC_SUSPEND,
3201*4882a593Smuzhiyun 				USB_INTRF_FUNC_SUSPEND_RW |
3202*4882a593Smuzhiyun 					USB_INTRF_FUNC_SUSPEND_LP,
3203*4882a593Smuzhiyun 				NULL, 0, USB_CTRL_SET_TIMEOUT);
3204*4882a593Smuzhiyun }
3205*4882a593Smuzhiyun 
3206*4882a593Smuzhiyun /*
3207*4882a593Smuzhiyun  * usb_disable_remote_wakeup - disable remote wakeup for a device
3208*4882a593Smuzhiyun  * @udev: target device
3209*4882a593Smuzhiyun  *
3210*4882a593Smuzhiyun  * For USB-2 devices: Clear the device's remote wakeup feature.
3211*4882a593Smuzhiyun  *
3212*4882a593Smuzhiyun  * For USB-3 devices: Assume there's only one function on the device and
3213*4882a593Smuzhiyun  * disable remote wake for the first interface.  FIXME if the interface
3214*4882a593Smuzhiyun  * association descriptor shows there's more than one function.
3215*4882a593Smuzhiyun  */
usb_disable_remote_wakeup(struct usb_device * udev)3216*4882a593Smuzhiyun static int usb_disable_remote_wakeup(struct usb_device *udev)
3217*4882a593Smuzhiyun {
3218*4882a593Smuzhiyun 	if (udev->speed < USB_SPEED_SUPER)
3219*4882a593Smuzhiyun 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3220*4882a593Smuzhiyun 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3221*4882a593Smuzhiyun 				USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3222*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
3223*4882a593Smuzhiyun 	else
3224*4882a593Smuzhiyun 		return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3225*4882a593Smuzhiyun 				USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3226*4882a593Smuzhiyun 				USB_INTRF_FUNC_SUSPEND,	0, NULL, 0,
3227*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
3228*4882a593Smuzhiyun }
3229*4882a593Smuzhiyun 
3230*4882a593Smuzhiyun /* Count of wakeup-enabled devices at or below udev */
usb_wakeup_enabled_descendants(struct usb_device * udev)3231*4882a593Smuzhiyun unsigned usb_wakeup_enabled_descendants(struct usb_device *udev)
3232*4882a593Smuzhiyun {
3233*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3234*4882a593Smuzhiyun 
3235*4882a593Smuzhiyun 	return udev->do_remote_wakeup +
3236*4882a593Smuzhiyun 			(hub ? hub->wakeup_enabled_descendants : 0);
3237*4882a593Smuzhiyun }
3238*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants);
3239*4882a593Smuzhiyun 
3240*4882a593Smuzhiyun /*
3241*4882a593Smuzhiyun  * usb_port_suspend - suspend a usb device's upstream port
3242*4882a593Smuzhiyun  * @udev: device that's no longer in active use, not a root hub
3243*4882a593Smuzhiyun  * Context: must be able to sleep; device not locked; pm locks held
3244*4882a593Smuzhiyun  *
3245*4882a593Smuzhiyun  * Suspends a USB device that isn't in active use, conserving power.
3246*4882a593Smuzhiyun  * Devices may wake out of a suspend, if anything important happens,
3247*4882a593Smuzhiyun  * using the remote wakeup mechanism.  They may also be taken out of
3248*4882a593Smuzhiyun  * suspend by the host, using usb_port_resume().  It's also routine
3249*4882a593Smuzhiyun  * to disconnect devices while they are suspended.
3250*4882a593Smuzhiyun  *
3251*4882a593Smuzhiyun  * This only affects the USB hardware for a device; its interfaces
3252*4882a593Smuzhiyun  * (and, for hubs, child devices) must already have been suspended.
3253*4882a593Smuzhiyun  *
3254*4882a593Smuzhiyun  * Selective port suspend reduces power; most suspended devices draw
3255*4882a593Smuzhiyun  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
3256*4882a593Smuzhiyun  * All devices below the suspended port are also suspended.
3257*4882a593Smuzhiyun  *
3258*4882a593Smuzhiyun  * Devices leave suspend state when the host wakes them up.  Some devices
3259*4882a593Smuzhiyun  * also support "remote wakeup", where the device can activate the USB
3260*4882a593Smuzhiyun  * tree above them to deliver data, such as a keypress or packet.  In
3261*4882a593Smuzhiyun  * some cases, this wakes the USB host.
3262*4882a593Smuzhiyun  *
3263*4882a593Smuzhiyun  * Suspending OTG devices may trigger HNP, if that's been enabled
3264*4882a593Smuzhiyun  * between a pair of dual-role devices.  That will change roles, such
3265*4882a593Smuzhiyun  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3266*4882a593Smuzhiyun  *
3267*4882a593Smuzhiyun  * Devices on USB hub ports have only one "suspend" state, corresponding
3268*4882a593Smuzhiyun  * to ACPI D2, "may cause the device to lose some context".
3269*4882a593Smuzhiyun  * State transitions include:
3270*4882a593Smuzhiyun  *
3271*4882a593Smuzhiyun  *   - suspend, resume ... when the VBUS power link stays live
3272*4882a593Smuzhiyun  *   - suspend, disconnect ... VBUS lost
3273*4882a593Smuzhiyun  *
3274*4882a593Smuzhiyun  * Once VBUS drop breaks the circuit, the port it's using has to go through
3275*4882a593Smuzhiyun  * normal re-enumeration procedures, starting with enabling VBUS power.
3276*4882a593Smuzhiyun  * Other than re-initializing the hub (plug/unplug, except for root hubs),
3277*4882a593Smuzhiyun  * Linux (2.6) currently has NO mechanisms to initiate that:  no hub_wq
3278*4882a593Smuzhiyun  * timer, no SRP, no requests through sysfs.
3279*4882a593Smuzhiyun  *
3280*4882a593Smuzhiyun  * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3281*4882a593Smuzhiyun  * suspended until their bus goes into global suspend (i.e., the root
3282*4882a593Smuzhiyun  * hub is suspended).  Nevertheless, we change @udev->state to
3283*4882a593Smuzhiyun  * USB_STATE_SUSPENDED as this is the device's "logical" state.  The actual
3284*4882a593Smuzhiyun  * upstream port setting is stored in @udev->port_is_suspended.
3285*4882a593Smuzhiyun  *
3286*4882a593Smuzhiyun  * Returns 0 on success, else negative errno.
3287*4882a593Smuzhiyun  */
usb_port_suspend(struct usb_device * udev,pm_message_t msg)3288*4882a593Smuzhiyun int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3289*4882a593Smuzhiyun {
3290*4882a593Smuzhiyun 	struct usb_hub	*hub = usb_hub_to_struct_hub(udev->parent);
3291*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[udev->portnum - 1];
3292*4882a593Smuzhiyun 	int		port1 = udev->portnum;
3293*4882a593Smuzhiyun 	int		status;
3294*4882a593Smuzhiyun 	bool		really_suspend = true;
3295*4882a593Smuzhiyun 
3296*4882a593Smuzhiyun 	usb_lock_port(port_dev);
3297*4882a593Smuzhiyun 
3298*4882a593Smuzhiyun 	/* enable remote wakeup when appropriate; this lets the device
3299*4882a593Smuzhiyun 	 * wake up the upstream hub (including maybe the root hub).
3300*4882a593Smuzhiyun 	 *
3301*4882a593Smuzhiyun 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
3302*4882a593Smuzhiyun 	 * we don't explicitly enable it here.
3303*4882a593Smuzhiyun 	 */
3304*4882a593Smuzhiyun 	if (udev->do_remote_wakeup) {
3305*4882a593Smuzhiyun 		status = usb_enable_remote_wakeup(udev);
3306*4882a593Smuzhiyun 		if (status) {
3307*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3308*4882a593Smuzhiyun 					status);
3309*4882a593Smuzhiyun 			/* bail if autosuspend is requested */
3310*4882a593Smuzhiyun 			if (PMSG_IS_AUTO(msg))
3311*4882a593Smuzhiyun 				goto err_wakeup;
3312*4882a593Smuzhiyun 		}
3313*4882a593Smuzhiyun 	}
3314*4882a593Smuzhiyun 
3315*4882a593Smuzhiyun 	/* disable USB2 hardware LPM */
3316*4882a593Smuzhiyun 	usb_disable_usb2_hardware_lpm(udev);
3317*4882a593Smuzhiyun 
3318*4882a593Smuzhiyun 	if (usb_disable_ltm(udev)) {
3319*4882a593Smuzhiyun 		dev_err(&udev->dev, "Failed to disable LTM before suspend\n");
3320*4882a593Smuzhiyun 		status = -ENOMEM;
3321*4882a593Smuzhiyun 		if (PMSG_IS_AUTO(msg))
3322*4882a593Smuzhiyun 			goto err_ltm;
3323*4882a593Smuzhiyun 	}
3324*4882a593Smuzhiyun 
3325*4882a593Smuzhiyun 	/* see 7.1.7.6 */
3326*4882a593Smuzhiyun 	if (hub_is_superspeed(hub->hdev))
3327*4882a593Smuzhiyun 		status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
3328*4882a593Smuzhiyun 
3329*4882a593Smuzhiyun 	/*
3330*4882a593Smuzhiyun 	 * For system suspend, we do not need to enable the suspend feature
3331*4882a593Smuzhiyun 	 * on individual USB-2 ports.  The devices will automatically go
3332*4882a593Smuzhiyun 	 * into suspend a few ms after the root hub stops sending packets.
3333*4882a593Smuzhiyun 	 * The USB 2.0 spec calls this "global suspend".
3334*4882a593Smuzhiyun 	 *
3335*4882a593Smuzhiyun 	 * However, many USB hubs have a bug: They don't relay wakeup requests
3336*4882a593Smuzhiyun 	 * from a downstream port if the port's suspend feature isn't on.
3337*4882a593Smuzhiyun 	 * Therefore we will turn on the suspend feature if udev or any of its
3338*4882a593Smuzhiyun 	 * descendants is enabled for remote wakeup.
3339*4882a593Smuzhiyun 	 */
3340*4882a593Smuzhiyun 	else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0)
3341*4882a593Smuzhiyun 		status = set_port_feature(hub->hdev, port1,
3342*4882a593Smuzhiyun 				USB_PORT_FEAT_SUSPEND);
3343*4882a593Smuzhiyun 	else {
3344*4882a593Smuzhiyun 		really_suspend = false;
3345*4882a593Smuzhiyun 		status = 0;
3346*4882a593Smuzhiyun 	}
3347*4882a593Smuzhiyun 	if (status) {
3348*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
3349*4882a593Smuzhiyun 
3350*4882a593Smuzhiyun 		/* Try to enable USB3 LTM again */
3351*4882a593Smuzhiyun 		usb_enable_ltm(udev);
3352*4882a593Smuzhiyun  err_ltm:
3353*4882a593Smuzhiyun 		/* Try to enable USB2 hardware LPM again */
3354*4882a593Smuzhiyun 		usb_enable_usb2_hardware_lpm(udev);
3355*4882a593Smuzhiyun 
3356*4882a593Smuzhiyun 		if (udev->do_remote_wakeup)
3357*4882a593Smuzhiyun 			(void) usb_disable_remote_wakeup(udev);
3358*4882a593Smuzhiyun  err_wakeup:
3359*4882a593Smuzhiyun 
3360*4882a593Smuzhiyun 		/* System sleep transitions should never fail */
3361*4882a593Smuzhiyun 		if (!PMSG_IS_AUTO(msg))
3362*4882a593Smuzhiyun 			status = 0;
3363*4882a593Smuzhiyun 	} else {
3364*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3365*4882a593Smuzhiyun 				(PMSG_IS_AUTO(msg) ? "auto-" : ""),
3366*4882a593Smuzhiyun 				udev->do_remote_wakeup);
3367*4882a593Smuzhiyun 		if (really_suspend) {
3368*4882a593Smuzhiyun 			udev->port_is_suspended = 1;
3369*4882a593Smuzhiyun 
3370*4882a593Smuzhiyun 			/* device has up to 10 msec to fully suspend */
3371*4882a593Smuzhiyun 			msleep(10);
3372*4882a593Smuzhiyun 		}
3373*4882a593Smuzhiyun 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
3374*4882a593Smuzhiyun 	}
3375*4882a593Smuzhiyun 
3376*4882a593Smuzhiyun 	if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3377*4882a593Smuzhiyun 			&& test_and_clear_bit(port1, hub->child_usage_bits))
3378*4882a593Smuzhiyun 		pm_runtime_put_sync(&port_dev->dev);
3379*4882a593Smuzhiyun 
3380*4882a593Smuzhiyun 	usb_mark_last_busy(hub->hdev);
3381*4882a593Smuzhiyun 
3382*4882a593Smuzhiyun 	usb_unlock_port(port_dev);
3383*4882a593Smuzhiyun 	return status;
3384*4882a593Smuzhiyun }
3385*4882a593Smuzhiyun 
3386*4882a593Smuzhiyun /*
3387*4882a593Smuzhiyun  * If the USB "suspend" state is in use (rather than "global suspend"),
3388*4882a593Smuzhiyun  * many devices will be individually taken out of suspend state using
3389*4882a593Smuzhiyun  * special "resume" signaling.  This routine kicks in shortly after
3390*4882a593Smuzhiyun  * hardware resume signaling is finished, either because of selective
3391*4882a593Smuzhiyun  * resume (by host) or remote wakeup (by device) ... now see what changed
3392*4882a593Smuzhiyun  * in the tree that's rooted at this device.
3393*4882a593Smuzhiyun  *
3394*4882a593Smuzhiyun  * If @udev->reset_resume is set then the device is reset before the
3395*4882a593Smuzhiyun  * status check is done.
3396*4882a593Smuzhiyun  */
finish_port_resume(struct usb_device * udev)3397*4882a593Smuzhiyun static int finish_port_resume(struct usb_device *udev)
3398*4882a593Smuzhiyun {
3399*4882a593Smuzhiyun 	int	status = 0;
3400*4882a593Smuzhiyun 	u16	devstatus = 0;
3401*4882a593Smuzhiyun 
3402*4882a593Smuzhiyun 	/* caller owns the udev device lock */
3403*4882a593Smuzhiyun 	dev_dbg(&udev->dev, "%s\n",
3404*4882a593Smuzhiyun 		udev->reset_resume ? "finish reset-resume" : "finish resume");
3405*4882a593Smuzhiyun 
3406*4882a593Smuzhiyun 	/* usb ch9 identifies four variants of SUSPENDED, based on what
3407*4882a593Smuzhiyun 	 * state the device resumes to.  Linux currently won't see the
3408*4882a593Smuzhiyun 	 * first two on the host side; they'd be inside hub_port_init()
3409*4882a593Smuzhiyun 	 * during many timeouts, but hub_wq can't suspend until later.
3410*4882a593Smuzhiyun 	 */
3411*4882a593Smuzhiyun 	usb_set_device_state(udev, udev->actconfig
3412*4882a593Smuzhiyun 			? USB_STATE_CONFIGURED
3413*4882a593Smuzhiyun 			: USB_STATE_ADDRESS);
3414*4882a593Smuzhiyun 
3415*4882a593Smuzhiyun 	/* 10.5.4.5 says not to reset a suspended port if the attached
3416*4882a593Smuzhiyun 	 * device is enabled for remote wakeup.  Hence the reset
3417*4882a593Smuzhiyun 	 * operation is carried out here, after the port has been
3418*4882a593Smuzhiyun 	 * resumed.
3419*4882a593Smuzhiyun 	 */
3420*4882a593Smuzhiyun 	if (udev->reset_resume) {
3421*4882a593Smuzhiyun 		/*
3422*4882a593Smuzhiyun 		 * If the device morphs or switches modes when it is reset,
3423*4882a593Smuzhiyun 		 * we don't want to perform a reset-resume.  We'll fail the
3424*4882a593Smuzhiyun 		 * resume, which will cause a logical disconnect, and then
3425*4882a593Smuzhiyun 		 * the device will be rediscovered.
3426*4882a593Smuzhiyun 		 */
3427*4882a593Smuzhiyun  retry_reset_resume:
3428*4882a593Smuzhiyun 		if (udev->quirks & USB_QUIRK_RESET)
3429*4882a593Smuzhiyun 			status = -ENODEV;
3430*4882a593Smuzhiyun 		else
3431*4882a593Smuzhiyun 			status = usb_reset_and_verify_device(udev);
3432*4882a593Smuzhiyun 	}
3433*4882a593Smuzhiyun 
3434*4882a593Smuzhiyun 	/* 10.5.4.5 says be sure devices in the tree are still there.
3435*4882a593Smuzhiyun 	 * For now let's assume the device didn't go crazy on resume,
3436*4882a593Smuzhiyun 	 * and device drivers will know about any resume quirks.
3437*4882a593Smuzhiyun 	 */
3438*4882a593Smuzhiyun 	if (status == 0) {
3439*4882a593Smuzhiyun 		devstatus = 0;
3440*4882a593Smuzhiyun 		status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
3441*4882a593Smuzhiyun 
3442*4882a593Smuzhiyun 		/* If a normal resume failed, try doing a reset-resume */
3443*4882a593Smuzhiyun 		if (status && !udev->reset_resume && udev->persist_enabled) {
3444*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "retry with reset-resume\n");
3445*4882a593Smuzhiyun 			udev->reset_resume = 1;
3446*4882a593Smuzhiyun 			goto retry_reset_resume;
3447*4882a593Smuzhiyun 		}
3448*4882a593Smuzhiyun 	}
3449*4882a593Smuzhiyun 
3450*4882a593Smuzhiyun 	if (status) {
3451*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3452*4882a593Smuzhiyun 				status);
3453*4882a593Smuzhiyun 	/*
3454*4882a593Smuzhiyun 	 * There are a few quirky devices which violate the standard
3455*4882a593Smuzhiyun 	 * by claiming to have remote wakeup enabled after a reset,
3456*4882a593Smuzhiyun 	 * which crash if the feature is cleared, hence check for
3457*4882a593Smuzhiyun 	 * udev->reset_resume
3458*4882a593Smuzhiyun 	 */
3459*4882a593Smuzhiyun 	} else if (udev->actconfig && !udev->reset_resume) {
3460*4882a593Smuzhiyun 		if (udev->speed < USB_SPEED_SUPER) {
3461*4882a593Smuzhiyun 			if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
3462*4882a593Smuzhiyun 				status = usb_disable_remote_wakeup(udev);
3463*4882a593Smuzhiyun 		} else {
3464*4882a593Smuzhiyun 			status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0,
3465*4882a593Smuzhiyun 					&devstatus);
3466*4882a593Smuzhiyun 			if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3467*4882a593Smuzhiyun 					| USB_INTRF_STAT_FUNC_RW))
3468*4882a593Smuzhiyun 				status = usb_disable_remote_wakeup(udev);
3469*4882a593Smuzhiyun 		}
3470*4882a593Smuzhiyun 
3471*4882a593Smuzhiyun 		if (status)
3472*4882a593Smuzhiyun 			dev_dbg(&udev->dev,
3473*4882a593Smuzhiyun 				"disable remote wakeup, status %d\n",
3474*4882a593Smuzhiyun 				status);
3475*4882a593Smuzhiyun 		status = 0;
3476*4882a593Smuzhiyun 	}
3477*4882a593Smuzhiyun 	return status;
3478*4882a593Smuzhiyun }
3479*4882a593Smuzhiyun 
3480*4882a593Smuzhiyun /*
3481*4882a593Smuzhiyun  * There are some SS USB devices which take longer time for link training.
3482*4882a593Smuzhiyun  * XHCI specs 4.19.4 says that when Link training is successful, port
3483*4882a593Smuzhiyun  * sets CCS bit to 1. So if SW reads port status before successful link
3484*4882a593Smuzhiyun  * training, then it will not find device to be present.
3485*4882a593Smuzhiyun  * USB Analyzer log with such buggy devices show that in some cases
3486*4882a593Smuzhiyun  * device switch on the RX termination after long delay of host enabling
3487*4882a593Smuzhiyun  * the VBUS. In few other cases it has been seen that device fails to
3488*4882a593Smuzhiyun  * negotiate link training in first attempt. It has been
3489*4882a593Smuzhiyun  * reported till now that few devices take as long as 2000 ms to train
3490*4882a593Smuzhiyun  * the link after host enabling its VBUS and termination. Following
3491*4882a593Smuzhiyun  * routine implements a 2000 ms timeout for link training. If in a case
3492*4882a593Smuzhiyun  * link trains before timeout, loop will exit earlier.
3493*4882a593Smuzhiyun  *
3494*4882a593Smuzhiyun  * There are also some 2.0 hard drive based devices and 3.0 thumb
3495*4882a593Smuzhiyun  * drives that, when plugged into a 2.0 only port, take a long
3496*4882a593Smuzhiyun  * time to set CCS after VBUS enable.
3497*4882a593Smuzhiyun  *
3498*4882a593Smuzhiyun  * FIXME: If a device was connected before suspend, but was removed
3499*4882a593Smuzhiyun  * while system was asleep, then the loop in the following routine will
3500*4882a593Smuzhiyun  * only exit at timeout.
3501*4882a593Smuzhiyun  *
3502*4882a593Smuzhiyun  * This routine should only be called when persist is enabled.
3503*4882a593Smuzhiyun  */
wait_for_connected(struct usb_device * udev,struct usb_hub * hub,int * port1,u16 * portchange,u16 * portstatus)3504*4882a593Smuzhiyun static int wait_for_connected(struct usb_device *udev,
3505*4882a593Smuzhiyun 		struct usb_hub *hub, int *port1,
3506*4882a593Smuzhiyun 		u16 *portchange, u16 *portstatus)
3507*4882a593Smuzhiyun {
3508*4882a593Smuzhiyun 	int status = 0, delay_ms = 0;
3509*4882a593Smuzhiyun 
3510*4882a593Smuzhiyun 	while (delay_ms < 2000) {
3511*4882a593Smuzhiyun 		if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3512*4882a593Smuzhiyun 			break;
3513*4882a593Smuzhiyun 		if (!port_is_power_on(hub, *portstatus)) {
3514*4882a593Smuzhiyun 			status = -ENODEV;
3515*4882a593Smuzhiyun 			break;
3516*4882a593Smuzhiyun 		}
3517*4882a593Smuzhiyun 		msleep(20);
3518*4882a593Smuzhiyun 		delay_ms += 20;
3519*4882a593Smuzhiyun 		status = hub_port_status(hub, *port1, portstatus, portchange);
3520*4882a593Smuzhiyun 	}
3521*4882a593Smuzhiyun 	dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
3522*4882a593Smuzhiyun 	return status;
3523*4882a593Smuzhiyun }
3524*4882a593Smuzhiyun 
3525*4882a593Smuzhiyun /*
3526*4882a593Smuzhiyun  * usb_port_resume - re-activate a suspended usb device's upstream port
3527*4882a593Smuzhiyun  * @udev: device to re-activate, not a root hub
3528*4882a593Smuzhiyun  * Context: must be able to sleep; device not locked; pm locks held
3529*4882a593Smuzhiyun  *
3530*4882a593Smuzhiyun  * This will re-activate the suspended device, increasing power usage
3531*4882a593Smuzhiyun  * while letting drivers communicate again with its endpoints.
3532*4882a593Smuzhiyun  * USB resume explicitly guarantees that the power session between
3533*4882a593Smuzhiyun  * the host and the device is the same as it was when the device
3534*4882a593Smuzhiyun  * suspended.
3535*4882a593Smuzhiyun  *
3536*4882a593Smuzhiyun  * If @udev->reset_resume is set then this routine won't check that the
3537*4882a593Smuzhiyun  * port is still enabled.  Furthermore, finish_port_resume() above will
3538*4882a593Smuzhiyun  * reset @udev.  The end result is that a broken power session can be
3539*4882a593Smuzhiyun  * recovered and @udev will appear to persist across a loss of VBUS power.
3540*4882a593Smuzhiyun  *
3541*4882a593Smuzhiyun  * For example, if a host controller doesn't maintain VBUS suspend current
3542*4882a593Smuzhiyun  * during a system sleep or is reset when the system wakes up, all the USB
3543*4882a593Smuzhiyun  * power sessions below it will be broken.  This is especially troublesome
3544*4882a593Smuzhiyun  * for mass-storage devices containing mounted filesystems, since the
3545*4882a593Smuzhiyun  * device will appear to have disconnected and all the memory mappings
3546*4882a593Smuzhiyun  * to it will be lost.  Using the USB_PERSIST facility, the device can be
3547*4882a593Smuzhiyun  * made to appear as if it had not disconnected.
3548*4882a593Smuzhiyun  *
3549*4882a593Smuzhiyun  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
3550*4882a593Smuzhiyun  * every effort to insure that the same device is present after the
3551*4882a593Smuzhiyun  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
3552*4882a593Smuzhiyun  * quite possible for a device to remain unaltered but its media to be
3553*4882a593Smuzhiyun  * changed.  If the user replaces a flash memory card while the system is
3554*4882a593Smuzhiyun  * asleep, he will have only himself to blame when the filesystem on the
3555*4882a593Smuzhiyun  * new card is corrupted and the system crashes.
3556*4882a593Smuzhiyun  *
3557*4882a593Smuzhiyun  * Returns 0 on success, else negative errno.
3558*4882a593Smuzhiyun  */
usb_port_resume(struct usb_device * udev,pm_message_t msg)3559*4882a593Smuzhiyun int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3560*4882a593Smuzhiyun {
3561*4882a593Smuzhiyun 	struct usb_hub	*hub = usb_hub_to_struct_hub(udev->parent);
3562*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[udev->portnum  - 1];
3563*4882a593Smuzhiyun 	int		port1 = udev->portnum;
3564*4882a593Smuzhiyun 	int		status;
3565*4882a593Smuzhiyun 	u16		portchange, portstatus;
3566*4882a593Smuzhiyun 
3567*4882a593Smuzhiyun 	if (!test_and_set_bit(port1, hub->child_usage_bits)) {
3568*4882a593Smuzhiyun 		status = pm_runtime_resume_and_get(&port_dev->dev);
3569*4882a593Smuzhiyun 		if (status < 0) {
3570*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3571*4882a593Smuzhiyun 					status);
3572*4882a593Smuzhiyun 			return status;
3573*4882a593Smuzhiyun 		}
3574*4882a593Smuzhiyun 	}
3575*4882a593Smuzhiyun 
3576*4882a593Smuzhiyun 	usb_lock_port(port_dev);
3577*4882a593Smuzhiyun 
3578*4882a593Smuzhiyun 	/* Skip the initial Clear-Suspend step for a remote wakeup */
3579*4882a593Smuzhiyun 	status = hub_port_status(hub, port1, &portstatus, &portchange);
3580*4882a593Smuzhiyun 	if (status == 0 && !port_is_suspended(hub, portstatus)) {
3581*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_SUSPEND)
3582*4882a593Smuzhiyun 			pm_wakeup_event(&udev->dev, 0);
3583*4882a593Smuzhiyun 		goto SuspendCleared;
3584*4882a593Smuzhiyun 	}
3585*4882a593Smuzhiyun 
3586*4882a593Smuzhiyun 	/* see 7.1.7.7; affects power usage, but not budgeting */
3587*4882a593Smuzhiyun 	if (hub_is_superspeed(hub->hdev))
3588*4882a593Smuzhiyun 		status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
3589*4882a593Smuzhiyun 	else
3590*4882a593Smuzhiyun 		status = usb_clear_port_feature(hub->hdev,
3591*4882a593Smuzhiyun 				port1, USB_PORT_FEAT_SUSPEND);
3592*4882a593Smuzhiyun 	if (status) {
3593*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
3594*4882a593Smuzhiyun 	} else {
3595*4882a593Smuzhiyun 		/* drive resume for USB_RESUME_TIMEOUT msec */
3596*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "usb %sresume\n",
3597*4882a593Smuzhiyun 				(PMSG_IS_AUTO(msg) ? "auto-" : ""));
3598*4882a593Smuzhiyun 		msleep(USB_RESUME_TIMEOUT);
3599*4882a593Smuzhiyun 
3600*4882a593Smuzhiyun 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
3601*4882a593Smuzhiyun 		 * stop resume signaling.  Then finish the resume
3602*4882a593Smuzhiyun 		 * sequence.
3603*4882a593Smuzhiyun 		 */
3604*4882a593Smuzhiyun 		status = hub_port_status(hub, port1, &portstatus, &portchange);
3605*4882a593Smuzhiyun 	}
3606*4882a593Smuzhiyun 
3607*4882a593Smuzhiyun  SuspendCleared:
3608*4882a593Smuzhiyun 	if (status == 0) {
3609*4882a593Smuzhiyun 		udev->port_is_suspended = 0;
3610*4882a593Smuzhiyun 		if (hub_is_superspeed(hub->hdev)) {
3611*4882a593Smuzhiyun 			if (portchange & USB_PORT_STAT_C_LINK_STATE)
3612*4882a593Smuzhiyun 				usb_clear_port_feature(hub->hdev, port1,
3613*4882a593Smuzhiyun 					USB_PORT_FEAT_C_PORT_LINK_STATE);
3614*4882a593Smuzhiyun 		} else {
3615*4882a593Smuzhiyun 			if (portchange & USB_PORT_STAT_C_SUSPEND)
3616*4882a593Smuzhiyun 				usb_clear_port_feature(hub->hdev, port1,
3617*4882a593Smuzhiyun 						USB_PORT_FEAT_C_SUSPEND);
3618*4882a593Smuzhiyun 		}
3619*4882a593Smuzhiyun 
3620*4882a593Smuzhiyun 		/* TRSMRCY = 10 msec */
3621*4882a593Smuzhiyun 		msleep(10);
3622*4882a593Smuzhiyun 	}
3623*4882a593Smuzhiyun 
3624*4882a593Smuzhiyun 	if (udev->persist_enabled)
3625*4882a593Smuzhiyun 		status = wait_for_connected(udev, hub, &port1, &portchange,
3626*4882a593Smuzhiyun 				&portstatus);
3627*4882a593Smuzhiyun 
3628*4882a593Smuzhiyun 	status = check_port_resume_type(udev,
3629*4882a593Smuzhiyun 			hub, port1, status, portchange, portstatus);
3630*4882a593Smuzhiyun 	if (status == 0)
3631*4882a593Smuzhiyun 		status = finish_port_resume(udev);
3632*4882a593Smuzhiyun 	if (status < 0) {
3633*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3634*4882a593Smuzhiyun 		hub_port_logical_disconnect(hub, port1);
3635*4882a593Smuzhiyun 	} else  {
3636*4882a593Smuzhiyun 		/* Try to enable USB2 hardware LPM */
3637*4882a593Smuzhiyun 		usb_enable_usb2_hardware_lpm(udev);
3638*4882a593Smuzhiyun 
3639*4882a593Smuzhiyun 		/* Try to enable USB3 LTM */
3640*4882a593Smuzhiyun 		usb_enable_ltm(udev);
3641*4882a593Smuzhiyun 	}
3642*4882a593Smuzhiyun 
3643*4882a593Smuzhiyun 	usb_unlock_port(port_dev);
3644*4882a593Smuzhiyun 
3645*4882a593Smuzhiyun 	return status;
3646*4882a593Smuzhiyun }
3647*4882a593Smuzhiyun 
usb_remote_wakeup(struct usb_device * udev)3648*4882a593Smuzhiyun int usb_remote_wakeup(struct usb_device *udev)
3649*4882a593Smuzhiyun {
3650*4882a593Smuzhiyun 	int	status = 0;
3651*4882a593Smuzhiyun 
3652*4882a593Smuzhiyun 	usb_lock_device(udev);
3653*4882a593Smuzhiyun 	if (udev->state == USB_STATE_SUSPENDED) {
3654*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
3655*4882a593Smuzhiyun 		status = usb_autoresume_device(udev);
3656*4882a593Smuzhiyun 		if (status == 0) {
3657*4882a593Smuzhiyun 			/* Let the drivers do their thing, then... */
3658*4882a593Smuzhiyun 			usb_autosuspend_device(udev);
3659*4882a593Smuzhiyun 		}
3660*4882a593Smuzhiyun 	}
3661*4882a593Smuzhiyun 	usb_unlock_device(udev);
3662*4882a593Smuzhiyun 	return status;
3663*4882a593Smuzhiyun }
3664*4882a593Smuzhiyun 
3665*4882a593Smuzhiyun /* Returns 1 if there was a remote wakeup and a connect status change. */
hub_handle_remote_wakeup(struct usb_hub * hub,unsigned int port,u16 portstatus,u16 portchange)3666*4882a593Smuzhiyun static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3667*4882a593Smuzhiyun 		u16 portstatus, u16 portchange)
3668*4882a593Smuzhiyun 		__must_hold(&port_dev->status_lock)
3669*4882a593Smuzhiyun {
3670*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port - 1];
3671*4882a593Smuzhiyun 	struct usb_device *hdev;
3672*4882a593Smuzhiyun 	struct usb_device *udev;
3673*4882a593Smuzhiyun 	int connect_change = 0;
3674*4882a593Smuzhiyun 	u16 link_state;
3675*4882a593Smuzhiyun 	int ret;
3676*4882a593Smuzhiyun 
3677*4882a593Smuzhiyun 	hdev = hub->hdev;
3678*4882a593Smuzhiyun 	udev = port_dev->child;
3679*4882a593Smuzhiyun 	if (!hub_is_superspeed(hdev)) {
3680*4882a593Smuzhiyun 		if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3681*4882a593Smuzhiyun 			return 0;
3682*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3683*4882a593Smuzhiyun 	} else {
3684*4882a593Smuzhiyun 		link_state = portstatus & USB_PORT_STAT_LINK_STATE;
3685*4882a593Smuzhiyun 		if (!udev || udev->state != USB_STATE_SUSPENDED ||
3686*4882a593Smuzhiyun 				(link_state != USB_SS_PORT_LS_U0 &&
3687*4882a593Smuzhiyun 				 link_state != USB_SS_PORT_LS_U1 &&
3688*4882a593Smuzhiyun 				 link_state != USB_SS_PORT_LS_U2))
3689*4882a593Smuzhiyun 			return 0;
3690*4882a593Smuzhiyun 	}
3691*4882a593Smuzhiyun 
3692*4882a593Smuzhiyun 	if (udev) {
3693*4882a593Smuzhiyun 		/* TRSMRCY = 10 msec */
3694*4882a593Smuzhiyun 		msleep(10);
3695*4882a593Smuzhiyun 
3696*4882a593Smuzhiyun 		usb_unlock_port(port_dev);
3697*4882a593Smuzhiyun 		ret = usb_remote_wakeup(udev);
3698*4882a593Smuzhiyun 		usb_lock_port(port_dev);
3699*4882a593Smuzhiyun 		if (ret < 0)
3700*4882a593Smuzhiyun 			connect_change = 1;
3701*4882a593Smuzhiyun 	} else {
3702*4882a593Smuzhiyun 		ret = -ENODEV;
3703*4882a593Smuzhiyun 		hub_port_disable(hub, port, 1);
3704*4882a593Smuzhiyun 	}
3705*4882a593Smuzhiyun 	dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3706*4882a593Smuzhiyun 	return connect_change;
3707*4882a593Smuzhiyun }
3708*4882a593Smuzhiyun 
check_ports_changed(struct usb_hub * hub)3709*4882a593Smuzhiyun static int check_ports_changed(struct usb_hub *hub)
3710*4882a593Smuzhiyun {
3711*4882a593Smuzhiyun 	int port1;
3712*4882a593Smuzhiyun 
3713*4882a593Smuzhiyun 	for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3714*4882a593Smuzhiyun 		u16 portstatus, portchange;
3715*4882a593Smuzhiyun 		int status;
3716*4882a593Smuzhiyun 
3717*4882a593Smuzhiyun 		status = hub_port_status(hub, port1, &portstatus, &portchange);
3718*4882a593Smuzhiyun 		if (!status && portchange)
3719*4882a593Smuzhiyun 			return 1;
3720*4882a593Smuzhiyun 	}
3721*4882a593Smuzhiyun 	return 0;
3722*4882a593Smuzhiyun }
3723*4882a593Smuzhiyun 
hub_suspend(struct usb_interface * intf,pm_message_t msg)3724*4882a593Smuzhiyun static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
3725*4882a593Smuzhiyun {
3726*4882a593Smuzhiyun 	struct usb_hub		*hub = usb_get_intfdata(intf);
3727*4882a593Smuzhiyun 	struct usb_device	*hdev = hub->hdev;
3728*4882a593Smuzhiyun 	unsigned		port1;
3729*4882a593Smuzhiyun 
3730*4882a593Smuzhiyun 	/*
3731*4882a593Smuzhiyun 	 * Warn if children aren't already suspended.
3732*4882a593Smuzhiyun 	 * Also, add up the number of wakeup-enabled descendants.
3733*4882a593Smuzhiyun 	 */
3734*4882a593Smuzhiyun 	hub->wakeup_enabled_descendants = 0;
3735*4882a593Smuzhiyun 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3736*4882a593Smuzhiyun 		struct usb_port *port_dev = hub->ports[port1 - 1];
3737*4882a593Smuzhiyun 		struct usb_device *udev = port_dev->child;
3738*4882a593Smuzhiyun 
3739*4882a593Smuzhiyun 		if (udev && udev->can_submit) {
3740*4882a593Smuzhiyun 			dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3741*4882a593Smuzhiyun 					dev_name(&udev->dev));
3742*4882a593Smuzhiyun 			if (PMSG_IS_AUTO(msg))
3743*4882a593Smuzhiyun 				return -EBUSY;
3744*4882a593Smuzhiyun 		}
3745*4882a593Smuzhiyun 		if (udev)
3746*4882a593Smuzhiyun 			hub->wakeup_enabled_descendants +=
3747*4882a593Smuzhiyun 					usb_wakeup_enabled_descendants(udev);
3748*4882a593Smuzhiyun 	}
3749*4882a593Smuzhiyun 
3750*4882a593Smuzhiyun 	if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3751*4882a593Smuzhiyun 		/* check if there are changes pending on hub ports */
3752*4882a593Smuzhiyun 		if (check_ports_changed(hub)) {
3753*4882a593Smuzhiyun 			if (PMSG_IS_AUTO(msg))
3754*4882a593Smuzhiyun 				return -EBUSY;
3755*4882a593Smuzhiyun 			pm_wakeup_event(&hdev->dev, 2000);
3756*4882a593Smuzhiyun 		}
3757*4882a593Smuzhiyun 	}
3758*4882a593Smuzhiyun 
3759*4882a593Smuzhiyun 	if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3760*4882a593Smuzhiyun 		/* Enable hub to send remote wakeup for all ports. */
3761*4882a593Smuzhiyun 		for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3762*4882a593Smuzhiyun 			set_port_feature(hdev,
3763*4882a593Smuzhiyun 					 port1 |
3764*4882a593Smuzhiyun 					 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3765*4882a593Smuzhiyun 					 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3766*4882a593Smuzhiyun 					 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3767*4882a593Smuzhiyun 					 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3768*4882a593Smuzhiyun 		}
3769*4882a593Smuzhiyun 	}
3770*4882a593Smuzhiyun 
3771*4882a593Smuzhiyun 	dev_dbg(&intf->dev, "%s\n", __func__);
3772*4882a593Smuzhiyun 
3773*4882a593Smuzhiyun 	/* stop hub_wq and related activity */
3774*4882a593Smuzhiyun 	hub_quiesce(hub, HUB_SUSPEND);
3775*4882a593Smuzhiyun 	return 0;
3776*4882a593Smuzhiyun }
3777*4882a593Smuzhiyun 
3778*4882a593Smuzhiyun /* Report wakeup requests from the ports of a resuming root hub */
report_wakeup_requests(struct usb_hub * hub)3779*4882a593Smuzhiyun static void report_wakeup_requests(struct usb_hub *hub)
3780*4882a593Smuzhiyun {
3781*4882a593Smuzhiyun 	struct usb_device	*hdev = hub->hdev;
3782*4882a593Smuzhiyun 	struct usb_device	*udev;
3783*4882a593Smuzhiyun 	struct usb_hcd		*hcd;
3784*4882a593Smuzhiyun 	unsigned long		resuming_ports;
3785*4882a593Smuzhiyun 	int			i;
3786*4882a593Smuzhiyun 
3787*4882a593Smuzhiyun 	if (hdev->parent)
3788*4882a593Smuzhiyun 		return;		/* Not a root hub */
3789*4882a593Smuzhiyun 
3790*4882a593Smuzhiyun 	hcd = bus_to_hcd(hdev->bus);
3791*4882a593Smuzhiyun 	if (hcd->driver->get_resuming_ports) {
3792*4882a593Smuzhiyun 
3793*4882a593Smuzhiyun 		/*
3794*4882a593Smuzhiyun 		 * The get_resuming_ports() method returns a bitmap (origin 0)
3795*4882a593Smuzhiyun 		 * of ports which have started wakeup signaling but have not
3796*4882a593Smuzhiyun 		 * yet finished resuming.  During system resume we will
3797*4882a593Smuzhiyun 		 * resume all the enabled ports, regardless of any wakeup
3798*4882a593Smuzhiyun 		 * signals, which means the wakeup requests would be lost.
3799*4882a593Smuzhiyun 		 * To prevent this, report them to the PM core here.
3800*4882a593Smuzhiyun 		 */
3801*4882a593Smuzhiyun 		resuming_ports = hcd->driver->get_resuming_ports(hcd);
3802*4882a593Smuzhiyun 		for (i = 0; i < hdev->maxchild; ++i) {
3803*4882a593Smuzhiyun 			if (test_bit(i, &resuming_ports)) {
3804*4882a593Smuzhiyun 				udev = hub->ports[i]->child;
3805*4882a593Smuzhiyun 				if (udev)
3806*4882a593Smuzhiyun 					pm_wakeup_event(&udev->dev, 0);
3807*4882a593Smuzhiyun 			}
3808*4882a593Smuzhiyun 		}
3809*4882a593Smuzhiyun 	}
3810*4882a593Smuzhiyun }
3811*4882a593Smuzhiyun 
hub_resume(struct usb_interface * intf)3812*4882a593Smuzhiyun static int hub_resume(struct usb_interface *intf)
3813*4882a593Smuzhiyun {
3814*4882a593Smuzhiyun 	struct usb_hub *hub = usb_get_intfdata(intf);
3815*4882a593Smuzhiyun 
3816*4882a593Smuzhiyun 	dev_dbg(&intf->dev, "%s\n", __func__);
3817*4882a593Smuzhiyun 	hub_activate(hub, HUB_RESUME);
3818*4882a593Smuzhiyun 
3819*4882a593Smuzhiyun 	/*
3820*4882a593Smuzhiyun 	 * This should be called only for system resume, not runtime resume.
3821*4882a593Smuzhiyun 	 * We can't tell the difference here, so some wakeup requests will be
3822*4882a593Smuzhiyun 	 * reported at the wrong time or more than once.  This shouldn't
3823*4882a593Smuzhiyun 	 * matter much, so long as they do get reported.
3824*4882a593Smuzhiyun 	 */
3825*4882a593Smuzhiyun 	report_wakeup_requests(hub);
3826*4882a593Smuzhiyun 	return 0;
3827*4882a593Smuzhiyun }
3828*4882a593Smuzhiyun 
hub_reset_resume(struct usb_interface * intf)3829*4882a593Smuzhiyun static int hub_reset_resume(struct usb_interface *intf)
3830*4882a593Smuzhiyun {
3831*4882a593Smuzhiyun 	struct usb_hub *hub = usb_get_intfdata(intf);
3832*4882a593Smuzhiyun 
3833*4882a593Smuzhiyun 	dev_dbg(&intf->dev, "%s\n", __func__);
3834*4882a593Smuzhiyun 	hub_activate(hub, HUB_RESET_RESUME);
3835*4882a593Smuzhiyun 	return 0;
3836*4882a593Smuzhiyun }
3837*4882a593Smuzhiyun 
3838*4882a593Smuzhiyun /**
3839*4882a593Smuzhiyun  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3840*4882a593Smuzhiyun  * @rhdev: struct usb_device for the root hub
3841*4882a593Smuzhiyun  *
3842*4882a593Smuzhiyun  * The USB host controller driver calls this function when its root hub
3843*4882a593Smuzhiyun  * is resumed and Vbus power has been interrupted or the controller
3844*4882a593Smuzhiyun  * has been reset.  The routine marks @rhdev as having lost power.
3845*4882a593Smuzhiyun  * When the hub driver is resumed it will take notice and carry out
3846*4882a593Smuzhiyun  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3847*4882a593Smuzhiyun  * the others will be disconnected.
3848*4882a593Smuzhiyun  */
usb_root_hub_lost_power(struct usb_device * rhdev)3849*4882a593Smuzhiyun void usb_root_hub_lost_power(struct usb_device *rhdev)
3850*4882a593Smuzhiyun {
3851*4882a593Smuzhiyun 	dev_notice(&rhdev->dev, "root hub lost power or was reset\n");
3852*4882a593Smuzhiyun 	rhdev->reset_resume = 1;
3853*4882a593Smuzhiyun }
3854*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3855*4882a593Smuzhiyun 
3856*4882a593Smuzhiyun static const char * const usb3_lpm_names[]  = {
3857*4882a593Smuzhiyun 	"U0",
3858*4882a593Smuzhiyun 	"U1",
3859*4882a593Smuzhiyun 	"U2",
3860*4882a593Smuzhiyun 	"U3",
3861*4882a593Smuzhiyun };
3862*4882a593Smuzhiyun 
3863*4882a593Smuzhiyun /*
3864*4882a593Smuzhiyun  * Send a Set SEL control transfer to the device, prior to enabling
3865*4882a593Smuzhiyun  * device-initiated U1 or U2.  This lets the device know the exit latencies from
3866*4882a593Smuzhiyun  * the time the device initiates a U1 or U2 exit, to the time it will receive a
3867*4882a593Smuzhiyun  * packet from the host.
3868*4882a593Smuzhiyun  *
3869*4882a593Smuzhiyun  * This function will fail if the SEL or PEL values for udev are greater than
3870*4882a593Smuzhiyun  * the maximum allowed values for the link state to be enabled.
3871*4882a593Smuzhiyun  */
usb_req_set_sel(struct usb_device * udev,enum usb3_link_state state)3872*4882a593Smuzhiyun static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3873*4882a593Smuzhiyun {
3874*4882a593Smuzhiyun 	struct usb_set_sel_req *sel_values;
3875*4882a593Smuzhiyun 	unsigned long long u1_sel;
3876*4882a593Smuzhiyun 	unsigned long long u1_pel;
3877*4882a593Smuzhiyun 	unsigned long long u2_sel;
3878*4882a593Smuzhiyun 	unsigned long long u2_pel;
3879*4882a593Smuzhiyun 	int ret;
3880*4882a593Smuzhiyun 
3881*4882a593Smuzhiyun 	if (udev->state != USB_STATE_CONFIGURED)
3882*4882a593Smuzhiyun 		return 0;
3883*4882a593Smuzhiyun 
3884*4882a593Smuzhiyun 	/* Convert SEL and PEL stored in ns to us */
3885*4882a593Smuzhiyun 	u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3886*4882a593Smuzhiyun 	u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3887*4882a593Smuzhiyun 	u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3888*4882a593Smuzhiyun 	u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3889*4882a593Smuzhiyun 
3890*4882a593Smuzhiyun 	/*
3891*4882a593Smuzhiyun 	 * Make sure that the calculated SEL and PEL values for the link
3892*4882a593Smuzhiyun 	 * state we're enabling aren't bigger than the max SEL/PEL
3893*4882a593Smuzhiyun 	 * value that will fit in the SET SEL control transfer.
3894*4882a593Smuzhiyun 	 * Otherwise the device would get an incorrect idea of the exit
3895*4882a593Smuzhiyun 	 * latency for the link state, and could start a device-initiated
3896*4882a593Smuzhiyun 	 * U1/U2 when the exit latencies are too high.
3897*4882a593Smuzhiyun 	 */
3898*4882a593Smuzhiyun 	if ((state == USB3_LPM_U1 &&
3899*4882a593Smuzhiyun 				(u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3900*4882a593Smuzhiyun 				 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3901*4882a593Smuzhiyun 			(state == USB3_LPM_U2 &&
3902*4882a593Smuzhiyun 			 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3903*4882a593Smuzhiyun 			  u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3904*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
3905*4882a593Smuzhiyun 				usb3_lpm_names[state], u1_sel, u1_pel);
3906*4882a593Smuzhiyun 		return -EINVAL;
3907*4882a593Smuzhiyun 	}
3908*4882a593Smuzhiyun 
3909*4882a593Smuzhiyun 	/*
3910*4882a593Smuzhiyun 	 * If we're enabling device-initiated LPM for one link state,
3911*4882a593Smuzhiyun 	 * but the other link state has a too high SEL or PEL value,
3912*4882a593Smuzhiyun 	 * just set those values to the max in the Set SEL request.
3913*4882a593Smuzhiyun 	 */
3914*4882a593Smuzhiyun 	if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3915*4882a593Smuzhiyun 		u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3916*4882a593Smuzhiyun 
3917*4882a593Smuzhiyun 	if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3918*4882a593Smuzhiyun 		u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3919*4882a593Smuzhiyun 
3920*4882a593Smuzhiyun 	if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3921*4882a593Smuzhiyun 		u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3922*4882a593Smuzhiyun 
3923*4882a593Smuzhiyun 	if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3924*4882a593Smuzhiyun 		u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3925*4882a593Smuzhiyun 
3926*4882a593Smuzhiyun 	/*
3927*4882a593Smuzhiyun 	 * usb_enable_lpm() can be called as part of a failed device reset,
3928*4882a593Smuzhiyun 	 * which may be initiated by an error path of a mass storage driver.
3929*4882a593Smuzhiyun 	 * Therefore, use GFP_NOIO.
3930*4882a593Smuzhiyun 	 */
3931*4882a593Smuzhiyun 	sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3932*4882a593Smuzhiyun 	if (!sel_values)
3933*4882a593Smuzhiyun 		return -ENOMEM;
3934*4882a593Smuzhiyun 
3935*4882a593Smuzhiyun 	sel_values->u1_sel = u1_sel;
3936*4882a593Smuzhiyun 	sel_values->u1_pel = u1_pel;
3937*4882a593Smuzhiyun 	sel_values->u2_sel = cpu_to_le16(u2_sel);
3938*4882a593Smuzhiyun 	sel_values->u2_pel = cpu_to_le16(u2_pel);
3939*4882a593Smuzhiyun 
3940*4882a593Smuzhiyun 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3941*4882a593Smuzhiyun 			USB_REQ_SET_SEL,
3942*4882a593Smuzhiyun 			USB_RECIP_DEVICE,
3943*4882a593Smuzhiyun 			0, 0,
3944*4882a593Smuzhiyun 			sel_values, sizeof *(sel_values),
3945*4882a593Smuzhiyun 			USB_CTRL_SET_TIMEOUT);
3946*4882a593Smuzhiyun 	kfree(sel_values);
3947*4882a593Smuzhiyun 	return ret;
3948*4882a593Smuzhiyun }
3949*4882a593Smuzhiyun 
3950*4882a593Smuzhiyun /*
3951*4882a593Smuzhiyun  * Enable or disable device-initiated U1 or U2 transitions.
3952*4882a593Smuzhiyun  */
usb_set_device_initiated_lpm(struct usb_device * udev,enum usb3_link_state state,bool enable)3953*4882a593Smuzhiyun static int usb_set_device_initiated_lpm(struct usb_device *udev,
3954*4882a593Smuzhiyun 		enum usb3_link_state state, bool enable)
3955*4882a593Smuzhiyun {
3956*4882a593Smuzhiyun 	int ret;
3957*4882a593Smuzhiyun 	int feature;
3958*4882a593Smuzhiyun 
3959*4882a593Smuzhiyun 	switch (state) {
3960*4882a593Smuzhiyun 	case USB3_LPM_U1:
3961*4882a593Smuzhiyun 		feature = USB_DEVICE_U1_ENABLE;
3962*4882a593Smuzhiyun 		break;
3963*4882a593Smuzhiyun 	case USB3_LPM_U2:
3964*4882a593Smuzhiyun 		feature = USB_DEVICE_U2_ENABLE;
3965*4882a593Smuzhiyun 		break;
3966*4882a593Smuzhiyun 	default:
3967*4882a593Smuzhiyun 		dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3968*4882a593Smuzhiyun 				__func__, enable ? "enable" : "disable");
3969*4882a593Smuzhiyun 		return -EINVAL;
3970*4882a593Smuzhiyun 	}
3971*4882a593Smuzhiyun 
3972*4882a593Smuzhiyun 	if (udev->state != USB_STATE_CONFIGURED) {
3973*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "%s: Can't %s %s state "
3974*4882a593Smuzhiyun 				"for unconfigured device.\n",
3975*4882a593Smuzhiyun 				__func__, enable ? "enable" : "disable",
3976*4882a593Smuzhiyun 				usb3_lpm_names[state]);
3977*4882a593Smuzhiyun 		return 0;
3978*4882a593Smuzhiyun 	}
3979*4882a593Smuzhiyun 
3980*4882a593Smuzhiyun 	if (enable) {
3981*4882a593Smuzhiyun 		/*
3982*4882a593Smuzhiyun 		 * Now send the control transfer to enable device-initiated LPM
3983*4882a593Smuzhiyun 		 * for either U1 or U2.
3984*4882a593Smuzhiyun 		 */
3985*4882a593Smuzhiyun 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3986*4882a593Smuzhiyun 				USB_REQ_SET_FEATURE,
3987*4882a593Smuzhiyun 				USB_RECIP_DEVICE,
3988*4882a593Smuzhiyun 				feature,
3989*4882a593Smuzhiyun 				0, NULL, 0,
3990*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
3991*4882a593Smuzhiyun 	} else {
3992*4882a593Smuzhiyun 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3993*4882a593Smuzhiyun 				USB_REQ_CLEAR_FEATURE,
3994*4882a593Smuzhiyun 				USB_RECIP_DEVICE,
3995*4882a593Smuzhiyun 				feature,
3996*4882a593Smuzhiyun 				0, NULL, 0,
3997*4882a593Smuzhiyun 				USB_CTRL_SET_TIMEOUT);
3998*4882a593Smuzhiyun 	}
3999*4882a593Smuzhiyun 	if (ret < 0) {
4000*4882a593Smuzhiyun 		dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
4001*4882a593Smuzhiyun 				enable ? "Enable" : "Disable",
4002*4882a593Smuzhiyun 				usb3_lpm_names[state]);
4003*4882a593Smuzhiyun 		return -EBUSY;
4004*4882a593Smuzhiyun 	}
4005*4882a593Smuzhiyun 	return 0;
4006*4882a593Smuzhiyun }
4007*4882a593Smuzhiyun 
usb_set_lpm_timeout(struct usb_device * udev,enum usb3_link_state state,int timeout)4008*4882a593Smuzhiyun static int usb_set_lpm_timeout(struct usb_device *udev,
4009*4882a593Smuzhiyun 		enum usb3_link_state state, int timeout)
4010*4882a593Smuzhiyun {
4011*4882a593Smuzhiyun 	int ret;
4012*4882a593Smuzhiyun 	int feature;
4013*4882a593Smuzhiyun 
4014*4882a593Smuzhiyun 	switch (state) {
4015*4882a593Smuzhiyun 	case USB3_LPM_U1:
4016*4882a593Smuzhiyun 		feature = USB_PORT_FEAT_U1_TIMEOUT;
4017*4882a593Smuzhiyun 		break;
4018*4882a593Smuzhiyun 	case USB3_LPM_U2:
4019*4882a593Smuzhiyun 		feature = USB_PORT_FEAT_U2_TIMEOUT;
4020*4882a593Smuzhiyun 		break;
4021*4882a593Smuzhiyun 	default:
4022*4882a593Smuzhiyun 		dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
4023*4882a593Smuzhiyun 				__func__);
4024*4882a593Smuzhiyun 		return -EINVAL;
4025*4882a593Smuzhiyun 	}
4026*4882a593Smuzhiyun 
4027*4882a593Smuzhiyun 	if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
4028*4882a593Smuzhiyun 			timeout != USB3_LPM_DEVICE_INITIATED) {
4029*4882a593Smuzhiyun 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
4030*4882a593Smuzhiyun 				"which is a reserved value.\n",
4031*4882a593Smuzhiyun 				usb3_lpm_names[state], timeout);
4032*4882a593Smuzhiyun 		return -EINVAL;
4033*4882a593Smuzhiyun 	}
4034*4882a593Smuzhiyun 
4035*4882a593Smuzhiyun 	ret = set_port_feature(udev->parent,
4036*4882a593Smuzhiyun 			USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
4037*4882a593Smuzhiyun 			feature);
4038*4882a593Smuzhiyun 	if (ret < 0) {
4039*4882a593Smuzhiyun 		dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
4040*4882a593Smuzhiyun 				"error code %i\n", usb3_lpm_names[state],
4041*4882a593Smuzhiyun 				timeout, ret);
4042*4882a593Smuzhiyun 		return -EBUSY;
4043*4882a593Smuzhiyun 	}
4044*4882a593Smuzhiyun 	if (state == USB3_LPM_U1)
4045*4882a593Smuzhiyun 		udev->u1_params.timeout = timeout;
4046*4882a593Smuzhiyun 	else
4047*4882a593Smuzhiyun 		udev->u2_params.timeout = timeout;
4048*4882a593Smuzhiyun 	return 0;
4049*4882a593Smuzhiyun }
4050*4882a593Smuzhiyun 
4051*4882a593Smuzhiyun /*
4052*4882a593Smuzhiyun  * Don't allow device intiated U1/U2 if the system exit latency + one bus
4053*4882a593Smuzhiyun  * interval is greater than the minimum service interval of any active
4054*4882a593Smuzhiyun  * periodic endpoint. See USB 3.2 section 9.4.9
4055*4882a593Smuzhiyun  */
usb_device_may_initiate_lpm(struct usb_device * udev,enum usb3_link_state state)4056*4882a593Smuzhiyun static bool usb_device_may_initiate_lpm(struct usb_device *udev,
4057*4882a593Smuzhiyun 					enum usb3_link_state state)
4058*4882a593Smuzhiyun {
4059*4882a593Smuzhiyun 	unsigned int sel;		/* us */
4060*4882a593Smuzhiyun 	int i, j;
4061*4882a593Smuzhiyun 
4062*4882a593Smuzhiyun 	if (state == USB3_LPM_U1)
4063*4882a593Smuzhiyun 		sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4064*4882a593Smuzhiyun 	else if (state == USB3_LPM_U2)
4065*4882a593Smuzhiyun 		sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4066*4882a593Smuzhiyun 	else
4067*4882a593Smuzhiyun 		return false;
4068*4882a593Smuzhiyun 
4069*4882a593Smuzhiyun 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
4070*4882a593Smuzhiyun 		struct usb_interface *intf;
4071*4882a593Smuzhiyun 		struct usb_endpoint_descriptor *desc;
4072*4882a593Smuzhiyun 		unsigned int interval;
4073*4882a593Smuzhiyun 
4074*4882a593Smuzhiyun 		intf = udev->actconfig->interface[i];
4075*4882a593Smuzhiyun 		if (!intf)
4076*4882a593Smuzhiyun 			continue;
4077*4882a593Smuzhiyun 
4078*4882a593Smuzhiyun 		for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) {
4079*4882a593Smuzhiyun 			desc = &intf->cur_altsetting->endpoint[j].desc;
4080*4882a593Smuzhiyun 
4081*4882a593Smuzhiyun 			if (usb_endpoint_xfer_int(desc) ||
4082*4882a593Smuzhiyun 			    usb_endpoint_xfer_isoc(desc)) {
4083*4882a593Smuzhiyun 				interval = (1 << (desc->bInterval - 1)) * 125;
4084*4882a593Smuzhiyun 				if (sel + 125 > interval)
4085*4882a593Smuzhiyun 					return false;
4086*4882a593Smuzhiyun 			}
4087*4882a593Smuzhiyun 		}
4088*4882a593Smuzhiyun 	}
4089*4882a593Smuzhiyun 	return true;
4090*4882a593Smuzhiyun }
4091*4882a593Smuzhiyun 
4092*4882a593Smuzhiyun /*
4093*4882a593Smuzhiyun  * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
4094*4882a593Smuzhiyun  * U1/U2 entry.
4095*4882a593Smuzhiyun  *
4096*4882a593Smuzhiyun  * We will attempt to enable U1 or U2, but there are no guarantees that the
4097*4882a593Smuzhiyun  * control transfers to set the hub timeout or enable device-initiated U1/U2
4098*4882a593Smuzhiyun  * will be successful.
4099*4882a593Smuzhiyun  *
4100*4882a593Smuzhiyun  * If the control transfer to enable device-initiated U1/U2 entry fails, then
4101*4882a593Smuzhiyun  * hub-initiated U1/U2 will be disabled.
4102*4882a593Smuzhiyun  *
4103*4882a593Smuzhiyun  * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
4104*4882a593Smuzhiyun  * driver know about it.  If that call fails, it should be harmless, and just
4105*4882a593Smuzhiyun  * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
4106*4882a593Smuzhiyun  */
usb_enable_link_state(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)4107*4882a593Smuzhiyun static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
4108*4882a593Smuzhiyun 		enum usb3_link_state state)
4109*4882a593Smuzhiyun {
4110*4882a593Smuzhiyun 	int timeout, ret;
4111*4882a593Smuzhiyun 	__u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
4112*4882a593Smuzhiyun 	__le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
4113*4882a593Smuzhiyun 
4114*4882a593Smuzhiyun 	/* If the device says it doesn't have *any* exit latency to come out of
4115*4882a593Smuzhiyun 	 * U1 or U2, it's probably lying.  Assume it doesn't implement that link
4116*4882a593Smuzhiyun 	 * state.
4117*4882a593Smuzhiyun 	 */
4118*4882a593Smuzhiyun 	if ((state == USB3_LPM_U1 && u1_mel == 0) ||
4119*4882a593Smuzhiyun 			(state == USB3_LPM_U2 && u2_mel == 0))
4120*4882a593Smuzhiyun 		return;
4121*4882a593Smuzhiyun 
4122*4882a593Smuzhiyun 	/*
4123*4882a593Smuzhiyun 	 * First, let the device know about the exit latencies
4124*4882a593Smuzhiyun 	 * associated with the link state we're about to enable.
4125*4882a593Smuzhiyun 	 */
4126*4882a593Smuzhiyun 	ret = usb_req_set_sel(udev, state);
4127*4882a593Smuzhiyun 	if (ret < 0) {
4128*4882a593Smuzhiyun 		dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
4129*4882a593Smuzhiyun 				usb3_lpm_names[state]);
4130*4882a593Smuzhiyun 		return;
4131*4882a593Smuzhiyun 	}
4132*4882a593Smuzhiyun 
4133*4882a593Smuzhiyun 	/* We allow the host controller to set the U1/U2 timeout internally
4134*4882a593Smuzhiyun 	 * first, so that it can change its schedule to account for the
4135*4882a593Smuzhiyun 	 * additional latency to send data to a device in a lower power
4136*4882a593Smuzhiyun 	 * link state.
4137*4882a593Smuzhiyun 	 */
4138*4882a593Smuzhiyun 	timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
4139*4882a593Smuzhiyun 
4140*4882a593Smuzhiyun 	/* xHCI host controller doesn't want to enable this LPM state. */
4141*4882a593Smuzhiyun 	if (timeout == 0)
4142*4882a593Smuzhiyun 		return;
4143*4882a593Smuzhiyun 
4144*4882a593Smuzhiyun 	if (timeout < 0) {
4145*4882a593Smuzhiyun 		dev_warn(&udev->dev, "Could not enable %s link state, "
4146*4882a593Smuzhiyun 				"xHCI error %i.\n", usb3_lpm_names[state],
4147*4882a593Smuzhiyun 				timeout);
4148*4882a593Smuzhiyun 		return;
4149*4882a593Smuzhiyun 	}
4150*4882a593Smuzhiyun 
4151*4882a593Smuzhiyun 	if (usb_set_lpm_timeout(udev, state, timeout)) {
4152*4882a593Smuzhiyun 		/* If we can't set the parent hub U1/U2 timeout,
4153*4882a593Smuzhiyun 		 * device-initiated LPM won't be allowed either, so let the xHCI
4154*4882a593Smuzhiyun 		 * host know that this link state won't be enabled.
4155*4882a593Smuzhiyun 		 */
4156*4882a593Smuzhiyun 		hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
4157*4882a593Smuzhiyun 		return;
4158*4882a593Smuzhiyun 	}
4159*4882a593Smuzhiyun 
4160*4882a593Smuzhiyun 	/* Only a configured device will accept the Set Feature
4161*4882a593Smuzhiyun 	 * U1/U2_ENABLE
4162*4882a593Smuzhiyun 	 */
4163*4882a593Smuzhiyun 	if (udev->actconfig &&
4164*4882a593Smuzhiyun 	    usb_device_may_initiate_lpm(udev, state)) {
4165*4882a593Smuzhiyun 		if (usb_set_device_initiated_lpm(udev, state, true)) {
4166*4882a593Smuzhiyun 			/*
4167*4882a593Smuzhiyun 			 * Request to enable device initiated U1/U2 failed,
4168*4882a593Smuzhiyun 			 * better to turn off lpm in this case.
4169*4882a593Smuzhiyun 			 */
4170*4882a593Smuzhiyun 			usb_set_lpm_timeout(udev, state, 0);
4171*4882a593Smuzhiyun 			hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
4172*4882a593Smuzhiyun 			return;
4173*4882a593Smuzhiyun 		}
4174*4882a593Smuzhiyun 	}
4175*4882a593Smuzhiyun 
4176*4882a593Smuzhiyun 	if (state == USB3_LPM_U1)
4177*4882a593Smuzhiyun 		udev->usb3_lpm_u1_enabled = 1;
4178*4882a593Smuzhiyun 	else if (state == USB3_LPM_U2)
4179*4882a593Smuzhiyun 		udev->usb3_lpm_u2_enabled = 1;
4180*4882a593Smuzhiyun }
4181*4882a593Smuzhiyun /*
4182*4882a593Smuzhiyun  * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
4183*4882a593Smuzhiyun  * U1/U2 entry.
4184*4882a593Smuzhiyun  *
4185*4882a593Smuzhiyun  * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
4186*4882a593Smuzhiyun  * If zero is returned, the parent will not allow the link to go into U1/U2.
4187*4882a593Smuzhiyun  *
4188*4882a593Smuzhiyun  * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
4189*4882a593Smuzhiyun  * it won't have an effect on the bus link state because the parent hub will
4190*4882a593Smuzhiyun  * still disallow device-initiated U1/U2 entry.
4191*4882a593Smuzhiyun  *
4192*4882a593Smuzhiyun  * If zero is returned, the xHCI host controller may still think U1/U2 entry is
4193*4882a593Smuzhiyun  * possible.  The result will be slightly more bus bandwidth will be taken up
4194*4882a593Smuzhiyun  * (to account for U1/U2 exit latency), but it should be harmless.
4195*4882a593Smuzhiyun  */
usb_disable_link_state(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)4196*4882a593Smuzhiyun static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
4197*4882a593Smuzhiyun 		enum usb3_link_state state)
4198*4882a593Smuzhiyun {
4199*4882a593Smuzhiyun 	switch (state) {
4200*4882a593Smuzhiyun 	case USB3_LPM_U1:
4201*4882a593Smuzhiyun 	case USB3_LPM_U2:
4202*4882a593Smuzhiyun 		break;
4203*4882a593Smuzhiyun 	default:
4204*4882a593Smuzhiyun 		dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
4205*4882a593Smuzhiyun 				__func__);
4206*4882a593Smuzhiyun 		return -EINVAL;
4207*4882a593Smuzhiyun 	}
4208*4882a593Smuzhiyun 
4209*4882a593Smuzhiyun 	if (usb_set_lpm_timeout(udev, state, 0))
4210*4882a593Smuzhiyun 		return -EBUSY;
4211*4882a593Smuzhiyun 
4212*4882a593Smuzhiyun 	usb_set_device_initiated_lpm(udev, state, false);
4213*4882a593Smuzhiyun 
4214*4882a593Smuzhiyun 	if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
4215*4882a593Smuzhiyun 		dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
4216*4882a593Smuzhiyun 				"bus schedule bandwidth may be impacted.\n",
4217*4882a593Smuzhiyun 				usb3_lpm_names[state]);
4218*4882a593Smuzhiyun 
4219*4882a593Smuzhiyun 	/* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
4220*4882a593Smuzhiyun 	 * is disabled. Hub will disallows link to enter U1/U2 as well,
4221*4882a593Smuzhiyun 	 * even device is initiating LPM. Hence LPM is disabled if hub LPM
4222*4882a593Smuzhiyun 	 * timeout set to 0, no matter device-initiated LPM is disabled or
4223*4882a593Smuzhiyun 	 * not.
4224*4882a593Smuzhiyun 	 */
4225*4882a593Smuzhiyun 	if (state == USB3_LPM_U1)
4226*4882a593Smuzhiyun 		udev->usb3_lpm_u1_enabled = 0;
4227*4882a593Smuzhiyun 	else if (state == USB3_LPM_U2)
4228*4882a593Smuzhiyun 		udev->usb3_lpm_u2_enabled = 0;
4229*4882a593Smuzhiyun 
4230*4882a593Smuzhiyun 	return 0;
4231*4882a593Smuzhiyun }
4232*4882a593Smuzhiyun 
4233*4882a593Smuzhiyun /*
4234*4882a593Smuzhiyun  * Disable hub-initiated and device-initiated U1 and U2 entry.
4235*4882a593Smuzhiyun  * Caller must own the bandwidth_mutex.
4236*4882a593Smuzhiyun  *
4237*4882a593Smuzhiyun  * This will call usb_enable_lpm() on failure, which will decrement
4238*4882a593Smuzhiyun  * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
4239*4882a593Smuzhiyun  */
usb_disable_lpm(struct usb_device * udev)4240*4882a593Smuzhiyun int usb_disable_lpm(struct usb_device *udev)
4241*4882a593Smuzhiyun {
4242*4882a593Smuzhiyun 	struct usb_hcd *hcd;
4243*4882a593Smuzhiyun 
4244*4882a593Smuzhiyun 	if (!udev || !udev->parent ||
4245*4882a593Smuzhiyun 			udev->speed < USB_SPEED_SUPER ||
4246*4882a593Smuzhiyun 			!udev->lpm_capable ||
4247*4882a593Smuzhiyun 			udev->state < USB_STATE_CONFIGURED)
4248*4882a593Smuzhiyun 		return 0;
4249*4882a593Smuzhiyun 
4250*4882a593Smuzhiyun 	hcd = bus_to_hcd(udev->bus);
4251*4882a593Smuzhiyun 	if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
4252*4882a593Smuzhiyun 		return 0;
4253*4882a593Smuzhiyun 
4254*4882a593Smuzhiyun 	udev->lpm_disable_count++;
4255*4882a593Smuzhiyun 	if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
4256*4882a593Smuzhiyun 		return 0;
4257*4882a593Smuzhiyun 
4258*4882a593Smuzhiyun 	/* If LPM is enabled, attempt to disable it. */
4259*4882a593Smuzhiyun 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
4260*4882a593Smuzhiyun 		goto enable_lpm;
4261*4882a593Smuzhiyun 	if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
4262*4882a593Smuzhiyun 		goto enable_lpm;
4263*4882a593Smuzhiyun 
4264*4882a593Smuzhiyun 	return 0;
4265*4882a593Smuzhiyun 
4266*4882a593Smuzhiyun enable_lpm:
4267*4882a593Smuzhiyun 	usb_enable_lpm(udev);
4268*4882a593Smuzhiyun 	return -EBUSY;
4269*4882a593Smuzhiyun }
4270*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_disable_lpm);
4271*4882a593Smuzhiyun 
4272*4882a593Smuzhiyun /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
usb_unlocked_disable_lpm(struct usb_device * udev)4273*4882a593Smuzhiyun int usb_unlocked_disable_lpm(struct usb_device *udev)
4274*4882a593Smuzhiyun {
4275*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4276*4882a593Smuzhiyun 	int ret;
4277*4882a593Smuzhiyun 
4278*4882a593Smuzhiyun 	if (!hcd)
4279*4882a593Smuzhiyun 		return -EINVAL;
4280*4882a593Smuzhiyun 
4281*4882a593Smuzhiyun 	mutex_lock(hcd->bandwidth_mutex);
4282*4882a593Smuzhiyun 	ret = usb_disable_lpm(udev);
4283*4882a593Smuzhiyun 	mutex_unlock(hcd->bandwidth_mutex);
4284*4882a593Smuzhiyun 
4285*4882a593Smuzhiyun 	return ret;
4286*4882a593Smuzhiyun }
4287*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
4288*4882a593Smuzhiyun 
4289*4882a593Smuzhiyun /*
4290*4882a593Smuzhiyun  * Attempt to enable device-initiated and hub-initiated U1 and U2 entry.  The
4291*4882a593Smuzhiyun  * xHCI host policy may prevent U1 or U2 from being enabled.
4292*4882a593Smuzhiyun  *
4293*4882a593Smuzhiyun  * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
4294*4882a593Smuzhiyun  * until the lpm_disable_count drops to zero.  Caller must own the
4295*4882a593Smuzhiyun  * bandwidth_mutex.
4296*4882a593Smuzhiyun  */
usb_enable_lpm(struct usb_device * udev)4297*4882a593Smuzhiyun void usb_enable_lpm(struct usb_device *udev)
4298*4882a593Smuzhiyun {
4299*4882a593Smuzhiyun 	struct usb_hcd *hcd;
4300*4882a593Smuzhiyun 	struct usb_hub *hub;
4301*4882a593Smuzhiyun 	struct usb_port *port_dev;
4302*4882a593Smuzhiyun 
4303*4882a593Smuzhiyun 	if (!udev || !udev->parent ||
4304*4882a593Smuzhiyun 			udev->speed < USB_SPEED_SUPER ||
4305*4882a593Smuzhiyun 			!udev->lpm_capable ||
4306*4882a593Smuzhiyun 			udev->state < USB_STATE_CONFIGURED)
4307*4882a593Smuzhiyun 		return;
4308*4882a593Smuzhiyun 
4309*4882a593Smuzhiyun 	udev->lpm_disable_count--;
4310*4882a593Smuzhiyun 	hcd = bus_to_hcd(udev->bus);
4311*4882a593Smuzhiyun 	/* Double check that we can both enable and disable LPM.
4312*4882a593Smuzhiyun 	 * Device must be configured to accept set feature U1/U2 timeout.
4313*4882a593Smuzhiyun 	 */
4314*4882a593Smuzhiyun 	if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4315*4882a593Smuzhiyun 			!hcd->driver->disable_usb3_lpm_timeout)
4316*4882a593Smuzhiyun 		return;
4317*4882a593Smuzhiyun 
4318*4882a593Smuzhiyun 	if (udev->lpm_disable_count > 0)
4319*4882a593Smuzhiyun 		return;
4320*4882a593Smuzhiyun 
4321*4882a593Smuzhiyun 	hub = usb_hub_to_struct_hub(udev->parent);
4322*4882a593Smuzhiyun 	if (!hub)
4323*4882a593Smuzhiyun 		return;
4324*4882a593Smuzhiyun 
4325*4882a593Smuzhiyun 	port_dev = hub->ports[udev->portnum - 1];
4326*4882a593Smuzhiyun 
4327*4882a593Smuzhiyun 	if (port_dev->usb3_lpm_u1_permit)
4328*4882a593Smuzhiyun 		usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4329*4882a593Smuzhiyun 
4330*4882a593Smuzhiyun 	if (port_dev->usb3_lpm_u2_permit)
4331*4882a593Smuzhiyun 		usb_enable_link_state(hcd, udev, USB3_LPM_U2);
4332*4882a593Smuzhiyun }
4333*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_enable_lpm);
4334*4882a593Smuzhiyun 
4335*4882a593Smuzhiyun /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
usb_unlocked_enable_lpm(struct usb_device * udev)4336*4882a593Smuzhiyun void usb_unlocked_enable_lpm(struct usb_device *udev)
4337*4882a593Smuzhiyun {
4338*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4339*4882a593Smuzhiyun 
4340*4882a593Smuzhiyun 	if (!hcd)
4341*4882a593Smuzhiyun 		return;
4342*4882a593Smuzhiyun 
4343*4882a593Smuzhiyun 	mutex_lock(hcd->bandwidth_mutex);
4344*4882a593Smuzhiyun 	usb_enable_lpm(udev);
4345*4882a593Smuzhiyun 	mutex_unlock(hcd->bandwidth_mutex);
4346*4882a593Smuzhiyun }
4347*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4348*4882a593Smuzhiyun 
4349*4882a593Smuzhiyun /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
hub_usb3_port_prepare_disable(struct usb_hub * hub,struct usb_port * port_dev)4350*4882a593Smuzhiyun static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4351*4882a593Smuzhiyun 					  struct usb_port *port_dev)
4352*4882a593Smuzhiyun {
4353*4882a593Smuzhiyun 	struct usb_device *udev = port_dev->child;
4354*4882a593Smuzhiyun 	int ret;
4355*4882a593Smuzhiyun 
4356*4882a593Smuzhiyun 	if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
4357*4882a593Smuzhiyun 		ret = hub_set_port_link_state(hub, port_dev->portnum,
4358*4882a593Smuzhiyun 					      USB_SS_PORT_LS_U0);
4359*4882a593Smuzhiyun 		if (!ret) {
4360*4882a593Smuzhiyun 			msleep(USB_RESUME_TIMEOUT);
4361*4882a593Smuzhiyun 			ret = usb_disable_remote_wakeup(udev);
4362*4882a593Smuzhiyun 		}
4363*4882a593Smuzhiyun 		if (ret)
4364*4882a593Smuzhiyun 			dev_warn(&udev->dev,
4365*4882a593Smuzhiyun 				 "Port disable: can't disable remote wake\n");
4366*4882a593Smuzhiyun 		udev->do_remote_wakeup = 0;
4367*4882a593Smuzhiyun 	}
4368*4882a593Smuzhiyun }
4369*4882a593Smuzhiyun 
4370*4882a593Smuzhiyun #else	/* CONFIG_PM */
4371*4882a593Smuzhiyun 
4372*4882a593Smuzhiyun #define hub_suspend		NULL
4373*4882a593Smuzhiyun #define hub_resume		NULL
4374*4882a593Smuzhiyun #define hub_reset_resume	NULL
4375*4882a593Smuzhiyun 
hub_usb3_port_prepare_disable(struct usb_hub * hub,struct usb_port * port_dev)4376*4882a593Smuzhiyun static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4377*4882a593Smuzhiyun 						 struct usb_port *port_dev) { }
4378*4882a593Smuzhiyun 
usb_disable_lpm(struct usb_device * udev)4379*4882a593Smuzhiyun int usb_disable_lpm(struct usb_device *udev)
4380*4882a593Smuzhiyun {
4381*4882a593Smuzhiyun 	return 0;
4382*4882a593Smuzhiyun }
4383*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_disable_lpm);
4384*4882a593Smuzhiyun 
usb_enable_lpm(struct usb_device * udev)4385*4882a593Smuzhiyun void usb_enable_lpm(struct usb_device *udev) { }
4386*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_enable_lpm);
4387*4882a593Smuzhiyun 
usb_unlocked_disable_lpm(struct usb_device * udev)4388*4882a593Smuzhiyun int usb_unlocked_disable_lpm(struct usb_device *udev)
4389*4882a593Smuzhiyun {
4390*4882a593Smuzhiyun 	return 0;
4391*4882a593Smuzhiyun }
4392*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
4393*4882a593Smuzhiyun 
usb_unlocked_enable_lpm(struct usb_device * udev)4394*4882a593Smuzhiyun void usb_unlocked_enable_lpm(struct usb_device *udev) { }
4395*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4396*4882a593Smuzhiyun 
usb_disable_ltm(struct usb_device * udev)4397*4882a593Smuzhiyun int usb_disable_ltm(struct usb_device *udev)
4398*4882a593Smuzhiyun {
4399*4882a593Smuzhiyun 	return 0;
4400*4882a593Smuzhiyun }
4401*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_disable_ltm);
4402*4882a593Smuzhiyun 
usb_enable_ltm(struct usb_device * udev)4403*4882a593Smuzhiyun void usb_enable_ltm(struct usb_device *udev) { }
4404*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_enable_ltm);
4405*4882a593Smuzhiyun 
hub_handle_remote_wakeup(struct usb_hub * hub,unsigned int port,u16 portstatus,u16 portchange)4406*4882a593Smuzhiyun static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4407*4882a593Smuzhiyun 		u16 portstatus, u16 portchange)
4408*4882a593Smuzhiyun {
4409*4882a593Smuzhiyun 	return 0;
4410*4882a593Smuzhiyun }
4411*4882a593Smuzhiyun 
4412*4882a593Smuzhiyun #endif	/* CONFIG_PM */
4413*4882a593Smuzhiyun 
4414*4882a593Smuzhiyun /*
4415*4882a593Smuzhiyun  * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
4416*4882a593Smuzhiyun  * a connection with a plugged-in cable but will signal the host when the cable
4417*4882a593Smuzhiyun  * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
4418*4882a593Smuzhiyun  */
hub_port_disable(struct usb_hub * hub,int port1,int set_state)4419*4882a593Smuzhiyun static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
4420*4882a593Smuzhiyun {
4421*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
4422*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
4423*4882a593Smuzhiyun 	int ret = 0;
4424*4882a593Smuzhiyun 
4425*4882a593Smuzhiyun 	if (!hub->error) {
4426*4882a593Smuzhiyun 		if (hub_is_superspeed(hub->hdev)) {
4427*4882a593Smuzhiyun 			hub_usb3_port_prepare_disable(hub, port_dev);
4428*4882a593Smuzhiyun 			ret = hub_set_port_link_state(hub, port_dev->portnum,
4429*4882a593Smuzhiyun 						      USB_SS_PORT_LS_U3);
4430*4882a593Smuzhiyun 		} else {
4431*4882a593Smuzhiyun 			ret = usb_clear_port_feature(hdev, port1,
4432*4882a593Smuzhiyun 					USB_PORT_FEAT_ENABLE);
4433*4882a593Smuzhiyun 		}
4434*4882a593Smuzhiyun 	}
4435*4882a593Smuzhiyun 	if (port_dev->child && set_state)
4436*4882a593Smuzhiyun 		usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
4437*4882a593Smuzhiyun 	if (ret && ret != -ENODEV)
4438*4882a593Smuzhiyun 		dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
4439*4882a593Smuzhiyun 	return ret;
4440*4882a593Smuzhiyun }
4441*4882a593Smuzhiyun 
4442*4882a593Smuzhiyun /*
4443*4882a593Smuzhiyun  * usb_port_disable - disable a usb device's upstream port
4444*4882a593Smuzhiyun  * @udev: device to disable
4445*4882a593Smuzhiyun  * Context: @udev locked, must be able to sleep.
4446*4882a593Smuzhiyun  *
4447*4882a593Smuzhiyun  * Disables a USB device that isn't in active use.
4448*4882a593Smuzhiyun  */
usb_port_disable(struct usb_device * udev)4449*4882a593Smuzhiyun int usb_port_disable(struct usb_device *udev)
4450*4882a593Smuzhiyun {
4451*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4452*4882a593Smuzhiyun 
4453*4882a593Smuzhiyun 	return hub_port_disable(hub, udev->portnum, 0);
4454*4882a593Smuzhiyun }
4455*4882a593Smuzhiyun 
4456*4882a593Smuzhiyun /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4457*4882a593Smuzhiyun  *
4458*4882a593Smuzhiyun  * Between connect detection and reset signaling there must be a delay
4459*4882a593Smuzhiyun  * of 100ms at least for debounce and power-settling.  The corresponding
4460*4882a593Smuzhiyun  * timer shall restart whenever the downstream port detects a disconnect.
4461*4882a593Smuzhiyun  *
4462*4882a593Smuzhiyun  * Apparently there are some bluetooth and irda-dongles and a number of
4463*4882a593Smuzhiyun  * low-speed devices for which this debounce period may last over a second.
4464*4882a593Smuzhiyun  * Not covered by the spec - but easy to deal with.
4465*4882a593Smuzhiyun  *
4466*4882a593Smuzhiyun  * This implementation uses a 1500ms total debounce timeout; if the
4467*4882a593Smuzhiyun  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
4468*4882a593Smuzhiyun  * every 25ms for transient disconnects.  When the port status has been
4469*4882a593Smuzhiyun  * unchanged for 100ms it returns the port status.
4470*4882a593Smuzhiyun  */
hub_port_debounce(struct usb_hub * hub,int port1,bool must_be_connected)4471*4882a593Smuzhiyun int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
4472*4882a593Smuzhiyun {
4473*4882a593Smuzhiyun 	int ret;
4474*4882a593Smuzhiyun 	u16 portchange, portstatus;
4475*4882a593Smuzhiyun 	unsigned connection = 0xffff;
4476*4882a593Smuzhiyun 	int total_time, stable_time = 0;
4477*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
4478*4882a593Smuzhiyun 
4479*4882a593Smuzhiyun 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4480*4882a593Smuzhiyun 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
4481*4882a593Smuzhiyun 		if (ret < 0)
4482*4882a593Smuzhiyun 			return ret;
4483*4882a593Smuzhiyun 
4484*4882a593Smuzhiyun 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4485*4882a593Smuzhiyun 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
4486*4882a593Smuzhiyun 			if (!must_be_connected ||
4487*4882a593Smuzhiyun 			     (connection == USB_PORT_STAT_CONNECTION))
4488*4882a593Smuzhiyun 				stable_time += HUB_DEBOUNCE_STEP;
4489*4882a593Smuzhiyun 			if (stable_time >= HUB_DEBOUNCE_STABLE)
4490*4882a593Smuzhiyun 				break;
4491*4882a593Smuzhiyun 		} else {
4492*4882a593Smuzhiyun 			stable_time = 0;
4493*4882a593Smuzhiyun 			connection = portstatus & USB_PORT_STAT_CONNECTION;
4494*4882a593Smuzhiyun 		}
4495*4882a593Smuzhiyun 
4496*4882a593Smuzhiyun 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
4497*4882a593Smuzhiyun 			usb_clear_port_feature(hub->hdev, port1,
4498*4882a593Smuzhiyun 					USB_PORT_FEAT_C_CONNECTION);
4499*4882a593Smuzhiyun 		}
4500*4882a593Smuzhiyun 
4501*4882a593Smuzhiyun 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4502*4882a593Smuzhiyun 			break;
4503*4882a593Smuzhiyun 		msleep(HUB_DEBOUNCE_STEP);
4504*4882a593Smuzhiyun 	}
4505*4882a593Smuzhiyun 
4506*4882a593Smuzhiyun 	dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4507*4882a593Smuzhiyun 			total_time, stable_time, portstatus);
4508*4882a593Smuzhiyun 
4509*4882a593Smuzhiyun 	if (stable_time < HUB_DEBOUNCE_STABLE)
4510*4882a593Smuzhiyun 		return -ETIMEDOUT;
4511*4882a593Smuzhiyun 	return portstatus;
4512*4882a593Smuzhiyun }
4513*4882a593Smuzhiyun 
usb_ep0_reinit(struct usb_device * udev)4514*4882a593Smuzhiyun void usb_ep0_reinit(struct usb_device *udev)
4515*4882a593Smuzhiyun {
4516*4882a593Smuzhiyun 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4517*4882a593Smuzhiyun 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
4518*4882a593Smuzhiyun 	usb_enable_endpoint(udev, &udev->ep0, true);
4519*4882a593Smuzhiyun }
4520*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_ep0_reinit);
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
4523*4882a593Smuzhiyun #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
4524*4882a593Smuzhiyun 
hub_set_address(struct usb_device * udev,int devnum)4525*4882a593Smuzhiyun static int hub_set_address(struct usb_device *udev, int devnum)
4526*4882a593Smuzhiyun {
4527*4882a593Smuzhiyun 	int retval;
4528*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4529*4882a593Smuzhiyun 
4530*4882a593Smuzhiyun 	/*
4531*4882a593Smuzhiyun 	 * The host controller will choose the device address,
4532*4882a593Smuzhiyun 	 * instead of the core having chosen it earlier
4533*4882a593Smuzhiyun 	 */
4534*4882a593Smuzhiyun 	if (!hcd->driver->address_device && devnum <= 1)
4535*4882a593Smuzhiyun 		return -EINVAL;
4536*4882a593Smuzhiyun 	if (udev->state == USB_STATE_ADDRESS)
4537*4882a593Smuzhiyun 		return 0;
4538*4882a593Smuzhiyun 	if (udev->state != USB_STATE_DEFAULT)
4539*4882a593Smuzhiyun 		return -EINVAL;
4540*4882a593Smuzhiyun 	if (hcd->driver->address_device)
4541*4882a593Smuzhiyun 		retval = hcd->driver->address_device(hcd, udev);
4542*4882a593Smuzhiyun 	else
4543*4882a593Smuzhiyun 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4544*4882a593Smuzhiyun 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
4545*4882a593Smuzhiyun 				NULL, 0, USB_CTRL_SET_TIMEOUT);
4546*4882a593Smuzhiyun 	if (retval == 0) {
4547*4882a593Smuzhiyun 		update_devnum(udev, devnum);
4548*4882a593Smuzhiyun 		/* Device now using proper address. */
4549*4882a593Smuzhiyun 		usb_set_device_state(udev, USB_STATE_ADDRESS);
4550*4882a593Smuzhiyun 		usb_ep0_reinit(udev);
4551*4882a593Smuzhiyun 	}
4552*4882a593Smuzhiyun 	return retval;
4553*4882a593Smuzhiyun }
4554*4882a593Smuzhiyun 
4555*4882a593Smuzhiyun /*
4556*4882a593Smuzhiyun  * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4557*4882a593Smuzhiyun  * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4558*4882a593Smuzhiyun  * enabled.
4559*4882a593Smuzhiyun  *
4560*4882a593Smuzhiyun  * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4561*4882a593Smuzhiyun  * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4562*4882a593Smuzhiyun  * support bit in the BOS descriptor.
4563*4882a593Smuzhiyun  */
hub_set_initial_usb2_lpm_policy(struct usb_device * udev)4564*4882a593Smuzhiyun static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4565*4882a593Smuzhiyun {
4566*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4567*4882a593Smuzhiyun 	int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
4568*4882a593Smuzhiyun 
4569*4882a593Smuzhiyun 	if (!udev->usb2_hw_lpm_capable || !udev->bos)
4570*4882a593Smuzhiyun 		return;
4571*4882a593Smuzhiyun 
4572*4882a593Smuzhiyun 	if (hub)
4573*4882a593Smuzhiyun 		connect_type = hub->ports[udev->portnum - 1]->connect_type;
4574*4882a593Smuzhiyun 
4575*4882a593Smuzhiyun 	if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
4576*4882a593Smuzhiyun 			connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4577*4882a593Smuzhiyun 		udev->usb2_hw_lpm_allowed = 1;
4578*4882a593Smuzhiyun 		usb_enable_usb2_hardware_lpm(udev);
4579*4882a593Smuzhiyun 	}
4580*4882a593Smuzhiyun }
4581*4882a593Smuzhiyun 
hub_enable_device(struct usb_device * udev)4582*4882a593Smuzhiyun static int hub_enable_device(struct usb_device *udev)
4583*4882a593Smuzhiyun {
4584*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4585*4882a593Smuzhiyun 
4586*4882a593Smuzhiyun 	if (!hcd->driver->enable_device)
4587*4882a593Smuzhiyun 		return 0;
4588*4882a593Smuzhiyun 	if (udev->state == USB_STATE_ADDRESS)
4589*4882a593Smuzhiyun 		return 0;
4590*4882a593Smuzhiyun 	if (udev->state != USB_STATE_DEFAULT)
4591*4882a593Smuzhiyun 		return -EINVAL;
4592*4882a593Smuzhiyun 
4593*4882a593Smuzhiyun 	return hcd->driver->enable_device(hcd, udev);
4594*4882a593Smuzhiyun }
4595*4882a593Smuzhiyun 
4596*4882a593Smuzhiyun /* Reset device, (re)assign address, get device descriptor.
4597*4882a593Smuzhiyun  * Device connection must be stable, no more debouncing needed.
4598*4882a593Smuzhiyun  * Returns device in USB_STATE_ADDRESS, except on error.
4599*4882a593Smuzhiyun  *
4600*4882a593Smuzhiyun  * If this is called for an already-existing device (as part of
4601*4882a593Smuzhiyun  * usb_reset_and_verify_device), the caller must own the device lock and
4602*4882a593Smuzhiyun  * the port lock.  For a newly detected device that is not accessible
4603*4882a593Smuzhiyun  * through any global pointers, it's not necessary to lock the device,
4604*4882a593Smuzhiyun  * but it is still necessary to lock the port.
4605*4882a593Smuzhiyun  */
4606*4882a593Smuzhiyun static int
hub_port_init(struct usb_hub * hub,struct usb_device * udev,int port1,int retry_counter)4607*4882a593Smuzhiyun hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
4608*4882a593Smuzhiyun 		int retry_counter)
4609*4882a593Smuzhiyun {
4610*4882a593Smuzhiyun 	struct usb_device	*hdev = hub->hdev;
4611*4882a593Smuzhiyun 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
4612*4882a593Smuzhiyun 	struct usb_port		*port_dev = hub->ports[port1 - 1];
4613*4882a593Smuzhiyun 	int			retries, operations, retval, i;
4614*4882a593Smuzhiyun 	unsigned		delay = HUB_SHORT_RESET_TIME;
4615*4882a593Smuzhiyun 	enum usb_device_speed	oldspeed = udev->speed;
4616*4882a593Smuzhiyun 	const char		*speed;
4617*4882a593Smuzhiyun 	int			devnum = udev->devnum;
4618*4882a593Smuzhiyun 	const char		*driver_name;
4619*4882a593Smuzhiyun 	bool			do_new_scheme;
4620*4882a593Smuzhiyun 
4621*4882a593Smuzhiyun 	/* root hub ports have a slightly longer reset period
4622*4882a593Smuzhiyun 	 * (from USB 2.0 spec, section 7.1.7.5)
4623*4882a593Smuzhiyun 	 */
4624*4882a593Smuzhiyun 	if (!hdev->parent) {
4625*4882a593Smuzhiyun 		delay = HUB_ROOT_RESET_TIME;
4626*4882a593Smuzhiyun 		if (port1 == hdev->bus->otg_port)
4627*4882a593Smuzhiyun 			hdev->bus->b_hnp_enable = 0;
4628*4882a593Smuzhiyun 	}
4629*4882a593Smuzhiyun 
4630*4882a593Smuzhiyun 	/* Some low speed devices have problems with the quick delay, so */
4631*4882a593Smuzhiyun 	/*  be a bit pessimistic with those devices. RHbug #23670 */
4632*4882a593Smuzhiyun 	if (oldspeed == USB_SPEED_LOW)
4633*4882a593Smuzhiyun 		delay = HUB_LONG_RESET_TIME;
4634*4882a593Smuzhiyun 
4635*4882a593Smuzhiyun 	/* Reset the device; full speed may morph to high speed */
4636*4882a593Smuzhiyun 	/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
4637*4882a593Smuzhiyun 	retval = hub_port_reset(hub, port1, udev, delay, false);
4638*4882a593Smuzhiyun 	if (retval < 0)		/* error or disconnect */
4639*4882a593Smuzhiyun 		goto fail;
4640*4882a593Smuzhiyun 	/* success, speed is known */
4641*4882a593Smuzhiyun 
4642*4882a593Smuzhiyun 	retval = -ENODEV;
4643*4882a593Smuzhiyun 
4644*4882a593Smuzhiyun 	/* Don't allow speed changes at reset, except usb 3.0 to faster */
4645*4882a593Smuzhiyun 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
4646*4882a593Smuzhiyun 	    !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
4647*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "device reset changed speed!\n");
4648*4882a593Smuzhiyun 		goto fail;
4649*4882a593Smuzhiyun 	}
4650*4882a593Smuzhiyun 	oldspeed = udev->speed;
4651*4882a593Smuzhiyun 
4652*4882a593Smuzhiyun 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4653*4882a593Smuzhiyun 	 * it's fixed size except for full speed devices.
4654*4882a593Smuzhiyun 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
4655*4882a593Smuzhiyun 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
4656*4882a593Smuzhiyun 	 */
4657*4882a593Smuzhiyun 	switch (udev->speed) {
4658*4882a593Smuzhiyun 	case USB_SPEED_SUPER_PLUS:
4659*4882a593Smuzhiyun 	case USB_SPEED_SUPER:
4660*4882a593Smuzhiyun 	case USB_SPEED_WIRELESS:	/* fixed at 512 */
4661*4882a593Smuzhiyun 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
4662*4882a593Smuzhiyun 		break;
4663*4882a593Smuzhiyun 	case USB_SPEED_HIGH:		/* fixed at 64 */
4664*4882a593Smuzhiyun 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
4665*4882a593Smuzhiyun 		break;
4666*4882a593Smuzhiyun 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
4667*4882a593Smuzhiyun 		/* to determine the ep0 maxpacket size, try to read
4668*4882a593Smuzhiyun 		 * the device descriptor to get bMaxPacketSize0 and
4669*4882a593Smuzhiyun 		 * then correct our initial guess.
4670*4882a593Smuzhiyun 		 */
4671*4882a593Smuzhiyun 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
4672*4882a593Smuzhiyun 		break;
4673*4882a593Smuzhiyun 	case USB_SPEED_LOW:		/* fixed at 8 */
4674*4882a593Smuzhiyun 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
4675*4882a593Smuzhiyun 		break;
4676*4882a593Smuzhiyun 	default:
4677*4882a593Smuzhiyun 		goto fail;
4678*4882a593Smuzhiyun 	}
4679*4882a593Smuzhiyun 
4680*4882a593Smuzhiyun 	if (udev->speed == USB_SPEED_WIRELESS)
4681*4882a593Smuzhiyun 		speed = "variable speed Wireless";
4682*4882a593Smuzhiyun 	else
4683*4882a593Smuzhiyun 		speed = usb_speed_string(udev->speed);
4684*4882a593Smuzhiyun 
4685*4882a593Smuzhiyun 	/*
4686*4882a593Smuzhiyun 	 * The controller driver may be NULL if the controller device
4687*4882a593Smuzhiyun 	 * is the middle device between platform device and roothub.
4688*4882a593Smuzhiyun 	 * This middle device may not need a device driver due to
4689*4882a593Smuzhiyun 	 * all hardware control can be at platform device driver, this
4690*4882a593Smuzhiyun 	 * platform device is usually a dual-role USB controller device.
4691*4882a593Smuzhiyun 	 */
4692*4882a593Smuzhiyun 	if (udev->bus->controller->driver)
4693*4882a593Smuzhiyun 		driver_name = udev->bus->controller->driver->name;
4694*4882a593Smuzhiyun 	else
4695*4882a593Smuzhiyun 		driver_name = udev->bus->sysdev->driver->name;
4696*4882a593Smuzhiyun 
4697*4882a593Smuzhiyun 	if (udev->speed < USB_SPEED_SUPER)
4698*4882a593Smuzhiyun 		dev_info(&udev->dev,
4699*4882a593Smuzhiyun 				"%s %s USB device number %d using %s\n",
4700*4882a593Smuzhiyun 				(udev->config) ? "reset" : "new", speed,
4701*4882a593Smuzhiyun 				devnum, driver_name);
4702*4882a593Smuzhiyun 
4703*4882a593Smuzhiyun 	/* Set up TT records, if needed  */
4704*4882a593Smuzhiyun 	if (hdev->tt) {
4705*4882a593Smuzhiyun 		udev->tt = hdev->tt;
4706*4882a593Smuzhiyun 		udev->ttport = hdev->ttport;
4707*4882a593Smuzhiyun 	} else if (udev->speed != USB_SPEED_HIGH
4708*4882a593Smuzhiyun 			&& hdev->speed == USB_SPEED_HIGH) {
4709*4882a593Smuzhiyun 		if (!hub->tt.hub) {
4710*4882a593Smuzhiyun 			dev_err(&udev->dev, "parent hub has no TT\n");
4711*4882a593Smuzhiyun 			retval = -EINVAL;
4712*4882a593Smuzhiyun 			goto fail;
4713*4882a593Smuzhiyun 		}
4714*4882a593Smuzhiyun 		udev->tt = &hub->tt;
4715*4882a593Smuzhiyun 		udev->ttport = port1;
4716*4882a593Smuzhiyun 	}
4717*4882a593Smuzhiyun 
4718*4882a593Smuzhiyun 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4719*4882a593Smuzhiyun 	 * Because device hardware and firmware is sometimes buggy in
4720*4882a593Smuzhiyun 	 * this area, and this is how Linux has done it for ages.
4721*4882a593Smuzhiyun 	 * Change it cautiously.
4722*4882a593Smuzhiyun 	 *
4723*4882a593Smuzhiyun 	 * NOTE:  If use_new_scheme() is true we will start by issuing
4724*4882a593Smuzhiyun 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
4725*4882a593Smuzhiyun 	 * so it may help with some non-standards-compliant devices.
4726*4882a593Smuzhiyun 	 * Otherwise we start with SET_ADDRESS and then try to read the
4727*4882a593Smuzhiyun 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4728*4882a593Smuzhiyun 	 * value.
4729*4882a593Smuzhiyun 	 */
4730*4882a593Smuzhiyun 	do_new_scheme = use_new_scheme(udev, retry_counter, port_dev);
4731*4882a593Smuzhiyun 
4732*4882a593Smuzhiyun 	for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
4733*4882a593Smuzhiyun 		if (do_new_scheme) {
4734*4882a593Smuzhiyun 			struct usb_device_descriptor *buf;
4735*4882a593Smuzhiyun 			int r = 0;
4736*4882a593Smuzhiyun 
4737*4882a593Smuzhiyun 			retval = hub_enable_device(udev);
4738*4882a593Smuzhiyun 			if (retval < 0) {
4739*4882a593Smuzhiyun 				dev_err(&udev->dev,
4740*4882a593Smuzhiyun 					"hub failed to enable device, error %d\n",
4741*4882a593Smuzhiyun 					retval);
4742*4882a593Smuzhiyun 				goto fail;
4743*4882a593Smuzhiyun 			}
4744*4882a593Smuzhiyun 
4745*4882a593Smuzhiyun #define GET_DESCRIPTOR_BUFSIZE	64
4746*4882a593Smuzhiyun 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4747*4882a593Smuzhiyun 			if (!buf) {
4748*4882a593Smuzhiyun 				retval = -ENOMEM;
4749*4882a593Smuzhiyun 				continue;
4750*4882a593Smuzhiyun 			}
4751*4882a593Smuzhiyun 
4752*4882a593Smuzhiyun 			/* Retry on all errors; some devices are flakey.
4753*4882a593Smuzhiyun 			 * 255 is for WUSB devices, we actually need to use
4754*4882a593Smuzhiyun 			 * 512 (WUSB1.0[4.8.1]).
4755*4882a593Smuzhiyun 			 */
4756*4882a593Smuzhiyun 			for (operations = 0; operations < GET_MAXPACKET0_TRIES;
4757*4882a593Smuzhiyun 					++operations) {
4758*4882a593Smuzhiyun 				buf->bMaxPacketSize0 = 0;
4759*4882a593Smuzhiyun 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4760*4882a593Smuzhiyun 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4761*4882a593Smuzhiyun 					USB_DT_DEVICE << 8, 0,
4762*4882a593Smuzhiyun 					buf, GET_DESCRIPTOR_BUFSIZE,
4763*4882a593Smuzhiyun 					initial_descriptor_timeout);
4764*4882a593Smuzhiyun 				switch (buf->bMaxPacketSize0) {
4765*4882a593Smuzhiyun 				case 8: case 16: case 32: case 64: case 255:
4766*4882a593Smuzhiyun 					if (buf->bDescriptorType ==
4767*4882a593Smuzhiyun 							USB_DT_DEVICE) {
4768*4882a593Smuzhiyun 						r = 0;
4769*4882a593Smuzhiyun 						break;
4770*4882a593Smuzhiyun 					}
4771*4882a593Smuzhiyun 					fallthrough;
4772*4882a593Smuzhiyun 				default:
4773*4882a593Smuzhiyun 					if (r == 0)
4774*4882a593Smuzhiyun 						r = -EPROTO;
4775*4882a593Smuzhiyun 					break;
4776*4882a593Smuzhiyun 				}
4777*4882a593Smuzhiyun 				/*
4778*4882a593Smuzhiyun 				 * Some devices time out if they are powered on
4779*4882a593Smuzhiyun 				 * when already connected. They need a second
4780*4882a593Smuzhiyun 				 * reset. But only on the first attempt,
4781*4882a593Smuzhiyun 				 * lest we get into a time out/reset loop
4782*4882a593Smuzhiyun 				 */
4783*4882a593Smuzhiyun 				if (r == 0 || (r == -ETIMEDOUT &&
4784*4882a593Smuzhiyun 						retries == 0 &&
4785*4882a593Smuzhiyun 						udev->speed > USB_SPEED_FULL))
4786*4882a593Smuzhiyun 					break;
4787*4882a593Smuzhiyun 			}
4788*4882a593Smuzhiyun 			udev->descriptor.bMaxPacketSize0 =
4789*4882a593Smuzhiyun 					buf->bMaxPacketSize0;
4790*4882a593Smuzhiyun 			kfree(buf);
4791*4882a593Smuzhiyun 
4792*4882a593Smuzhiyun 			retval = hub_port_reset(hub, port1, udev, delay, false);
4793*4882a593Smuzhiyun 			if (retval < 0)		/* error or disconnect */
4794*4882a593Smuzhiyun 				goto fail;
4795*4882a593Smuzhiyun 			if (oldspeed != udev->speed) {
4796*4882a593Smuzhiyun 				dev_dbg(&udev->dev,
4797*4882a593Smuzhiyun 					"device reset changed speed!\n");
4798*4882a593Smuzhiyun 				retval = -ENODEV;
4799*4882a593Smuzhiyun 				goto fail;
4800*4882a593Smuzhiyun 			}
4801*4882a593Smuzhiyun 			if (r) {
4802*4882a593Smuzhiyun 				if (r != -ENODEV)
4803*4882a593Smuzhiyun 					dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4804*4882a593Smuzhiyun 							r);
4805*4882a593Smuzhiyun 				retval = -EMSGSIZE;
4806*4882a593Smuzhiyun 				continue;
4807*4882a593Smuzhiyun 			}
4808*4882a593Smuzhiyun #undef GET_DESCRIPTOR_BUFSIZE
4809*4882a593Smuzhiyun 		}
4810*4882a593Smuzhiyun 
4811*4882a593Smuzhiyun 		/*
4812*4882a593Smuzhiyun 		 * If device is WUSB, we already assigned an
4813*4882a593Smuzhiyun 		 * unauthorized address in the Connect Ack sequence;
4814*4882a593Smuzhiyun 		 * authorization will assign the final address.
4815*4882a593Smuzhiyun 		 */
4816*4882a593Smuzhiyun 		if (udev->wusb == 0) {
4817*4882a593Smuzhiyun 			for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
4818*4882a593Smuzhiyun 				retval = hub_set_address(udev, devnum);
4819*4882a593Smuzhiyun 				if (retval >= 0)
4820*4882a593Smuzhiyun 					break;
4821*4882a593Smuzhiyun 				msleep(200);
4822*4882a593Smuzhiyun 			}
4823*4882a593Smuzhiyun 			if (retval < 0) {
4824*4882a593Smuzhiyun 				if (retval != -ENODEV)
4825*4882a593Smuzhiyun 					dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4826*4882a593Smuzhiyun 							devnum, retval);
4827*4882a593Smuzhiyun 				goto fail;
4828*4882a593Smuzhiyun 			}
4829*4882a593Smuzhiyun 			if (udev->speed >= USB_SPEED_SUPER) {
4830*4882a593Smuzhiyun 				devnum = udev->devnum;
4831*4882a593Smuzhiyun 				dev_info(&udev->dev,
4832*4882a593Smuzhiyun 						"%s SuperSpeed%s%s USB device number %d using %s\n",
4833*4882a593Smuzhiyun 						(udev->config) ? "reset" : "new",
4834*4882a593Smuzhiyun 					 (udev->speed == USB_SPEED_SUPER_PLUS) ?
4835*4882a593Smuzhiyun 							"Plus Gen 2" : " Gen 1",
4836*4882a593Smuzhiyun 					 (udev->rx_lanes == 2 && udev->tx_lanes == 2) ?
4837*4882a593Smuzhiyun 							"x2" : "",
4838*4882a593Smuzhiyun 					 devnum, driver_name);
4839*4882a593Smuzhiyun 			}
4840*4882a593Smuzhiyun 
4841*4882a593Smuzhiyun 			/* cope with hardware quirkiness:
4842*4882a593Smuzhiyun 			 *  - let SET_ADDRESS settle, some device hardware wants it
4843*4882a593Smuzhiyun 			 *  - read ep0 maxpacket even for high and low speed,
4844*4882a593Smuzhiyun 			 */
4845*4882a593Smuzhiyun 			msleep(10);
4846*4882a593Smuzhiyun 			if (do_new_scheme)
4847*4882a593Smuzhiyun 				break;
4848*4882a593Smuzhiyun 		}
4849*4882a593Smuzhiyun 
4850*4882a593Smuzhiyun 		retval = usb_get_device_descriptor(udev, 8);
4851*4882a593Smuzhiyun 		if (retval < 8) {
4852*4882a593Smuzhiyun 			if (retval != -ENODEV)
4853*4882a593Smuzhiyun 				dev_err(&udev->dev,
4854*4882a593Smuzhiyun 					"device descriptor read/8, error %d\n",
4855*4882a593Smuzhiyun 					retval);
4856*4882a593Smuzhiyun 			if (retval >= 0)
4857*4882a593Smuzhiyun 				retval = -EMSGSIZE;
4858*4882a593Smuzhiyun 		} else {
4859*4882a593Smuzhiyun 			u32 delay;
4860*4882a593Smuzhiyun 
4861*4882a593Smuzhiyun 			retval = 0;
4862*4882a593Smuzhiyun 
4863*4882a593Smuzhiyun 			delay = udev->parent->hub_delay;
4864*4882a593Smuzhiyun 			udev->hub_delay = min_t(u32, delay,
4865*4882a593Smuzhiyun 						USB_TP_TRANSMISSION_DELAY_MAX);
4866*4882a593Smuzhiyun 			retval = usb_set_isoch_delay(udev);
4867*4882a593Smuzhiyun 			if (retval) {
4868*4882a593Smuzhiyun 				dev_dbg(&udev->dev,
4869*4882a593Smuzhiyun 					"Failed set isoch delay, error %d\n",
4870*4882a593Smuzhiyun 					retval);
4871*4882a593Smuzhiyun 				retval = 0;
4872*4882a593Smuzhiyun 			}
4873*4882a593Smuzhiyun 			break;
4874*4882a593Smuzhiyun 		}
4875*4882a593Smuzhiyun 	}
4876*4882a593Smuzhiyun 	if (retval)
4877*4882a593Smuzhiyun 		goto fail;
4878*4882a593Smuzhiyun 
4879*4882a593Smuzhiyun 	/*
4880*4882a593Smuzhiyun 	 * Some superspeed devices have finished the link training process
4881*4882a593Smuzhiyun 	 * and attached to a superspeed hub port, but the device descriptor
4882*4882a593Smuzhiyun 	 * got from those devices show they aren't superspeed devices. Warm
4883*4882a593Smuzhiyun 	 * reset the port attached by the devices can fix them.
4884*4882a593Smuzhiyun 	 */
4885*4882a593Smuzhiyun 	if ((udev->speed >= USB_SPEED_SUPER) &&
4886*4882a593Smuzhiyun 			(le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4887*4882a593Smuzhiyun 		dev_err(&udev->dev, "got a wrong device descriptor, "
4888*4882a593Smuzhiyun 				"warm reset device\n");
4889*4882a593Smuzhiyun 		hub_port_reset(hub, port1, udev,
4890*4882a593Smuzhiyun 				HUB_BH_RESET_TIME, true);
4891*4882a593Smuzhiyun 		retval = -EINVAL;
4892*4882a593Smuzhiyun 		goto fail;
4893*4882a593Smuzhiyun 	}
4894*4882a593Smuzhiyun 
4895*4882a593Smuzhiyun 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4896*4882a593Smuzhiyun 			udev->speed >= USB_SPEED_SUPER)
4897*4882a593Smuzhiyun 		i = 512;
4898*4882a593Smuzhiyun 	else
4899*4882a593Smuzhiyun 		i = udev->descriptor.bMaxPacketSize0;
4900*4882a593Smuzhiyun 	if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
4901*4882a593Smuzhiyun 		if (udev->speed == USB_SPEED_LOW ||
4902*4882a593Smuzhiyun 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
4903*4882a593Smuzhiyun 			dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
4904*4882a593Smuzhiyun 			retval = -EMSGSIZE;
4905*4882a593Smuzhiyun 			goto fail;
4906*4882a593Smuzhiyun 		}
4907*4882a593Smuzhiyun 		if (udev->speed == USB_SPEED_FULL)
4908*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4909*4882a593Smuzhiyun 		else
4910*4882a593Smuzhiyun 			dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
4911*4882a593Smuzhiyun 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
4912*4882a593Smuzhiyun 		usb_ep0_reinit(udev);
4913*4882a593Smuzhiyun 	}
4914*4882a593Smuzhiyun 
4915*4882a593Smuzhiyun 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4916*4882a593Smuzhiyun 	if (retval < (signed)sizeof(udev->descriptor)) {
4917*4882a593Smuzhiyun 		if (retval != -ENODEV)
4918*4882a593Smuzhiyun 			dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4919*4882a593Smuzhiyun 					retval);
4920*4882a593Smuzhiyun 		if (retval >= 0)
4921*4882a593Smuzhiyun 			retval = -ENOMSG;
4922*4882a593Smuzhiyun 		goto fail;
4923*4882a593Smuzhiyun 	}
4924*4882a593Smuzhiyun 
4925*4882a593Smuzhiyun 	usb_detect_quirks(udev);
4926*4882a593Smuzhiyun 
4927*4882a593Smuzhiyun 	if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4928*4882a593Smuzhiyun 		retval = usb_get_bos_descriptor(udev);
4929*4882a593Smuzhiyun 		if (!retval) {
4930*4882a593Smuzhiyun 			udev->lpm_capable = usb_device_supports_lpm(udev);
4931*4882a593Smuzhiyun 			usb_set_lpm_parameters(udev);
4932*4882a593Smuzhiyun 		}
4933*4882a593Smuzhiyun 	}
4934*4882a593Smuzhiyun 
4935*4882a593Smuzhiyun 	retval = 0;
4936*4882a593Smuzhiyun 	/* notify HCD that we have a device connected and addressed */
4937*4882a593Smuzhiyun 	if (hcd->driver->update_device)
4938*4882a593Smuzhiyun 		hcd->driver->update_device(hcd, udev);
4939*4882a593Smuzhiyun 	hub_set_initial_usb2_lpm_policy(udev);
4940*4882a593Smuzhiyun fail:
4941*4882a593Smuzhiyun 	if (retval) {
4942*4882a593Smuzhiyun 		hub_port_disable(hub, port1, 0);
4943*4882a593Smuzhiyun 		update_devnum(udev, devnum);	/* for disconnect processing */
4944*4882a593Smuzhiyun 	}
4945*4882a593Smuzhiyun 	return retval;
4946*4882a593Smuzhiyun }
4947*4882a593Smuzhiyun 
4948*4882a593Smuzhiyun static void
check_highspeed(struct usb_hub * hub,struct usb_device * udev,int port1)4949*4882a593Smuzhiyun check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
4950*4882a593Smuzhiyun {
4951*4882a593Smuzhiyun 	struct usb_qualifier_descriptor	*qual;
4952*4882a593Smuzhiyun 	int				status;
4953*4882a593Smuzhiyun 
4954*4882a593Smuzhiyun 	if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4955*4882a593Smuzhiyun 		return;
4956*4882a593Smuzhiyun 
4957*4882a593Smuzhiyun 	qual = kmalloc(sizeof *qual, GFP_KERNEL);
4958*4882a593Smuzhiyun 	if (qual == NULL)
4959*4882a593Smuzhiyun 		return;
4960*4882a593Smuzhiyun 
4961*4882a593Smuzhiyun 	status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
4962*4882a593Smuzhiyun 			qual, sizeof *qual);
4963*4882a593Smuzhiyun 	if (status == sizeof *qual) {
4964*4882a593Smuzhiyun 		dev_info(&udev->dev, "not running at top speed; "
4965*4882a593Smuzhiyun 			"connect to a high speed hub\n");
4966*4882a593Smuzhiyun 		/* hub LEDs are probably harder to miss than syslog */
4967*4882a593Smuzhiyun 		if (hub->has_indicators) {
4968*4882a593Smuzhiyun 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
4969*4882a593Smuzhiyun 			queue_delayed_work(system_power_efficient_wq,
4970*4882a593Smuzhiyun 					&hub->leds, 0);
4971*4882a593Smuzhiyun 		}
4972*4882a593Smuzhiyun 	}
4973*4882a593Smuzhiyun 	kfree(qual);
4974*4882a593Smuzhiyun }
4975*4882a593Smuzhiyun 
4976*4882a593Smuzhiyun static unsigned
hub_power_remaining(struct usb_hub * hub)4977*4882a593Smuzhiyun hub_power_remaining(struct usb_hub *hub)
4978*4882a593Smuzhiyun {
4979*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
4980*4882a593Smuzhiyun 	int remaining;
4981*4882a593Smuzhiyun 	int port1;
4982*4882a593Smuzhiyun 
4983*4882a593Smuzhiyun 	if (!hub->limited_power)
4984*4882a593Smuzhiyun 		return 0;
4985*4882a593Smuzhiyun 
4986*4882a593Smuzhiyun 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4987*4882a593Smuzhiyun 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4988*4882a593Smuzhiyun 		struct usb_port *port_dev = hub->ports[port1 - 1];
4989*4882a593Smuzhiyun 		struct usb_device *udev = port_dev->child;
4990*4882a593Smuzhiyun 		unsigned unit_load;
4991*4882a593Smuzhiyun 		int delta;
4992*4882a593Smuzhiyun 
4993*4882a593Smuzhiyun 		if (!udev)
4994*4882a593Smuzhiyun 			continue;
4995*4882a593Smuzhiyun 		if (hub_is_superspeed(udev))
4996*4882a593Smuzhiyun 			unit_load = 150;
4997*4882a593Smuzhiyun 		else
4998*4882a593Smuzhiyun 			unit_load = 100;
4999*4882a593Smuzhiyun 
5000*4882a593Smuzhiyun 		/*
5001*4882a593Smuzhiyun 		 * Unconfigured devices may not use more than one unit load,
5002*4882a593Smuzhiyun 		 * or 8mA for OTG ports
5003*4882a593Smuzhiyun 		 */
5004*4882a593Smuzhiyun 		if (udev->actconfig)
5005*4882a593Smuzhiyun 			delta = usb_get_max_power(udev, udev->actconfig);
5006*4882a593Smuzhiyun 		else if (port1 != udev->bus->otg_port || hdev->parent)
5007*4882a593Smuzhiyun 			delta = unit_load;
5008*4882a593Smuzhiyun 		else
5009*4882a593Smuzhiyun 			delta = 8;
5010*4882a593Smuzhiyun 		if (delta > hub->mA_per_port)
5011*4882a593Smuzhiyun 			dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
5012*4882a593Smuzhiyun 					delta, hub->mA_per_port);
5013*4882a593Smuzhiyun 		remaining -= delta;
5014*4882a593Smuzhiyun 	}
5015*4882a593Smuzhiyun 	if (remaining < 0) {
5016*4882a593Smuzhiyun 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
5017*4882a593Smuzhiyun 			-remaining);
5018*4882a593Smuzhiyun 		remaining = 0;
5019*4882a593Smuzhiyun 	}
5020*4882a593Smuzhiyun 	return remaining;
5021*4882a593Smuzhiyun }
5022*4882a593Smuzhiyun 
5023*4882a593Smuzhiyun 
descriptors_changed(struct usb_device * udev,struct usb_device_descriptor * old_device_descriptor,struct usb_host_bos * old_bos)5024*4882a593Smuzhiyun static int descriptors_changed(struct usb_device *udev,
5025*4882a593Smuzhiyun 		struct usb_device_descriptor *old_device_descriptor,
5026*4882a593Smuzhiyun 		struct usb_host_bos *old_bos)
5027*4882a593Smuzhiyun {
5028*4882a593Smuzhiyun 	int		changed = 0;
5029*4882a593Smuzhiyun 	unsigned	index;
5030*4882a593Smuzhiyun 	unsigned	serial_len = 0;
5031*4882a593Smuzhiyun 	unsigned	len;
5032*4882a593Smuzhiyun 	unsigned	old_length;
5033*4882a593Smuzhiyun 	int		length;
5034*4882a593Smuzhiyun 	char		*buf;
5035*4882a593Smuzhiyun 
5036*4882a593Smuzhiyun 	if (memcmp(&udev->descriptor, old_device_descriptor,
5037*4882a593Smuzhiyun 			sizeof(*old_device_descriptor)) != 0)
5038*4882a593Smuzhiyun 		return 1;
5039*4882a593Smuzhiyun 
5040*4882a593Smuzhiyun 	if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5041*4882a593Smuzhiyun 		return 1;
5042*4882a593Smuzhiyun 	if (udev->bos) {
5043*4882a593Smuzhiyun 		len = le16_to_cpu(udev->bos->desc->wTotalLength);
5044*4882a593Smuzhiyun 		if (len != le16_to_cpu(old_bos->desc->wTotalLength))
5045*4882a593Smuzhiyun 			return 1;
5046*4882a593Smuzhiyun 		if (memcmp(udev->bos->desc, old_bos->desc, len))
5047*4882a593Smuzhiyun 			return 1;
5048*4882a593Smuzhiyun 	}
5049*4882a593Smuzhiyun 
5050*4882a593Smuzhiyun 	/* Since the idVendor, idProduct, and bcdDevice values in the
5051*4882a593Smuzhiyun 	 * device descriptor haven't changed, we will assume the
5052*4882a593Smuzhiyun 	 * Manufacturer and Product strings haven't changed either.
5053*4882a593Smuzhiyun 	 * But the SerialNumber string could be different (e.g., a
5054*4882a593Smuzhiyun 	 * different flash card of the same brand).
5055*4882a593Smuzhiyun 	 */
5056*4882a593Smuzhiyun 	if (udev->serial)
5057*4882a593Smuzhiyun 		serial_len = strlen(udev->serial) + 1;
5058*4882a593Smuzhiyun 
5059*4882a593Smuzhiyun 	len = serial_len;
5060*4882a593Smuzhiyun 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5061*4882a593Smuzhiyun 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5062*4882a593Smuzhiyun 		len = max(len, old_length);
5063*4882a593Smuzhiyun 	}
5064*4882a593Smuzhiyun 
5065*4882a593Smuzhiyun 	buf = kmalloc(len, GFP_NOIO);
5066*4882a593Smuzhiyun 	if (!buf)
5067*4882a593Smuzhiyun 		/* assume the worst */
5068*4882a593Smuzhiyun 		return 1;
5069*4882a593Smuzhiyun 
5070*4882a593Smuzhiyun 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5071*4882a593Smuzhiyun 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5072*4882a593Smuzhiyun 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5073*4882a593Smuzhiyun 				old_length);
5074*4882a593Smuzhiyun 		if (length != old_length) {
5075*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "config index %d, error %d\n",
5076*4882a593Smuzhiyun 					index, length);
5077*4882a593Smuzhiyun 			changed = 1;
5078*4882a593Smuzhiyun 			break;
5079*4882a593Smuzhiyun 		}
5080*4882a593Smuzhiyun 		if (memcmp(buf, udev->rawdescriptors[index], old_length)
5081*4882a593Smuzhiyun 				!= 0) {
5082*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
5083*4882a593Smuzhiyun 				index,
5084*4882a593Smuzhiyun 				((struct usb_config_descriptor *) buf)->
5085*4882a593Smuzhiyun 					bConfigurationValue);
5086*4882a593Smuzhiyun 			changed = 1;
5087*4882a593Smuzhiyun 			break;
5088*4882a593Smuzhiyun 		}
5089*4882a593Smuzhiyun 	}
5090*4882a593Smuzhiyun 
5091*4882a593Smuzhiyun 	if (!changed && serial_len) {
5092*4882a593Smuzhiyun 		length = usb_string(udev, udev->descriptor.iSerialNumber,
5093*4882a593Smuzhiyun 				buf, serial_len);
5094*4882a593Smuzhiyun 		if (length + 1 != serial_len) {
5095*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "serial string error %d\n",
5096*4882a593Smuzhiyun 					length);
5097*4882a593Smuzhiyun 			changed = 1;
5098*4882a593Smuzhiyun 		} else if (memcmp(buf, udev->serial, length) != 0) {
5099*4882a593Smuzhiyun 			dev_dbg(&udev->dev, "serial string changed\n");
5100*4882a593Smuzhiyun 			changed = 1;
5101*4882a593Smuzhiyun 		}
5102*4882a593Smuzhiyun 	}
5103*4882a593Smuzhiyun 
5104*4882a593Smuzhiyun 	kfree(buf);
5105*4882a593Smuzhiyun 	return changed;
5106*4882a593Smuzhiyun }
5107*4882a593Smuzhiyun 
hub_port_connect(struct usb_hub * hub,int port1,u16 portstatus,u16 portchange)5108*4882a593Smuzhiyun static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
5109*4882a593Smuzhiyun 		u16 portchange)
5110*4882a593Smuzhiyun {
5111*4882a593Smuzhiyun 	int status = -ENODEV;
5112*4882a593Smuzhiyun 	int i;
5113*4882a593Smuzhiyun 	unsigned unit_load;
5114*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
5115*4882a593Smuzhiyun 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
5116*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
5117*4882a593Smuzhiyun 	struct usb_device *udev = port_dev->child;
5118*4882a593Smuzhiyun 	static int unreliable_port = -1;
5119*4882a593Smuzhiyun 	bool retry_locked;
5120*4882a593Smuzhiyun 
5121*4882a593Smuzhiyun 	/* Disconnect any existing devices under this port */
5122*4882a593Smuzhiyun 	if (udev) {
5123*4882a593Smuzhiyun 		if (hcd->usb_phy && !hdev->parent)
5124*4882a593Smuzhiyun 			usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
5125*4882a593Smuzhiyun 		usb_disconnect(&port_dev->child);
5126*4882a593Smuzhiyun 	}
5127*4882a593Smuzhiyun 
5128*4882a593Smuzhiyun 	/* We can forget about a "removed" device when there's a physical
5129*4882a593Smuzhiyun 	 * disconnect or the connect status changes.
5130*4882a593Smuzhiyun 	 */
5131*4882a593Smuzhiyun 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
5132*4882a593Smuzhiyun 			(portchange & USB_PORT_STAT_C_CONNECTION))
5133*4882a593Smuzhiyun 		clear_bit(port1, hub->removed_bits);
5134*4882a593Smuzhiyun 
5135*4882a593Smuzhiyun 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
5136*4882a593Smuzhiyun 				USB_PORT_STAT_C_ENABLE)) {
5137*4882a593Smuzhiyun 		status = hub_port_debounce_be_stable(hub, port1);
5138*4882a593Smuzhiyun 		if (status < 0) {
5139*4882a593Smuzhiyun 			if (status != -ENODEV &&
5140*4882a593Smuzhiyun 				port1 != unreliable_port &&
5141*4882a593Smuzhiyun 				printk_ratelimit())
5142*4882a593Smuzhiyun 				dev_err(&port_dev->dev, "connect-debounce failed\n");
5143*4882a593Smuzhiyun 			portstatus &= ~USB_PORT_STAT_CONNECTION;
5144*4882a593Smuzhiyun 			unreliable_port = port1;
5145*4882a593Smuzhiyun 		} else {
5146*4882a593Smuzhiyun 			portstatus = status;
5147*4882a593Smuzhiyun 		}
5148*4882a593Smuzhiyun 	}
5149*4882a593Smuzhiyun 
5150*4882a593Smuzhiyun 	/* Return now if debouncing failed or nothing is connected or
5151*4882a593Smuzhiyun 	 * the device was "removed".
5152*4882a593Smuzhiyun 	 */
5153*4882a593Smuzhiyun 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
5154*4882a593Smuzhiyun 			test_bit(port1, hub->removed_bits)) {
5155*4882a593Smuzhiyun 
5156*4882a593Smuzhiyun 		/*
5157*4882a593Smuzhiyun 		 * maybe switch power back on (e.g. root hub was reset)
5158*4882a593Smuzhiyun 		 * but only if the port isn't owned by someone else.
5159*4882a593Smuzhiyun 		 */
5160*4882a593Smuzhiyun 		if (hub_is_port_power_switchable(hub)
5161*4882a593Smuzhiyun 				&& !port_is_power_on(hub, portstatus)
5162*4882a593Smuzhiyun 				&& !port_dev->port_owner)
5163*4882a593Smuzhiyun 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
5164*4882a593Smuzhiyun 
5165*4882a593Smuzhiyun 		if (portstatus & USB_PORT_STAT_ENABLE)
5166*4882a593Smuzhiyun 			goto done;
5167*4882a593Smuzhiyun 		return;
5168*4882a593Smuzhiyun 	}
5169*4882a593Smuzhiyun 	if (hub_is_superspeed(hub->hdev))
5170*4882a593Smuzhiyun 		unit_load = 150;
5171*4882a593Smuzhiyun 	else
5172*4882a593Smuzhiyun 		unit_load = 100;
5173*4882a593Smuzhiyun 
5174*4882a593Smuzhiyun 	status = 0;
5175*4882a593Smuzhiyun 
5176*4882a593Smuzhiyun 	for (i = 0; i < PORT_INIT_TRIES; i++) {
5177*4882a593Smuzhiyun 		usb_lock_port(port_dev);
5178*4882a593Smuzhiyun 		mutex_lock(hcd->address0_mutex);
5179*4882a593Smuzhiyun 		retry_locked = true;
5180*4882a593Smuzhiyun 		/* reallocate for each attempt, since references
5181*4882a593Smuzhiyun 		 * to the previous one can escape in various ways
5182*4882a593Smuzhiyun 		 */
5183*4882a593Smuzhiyun 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
5184*4882a593Smuzhiyun 		if (!udev) {
5185*4882a593Smuzhiyun 			dev_err(&port_dev->dev,
5186*4882a593Smuzhiyun 					"couldn't allocate usb_device\n");
5187*4882a593Smuzhiyun 			mutex_unlock(hcd->address0_mutex);
5188*4882a593Smuzhiyun 			usb_unlock_port(port_dev);
5189*4882a593Smuzhiyun 			goto done;
5190*4882a593Smuzhiyun 		}
5191*4882a593Smuzhiyun 
5192*4882a593Smuzhiyun 		usb_set_device_state(udev, USB_STATE_POWERED);
5193*4882a593Smuzhiyun 		udev->bus_mA = hub->mA_per_port;
5194*4882a593Smuzhiyun 		udev->level = hdev->level + 1;
5195*4882a593Smuzhiyun 		udev->wusb = hub_is_wusb(hub);
5196*4882a593Smuzhiyun 
5197*4882a593Smuzhiyun 		/* Devices connected to SuperSpeed hubs are USB 3.0 or later */
5198*4882a593Smuzhiyun 		if (hub_is_superspeed(hub->hdev))
5199*4882a593Smuzhiyun 			udev->speed = USB_SPEED_SUPER;
5200*4882a593Smuzhiyun 		else
5201*4882a593Smuzhiyun 			udev->speed = USB_SPEED_UNKNOWN;
5202*4882a593Smuzhiyun 
5203*4882a593Smuzhiyun 		choose_devnum(udev);
5204*4882a593Smuzhiyun 		if (udev->devnum <= 0) {
5205*4882a593Smuzhiyun 			status = -ENOTCONN;	/* Don't retry */
5206*4882a593Smuzhiyun 			goto loop;
5207*4882a593Smuzhiyun 		}
5208*4882a593Smuzhiyun 
5209*4882a593Smuzhiyun 		/* reset (non-USB 3.0 devices) and get descriptor */
5210*4882a593Smuzhiyun 		status = hub_port_init(hub, udev, port1, i);
5211*4882a593Smuzhiyun 		if (status < 0)
5212*4882a593Smuzhiyun 			goto loop;
5213*4882a593Smuzhiyun 
5214*4882a593Smuzhiyun 		mutex_unlock(hcd->address0_mutex);
5215*4882a593Smuzhiyun 		usb_unlock_port(port_dev);
5216*4882a593Smuzhiyun 		retry_locked = false;
5217*4882a593Smuzhiyun 
5218*4882a593Smuzhiyun 		if (udev->quirks & USB_QUIRK_DELAY_INIT)
5219*4882a593Smuzhiyun 			msleep(2000);
5220*4882a593Smuzhiyun 
5221*4882a593Smuzhiyun 		/* consecutive bus-powered hubs aren't reliable; they can
5222*4882a593Smuzhiyun 		 * violate the voltage drop budget.  if the new child has
5223*4882a593Smuzhiyun 		 * a "powered" LED, users should notice we didn't enable it
5224*4882a593Smuzhiyun 		 * (without reading syslog), even without per-port LEDs
5225*4882a593Smuzhiyun 		 * on the parent.
5226*4882a593Smuzhiyun 		 */
5227*4882a593Smuzhiyun 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
5228*4882a593Smuzhiyun 				&& udev->bus_mA <= unit_load) {
5229*4882a593Smuzhiyun 			u16	devstat;
5230*4882a593Smuzhiyun 
5231*4882a593Smuzhiyun 			status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
5232*4882a593Smuzhiyun 					&devstat);
5233*4882a593Smuzhiyun 			if (status) {
5234*4882a593Smuzhiyun 				dev_dbg(&udev->dev, "get status %d ?\n", status);
5235*4882a593Smuzhiyun 				goto loop_disable;
5236*4882a593Smuzhiyun 			}
5237*4882a593Smuzhiyun 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
5238*4882a593Smuzhiyun 				dev_err(&udev->dev,
5239*4882a593Smuzhiyun 					"can't connect bus-powered hub "
5240*4882a593Smuzhiyun 					"to this port\n");
5241*4882a593Smuzhiyun 				if (hub->has_indicators) {
5242*4882a593Smuzhiyun 					hub->indicator[port1-1] =
5243*4882a593Smuzhiyun 						INDICATOR_AMBER_BLINK;
5244*4882a593Smuzhiyun 					queue_delayed_work(
5245*4882a593Smuzhiyun 						system_power_efficient_wq,
5246*4882a593Smuzhiyun 						&hub->leds, 0);
5247*4882a593Smuzhiyun 				}
5248*4882a593Smuzhiyun 				status = -ENOTCONN;	/* Don't retry */
5249*4882a593Smuzhiyun 				goto loop_disable;
5250*4882a593Smuzhiyun 			}
5251*4882a593Smuzhiyun 		}
5252*4882a593Smuzhiyun 
5253*4882a593Smuzhiyun 		/* check for devices running slower than they could */
5254*4882a593Smuzhiyun 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
5255*4882a593Smuzhiyun 				&& udev->speed == USB_SPEED_FULL
5256*4882a593Smuzhiyun 				&& highspeed_hubs != 0)
5257*4882a593Smuzhiyun 			check_highspeed(hub, udev, port1);
5258*4882a593Smuzhiyun 
5259*4882a593Smuzhiyun 		/* Store the parent's children[] pointer.  At this point
5260*4882a593Smuzhiyun 		 * udev becomes globally accessible, although presumably
5261*4882a593Smuzhiyun 		 * no one will look at it until hdev is unlocked.
5262*4882a593Smuzhiyun 		 */
5263*4882a593Smuzhiyun 		status = 0;
5264*4882a593Smuzhiyun 
5265*4882a593Smuzhiyun 		mutex_lock(&usb_port_peer_mutex);
5266*4882a593Smuzhiyun 
5267*4882a593Smuzhiyun 		/* We mustn't add new devices if the parent hub has
5268*4882a593Smuzhiyun 		 * been disconnected; we would race with the
5269*4882a593Smuzhiyun 		 * recursively_mark_NOTATTACHED() routine.
5270*4882a593Smuzhiyun 		 */
5271*4882a593Smuzhiyun 		spin_lock_irq(&device_state_lock);
5272*4882a593Smuzhiyun 		if (hdev->state == USB_STATE_NOTATTACHED)
5273*4882a593Smuzhiyun 			status = -ENOTCONN;
5274*4882a593Smuzhiyun 		else
5275*4882a593Smuzhiyun 			port_dev->child = udev;
5276*4882a593Smuzhiyun 		spin_unlock_irq(&device_state_lock);
5277*4882a593Smuzhiyun 		mutex_unlock(&usb_port_peer_mutex);
5278*4882a593Smuzhiyun 
5279*4882a593Smuzhiyun 		/* Run it through the hoops (find a driver, etc) */
5280*4882a593Smuzhiyun 		if (!status) {
5281*4882a593Smuzhiyun 			status = usb_new_device(udev);
5282*4882a593Smuzhiyun 			if (status) {
5283*4882a593Smuzhiyun 				mutex_lock(&usb_port_peer_mutex);
5284*4882a593Smuzhiyun 				spin_lock_irq(&device_state_lock);
5285*4882a593Smuzhiyun 				port_dev->child = NULL;
5286*4882a593Smuzhiyun 				spin_unlock_irq(&device_state_lock);
5287*4882a593Smuzhiyun 				mutex_unlock(&usb_port_peer_mutex);
5288*4882a593Smuzhiyun 			} else {
5289*4882a593Smuzhiyun 				if (hcd->usb_phy && !hdev->parent)
5290*4882a593Smuzhiyun 					usb_phy_notify_connect(hcd->usb_phy,
5291*4882a593Smuzhiyun 							udev->speed);
5292*4882a593Smuzhiyun 			}
5293*4882a593Smuzhiyun 		}
5294*4882a593Smuzhiyun 
5295*4882a593Smuzhiyun 		if (status)
5296*4882a593Smuzhiyun 			goto loop_disable;
5297*4882a593Smuzhiyun 
5298*4882a593Smuzhiyun 		status = hub_power_remaining(hub);
5299*4882a593Smuzhiyun 		if (status)
5300*4882a593Smuzhiyun 			dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
5301*4882a593Smuzhiyun 
5302*4882a593Smuzhiyun 		return;
5303*4882a593Smuzhiyun 
5304*4882a593Smuzhiyun loop_disable:
5305*4882a593Smuzhiyun 		hub_port_disable(hub, port1, 1);
5306*4882a593Smuzhiyun loop:
5307*4882a593Smuzhiyun 		usb_ep0_reinit(udev);
5308*4882a593Smuzhiyun 		release_devnum(udev);
5309*4882a593Smuzhiyun 		hub_free_dev(udev);
5310*4882a593Smuzhiyun 		if (retry_locked) {
5311*4882a593Smuzhiyun 			mutex_unlock(hcd->address0_mutex);
5312*4882a593Smuzhiyun 			usb_unlock_port(port_dev);
5313*4882a593Smuzhiyun 		}
5314*4882a593Smuzhiyun 		usb_put_dev(udev);
5315*4882a593Smuzhiyun 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
5316*4882a593Smuzhiyun 			break;
5317*4882a593Smuzhiyun 
5318*4882a593Smuzhiyun 		/* When halfway through our retry count, power-cycle the port */
5319*4882a593Smuzhiyun 		if (i == (PORT_INIT_TRIES - 1) / 2) {
5320*4882a593Smuzhiyun 			dev_info(&port_dev->dev, "attempt power cycle\n");
5321*4882a593Smuzhiyun 			usb_hub_set_port_power(hdev, hub, port1, false);
5322*4882a593Smuzhiyun 			msleep(2 * hub_power_on_good_delay(hub));
5323*4882a593Smuzhiyun 			usb_hub_set_port_power(hdev, hub, port1, true);
5324*4882a593Smuzhiyun 			msleep(hub_power_on_good_delay(hub));
5325*4882a593Smuzhiyun 		}
5326*4882a593Smuzhiyun 	}
5327*4882a593Smuzhiyun 	if (hub->hdev->parent ||
5328*4882a593Smuzhiyun 			!hcd->driver->port_handed_over ||
5329*4882a593Smuzhiyun 			!(hcd->driver->port_handed_over)(hcd, port1)) {
5330*4882a593Smuzhiyun 		if (status != -ENOTCONN && status != -ENODEV)
5331*4882a593Smuzhiyun 			dev_err(&port_dev->dev,
5332*4882a593Smuzhiyun 					"unable to enumerate USB device\n");
5333*4882a593Smuzhiyun 	}
5334*4882a593Smuzhiyun 
5335*4882a593Smuzhiyun done:
5336*4882a593Smuzhiyun 	hub_port_disable(hub, port1, 1);
5337*4882a593Smuzhiyun 	if (hcd->driver->relinquish_port && !hub->hdev->parent) {
5338*4882a593Smuzhiyun 		if ((status != -ENOTCONN && status != -ENODEV) ||
5339*4882a593Smuzhiyun 		    (status == -ENOTCONN && of_machine_is_compatible("rockchip,rk3288")))
5340*4882a593Smuzhiyun 			hcd->driver->relinquish_port(hcd, port1);
5341*4882a593Smuzhiyun 	}
5342*4882a593Smuzhiyun }
5343*4882a593Smuzhiyun 
5344*4882a593Smuzhiyun /* Handle physical or logical connection change events.
5345*4882a593Smuzhiyun  * This routine is called when:
5346*4882a593Smuzhiyun  *	a port connection-change occurs;
5347*4882a593Smuzhiyun  *	a port enable-change occurs (often caused by EMI);
5348*4882a593Smuzhiyun  *	usb_reset_and_verify_device() encounters changed descriptors (as from
5349*4882a593Smuzhiyun  *		a firmware download)
5350*4882a593Smuzhiyun  * caller already locked the hub
5351*4882a593Smuzhiyun  */
hub_port_connect_change(struct usb_hub * hub,int port1,u16 portstatus,u16 portchange)5352*4882a593Smuzhiyun static void hub_port_connect_change(struct usb_hub *hub, int port1,
5353*4882a593Smuzhiyun 					u16 portstatus, u16 portchange)
5354*4882a593Smuzhiyun 		__must_hold(&port_dev->status_lock)
5355*4882a593Smuzhiyun {
5356*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
5357*4882a593Smuzhiyun 	struct usb_device *udev = port_dev->child;
5358*4882a593Smuzhiyun 	struct usb_device_descriptor descriptor;
5359*4882a593Smuzhiyun 	int status = -ENODEV;
5360*4882a593Smuzhiyun 	int retval;
5361*4882a593Smuzhiyun 
5362*4882a593Smuzhiyun 	dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
5363*4882a593Smuzhiyun 			portchange, portspeed(hub, portstatus));
5364*4882a593Smuzhiyun 
5365*4882a593Smuzhiyun 	if (hub->has_indicators) {
5366*4882a593Smuzhiyun 		set_port_led(hub, port1, HUB_LED_AUTO);
5367*4882a593Smuzhiyun 		hub->indicator[port1-1] = INDICATOR_AUTO;
5368*4882a593Smuzhiyun 	}
5369*4882a593Smuzhiyun 
5370*4882a593Smuzhiyun #ifdef	CONFIG_USB_OTG
5371*4882a593Smuzhiyun 	/* during HNP, don't repeat the debounce */
5372*4882a593Smuzhiyun 	if (hub->hdev->bus->is_b_host)
5373*4882a593Smuzhiyun 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
5374*4882a593Smuzhiyun 				USB_PORT_STAT_C_ENABLE);
5375*4882a593Smuzhiyun #endif
5376*4882a593Smuzhiyun 
5377*4882a593Smuzhiyun 	/* Try to resuscitate an existing device */
5378*4882a593Smuzhiyun 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
5379*4882a593Smuzhiyun 			udev->state != USB_STATE_NOTATTACHED) {
5380*4882a593Smuzhiyun 		if (portstatus & USB_PORT_STAT_ENABLE) {
5381*4882a593Smuzhiyun 			/*
5382*4882a593Smuzhiyun 			 * USB-3 connections are initialized automatically by
5383*4882a593Smuzhiyun 			 * the hostcontroller hardware. Therefore check for
5384*4882a593Smuzhiyun 			 * changed device descriptors before resuscitating the
5385*4882a593Smuzhiyun 			 * device.
5386*4882a593Smuzhiyun 			 */
5387*4882a593Smuzhiyun 			descriptor = udev->descriptor;
5388*4882a593Smuzhiyun 			retval = usb_get_device_descriptor(udev,
5389*4882a593Smuzhiyun 					sizeof(udev->descriptor));
5390*4882a593Smuzhiyun 			if (retval < 0) {
5391*4882a593Smuzhiyun 				dev_dbg(&udev->dev,
5392*4882a593Smuzhiyun 						"can't read device descriptor %d\n",
5393*4882a593Smuzhiyun 						retval);
5394*4882a593Smuzhiyun 			} else {
5395*4882a593Smuzhiyun 				if (descriptors_changed(udev, &descriptor,
5396*4882a593Smuzhiyun 						udev->bos)) {
5397*4882a593Smuzhiyun 					dev_dbg(&udev->dev,
5398*4882a593Smuzhiyun 							"device descriptor has changed\n");
5399*4882a593Smuzhiyun 					/* for disconnect() calls */
5400*4882a593Smuzhiyun 					udev->descriptor = descriptor;
5401*4882a593Smuzhiyun 				} else {
5402*4882a593Smuzhiyun 					status = 0; /* Nothing to do */
5403*4882a593Smuzhiyun 				}
5404*4882a593Smuzhiyun 			}
5405*4882a593Smuzhiyun #ifdef CONFIG_PM
5406*4882a593Smuzhiyun 		} else if (udev->state == USB_STATE_SUSPENDED &&
5407*4882a593Smuzhiyun 				udev->persist_enabled) {
5408*4882a593Smuzhiyun 			/* For a suspended device, treat this as a
5409*4882a593Smuzhiyun 			 * remote wakeup event.
5410*4882a593Smuzhiyun 			 */
5411*4882a593Smuzhiyun 			usb_unlock_port(port_dev);
5412*4882a593Smuzhiyun 			status = usb_remote_wakeup(udev);
5413*4882a593Smuzhiyun 			usb_lock_port(port_dev);
5414*4882a593Smuzhiyun #endif
5415*4882a593Smuzhiyun 		} else {
5416*4882a593Smuzhiyun 			/* Don't resuscitate */;
5417*4882a593Smuzhiyun 		}
5418*4882a593Smuzhiyun 	}
5419*4882a593Smuzhiyun 	clear_bit(port1, hub->change_bits);
5420*4882a593Smuzhiyun 
5421*4882a593Smuzhiyun 	/* successfully revalidated the connection */
5422*4882a593Smuzhiyun 	if (status == 0)
5423*4882a593Smuzhiyun 		return;
5424*4882a593Smuzhiyun 
5425*4882a593Smuzhiyun 	usb_unlock_port(port_dev);
5426*4882a593Smuzhiyun 	hub_port_connect(hub, port1, portstatus, portchange);
5427*4882a593Smuzhiyun 	usb_lock_port(port_dev);
5428*4882a593Smuzhiyun }
5429*4882a593Smuzhiyun 
5430*4882a593Smuzhiyun /* Handle notifying userspace about hub over-current events */
port_over_current_notify(struct usb_port * port_dev)5431*4882a593Smuzhiyun static void port_over_current_notify(struct usb_port *port_dev)
5432*4882a593Smuzhiyun {
5433*4882a593Smuzhiyun 	char *envp[3];
5434*4882a593Smuzhiyun 	struct device *hub_dev;
5435*4882a593Smuzhiyun 	char *port_dev_path;
5436*4882a593Smuzhiyun 
5437*4882a593Smuzhiyun 	sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count");
5438*4882a593Smuzhiyun 
5439*4882a593Smuzhiyun 	hub_dev = port_dev->dev.parent;
5440*4882a593Smuzhiyun 
5441*4882a593Smuzhiyun 	if (!hub_dev)
5442*4882a593Smuzhiyun 		return;
5443*4882a593Smuzhiyun 
5444*4882a593Smuzhiyun 	port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL);
5445*4882a593Smuzhiyun 	if (!port_dev_path)
5446*4882a593Smuzhiyun 		return;
5447*4882a593Smuzhiyun 
5448*4882a593Smuzhiyun 	envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path);
5449*4882a593Smuzhiyun 	if (!envp[0])
5450*4882a593Smuzhiyun 		goto exit_path;
5451*4882a593Smuzhiyun 
5452*4882a593Smuzhiyun 	envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u",
5453*4882a593Smuzhiyun 			port_dev->over_current_count);
5454*4882a593Smuzhiyun 	if (!envp[1])
5455*4882a593Smuzhiyun 		goto exit;
5456*4882a593Smuzhiyun 
5457*4882a593Smuzhiyun 	envp[2] = NULL;
5458*4882a593Smuzhiyun 	kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp);
5459*4882a593Smuzhiyun 
5460*4882a593Smuzhiyun 	kfree(envp[1]);
5461*4882a593Smuzhiyun exit:
5462*4882a593Smuzhiyun 	kfree(envp[0]);
5463*4882a593Smuzhiyun exit_path:
5464*4882a593Smuzhiyun 	kfree(port_dev_path);
5465*4882a593Smuzhiyun }
5466*4882a593Smuzhiyun 
port_event(struct usb_hub * hub,int port1)5467*4882a593Smuzhiyun static void port_event(struct usb_hub *hub, int port1)
5468*4882a593Smuzhiyun 		__must_hold(&port_dev->status_lock)
5469*4882a593Smuzhiyun {
5470*4882a593Smuzhiyun 	int connect_change;
5471*4882a593Smuzhiyun 	struct usb_port *port_dev = hub->ports[port1 - 1];
5472*4882a593Smuzhiyun 	struct usb_device *udev = port_dev->child;
5473*4882a593Smuzhiyun 	struct usb_device *hdev = hub->hdev;
5474*4882a593Smuzhiyun 	u16 portstatus, portchange;
5475*4882a593Smuzhiyun 
5476*4882a593Smuzhiyun 	connect_change = test_bit(port1, hub->change_bits);
5477*4882a593Smuzhiyun 	clear_bit(port1, hub->event_bits);
5478*4882a593Smuzhiyun 	clear_bit(port1, hub->wakeup_bits);
5479*4882a593Smuzhiyun 
5480*4882a593Smuzhiyun 	if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
5481*4882a593Smuzhiyun 		return;
5482*4882a593Smuzhiyun 
5483*4882a593Smuzhiyun 	if (portchange & USB_PORT_STAT_C_CONNECTION) {
5484*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
5485*4882a593Smuzhiyun 		connect_change = 1;
5486*4882a593Smuzhiyun 	}
5487*4882a593Smuzhiyun 
5488*4882a593Smuzhiyun 	if (portchange & USB_PORT_STAT_C_ENABLE) {
5489*4882a593Smuzhiyun 		if (!connect_change)
5490*4882a593Smuzhiyun 			dev_dbg(&port_dev->dev, "enable change, status %08x\n",
5491*4882a593Smuzhiyun 					portstatus);
5492*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
5493*4882a593Smuzhiyun 
5494*4882a593Smuzhiyun 		/*
5495*4882a593Smuzhiyun 		 * EM interference sometimes causes badly shielded USB devices
5496*4882a593Smuzhiyun 		 * to be shutdown by the hub, this hack enables them again.
5497*4882a593Smuzhiyun 		 * Works at least with mouse driver.
5498*4882a593Smuzhiyun 		 */
5499*4882a593Smuzhiyun 		if (!(portstatus & USB_PORT_STAT_ENABLE)
5500*4882a593Smuzhiyun 		    && !connect_change && udev) {
5501*4882a593Smuzhiyun 			dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
5502*4882a593Smuzhiyun 			connect_change = 1;
5503*4882a593Smuzhiyun 		}
5504*4882a593Smuzhiyun 	}
5505*4882a593Smuzhiyun 
5506*4882a593Smuzhiyun 	if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
5507*4882a593Smuzhiyun 		u16 status = 0, unused;
5508*4882a593Smuzhiyun 		port_dev->over_current_count++;
5509*4882a593Smuzhiyun 		port_over_current_notify(port_dev);
5510*4882a593Smuzhiyun 
5511*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "over-current change #%u\n",
5512*4882a593Smuzhiyun 			port_dev->over_current_count);
5513*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1,
5514*4882a593Smuzhiyun 				USB_PORT_FEAT_C_OVER_CURRENT);
5515*4882a593Smuzhiyun 		msleep(100);	/* Cool down */
5516*4882a593Smuzhiyun 		hub_power_on(hub, true);
5517*4882a593Smuzhiyun 		hub_port_status(hub, port1, &status, &unused);
5518*4882a593Smuzhiyun 		if (status & USB_PORT_STAT_OVERCURRENT)
5519*4882a593Smuzhiyun 			dev_err(&port_dev->dev, "over-current condition\n");
5520*4882a593Smuzhiyun 	}
5521*4882a593Smuzhiyun 
5522*4882a593Smuzhiyun 	if (portchange & USB_PORT_STAT_C_RESET) {
5523*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "reset change\n");
5524*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
5525*4882a593Smuzhiyun 	}
5526*4882a593Smuzhiyun 	if ((portchange & USB_PORT_STAT_C_BH_RESET)
5527*4882a593Smuzhiyun 	    && hub_is_superspeed(hdev)) {
5528*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "warm reset change\n");
5529*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1,
5530*4882a593Smuzhiyun 				USB_PORT_FEAT_C_BH_PORT_RESET);
5531*4882a593Smuzhiyun 	}
5532*4882a593Smuzhiyun 	if (portchange & USB_PORT_STAT_C_LINK_STATE) {
5533*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "link state change\n");
5534*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1,
5535*4882a593Smuzhiyun 				USB_PORT_FEAT_C_PORT_LINK_STATE);
5536*4882a593Smuzhiyun 	}
5537*4882a593Smuzhiyun 	if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
5538*4882a593Smuzhiyun 		dev_warn(&port_dev->dev, "config error\n");
5539*4882a593Smuzhiyun 		usb_clear_port_feature(hdev, port1,
5540*4882a593Smuzhiyun 				USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
5541*4882a593Smuzhiyun 	}
5542*4882a593Smuzhiyun 
5543*4882a593Smuzhiyun 	/* skip port actions that require the port to be powered on */
5544*4882a593Smuzhiyun 	if (!pm_runtime_active(&port_dev->dev))
5545*4882a593Smuzhiyun 		return;
5546*4882a593Smuzhiyun 
5547*4882a593Smuzhiyun 	if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
5548*4882a593Smuzhiyun 		connect_change = 1;
5549*4882a593Smuzhiyun 
5550*4882a593Smuzhiyun 	/*
5551*4882a593Smuzhiyun 	 * Warm reset a USB3 protocol port if it's in
5552*4882a593Smuzhiyun 	 * SS.Inactive state.
5553*4882a593Smuzhiyun 	 */
5554*4882a593Smuzhiyun 	if (hub_port_warm_reset_required(hub, port1, portstatus)) {
5555*4882a593Smuzhiyun 		dev_dbg(&port_dev->dev, "do warm reset\n");
5556*4882a593Smuzhiyun 		if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
5557*4882a593Smuzhiyun 				|| udev->state == USB_STATE_NOTATTACHED) {
5558*4882a593Smuzhiyun 			if (hub_port_reset(hub, port1, NULL,
5559*4882a593Smuzhiyun 					HUB_BH_RESET_TIME, true) < 0)
5560*4882a593Smuzhiyun 				hub_port_disable(hub, port1, 1);
5561*4882a593Smuzhiyun 		} else {
5562*4882a593Smuzhiyun 			usb_unlock_port(port_dev);
5563*4882a593Smuzhiyun 			usb_lock_device(udev);
5564*4882a593Smuzhiyun 			usb_reset_device(udev);
5565*4882a593Smuzhiyun 			usb_unlock_device(udev);
5566*4882a593Smuzhiyun 			usb_lock_port(port_dev);
5567*4882a593Smuzhiyun 			connect_change = 0;
5568*4882a593Smuzhiyun 		}
5569*4882a593Smuzhiyun 	}
5570*4882a593Smuzhiyun 
5571*4882a593Smuzhiyun 	if (connect_change)
5572*4882a593Smuzhiyun 		hub_port_connect_change(hub, port1, portstatus, portchange);
5573*4882a593Smuzhiyun }
5574*4882a593Smuzhiyun 
hub_event(struct work_struct * work)5575*4882a593Smuzhiyun static void hub_event(struct work_struct *work)
5576*4882a593Smuzhiyun {
5577*4882a593Smuzhiyun 	struct usb_device *hdev;
5578*4882a593Smuzhiyun 	struct usb_interface *intf;
5579*4882a593Smuzhiyun 	struct usb_hub *hub;
5580*4882a593Smuzhiyun 	struct device *hub_dev;
5581*4882a593Smuzhiyun 	u16 hubstatus;
5582*4882a593Smuzhiyun 	u16 hubchange;
5583*4882a593Smuzhiyun 	int i, ret;
5584*4882a593Smuzhiyun 
5585*4882a593Smuzhiyun 	hub = container_of(work, struct usb_hub, events);
5586*4882a593Smuzhiyun 	hdev = hub->hdev;
5587*4882a593Smuzhiyun 	hub_dev = hub->intfdev;
5588*4882a593Smuzhiyun 	intf = to_usb_interface(hub_dev);
5589*4882a593Smuzhiyun 
5590*4882a593Smuzhiyun 	kcov_remote_start_usb((u64)hdev->bus->busnum);
5591*4882a593Smuzhiyun 
5592*4882a593Smuzhiyun 	dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
5593*4882a593Smuzhiyun 			hdev->state, hdev->maxchild,
5594*4882a593Smuzhiyun 			/* NOTE: expects max 15 ports... */
5595*4882a593Smuzhiyun 			(u16) hub->change_bits[0],
5596*4882a593Smuzhiyun 			(u16) hub->event_bits[0]);
5597*4882a593Smuzhiyun 
5598*4882a593Smuzhiyun 	/* Lock the device, then check to see if we were
5599*4882a593Smuzhiyun 	 * disconnected while waiting for the lock to succeed. */
5600*4882a593Smuzhiyun 	usb_lock_device(hdev);
5601*4882a593Smuzhiyun 	if (unlikely(hub->disconnected))
5602*4882a593Smuzhiyun 		goto out_hdev_lock;
5603*4882a593Smuzhiyun 
5604*4882a593Smuzhiyun 	/* If the hub has died, clean up after it */
5605*4882a593Smuzhiyun 	if (hdev->state == USB_STATE_NOTATTACHED) {
5606*4882a593Smuzhiyun 		hub->error = -ENODEV;
5607*4882a593Smuzhiyun 		hub_quiesce(hub, HUB_DISCONNECT);
5608*4882a593Smuzhiyun 		goto out_hdev_lock;
5609*4882a593Smuzhiyun 	}
5610*4882a593Smuzhiyun 
5611*4882a593Smuzhiyun 	/* Autoresume */
5612*4882a593Smuzhiyun 	ret = usb_autopm_get_interface(intf);
5613*4882a593Smuzhiyun 	if (ret) {
5614*4882a593Smuzhiyun 		dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
5615*4882a593Smuzhiyun 		goto out_hdev_lock;
5616*4882a593Smuzhiyun 	}
5617*4882a593Smuzhiyun 
5618*4882a593Smuzhiyun 	/* If this is an inactive hub, do nothing */
5619*4882a593Smuzhiyun 	if (hub->quiescing)
5620*4882a593Smuzhiyun 		goto out_autopm;
5621*4882a593Smuzhiyun 
5622*4882a593Smuzhiyun 	if (hub->error) {
5623*4882a593Smuzhiyun 		dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
5624*4882a593Smuzhiyun 
5625*4882a593Smuzhiyun 		ret = usb_reset_device(hdev);
5626*4882a593Smuzhiyun 		if (ret) {
5627*4882a593Smuzhiyun 			dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5628*4882a593Smuzhiyun 			goto out_autopm;
5629*4882a593Smuzhiyun 		}
5630*4882a593Smuzhiyun 
5631*4882a593Smuzhiyun 		hub->nerrors = 0;
5632*4882a593Smuzhiyun 		hub->error = 0;
5633*4882a593Smuzhiyun 	}
5634*4882a593Smuzhiyun 
5635*4882a593Smuzhiyun 	/* deal with port status changes */
5636*4882a593Smuzhiyun 	for (i = 1; i <= hdev->maxchild; i++) {
5637*4882a593Smuzhiyun 		struct usb_port *port_dev = hub->ports[i - 1];
5638*4882a593Smuzhiyun 
5639*4882a593Smuzhiyun 		if (test_bit(i, hub->event_bits)
5640*4882a593Smuzhiyun 				|| test_bit(i, hub->change_bits)
5641*4882a593Smuzhiyun 				|| test_bit(i, hub->wakeup_bits)) {
5642*4882a593Smuzhiyun 			/*
5643*4882a593Smuzhiyun 			 * The get_noresume and barrier ensure that if
5644*4882a593Smuzhiyun 			 * the port was in the process of resuming, we
5645*4882a593Smuzhiyun 			 * flush that work and keep the port active for
5646*4882a593Smuzhiyun 			 * the duration of the port_event().  However,
5647*4882a593Smuzhiyun 			 * if the port is runtime pm suspended
5648*4882a593Smuzhiyun 			 * (powered-off), we leave it in that state, run
5649*4882a593Smuzhiyun 			 * an abbreviated port_event(), and move on.
5650*4882a593Smuzhiyun 			 */
5651*4882a593Smuzhiyun 			pm_runtime_get_noresume(&port_dev->dev);
5652*4882a593Smuzhiyun 			pm_runtime_barrier(&port_dev->dev);
5653*4882a593Smuzhiyun 			usb_lock_port(port_dev);
5654*4882a593Smuzhiyun 			port_event(hub, i);
5655*4882a593Smuzhiyun 			usb_unlock_port(port_dev);
5656*4882a593Smuzhiyun 			pm_runtime_put_sync(&port_dev->dev);
5657*4882a593Smuzhiyun 		}
5658*4882a593Smuzhiyun 	}
5659*4882a593Smuzhiyun 
5660*4882a593Smuzhiyun 	/* deal with hub status changes */
5661*4882a593Smuzhiyun 	if (test_and_clear_bit(0, hub->event_bits) == 0)
5662*4882a593Smuzhiyun 		;	/* do nothing */
5663*4882a593Smuzhiyun 	else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5664*4882a593Smuzhiyun 		dev_err(hub_dev, "get_hub_status failed\n");
5665*4882a593Smuzhiyun 	else {
5666*4882a593Smuzhiyun 		if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5667*4882a593Smuzhiyun 			dev_dbg(hub_dev, "power change\n");
5668*4882a593Smuzhiyun 			clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
5669*4882a593Smuzhiyun 			if (hubstatus & HUB_STATUS_LOCAL_POWER)
5670*4882a593Smuzhiyun 				/* FIXME: Is this always true? */
5671*4882a593Smuzhiyun 				hub->limited_power = 1;
5672*4882a593Smuzhiyun 			else
5673*4882a593Smuzhiyun 				hub->limited_power = 0;
5674*4882a593Smuzhiyun 		}
5675*4882a593Smuzhiyun 		if (hubchange & HUB_CHANGE_OVERCURRENT) {
5676*4882a593Smuzhiyun 			u16 status = 0;
5677*4882a593Smuzhiyun 			u16 unused;
5678*4882a593Smuzhiyun 
5679*4882a593Smuzhiyun 			dev_dbg(hub_dev, "over-current change\n");
5680*4882a593Smuzhiyun 			clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5681*4882a593Smuzhiyun 			msleep(500);	/* Cool down */
5682*4882a593Smuzhiyun 			hub_power_on(hub, true);
5683*4882a593Smuzhiyun 			hub_hub_status(hub, &status, &unused);
5684*4882a593Smuzhiyun 			if (status & HUB_STATUS_OVERCURRENT)
5685*4882a593Smuzhiyun 				dev_err(hub_dev, "over-current condition\n");
5686*4882a593Smuzhiyun 		}
5687*4882a593Smuzhiyun 	}
5688*4882a593Smuzhiyun 
5689*4882a593Smuzhiyun out_autopm:
5690*4882a593Smuzhiyun 	/* Balance the usb_autopm_get_interface() above */
5691*4882a593Smuzhiyun 	usb_autopm_put_interface_no_suspend(intf);
5692*4882a593Smuzhiyun out_hdev_lock:
5693*4882a593Smuzhiyun 	usb_unlock_device(hdev);
5694*4882a593Smuzhiyun 
5695*4882a593Smuzhiyun 	/* Balance the stuff in kick_hub_wq() and allow autosuspend */
5696*4882a593Smuzhiyun 	usb_autopm_put_interface(intf);
5697*4882a593Smuzhiyun 	kref_put(&hub->kref, hub_release);
5698*4882a593Smuzhiyun 
5699*4882a593Smuzhiyun 	kcov_remote_stop();
5700*4882a593Smuzhiyun }
5701*4882a593Smuzhiyun 
5702*4882a593Smuzhiyun static const struct usb_device_id hub_id_table[] = {
5703*4882a593Smuzhiyun     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5704*4882a593Smuzhiyun                    | USB_DEVICE_ID_MATCH_PRODUCT
5705*4882a593Smuzhiyun                    | USB_DEVICE_ID_MATCH_INT_CLASS,
5706*4882a593Smuzhiyun       .idVendor = USB_VENDOR_SMSC,
5707*4882a593Smuzhiyun       .idProduct = USB_PRODUCT_USB5534B,
5708*4882a593Smuzhiyun       .bInterfaceClass = USB_CLASS_HUB,
5709*4882a593Smuzhiyun       .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5710*4882a593Smuzhiyun     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5711*4882a593Smuzhiyun                    | USB_DEVICE_ID_MATCH_PRODUCT,
5712*4882a593Smuzhiyun       .idVendor = USB_VENDOR_CYPRESS,
5713*4882a593Smuzhiyun       .idProduct = USB_PRODUCT_CY7C65632,
5714*4882a593Smuzhiyun       .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5715*4882a593Smuzhiyun     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5716*4882a593Smuzhiyun 			| USB_DEVICE_ID_MATCH_INT_CLASS,
5717*4882a593Smuzhiyun       .idVendor = USB_VENDOR_GENESYS_LOGIC,
5718*4882a593Smuzhiyun       .bInterfaceClass = USB_CLASS_HUB,
5719*4882a593Smuzhiyun       .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
5720*4882a593Smuzhiyun     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5721*4882a593Smuzhiyun       .bDeviceClass = USB_CLASS_HUB},
5722*4882a593Smuzhiyun     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5723*4882a593Smuzhiyun       .bInterfaceClass = USB_CLASS_HUB},
5724*4882a593Smuzhiyun     { }						/* Terminating entry */
5725*4882a593Smuzhiyun };
5726*4882a593Smuzhiyun 
5727*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, hub_id_table);
5728*4882a593Smuzhiyun 
5729*4882a593Smuzhiyun static struct usb_driver hub_driver = {
5730*4882a593Smuzhiyun 	.name =		"hub",
5731*4882a593Smuzhiyun 	.probe =	hub_probe,
5732*4882a593Smuzhiyun 	.disconnect =	hub_disconnect,
5733*4882a593Smuzhiyun 	.suspend =	hub_suspend,
5734*4882a593Smuzhiyun 	.resume =	hub_resume,
5735*4882a593Smuzhiyun 	.reset_resume =	hub_reset_resume,
5736*4882a593Smuzhiyun 	.pre_reset =	hub_pre_reset,
5737*4882a593Smuzhiyun 	.post_reset =	hub_post_reset,
5738*4882a593Smuzhiyun 	.unlocked_ioctl = hub_ioctl,
5739*4882a593Smuzhiyun 	.id_table =	hub_id_table,
5740*4882a593Smuzhiyun 	.supports_autosuspend =	1,
5741*4882a593Smuzhiyun };
5742*4882a593Smuzhiyun 
usb_hub_init(void)5743*4882a593Smuzhiyun int usb_hub_init(void)
5744*4882a593Smuzhiyun {
5745*4882a593Smuzhiyun 	if (usb_register(&hub_driver) < 0) {
5746*4882a593Smuzhiyun 		printk(KERN_ERR "%s: can't register hub driver\n",
5747*4882a593Smuzhiyun 			usbcore_name);
5748*4882a593Smuzhiyun 		return -1;
5749*4882a593Smuzhiyun 	}
5750*4882a593Smuzhiyun 
5751*4882a593Smuzhiyun 	/*
5752*4882a593Smuzhiyun 	 * The workqueue needs to be freezable to avoid interfering with
5753*4882a593Smuzhiyun 	 * USB-PERSIST port handover. Otherwise it might see that a full-speed
5754*4882a593Smuzhiyun 	 * device was gone before the EHCI controller had handed its port
5755*4882a593Smuzhiyun 	 * over to the companion full-speed controller.
5756*4882a593Smuzhiyun 	 */
5757*4882a593Smuzhiyun 	hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
5758*4882a593Smuzhiyun 	if (hub_wq)
5759*4882a593Smuzhiyun 		return 0;
5760*4882a593Smuzhiyun 
5761*4882a593Smuzhiyun 	/* Fall through if kernel_thread failed */
5762*4882a593Smuzhiyun 	usb_deregister(&hub_driver);
5763*4882a593Smuzhiyun 	pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
5764*4882a593Smuzhiyun 
5765*4882a593Smuzhiyun 	return -1;
5766*4882a593Smuzhiyun }
5767*4882a593Smuzhiyun 
usb_hub_cleanup(void)5768*4882a593Smuzhiyun void usb_hub_cleanup(void)
5769*4882a593Smuzhiyun {
5770*4882a593Smuzhiyun 	destroy_workqueue(hub_wq);
5771*4882a593Smuzhiyun 
5772*4882a593Smuzhiyun 	/*
5773*4882a593Smuzhiyun 	 * Hub resources are freed for us by usb_deregister. It calls
5774*4882a593Smuzhiyun 	 * usb_driver_purge on every device which in turn calls that
5775*4882a593Smuzhiyun 	 * devices disconnect function if it is using this driver.
5776*4882a593Smuzhiyun 	 * The hub_disconnect function takes care of releasing the
5777*4882a593Smuzhiyun 	 * individual hub resources. -greg
5778*4882a593Smuzhiyun 	 */
5779*4882a593Smuzhiyun 	usb_deregister(&hub_driver);
5780*4882a593Smuzhiyun } /* usb_hub_cleanup() */
5781*4882a593Smuzhiyun 
5782*4882a593Smuzhiyun /**
5783*4882a593Smuzhiyun  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
5784*4882a593Smuzhiyun  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5785*4882a593Smuzhiyun  *
5786*4882a593Smuzhiyun  * WARNING - don't use this routine to reset a composite device
5787*4882a593Smuzhiyun  * (one with multiple interfaces owned by separate drivers)!
5788*4882a593Smuzhiyun  * Use usb_reset_device() instead.
5789*4882a593Smuzhiyun  *
5790*4882a593Smuzhiyun  * Do a port reset, reassign the device's address, and establish its
5791*4882a593Smuzhiyun  * former operating configuration.  If the reset fails, or the device's
5792*4882a593Smuzhiyun  * descriptors change from their values before the reset, or the original
5793*4882a593Smuzhiyun  * configuration and altsettings cannot be restored, a flag will be set
5794*4882a593Smuzhiyun  * telling hub_wq to pretend the device has been disconnected and then
5795*4882a593Smuzhiyun  * re-connected.  All drivers will be unbound, and the device will be
5796*4882a593Smuzhiyun  * re-enumerated and probed all over again.
5797*4882a593Smuzhiyun  *
5798*4882a593Smuzhiyun  * Return: 0 if the reset succeeded, -ENODEV if the device has been
5799*4882a593Smuzhiyun  * flagged for logical disconnection, or some other negative error code
5800*4882a593Smuzhiyun  * if the reset wasn't even attempted.
5801*4882a593Smuzhiyun  *
5802*4882a593Smuzhiyun  * Note:
5803*4882a593Smuzhiyun  * The caller must own the device lock and the port lock, the latter is
5804*4882a593Smuzhiyun  * taken by usb_reset_device().  For example, it's safe to use
5805*4882a593Smuzhiyun  * usb_reset_device() from a driver probe() routine after downloading
5806*4882a593Smuzhiyun  * new firmware.  For calls that might not occur during probe(), drivers
5807*4882a593Smuzhiyun  * should lock the device using usb_lock_device_for_reset().
5808*4882a593Smuzhiyun  *
5809*4882a593Smuzhiyun  * Locking exception: This routine may also be called from within an
5810*4882a593Smuzhiyun  * autoresume handler.  Such usage won't conflict with other tasks
5811*4882a593Smuzhiyun  * holding the device lock because these tasks should always call
5812*4882a593Smuzhiyun  * usb_autopm_resume_device(), thereby preventing any unwanted
5813*4882a593Smuzhiyun  * autoresume.  The autoresume handler is expected to have already
5814*4882a593Smuzhiyun  * acquired the port lock before calling this routine.
5815*4882a593Smuzhiyun  */
usb_reset_and_verify_device(struct usb_device * udev)5816*4882a593Smuzhiyun static int usb_reset_and_verify_device(struct usb_device *udev)
5817*4882a593Smuzhiyun {
5818*4882a593Smuzhiyun 	struct usb_device		*parent_hdev = udev->parent;
5819*4882a593Smuzhiyun 	struct usb_hub			*parent_hub;
5820*4882a593Smuzhiyun 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
5821*4882a593Smuzhiyun 	struct usb_device_descriptor	descriptor = udev->descriptor;
5822*4882a593Smuzhiyun 	struct usb_host_bos		*bos;
5823*4882a593Smuzhiyun 	int				i, j, ret = 0;
5824*4882a593Smuzhiyun 	int				port1 = udev->portnum;
5825*4882a593Smuzhiyun 
5826*4882a593Smuzhiyun 	if (udev->state == USB_STATE_NOTATTACHED ||
5827*4882a593Smuzhiyun 			udev->state == USB_STATE_SUSPENDED) {
5828*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5829*4882a593Smuzhiyun 				udev->state);
5830*4882a593Smuzhiyun 		return -EINVAL;
5831*4882a593Smuzhiyun 	}
5832*4882a593Smuzhiyun 
5833*4882a593Smuzhiyun 	if (!parent_hdev)
5834*4882a593Smuzhiyun 		return -EISDIR;
5835*4882a593Smuzhiyun 
5836*4882a593Smuzhiyun 	parent_hub = usb_hub_to_struct_hub(parent_hdev);
5837*4882a593Smuzhiyun 
5838*4882a593Smuzhiyun 	/* Disable USB2 hardware LPM.
5839*4882a593Smuzhiyun 	 * It will be re-enabled by the enumeration process.
5840*4882a593Smuzhiyun 	 */
5841*4882a593Smuzhiyun 	usb_disable_usb2_hardware_lpm(udev);
5842*4882a593Smuzhiyun 
5843*4882a593Smuzhiyun 	/* Disable LPM while we reset the device and reinstall the alt settings.
5844*4882a593Smuzhiyun 	 * Device-initiated LPM, and system exit latency settings are cleared
5845*4882a593Smuzhiyun 	 * when the device is reset, so we have to set them up again.
5846*4882a593Smuzhiyun 	 */
5847*4882a593Smuzhiyun 	ret = usb_unlocked_disable_lpm(udev);
5848*4882a593Smuzhiyun 	if (ret) {
5849*4882a593Smuzhiyun 		dev_err(&udev->dev, "%s Failed to disable LPM\n", __func__);
5850*4882a593Smuzhiyun 		goto re_enumerate_no_bos;
5851*4882a593Smuzhiyun 	}
5852*4882a593Smuzhiyun 
5853*4882a593Smuzhiyun 	bos = udev->bos;
5854*4882a593Smuzhiyun 	udev->bos = NULL;
5855*4882a593Smuzhiyun 
5856*4882a593Smuzhiyun 	mutex_lock(hcd->address0_mutex);
5857*4882a593Smuzhiyun 
5858*4882a593Smuzhiyun 	for (i = 0; i < PORT_INIT_TRIES; ++i) {
5859*4882a593Smuzhiyun 
5860*4882a593Smuzhiyun 		/* ep0 maxpacket size may change; let the HCD know about it.
5861*4882a593Smuzhiyun 		 * Other endpoints will be handled by re-enumeration. */
5862*4882a593Smuzhiyun 		usb_ep0_reinit(udev);
5863*4882a593Smuzhiyun 		ret = hub_port_init(parent_hub, udev, port1, i);
5864*4882a593Smuzhiyun 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
5865*4882a593Smuzhiyun 			break;
5866*4882a593Smuzhiyun 	}
5867*4882a593Smuzhiyun 	mutex_unlock(hcd->address0_mutex);
5868*4882a593Smuzhiyun 
5869*4882a593Smuzhiyun 	if (ret < 0)
5870*4882a593Smuzhiyun 		goto re_enumerate;
5871*4882a593Smuzhiyun 
5872*4882a593Smuzhiyun 	/* Device might have changed firmware (DFU or similar) */
5873*4882a593Smuzhiyun 	if (descriptors_changed(udev, &descriptor, bos)) {
5874*4882a593Smuzhiyun 		dev_info(&udev->dev, "device firmware changed\n");
5875*4882a593Smuzhiyun 		udev->descriptor = descriptor;	/* for disconnect() calls */
5876*4882a593Smuzhiyun 		goto re_enumerate;
5877*4882a593Smuzhiyun 	}
5878*4882a593Smuzhiyun 
5879*4882a593Smuzhiyun 	/* Restore the device's previous configuration */
5880*4882a593Smuzhiyun 	if (!udev->actconfig)
5881*4882a593Smuzhiyun 		goto done;
5882*4882a593Smuzhiyun 
5883*4882a593Smuzhiyun 	mutex_lock(hcd->bandwidth_mutex);
5884*4882a593Smuzhiyun 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5885*4882a593Smuzhiyun 	if (ret < 0) {
5886*4882a593Smuzhiyun 		dev_warn(&udev->dev,
5887*4882a593Smuzhiyun 				"Busted HC?  Not enough HCD resources for "
5888*4882a593Smuzhiyun 				"old configuration.\n");
5889*4882a593Smuzhiyun 		mutex_unlock(hcd->bandwidth_mutex);
5890*4882a593Smuzhiyun 		goto re_enumerate;
5891*4882a593Smuzhiyun 	}
5892*4882a593Smuzhiyun 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5893*4882a593Smuzhiyun 			USB_REQ_SET_CONFIGURATION, 0,
5894*4882a593Smuzhiyun 			udev->actconfig->desc.bConfigurationValue, 0,
5895*4882a593Smuzhiyun 			NULL, 0, USB_CTRL_SET_TIMEOUT);
5896*4882a593Smuzhiyun 	if (ret < 0) {
5897*4882a593Smuzhiyun 		dev_err(&udev->dev,
5898*4882a593Smuzhiyun 			"can't restore configuration #%d (error=%d)\n",
5899*4882a593Smuzhiyun 			udev->actconfig->desc.bConfigurationValue, ret);
5900*4882a593Smuzhiyun 		mutex_unlock(hcd->bandwidth_mutex);
5901*4882a593Smuzhiyun 		goto re_enumerate;
5902*4882a593Smuzhiyun 	}
5903*4882a593Smuzhiyun 	mutex_unlock(hcd->bandwidth_mutex);
5904*4882a593Smuzhiyun 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
5905*4882a593Smuzhiyun 
5906*4882a593Smuzhiyun 	/* Put interfaces back into the same altsettings as before.
5907*4882a593Smuzhiyun 	 * Don't bother to send the Set-Interface request for interfaces
5908*4882a593Smuzhiyun 	 * that were already in altsetting 0; besides being unnecessary,
5909*4882a593Smuzhiyun 	 * many devices can't handle it.  Instead just reset the host-side
5910*4882a593Smuzhiyun 	 * endpoint state.
5911*4882a593Smuzhiyun 	 */
5912*4882a593Smuzhiyun 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
5913*4882a593Smuzhiyun 		struct usb_host_config *config = udev->actconfig;
5914*4882a593Smuzhiyun 		struct usb_interface *intf = config->interface[i];
5915*4882a593Smuzhiyun 		struct usb_interface_descriptor *desc;
5916*4882a593Smuzhiyun 
5917*4882a593Smuzhiyun 		desc = &intf->cur_altsetting->desc;
5918*4882a593Smuzhiyun 		if (desc->bAlternateSetting == 0) {
5919*4882a593Smuzhiyun 			usb_disable_interface(udev, intf, true);
5920*4882a593Smuzhiyun 			usb_enable_interface(udev, intf, true);
5921*4882a593Smuzhiyun 			ret = 0;
5922*4882a593Smuzhiyun 		} else {
5923*4882a593Smuzhiyun 			/* Let the bandwidth allocation function know that this
5924*4882a593Smuzhiyun 			 * device has been reset, and it will have to use
5925*4882a593Smuzhiyun 			 * alternate setting 0 as the current alternate setting.
5926*4882a593Smuzhiyun 			 */
5927*4882a593Smuzhiyun 			intf->resetting_device = 1;
5928*4882a593Smuzhiyun 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
5929*4882a593Smuzhiyun 					desc->bAlternateSetting);
5930*4882a593Smuzhiyun 			intf->resetting_device = 0;
5931*4882a593Smuzhiyun 		}
5932*4882a593Smuzhiyun 		if (ret < 0) {
5933*4882a593Smuzhiyun 			dev_err(&udev->dev, "failed to restore interface %d "
5934*4882a593Smuzhiyun 				"altsetting %d (error=%d)\n",
5935*4882a593Smuzhiyun 				desc->bInterfaceNumber,
5936*4882a593Smuzhiyun 				desc->bAlternateSetting,
5937*4882a593Smuzhiyun 				ret);
5938*4882a593Smuzhiyun 			goto re_enumerate;
5939*4882a593Smuzhiyun 		}
5940*4882a593Smuzhiyun 		/* Resetting also frees any allocated streams */
5941*4882a593Smuzhiyun 		for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5942*4882a593Smuzhiyun 			intf->cur_altsetting->endpoint[j].streams = 0;
5943*4882a593Smuzhiyun 	}
5944*4882a593Smuzhiyun 
5945*4882a593Smuzhiyun done:
5946*4882a593Smuzhiyun 	/* Now that the alt settings are re-installed, enable LTM and LPM. */
5947*4882a593Smuzhiyun 	usb_enable_usb2_hardware_lpm(udev);
5948*4882a593Smuzhiyun 	usb_unlocked_enable_lpm(udev);
5949*4882a593Smuzhiyun 	usb_enable_ltm(udev);
5950*4882a593Smuzhiyun 	usb_release_bos_descriptor(udev);
5951*4882a593Smuzhiyun 	udev->bos = bos;
5952*4882a593Smuzhiyun 	return 0;
5953*4882a593Smuzhiyun 
5954*4882a593Smuzhiyun re_enumerate:
5955*4882a593Smuzhiyun 	usb_release_bos_descriptor(udev);
5956*4882a593Smuzhiyun 	udev->bos = bos;
5957*4882a593Smuzhiyun re_enumerate_no_bos:
5958*4882a593Smuzhiyun 	/* LPM state doesn't matter when we're about to destroy the device. */
5959*4882a593Smuzhiyun 	hub_port_logical_disconnect(parent_hub, port1);
5960*4882a593Smuzhiyun 	return -ENODEV;
5961*4882a593Smuzhiyun }
5962*4882a593Smuzhiyun 
5963*4882a593Smuzhiyun /**
5964*4882a593Smuzhiyun  * usb_reset_device - warn interface drivers and perform a USB port reset
5965*4882a593Smuzhiyun  * @udev: device to reset (not in NOTATTACHED state)
5966*4882a593Smuzhiyun  *
5967*4882a593Smuzhiyun  * Warns all drivers bound to registered interfaces (using their pre_reset
5968*4882a593Smuzhiyun  * method), performs the port reset, and then lets the drivers know that
5969*4882a593Smuzhiyun  * the reset is over (using their post_reset method).
5970*4882a593Smuzhiyun  *
5971*4882a593Smuzhiyun  * Return: The same as for usb_reset_and_verify_device().
5972*4882a593Smuzhiyun  *
5973*4882a593Smuzhiyun  * Note:
5974*4882a593Smuzhiyun  * The caller must own the device lock.  For example, it's safe to use
5975*4882a593Smuzhiyun  * this from a driver probe() routine after downloading new firmware.
5976*4882a593Smuzhiyun  * For calls that might not occur during probe(), drivers should lock
5977*4882a593Smuzhiyun  * the device using usb_lock_device_for_reset().
5978*4882a593Smuzhiyun  *
5979*4882a593Smuzhiyun  * If an interface is currently being probed or disconnected, we assume
5980*4882a593Smuzhiyun  * its driver knows how to handle resets.  For all other interfaces,
5981*4882a593Smuzhiyun  * if the driver doesn't have pre_reset and post_reset methods then
5982*4882a593Smuzhiyun  * we attempt to unbind it and rebind afterward.
5983*4882a593Smuzhiyun  */
usb_reset_device(struct usb_device * udev)5984*4882a593Smuzhiyun int usb_reset_device(struct usb_device *udev)
5985*4882a593Smuzhiyun {
5986*4882a593Smuzhiyun 	int ret;
5987*4882a593Smuzhiyun 	int i;
5988*4882a593Smuzhiyun 	unsigned int noio_flag;
5989*4882a593Smuzhiyun 	struct usb_port *port_dev;
5990*4882a593Smuzhiyun 	struct usb_host_config *config = udev->actconfig;
5991*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
5992*4882a593Smuzhiyun 
5993*4882a593Smuzhiyun 	if (udev->state == USB_STATE_NOTATTACHED) {
5994*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5995*4882a593Smuzhiyun 				udev->state);
5996*4882a593Smuzhiyun 		return -EINVAL;
5997*4882a593Smuzhiyun 	}
5998*4882a593Smuzhiyun 
5999*4882a593Smuzhiyun 	if (!udev->parent) {
6000*4882a593Smuzhiyun 		/* this requires hcd-specific logic; see ohci_restart() */
6001*4882a593Smuzhiyun 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
6002*4882a593Smuzhiyun 		return -EISDIR;
6003*4882a593Smuzhiyun 	}
6004*4882a593Smuzhiyun 
6005*4882a593Smuzhiyun 	port_dev = hub->ports[udev->portnum - 1];
6006*4882a593Smuzhiyun 
6007*4882a593Smuzhiyun 	/*
6008*4882a593Smuzhiyun 	 * Don't allocate memory with GFP_KERNEL in current
6009*4882a593Smuzhiyun 	 * context to avoid possible deadlock if usb mass
6010*4882a593Smuzhiyun 	 * storage interface or usbnet interface(iSCSI case)
6011*4882a593Smuzhiyun 	 * is included in current configuration. The easist
6012*4882a593Smuzhiyun 	 * approach is to do it for every device reset,
6013*4882a593Smuzhiyun 	 * because the device 'memalloc_noio' flag may have
6014*4882a593Smuzhiyun 	 * not been set before reseting the usb device.
6015*4882a593Smuzhiyun 	 */
6016*4882a593Smuzhiyun 	noio_flag = memalloc_noio_save();
6017*4882a593Smuzhiyun 
6018*4882a593Smuzhiyun 	/* Prevent autosuspend during the reset */
6019*4882a593Smuzhiyun 	usb_autoresume_device(udev);
6020*4882a593Smuzhiyun 
6021*4882a593Smuzhiyun 	if (config) {
6022*4882a593Smuzhiyun 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
6023*4882a593Smuzhiyun 			struct usb_interface *cintf = config->interface[i];
6024*4882a593Smuzhiyun 			struct usb_driver *drv;
6025*4882a593Smuzhiyun 			int unbind = 0;
6026*4882a593Smuzhiyun 
6027*4882a593Smuzhiyun 			if (cintf->dev.driver) {
6028*4882a593Smuzhiyun 				drv = to_usb_driver(cintf->dev.driver);
6029*4882a593Smuzhiyun 				if (drv->pre_reset && drv->post_reset)
6030*4882a593Smuzhiyun 					unbind = (drv->pre_reset)(cintf);
6031*4882a593Smuzhiyun 				else if (cintf->condition ==
6032*4882a593Smuzhiyun 						USB_INTERFACE_BOUND)
6033*4882a593Smuzhiyun 					unbind = 1;
6034*4882a593Smuzhiyun 				if (unbind)
6035*4882a593Smuzhiyun 					usb_forced_unbind_intf(cintf);
6036*4882a593Smuzhiyun 			}
6037*4882a593Smuzhiyun 		}
6038*4882a593Smuzhiyun 	}
6039*4882a593Smuzhiyun 
6040*4882a593Smuzhiyun 	usb_lock_port(port_dev);
6041*4882a593Smuzhiyun 	ret = usb_reset_and_verify_device(udev);
6042*4882a593Smuzhiyun 	usb_unlock_port(port_dev);
6043*4882a593Smuzhiyun 
6044*4882a593Smuzhiyun 	if (config) {
6045*4882a593Smuzhiyun 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
6046*4882a593Smuzhiyun 			struct usb_interface *cintf = config->interface[i];
6047*4882a593Smuzhiyun 			struct usb_driver *drv;
6048*4882a593Smuzhiyun 			int rebind = cintf->needs_binding;
6049*4882a593Smuzhiyun 
6050*4882a593Smuzhiyun 			if (!rebind && cintf->dev.driver) {
6051*4882a593Smuzhiyun 				drv = to_usb_driver(cintf->dev.driver);
6052*4882a593Smuzhiyun 				if (drv->post_reset)
6053*4882a593Smuzhiyun 					rebind = (drv->post_reset)(cintf);
6054*4882a593Smuzhiyun 				else if (cintf->condition ==
6055*4882a593Smuzhiyun 						USB_INTERFACE_BOUND)
6056*4882a593Smuzhiyun 					rebind = 1;
6057*4882a593Smuzhiyun 				if (rebind)
6058*4882a593Smuzhiyun 					cintf->needs_binding = 1;
6059*4882a593Smuzhiyun 			}
6060*4882a593Smuzhiyun 		}
6061*4882a593Smuzhiyun 
6062*4882a593Smuzhiyun 		/* If the reset failed, hub_wq will unbind drivers later */
6063*4882a593Smuzhiyun 		if (ret == 0)
6064*4882a593Smuzhiyun 			usb_unbind_and_rebind_marked_interfaces(udev);
6065*4882a593Smuzhiyun 	}
6066*4882a593Smuzhiyun 
6067*4882a593Smuzhiyun 	usb_autosuspend_device(udev);
6068*4882a593Smuzhiyun 	memalloc_noio_restore(noio_flag);
6069*4882a593Smuzhiyun 	return ret;
6070*4882a593Smuzhiyun }
6071*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_reset_device);
6072*4882a593Smuzhiyun 
6073*4882a593Smuzhiyun 
6074*4882a593Smuzhiyun /**
6075*4882a593Smuzhiyun  * usb_queue_reset_device - Reset a USB device from an atomic context
6076*4882a593Smuzhiyun  * @iface: USB interface belonging to the device to reset
6077*4882a593Smuzhiyun  *
6078*4882a593Smuzhiyun  * This function can be used to reset a USB device from an atomic
6079*4882a593Smuzhiyun  * context, where usb_reset_device() won't work (as it blocks).
6080*4882a593Smuzhiyun  *
6081*4882a593Smuzhiyun  * Doing a reset via this method is functionally equivalent to calling
6082*4882a593Smuzhiyun  * usb_reset_device(), except for the fact that it is delayed to a
6083*4882a593Smuzhiyun  * workqueue. This means that any drivers bound to other interfaces
6084*4882a593Smuzhiyun  * might be unbound, as well as users from usbfs in user space.
6085*4882a593Smuzhiyun  *
6086*4882a593Smuzhiyun  * Corner cases:
6087*4882a593Smuzhiyun  *
6088*4882a593Smuzhiyun  * - Scheduling two resets at the same time from two different drivers
6089*4882a593Smuzhiyun  *   attached to two different interfaces of the same device is
6090*4882a593Smuzhiyun  *   possible; depending on how the driver attached to each interface
6091*4882a593Smuzhiyun  *   handles ->pre_reset(), the second reset might happen or not.
6092*4882a593Smuzhiyun  *
6093*4882a593Smuzhiyun  * - If the reset is delayed so long that the interface is unbound from
6094*4882a593Smuzhiyun  *   its driver, the reset will be skipped.
6095*4882a593Smuzhiyun  *
6096*4882a593Smuzhiyun  * - This function can be called during .probe().  It can also be called
6097*4882a593Smuzhiyun  *   during .disconnect(), but doing so is pointless because the reset
6098*4882a593Smuzhiyun  *   will not occur.  If you really want to reset the device during
6099*4882a593Smuzhiyun  *   .disconnect(), call usb_reset_device() directly -- but watch out
6100*4882a593Smuzhiyun  *   for nested unbinding issues!
6101*4882a593Smuzhiyun  */
usb_queue_reset_device(struct usb_interface * iface)6102*4882a593Smuzhiyun void usb_queue_reset_device(struct usb_interface *iface)
6103*4882a593Smuzhiyun {
6104*4882a593Smuzhiyun 	if (schedule_work(&iface->reset_ws))
6105*4882a593Smuzhiyun 		usb_get_intf(iface);
6106*4882a593Smuzhiyun }
6107*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_queue_reset_device);
6108*4882a593Smuzhiyun 
6109*4882a593Smuzhiyun /**
6110*4882a593Smuzhiyun  * usb_hub_find_child - Get the pointer of child device
6111*4882a593Smuzhiyun  * attached to the port which is specified by @port1.
6112*4882a593Smuzhiyun  * @hdev: USB device belonging to the usb hub
6113*4882a593Smuzhiyun  * @port1: port num to indicate which port the child device
6114*4882a593Smuzhiyun  *	is attached to.
6115*4882a593Smuzhiyun  *
6116*4882a593Smuzhiyun  * USB drivers call this function to get hub's child device
6117*4882a593Smuzhiyun  * pointer.
6118*4882a593Smuzhiyun  *
6119*4882a593Smuzhiyun  * Return: %NULL if input param is invalid and
6120*4882a593Smuzhiyun  * child's usb_device pointer if non-NULL.
6121*4882a593Smuzhiyun  */
usb_hub_find_child(struct usb_device * hdev,int port1)6122*4882a593Smuzhiyun struct usb_device *usb_hub_find_child(struct usb_device *hdev,
6123*4882a593Smuzhiyun 		int port1)
6124*4882a593Smuzhiyun {
6125*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6126*4882a593Smuzhiyun 
6127*4882a593Smuzhiyun 	if (port1 < 1 || port1 > hdev->maxchild)
6128*4882a593Smuzhiyun 		return NULL;
6129*4882a593Smuzhiyun 	return hub->ports[port1 - 1]->child;
6130*4882a593Smuzhiyun }
6131*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hub_find_child);
6132*4882a593Smuzhiyun 
usb_hub_adjust_deviceremovable(struct usb_device * hdev,struct usb_hub_descriptor * desc)6133*4882a593Smuzhiyun void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
6134*4882a593Smuzhiyun 		struct usb_hub_descriptor *desc)
6135*4882a593Smuzhiyun {
6136*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6137*4882a593Smuzhiyun 	enum usb_port_connect_type connect_type;
6138*4882a593Smuzhiyun 	int i;
6139*4882a593Smuzhiyun 
6140*4882a593Smuzhiyun 	if (!hub)
6141*4882a593Smuzhiyun 		return;
6142*4882a593Smuzhiyun 
6143*4882a593Smuzhiyun 	if (!hub_is_superspeed(hdev)) {
6144*4882a593Smuzhiyun 		for (i = 1; i <= hdev->maxchild; i++) {
6145*4882a593Smuzhiyun 			struct usb_port *port_dev = hub->ports[i - 1];
6146*4882a593Smuzhiyun 
6147*4882a593Smuzhiyun 			connect_type = port_dev->connect_type;
6148*4882a593Smuzhiyun 			if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
6149*4882a593Smuzhiyun 				u8 mask = 1 << (i%8);
6150*4882a593Smuzhiyun 
6151*4882a593Smuzhiyun 				if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
6152*4882a593Smuzhiyun 					dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
6153*4882a593Smuzhiyun 					desc->u.hs.DeviceRemovable[i/8]	|= mask;
6154*4882a593Smuzhiyun 				}
6155*4882a593Smuzhiyun 			}
6156*4882a593Smuzhiyun 		}
6157*4882a593Smuzhiyun 	} else {
6158*4882a593Smuzhiyun 		u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
6159*4882a593Smuzhiyun 
6160*4882a593Smuzhiyun 		for (i = 1; i <= hdev->maxchild; i++) {
6161*4882a593Smuzhiyun 			struct usb_port *port_dev = hub->ports[i - 1];
6162*4882a593Smuzhiyun 
6163*4882a593Smuzhiyun 			connect_type = port_dev->connect_type;
6164*4882a593Smuzhiyun 			if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
6165*4882a593Smuzhiyun 				u16 mask = 1 << i;
6166*4882a593Smuzhiyun 
6167*4882a593Smuzhiyun 				if (!(port_removable & mask)) {
6168*4882a593Smuzhiyun 					dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
6169*4882a593Smuzhiyun 					port_removable |= mask;
6170*4882a593Smuzhiyun 				}
6171*4882a593Smuzhiyun 			}
6172*4882a593Smuzhiyun 		}
6173*4882a593Smuzhiyun 
6174*4882a593Smuzhiyun 		desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
6175*4882a593Smuzhiyun 	}
6176*4882a593Smuzhiyun }
6177*4882a593Smuzhiyun 
6178*4882a593Smuzhiyun #ifdef CONFIG_ACPI
6179*4882a593Smuzhiyun /**
6180*4882a593Smuzhiyun  * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
6181*4882a593Smuzhiyun  * @hdev: USB device belonging to the usb hub
6182*4882a593Smuzhiyun  * @port1: port num of the port
6183*4882a593Smuzhiyun  *
6184*4882a593Smuzhiyun  * Return: Port's acpi handle if successful, %NULL if params are
6185*4882a593Smuzhiyun  * invalid.
6186*4882a593Smuzhiyun  */
usb_get_hub_port_acpi_handle(struct usb_device * hdev,int port1)6187*4882a593Smuzhiyun acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
6188*4882a593Smuzhiyun 	int port1)
6189*4882a593Smuzhiyun {
6190*4882a593Smuzhiyun 	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6191*4882a593Smuzhiyun 
6192*4882a593Smuzhiyun 	if (!hub)
6193*4882a593Smuzhiyun 		return NULL;
6194*4882a593Smuzhiyun 
6195*4882a593Smuzhiyun 	return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
6196*4882a593Smuzhiyun }
6197*4882a593Smuzhiyun #endif
6198