Lines Matching +full:usb +full:- +full:hub
1 // SPDX-License-Identifier: GPL-2.0
3 * USB hub driver.
23 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
26 #include <linux/usb/onboard_hub.h>
27 #include <linux/usb/otg.h>
28 #include <linux/usb/quirks.h>
39 #include "hub.h"
54 /* Protect struct usb_device->state and ->children members
55 * Note: Both are also protected by ->dev.sem, except that ->state can
59 /* workqueue to process hub events */
63 /* synchronize hub-port add/remove and peering operations */
73 * 10 seconds to send reply for the initial 64-byte descriptor request.
75 /* define initial 64-byte descriptor request timeout in milliseconds */
79 "initial 64-byte descriptor request timeout in milliseconds "
80 "(default 5000 - 5.0 seconds)");
83 * As of 2.6.10 we introduce a new USB device initialization scheme which
91 * hub driver's behavior. On the first initialization attempt, if the
119 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
120 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
123 static inline char *portspeed(struct usb_hub *hub, int portstatus) in portspeed() argument
125 if (hub_is_superspeedplus(hub->hdev)) in portspeed()
127 if (hub_is_superspeed(hub->hdev)) in portspeed()
140 if (!hdev || !hdev->actconfig || !hdev->maxchild) in usb_hub_to_struct_hub()
142 return usb_get_intfdata(hdev->actconfig->interface[0]); in usb_hub_to_struct_hub()
148 if (udev->quirks & USB_QUIRK_NO_LPM) in usb_device_supports_lpm()
151 /* USB 2.1 (and greater) devices indicate LPM support through in usb_device_supports_lpm()
152 * their USB 2.0 Extended Capabilities BOS descriptor. in usb_device_supports_lpm()
154 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) { in usb_device_supports_lpm()
155 if (udev->bos->ext_cap && in usb_device_supports_lpm()
157 le32_to_cpu(udev->bos->ext_cap->bmAttributes))) in usb_device_supports_lpm()
163 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM. in usb_device_supports_lpm()
167 if (!udev->bos->ss_cap) { in usb_device_supports_lpm()
168 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n"); in usb_device_supports_lpm()
172 if (udev->bos->ss_cap->bU1devExitLat == 0 && in usb_device_supports_lpm()
173 udev->bos->ss_cap->bU2DevExitLat == 0) { in usb_device_supports_lpm()
174 if (udev->parent) in usb_device_supports_lpm()
175 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n"); in usb_device_supports_lpm()
177 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n"); in usb_device_supports_lpm()
181 if (!udev->parent || udev->parent->lpm_capable) in usb_device_supports_lpm()
189 * See USB 3.1 section C.1.5.2
194 struct usb_hub *hub, in usb_set_lpm_mel() argument
204 * and the hub header decode latency. See USB 3.1 section C 2.2.1 in usb_set_lpm_mel()
207 total_mel = hub_lpm_params->mel + in usb_set_lpm_mel()
209 hub->descriptor->u.ss.bHubHdrDecLat * 100; in usb_set_lpm_mel()
213 * each link + wHubDelay for each hub. Add only for last link. in usb_set_lpm_mel()
217 total_mel += (__le16_to_cpu(hub->descriptor->u.ss.wHubDelay) + in usb_set_lpm_mel()
222 * after receiving PING. Also add 2100ns as stated in USB 3.1 C 1.5.2.4 in usb_set_lpm_mel()
228 if (!hub->hdev->parent) in usb_set_lpm_mel()
231 udev_lpm_params->mel = total_mel; in usb_set_lpm_mel()
241 struct usb_hub *hub, in usb_set_lpm_pel() argument
251 * device and the parent hub into U0. The exit latency is the bigger of in usb_set_lpm_pel()
252 * the device exit latency or the hub exit latency. in usb_set_lpm_pel()
260 * When the hub starts to receive the LFPS, there is a slight delay for in usb_set_lpm_pel()
263 * delay, plus the PEL that we calculated for this hub. in usb_set_lpm_pel()
265 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; in usb_set_lpm_pel()
268 * According to figure C-7 in the USB 3.0 spec, the PEL for this device in usb_set_lpm_pel()
272 udev_lpm_params->pel = first_link_pel; in usb_set_lpm_pel()
274 udev_lpm_params->pel = hub_pel; in usb_set_lpm_pel()
278 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
283 * - t1: device PEL
284 * - t2: time for the ERDY to make it from the device to the host.
285 * - t3: a host-specific delay to process the ERDY.
286 * - t4: time for the packet to make it from the host to the device.
300 total_sel = udev_lpm_params->pel; in usb_set_lpm_sel()
302 for (parent = udev->parent, num_hubs = 0; parent->parent; in usb_set_lpm_sel()
303 parent = parent->parent) in usb_set_lpm_sel()
305 /* t2 = 2.1us + 250ns * (num_hubs - 1) */ in usb_set_lpm_sel()
307 total_sel += 2100 + 250 * (num_hubs - 1); in usb_set_lpm_sel()
312 udev_lpm_params->sel = total_sel; in usb_set_lpm_sel()
317 struct usb_hub *hub; in usb_set_lpm_parameters() local
324 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER) in usb_set_lpm_parameters()
327 hub = usb_hub_to_struct_hub(udev->parent); in usb_set_lpm_parameters()
331 if (!hub) in usb_set_lpm_parameters()
334 udev_u1_del = udev->bos->ss_cap->bU1devExitLat; in usb_set_lpm_parameters()
335 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat); in usb_set_lpm_parameters()
336 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; in usb_set_lpm_parameters()
337 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat); in usb_set_lpm_parameters()
339 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, in usb_set_lpm_parameters()
340 hub, &udev->parent->u1_params, hub_u1_del); in usb_set_lpm_parameters()
342 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, in usb_set_lpm_parameters()
343 hub, &udev->parent->u2_params, hub_u2_del); in usb_set_lpm_parameters()
347 * when the parent hub notices the downstream port is trying to in usb_set_lpm_parameters()
348 * transition to U0 to when the hub initiates a U0 transition on its in usb_set_lpm_parameters()
352 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking in usb_set_lpm_parameters()
355 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I in usb_set_lpm_parameters()
356 * assume the device exit latencies they are talking about are the hub in usb_set_lpm_parameters()
364 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, in usb_set_lpm_parameters()
365 hub, &udev->parent->u1_params, hub_u1_del, in usb_set_lpm_parameters()
369 port_to_port_delay = 1 + hub_u2_del - hub_u1_del; in usb_set_lpm_parameters()
373 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, in usb_set_lpm_parameters()
374 hub, &udev->parent->u2_params, hub_u2_del, in usb_set_lpm_parameters()
378 usb_set_lpm_sel(udev, &udev->u1_params); in usb_set_lpm_parameters()
379 usb_set_lpm_sel(udev, &udev->u2_params); in usb_set_lpm_parameters()
382 /* USB 2.0 spec Section 11.24.4.5 */
407 size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1; in get_hub_descriptor()
409 return -EMSGSIZE; in get_hub_descriptor()
413 return -EINVAL; in get_hub_descriptor()
417 * USB 2.0 spec Section 11.24.2.1
426 * USB 2.0 spec Section 11.24.2.2
436 * USB 2.0 spec Section 11.24.2.13
462 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
465 static void set_port_led(struct usb_hub *hub, int port1, int selector) in set_port_led() argument
467 struct usb_port *port_dev = hub->ports[port1 - 1]; in set_port_led()
470 status = set_port_feature(hub->hdev, (selector << 8) | port1, in set_port_led()
472 dev_dbg(&port_dev->dev, "indicator %s status %d\n", in set_port_led()
480 struct usb_hub *hub = in led_work() local
482 struct usb_device *hdev = hub->hdev; in led_work()
485 int cursor = -1; in led_work()
487 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) in led_work()
490 for (i = 0; i < hdev->maxchild; i++) { in led_work()
493 /* 30%-50% duty cycle */ in led_work()
495 switch (hub->indicator[i]) { in led_work()
534 set_port_led(hub, i + 1, selector); in led_work()
535 hub->indicator[i] = mode; in led_work()
539 cursor %= hdev->maxchild; in led_work()
540 set_port_led(hub, cursor + 1, HUB_LED_GREEN); in led_work()
541 hub->indicator[cursor] = INDICATOR_CYCLE; in led_work()
546 &hub->leds, LED_CYCLE_PERIOD); in led_work()
549 /* use a short timeout for hub/port status fetches */
554 * USB 2.0 spec Section 11.24.2.6
559 int i, status = -ETIMEDOUT; in get_hub_status()
562 (status == -ETIMEDOUT || status == -EPIPE); i++) { in get_hub_status()
571 * USB 2.0 spec Section 11.24.2.7
572 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
577 int i, status = -ETIMEDOUT; in get_port_status()
580 (status == -ETIMEDOUT || status == -EPIPE); i++) { in get_port_status()
588 static int hub_ext_port_status(struct usb_hub *hub, int port1, int type, in hub_ext_port_status() argument
597 mutex_lock(&hub->status_mutex); in hub_ext_port_status()
598 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len); in hub_ext_port_status()
600 if (ret != -ENODEV) in hub_ext_port_status()
601 dev_err(hub->intfdev, in hub_ext_port_status()
604 ret = -EIO; in hub_ext_port_status()
606 *status = le16_to_cpu(hub->status->port.wPortStatus); in hub_ext_port_status()
607 *change = le16_to_cpu(hub->status->port.wPortChange); in hub_ext_port_status()
610 hub->status->port.dwExtPortStatus); in hub_ext_port_status()
613 mutex_unlock(&hub->status_mutex); in hub_ext_port_status()
617 int usb_hub_port_status(struct usb_hub *hub, int port1, in usb_hub_port_status() argument
620 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS, in usb_hub_port_status()
624 static void hub_resubmit_irq_urb(struct usb_hub *hub) in hub_resubmit_irq_urb() argument
629 spin_lock_irqsave(&hub->irq_urb_lock, flags); in hub_resubmit_irq_urb()
631 if (hub->quiescing) { in hub_resubmit_irq_urb()
632 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); in hub_resubmit_irq_urb()
636 status = usb_submit_urb(hub->urb, GFP_ATOMIC); in hub_resubmit_irq_urb()
637 if (status && status != -ENODEV && status != -EPERM && in hub_resubmit_irq_urb()
638 status != -ESHUTDOWN) { in hub_resubmit_irq_urb()
639 dev_err(hub->intfdev, "resubmit --> %d\n", status); in hub_resubmit_irq_urb()
640 mod_timer(&hub->irq_urb_retry, jiffies + HZ); in hub_resubmit_irq_urb()
643 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); in hub_resubmit_irq_urb()
648 struct usb_hub *hub = from_timer(hub, t, irq_urb_retry); in hub_retry_irq_urb() local
650 hub_resubmit_irq_urb(hub); in hub_retry_irq_urb()
654 static void kick_hub_wq(struct usb_hub *hub) in kick_hub_wq() argument
658 if (hub->disconnected || work_pending(&hub->events)) in kick_hub_wq()
666 * work for this hub. Therefore put the interface either when in kick_hub_wq()
669 intf = to_usb_interface(hub->intfdev); in kick_hub_wq()
671 kref_get(&hub->kref); in kick_hub_wq()
673 if (queue_work(hub_wq, &hub->events)) in kick_hub_wq()
678 kref_put(&hub->kref, hub_release); in kick_hub_wq()
683 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_kick_hub_wq() local
685 if (hub) in usb_kick_hub_wq()
686 kick_hub_wq(hub); in usb_kick_hub_wq()
690 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
693 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
694 * device initiates resume, so the USB core will not receive notice of the
695 * resume through the normal hub interrupt URB.
700 struct usb_hub *hub; in usb_wakeup_notification() local
706 hub = usb_hub_to_struct_hub(hdev); in usb_wakeup_notification()
707 if (hub) { in usb_wakeup_notification()
708 port_dev = hub->ports[portnum - 1]; in usb_wakeup_notification()
709 if (port_dev && port_dev->child) in usb_wakeup_notification()
710 pm_wakeup_event(&port_dev->child->dev, 0); in usb_wakeup_notification()
712 set_bit(portnum, hub->wakeup_bits); in usb_wakeup_notification()
713 kick_hub_wq(hub); in usb_wakeup_notification()
721 struct usb_hub *hub = urb->context; in hub_irq() local
722 int status = urb->status; in hub_irq()
727 case -ENOENT: /* synchronous unlink */ in hub_irq()
728 case -ECONNRESET: /* async unlink */ in hub_irq()
729 case -ESHUTDOWN: /* hardware going away */ in hub_irq()
733 /* Cause a hub reset after 10 consecutive errors */ in hub_irq()
734 dev_dbg(hub->intfdev, "transfer --> %d\n", status); in hub_irq()
735 if ((++hub->nerrors < 10) || hub->error) in hub_irq()
737 hub->error = status; in hub_irq()
743 for (i = 0; i < urb->actual_length; ++i) in hub_irq()
744 bits |= ((unsigned long) ((*hub->buffer)[i])) in hub_irq()
746 hub->event_bits[0] = bits; in hub_irq()
750 hub->nerrors = 0; in hub_irq()
753 kick_hub_wq(hub); in hub_irq()
756 hub_resubmit_irq_urb(hub); in hub_irq()
759 /* USB 2.0 spec Section 11.24.2.3 */
781 * both can talk to the same hub concurrently.
785 struct usb_hub *hub = in hub_tt_work() local
789 spin_lock_irqsave(&hub->tt.lock, flags); in hub_tt_work()
790 while (!list_empty(&hub->tt.clear_list)) { in hub_tt_work()
793 struct usb_device *hdev = hub->hdev; in hub_tt_work()
797 next = hub->tt.clear_list.next; in hub_tt_work()
799 list_del(&clear->clear_list); in hub_tt_work()
802 spin_unlock_irqrestore(&hub->tt.lock, flags); in hub_tt_work()
803 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt); in hub_tt_work()
804 if (status && status != -ENODEV) in hub_tt_work()
805 dev_err(&hdev->dev, in hub_tt_work()
807 clear->tt, clear->devinfo, status); in hub_tt_work()
810 drv = clear->hcd->driver; in hub_tt_work()
811 if (drv->clear_tt_buffer_complete) in hub_tt_work()
812 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); in hub_tt_work()
815 spin_lock_irqsave(&hub->tt.lock, flags); in hub_tt_work()
817 spin_unlock_irqrestore(&hub->tt.lock, flags); in hub_tt_work()
821 * usb_hub_set_port_power - control hub port's power state
822 * @hdev: USB device belonging to the usb hub
823 * @hub: target hub
832 int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, in usb_hub_set_port_power() argument
846 set_bit(port1, hub->power_bits); in usb_hub_set_port_power()
848 clear_bit(port1, hub->power_bits); in usb_hub_set_port_power()
853 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
856 * High speed HCDs use this to tell the hub driver that some split control or
861 * It may not be possible for that hub to handle additional full (or low)
868 struct usb_device *udev = urb->dev; in usb_hub_clear_tt_buffer()
869 int pipe = urb->pipe; in usb_hub_clear_tt_buffer()
870 struct usb_tt *tt = udev->tt; in usb_hub_clear_tt_buffer()
876 * there can be many TTs per hub). even if they're uncommon. in usb_hub_clear_tt_buffer()
880 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); in usb_hub_clear_tt_buffer()
882 return -ENOMEM; in usb_hub_clear_tt_buffer()
886 clear->tt = tt->multi ? udev->ttport : 1; in usb_hub_clear_tt_buffer()
887 clear->devinfo = usb_pipeendpoint (pipe); in usb_hub_clear_tt_buffer()
888 clear->devinfo |= ((u16)udev->devaddr) << 4; in usb_hub_clear_tt_buffer()
889 clear->devinfo |= usb_pipecontrol(pipe) in usb_hub_clear_tt_buffer()
893 clear->devinfo |= 1 << 15; in usb_hub_clear_tt_buffer()
896 clear->hcd = bus_to_hcd(udev->bus); in usb_hub_clear_tt_buffer()
897 clear->ep = urb->ep; in usb_hub_clear_tt_buffer()
900 spin_lock_irqsave(&tt->lock, flags); in usb_hub_clear_tt_buffer()
901 list_add_tail(&clear->clear_list, &tt->clear_list); in usb_hub_clear_tt_buffer()
902 schedule_work(&tt->clear_work); in usb_hub_clear_tt_buffer()
903 spin_unlock_irqrestore(&tt->lock, flags); in usb_hub_clear_tt_buffer()
908 static void hub_power_on(struct usb_hub *hub, bool do_delay) in hub_power_on() argument
914 * USB 2.0 hubs. Some hubs do not implement port-power switching in hub_power_on()
916 * unless we send these messages to the hub. in hub_power_on()
918 if (hub_is_port_power_switchable(hub)) in hub_power_on()
919 dev_dbg(hub->intfdev, "enabling power on all ports\n"); in hub_power_on()
921 dev_dbg(hub->intfdev, "trying to enable port power on " in hub_power_on()
922 "non-switchable hub\n"); in hub_power_on()
923 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) in hub_power_on()
924 if (test_bit(port1, hub->power_bits)) in hub_power_on()
925 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); in hub_power_on()
927 usb_clear_port_feature(hub->hdev, port1, in hub_power_on()
930 msleep(hub_power_on_good_delay(hub)); in hub_power_on()
933 static int hub_hub_status(struct usb_hub *hub, in hub_hub_status() argument
938 mutex_lock(&hub->status_mutex); in hub_hub_status()
939 ret = get_hub_status(hub->hdev, &hub->status->hub); in hub_hub_status()
941 if (ret != -ENODEV) in hub_hub_status()
942 dev_err(hub->intfdev, in hub_hub_status()
945 *status = le16_to_cpu(hub->status->hub.wHubStatus); in hub_hub_status()
946 *change = le16_to_cpu(hub->status->hub.wHubChange); in hub_hub_status()
949 mutex_unlock(&hub->status_mutex); in hub_hub_status()
953 static int hub_set_port_link_state(struct usb_hub *hub, int port1, in hub_set_port_link_state() argument
956 return set_port_feature(hub->hdev, in hub_set_port_link_state()
962 * Disable a port and mark a logical connect-change event, so that some
964 * and will re-enumerate if there actually is a device attached.
966 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) in hub_port_logical_disconnect() argument
968 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n"); in hub_port_logical_disconnect()
969 hub_port_disable(hub, port1, 1); in hub_port_logical_disconnect()
972 * - some devices won't enumerate without a VBUS power cycle in hub_port_logical_disconnect()
973 * - SRP saves power that way in hub_port_logical_disconnect()
974 * - ... new call, TBD ... in hub_port_logical_disconnect()
975 * That's easy if this hub can switch power per-port, and in hub_port_logical_disconnect()
980 set_bit(port1, hub->change_bits); in hub_port_logical_disconnect()
981 kick_hub_wq(hub); in hub_port_logical_disconnect()
985 * usb_remove_device - disable a device's port on its parent hub
998 struct usb_hub *hub; in usb_remove_device() local
1002 if (!udev->parent) /* Can't remove a root hub */ in usb_remove_device()
1003 return -EINVAL; in usb_remove_device()
1004 hub = usb_hub_to_struct_hub(udev->parent); in usb_remove_device()
1005 intf = to_usb_interface(hub->intfdev); in usb_remove_device()
1011 set_bit(udev->portnum, hub->removed_bits); in usb_remove_device()
1012 hub_port_logical_disconnect(hub, udev->portnum); in usb_remove_device()
1025 static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) in hub_activate() argument
1027 struct usb_device *hdev = hub->hdev; in hub_activate()
1037 device_lock(&hdev->dev); in hub_activate()
1039 /* Was the hub disconnected while we were waiting? */ in hub_activate()
1040 if (hub->disconnected) in hub_activate()
1046 kref_get(&hub->kref); in hub_activate()
1048 /* The superspeed hub except for root hub has to use Hub Depth in hub_activate()
1050 * it uses to determine the downstream port number. So hub driver in hub_activate()
1051 * should send a set hub depth request to superspeed hub after in hub_activate()
1052 * the superspeed hub is set configuration in initialization or in hub_activate()
1059 if (hdev->parent && hub_is_superspeed(hdev)) { in hub_activate()
1062 hdev->level - 1, 0, NULL, 0, in hub_activate()
1065 dev_err(hub->intfdev, in hub_activate()
1066 "set hub depth failed\n"); in hub_activate()
1070 * hub's initial power-up delays. This is pretty awkward in hub_activate()
1071 * and the implementation looks like a home-brewed sort of in hub_activate()
1073 * root hub (assuming usbcore is compiled into the kernel in hub_activate()
1082 delay = hub_power_on_good_delay(hub); in hub_activate()
1084 hub_power_on(hub, false); in hub_activate()
1085 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); in hub_activate()
1087 &hub->init_work, in hub_activate()
1092 to_usb_interface(hub->intfdev)); in hub_activate()
1095 /* The internal host controller state for the hub device in hub_activate()
1097 * Update the device's info so the HW knows it's a hub. in hub_activate()
1099 hcd = bus_to_hcd(hdev->bus); in hub_activate()
1100 if (hcd->driver->update_hub_device) { in hub_activate()
1101 ret = hcd->driver->update_hub_device(hcd, hdev, in hub_activate()
1102 &hub->tt, GFP_NOIO); in hub_activate()
1104 dev_err(hub->intfdev, in hub_activate()
1105 "Host not accepting hub info update\n"); in hub_activate()
1106 dev_err(hub->intfdev, in hub_activate()
1107 "LS/FS devices and hubs may not work under this hub\n"); in hub_activate()
1110 hub_power_on(hub, true); in hub_activate()
1112 hub_power_on(hub, true); in hub_activate()
1115 } else if (hub_is_superspeed(hub->hdev)) in hub_activate()
1121 * Check each port and set hub->change_bits to let hub_wq know in hub_activate()
1124 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { in hub_activate()
1125 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_activate()
1126 struct usb_device *udev = port_dev->child; in hub_activate()
1130 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in hub_activate()
1135 dev_dbg(&port_dev->dev, "status %04x change %04x\n", in hub_activate()
1148 udev->state == USB_STATE_NOTATTACHED)) { in hub_activate()
1161 /* Make sure a warm-reset request is handled by port_event */ in hub_activate()
1163 hub_port_warm_reset_required(hub, port1, portstatus)) in hub_activate()
1164 set_bit(port1, hub->event_bits); in hub_activate()
1176 /* Clear status-change flags; we'll debounce later */ in hub_activate()
1179 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1184 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1189 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1193 hub_is_superspeed(hub->hdev)) { in hub_activate()
1195 usb_clear_port_feature(hub->hdev, port1, in hub_activate()
1203 clear_bit(port1, hub->removed_bits); in hub_activate()
1205 if (!udev || udev->state == USB_STATE_NOTATTACHED) { in hub_activate()
1216 set_bit(port1, hub->change_bits); in hub_activate()
1226 * for USB 3.0 hubs, since they don't have a suspend in hub_activate()
1228 * bit on device-initiated resume. in hub_activate()
1230 if (portchange || (hub_is_superspeed(hub->hdev) && in hub_activate()
1232 set_bit(port1, hub->event_bits); in hub_activate()
1234 } else if (udev->persist_enabled) { in hub_activate()
1236 udev->reset_resume = 1; in hub_activate()
1241 if (test_bit(port1, hub->power_bits)) in hub_activate()
1242 set_bit(port1, hub->change_bits); in hub_activate()
1247 set_bit(port1, hub->change_bits); in hub_activate()
1251 /* If no port-status-change flags were set, we don't need any in hub_activate()
1256 * If any port-status changes do occur during this delay, hub_wq in hub_activate()
1264 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3); in hub_activate()
1266 &hub->init_work, in hub_activate()
1268 device_unlock(&hdev->dev); in hub_activate()
1275 hub->quiescing = 0; in hub_activate()
1277 status = usb_submit_urb(hub->urb, GFP_NOIO); in hub_activate()
1279 dev_err(hub->intfdev, "activate --> %d\n", status); in hub_activate()
1280 if (hub->has_indicators && blinkenlights) in hub_activate()
1282 &hub->leds, LED_CYCLE_PERIOD); in hub_activate()
1285 kick_hub_wq(hub); in hub_activate()
1290 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); in hub_activate()
1291 device_unlock(&hdev->dev); in hub_activate()
1294 kref_put(&hub->kref, hub_release); in hub_activate()
1300 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); in hub_init_func2() local
1302 hub_activate(hub, HUB_INIT2); in hub_init_func2()
1307 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); in hub_init_func3() local
1309 hub_activate(hub, HUB_INIT3); in hub_init_func3()
1316 static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) in hub_quiesce() argument
1318 struct usb_device *hdev = hub->hdev; in hub_quiesce()
1322 /* hub_wq and related activity won't re-trigger */ in hub_quiesce()
1323 spin_lock_irqsave(&hub->irq_urb_lock, flags); in hub_quiesce()
1324 hub->quiescing = 1; in hub_quiesce()
1325 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); in hub_quiesce()
1329 for (i = 0; i < hdev->maxchild; ++i) { in hub_quiesce()
1330 if (hub->ports[i]->child) in hub_quiesce()
1331 usb_disconnect(&hub->ports[i]->child); in hub_quiesce()
1336 del_timer_sync(&hub->irq_urb_retry); in hub_quiesce()
1337 usb_kill_urb(hub->urb); in hub_quiesce()
1338 if (hub->has_indicators) in hub_quiesce()
1339 cancel_delayed_work_sync(&hub->leds); in hub_quiesce()
1340 if (hub->tt.hub) in hub_quiesce()
1341 flush_work(&hub->tt.clear_work); in hub_quiesce()
1344 static void hub_pm_barrier_for_all_ports(struct usb_hub *hub) in hub_pm_barrier_for_all_ports() argument
1348 for (i = 0; i < hub->hdev->maxchild; ++i) in hub_pm_barrier_for_all_ports()
1349 pm_runtime_barrier(&hub->ports[i]->dev); in hub_pm_barrier_for_all_ports()
1352 /* caller has locked the hub device */
1355 struct usb_hub *hub = usb_get_intfdata(intf); in hub_pre_reset() local
1357 hub_quiesce(hub, HUB_PRE_RESET); in hub_pre_reset()
1358 hub->in_reset = 1; in hub_pre_reset()
1359 hub_pm_barrier_for_all_ports(hub); in hub_pre_reset()
1363 /* caller has locked the hub device */
1366 struct usb_hub *hub = usb_get_intfdata(intf); in hub_post_reset() local
1368 hub->in_reset = 0; in hub_post_reset()
1369 hub_pm_barrier_for_all_ports(hub); in hub_post_reset()
1370 hub_activate(hub, HUB_POST_RESET); in hub_post_reset()
1374 static int hub_configure(struct usb_hub *hub, in hub_configure() argument
1378 struct usb_device *hdev = hub->hdev; in hub_configure()
1379 struct device *hub_dev = hub->intfdev; in hub_configure()
1389 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); in hub_configure()
1390 if (!hub->buffer) { in hub_configure()
1391 ret = -ENOMEM; in hub_configure()
1395 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); in hub_configure()
1396 if (!hub->status) { in hub_configure()
1397 ret = -ENOMEM; in hub_configure()
1400 mutex_init(&hub->status_mutex); in hub_configure()
1402 hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL); in hub_configure()
1403 if (!hub->descriptor) { in hub_configure()
1404 ret = -ENOMEM; in hub_configure()
1408 /* Request the entire hub descriptor. in hub_configure()
1409 * hub->descriptor can handle USB_MAXCHILDREN ports, in hub_configure()
1410 * but a (non-SS) hub can/will return fewer bytes here. in hub_configure()
1412 ret = get_hub_descriptor(hdev, hub->descriptor); in hub_configure()
1414 message = "can't read hub descriptor"; in hub_configure()
1422 if (hub->descriptor->bNbrPorts > maxchild) { in hub_configure()
1423 message = "hub has too many ports!"; in hub_configure()
1424 ret = -ENODEV; in hub_configure()
1426 } else if (hub->descriptor->bNbrPorts == 0) { in hub_configure()
1427 message = "hub doesn't have any ports!"; in hub_configure()
1428 ret = -ENODEV; in hub_configure()
1433 * Accumulate wHubDelay + 40ns for every hub in the tree of devices. in hub_configure()
1437 u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay); in hub_configure()
1439 if (hdev->parent) in hub_configure()
1440 delay += hdev->parent->hub_delay; in hub_configure()
1443 hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX); in hub_configure()
1446 maxchild = hub->descriptor->bNbrPorts; in hub_configure()
1450 hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL); in hub_configure()
1451 if (!hub->ports) { in hub_configure()
1452 ret = -ENOMEM; in hub_configure()
1456 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); in hub_configure()
1465 /* FIXME for USB 3.0, skip for now */ in hub_configure()
1471 portstr[i] = hub->descriptor->u.hs.DeviceRemovable in hub_configure()
1477 dev_dbg(hub_dev, "standalone hub\n"); in hub_configure()
1488 dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); in hub_configure()
1494 dev_dbg(hub_dev, "global over-current protection\n"); in hub_configure()
1497 dev_dbg(hub_dev, "individual port over-current protection\n"); in hub_configure()
1501 dev_dbg(hub_dev, "no over-current protection\n"); in hub_configure()
1505 spin_lock_init(&hub->tt.lock); in hub_configure()
1506 INIT_LIST_HEAD(&hub->tt.clear_list); in hub_configure()
1507 INIT_WORK(&hub->tt.clear_work, hub_tt_work); in hub_configure()
1508 switch (hdev->descriptor.bDeviceProtocol) { in hub_configure()
1513 hub->tt.hub = hdev; in hub_configure()
1519 hub->tt.multi = 1; in hub_configure()
1523 hub->tt.hub = hdev; in hub_configure()
1526 /* USB 3.0 hubs don't have a TT */ in hub_configure()
1529 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", in hub_configure()
1530 hdev->descriptor.bDeviceProtocol); in hub_configure()
1537 if (hdev->descriptor.bDeviceProtocol != 0) { in hub_configure()
1538 hub->tt.think_time = 666; in hub_configure()
1541 8, hub->tt.think_time); in hub_configure()
1545 hub->tt.think_time = 666 * 2; in hub_configure()
1548 16, hub->tt.think_time); in hub_configure()
1551 hub->tt.think_time = 666 * 3; in hub_configure()
1554 24, hub->tt.think_time); in hub_configure()
1557 hub->tt.think_time = 666 * 4; in hub_configure()
1560 32, hub->tt.think_time); in hub_configure()
1564 /* probe() zeroes hub->indicator[] */ in hub_configure()
1566 hub->has_indicators = 1; in hub_configure()
1571 hub->descriptor->bPwrOn2PwrGood * 2); in hub_configure()
1573 /* power budgeting mostly matters with bus-powered hubs, in hub_configure()
1574 * and battery-powered root hubs (may provide just 8 mA). in hub_configure()
1578 message = "can't get hub status"; in hub_configure()
1581 hcd = bus_to_hcd(hdev->bus); in hub_configure()
1582 if (hdev == hdev->bus->root_hub) { in hub_configure()
1583 if (hcd->power_budget > 0) in hub_configure()
1584 hdev->bus_mA = hcd->power_budget; in hub_configure()
1586 hdev->bus_mA = full_load * maxchild; in hub_configure()
1587 if (hdev->bus_mA >= full_load) in hub_configure()
1588 hub->mA_per_port = full_load; in hub_configure()
1590 hub->mA_per_port = hdev->bus_mA; in hub_configure()
1591 hub->limited_power = 1; in hub_configure()
1594 int remaining = hdev->bus_mA - in hub_configure()
1595 hub->descriptor->bHubContrCurrent; in hub_configure()
1597 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", in hub_configure()
1598 hub->descriptor->bHubContrCurrent); in hub_configure()
1599 hub->limited_power = 1; in hub_configure()
1605 hub->mA_per_port = unit_load; /* 7.2.1 */ in hub_configure()
1607 } else { /* Self-powered external hub */ in hub_configure()
1608 /* FIXME: What about battery-powered external hubs that in hub_configure()
1610 hub->mA_per_port = full_load; in hub_configure()
1612 if (hub->mA_per_port < full_load) in hub_configure()
1614 hub->mA_per_port); in hub_configure()
1616 ret = hub_hub_status(hub, &hubstatus, &hubchange); in hub_configure()
1618 message = "can't get hub status"; in hub_configure()
1623 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) in hub_configure()
1629 dev_dbg(hub_dev, "%sover-current condition exists\n", in hub_configure()
1638 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); in hub_configure()
1641 if (maxp > sizeof(*hub->buffer)) in hub_configure()
1642 maxp = sizeof(*hub->buffer); in hub_configure()
1644 hub->urb = usb_alloc_urb(0, GFP_KERNEL); in hub_configure()
1645 if (!hub->urb) { in hub_configure()
1646 ret = -ENOMEM; in hub_configure()
1650 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, in hub_configure()
1651 hub, endpoint->bInterval); in hub_configure()
1653 /* maybe cycle the hub leds */ in hub_configure()
1654 if (hub->has_indicators && blinkenlights) in hub_configure()
1655 hub->indicator[0] = INDICATOR_CYCLE; in hub_configure()
1659 ret = usb_hub_create_port_device(hub, i + 1); in hub_configure()
1661 dev_err(hub->intfdev, in hub_configure()
1666 hdev->maxchild = i; in hub_configure()
1667 for (i = 0; i < hdev->maxchild; i++) { in hub_configure()
1668 struct usb_port *port_dev = hub->ports[i]; in hub_configure()
1670 pm_runtime_put(&port_dev->dev); in hub_configure()
1677 /* Update the HCD's internal representation of this hub before hub_wq in hub_configure()
1678 * starts getting port status changes for devices under the hub. in hub_configure()
1680 if (hcd->driver->update_hub_device) { in hub_configure()
1681 ret = hcd->driver->update_hub_device(hcd, hdev, in hub_configure()
1682 &hub->tt, GFP_KERNEL); in hub_configure()
1684 message = "can't update HCD hub info"; in hub_configure()
1689 usb_hub_adjust_deviceremovable(hdev, hub->descriptor); in hub_configure()
1691 hub_activate(hub, HUB_INIT); in hub_configure()
1703 struct usb_hub *hub = container_of(kref, struct usb_hub, kref); in hub_release() local
1705 usb_put_dev(hub->hdev); in hub_release()
1706 usb_put_intf(to_usb_interface(hub->intfdev)); in hub_release()
1707 kfree(hub); in hub_release()
1714 struct usb_hub *hub = usb_get_intfdata(intf); in hub_disconnect() local
1719 * Stop adding new hub events. We do not want to block here and thus in hub_disconnect()
1722 hub->disconnected = 1; in hub_disconnect()
1724 /* Disconnect all children and quiesce the hub */ in hub_disconnect()
1725 hub->error = 0; in hub_disconnect()
1726 hub_quiesce(hub, HUB_DISCONNECT); in hub_disconnect()
1732 port1 = hdev->maxchild; in hub_disconnect()
1733 hdev->maxchild = 0; in hub_disconnect()
1737 for (; port1 > 0; --port1) in hub_disconnect()
1738 usb_hub_remove_port_device(hub, port1); in hub_disconnect()
1742 if (hub->hdev->speed == USB_SPEED_HIGH) in hub_disconnect()
1743 highspeed_hubs--; in hub_disconnect()
1745 usb_free_urb(hub->urb); in hub_disconnect()
1746 kfree(hub->ports); in hub_disconnect()
1747 kfree(hub->descriptor); in hub_disconnect()
1748 kfree(hub->status); in hub_disconnect()
1749 kfree(hub->buffer); in hub_disconnect()
1751 pm_suspend_ignore_children(&intf->dev, false); in hub_disconnect()
1753 if (hub->quirk_disable_autosuspend) in hub_disconnect()
1756 onboard_hub_destroy_pdevs(&hub->onboard_hub_devs); in hub_disconnect()
1758 kref_put(&hub->kref, hub_release); in hub_disconnect()
1765 if (desc->desc.bInterfaceSubClass != 0 && in hub_descriptor_is_sane()
1766 desc->desc.bInterfaceSubClass != 1) in hub_descriptor_is_sane()
1769 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ in hub_descriptor_is_sane()
1770 if (desc->desc.bNumEndpoints != 1) in hub_descriptor_is_sane()
1774 if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc)) in hub_descriptor_is_sane()
1784 struct usb_hub *hub; in hub_probe() local
1786 desc = intf->cur_altsetting; in hub_probe()
1793 * - Unlike other drivers, the hub driver does not rely on the in hub_probe()
1796 * change on hub downstream ports, so it is safe to do it. in hub_probe()
1798 * - The patch might cause one or more auto supend/resume for in hub_probe()
1799 * below very rare devices when they are plugged into hub in hub_probe()
1810 * autosuspend delay of their parent hub in the probe() to one in hub_probe()
1814 * - The patch may cause one or more auto suspend/resume on in hub_probe()
1815 * hub during running 'lsusb', but it is probably too in hub_probe()
1818 * - Change autosuspend delay of hub can avoid unnecessary auto in hub_probe()
1819 * suspend timer for hub, also may decrease power consumption in hub_probe()
1820 * of USB bus. in hub_probe()
1822 * - If user has indicated to prevent autosuspend by passing in hub_probe()
1823 * usbcore.autosuspend = -1 then keep autosuspend disabled. in hub_probe()
1826 if (hdev->dev.power.autosuspend_delay >= 0) in hub_probe()
1827 pm_runtime_set_autosuspend_delay(&hdev->dev, 0); in hub_probe()
1835 if (hdev->parent) { /* normal device */ in hub_probe()
1837 } else { /* root hub */ in hub_probe()
1838 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; in hub_probe()
1840 if (drv->bus_suspend && drv->bus_resume) in hub_probe()
1844 if (hdev->level == MAX_TOPO_LEVEL) { in hub_probe()
1845 dev_err(&intf->dev, in hub_probe()
1846 "Unsupported bus topology: hub nested too deep\n"); in hub_probe()
1847 return -E2BIG; in hub_probe()
1851 if (hdev->parent) { in hub_probe()
1852 dev_warn(&intf->dev, "ignoring external hub\n"); in hub_probe()
1853 return -ENODEV; in hub_probe()
1858 dev_err(&intf->dev, "bad descriptor, ignoring hub\n"); in hub_probe()
1859 return -EIO; in hub_probe()
1862 /* We found a hub */ in hub_probe()
1863 dev_info(&intf->dev, "USB hub found\n"); in hub_probe()
1865 hub = kzalloc(sizeof(*hub), GFP_KERNEL); in hub_probe()
1866 if (!hub) in hub_probe()
1867 return -ENOMEM; in hub_probe()
1869 kref_init(&hub->kref); in hub_probe()
1870 hub->intfdev = &intf->dev; in hub_probe()
1871 hub->hdev = hdev; in hub_probe()
1872 INIT_DELAYED_WORK(&hub->leds, led_work); in hub_probe()
1873 INIT_DELAYED_WORK(&hub->init_work, NULL); in hub_probe()
1874 INIT_WORK(&hub->events, hub_event); in hub_probe()
1875 INIT_LIST_HEAD(&hub->onboard_hub_devs); in hub_probe()
1876 spin_lock_init(&hub->irq_urb_lock); in hub_probe()
1877 timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0); in hub_probe()
1881 usb_set_intfdata(intf, hub); in hub_probe()
1882 intf->needs_remote_wakeup = 1; in hub_probe()
1883 pm_suspend_ignore_children(&intf->dev, true); in hub_probe()
1885 if (hdev->speed == USB_SPEED_HIGH) in hub_probe()
1888 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) in hub_probe()
1889 hub->quirk_check_port_auto_suspend = 1; in hub_probe()
1891 if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) { in hub_probe()
1892 hub->quirk_disable_autosuspend = 1; in hub_probe()
1896 if (hub_configure(hub, &desc->endpoint[0].desc) >= 0) { in hub_probe()
1897 onboard_hub_create_pdevs(hdev, &hub->onboard_hub_devs); in hub_probe()
1903 return -ENODEV; in hub_probe()
1910 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in hub_ioctl() local
1912 /* assert ifno == 0 (part of hub spec) */ in hub_ioctl()
1919 if (hdev->devnum <= 0) in hub_ioctl()
1920 info->nports = 0; in hub_ioctl()
1922 info->nports = hdev->maxchild; in hub_ioctl()
1923 for (i = 0; i < info->nports; i++) { in hub_ioctl()
1924 if (hub->ports[i]->child == NULL) in hub_ioctl()
1925 info->port[i] = 0; in hub_ioctl()
1927 info->port[i] = in hub_ioctl()
1928 hub->ports[i]->child->devnum; in hub_ioctl()
1933 return info->nports + 1; in hub_ioctl()
1937 return -ENOSYS; in hub_ioctl()
1942 * Allow user programs to claim ports on a hub. When a device is attached
1948 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in find_port_owner() local
1950 if (hdev->state == USB_STATE_NOTATTACHED) in find_port_owner()
1951 return -ENODEV; in find_port_owner()
1952 if (port1 == 0 || port1 > hdev->maxchild) in find_port_owner()
1953 return -EINVAL; in find_port_owner()
1955 /* Devices not managed by the hub driver in find_port_owner()
1958 *ppowner = &(hub->ports[port1 - 1]->port_owner); in find_port_owner()
1973 return -EBUSY; in usb_hub_claim_port()
1989 return -ENOENT; in usb_hub_release_port()
1997 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_hub_release_all_ports() local
2000 for (n = 0; n < hdev->maxchild; n++) { in usb_hub_release_all_ports()
2001 if (hub->ports[n]->port_owner == owner) in usb_hub_release_all_ports()
2002 hub->ports[n]->port_owner = NULL; in usb_hub_release_all_ports()
2010 struct usb_hub *hub; in usb_device_is_owned() local
2012 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) in usb_device_is_owned()
2014 hub = usb_hub_to_struct_hub(udev->parent); in usb_device_is_owned()
2015 return !!hub->ports[udev->portnum - 1]->port_owner; in usb_device_is_owned()
2020 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in recursively_mark_NOTATTACHED() local
2023 for (i = 0; i < udev->maxchild; ++i) { in recursively_mark_NOTATTACHED()
2024 if (hub->ports[i]->child) in recursively_mark_NOTATTACHED()
2025 recursively_mark_NOTATTACHED(hub->ports[i]->child); in recursively_mark_NOTATTACHED()
2027 if (udev->state == USB_STATE_SUSPENDED) in recursively_mark_NOTATTACHED()
2028 udev->active_duration -= jiffies; in recursively_mark_NOTATTACHED()
2029 udev->state = USB_STATE_NOTATTACHED; in recursively_mark_NOTATTACHED()
2033 * usb_set_device_state - change a device's current state (usbcore, hcds)
2037 * udev->state is _not_ fully protected by the device lock. Although
2048 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
2049 * Otherwise udev->state is set to new_state, and if new_state is
2057 int wakeup = -1; in usb_set_device_state()
2060 if (udev->state == USB_STATE_NOTATTACHED) in usb_set_device_state()
2064 /* root hub wakeup capabilities are managed out-of-band in usb_set_device_state()
2067 if (udev->parent) { in usb_set_device_state()
2068 if (udev->state == USB_STATE_SUSPENDED in usb_set_device_state()
2072 wakeup = (udev->quirks & in usb_set_device_state()
2074 udev->actconfig->desc.bmAttributes & in usb_set_device_state()
2079 if (udev->state == USB_STATE_SUSPENDED && in usb_set_device_state()
2081 udev->active_duration -= jiffies; in usb_set_device_state()
2083 udev->state != USB_STATE_SUSPENDED) in usb_set_device_state()
2084 udev->active_duration += jiffies; in usb_set_device_state()
2085 udev->state = new_state; in usb_set_device_state()
2090 device_set_wakeup_capable(&udev->dev, wakeup); in usb_set_device_state()
2097 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2098 * USB-2.0 buses they are also used as device addresses, however on
2099 * USB-3.0 buses the address is assigned by the controller hardware
2103 * device <-> virtual port number becomes 1:1. Why? to simplify the
2105 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2108 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2112 * We add 1 as an offset to the one-based USB-stack port number
2113 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2114 * 0 is reserved by USB for default address; (b) Linux's USB stack
2115 * uses always #1 for the root hub of the controller. So USB stack's
2116 * port #1, which is wusb virtual-port #0 has address #2.
2126 struct usb_bus *bus = udev->bus; in choose_devnum()
2128 /* be safe when more hub events are proceed in parallel */ in choose_devnum()
2129 mutex_lock(&bus->devnum_next_mutex); in choose_devnum()
2130 if (udev->wusb) { in choose_devnum()
2131 devnum = udev->portnum + 1; in choose_devnum()
2132 BUG_ON(test_bit(devnum, bus->devmap.devicemap)); in choose_devnum()
2135 * bus->devnum_next. */ in choose_devnum()
2136 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, in choose_devnum()
2137 bus->devnum_next); in choose_devnum()
2139 devnum = find_next_zero_bit(bus->devmap.devicemap, in choose_devnum()
2141 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); in choose_devnum()
2144 set_bit(devnum, bus->devmap.devicemap); in choose_devnum()
2145 udev->devnum = devnum; in choose_devnum()
2147 mutex_unlock(&bus->devnum_next_mutex); in choose_devnum()
2152 if (udev->devnum > 0) { in release_devnum()
2153 clear_bit(udev->devnum, udev->bus->devmap.devicemap); in release_devnum()
2154 udev->devnum = -1; in release_devnum()
2161 if (!udev->wusb) in update_devnum()
2162 udev->devnum = devnum; in update_devnum()
2163 if (!udev->devaddr) in update_devnum()
2164 udev->devaddr = (u8)devnum; in update_devnum()
2169 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_free_dev()
2172 if (hcd->driver->free_dev && udev->parent) in hub_free_dev()
2173 hcd->driver->free_dev(hcd, udev); in hub_free_dev()
2178 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in hub_disconnect_children() local
2182 for (i = 0; i < udev->maxchild; i++) { in hub_disconnect_children()
2183 if (hub->ports[i]->child) in hub_disconnect_children()
2184 usb_disconnect(&hub->ports[i]->child); in hub_disconnect_children()
2189 * usb_disconnect - disconnect a device (usbcore-internal)
2196 * If *pdev is a normal device then the parent hub must already be locked.
2197 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
2200 * Only hub drivers (including virtual root hub drivers for host
2209 struct usb_hub *hub = NULL; in usb_disconnect() local
2217 dev_info(&udev->dev, "USB disconnect, device number %d\n", in usb_disconnect()
2218 udev->devnum); in usb_disconnect()
2221 * Ensure that the pm runtime code knows that the USB device in usb_disconnect()
2224 pm_runtime_barrier(&udev->dev); in usb_disconnect()
2234 dev_dbg(&udev->dev, "unregistering device\n"); in usb_disconnect()
2238 if (udev->parent) { in usb_disconnect()
2239 port1 = udev->portnum; in usb_disconnect()
2240 hub = usb_hub_to_struct_hub(udev->parent); in usb_disconnect()
2241 port_dev = hub->ports[port1 - 1]; in usb_disconnect()
2243 sysfs_remove_link(&udev->dev.kobj, "port"); in usb_disconnect()
2244 sysfs_remove_link(&port_dev->dev.kobj, "device"); in usb_disconnect()
2247 * As usb_port_runtime_resume() de-references udev, make in usb_disconnect()
2250 if (!test_and_set_bit(port1, hub->child_usage_bits)) in usb_disconnect()
2251 pm_runtime_get_sync(&port_dev->dev); in usb_disconnect()
2254 usb_remove_ep_devs(&udev->ep0); in usb_disconnect()
2258 * for de-configuring the device and invoking the remove-device in usb_disconnect()
2261 device_del(&udev->dev); in usb_disconnect()
2273 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) in usb_disconnect()
2274 pm_runtime_put(&port_dev->dev); in usb_disconnect()
2278 put_device(&udev->dev); in usb_disconnect()
2286 dev_info(&udev->dev, "%s: %s\n", id, string); in show_string()
2291 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice); in announce_device()
2293 dev_info(&udev->dev, in announce_device()
2294 "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n", in announce_device()
2295 le16_to_cpu(udev->descriptor.idVendor), in announce_device()
2296 le16_to_cpu(udev->descriptor.idProduct), in announce_device()
2298 dev_info(&udev->dev, in announce_device()
2299 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", in announce_device()
2300 udev->descriptor.iManufacturer, in announce_device()
2301 udev->descriptor.iProduct, in announce_device()
2302 udev->descriptor.iSerialNumber); in announce_device()
2303 show_string(udev, "Product", udev->product); in announce_device()
2304 show_string(udev, "Manufacturer", udev->manufacturer); in announce_device()
2305 show_string(udev, "SerialNumber", udev->serial); in announce_device()
2313 * usb_enumerate_device_otg - FIXME (usbcore-internal)
2316 * Finish enumeration for On-The-Go devices
2326 * OTG-aware devices on OTG-capable root hubs may be able to use SRP, in usb_enumerate_device_otg()
2330 if (!udev->bus->is_b_host in usb_enumerate_device_otg()
2331 && udev->config in usb_enumerate_device_otg()
2332 && udev->parent == udev->bus->root_hub) { in usb_enumerate_device_otg()
2334 struct usb_bus *bus = udev->bus; in usb_enumerate_device_otg()
2335 unsigned port1 = udev->portnum; in usb_enumerate_device_otg()
2338 err = __usb_get_extra_descriptor(udev->rawdescriptors[0], in usb_enumerate_device_otg()
2339 le16_to_cpu(udev->config[0].desc.wTotalLength), in usb_enumerate_device_otg()
2341 if (err || !(desc->bmAttributes & USB_OTG_HNP)) in usb_enumerate_device_otg()
2344 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", in usb_enumerate_device_otg()
2345 (port1 == bus->otg_port) ? "" : "non-"); in usb_enumerate_device_otg()
2348 if (port1 == bus->otg_port) { in usb_enumerate_device_otg()
2349 bus->b_hnp_enable = 1; in usb_enumerate_device_otg()
2361 dev_err(&udev->dev, "can't set HNP mode: %d\n", in usb_enumerate_device_otg()
2363 bus->b_hnp_enable = 0; in usb_enumerate_device_otg()
2365 } else if (desc->bLength == sizeof in usb_enumerate_device_otg()
2375 dev_err(&udev->dev, in usb_enumerate_device_otg()
2386 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2390 * and FIXME -- all comments that apply to them apply here wrt to
2402 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_enumerate_device()
2404 if (udev->config == NULL) { in usb_enumerate_device()
2407 if (err != -ENODEV) in usb_enumerate_device()
2408 dev_err(&udev->dev, "can't read configurations, error %d\n", in usb_enumerate_device()
2415 udev->product = usb_cache_string(udev, udev->descriptor.iProduct); in usb_enumerate_device()
2416 udev->manufacturer = usb_cache_string(udev, in usb_enumerate_device()
2417 udev->descriptor.iManufacturer); in usb_enumerate_device()
2418 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); in usb_enumerate_device()
2424 if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support && in usb_enumerate_device()
2429 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable in usb_enumerate_device()
2430 || udev->bus->is_b_host)) { in usb_enumerate_device()
2433 dev_dbg(&udev->dev, "HNP fail, %d\n", err); in usb_enumerate_device()
2435 return -ENOTSUPP; in usb_enumerate_device()
2445 struct usb_device *hdev = udev->parent; in set_usb_port_removable()
2446 struct usb_hub *hub; in set_usb_port_removable() local
2447 u8 port = udev->portnum; in set_usb_port_removable()
2451 dev_set_removable(&udev->dev, DEVICE_REMOVABLE_UNKNOWN); in set_usb_port_removable()
2456 hub = usb_hub_to_struct_hub(udev->parent); in set_usb_port_removable()
2462 switch (hub->ports[udev->portnum - 1]->connect_type) { in set_usb_port_removable()
2464 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); in set_usb_port_removable()
2468 dev_set_removable(&udev->dev, DEVICE_FIXED); in set_usb_port_removable()
2475 * Otherwise, check whether the hub knows whether a port is removable in set_usb_port_removable()
2478 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); in set_usb_port_removable()
2484 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) in set_usb_port_removable()
2488 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) in set_usb_port_removable()
2493 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); in set_usb_port_removable()
2495 dev_set_removable(&udev->dev, DEVICE_FIXED); in set_usb_port_removable()
2500 * usb_new_device - perform initial device setup (usbcore-internal)
2506 * the parent hub (if udev is a normal device) or else the
2507 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
2513 * Only the hub driver or root-hub registrar should ever call this.
2524 if (udev->parent) { in usb_new_device()
2525 /* Initialize non-root-hub device wakeup to disabled; in usb_new_device()
2529 device_init_wakeup(&udev->dev, 0); in usb_new_device()
2532 /* Tell the runtime-PM framework the device is active */ in usb_new_device()
2533 pm_runtime_set_active(&udev->dev); in usb_new_device()
2534 pm_runtime_get_noresume(&udev->dev); in usb_new_device()
2535 pm_runtime_use_autosuspend(&udev->dev); in usb_new_device()
2536 pm_runtime_enable(&udev->dev); in usb_new_device()
2546 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", in usb_new_device()
2547 udev->devnum, udev->bus->busnum, in usb_new_device()
2548 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); in usb_new_device()
2549 /* export the usbdev device-node for libusb */ in usb_new_device()
2550 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, in usb_new_device()
2551 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); in usb_new_device()
2556 if (udev->serial) in usb_new_device()
2557 add_device_randomness(udev->serial, strlen(udev->serial)); in usb_new_device()
2558 if (udev->product) in usb_new_device()
2559 add_device_randomness(udev->product, strlen(udev->product)); in usb_new_device()
2560 if (udev->manufacturer) in usb_new_device()
2561 add_device_randomness(udev->manufacturer, in usb_new_device()
2562 strlen(udev->manufacturer)); in usb_new_device()
2564 device_enable_async_suspend(&udev->dev); in usb_new_device()
2566 /* check whether the hub or firmware marks this port as non-removable */ in usb_new_device()
2570 * for configuring the device and invoking the add-device in usb_new_device()
2573 err = device_add(&udev->dev); in usb_new_device()
2575 dev_err(&udev->dev, "can't device_add, error %d\n", err); in usb_new_device()
2579 /* Create link files between child device and usb port device. */ in usb_new_device()
2580 if (udev->parent) { in usb_new_device()
2581 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_new_device() local
2582 int port1 = udev->portnum; in usb_new_device()
2583 struct usb_port *port_dev = hub->ports[port1 - 1]; in usb_new_device()
2585 err = sysfs_create_link(&udev->dev.kobj, in usb_new_device()
2586 &port_dev->dev.kobj, "port"); in usb_new_device()
2590 err = sysfs_create_link(&port_dev->dev.kobj, in usb_new_device()
2591 &udev->dev.kobj, "device"); in usb_new_device()
2593 sysfs_remove_link(&udev->dev.kobj, "port"); in usb_new_device()
2597 if (!test_and_set_bit(port1, hub->child_usage_bits)) in usb_new_device()
2598 pm_runtime_get_sync(&port_dev->dev); in usb_new_device()
2601 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); in usb_new_device()
2603 pm_runtime_put_sync_autosuspend(&udev->dev); in usb_new_device()
2608 pm_runtime_disable(&udev->dev); in usb_new_device()
2609 pm_runtime_set_suspended(&udev->dev); in usb_new_device()
2615 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2616 * @usb_dev: USB device
2618 * Move the USB device to a very basic state where interfaces are disabled
2629 if (usb_dev->authorized == 0) in usb_deauthorize_device()
2632 usb_dev->authorized = 0; in usb_deauthorize_device()
2633 usb_set_configuration(usb_dev, -1); in usb_deauthorize_device()
2646 if (usb_dev->authorized == 1) in usb_authorize_device()
2651 dev_err(&usb_dev->dev, in usb_authorize_device()
2656 if (usb_dev->wusb) { in usb_authorize_device()
2657 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); in usb_authorize_device()
2659 dev_err(&usb_dev->dev, "can't re-read device descriptor for " in usb_authorize_device()
2665 usb_dev->authorized = 1; in usb_authorize_device()
2673 dev_err(&usb_dev->dev, in usb_authorize_device()
2679 dev_info(&usb_dev->dev, "authorized to connect\n"); in usb_authorize_device()
2690 * get_port_ssp_rate - Match the extended port status to SSP rate
2691 * @hdev: The hub device
2701 struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap; in get_port_ssp_rate()
2714 ssac = le32_to_cpu(ssp_cap->bmAttributes) & in get_port_ssp_rate()
2720 attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); in get_port_ssp_rate()
2764 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2765 static unsigned hub_is_wusb(struct usb_hub *hub) in hub_is_wusb() argument
2768 if (hub->hdev->parent != NULL) /* not a root hub? */ in hub_is_wusb()
2770 hcd = bus_to_hcd(hub->hdev->bus); in hub_is_wusb()
2771 return hcd->wireless; in hub_is_wusb()
2802 (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || in use_new_scheme()
2812 if (udev->speed >= USB_SPEED_SUPER) in use_new_scheme()
2825 /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
2828 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, in hub_port_warm_reset_required() argument
2833 if (!hub_is_superspeed(hub->hdev)) in hub_port_warm_reset_required()
2836 if (test_bit(port1, hub->warm_reset_bits)) in hub_port_warm_reset_required()
2844 static int hub_port_wait_reset(struct usb_hub *hub, int port1, in hub_port_wait_reset() argument
2859 if (hub_is_superspeedplus(hub->hdev)) in hub_port_wait_reset()
2860 ret = hub_ext_port_status(hub, port1, in hub_port_wait_reset()
2865 ret = usb_hub_port_status(hub, port1, &portstatus, in hub_port_wait_reset()
2874 * to re-establish a connection after the reset is complete, in hub_port_wait_reset()
2875 * so also wait for the connection to be re-established. in hub_port_wait_reset()
2885 dev_dbg(&hub->ports[port1 - 1]->dev, in hub_port_wait_reset()
2891 return -EBUSY; in hub_port_wait_reset()
2893 if (hub_port_warm_reset_required(hub, port1, portstatus)) in hub_port_wait_reset()
2894 return -ENOTCONN; in hub_port_wait_reset()
2898 return -ENOTCONN; in hub_port_wait_reset()
2901 * A USB 3.0 connection may bounce if multiple warm resets were issued, in hub_port_wait_reset()
2902 * but the device may have successfully re-connected. Ignore it. in hub_port_wait_reset()
2904 if (!hub_is_superspeed(hub->hdev) && in hub_port_wait_reset()
2906 usb_clear_port_feature(hub->hdev, port1, in hub_port_wait_reset()
2908 return -EAGAIN; in hub_port_wait_reset()
2912 return -EBUSY; in hub_port_wait_reset()
2917 if (hub_is_superspeedplus(hub->hdev)) { in hub_port_wait_reset()
2919 udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; in hub_port_wait_reset()
2920 udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1; in hub_port_wait_reset()
2921 udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus); in hub_port_wait_reset()
2923 udev->rx_lanes = 1; in hub_port_wait_reset()
2924 udev->tx_lanes = 1; in hub_port_wait_reset()
2925 udev->ssp_rate = USB_SSP_GEN_UNKNOWN; in hub_port_wait_reset()
2927 if (hub_is_wusb(hub)) in hub_port_wait_reset()
2928 udev->speed = USB_SPEED_WIRELESS; in hub_port_wait_reset()
2929 else if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN) in hub_port_wait_reset()
2930 udev->speed = USB_SPEED_SUPER_PLUS; in hub_port_wait_reset()
2931 else if (hub_is_superspeed(hub->hdev)) in hub_port_wait_reset()
2932 udev->speed = USB_SPEED_SUPER; in hub_port_wait_reset()
2934 udev->speed = USB_SPEED_HIGH; in hub_port_wait_reset()
2936 udev->speed = USB_SPEED_LOW; in hub_port_wait_reset()
2938 udev->speed = USB_SPEED_FULL; in hub_port_wait_reset()
2943 static int hub_port_reset(struct usb_hub *hub, int port1, in hub_port_reset() argument
2948 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_reset()
2951 if (!hub_is_superspeed(hub->hdev)) { in hub_port_reset()
2953 dev_err(hub->intfdev, "only USB3 hub support " in hub_port_reset()
2955 return -EINVAL; in hub_port_reset()
2966 if (usb_hub_port_status(hub, port1, &portstatus, in hub_port_reset()
2968 if (hub_port_warm_reset_required(hub, port1, in hub_port_reset()
2972 clear_bit(port1, hub->warm_reset_bits); in hub_port_reset()
2976 status = set_port_feature(hub->hdev, port1, (warm ? in hub_port_reset()
2979 if (status == -ENODEV) { in hub_port_reset()
2980 ; /* The hub is gone */ in hub_port_reset()
2982 dev_err(&port_dev->dev, in hub_port_reset()
2986 status = hub_port_wait_reset(hub, port1, udev, delay, in hub_port_reset()
2988 if (status && status != -ENOTCONN && status != -ENODEV) in hub_port_reset()
2989 dev_dbg(hub->intfdev, in hub_port_reset()
2998 if (status == 0 || status == -ENOTCONN || status == -ENODEV || in hub_port_reset()
2999 (status == -EBUSY && i == PORT_RESET_TRIES - 1)) { in hub_port_reset()
3000 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3003 if (!hub_is_superspeed(hub->hdev)) in hub_port_reset()
3006 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3008 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3012 usb_clear_port_feature(hub->hdev, port1, in hub_port_reset()
3016 * If a USB 3.0 device migrates from reset to an error in hub_port_reset()
3017 * state, re-issue the warm reset. in hub_port_reset()
3019 if (usb_hub_port_status(hub, port1, in hub_port_reset()
3023 if (!hub_port_warm_reset_required(hub, port1, in hub_port_reset()
3032 dev_dbg(&port_dev->dev, in hub_port_reset()
3038 dev_dbg(&port_dev->dev, in hub_port_reset()
3044 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); in hub_port_reset()
3048 if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM) in hub_port_reset()
3054 /* Hub needs extra delay after resetting its port. */ in hub_port_reset()
3055 if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET) in hub_port_reset()
3062 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_port_reset()
3068 if (hcd->driver->reset_device) in hub_port_reset()
3069 hcd->driver->reset_device(hcd, udev); in hub_port_reset()
3078 if (!hub_is_superspeed(hub->hdev)) in hub_port_reset()
3085 int usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus) in usb_port_is_power_on() argument
3089 if (hub_is_superspeed(hub->hdev)) { in usb_port_is_power_on()
3101 __acquires(&port_dev->status_lock) in usb_lock_port()
3103 mutex_lock(&port_dev->status_lock); in usb_lock_port()
3104 __acquire(&port_dev->status_lock); in usb_lock_port()
3108 __releases(&port_dev->status_lock) in usb_unlock_port()
3110 mutex_unlock(&port_dev->status_lock); in usb_unlock_port()
3111 __release(&port_dev->status_lock); in usb_unlock_port()
3117 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) in port_is_suspended() argument
3121 if (hub_is_superspeed(hub->hdev)) { in port_is_suspended()
3134 * is ready for a reset-resume, or should be disconnected.
3137 struct usb_hub *hub, int port1, in check_port_resume_type() argument
3140 struct usb_port *port_dev = hub->ports[port1 - 1]; in check_port_resume_type()
3145 if (status == 0 && udev->reset_resume in check_port_resume_type()
3146 && hub_port_warm_reset_required(hub, port1, portstatus)) { in check_port_resume_type()
3150 else if (status || port_is_suspended(hub, portstatus) || in check_port_resume_type()
3151 !usb_port_is_power_on(hub, portstatus)) { in check_port_resume_type()
3153 status = -ENODEV; in check_port_resume_type()
3155 if (retries--) { in check_port_resume_type()
3157 status = usb_hub_port_status(hub, port1, &portstatus, in check_port_resume_type()
3161 status = -ENODEV; in check_port_resume_type()
3165 * so try a reset-resume instead. in check_port_resume_type()
3167 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { in check_port_resume_type()
3168 if (udev->persist_enabled) in check_port_resume_type()
3169 udev->reset_resume = 1; in check_port_resume_type()
3171 status = -ENODEV; in check_port_resume_type()
3175 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", in check_port_resume_type()
3177 } else if (udev->reset_resume) { in check_port_resume_type()
3179 /* Late port handoff can set status-change bits */ in check_port_resume_type()
3181 usb_clear_port_feature(hub->hdev, port1, in check_port_resume_type()
3184 usb_clear_port_feature(hub->hdev, port1, in check_port_resume_type()
3188 * Whatever made this reset-resume necessary may have in check_port_resume_type()
3189 * turned on the port1 bit in hub->change_bits. But after in check_port_resume_type()
3190 * a successful reset-resume we want the bit to be clear; in check_port_resume_type()
3192 * following the reset-resume. in check_port_resume_type()
3194 clear_bit(port1, hub->change_bits); in check_port_resume_type()
3202 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_disable_ltm()
3205 if (!usb_device_supports_ltm(hcd->self.root_hub) || in usb_disable_ltm()
3212 if (!udev->actconfig) in usb_disable_ltm()
3224 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_enable_ltm()
3227 if (!usb_device_supports_ltm(hcd->self.root_hub) || in usb_enable_ltm()
3234 if (!udev->actconfig) in usb_enable_ltm()
3245 * usb_enable_remote_wakeup - enable remote wakeup for a device
3248 * For USB-2 devices: Set the device's remote wakeup feature.
3250 * For USB-3 devices: Assume there's only one function on the device and
3256 if (udev->speed < USB_SPEED_SUPER) in usb_enable_remote_wakeup()
3271 * usb_disable_remote_wakeup - disable remote wakeup for a device
3274 * For USB-2 devices: Clear the device's remote wakeup feature.
3276 * For USB-3 devices: Assume there's only one function on the device and
3282 if (udev->speed < USB_SPEED_SUPER) in usb_disable_remote_wakeup()
3294 /* Count of wakeup-enabled devices at or below udev */
3297 struct usb_hub *hub = usb_hub_to_struct_hub(udev); in usb_wakeup_enabled_descendants() local
3299 return udev->do_remote_wakeup + in usb_wakeup_enabled_descendants()
3300 (hub ? hub->wakeup_enabled_descendants : 0); in usb_wakeup_enabled_descendants()
3305 * usb_port_suspend - suspend a usb device's upstream port
3306 * @udev: device that's no longer in active use, not a root hub
3309 * Suspends a USB device that isn't in active use, conserving power.
3315 * This only affects the USB hardware for a device; its interfaces
3323 * also support "remote wakeup", where the device can activate the USB
3325 * some cases, this wakes the USB host.
3328 * between a pair of dual-role devices. That will change roles, such
3329 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3331 * Devices on USB hub ports have only one "suspend" state, corresponding
3335 * - suspend, resume ... when the VBUS power link stays live
3336 * - suspend, disconnect ... VBUS lost
3339 * normal re-enumeration procedures, starting with enabling VBUS power.
3340 * Other than re-initializing the hub (plug/unplug, except for root hubs),
3344 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3346 * hub is suspended). Nevertheless, we change @udev->state to
3348 * upstream port setting is stored in @udev->port_is_suspended.
3354 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_port_suspend() local
3355 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; in usb_port_suspend()
3356 int port1 = udev->portnum; in usb_port_suspend()
3363 * wake up the upstream hub (including maybe the root hub). in usb_port_suspend()
3368 if (udev->do_remote_wakeup) { in usb_port_suspend()
3371 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", in usb_port_suspend()
3383 dev_err(&udev->dev, "Failed to disable LTM before suspend\n"); in usb_port_suspend()
3384 status = -ENOMEM; in usb_port_suspend()
3390 if (hub_is_superspeed(hub->hdev)) in usb_port_suspend()
3391 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); in usb_port_suspend()
3395 * on individual USB-2 ports. The devices will automatically go in usb_port_suspend()
3396 * into suspend a few ms after the root hub stops sending packets. in usb_port_suspend()
3397 * The USB 2.0 spec calls this "global suspend". in usb_port_suspend()
3399 * However, many USB hubs have a bug: They don't relay wakeup requests in usb_port_suspend()
3405 status = set_port_feature(hub->hdev, port1, in usb_port_suspend()
3415 if (status == -ETIMEDOUT) { in usb_port_suspend()
3420 ret = usb_hub_port_status(hub, port1, &portstatus, in usb_port_suspend()
3423 dev_dbg(&port_dev->dev, in usb_port_suspend()
3426 if (ret == 0 && port_is_suspended(hub, portstatus)) { in usb_port_suspend()
3432 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); in usb_port_suspend()
3440 if (udev->do_remote_wakeup) in usb_port_suspend()
3449 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", in usb_port_suspend()
3450 (PMSG_IS_AUTO(msg) ? "auto-" : ""), in usb_port_suspend()
3451 udev->do_remote_wakeup); in usb_port_suspend()
3453 udev->port_is_suspended = 1; in usb_port_suspend()
3461 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled in usb_port_suspend()
3462 && test_and_clear_bit(port1, hub->child_usage_bits)) in usb_port_suspend()
3463 pm_runtime_put_sync(&port_dev->dev); in usb_port_suspend()
3465 usb_mark_last_busy(hub->hdev); in usb_port_suspend()
3472 * If the USB "suspend" state is in use (rather than "global suspend"),
3479 * If @udev->reset_resume is set then the device is reset before the
3488 dev_dbg(&udev->dev, "%s\n", in finish_port_resume()
3489 udev->reset_resume ? "finish reset-resume" : "finish resume"); in finish_port_resume()
3491 /* usb ch9 identifies four variants of SUSPENDED, based on what in finish_port_resume()
3496 usb_set_device_state(udev, udev->actconfig in finish_port_resume()
3505 if (udev->reset_resume) { in finish_port_resume()
3508 * we don't want to perform a reset-resume. We'll fail the in finish_port_resume()
3513 if (udev->quirks & USB_QUIRK_RESET) in finish_port_resume()
3514 status = -ENODEV; in finish_port_resume()
3527 /* If a normal resume failed, try doing a reset-resume */ in finish_port_resume()
3528 if (status && !udev->reset_resume && udev->persist_enabled) { in finish_port_resume()
3529 dev_dbg(&udev->dev, "retry with reset-resume\n"); in finish_port_resume()
3530 udev->reset_resume = 1; in finish_port_resume()
3536 dev_dbg(&udev->dev, "gone after usb resume? status %d\n", in finish_port_resume()
3542 * udev->reset_resume in finish_port_resume()
3544 } else if (udev->actconfig && !udev->reset_resume) { in finish_port_resume()
3545 if (udev->speed < USB_SPEED_SUPER) { in finish_port_resume()
3557 dev_dbg(&udev->dev, in finish_port_resume()
3566 * There are some SS USB devices which take longer time for link training.
3570 * USB Analyzer log with such buggy devices show that in some cases
3590 struct usb_hub *hub, int port1, in wait_for_connected() argument
3598 if (!usb_port_is_power_on(hub, *portstatus)) { in wait_for_connected()
3599 status = -ENODEV; in wait_for_connected()
3604 status = usb_hub_port_status(hub, port1, portstatus, portchange); in wait_for_connected()
3606 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); in wait_for_connected()
3611 * usb_port_resume - re-activate a suspended usb device's upstream port
3612 * @udev: device to re-activate, not a root hub
3615 * This will re-activate the suspended device, increasing power usage
3617 * USB resume explicitly guarantees that the power session between
3621 * If @udev->reset_resume is set then this routine won't check that the
3627 * during a system sleep or is reset when the system wakes up, all the USB
3629 * for mass-storage devices containing mounted filesystems, since the
3646 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_port_resume() local
3647 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; in usb_port_resume()
3648 int port1 = udev->portnum; in usb_port_resume()
3652 if (!test_and_set_bit(port1, hub->child_usage_bits)) { in usb_port_resume()
3653 status = pm_runtime_resume_and_get(&port_dev->dev); in usb_port_resume()
3655 dev_dbg(&udev->dev, "can't resume usb port, status %d\n", in usb_port_resume()
3663 /* Skip the initial Clear-Suspend step for a remote wakeup */ in usb_port_resume()
3664 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in usb_port_resume()
3665 if (status == 0 && !port_is_suspended(hub, portstatus)) { in usb_port_resume()
3667 pm_wakeup_event(&udev->dev, 0); in usb_port_resume()
3672 if (hub_is_superspeed(hub->hdev)) in usb_port_resume()
3673 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); in usb_port_resume()
3675 status = usb_clear_port_feature(hub->hdev, in usb_port_resume()
3678 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); in usb_port_resume()
3681 dev_dbg(&udev->dev, "usb %sresume\n", in usb_port_resume()
3682 (PMSG_IS_AUTO(msg) ? "auto-" : "")); in usb_port_resume()
3689 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in usb_port_resume()
3694 udev->port_is_suspended = 0; in usb_port_resume()
3695 if (hub_is_superspeed(hub->hdev)) { in usb_port_resume()
3697 usb_clear_port_feature(hub->hdev, port1, in usb_port_resume()
3701 usb_clear_port_feature(hub->hdev, port1, in usb_port_resume()
3709 if (udev->persist_enabled) in usb_port_resume()
3710 status = wait_for_connected(udev, hub, port1, &portchange, in usb_port_resume()
3714 hub, port1, status, portchange, portstatus); in usb_port_resume()
3718 dev_dbg(&udev->dev, "can't resume, status %d\n", status); in usb_port_resume()
3719 hub_port_logical_disconnect(hub, port1); in usb_port_resume()
3738 if (udev->state == USB_STATE_SUSPENDED) { in usb_remote_wakeup()
3739 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); in usb_remote_wakeup()
3751 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, in hub_handle_remote_wakeup() argument
3753 __must_hold(&port_dev->status_lock) in hub_handle_remote_wakeup()
3755 struct usb_port *port_dev = hub->ports[port - 1]; in hub_handle_remote_wakeup()
3762 hdev = hub->hdev; in hub_handle_remote_wakeup()
3763 udev = port_dev->child; in hub_handle_remote_wakeup()
3770 if (!udev || udev->state != USB_STATE_SUSPENDED || in hub_handle_remote_wakeup()
3787 ret = -ENODEV; in hub_handle_remote_wakeup()
3788 hub_port_disable(hub, port, 1); in hub_handle_remote_wakeup()
3790 dev_dbg(&port_dev->dev, "resume, status %d\n", ret); in hub_handle_remote_wakeup()
3794 static int check_ports_changed(struct usb_hub *hub) in check_ports_changed() argument
3798 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { in check_ports_changed()
3802 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); in check_ports_changed()
3811 struct usb_hub *hub = usb_get_intfdata(intf); in hub_suspend() local
3812 struct usb_device *hdev = hub->hdev; in hub_suspend()
3817 * Also, add up the number of wakeup-enabled descendants. in hub_suspend()
3819 hub->wakeup_enabled_descendants = 0; in hub_suspend()
3820 for (port1 = 1; port1 <= hdev->maxchild; port1++) { in hub_suspend()
3821 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_suspend()
3822 struct usb_device *udev = port_dev->child; in hub_suspend()
3824 if (udev && udev->can_submit) { in hub_suspend()
3825 dev_warn(&port_dev->dev, "device %s not suspended yet\n", in hub_suspend()
3826 dev_name(&udev->dev)); in hub_suspend()
3828 return -EBUSY; in hub_suspend()
3831 hub->wakeup_enabled_descendants += in hub_suspend()
3835 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { in hub_suspend()
3836 /* check if there are changes pending on hub ports */ in hub_suspend()
3837 if (check_ports_changed(hub)) { in hub_suspend()
3839 return -EBUSY; in hub_suspend()
3840 pm_wakeup_event(&hdev->dev, 2000); in hub_suspend()
3844 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { in hub_suspend()
3845 /* Enable hub to send remote wakeup for all ports. */ in hub_suspend()
3846 for (port1 = 1; port1 <= hdev->maxchild; port1++) { in hub_suspend()
3856 dev_dbg(&intf->dev, "%s\n", __func__); in hub_suspend()
3859 hub_quiesce(hub, HUB_SUSPEND); in hub_suspend()
3863 /* Report wakeup requests from the ports of a resuming root hub */
3864 static void report_wakeup_requests(struct usb_hub *hub) in report_wakeup_requests() argument
3866 struct usb_device *hdev = hub->hdev; in report_wakeup_requests()
3872 if (hdev->parent) in report_wakeup_requests()
3873 return; /* Not a root hub */ in report_wakeup_requests()
3875 hcd = bus_to_hcd(hdev->bus); in report_wakeup_requests()
3876 if (hcd->driver->get_resuming_ports) { in report_wakeup_requests()
3886 resuming_ports = hcd->driver->get_resuming_ports(hcd); in report_wakeup_requests()
3887 for (i = 0; i < hdev->maxchild; ++i) { in report_wakeup_requests()
3889 udev = hub->ports[i]->child; in report_wakeup_requests()
3891 pm_wakeup_event(&udev->dev, 0); in report_wakeup_requests()
3899 struct usb_hub *hub = usb_get_intfdata(intf); in hub_resume() local
3901 dev_dbg(&intf->dev, "%s\n", __func__); in hub_resume()
3902 hub_activate(hub, HUB_RESUME); in hub_resume()
3910 report_wakeup_requests(hub); in hub_resume()
3916 struct usb_hub *hub = usb_get_intfdata(intf); in hub_reset_resume() local
3918 dev_dbg(&intf->dev, "%s\n", __func__); in hub_reset_resume()
3919 hub_activate(hub, HUB_RESET_RESUME); in hub_reset_resume()
3924 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3925 * @rhdev: struct usb_device for the root hub
3927 * The USB host controller driver calls this function when its root hub
3930 * When the hub driver is resumed it will take notice and carry out
3931 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3936 dev_notice(&rhdev->dev, "root hub lost power or was reset\n"); in usb_root_hub_lost_power()
3937 rhdev->reset_resume = 1; in usb_root_hub_lost_power()
3950 * device-initiated U1 or U2. This lets the device know the exit latencies from
3966 if (!udev->parent || udev->speed < USB_SPEED_SUPER || !udev->lpm_capable) in usb_req_set_sel()
3970 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in usb_req_set_sel()
3971 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); in usb_req_set_sel()
3972 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in usb_req_set_sel()
3973 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); in usb_req_set_sel()
3980 * latency for the link state, and could start a device-initiated in usb_req_set_sel()
3987 dev_dbg(&udev->dev, "Device-initiated U1/U2 disabled due to long SEL or PEL\n"); in usb_req_set_sel()
3988 return -EINVAL; in usb_req_set_sel()
3998 return -ENOMEM; in usb_req_set_sel()
4000 sel_values->u1_sel = u1_sel; in usb_req_set_sel()
4001 sel_values->u1_pel = u1_pel; in usb_req_set_sel()
4002 sel_values->u2_sel = cpu_to_le16(u2_sel); in usb_req_set_sel()
4003 sel_values->u2_pel = cpu_to_le16(u2_pel); in usb_req_set_sel()
4014 udev->lpm_devinit_allow = 1; in usb_req_set_sel()
4020 * Enable or disable device-initiated U1 or U2 transitions.
4036 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", in usb_set_device_initiated_lpm()
4038 return -EINVAL; in usb_set_device_initiated_lpm()
4041 if (udev->state != USB_STATE_CONFIGURED) { in usb_set_device_initiated_lpm()
4042 dev_dbg(&udev->dev, "%s: Can't %s %s state " in usb_set_device_initiated_lpm()
4051 * Now send the control transfer to enable device-initiated LPM in usb_set_device_initiated_lpm()
4069 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", in usb_set_device_initiated_lpm()
4072 return -EBUSY; in usb_set_device_initiated_lpm()
4091 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", in usb_set_lpm_timeout()
4093 return -EINVAL; in usb_set_lpm_timeout()
4098 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " in usb_set_lpm_timeout()
4101 return -EINVAL; in usb_set_lpm_timeout()
4104 ret = set_port_feature(udev->parent, in usb_set_lpm_timeout()
4105 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, in usb_set_lpm_timeout()
4108 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," in usb_set_lpm_timeout()
4111 return -EBUSY; in usb_set_lpm_timeout()
4114 udev->u1_params.timeout = timeout; in usb_set_lpm_timeout()
4116 udev->u2_params.timeout = timeout; in usb_set_lpm_timeout()
4123 * periodic endpoint. See USB 3.2 section 9.4.9
4131 if (!udev->lpm_devinit_allow) in usb_device_may_initiate_lpm()
4135 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in usb_device_may_initiate_lpm()
4137 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in usb_device_may_initiate_lpm()
4141 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { in usb_device_may_initiate_lpm()
4146 intf = udev->actconfig->interface[i]; in usb_device_may_initiate_lpm()
4150 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) { in usb_device_may_initiate_lpm()
4151 desc = &intf->cur_altsetting->endpoint[j].desc; in usb_device_may_initiate_lpm()
4155 interval = (1 << (desc->bInterval - 1)) * 125; in usb_device_may_initiate_lpm()
4165 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
4169 * control transfers to set the hub timeout or enable device-initiated U1/U2
4172 * If the control transfer to enable device-initiated U1/U2 entry fails, then
4173 * hub-initiated U1/U2 will be disabled.
4175 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
4183 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat; in usb_enable_link_state()
4184 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat; in usb_enable_link_state()
4199 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); in usb_enable_link_state()
4206 dev_warn(&udev->dev, "Could not enable %s link state, " in usb_enable_link_state()
4213 /* If we can't set the parent hub U1/U2 timeout, in usb_enable_link_state()
4214 * device-initiated LPM won't be allowed either, so let the xHCI in usb_enable_link_state()
4217 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); in usb_enable_link_state()
4224 if (udev->actconfig && in usb_enable_link_state()
4232 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); in usb_enable_link_state()
4238 udev->usb3_lpm_u1_enabled = 1; in usb_enable_link_state()
4240 udev->usb3_lpm_u2_enabled = 1; in usb_enable_link_state()
4243 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
4246 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
4249 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
4250 * it won't have an effect on the bus link state because the parent hub will
4251 * still disallow device-initiated U1/U2 entry.
4265 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", in usb_disable_link_state()
4267 return -EINVAL; in usb_disable_link_state()
4271 return -EBUSY; in usb_disable_link_state()
4275 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) in usb_disable_link_state()
4276 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " in usb_disable_link_state()
4280 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM in usb_disable_link_state()
4281 * is disabled. Hub will disallows link to enter U1/U2 as well, in usb_disable_link_state()
4282 * even device is initiating LPM. Hence LPM is disabled if hub LPM in usb_disable_link_state()
4283 * timeout set to 0, no matter device-initiated LPM is disabled or in usb_disable_link_state()
4287 udev->usb3_lpm_u1_enabled = 0; in usb_disable_link_state()
4289 udev->usb3_lpm_u2_enabled = 0; in usb_disable_link_state()
4295 * Disable hub-initiated and device-initiated U1 and U2 entry.
4299 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
4305 if (!udev || !udev->parent || in usb_disable_lpm()
4306 udev->speed < USB_SPEED_SUPER || in usb_disable_lpm()
4307 !udev->lpm_capable || in usb_disable_lpm()
4308 udev->state < USB_STATE_CONFIGURED) in usb_disable_lpm()
4311 hcd = bus_to_hcd(udev->bus); in usb_disable_lpm()
4312 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) in usb_disable_lpm()
4315 udev->lpm_disable_count++; in usb_disable_lpm()
4316 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) in usb_disable_lpm()
4329 return -EBUSY; in usb_disable_lpm()
4336 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_unlocked_disable_lpm()
4340 return -EINVAL; in usb_unlocked_disable_lpm()
4342 mutex_lock(hcd->bandwidth_mutex); in usb_unlocked_disable_lpm()
4344 mutex_unlock(hcd->bandwidth_mutex); in usb_unlocked_disable_lpm()
4351 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
4361 struct usb_hub *hub; in usb_enable_lpm() local
4364 if (!udev || !udev->parent || in usb_enable_lpm()
4365 udev->speed < USB_SPEED_SUPER || in usb_enable_lpm()
4366 !udev->lpm_capable || in usb_enable_lpm()
4367 udev->state < USB_STATE_CONFIGURED) in usb_enable_lpm()
4370 udev->lpm_disable_count--; in usb_enable_lpm()
4371 hcd = bus_to_hcd(udev->bus); in usb_enable_lpm()
4375 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || in usb_enable_lpm()
4376 !hcd->driver->disable_usb3_lpm_timeout) in usb_enable_lpm()
4379 if (udev->lpm_disable_count > 0) in usb_enable_lpm()
4382 hub = usb_hub_to_struct_hub(udev->parent); in usb_enable_lpm()
4383 if (!hub) in usb_enable_lpm()
4386 port_dev = hub->ports[udev->portnum - 1]; in usb_enable_lpm()
4388 if (port_dev->usb3_lpm_u1_permit) in usb_enable_lpm()
4391 if (port_dev->usb3_lpm_u2_permit) in usb_enable_lpm()
4399 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_unlocked_enable_lpm()
4404 mutex_lock(hcd->bandwidth_mutex); in usb_unlocked_enable_lpm()
4406 mutex_unlock(hcd->bandwidth_mutex); in usb_unlocked_enable_lpm()
4411 static void hub_usb3_port_prepare_disable(struct usb_hub *hub, in hub_usb3_port_prepare_disable() argument
4414 struct usb_device *udev = port_dev->child; in hub_usb3_port_prepare_disable()
4417 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { in hub_usb3_port_prepare_disable()
4418 ret = hub_set_port_link_state(hub, port_dev->portnum, in hub_usb3_port_prepare_disable()
4425 dev_warn(&udev->dev, in hub_usb3_port_prepare_disable()
4427 udev->do_remote_wakeup = 0; in hub_usb3_port_prepare_disable()
4437 static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, in hub_usb3_port_prepare_disable() argument
4467 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, in hub_handle_remote_wakeup() argument
4481 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
4482 * a connection with a plugged-in cable but will signal the host when the cable
4483 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
4485 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) in hub_port_disable() argument
4487 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_disable()
4488 struct usb_device *hdev = hub->hdev; in hub_port_disable()
4491 if (!hub->error) { in hub_port_disable()
4492 if (hub_is_superspeed(hub->hdev)) { in hub_port_disable()
4493 hub_usb3_port_prepare_disable(hub, port_dev); in hub_port_disable()
4494 ret = hub_set_port_link_state(hub, port_dev->portnum, in hub_port_disable()
4501 if (port_dev->child && set_state) in hub_port_disable()
4502 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); in hub_port_disable()
4503 if (ret && ret != -ENODEV) in hub_port_disable()
4504 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); in hub_port_disable()
4509 * usb_port_disable - disable a usb device's upstream port
4513 * Disables a USB device that isn't in active use.
4517 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_port_disable() local
4519 return hub_port_disable(hub, udev->portnum, 0); in usb_port_disable()
4522 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4525 * of 100ms at least for debounce and power-settling. The corresponding
4528 * Apparently there are some bluetooth and irda-dongles and a number of
4529 * low-speed devices for which this debounce period may last over a second.
4530 * Not covered by the spec - but easy to deal with.
4533 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4537 int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) in hub_port_debounce() argument
4543 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_debounce()
4546 ret = usb_hub_port_status(hub, port1, &portstatus, &portchange); in hub_port_debounce()
4563 usb_clear_port_feature(hub->hdev, port1, in hub_port_debounce()
4572 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", in hub_port_debounce()
4576 return -ETIMEDOUT; in hub_port_debounce()
4584 usb_enable_endpoint(udev, &udev->ep0, true); in usb_ep0_reinit()
4594 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_set_address()
4600 if (!hcd->driver->address_device && devnum <= 1) in hub_set_address()
4601 return -EINVAL; in hub_set_address()
4602 if (udev->state == USB_STATE_ADDRESS) in hub_set_address()
4604 if (udev->state != USB_STATE_DEFAULT) in hub_set_address()
4605 return -EINVAL; in hub_set_address()
4606 if (hcd->driver->address_device) in hub_set_address()
4607 retval = hcd->driver->address_device(hcd, udev); in hub_set_address()
4622 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4623 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4626 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4627 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4632 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in hub_set_initial_usb2_lpm_policy() local
4635 if (!udev->usb2_hw_lpm_capable || !udev->bos) in hub_set_initial_usb2_lpm_policy()
4638 if (hub) in hub_set_initial_usb2_lpm_policy()
4639 connect_type = hub->ports[udev->portnum - 1]->connect_type; in hub_set_initial_usb2_lpm_policy()
4641 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || in hub_set_initial_usb2_lpm_policy()
4643 udev->usb2_hw_lpm_allowed = 1; in hub_set_initial_usb2_lpm_policy()
4650 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in hub_enable_device()
4652 if (!hcd->driver->enable_device) in hub_enable_device()
4654 if (udev->state == USB_STATE_ADDRESS) in hub_enable_device()
4656 if (udev->state != USB_STATE_DEFAULT) in hub_enable_device()
4657 return -EINVAL; in hub_enable_device()
4659 return hcd->driver->enable_device(hcd, udev); in hub_enable_device()
4666 * If this is called for an already-existing device (as part of
4673 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, in hub_port_init() argument
4676 struct usb_device *hdev = hub->hdev; in hub_port_init()
4677 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); in hub_port_init()
4678 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_init()
4681 enum usb_device_speed oldspeed = udev->speed; in hub_port_init()
4683 int devnum = udev->devnum; in hub_port_init()
4687 /* root hub ports have a slightly longer reset period in hub_port_init()
4688 * (from USB 2.0 spec, section 7.1.7.5) in hub_port_init()
4690 if (!hdev->parent) { in hub_port_init()
4692 if (port1 == hdev->bus->otg_port) in hub_port_init()
4693 hdev->bus->b_hnp_enable = 0; in hub_port_init()
4702 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ in hub_port_init()
4703 retval = hub_port_reset(hub, port1, udev, delay, false); in hub_port_init()
4708 retval = -ENODEV; in hub_port_init()
4710 /* Don't allow speed changes at reset, except usb 3.0 to faster */ in hub_port_init()
4711 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && in hub_port_init()
4712 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { in hub_port_init()
4713 dev_dbg(&udev->dev, "device reset changed speed!\n"); in hub_port_init()
4716 oldspeed = udev->speed; in hub_port_init()
4718 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... in hub_port_init()
4720 * For Wireless USB devices, ep0 max packet is always 512 (tho in hub_port_init()
4723 switch (udev->speed) { in hub_port_init()
4727 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); in hub_port_init()
4730 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); in hub_port_init()
4737 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); in hub_port_init()
4740 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); in hub_port_init()
4746 if (udev->speed == USB_SPEED_WIRELESS) in hub_port_init()
4749 speed = usb_speed_string(udev->speed); in hub_port_init()
4756 * platform device is usually a dual-role USB controller device. in hub_port_init()
4758 if (udev->bus->controller->driver) in hub_port_init()
4759 driver_name = udev->bus->controller->driver->name; in hub_port_init()
4761 driver_name = udev->bus->sysdev->driver->name; in hub_port_init()
4763 if (udev->speed < USB_SPEED_SUPER) in hub_port_init()
4764 dev_info(&udev->dev, in hub_port_init()
4765 "%s %s USB device number %d using %s\n", in hub_port_init()
4766 (udev->config) ? "reset" : "new", speed, in hub_port_init()
4770 if (hdev->tt) { in hub_port_init()
4771 udev->tt = hdev->tt; in hub_port_init()
4772 udev->ttport = hdev->ttport; in hub_port_init()
4773 } else if (udev->speed != USB_SPEED_HIGH in hub_port_init()
4774 && hdev->speed == USB_SPEED_HIGH) { in hub_port_init()
4775 if (!hub->tt.hub) { in hub_port_init()
4776 dev_err(&udev->dev, "parent hub has no TT\n"); in hub_port_init()
4777 retval = -EINVAL; in hub_port_init()
4780 udev->tt = &hub->tt; in hub_port_init()
4781 udev->ttport = port1; in hub_port_init()
4790 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, in hub_port_init()
4791 * so it may help with some non-standards-compliant devices. in hub_port_init()
4805 dev_err(&udev->dev, in hub_port_init()
4806 "hub failed to enable device, error %d\n", in hub_port_init()
4814 retval = -ENOMEM; in hub_port_init()
4824 buf->bMaxPacketSize0 = 0; in hub_port_init()
4830 switch (buf->bMaxPacketSize0) { in hub_port_init()
4832 if (buf->bDescriptorType == in hub_port_init()
4840 r = -EPROTO; in hub_port_init()
4849 if (r == 0 || (r == -ETIMEDOUT && in hub_port_init()
4851 udev->speed > USB_SPEED_FULL)) in hub_port_init()
4854 udev->descriptor.bMaxPacketSize0 = in hub_port_init()
4855 buf->bMaxPacketSize0; in hub_port_init()
4858 retval = hub_port_reset(hub, port1, udev, delay, false); in hub_port_init()
4861 if (oldspeed != udev->speed) { in hub_port_init()
4862 dev_dbg(&udev->dev, in hub_port_init()
4864 retval = -ENODEV; in hub_port_init()
4868 if (r != -ENODEV) in hub_port_init()
4869 dev_err(&udev->dev, "device descriptor read/64, error %d\n", in hub_port_init()
4871 retval = -EMSGSIZE; in hub_port_init()
4882 if (udev->wusb == 0) { in hub_port_init()
4890 if (retval != -ENODEV) in hub_port_init()
4891 dev_err(&udev->dev, "device not accepting address %d, error %d\n", in hub_port_init()
4895 if (udev->speed >= USB_SPEED_SUPER) { in hub_port_init()
4896 devnum = udev->devnum; in hub_port_init()
4897 dev_info(&udev->dev, in hub_port_init()
4898 "%s SuperSpeed%s%s USB device number %d using %s\n", in hub_port_init()
4899 (udev->config) ? "reset" : "new", in hub_port_init()
4900 (udev->speed == USB_SPEED_SUPER_PLUS) ? in hub_port_init()
4902 (udev->ssp_rate == USB_SSP_GEN_2x2) ? in hub_port_init()
4904 (udev->ssp_rate == USB_SSP_GEN_2x1) ? in hub_port_init()
4906 (udev->ssp_rate == USB_SSP_GEN_1x2) ? in hub_port_init()
4912 * - let SET_ADDRESS settle, some device hardware wants it in hub_port_init()
4913 * - read ep0 maxpacket even for high and low speed, in hub_port_init()
4922 if (retval != -ENODEV) in hub_port_init()
4923 dev_err(&udev->dev, in hub_port_init()
4927 retval = -EMSGSIZE; in hub_port_init()
4933 delay = udev->parent->hub_delay; in hub_port_init()
4934 udev->hub_delay = min_t(u32, delay, in hub_port_init()
4938 dev_dbg(&udev->dev, in hub_port_init()
4951 * and attached to a superspeed hub port, but the device descriptor in hub_port_init()
4955 if ((udev->speed >= USB_SPEED_SUPER) && in hub_port_init()
4956 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { in hub_port_init()
4957 dev_err(&udev->dev, "got a wrong device descriptor, " in hub_port_init()
4959 hub_port_reset(hub, port1, udev, in hub_port_init()
4961 retval = -EINVAL; in hub_port_init()
4965 if (udev->descriptor.bMaxPacketSize0 == 0xff || in hub_port_init()
4966 udev->speed >= USB_SPEED_SUPER) in hub_port_init()
4969 i = udev->descriptor.bMaxPacketSize0; in hub_port_init()
4970 if (usb_endpoint_maxp(&udev->ep0.desc) != i) { in hub_port_init()
4971 if (udev->speed == USB_SPEED_LOW || in hub_port_init()
4973 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); in hub_port_init()
4974 retval = -EMSGSIZE; in hub_port_init()
4977 if (udev->speed == USB_SPEED_FULL) in hub_port_init()
4978 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); in hub_port_init()
4980 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); in hub_port_init()
4981 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); in hub_port_init()
4986 if (retval < (signed)sizeof(udev->descriptor)) { in hub_port_init()
4987 if (retval != -ENODEV) in hub_port_init()
4988 dev_err(&udev->dev, "device descriptor read/all, error %d\n", in hub_port_init()
4991 retval = -ENOMSG; in hub_port_init()
4997 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { in hub_port_init()
5000 udev->lpm_capable = usb_device_supports_lpm(udev); in hub_port_init()
5001 udev->lpm_disable_count = 1; in hub_port_init()
5009 if (hcd->driver->update_device) in hub_port_init()
5010 hcd->driver->update_device(hcd, udev); in hub_port_init()
5014 hub_port_disable(hub, port1, 0); in hub_port_init()
5021 check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) in check_highspeed() argument
5026 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) in check_highspeed()
5036 dev_info(&udev->dev, "not running at top speed; " in check_highspeed()
5037 "connect to a high speed hub\n"); in check_highspeed()
5038 /* hub LEDs are probably harder to miss than syslog */ in check_highspeed()
5039 if (hub->has_indicators) { in check_highspeed()
5040 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; in check_highspeed()
5042 &hub->leds, 0); in check_highspeed()
5049 hub_power_remaining(struct usb_hub *hub) in hub_power_remaining() argument
5051 struct usb_device *hdev = hub->hdev; in hub_power_remaining()
5055 if (!hub->limited_power) in hub_power_remaining()
5058 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; in hub_power_remaining()
5059 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { in hub_power_remaining()
5060 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_power_remaining()
5061 struct usb_device *udev = port_dev->child; in hub_power_remaining()
5076 if (udev->actconfig) in hub_power_remaining()
5077 delta = usb_get_max_power(udev, udev->actconfig); in hub_power_remaining()
5078 else if (port1 != udev->bus->otg_port || hdev->parent) in hub_power_remaining()
5082 if (delta > hub->mA_per_port) in hub_power_remaining()
5083 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", in hub_power_remaining()
5084 delta, hub->mA_per_port); in hub_power_remaining()
5085 remaining -= delta; in hub_power_remaining()
5088 dev_warn(hub->intfdev, "%dmA over power budget!\n", in hub_power_remaining()
5089 -remaining); in hub_power_remaining()
5108 if (memcmp(&udev->descriptor, old_device_descriptor, in descriptors_changed()
5112 if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) in descriptors_changed()
5114 if (udev->bos) { in descriptors_changed()
5115 len = le16_to_cpu(udev->bos->desc->wTotalLength); in descriptors_changed()
5116 if (len != le16_to_cpu(old_bos->desc->wTotalLength)) in descriptors_changed()
5118 if (memcmp(udev->bos->desc, old_bos->desc, len)) in descriptors_changed()
5128 if (udev->serial) in descriptors_changed()
5129 serial_len = strlen(udev->serial) + 1; in descriptors_changed()
5132 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { in descriptors_changed()
5133 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); in descriptors_changed()
5142 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { in descriptors_changed()
5143 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); in descriptors_changed()
5147 dev_dbg(&udev->dev, "config index %d, error %d\n", in descriptors_changed()
5152 if (memcmp(buf, udev->rawdescriptors[index], old_length) in descriptors_changed()
5154 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", in descriptors_changed()
5156 ((struct usb_config_descriptor *) buf)-> in descriptors_changed()
5164 length = usb_string(udev, udev->descriptor.iSerialNumber, in descriptors_changed()
5167 dev_dbg(&udev->dev, "serial string error %d\n", in descriptors_changed()
5170 } else if (memcmp(buf, udev->serial, length) != 0) { in descriptors_changed()
5171 dev_dbg(&udev->dev, "serial string changed\n"); in descriptors_changed()
5180 static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, in hub_port_connect() argument
5183 int status = -ENODEV; in hub_port_connect()
5186 struct usb_device *hdev = hub->hdev; in hub_port_connect()
5187 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); in hub_port_connect()
5188 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_connect()
5189 struct usb_device *udev = port_dev->child; in hub_port_connect()
5190 static int unreliable_port = -1; in hub_port_connect()
5195 if (hcd->usb_phy && !hdev->parent) in hub_port_connect()
5196 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); in hub_port_connect()
5197 usb_disconnect(&port_dev->child); in hub_port_connect()
5205 clear_bit(port1, hub->removed_bits); in hub_port_connect()
5209 status = hub_port_debounce_be_stable(hub, port1); in hub_port_connect()
5211 if (status != -ENODEV && in hub_port_connect()
5214 dev_err(&port_dev->dev, "connect-debounce failed\n"); in hub_port_connect()
5226 test_bit(port1, hub->removed_bits)) { in hub_port_connect()
5229 * maybe switch power back on (e.g. root hub was reset) in hub_port_connect()
5232 if (hub_is_port_power_switchable(hub) in hub_port_connect()
5233 && !usb_port_is_power_on(hub, portstatus) in hub_port_connect()
5234 && !port_dev->port_owner) in hub_port_connect()
5241 if (hub_is_superspeed(hub->hdev)) in hub_port_connect()
5250 mutex_lock(hcd->address0_mutex); in hub_port_connect()
5255 udev = usb_alloc_dev(hdev, hdev->bus, port1); in hub_port_connect()
5257 dev_err(&port_dev->dev, in hub_port_connect()
5259 mutex_unlock(hcd->address0_mutex); in hub_port_connect()
5265 udev->bus_mA = hub->mA_per_port; in hub_port_connect()
5266 udev->level = hdev->level + 1; in hub_port_connect()
5267 udev->wusb = hub_is_wusb(hub); in hub_port_connect()
5269 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ in hub_port_connect()
5270 if (hub_is_superspeed(hub->hdev)) in hub_port_connect()
5271 udev->speed = USB_SPEED_SUPER; in hub_port_connect()
5273 udev->speed = USB_SPEED_UNKNOWN; in hub_port_connect()
5276 if (udev->devnum <= 0) { in hub_port_connect()
5277 status = -ENOTCONN; /* Don't retry */ in hub_port_connect()
5281 /* reset (non-USB 3.0 devices) and get descriptor */ in hub_port_connect()
5282 status = hub_port_init(hub, udev, port1, i); in hub_port_connect()
5286 mutex_unlock(hcd->address0_mutex); in hub_port_connect()
5290 if (udev->quirks & USB_QUIRK_DELAY_INIT) in hub_port_connect()
5293 /* consecutive bus-powered hubs aren't reliable; they can in hub_port_connect()
5296 * (without reading syslog), even without per-port LEDs in hub_port_connect()
5299 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB in hub_port_connect()
5300 && udev->bus_mA <= unit_load) { in hub_port_connect()
5306 dev_dbg(&udev->dev, "get status %d ?\n", status); in hub_port_connect()
5310 dev_err(&udev->dev, in hub_port_connect()
5311 "can't connect bus-powered hub " in hub_port_connect()
5313 if (hub->has_indicators) { in hub_port_connect()
5314 hub->indicator[port1-1] = in hub_port_connect()
5318 &hub->leds, 0); in hub_port_connect()
5320 status = -ENOTCONN; /* Don't retry */ in hub_port_connect()
5326 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 in hub_port_connect()
5327 && udev->speed == USB_SPEED_FULL in hub_port_connect()
5329 check_highspeed(hub, udev, port1); in hub_port_connect()
5339 /* We mustn't add new devices if the parent hub has in hub_port_connect()
5344 if (hdev->state == USB_STATE_NOTATTACHED) in hub_port_connect()
5345 status = -ENOTCONN; in hub_port_connect()
5347 port_dev->child = udev; in hub_port_connect()
5357 port_dev->child = NULL; in hub_port_connect()
5361 if (hcd->usb_phy && !hdev->parent) in hub_port_connect()
5362 usb_phy_notify_connect(hcd->usb_phy, in hub_port_connect()
5363 udev->speed); in hub_port_connect()
5370 status = hub_power_remaining(hub); in hub_port_connect()
5372 dev_dbg(hub->intfdev, "%dmA power budget left\n", status); in hub_port_connect()
5377 hub_port_disable(hub, port1, 1); in hub_port_connect()
5383 mutex_unlock(hcd->address0_mutex); in hub_port_connect()
5387 if ((status == -ENOTCONN) || (status == -ENOTSUPP)) in hub_port_connect()
5390 /* When halfway through our retry count, power-cycle the port */ in hub_port_connect()
5391 if (i == (PORT_INIT_TRIES - 1) / 2) { in hub_port_connect()
5392 dev_info(&port_dev->dev, "attempt power cycle\n"); in hub_port_connect()
5393 usb_hub_set_port_power(hdev, hub, port1, false); in hub_port_connect()
5394 msleep(2 * hub_power_on_good_delay(hub)); in hub_port_connect()
5395 usb_hub_set_port_power(hdev, hub, port1, true); in hub_port_connect()
5396 msleep(hub_power_on_good_delay(hub)); in hub_port_connect()
5399 if (hub->hdev->parent || in hub_port_connect()
5400 !hcd->driver->port_handed_over || in hub_port_connect()
5401 !(hcd->driver->port_handed_over)(hcd, port1)) { in hub_port_connect()
5402 if (status != -ENOTCONN && status != -ENODEV) in hub_port_connect()
5403 dev_err(&port_dev->dev, in hub_port_connect()
5404 "unable to enumerate USB device\n"); in hub_port_connect()
5408 hub_port_disable(hub, port1, 1); in hub_port_connect()
5409 if (hcd->driver->relinquish_port && !hub->hdev->parent) { in hub_port_connect()
5410 if (status != -ENOTCONN && status != -ENODEV) in hub_port_connect()
5411 hcd->driver->relinquish_port(hcd, port1); in hub_port_connect()
5417 * a port connection-change occurs;
5418 * a port enable-change occurs (often caused by EMI);
5421 * caller already locked the hub
5423 static void hub_port_connect_change(struct usb_hub *hub, int port1, in hub_port_connect_change() argument
5425 __must_hold(&port_dev->status_lock) in hub_port_connect_change()
5427 struct usb_port *port_dev = hub->ports[port1 - 1]; in hub_port_connect_change()
5428 struct usb_device *udev = port_dev->child; in hub_port_connect_change()
5430 int status = -ENODEV; in hub_port_connect_change()
5433 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, in hub_port_connect_change()
5434 portchange, portspeed(hub, portstatus)); in hub_port_connect_change()
5436 if (hub->has_indicators) { in hub_port_connect_change()
5437 set_port_led(hub, port1, HUB_LED_AUTO); in hub_port_connect_change()
5438 hub->indicator[port1-1] = INDICATOR_AUTO; in hub_port_connect_change()
5443 if (hub->hdev->bus->is_b_host) in hub_port_connect_change()
5450 udev->state != USB_STATE_NOTATTACHED) { in hub_port_connect_change()
5453 * USB-3 connections are initialized automatically by in hub_port_connect_change()
5458 descriptor = udev->descriptor; in hub_port_connect_change()
5460 sizeof(udev->descriptor)); in hub_port_connect_change()
5462 dev_dbg(&udev->dev, in hub_port_connect_change()
5467 udev->bos)) { in hub_port_connect_change()
5468 dev_dbg(&udev->dev, in hub_port_connect_change()
5471 udev->descriptor = descriptor; in hub_port_connect_change()
5477 } else if (udev->state == USB_STATE_SUSPENDED && in hub_port_connect_change()
5478 udev->persist_enabled) { in hub_port_connect_change()
5490 clear_bit(port1, hub->change_bits); in hub_port_connect_change()
5497 hub_port_connect(hub, port1, portstatus, portchange); in hub_port_connect_change()
5501 /* Handle notifying userspace about hub over-current events */
5508 sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); in port_over_current_notify()
5510 hub_dev = port_dev->dev.parent; in port_over_current_notify()
5515 port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); in port_over_current_notify()
5524 port_dev->over_current_count); in port_over_current_notify()
5528 kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); in port_over_current_notify()
5536 static void port_event(struct usb_hub *hub, int port1) in port_event() argument
5537 __must_hold(&port_dev->status_lock) in port_event()
5540 struct usb_port *port_dev = hub->ports[port1 - 1]; in port_event()
5541 struct usb_device *udev = port_dev->child; in port_event()
5542 struct usb_device *hdev = hub->hdev; in port_event()
5546 connect_change = test_bit(port1, hub->change_bits); in port_event()
5547 clear_bit(port1, hub->event_bits); in port_event()
5548 clear_bit(port1, hub->wakeup_bits); in port_event()
5550 if (usb_hub_port_status(hub, port1, &portstatus, &portchange) < 0) in port_event()
5560 dev_dbg(&port_dev->dev, "enable change, status %08x\n", in port_event()
5565 * EM interference sometimes causes badly shielded USB devices in port_event()
5566 * to be shutdown by the hub, this hack enables them again. in port_event()
5571 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); in port_event()
5578 port_dev->over_current_count++; in port_event()
5581 dev_dbg(&port_dev->dev, "over-current change #%u\n", in port_event()
5582 port_dev->over_current_count); in port_event()
5586 hub_power_on(hub, true); in port_event()
5587 usb_hub_port_status(hub, port1, &status, &unused); in port_event()
5589 dev_err(&port_dev->dev, "over-current condition\n"); in port_event()
5593 dev_dbg(&port_dev->dev, "reset change\n"); in port_event()
5598 dev_dbg(&port_dev->dev, "warm reset change\n"); in port_event()
5603 dev_dbg(&port_dev->dev, "link state change\n"); in port_event()
5608 dev_warn(&port_dev->dev, "config error\n"); in port_event()
5614 if (!pm_runtime_active(&port_dev->dev)) in port_event()
5617 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) in port_event()
5626 while (hub_port_warm_reset_required(hub, port1, portstatus)) { in port_event()
5631 usb_hub_port_status(hub, port1, &portstatus, &unused); in port_event()
5632 dev_dbg(&port_dev->dev, "Wait for inactive link disconnect detect\n"); in port_event()
5635 || udev->state == USB_STATE_NOTATTACHED) { in port_event()
5636 dev_dbg(&port_dev->dev, "do warm reset, port only\n"); in port_event()
5637 if (hub_port_reset(hub, port1, NULL, in port_event()
5639 hub_port_disable(hub, port1, 1); in port_event()
5641 dev_dbg(&port_dev->dev, "do warm reset, full device\n"); in port_event()
5653 hub_port_connect_change(hub, port1, portstatus, portchange); in port_event()
5660 struct usb_hub *hub; in hub_event() local
5666 hub = container_of(work, struct usb_hub, events); in hub_event()
5667 hdev = hub->hdev; in hub_event()
5668 hub_dev = hub->intfdev; in hub_event()
5671 kcov_remote_start_usb((u64)hdev->bus->busnum); in hub_event()
5674 hdev->state, hdev->maxchild, in hub_event()
5676 (u16) hub->change_bits[0], in hub_event()
5677 (u16) hub->event_bits[0]); in hub_event()
5682 if (unlikely(hub->disconnected)) in hub_event()
5685 /* If the hub has died, clean up after it */ in hub_event()
5686 if (hdev->state == USB_STATE_NOTATTACHED) { in hub_event()
5687 hub->error = -ENODEV; in hub_event()
5688 hub_quiesce(hub, HUB_DISCONNECT); in hub_event()
5699 /* If this is an inactive hub, do nothing */ in hub_event()
5700 if (hub->quiescing) in hub_event()
5703 if (hub->error) { in hub_event()
5704 dev_dbg(hub_dev, "resetting for error %d\n", hub->error); in hub_event()
5708 dev_dbg(hub_dev, "error resetting hub: %d\n", ret); in hub_event()
5712 hub->nerrors = 0; in hub_event()
5713 hub->error = 0; in hub_event()
5717 for (i = 1; i <= hdev->maxchild; i++) { in hub_event()
5718 struct usb_port *port_dev = hub->ports[i - 1]; in hub_event()
5720 if (test_bit(i, hub->event_bits) in hub_event()
5721 || test_bit(i, hub->change_bits) in hub_event()
5722 || test_bit(i, hub->wakeup_bits)) { in hub_event()
5729 * (powered-off), we leave it in that state, run in hub_event()
5732 pm_runtime_get_noresume(&port_dev->dev); in hub_event()
5733 pm_runtime_barrier(&port_dev->dev); in hub_event()
5735 port_event(hub, i); in hub_event()
5737 pm_runtime_put_sync(&port_dev->dev); in hub_event()
5741 /* deal with hub status changes */ in hub_event()
5742 if (test_and_clear_bit(0, hub->event_bits) == 0) in hub_event()
5744 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) in hub_event()
5752 hub->limited_power = 1; in hub_event()
5754 hub->limited_power = 0; in hub_event()
5760 dev_dbg(hub_dev, "over-current change\n"); in hub_event()
5763 hub_power_on(hub, true); in hub_event()
5764 hub_hub_status(hub, &status, &unused); in hub_event()
5766 dev_err(hub_dev, "over-current condition\n"); in hub_event()
5778 kref_put(&hub->kref, hub_release); in hub_event()
5808 MODULE_DEVICE_TABLE(usb, hub_id_table);
5811 .name = "hub",
5827 printk(KERN_ERR "%s: can't register hub driver\n", in usb_hub_init()
5829 return -1; in usb_hub_init()
5834 * USB-PERSIST port handover. Otherwise it might see that a full-speed in usb_hub_init()
5836 * over to the companion full-speed controller. in usb_hub_init()
5844 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); in usb_hub_init()
5846 return -1; in usb_hub_init()
5854 * Hub resources are freed for us by usb_deregister. It calls in usb_hub_cleanup()
5858 * individual hub resources. -greg in usb_hub_cleanup()
5864 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
5867 * WARNING - don't use this routine to reset a composite device
5876 * re-connected. All drivers will be unbound, and the device will be
5877 * re-enumerated and probed all over again.
5879 * Return: 0 if the reset succeeded, -ENODEV if the device has been
5899 struct usb_device *parent_hdev = udev->parent; in usb_reset_and_verify_device()
5901 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_reset_and_verify_device()
5902 struct usb_device_descriptor descriptor = udev->descriptor; in usb_reset_and_verify_device()
5905 int port1 = udev->portnum; in usb_reset_and_verify_device()
5907 if (udev->state == USB_STATE_NOTATTACHED || in usb_reset_and_verify_device()
5908 udev->state == USB_STATE_SUSPENDED) { in usb_reset_and_verify_device()
5909 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", in usb_reset_and_verify_device()
5910 udev->state); in usb_reset_and_verify_device()
5911 return -EINVAL; in usb_reset_and_verify_device()
5915 return -EISDIR; in usb_reset_and_verify_device()
5920 * It will be re-enabled by the enumeration process. in usb_reset_and_verify_device()
5924 bos = udev->bos; in usb_reset_and_verify_device()
5925 udev->bos = NULL; in usb_reset_and_verify_device()
5927 mutex_lock(hcd->address0_mutex); in usb_reset_and_verify_device()
5932 * Other endpoints will be handled by re-enumeration. */ in usb_reset_and_verify_device()
5935 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) in usb_reset_and_verify_device()
5938 mutex_unlock(hcd->address0_mutex); in usb_reset_and_verify_device()
5945 dev_info(&udev->dev, "device firmware changed\n"); in usb_reset_and_verify_device()
5946 udev->descriptor = descriptor; /* for disconnect() calls */ in usb_reset_and_verify_device()
5951 if (!udev->actconfig) in usb_reset_and_verify_device()
5954 mutex_lock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
5955 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); in usb_reset_and_verify_device()
5957 dev_warn(&udev->dev, in usb_reset_and_verify_device()
5960 mutex_unlock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
5965 udev->actconfig->desc.bConfigurationValue, 0, in usb_reset_and_verify_device()
5968 dev_err(&udev->dev, in usb_reset_and_verify_device()
5970 udev->actconfig->desc.bConfigurationValue, ret); in usb_reset_and_verify_device()
5971 mutex_unlock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
5974 mutex_unlock(hcd->bandwidth_mutex); in usb_reset_and_verify_device()
5978 * Don't bother to send the Set-Interface request for interfaces in usb_reset_and_verify_device()
5980 * many devices can't handle it. Instead just reset the host-side in usb_reset_and_verify_device()
5983 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { in usb_reset_and_verify_device()
5984 struct usb_host_config *config = udev->actconfig; in usb_reset_and_verify_device()
5985 struct usb_interface *intf = config->interface[i]; in usb_reset_and_verify_device()
5988 desc = &intf->cur_altsetting->desc; in usb_reset_and_verify_device()
5989 if (desc->bAlternateSetting == 0) { in usb_reset_and_verify_device()
5998 intf->resetting_device = 1; in usb_reset_and_verify_device()
5999 ret = usb_set_interface(udev, desc->bInterfaceNumber, in usb_reset_and_verify_device()
6000 desc->bAlternateSetting); in usb_reset_and_verify_device()
6001 intf->resetting_device = 0; in usb_reset_and_verify_device()
6004 dev_err(&udev->dev, "failed to restore interface %d " in usb_reset_and_verify_device()
6006 desc->bInterfaceNumber, in usb_reset_and_verify_device()
6007 desc->bAlternateSetting, in usb_reset_and_verify_device()
6012 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) in usb_reset_and_verify_device()
6013 intf->cur_altsetting->endpoint[j].streams = 0; in usb_reset_and_verify_device()
6017 /* Now that the alt settings are re-installed, enable LTM and LPM. */ in usb_reset_and_verify_device()
6022 udev->bos = bos; in usb_reset_and_verify_device()
6027 udev->bos = bos; in usb_reset_and_verify_device()
6029 return -ENODEV; in usb_reset_and_verify_device()
6033 * usb_reset_device - warn interface drivers and perform a USB port reset
6043 * being unbound or re-bound during the ongoing reset its disconnect()
6045 * routine returns -EINPROGRESS.
6064 struct usb_host_config *config = udev->actconfig; in usb_reset_device()
6065 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); in usb_reset_device() local
6067 if (udev->state == USB_STATE_NOTATTACHED) { in usb_reset_device()
6068 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", in usb_reset_device()
6069 udev->state); in usb_reset_device()
6070 return -EINVAL; in usb_reset_device()
6073 if (!udev->parent) { in usb_reset_device()
6074 /* this requires hcd-specific logic; see ohci_restart() */ in usb_reset_device()
6075 dev_dbg(&udev->dev, "%s for root hub!\n", __func__); in usb_reset_device()
6076 return -EISDIR; in usb_reset_device()
6079 if (udev->reset_in_progress) in usb_reset_device()
6080 return -EINPROGRESS; in usb_reset_device()
6081 udev->reset_in_progress = 1; in usb_reset_device()
6083 port_dev = hub->ports[udev->portnum - 1]; in usb_reset_device()
6087 * context to avoid possible deadlock if usb mass in usb_reset_device()
6092 * not been set before reseting the usb device. in usb_reset_device()
6100 for (i = 0; i < config->desc.bNumInterfaces; ++i) { in usb_reset_device()
6101 struct usb_interface *cintf = config->interface[i]; in usb_reset_device()
6105 if (cintf->dev.driver) { in usb_reset_device()
6106 drv = to_usb_driver(cintf->dev.driver); in usb_reset_device()
6107 if (drv->pre_reset && drv->post_reset) in usb_reset_device()
6108 unbind = (drv->pre_reset)(cintf); in usb_reset_device()
6109 else if (cintf->condition == in usb_reset_device()
6123 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { in usb_reset_device()
6124 struct usb_interface *cintf = config->interface[i]; in usb_reset_device()
6126 int rebind = cintf->needs_binding; in usb_reset_device()
6128 if (!rebind && cintf->dev.driver) { in usb_reset_device()
6129 drv = to_usb_driver(cintf->dev.driver); in usb_reset_device()
6130 if (drv->post_reset) in usb_reset_device()
6131 rebind = (drv->post_reset)(cintf); in usb_reset_device()
6132 else if (cintf->condition == in usb_reset_device()
6136 cintf->needs_binding = 1; in usb_reset_device()
6147 udev->reset_in_progress = 0; in usb_reset_device()
6154 * usb_queue_reset_device - Reset a USB device from an atomic context
6155 * @iface: USB interface belonging to the device to reset
6157 * This function can be used to reset a USB device from an atomic
6167 * - Scheduling two resets at the same time from two different drivers
6170 * handles ->pre_reset(), the second reset might happen or not.
6172 * - If the reset is delayed so long that the interface is unbound from
6175 * - This function can be called during .probe(). It can also be called
6178 * .disconnect(), call usb_reset_device() directly -- but watch out
6183 if (schedule_work(&iface->reset_ws)) in usb_queue_reset_device()
6189 * usb_hub_find_child - Get the pointer of child device
6191 * @hdev: USB device belonging to the usb hub
6195 * USB drivers call this function to get hub's child device
6199 * child's usb_device pointer if non-NULL.
6204 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_hub_find_child() local
6206 if (port1 < 1 || port1 > hdev->maxchild) in usb_hub_find_child()
6208 return hub->ports[port1 - 1]->child; in usb_hub_find_child()
6215 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_hub_adjust_deviceremovable() local
6219 if (!hub) in usb_hub_adjust_deviceremovable()
6223 for (i = 1; i <= hdev->maxchild; i++) { in usb_hub_adjust_deviceremovable()
6224 struct usb_port *port_dev = hub->ports[i - 1]; in usb_hub_adjust_deviceremovable()
6226 connect_type = port_dev->connect_type; in usb_hub_adjust_deviceremovable()
6230 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { in usb_hub_adjust_deviceremovable()
6231 … dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); in usb_hub_adjust_deviceremovable()
6232 desc->u.hs.DeviceRemovable[i/8] |= mask; in usb_hub_adjust_deviceremovable()
6237 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); in usb_hub_adjust_deviceremovable()
6239 for (i = 1; i <= hdev->maxchild; i++) { in usb_hub_adjust_deviceremovable()
6240 struct usb_port *port_dev = hub->ports[i - 1]; in usb_hub_adjust_deviceremovable()
6242 connect_type = port_dev->connect_type; in usb_hub_adjust_deviceremovable()
6247 … dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); in usb_hub_adjust_deviceremovable()
6253 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); in usb_hub_adjust_deviceremovable()
6259 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
6260 * @hdev: USB device belonging to the usb hub
6269 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); in usb_get_hub_port_acpi_handle() local
6271 if (!hub) in usb_get_hub_port_acpi_handle()
6274 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); in usb_get_hub_port_acpi_handle()