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