1 // SPDX-License-Identifier: GPL-2.0
5 * (C) Copyright 1999 Linus Torvalds
6 * (C) Copyright 1999 Johannes Erdfelt
7 * (C) Copyright 1999 Gregory P. Smith
8 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
10 * Released under the GPLv2 only.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/completion.h>
18 #include <linux/sched/mm.h>
19 #include <linux/list.h>
20 #include <linux/slab.h>
21 #include <linux/kcov.h>
22 #include <linux/ioctl.h>
23 #include <linux/usb.h>
24 #include <linux/usbdevice_fs.h>
25 #include <linux/usb/hcd.h>
26 #include <linux/usb/otg.h>
27 #include <linux/usb/quirks.h>
28 #include <linux/workqueue.h>
29 #include <linux/mutex.h>
30 #include <linux/random.h>
31 #include <linux/pm_qos.h>
32 #include <linux/kobject.h>
34 #include <linux/bitfield.h>
35 #include <linux/uaccess.h>
36 #include <asm/byteorder.h>
39 #include "otg_productlist.h"
41 #define USB_VENDOR_GENESYS_LOGIC 0x05e3
42 #define USB_VENDOR_SMSC 0x0424
43 #define USB_PRODUCT_USB5534B 0x5534
44 #define USB_VENDOR_CYPRESS 0x04b4
45 #define USB_PRODUCT_CY7C65632 0x6570
46 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
47 #define HUB_QUIRK_DISABLE_AUTOSUSPEND 0x02
49 #define USB_TP_TRANSMISSION_DELAY 40 /* ns */
50 #define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */
52 /* Protect struct usb_device->state and ->children members
53 * Note: Both are also protected by ->dev.sem, except that ->state can
54 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
55 static DEFINE_SPINLOCK(device_state_lock);
57 /* workqueue to process hub events */
58 static struct workqueue_struct *hub_wq;
59 static void hub_event(struct work_struct *work);
61 /* synchronize hub-port add/remove and peering operations */
62 DEFINE_MUTEX(usb_port_peer_mutex);
64 /* cycle leds on hubs that aren't blinking for attention */
65 static bool blinkenlights;
66 module_param(blinkenlights, bool, S_IRUGO);
67 MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
70 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
71 * 10 seconds to send reply for the initial 64-byte descriptor request.
73 /* define initial 64-byte descriptor request timeout in milliseconds */
74 static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
75 module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
76 MODULE_PARM_DESC(initial_descriptor_timeout,
77 "initial 64-byte descriptor request timeout in milliseconds "
78 "(default 5000 - 5.0 seconds)");
81 * As of 2.6.10 we introduce a new USB device initialization scheme which
82 * closely resembles the way Windows works. Hopefully it will be compatible
83 * with a wider range of devices than the old scheme. However some previously
84 * working devices may start giving rise to "device not accepting address"
85 * errors; if that happens the user can try the old scheme by adjusting the
86 * following module parameters.
88 * For maximum flexibility there are two boolean parameters to control the
89 * hub driver's behavior. On the first initialization attempt, if the
90 * "old_scheme_first" parameter is set then the old scheme will be used,
91 * otherwise the new scheme is used. If that fails and "use_both_schemes"
92 * is set, then the driver will make another attempt, using the other scheme.
94 static bool old_scheme_first;
95 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
96 MODULE_PARM_DESC(old_scheme_first,
97 "start with the old device initialization scheme");
99 static bool use_both_schemes = true;
100 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
101 MODULE_PARM_DESC(use_both_schemes,
102 "try the other device initialization scheme if the "
105 /* Mutual exclusion for EHCI CF initialization. This interferes with
106 * port reset on some companion controllers.
108 DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
109 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
111 #define HUB_DEBOUNCE_TIMEOUT 2000
112 #define HUB_DEBOUNCE_STEP 25
113 #define HUB_DEBOUNCE_STABLE 100
115 static void hub_release(struct kref *kref);
116 static int usb_reset_and_verify_device(struct usb_device *udev);
117 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
118 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
121 static inline char *portspeed(struct usb_hub *hub, int portstatus)
123 if (hub_is_superspeedplus(hub->hdev))
125 if (hub_is_superspeed(hub->hdev))
127 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
129 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
135 /* Note that hdev or one of its children must be locked! */
136 struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
138 if (!hdev || !hdev->actconfig || !hdev->maxchild)
140 return usb_get_intfdata(hdev->actconfig->interface[0]);
143 int usb_device_supports_lpm(struct usb_device *udev)
145 /* Some devices have trouble with LPM */
146 if (udev->quirks & USB_QUIRK_NO_LPM)
149 /* USB 2.1 (and greater) devices indicate LPM support through
150 * their USB 2.0 Extended Capabilities BOS descriptor.
152 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
153 if (udev->bos->ext_cap &&
155 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
161 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
162 * However, there are some that don't, and they set the U1/U2 exit
165 if (!udev->bos->ss_cap) {
166 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
170 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
171 udev->bos->ss_cap->bU2DevExitLat == 0) {
173 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
175 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
179 if (!udev->parent || udev->parent->lpm_capable)
185 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
188 static void usb_set_lpm_mel(struct usb_device *udev,
189 struct usb3_lpm_parameters *udev_lpm_params,
190 unsigned int udev_exit_latency,
192 struct usb3_lpm_parameters *hub_lpm_params,
193 unsigned int hub_exit_latency)
195 unsigned int total_mel;
196 unsigned int device_mel;
197 unsigned int hub_mel;
200 * Calculate the time it takes to transition all links from the roothub
201 * to the parent hub into U0. The parent hub must then decode the
202 * packet (hub header decode latency) to figure out which port it was
205 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
206 * means 0.1us). Multiply that by 100 to get nanoseconds.
208 total_mel = hub_lpm_params->mel +
209 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
212 * How long will it take to transition the downstream hub's port into
213 * U0? The greater of either the hub exit latency or the device exit
216 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
217 * Multiply that by 1000 to get nanoseconds.
219 device_mel = udev_exit_latency * 1000;
220 hub_mel = hub_exit_latency * 1000;
221 if (device_mel > hub_mel)
222 total_mel += device_mel;
224 total_mel += hub_mel;
226 udev_lpm_params->mel = total_mel;
230 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
231 * a transition from either U1 or U2.
233 static void usb_set_lpm_pel(struct usb_device *udev,
234 struct usb3_lpm_parameters *udev_lpm_params,
235 unsigned int udev_exit_latency,
237 struct usb3_lpm_parameters *hub_lpm_params,
238 unsigned int hub_exit_latency,
239 unsigned int port_to_port_exit_latency)
241 unsigned int first_link_pel;
242 unsigned int hub_pel;
245 * First, the device sends an LFPS to transition the link between the
246 * device and the parent hub into U0. The exit latency is the bigger of
247 * the device exit latency or the hub exit latency.
249 if (udev_exit_latency > hub_exit_latency)
250 first_link_pel = udev_exit_latency * 1000;
252 first_link_pel = hub_exit_latency * 1000;
255 * When the hub starts to receive the LFPS, there is a slight delay for
256 * it to figure out that one of the ports is sending an LFPS. Then it
257 * will forward the LFPS to its upstream link. The exit latency is the
258 * delay, plus the PEL that we calculated for this hub.
260 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
263 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
264 * is the greater of the two exit latencies.
266 if (first_link_pel > hub_pel)
267 udev_lpm_params->pel = first_link_pel;
269 udev_lpm_params->pel = hub_pel;
273 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
274 * when a device initiates a transition to U0, until when it will receive the
275 * first packet from the host controller.
277 * Section C.1.5.1 describes the four components to this:
279 * - t2: time for the ERDY to make it from the device to the host.
280 * - t3: a host-specific delay to process the ERDY.
281 * - t4: time for the packet to make it from the host to the device.
283 * t3 is specific to both the xHCI host and the platform the host is integrated
284 * into. The Intel HW folks have said it's negligible, FIXME if a different
285 * vendor says otherwise.
287 static void usb_set_lpm_sel(struct usb_device *udev,
288 struct usb3_lpm_parameters *udev_lpm_params)
290 struct usb_device *parent;
291 unsigned int num_hubs;
292 unsigned int total_sel;
294 /* t1 = device PEL */
295 total_sel = udev_lpm_params->pel;
296 /* How many external hubs are in between the device & the root port. */
297 for (parent = udev->parent, num_hubs = 0; parent->parent;
298 parent = parent->parent)
300 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
302 total_sel += 2100 + 250 * (num_hubs - 1);
304 /* t4 = 250ns * num_hubs */
305 total_sel += 250 * num_hubs;
307 udev_lpm_params->sel = total_sel;
310 static void usb_set_lpm_parameters(struct usb_device *udev)
313 unsigned int port_to_port_delay;
314 unsigned int udev_u1_del;
315 unsigned int udev_u2_del;
316 unsigned int hub_u1_del;
317 unsigned int hub_u2_del;
319 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
322 hub = usb_hub_to_struct_hub(udev->parent);
323 /* It doesn't take time to transition the roothub into U0, since it
324 * doesn't have an upstream link.
329 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
330 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
331 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
332 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
334 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
335 hub, &udev->parent->u1_params, hub_u1_del);
337 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
338 hub, &udev->parent->u2_params, hub_u2_del);
341 * Appendix C, section C.2.2.2, says that there is a slight delay from
342 * when the parent hub notices the downstream port is trying to
343 * transition to U0 to when the hub initiates a U0 transition on its
344 * upstream port. The section says the delays are tPort2PortU1EL and
345 * tPort2PortU2EL, but it doesn't define what they are.
347 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
348 * about the same delays. Use the maximum delay calculations from those
349 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
350 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
351 * assume the device exit latencies they are talking about are the hub
354 * What do we do if the U2 exit latency is less than the U1 exit
355 * latency? It's possible, although not likely...
357 port_to_port_delay = 1;
359 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
360 hub, &udev->parent->u1_params, hub_u1_del,
363 if (hub_u2_del > hub_u1_del)
364 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
366 port_to_port_delay = 1 + hub_u1_del;
368 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
369 hub, &udev->parent->u2_params, hub_u2_del,
372 /* Now that we've got PEL, calculate SEL. */
373 usb_set_lpm_sel(udev, &udev->u1_params);
374 usb_set_lpm_sel(udev, &udev->u2_params);
377 /* USB 2.0 spec Section 11.24.4.5 */
378 static int get_hub_descriptor(struct usb_device *hdev,
379 struct usb_hub_descriptor *desc)
384 if (hub_is_superspeed(hdev)) {
385 dtype = USB_DT_SS_HUB;
386 size = USB_DT_SS_HUB_SIZE;
389 size = sizeof(struct usb_hub_descriptor);
392 for (i = 0; i < 3; i++) {
393 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
394 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
395 dtype << 8, 0, desc, size,
396 USB_CTRL_GET_TIMEOUT);
397 if (hub_is_superspeed(hdev)) {
400 } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) {
401 /* Make sure we have the DeviceRemovable field. */
402 size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1;
412 * USB 2.0 spec Section 11.24.2.1
414 static int clear_hub_feature(struct usb_device *hdev, int feature)
416 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
417 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
421 * USB 2.0 spec Section 11.24.2.2
423 int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
425 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
426 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
431 * USB 2.0 spec Section 11.24.2.13
433 static int set_port_feature(struct usb_device *hdev, int port1, int feature)
435 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
436 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
440 static char *to_led_name(int selector)
457 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
458 * for info about using port indicators
460 static void set_port_led(struct usb_hub *hub, int port1, int selector)
462 struct usb_port *port_dev = hub->ports[port1 - 1];
465 status = set_port_feature(hub->hdev, (selector << 8) | port1,
466 USB_PORT_FEAT_INDICATOR);
467 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
468 to_led_name(selector), status);
471 #define LED_CYCLE_PERIOD ((2*HZ)/3)
473 static void led_work(struct work_struct *work)
475 struct usb_hub *hub =
476 container_of(work, struct usb_hub, leds.work);
477 struct usb_device *hdev = hub->hdev;
479 unsigned changed = 0;
482 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
485 for (i = 0; i < hdev->maxchild; i++) {
486 unsigned selector, mode;
488 /* 30%-50% duty cycle */
490 switch (hub->indicator[i]) {
492 case INDICATOR_CYCLE:
494 selector = HUB_LED_AUTO;
495 mode = INDICATOR_AUTO;
497 /* blinking green = sw attention */
498 case INDICATOR_GREEN_BLINK:
499 selector = HUB_LED_GREEN;
500 mode = INDICATOR_GREEN_BLINK_OFF;
502 case INDICATOR_GREEN_BLINK_OFF:
503 selector = HUB_LED_OFF;
504 mode = INDICATOR_GREEN_BLINK;
506 /* blinking amber = hw attention */
507 case INDICATOR_AMBER_BLINK:
508 selector = HUB_LED_AMBER;
509 mode = INDICATOR_AMBER_BLINK_OFF;
511 case INDICATOR_AMBER_BLINK_OFF:
512 selector = HUB_LED_OFF;
513 mode = INDICATOR_AMBER_BLINK;
515 /* blink green/amber = reserved */
516 case INDICATOR_ALT_BLINK:
517 selector = HUB_LED_GREEN;
518 mode = INDICATOR_ALT_BLINK_OFF;
520 case INDICATOR_ALT_BLINK_OFF:
521 selector = HUB_LED_AMBER;
522 mode = INDICATOR_ALT_BLINK;
527 if (selector != HUB_LED_AUTO)
529 set_port_led(hub, i + 1, selector);
530 hub->indicator[i] = mode;
532 if (!changed && blinkenlights) {
534 cursor %= hdev->maxchild;
535 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
536 hub->indicator[cursor] = INDICATOR_CYCLE;
540 queue_delayed_work(system_power_efficient_wq,
541 &hub->leds, LED_CYCLE_PERIOD);
544 /* use a short timeout for hub/port status fetches */
545 #define USB_STS_TIMEOUT 1000
546 #define USB_STS_RETRIES 5
549 * USB 2.0 spec Section 11.24.2.6
551 static int get_hub_status(struct usb_device *hdev,
552 struct usb_hub_status *data)
554 int i, status = -ETIMEDOUT;
556 for (i = 0; i < USB_STS_RETRIES &&
557 (status == -ETIMEDOUT || status == -EPIPE); i++) {
558 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
559 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
560 data, sizeof(*data), USB_STS_TIMEOUT);
566 * USB 2.0 spec Section 11.24.2.7
567 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
569 static int get_port_status(struct usb_device *hdev, int port1,
570 void *data, u16 value, u16 length)
572 int i, status = -ETIMEDOUT;
574 for (i = 0; i < USB_STS_RETRIES &&
575 (status == -ETIMEDOUT || status == -EPIPE); i++) {
576 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
577 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
578 port1, data, length, USB_STS_TIMEOUT);
583 static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
584 u16 *status, u16 *change, u32 *ext_status)
589 if (type != HUB_PORT_STATUS)
592 mutex_lock(&hub->status_mutex);
593 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
596 dev_err(hub->intfdev,
597 "%s failed (err = %d)\n", __func__, ret);
601 *status = le16_to_cpu(hub->status->port.wPortStatus);
602 *change = le16_to_cpu(hub->status->port.wPortChange);
603 if (type != HUB_PORT_STATUS && ext_status)
604 *ext_status = le32_to_cpu(
605 hub->status->port.dwExtPortStatus);
608 mutex_unlock(&hub->status_mutex);
612 static int hub_port_status(struct usb_hub *hub, int port1,
613 u16 *status, u16 *change)
615 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
616 status, change, NULL);
619 static void hub_resubmit_irq_urb(struct usb_hub *hub)
624 spin_lock_irqsave(&hub->irq_urb_lock, flags);
626 if (hub->quiescing) {
627 spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
631 status = usb_submit_urb(hub->urb, GFP_ATOMIC);
632 if (status && status != -ENODEV && status != -EPERM &&
633 status != -ESHUTDOWN) {
634 dev_err(hub->intfdev, "resubmit --> %d\n", status);
635 mod_timer(&hub->irq_urb_retry, jiffies + HZ);
638 spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
641 static void hub_retry_irq_urb(struct timer_list *t)
643 struct usb_hub *hub = from_timer(hub, t, irq_urb_retry);
645 hub_resubmit_irq_urb(hub);
649 static void kick_hub_wq(struct usb_hub *hub)
651 struct usb_interface *intf;
653 if (hub->disconnected || work_pending(&hub->events))
657 * Suppress autosuspend until the event is proceed.
659 * Be careful and make sure that the symmetric operation is
660 * always called. We are here only when there is no pending
661 * work for this hub. Therefore put the interface either when
662 * the new work is called or when it is canceled.
664 intf = to_usb_interface(hub->intfdev);
665 usb_autopm_get_interface_no_resume(intf);
666 kref_get(&hub->kref);
668 if (queue_work(hub_wq, &hub->events))
671 /* the work has already been scheduled */
672 usb_autopm_put_interface_async(intf);
673 kref_put(&hub->kref, hub_release);
676 void usb_kick_hub_wq(struct usb_device *hdev)
678 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
685 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
686 * Notification, which indicates it had initiated remote wakeup.
688 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
689 * device initiates resume, so the USB core will not receive notice of the
690 * resume through the normal hub interrupt URB.
692 void usb_wakeup_notification(struct usb_device *hdev,
693 unsigned int portnum)
696 struct usb_port *port_dev;
701 hub = usb_hub_to_struct_hub(hdev);
703 port_dev = hub->ports[portnum - 1];
704 if (port_dev && port_dev->child)
705 pm_wakeup_event(&port_dev->child->dev, 0);
707 set_bit(portnum, hub->wakeup_bits);
711 EXPORT_SYMBOL_GPL(usb_wakeup_notification);
713 /* completion function, fires on port status changes and various faults */
714 static void hub_irq(struct urb *urb)
716 struct usb_hub *hub = urb->context;
717 int status = urb->status;
722 case -ENOENT: /* synchronous unlink */
723 case -ECONNRESET: /* async unlink */
724 case -ESHUTDOWN: /* hardware going away */
727 default: /* presumably an error */
728 /* Cause a hub reset after 10 consecutive errors */
729 dev_dbg(hub->intfdev, "transfer --> %d\n", status);
730 if ((++hub->nerrors < 10) || hub->error)
735 /* let hub_wq handle things */
736 case 0: /* we got data: port status changed */
738 for (i = 0; i < urb->actual_length; ++i)
739 bits |= ((unsigned long) ((*hub->buffer)[i]))
741 hub->event_bits[0] = bits;
747 /* Something happened, let hub_wq figure it out */
751 hub_resubmit_irq_urb(hub);
754 /* USB 2.0 spec Section 11.24.2.3 */
756 hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
758 /* Need to clear both directions for control ep */
759 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
760 USB_ENDPOINT_XFER_CONTROL) {
761 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
762 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
763 devinfo ^ 0x8000, tt, NULL, 0, 1000);
767 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
768 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
773 * enumeration blocks hub_wq for a long time. we use keventd instead, since
774 * long blocking there is the exception, not the rule. accordingly, HCDs
775 * talking to TTs must queue control transfers (not just bulk and iso), so
776 * both can talk to the same hub concurrently.
778 static void hub_tt_work(struct work_struct *work)
780 struct usb_hub *hub =
781 container_of(work, struct usb_hub, tt.clear_work);
784 spin_lock_irqsave(&hub->tt.lock, flags);
785 while (!list_empty(&hub->tt.clear_list)) {
786 struct list_head *next;
787 struct usb_tt_clear *clear;
788 struct usb_device *hdev = hub->hdev;
789 const struct hc_driver *drv;
792 next = hub->tt.clear_list.next;
793 clear = list_entry(next, struct usb_tt_clear, clear_list);
794 list_del(&clear->clear_list);
796 /* drop lock so HCD can concurrently report other TT errors */
797 spin_unlock_irqrestore(&hub->tt.lock, flags);
798 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
799 if (status && status != -ENODEV)
801 "clear tt %d (%04x) error %d\n",
802 clear->tt, clear->devinfo, status);
804 /* Tell the HCD, even if the operation failed */
805 drv = clear->hcd->driver;
806 if (drv->clear_tt_buffer_complete)
807 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
810 spin_lock_irqsave(&hub->tt.lock, flags);
812 spin_unlock_irqrestore(&hub->tt.lock, flags);
816 * usb_hub_set_port_power - control hub port's power state
817 * @hdev: USB device belonging to the usb hub
820 * @set: expected status
822 * call this function to control port's power via setting or
823 * clearing the port's PORT_POWER feature.
825 * Return: 0 if successful. A negative error code otherwise.
827 int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
833 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
835 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
841 set_bit(port1, hub->power_bits);
843 clear_bit(port1, hub->power_bits);
848 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
849 * @urb: an URB associated with the failed or incomplete split transaction
851 * High speed HCDs use this to tell the hub driver that some split control or
852 * bulk transaction failed in a way that requires clearing internal state of
853 * a transaction translator. This is normally detected (and reported) from
856 * It may not be possible for that hub to handle additional full (or low)
857 * speed transactions until that state is fully cleared out.
859 * Return: 0 if successful. A negative error code otherwise.
861 int usb_hub_clear_tt_buffer(struct urb *urb)
863 struct usb_device *udev = urb->dev;
864 int pipe = urb->pipe;
865 struct usb_tt *tt = udev->tt;
867 struct usb_tt_clear *clear;
869 /* we've got to cope with an arbitrary number of pending TT clears,
870 * since each TT has "at least two" buffers that can need it (and
871 * there can be many TTs per hub). even if they're uncommon.
873 clear = kmalloc(sizeof *clear, GFP_ATOMIC);
875 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
876 /* FIXME recover somehow ... RESET_TT? */
880 /* info that CLEAR_TT_BUFFER needs */
881 clear->tt = tt->multi ? udev->ttport : 1;
882 clear->devinfo = usb_pipeendpoint (pipe);
883 clear->devinfo |= ((u16)udev->devaddr) << 4;
884 clear->devinfo |= usb_pipecontrol(pipe)
885 ? (USB_ENDPOINT_XFER_CONTROL << 11)
886 : (USB_ENDPOINT_XFER_BULK << 11);
887 if (usb_pipein(pipe))
888 clear->devinfo |= 1 << 15;
890 /* info for completion callback */
891 clear->hcd = bus_to_hcd(udev->bus);
894 /* tell keventd to clear state for this TT */
895 spin_lock_irqsave(&tt->lock, flags);
896 list_add_tail(&clear->clear_list, &tt->clear_list);
897 schedule_work(&tt->clear_work);
898 spin_unlock_irqrestore(&tt->lock, flags);
901 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
903 static void hub_power_on(struct usb_hub *hub, bool do_delay)
907 /* Enable power on each port. Some hubs have reserved values
908 * of LPSM (> 2) in their descriptors, even though they are
909 * USB 2.0 hubs. Some hubs do not implement port-power switching
910 * but only emulate it. In all cases, the ports won't work
911 * unless we send these messages to the hub.
913 if (hub_is_port_power_switchable(hub))
914 dev_dbg(hub->intfdev, "enabling power on all ports\n");
916 dev_dbg(hub->intfdev, "trying to enable port power on "
917 "non-switchable hub\n");
918 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
919 if (test_bit(port1, hub->power_bits))
920 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
922 usb_clear_port_feature(hub->hdev, port1,
923 USB_PORT_FEAT_POWER);
925 msleep(hub_power_on_good_delay(hub));
928 static int hub_hub_status(struct usb_hub *hub,
929 u16 *status, u16 *change)
933 mutex_lock(&hub->status_mutex);
934 ret = get_hub_status(hub->hdev, &hub->status->hub);
937 dev_err(hub->intfdev,
938 "%s failed (err = %d)\n", __func__, ret);
940 *status = le16_to_cpu(hub->status->hub.wHubStatus);
941 *change = le16_to_cpu(hub->status->hub.wHubChange);
944 mutex_unlock(&hub->status_mutex);
948 static int hub_set_port_link_state(struct usb_hub *hub, int port1,
949 unsigned int link_status)
951 return set_port_feature(hub->hdev,
952 port1 | (link_status << 3),
953 USB_PORT_FEAT_LINK_STATE);
957 * Disable a port and mark a logical connect-change event, so that some
958 * time later hub_wq will disconnect() any existing usb_device on the port
959 * and will re-enumerate if there actually is a device attached.
961 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
963 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
964 hub_port_disable(hub, port1, 1);
966 /* FIXME let caller ask to power down the port:
967 * - some devices won't enumerate without a VBUS power cycle
968 * - SRP saves power that way
969 * - ... new call, TBD ...
970 * That's easy if this hub can switch power per-port, and
971 * hub_wq reactivates the port later (timer, SRP, etc).
972 * Powerdown must be optional, because of reset/DFU.
975 set_bit(port1, hub->change_bits);
980 * usb_remove_device - disable a device's port on its parent hub
981 * @udev: device to be disabled and removed
982 * Context: @udev locked, must be able to sleep.
984 * After @udev's port has been disabled, hub_wq is notified and it will
985 * see that the device has been disconnected. When the device is
986 * physically unplugged and something is plugged in, the events will
987 * be received and processed normally.
989 * Return: 0 if successful. A negative error code otherwise.
991 int usb_remove_device(struct usb_device *udev)
994 struct usb_interface *intf;
997 if (!udev->parent) /* Can't remove a root hub */
999 hub = usb_hub_to_struct_hub(udev->parent);
1000 intf = to_usb_interface(hub->intfdev);
1002 ret = usb_autopm_get_interface(intf);
1006 set_bit(udev->portnum, hub->removed_bits);
1007 hub_port_logical_disconnect(hub, udev->portnum);
1008 usb_autopm_put_interface(intf);
1012 enum hub_activation_type {
1013 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
1014 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
1017 static void hub_init_func2(struct work_struct *ws);
1018 static void hub_init_func3(struct work_struct *ws);
1020 static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1022 struct usb_device *hdev = hub->hdev;
1023 struct usb_hcd *hcd;
1027 bool need_debounce_delay = false;
1030 /* Continue a partial initialization */
1031 if (type == HUB_INIT2 || type == HUB_INIT3) {
1032 device_lock(&hdev->dev);
1034 /* Was the hub disconnected while we were waiting? */
1035 if (hub->disconnected)
1037 if (type == HUB_INIT2)
1041 kref_get(&hub->kref);
1043 /* The superspeed hub except for root hub has to use Hub Depth
1044 * value as an offset into the route string to locate the bits
1045 * it uses to determine the downstream port number. So hub driver
1046 * should send a set hub depth request to superspeed hub after
1047 * the superspeed hub is set configuration in initialization or
1050 * After a resume, port power should still be on.
1051 * For any other type of activation, turn it on.
1053 if (type != HUB_RESUME) {
1054 if (hdev->parent && hub_is_superspeed(hdev)) {
1055 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1056 HUB_SET_DEPTH, USB_RT_HUB,
1057 hdev->level - 1, 0, NULL, 0,
1058 USB_CTRL_SET_TIMEOUT);
1060 dev_err(hub->intfdev,
1061 "set hub depth failed\n");
1064 /* Speed up system boot by using a delayed_work for the
1065 * hub's initial power-up delays. This is pretty awkward
1066 * and the implementation looks like a home-brewed sort of
1067 * setjmp/longjmp, but it saves at least 100 ms for each
1068 * root hub (assuming usbcore is compiled into the kernel
1069 * rather than as a module). It adds up.
1071 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1072 * because for those activation types the ports have to be
1073 * operational when we return. In theory this could be done
1074 * for HUB_POST_RESET, but it's easier not to.
1076 if (type == HUB_INIT) {
1077 delay = hub_power_on_good_delay(hub);
1079 hub_power_on(hub, false);
1080 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
1081 queue_delayed_work(system_power_efficient_wq,
1083 msecs_to_jiffies(delay));
1085 /* Suppress autosuspend until init is done */
1086 usb_autopm_get_interface_no_resume(
1087 to_usb_interface(hub->intfdev));
1088 return; /* Continues at init2: below */
1089 } else if (type == HUB_RESET_RESUME) {
1090 /* The internal host controller state for the hub device
1091 * may be gone after a host power loss on system resume.
1092 * Update the device's info so the HW knows it's a hub.
1094 hcd = bus_to_hcd(hdev->bus);
1095 if (hcd->driver->update_hub_device) {
1096 ret = hcd->driver->update_hub_device(hcd, hdev,
1097 &hub->tt, GFP_NOIO);
1099 dev_err(hub->intfdev,
1100 "Host not accepting hub info update\n");
1101 dev_err(hub->intfdev,
1102 "LS/FS devices and hubs may not work under this hub\n");
1105 hub_power_on(hub, true);
1107 hub_power_on(hub, true);
1113 * Check each port and set hub->change_bits to let hub_wq know
1114 * which ports need attention.
1116 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1117 struct usb_port *port_dev = hub->ports[port1 - 1];
1118 struct usb_device *udev = port_dev->child;
1119 u16 portstatus, portchange;
1121 portstatus = portchange = 0;
1122 status = hub_port_status(hub, port1, &portstatus, &portchange);
1126 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1127 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1128 portstatus, portchange);
1131 * After anything other than HUB_RESUME (i.e., initialization
1132 * or any sort of reset), every port should be disabled.
1133 * Unconnected ports should likewise be disabled (paranoia),
1134 * and so should ports for which we have no usb_device.
1136 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1137 type != HUB_RESUME ||
1138 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1140 udev->state == USB_STATE_NOTATTACHED)) {
1142 * USB3 protocol ports will automatically transition
1143 * to Enabled state when detect an USB3.0 device attach.
1144 * Do not disable USB3 protocol ports, just pretend
1147 portstatus &= ~USB_PORT_STAT_ENABLE;
1148 if (!hub_is_superspeed(hdev))
1149 usb_clear_port_feature(hdev, port1,
1150 USB_PORT_FEAT_ENABLE);
1153 /* Make sure a warm-reset request is handled by port_event */
1154 if (type == HUB_RESUME &&
1155 hub_port_warm_reset_required(hub, port1, portstatus))
1156 set_bit(port1, hub->event_bits);
1159 * Add debounce if USB3 link is in polling/link training state.
1160 * Link will automatically transition to Enabled state after
1161 * link training completes.
1163 if (hub_is_superspeed(hdev) &&
1164 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
1165 USB_SS_PORT_LS_POLLING))
1166 need_debounce_delay = true;
1168 /* Clear status-change flags; we'll debounce later */
1169 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1170 need_debounce_delay = true;
1171 usb_clear_port_feature(hub->hdev, port1,
1172 USB_PORT_FEAT_C_CONNECTION);
1174 if (portchange & USB_PORT_STAT_C_ENABLE) {
1175 need_debounce_delay = true;
1176 usb_clear_port_feature(hub->hdev, port1,
1177 USB_PORT_FEAT_C_ENABLE);
1179 if (portchange & USB_PORT_STAT_C_RESET) {
1180 need_debounce_delay = true;
1181 usb_clear_port_feature(hub->hdev, port1,
1182 USB_PORT_FEAT_C_RESET);
1184 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1185 hub_is_superspeed(hub->hdev)) {
1186 need_debounce_delay = true;
1187 usb_clear_port_feature(hub->hdev, port1,
1188 USB_PORT_FEAT_C_BH_PORT_RESET);
1190 /* We can forget about a "removed" device when there's a
1191 * physical disconnect or the connect status changes.
1193 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1194 (portchange & USB_PORT_STAT_C_CONNECTION))
1195 clear_bit(port1, hub->removed_bits);
1197 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1198 /* Tell hub_wq to disconnect the device or
1199 * check for a new connection or over current condition.
1200 * Based on USB2.0 Spec Section 11.12.5,
1201 * C_PORT_OVER_CURRENT could be set while
1202 * PORT_OVER_CURRENT is not. So check for any of them.
1204 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
1205 (portchange & USB_PORT_STAT_C_CONNECTION) ||
1206 (portstatus & USB_PORT_STAT_OVERCURRENT) ||
1207 (portchange & USB_PORT_STAT_C_OVERCURRENT))
1208 set_bit(port1, hub->change_bits);
1210 } else if (portstatus & USB_PORT_STAT_ENABLE) {
1211 bool port_resumed = (portstatus &
1212 USB_PORT_STAT_LINK_STATE) ==
1214 /* The power session apparently survived the resume.
1215 * If there was an overcurrent or suspend change
1216 * (i.e., remote wakeup request), have hub_wq
1217 * take care of it. Look at the port link state
1218 * for USB 3.0 hubs, since they don't have a suspend
1219 * change bit, and they don't set the port link change
1220 * bit on device-initiated resume.
1222 if (portchange || (hub_is_superspeed(hub->hdev) &&
1224 set_bit(port1, hub->change_bits);
1226 } else if (udev->persist_enabled) {
1228 udev->reset_resume = 1;
1230 /* Don't set the change_bits when the device
1233 if (test_bit(port1, hub->power_bits))
1234 set_bit(port1, hub->change_bits);
1237 /* The power session is gone; tell hub_wq */
1238 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1239 set_bit(port1, hub->change_bits);
1243 /* If no port-status-change flags were set, we don't need any
1244 * debouncing. If flags were set we can try to debounce the
1245 * ports all at once right now, instead of letting hub_wq do them
1246 * one at a time later on.
1248 * If any port-status changes do occur during this delay, hub_wq
1249 * will see them later and handle them normally.
1251 if (need_debounce_delay) {
1252 delay = HUB_DEBOUNCE_STABLE;
1254 /* Don't do a long sleep inside a workqueue routine */
1255 if (type == HUB_INIT2) {
1256 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
1257 queue_delayed_work(system_power_efficient_wq,
1259 msecs_to_jiffies(delay));
1260 device_unlock(&hdev->dev);
1261 return; /* Continues at init3: below */
1269 status = usb_submit_urb(hub->urb, GFP_NOIO);
1271 dev_err(hub->intfdev, "activate --> %d\n", status);
1272 if (hub->has_indicators && blinkenlights)
1273 queue_delayed_work(system_power_efficient_wq,
1274 &hub->leds, LED_CYCLE_PERIOD);
1276 /* Scan all ports that need attention */
1279 if (type == HUB_INIT2 || type == HUB_INIT3) {
1280 /* Allow autosuspend if it was suppressed */
1282 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
1283 device_unlock(&hdev->dev);
1286 kref_put(&hub->kref, hub_release);
1289 /* Implement the continuations for the delays above */
1290 static void hub_init_func2(struct work_struct *ws)
1292 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1294 hub_activate(hub, HUB_INIT2);
1297 static void hub_init_func3(struct work_struct *ws)
1299 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1301 hub_activate(hub, HUB_INIT3);
1304 enum hub_quiescing_type {
1305 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1308 static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1310 struct usb_device *hdev = hub->hdev;
1311 unsigned long flags;
1314 /* hub_wq and related activity won't re-trigger */
1315 spin_lock_irqsave(&hub->irq_urb_lock, flags);
1317 spin_unlock_irqrestore(&hub->irq_urb_lock, flags);
1319 if (type != HUB_SUSPEND) {
1320 /* Disconnect all the children */
1321 for (i = 0; i < hdev->maxchild; ++i) {
1322 if (hub->ports[i]->child)
1323 usb_disconnect(&hub->ports[i]->child);
1327 /* Stop hub_wq and related activity */
1328 del_timer_sync(&hub->irq_urb_retry);
1329 usb_kill_urb(hub->urb);
1330 if (hub->has_indicators)
1331 cancel_delayed_work_sync(&hub->leds);
1333 flush_work(&hub->tt.clear_work);
1336 static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1340 for (i = 0; i < hub->hdev->maxchild; ++i)
1341 pm_runtime_barrier(&hub->ports[i]->dev);
1344 /* caller has locked the hub device */
1345 static int hub_pre_reset(struct usb_interface *intf)
1347 struct usb_hub *hub = usb_get_intfdata(intf);
1349 hub_quiesce(hub, HUB_PRE_RESET);
1351 hub_pm_barrier_for_all_ports(hub);
1355 /* caller has locked the hub device */
1356 static int hub_post_reset(struct usb_interface *intf)
1358 struct usb_hub *hub = usb_get_intfdata(intf);
1361 hub_pm_barrier_for_all_ports(hub);
1362 hub_activate(hub, HUB_POST_RESET);
1366 static int hub_configure(struct usb_hub *hub,
1367 struct usb_endpoint_descriptor *endpoint)
1369 struct usb_hcd *hcd;
1370 struct usb_device *hdev = hub->hdev;
1371 struct device *hub_dev = hub->intfdev;
1372 u16 hubstatus, hubchange;
1373 u16 wHubCharacteristics;
1376 char *message = "out of memory";
1381 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1387 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1392 mutex_init(&hub->status_mutex);
1394 hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1395 if (!hub->descriptor) {
1400 /* Request the entire hub descriptor.
1401 * hub->descriptor can handle USB_MAXCHILDREN ports,
1402 * but a (non-SS) hub can/will return fewer bytes here.
1404 ret = get_hub_descriptor(hdev, hub->descriptor);
1406 message = "can't read hub descriptor";
1410 maxchild = USB_MAXCHILDREN;
1411 if (hub_is_superspeed(hdev))
1412 maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS);
1414 if (hub->descriptor->bNbrPorts > maxchild) {
1415 message = "hub has too many ports!";
1418 } else if (hub->descriptor->bNbrPorts == 0) {
1419 message = "hub doesn't have any ports!";
1425 * Accumulate wHubDelay + 40ns for every hub in the tree of devices.
1426 * The resulting value will be used for SetIsochDelay() request.
1428 if (hub_is_superspeed(hdev) || hub_is_superspeedplus(hdev)) {
1429 u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay);
1432 delay += hdev->parent->hub_delay;
1434 delay += USB_TP_TRANSMISSION_DELAY;
1435 hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX);
1438 maxchild = hub->descriptor->bNbrPorts;
1439 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1440 (maxchild == 1) ? "" : "s");
1442 hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL);
1448 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1449 if (hub_is_superspeed(hdev)) {
1457 /* FIXME for USB 3.0, skip for now */
1458 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1459 !(hub_is_superspeed(hdev))) {
1460 char portstr[USB_MAXCHILDREN + 1];
1462 for (i = 0; i < maxchild; i++)
1463 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1464 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1466 portstr[maxchild] = 0;
1467 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1469 dev_dbg(hub_dev, "standalone hub\n");
1471 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
1472 case HUB_CHAR_COMMON_LPSM:
1473 dev_dbg(hub_dev, "ganged power switching\n");
1475 case HUB_CHAR_INDV_PORT_LPSM:
1476 dev_dbg(hub_dev, "individual port power switching\n");
1478 case HUB_CHAR_NO_LPSM:
1480 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1484 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
1485 case HUB_CHAR_COMMON_OCPM:
1486 dev_dbg(hub_dev, "global over-current protection\n");
1488 case HUB_CHAR_INDV_PORT_OCPM:
1489 dev_dbg(hub_dev, "individual port over-current protection\n");
1491 case HUB_CHAR_NO_OCPM:
1493 dev_dbg(hub_dev, "no over-current protection\n");
1497 spin_lock_init(&hub->tt.lock);
1498 INIT_LIST_HEAD(&hub->tt.clear_list);
1499 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1500 switch (hdev->descriptor.bDeviceProtocol) {
1503 case USB_HUB_PR_HS_SINGLE_TT:
1504 dev_dbg(hub_dev, "Single TT\n");
1507 case USB_HUB_PR_HS_MULTI_TT:
1508 ret = usb_set_interface(hdev, 0, 1);
1510 dev_dbg(hub_dev, "TT per port\n");
1513 dev_err(hub_dev, "Using single TT (err %d)\n",
1518 /* USB 3.0 hubs don't have a TT */
1521 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1522 hdev->descriptor.bDeviceProtocol);
1526 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
1527 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1528 case HUB_TTTT_8_BITS:
1529 if (hdev->descriptor.bDeviceProtocol != 0) {
1530 hub->tt.think_time = 666;
1531 dev_dbg(hub_dev, "TT requires at most %d "
1532 "FS bit times (%d ns)\n",
1533 8, hub->tt.think_time);
1536 case HUB_TTTT_16_BITS:
1537 hub->tt.think_time = 666 * 2;
1538 dev_dbg(hub_dev, "TT requires at most %d "
1539 "FS bit times (%d ns)\n",
1540 16, hub->tt.think_time);
1542 case HUB_TTTT_24_BITS:
1543 hub->tt.think_time = 666 * 3;
1544 dev_dbg(hub_dev, "TT requires at most %d "
1545 "FS bit times (%d ns)\n",
1546 24, hub->tt.think_time);
1548 case HUB_TTTT_32_BITS:
1549 hub->tt.think_time = 666 * 4;
1550 dev_dbg(hub_dev, "TT requires at most %d "
1551 "FS bit times (%d ns)\n",
1552 32, hub->tt.think_time);
1556 /* probe() zeroes hub->indicator[] */
1557 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1558 hub->has_indicators = 1;
1559 dev_dbg(hub_dev, "Port indicators are supported\n");
1562 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1563 hub->descriptor->bPwrOn2PwrGood * 2);
1565 /* power budgeting mostly matters with bus-powered hubs,
1566 * and battery-powered root hubs (may provide just 8 mA).
1568 ret = usb_get_std_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
1570 message = "can't get hub status";
1573 hcd = bus_to_hcd(hdev->bus);
1574 if (hdev == hdev->bus->root_hub) {
1575 if (hcd->power_budget > 0)
1576 hdev->bus_mA = hcd->power_budget;
1578 hdev->bus_mA = full_load * maxchild;
1579 if (hdev->bus_mA >= full_load)
1580 hub->mA_per_port = full_load;
1582 hub->mA_per_port = hdev->bus_mA;
1583 hub->limited_power = 1;
1585 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1586 int remaining = hdev->bus_mA -
1587 hub->descriptor->bHubContrCurrent;
1589 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1590 hub->descriptor->bHubContrCurrent);
1591 hub->limited_power = 1;
1593 if (remaining < maxchild * unit_load)
1595 "insufficient power available "
1596 "to use all downstream ports\n");
1597 hub->mA_per_port = unit_load; /* 7.2.1 */
1599 } else { /* Self-powered external hub */
1600 /* FIXME: What about battery-powered external hubs that
1601 * provide less current per port? */
1602 hub->mA_per_port = full_load;
1604 if (hub->mA_per_port < full_load)
1605 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1608 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1610 message = "can't get hub status";
1614 /* local power status reports aren't always correct */
1615 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1616 dev_dbg(hub_dev, "local power source is %s\n",
1617 (hubstatus & HUB_STATUS_LOCAL_POWER)
1618 ? "lost (inactive)" : "good");
1620 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1621 dev_dbg(hub_dev, "%sover-current condition exists\n",
1622 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1624 /* set up the interrupt endpoint
1625 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1626 * bytes as USB2.0[11.12.3] says because some hubs are known
1627 * to send more data (and thus cause overflow). For root hubs,
1628 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1629 * to be big enough for at least USB_MAXCHILDREN ports. */
1630 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1631 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1633 if (maxp > sizeof(*hub->buffer))
1634 maxp = sizeof(*hub->buffer);
1636 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1642 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1643 hub, endpoint->bInterval);
1645 /* maybe cycle the hub leds */
1646 if (hub->has_indicators && blinkenlights)
1647 hub->indicator[0] = INDICATOR_CYCLE;
1649 mutex_lock(&usb_port_peer_mutex);
1650 for (i = 0; i < maxchild; i++) {
1651 ret = usb_hub_create_port_device(hub, i + 1);
1653 dev_err(hub->intfdev,
1654 "couldn't create port%d device.\n", i + 1);
1659 for (i = 0; i < hdev->maxchild; i++) {
1660 struct usb_port *port_dev = hub->ports[i];
1662 pm_runtime_put(&port_dev->dev);
1665 mutex_unlock(&usb_port_peer_mutex);
1669 /* Update the HCD's internal representation of this hub before hub_wq
1670 * starts getting port status changes for devices under the hub.
1672 if (hcd->driver->update_hub_device) {
1673 ret = hcd->driver->update_hub_device(hcd, hdev,
1674 &hub->tt, GFP_KERNEL);
1676 message = "can't update HCD hub info";
1681 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1683 hub_activate(hub, HUB_INIT);
1687 dev_err(hub_dev, "config failed, %s (err %d)\n",
1689 /* hub_disconnect() frees urb and descriptor */
1693 static void hub_release(struct kref *kref)
1695 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1697 usb_put_dev(hub->hdev);
1698 usb_put_intf(to_usb_interface(hub->intfdev));
1702 static unsigned highspeed_hubs;
1704 static void hub_disconnect(struct usb_interface *intf)
1706 struct usb_hub *hub = usb_get_intfdata(intf);
1707 struct usb_device *hdev = interface_to_usbdev(intf);
1711 * Stop adding new hub events. We do not want to block here and thus
1712 * will not try to remove any pending work item.
1714 hub->disconnected = 1;
1716 /* Disconnect all children and quiesce the hub */
1718 hub_quiesce(hub, HUB_DISCONNECT);
1720 mutex_lock(&usb_port_peer_mutex);
1722 /* Avoid races with recursively_mark_NOTATTACHED() */
1723 spin_lock_irq(&device_state_lock);
1724 port1 = hdev->maxchild;
1726 usb_set_intfdata(intf, NULL);
1727 spin_unlock_irq(&device_state_lock);
1729 for (; port1 > 0; --port1)
1730 usb_hub_remove_port_device(hub, port1);
1732 mutex_unlock(&usb_port_peer_mutex);
1734 if (hub->hdev->speed == USB_SPEED_HIGH)
1737 usb_free_urb(hub->urb);
1739 kfree(hub->descriptor);
1743 pm_suspend_ignore_children(&intf->dev, false);
1745 if (hub->quirk_disable_autosuspend)
1746 usb_autopm_put_interface(intf);
1748 kref_put(&hub->kref, hub_release);
1751 static bool hub_descriptor_is_sane(struct usb_host_interface *desc)
1753 /* Some hubs have a subclass of 1, which AFAICT according to the */
1754 /* specs is not defined, but it works */
1755 if (desc->desc.bInterfaceSubClass != 0 &&
1756 desc->desc.bInterfaceSubClass != 1)
1759 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1760 if (desc->desc.bNumEndpoints != 1)
1763 /* If the first endpoint is not interrupt IN, we'd better punt! */
1764 if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc))
1770 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1772 struct usb_host_interface *desc;
1773 struct usb_device *hdev;
1774 struct usb_hub *hub;
1776 desc = intf->cur_altsetting;
1777 hdev = interface_to_usbdev(intf);
1780 * Set default autosuspend delay as 0 to speedup bus suspend,
1781 * based on the below considerations:
1783 * - Unlike other drivers, the hub driver does not rely on the
1784 * autosuspend delay to provide enough time to handle a wakeup
1785 * event, and the submitted status URB is just to check future
1786 * change on hub downstream ports, so it is safe to do it.
1788 * - The patch might cause one or more auto supend/resume for
1789 * below very rare devices when they are plugged into hub
1792 * devices having trouble initializing, and disconnect
1793 * themselves from the bus and then reconnect a second
1796 * devices just for downloading firmware, and disconnects
1797 * themselves after completing it
1799 * For these quite rare devices, their drivers may change the
1800 * autosuspend delay of their parent hub in the probe() to one
1801 * appropriate value to avoid the subtle problem if someone
1804 * - The patch may cause one or more auto suspend/resume on
1805 * hub during running 'lsusb', but it is probably too
1806 * infrequent to worry about.
1808 * - Change autosuspend delay of hub can avoid unnecessary auto
1809 * suspend timer for hub, also may decrease power consumption
1812 * - If user has indicated to prevent autosuspend by passing
1813 * usbcore.autosuspend = -1 then keep autosuspend disabled.
1816 if (hdev->dev.power.autosuspend_delay >= 0)
1817 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1821 * Hubs have proper suspend/resume support, except for root hubs
1822 * where the controller driver doesn't have bus_suspend and
1823 * bus_resume methods.
1825 if (hdev->parent) { /* normal device */
1826 usb_enable_autosuspend(hdev);
1827 } else { /* root hub */
1828 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1830 if (drv->bus_suspend && drv->bus_resume)
1831 usb_enable_autosuspend(hdev);
1834 if (hdev->level == MAX_TOPO_LEVEL) {
1836 "Unsupported bus topology: hub nested too deep\n");
1840 #ifdef CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB
1842 dev_warn(&intf->dev, "ignoring external hub\n");
1847 if (!hub_descriptor_is_sane(desc)) {
1848 dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
1852 /* We found a hub */
1853 dev_info(&intf->dev, "USB hub found\n");
1855 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1859 kref_init(&hub->kref);
1860 hub->intfdev = &intf->dev;
1862 INIT_DELAYED_WORK(&hub->leds, led_work);
1863 INIT_DELAYED_WORK(&hub->init_work, NULL);
1864 INIT_WORK(&hub->events, hub_event);
1865 spin_lock_init(&hub->irq_urb_lock);
1866 timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0);
1870 usb_set_intfdata(intf, hub);
1871 intf->needs_remote_wakeup = 1;
1872 pm_suspend_ignore_children(&intf->dev, true);
1874 if (hdev->speed == USB_SPEED_HIGH)
1877 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1878 hub->quirk_check_port_auto_suspend = 1;
1880 if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) {
1881 hub->quirk_disable_autosuspend = 1;
1882 usb_autopm_get_interface_no_resume(intf);
1885 if (hub_configure(hub, &desc->endpoint[0].desc) >= 0)
1888 hub_disconnect(intf);
1893 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1895 struct usb_device *hdev = interface_to_usbdev(intf);
1896 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1898 /* assert ifno == 0 (part of hub spec) */
1900 case USBDEVFS_HUB_PORTINFO: {
1901 struct usbdevfs_hub_portinfo *info = user_data;
1904 spin_lock_irq(&device_state_lock);
1905 if (hdev->devnum <= 0)
1908 info->nports = hdev->maxchild;
1909 for (i = 0; i < info->nports; i++) {
1910 if (hub->ports[i]->child == NULL)
1914 hub->ports[i]->child->devnum;
1917 spin_unlock_irq(&device_state_lock);
1919 return info->nports + 1;
1928 * Allow user programs to claim ports on a hub. When a device is attached
1929 * to one of these "claimed" ports, the program will "own" the device.
1931 static int find_port_owner(struct usb_device *hdev, unsigned port1,
1932 struct usb_dev_state ***ppowner)
1934 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1936 if (hdev->state == USB_STATE_NOTATTACHED)
1938 if (port1 == 0 || port1 > hdev->maxchild)
1941 /* Devices not managed by the hub driver
1942 * will always have maxchild equal to 0.
1944 *ppowner = &(hub->ports[port1 - 1]->port_owner);
1948 /* In the following three functions, the caller must hold hdev's lock */
1949 int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1950 struct usb_dev_state *owner)
1953 struct usb_dev_state **powner;
1955 rc = find_port_owner(hdev, port1, &powner);
1963 EXPORT_SYMBOL_GPL(usb_hub_claim_port);
1965 int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1966 struct usb_dev_state *owner)
1969 struct usb_dev_state **powner;
1971 rc = find_port_owner(hdev, port1, &powner);
1974 if (*powner != owner)
1979 EXPORT_SYMBOL_GPL(usb_hub_release_port);
1981 void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
1983 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1986 for (n = 0; n < hdev->maxchild; n++) {
1987 if (hub->ports[n]->port_owner == owner)
1988 hub->ports[n]->port_owner = NULL;
1993 /* The caller must hold udev's lock */
1994 bool usb_device_is_owned(struct usb_device *udev)
1996 struct usb_hub *hub;
1998 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
2000 hub = usb_hub_to_struct_hub(udev->parent);
2001 return !!hub->ports[udev->portnum - 1]->port_owner;
2004 static void recursively_mark_NOTATTACHED(struct usb_device *udev)
2006 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2009 for (i = 0; i < udev->maxchild; ++i) {
2010 if (hub->ports[i]->child)
2011 recursively_mark_NOTATTACHED(hub->ports[i]->child);
2013 if (udev->state == USB_STATE_SUSPENDED)
2014 udev->active_duration -= jiffies;
2015 udev->state = USB_STATE_NOTATTACHED;
2019 * usb_set_device_state - change a device's current state (usbcore, hcds)
2020 * @udev: pointer to device whose state should be changed
2021 * @new_state: new state value to be stored
2023 * udev->state is _not_ fully protected by the device lock. Although
2024 * most transitions are made only while holding the lock, the state can
2025 * can change to USB_STATE_NOTATTACHED at almost any time. This
2026 * is so that devices can be marked as disconnected as soon as possible,
2027 * without having to wait for any semaphores to be released. As a result,
2028 * all changes to any device's state must be protected by the
2029 * device_state_lock spinlock.
2031 * Once a device has been added to the device tree, all changes to its state
2032 * should be made using this routine. The state should _not_ be set directly.
2034 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
2035 * Otherwise udev->state is set to new_state, and if new_state is
2036 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
2037 * to USB_STATE_NOTATTACHED.
2039 void usb_set_device_state(struct usb_device *udev,
2040 enum usb_device_state new_state)
2042 unsigned long flags;
2045 spin_lock_irqsave(&device_state_lock, flags);
2046 if (udev->state == USB_STATE_NOTATTACHED)
2048 else if (new_state != USB_STATE_NOTATTACHED) {
2050 /* root hub wakeup capabilities are managed out-of-band
2051 * and may involve silicon errata ... ignore them here.
2054 if (udev->state == USB_STATE_SUSPENDED
2055 || new_state == USB_STATE_SUSPENDED)
2056 ; /* No change to wakeup settings */
2057 else if (new_state == USB_STATE_CONFIGURED)
2058 wakeup = (udev->quirks &
2059 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
2060 udev->actconfig->desc.bmAttributes &
2061 USB_CONFIG_ATT_WAKEUP;
2065 if (udev->state == USB_STATE_SUSPENDED &&
2066 new_state != USB_STATE_SUSPENDED)
2067 udev->active_duration -= jiffies;
2068 else if (new_state == USB_STATE_SUSPENDED &&
2069 udev->state != USB_STATE_SUSPENDED)
2070 udev->active_duration += jiffies;
2071 udev->state = new_state;
2073 recursively_mark_NOTATTACHED(udev);
2074 spin_unlock_irqrestore(&device_state_lock, flags);
2076 device_set_wakeup_capable(&udev->dev, wakeup);
2078 EXPORT_SYMBOL_GPL(usb_set_device_state);
2081 * Choose a device number.
2083 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2084 * USB-2.0 buses they are also used as device addresses, however on
2085 * USB-3.0 buses the address is assigned by the controller hardware
2086 * and it usually is not the same as the device number.
2088 * WUSB devices are simple: they have no hubs behind, so the mapping
2089 * device <-> virtual port number becomes 1:1. Why? to simplify the
2090 * life of the device connection logic in
2091 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2092 * handshake we need to assign a temporary address in the unauthorized
2093 * space. For simplicity we use the first virtual port number found to
2094 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2095 * and that becomes it's address [X < 128] or its unauthorized address
2098 * We add 1 as an offset to the one-based USB-stack port number
2099 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2100 * 0 is reserved by USB for default address; (b) Linux's USB stack
2101 * uses always #1 for the root hub of the controller. So USB stack's
2102 * port #1, which is wusb virtual-port #0 has address #2.
2104 * Devices connected under xHCI are not as simple. The host controller
2105 * supports virtualization, so the hardware assigns device addresses and
2106 * the HCD must setup data structures before issuing a set address
2107 * command to the hardware.
2109 static void choose_devnum(struct usb_device *udev)
2112 struct usb_bus *bus = udev->bus;
2114 /* be safe when more hub events are proceed in parallel */
2115 mutex_lock(&bus->devnum_next_mutex);
2117 devnum = udev->portnum + 1;
2118 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2120 /* Try to allocate the next devnum beginning at
2121 * bus->devnum_next. */
2122 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2125 devnum = find_next_zero_bit(bus->devmap.devicemap,
2127 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
2130 set_bit(devnum, bus->devmap.devicemap);
2131 udev->devnum = devnum;
2133 mutex_unlock(&bus->devnum_next_mutex);
2136 static void release_devnum(struct usb_device *udev)
2138 if (udev->devnum > 0) {
2139 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2144 static void update_devnum(struct usb_device *udev, int devnum)
2146 /* The address for a WUSB device is managed by wusbcore. */
2148 udev->devnum = devnum;
2150 udev->devaddr = (u8)devnum;
2153 static void hub_free_dev(struct usb_device *udev)
2155 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2157 /* Root hubs aren't real devices, so don't free HCD resources */
2158 if (hcd->driver->free_dev && udev->parent)
2159 hcd->driver->free_dev(hcd, udev);
2162 static void hub_disconnect_children(struct usb_device *udev)
2164 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2167 /* Free up all the children before we remove this device */
2168 for (i = 0; i < udev->maxchild; i++) {
2169 if (hub->ports[i]->child)
2170 usb_disconnect(&hub->ports[i]->child);
2175 * usb_disconnect - disconnect a device (usbcore-internal)
2176 * @pdev: pointer to device being disconnected
2178 * Context: task context, might sleep
2180 * Something got disconnected. Get rid of it and all of its children.
2182 * If *pdev is a normal device then the parent hub must already be locked.
2183 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
2184 * which protects the set of root hubs as well as the list of buses.
2186 * Only hub drivers (including virtual root hub drivers for host
2187 * controllers) should ever call this.
2189 * This call is synchronous, and may not be used in an interrupt context.
2191 void usb_disconnect(struct usb_device **pdev)
2193 struct usb_port *port_dev = NULL;
2194 struct usb_device *udev = *pdev;
2195 struct usb_hub *hub = NULL;
2198 /* mark the device as inactive, so any further urb submissions for
2199 * this device (and any of its children) will fail immediately.
2200 * this quiesces everything except pending urbs.
2202 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2203 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2207 * Ensure that the pm runtime code knows that the USB device
2208 * is in the process of being disconnected.
2210 pm_runtime_barrier(&udev->dev);
2212 usb_lock_device(udev);
2214 hub_disconnect_children(udev);
2216 /* deallocate hcd/hardware state ... nuking all pending urbs and
2217 * cleaning up all state associated with the current configuration
2218 * so that the hardware is now fully quiesced.
2220 dev_dbg(&udev->dev, "unregistering device\n");
2221 usb_disable_device(udev, 0);
2222 usb_hcd_synchronize_unlinks(udev);
2225 port1 = udev->portnum;
2226 hub = usb_hub_to_struct_hub(udev->parent);
2227 port_dev = hub->ports[port1 - 1];
2229 sysfs_remove_link(&udev->dev.kobj, "port");
2230 sysfs_remove_link(&port_dev->dev.kobj, "device");
2233 * As usb_port_runtime_resume() de-references udev, make
2234 * sure no resumes occur during removal
2236 if (!test_and_set_bit(port1, hub->child_usage_bits))
2237 pm_runtime_get_sync(&port_dev->dev);
2240 usb_remove_ep_devs(&udev->ep0);
2241 usb_unlock_device(udev);
2243 /* Unregister the device. The device driver is responsible
2244 * for de-configuring the device and invoking the remove-device
2245 * notifier chain (used by usbfs and possibly others).
2247 device_del(&udev->dev);
2249 /* Free the device number and delete the parent's children[]
2250 * (or root_hub) pointer.
2252 release_devnum(udev);
2254 /* Avoid races with recursively_mark_NOTATTACHED() */
2255 spin_lock_irq(&device_state_lock);
2257 spin_unlock_irq(&device_state_lock);
2259 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2260 pm_runtime_put(&port_dev->dev);
2264 put_device(&udev->dev);
2267 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
2268 static void show_string(struct usb_device *udev, char *id, char *string)
2272 dev_info(&udev->dev, "%s: %s\n", id, string);
2275 static void announce_device(struct usb_device *udev)
2277 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2279 dev_info(&udev->dev,
2280 "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n",
2281 le16_to_cpu(udev->descriptor.idVendor),
2282 le16_to_cpu(udev->descriptor.idProduct),
2283 bcdDevice >> 8, bcdDevice & 0xff);
2284 dev_info(&udev->dev,
2285 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2286 udev->descriptor.iManufacturer,
2287 udev->descriptor.iProduct,
2288 udev->descriptor.iSerialNumber);
2289 show_string(udev, "Product", udev->product);
2290 show_string(udev, "Manufacturer", udev->manufacturer);
2291 show_string(udev, "SerialNumber", udev->serial);
2294 static inline void announce_device(struct usb_device *udev) { }
2299 * usb_enumerate_device_otg - FIXME (usbcore-internal)
2300 * @udev: newly addressed device (in ADDRESS state)
2302 * Finish enumeration for On-The-Go devices
2304 * Return: 0 if successful. A negative error code otherwise.
2306 static int usb_enumerate_device_otg(struct usb_device *udev)
2310 #ifdef CONFIG_USB_OTG
2312 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2313 * to wake us after we've powered off VBUS; and HNP, switching roles
2314 * "host" to "peripheral". The OTG descriptor helps figure this out.
2316 if (!udev->bus->is_b_host
2318 && udev->parent == udev->bus->root_hub) {
2319 struct usb_otg_descriptor *desc = NULL;
2320 struct usb_bus *bus = udev->bus;
2321 unsigned port1 = udev->portnum;
2323 /* descriptor may appear anywhere in config */
2324 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
2325 le16_to_cpu(udev->config[0].desc.wTotalLength),
2326 USB_DT_OTG, (void **) &desc, sizeof(*desc));
2327 if (err || !(desc->bmAttributes & USB_OTG_HNP))
2330 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
2331 (port1 == bus->otg_port) ? "" : "non-");
2333 /* enable HNP before suspend, it's simpler */
2334 if (port1 == bus->otg_port) {
2335 bus->b_hnp_enable = 1;
2336 err = usb_control_msg(udev,
2337 usb_sndctrlpipe(udev, 0),
2338 USB_REQ_SET_FEATURE, 0,
2339 USB_DEVICE_B_HNP_ENABLE,
2341 USB_CTRL_SET_TIMEOUT);
2344 * OTG MESSAGE: report errors here,
2345 * customize to match your product.
2347 dev_err(&udev->dev, "can't set HNP mode: %d\n",
2349 bus->b_hnp_enable = 0;
2351 } else if (desc->bLength == sizeof
2352 (struct usb_otg_descriptor)) {
2353 /* Set a_alt_hnp_support for legacy otg device */
2354 err = usb_control_msg(udev,
2355 usb_sndctrlpipe(udev, 0),
2356 USB_REQ_SET_FEATURE, 0,
2357 USB_DEVICE_A_ALT_HNP_SUPPORT,
2359 USB_CTRL_SET_TIMEOUT);
2362 "set a_alt_hnp_support failed: %d\n",
2372 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2373 * @udev: newly addressed device (in ADDRESS state)
2375 * This is only called by usb_new_device() and usb_authorize_device()
2376 * and FIXME -- all comments that apply to them apply here wrt to
2379 * If the device is WUSB and not authorized, we don't attempt to read
2380 * the string descriptors, as they will be errored out by the device
2381 * until it has been authorized.
2383 * Return: 0 if successful. A negative error code otherwise.
2385 static int usb_enumerate_device(struct usb_device *udev)
2388 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2390 if (udev->config == NULL) {
2391 err = usb_get_configuration(udev);
2394 dev_err(&udev->dev, "can't read configurations, error %d\n",
2400 /* read the standard strings and cache them if present */
2401 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2402 udev->manufacturer = usb_cache_string(udev,
2403 udev->descriptor.iManufacturer);
2404 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2406 err = usb_enumerate_device_otg(udev);
2410 if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support &&
2411 !is_targeted(udev)) {
2412 /* Maybe it can talk to us, though we can't talk to it.
2413 * (Includes HNP test device.)
2415 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2416 || udev->bus->is_b_host)) {
2417 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2419 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2424 usb_detect_interface_quirks(udev);
2429 static void set_usb_port_removable(struct usb_device *udev)
2431 struct usb_device *hdev = udev->parent;
2432 struct usb_hub *hub;
2433 u8 port = udev->portnum;
2434 u16 wHubCharacteristics;
2435 bool removable = true;
2440 hub = usb_hub_to_struct_hub(udev->parent);
2443 * If the platform firmware has provided information about a port,
2444 * use that to determine whether it's removable.
2446 switch (hub->ports[udev->portnum - 1]->connect_type) {
2447 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2448 udev->removable = USB_DEVICE_REMOVABLE;
2450 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2451 case USB_PORT_NOT_USED:
2452 udev->removable = USB_DEVICE_FIXED;
2459 * Otherwise, check whether the hub knows whether a port is removable
2462 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2464 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2467 if (hub_is_superspeed(hdev)) {
2468 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2472 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2477 udev->removable = USB_DEVICE_REMOVABLE;
2479 udev->removable = USB_DEVICE_FIXED;
2484 * usb_new_device - perform initial device setup (usbcore-internal)
2485 * @udev: newly addressed device (in ADDRESS state)
2487 * This is called with devices which have been detected but not fully
2488 * enumerated. The device descriptor is available, but not descriptors
2489 * for any device configuration. The caller must have locked either
2490 * the parent hub (if udev is a normal device) or else the
2491 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
2492 * udev has already been installed, but udev is not yet visible through
2493 * sysfs or other filesystem code.
2495 * This call is synchronous, and may not be used in an interrupt context.
2497 * Only the hub driver or root-hub registrar should ever call this.
2499 * Return: Whether the device is configured properly or not. Zero if the
2500 * interface was registered with the driver core; else a negative errno
2504 int usb_new_device(struct usb_device *udev)
2509 /* Initialize non-root-hub device wakeup to disabled;
2510 * device (un)configuration controls wakeup capable
2511 * sysfs power/wakeup controls wakeup enabled/disabled
2513 device_init_wakeup(&udev->dev, 0);
2516 /* Tell the runtime-PM framework the device is active */
2517 pm_runtime_set_active(&udev->dev);
2518 pm_runtime_get_noresume(&udev->dev);
2519 pm_runtime_use_autosuspend(&udev->dev);
2520 pm_runtime_enable(&udev->dev);
2522 /* By default, forbid autosuspend for all devices. It will be
2523 * allowed for hubs during binding.
2525 usb_disable_autosuspend(udev);
2527 err = usb_enumerate_device(udev); /* Read descriptors */
2530 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2531 udev->devnum, udev->bus->busnum,
2532 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2533 /* export the usbdev device-node for libusb */
2534 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2535 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2537 /* Tell the world! */
2538 announce_device(udev);
2541 add_device_randomness(udev->serial, strlen(udev->serial));
2543 add_device_randomness(udev->product, strlen(udev->product));
2544 if (udev->manufacturer)
2545 add_device_randomness(udev->manufacturer,
2546 strlen(udev->manufacturer));
2548 device_enable_async_suspend(&udev->dev);
2550 /* check whether the hub or firmware marks this port as non-removable */
2552 set_usb_port_removable(udev);
2554 /* Register the device. The device driver is responsible
2555 * for configuring the device and invoking the add-device
2556 * notifier chain (used by usbfs and possibly others).
2558 err = device_add(&udev->dev);
2560 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2564 /* Create link files between child device and usb port device. */
2566 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2567 int port1 = udev->portnum;
2568 struct usb_port *port_dev = hub->ports[port1 - 1];
2570 err = sysfs_create_link(&udev->dev.kobj,
2571 &port_dev->dev.kobj, "port");
2575 err = sysfs_create_link(&port_dev->dev.kobj,
2576 &udev->dev.kobj, "device");
2578 sysfs_remove_link(&udev->dev.kobj, "port");
2582 if (!test_and_set_bit(port1, hub->child_usage_bits))
2583 pm_runtime_get_sync(&port_dev->dev);
2586 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2587 usb_mark_last_busy(udev);
2588 pm_runtime_put_sync_autosuspend(&udev->dev);
2592 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2593 pm_runtime_disable(&udev->dev);
2594 pm_runtime_set_suspended(&udev->dev);
2600 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2601 * @usb_dev: USB device
2603 * Move the USB device to a very basic state where interfaces are disabled
2604 * and the device is in fact unconfigured and unusable.
2606 * We share a lock (that we have) with device_del(), so we need to
2611 int usb_deauthorize_device(struct usb_device *usb_dev)
2613 usb_lock_device(usb_dev);
2614 if (usb_dev->authorized == 0)
2615 goto out_unauthorized;
2617 usb_dev->authorized = 0;
2618 usb_set_configuration(usb_dev, -1);
2621 usb_unlock_device(usb_dev);
2626 int usb_authorize_device(struct usb_device *usb_dev)
2630 usb_lock_device(usb_dev);
2631 if (usb_dev->authorized == 1)
2632 goto out_authorized;
2634 result = usb_autoresume_device(usb_dev);
2636 dev_err(&usb_dev->dev,
2637 "can't autoresume for authorization: %d\n", result);
2638 goto error_autoresume;
2641 if (usb_dev->wusb) {
2642 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2644 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2645 "authorization: %d\n", result);
2646 goto error_device_descriptor;
2650 usb_dev->authorized = 1;
2651 /* Choose and set the configuration. This registers the interfaces
2652 * with the driver core and lets interface drivers bind to them.
2654 c = usb_choose_configuration(usb_dev);
2656 result = usb_set_configuration(usb_dev, c);
2658 dev_err(&usb_dev->dev,
2659 "can't set config #%d, error %d\n", c, result);
2660 /* This need not be fatal. The user can try to
2661 * set other configurations. */
2664 dev_info(&usb_dev->dev, "authorized to connect\n");
2666 error_device_descriptor:
2667 usb_autosuspend_device(usb_dev);
2670 usb_unlock_device(usb_dev); /* complements locktree */
2675 * get_port_ssp_rate - Match the extended port status to SSP rate
2676 * @hdev: The hub device
2677 * @ext_portstatus: extended port status
2679 * Match the extended port status speed id to the SuperSpeed Plus sublink speed
2680 * capability attributes. Base on the number of connected lanes and speed,
2681 * return the corresponding enum usb_ssp_rate.
2683 static enum usb_ssp_rate get_port_ssp_rate(struct usb_device *hdev,
2686 struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap;
2696 speed_id = ext_portstatus & USB_EXT_PORT_STAT_RX_SPEED_ID;
2697 lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1;
2699 ssac = le32_to_cpu(ssp_cap->bmAttributes) &
2700 USB_SSP_SUBLINK_SPEED_ATTRIBS;
2702 for (i = 0; i <= ssac; i++) {
2705 attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
2706 ssid = FIELD_GET(USB_SSP_SUBLINK_SPEED_SSID, attr);
2707 if (speed_id == ssid) {
2713 * Note: currently asymmetric lane types are only
2714 * applicable for SSIC operate in SuperSpeed protocol
2716 type = FIELD_GET(USB_SSP_SUBLINK_SPEED_ST, attr);
2717 if (type == USB_SSP_SUBLINK_SPEED_ST_ASYM_RX ||
2718 type == USB_SSP_SUBLINK_SPEED_ST_ASYM_TX)
2721 if (FIELD_GET(USB_SSP_SUBLINK_SPEED_LP, attr) !=
2722 USB_SSP_SUBLINK_SPEED_LP_SSP)
2725 lse = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSE, attr);
2726 mantissa = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSM, attr);
2728 /* Convert to Gbps */
2729 for (; lse < USB_SSP_SUBLINK_SPEED_LSE_GBPS; lse++)
2732 if (mantissa >= 10 && lanes == 1)
2733 return USB_SSP_GEN_2x1;
2735 if (mantissa >= 10 && lanes == 2)
2736 return USB_SSP_GEN_2x2;
2738 if (mantissa >= 5 && lanes == 2)
2739 return USB_SSP_GEN_1x2;
2746 return USB_SSP_GEN_UNKNOWN;
2749 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2750 static unsigned hub_is_wusb(struct usb_hub *hub)
2752 struct usb_hcd *hcd;
2753 if (hub->hdev->parent != NULL) /* not a root hub? */
2755 hcd = bus_to_hcd(hub->hdev->bus);
2756 return hcd->wireless;
2760 #ifdef CONFIG_USB_FEW_INIT_RETRIES
2761 #define PORT_RESET_TRIES 2
2762 #define SET_ADDRESS_TRIES 1
2763 #define GET_DESCRIPTOR_TRIES 1
2764 #define GET_MAXPACKET0_TRIES 1
2765 #define PORT_INIT_TRIES 4
2768 #define PORT_RESET_TRIES 5
2769 #define SET_ADDRESS_TRIES 2
2770 #define GET_DESCRIPTOR_TRIES 2
2771 #define GET_MAXPACKET0_TRIES 3
2772 #define PORT_INIT_TRIES 4
2773 #endif /* CONFIG_USB_FEW_INIT_RETRIES */
2775 #define HUB_ROOT_RESET_TIME 60 /* times are in msec */
2776 #define HUB_SHORT_RESET_TIME 10
2777 #define HUB_BH_RESET_TIME 50
2778 #define HUB_LONG_RESET_TIME 200
2779 #define HUB_RESET_TIMEOUT 800
2781 static bool use_new_scheme(struct usb_device *udev, int retry,
2782 struct usb_port *port_dev)
2784 int old_scheme_first_port =
2785 (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) ||
2789 * "New scheme" enumeration causes an extra state transition to be
2790 * exposed to an xhci host and causes USB3 devices to receive control
2791 * commands in the default state. This has been seen to cause
2792 * enumeration failures, so disable this enumeration scheme for USB3
2795 if (udev->speed >= USB_SPEED_SUPER)
2799 * If use_both_schemes is set, use the first scheme (whichever
2800 * it is) for the larger half of the retries, then use the other
2801 * scheme. Otherwise, use the first scheme for all the retries.
2803 if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2)
2804 return old_scheme_first_port; /* Second half */
2805 return !old_scheme_first_port; /* First half or all */
2808 /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
2809 * Port warm reset is required to recover
2811 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2816 if (!hub_is_superspeed(hub->hdev))
2819 if (test_bit(port1, hub->warm_reset_bits))
2822 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2823 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2824 || link_state == USB_SS_PORT_LS_COMP_MOD;
2827 static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2828 struct usb_device *udev, unsigned int delay, bool warm)
2830 int delay_time, ret;
2833 u32 ext_portstatus = 0;
2835 for (delay_time = 0;
2836 delay_time < HUB_RESET_TIMEOUT;
2837 delay_time += delay) {
2838 /* wait to give the device a chance to reset */
2841 /* read and decode port status */
2842 if (hub_is_superspeedplus(hub->hdev))
2843 ret = hub_ext_port_status(hub, port1,
2844 HUB_EXT_PORT_STATUS,
2845 &portstatus, &portchange,
2848 ret = hub_port_status(hub, port1, &portstatus,
2854 * The port state is unknown until the reset completes.
2856 * On top of that, some chips may require additional time
2857 * to re-establish a connection after the reset is complete,
2858 * so also wait for the connection to be re-established.
2860 if (!(portstatus & USB_PORT_STAT_RESET) &&
2861 (portstatus & USB_PORT_STAT_CONNECTION))
2864 /* switch to the long delay after two short delay failures */
2865 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2866 delay = HUB_LONG_RESET_TIME;
2868 dev_dbg(&hub->ports[port1 - 1]->dev,
2869 "not %sreset yet, waiting %dms\n",
2870 warm ? "warm " : "", delay);
2873 if ((portstatus & USB_PORT_STAT_RESET))
2876 if (hub_port_warm_reset_required(hub, port1, portstatus))
2879 /* Device went away? */
2880 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2883 /* Retry if connect change is set but status is still connected.
2884 * A USB 3.0 connection may bounce if multiple warm resets were issued,
2885 * but the device may have successfully re-connected. Ignore it.
2887 if (!hub_is_superspeed(hub->hdev) &&
2888 (portchange & USB_PORT_STAT_C_CONNECTION)) {
2889 usb_clear_port_feature(hub->hdev, port1,
2890 USB_PORT_FEAT_C_CONNECTION);
2894 if (!(portstatus & USB_PORT_STAT_ENABLE))
2900 if (hub_is_superspeedplus(hub->hdev)) {
2901 /* extended portstatus Rx and Tx lane count are zero based */
2902 udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1;
2903 udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1;
2904 udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus);
2908 udev->ssp_rate = USB_SSP_GEN_UNKNOWN;
2910 if (hub_is_wusb(hub))
2911 udev->speed = USB_SPEED_WIRELESS;
2912 else if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN)
2913 udev->speed = USB_SPEED_SUPER_PLUS;
2914 else if (hub_is_superspeed(hub->hdev))
2915 udev->speed = USB_SPEED_SUPER;
2916 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2917 udev->speed = USB_SPEED_HIGH;
2918 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2919 udev->speed = USB_SPEED_LOW;
2921 udev->speed = USB_SPEED_FULL;
2925 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
2926 static int hub_port_reset(struct usb_hub *hub, int port1,
2927 struct usb_device *udev, unsigned int delay, bool warm)
2930 u16 portchange, portstatus;
2931 struct usb_port *port_dev = hub->ports[port1 - 1];
2932 int reset_recovery_time;
2934 if (!hub_is_superspeed(hub->hdev)) {
2936 dev_err(hub->intfdev, "only USB3 hub support "
2940 /* Block EHCI CF initialization during the port reset.
2941 * Some companion controllers don't like it when they mix.
2943 down_read(&ehci_cf_port_reset_rwsem);
2946 * If the caller hasn't explicitly requested a warm reset,
2947 * double check and see if one is needed.
2949 if (hub_port_status(hub, port1, &portstatus, &portchange) == 0)
2950 if (hub_port_warm_reset_required(hub, port1,
2954 clear_bit(port1, hub->warm_reset_bits);
2956 /* Reset the port */
2957 for (i = 0; i < PORT_RESET_TRIES; i++) {
2958 status = set_port_feature(hub->hdev, port1, (warm ?
2959 USB_PORT_FEAT_BH_PORT_RESET :
2960 USB_PORT_FEAT_RESET));
2961 if (status == -ENODEV) {
2962 ; /* The hub is gone */
2963 } else if (status) {
2964 dev_err(&port_dev->dev,
2965 "cannot %sreset (err = %d)\n",
2966 warm ? "warm " : "", status);
2968 status = hub_port_wait_reset(hub, port1, udev, delay,
2970 if (status && status != -ENOTCONN && status != -ENODEV)
2971 dev_dbg(hub->intfdev,
2972 "port_wait_reset: err = %d\n",
2976 /* Check for disconnect or reset */
2977 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2978 usb_clear_port_feature(hub->hdev, port1,
2979 USB_PORT_FEAT_C_RESET);
2981 if (!hub_is_superspeed(hub->hdev))
2984 usb_clear_port_feature(hub->hdev, port1,
2985 USB_PORT_FEAT_C_BH_PORT_RESET);
2986 usb_clear_port_feature(hub->hdev, port1,
2987 USB_PORT_FEAT_C_PORT_LINK_STATE);
2990 usb_clear_port_feature(hub->hdev, port1,
2991 USB_PORT_FEAT_C_CONNECTION);
2994 * If a USB 3.0 device migrates from reset to an error
2995 * state, re-issue the warm reset.
2997 if (hub_port_status(hub, port1,
2998 &portstatus, &portchange) < 0)
3001 if (!hub_port_warm_reset_required(hub, port1,
3006 * If the port is in SS.Inactive or Compliance Mode, the
3007 * hot or warm reset failed. Try another warm reset.
3010 dev_dbg(&port_dev->dev,
3011 "hot reset failed, warm reset\n");
3016 dev_dbg(&port_dev->dev,
3017 "not enabled, trying %sreset again...\n",
3018 warm ? "warm " : "");
3019 delay = HUB_LONG_RESET_TIME;
3022 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
3026 if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM)
3027 usleep_range(10000, 12000);
3029 /* TRSTRCY = 10 ms; plus some extra */
3030 reset_recovery_time = 10 + 40;
3032 /* Hub needs extra delay after resetting its port. */
3033 if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET)
3034 reset_recovery_time += 100;
3036 msleep(reset_recovery_time);
3040 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3042 update_devnum(udev, 0);
3043 /* The xHC may think the device is already reset,
3044 * so ignore the status.
3046 if (hcd->driver->reset_device)
3047 hcd->driver->reset_device(hcd, udev);
3049 usb_set_device_state(udev, USB_STATE_DEFAULT);
3053 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
3056 if (!hub_is_superspeed(hub->hdev))
3057 up_read(&ehci_cf_port_reset_rwsem);
3062 /* Check if a port is power on */
3063 static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
3067 if (hub_is_superspeed(hub->hdev)) {
3068 if (portstatus & USB_SS_PORT_STAT_POWER)
3071 if (portstatus & USB_PORT_STAT_POWER)
3078 static void usb_lock_port(struct usb_port *port_dev)
3079 __acquires(&port_dev->status_lock)
3081 mutex_lock(&port_dev->status_lock);
3082 __acquire(&port_dev->status_lock);
3085 static void usb_unlock_port(struct usb_port *port_dev)
3086 __releases(&port_dev->status_lock)
3088 mutex_unlock(&port_dev->status_lock);
3089 __release(&port_dev->status_lock);
3094 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
3095 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
3099 if (hub_is_superspeed(hub->hdev)) {
3100 if ((portstatus & USB_PORT_STAT_LINK_STATE)
3101 == USB_SS_PORT_LS_U3)
3104 if (portstatus & USB_PORT_STAT_SUSPEND)
3111 /* Determine whether the device on a port is ready for a normal resume,
3112 * is ready for a reset-resume, or should be disconnected.
3114 static int check_port_resume_type(struct usb_device *udev,
3115 struct usb_hub *hub, int port1,
3116 int status, u16 portchange, u16 portstatus)
3118 struct usb_port *port_dev = hub->ports[port1 - 1];
3122 /* Is a warm reset needed to recover the connection? */
3123 if (status == 0 && udev->reset_resume
3124 && hub_port_warm_reset_required(hub, port1, portstatus)) {
3127 /* Is the device still present? */
3128 else if (status || port_is_suspended(hub, portstatus) ||
3129 !port_is_power_on(hub, portstatus)) {
3132 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
3134 usleep_range(200, 300);
3135 status = hub_port_status(hub, port1, &portstatus,
3142 /* Can't do a normal resume if the port isn't enabled,
3143 * so try a reset-resume instead.
3145 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
3146 if (udev->persist_enabled)
3147 udev->reset_resume = 1;
3153 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
3154 portchange, portstatus, status);
3155 } else if (udev->reset_resume) {
3157 /* Late port handoff can set status-change bits */
3158 if (portchange & USB_PORT_STAT_C_CONNECTION)
3159 usb_clear_port_feature(hub->hdev, port1,
3160 USB_PORT_FEAT_C_CONNECTION);
3161 if (portchange & USB_PORT_STAT_C_ENABLE)
3162 usb_clear_port_feature(hub->hdev, port1,
3163 USB_PORT_FEAT_C_ENABLE);
3166 * Whatever made this reset-resume necessary may have
3167 * turned on the port1 bit in hub->change_bits. But after
3168 * a successful reset-resume we want the bit to be clear;
3169 * if it was on it would indicate that something happened
3170 * following the reset-resume.
3172 clear_bit(port1, hub->change_bits);
3178 int usb_disable_ltm(struct usb_device *udev)
3180 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3182 /* Check if the roothub and device supports LTM. */
3183 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3184 !usb_device_supports_ltm(udev))
3187 /* Clear Feature LTM Enable can only be sent if the device is
3190 if (!udev->actconfig)
3193 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3194 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3195 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3196 USB_CTRL_SET_TIMEOUT);
3198 EXPORT_SYMBOL_GPL(usb_disable_ltm);
3200 void usb_enable_ltm(struct usb_device *udev)
3202 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3204 /* Check if the roothub and device supports LTM. */
3205 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3206 !usb_device_supports_ltm(udev))
3209 /* Set Feature LTM Enable can only be sent if the device is
3212 if (!udev->actconfig)
3215 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3216 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3217 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3218 USB_CTRL_SET_TIMEOUT);
3220 EXPORT_SYMBOL_GPL(usb_enable_ltm);
3223 * usb_enable_remote_wakeup - enable remote wakeup for a device
3224 * @udev: target device
3226 * For USB-2 devices: Set the device's remote wakeup feature.
3228 * For USB-3 devices: Assume there's only one function on the device and
3229 * enable remote wake for the first interface. FIXME if the interface
3230 * association descriptor shows there's more than one function.
3232 static int usb_enable_remote_wakeup(struct usb_device *udev)
3234 if (udev->speed < USB_SPEED_SUPER)
3235 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3236 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3237 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3238 USB_CTRL_SET_TIMEOUT);
3240 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3241 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3242 USB_INTRF_FUNC_SUSPEND,
3243 USB_INTRF_FUNC_SUSPEND_RW |
3244 USB_INTRF_FUNC_SUSPEND_LP,
3245 NULL, 0, USB_CTRL_SET_TIMEOUT);
3249 * usb_disable_remote_wakeup - disable remote wakeup for a device
3250 * @udev: target device
3252 * For USB-2 devices: Clear the device's remote wakeup feature.
3254 * For USB-3 devices: Assume there's only one function on the device and
3255 * disable remote wake for the first interface. FIXME if the interface
3256 * association descriptor shows there's more than one function.
3258 static int usb_disable_remote_wakeup(struct usb_device *udev)
3260 if (udev->speed < USB_SPEED_SUPER)
3261 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3262 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3263 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3264 USB_CTRL_SET_TIMEOUT);
3266 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3267 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3268 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3269 USB_CTRL_SET_TIMEOUT);
3272 /* Count of wakeup-enabled devices at or below udev */
3273 unsigned usb_wakeup_enabled_descendants(struct usb_device *udev)
3275 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3277 return udev->do_remote_wakeup +
3278 (hub ? hub->wakeup_enabled_descendants : 0);
3280 EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants);
3283 * usb_port_suspend - suspend a usb device's upstream port
3284 * @udev: device that's no longer in active use, not a root hub
3285 * Context: must be able to sleep; device not locked; pm locks held
3287 * Suspends a USB device that isn't in active use, conserving power.
3288 * Devices may wake out of a suspend, if anything important happens,
3289 * using the remote wakeup mechanism. They may also be taken out of
3290 * suspend by the host, using usb_port_resume(). It's also routine
3291 * to disconnect devices while they are suspended.
3293 * This only affects the USB hardware for a device; its interfaces
3294 * (and, for hubs, child devices) must already have been suspended.
3296 * Selective port suspend reduces power; most suspended devices draw
3297 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3298 * All devices below the suspended port are also suspended.
3300 * Devices leave suspend state when the host wakes them up. Some devices
3301 * also support "remote wakeup", where the device can activate the USB
3302 * tree above them to deliver data, such as a keypress or packet. In
3303 * some cases, this wakes the USB host.
3305 * Suspending OTG devices may trigger HNP, if that's been enabled
3306 * between a pair of dual-role devices. That will change roles, such
3307 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3309 * Devices on USB hub ports have only one "suspend" state, corresponding
3310 * to ACPI D2, "may cause the device to lose some context".
3311 * State transitions include:
3313 * - suspend, resume ... when the VBUS power link stays live
3314 * - suspend, disconnect ... VBUS lost
3316 * Once VBUS drop breaks the circuit, the port it's using has to go through
3317 * normal re-enumeration procedures, starting with enabling VBUS power.
3318 * Other than re-initializing the hub (plug/unplug, except for root hubs),
3319 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
3320 * timer, no SRP, no requests through sysfs.
3322 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3323 * suspended until their bus goes into global suspend (i.e., the root
3324 * hub is suspended). Nevertheless, we change @udev->state to
3325 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3326 * upstream port setting is stored in @udev->port_is_suspended.
3328 * Returns 0 on success, else negative errno.
3330 int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3332 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3333 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
3334 int port1 = udev->portnum;
3336 bool really_suspend = true;
3338 usb_lock_port(port_dev);
3340 /* enable remote wakeup when appropriate; this lets the device
3341 * wake up the upstream hub (including maybe the root hub).
3343 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3344 * we don't explicitly enable it here.
3346 if (udev->do_remote_wakeup) {
3347 status = usb_enable_remote_wakeup(udev);
3349 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3351 /* bail if autosuspend is requested */
3352 if (PMSG_IS_AUTO(msg))
3357 /* disable USB2 hardware LPM */
3358 usb_disable_usb2_hardware_lpm(udev);
3360 if (usb_disable_ltm(udev)) {
3361 dev_err(&udev->dev, "Failed to disable LTM before suspend\n");
3363 if (PMSG_IS_AUTO(msg))
3368 if (hub_is_superspeed(hub->hdev))
3369 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
3372 * For system suspend, we do not need to enable the suspend feature
3373 * on individual USB-2 ports. The devices will automatically go
3374 * into suspend a few ms after the root hub stops sending packets.
3375 * The USB 2.0 spec calls this "global suspend".
3377 * However, many USB hubs have a bug: They don't relay wakeup requests
3378 * from a downstream port if the port's suspend feature isn't on.
3379 * Therefore we will turn on the suspend feature if udev or any of its
3380 * descendants is enabled for remote wakeup.
3382 else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0)
3383 status = set_port_feature(hub->hdev, port1,
3384 USB_PORT_FEAT_SUSPEND);
3386 really_suspend = false;
3390 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
3392 /* Try to enable USB3 LTM again */
3393 usb_enable_ltm(udev);
3395 /* Try to enable USB2 hardware LPM again */
3396 usb_enable_usb2_hardware_lpm(udev);
3398 if (udev->do_remote_wakeup)
3399 (void) usb_disable_remote_wakeup(udev);
3402 /* System sleep transitions should never fail */
3403 if (!PMSG_IS_AUTO(msg))
3406 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3407 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3408 udev->do_remote_wakeup);
3409 if (really_suspend) {
3410 udev->port_is_suspended = 1;
3412 /* device has up to 10 msec to fully suspend */
3415 usb_set_device_state(udev, USB_STATE_SUSPENDED);
3418 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3419 && test_and_clear_bit(port1, hub->child_usage_bits))
3420 pm_runtime_put_sync(&port_dev->dev);
3422 usb_mark_last_busy(hub->hdev);
3424 usb_unlock_port(port_dev);
3429 * If the USB "suspend" state is in use (rather than "global suspend"),
3430 * many devices will be individually taken out of suspend state using
3431 * special "resume" signaling. This routine kicks in shortly after
3432 * hardware resume signaling is finished, either because of selective
3433 * resume (by host) or remote wakeup (by device) ... now see what changed
3434 * in the tree that's rooted at this device.
3436 * If @udev->reset_resume is set then the device is reset before the
3437 * status check is done.
3439 static int finish_port_resume(struct usb_device *udev)
3444 /* caller owns the udev device lock */
3445 dev_dbg(&udev->dev, "%s\n",
3446 udev->reset_resume ? "finish reset-resume" : "finish resume");
3448 /* usb ch9 identifies four variants of SUSPENDED, based on what
3449 * state the device resumes to. Linux currently won't see the
3450 * first two on the host side; they'd be inside hub_port_init()
3451 * during many timeouts, but hub_wq can't suspend until later.
3453 usb_set_device_state(udev, udev->actconfig
3454 ? USB_STATE_CONFIGURED
3455 : USB_STATE_ADDRESS);
3457 /* 10.5.4.5 says not to reset a suspended port if the attached
3458 * device is enabled for remote wakeup. Hence the reset
3459 * operation is carried out here, after the port has been
3462 if (udev->reset_resume) {
3464 * If the device morphs or switches modes when it is reset,
3465 * we don't want to perform a reset-resume. We'll fail the
3466 * resume, which will cause a logical disconnect, and then
3467 * the device will be rediscovered.
3470 if (udev->quirks & USB_QUIRK_RESET)
3473 status = usb_reset_and_verify_device(udev);
3476 /* 10.5.4.5 says be sure devices in the tree are still there.
3477 * For now let's assume the device didn't go crazy on resume,
3478 * and device drivers will know about any resume quirks.
3482 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
3484 /* If a normal resume failed, try doing a reset-resume */
3485 if (status && !udev->reset_resume && udev->persist_enabled) {
3486 dev_dbg(&udev->dev, "retry with reset-resume\n");
3487 udev->reset_resume = 1;
3488 goto retry_reset_resume;
3493 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3496 * There are a few quirky devices which violate the standard
3497 * by claiming to have remote wakeup enabled after a reset,
3498 * which crash if the feature is cleared, hence check for
3499 * udev->reset_resume
3501 } else if (udev->actconfig && !udev->reset_resume) {
3502 if (udev->speed < USB_SPEED_SUPER) {
3503 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
3504 status = usb_disable_remote_wakeup(udev);
3506 status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0,
3508 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3509 | USB_INTRF_STAT_FUNC_RW))
3510 status = usb_disable_remote_wakeup(udev);
3515 "disable remote wakeup, status %d\n",
3523 * There are some SS USB devices which take longer time for link training.
3524 * XHCI specs 4.19.4 says that when Link training is successful, port
3525 * sets CCS bit to 1. So if SW reads port status before successful link
3526 * training, then it will not find device to be present.
3527 * USB Analyzer log with such buggy devices show that in some cases
3528 * device switch on the RX termination after long delay of host enabling
3529 * the VBUS. In few other cases it has been seen that device fails to
3530 * negotiate link training in first attempt. It has been
3531 * reported till now that few devices take as long as 2000 ms to train
3532 * the link after host enabling its VBUS and termination. Following
3533 * routine implements a 2000 ms timeout for link training. If in a case
3534 * link trains before timeout, loop will exit earlier.
3536 * There are also some 2.0 hard drive based devices and 3.0 thumb
3537 * drives that, when plugged into a 2.0 only port, take a long
3538 * time to set CCS after VBUS enable.
3540 * FIXME: If a device was connected before suspend, but was removed
3541 * while system was asleep, then the loop in the following routine will
3542 * only exit at timeout.
3544 * This routine should only be called when persist is enabled.
3546 static int wait_for_connected(struct usb_device *udev,
3547 struct usb_hub *hub, int *port1,
3548 u16 *portchange, u16 *portstatus)
3550 int status = 0, delay_ms = 0;
3552 while (delay_ms < 2000) {
3553 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3555 if (!port_is_power_on(hub, *portstatus)) {
3561 status = hub_port_status(hub, *port1, portstatus, portchange);
3563 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
3568 * usb_port_resume - re-activate a suspended usb device's upstream port
3569 * @udev: device to re-activate, not a root hub
3570 * Context: must be able to sleep; device not locked; pm locks held
3572 * This will re-activate the suspended device, increasing power usage
3573 * while letting drivers communicate again with its endpoints.
3574 * USB resume explicitly guarantees that the power session between
3575 * the host and the device is the same as it was when the device
3578 * If @udev->reset_resume is set then this routine won't check that the
3579 * port is still enabled. Furthermore, finish_port_resume() above will
3580 * reset @udev. The end result is that a broken power session can be
3581 * recovered and @udev will appear to persist across a loss of VBUS power.
3583 * For example, if a host controller doesn't maintain VBUS suspend current
3584 * during a system sleep or is reset when the system wakes up, all the USB
3585 * power sessions below it will be broken. This is especially troublesome
3586 * for mass-storage devices containing mounted filesystems, since the
3587 * device will appear to have disconnected and all the memory mappings
3588 * to it will be lost. Using the USB_PERSIST facility, the device can be
3589 * made to appear as if it had not disconnected.
3591 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
3592 * every effort to insure that the same device is present after the
3593 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3594 * quite possible for a device to remain unaltered but its media to be
3595 * changed. If the user replaces a flash memory card while the system is
3596 * asleep, he will have only himself to blame when the filesystem on the
3597 * new card is corrupted and the system crashes.
3599 * Returns 0 on success, else negative errno.
3601 int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3603 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3604 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
3605 int port1 = udev->portnum;
3607 u16 portchange, portstatus;
3609 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
3610 status = pm_runtime_resume_and_get(&port_dev->dev);
3612 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3618 usb_lock_port(port_dev);
3620 /* Skip the initial Clear-Suspend step for a remote wakeup */
3621 status = hub_port_status(hub, port1, &portstatus, &portchange);
3622 if (status == 0 && !port_is_suspended(hub, portstatus)) {
3623 if (portchange & USB_PORT_STAT_C_SUSPEND)
3624 pm_wakeup_event(&udev->dev, 0);
3625 goto SuspendCleared;
3628 /* see 7.1.7.7; affects power usage, but not budgeting */
3629 if (hub_is_superspeed(hub->hdev))
3630 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
3632 status = usb_clear_port_feature(hub->hdev,
3633 port1, USB_PORT_FEAT_SUSPEND);
3635 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
3637 /* drive resume for USB_RESUME_TIMEOUT msec */
3638 dev_dbg(&udev->dev, "usb %sresume\n",
3639 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
3640 msleep(USB_RESUME_TIMEOUT);
3642 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3643 * stop resume signaling. Then finish the resume
3646 status = hub_port_status(hub, port1, &portstatus, &portchange);
3651 udev->port_is_suspended = 0;
3652 if (hub_is_superspeed(hub->hdev)) {
3653 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3654 usb_clear_port_feature(hub->hdev, port1,
3655 USB_PORT_FEAT_C_PORT_LINK_STATE);
3657 if (portchange & USB_PORT_STAT_C_SUSPEND)
3658 usb_clear_port_feature(hub->hdev, port1,
3659 USB_PORT_FEAT_C_SUSPEND);
3662 /* TRSMRCY = 10 msec */
3666 if (udev->persist_enabled)
3667 status = wait_for_connected(udev, hub, &port1, &portchange,
3670 status = check_port_resume_type(udev,
3671 hub, port1, status, portchange, portstatus);
3673 status = finish_port_resume(udev);
3675 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3676 hub_port_logical_disconnect(hub, port1);
3678 /* Try to enable USB2 hardware LPM */
3679 usb_enable_usb2_hardware_lpm(udev);
3681 /* Try to enable USB3 LTM */
3682 usb_enable_ltm(udev);
3685 usb_unlock_port(port_dev);
3690 int usb_remote_wakeup(struct usb_device *udev)
3694 usb_lock_device(udev);
3695 if (udev->state == USB_STATE_SUSPENDED) {
3696 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
3697 status = usb_autoresume_device(udev);
3699 /* Let the drivers do their thing, then... */
3700 usb_autosuspend_device(udev);
3703 usb_unlock_device(udev);
3707 /* Returns 1 if there was a remote wakeup and a connect status change. */
3708 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3709 u16 portstatus, u16 portchange)
3710 __must_hold(&port_dev->status_lock)
3712 struct usb_port *port_dev = hub->ports[port - 1];
3713 struct usb_device *hdev;
3714 struct usb_device *udev;
3715 int connect_change = 0;
3720 udev = port_dev->child;
3721 if (!hub_is_superspeed(hdev)) {
3722 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3724 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3726 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
3727 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3728 (link_state != USB_SS_PORT_LS_U0 &&
3729 link_state != USB_SS_PORT_LS_U1 &&
3730 link_state != USB_SS_PORT_LS_U2))
3735 /* TRSMRCY = 10 msec */
3738 usb_unlock_port(port_dev);
3739 ret = usb_remote_wakeup(udev);
3740 usb_lock_port(port_dev);
3745 hub_port_disable(hub, port, 1);
3747 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3748 return connect_change;
3751 static int check_ports_changed(struct usb_hub *hub)
3755 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3756 u16 portstatus, portchange;
3759 status = hub_port_status(hub, port1, &portstatus, &portchange);
3760 if (!status && portchange)
3766 static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
3768 struct usb_hub *hub = usb_get_intfdata(intf);
3769 struct usb_device *hdev = hub->hdev;
3773 * Warn if children aren't already suspended.
3774 * Also, add up the number of wakeup-enabled descendants.
3776 hub->wakeup_enabled_descendants = 0;
3777 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3778 struct usb_port *port_dev = hub->ports[port1 - 1];
3779 struct usb_device *udev = port_dev->child;
3781 if (udev && udev->can_submit) {
3782 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3783 dev_name(&udev->dev));
3784 if (PMSG_IS_AUTO(msg))
3788 hub->wakeup_enabled_descendants +=
3789 usb_wakeup_enabled_descendants(udev);
3792 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3793 /* check if there are changes pending on hub ports */
3794 if (check_ports_changed(hub)) {
3795 if (PMSG_IS_AUTO(msg))
3797 pm_wakeup_event(&hdev->dev, 2000);
3801 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3802 /* Enable hub to send remote wakeup for all ports. */
3803 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3804 set_port_feature(hdev,
3806 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3807 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3808 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3809 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3813 dev_dbg(&intf->dev, "%s\n", __func__);
3815 /* stop hub_wq and related activity */
3816 hub_quiesce(hub, HUB_SUSPEND);
3820 /* Report wakeup requests from the ports of a resuming root hub */
3821 static void report_wakeup_requests(struct usb_hub *hub)
3823 struct usb_device *hdev = hub->hdev;
3824 struct usb_device *udev;
3825 struct usb_hcd *hcd;
3826 unsigned long resuming_ports;
3830 return; /* Not a root hub */
3832 hcd = bus_to_hcd(hdev->bus);
3833 if (hcd->driver->get_resuming_ports) {
3836 * The get_resuming_ports() method returns a bitmap (origin 0)
3837 * of ports which have started wakeup signaling but have not
3838 * yet finished resuming. During system resume we will
3839 * resume all the enabled ports, regardless of any wakeup
3840 * signals, which means the wakeup requests would be lost.
3841 * To prevent this, report them to the PM core here.
3843 resuming_ports = hcd->driver->get_resuming_ports(hcd);
3844 for (i = 0; i < hdev->maxchild; ++i) {
3845 if (test_bit(i, &resuming_ports)) {
3846 udev = hub->ports[i]->child;
3848 pm_wakeup_event(&udev->dev, 0);
3854 static int hub_resume(struct usb_interface *intf)
3856 struct usb_hub *hub = usb_get_intfdata(intf);
3858 dev_dbg(&intf->dev, "%s\n", __func__);
3859 hub_activate(hub, HUB_RESUME);
3862 * This should be called only for system resume, not runtime resume.
3863 * We can't tell the difference here, so some wakeup requests will be
3864 * reported at the wrong time or more than once. This shouldn't
3865 * matter much, so long as they do get reported.
3867 report_wakeup_requests(hub);
3871 static int hub_reset_resume(struct usb_interface *intf)
3873 struct usb_hub *hub = usb_get_intfdata(intf);
3875 dev_dbg(&intf->dev, "%s\n", __func__);
3876 hub_activate(hub, HUB_RESET_RESUME);
3881 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3882 * @rhdev: struct usb_device for the root hub
3884 * The USB host controller driver calls this function when its root hub
3885 * is resumed and Vbus power has been interrupted or the controller
3886 * has been reset. The routine marks @rhdev as having lost power.
3887 * When the hub driver is resumed it will take notice and carry out
3888 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3889 * the others will be disconnected.
3891 void usb_root_hub_lost_power(struct usb_device *rhdev)
3893 dev_notice(&rhdev->dev, "root hub lost power or was reset\n");
3894 rhdev->reset_resume = 1;
3896 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3898 static const char * const usb3_lpm_names[] = {
3906 * Send a Set SEL control transfer to the device, prior to enabling
3907 * device-initiated U1 or U2. This lets the device know the exit latencies from
3908 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3909 * packet from the host.
3911 * This function will fail if the SEL or PEL values for udev are greater than
3912 * the maximum allowed values for the link state to be enabled.
3914 static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3916 struct usb_set_sel_req *sel_values;
3917 unsigned long long u1_sel;
3918 unsigned long long u1_pel;
3919 unsigned long long u2_sel;
3920 unsigned long long u2_pel;
3923 if (udev->state != USB_STATE_CONFIGURED)
3926 /* Convert SEL and PEL stored in ns to us */
3927 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3928 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3929 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3930 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3933 * Make sure that the calculated SEL and PEL values for the link
3934 * state we're enabling aren't bigger than the max SEL/PEL
3935 * value that will fit in the SET SEL control transfer.
3936 * Otherwise the device would get an incorrect idea of the exit
3937 * latency for the link state, and could start a device-initiated
3938 * U1/U2 when the exit latencies are too high.
3940 if ((state == USB3_LPM_U1 &&
3941 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3942 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3943 (state == USB3_LPM_U2 &&
3944 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3945 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3946 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
3947 usb3_lpm_names[state], u1_sel, u1_pel);
3952 * If we're enabling device-initiated LPM for one link state,
3953 * but the other link state has a too high SEL or PEL value,
3954 * just set those values to the max in the Set SEL request.
3956 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3957 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3959 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3960 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3962 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3963 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3965 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3966 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3969 * usb_enable_lpm() can be called as part of a failed device reset,
3970 * which may be initiated by an error path of a mass storage driver.
3971 * Therefore, use GFP_NOIO.
3973 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3977 sel_values->u1_sel = u1_sel;
3978 sel_values->u1_pel = u1_pel;
3979 sel_values->u2_sel = cpu_to_le16(u2_sel);
3980 sel_values->u2_pel = cpu_to_le16(u2_pel);
3982 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3986 sel_values, sizeof *(sel_values),
3987 USB_CTRL_SET_TIMEOUT);
3993 * Enable or disable device-initiated U1 or U2 transitions.
3995 static int usb_set_device_initiated_lpm(struct usb_device *udev,
3996 enum usb3_link_state state, bool enable)
4003 feature = USB_DEVICE_U1_ENABLE;
4006 feature = USB_DEVICE_U2_ENABLE;
4009 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
4010 __func__, enable ? "enable" : "disable");
4014 if (udev->state != USB_STATE_CONFIGURED) {
4015 dev_dbg(&udev->dev, "%s: Can't %s %s state "
4016 "for unconfigured device.\n",
4017 __func__, enable ? "enable" : "disable",
4018 usb3_lpm_names[state]);
4024 * Now send the control transfer to enable device-initiated LPM
4025 * for either U1 or U2.
4027 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4028 USB_REQ_SET_FEATURE,
4032 USB_CTRL_SET_TIMEOUT);
4034 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4035 USB_REQ_CLEAR_FEATURE,
4039 USB_CTRL_SET_TIMEOUT);
4042 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
4043 enable ? "Enable" : "Disable",
4044 usb3_lpm_names[state]);
4050 static int usb_set_lpm_timeout(struct usb_device *udev,
4051 enum usb3_link_state state, int timeout)
4058 feature = USB_PORT_FEAT_U1_TIMEOUT;
4061 feature = USB_PORT_FEAT_U2_TIMEOUT;
4064 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
4069 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
4070 timeout != USB3_LPM_DEVICE_INITIATED) {
4071 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
4072 "which is a reserved value.\n",
4073 usb3_lpm_names[state], timeout);
4077 ret = set_port_feature(udev->parent,
4078 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
4081 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
4082 "error code %i\n", usb3_lpm_names[state],
4086 if (state == USB3_LPM_U1)
4087 udev->u1_params.timeout = timeout;
4089 udev->u2_params.timeout = timeout;
4094 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
4097 * We will attempt to enable U1 or U2, but there are no guarantees that the
4098 * control transfers to set the hub timeout or enable device-initiated U1/U2
4099 * will be successful.
4101 * If the control transfer to enable device-initiated U1/U2 entry fails, then
4102 * hub-initiated U1/U2 will be disabled.
4104 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
4105 * driver know about it. If that call fails, it should be harmless, and just
4106 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
4108 static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
4109 enum usb3_link_state state)
4112 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
4113 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
4115 /* If the device says it doesn't have *any* exit latency to come out of
4116 * U1 or U2, it's probably lying. Assume it doesn't implement that link
4119 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
4120 (state == USB3_LPM_U2 && u2_mel == 0))
4124 * First, let the device know about the exit latencies
4125 * associated with the link state we're about to enable.
4127 ret = usb_req_set_sel(udev, state);
4129 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
4130 usb3_lpm_names[state]);
4134 /* We allow the host controller to set the U1/U2 timeout internally
4135 * first, so that it can change its schedule to account for the
4136 * additional latency to send data to a device in a lower power
4139 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
4141 /* xHCI host controller doesn't want to enable this LPM state. */
4146 dev_warn(&udev->dev, "Could not enable %s link state, "
4147 "xHCI error %i.\n", usb3_lpm_names[state],
4152 if (usb_set_lpm_timeout(udev, state, timeout)) {
4153 /* If we can't set the parent hub U1/U2 timeout,
4154 * device-initiated LPM won't be allowed either, so let the xHCI
4155 * host know that this link state won't be enabled.
4157 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
4161 /* Only a configured device will accept the Set Feature
4164 if (udev->actconfig &&
4165 usb_set_device_initiated_lpm(udev, state, true) == 0) {
4166 if (state == USB3_LPM_U1)
4167 udev->usb3_lpm_u1_enabled = 1;
4168 else if (state == USB3_LPM_U2)
4169 udev->usb3_lpm_u2_enabled = 1;
4171 /* Don't request U1/U2 entry if the device
4172 * cannot transition to U1/U2.
4174 usb_set_lpm_timeout(udev, state, 0);
4175 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
4180 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
4183 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
4184 * If zero is returned, the parent will not allow the link to go into U1/U2.
4186 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
4187 * it won't have an effect on the bus link state because the parent hub will
4188 * still disallow device-initiated U1/U2 entry.
4190 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
4191 * possible. The result will be slightly more bus bandwidth will be taken up
4192 * (to account for U1/U2 exit latency), but it should be harmless.
4194 static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
4195 enum usb3_link_state state)
4202 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
4207 if (usb_set_lpm_timeout(udev, state, 0))
4210 usb_set_device_initiated_lpm(udev, state, false);
4212 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
4213 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
4214 "bus schedule bandwidth may be impacted.\n",
4215 usb3_lpm_names[state]);
4217 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
4218 * is disabled. Hub will disallows link to enter U1/U2 as well,
4219 * even device is initiating LPM. Hence LPM is disabled if hub LPM
4220 * timeout set to 0, no matter device-initiated LPM is disabled or
4223 if (state == USB3_LPM_U1)
4224 udev->usb3_lpm_u1_enabled = 0;
4225 else if (state == USB3_LPM_U2)
4226 udev->usb3_lpm_u2_enabled = 0;
4232 * Disable hub-initiated and device-initiated U1 and U2 entry.
4233 * Caller must own the bandwidth_mutex.
4235 * This will call usb_enable_lpm() on failure, which will decrement
4236 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
4238 int usb_disable_lpm(struct usb_device *udev)
4240 struct usb_hcd *hcd;
4242 if (!udev || !udev->parent ||
4243 udev->speed < USB_SPEED_SUPER ||
4244 !udev->lpm_capable ||
4245 udev->state < USB_STATE_CONFIGURED)
4248 hcd = bus_to_hcd(udev->bus);
4249 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
4252 udev->lpm_disable_count++;
4253 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
4256 /* If LPM is enabled, attempt to disable it. */
4257 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
4259 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
4265 usb_enable_lpm(udev);
4268 EXPORT_SYMBOL_GPL(usb_disable_lpm);
4270 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
4271 int usb_unlocked_disable_lpm(struct usb_device *udev)
4273 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4279 mutex_lock(hcd->bandwidth_mutex);
4280 ret = usb_disable_lpm(udev);
4281 mutex_unlock(hcd->bandwidth_mutex);
4285 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
4288 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
4289 * xHCI host policy may prevent U1 or U2 from being enabled.
4291 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
4292 * until the lpm_disable_count drops to zero. Caller must own the
4295 void usb_enable_lpm(struct usb_device *udev)
4297 struct usb_hcd *hcd;
4298 struct usb_hub *hub;
4299 struct usb_port *port_dev;
4301 if (!udev || !udev->parent ||
4302 udev->speed < USB_SPEED_SUPER ||
4303 !udev->lpm_capable ||
4304 udev->state < USB_STATE_CONFIGURED)
4307 udev->lpm_disable_count--;
4308 hcd = bus_to_hcd(udev->bus);
4309 /* Double check that we can both enable and disable LPM.
4310 * Device must be configured to accept set feature U1/U2 timeout.
4312 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4313 !hcd->driver->disable_usb3_lpm_timeout)
4316 if (udev->lpm_disable_count > 0)
4319 hub = usb_hub_to_struct_hub(udev->parent);
4323 port_dev = hub->ports[udev->portnum - 1];
4325 if (port_dev->usb3_lpm_u1_permit)
4326 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4328 if (port_dev->usb3_lpm_u2_permit)
4329 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
4331 EXPORT_SYMBOL_GPL(usb_enable_lpm);
4333 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
4334 void usb_unlocked_enable_lpm(struct usb_device *udev)
4336 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4341 mutex_lock(hcd->bandwidth_mutex);
4342 usb_enable_lpm(udev);
4343 mutex_unlock(hcd->bandwidth_mutex);
4345 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4347 /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
4348 static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4349 struct usb_port *port_dev)
4351 struct usb_device *udev = port_dev->child;
4354 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
4355 ret = hub_set_port_link_state(hub, port_dev->portnum,
4358 msleep(USB_RESUME_TIMEOUT);
4359 ret = usb_disable_remote_wakeup(udev);
4362 dev_warn(&udev->dev,
4363 "Port disable: can't disable remote wake\n");
4364 udev->do_remote_wakeup = 0;
4368 #else /* CONFIG_PM */
4370 #define hub_suspend NULL
4371 #define hub_resume NULL
4372 #define hub_reset_resume NULL
4374 static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4375 struct usb_port *port_dev) { }
4377 int usb_disable_lpm(struct usb_device *udev)
4381 EXPORT_SYMBOL_GPL(usb_disable_lpm);
4383 void usb_enable_lpm(struct usb_device *udev) { }
4384 EXPORT_SYMBOL_GPL(usb_enable_lpm);
4386 int usb_unlocked_disable_lpm(struct usb_device *udev)
4390 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
4392 void usb_unlocked_enable_lpm(struct usb_device *udev) { }
4393 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4395 int usb_disable_ltm(struct usb_device *udev)
4399 EXPORT_SYMBOL_GPL(usb_disable_ltm);
4401 void usb_enable_ltm(struct usb_device *udev) { }
4402 EXPORT_SYMBOL_GPL(usb_enable_ltm);
4404 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4405 u16 portstatus, u16 portchange)
4410 #endif /* CONFIG_PM */
4413 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
4414 * a connection with a plugged-in cable but will signal the host when the cable
4415 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
4417 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
4419 struct usb_port *port_dev = hub->ports[port1 - 1];
4420 struct usb_device *hdev = hub->hdev;
4424 if (hub_is_superspeed(hub->hdev)) {
4425 hub_usb3_port_prepare_disable(hub, port_dev);
4426 ret = hub_set_port_link_state(hub, port_dev->portnum,
4429 ret = usb_clear_port_feature(hdev, port1,
4430 USB_PORT_FEAT_ENABLE);
4433 if (port_dev->child && set_state)
4434 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
4435 if (ret && ret != -ENODEV)
4436 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
4441 * usb_port_disable - disable a usb device's upstream port
4442 * @udev: device to disable
4443 * Context: @udev locked, must be able to sleep.
4445 * Disables a USB device that isn't in active use.
4447 int usb_port_disable(struct usb_device *udev)
4449 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4451 return hub_port_disable(hub, udev->portnum, 0);
4454 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4456 * Between connect detection and reset signaling there must be a delay
4457 * of 100ms at least for debounce and power-settling. The corresponding
4458 * timer shall restart whenever the downstream port detects a disconnect.
4460 * Apparently there are some bluetooth and irda-dongles and a number of
4461 * low-speed devices for which this debounce period may last over a second.
4462 * Not covered by the spec - but easy to deal with.
4464 * This implementation uses a 1500ms total debounce timeout; if the
4465 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4466 * every 25ms for transient disconnects. When the port status has been
4467 * unchanged for 100ms it returns the port status.
4469 int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
4472 u16 portchange, portstatus;
4473 unsigned connection = 0xffff;
4474 int total_time, stable_time = 0;
4475 struct usb_port *port_dev = hub->ports[port1 - 1];
4477 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4478 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4482 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4483 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
4484 if (!must_be_connected ||
4485 (connection == USB_PORT_STAT_CONNECTION))
4486 stable_time += HUB_DEBOUNCE_STEP;
4487 if (stable_time >= HUB_DEBOUNCE_STABLE)
4491 connection = portstatus & USB_PORT_STAT_CONNECTION;
4494 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4495 usb_clear_port_feature(hub->hdev, port1,
4496 USB_PORT_FEAT_C_CONNECTION);
4499 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4501 msleep(HUB_DEBOUNCE_STEP);
4504 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4505 total_time, stable_time, portstatus);
4507 if (stable_time < HUB_DEBOUNCE_STABLE)
4512 void usb_ep0_reinit(struct usb_device *udev)
4514 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4515 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
4516 usb_enable_endpoint(udev, &udev->ep0, true);
4518 EXPORT_SYMBOL_GPL(usb_ep0_reinit);
4520 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4521 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4523 static int hub_set_address(struct usb_device *udev, int devnum)
4526 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4529 * The host controller will choose the device address,
4530 * instead of the core having chosen it earlier
4532 if (!hcd->driver->address_device && devnum <= 1)
4534 if (udev->state == USB_STATE_ADDRESS)
4536 if (udev->state != USB_STATE_DEFAULT)
4538 if (hcd->driver->address_device)
4539 retval = hcd->driver->address_device(hcd, udev);
4541 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4542 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4543 NULL, 0, USB_CTRL_SET_TIMEOUT);
4545 update_devnum(udev, devnum);
4546 /* Device now using proper address. */
4547 usb_set_device_state(udev, USB_STATE_ADDRESS);
4548 usb_ep0_reinit(udev);
4554 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4555 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4558 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4559 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4560 * support bit in the BOS descriptor.
4562 static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4564 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4565 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
4567 if (!udev->usb2_hw_lpm_capable || !udev->bos)
4571 connect_type = hub->ports[udev->portnum - 1]->connect_type;
4573 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
4574 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4575 udev->usb2_hw_lpm_allowed = 1;
4576 usb_enable_usb2_hardware_lpm(udev);
4580 static int hub_enable_device(struct usb_device *udev)
4582 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4584 if (!hcd->driver->enable_device)
4586 if (udev->state == USB_STATE_ADDRESS)
4588 if (udev->state != USB_STATE_DEFAULT)
4591 return hcd->driver->enable_device(hcd, udev);
4594 /* Reset device, (re)assign address, get device descriptor.
4595 * Device connection must be stable, no more debouncing needed.
4596 * Returns device in USB_STATE_ADDRESS, except on error.
4598 * If this is called for an already-existing device (as part of
4599 * usb_reset_and_verify_device), the caller must own the device lock and
4600 * the port lock. For a newly detected device that is not accessible
4601 * through any global pointers, it's not necessary to lock the device,
4602 * but it is still necessary to lock the port.
4605 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
4608 struct usb_device *hdev = hub->hdev;
4609 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4610 struct usb_port *port_dev = hub->ports[port1 - 1];
4611 int retries, operations, retval, i;
4612 unsigned delay = HUB_SHORT_RESET_TIME;
4613 enum usb_device_speed oldspeed = udev->speed;
4615 int devnum = udev->devnum;
4616 const char *driver_name;
4619 /* root hub ports have a slightly longer reset period
4620 * (from USB 2.0 spec, section 7.1.7.5)
4622 if (!hdev->parent) {
4623 delay = HUB_ROOT_RESET_TIME;
4624 if (port1 == hdev->bus->otg_port)
4625 hdev->bus->b_hnp_enable = 0;
4628 /* Some low speed devices have problems with the quick delay, so */
4629 /* be a bit pessimistic with those devices. RHbug #23670 */
4630 if (oldspeed == USB_SPEED_LOW)
4631 delay = HUB_LONG_RESET_TIME;
4633 mutex_lock(hcd->address0_mutex);
4635 /* Reset the device; full speed may morph to high speed */
4636 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
4637 retval = hub_port_reset(hub, port1, udev, delay, false);
4638 if (retval < 0) /* error or disconnect */
4640 /* success, speed is known */
4644 /* Don't allow speed changes at reset, except usb 3.0 to faster */
4645 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
4646 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
4647 dev_dbg(&udev->dev, "device reset changed speed!\n");
4650 oldspeed = udev->speed;
4652 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4653 * it's fixed size except for full speed devices.
4654 * For Wireless USB devices, ep0 max packet is always 512 (tho
4655 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
4657 switch (udev->speed) {
4658 case USB_SPEED_SUPER_PLUS:
4659 case USB_SPEED_SUPER:
4660 case USB_SPEED_WIRELESS: /* fixed at 512 */
4661 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
4663 case USB_SPEED_HIGH: /* fixed at 64 */
4664 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
4666 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4667 /* to determine the ep0 maxpacket size, try to read
4668 * the device descriptor to get bMaxPacketSize0 and
4669 * then correct our initial guess.
4671 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
4673 case USB_SPEED_LOW: /* fixed at 8 */
4674 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
4680 if (udev->speed == USB_SPEED_WIRELESS)
4681 speed = "variable speed Wireless";
4683 speed = usb_speed_string(udev->speed);
4686 * The controller driver may be NULL if the controller device
4687 * is the middle device between platform device and roothub.
4688 * This middle device may not need a device driver due to
4689 * all hardware control can be at platform device driver, this
4690 * platform device is usually a dual-role USB controller device.
4692 if (udev->bus->controller->driver)
4693 driver_name = udev->bus->controller->driver->name;
4695 driver_name = udev->bus->sysdev->driver->name;
4697 if (udev->speed < USB_SPEED_SUPER)
4698 dev_info(&udev->dev,
4699 "%s %s USB device number %d using %s\n",
4700 (udev->config) ? "reset" : "new", speed,
4701 devnum, driver_name);
4703 /* Set up TT records, if needed */
4705 udev->tt = hdev->tt;
4706 udev->ttport = hdev->ttport;
4707 } else if (udev->speed != USB_SPEED_HIGH
4708 && hdev->speed == USB_SPEED_HIGH) {
4710 dev_err(&udev->dev, "parent hub has no TT\n");
4714 udev->tt = &hub->tt;
4715 udev->ttport = port1;
4718 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4719 * Because device hardware and firmware is sometimes buggy in
4720 * this area, and this is how Linux has done it for ages.
4721 * Change it cautiously.
4723 * NOTE: If use_new_scheme() is true we will start by issuing
4724 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4725 * so it may help with some non-standards-compliant devices.
4726 * Otherwise we start with SET_ADDRESS and then try to read the
4727 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4730 do_new_scheme = use_new_scheme(udev, retry_counter, port_dev);
4732 for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
4733 if (do_new_scheme) {
4734 struct usb_device_descriptor *buf;
4737 retval = hub_enable_device(udev);
4740 "hub failed to enable device, error %d\n",
4745 #define GET_DESCRIPTOR_BUFSIZE 64
4746 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4752 /* Retry on all errors; some devices are flakey.
4753 * 255 is for WUSB devices, we actually need to use
4754 * 512 (WUSB1.0[4.8.1]).
4756 for (operations = 0; operations < GET_MAXPACKET0_TRIES;
4758 buf->bMaxPacketSize0 = 0;
4759 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4760 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4761 USB_DT_DEVICE << 8, 0,
4762 buf, GET_DESCRIPTOR_BUFSIZE,
4763 initial_descriptor_timeout);
4764 switch (buf->bMaxPacketSize0) {
4765 case 8: case 16: case 32: case 64: case 255:
4766 if (buf->bDescriptorType ==
4778 * Some devices time out if they are powered on
4779 * when already connected. They need a second
4780 * reset. But only on the first attempt,
4781 * lest we get into a time out/reset loop
4783 if (r == 0 || (r == -ETIMEDOUT &&
4785 udev->speed > USB_SPEED_FULL))
4788 udev->descriptor.bMaxPacketSize0 =
4789 buf->bMaxPacketSize0;
4792 retval = hub_port_reset(hub, port1, udev, delay, false);
4793 if (retval < 0) /* error or disconnect */
4795 if (oldspeed != udev->speed) {
4797 "device reset changed speed!\n");
4803 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4808 #undef GET_DESCRIPTOR_BUFSIZE
4812 * If device is WUSB, we already assigned an
4813 * unauthorized address in the Connect Ack sequence;
4814 * authorization will assign the final address.
4816 if (udev->wusb == 0) {
4817 for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
4818 retval = hub_set_address(udev, devnum);
4824 if (retval != -ENODEV)
4825 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4829 if (udev->speed >= USB_SPEED_SUPER) {
4830 devnum = udev->devnum;
4831 dev_info(&udev->dev,
4832 "%s SuperSpeed%s%s USB device number %d using %s\n",
4833 (udev->config) ? "reset" : "new",
4834 (udev->speed == USB_SPEED_SUPER_PLUS) ?
4836 (udev->ssp_rate == USB_SSP_GEN_2x2) ?
4838 (udev->ssp_rate == USB_SSP_GEN_2x1) ?
4840 (udev->ssp_rate == USB_SSP_GEN_1x2) ?
4842 devnum, driver_name);
4845 /* cope with hardware quirkiness:
4846 * - let SET_ADDRESS settle, some device hardware wants it
4847 * - read ep0 maxpacket even for high and low speed,
4854 retval = usb_get_device_descriptor(udev, 8);
4856 if (retval != -ENODEV)
4858 "device descriptor read/8, error %d\n",
4867 delay = udev->parent->hub_delay;
4868 udev->hub_delay = min_t(u32, delay,
4869 USB_TP_TRANSMISSION_DELAY_MAX);
4870 retval = usb_set_isoch_delay(udev);
4873 "Failed set isoch delay, error %d\n",
4884 * Some superspeed devices have finished the link training process
4885 * and attached to a superspeed hub port, but the device descriptor
4886 * got from those devices show they aren't superspeed devices. Warm
4887 * reset the port attached by the devices can fix them.
4889 if ((udev->speed >= USB_SPEED_SUPER) &&
4890 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4891 dev_err(&udev->dev, "got a wrong device descriptor, "
4892 "warm reset device\n");
4893 hub_port_reset(hub, port1, udev,
4894 HUB_BH_RESET_TIME, true);
4899 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4900 udev->speed >= USB_SPEED_SUPER)
4903 i = udev->descriptor.bMaxPacketSize0;
4904 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
4905 if (udev->speed == USB_SPEED_LOW ||
4906 !(i == 8 || i == 16 || i == 32 || i == 64)) {
4907 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
4911 if (udev->speed == USB_SPEED_FULL)
4912 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4914 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
4915 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
4916 usb_ep0_reinit(udev);
4919 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4920 if (retval < (signed)sizeof(udev->descriptor)) {
4921 if (retval != -ENODEV)
4922 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4929 usb_detect_quirks(udev);
4931 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4932 retval = usb_get_bos_descriptor(udev);
4934 udev->lpm_capable = usb_device_supports_lpm(udev);
4935 usb_set_lpm_parameters(udev);
4940 /* notify HCD that we have a device connected and addressed */
4941 if (hcd->driver->update_device)
4942 hcd->driver->update_device(hcd, udev);
4943 hub_set_initial_usb2_lpm_policy(udev);
4946 hub_port_disable(hub, port1, 0);
4947 update_devnum(udev, devnum); /* for disconnect processing */
4949 mutex_unlock(hcd->address0_mutex);
4954 check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
4956 struct usb_qualifier_descriptor *qual;
4959 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4962 qual = kmalloc(sizeof *qual, GFP_KERNEL);
4966 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
4967 qual, sizeof *qual);
4968 if (status == sizeof *qual) {
4969 dev_info(&udev->dev, "not running at top speed; "
4970 "connect to a high speed hub\n");
4971 /* hub LEDs are probably harder to miss than syslog */
4972 if (hub->has_indicators) {
4973 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
4974 queue_delayed_work(system_power_efficient_wq,
4982 hub_power_remaining(struct usb_hub *hub)
4984 struct usb_device *hdev = hub->hdev;
4988 if (!hub->limited_power)
4991 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4992 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4993 struct usb_port *port_dev = hub->ports[port1 - 1];
4994 struct usb_device *udev = port_dev->child;
5000 if (hub_is_superspeed(udev))
5006 * Unconfigured devices may not use more than one unit load,
5007 * or 8mA for OTG ports
5009 if (udev->actconfig)
5010 delta = usb_get_max_power(udev, udev->actconfig);
5011 else if (port1 != udev->bus->otg_port || hdev->parent)
5015 if (delta > hub->mA_per_port)
5016 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
5017 delta, hub->mA_per_port);
5020 if (remaining < 0) {
5021 dev_warn(hub->intfdev, "%dmA over power budget!\n",
5029 static int descriptors_changed(struct usb_device *udev,
5030 struct usb_device_descriptor *old_device_descriptor,
5031 struct usb_host_bos *old_bos)
5035 unsigned serial_len = 0;
5037 unsigned old_length;
5041 if (memcmp(&udev->descriptor, old_device_descriptor,
5042 sizeof(*old_device_descriptor)) != 0)
5045 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5048 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5049 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
5051 if (memcmp(udev->bos->desc, old_bos->desc, len))
5055 /* Since the idVendor, idProduct, and bcdDevice values in the
5056 * device descriptor haven't changed, we will assume the
5057 * Manufacturer and Product strings haven't changed either.
5058 * But the SerialNumber string could be different (e.g., a
5059 * different flash card of the same brand).
5062 serial_len = strlen(udev->serial) + 1;
5065 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5066 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5067 len = max(len, old_length);
5070 buf = kmalloc(len, GFP_NOIO);
5072 /* assume the worst */
5075 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
5076 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5077 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5079 if (length != old_length) {
5080 dev_dbg(&udev->dev, "config index %d, error %d\n",
5085 if (memcmp(buf, udev->rawdescriptors[index], old_length)
5087 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
5089 ((struct usb_config_descriptor *) buf)->
5090 bConfigurationValue);
5096 if (!changed && serial_len) {
5097 length = usb_string(udev, udev->descriptor.iSerialNumber,
5099 if (length + 1 != serial_len) {
5100 dev_dbg(&udev->dev, "serial string error %d\n",
5103 } else if (memcmp(buf, udev->serial, length) != 0) {
5104 dev_dbg(&udev->dev, "serial string changed\n");
5113 static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
5116 int status = -ENODEV;
5119 struct usb_device *hdev = hub->hdev;
5120 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
5121 struct usb_port *port_dev = hub->ports[port1 - 1];
5122 struct usb_device *udev = port_dev->child;
5123 static int unreliable_port = -1;
5125 /* Disconnect any existing devices under this port */
5127 if (hcd->usb_phy && !hdev->parent)
5128 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
5129 usb_disconnect(&port_dev->child);
5132 /* We can forget about a "removed" device when there's a physical
5133 * disconnect or the connect status changes.
5135 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
5136 (portchange & USB_PORT_STAT_C_CONNECTION))
5137 clear_bit(port1, hub->removed_bits);
5139 if (portchange & (USB_PORT_STAT_C_CONNECTION |
5140 USB_PORT_STAT_C_ENABLE)) {
5141 status = hub_port_debounce_be_stable(hub, port1);
5143 if (status != -ENODEV &&
5144 port1 != unreliable_port &&
5146 dev_err(&port_dev->dev, "connect-debounce failed\n");
5147 portstatus &= ~USB_PORT_STAT_CONNECTION;
5148 unreliable_port = port1;
5150 portstatus = status;
5154 /* Return now if debouncing failed or nothing is connected or
5155 * the device was "removed".
5157 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
5158 test_bit(port1, hub->removed_bits)) {
5161 * maybe switch power back on (e.g. root hub was reset)
5162 * but only if the port isn't owned by someone else.
5164 if (hub_is_port_power_switchable(hub)
5165 && !port_is_power_on(hub, portstatus)
5166 && !port_dev->port_owner)
5167 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
5169 if (portstatus & USB_PORT_STAT_ENABLE)
5173 if (hub_is_superspeed(hub->hdev))
5179 for (i = 0; i < PORT_INIT_TRIES; i++) {
5181 /* reallocate for each attempt, since references
5182 * to the previous one can escape in various ways
5184 udev = usb_alloc_dev(hdev, hdev->bus, port1);
5186 dev_err(&port_dev->dev,
5187 "couldn't allocate usb_device\n");
5191 usb_set_device_state(udev, USB_STATE_POWERED);
5192 udev->bus_mA = hub->mA_per_port;
5193 udev->level = hdev->level + 1;
5194 udev->wusb = hub_is_wusb(hub);
5196 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
5197 if (hub_is_superspeed(hub->hdev))
5198 udev->speed = USB_SPEED_SUPER;
5200 udev->speed = USB_SPEED_UNKNOWN;
5202 choose_devnum(udev);
5203 if (udev->devnum <= 0) {
5204 status = -ENOTCONN; /* Don't retry */
5208 /* reset (non-USB 3.0 devices) and get descriptor */
5209 usb_lock_port(port_dev);
5210 status = hub_port_init(hub, udev, port1, i);
5211 usb_unlock_port(port_dev);
5215 if (udev->quirks & USB_QUIRK_DELAY_INIT)
5218 /* consecutive bus-powered hubs aren't reliable; they can
5219 * violate the voltage drop budget. if the new child has
5220 * a "powered" LED, users should notice we didn't enable it
5221 * (without reading syslog), even without per-port LEDs
5224 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
5225 && udev->bus_mA <= unit_load) {
5228 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
5231 dev_dbg(&udev->dev, "get status %d ?\n", status);
5234 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
5236 "can't connect bus-powered hub "
5238 if (hub->has_indicators) {
5239 hub->indicator[port1-1] =
5240 INDICATOR_AMBER_BLINK;
5242 system_power_efficient_wq,
5245 status = -ENOTCONN; /* Don't retry */
5250 /* check for devices running slower than they could */
5251 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
5252 && udev->speed == USB_SPEED_FULL
5253 && highspeed_hubs != 0)
5254 check_highspeed(hub, udev, port1);
5256 /* Store the parent's children[] pointer. At this point
5257 * udev becomes globally accessible, although presumably
5258 * no one will look at it until hdev is unlocked.
5262 mutex_lock(&usb_port_peer_mutex);
5264 /* We mustn't add new devices if the parent hub has
5265 * been disconnected; we would race with the
5266 * recursively_mark_NOTATTACHED() routine.
5268 spin_lock_irq(&device_state_lock);
5269 if (hdev->state == USB_STATE_NOTATTACHED)
5272 port_dev->child = udev;
5273 spin_unlock_irq(&device_state_lock);
5274 mutex_unlock(&usb_port_peer_mutex);
5276 /* Run it through the hoops (find a driver, etc) */
5278 status = usb_new_device(udev);
5280 mutex_lock(&usb_port_peer_mutex);
5281 spin_lock_irq(&device_state_lock);
5282 port_dev->child = NULL;
5283 spin_unlock_irq(&device_state_lock);
5284 mutex_unlock(&usb_port_peer_mutex);
5286 if (hcd->usb_phy && !hdev->parent)
5287 usb_phy_notify_connect(hcd->usb_phy,
5295 status = hub_power_remaining(hub);
5297 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
5302 hub_port_disable(hub, port1, 1);
5304 usb_ep0_reinit(udev);
5305 release_devnum(udev);
5308 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
5311 /* When halfway through our retry count, power-cycle the port */
5312 if (i == (PORT_INIT_TRIES - 1) / 2) {
5313 dev_info(&port_dev->dev, "attempt power cycle\n");
5314 usb_hub_set_port_power(hdev, hub, port1, false);
5315 msleep(2 * hub_power_on_good_delay(hub));
5316 usb_hub_set_port_power(hdev, hub, port1, true);
5317 msleep(hub_power_on_good_delay(hub));
5320 if (hub->hdev->parent ||
5321 !hcd->driver->port_handed_over ||
5322 !(hcd->driver->port_handed_over)(hcd, port1)) {
5323 if (status != -ENOTCONN && status != -ENODEV)
5324 dev_err(&port_dev->dev,
5325 "unable to enumerate USB device\n");
5329 hub_port_disable(hub, port1, 1);
5330 if (hcd->driver->relinquish_port && !hub->hdev->parent) {
5331 if (status != -ENOTCONN && status != -ENODEV)
5332 hcd->driver->relinquish_port(hcd, port1);
5336 /* Handle physical or logical connection change events.
5337 * This routine is called when:
5338 * a port connection-change occurs;
5339 * a port enable-change occurs (often caused by EMI);
5340 * usb_reset_and_verify_device() encounters changed descriptors (as from
5341 * a firmware download)
5342 * caller already locked the hub
5344 static void hub_port_connect_change(struct usb_hub *hub, int port1,
5345 u16 portstatus, u16 portchange)
5346 __must_hold(&port_dev->status_lock)
5348 struct usb_port *port_dev = hub->ports[port1 - 1];
5349 struct usb_device *udev = port_dev->child;
5350 struct usb_device_descriptor descriptor;
5351 int status = -ENODEV;
5354 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
5355 portchange, portspeed(hub, portstatus));
5357 if (hub->has_indicators) {
5358 set_port_led(hub, port1, HUB_LED_AUTO);
5359 hub->indicator[port1-1] = INDICATOR_AUTO;
5362 #ifdef CONFIG_USB_OTG
5363 /* during HNP, don't repeat the debounce */
5364 if (hub->hdev->bus->is_b_host)
5365 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
5366 USB_PORT_STAT_C_ENABLE);
5369 /* Try to resuscitate an existing device */
5370 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
5371 udev->state != USB_STATE_NOTATTACHED) {
5372 if (portstatus & USB_PORT_STAT_ENABLE) {
5374 * USB-3 connections are initialized automatically by
5375 * the hostcontroller hardware. Therefore check for
5376 * changed device descriptors before resuscitating the
5379 descriptor = udev->descriptor;
5380 retval = usb_get_device_descriptor(udev,
5381 sizeof(udev->descriptor));
5384 "can't read device descriptor %d\n",
5387 if (descriptors_changed(udev, &descriptor,
5390 "device descriptor has changed\n");
5391 /* for disconnect() calls */
5392 udev->descriptor = descriptor;
5394 status = 0; /* Nothing to do */
5398 } else if (udev->state == USB_STATE_SUSPENDED &&
5399 udev->persist_enabled) {
5400 /* For a suspended device, treat this as a
5401 * remote wakeup event.
5403 usb_unlock_port(port_dev);
5404 status = usb_remote_wakeup(udev);
5405 usb_lock_port(port_dev);
5408 /* Don't resuscitate */;
5411 clear_bit(port1, hub->change_bits);
5413 /* successfully revalidated the connection */
5417 usb_unlock_port(port_dev);
5418 hub_port_connect(hub, port1, portstatus, portchange);
5419 usb_lock_port(port_dev);
5422 /* Handle notifying userspace about hub over-current events */
5423 static void port_over_current_notify(struct usb_port *port_dev)
5426 struct device *hub_dev;
5427 char *port_dev_path;
5429 sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count");
5431 hub_dev = port_dev->dev.parent;
5436 port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL);
5440 envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path);
5444 envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u",
5445 port_dev->over_current_count);
5450 kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp);
5456 kfree(port_dev_path);
5459 static void port_event(struct usb_hub *hub, int port1)
5460 __must_hold(&port_dev->status_lock)
5463 struct usb_port *port_dev = hub->ports[port1 - 1];
5464 struct usb_device *udev = port_dev->child;
5465 struct usb_device *hdev = hub->hdev;
5466 u16 portstatus, portchange;
5468 connect_change = test_bit(port1, hub->change_bits);
5469 clear_bit(port1, hub->event_bits);
5470 clear_bit(port1, hub->wakeup_bits);
5472 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
5475 if (portchange & USB_PORT_STAT_C_CONNECTION) {
5476 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
5480 if (portchange & USB_PORT_STAT_C_ENABLE) {
5481 if (!connect_change)
5482 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
5484 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
5487 * EM interference sometimes causes badly shielded USB devices
5488 * to be shutdown by the hub, this hack enables them again.
5489 * Works at least with mouse driver.
5491 if (!(portstatus & USB_PORT_STAT_ENABLE)
5492 && !connect_change && udev) {
5493 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
5498 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
5499 u16 status = 0, unused;
5500 port_dev->over_current_count++;
5501 port_over_current_notify(port_dev);
5503 dev_dbg(&port_dev->dev, "over-current change #%u\n",
5504 port_dev->over_current_count);
5505 usb_clear_port_feature(hdev, port1,
5506 USB_PORT_FEAT_C_OVER_CURRENT);
5507 msleep(100); /* Cool down */
5508 hub_power_on(hub, true);
5509 hub_port_status(hub, port1, &status, &unused);
5510 if (status & USB_PORT_STAT_OVERCURRENT)
5511 dev_err(&port_dev->dev, "over-current condition\n");
5514 if (portchange & USB_PORT_STAT_C_RESET) {
5515 dev_dbg(&port_dev->dev, "reset change\n");
5516 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
5518 if ((portchange & USB_PORT_STAT_C_BH_RESET)
5519 && hub_is_superspeed(hdev)) {
5520 dev_dbg(&port_dev->dev, "warm reset change\n");
5521 usb_clear_port_feature(hdev, port1,
5522 USB_PORT_FEAT_C_BH_PORT_RESET);
5524 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
5525 dev_dbg(&port_dev->dev, "link state change\n");
5526 usb_clear_port_feature(hdev, port1,
5527 USB_PORT_FEAT_C_PORT_LINK_STATE);
5529 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
5530 dev_warn(&port_dev->dev, "config error\n");
5531 usb_clear_port_feature(hdev, port1,
5532 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
5535 /* skip port actions that require the port to be powered on */
5536 if (!pm_runtime_active(&port_dev->dev))
5539 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
5543 * Warm reset a USB3 protocol port if it's in
5544 * SS.Inactive state.
5546 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
5547 dev_dbg(&port_dev->dev, "do warm reset\n");
5548 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
5549 || udev->state == USB_STATE_NOTATTACHED) {
5550 if (hub_port_reset(hub, port1, NULL,
5551 HUB_BH_RESET_TIME, true) < 0)
5552 hub_port_disable(hub, port1, 1);
5554 usb_unlock_port(port_dev);
5555 usb_lock_device(udev);
5556 usb_reset_device(udev);
5557 usb_unlock_device(udev);
5558 usb_lock_port(port_dev);
5564 hub_port_connect_change(hub, port1, portstatus, portchange);
5567 static void hub_event(struct work_struct *work)
5569 struct usb_device *hdev;
5570 struct usb_interface *intf;
5571 struct usb_hub *hub;
5572 struct device *hub_dev;
5577 hub = container_of(work, struct usb_hub, events);
5579 hub_dev = hub->intfdev;
5580 intf = to_usb_interface(hub_dev);
5582 kcov_remote_start_usb((u64)hdev->bus->busnum);
5584 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
5585 hdev->state, hdev->maxchild,
5586 /* NOTE: expects max 15 ports... */
5587 (u16) hub->change_bits[0],
5588 (u16) hub->event_bits[0]);
5590 /* Lock the device, then check to see if we were
5591 * disconnected while waiting for the lock to succeed. */
5592 usb_lock_device(hdev);
5593 if (unlikely(hub->disconnected))
5596 /* If the hub has died, clean up after it */
5597 if (hdev->state == USB_STATE_NOTATTACHED) {
5598 hub->error = -ENODEV;
5599 hub_quiesce(hub, HUB_DISCONNECT);
5604 ret = usb_autopm_get_interface(intf);
5606 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
5610 /* If this is an inactive hub, do nothing */
5615 dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
5617 ret = usb_reset_device(hdev);
5619 dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5627 /* deal with port status changes */
5628 for (i = 1; i <= hdev->maxchild; i++) {
5629 struct usb_port *port_dev = hub->ports[i - 1];
5631 if (test_bit(i, hub->event_bits)
5632 || test_bit(i, hub->change_bits)
5633 || test_bit(i, hub->wakeup_bits)) {
5635 * The get_noresume and barrier ensure that if
5636 * the port was in the process of resuming, we
5637 * flush that work and keep the port active for
5638 * the duration of the port_event(). However,
5639 * if the port is runtime pm suspended
5640 * (powered-off), we leave it in that state, run
5641 * an abbreviated port_event(), and move on.
5643 pm_runtime_get_noresume(&port_dev->dev);
5644 pm_runtime_barrier(&port_dev->dev);
5645 usb_lock_port(port_dev);
5647 usb_unlock_port(port_dev);
5648 pm_runtime_put_sync(&port_dev->dev);
5652 /* deal with hub status changes */
5653 if (test_and_clear_bit(0, hub->event_bits) == 0)
5655 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5656 dev_err(hub_dev, "get_hub_status failed\n");
5658 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5659 dev_dbg(hub_dev, "power change\n");
5660 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
5661 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5662 /* FIXME: Is this always true? */
5663 hub->limited_power = 1;
5665 hub->limited_power = 0;
5667 if (hubchange & HUB_CHANGE_OVERCURRENT) {
5671 dev_dbg(hub_dev, "over-current change\n");
5672 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5673 msleep(500); /* Cool down */
5674 hub_power_on(hub, true);
5675 hub_hub_status(hub, &status, &unused);
5676 if (status & HUB_STATUS_OVERCURRENT)
5677 dev_err(hub_dev, "over-current condition\n");
5682 /* Balance the usb_autopm_get_interface() above */
5683 usb_autopm_put_interface_no_suspend(intf);
5685 usb_unlock_device(hdev);
5687 /* Balance the stuff in kick_hub_wq() and allow autosuspend */
5688 usb_autopm_put_interface(intf);
5689 kref_put(&hub->kref, hub_release);
5694 static const struct usb_device_id hub_id_table[] = {
5695 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5696 | USB_DEVICE_ID_MATCH_PRODUCT
5697 | USB_DEVICE_ID_MATCH_INT_CLASS,
5698 .idVendor = USB_VENDOR_SMSC,
5699 .idProduct = USB_PRODUCT_USB5534B,
5700 .bInterfaceClass = USB_CLASS_HUB,
5701 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5702 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5703 | USB_DEVICE_ID_MATCH_PRODUCT,
5704 .idVendor = USB_VENDOR_CYPRESS,
5705 .idProduct = USB_PRODUCT_CY7C65632,
5706 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
5707 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
5708 | USB_DEVICE_ID_MATCH_INT_CLASS,
5709 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5710 .bInterfaceClass = USB_CLASS_HUB,
5711 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
5712 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5713 .bDeviceClass = USB_CLASS_HUB},
5714 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5715 .bInterfaceClass = USB_CLASS_HUB},
5716 { } /* Terminating entry */
5719 MODULE_DEVICE_TABLE(usb, hub_id_table);
5721 static struct usb_driver hub_driver = {
5724 .disconnect = hub_disconnect,
5725 .suspend = hub_suspend,
5726 .resume = hub_resume,
5727 .reset_resume = hub_reset_resume,
5728 .pre_reset = hub_pre_reset,
5729 .post_reset = hub_post_reset,
5730 .unlocked_ioctl = hub_ioctl,
5731 .id_table = hub_id_table,
5732 .supports_autosuspend = 1,
5735 int usb_hub_init(void)
5737 if (usb_register(&hub_driver) < 0) {
5738 printk(KERN_ERR "%s: can't register hub driver\n",
5744 * The workqueue needs to be freezable to avoid interfering with
5745 * USB-PERSIST port handover. Otherwise it might see that a full-speed
5746 * device was gone before the EHCI controller had handed its port
5747 * over to the companion full-speed controller.
5749 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
5753 /* Fall through if kernel_thread failed */
5754 usb_deregister(&hub_driver);
5755 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
5760 void usb_hub_cleanup(void)
5762 destroy_workqueue(hub_wq);
5765 * Hub resources are freed for us by usb_deregister. It calls
5766 * usb_driver_purge on every device which in turn calls that
5767 * devices disconnect function if it is using this driver.
5768 * The hub_disconnect function takes care of releasing the
5769 * individual hub resources. -greg
5771 usb_deregister(&hub_driver);
5772 } /* usb_hub_cleanup() */
5775 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
5776 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5778 * WARNING - don't use this routine to reset a composite device
5779 * (one with multiple interfaces owned by separate drivers)!
5780 * Use usb_reset_device() instead.
5782 * Do a port reset, reassign the device's address, and establish its
5783 * former operating configuration. If the reset fails, or the device's
5784 * descriptors change from their values before the reset, or the original
5785 * configuration and altsettings cannot be restored, a flag will be set
5786 * telling hub_wq to pretend the device has been disconnected and then
5787 * re-connected. All drivers will be unbound, and the device will be
5788 * re-enumerated and probed all over again.
5790 * Return: 0 if the reset succeeded, -ENODEV if the device has been
5791 * flagged for logical disconnection, or some other negative error code
5792 * if the reset wasn't even attempted.
5795 * The caller must own the device lock and the port lock, the latter is
5796 * taken by usb_reset_device(). For example, it's safe to use
5797 * usb_reset_device() from a driver probe() routine after downloading
5798 * new firmware. For calls that might not occur during probe(), drivers
5799 * should lock the device using usb_lock_device_for_reset().
5801 * Locking exception: This routine may also be called from within an
5802 * autoresume handler. Such usage won't conflict with other tasks
5803 * holding the device lock because these tasks should always call
5804 * usb_autopm_resume_device(), thereby preventing any unwanted
5805 * autoresume. The autoresume handler is expected to have already
5806 * acquired the port lock before calling this routine.
5808 static int usb_reset_and_verify_device(struct usb_device *udev)
5810 struct usb_device *parent_hdev = udev->parent;
5811 struct usb_hub *parent_hub;
5812 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
5813 struct usb_device_descriptor descriptor = udev->descriptor;
5814 struct usb_host_bos *bos;
5816 int port1 = udev->portnum;
5818 if (udev->state == USB_STATE_NOTATTACHED ||
5819 udev->state == USB_STATE_SUSPENDED) {
5820 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5828 parent_hub = usb_hub_to_struct_hub(parent_hdev);
5830 /* Disable USB2 hardware LPM.
5831 * It will be re-enabled by the enumeration process.
5833 usb_disable_usb2_hardware_lpm(udev);
5835 /* Disable LPM while we reset the device and reinstall the alt settings.
5836 * Device-initiated LPM, and system exit latency settings are cleared
5837 * when the device is reset, so we have to set them up again.
5839 ret = usb_unlocked_disable_lpm(udev);
5841 dev_err(&udev->dev, "%s Failed to disable LPM\n", __func__);
5842 goto re_enumerate_no_bos;
5848 for (i = 0; i < PORT_INIT_TRIES; ++i) {
5850 /* ep0 maxpacket size may change; let the HCD know about it.
5851 * Other endpoints will be handled by re-enumeration. */
5852 usb_ep0_reinit(udev);
5853 ret = hub_port_init(parent_hub, udev, port1, i);
5854 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
5861 /* Device might have changed firmware (DFU or similar) */
5862 if (descriptors_changed(udev, &descriptor, bos)) {
5863 dev_info(&udev->dev, "device firmware changed\n");
5864 udev->descriptor = descriptor; /* for disconnect() calls */
5868 /* Restore the device's previous configuration */
5869 if (!udev->actconfig)
5872 mutex_lock(hcd->bandwidth_mutex);
5873 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5875 dev_warn(&udev->dev,
5876 "Busted HC? Not enough HCD resources for "
5877 "old configuration.\n");
5878 mutex_unlock(hcd->bandwidth_mutex);
5881 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5882 USB_REQ_SET_CONFIGURATION, 0,
5883 udev->actconfig->desc.bConfigurationValue, 0,
5884 NULL, 0, USB_CTRL_SET_TIMEOUT);
5887 "can't restore configuration #%d (error=%d)\n",
5888 udev->actconfig->desc.bConfigurationValue, ret);
5889 mutex_unlock(hcd->bandwidth_mutex);
5892 mutex_unlock(hcd->bandwidth_mutex);
5893 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5895 /* Put interfaces back into the same altsettings as before.
5896 * Don't bother to send the Set-Interface request for interfaces
5897 * that were already in altsetting 0; besides being unnecessary,
5898 * many devices can't handle it. Instead just reset the host-side
5901 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
5902 struct usb_host_config *config = udev->actconfig;
5903 struct usb_interface *intf = config->interface[i];
5904 struct usb_interface_descriptor *desc;
5906 desc = &intf->cur_altsetting->desc;
5907 if (desc->bAlternateSetting == 0) {
5908 usb_disable_interface(udev, intf, true);
5909 usb_enable_interface(udev, intf, true);
5912 /* Let the bandwidth allocation function know that this
5913 * device has been reset, and it will have to use
5914 * alternate setting 0 as the current alternate setting.
5916 intf->resetting_device = 1;
5917 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5918 desc->bAlternateSetting);
5919 intf->resetting_device = 0;
5922 dev_err(&udev->dev, "failed to restore interface %d "
5923 "altsetting %d (error=%d)\n",
5924 desc->bInterfaceNumber,
5925 desc->bAlternateSetting,
5929 /* Resetting also frees any allocated streams */
5930 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5931 intf->cur_altsetting->endpoint[j].streams = 0;
5935 /* Now that the alt settings are re-installed, enable LTM and LPM. */
5936 usb_enable_usb2_hardware_lpm(udev);
5937 usb_unlocked_enable_lpm(udev);
5938 usb_enable_ltm(udev);
5939 usb_release_bos_descriptor(udev);
5944 usb_release_bos_descriptor(udev);
5946 re_enumerate_no_bos:
5947 /* LPM state doesn't matter when we're about to destroy the device. */
5948 hub_port_logical_disconnect(parent_hub, port1);
5953 * usb_reset_device - warn interface drivers and perform a USB port reset
5954 * @udev: device to reset (not in NOTATTACHED state)
5956 * Warns all drivers bound to registered interfaces (using their pre_reset
5957 * method), performs the port reset, and then lets the drivers know that
5958 * the reset is over (using their post_reset method).
5960 * Return: The same as for usb_reset_and_verify_device().
5963 * The caller must own the device lock. For example, it's safe to use
5964 * this from a driver probe() routine after downloading new firmware.
5965 * For calls that might not occur during probe(), drivers should lock
5966 * the device using usb_lock_device_for_reset().
5968 * If an interface is currently being probed or disconnected, we assume
5969 * its driver knows how to handle resets. For all other interfaces,
5970 * if the driver doesn't have pre_reset and post_reset methods then
5971 * we attempt to unbind it and rebind afterward.
5973 int usb_reset_device(struct usb_device *udev)
5977 unsigned int noio_flag;
5978 struct usb_port *port_dev;
5979 struct usb_host_config *config = udev->actconfig;
5980 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
5982 if (udev->state == USB_STATE_NOTATTACHED) {
5983 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5988 if (!udev->parent) {
5989 /* this requires hcd-specific logic; see ohci_restart() */
5990 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5994 port_dev = hub->ports[udev->portnum - 1];
5997 * Don't allocate memory with GFP_KERNEL in current
5998 * context to avoid possible deadlock if usb mass
5999 * storage interface or usbnet interface(iSCSI case)
6000 * is included in current configuration. The easist
6001 * approach is to do it for every device reset,
6002 * because the device 'memalloc_noio' flag may have
6003 * not been set before reseting the usb device.
6005 noio_flag = memalloc_noio_save();
6007 /* Prevent autosuspend during the reset */
6008 usb_autoresume_device(udev);
6011 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
6012 struct usb_interface *cintf = config->interface[i];
6013 struct usb_driver *drv;
6016 if (cintf->dev.driver) {
6017 drv = to_usb_driver(cintf->dev.driver);
6018 if (drv->pre_reset && drv->post_reset)
6019 unbind = (drv->pre_reset)(cintf);
6020 else if (cintf->condition ==
6021 USB_INTERFACE_BOUND)
6024 usb_forced_unbind_intf(cintf);
6029 usb_lock_port(port_dev);
6030 ret = usb_reset_and_verify_device(udev);
6031 usb_unlock_port(port_dev);
6034 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
6035 struct usb_interface *cintf = config->interface[i];
6036 struct usb_driver *drv;
6037 int rebind = cintf->needs_binding;
6039 if (!rebind && cintf->dev.driver) {
6040 drv = to_usb_driver(cintf->dev.driver);
6041 if (drv->post_reset)
6042 rebind = (drv->post_reset)(cintf);
6043 else if (cintf->condition ==
6044 USB_INTERFACE_BOUND)
6047 cintf->needs_binding = 1;
6051 /* If the reset failed, hub_wq will unbind drivers later */
6053 usb_unbind_and_rebind_marked_interfaces(udev);
6056 usb_autosuspend_device(udev);
6057 memalloc_noio_restore(noio_flag);
6060 EXPORT_SYMBOL_GPL(usb_reset_device);
6064 * usb_queue_reset_device - Reset a USB device from an atomic context
6065 * @iface: USB interface belonging to the device to reset
6067 * This function can be used to reset a USB device from an atomic
6068 * context, where usb_reset_device() won't work (as it blocks).
6070 * Doing a reset via this method is functionally equivalent to calling
6071 * usb_reset_device(), except for the fact that it is delayed to a
6072 * workqueue. This means that any drivers bound to other interfaces
6073 * might be unbound, as well as users from usbfs in user space.
6077 * - Scheduling two resets at the same time from two different drivers
6078 * attached to two different interfaces of the same device is
6079 * possible; depending on how the driver attached to each interface
6080 * handles ->pre_reset(), the second reset might happen or not.
6082 * - If the reset is delayed so long that the interface is unbound from
6083 * its driver, the reset will be skipped.
6085 * - This function can be called during .probe(). It can also be called
6086 * during .disconnect(), but doing so is pointless because the reset
6087 * will not occur. If you really want to reset the device during
6088 * .disconnect(), call usb_reset_device() directly -- but watch out
6089 * for nested unbinding issues!
6091 void usb_queue_reset_device(struct usb_interface *iface)
6093 if (schedule_work(&iface->reset_ws))
6094 usb_get_intf(iface);
6096 EXPORT_SYMBOL_GPL(usb_queue_reset_device);
6099 * usb_hub_find_child - Get the pointer of child device
6100 * attached to the port which is specified by @port1.
6101 * @hdev: USB device belonging to the usb hub
6102 * @port1: port num to indicate which port the child device
6105 * USB drivers call this function to get hub's child device
6108 * Return: %NULL if input param is invalid and
6109 * child's usb_device pointer if non-NULL.
6111 struct usb_device *usb_hub_find_child(struct usb_device *hdev,
6114 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6116 if (port1 < 1 || port1 > hdev->maxchild)
6118 return hub->ports[port1 - 1]->child;
6120 EXPORT_SYMBOL_GPL(usb_hub_find_child);
6122 void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
6123 struct usb_hub_descriptor *desc)
6125 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6126 enum usb_port_connect_type connect_type;
6132 if (!hub_is_superspeed(hdev)) {
6133 for (i = 1; i <= hdev->maxchild; i++) {
6134 struct usb_port *port_dev = hub->ports[i - 1];
6136 connect_type = port_dev->connect_type;
6137 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
6138 u8 mask = 1 << (i%8);
6140 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
6141 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
6142 desc->u.hs.DeviceRemovable[i/8] |= mask;
6147 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
6149 for (i = 1; i <= hdev->maxchild; i++) {
6150 struct usb_port *port_dev = hub->ports[i - 1];
6152 connect_type = port_dev->connect_type;
6153 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
6156 if (!(port_removable & mask)) {
6157 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
6158 port_removable |= mask;
6163 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
6169 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
6170 * @hdev: USB device belonging to the usb hub
6171 * @port1: port num of the port
6173 * Return: Port's acpi handle if successful, %NULL if params are
6176 acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
6179 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
6184 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);