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