Lines Matching +full:usb +full:- +full:hub
1 // SPDX-License-Identifier: GPL-2.0
3 * USB hub driver.
24 #include <linux/usb.h>
26 #include <linux/usb/hcd.h>
27 #include <linux/usb/onboard_dev.h>
28 #include <linux/usb/otg.h>
29 #include <linux/usb/quirks.h>
40 #include "hub.h"
71 /* Protect struct usb_device->state and ->children members
72 * Note: Both are also protected by ->dev.sem, except that ->state can
76 /* workqueue to process hub events */
80 /* synchronize hub-port add/remove and peering operations */
90 * 10 seconds to send reply for the initial 64-byte descriptor request.
92 /* define initial 64-byte descriptor request timeout in milliseconds */
96 "initial 64-byte descriptor request timeout in milliseconds "
97 "(default 5000 - 5.0 seconds)");
100 * As of 2.6.10 we introduce a new USB device initialization scheme which
108 * hub driver's behavior. On the first initialization attempt, if the
135 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
136 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
139 static inline char *portspeed(struct usb_hub *hub, int portstatus) in portspeed() argument
141 if (hub_is_superspeedplus(hub->hdev)) in portspeed()
143 if (hub_is_superspeed(hub->hdev)) in portspeed()
156 if (!hdev || !hdev->actconfig || !hdev->maxchild) in usb_hub_to_struct_hub()
158 return usb_get_intfdata(hdev->actconfig->interface[0]); in usb_hub_to_struct_hub()
164 if (udev->quirks & USB_QUIRK_NO_LPM) in usb_device_supports_lpm()
168 if (!udev->bos) in usb_device_supports_lpm()
171 /* USB 2.1 (and greater) devices indicate LPM support through in usb_device_supports_lpm()
172 * their USB 2.0 Extended Capabilities BOS descriptor. in usb_device_supports_lpm()
174 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) { in usb_device_supports_lpm()
175 if (udev->bos->ext_cap && in usb_device_supports_lpm()
177 le32_to_cpu(udev->bos->ext_cap->bmAttributes))) in usb_device_supports_lpm()
183 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM. in usb_device_supports_lpm()
187 if (!udev->bos->ss_cap) { in usb_device_supports_lpm()
188 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n"); in usb_device_supports_lpm()
192 if (udev->bos->ss_cap->bU1devExitLat == 0 && in usb_device_supports_lpm()
193 udev->bos->ss_cap->bU2DevExitLat == 0) { in usb_device_supports_lpm()
194 if (udev->parent) in usb_device_supports_lpm()
195 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n"); in usb_device_supports_lpm()
197 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n"); in usb_device_supports_lpm()
201 if (!udev->parent || udev->parent->lpm_capable) in usb_device_supports_lpm()
209 * See USB 3.1 section C.1.5.2
214 struct usb_hub *hub, in usb_set_lpm_mel() argument
224 * and the hub header decode latency. See USB 3.1 section C 2.2.1 in usb_set_lpm_mel()
227 total_mel = hub_lpm_params->mel + in usb_set_lpm_mel()
229 hub->descriptor->u.ss.bHubHdrDecLat * 100; in usb_set_lpm_mel()
233 * each link + wHubDelay for each hub. Add only for last link. in usb_set_lpm_mel()
237 total_mel += (__le16_to_cpu(hub->descriptor->u.ss.wHubDelay) + in usb_set_lpm_mel()
242 * after receiving PING. Also add 2100ns as stated in USB 3.1 C 1.5.2.4 in usb_set_lpm_mel()
248 if (!hub->hdev->parent) in usb_set_lpm_mel()
251 udev_lpm_params->mel = total_mel; in usb_set_lpm_mel()
261 struct usb_hub *hub, in usb_set_lpm_pel() argument
271 * device and the parent hub into U0. The exit latency is the bigger of in usb_set_lpm_pel()
272 * the device exit latency or the hub exit latency. in usb_set_lpm_pel()
280 * When the hub starts to receive the LFPS, there is a slight delay for in usb_set_lpm_pel()
283 * delay, plus the PEL that we calculated for this hub. in usb_set_lpm_pel()
285 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; in usb_set_lpm_pel()
288 * According to figure C-7 in the USB 3.0 spec, the PEL for this device in usb_set_lpm_pel()
292 udev_lpm_params->pel = first_link_pel; in usb_set_lpm_pel()
294 udev_lpm_params->pel = hub_pel; in usb_set_lpm_pel()
298 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
303 * - t1: device PEL
304 * - t2: time for the ERDY to make it from the device to the host.
305 * - t3: a host-specific delay to process the ERDY.
306 * - t4: time for the packet to make it from the host to the device.
320 total_sel = udev_lpm_params->pel; in usb_set_lpm_sel()
322 for (parent = udev->parent, num_hubs = 0; parent->parent; in usb_set_lpm_sel()
323 parent = parent->parent) in usb_set_lpm_sel()
325 /* t2 = 2.1us + 250ns * (num_hubs - 1) */ in usb_set_lpm_sel()
327 total_sel += 2100 + 250 * (num_hubs - 1); in usb_set_lpm_sel()
332 udev_lpm_params->sel = total_sel; in usb_set_lpm_sel()
337 struct usb_hub *hub; in usb_set_lpm_parameters() local
344 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER) in usb_set_lpm_parameters()
348 if (!udev->bos) in usb_set_lpm_parameters()
351 hub = usb_hub_to_struct_hub(udev->parent); in usb_set_lpm_parameters()
355 if (!hub) in usb_set_lpm_parameters()
358 udev_u1_del = udev->bos->ss_cap->bU1devExitLat; in usb_set_lpm_parameters()
359 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat); in usb_set_lpm_parameters()
360 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; in usb_set_lpm_parameters()
361 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat); in usb_set_lpm_parameters()
363 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, in usb_set_lpm_parameters()
364 hub, &udev->parent->u1_params, hub_u1_del); in usb_set_lpm_parameters()
366 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, in usb_set_lpm_parameters()
367 hub, &udev->parent->u2_params, hub_u2_del); in usb_set_lpm_parameters()
371 * when the parent hub notices the downstream port is trying to in usb_set_lpm_parameters()
372 * transition to U0 to when the hub initiates a U0 transition on its in usb_set_lpm_parameters()
376 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking in usb_set_lpm_parameters()
379 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I in usb_set_lpm_parameters()
380 * assume the device exit latencies they are talking about are the hub in usb_set_lpm_parameters()
388 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, in usb_set_lpm_parameters()
389 hub, &udev->parent->u1_params, hub_u1_del, in usb_set_lpm_parameters()
393 port_to_port_delay = 1 + hub_u2_del - hub_u1_del; in usb_set_lpm_parameters()
397 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, in usb_set_lpm_parameters()
398 hub, &udev->parent->u2_params, hub_u2_del, in usb_set_lpm_parameters()
402 usb_set_lpm_sel(udev, &udev->u1_params); in usb_set_lpm_parameters()
403 usb_set_lpm_sel(udev, &udev->u2_params); in usb_set_lpm_parameters()
406 /* USB 2.0 spec Section 11.24.4.5 */
431 size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1; in get_hub_descriptor()
433 return -EMSGSIZE; in get_hub_descriptor()
437 return -EINVAL; in get_hub_descriptor()
441 * USB 2.0 spec Section 11.24.2.1
450 * USB 2.0 spec Section 11.24.2.2
460 * USB 2.0 spec Section 11.24.2.13
486 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
489 static void set_port_led(struct usb_hub *hub, int port1, int selector) in set_port_led() argument
491 struct usb_port *port_dev = hub->ports[port1 - 1]; in set_port_led()
494 status = set_port_feature(hub->hdev, (selector << 8) | port1, in set_port_led()
496 dev_dbg(&port_dev->dev, "indicator %s status %d\n", in set_port_led()
504 struct usb_hub *hub = in led_work() local
506 struct usb_device *hdev = hub->hdev; in led_work()
509 int cursor = -1; in led_work()
511 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) in led_work()
514 for (i = 0; i < hdev->maxchild; i++) { in led_work()
517 /* 30%-50% duty cycle */ in led_work()
519 switch (hub->indicator[i]) { in led_work()
558 set_port_led(hub, i + 1, selector); in led_work()
559 hub->indicator[i] = mode; in led_work()
563 cursor %= hdev->maxchild; in led_work()
564 set_port_led(hub, cursor + 1, HUB_LED_GREEN); in led_work()
565 hub->indicator[cursor] = INDICATOR_CYCLE; in led_work()
570 &hub->leds, LED_CYCLE_PERIOD); in led_work()
573 /* use a short timeout for hub/port status fetches */
578 * USB 2.0 spec Section 11.24.2.6
583 int i, status = -ETIMEDOUT; in get_hub_status()
586 (status == -ETIMEDOUT || status == -EPIPE); i++) { in get_hub_status()
595 * USB 2.0 spec Section 11.24.2.7
596 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
601 int i, status = -ETIMEDOUT; in get_port_status()
604 (status == -ETIMEDOUT || status == -EPIPE); i++) { in get_port_status()
612 static int hub_ext_port_status(struct usb_hub *hub, int port1, int type, in hub_ext_port_status() argument
621 mutex_lock(&hub->status_mutex); in hub_ext_port_status()
622 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len); in hub_ext_port_status()
624 if (ret != -ENODEV) in hub_ext_port_status()
625 dev_err(hub->intfdev, in hub_ext_port_status()
628 ret = -EIO; in hub_ext_port_status()
630 *status = le16_to_cpu(hub->status->port.wPortStatus); in hub_ext_port_status()
631 *change = le16_to_cpu(hub->status->port.wPortChange); in hub_ext_port_status()
634 hub->status->port.dwExtPortStatus); in hub_ext_port_status()
637 mutex_unlock(&hub->status_mutex); in hub_ext_port_status()
641 * protects hub->status, and the phy driver only checks the port in hub_ext_port_status()
645 struct usb_device *hdev = hub->hdev; in hub_ext_port_status()
649 * since the USB PHY only cares about changes at the next in hub_ext_port_status()
653 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); in hub_ext_port_status()
660 usb_phy_roothub_notify_connect(hcd->phy_roothub, port1 - 1); in hub_ext_port_status()
662 usb_phy_roothub_notify_disconnect(hcd->phy_roothub, port1 - 1); in hub_ext_port_status()
669 int usb_hub_port_status(struct usb_hub *hub, int port1, in usb_hub_port_status() argument
672 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS, in usb_hub_port_status()
676 static void hub_resubmit_irq_urb(struct usb_hub *hub) in hub_resubmit_irq_urb() argument
681 spin_lock_irqsave(&hub->irq_urb_lock, flags); in hub_resubmit_irq_urb()
683 if (hub->quiescing) { in hub_resubmit_irq_urb()
684 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); in hub_resubmit_irq_urb()
688 status = usb_submit_urb(hub->urb, GFP_ATOMIC); in hub_resubmit_irq_urb()
689 if (status && status != -ENODEV && status != -EPERM && in hub_resubmit_irq_urb()
690 status != -ESHUTDOWN) { in hub_resubmit_irq_urb()
691 dev_err(hub->intfdev, "resubmit --> %d\n", status); in hub_resubmit_irq_urb()
692 mod_timer(&hub->irq_urb_retry, jiffies + HZ); in hub_resubmit_irq_urb()
695 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); in hub_resubmit_irq_urb()
700 struct usb_hub *hub = from_timer(hub, t, irq_urb_retry); in hub_retry_irq_urb() local
702 hub_resubmit_irq_urb(hub); in hub_retry_irq_urb()
706 static void kick_hub_wq(struct usb_hub *hub) in kick_hub_wq() argument
710 if (hub->disconnected || work_pending(&hub->events)) in kick_hub_wq()
718 * work for this hub. Therefore put the interface either when in kick_hub_wq()
721 intf = to_usb_interface(hub->intfdev); in kick_hub_wq()
723 hub_get(hub); in kick_hub_wq()
725 if (queue_work(hub_wq, &hub->events)) in kick_hub_wq()
730 hub_put(hub); in kick_hub_wq()
735 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_kick_hub_wq() local
737 if (hub) in usb_kick_hub_wq()
738 kick_hub_wq(hub); in usb_kick_hub_wq()
742 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
745 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
746 * device initiates resume, so the USB core will not receive notice of the
747 * resume through the normal hub interrupt URB.
752 struct usb_hub *hub; in usb_wakeup_notification() local
758 hub = usb_hub_to_struct_hub(hdev); in usb_wakeup_notification()
759 if (hub) { in usb_wakeup_notification()
760 port_dev = hub->ports[portnum - 1]; in usb_wakeup_notification()
761 if (port_dev && port_dev->child) in usb_wakeup_notification()
762 pm_wakeup_event(&port_dev->child->dev, 0); in usb_wakeup_notification()
764 set_bit(portnum, hub->wakeup_bits); in usb_wakeup_notification()
765 kick_hub_wq(hub); in usb_wakeup_notification()
773 struct usb_hub *hub = urb->context; in hub_irq() local
774 int status = urb->status; in hub_irq()
779 case -ENOENT: /* synchronous unlink */ in hub_irq()
780 case -ECONNRESET: /* async unlink */ in hub_irq()
781 case -ESHUTDOWN: /* hardware going away */ in hub_irq()
785 /* Cause a hub reset after 10 consecutive errors */ in hub_irq()
786 dev_dbg(hub->intfdev, "transfer --> %d\n", status); in hub_irq()
787 if ((++hub->nerrors < 10) || hub->error) in hub_irq()
789 hub->error = status; in hub_irq()
795 for (i = 0; i < urb->actual_length; ++i) in hub_irq()
796 bits |= ((unsigned long) ((*hub->buffer)[i])) in hub_irq()
798 hub->event_bits[0] = bits; in hub_irq()
802 hub->nerrors = 0; in hub_irq()
805 kick_hub_wq(hub); in hub_irq()
808 hub_resubmit_irq_urb(hub); in hub_irq()
811 /* USB 2.0 spec Section 11.24.2.3 */
833 * both can talk to the same hub concurrently.
837 struct usb_hub *hub = in hub_tt_work() local
841 spin_lock_irqsave(&hub->tt.lock, flags); in hub_tt_work()
842 while (!list_empty(&hub->tt.clear_list)) { in hub_tt_work()
845 struct usb_device *hdev = hub->hdev; in hub_tt_work()
849 next = hub->tt.clear_list.next; in hub_tt_work()
851 list_del(&clear->clear_list); in hub_tt_work()
854 spin_unlock_irqrestore(&hub->tt.lock, flags); in hub_tt_work()
855 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt); in hub_tt_work()
856 if (status && status != -ENODEV) in hub_tt_work()
857 dev_err(&hdev->dev, in hub_tt_work()
859 clear->tt, clear->devinfo, status); in hub_tt_work()
862 drv = clear->hcd->driver; in hub_tt_work()
863 if (drv->clear_tt_buffer_complete) in hub_tt_work()
864 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); in hub_tt_work()
867 spin_lock_irqsave(&hub->tt.lock, flags); in hub_tt_work()
869 spin_unlock_irqrestore(&hub->tt.lock, flags); in hub_tt_work()
873 * usb_hub_set_port_power - control hub port's power state
874 * @hdev: USB device belonging to the usb hub
875 * @hub: target hub
884 int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, in usb_hub_set_port_power() argument
898 set_bit(port1, hub->power_bits); in usb_hub_set_port_power()
900 clear_bit(port1, hub->power_bits); in usb_hub_set_port_power()
905 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
908 * High speed HCDs use this to tell the hub driver that some split control or
913 * It may not be possible for that hub to handle additional full (or low)
920 struct usb_device *udev = urb->dev; in usb_hub_clear_tt_buffer()
921 int pipe = urb->pipe; in usb_hub_clear_tt_buffer()
922 struct usb_tt *tt = udev->tt; in usb_hub_clear_tt_buffer()
928 * there can be many TTs per hub). even if they're uncommon. in usb_hub_clear_tt_buffer()
932 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); in usb_hub_clear_tt_buffer()
934 return -ENOMEM; in usb_hub_clear_tt_buffer()
938 clear->tt = tt->multi ? udev->ttport : 1; in usb_hub_clear_tt_buffer()
939 clear->devinfo = usb_pipeendpoint (pipe); in usb_hub_clear_tt_buffer()
940 clear->devinfo |= ((u16)udev->devaddr) << 4; in usb_hub_clear_tt_buffer()
941 clear->devinfo |= usb_pipecontrol(pipe) in usb_hub_clear_tt_buffer()
945 clear->devinfo |= 1 << 15; in usb_hub_clear_tt_buffer()
948 clear->hcd = bus_to_hcd(udev->bus); in usb_hub_clear_tt_buffer()
949 clear->ep = urb->ep; in usb_hub_clear_tt_buffer()
952 spin_lock_irqsave(&tt->lock, flags); in usb_hub_clear_tt_buffer()
953 list_add_tail(&clear->clear_list, &tt->clear_list); in usb_hub_clear_tt_buffer()
954 schedule_work(&tt->clear_work); in usb_hub_clear_tt_buffer()
955 spin_unlock_irqrestore(&tt->lock, flags); in usb_hub_clear_tt_buffer()
960 static void hub_power_on(struct usb_hub *hub, bool do_delay) in hub_power_on() argument
966 * USB 2.0 hubs. Some hubs do not implement port-power switching in hub_power_on()
968 * unless we send these messages to the hub. in hub_power_on()
970 if (hub_is_port_power_switchable(hub)) in hub_power_on()
971 dev_dbg(hub->intfdev, "enabling power on all ports\n"); in hub_power_on()
973 dev_dbg(hub->intfdev, "trying to enable port power on " in hub_power_on()
974 "non-switchable hub\n"); in hub_power_on()
975 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) in hub_power_on()
976 if (test_bit(port1, hub->power_bits)) in hub_power_on()
977 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); in hub_power_on()
979 usb_clear_port_feature(hub->hdev, port1, in hub_power_on()
982 msleep(hub_power_on_good_delay(hub)); in hub_power_on()
985 static int hub_hub_status(struct usb_hub *hub, in hub_hub_status() argument
990 mutex_lock(&hub->status_mutex); in hub_hub_status()
991 ret = get_hub_status(hub->hdev, &hub->status->hub); in hub_hub_status()
993 if (ret != -ENODEV) in hub_hub_status()
994 dev_err(hub->intfdev, in hub_hub_status()
997 *status = le16_to_cpu(hub->status->hub.wHubStatus); in hub_hub_status()
998 *change = le16_to_cpu(hub->status->hub.wHubChange); in hub_hub_status()
1001 mutex_unlock(&hub->status_mutex); in hub_hub_status()
1005 static int hub_set_port_link_state(struct usb_hub *hub, int port1, in hub_set_port_link_state() argument
1008 return set_port_feature(hub->hdev, in hub_set_port_link_state()
1014 * Disable a port and mark a logical connect-change event, so that some
1016 * and will re-enumerate if there actually is a device attached.
1018 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) in hub_port_logical_disconnect() argument
1020 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n"); in hub_port_logical_disconnect()
1021 hub_port_disable(hub, port1, 1); in hub_port_logical_disconnect()
1024 * - some devices won't enumerate without a VBUS power cycle in hub_port_logical_disconnect()
1025 * - SRP saves power that way in hub_port_logical_disconnect()
1026 * - ... new call, TBD ... in hub_port_logical_disconnect()
1027 * That's easy if this hub can switch power per-port, and in hub_port_logical_disconnect()
1032 set_bit(port1, hub->change_bits); in hub_port_logical_disconnect()
1033 kick_hub_wq(hub); in hub_port_logical_disconnect()
1037 * usb_remove_device - disable a device's port on its parent hub
1050 struct usb_hub *hub; in usb_remove_device() local
1054 if (!udev->parent) /* Can't remove a root hub */ in usb_remove_device()
1055 return -EINVAL; in usb_remove_device()
1056 hub = usb_hub_to_struct_hub(udev->parent); in usb_remove_device()
1057 intf = to_usb_interface(hub->intfdev); in usb_remove_device()
1063 set_bit(udev->portnum, hub->removed_bits); in usb_remove_device()
1064 hub_port_logical_disconnect(hub, udev->portnum); in usb_remove_device()
1077 static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) in hub_activate() argument
1079 struct usb_device *hdev = hub->hdev; in hub_activate()
1089 device_lock(&hdev->dev); in hub_activate()
1091 /* Was the hub disconnected while we were waiting? */ in hub_activate()
1092 if (hub->disconnected) in hub_activate()
1098 hub_get(hub); in hub_activate()
1100 /* The superspeed hub except for root hub has to use Hub Depth in hub_activate()
1102 * it uses to determine the downstream port number. So hub driver in hub_activate()
1103 * should send a set hub depth request to superspeed hub after in hub_activate()
1104 * the superspeed hub is set configuration in initialization or in hub_activate()
1111 if (hdev->parent && hub_is_superspeed(hdev)) { in hub_activate()
1114 hdev->level - 1, 0, NULL, 0, in hub_activate()
1117 dev_err(hub->intfdev, in hub_activate()
1118 "set hub depth failed\n"); in hub_activate()
1122 * hub's initial power-up delays. This is pretty awkward in hub_activate()
1123 * and the implementation looks like a home-brewed sort of in hub_activate()
1125 * root hub (assuming usbcore is compiled into the kernel in hub_activate()
1134 delay = hub_power_on_good_delay(hub); in hub_activate()
1136 hub_power_on(hub, false); in hub_activate()
1137 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); in hub_activate()
1139 &hub->init_work, in hub_activate()
1144 to_usb_interface(hub->intfdev)); in hub_activate()
1147 /* The internal host controller state for the hub device in hub_activate()
1149 * Update the device's info so the HW knows it's a hub. in hub_activate()
1151 hcd = bus_to_hcd(hdev->bus); in hub_activate()
1152 if (hcd->driver->update_hub_device) { in hub_activate()
1153 ret = hcd->driver->update_hub_device(hcd, hdev, in hub_activate()
1154 &hub->tt, GFP_NOIO); in hub_activate()
1156 dev_err(hub->intfdev, in hub_activate()
1157 "Host not accepting hub info update\n"); in hub_activate()
1158 dev_err(hub->intfdev, in hub_activate()
1159 "LS/FS devices and hubs may not work under this hub\n"); in hub_activate()
1162 hub_power_on(hub, true); in hub_activate()
1164 hub_power_on(hub, true); in hub_activate()
1167 } else if (hub_is_superspeed(hub->hdev)) in hub_activate()
1173 * Check each port and set hub->change_bits to let hub_wq know in hub_activate()
1176 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { in hub_activate()
1177 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_activate()
1178 struct usb_device *udev = port_dev->child; in hub_activate()
1182 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in hub_activate()
1187 dev_dbg(&port_dev->dev, "status %04x change %04x\n", in hub_activate()
1200 udev->state == USB_STATE_NOTATTACHED)) { in hub_activate()
1213 /* Make sure a warm-reset request is handled by port_event */ in hub_activate()
1215 hub_port_warm_reset_required(hub, port1, portstatus)) in hub_activate()
1216 set_bit(port1, hub->event_bits); in hub_activate()
1228 /* Clear status-change flags; we'll debounce later */ in hub_activate()
1231 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1236 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1241 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1245 hub_is_superspeed(hub->hdev)) { in hub_activate()
1247 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1255 clear_bit(port1, hub->removed_bits); in hub_activate()
1257 if (!udev || udev->state == USB_STATE_NOTATTACHED) { in hub_activate()
1268 set_bit(port1, hub->change_bits); in hub_activate()
1278 * for USB 3.0 hubs, since they don't have a suspend in hub_activate()
1280 * bit on device-initiated resume. in hub_activate()
1282 if (portchange || (hub_is_superspeed(hub->hdev) && in hub_activate()
1284 set_bit(port1, hub->event_bits); in hub_activate()
1286 } else if (udev->persist_enabled) { in hub_activate()
1288 udev->reset_resume = 1; in hub_activate()
1293 if (test_bit(port1, hub->power_bits)) in hub_activate()
1294 set_bit(port1, hub->change_bits); in hub_activate()
1299 set_bit(port1, hub->change_bits); in hub_activate()
1303 /* If no port-status-change flags were set, we don't need any in hub_activate()
1308 * If any port-status changes do occur during this delay, hub_wq in hub_activate()
1316 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3); in hub_activate()
1318 &hub->init_work, in hub_activate()
1320 device_unlock(&hdev->dev); in hub_activate()
1327 hub->quiescing = 0; in hub_activate()
1329 status = usb_submit_urb(hub->urb, GFP_NOIO); in hub_activate()
1331 dev_err(hub->intfdev, "activate --> %d\n", status); in hub_activate()
1332 if (hub->has_indicators && blinkenlights) in hub_activate()
1334 &hub->leds, LED_CYCLE_PERIOD); in hub_activate()
1337 kick_hub_wq(hub); in hub_activate()
1342 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); in hub_activate()
1343 device_unlock(&hdev->dev); in hub_activate()
1346 hub_put(hub); in hub_activate()
1352 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); in hub_init_func2() local
1354 hub_activate(hub, HUB_INIT2); in hub_init_func2()
1359 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); in hub_init_func3() local
1361 hub_activate(hub, HUB_INIT3); in hub_init_func3()
1368 static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) in hub_quiesce() argument
1370 struct usb_device *hdev = hub->hdev; in hub_quiesce()
1374 /* hub_wq and related activity won't re-trigger */ in hub_quiesce()
1375 spin_lock_irqsave(&hub->irq_urb_lock, flags); in hub_quiesce()
1376 hub->quiescing = 1; in hub_quiesce()
1377 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); in hub_quiesce()
1381 for (i = 0; i < hdev->maxchild; ++i) { in hub_quiesce()
1382 if (hub->ports[i]->child) in hub_quiesce()
1383 usb_disconnect(&hub->ports[i]->child); in hub_quiesce()
1388 timer_delete_sync(&hub->irq_urb_retry); in hub_quiesce()
1389 usb_kill_urb(hub->urb); in hub_quiesce()
1390 if (hub->has_indicators) in hub_quiesce()
1391 cancel_delayed_work_sync(&hub->leds); in hub_quiesce()
1392 if (hub->tt.hub) in hub_quiesce()
1393 flush_work(&hub->tt.clear_work); in hub_quiesce()
1396 static void hub_pm_barrier_for_all_ports(struct usb_hub *hub) in hub_pm_barrier_for_all_ports() argument
1400 for (i = 0; i < hub->hdev->maxchild; ++i) in hub_pm_barrier_for_all_ports()
1401 pm_runtime_barrier(&hub->ports[i]->dev); in hub_pm_barrier_for_all_ports()
1404 /* caller has locked the hub device */
1407 struct usb_hub *hub = usb_get_intfdata(intf); in hub_pre_reset() local
1409 hub_quiesce(hub, HUB_PRE_RESET); in hub_pre_reset()
1410 hub->in_reset = 1; in hub_pre_reset()
1411 hub_pm_barrier_for_all_ports(hub); in hub_pre_reset()
1415 /* caller has locked the hub device */
1418 struct usb_hub *hub = usb_get_intfdata(intf); in hub_post_reset() local
1420 hub->in_reset = 0; in hub_post_reset()
1421 hub_pm_barrier_for_all_ports(hub); in hub_post_reset()
1422 hub_activate(hub, HUB_POST_RESET); in hub_post_reset()
1426 static int hub_configure(struct usb_hub *hub, in hub_configure() argument
1430 struct usb_device *hdev = hub->hdev; in hub_configure()
1431 struct device *hub_dev = hub->intfdev; in hub_configure()
1441 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); in hub_configure()
1442 if (!hub->buffer) { in hub_configure()
1443 ret = -ENOMEM; in hub_configure()
1447 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); in hub_configure()
1448 if (!hub->status) { in hub_configure()
1449 ret = -ENOMEM; in hub_configure()
1452 mutex_init(&hub->status_mutex); in hub_configure()
1454 hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL); in hub_configure()
1455 if (!hub->descriptor) { in hub_configure()
1456 ret = -ENOMEM; in hub_configure()
1460 /* Request the entire hub descriptor. in hub_configure()
1461 * hub->descriptor can handle USB_MAXCHILDREN ports, in hub_configure()
1462 * but a (non-SS) hub can/will return fewer bytes here. in hub_configure()
1464 ret = get_hub_descriptor(hdev, hub->descriptor); in hub_configure()
1466 message = "can't read hub descriptor"; in hub_configure()
1474 if (hub->descriptor->bNbrPorts > maxchild) { in hub_configure()
1475 message = "hub has too many ports!"; in hub_configure()
1476 ret = -ENODEV; in hub_configure()
1478 } else if (hub->descriptor->bNbrPorts == 0) { in hub_configure()
1479 message = "hub doesn't have any ports!"; in hub_configure()
1480 ret = -ENODEV; in hub_configure()
1485 * Accumulate wHubDelay + 40ns for every hub in the tree of devices. in hub_configure()
1489 u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay); in hub_configure()
1491 if (hdev->parent) in hub_configure()
1492 delay += hdev->parent->hub_delay; in hub_configure()
1495 hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX); in hub_configure()
1498 maxchild = hub->descriptor->bNbrPorts; in hub_configure()
1502 hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL); in hub_configure()
1503 if (!hub->ports) { in hub_configure()
1504 ret = -ENOMEM; in hub_configure()
1508 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); in hub_configure()
1517 /* FIXME for USB 3.0, skip for now */ in hub_configure()
1523 portstr[i] = hub->descriptor->u.hs.DeviceRemovable in hub_configure()
1529 dev_dbg(hub_dev, "standalone hub\n"); in hub_configure()
1540 dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); in hub_configure()
1546 dev_dbg(hub_dev, "global over-current protection\n"); in hub_configure()
1549 dev_dbg(hub_dev, "individual port over-current protection\n"); in hub_configure()
1553 dev_dbg(hub_dev, "no over-current protection\n"); in hub_configure()
1557 spin_lock_init(&hub->tt.lock); in hub_configure()
1558 INIT_LIST_HEAD(&hub->tt.clear_list); in hub_configure()
1559 INIT_WORK(&hub->tt.clear_work, hub_tt_work); in hub_configure()
1560 switch (hdev->descriptor.bDeviceProtocol) { in hub_configure()
1565 hub->tt.hub = hdev; in hub_configure()
1571 hub->tt.multi = 1; in hub_configure()
1575 hub->tt.hub = hdev; in hub_configure()
1578 /* USB 3.0 hubs don't have a TT */ in hub_configure()
1581 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", in hub_configure()
1582 hdev->descriptor.bDeviceProtocol); in hub_configure()
1589 if (hdev->descriptor.bDeviceProtocol != 0) { in hub_configure()
1590 hub->tt.think_time = 666; in hub_configure()
1593 8, hub->tt.think_time); in hub_configure()
1597 hub->tt.think_time = 666 * 2; in hub_configure()
1600 16, hub->tt.think_time); in hub_configure()
1603 hub->tt.think_time = 666 * 3; in hub_configure()
1606 24, hub->tt.think_time); in hub_configure()
1609 hub->tt.think_time = 666 * 4; in hub_configure()
1612 32, hub->tt.think_time); in hub_configure()
1616 /* probe() zeroes hub->indicator[] */ in hub_configure()
1618 hub->has_indicators = 1; in hub_configure()
1623 hub->descriptor->bPwrOn2PwrGood * 2); in hub_configure()
1625 /* power budgeting mostly matters with bus-powered hubs, in hub_configure()
1626 * and battery-powered root hubs (may provide just 8 mA). in hub_configure()
1630 message = "can't get hub status"; in hub_configure()
1633 hcd = bus_to_hcd(hdev->bus); in hub_configure()
1634 if (hdev == hdev->bus->root_hub) { in hub_configure()
1635 if (hcd->power_budget > 0) in hub_configure()
1636 hdev->bus_mA = hcd->power_budget; in hub_configure()
1638 hdev->bus_mA = full_load * maxchild; in hub_configure()
1639 if (hdev->bus_mA >= full_load) in hub_configure()
1640 hub->mA_per_port = full_load; in hub_configure()
1642 hub->mA_per_port = hdev->bus_mA; in hub_configure()
1643 hub->limited_power = 1; in hub_configure()
1646 int remaining = hdev->bus_mA - in hub_configure()
1647 hub->descriptor->bHubContrCurrent; in hub_configure()
1649 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", in hub_configure()
1650 hub->descriptor->bHubContrCurrent); in hub_configure()
1651 hub->limited_power = 1; in hub_configure()
1657 hub->mA_per_port = unit_load; /* 7.2.1 */ in hub_configure()
1659 } else { /* Self-powered external hub */ in hub_configure()
1660 /* FIXME: What about battery-powered external hubs that in hub_configure()
1662 hub->mA_per_port = full_load; in hub_configure()
1664 if (hub->mA_per_port < full_load) in hub_configure()
1666 hub->mA_per_port); in hub_configure()
1668 ret = hub_hub_status(hub, &hubstatus, &hubchange); in hub_configure()
1670 message = "can't get hub status"; in hub_configure()
1675 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) in hub_configure()
1681 dev_dbg(hub_dev, "%sover-current condition exists\n", in hub_configure()
1690 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); in hub_configure()
1693 if (maxp > sizeof(*hub->buffer)) in hub_configure()
1694 maxp = sizeof(*hub->buffer); in hub_configure()
1696 hub->urb = usb_alloc_urb(0, GFP_KERNEL); in hub_configure()
1697 if (!hub->urb) { in hub_configure()
1698 ret = -ENOMEM; in hub_configure()
1702 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, in hub_configure()
1703 hub, endpoint->bInterval); in hub_configure()
1705 /* maybe cycle the hub leds */ in hub_configure()
1706 if (hub->has_indicators && blinkenlights) in hub_configure()
1707 hub->indicator[0] = INDICATOR_CYCLE; in hub_configure()
1711 ret = usb_hub_create_port_device(hub, i + 1); in hub_configure()
1713 dev_err(hub->intfdev, in hub_configure()
1718 hdev->maxchild = i; in hub_configure()
1719 for (i = 0; i < hdev->maxchild; i++) { in hub_configure()
1720 struct usb_port *port_dev = hub->ports[i]; in hub_configure()
1722 pm_runtime_put(&port_dev->dev); in hub_configure()
1729 /* Update the HCD's internal representation of this hub before hub_wq in hub_configure()
1730 * starts getting port status changes for devices under the hub. in hub_configure()
1732 if (hcd->driver->update_hub_device) { in hub_configure()
1733 ret = hcd->driver->update_hub_device(hcd, hdev, in hub_configure()
1734 &hub->tt, GFP_KERNEL); in hub_configure()
1736 message = "can't update HCD hub info"; in hub_configure()
1741 usb_hub_adjust_deviceremovable(hdev, hub->descriptor); in hub_configure()
1743 hub_activate(hub, HUB_INIT); in hub_configure()
1755 struct usb_hub *hub = container_of(kref, struct usb_hub, kref); in hub_release() local
1757 usb_put_dev(hub->hdev); in hub_release()
1758 usb_put_intf(to_usb_interface(hub->intfdev)); in hub_release()
1759 kfree(hub); in hub_release()
1762 void hub_get(struct usb_hub *hub) in hub_get() argument
1764 kref_get(&hub->kref); in hub_get()
1767 void hub_put(struct usb_hub *hub) in hub_put() argument
1769 kref_put(&hub->kref, hub_release); in hub_put()
1776 struct usb_hub *hub = usb_get_intfdata(intf); in hub_disconnect() local
1781 * Stop adding new hub events. We do not want to block here and thus in hub_disconnect()
1784 hub->disconnected = 1; in hub_disconnect()
1786 /* Disconnect all children and quiesce the hub */ in hub_disconnect()
1787 hub->error = 0; in hub_disconnect()
1788 hub_quiesce(hub, HUB_DISCONNECT); in hub_disconnect()
1794 port1 = hdev->maxchild; in hub_disconnect()
1795 hdev->maxchild = 0; in hub_disconnect()
1799 for (; port1 > 0; --port1) in hub_disconnect()
1800 usb_hub_remove_port_device(hub, port1); in hub_disconnect()
1804 if (hub->hdev->speed == USB_SPEED_HIGH) in hub_disconnect()
1805 highspeed_hubs--; in hub_disconnect()
1807 usb_free_urb(hub->urb); in hub_disconnect()
1808 kfree(hub->ports); in hub_disconnect()
1809 kfree(hub->descriptor); in hub_disconnect()
1810 kfree(hub->status); in hub_disconnect()
1811 kfree(hub->buffer); in hub_disconnect()
1813 pm_suspend_ignore_children(&intf->dev, false); in hub_disconnect()
1815 if (hub->quirk_disable_autosuspend) in hub_disconnect()
1818 onboard_dev_destroy_pdevs(&hub->onboard_devs); in hub_disconnect()
1820 hub_put(hub); in hub_disconnect()
1827 if (desc->desc.bInterfaceSubClass != 0 && in hub_descriptor_is_sane()
1828 desc->desc.bInterfaceSubClass != 1) in hub_descriptor_is_sane()
1831 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ in hub_descriptor_is_sane()
1832 if (desc->desc.bNumEndpoints != 1) in hub_descriptor_is_sane()
1836 if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc)) in hub_descriptor_is_sane()
1846 struct usb_hub *hub; in hub_probe() local
1848 desc = intf->cur_altsetting; in hub_probe()
1852 * The USB 2.0 spec prohibits hubs from having more than one in hub_probe()
1856 if (hdev->descriptor.bNumConfigurations > 1 || in hub_probe()
1857 hdev->actconfig->desc.bNumInterfaces > 1) { in hub_probe()
1858 dev_err(&intf->dev, "Invalid hub with more than one config or interface\n"); in hub_probe()
1859 return -EINVAL; in hub_probe()
1866 * - Unlike other drivers, the hub driver does not rely on the in hub_probe()
1869 * change on hub downstream ports, so it is safe to do it. in hub_probe()
1871 * - The patch might cause one or more auto supend/resume for in hub_probe()
1872 * below very rare devices when they are plugged into hub in hub_probe()
1883 * autosuspend delay of their parent hub in the probe() to one in hub_probe()
1887 * - The patch may cause one or more auto suspend/resume on in hub_probe()
1888 * hub during running 'lsusb', but it is probably too in hub_probe()
1891 * - Change autosuspend delay of hub can avoid unnecessary auto in hub_probe()
1892 * suspend timer for hub, also may decrease power consumption in hub_probe()
1893 * of USB bus. in hub_probe()
1895 * - If user has indicated to prevent autosuspend by passing in hub_probe()
1896 * usbcore.autosuspend = -1 then keep autosuspend disabled. in hub_probe()
1899 if (hdev->dev.power.autosuspend_delay >= 0) in hub_probe()
1900 pm_runtime_set_autosuspend_delay(&hdev->dev, 0); in hub_probe()
1908 if (hdev->parent) { /* normal device */ in hub_probe()
1910 } else { /* root hub */ in hub_probe()
1911 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; in hub_probe()
1913 if (drv->bus_suspend && drv->bus_resume) in hub_probe()
1917 if (hdev->level == MAX_TOPO_LEVEL) { in hub_probe()
1918 dev_err(&intf->dev, in hub_probe()
1919 "Unsupported bus topology: hub nested too deep\n"); in hub_probe()
1920 return -E2BIG; in hub_probe()
1924 if (hdev->parent) { in hub_probe()
1925 dev_warn(&intf->dev, "ignoring external hub\n"); in hub_probe()
1926 return -ENODEV; in hub_probe()
1931 dev_err(&intf->dev, "bad descriptor, ignoring hub\n"); in hub_probe()
1932 return -EIO; in hub_probe()
1935 /* We found a hub */ in hub_probe()
1936 dev_info(&intf->dev, "USB hub found\n"); in hub_probe()
1938 hub = kzalloc(sizeof(*hub), GFP_KERNEL); in hub_probe()
1939 if (!hub) in hub_probe()
1940 return -ENOMEM; in hub_probe()
1942 kref_init(&hub->kref); in hub_probe()
1943 hub->intfdev = &intf->dev; in hub_probe()
1944 hub->hdev = hdev; in hub_probe()
1945 INIT_DELAYED_WORK(&hub->leds, led_work); in hub_probe()
1946 INIT_DELAYED_WORK(&hub->init_work, NULL); in hub_probe()
1947 INIT_WORK(&hub->events, hub_event); in hub_probe()
1948 INIT_LIST_HEAD(&hub->onboard_devs); in hub_probe()
1949 spin_lock_init(&hub->irq_urb_lock); in hub_probe()
1950 timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0); in hub_probe()
1954 usb_set_intfdata(intf, hub); in hub_probe()
1955 intf->needs_remote_wakeup = 1; in hub_probe()
1956 pm_suspend_ignore_children(&intf->dev, true); in hub_probe()
1958 if (hdev->speed == USB_SPEED_HIGH) in hub_probe()
1961 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) in hub_probe()
1962 hub->quirk_check_port_auto_suspend = 1; in hub_probe()
1964 if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) { in hub_probe()
1965 hub->quirk_disable_autosuspend = 1; in hub_probe()
1969 if ((id->driver_info & HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL) && in hub_probe()
1970 desc->endpoint[0].desc.bInterval > USB_REDUCE_FRAME_INTR_BINTERVAL) { in hub_probe()
1971 desc->endpoint[0].desc.bInterval = in hub_probe()
1977 if (hub_configure(hub, &desc->endpoint[0].desc) >= 0) { in hub_probe()
1978 onboard_dev_create_pdevs(hdev, &hub->onboard_devs); in hub_probe()
1984 return -ENODEV; in hub_probe()
1991 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in hub_ioctl() local
1993 /* assert ifno == 0 (part of hub spec) */ in hub_ioctl()
2000 if (hdev->devnum <= 0) in hub_ioctl()
2001 info->nports = 0; in hub_ioctl()
2003 info->nports = hdev->maxchild; in hub_ioctl()
2004 for (i = 0; i < info->nports; i++) { in hub_ioctl()
2005 if (hub->ports[i]->child == NULL) in hub_ioctl()
2006 info->port[i] = 0; in hub_ioctl()
2008 info->port[i] = in hub_ioctl()
2009 hub->ports[i]->child->devnum; in hub_ioctl()
2014 return info->nports + 1; in hub_ioctl()
2018 return -ENOSYS; in hub_ioctl()
2023 * Allow user programs to claim ports on a hub. When a device is attached
2029 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in find_port_owner() local
2031 if (hdev->state == USB_STATE_NOTATTACHED) in find_port_owner()
2032 return -ENODEV; in find_port_owner()
2033 if (port1 == 0 || port1 > hdev->maxchild) in find_port_owner()
2034 return -EINVAL; in find_port_owner()
2036 /* Devices not managed by the hub driver in find_port_owner()
2039 *ppowner = &(hub->ports[port1 - 1]->port_owner); in find_port_owner()
2054 return -EBUSY; in usb_hub_claim_port()
2070 return -ENOENT; in usb_hub_release_port()
2078 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_hub_release_all_ports() local
2081 for (n = 0; n < hdev->maxchild; n++) { in usb_hub_release_all_ports()
2082 if (hub->ports[n]->port_owner == owner) in usb_hub_release_all_ports()
2083 hub->ports[n]->port_owner = NULL; in usb_hub_release_all_ports()
2091 struct usb_hub *hub; in usb_device_is_owned() local
2093 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) in usb_device_is_owned()
2095 hub = usb_hub_to_struct_hub(udev->parent); in usb_device_is_owned()
2096 return !!hub->ports[udev->portnum - 1]->port_owner; in usb_device_is_owned()
2101 struct usb_hub *hub; in update_port_device_state() local
2104 if (udev->parent) { in update_port_device_state()
2105 hub = usb_hub_to_struct_hub(udev->parent); in update_port_device_state()
2109 * has a test step to unbind the hub before running the in update_port_device_state()
2111 * which will set the hub's maxchild to 0, further in update_port_device_state()
2114 if (hub) { in update_port_device_state()
2115 port_dev = hub->ports[udev->portnum - 1]; in update_port_device_state()
2116 WRITE_ONCE(port_dev->state, udev->state); in update_port_device_state()
2117 sysfs_notify_dirent(port_dev->state_kn); in update_port_device_state()
2124 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in recursively_mark_NOTATTACHED() local
2127 for (i = 0; i < udev->maxchild; ++i) { in recursively_mark_NOTATTACHED()
2128 if (hub->ports[i]->child) in recursively_mark_NOTATTACHED()
2129 recursively_mark_NOTATTACHED(hub->ports[i]->child); in recursively_mark_NOTATTACHED()
2131 if (udev->state == USB_STATE_SUSPENDED) in recursively_mark_NOTATTACHED()
2132 udev->active_duration -= jiffies; in recursively_mark_NOTATTACHED()
2133 udev->state = USB_STATE_NOTATTACHED; in recursively_mark_NOTATTACHED()
2138 * usb_set_device_state - change a device's current state (usbcore, hcds)
2142 * udev->state is _not_ fully protected by the device lock. Although
2153 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
2154 * Otherwise udev->state is set to new_state, and if new_state is
2162 int wakeup = -1; in usb_set_device_state()
2165 if (udev->state == USB_STATE_NOTATTACHED) in usb_set_device_state()
2169 /* root hub wakeup capabilities are managed out-of-band in usb_set_device_state()
2172 if (udev->parent) { in usb_set_device_state()
2173 if (udev->state == USB_STATE_SUSPENDED in usb_set_device_state()
2177 wakeup = (udev->quirks & in usb_set_device_state()
2179 udev->actconfig->desc.bmAttributes & in usb_set_device_state()
2184 if (udev->state == USB_STATE_SUSPENDED && in usb_set_device_state()
2186 udev->active_duration -= jiffies; in usb_set_device_state()
2188 udev->state != USB_STATE_SUSPENDED) in usb_set_device_state()
2189 udev->active_duration += jiffies; in usb_set_device_state()
2190 udev->state = new_state; in usb_set_device_state()
2196 device_set_wakeup_capable(&udev->dev, wakeup); in usb_set_device_state()
2203 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2204 * USB-2.0 buses they are also used as device addresses, however on
2205 * USB-3.0 buses the address is assigned by the controller hardware
2216 struct usb_bus *bus = udev->bus; in choose_devnum()
2218 /* be safe when more hub events are proceed in parallel */ in choose_devnum()
2219 mutex_lock(&bus->devnum_next_mutex); in choose_devnum()
2221 /* Try to allocate the next devnum beginning at bus->devnum_next. */ in choose_devnum()
2222 devnum = find_next_zero_bit(bus->devmap, 128, bus->devnum_next); in choose_devnum()
2224 devnum = find_next_zero_bit(bus->devmap, 128, 1); in choose_devnum()
2225 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); in choose_devnum()
2227 set_bit(devnum, bus->devmap); in choose_devnum()
2228 udev->devnum = devnum; in choose_devnum()
2230 mutex_unlock(&bus->devnum_next_mutex); in choose_devnum()
2235 if (udev->devnum > 0) { in release_devnum()
2236 clear_bit(udev->devnum, udev->bus->devmap); in release_devnum()
2237 udev->devnum = -1; in release_devnum()
2243 udev->devnum = devnum; in update_devnum()
2244 if (!udev->devaddr) in update_devnum()
2245 udev->devaddr = (u8)devnum; in update_devnum()
2250 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_free_dev()
2253 if (hcd->driver->free_dev && udev->parent) in hub_free_dev()
2254 hcd->driver->free_dev(hcd, udev); in hub_free_dev()
2259 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in hub_disconnect_children() local
2263 for (i = 0; i < udev->maxchild; i++) { in hub_disconnect_children()
2264 if (hub->ports[i]->child) in hub_disconnect_children()
2265 usb_disconnect(&hub->ports[i]->child); in hub_disconnect_children()
2270 * usb_disconnect - disconnect a device (usbcore-internal)
2277 * If *pdev is a normal device then the parent hub must already be locked.
2278 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
2281 * Only hub drivers (including virtual root hub drivers for host
2290 struct usb_hub *hub = NULL; in usb_disconnect() local
2298 dev_info(&udev->dev, "USB disconnect, device number %d\n", in usb_disconnect()
2299 udev->devnum); in usb_disconnect()
2302 * Ensure that the pm runtime code knows that the USB device in usb_disconnect()
2305 pm_runtime_barrier(&udev->dev); in usb_disconnect()
2315 dev_dbg(&udev->dev, "unregistering device\n"); in usb_disconnect()
2319 if (udev->parent) { in usb_disconnect()
2320 port1 = udev->portnum; in usb_disconnect()
2321 hub = usb_hub_to_struct_hub(udev->parent); in usb_disconnect()
2322 port_dev = hub->ports[port1 - 1]; in usb_disconnect()
2324 sysfs_remove_link(&udev->dev.kobj, "port"); in usb_disconnect()
2325 sysfs_remove_link(&port_dev->dev.kobj, "device"); in usb_disconnect()
2328 * As usb_port_runtime_resume() de-references udev, make in usb_disconnect()
2331 if (!test_and_set_bit(port1, hub->child_usage_bits)) in usb_disconnect()
2332 pm_runtime_get_sync(&port_dev->dev); in usb_disconnect()
2334 typec_deattach(port_dev->connector, &udev->dev); in usb_disconnect()
2337 usb_remove_ep_devs(&udev->ep0); in usb_disconnect()
2341 * for de-configuring the device and invoking the remove-device in usb_disconnect()
2344 device_del(&udev->dev); in usb_disconnect()
2356 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) in usb_disconnect()
2357 pm_runtime_put(&port_dev->dev); in usb_disconnect()
2361 put_device(&udev->dev); in usb_disconnect()
2369 dev_info(&udev->dev, "%s: %s\n", id, string); in show_string()
2374 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice); in announce_device()
2376 dev_info(&udev->dev, in announce_device()
2377 "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n", in announce_device()
2378 le16_to_cpu(udev->descriptor.idVendor), in announce_device()
2379 le16_to_cpu(udev->descriptor.idProduct), in announce_device()
2381 dev_info(&udev->dev, in announce_device()
2382 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", in announce_device()
2383 udev->descriptor.iManufacturer, in announce_device()
2384 udev->descriptor.iProduct, in announce_device()
2385 udev->descriptor.iSerialNumber); in announce_device()
2386 show_string(udev, "Product", udev->product); in announce_device()
2387 show_string(udev, "Manufacturer", udev->manufacturer); in announce_device()
2388 show_string(udev, "SerialNumber", udev->serial); in announce_device()
2396 * usb_enumerate_device_otg - FIXME (usbcore-internal)
2399 * Finish enumeration for On-The-Go devices
2409 * OTG-aware devices on OTG-capable root hubs may be able to use SRP, in usb_enumerate_device_otg()
2413 if (!udev->bus->is_b_host in usb_enumerate_device_otg()
2414 && udev->config in usb_enumerate_device_otg()
2415 && udev->parent == udev->bus->root_hub) { in usb_enumerate_device_otg()
2417 struct usb_bus *bus = udev->bus; in usb_enumerate_device_otg()
2418 unsigned port1 = udev->portnum; in usb_enumerate_device_otg()
2421 err = __usb_get_extra_descriptor(udev->rawdescriptors[0], in usb_enumerate_device_otg()
2422 le16_to_cpu(udev->config[0].desc.wTotalLength), in usb_enumerate_device_otg()
2424 if (err || !(desc->bmAttributes & USB_OTG_HNP)) in usb_enumerate_device_otg()
2427 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", in usb_enumerate_device_otg()
2428 (port1 == bus->otg_port) ? "" : "non-"); in usb_enumerate_device_otg()
2431 if (port1 == bus->otg_port) { in usb_enumerate_device_otg()
2432 bus->b_hnp_enable = 1; in usb_enumerate_device_otg()
2444 dev_err(&udev->dev, "can't set HNP mode: %d\n", in usb_enumerate_device_otg()
2446 bus->b_hnp_enable = 0; in usb_enumerate_device_otg()
2448 } else if (desc->bLength == sizeof in usb_enumerate_device_otg()
2456 if (bus->otg_port != 0) { in usb_enumerate_device_otg()
2465 dev_err(&udev->dev, in usb_enumerate_device_otg()
2477 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2480 * This is only called by usb_new_device() -- all comments that apply there
2492 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_enumerate_device()
2494 if (udev->config == NULL) { in usb_enumerate_device()
2497 if (err != -ENODEV) in usb_enumerate_device()
2498 dev_err(&udev->dev, "can't read configurations, error %d\n", in usb_enumerate_device()
2505 udev->product = usb_cache_string(udev, udev->descriptor.iProduct); in usb_enumerate_device()
2506 udev->manufacturer = usb_cache_string(udev, in usb_enumerate_device()
2507 udev->descriptor.iManufacturer); in usb_enumerate_device()
2508 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); in usb_enumerate_device()
2514 if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support && in usb_enumerate_device()
2519 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable in usb_enumerate_device()
2520 || udev->bus->is_b_host)) { in usb_enumerate_device()
2523 dev_dbg(&udev->dev, "HNP fail, %d\n", err); in usb_enumerate_device()
2525 return -ENOTSUPP; in usb_enumerate_device()
2535 struct usb_device *hdev = udev->parent; in set_usb_port_removable()
2536 struct usb_hub *hub; in set_usb_port_removable() local
2537 u8 port = udev->portnum; in set_usb_port_removable()
2541 dev_set_removable(&udev->dev, DEVICE_REMOVABLE_UNKNOWN); in set_usb_port_removable()
2546 hub = usb_hub_to_struct_hub(udev->parent); in set_usb_port_removable()
2552 switch (hub->ports[udev->portnum - 1]->connect_type) { in set_usb_port_removable()
2554 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); in set_usb_port_removable()
2558 dev_set_removable(&udev->dev, DEVICE_FIXED); in set_usb_port_removable()
2565 * Otherwise, check whether the hub knows whether a port is removable in set_usb_port_removable()
2568 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); in set_usb_port_removable()
2574 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) in set_usb_port_removable()
2578 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) in set_usb_port_removable()
2583 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); in set_usb_port_removable()
2585 dev_set_removable(&udev->dev, DEVICE_FIXED); in set_usb_port_removable()
2590 * usb_new_device - perform initial device setup (usbcore-internal)
2596 * the parent hub (if udev is a normal device) or else the
2597 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
2603 * Only the hub driver or root-hub registrar should ever call this.
2614 if (udev->parent) { in usb_new_device()
2615 /* Initialize non-root-hub device wakeup to disabled; in usb_new_device()
2619 device_init_wakeup(&udev->dev, 0); in usb_new_device()
2622 /* Tell the runtime-PM framework the device is active */ in usb_new_device()
2623 pm_runtime_set_active(&udev->dev); in usb_new_device()
2624 pm_runtime_get_noresume(&udev->dev); in usb_new_device()
2625 pm_runtime_use_autosuspend(&udev->dev); in usb_new_device()
2626 pm_runtime_enable(&udev->dev); in usb_new_device()
2636 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", in usb_new_device()
2637 udev->devnum, udev->bus->busnum, in usb_new_device()
2638 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); in usb_new_device()
2639 /* export the usbdev device-node for libusb */ in usb_new_device()
2640 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, in usb_new_device()
2641 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); in usb_new_device()
2646 if (udev->serial) in usb_new_device()
2647 add_device_randomness(udev->serial, strlen(udev->serial)); in usb_new_device()
2648 if (udev->product) in usb_new_device()
2649 add_device_randomness(udev->product, strlen(udev->product)); in usb_new_device()
2650 if (udev->manufacturer) in usb_new_device()
2651 add_device_randomness(udev->manufacturer, in usb_new_device()
2652 strlen(udev->manufacturer)); in usb_new_device()
2654 device_enable_async_suspend(&udev->dev); in usb_new_device()
2656 /* check whether the hub or firmware marks this port as non-removable */ in usb_new_device()
2660 * for configuring the device and invoking the add-device in usb_new_device()
2663 err = device_add(&udev->dev); in usb_new_device()
2665 dev_err(&udev->dev, "can't device_add, error %d\n", err); in usb_new_device()
2669 /* Create link files between child device and usb port device. */ in usb_new_device()
2670 if (udev->parent) { in usb_new_device()
2671 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_new_device() local
2672 int port1 = udev->portnum; in usb_new_device()
2673 struct usb_port *port_dev = hub->ports[port1 - 1]; in usb_new_device()
2675 err = sysfs_create_link(&udev->dev.kobj, in usb_new_device()
2676 &port_dev->dev.kobj, "port"); in usb_new_device()
2680 err = sysfs_create_link(&port_dev->dev.kobj, in usb_new_device()
2681 &udev->dev.kobj, "device"); in usb_new_device()
2683 sysfs_remove_link(&udev->dev.kobj, "port"); in usb_new_device()
2687 if (!test_and_set_bit(port1, hub->child_usage_bits)) in usb_new_device()
2688 pm_runtime_get_sync(&port_dev->dev); in usb_new_device()
2690 typec_attach(port_dev->connector, &udev->dev); in usb_new_device()
2693 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); in usb_new_device()
2695 pm_runtime_put_sync_autosuspend(&udev->dev); in usb_new_device()
2699 device_del(&udev->dev); in usb_new_device()
2702 pm_runtime_disable(&udev->dev); in usb_new_device()
2703 pm_runtime_set_suspended(&udev->dev); in usb_new_device()
2709 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2710 * @usb_dev: USB device
2712 * Move the USB device to a very basic state where interfaces are disabled
2723 if (usb_dev->authorized == 0) in usb_deauthorize_device()
2726 usb_dev->authorized = 0; in usb_deauthorize_device()
2727 usb_set_configuration(usb_dev, -1); in usb_deauthorize_device()
2740 if (usb_dev->authorized == 1) in usb_authorize_device()
2745 dev_err(&usb_dev->dev, in usb_authorize_device()
2750 usb_dev->authorized = 1; in usb_authorize_device()
2758 dev_err(&usb_dev->dev, in usb_authorize_device()
2764 dev_info(&usb_dev->dev, "authorized to connect\n"); in usb_authorize_device()
2774 * get_port_ssp_rate - Match the extended port status to SSP rate
2775 * @hdev: The hub device
2792 if (!hdev->bos) in get_port_ssp_rate()
2795 ssp_cap = hdev->bos->ssp_cap; in get_port_ssp_rate()
2802 ssac = le32_to_cpu(ssp_cap->bmAttributes) & in get_port_ssp_rate()
2808 attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); in get_port_ssp_rate()
2879 (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || in use_new_scheme()
2889 if (udev->speed >= USB_SPEED_SUPER) in use_new_scheme()
2902 /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
2905 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, in hub_port_warm_reset_required() argument
2910 if (!hub_is_superspeed(hub->hdev)) in hub_port_warm_reset_required()
2913 if (test_bit(port1, hub->warm_reset_bits)) in hub_port_warm_reset_required()
2921 static int hub_port_wait_reset(struct usb_hub *hub, int port1, in hub_port_wait_reset() argument
2936 if (hub_is_superspeedplus(hub->hdev)) in hub_port_wait_reset()
2937 ret = hub_ext_port_status(hub, port1, in hub_port_wait_reset()
2942 ret = usb_hub_port_status(hub, port1, &portstatus, in hub_port_wait_reset()
2951 * to re-establish a connection after the reset is complete, in hub_port_wait_reset()
2952 * so also wait for the connection to be re-established. in hub_port_wait_reset()
2962 dev_dbg(&hub->ports[port1 - 1]->dev, in hub_port_wait_reset()
2968 return -EBUSY; in hub_port_wait_reset()
2970 if (hub_port_warm_reset_required(hub, port1, portstatus)) in hub_port_wait_reset()
2971 return -ENOTCONN; in hub_port_wait_reset()
2975 return -ENOTCONN; in hub_port_wait_reset()
2978 * A USB 3.0 connection may bounce if multiple warm resets were issued, in hub_port_wait_reset()
2979 * but the device may have successfully re-connected. Ignore it. in hub_port_wait_reset()
2981 if (!hub_is_superspeed(hub->hdev) && in hub_port_wait_reset()
2983 usb_clear_port_feature(hub->hdev, port1, in hub_port_wait_reset()
2985 return -EAGAIN; in hub_port_wait_reset()
2989 return -EBUSY; in hub_port_wait_reset()
2994 if (hub_is_superspeedplus(hub->hdev)) { in hub_port_wait_reset()
2996 udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; in hub_port_wait_reset()
2997 udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1; in hub_port_wait_reset()
2998 udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus); in hub_port_wait_reset()
3000 udev->rx_lanes = 1; in hub_port_wait_reset()
3001 udev->tx_lanes = 1; in hub_port_wait_reset()
3002 udev->ssp_rate = USB_SSP_GEN_UNKNOWN; in hub_port_wait_reset()
3004 if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN) in hub_port_wait_reset()
3005 udev->speed = USB_SPEED_SUPER_PLUS; in hub_port_wait_reset()
3006 else if (hub_is_superspeed(hub->hdev)) in hub_port_wait_reset()
3007 udev->speed = USB_SPEED_SUPER; in hub_port_wait_reset()
3009 udev->speed = USB_SPEED_HIGH; in hub_port_wait_reset()
3011 udev->speed = USB_SPEED_LOW; in hub_port_wait_reset()
3013 udev->speed = USB_SPEED_FULL; in hub_port_wait_reset()
3018 static int hub_port_reset(struct usb_hub *hub, int port1, in hub_port_reset() argument
3023 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_reset()
3026 if (!hub_is_superspeed(hub->hdev)) { in hub_port_reset()
3028 dev_err(hub->intfdev, "only USB3 hub support " in hub_port_reset()
3030 return -EINVAL; in hub_port_reset()
3041 if (usb_hub_port_status(hub, port1, &portstatus, in hub_port_reset()
3043 if (hub_port_warm_reset_required(hub, port1, in hub_port_reset()
3047 clear_bit(port1, hub->warm_reset_bits); in hub_port_reset()
3051 status = set_port_feature(hub->hdev, port1, (warm ? in hub_port_reset()
3054 if (status == -ENODEV) { in hub_port_reset()
3055 ; /* The hub is gone */ in hub_port_reset()
3057 dev_err(&port_dev->dev, in hub_port_reset()
3061 status = hub_port_wait_reset(hub, port1, udev, delay, in hub_port_reset()
3063 if (status && status != -ENOTCONN && status != -ENODEV) in hub_port_reset()
3064 dev_dbg(hub->intfdev, in hub_port_reset()
3073 if (status == 0 || status == -ENOTCONN || status == -ENODEV || in hub_port_reset()
3074 (status == -EBUSY && i == PORT_RESET_TRIES - 1)) { in hub_port_reset()
3075 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3078 if (!hub_is_superspeed(hub->hdev)) in hub_port_reset()
3081 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3083 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3087 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3091 * If a USB 3.0 device migrates from reset to an error in hub_port_reset()
3092 * state, re-issue the warm reset. in hub_port_reset()
3094 if (usb_hub_port_status(hub, port1, in hub_port_reset()
3098 if (!hub_port_warm_reset_required(hub, port1, in hub_port_reset()
3107 dev_dbg(&port_dev->dev, in hub_port_reset()
3113 dev_dbg(&port_dev->dev, in hub_port_reset()
3119 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); in hub_port_reset()
3123 if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM) in hub_port_reset()
3129 /* Hub needs extra delay after resetting its port. */ in hub_port_reset()
3130 if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET) in hub_port_reset()
3137 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_port_reset()
3143 if (hcd->driver->reset_device) in hub_port_reset()
3144 hcd->driver->reset_device(hcd, udev); in hub_port_reset()
3153 if (!hub_is_superspeed(hub->hdev)) in hub_port_reset()
3160 * hub_port_stop_enumerate - stop USB enumeration or ignore port events
3161 * @hub: target hub
3177 static bool hub_port_stop_enumerate(struct usb_hub *hub, int port1, int retries) in hub_port_stop_enumerate() argument
3179 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_stop_enumerate()
3181 if (port_dev->early_stop) { in hub_port_stop_enumerate()
3182 if (port_dev->ignore_event) in hub_port_stop_enumerate()
3194 port_dev->ignore_event = 1; in hub_port_stop_enumerate()
3196 port_dev->ignore_event = 0; in hub_port_stop_enumerate()
3198 return port_dev->ignore_event; in hub_port_stop_enumerate()
3202 int usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus) in usb_port_is_power_on() argument
3206 if (hub_is_superspeed(hub->hdev)) { in usb_port_is_power_on()
3218 __acquires(&port_dev->status_lock) in usb_lock_port()
3220 mutex_lock(&port_dev->status_lock); in usb_lock_port()
3221 __acquire(&port_dev->status_lock); in usb_lock_port()
3225 __releases(&port_dev->status_lock) in usb_unlock_port()
3227 mutex_unlock(&port_dev->status_lock); in usb_unlock_port()
3228 __release(&port_dev->status_lock); in usb_unlock_port()
3234 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) in port_is_suspended() argument
3238 if (hub_is_superspeed(hub->hdev)) { in port_is_suspended()
3251 * is ready for a reset-resume, or should be disconnected.
3254 struct usb_hub *hub, int port1, in check_port_resume_type() argument
3257 struct usb_port *port_dev = hub->ports[port1 - 1]; in check_port_resume_type()
3262 if (status == 0 && udev->reset_resume in check_port_resume_type()
3263 && hub_port_warm_reset_required(hub, port1, portstatus)) { in check_port_resume_type()
3267 else if (status || port_is_suspended(hub, portstatus) || in check_port_resume_type()
3268 !usb_port_is_power_on(hub, portstatus)) { in check_port_resume_type()
3270 status = -ENODEV; in check_port_resume_type()
3272 if (retries--) { in check_port_resume_type()
3274 status = usb_hub_port_status(hub, port1, &portstatus, in check_port_resume_type()
3278 status = -ENODEV; in check_port_resume_type()
3282 * so try a reset-resume instead. in check_port_resume_type()
3284 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { in check_port_resume_type()
3285 if (udev->persist_enabled) in check_port_resume_type()
3286 udev->reset_resume = 1; in check_port_resume_type()
3288 status = -ENODEV; in check_port_resume_type()
3292 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", in check_port_resume_type()
3294 } else if (udev->reset_resume) { in check_port_resume_type()
3296 /* Late port handoff can set status-change bits */ in check_port_resume_type()
3298 usb_clear_port_feature(hub->hdev, port1, in check_port_resume_type()
3301 usb_clear_port_feature(hub->hdev, port1, in check_port_resume_type()
3305 * Whatever made this reset-resume necessary may have in check_port_resume_type()
3306 * turned on the port1 bit in hub->change_bits. But after in check_port_resume_type()
3307 * a successful reset-resume we want the bit to be clear; in check_port_resume_type()
3309 * following the reset-resume. in check_port_resume_type()
3311 clear_bit(port1, hub->change_bits); in check_port_resume_type()
3319 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_disable_ltm()
3322 if (!usb_device_supports_ltm(hcd->self.root_hub) || in usb_disable_ltm()
3329 if (!udev->actconfig) in usb_disable_ltm()
3341 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_enable_ltm()
3344 if (!usb_device_supports_ltm(hcd->self.root_hub) || in usb_enable_ltm()
3351 if (!udev->actconfig) in usb_enable_ltm()
3362 * usb_enable_remote_wakeup - enable remote wakeup for a device
3365 * For USB-2 devices: Set the device's remote wakeup feature.
3367 * For USB-3 devices: Assume there's only one function on the device and
3373 if (udev->speed < USB_SPEED_SUPER) in usb_enable_remote_wakeup()
3388 * usb_disable_remote_wakeup - disable remote wakeup for a device
3391 * For USB-2 devices: Clear the device's remote wakeup feature.
3393 * For USB-3 devices: Assume there's only one function on the device and
3399 if (udev->speed < USB_SPEED_SUPER) in usb_disable_remote_wakeup()
3411 /* Count of wakeup-enabled devices at or below udev */
3414 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in usb_wakeup_enabled_descendants() local
3416 return udev->do_remote_wakeup + in usb_wakeup_enabled_descendants()
3417 (hub ? hub->wakeup_enabled_descendants : 0); in usb_wakeup_enabled_descendants()
3422 * usb_port_suspend - suspend a usb device's upstream port
3423 * @udev: device that's no longer in active use, not a root hub
3426 * Suspends a USB device that isn't in active use, conserving power.
3432 * This only affects the USB hardware for a device; its interfaces
3440 * also support "remote wakeup", where the device can activate the USB
3442 * some cases, this wakes the USB host.
3445 * between a pair of dual-role devices. That will change roles, such
3446 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3448 * Devices on USB hub ports have only one "suspend" state, corresponding
3452 * - suspend, resume ... when the VBUS power link stays live
3453 * - suspend, disconnect ... VBUS lost
3456 * normal re-enumeration procedures, starting with enabling VBUS power.
3457 * Other than re-initializing the hub (plug/unplug, except for root hubs),
3461 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3463 * hub is suspended). Nevertheless, we change @udev->state to
3465 * upstream port setting is stored in @udev->port_is_suspended.
3471 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_port_suspend() local
3472 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; in usb_port_suspend()
3473 int port1 = udev->portnum; in usb_port_suspend()
3480 * wake up the upstream hub (including maybe the root hub). in usb_port_suspend()
3485 if (udev->do_remote_wakeup) { in usb_port_suspend()
3488 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", in usb_port_suspend()
3500 dev_err(&udev->dev, "Failed to disable LTM before suspend\n"); in usb_port_suspend()
3501 status = -ENOMEM; in usb_port_suspend()
3507 if (hub_is_superspeed(hub->hdev)) in usb_port_suspend()
3508 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); in usb_port_suspend()
3512 * on individual USB-2 ports. The devices will automatically go in usb_port_suspend()
3513 * into suspend a few ms after the root hub stops sending packets. in usb_port_suspend()
3514 * The USB 2.0 spec calls this "global suspend". in usb_port_suspend()
3516 * However, many USB hubs have a bug: They don't relay wakeup requests in usb_port_suspend()
3522 status = set_port_feature(hub->hdev, port1, in usb_port_suspend()
3532 if (status == -ETIMEDOUT) { in usb_port_suspend()
3537 ret = usb_hub_port_status(hub, port1, &portstatus, in usb_port_suspend()
3540 dev_dbg(&port_dev->dev, in usb_port_suspend()
3543 if (ret == 0 && port_is_suspended(hub, portstatus)) { in usb_port_suspend()
3549 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); in usb_port_suspend()
3557 if (udev->do_remote_wakeup) in usb_port_suspend()
3566 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", in usb_port_suspend()
3567 (PMSG_IS_AUTO(msg) ? "auto-" : ""), in usb_port_suspend()
3568 udev->do_remote_wakeup); in usb_port_suspend()
3570 udev->port_is_suspended = 1; in usb_port_suspend()
3578 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled in usb_port_suspend()
3579 && test_and_clear_bit(port1, hub->child_usage_bits)) in usb_port_suspend()
3580 pm_runtime_put_sync(&port_dev->dev); in usb_port_suspend()
3582 usb_mark_last_busy(hub->hdev); in usb_port_suspend()
3589 * If the USB "suspend" state is in use (rather than "global suspend"),
3596 * If @udev->reset_resume is set then the device is reset before the
3605 dev_dbg(&udev->dev, "%s\n", in finish_port_resume()
3606 udev->reset_resume ? "finish reset-resume" : "finish resume"); in finish_port_resume()
3608 /* usb ch9 identifies four variants of SUSPENDED, based on what in finish_port_resume()
3613 usb_set_device_state(udev, udev->actconfig in finish_port_resume()
3622 if (udev->reset_resume) { in finish_port_resume()
3625 * we don't want to perform a reset-resume. We'll fail the in finish_port_resume()
3630 if (udev->quirks & USB_QUIRK_RESET) in finish_port_resume()
3631 status = -ENODEV; in finish_port_resume()
3644 /* If a normal resume failed, try doing a reset-resume */ in finish_port_resume()
3645 if (status && !udev->reset_resume && udev->persist_enabled) { in finish_port_resume()
3646 dev_dbg(&udev->dev, "retry with reset-resume\n"); in finish_port_resume()
3647 udev->reset_resume = 1; in finish_port_resume()
3653 dev_dbg(&udev->dev, "gone after usb resume? status %d\n", in finish_port_resume()
3659 * udev->reset_resume in finish_port_resume()
3661 } else if (udev->actconfig && !udev->reset_resume) { in finish_port_resume()
3662 if (udev->speed < USB_SPEED_SUPER) { in finish_port_resume()
3674 dev_dbg(&udev->dev, in finish_port_resume()
3683 * There are some SS USB devices which take longer time for link training.
3687 * USB Analyzer log with such buggy devices show that in some cases
3707 struct usb_hub *hub, int port1, in wait_for_connected() argument
3715 if (!usb_port_is_power_on(hub, *portstatus)) { in wait_for_connected()
3716 status = -ENODEV; in wait_for_connected()
3721 status = usb_hub_port_status(hub, port1, portstatus, portchange); in wait_for_connected()
3723 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); in wait_for_connected()
3728 * usb_port_resume - re-activate a suspended usb device's upstream port
3729 * @udev: device to re-activate, not a root hub
3732 * This will re-activate the suspended device, increasing power usage
3734 * USB resume explicitly guarantees that the power session between
3738 * If @udev->reset_resume is set then this routine won't check that the
3744 * during a system sleep or is reset when the system wakes up, all the USB
3746 * for mass-storage devices containing mounted filesystems, since the
3763 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_port_resume() local
3764 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; in usb_port_resume()
3765 int port1 = udev->portnum; in usb_port_resume()
3769 if (!test_and_set_bit(port1, hub->child_usage_bits)) { in usb_port_resume()
3770 status = pm_runtime_resume_and_get(&port_dev->dev); in usb_port_resume()
3772 dev_dbg(&udev->dev, "can't resume usb port, status %d\n", in usb_port_resume()
3780 /* Skip the initial Clear-Suspend step for a remote wakeup */ in usb_port_resume()
3781 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in usb_port_resume()
3782 if (status == 0 && !port_is_suspended(hub, portstatus)) { in usb_port_resume()
3784 pm_wakeup_event(&udev->dev, 0); in usb_port_resume()
3789 if (hub_is_superspeed(hub->hdev)) in usb_port_resume()
3790 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); in usb_port_resume()
3792 status = usb_clear_port_feature(hub->hdev, in usb_port_resume()
3795 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); in usb_port_resume()
3798 dev_dbg(&udev->dev, "usb %sresume\n", in usb_port_resume()
3799 (PMSG_IS_AUTO(msg) ? "auto-" : "")); in usb_port_resume()
3806 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in usb_port_resume()
3811 udev->port_is_suspended = 0; in usb_port_resume()
3812 if (hub_is_superspeed(hub->hdev)) { in usb_port_resume()
3814 usb_clear_port_feature(hub->hdev, port1, in usb_port_resume()
3818 usb_clear_port_feature(hub->hdev, port1, in usb_port_resume()
3826 if (udev->persist_enabled) in usb_port_resume()
3827 status = wait_for_connected(udev, hub, port1, &portchange, in usb_port_resume()
3831 hub, port1, status, portchange, portstatus); in usb_port_resume()
3835 dev_dbg(&udev->dev, "can't resume, status %d\n", status); in usb_port_resume()
3836 hub_port_logical_disconnect(hub, port1); in usb_port_resume()
3855 if (udev->state == USB_STATE_SUSPENDED) { in usb_remote_wakeup()
3856 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); in usb_remote_wakeup()
3868 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, in hub_handle_remote_wakeup() argument
3870 __must_hold(&port_dev->status_lock) in hub_handle_remote_wakeup()
3872 struct usb_port *port_dev = hub->ports[port - 1]; in hub_handle_remote_wakeup()
3879 hdev = hub->hdev; in hub_handle_remote_wakeup()
3880 udev = port_dev->child; in hub_handle_remote_wakeup()
3887 if (!udev || udev->state != USB_STATE_SUSPENDED || in hub_handle_remote_wakeup()
3904 ret = -ENODEV; in hub_handle_remote_wakeup()
3905 hub_port_disable(hub, port, 1); in hub_handle_remote_wakeup()
3907 dev_dbg(&port_dev->dev, "resume, status %d\n", ret); in hub_handle_remote_wakeup()
3911 static int check_ports_changed(struct usb_hub *hub) in check_ports_changed() argument
3915 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { in check_ports_changed()
3919 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in check_ports_changed()
3928 struct usb_hub *hub = usb_get_intfdata(intf); in hub_suspend() local
3929 struct usb_device *hdev = hub->hdev; in hub_suspend()
3934 * Also, add up the number of wakeup-enabled descendants. in hub_suspend()
3936 hub->wakeup_enabled_descendants = 0; in hub_suspend()
3937 for (port1 = 1; port1 <= hdev->maxchild; port1++) { in hub_suspend()
3938 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_suspend()
3939 struct usb_device *udev = port_dev->child; in hub_suspend()
3941 if (udev && udev->can_submit) { in hub_suspend()
3942 dev_warn(&port_dev->dev, "device %s not suspended yet\n", in hub_suspend()
3943 dev_name(&udev->dev)); in hub_suspend()
3945 return -EBUSY; in hub_suspend()
3948 hub->wakeup_enabled_descendants += in hub_suspend()
3952 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { in hub_suspend()
3953 /* check if there are changes pending on hub ports */ in hub_suspend()
3954 if (check_ports_changed(hub)) { in hub_suspend()
3956 return -EBUSY; in hub_suspend()
3957 pm_wakeup_event(&hdev->dev, 2000); in hub_suspend()
3961 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { in hub_suspend()
3962 /* Enable hub to send remote wakeup for all ports. */ in hub_suspend()
3963 for (port1 = 1; port1 <= hdev->maxchild; port1++) { in hub_suspend()
3973 dev_dbg(&intf->dev, "%s\n", __func__); in hub_suspend()
3976 hub_quiesce(hub, HUB_SUSPEND); in hub_suspend()
3980 /* Report wakeup requests from the ports of a resuming root hub */
3981 static void report_wakeup_requests(struct usb_hub *hub) in report_wakeup_requests() argument
3983 struct usb_device *hdev = hub->hdev; in report_wakeup_requests()
3989 if (hdev->parent) in report_wakeup_requests()
3990 return; /* Not a root hub */ in report_wakeup_requests()
3992 hcd = bus_to_hcd(hdev->bus); in report_wakeup_requests()
3993 if (hcd->driver->get_resuming_ports) { in report_wakeup_requests()
4003 resuming_ports = hcd->driver->get_resuming_ports(hcd); in report_wakeup_requests()
4004 for (i = 0; i < hdev->maxchild; ++i) { in report_wakeup_requests()
4006 udev = hub->ports[i]->child; in report_wakeup_requests()
4008 pm_wakeup_event(&udev->dev, 0); in report_wakeup_requests()
4016 struct usb_hub *hub = usb_get_intfdata(intf); in hub_resume() local
4018 dev_dbg(&intf->dev, "%s\n", __func__); in hub_resume()
4019 hub_activate(hub, HUB_RESUME); in hub_resume()
4027 report_wakeup_requests(hub); in hub_resume()
4033 struct usb_hub *hub = usb_get_intfdata(intf); in hub_reset_resume() local
4035 dev_dbg(&intf->dev, "%s\n", __func__); in hub_reset_resume()
4036 hub_activate(hub, HUB_RESET_RESUME); in hub_reset_resume()
4041 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
4042 * @rhdev: struct usb_device for the root hub
4044 * The USB host controller driver calls this function when its root hub
4047 * When the hub driver is resumed it will take notice and carry out
4048 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
4053 dev_notice(&rhdev->dev, "root hub lost power or was reset\n"); in usb_root_hub_lost_power()
4054 rhdev->reset_resume = 1; in usb_root_hub_lost_power()
4067 * device-initiated U1 or U2. This lets the device know the exit latencies from
4083 if (!udev->parent || udev->speed < USB_SPEED_SUPER || !udev->lpm_capable) in usb_req_set_sel()
4087 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in usb_req_set_sel()
4088 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); in usb_req_set_sel()
4089 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in usb_req_set_sel()
4090 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); in usb_req_set_sel()
4097 * latency for the link state, and could start a device-initiated in usb_req_set_sel()
4104 dev_dbg(&udev->dev, "Device-initiated U1/U2 disabled due to long SEL or PEL\n"); in usb_req_set_sel()
4105 return -EINVAL; in usb_req_set_sel()
4115 return -ENOMEM; in usb_req_set_sel()
4117 sel_values->u1_sel = u1_sel; in usb_req_set_sel()
4118 sel_values->u1_pel = u1_pel; in usb_req_set_sel()
4119 sel_values->u2_sel = cpu_to_le16(u2_sel); in usb_req_set_sel()
4120 sel_values->u2_pel = cpu_to_le16(u2_pel); in usb_req_set_sel()
4131 udev->lpm_devinit_allow = 1; in usb_req_set_sel()
4137 * Enable or disable device-initiated U1 or U2 transitions.
4153 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", in usb_set_device_initiated_lpm()
4155 return -EINVAL; in usb_set_device_initiated_lpm()
4158 if (udev->state != USB_STATE_CONFIGURED) { in usb_set_device_initiated_lpm()
4159 dev_dbg(&udev->dev, "%s: Can't %s %s state " in usb_set_device_initiated_lpm()
4168 * Now send the control transfer to enable device-initiated LPM in usb_set_device_initiated_lpm()
4186 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", in usb_set_device_initiated_lpm()
4188 return -EBUSY; in usb_set_device_initiated_lpm()
4207 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", in usb_set_lpm_timeout()
4209 return -EINVAL; in usb_set_lpm_timeout()
4214 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " in usb_set_lpm_timeout()
4217 return -EINVAL; in usb_set_lpm_timeout()
4220 ret = set_port_feature(udev->parent, in usb_set_lpm_timeout()
4221 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, in usb_set_lpm_timeout()
4224 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," in usb_set_lpm_timeout()
4227 return -EBUSY; in usb_set_lpm_timeout()
4230 udev->u1_params.timeout = timeout; in usb_set_lpm_timeout()
4232 udev->u2_params.timeout = timeout; in usb_set_lpm_timeout()
4239 * periodic endpoint. See USB 3.2 section 9.4.9
4247 if (!udev->lpm_devinit_allow) in usb_device_may_initiate_lpm()
4251 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in usb_device_may_initiate_lpm()
4253 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in usb_device_may_initiate_lpm()
4257 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { in usb_device_may_initiate_lpm()
4262 intf = udev->actconfig->interface[i]; in usb_device_may_initiate_lpm()
4266 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) { in usb_device_may_initiate_lpm()
4267 desc = &intf->cur_altsetting->endpoint[j].desc; in usb_device_may_initiate_lpm()
4271 interval = (1 << (desc->bInterval - 1)) * 125; in usb_device_may_initiate_lpm()
4281 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
4285 * control transfers to set the hub timeout or enable device-initiated U1/U2
4288 * If the control transfer to enable device-initiated U1/U2 entry fails, then
4289 * hub-initiated U1/U2 will be disabled.
4291 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
4303 if (!udev->bos) in usb_enable_link_state()
4306 u1_mel = udev->bos->ss_cap->bU1devExitLat; in usb_enable_link_state()
4307 u2_mel = udev->bos->ss_cap->bU2DevExitLat; in usb_enable_link_state()
4322 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); in usb_enable_link_state()
4329 dev_warn(&udev->dev, "Could not enable %s link state, " in usb_enable_link_state()
4336 /* If we can't set the parent hub U1/U2 timeout, in usb_enable_link_state()
4337 * device-initiated LPM won't be allowed either, so let the xHCI in usb_enable_link_state()
4340 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); in usb_enable_link_state()
4347 if (udev->actconfig && in usb_enable_link_state()
4355 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); in usb_enable_link_state()
4361 udev->usb3_lpm_u1_enabled = 1; in usb_enable_link_state()
4363 udev->usb3_lpm_u2_enabled = 1; in usb_enable_link_state()
4366 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
4369 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
4372 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
4373 * it won't have an effect on the bus link state because the parent hub will
4374 * still disallow device-initiated U1/U2 entry.
4388 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", in usb_disable_link_state()
4390 return -EINVAL; in usb_disable_link_state()
4394 return -EBUSY; in usb_disable_link_state()
4398 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) in usb_disable_link_state()
4399 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " in usb_disable_link_state()
4403 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM in usb_disable_link_state()
4404 * is disabled. Hub will disallows link to enter U1/U2 as well, in usb_disable_link_state()
4405 * even device is initiating LPM. Hence LPM is disabled if hub LPM in usb_disable_link_state()
4406 * timeout set to 0, no matter device-initiated LPM is disabled or in usb_disable_link_state()
4410 udev->usb3_lpm_u1_enabled = 0; in usb_disable_link_state()
4412 udev->usb3_lpm_u2_enabled = 0; in usb_disable_link_state()
4418 * Disable hub-initiated and device-initiated U1 and U2 entry.
4422 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
4428 if (!udev || !udev->parent || in usb_disable_lpm()
4429 udev->speed < USB_SPEED_SUPER || in usb_disable_lpm()
4430 !udev->lpm_capable || in usb_disable_lpm()
4431 udev->state < USB_STATE_CONFIGURED) in usb_disable_lpm()
4434 hcd = bus_to_hcd(udev->bus); in usb_disable_lpm()
4435 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) in usb_disable_lpm()
4438 udev->lpm_disable_count++; in usb_disable_lpm()
4439 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) in usb_disable_lpm()
4452 return -EBUSY; in usb_disable_lpm()
4459 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_unlocked_disable_lpm()
4463 return -EINVAL; in usb_unlocked_disable_lpm()
4465 mutex_lock(hcd->bandwidth_mutex); in usb_unlocked_disable_lpm()
4467 mutex_unlock(hcd->bandwidth_mutex); in usb_unlocked_disable_lpm()
4474 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
4484 struct usb_hub *hub; in usb_enable_lpm() local
4487 if (!udev || !udev->parent || in usb_enable_lpm()
4488 udev->speed < USB_SPEED_SUPER || in usb_enable_lpm()
4489 !udev->lpm_capable || in usb_enable_lpm()
4490 udev->state < USB_STATE_CONFIGURED) in usb_enable_lpm()
4493 udev->lpm_disable_count--; in usb_enable_lpm()
4494 hcd = bus_to_hcd(udev->bus); in usb_enable_lpm()
4498 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || in usb_enable_lpm()
4499 !hcd->driver->disable_usb3_lpm_timeout) in usb_enable_lpm()
4502 if (udev->lpm_disable_count > 0) in usb_enable_lpm()
4505 hub = usb_hub_to_struct_hub(udev->parent); in usb_enable_lpm()
4506 if (!hub) in usb_enable_lpm()
4509 port_dev = hub->ports[udev->portnum - 1]; in usb_enable_lpm()
4511 if (port_dev->usb3_lpm_u1_permit) in usb_enable_lpm()
4514 if (port_dev->usb3_lpm_u2_permit) in usb_enable_lpm()
4522 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_unlocked_enable_lpm()
4527 mutex_lock(hcd->bandwidth_mutex); in usb_unlocked_enable_lpm()
4529 mutex_unlock(hcd->bandwidth_mutex); in usb_unlocked_enable_lpm()
4534 static void hub_usb3_port_prepare_disable(struct usb_hub *hub, in hub_usb3_port_prepare_disable() argument
4537 struct usb_device *udev = port_dev->child; in hub_usb3_port_prepare_disable()
4540 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { in hub_usb3_port_prepare_disable()
4541 ret = hub_set_port_link_state(hub, port_dev->portnum, in hub_usb3_port_prepare_disable()
4548 dev_warn(&udev->dev, in hub_usb3_port_prepare_disable()
4550 udev->do_remote_wakeup = 0; in hub_usb3_port_prepare_disable()
4560 static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, in hub_usb3_port_prepare_disable() argument
4590 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, in hub_handle_remote_wakeup() argument
4604 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
4605 * a connection with a plugged-in cable but will signal the host when the cable
4606 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
4608 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) in hub_port_disable() argument
4610 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_disable()
4611 struct usb_device *hdev = hub->hdev; in hub_port_disable()
4614 if (!hub->error) { in hub_port_disable()
4615 if (hub_is_superspeed(hub->hdev)) { in hub_port_disable()
4616 hub_usb3_port_prepare_disable(hub, port_dev); in hub_port_disable()
4617 ret = hub_set_port_link_state(hub, port_dev->portnum, in hub_port_disable()
4624 if (port_dev->child && set_state) in hub_port_disable()
4625 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); in hub_port_disable()
4626 if (ret && ret != -ENODEV) in hub_port_disable()
4627 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); in hub_port_disable()
4632 * usb_port_disable - disable a usb device's upstream port
4636 * Disables a USB device that isn't in active use.
4640 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_port_disable() local
4642 return hub_port_disable(hub, udev->portnum, 0); in usb_port_disable()
4645 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4648 * of 100ms at least for debounce and power-settling. The corresponding
4651 * Apparently there are some bluetooth and irda-dongles and a number of
4652 * low-speed devices for which this debounce period may last over a second.
4653 * Not covered by the spec - but easy to deal with.
4656 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4660 int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) in hub_port_debounce() argument
4666 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_debounce()
4669 ret = usb_hub_port_status(hub, port1, &portstatus, &portchange); in hub_port_debounce()
4686 usb_clear_port_feature(hub->hdev, port1, in hub_port_debounce()
4695 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", in hub_port_debounce()
4699 return -ETIMEDOUT; in hub_port_debounce()
4707 usb_enable_endpoint(udev, &udev->ep0, true); in usb_ep0_reinit()
4715 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_set_address()
4716 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in hub_set_address() local
4718 if (hub->hdev->quirks & USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT) in hub_set_address()
4725 if (!hcd->driver->address_device && devnum <= 1) in hub_set_address()
4726 return -EINVAL; in hub_set_address()
4727 if (udev->state == USB_STATE_ADDRESS) in hub_set_address()
4729 if (udev->state != USB_STATE_DEFAULT) in hub_set_address()
4730 return -EINVAL; in hub_set_address()
4731 if (hcd->driver->address_device) in hub_set_address()
4732 retval = hcd->driver->address_device(hcd, udev, timeout_ms); in hub_set_address()
4747 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4748 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4751 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4752 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4757 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in hub_set_initial_usb2_lpm_policy() local
4760 if (!udev->usb2_hw_lpm_capable || !udev->bos) in hub_set_initial_usb2_lpm_policy()
4763 if (hub) in hub_set_initial_usb2_lpm_policy()
4764 connect_type = hub->ports[udev->portnum - 1]->connect_type; in hub_set_initial_usb2_lpm_policy()
4766 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || in hub_set_initial_usb2_lpm_policy()
4768 udev->usb2_hw_lpm_allowed = 1; in hub_set_initial_usb2_lpm_policy()
4775 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_enable_device()
4777 if (!hcd->driver->enable_device) in hub_enable_device()
4779 if (udev->state == USB_STATE_ADDRESS) in hub_enable_device()
4781 if (udev->state != USB_STATE_DEFAULT) in hub_enable_device()
4782 return -EINVAL; in hub_enable_device()
4784 return hcd->driver->enable_device(hcd, udev); in hub_enable_device()
4797 * which might not be kosher according to the USB spec but it's what
4814 buf->bDescriptorType = buf->bMaxPacketSize0 = 0; in get_bMaxPacketSize0()
4820 switch (buf->bMaxPacketSize0) { in get_bMaxPacketSize0()
4822 if (buf->bDescriptorType == USB_DT_DEVICE) { in get_bMaxPacketSize0()
4823 rc = buf->bMaxPacketSize0; in get_bMaxPacketSize0()
4829 rc = -EPROTO; in get_bMaxPacketSize0()
4837 * attempt, lest we get into a time-out/reset loop. in get_bMaxPacketSize0()
4839 if (rc > 0 || (rc == -ETIMEDOUT && first_time && in get_bMaxPacketSize0()
4840 udev->speed > USB_SPEED_FULL)) in get_bMaxPacketSize0()
4852 * If this is called for an already-existing device (as part of
4860 * @udev->descriptor. For an already existing device, @dev_descr
4861 * must be non-NULL. The device descriptor will be stored there,
4862 * not in @udev->descriptor, because descriptors for registered
4866 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, in hub_port_init() argument
4869 struct usb_device *hdev = hub->hdev; in hub_port_init()
4870 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); in hub_port_init()
4871 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_init()
4874 enum usb_device_speed oldspeed = udev->speed; in hub_port_init()
4876 int devnum = udev->devnum; in hub_port_init()
4885 return -ENOMEM; in hub_port_init()
4887 /* root hub ports have a slightly longer reset period in hub_port_init()
4888 * (from USB 2.0 spec, section 7.1.7.5) in hub_port_init()
4890 if (!hdev->parent) { in hub_port_init()
4892 if (port1 == hdev->bus->otg_port) in hub_port_init()
4893 hdev->bus->b_hnp_enable = 0; in hub_port_init()
4902 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ in hub_port_init()
4903 retval = hub_port_reset(hub, port1, udev, delay, false); in hub_port_init()
4908 retval = -ENODEV; in hub_port_init()
4910 /* Don't allow speed changes at reset, except usb 3.0 to faster */ in hub_port_init()
4911 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && in hub_port_init()
4912 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { in hub_port_init()
4913 dev_dbg(&udev->dev, "device reset changed speed!\n"); in hub_port_init()
4916 oldspeed = udev->speed; in hub_port_init()
4919 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... in hub_port_init()
4922 switch (udev->speed) { in hub_port_init()
4925 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); in hub_port_init()
4928 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); in hub_port_init()
4935 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); in hub_port_init()
4938 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); in hub_port_init()
4945 speed = usb_speed_string(udev->speed); in hub_port_init()
4952 * platform device is usually a dual-role USB controller device. in hub_port_init()
4954 if (udev->bus->controller->driver) in hub_port_init()
4955 driver_name = udev->bus->controller->driver->name; in hub_port_init()
4957 driver_name = udev->bus->sysdev->driver->name; in hub_port_init()
4959 if (udev->speed < USB_SPEED_SUPER) in hub_port_init()
4960 dev_info(&udev->dev, in hub_port_init()
4961 "%s %s USB device number %d using %s\n", in hub_port_init()
4967 if (hdev->tt) { in hub_port_init()
4968 udev->tt = hdev->tt; in hub_port_init()
4969 udev->ttport = hdev->ttport; in hub_port_init()
4970 } else if (udev->speed != USB_SPEED_HIGH in hub_port_init()
4971 && hdev->speed == USB_SPEED_HIGH) { in hub_port_init()
4972 if (!hub->tt.hub) { in hub_port_init()
4973 dev_err(&udev->dev, "parent hub has no TT\n"); in hub_port_init()
4974 retval = -EINVAL; in hub_port_init()
4977 udev->tt = &hub->tt; in hub_port_init()
4978 udev->ttport = port1; in hub_port_init()
4988 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, in hub_port_init()
4989 * so it may help with some non-standards-compliant devices. in hub_port_init()
4997 if (hub_port_stop_enumerate(hub, port1, retries)) { in hub_port_init()
4998 retval = -ENODEV; in hub_port_init()
5005 dev_err(&udev->dev, in hub_port_init()
5006 "hub failed to enable device, error %d\n", in hub_port_init()
5014 maxp0 != udev->descriptor.bMaxPacketSize0) { in hub_port_init()
5015 dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); in hub_port_init()
5016 retval = -ENODEV; in hub_port_init()
5020 retval = hub_port_reset(hub, port1, udev, delay, false); in hub_port_init()
5023 if (oldspeed != udev->speed) { in hub_port_init()
5024 dev_dbg(&udev->dev, in hub_port_init()
5026 retval = -ENODEV; in hub_port_init()
5030 if (maxp0 != -ENODEV) in hub_port_init()
5031 dev_err(&udev->dev, "device descriptor read/64, error %d\n", in hub_port_init()
5045 if (retval != -ENODEV) in hub_port_init()
5046 dev_err(&udev->dev, "device not accepting address %d, error %d\n", in hub_port_init()
5050 if (udev->speed >= USB_SPEED_SUPER) { in hub_port_init()
5051 devnum = udev->devnum; in hub_port_init()
5052 dev_info(&udev->dev, in hub_port_init()
5053 "%s SuperSpeed%s%s USB device number %d using %s\n", in hub_port_init()
5054 (udev->config) ? "reset" : "new", in hub_port_init()
5055 (udev->speed == USB_SPEED_SUPER_PLUS) ? in hub_port_init()
5057 (udev->ssp_rate == USB_SSP_GEN_2x2) ? in hub_port_init()
5059 (udev->ssp_rate == USB_SSP_GEN_2x1) ? in hub_port_init()
5061 (udev->ssp_rate == USB_SSP_GEN_1x2) ? in hub_port_init()
5068 * - let SET_ADDRESS settle, some device hardware wants it in hub_port_init()
5069 * - read ep0 maxpacket even for high and low speed, in hub_port_init()
5079 if (retval != -ENODEV) in hub_port_init()
5080 dev_err(&udev->dev, in hub_port_init()
5086 if (!initial && maxp0 != udev->descriptor.bMaxPacketSize0) { in hub_port_init()
5087 dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); in hub_port_init()
5088 retval = -ENODEV; in hub_port_init()
5092 delay = udev->parent->hub_delay; in hub_port_init()
5093 udev->hub_delay = min_t(u32, delay, in hub_port_init()
5097 dev_dbg(&udev->dev, in hub_port_init()
5114 if (udev->speed >= USB_SPEED_SUPER) { in hub_port_init()
5120 if (usb_endpoint_maxp(&udev->ep0.desc) == i) { in hub_port_init()
5122 } else if (((udev->speed == USB_SPEED_FULL || in hub_port_init()
5123 udev->speed == USB_SPEED_HIGH) && in hub_port_init()
5125 (udev->speed >= USB_SPEED_SUPER && i > 0)) { in hub_port_init()
5127 if (udev->speed == USB_SPEED_FULL) in hub_port_init()
5128 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); in hub_port_init()
5130 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); in hub_port_init()
5131 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); in hub_port_init()
5135 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", maxp0); in hub_port_init()
5136 retval = -EMSGSIZE; in hub_port_init()
5143 if (retval != -ENODEV) in hub_port_init()
5144 dev_err(&udev->dev, "device descriptor read/all, error %d\n", in hub_port_init()
5149 udev->descriptor = *descr; in hub_port_init()
5156 * and attached to a superspeed hub port, but the device descriptor in hub_port_init()
5160 if ((udev->speed >= USB_SPEED_SUPER) && in hub_port_init()
5161 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { in hub_port_init()
5162 dev_err(&udev->dev, "got a wrong device descriptor, warm reset device\n"); in hub_port_init()
5163 hub_port_reset(hub, port1, udev, HUB_BH_RESET_TIME, true); in hub_port_init()
5164 retval = -EINVAL; in hub_port_init()
5170 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { in hub_port_init()
5173 udev->lpm_capable = usb_device_supports_lpm(udev); in hub_port_init()
5174 udev->lpm_disable_count = 1; in hub_port_init()
5182 if (hcd->driver->update_device) in hub_port_init()
5183 hcd->driver->update_device(hcd, udev); in hub_port_init()
5187 hub_port_disable(hub, port1, 0); in hub_port_init()
5195 check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) in check_highspeed() argument
5200 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) in check_highspeed()
5210 dev_info(&udev->dev, "not running at top speed; " in check_highspeed()
5211 "connect to a high speed hub\n"); in check_highspeed()
5212 /* hub LEDs are probably harder to miss than syslog */ in check_highspeed()
5213 if (hub->has_indicators) { in check_highspeed()
5214 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; in check_highspeed()
5216 &hub->leds, 0); in check_highspeed()
5223 hub_power_remaining(struct usb_hub *hub) in hub_power_remaining() argument
5225 struct usb_device *hdev = hub->hdev; in hub_power_remaining()
5229 if (!hub->limited_power) in hub_power_remaining()
5232 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; in hub_power_remaining()
5233 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { in hub_power_remaining()
5234 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_power_remaining()
5235 struct usb_device *udev = port_dev->child; in hub_power_remaining()
5250 if (udev->actconfig) in hub_power_remaining()
5251 delta = usb_get_max_power(udev, udev->actconfig); in hub_power_remaining()
5252 else if (port1 != udev->bus->otg_port || hdev->parent) in hub_power_remaining()
5256 if (delta > hub->mA_per_port) in hub_power_remaining()
5257 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", in hub_power_remaining()
5258 delta, hub->mA_per_port); in hub_power_remaining()
5259 remaining -= delta; in hub_power_remaining()
5262 dev_warn(hub->intfdev, "%dmA over power budget!\n", in hub_power_remaining()
5263 -remaining); in hub_power_remaining()
5282 if (memcmp(&udev->descriptor, new_device_descriptor, in descriptors_changed()
5286 if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) in descriptors_changed()
5288 if (udev->bos) { in descriptors_changed()
5289 len = le16_to_cpu(udev->bos->desc->wTotalLength); in descriptors_changed()
5290 if (len != le16_to_cpu(old_bos->desc->wTotalLength)) in descriptors_changed()
5292 if (memcmp(udev->bos->desc, old_bos->desc, len)) in descriptors_changed()
5302 if (udev->serial) in descriptors_changed()
5303 serial_len = strlen(udev->serial) + 1; in descriptors_changed()
5306 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { in descriptors_changed()
5307 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); in descriptors_changed()
5316 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { in descriptors_changed()
5317 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); in descriptors_changed()
5321 dev_dbg(&udev->dev, "config index %d, error %d\n", in descriptors_changed()
5326 if (memcmp(buf, udev->rawdescriptors[index], old_length) in descriptors_changed()
5328 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", in descriptors_changed()
5330 ((struct usb_config_descriptor *) buf)-> in descriptors_changed()
5338 length = usb_string(udev, udev->descriptor.iSerialNumber, in descriptors_changed()
5341 dev_dbg(&udev->dev, "serial string error %d\n", in descriptors_changed()
5344 } else if (memcmp(buf, udev->serial, length) != 0) { in descriptors_changed()
5345 dev_dbg(&udev->dev, "serial string changed\n"); in descriptors_changed()
5354 static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, in hub_port_connect() argument
5357 int status = -ENODEV; in hub_port_connect()
5360 struct usb_device *hdev = hub->hdev; in hub_port_connect()
5361 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); in hub_port_connect()
5362 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_connect()
5363 struct usb_device *udev = port_dev->child; in hub_port_connect()
5364 static int unreliable_port = -1; in hub_port_connect()
5369 if (hcd->usb_phy && !hdev->parent) in hub_port_connect()
5370 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); in hub_port_connect()
5371 usb_disconnect(&port_dev->child); in hub_port_connect()
5379 clear_bit(port1, hub->removed_bits); in hub_port_connect()
5383 status = hub_port_debounce_be_stable(hub, port1); in hub_port_connect()
5385 if (status != -ENODEV && in hub_port_connect()
5388 dev_err(&port_dev->dev, "connect-debounce failed\n"); in hub_port_connect()
5400 test_bit(port1, hub->removed_bits)) { in hub_port_connect()
5403 * maybe switch power back on (e.g. root hub was reset) in hub_port_connect()
5406 if (hub_is_port_power_switchable(hub) in hub_port_connect()
5407 && !usb_port_is_power_on(hub, portstatus) in hub_port_connect()
5408 && !port_dev->port_owner) in hub_port_connect()
5415 if (hub_is_superspeed(hub->hdev)) in hub_port_connect()
5423 if (hub_port_stop_enumerate(hub, port1, i)) { in hub_port_connect()
5424 status = -ENODEV; in hub_port_connect()
5429 mutex_lock(hcd->address0_mutex); in hub_port_connect()
5434 udev = usb_alloc_dev(hdev, hdev->bus, port1); in hub_port_connect()
5436 dev_err(&port_dev->dev, in hub_port_connect()
5438 mutex_unlock(hcd->address0_mutex); in hub_port_connect()
5444 udev->bus_mA = hub->mA_per_port; in hub_port_connect()
5445 udev->level = hdev->level + 1; in hub_port_connect()
5447 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ in hub_port_connect()
5448 if (hub_is_superspeed(hub->hdev)) in hub_port_connect()
5449 udev->speed = USB_SPEED_SUPER; in hub_port_connect()
5451 udev->speed = USB_SPEED_UNKNOWN; in hub_port_connect()
5454 if (udev->devnum <= 0) { in hub_port_connect()
5455 status = -ENOTCONN; /* Don't retry */ in hub_port_connect()
5459 /* reset (non-USB 3.0 devices) and get descriptor */ in hub_port_connect()
5460 status = hub_port_init(hub, udev, port1, i, NULL); in hub_port_connect()
5464 mutex_unlock(hcd->address0_mutex); in hub_port_connect()
5468 if (udev->quirks & USB_QUIRK_DELAY_INIT) in hub_port_connect()
5471 /* consecutive bus-powered hubs aren't reliable; they can in hub_port_connect()
5474 * (without reading syslog), even without per-port LEDs in hub_port_connect()
5477 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB in hub_port_connect()
5478 && udev->bus_mA <= unit_load) { in hub_port_connect()
5484 dev_dbg(&udev->dev, "get status %d ?\n", status); in hub_port_connect()
5488 dev_err(&udev->dev, in hub_port_connect()
5489 "can't connect bus-powered hub " in hub_port_connect()
5491 if (hub->has_indicators) { in hub_port_connect()
5492 hub->indicator[port1-1] = in hub_port_connect()
5496 &hub->leds, 0); in hub_port_connect()
5498 status = -ENOTCONN; /* Don't retry */ in hub_port_connect()
5504 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 in hub_port_connect()
5505 && udev->speed == USB_SPEED_FULL in hub_port_connect()
5507 check_highspeed(hub, udev, port1); in hub_port_connect()
5517 /* We mustn't add new devices if the parent hub has in hub_port_connect()
5522 if (hdev->state == USB_STATE_NOTATTACHED) in hub_port_connect()
5523 status = -ENOTCONN; in hub_port_connect()
5525 port_dev->child = udev; in hub_port_connect()
5535 port_dev->child = NULL; in hub_port_connect()
5539 if (hcd->usb_phy && !hdev->parent) in hub_port_connect()
5540 usb_phy_notify_connect(hcd->usb_phy, in hub_port_connect()
5541 udev->speed); in hub_port_connect()
5548 status = hub_power_remaining(hub); in hub_port_connect()
5550 dev_dbg(hub->intfdev, "%dmA power budget left\n", status); in hub_port_connect()
5555 hub_port_disable(hub, port1, 1); in hub_port_connect()
5561 mutex_unlock(hcd->address0_mutex); in hub_port_connect()
5565 if ((status == -ENOTCONN) || (status == -ENOTSUPP)) in hub_port_connect()
5568 /* When halfway through our retry count, power-cycle the port */ in hub_port_connect()
5569 if (i == (PORT_INIT_TRIES - 1) / 2) { in hub_port_connect()
5570 dev_info(&port_dev->dev, "attempt power cycle\n"); in hub_port_connect()
5571 usb_hub_set_port_power(hdev, hub, port1, false); in hub_port_connect()
5572 msleep(2 * hub_power_on_good_delay(hub)); in hub_port_connect()
5573 usb_hub_set_port_power(hdev, hub, port1, true); in hub_port_connect()
5574 msleep(hub_power_on_good_delay(hub)); in hub_port_connect()
5577 if (hub->hdev->parent || in hub_port_connect()
5578 !hcd->driver->port_handed_over || in hub_port_connect()
5579 !(hcd->driver->port_handed_over)(hcd, port1)) { in hub_port_connect()
5580 if (status != -ENOTCONN && status != -ENODEV) in hub_port_connect()
5581 dev_err(&port_dev->dev, in hub_port_connect()
5582 "unable to enumerate USB device\n"); in hub_port_connect()
5586 hub_port_disable(hub, port1, 1); in hub_port_connect()
5587 if (hcd->driver->relinquish_port && !hub->hdev->parent) { in hub_port_connect()
5588 if (status != -ENOTCONN && status != -ENODEV) in hub_port_connect()
5589 hcd->driver->relinquish_port(hcd, port1); in hub_port_connect()
5595 * a port connection-change occurs;
5596 * a port enable-change occurs (often caused by EMI);
5599 * caller already locked the hub
5601 static void hub_port_connect_change(struct usb_hub *hub, int port1, in hub_port_connect_change() argument
5603 __must_hold(&port_dev->status_lock) in hub_port_connect_change()
5605 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_connect_change()
5606 struct usb_device *udev = port_dev->child; in hub_port_connect_change()
5608 int status = -ENODEV; in hub_port_connect_change()
5610 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, in hub_port_connect_change()
5611 portchange, portspeed(hub, portstatus)); in hub_port_connect_change()
5613 if (hub->has_indicators) { in hub_port_connect_change()
5614 set_port_led(hub, port1, HUB_LED_AUTO); in hub_port_connect_change()
5615 hub->indicator[port1-1] = INDICATOR_AUTO; in hub_port_connect_change()
5620 if (hub->hdev->bus->is_b_host) in hub_port_connect_change()
5627 udev->state != USB_STATE_NOTATTACHED) { in hub_port_connect_change()
5630 * USB-3 connections are initialized automatically by in hub_port_connect_change()
5637 dev_dbg(&udev->dev, in hub_port_connect_change()
5642 udev->bos)) { in hub_port_connect_change()
5643 dev_dbg(&udev->dev, in hub_port_connect_change()
5651 } else if (udev->state == USB_STATE_SUSPENDED && in hub_port_connect_change()
5652 udev->persist_enabled) { in hub_port_connect_change()
5664 clear_bit(port1, hub->change_bits); in hub_port_connect_change()
5671 hub_port_connect(hub, port1, portstatus, portchange); in hub_port_connect_change()
5675 /* Handle notifying userspace about hub over-current events */
5682 sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); in port_over_current_notify()
5684 hub_dev = port_dev->dev.parent; in port_over_current_notify()
5689 port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); in port_over_current_notify()
5698 port_dev->over_current_count); in port_over_current_notify()
5702 kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); in port_over_current_notify()
5710 static void port_event(struct usb_hub *hub, int port1) in port_event() argument
5711 __must_hold(&port_dev->status_lock) in port_event()
5714 struct usb_port *port_dev = hub->ports[port1 - 1]; in port_event()
5715 struct usb_device *udev = port_dev->child; in port_event()
5716 struct usb_device *hdev = hub->hdev; in port_event()
5720 connect_change = test_bit(port1, hub->change_bits); in port_event()
5721 clear_bit(port1, hub->event_bits); in port_event()
5722 clear_bit(port1, hub->wakeup_bits); in port_event()
5724 if (usb_hub_port_status(hub, port1, &portstatus, &portchange) < 0) in port_event()
5734 dev_dbg(&port_dev->dev, "enable change, status %08x\n", in port_event()
5739 * EM interference sometimes causes badly shielded USB devices in port_event()
5740 * to be shutdown by the hub, this hack enables them again. in port_event()
5745 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); in port_event()
5752 port_dev->over_current_count++; in port_event()
5755 dev_dbg(&port_dev->dev, "over-current change #%u\n", in port_event()
5756 port_dev->over_current_count); in port_event()
5760 hub_power_on(hub, true); in port_event()
5761 usb_hub_port_status(hub, port1, &status, &unused); in port_event()
5763 dev_err(&port_dev->dev, "over-current condition\n"); in port_event()
5767 dev_dbg(&port_dev->dev, "reset change\n"); in port_event()
5772 dev_dbg(&port_dev->dev, "warm reset change\n"); in port_event()
5777 dev_dbg(&port_dev->dev, "link state change\n"); in port_event()
5782 dev_warn(&port_dev->dev, "config error\n"); in port_event()
5788 if (!pm_runtime_active(&port_dev->dev)) in port_event()
5792 if (port_dev->ignore_event && port_dev->early_stop) in port_event()
5795 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) in port_event()
5804 while (hub_port_warm_reset_required(hub, port1, portstatus)) { in port_event()
5809 usb_hub_port_status(hub, port1, &portstatus, &unused); in port_event()
5810 dev_dbg(&port_dev->dev, "Wait for inactive link disconnect detect\n"); in port_event()
5813 || udev->state == USB_STATE_NOTATTACHED) { in port_event()
5814 dev_dbg(&port_dev->dev, "do warm reset, port only\n"); in port_event()
5815 if (hub_port_reset(hub, port1, NULL, in port_event()
5817 hub_port_disable(hub, port1, 1); in port_event()
5819 dev_dbg(&port_dev->dev, "do warm reset, full device\n"); in port_event()
5831 hub_port_connect_change(hub, port1, portstatus, portchange); in port_event()
5838 struct usb_hub *hub; in hub_event() local
5844 hub = container_of(work, struct usb_hub, events); in hub_event()
5845 hdev = hub->hdev; in hub_event()
5846 hub_dev = hub->intfdev; in hub_event()
5849 kcov_remote_start_usb((u64)hdev->bus->busnum); in hub_event()
5852 hdev->state, hdev->maxchild, in hub_event()
5854 (u16) hub->change_bits[0], in hub_event()
5855 (u16) hub->event_bits[0]); in hub_event()
5860 if (unlikely(hub->disconnected)) in hub_event()
5863 /* If the hub has died, clean up after it */ in hub_event()
5864 if (hdev->state == USB_STATE_NOTATTACHED) { in hub_event()
5865 hub->error = -ENODEV; in hub_event()
5866 hub_quiesce(hub, HUB_DISCONNECT); in hub_event()
5877 /* If this is an inactive hub, do nothing */ in hub_event()
5878 if (hub->quiescing) in hub_event()
5881 if (hub->error) { in hub_event()
5882 dev_dbg(hub_dev, "resetting for error %d\n", hub->error); in hub_event()
5886 dev_dbg(hub_dev, "error resetting hub: %d\n", ret); in hub_event()
5890 hub->nerrors = 0; in hub_event()
5891 hub->error = 0; in hub_event()
5895 for (i = 1; i <= hdev->maxchild; i++) { in hub_event()
5896 struct usb_port *port_dev = hub->ports[i - 1]; in hub_event()
5898 if (test_bit(i, hub->event_bits) in hub_event()
5899 || test_bit(i, hub->change_bits) in hub_event()
5900 || test_bit(i, hub->wakeup_bits)) { in hub_event()
5907 * (powered-off), we leave it in that state, run in hub_event()
5910 pm_runtime_get_noresume(&port_dev->dev); in hub_event()
5911 pm_runtime_barrier(&port_dev->dev); in hub_event()
5913 port_event(hub, i); in hub_event()
5915 pm_runtime_put_sync(&port_dev->dev); in hub_event()
5919 /* deal with hub status changes */ in hub_event()
5920 if (test_and_clear_bit(0, hub->event_bits) == 0) in hub_event()
5922 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) in hub_event()
5930 hub->limited_power = 1; in hub_event()
5932 hub->limited_power = 0; in hub_event()
5938 dev_dbg(hub_dev, "over-current change\n"); in hub_event()
5941 hub_power_on(hub, true); in hub_event()
5942 hub_hub_status(hub, &status, &unused); in hub_event()
5944 dev_err(hub_dev, "over-current condition\n"); in hub_event()
5956 hub_put(hub); in hub_event()
6011 MODULE_DEVICE_TABLE(usb, hub_id_table);
6014 .name = "hub",
6030 printk(KERN_ERR "%s: can't register hub driver\n", in usb_hub_init()
6032 return -1; in usb_hub_init()
6037 * USB-PERSIST port handover. Otherwise it might see that a full-speed in usb_hub_init()
6039 * over to the companion full-speed controller. in usb_hub_init()
6047 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); in usb_hub_init()
6049 return -1; in usb_hub_init()
6057 * Hub resources are freed for us by usb_deregister. It calls in usb_hub_cleanup()
6061 * individual hub resources. -greg in usb_hub_cleanup()
6067 * hub_hc_release_resources - clear resources used by host controller
6073 * making any operation on resuming usb device. The host controller resources
6075 * usb device in tree toward the root hub. This function is used only during
6076 * resuming device when usb device require reinitialization – that is, when
6077 * flag udev->reset_resume is set.
6083 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in hub_hc_release_resources() local
6084 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_hc_release_resources()
6088 for (i = 0; i < udev->maxchild; i++) in hub_hc_release_resources()
6089 if (hub->ports[i]->child) in hub_hc_release_resources()
6090 hub_hc_release_resources(hub->ports[i]->child); in hub_hc_release_resources()
6092 if (hcd->driver->reset_device) in hub_hc_release_resources()
6093 hcd->driver->reset_device(hcd, udev); in hub_hc_release_resources()
6097 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
6100 * WARNING - don't use this routine to reset a composite device
6109 * re-connected. All drivers will be unbound, and the device will be
6110 * re-enumerated and probed all over again.
6112 * Return: 0 if the reset succeeded, -ENODEV if the device has been
6132 struct usb_device *parent_hdev = udev->parent; in usb_reset_and_verify_device()
6134 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_reset_and_verify_device()
6138 int port1 = udev->portnum; in usb_reset_and_verify_device()
6140 if (udev->state == USB_STATE_NOTATTACHED || in usb_reset_and_verify_device()
6141 udev->state == USB_STATE_SUSPENDED) { in usb_reset_and_verify_device()
6142 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", in usb_reset_and_verify_device()
6143 udev->state); in usb_reset_and_verify_device()
6144 return -EINVAL; in usb_reset_and_verify_device()
6148 return -EISDIR; in usb_reset_and_verify_device()
6153 * It will be re-enabled by the enumeration process. in usb_reset_and_verify_device()
6157 bos = udev->bos; in usb_reset_and_verify_device()
6158 udev->bos = NULL; in usb_reset_and_verify_device()
6160 if (udev->reset_resume) in usb_reset_and_verify_device()
6163 mutex_lock(hcd->address0_mutex); in usb_reset_and_verify_device()
6167 ret = -ENODEV; in usb_reset_and_verify_device()
6172 * Other endpoints will be handled by re-enumeration. */ in usb_reset_and_verify_device()
6175 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) in usb_reset_and_verify_device()
6178 mutex_unlock(hcd->address0_mutex); in usb_reset_and_verify_device()
6185 dev_info(&udev->dev, "device firmware changed\n"); in usb_reset_and_verify_device()
6190 if (!udev->actconfig) in usb_reset_and_verify_device()
6193 mutex_lock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
6194 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); in usb_reset_and_verify_device()
6196 dev_warn(&udev->dev, in usb_reset_and_verify_device()
6199 mutex_unlock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
6204 udev->actconfig->desc.bConfigurationValue, 0, in usb_reset_and_verify_device()
6207 dev_err(&udev->dev, in usb_reset_and_verify_device()
6209 udev->actconfig->desc.bConfigurationValue, ret); in usb_reset_and_verify_device()
6210 mutex_unlock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
6213 mutex_unlock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
6217 * Don't bother to send the Set-Interface request for interfaces in usb_reset_and_verify_device()
6219 * many devices can't handle it. Instead just reset the host-side in usb_reset_and_verify_device()
6222 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { in usb_reset_and_verify_device()
6223 struct usb_host_config *config = udev->actconfig; in usb_reset_and_verify_device()
6224 struct usb_interface *intf = config->interface[i]; in usb_reset_and_verify_device()
6227 desc = &intf->cur_altsetting->desc; in usb_reset_and_verify_device()
6228 if (desc->bAlternateSetting == 0) { in usb_reset_and_verify_device()
6237 intf->resetting_device = 1; in usb_reset_and_verify_device()
6238 ret = usb_set_interface(udev, desc->bInterfaceNumber, in usb_reset_and_verify_device()
6239 desc->bAlternateSetting); in usb_reset_and_verify_device()
6240 intf->resetting_device = 0; in usb_reset_and_verify_device()
6243 dev_err(&udev->dev, "failed to restore interface %d " in usb_reset_and_verify_device()
6245 desc->bInterfaceNumber, in usb_reset_and_verify_device()
6246 desc->bAlternateSetting, in usb_reset_and_verify_device()
6251 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) in usb_reset_and_verify_device()
6252 intf->cur_altsetting->endpoint[j].streams = 0; in usb_reset_and_verify_device()
6256 /* Now that the alt settings are re-installed, enable LTM and LPM. */ in usb_reset_and_verify_device()
6261 udev->bos = bos; in usb_reset_and_verify_device()
6266 udev->bos = bos; in usb_reset_and_verify_device()
6268 return -ENODEV; in usb_reset_and_verify_device()
6272 * usb_reset_device - warn interface drivers and perform a USB port reset
6282 * being unbound or re-bound during the ongoing reset its disconnect()
6284 * routine returns -EINPROGRESS.
6303 struct usb_host_config *config = udev->actconfig; in usb_reset_device()
6304 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_reset_device() local
6306 if (udev->state == USB_STATE_NOTATTACHED) { in usb_reset_device()
6307 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", in usb_reset_device()
6308 udev->state); in usb_reset_device()
6309 return -EINVAL; in usb_reset_device()
6312 if (!udev->parent) { in usb_reset_device()
6313 /* this requires hcd-specific logic; see ohci_restart() */ in usb_reset_device()
6314 dev_dbg(&udev->dev, "%s for root hub!\n", __func__); in usb_reset_device()
6315 return -EISDIR; in usb_reset_device()
6318 if (udev->reset_in_progress) in usb_reset_device()
6319 return -EINPROGRESS; in usb_reset_device()
6320 udev->reset_in_progress = 1; in usb_reset_device()
6322 port_dev = hub->ports[udev->portnum - 1]; in usb_reset_device()
6326 * context to avoid possible deadlock if usb mass in usb_reset_device()
6331 * not been set before reseting the usb device. in usb_reset_device()
6339 for (i = 0; i < config->desc.bNumInterfaces; ++i) { in usb_reset_device()
6340 struct usb_interface *cintf = config->interface[i]; in usb_reset_device()
6344 if (cintf->dev.driver) { in usb_reset_device()
6345 drv = to_usb_driver(cintf->dev.driver); in usb_reset_device()
6346 if (drv->pre_reset && drv->post_reset) in usb_reset_device()
6347 unbind = (drv->pre_reset)(cintf); in usb_reset_device()
6348 else if (cintf->condition == in usb_reset_device()
6362 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { in usb_reset_device()
6363 struct usb_interface *cintf = config->interface[i]; in usb_reset_device()
6365 int rebind = cintf->needs_binding; in usb_reset_device()
6367 if (!rebind && cintf->dev.driver) { in usb_reset_device()
6368 drv = to_usb_driver(cintf->dev.driver); in usb_reset_device()
6369 if (drv->post_reset) in usb_reset_device()
6370 rebind = (drv->post_reset)(cintf); in usb_reset_device()
6371 else if (cintf->condition == in usb_reset_device()
6375 cintf->needs_binding = 1; in usb_reset_device()
6386 udev->reset_in_progress = 0; in usb_reset_device()
6393 * usb_queue_reset_device - Reset a USB device from an atomic context
6394 * @iface: USB interface belonging to the device to reset
6396 * This function can be used to reset a USB device from an atomic
6406 * - Scheduling two resets at the same time from two different drivers
6409 * handles ->pre_reset(), the second reset might happen or not.
6411 * - If the reset is delayed so long that the interface is unbound from
6414 * - This function can be called during .probe(). It can also be called
6417 * .disconnect(), call usb_reset_device() directly -- but watch out
6422 if (schedule_work(&iface->reset_ws)) in usb_queue_reset_device()
6428 * usb_hub_find_child - Get the pointer of child device
6430 * @hdev: USB device belonging to the usb hub
6434 * USB drivers call this function to get hub's child device
6438 * child's usb_device pointer if non-NULL.
6443 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_hub_find_child() local
6445 if (port1 < 1 || port1 > hdev->maxchild) in usb_hub_find_child()
6447 return hub->ports[port1 - 1]->child; in usb_hub_find_child()
6454 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_hub_adjust_deviceremovable() local
6458 if (!hub) in usb_hub_adjust_deviceremovable()
6462 for (i = 1; i <= hdev->maxchild; i++) { in usb_hub_adjust_deviceremovable()
6463 struct usb_port *port_dev = hub->ports[i - 1]; in usb_hub_adjust_deviceremovable()
6465 connect_type = port_dev->connect_type; in usb_hub_adjust_deviceremovable()
6469 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { in usb_hub_adjust_deviceremovable()
6470 … dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); in usb_hub_adjust_deviceremovable()
6471 desc->u.hs.DeviceRemovable[i/8] |= mask; in usb_hub_adjust_deviceremovable()
6476 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); in usb_hub_adjust_deviceremovable()
6478 for (i = 1; i <= hdev->maxchild; i++) { in usb_hub_adjust_deviceremovable()
6479 struct usb_port *port_dev = hub->ports[i - 1]; in usb_hub_adjust_deviceremovable()
6481 connect_type = port_dev->connect_type; in usb_hub_adjust_deviceremovable()
6486 … dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); in usb_hub_adjust_deviceremovable()
6492 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); in usb_hub_adjust_deviceremovable()
6498 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
6499 * @hdev: USB device belonging to the usb hub
6508 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_get_hub_port_acpi_handle() local
6510 if (!hub) in usb_get_hub_port_acpi_handle()
6513 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); in usb_get_hub_port_acpi_handle()