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