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