162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * USB hub driver. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * (C) Copyright 1999 Linus Torvalds 662306a36Sopenharmony_ci * (C) Copyright 1999 Johannes Erdfelt 762306a36Sopenharmony_ci * (C) Copyright 1999 Gregory P. Smith 862306a36Sopenharmony_ci * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * Released under the GPLv2 only. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include <linux/kernel.h> 1462306a36Sopenharmony_ci#include <linux/errno.h> 1562306a36Sopenharmony_ci#include <linux/module.h> 1662306a36Sopenharmony_ci#include <linux/moduleparam.h> 1762306a36Sopenharmony_ci#include <linux/completion.h> 1862306a36Sopenharmony_ci#include <linux/sched/mm.h> 1962306a36Sopenharmony_ci#include <linux/list.h> 2062306a36Sopenharmony_ci#include <linux/slab.h> 2162306a36Sopenharmony_ci#include <linux/kcov.h> 2262306a36Sopenharmony_ci#include <linux/ioctl.h> 2362306a36Sopenharmony_ci#include <linux/usb.h> 2462306a36Sopenharmony_ci#include <linux/usbdevice_fs.h> 2562306a36Sopenharmony_ci#include <linux/usb/hcd.h> 2662306a36Sopenharmony_ci#include <linux/usb/onboard_hub.h> 2762306a36Sopenharmony_ci#include <linux/usb/otg.h> 2862306a36Sopenharmony_ci#include <linux/usb/quirks.h> 2962306a36Sopenharmony_ci#include <linux/workqueue.h> 3062306a36Sopenharmony_ci#include <linux/mutex.h> 3162306a36Sopenharmony_ci#include <linux/random.h> 3262306a36Sopenharmony_ci#include <linux/pm_qos.h> 3362306a36Sopenharmony_ci#include <linux/kobject.h> 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#include <linux/bitfield.h> 3662306a36Sopenharmony_ci#include <linux/uaccess.h> 3762306a36Sopenharmony_ci#include <asm/byteorder.h> 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci#include "hub.h" 4062306a36Sopenharmony_ci#include "otg_productlist.h" 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci#define USB_VENDOR_GENESYS_LOGIC 0x05e3 4362306a36Sopenharmony_ci#define USB_VENDOR_SMSC 0x0424 4462306a36Sopenharmony_ci#define USB_PRODUCT_USB5534B 0x5534 4562306a36Sopenharmony_ci#define USB_VENDOR_CYPRESS 0x04b4 4662306a36Sopenharmony_ci#define USB_PRODUCT_CY7C65632 0x6570 4762306a36Sopenharmony_ci#define USB_VENDOR_TEXAS_INSTRUMENTS 0x0451 4862306a36Sopenharmony_ci#define USB_PRODUCT_TUSB8041_USB3 0x8140 4962306a36Sopenharmony_ci#define USB_PRODUCT_TUSB8041_USB2 0x8142 5062306a36Sopenharmony_ci#define USB_VENDOR_MICROCHIP 0x0424 5162306a36Sopenharmony_ci#define USB_PRODUCT_USB4913 0x4913 5262306a36Sopenharmony_ci#define USB_PRODUCT_USB4914 0x4914 5362306a36Sopenharmony_ci#define USB_PRODUCT_USB4915 0x4915 5462306a36Sopenharmony_ci#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND BIT(0) 5562306a36Sopenharmony_ci#define HUB_QUIRK_DISABLE_AUTOSUSPEND BIT(1) 5662306a36Sopenharmony_ci#define HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL BIT(2) 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci#define USB_TP_TRANSMISSION_DELAY 40 /* ns */ 5962306a36Sopenharmony_ci#define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */ 6062306a36Sopenharmony_ci#define USB_PING_RESPONSE_TIME 400 /* ns */ 6162306a36Sopenharmony_ci#define USB_REDUCE_FRAME_INTR_BINTERVAL 9 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci/* Protect struct usb_device->state and ->children members 6462306a36Sopenharmony_ci * Note: Both are also protected by ->dev.sem, except that ->state can 6562306a36Sopenharmony_ci * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 6662306a36Sopenharmony_cistatic DEFINE_SPINLOCK(device_state_lock); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/* workqueue to process hub events */ 6962306a36Sopenharmony_cistatic struct workqueue_struct *hub_wq; 7062306a36Sopenharmony_cistatic void hub_event(struct work_struct *work); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci/* synchronize hub-port add/remove and peering operations */ 7362306a36Sopenharmony_ciDEFINE_MUTEX(usb_port_peer_mutex); 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci/* cycle leds on hubs that aren't blinking for attention */ 7662306a36Sopenharmony_cistatic bool blinkenlights; 7762306a36Sopenharmony_cimodule_param(blinkenlights, bool, S_IRUGO); 7862306a36Sopenharmony_ciMODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs"); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci/* 8162306a36Sopenharmony_ci * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 8262306a36Sopenharmony_ci * 10 seconds to send reply for the initial 64-byte descriptor request. 8362306a36Sopenharmony_ci */ 8462306a36Sopenharmony_ci/* define initial 64-byte descriptor request timeout in milliseconds */ 8562306a36Sopenharmony_cistatic int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 8662306a36Sopenharmony_cimodule_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 8762306a36Sopenharmony_ciMODULE_PARM_DESC(initial_descriptor_timeout, 8862306a36Sopenharmony_ci "initial 64-byte descriptor request timeout in milliseconds " 8962306a36Sopenharmony_ci "(default 5000 - 5.0 seconds)"); 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* 9262306a36Sopenharmony_ci * As of 2.6.10 we introduce a new USB device initialization scheme which 9362306a36Sopenharmony_ci * closely resembles the way Windows works. Hopefully it will be compatible 9462306a36Sopenharmony_ci * with a wider range of devices than the old scheme. However some previously 9562306a36Sopenharmony_ci * working devices may start giving rise to "device not accepting address" 9662306a36Sopenharmony_ci * errors; if that happens the user can try the old scheme by adjusting the 9762306a36Sopenharmony_ci * following module parameters. 9862306a36Sopenharmony_ci * 9962306a36Sopenharmony_ci * For maximum flexibility there are two boolean parameters to control the 10062306a36Sopenharmony_ci * hub driver's behavior. On the first initialization attempt, if the 10162306a36Sopenharmony_ci * "old_scheme_first" parameter is set then the old scheme will be used, 10262306a36Sopenharmony_ci * otherwise the new scheme is used. If that fails and "use_both_schemes" 10362306a36Sopenharmony_ci * is set, then the driver will make another attempt, using the other scheme. 10462306a36Sopenharmony_ci */ 10562306a36Sopenharmony_cistatic bool old_scheme_first; 10662306a36Sopenharmony_cimodule_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 10762306a36Sopenharmony_ciMODULE_PARM_DESC(old_scheme_first, 10862306a36Sopenharmony_ci "start with the old device initialization scheme"); 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistatic bool use_both_schemes = true; 11162306a36Sopenharmony_cimodule_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 11262306a36Sopenharmony_ciMODULE_PARM_DESC(use_both_schemes, 11362306a36Sopenharmony_ci "try the other device initialization scheme if the " 11462306a36Sopenharmony_ci "first one fails"); 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci/* Mutual exclusion for EHCI CF initialization. This interferes with 11762306a36Sopenharmony_ci * port reset on some companion controllers. 11862306a36Sopenharmony_ci */ 11962306a36Sopenharmony_ciDECLARE_RWSEM(ehci_cf_port_reset_rwsem); 12062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci#define HUB_DEBOUNCE_TIMEOUT 2000 12362306a36Sopenharmony_ci#define HUB_DEBOUNCE_STEP 25 12462306a36Sopenharmony_ci#define HUB_DEBOUNCE_STABLE 100 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cistatic void hub_release(struct kref *kref); 12762306a36Sopenharmony_cistatic int usb_reset_and_verify_device(struct usb_device *udev); 12862306a36Sopenharmony_cistatic int hub_port_disable(struct usb_hub *hub, int port1, int set_state); 12962306a36Sopenharmony_cistatic bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 13062306a36Sopenharmony_ci u16 portstatus); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_cistatic inline char *portspeed(struct usb_hub *hub, int portstatus) 13362306a36Sopenharmony_ci{ 13462306a36Sopenharmony_ci if (hub_is_superspeedplus(hub->hdev)) 13562306a36Sopenharmony_ci return "10.0 Gb/s"; 13662306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) 13762306a36Sopenharmony_ci return "5.0 Gb/s"; 13862306a36Sopenharmony_ci if (portstatus & USB_PORT_STAT_HIGH_SPEED) 13962306a36Sopenharmony_ci return "480 Mb/s"; 14062306a36Sopenharmony_ci else if (portstatus & USB_PORT_STAT_LOW_SPEED) 14162306a36Sopenharmony_ci return "1.5 Mb/s"; 14262306a36Sopenharmony_ci else 14362306a36Sopenharmony_ci return "12 Mb/s"; 14462306a36Sopenharmony_ci} 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci/* Note that hdev or one of its children must be locked! */ 14762306a36Sopenharmony_cistruct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci if (!hdev || !hdev->actconfig || !hdev->maxchild) 15062306a36Sopenharmony_ci return NULL; 15162306a36Sopenharmony_ci return usb_get_intfdata(hdev->actconfig->interface[0]); 15262306a36Sopenharmony_ci} 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciint usb_device_supports_lpm(struct usb_device *udev) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci /* Some devices have trouble with LPM */ 15762306a36Sopenharmony_ci if (udev->quirks & USB_QUIRK_NO_LPM) 15862306a36Sopenharmony_ci return 0; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci /* Skip if the device BOS descriptor couldn't be read */ 16162306a36Sopenharmony_ci if (!udev->bos) 16262306a36Sopenharmony_ci return 0; 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci /* USB 2.1 (and greater) devices indicate LPM support through 16562306a36Sopenharmony_ci * their USB 2.0 Extended Capabilities BOS descriptor. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) { 16862306a36Sopenharmony_ci if (udev->bos->ext_cap && 16962306a36Sopenharmony_ci (USB_LPM_SUPPORT & 17062306a36Sopenharmony_ci le32_to_cpu(udev->bos->ext_cap->bmAttributes))) 17162306a36Sopenharmony_ci return 1; 17262306a36Sopenharmony_ci return 0; 17362306a36Sopenharmony_ci } 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci /* 17662306a36Sopenharmony_ci * According to the USB 3.0 spec, all USB 3.0 devices must support LPM. 17762306a36Sopenharmony_ci * However, there are some that don't, and they set the U1/U2 exit 17862306a36Sopenharmony_ci * latencies to zero. 17962306a36Sopenharmony_ci */ 18062306a36Sopenharmony_ci if (!udev->bos->ss_cap) { 18162306a36Sopenharmony_ci dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n"); 18262306a36Sopenharmony_ci return 0; 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci if (udev->bos->ss_cap->bU1devExitLat == 0 && 18662306a36Sopenharmony_ci udev->bos->ss_cap->bU2DevExitLat == 0) { 18762306a36Sopenharmony_ci if (udev->parent) 18862306a36Sopenharmony_ci dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n"); 18962306a36Sopenharmony_ci else 19062306a36Sopenharmony_ci dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n"); 19162306a36Sopenharmony_ci return 0; 19262306a36Sopenharmony_ci } 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci if (!udev->parent || udev->parent->lpm_capable) 19562306a36Sopenharmony_ci return 1; 19662306a36Sopenharmony_ci return 0; 19762306a36Sopenharmony_ci} 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci/* 20062306a36Sopenharmony_ci * Set the Maximum Exit Latency (MEL) for the host to wakup up the path from 20162306a36Sopenharmony_ci * U1/U2, send a PING to the device and receive a PING_RESPONSE. 20262306a36Sopenharmony_ci * See USB 3.1 section C.1.5.2 20362306a36Sopenharmony_ci */ 20462306a36Sopenharmony_cistatic void usb_set_lpm_mel(struct usb_device *udev, 20562306a36Sopenharmony_ci struct usb3_lpm_parameters *udev_lpm_params, 20662306a36Sopenharmony_ci unsigned int udev_exit_latency, 20762306a36Sopenharmony_ci struct usb_hub *hub, 20862306a36Sopenharmony_ci struct usb3_lpm_parameters *hub_lpm_params, 20962306a36Sopenharmony_ci unsigned int hub_exit_latency) 21062306a36Sopenharmony_ci{ 21162306a36Sopenharmony_ci unsigned int total_mel; 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci /* 21462306a36Sopenharmony_ci * tMEL1. time to transition path from host to device into U0. 21562306a36Sopenharmony_ci * MEL for parent already contains the delay up to parent, so only add 21662306a36Sopenharmony_ci * the exit latency for the last link (pick the slower exit latency), 21762306a36Sopenharmony_ci * and the hub header decode latency. See USB 3.1 section C 2.2.1 21862306a36Sopenharmony_ci * Store MEL in nanoseconds 21962306a36Sopenharmony_ci */ 22062306a36Sopenharmony_ci total_mel = hub_lpm_params->mel + 22162306a36Sopenharmony_ci max(udev_exit_latency, hub_exit_latency) * 1000 + 22262306a36Sopenharmony_ci hub->descriptor->u.ss.bHubHdrDecLat * 100; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci /* 22562306a36Sopenharmony_ci * tMEL2. Time to submit PING packet. Sum of tTPTransmissionDelay for 22662306a36Sopenharmony_ci * each link + wHubDelay for each hub. Add only for last link. 22762306a36Sopenharmony_ci * tMEL4, the time for PING_RESPONSE to traverse upstream is similar. 22862306a36Sopenharmony_ci * Multiply by 2 to include it as well. 22962306a36Sopenharmony_ci */ 23062306a36Sopenharmony_ci total_mel += (__le16_to_cpu(hub->descriptor->u.ss.wHubDelay) + 23162306a36Sopenharmony_ci USB_TP_TRANSMISSION_DELAY) * 2; 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci /* 23462306a36Sopenharmony_ci * tMEL3, tPingResponse. Time taken by device to generate PING_RESPONSE 23562306a36Sopenharmony_ci * after receiving PING. Also add 2100ns as stated in USB 3.1 C 1.5.2.4 23662306a36Sopenharmony_ci * to cover the delay if the PING_RESPONSE is queued behind a Max Packet 23762306a36Sopenharmony_ci * Size DP. 23862306a36Sopenharmony_ci * Note these delays should be added only once for the entire path, so 23962306a36Sopenharmony_ci * add them to the MEL of the device connected to the roothub. 24062306a36Sopenharmony_ci */ 24162306a36Sopenharmony_ci if (!hub->hdev->parent) 24262306a36Sopenharmony_ci total_mel += USB_PING_RESPONSE_TIME + 2100; 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci udev_lpm_params->mel = total_mel; 24562306a36Sopenharmony_ci} 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci/* 24862306a36Sopenharmony_ci * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate 24962306a36Sopenharmony_ci * a transition from either U1 or U2. 25062306a36Sopenharmony_ci */ 25162306a36Sopenharmony_cistatic void usb_set_lpm_pel(struct usb_device *udev, 25262306a36Sopenharmony_ci struct usb3_lpm_parameters *udev_lpm_params, 25362306a36Sopenharmony_ci unsigned int udev_exit_latency, 25462306a36Sopenharmony_ci struct usb_hub *hub, 25562306a36Sopenharmony_ci struct usb3_lpm_parameters *hub_lpm_params, 25662306a36Sopenharmony_ci unsigned int hub_exit_latency, 25762306a36Sopenharmony_ci unsigned int port_to_port_exit_latency) 25862306a36Sopenharmony_ci{ 25962306a36Sopenharmony_ci unsigned int first_link_pel; 26062306a36Sopenharmony_ci unsigned int hub_pel; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci /* 26362306a36Sopenharmony_ci * First, the device sends an LFPS to transition the link between the 26462306a36Sopenharmony_ci * device and the parent hub into U0. The exit latency is the bigger of 26562306a36Sopenharmony_ci * the device exit latency or the hub exit latency. 26662306a36Sopenharmony_ci */ 26762306a36Sopenharmony_ci if (udev_exit_latency > hub_exit_latency) 26862306a36Sopenharmony_ci first_link_pel = udev_exit_latency * 1000; 26962306a36Sopenharmony_ci else 27062306a36Sopenharmony_ci first_link_pel = hub_exit_latency * 1000; 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci /* 27362306a36Sopenharmony_ci * When the hub starts to receive the LFPS, there is a slight delay for 27462306a36Sopenharmony_ci * it to figure out that one of the ports is sending an LFPS. Then it 27562306a36Sopenharmony_ci * will forward the LFPS to its upstream link. The exit latency is the 27662306a36Sopenharmony_ci * delay, plus the PEL that we calculated for this hub. 27762306a36Sopenharmony_ci */ 27862306a36Sopenharmony_ci hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci /* 28162306a36Sopenharmony_ci * According to figure C-7 in the USB 3.0 spec, the PEL for this device 28262306a36Sopenharmony_ci * is the greater of the two exit latencies. 28362306a36Sopenharmony_ci */ 28462306a36Sopenharmony_ci if (first_link_pel > hub_pel) 28562306a36Sopenharmony_ci udev_lpm_params->pel = first_link_pel; 28662306a36Sopenharmony_ci else 28762306a36Sopenharmony_ci udev_lpm_params->pel = hub_pel; 28862306a36Sopenharmony_ci} 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci/* 29162306a36Sopenharmony_ci * Set the System Exit Latency (SEL) to indicate the total worst-case time from 29262306a36Sopenharmony_ci * when a device initiates a transition to U0, until when it will receive the 29362306a36Sopenharmony_ci * first packet from the host controller. 29462306a36Sopenharmony_ci * 29562306a36Sopenharmony_ci * Section C.1.5.1 describes the four components to this: 29662306a36Sopenharmony_ci * - t1: device PEL 29762306a36Sopenharmony_ci * - t2: time for the ERDY to make it from the device to the host. 29862306a36Sopenharmony_ci * - t3: a host-specific delay to process the ERDY. 29962306a36Sopenharmony_ci * - t4: time for the packet to make it from the host to the device. 30062306a36Sopenharmony_ci * 30162306a36Sopenharmony_ci * t3 is specific to both the xHCI host and the platform the host is integrated 30262306a36Sopenharmony_ci * into. The Intel HW folks have said it's negligible, FIXME if a different 30362306a36Sopenharmony_ci * vendor says otherwise. 30462306a36Sopenharmony_ci */ 30562306a36Sopenharmony_cistatic void usb_set_lpm_sel(struct usb_device *udev, 30662306a36Sopenharmony_ci struct usb3_lpm_parameters *udev_lpm_params) 30762306a36Sopenharmony_ci{ 30862306a36Sopenharmony_ci struct usb_device *parent; 30962306a36Sopenharmony_ci unsigned int num_hubs; 31062306a36Sopenharmony_ci unsigned int total_sel; 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci /* t1 = device PEL */ 31362306a36Sopenharmony_ci total_sel = udev_lpm_params->pel; 31462306a36Sopenharmony_ci /* How many external hubs are in between the device & the root port. */ 31562306a36Sopenharmony_ci for (parent = udev->parent, num_hubs = 0; parent->parent; 31662306a36Sopenharmony_ci parent = parent->parent) 31762306a36Sopenharmony_ci num_hubs++; 31862306a36Sopenharmony_ci /* t2 = 2.1us + 250ns * (num_hubs - 1) */ 31962306a36Sopenharmony_ci if (num_hubs > 0) 32062306a36Sopenharmony_ci total_sel += 2100 + 250 * (num_hubs - 1); 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci /* t4 = 250ns * num_hubs */ 32362306a36Sopenharmony_ci total_sel += 250 * num_hubs; 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci udev_lpm_params->sel = total_sel; 32662306a36Sopenharmony_ci} 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_cistatic void usb_set_lpm_parameters(struct usb_device *udev) 32962306a36Sopenharmony_ci{ 33062306a36Sopenharmony_ci struct usb_hub *hub; 33162306a36Sopenharmony_ci unsigned int port_to_port_delay; 33262306a36Sopenharmony_ci unsigned int udev_u1_del; 33362306a36Sopenharmony_ci unsigned int udev_u2_del; 33462306a36Sopenharmony_ci unsigned int hub_u1_del; 33562306a36Sopenharmony_ci unsigned int hub_u2_del; 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER) 33862306a36Sopenharmony_ci return; 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ci /* Skip if the device BOS descriptor couldn't be read */ 34162306a36Sopenharmony_ci if (!udev->bos) 34262306a36Sopenharmony_ci return; 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 34562306a36Sopenharmony_ci /* It doesn't take time to transition the roothub into U0, since it 34662306a36Sopenharmony_ci * doesn't have an upstream link. 34762306a36Sopenharmony_ci */ 34862306a36Sopenharmony_ci if (!hub) 34962306a36Sopenharmony_ci return; 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_ci udev_u1_del = udev->bos->ss_cap->bU1devExitLat; 35262306a36Sopenharmony_ci udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat); 35362306a36Sopenharmony_ci hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; 35462306a36Sopenharmony_ci hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat); 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, 35762306a36Sopenharmony_ci hub, &udev->parent->u1_params, hub_u1_del); 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, 36062306a36Sopenharmony_ci hub, &udev->parent->u2_params, hub_u2_del); 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci /* 36362306a36Sopenharmony_ci * Appendix C, section C.2.2.2, says that there is a slight delay from 36462306a36Sopenharmony_ci * when the parent hub notices the downstream port is trying to 36562306a36Sopenharmony_ci * transition to U0 to when the hub initiates a U0 transition on its 36662306a36Sopenharmony_ci * upstream port. The section says the delays are tPort2PortU1EL and 36762306a36Sopenharmony_ci * tPort2PortU2EL, but it doesn't define what they are. 36862306a36Sopenharmony_ci * 36962306a36Sopenharmony_ci * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking 37062306a36Sopenharmony_ci * about the same delays. Use the maximum delay calculations from those 37162306a36Sopenharmony_ci * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For 37262306a36Sopenharmony_ci * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I 37362306a36Sopenharmony_ci * assume the device exit latencies they are talking about are the hub 37462306a36Sopenharmony_ci * exit latencies. 37562306a36Sopenharmony_ci * 37662306a36Sopenharmony_ci * What do we do if the U2 exit latency is less than the U1 exit 37762306a36Sopenharmony_ci * latency? It's possible, although not likely... 37862306a36Sopenharmony_ci */ 37962306a36Sopenharmony_ci port_to_port_delay = 1; 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, 38262306a36Sopenharmony_ci hub, &udev->parent->u1_params, hub_u1_del, 38362306a36Sopenharmony_ci port_to_port_delay); 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci if (hub_u2_del > hub_u1_del) 38662306a36Sopenharmony_ci port_to_port_delay = 1 + hub_u2_del - hub_u1_del; 38762306a36Sopenharmony_ci else 38862306a36Sopenharmony_ci port_to_port_delay = 1 + hub_u1_del; 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, 39162306a36Sopenharmony_ci hub, &udev->parent->u2_params, hub_u2_del, 39262306a36Sopenharmony_ci port_to_port_delay); 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci /* Now that we've got PEL, calculate SEL. */ 39562306a36Sopenharmony_ci usb_set_lpm_sel(udev, &udev->u1_params); 39662306a36Sopenharmony_ci usb_set_lpm_sel(udev, &udev->u2_params); 39762306a36Sopenharmony_ci} 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci/* USB 2.0 spec Section 11.24.4.5 */ 40062306a36Sopenharmony_cistatic int get_hub_descriptor(struct usb_device *hdev, 40162306a36Sopenharmony_ci struct usb_hub_descriptor *desc) 40262306a36Sopenharmony_ci{ 40362306a36Sopenharmony_ci int i, ret, size; 40462306a36Sopenharmony_ci unsigned dtype; 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci if (hub_is_superspeed(hdev)) { 40762306a36Sopenharmony_ci dtype = USB_DT_SS_HUB; 40862306a36Sopenharmony_ci size = USB_DT_SS_HUB_SIZE; 40962306a36Sopenharmony_ci } else { 41062306a36Sopenharmony_ci dtype = USB_DT_HUB; 41162306a36Sopenharmony_ci size = sizeof(struct usb_hub_descriptor); 41262306a36Sopenharmony_ci } 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ci for (i = 0; i < 3; i++) { 41562306a36Sopenharmony_ci ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 41662306a36Sopenharmony_ci USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 41762306a36Sopenharmony_ci dtype << 8, 0, desc, size, 41862306a36Sopenharmony_ci USB_CTRL_GET_TIMEOUT); 41962306a36Sopenharmony_ci if (hub_is_superspeed(hdev)) { 42062306a36Sopenharmony_ci if (ret == size) 42162306a36Sopenharmony_ci return ret; 42262306a36Sopenharmony_ci } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) { 42362306a36Sopenharmony_ci /* Make sure we have the DeviceRemovable field. */ 42462306a36Sopenharmony_ci size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1; 42562306a36Sopenharmony_ci if (ret < size) 42662306a36Sopenharmony_ci return -EMSGSIZE; 42762306a36Sopenharmony_ci return ret; 42862306a36Sopenharmony_ci } 42962306a36Sopenharmony_ci } 43062306a36Sopenharmony_ci return -EINVAL; 43162306a36Sopenharmony_ci} 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci/* 43462306a36Sopenharmony_ci * USB 2.0 spec Section 11.24.2.1 43562306a36Sopenharmony_ci */ 43662306a36Sopenharmony_cistatic int clear_hub_feature(struct usb_device *hdev, int feature) 43762306a36Sopenharmony_ci{ 43862306a36Sopenharmony_ci return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 43962306a36Sopenharmony_ci USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 44062306a36Sopenharmony_ci} 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci/* 44362306a36Sopenharmony_ci * USB 2.0 spec Section 11.24.2.2 44462306a36Sopenharmony_ci */ 44562306a36Sopenharmony_ciint usb_clear_port_feature(struct usb_device *hdev, int port1, int feature) 44662306a36Sopenharmony_ci{ 44762306a36Sopenharmony_ci return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 44862306a36Sopenharmony_ci USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 44962306a36Sopenharmony_ci NULL, 0, 1000); 45062306a36Sopenharmony_ci} 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci/* 45362306a36Sopenharmony_ci * USB 2.0 spec Section 11.24.2.13 45462306a36Sopenharmony_ci */ 45562306a36Sopenharmony_cistatic int set_port_feature(struct usb_device *hdev, int port1, int feature) 45662306a36Sopenharmony_ci{ 45762306a36Sopenharmony_ci return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 45862306a36Sopenharmony_ci USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 45962306a36Sopenharmony_ci NULL, 0, 1000); 46062306a36Sopenharmony_ci} 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_cistatic char *to_led_name(int selector) 46362306a36Sopenharmony_ci{ 46462306a36Sopenharmony_ci switch (selector) { 46562306a36Sopenharmony_ci case HUB_LED_AMBER: 46662306a36Sopenharmony_ci return "amber"; 46762306a36Sopenharmony_ci case HUB_LED_GREEN: 46862306a36Sopenharmony_ci return "green"; 46962306a36Sopenharmony_ci case HUB_LED_OFF: 47062306a36Sopenharmony_ci return "off"; 47162306a36Sopenharmony_ci case HUB_LED_AUTO: 47262306a36Sopenharmony_ci return "auto"; 47362306a36Sopenharmony_ci default: 47462306a36Sopenharmony_ci return "??"; 47562306a36Sopenharmony_ci } 47662306a36Sopenharmony_ci} 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci/* 47962306a36Sopenharmony_ci * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 48062306a36Sopenharmony_ci * for info about using port indicators 48162306a36Sopenharmony_ci */ 48262306a36Sopenharmony_cistatic void set_port_led(struct usb_hub *hub, int port1, int selector) 48362306a36Sopenharmony_ci{ 48462306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 48562306a36Sopenharmony_ci int status; 48662306a36Sopenharmony_ci 48762306a36Sopenharmony_ci status = set_port_feature(hub->hdev, (selector << 8) | port1, 48862306a36Sopenharmony_ci USB_PORT_FEAT_INDICATOR); 48962306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "indicator %s status %d\n", 49062306a36Sopenharmony_ci to_led_name(selector), status); 49162306a36Sopenharmony_ci} 49262306a36Sopenharmony_ci 49362306a36Sopenharmony_ci#define LED_CYCLE_PERIOD ((2*HZ)/3) 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_cistatic void led_work(struct work_struct *work) 49662306a36Sopenharmony_ci{ 49762306a36Sopenharmony_ci struct usb_hub *hub = 49862306a36Sopenharmony_ci container_of(work, struct usb_hub, leds.work); 49962306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 50062306a36Sopenharmony_ci unsigned i; 50162306a36Sopenharmony_ci unsigned changed = 0; 50262306a36Sopenharmony_ci int cursor = -1; 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ci if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 50562306a36Sopenharmony_ci return; 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci for (i = 0; i < hdev->maxchild; i++) { 50862306a36Sopenharmony_ci unsigned selector, mode; 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci /* 30%-50% duty cycle */ 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci switch (hub->indicator[i]) { 51362306a36Sopenharmony_ci /* cycle marker */ 51462306a36Sopenharmony_ci case INDICATOR_CYCLE: 51562306a36Sopenharmony_ci cursor = i; 51662306a36Sopenharmony_ci selector = HUB_LED_AUTO; 51762306a36Sopenharmony_ci mode = INDICATOR_AUTO; 51862306a36Sopenharmony_ci break; 51962306a36Sopenharmony_ci /* blinking green = sw attention */ 52062306a36Sopenharmony_ci case INDICATOR_GREEN_BLINK: 52162306a36Sopenharmony_ci selector = HUB_LED_GREEN; 52262306a36Sopenharmony_ci mode = INDICATOR_GREEN_BLINK_OFF; 52362306a36Sopenharmony_ci break; 52462306a36Sopenharmony_ci case INDICATOR_GREEN_BLINK_OFF: 52562306a36Sopenharmony_ci selector = HUB_LED_OFF; 52662306a36Sopenharmony_ci mode = INDICATOR_GREEN_BLINK; 52762306a36Sopenharmony_ci break; 52862306a36Sopenharmony_ci /* blinking amber = hw attention */ 52962306a36Sopenharmony_ci case INDICATOR_AMBER_BLINK: 53062306a36Sopenharmony_ci selector = HUB_LED_AMBER; 53162306a36Sopenharmony_ci mode = INDICATOR_AMBER_BLINK_OFF; 53262306a36Sopenharmony_ci break; 53362306a36Sopenharmony_ci case INDICATOR_AMBER_BLINK_OFF: 53462306a36Sopenharmony_ci selector = HUB_LED_OFF; 53562306a36Sopenharmony_ci mode = INDICATOR_AMBER_BLINK; 53662306a36Sopenharmony_ci break; 53762306a36Sopenharmony_ci /* blink green/amber = reserved */ 53862306a36Sopenharmony_ci case INDICATOR_ALT_BLINK: 53962306a36Sopenharmony_ci selector = HUB_LED_GREEN; 54062306a36Sopenharmony_ci mode = INDICATOR_ALT_BLINK_OFF; 54162306a36Sopenharmony_ci break; 54262306a36Sopenharmony_ci case INDICATOR_ALT_BLINK_OFF: 54362306a36Sopenharmony_ci selector = HUB_LED_AMBER; 54462306a36Sopenharmony_ci mode = INDICATOR_ALT_BLINK; 54562306a36Sopenharmony_ci break; 54662306a36Sopenharmony_ci default: 54762306a36Sopenharmony_ci continue; 54862306a36Sopenharmony_ci } 54962306a36Sopenharmony_ci if (selector != HUB_LED_AUTO) 55062306a36Sopenharmony_ci changed = 1; 55162306a36Sopenharmony_ci set_port_led(hub, i + 1, selector); 55262306a36Sopenharmony_ci hub->indicator[i] = mode; 55362306a36Sopenharmony_ci } 55462306a36Sopenharmony_ci if (!changed && blinkenlights) { 55562306a36Sopenharmony_ci cursor++; 55662306a36Sopenharmony_ci cursor %= hdev->maxchild; 55762306a36Sopenharmony_ci set_port_led(hub, cursor + 1, HUB_LED_GREEN); 55862306a36Sopenharmony_ci hub->indicator[cursor] = INDICATOR_CYCLE; 55962306a36Sopenharmony_ci changed++; 56062306a36Sopenharmony_ci } 56162306a36Sopenharmony_ci if (changed) 56262306a36Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 56362306a36Sopenharmony_ci &hub->leds, LED_CYCLE_PERIOD); 56462306a36Sopenharmony_ci} 56562306a36Sopenharmony_ci 56662306a36Sopenharmony_ci/* use a short timeout for hub/port status fetches */ 56762306a36Sopenharmony_ci#define USB_STS_TIMEOUT 1000 56862306a36Sopenharmony_ci#define USB_STS_RETRIES 5 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci/* 57162306a36Sopenharmony_ci * USB 2.0 spec Section 11.24.2.6 57262306a36Sopenharmony_ci */ 57362306a36Sopenharmony_cistatic int get_hub_status(struct usb_device *hdev, 57462306a36Sopenharmony_ci struct usb_hub_status *data) 57562306a36Sopenharmony_ci{ 57662306a36Sopenharmony_ci int i, status = -ETIMEDOUT; 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci for (i = 0; i < USB_STS_RETRIES && 57962306a36Sopenharmony_ci (status == -ETIMEDOUT || status == -EPIPE); i++) { 58062306a36Sopenharmony_ci status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 58162306a36Sopenharmony_ci USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 58262306a36Sopenharmony_ci data, sizeof(*data), USB_STS_TIMEOUT); 58362306a36Sopenharmony_ci } 58462306a36Sopenharmony_ci return status; 58562306a36Sopenharmony_ci} 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci/* 58862306a36Sopenharmony_ci * USB 2.0 spec Section 11.24.2.7 58962306a36Sopenharmony_ci * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6 59062306a36Sopenharmony_ci */ 59162306a36Sopenharmony_cistatic int get_port_status(struct usb_device *hdev, int port1, 59262306a36Sopenharmony_ci void *data, u16 value, u16 length) 59362306a36Sopenharmony_ci{ 59462306a36Sopenharmony_ci int i, status = -ETIMEDOUT; 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci for (i = 0; i < USB_STS_RETRIES && 59762306a36Sopenharmony_ci (status == -ETIMEDOUT || status == -EPIPE); i++) { 59862306a36Sopenharmony_ci status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 59962306a36Sopenharmony_ci USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value, 60062306a36Sopenharmony_ci port1, data, length, USB_STS_TIMEOUT); 60162306a36Sopenharmony_ci } 60262306a36Sopenharmony_ci return status; 60362306a36Sopenharmony_ci} 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_cistatic int hub_ext_port_status(struct usb_hub *hub, int port1, int type, 60662306a36Sopenharmony_ci u16 *status, u16 *change, u32 *ext_status) 60762306a36Sopenharmony_ci{ 60862306a36Sopenharmony_ci int ret; 60962306a36Sopenharmony_ci int len = 4; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci if (type != HUB_PORT_STATUS) 61262306a36Sopenharmony_ci len = 8; 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci mutex_lock(&hub->status_mutex); 61562306a36Sopenharmony_ci ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len); 61662306a36Sopenharmony_ci if (ret < len) { 61762306a36Sopenharmony_ci if (ret != -ENODEV) 61862306a36Sopenharmony_ci dev_err(hub->intfdev, 61962306a36Sopenharmony_ci "%s failed (err = %d)\n", __func__, ret); 62062306a36Sopenharmony_ci if (ret >= 0) 62162306a36Sopenharmony_ci ret = -EIO; 62262306a36Sopenharmony_ci } else { 62362306a36Sopenharmony_ci *status = le16_to_cpu(hub->status->port.wPortStatus); 62462306a36Sopenharmony_ci *change = le16_to_cpu(hub->status->port.wPortChange); 62562306a36Sopenharmony_ci if (type != HUB_PORT_STATUS && ext_status) 62662306a36Sopenharmony_ci *ext_status = le32_to_cpu( 62762306a36Sopenharmony_ci hub->status->port.dwExtPortStatus); 62862306a36Sopenharmony_ci ret = 0; 62962306a36Sopenharmony_ci } 63062306a36Sopenharmony_ci mutex_unlock(&hub->status_mutex); 63162306a36Sopenharmony_ci return ret; 63262306a36Sopenharmony_ci} 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ciint usb_hub_port_status(struct usb_hub *hub, int port1, 63562306a36Sopenharmony_ci u16 *status, u16 *change) 63662306a36Sopenharmony_ci{ 63762306a36Sopenharmony_ci return hub_ext_port_status(hub, port1, HUB_PORT_STATUS, 63862306a36Sopenharmony_ci status, change, NULL); 63962306a36Sopenharmony_ci} 64062306a36Sopenharmony_ci 64162306a36Sopenharmony_cistatic void hub_resubmit_irq_urb(struct usb_hub *hub) 64262306a36Sopenharmony_ci{ 64362306a36Sopenharmony_ci unsigned long flags; 64462306a36Sopenharmony_ci int status; 64562306a36Sopenharmony_ci 64662306a36Sopenharmony_ci spin_lock_irqsave(&hub->irq_urb_lock, flags); 64762306a36Sopenharmony_ci 64862306a36Sopenharmony_ci if (hub->quiescing) { 64962306a36Sopenharmony_ci spin_unlock_irqrestore(&hub->irq_urb_lock, flags); 65062306a36Sopenharmony_ci return; 65162306a36Sopenharmony_ci } 65262306a36Sopenharmony_ci 65362306a36Sopenharmony_ci status = usb_submit_urb(hub->urb, GFP_ATOMIC); 65462306a36Sopenharmony_ci if (status && status != -ENODEV && status != -EPERM && 65562306a36Sopenharmony_ci status != -ESHUTDOWN) { 65662306a36Sopenharmony_ci dev_err(hub->intfdev, "resubmit --> %d\n", status); 65762306a36Sopenharmony_ci mod_timer(&hub->irq_urb_retry, jiffies + HZ); 65862306a36Sopenharmony_ci } 65962306a36Sopenharmony_ci 66062306a36Sopenharmony_ci spin_unlock_irqrestore(&hub->irq_urb_lock, flags); 66162306a36Sopenharmony_ci} 66262306a36Sopenharmony_ci 66362306a36Sopenharmony_cistatic void hub_retry_irq_urb(struct timer_list *t) 66462306a36Sopenharmony_ci{ 66562306a36Sopenharmony_ci struct usb_hub *hub = from_timer(hub, t, irq_urb_retry); 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci hub_resubmit_irq_urb(hub); 66862306a36Sopenharmony_ci} 66962306a36Sopenharmony_ci 67062306a36Sopenharmony_ci 67162306a36Sopenharmony_cistatic void kick_hub_wq(struct usb_hub *hub) 67262306a36Sopenharmony_ci{ 67362306a36Sopenharmony_ci struct usb_interface *intf; 67462306a36Sopenharmony_ci 67562306a36Sopenharmony_ci if (hub->disconnected || work_pending(&hub->events)) 67662306a36Sopenharmony_ci return; 67762306a36Sopenharmony_ci 67862306a36Sopenharmony_ci /* 67962306a36Sopenharmony_ci * Suppress autosuspend until the event is proceed. 68062306a36Sopenharmony_ci * 68162306a36Sopenharmony_ci * Be careful and make sure that the symmetric operation is 68262306a36Sopenharmony_ci * always called. We are here only when there is no pending 68362306a36Sopenharmony_ci * work for this hub. Therefore put the interface either when 68462306a36Sopenharmony_ci * the new work is called or when it is canceled. 68562306a36Sopenharmony_ci */ 68662306a36Sopenharmony_ci intf = to_usb_interface(hub->intfdev); 68762306a36Sopenharmony_ci usb_autopm_get_interface_no_resume(intf); 68862306a36Sopenharmony_ci kref_get(&hub->kref); 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci if (queue_work(hub_wq, &hub->events)) 69162306a36Sopenharmony_ci return; 69262306a36Sopenharmony_ci 69362306a36Sopenharmony_ci /* the work has already been scheduled */ 69462306a36Sopenharmony_ci usb_autopm_put_interface_async(intf); 69562306a36Sopenharmony_ci kref_put(&hub->kref, hub_release); 69662306a36Sopenharmony_ci} 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_civoid usb_kick_hub_wq(struct usb_device *hdev) 69962306a36Sopenharmony_ci{ 70062306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 70162306a36Sopenharmony_ci 70262306a36Sopenharmony_ci if (hub) 70362306a36Sopenharmony_ci kick_hub_wq(hub); 70462306a36Sopenharmony_ci} 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci/* 70762306a36Sopenharmony_ci * Let the USB core know that a USB 3.0 device has sent a Function Wake Device 70862306a36Sopenharmony_ci * Notification, which indicates it had initiated remote wakeup. 70962306a36Sopenharmony_ci * 71062306a36Sopenharmony_ci * USB 3.0 hubs do not report the port link state change from U3 to U0 when the 71162306a36Sopenharmony_ci * device initiates resume, so the USB core will not receive notice of the 71262306a36Sopenharmony_ci * resume through the normal hub interrupt URB. 71362306a36Sopenharmony_ci */ 71462306a36Sopenharmony_civoid usb_wakeup_notification(struct usb_device *hdev, 71562306a36Sopenharmony_ci unsigned int portnum) 71662306a36Sopenharmony_ci{ 71762306a36Sopenharmony_ci struct usb_hub *hub; 71862306a36Sopenharmony_ci struct usb_port *port_dev; 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_ci if (!hdev) 72162306a36Sopenharmony_ci return; 72262306a36Sopenharmony_ci 72362306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(hdev); 72462306a36Sopenharmony_ci if (hub) { 72562306a36Sopenharmony_ci port_dev = hub->ports[portnum - 1]; 72662306a36Sopenharmony_ci if (port_dev && port_dev->child) 72762306a36Sopenharmony_ci pm_wakeup_event(&port_dev->child->dev, 0); 72862306a36Sopenharmony_ci 72962306a36Sopenharmony_ci set_bit(portnum, hub->wakeup_bits); 73062306a36Sopenharmony_ci kick_hub_wq(hub); 73162306a36Sopenharmony_ci } 73262306a36Sopenharmony_ci} 73362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_wakeup_notification); 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_ci/* completion function, fires on port status changes and various faults */ 73662306a36Sopenharmony_cistatic void hub_irq(struct urb *urb) 73762306a36Sopenharmony_ci{ 73862306a36Sopenharmony_ci struct usb_hub *hub = urb->context; 73962306a36Sopenharmony_ci int status = urb->status; 74062306a36Sopenharmony_ci unsigned i; 74162306a36Sopenharmony_ci unsigned long bits; 74262306a36Sopenharmony_ci 74362306a36Sopenharmony_ci switch (status) { 74462306a36Sopenharmony_ci case -ENOENT: /* synchronous unlink */ 74562306a36Sopenharmony_ci case -ECONNRESET: /* async unlink */ 74662306a36Sopenharmony_ci case -ESHUTDOWN: /* hardware going away */ 74762306a36Sopenharmony_ci return; 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci default: /* presumably an error */ 75062306a36Sopenharmony_ci /* Cause a hub reset after 10 consecutive errors */ 75162306a36Sopenharmony_ci dev_dbg(hub->intfdev, "transfer --> %d\n", status); 75262306a36Sopenharmony_ci if ((++hub->nerrors < 10) || hub->error) 75362306a36Sopenharmony_ci goto resubmit; 75462306a36Sopenharmony_ci hub->error = status; 75562306a36Sopenharmony_ci fallthrough; 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci /* let hub_wq handle things */ 75862306a36Sopenharmony_ci case 0: /* we got data: port status changed */ 75962306a36Sopenharmony_ci bits = 0; 76062306a36Sopenharmony_ci for (i = 0; i < urb->actual_length; ++i) 76162306a36Sopenharmony_ci bits |= ((unsigned long) ((*hub->buffer)[i])) 76262306a36Sopenharmony_ci << (i*8); 76362306a36Sopenharmony_ci hub->event_bits[0] = bits; 76462306a36Sopenharmony_ci break; 76562306a36Sopenharmony_ci } 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_ci hub->nerrors = 0; 76862306a36Sopenharmony_ci 76962306a36Sopenharmony_ci /* Something happened, let hub_wq figure it out */ 77062306a36Sopenharmony_ci kick_hub_wq(hub); 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ciresubmit: 77362306a36Sopenharmony_ci hub_resubmit_irq_urb(hub); 77462306a36Sopenharmony_ci} 77562306a36Sopenharmony_ci 77662306a36Sopenharmony_ci/* USB 2.0 spec Section 11.24.2.3 */ 77762306a36Sopenharmony_cistatic inline int 77862306a36Sopenharmony_cihub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt) 77962306a36Sopenharmony_ci{ 78062306a36Sopenharmony_ci /* Need to clear both directions for control ep */ 78162306a36Sopenharmony_ci if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) == 78262306a36Sopenharmony_ci USB_ENDPOINT_XFER_CONTROL) { 78362306a36Sopenharmony_ci int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 78462306a36Sopenharmony_ci HUB_CLEAR_TT_BUFFER, USB_RT_PORT, 78562306a36Sopenharmony_ci devinfo ^ 0x8000, tt, NULL, 0, 1000); 78662306a36Sopenharmony_ci if (status) 78762306a36Sopenharmony_ci return status; 78862306a36Sopenharmony_ci } 78962306a36Sopenharmony_ci return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 79062306a36Sopenharmony_ci HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 79162306a36Sopenharmony_ci tt, NULL, 0, 1000); 79262306a36Sopenharmony_ci} 79362306a36Sopenharmony_ci 79462306a36Sopenharmony_ci/* 79562306a36Sopenharmony_ci * enumeration blocks hub_wq for a long time. we use keventd instead, since 79662306a36Sopenharmony_ci * long blocking there is the exception, not the rule. accordingly, HCDs 79762306a36Sopenharmony_ci * talking to TTs must queue control transfers (not just bulk and iso), so 79862306a36Sopenharmony_ci * both can talk to the same hub concurrently. 79962306a36Sopenharmony_ci */ 80062306a36Sopenharmony_cistatic void hub_tt_work(struct work_struct *work) 80162306a36Sopenharmony_ci{ 80262306a36Sopenharmony_ci struct usb_hub *hub = 80362306a36Sopenharmony_ci container_of(work, struct usb_hub, tt.clear_work); 80462306a36Sopenharmony_ci unsigned long flags; 80562306a36Sopenharmony_ci 80662306a36Sopenharmony_ci spin_lock_irqsave(&hub->tt.lock, flags); 80762306a36Sopenharmony_ci while (!list_empty(&hub->tt.clear_list)) { 80862306a36Sopenharmony_ci struct list_head *next; 80962306a36Sopenharmony_ci struct usb_tt_clear *clear; 81062306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 81162306a36Sopenharmony_ci const struct hc_driver *drv; 81262306a36Sopenharmony_ci int status; 81362306a36Sopenharmony_ci 81462306a36Sopenharmony_ci next = hub->tt.clear_list.next; 81562306a36Sopenharmony_ci clear = list_entry(next, struct usb_tt_clear, clear_list); 81662306a36Sopenharmony_ci list_del(&clear->clear_list); 81762306a36Sopenharmony_ci 81862306a36Sopenharmony_ci /* drop lock so HCD can concurrently report other TT errors */ 81962306a36Sopenharmony_ci spin_unlock_irqrestore(&hub->tt.lock, flags); 82062306a36Sopenharmony_ci status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt); 82162306a36Sopenharmony_ci if (status && status != -ENODEV) 82262306a36Sopenharmony_ci dev_err(&hdev->dev, 82362306a36Sopenharmony_ci "clear tt %d (%04x) error %d\n", 82462306a36Sopenharmony_ci clear->tt, clear->devinfo, status); 82562306a36Sopenharmony_ci 82662306a36Sopenharmony_ci /* Tell the HCD, even if the operation failed */ 82762306a36Sopenharmony_ci drv = clear->hcd->driver; 82862306a36Sopenharmony_ci if (drv->clear_tt_buffer_complete) 82962306a36Sopenharmony_ci (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci kfree(clear); 83262306a36Sopenharmony_ci spin_lock_irqsave(&hub->tt.lock, flags); 83362306a36Sopenharmony_ci } 83462306a36Sopenharmony_ci spin_unlock_irqrestore(&hub->tt.lock, flags); 83562306a36Sopenharmony_ci} 83662306a36Sopenharmony_ci 83762306a36Sopenharmony_ci/** 83862306a36Sopenharmony_ci * usb_hub_set_port_power - control hub port's power state 83962306a36Sopenharmony_ci * @hdev: USB device belonging to the usb hub 84062306a36Sopenharmony_ci * @hub: target hub 84162306a36Sopenharmony_ci * @port1: port index 84262306a36Sopenharmony_ci * @set: expected status 84362306a36Sopenharmony_ci * 84462306a36Sopenharmony_ci * call this function to control port's power via setting or 84562306a36Sopenharmony_ci * clearing the port's PORT_POWER feature. 84662306a36Sopenharmony_ci * 84762306a36Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 84862306a36Sopenharmony_ci */ 84962306a36Sopenharmony_ciint usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, 85062306a36Sopenharmony_ci int port1, bool set) 85162306a36Sopenharmony_ci{ 85262306a36Sopenharmony_ci int ret; 85362306a36Sopenharmony_ci 85462306a36Sopenharmony_ci if (set) 85562306a36Sopenharmony_ci ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 85662306a36Sopenharmony_ci else 85762306a36Sopenharmony_ci ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 85862306a36Sopenharmony_ci 85962306a36Sopenharmony_ci if (ret) 86062306a36Sopenharmony_ci return ret; 86162306a36Sopenharmony_ci 86262306a36Sopenharmony_ci if (set) 86362306a36Sopenharmony_ci set_bit(port1, hub->power_bits); 86462306a36Sopenharmony_ci else 86562306a36Sopenharmony_ci clear_bit(port1, hub->power_bits); 86662306a36Sopenharmony_ci return 0; 86762306a36Sopenharmony_ci} 86862306a36Sopenharmony_ci 86962306a36Sopenharmony_ci/** 87062306a36Sopenharmony_ci * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub 87162306a36Sopenharmony_ci * @urb: an URB associated with the failed or incomplete split transaction 87262306a36Sopenharmony_ci * 87362306a36Sopenharmony_ci * High speed HCDs use this to tell the hub driver that some split control or 87462306a36Sopenharmony_ci * bulk transaction failed in a way that requires clearing internal state of 87562306a36Sopenharmony_ci * a transaction translator. This is normally detected (and reported) from 87662306a36Sopenharmony_ci * interrupt context. 87762306a36Sopenharmony_ci * 87862306a36Sopenharmony_ci * It may not be possible for that hub to handle additional full (or low) 87962306a36Sopenharmony_ci * speed transactions until that state is fully cleared out. 88062306a36Sopenharmony_ci * 88162306a36Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 88262306a36Sopenharmony_ci */ 88362306a36Sopenharmony_ciint usb_hub_clear_tt_buffer(struct urb *urb) 88462306a36Sopenharmony_ci{ 88562306a36Sopenharmony_ci struct usb_device *udev = urb->dev; 88662306a36Sopenharmony_ci int pipe = urb->pipe; 88762306a36Sopenharmony_ci struct usb_tt *tt = udev->tt; 88862306a36Sopenharmony_ci unsigned long flags; 88962306a36Sopenharmony_ci struct usb_tt_clear *clear; 89062306a36Sopenharmony_ci 89162306a36Sopenharmony_ci /* we've got to cope with an arbitrary number of pending TT clears, 89262306a36Sopenharmony_ci * since each TT has "at least two" buffers that can need it (and 89362306a36Sopenharmony_ci * there can be many TTs per hub). even if they're uncommon. 89462306a36Sopenharmony_ci */ 89562306a36Sopenharmony_ci clear = kmalloc(sizeof *clear, GFP_ATOMIC); 89662306a36Sopenharmony_ci if (clear == NULL) { 89762306a36Sopenharmony_ci dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 89862306a36Sopenharmony_ci /* FIXME recover somehow ... RESET_TT? */ 89962306a36Sopenharmony_ci return -ENOMEM; 90062306a36Sopenharmony_ci } 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_ci /* info that CLEAR_TT_BUFFER needs */ 90362306a36Sopenharmony_ci clear->tt = tt->multi ? udev->ttport : 1; 90462306a36Sopenharmony_ci clear->devinfo = usb_pipeendpoint (pipe); 90562306a36Sopenharmony_ci clear->devinfo |= ((u16)udev->devaddr) << 4; 90662306a36Sopenharmony_ci clear->devinfo |= usb_pipecontrol(pipe) 90762306a36Sopenharmony_ci ? (USB_ENDPOINT_XFER_CONTROL << 11) 90862306a36Sopenharmony_ci : (USB_ENDPOINT_XFER_BULK << 11); 90962306a36Sopenharmony_ci if (usb_pipein(pipe)) 91062306a36Sopenharmony_ci clear->devinfo |= 1 << 15; 91162306a36Sopenharmony_ci 91262306a36Sopenharmony_ci /* info for completion callback */ 91362306a36Sopenharmony_ci clear->hcd = bus_to_hcd(udev->bus); 91462306a36Sopenharmony_ci clear->ep = urb->ep; 91562306a36Sopenharmony_ci 91662306a36Sopenharmony_ci /* tell keventd to clear state for this TT */ 91762306a36Sopenharmony_ci spin_lock_irqsave(&tt->lock, flags); 91862306a36Sopenharmony_ci list_add_tail(&clear->clear_list, &tt->clear_list); 91962306a36Sopenharmony_ci schedule_work(&tt->clear_work); 92062306a36Sopenharmony_ci spin_unlock_irqrestore(&tt->lock, flags); 92162306a36Sopenharmony_ci return 0; 92262306a36Sopenharmony_ci} 92362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); 92462306a36Sopenharmony_ci 92562306a36Sopenharmony_cistatic void hub_power_on(struct usb_hub *hub, bool do_delay) 92662306a36Sopenharmony_ci{ 92762306a36Sopenharmony_ci int port1; 92862306a36Sopenharmony_ci 92962306a36Sopenharmony_ci /* Enable power on each port. Some hubs have reserved values 93062306a36Sopenharmony_ci * of LPSM (> 2) in their descriptors, even though they are 93162306a36Sopenharmony_ci * USB 2.0 hubs. Some hubs do not implement port-power switching 93262306a36Sopenharmony_ci * but only emulate it. In all cases, the ports won't work 93362306a36Sopenharmony_ci * unless we send these messages to the hub. 93462306a36Sopenharmony_ci */ 93562306a36Sopenharmony_ci if (hub_is_port_power_switchable(hub)) 93662306a36Sopenharmony_ci dev_dbg(hub->intfdev, "enabling power on all ports\n"); 93762306a36Sopenharmony_ci else 93862306a36Sopenharmony_ci dev_dbg(hub->intfdev, "trying to enable port power on " 93962306a36Sopenharmony_ci "non-switchable hub\n"); 94062306a36Sopenharmony_ci for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) 94162306a36Sopenharmony_ci if (test_bit(port1, hub->power_bits)) 94262306a36Sopenharmony_ci set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 94362306a36Sopenharmony_ci else 94462306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 94562306a36Sopenharmony_ci USB_PORT_FEAT_POWER); 94662306a36Sopenharmony_ci if (do_delay) 94762306a36Sopenharmony_ci msleep(hub_power_on_good_delay(hub)); 94862306a36Sopenharmony_ci} 94962306a36Sopenharmony_ci 95062306a36Sopenharmony_cistatic int hub_hub_status(struct usb_hub *hub, 95162306a36Sopenharmony_ci u16 *status, u16 *change) 95262306a36Sopenharmony_ci{ 95362306a36Sopenharmony_ci int ret; 95462306a36Sopenharmony_ci 95562306a36Sopenharmony_ci mutex_lock(&hub->status_mutex); 95662306a36Sopenharmony_ci ret = get_hub_status(hub->hdev, &hub->status->hub); 95762306a36Sopenharmony_ci if (ret < 0) { 95862306a36Sopenharmony_ci if (ret != -ENODEV) 95962306a36Sopenharmony_ci dev_err(hub->intfdev, 96062306a36Sopenharmony_ci "%s failed (err = %d)\n", __func__, ret); 96162306a36Sopenharmony_ci } else { 96262306a36Sopenharmony_ci *status = le16_to_cpu(hub->status->hub.wHubStatus); 96362306a36Sopenharmony_ci *change = le16_to_cpu(hub->status->hub.wHubChange); 96462306a36Sopenharmony_ci ret = 0; 96562306a36Sopenharmony_ci } 96662306a36Sopenharmony_ci mutex_unlock(&hub->status_mutex); 96762306a36Sopenharmony_ci return ret; 96862306a36Sopenharmony_ci} 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_cistatic int hub_set_port_link_state(struct usb_hub *hub, int port1, 97162306a36Sopenharmony_ci unsigned int link_status) 97262306a36Sopenharmony_ci{ 97362306a36Sopenharmony_ci return set_port_feature(hub->hdev, 97462306a36Sopenharmony_ci port1 | (link_status << 3), 97562306a36Sopenharmony_ci USB_PORT_FEAT_LINK_STATE); 97662306a36Sopenharmony_ci} 97762306a36Sopenharmony_ci 97862306a36Sopenharmony_ci/* 97962306a36Sopenharmony_ci * Disable a port and mark a logical connect-change event, so that some 98062306a36Sopenharmony_ci * time later hub_wq will disconnect() any existing usb_device on the port 98162306a36Sopenharmony_ci * and will re-enumerate if there actually is a device attached. 98262306a36Sopenharmony_ci */ 98362306a36Sopenharmony_cistatic void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 98462306a36Sopenharmony_ci{ 98562306a36Sopenharmony_ci dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n"); 98662306a36Sopenharmony_ci hub_port_disable(hub, port1, 1); 98762306a36Sopenharmony_ci 98862306a36Sopenharmony_ci /* FIXME let caller ask to power down the port: 98962306a36Sopenharmony_ci * - some devices won't enumerate without a VBUS power cycle 99062306a36Sopenharmony_ci * - SRP saves power that way 99162306a36Sopenharmony_ci * - ... new call, TBD ... 99262306a36Sopenharmony_ci * That's easy if this hub can switch power per-port, and 99362306a36Sopenharmony_ci * hub_wq reactivates the port later (timer, SRP, etc). 99462306a36Sopenharmony_ci * Powerdown must be optional, because of reset/DFU. 99562306a36Sopenharmony_ci */ 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ci set_bit(port1, hub->change_bits); 99862306a36Sopenharmony_ci kick_hub_wq(hub); 99962306a36Sopenharmony_ci} 100062306a36Sopenharmony_ci 100162306a36Sopenharmony_ci/** 100262306a36Sopenharmony_ci * usb_remove_device - disable a device's port on its parent hub 100362306a36Sopenharmony_ci * @udev: device to be disabled and removed 100462306a36Sopenharmony_ci * Context: @udev locked, must be able to sleep. 100562306a36Sopenharmony_ci * 100662306a36Sopenharmony_ci * After @udev's port has been disabled, hub_wq is notified and it will 100762306a36Sopenharmony_ci * see that the device has been disconnected. When the device is 100862306a36Sopenharmony_ci * physically unplugged and something is plugged in, the events will 100962306a36Sopenharmony_ci * be received and processed normally. 101062306a36Sopenharmony_ci * 101162306a36Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 101262306a36Sopenharmony_ci */ 101362306a36Sopenharmony_ciint usb_remove_device(struct usb_device *udev) 101462306a36Sopenharmony_ci{ 101562306a36Sopenharmony_ci struct usb_hub *hub; 101662306a36Sopenharmony_ci struct usb_interface *intf; 101762306a36Sopenharmony_ci int ret; 101862306a36Sopenharmony_ci 101962306a36Sopenharmony_ci if (!udev->parent) /* Can't remove a root hub */ 102062306a36Sopenharmony_ci return -EINVAL; 102162306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 102262306a36Sopenharmony_ci intf = to_usb_interface(hub->intfdev); 102362306a36Sopenharmony_ci 102462306a36Sopenharmony_ci ret = usb_autopm_get_interface(intf); 102562306a36Sopenharmony_ci if (ret < 0) 102662306a36Sopenharmony_ci return ret; 102762306a36Sopenharmony_ci 102862306a36Sopenharmony_ci set_bit(udev->portnum, hub->removed_bits); 102962306a36Sopenharmony_ci hub_port_logical_disconnect(hub, udev->portnum); 103062306a36Sopenharmony_ci usb_autopm_put_interface(intf); 103162306a36Sopenharmony_ci return 0; 103262306a36Sopenharmony_ci} 103362306a36Sopenharmony_ci 103462306a36Sopenharmony_cienum hub_activation_type { 103562306a36Sopenharmony_ci HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ 103662306a36Sopenharmony_ci HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 103762306a36Sopenharmony_ci}; 103862306a36Sopenharmony_ci 103962306a36Sopenharmony_cistatic void hub_init_func2(struct work_struct *ws); 104062306a36Sopenharmony_cistatic void hub_init_func3(struct work_struct *ws); 104162306a36Sopenharmony_ci 104262306a36Sopenharmony_cistatic void hub_activate(struct usb_hub *hub, enum hub_activation_type type) 104362306a36Sopenharmony_ci{ 104462306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 104562306a36Sopenharmony_ci struct usb_hcd *hcd; 104662306a36Sopenharmony_ci int ret; 104762306a36Sopenharmony_ci int port1; 104862306a36Sopenharmony_ci int status; 104962306a36Sopenharmony_ci bool need_debounce_delay = false; 105062306a36Sopenharmony_ci unsigned delay; 105162306a36Sopenharmony_ci 105262306a36Sopenharmony_ci /* Continue a partial initialization */ 105362306a36Sopenharmony_ci if (type == HUB_INIT2 || type == HUB_INIT3) { 105462306a36Sopenharmony_ci device_lock(&hdev->dev); 105562306a36Sopenharmony_ci 105662306a36Sopenharmony_ci /* Was the hub disconnected while we were waiting? */ 105762306a36Sopenharmony_ci if (hub->disconnected) 105862306a36Sopenharmony_ci goto disconnected; 105962306a36Sopenharmony_ci if (type == HUB_INIT2) 106062306a36Sopenharmony_ci goto init2; 106162306a36Sopenharmony_ci goto init3; 106262306a36Sopenharmony_ci } 106362306a36Sopenharmony_ci kref_get(&hub->kref); 106462306a36Sopenharmony_ci 106562306a36Sopenharmony_ci /* The superspeed hub except for root hub has to use Hub Depth 106662306a36Sopenharmony_ci * value as an offset into the route string to locate the bits 106762306a36Sopenharmony_ci * it uses to determine the downstream port number. So hub driver 106862306a36Sopenharmony_ci * should send a set hub depth request to superspeed hub after 106962306a36Sopenharmony_ci * the superspeed hub is set configuration in initialization or 107062306a36Sopenharmony_ci * reset procedure. 107162306a36Sopenharmony_ci * 107262306a36Sopenharmony_ci * After a resume, port power should still be on. 107362306a36Sopenharmony_ci * For any other type of activation, turn it on. 107462306a36Sopenharmony_ci */ 107562306a36Sopenharmony_ci if (type != HUB_RESUME) { 107662306a36Sopenharmony_ci if (hdev->parent && hub_is_superspeed(hdev)) { 107762306a36Sopenharmony_ci ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 107862306a36Sopenharmony_ci HUB_SET_DEPTH, USB_RT_HUB, 107962306a36Sopenharmony_ci hdev->level - 1, 0, NULL, 0, 108062306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 108162306a36Sopenharmony_ci if (ret < 0) 108262306a36Sopenharmony_ci dev_err(hub->intfdev, 108362306a36Sopenharmony_ci "set hub depth failed\n"); 108462306a36Sopenharmony_ci } 108562306a36Sopenharmony_ci 108662306a36Sopenharmony_ci /* Speed up system boot by using a delayed_work for the 108762306a36Sopenharmony_ci * hub's initial power-up delays. This is pretty awkward 108862306a36Sopenharmony_ci * and the implementation looks like a home-brewed sort of 108962306a36Sopenharmony_ci * setjmp/longjmp, but it saves at least 100 ms for each 109062306a36Sopenharmony_ci * root hub (assuming usbcore is compiled into the kernel 109162306a36Sopenharmony_ci * rather than as a module). It adds up. 109262306a36Sopenharmony_ci * 109362306a36Sopenharmony_ci * This can't be done for HUB_RESUME or HUB_RESET_RESUME 109462306a36Sopenharmony_ci * because for those activation types the ports have to be 109562306a36Sopenharmony_ci * operational when we return. In theory this could be done 109662306a36Sopenharmony_ci * for HUB_POST_RESET, but it's easier not to. 109762306a36Sopenharmony_ci */ 109862306a36Sopenharmony_ci if (type == HUB_INIT) { 109962306a36Sopenharmony_ci delay = hub_power_on_good_delay(hub); 110062306a36Sopenharmony_ci 110162306a36Sopenharmony_ci hub_power_on(hub, false); 110262306a36Sopenharmony_ci INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); 110362306a36Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 110462306a36Sopenharmony_ci &hub->init_work, 110562306a36Sopenharmony_ci msecs_to_jiffies(delay)); 110662306a36Sopenharmony_ci 110762306a36Sopenharmony_ci /* Suppress autosuspend until init is done */ 110862306a36Sopenharmony_ci usb_autopm_get_interface_no_resume( 110962306a36Sopenharmony_ci to_usb_interface(hub->intfdev)); 111062306a36Sopenharmony_ci return; /* Continues at init2: below */ 111162306a36Sopenharmony_ci } else if (type == HUB_RESET_RESUME) { 111262306a36Sopenharmony_ci /* The internal host controller state for the hub device 111362306a36Sopenharmony_ci * may be gone after a host power loss on system resume. 111462306a36Sopenharmony_ci * Update the device's info so the HW knows it's a hub. 111562306a36Sopenharmony_ci */ 111662306a36Sopenharmony_ci hcd = bus_to_hcd(hdev->bus); 111762306a36Sopenharmony_ci if (hcd->driver->update_hub_device) { 111862306a36Sopenharmony_ci ret = hcd->driver->update_hub_device(hcd, hdev, 111962306a36Sopenharmony_ci &hub->tt, GFP_NOIO); 112062306a36Sopenharmony_ci if (ret < 0) { 112162306a36Sopenharmony_ci dev_err(hub->intfdev, 112262306a36Sopenharmony_ci "Host not accepting hub info update\n"); 112362306a36Sopenharmony_ci dev_err(hub->intfdev, 112462306a36Sopenharmony_ci "LS/FS devices and hubs may not work under this hub\n"); 112562306a36Sopenharmony_ci } 112662306a36Sopenharmony_ci } 112762306a36Sopenharmony_ci hub_power_on(hub, true); 112862306a36Sopenharmony_ci } else { 112962306a36Sopenharmony_ci hub_power_on(hub, true); 113062306a36Sopenharmony_ci } 113162306a36Sopenharmony_ci /* Give some time on remote wakeup to let links to transit to U0 */ 113262306a36Sopenharmony_ci } else if (hub_is_superspeed(hub->hdev)) 113362306a36Sopenharmony_ci msleep(20); 113462306a36Sopenharmony_ci 113562306a36Sopenharmony_ci init2: 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_ci /* 113862306a36Sopenharmony_ci * Check each port and set hub->change_bits to let hub_wq know 113962306a36Sopenharmony_ci * which ports need attention. 114062306a36Sopenharmony_ci */ 114162306a36Sopenharmony_ci for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 114262306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 114362306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 114462306a36Sopenharmony_ci u16 portstatus, portchange; 114562306a36Sopenharmony_ci 114662306a36Sopenharmony_ci portstatus = portchange = 0; 114762306a36Sopenharmony_ci status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 114862306a36Sopenharmony_ci if (status) 114962306a36Sopenharmony_ci goto abort; 115062306a36Sopenharmony_ci 115162306a36Sopenharmony_ci if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 115262306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "status %04x change %04x\n", 115362306a36Sopenharmony_ci portstatus, portchange); 115462306a36Sopenharmony_ci 115562306a36Sopenharmony_ci /* 115662306a36Sopenharmony_ci * After anything other than HUB_RESUME (i.e., initialization 115762306a36Sopenharmony_ci * or any sort of reset), every port should be disabled. 115862306a36Sopenharmony_ci * Unconnected ports should likewise be disabled (paranoia), 115962306a36Sopenharmony_ci * and so should ports for which we have no usb_device. 116062306a36Sopenharmony_ci */ 116162306a36Sopenharmony_ci if ((portstatus & USB_PORT_STAT_ENABLE) && ( 116262306a36Sopenharmony_ci type != HUB_RESUME || 116362306a36Sopenharmony_ci !(portstatus & USB_PORT_STAT_CONNECTION) || 116462306a36Sopenharmony_ci !udev || 116562306a36Sopenharmony_ci udev->state == USB_STATE_NOTATTACHED)) { 116662306a36Sopenharmony_ci /* 116762306a36Sopenharmony_ci * USB3 protocol ports will automatically transition 116862306a36Sopenharmony_ci * to Enabled state when detect an USB3.0 device attach. 116962306a36Sopenharmony_ci * Do not disable USB3 protocol ports, just pretend 117062306a36Sopenharmony_ci * power was lost 117162306a36Sopenharmony_ci */ 117262306a36Sopenharmony_ci portstatus &= ~USB_PORT_STAT_ENABLE; 117362306a36Sopenharmony_ci if (!hub_is_superspeed(hdev)) 117462306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, 117562306a36Sopenharmony_ci USB_PORT_FEAT_ENABLE); 117662306a36Sopenharmony_ci } 117762306a36Sopenharmony_ci 117862306a36Sopenharmony_ci /* Make sure a warm-reset request is handled by port_event */ 117962306a36Sopenharmony_ci if (type == HUB_RESUME && 118062306a36Sopenharmony_ci hub_port_warm_reset_required(hub, port1, portstatus)) 118162306a36Sopenharmony_ci set_bit(port1, hub->event_bits); 118262306a36Sopenharmony_ci 118362306a36Sopenharmony_ci /* 118462306a36Sopenharmony_ci * Add debounce if USB3 link is in polling/link training state. 118562306a36Sopenharmony_ci * Link will automatically transition to Enabled state after 118662306a36Sopenharmony_ci * link training completes. 118762306a36Sopenharmony_ci */ 118862306a36Sopenharmony_ci if (hub_is_superspeed(hdev) && 118962306a36Sopenharmony_ci ((portstatus & USB_PORT_STAT_LINK_STATE) == 119062306a36Sopenharmony_ci USB_SS_PORT_LS_POLLING)) 119162306a36Sopenharmony_ci need_debounce_delay = true; 119262306a36Sopenharmony_ci 119362306a36Sopenharmony_ci /* Clear status-change flags; we'll debounce later */ 119462306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_CONNECTION) { 119562306a36Sopenharmony_ci need_debounce_delay = true; 119662306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 119762306a36Sopenharmony_ci USB_PORT_FEAT_C_CONNECTION); 119862306a36Sopenharmony_ci } 119962306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_ENABLE) { 120062306a36Sopenharmony_ci need_debounce_delay = true; 120162306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 120262306a36Sopenharmony_ci USB_PORT_FEAT_C_ENABLE); 120362306a36Sopenharmony_ci } 120462306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_RESET) { 120562306a36Sopenharmony_ci need_debounce_delay = true; 120662306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 120762306a36Sopenharmony_ci USB_PORT_FEAT_C_RESET); 120862306a36Sopenharmony_ci } 120962306a36Sopenharmony_ci if ((portchange & USB_PORT_STAT_C_BH_RESET) && 121062306a36Sopenharmony_ci hub_is_superspeed(hub->hdev)) { 121162306a36Sopenharmony_ci need_debounce_delay = true; 121262306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 121362306a36Sopenharmony_ci USB_PORT_FEAT_C_BH_PORT_RESET); 121462306a36Sopenharmony_ci } 121562306a36Sopenharmony_ci /* We can forget about a "removed" device when there's a 121662306a36Sopenharmony_ci * physical disconnect or the connect status changes. 121762306a36Sopenharmony_ci */ 121862306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_CONNECTION) || 121962306a36Sopenharmony_ci (portchange & USB_PORT_STAT_C_CONNECTION)) 122062306a36Sopenharmony_ci clear_bit(port1, hub->removed_bits); 122162306a36Sopenharmony_ci 122262306a36Sopenharmony_ci if (!udev || udev->state == USB_STATE_NOTATTACHED) { 122362306a36Sopenharmony_ci /* Tell hub_wq to disconnect the device or 122462306a36Sopenharmony_ci * check for a new connection or over current condition. 122562306a36Sopenharmony_ci * Based on USB2.0 Spec Section 11.12.5, 122662306a36Sopenharmony_ci * C_PORT_OVER_CURRENT could be set while 122762306a36Sopenharmony_ci * PORT_OVER_CURRENT is not. So check for any of them. 122862306a36Sopenharmony_ci */ 122962306a36Sopenharmony_ci if (udev || (portstatus & USB_PORT_STAT_CONNECTION) || 123062306a36Sopenharmony_ci (portchange & USB_PORT_STAT_C_CONNECTION) || 123162306a36Sopenharmony_ci (portstatus & USB_PORT_STAT_OVERCURRENT) || 123262306a36Sopenharmony_ci (portchange & USB_PORT_STAT_C_OVERCURRENT)) 123362306a36Sopenharmony_ci set_bit(port1, hub->change_bits); 123462306a36Sopenharmony_ci 123562306a36Sopenharmony_ci } else if (portstatus & USB_PORT_STAT_ENABLE) { 123662306a36Sopenharmony_ci bool port_resumed = (portstatus & 123762306a36Sopenharmony_ci USB_PORT_STAT_LINK_STATE) == 123862306a36Sopenharmony_ci USB_SS_PORT_LS_U0; 123962306a36Sopenharmony_ci /* The power session apparently survived the resume. 124062306a36Sopenharmony_ci * If there was an overcurrent or suspend change 124162306a36Sopenharmony_ci * (i.e., remote wakeup request), have hub_wq 124262306a36Sopenharmony_ci * take care of it. Look at the port link state 124362306a36Sopenharmony_ci * for USB 3.0 hubs, since they don't have a suspend 124462306a36Sopenharmony_ci * change bit, and they don't set the port link change 124562306a36Sopenharmony_ci * bit on device-initiated resume. 124662306a36Sopenharmony_ci */ 124762306a36Sopenharmony_ci if (portchange || (hub_is_superspeed(hub->hdev) && 124862306a36Sopenharmony_ci port_resumed)) 124962306a36Sopenharmony_ci set_bit(port1, hub->event_bits); 125062306a36Sopenharmony_ci 125162306a36Sopenharmony_ci } else if (udev->persist_enabled) { 125262306a36Sopenharmony_ci#ifdef CONFIG_PM 125362306a36Sopenharmony_ci udev->reset_resume = 1; 125462306a36Sopenharmony_ci#endif 125562306a36Sopenharmony_ci /* Don't set the change_bits when the device 125662306a36Sopenharmony_ci * was powered off. 125762306a36Sopenharmony_ci */ 125862306a36Sopenharmony_ci if (test_bit(port1, hub->power_bits)) 125962306a36Sopenharmony_ci set_bit(port1, hub->change_bits); 126062306a36Sopenharmony_ci 126162306a36Sopenharmony_ci } else { 126262306a36Sopenharmony_ci /* The power session is gone; tell hub_wq */ 126362306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_NOTATTACHED); 126462306a36Sopenharmony_ci set_bit(port1, hub->change_bits); 126562306a36Sopenharmony_ci } 126662306a36Sopenharmony_ci } 126762306a36Sopenharmony_ci 126862306a36Sopenharmony_ci /* If no port-status-change flags were set, we don't need any 126962306a36Sopenharmony_ci * debouncing. If flags were set we can try to debounce the 127062306a36Sopenharmony_ci * ports all at once right now, instead of letting hub_wq do them 127162306a36Sopenharmony_ci * one at a time later on. 127262306a36Sopenharmony_ci * 127362306a36Sopenharmony_ci * If any port-status changes do occur during this delay, hub_wq 127462306a36Sopenharmony_ci * will see them later and handle them normally. 127562306a36Sopenharmony_ci */ 127662306a36Sopenharmony_ci if (need_debounce_delay) { 127762306a36Sopenharmony_ci delay = HUB_DEBOUNCE_STABLE; 127862306a36Sopenharmony_ci 127962306a36Sopenharmony_ci /* Don't do a long sleep inside a workqueue routine */ 128062306a36Sopenharmony_ci if (type == HUB_INIT2) { 128162306a36Sopenharmony_ci INIT_DELAYED_WORK(&hub->init_work, hub_init_func3); 128262306a36Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 128362306a36Sopenharmony_ci &hub->init_work, 128462306a36Sopenharmony_ci msecs_to_jiffies(delay)); 128562306a36Sopenharmony_ci device_unlock(&hdev->dev); 128662306a36Sopenharmony_ci return; /* Continues at init3: below */ 128762306a36Sopenharmony_ci } else { 128862306a36Sopenharmony_ci msleep(delay); 128962306a36Sopenharmony_ci } 129062306a36Sopenharmony_ci } 129162306a36Sopenharmony_ci init3: 129262306a36Sopenharmony_ci hub->quiescing = 0; 129362306a36Sopenharmony_ci 129462306a36Sopenharmony_ci status = usb_submit_urb(hub->urb, GFP_NOIO); 129562306a36Sopenharmony_ci if (status < 0) 129662306a36Sopenharmony_ci dev_err(hub->intfdev, "activate --> %d\n", status); 129762306a36Sopenharmony_ci if (hub->has_indicators && blinkenlights) 129862306a36Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 129962306a36Sopenharmony_ci &hub->leds, LED_CYCLE_PERIOD); 130062306a36Sopenharmony_ci 130162306a36Sopenharmony_ci /* Scan all ports that need attention */ 130262306a36Sopenharmony_ci kick_hub_wq(hub); 130362306a36Sopenharmony_ci abort: 130462306a36Sopenharmony_ci if (type == HUB_INIT2 || type == HUB_INIT3) { 130562306a36Sopenharmony_ci /* Allow autosuspend if it was suppressed */ 130662306a36Sopenharmony_ci disconnected: 130762306a36Sopenharmony_ci usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); 130862306a36Sopenharmony_ci device_unlock(&hdev->dev); 130962306a36Sopenharmony_ci } 131062306a36Sopenharmony_ci 131162306a36Sopenharmony_ci kref_put(&hub->kref, hub_release); 131262306a36Sopenharmony_ci} 131362306a36Sopenharmony_ci 131462306a36Sopenharmony_ci/* Implement the continuations for the delays above */ 131562306a36Sopenharmony_cistatic void hub_init_func2(struct work_struct *ws) 131662306a36Sopenharmony_ci{ 131762306a36Sopenharmony_ci struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 131862306a36Sopenharmony_ci 131962306a36Sopenharmony_ci hub_activate(hub, HUB_INIT2); 132062306a36Sopenharmony_ci} 132162306a36Sopenharmony_ci 132262306a36Sopenharmony_cistatic void hub_init_func3(struct work_struct *ws) 132362306a36Sopenharmony_ci{ 132462306a36Sopenharmony_ci struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 132562306a36Sopenharmony_ci 132662306a36Sopenharmony_ci hub_activate(hub, HUB_INIT3); 132762306a36Sopenharmony_ci} 132862306a36Sopenharmony_ci 132962306a36Sopenharmony_cienum hub_quiescing_type { 133062306a36Sopenharmony_ci HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND 133162306a36Sopenharmony_ci}; 133262306a36Sopenharmony_ci 133362306a36Sopenharmony_cistatic void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) 133462306a36Sopenharmony_ci{ 133562306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 133662306a36Sopenharmony_ci unsigned long flags; 133762306a36Sopenharmony_ci int i; 133862306a36Sopenharmony_ci 133962306a36Sopenharmony_ci /* hub_wq and related activity won't re-trigger */ 134062306a36Sopenharmony_ci spin_lock_irqsave(&hub->irq_urb_lock, flags); 134162306a36Sopenharmony_ci hub->quiescing = 1; 134262306a36Sopenharmony_ci spin_unlock_irqrestore(&hub->irq_urb_lock, flags); 134362306a36Sopenharmony_ci 134462306a36Sopenharmony_ci if (type != HUB_SUSPEND) { 134562306a36Sopenharmony_ci /* Disconnect all the children */ 134662306a36Sopenharmony_ci for (i = 0; i < hdev->maxchild; ++i) { 134762306a36Sopenharmony_ci if (hub->ports[i]->child) 134862306a36Sopenharmony_ci usb_disconnect(&hub->ports[i]->child); 134962306a36Sopenharmony_ci } 135062306a36Sopenharmony_ci } 135162306a36Sopenharmony_ci 135262306a36Sopenharmony_ci /* Stop hub_wq and related activity */ 135362306a36Sopenharmony_ci del_timer_sync(&hub->irq_urb_retry); 135462306a36Sopenharmony_ci usb_kill_urb(hub->urb); 135562306a36Sopenharmony_ci if (hub->has_indicators) 135662306a36Sopenharmony_ci cancel_delayed_work_sync(&hub->leds); 135762306a36Sopenharmony_ci if (hub->tt.hub) 135862306a36Sopenharmony_ci flush_work(&hub->tt.clear_work); 135962306a36Sopenharmony_ci} 136062306a36Sopenharmony_ci 136162306a36Sopenharmony_cistatic void hub_pm_barrier_for_all_ports(struct usb_hub *hub) 136262306a36Sopenharmony_ci{ 136362306a36Sopenharmony_ci int i; 136462306a36Sopenharmony_ci 136562306a36Sopenharmony_ci for (i = 0; i < hub->hdev->maxchild; ++i) 136662306a36Sopenharmony_ci pm_runtime_barrier(&hub->ports[i]->dev); 136762306a36Sopenharmony_ci} 136862306a36Sopenharmony_ci 136962306a36Sopenharmony_ci/* caller has locked the hub device */ 137062306a36Sopenharmony_cistatic int hub_pre_reset(struct usb_interface *intf) 137162306a36Sopenharmony_ci{ 137262306a36Sopenharmony_ci struct usb_hub *hub = usb_get_intfdata(intf); 137362306a36Sopenharmony_ci 137462306a36Sopenharmony_ci hub_quiesce(hub, HUB_PRE_RESET); 137562306a36Sopenharmony_ci hub->in_reset = 1; 137662306a36Sopenharmony_ci hub_pm_barrier_for_all_ports(hub); 137762306a36Sopenharmony_ci return 0; 137862306a36Sopenharmony_ci} 137962306a36Sopenharmony_ci 138062306a36Sopenharmony_ci/* caller has locked the hub device */ 138162306a36Sopenharmony_cistatic int hub_post_reset(struct usb_interface *intf) 138262306a36Sopenharmony_ci{ 138362306a36Sopenharmony_ci struct usb_hub *hub = usb_get_intfdata(intf); 138462306a36Sopenharmony_ci 138562306a36Sopenharmony_ci hub->in_reset = 0; 138662306a36Sopenharmony_ci hub_pm_barrier_for_all_ports(hub); 138762306a36Sopenharmony_ci hub_activate(hub, HUB_POST_RESET); 138862306a36Sopenharmony_ci return 0; 138962306a36Sopenharmony_ci} 139062306a36Sopenharmony_ci 139162306a36Sopenharmony_cistatic int hub_configure(struct usb_hub *hub, 139262306a36Sopenharmony_ci struct usb_endpoint_descriptor *endpoint) 139362306a36Sopenharmony_ci{ 139462306a36Sopenharmony_ci struct usb_hcd *hcd; 139562306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 139662306a36Sopenharmony_ci struct device *hub_dev = hub->intfdev; 139762306a36Sopenharmony_ci u16 hubstatus, hubchange; 139862306a36Sopenharmony_ci u16 wHubCharacteristics; 139962306a36Sopenharmony_ci unsigned int pipe; 140062306a36Sopenharmony_ci int maxp, ret, i; 140162306a36Sopenharmony_ci char *message = "out of memory"; 140262306a36Sopenharmony_ci unsigned unit_load; 140362306a36Sopenharmony_ci unsigned full_load; 140462306a36Sopenharmony_ci unsigned maxchild; 140562306a36Sopenharmony_ci 140662306a36Sopenharmony_ci hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); 140762306a36Sopenharmony_ci if (!hub->buffer) { 140862306a36Sopenharmony_ci ret = -ENOMEM; 140962306a36Sopenharmony_ci goto fail; 141062306a36Sopenharmony_ci } 141162306a36Sopenharmony_ci 141262306a36Sopenharmony_ci hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 141362306a36Sopenharmony_ci if (!hub->status) { 141462306a36Sopenharmony_ci ret = -ENOMEM; 141562306a36Sopenharmony_ci goto fail; 141662306a36Sopenharmony_ci } 141762306a36Sopenharmony_ci mutex_init(&hub->status_mutex); 141862306a36Sopenharmony_ci 141962306a36Sopenharmony_ci hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL); 142062306a36Sopenharmony_ci if (!hub->descriptor) { 142162306a36Sopenharmony_ci ret = -ENOMEM; 142262306a36Sopenharmony_ci goto fail; 142362306a36Sopenharmony_ci } 142462306a36Sopenharmony_ci 142562306a36Sopenharmony_ci /* Request the entire hub descriptor. 142662306a36Sopenharmony_ci * hub->descriptor can handle USB_MAXCHILDREN ports, 142762306a36Sopenharmony_ci * but a (non-SS) hub can/will return fewer bytes here. 142862306a36Sopenharmony_ci */ 142962306a36Sopenharmony_ci ret = get_hub_descriptor(hdev, hub->descriptor); 143062306a36Sopenharmony_ci if (ret < 0) { 143162306a36Sopenharmony_ci message = "can't read hub descriptor"; 143262306a36Sopenharmony_ci goto fail; 143362306a36Sopenharmony_ci } 143462306a36Sopenharmony_ci 143562306a36Sopenharmony_ci maxchild = USB_MAXCHILDREN; 143662306a36Sopenharmony_ci if (hub_is_superspeed(hdev)) 143762306a36Sopenharmony_ci maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS); 143862306a36Sopenharmony_ci 143962306a36Sopenharmony_ci if (hub->descriptor->bNbrPorts > maxchild) { 144062306a36Sopenharmony_ci message = "hub has too many ports!"; 144162306a36Sopenharmony_ci ret = -ENODEV; 144262306a36Sopenharmony_ci goto fail; 144362306a36Sopenharmony_ci } else if (hub->descriptor->bNbrPorts == 0) { 144462306a36Sopenharmony_ci message = "hub doesn't have any ports!"; 144562306a36Sopenharmony_ci ret = -ENODEV; 144662306a36Sopenharmony_ci goto fail; 144762306a36Sopenharmony_ci } 144862306a36Sopenharmony_ci 144962306a36Sopenharmony_ci /* 145062306a36Sopenharmony_ci * Accumulate wHubDelay + 40ns for every hub in the tree of devices. 145162306a36Sopenharmony_ci * The resulting value will be used for SetIsochDelay() request. 145262306a36Sopenharmony_ci */ 145362306a36Sopenharmony_ci if (hub_is_superspeed(hdev) || hub_is_superspeedplus(hdev)) { 145462306a36Sopenharmony_ci u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay); 145562306a36Sopenharmony_ci 145662306a36Sopenharmony_ci if (hdev->parent) 145762306a36Sopenharmony_ci delay += hdev->parent->hub_delay; 145862306a36Sopenharmony_ci 145962306a36Sopenharmony_ci delay += USB_TP_TRANSMISSION_DELAY; 146062306a36Sopenharmony_ci hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX); 146162306a36Sopenharmony_ci } 146262306a36Sopenharmony_ci 146362306a36Sopenharmony_ci maxchild = hub->descriptor->bNbrPorts; 146462306a36Sopenharmony_ci dev_info(hub_dev, "%d port%s detected\n", maxchild, 146562306a36Sopenharmony_ci (maxchild == 1) ? "" : "s"); 146662306a36Sopenharmony_ci 146762306a36Sopenharmony_ci hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL); 146862306a36Sopenharmony_ci if (!hub->ports) { 146962306a36Sopenharmony_ci ret = -ENOMEM; 147062306a36Sopenharmony_ci goto fail; 147162306a36Sopenharmony_ci } 147262306a36Sopenharmony_ci 147362306a36Sopenharmony_ci wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 147462306a36Sopenharmony_ci if (hub_is_superspeed(hdev)) { 147562306a36Sopenharmony_ci unit_load = 150; 147662306a36Sopenharmony_ci full_load = 900; 147762306a36Sopenharmony_ci } else { 147862306a36Sopenharmony_ci unit_load = 100; 147962306a36Sopenharmony_ci full_load = 500; 148062306a36Sopenharmony_ci } 148162306a36Sopenharmony_ci 148262306a36Sopenharmony_ci /* FIXME for USB 3.0, skip for now */ 148362306a36Sopenharmony_ci if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && 148462306a36Sopenharmony_ci !(hub_is_superspeed(hdev))) { 148562306a36Sopenharmony_ci char portstr[USB_MAXCHILDREN + 1]; 148662306a36Sopenharmony_ci 148762306a36Sopenharmony_ci for (i = 0; i < maxchild; i++) 148862306a36Sopenharmony_ci portstr[i] = hub->descriptor->u.hs.DeviceRemovable 148962306a36Sopenharmony_ci [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 149062306a36Sopenharmony_ci ? 'F' : 'R'; 149162306a36Sopenharmony_ci portstr[maxchild] = 0; 149262306a36Sopenharmony_ci dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 149362306a36Sopenharmony_ci } else 149462306a36Sopenharmony_ci dev_dbg(hub_dev, "standalone hub\n"); 149562306a36Sopenharmony_ci 149662306a36Sopenharmony_ci switch (wHubCharacteristics & HUB_CHAR_LPSM) { 149762306a36Sopenharmony_ci case HUB_CHAR_COMMON_LPSM: 149862306a36Sopenharmony_ci dev_dbg(hub_dev, "ganged power switching\n"); 149962306a36Sopenharmony_ci break; 150062306a36Sopenharmony_ci case HUB_CHAR_INDV_PORT_LPSM: 150162306a36Sopenharmony_ci dev_dbg(hub_dev, "individual port power switching\n"); 150262306a36Sopenharmony_ci break; 150362306a36Sopenharmony_ci case HUB_CHAR_NO_LPSM: 150462306a36Sopenharmony_ci case HUB_CHAR_LPSM: 150562306a36Sopenharmony_ci dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 150662306a36Sopenharmony_ci break; 150762306a36Sopenharmony_ci } 150862306a36Sopenharmony_ci 150962306a36Sopenharmony_ci switch (wHubCharacteristics & HUB_CHAR_OCPM) { 151062306a36Sopenharmony_ci case HUB_CHAR_COMMON_OCPM: 151162306a36Sopenharmony_ci dev_dbg(hub_dev, "global over-current protection\n"); 151262306a36Sopenharmony_ci break; 151362306a36Sopenharmony_ci case HUB_CHAR_INDV_PORT_OCPM: 151462306a36Sopenharmony_ci dev_dbg(hub_dev, "individual port over-current protection\n"); 151562306a36Sopenharmony_ci break; 151662306a36Sopenharmony_ci case HUB_CHAR_NO_OCPM: 151762306a36Sopenharmony_ci case HUB_CHAR_OCPM: 151862306a36Sopenharmony_ci dev_dbg(hub_dev, "no over-current protection\n"); 151962306a36Sopenharmony_ci break; 152062306a36Sopenharmony_ci } 152162306a36Sopenharmony_ci 152262306a36Sopenharmony_ci spin_lock_init(&hub->tt.lock); 152362306a36Sopenharmony_ci INIT_LIST_HEAD(&hub->tt.clear_list); 152462306a36Sopenharmony_ci INIT_WORK(&hub->tt.clear_work, hub_tt_work); 152562306a36Sopenharmony_ci switch (hdev->descriptor.bDeviceProtocol) { 152662306a36Sopenharmony_ci case USB_HUB_PR_FS: 152762306a36Sopenharmony_ci break; 152862306a36Sopenharmony_ci case USB_HUB_PR_HS_SINGLE_TT: 152962306a36Sopenharmony_ci dev_dbg(hub_dev, "Single TT\n"); 153062306a36Sopenharmony_ci hub->tt.hub = hdev; 153162306a36Sopenharmony_ci break; 153262306a36Sopenharmony_ci case USB_HUB_PR_HS_MULTI_TT: 153362306a36Sopenharmony_ci ret = usb_set_interface(hdev, 0, 1); 153462306a36Sopenharmony_ci if (ret == 0) { 153562306a36Sopenharmony_ci dev_dbg(hub_dev, "TT per port\n"); 153662306a36Sopenharmony_ci hub->tt.multi = 1; 153762306a36Sopenharmony_ci } else 153862306a36Sopenharmony_ci dev_err(hub_dev, "Using single TT (err %d)\n", 153962306a36Sopenharmony_ci ret); 154062306a36Sopenharmony_ci hub->tt.hub = hdev; 154162306a36Sopenharmony_ci break; 154262306a36Sopenharmony_ci case USB_HUB_PR_SS: 154362306a36Sopenharmony_ci /* USB 3.0 hubs don't have a TT */ 154462306a36Sopenharmony_ci break; 154562306a36Sopenharmony_ci default: 154662306a36Sopenharmony_ci dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 154762306a36Sopenharmony_ci hdev->descriptor.bDeviceProtocol); 154862306a36Sopenharmony_ci break; 154962306a36Sopenharmony_ci } 155062306a36Sopenharmony_ci 155162306a36Sopenharmony_ci /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 155262306a36Sopenharmony_ci switch (wHubCharacteristics & HUB_CHAR_TTTT) { 155362306a36Sopenharmony_ci case HUB_TTTT_8_BITS: 155462306a36Sopenharmony_ci if (hdev->descriptor.bDeviceProtocol != 0) { 155562306a36Sopenharmony_ci hub->tt.think_time = 666; 155662306a36Sopenharmony_ci dev_dbg(hub_dev, "TT requires at most %d " 155762306a36Sopenharmony_ci "FS bit times (%d ns)\n", 155862306a36Sopenharmony_ci 8, hub->tt.think_time); 155962306a36Sopenharmony_ci } 156062306a36Sopenharmony_ci break; 156162306a36Sopenharmony_ci case HUB_TTTT_16_BITS: 156262306a36Sopenharmony_ci hub->tt.think_time = 666 * 2; 156362306a36Sopenharmony_ci dev_dbg(hub_dev, "TT requires at most %d " 156462306a36Sopenharmony_ci "FS bit times (%d ns)\n", 156562306a36Sopenharmony_ci 16, hub->tt.think_time); 156662306a36Sopenharmony_ci break; 156762306a36Sopenharmony_ci case HUB_TTTT_24_BITS: 156862306a36Sopenharmony_ci hub->tt.think_time = 666 * 3; 156962306a36Sopenharmony_ci dev_dbg(hub_dev, "TT requires at most %d " 157062306a36Sopenharmony_ci "FS bit times (%d ns)\n", 157162306a36Sopenharmony_ci 24, hub->tt.think_time); 157262306a36Sopenharmony_ci break; 157362306a36Sopenharmony_ci case HUB_TTTT_32_BITS: 157462306a36Sopenharmony_ci hub->tt.think_time = 666 * 4; 157562306a36Sopenharmony_ci dev_dbg(hub_dev, "TT requires at most %d " 157662306a36Sopenharmony_ci "FS bit times (%d ns)\n", 157762306a36Sopenharmony_ci 32, hub->tt.think_time); 157862306a36Sopenharmony_ci break; 157962306a36Sopenharmony_ci } 158062306a36Sopenharmony_ci 158162306a36Sopenharmony_ci /* probe() zeroes hub->indicator[] */ 158262306a36Sopenharmony_ci if (wHubCharacteristics & HUB_CHAR_PORTIND) { 158362306a36Sopenharmony_ci hub->has_indicators = 1; 158462306a36Sopenharmony_ci dev_dbg(hub_dev, "Port indicators are supported\n"); 158562306a36Sopenharmony_ci } 158662306a36Sopenharmony_ci 158762306a36Sopenharmony_ci dev_dbg(hub_dev, "power on to power good time: %dms\n", 158862306a36Sopenharmony_ci hub->descriptor->bPwrOn2PwrGood * 2); 158962306a36Sopenharmony_ci 159062306a36Sopenharmony_ci /* power budgeting mostly matters with bus-powered hubs, 159162306a36Sopenharmony_ci * and battery-powered root hubs (may provide just 8 mA). 159262306a36Sopenharmony_ci */ 159362306a36Sopenharmony_ci ret = usb_get_std_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 159462306a36Sopenharmony_ci if (ret) { 159562306a36Sopenharmony_ci message = "can't get hub status"; 159662306a36Sopenharmony_ci goto fail; 159762306a36Sopenharmony_ci } 159862306a36Sopenharmony_ci hcd = bus_to_hcd(hdev->bus); 159962306a36Sopenharmony_ci if (hdev == hdev->bus->root_hub) { 160062306a36Sopenharmony_ci if (hcd->power_budget > 0) 160162306a36Sopenharmony_ci hdev->bus_mA = hcd->power_budget; 160262306a36Sopenharmony_ci else 160362306a36Sopenharmony_ci hdev->bus_mA = full_load * maxchild; 160462306a36Sopenharmony_ci if (hdev->bus_mA >= full_load) 160562306a36Sopenharmony_ci hub->mA_per_port = full_load; 160662306a36Sopenharmony_ci else { 160762306a36Sopenharmony_ci hub->mA_per_port = hdev->bus_mA; 160862306a36Sopenharmony_ci hub->limited_power = 1; 160962306a36Sopenharmony_ci } 161062306a36Sopenharmony_ci } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 161162306a36Sopenharmony_ci int remaining = hdev->bus_mA - 161262306a36Sopenharmony_ci hub->descriptor->bHubContrCurrent; 161362306a36Sopenharmony_ci 161462306a36Sopenharmony_ci dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 161562306a36Sopenharmony_ci hub->descriptor->bHubContrCurrent); 161662306a36Sopenharmony_ci hub->limited_power = 1; 161762306a36Sopenharmony_ci 161862306a36Sopenharmony_ci if (remaining < maxchild * unit_load) 161962306a36Sopenharmony_ci dev_warn(hub_dev, 162062306a36Sopenharmony_ci "insufficient power available " 162162306a36Sopenharmony_ci "to use all downstream ports\n"); 162262306a36Sopenharmony_ci hub->mA_per_port = unit_load; /* 7.2.1 */ 162362306a36Sopenharmony_ci 162462306a36Sopenharmony_ci } else { /* Self-powered external hub */ 162562306a36Sopenharmony_ci /* FIXME: What about battery-powered external hubs that 162662306a36Sopenharmony_ci * provide less current per port? */ 162762306a36Sopenharmony_ci hub->mA_per_port = full_load; 162862306a36Sopenharmony_ci } 162962306a36Sopenharmony_ci if (hub->mA_per_port < full_load) 163062306a36Sopenharmony_ci dev_dbg(hub_dev, "%umA bus power budget for each child\n", 163162306a36Sopenharmony_ci hub->mA_per_port); 163262306a36Sopenharmony_ci 163362306a36Sopenharmony_ci ret = hub_hub_status(hub, &hubstatus, &hubchange); 163462306a36Sopenharmony_ci if (ret < 0) { 163562306a36Sopenharmony_ci message = "can't get hub status"; 163662306a36Sopenharmony_ci goto fail; 163762306a36Sopenharmony_ci } 163862306a36Sopenharmony_ci 163962306a36Sopenharmony_ci /* local power status reports aren't always correct */ 164062306a36Sopenharmony_ci if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 164162306a36Sopenharmony_ci dev_dbg(hub_dev, "local power source is %s\n", 164262306a36Sopenharmony_ci (hubstatus & HUB_STATUS_LOCAL_POWER) 164362306a36Sopenharmony_ci ? "lost (inactive)" : "good"); 164462306a36Sopenharmony_ci 164562306a36Sopenharmony_ci if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 164662306a36Sopenharmony_ci dev_dbg(hub_dev, "%sover-current condition exists\n", 164762306a36Sopenharmony_ci (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 164862306a36Sopenharmony_ci 164962306a36Sopenharmony_ci /* set up the interrupt endpoint 165062306a36Sopenharmony_ci * We use the EP's maxpacket size instead of (PORTS+1+7)/8 165162306a36Sopenharmony_ci * bytes as USB2.0[11.12.3] says because some hubs are known 165262306a36Sopenharmony_ci * to send more data (and thus cause overflow). For root hubs, 165362306a36Sopenharmony_ci * maxpktsize is defined in hcd.c's fake endpoint descriptors 165462306a36Sopenharmony_ci * to be big enough for at least USB_MAXCHILDREN ports. */ 165562306a36Sopenharmony_ci pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 165662306a36Sopenharmony_ci maxp = usb_maxpacket(hdev, pipe); 165762306a36Sopenharmony_ci 165862306a36Sopenharmony_ci if (maxp > sizeof(*hub->buffer)) 165962306a36Sopenharmony_ci maxp = sizeof(*hub->buffer); 166062306a36Sopenharmony_ci 166162306a36Sopenharmony_ci hub->urb = usb_alloc_urb(0, GFP_KERNEL); 166262306a36Sopenharmony_ci if (!hub->urb) { 166362306a36Sopenharmony_ci ret = -ENOMEM; 166462306a36Sopenharmony_ci goto fail; 166562306a36Sopenharmony_ci } 166662306a36Sopenharmony_ci 166762306a36Sopenharmony_ci usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 166862306a36Sopenharmony_ci hub, endpoint->bInterval); 166962306a36Sopenharmony_ci 167062306a36Sopenharmony_ci /* maybe cycle the hub leds */ 167162306a36Sopenharmony_ci if (hub->has_indicators && blinkenlights) 167262306a36Sopenharmony_ci hub->indicator[0] = INDICATOR_CYCLE; 167362306a36Sopenharmony_ci 167462306a36Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 167562306a36Sopenharmony_ci for (i = 0; i < maxchild; i++) { 167662306a36Sopenharmony_ci ret = usb_hub_create_port_device(hub, i + 1); 167762306a36Sopenharmony_ci if (ret < 0) { 167862306a36Sopenharmony_ci dev_err(hub->intfdev, 167962306a36Sopenharmony_ci "couldn't create port%d device.\n", i + 1); 168062306a36Sopenharmony_ci break; 168162306a36Sopenharmony_ci } 168262306a36Sopenharmony_ci } 168362306a36Sopenharmony_ci hdev->maxchild = i; 168462306a36Sopenharmony_ci for (i = 0; i < hdev->maxchild; i++) { 168562306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[i]; 168662306a36Sopenharmony_ci 168762306a36Sopenharmony_ci pm_runtime_put(&port_dev->dev); 168862306a36Sopenharmony_ci } 168962306a36Sopenharmony_ci 169062306a36Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 169162306a36Sopenharmony_ci if (ret < 0) 169262306a36Sopenharmony_ci goto fail; 169362306a36Sopenharmony_ci 169462306a36Sopenharmony_ci /* Update the HCD's internal representation of this hub before hub_wq 169562306a36Sopenharmony_ci * starts getting port status changes for devices under the hub. 169662306a36Sopenharmony_ci */ 169762306a36Sopenharmony_ci if (hcd->driver->update_hub_device) { 169862306a36Sopenharmony_ci ret = hcd->driver->update_hub_device(hcd, hdev, 169962306a36Sopenharmony_ci &hub->tt, GFP_KERNEL); 170062306a36Sopenharmony_ci if (ret < 0) { 170162306a36Sopenharmony_ci message = "can't update HCD hub info"; 170262306a36Sopenharmony_ci goto fail; 170362306a36Sopenharmony_ci } 170462306a36Sopenharmony_ci } 170562306a36Sopenharmony_ci 170662306a36Sopenharmony_ci usb_hub_adjust_deviceremovable(hdev, hub->descriptor); 170762306a36Sopenharmony_ci 170862306a36Sopenharmony_ci hub_activate(hub, HUB_INIT); 170962306a36Sopenharmony_ci return 0; 171062306a36Sopenharmony_ci 171162306a36Sopenharmony_cifail: 171262306a36Sopenharmony_ci dev_err(hub_dev, "config failed, %s (err %d)\n", 171362306a36Sopenharmony_ci message, ret); 171462306a36Sopenharmony_ci /* hub_disconnect() frees urb and descriptor */ 171562306a36Sopenharmony_ci return ret; 171662306a36Sopenharmony_ci} 171762306a36Sopenharmony_ci 171862306a36Sopenharmony_cistatic void hub_release(struct kref *kref) 171962306a36Sopenharmony_ci{ 172062306a36Sopenharmony_ci struct usb_hub *hub = container_of(kref, struct usb_hub, kref); 172162306a36Sopenharmony_ci 172262306a36Sopenharmony_ci usb_put_dev(hub->hdev); 172362306a36Sopenharmony_ci usb_put_intf(to_usb_interface(hub->intfdev)); 172462306a36Sopenharmony_ci kfree(hub); 172562306a36Sopenharmony_ci} 172662306a36Sopenharmony_ci 172762306a36Sopenharmony_cistatic unsigned highspeed_hubs; 172862306a36Sopenharmony_ci 172962306a36Sopenharmony_cistatic void hub_disconnect(struct usb_interface *intf) 173062306a36Sopenharmony_ci{ 173162306a36Sopenharmony_ci struct usb_hub *hub = usb_get_intfdata(intf); 173262306a36Sopenharmony_ci struct usb_device *hdev = interface_to_usbdev(intf); 173362306a36Sopenharmony_ci int port1; 173462306a36Sopenharmony_ci 173562306a36Sopenharmony_ci /* 173662306a36Sopenharmony_ci * Stop adding new hub events. We do not want to block here and thus 173762306a36Sopenharmony_ci * will not try to remove any pending work item. 173862306a36Sopenharmony_ci */ 173962306a36Sopenharmony_ci hub->disconnected = 1; 174062306a36Sopenharmony_ci 174162306a36Sopenharmony_ci /* Disconnect all children and quiesce the hub */ 174262306a36Sopenharmony_ci hub->error = 0; 174362306a36Sopenharmony_ci hub_quiesce(hub, HUB_DISCONNECT); 174462306a36Sopenharmony_ci 174562306a36Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 174662306a36Sopenharmony_ci 174762306a36Sopenharmony_ci /* Avoid races with recursively_mark_NOTATTACHED() */ 174862306a36Sopenharmony_ci spin_lock_irq(&device_state_lock); 174962306a36Sopenharmony_ci port1 = hdev->maxchild; 175062306a36Sopenharmony_ci hdev->maxchild = 0; 175162306a36Sopenharmony_ci usb_set_intfdata(intf, NULL); 175262306a36Sopenharmony_ci spin_unlock_irq(&device_state_lock); 175362306a36Sopenharmony_ci 175462306a36Sopenharmony_ci for (; port1 > 0; --port1) 175562306a36Sopenharmony_ci usb_hub_remove_port_device(hub, port1); 175662306a36Sopenharmony_ci 175762306a36Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 175862306a36Sopenharmony_ci 175962306a36Sopenharmony_ci if (hub->hdev->speed == USB_SPEED_HIGH) 176062306a36Sopenharmony_ci highspeed_hubs--; 176162306a36Sopenharmony_ci 176262306a36Sopenharmony_ci usb_free_urb(hub->urb); 176362306a36Sopenharmony_ci kfree(hub->ports); 176462306a36Sopenharmony_ci kfree(hub->descriptor); 176562306a36Sopenharmony_ci kfree(hub->status); 176662306a36Sopenharmony_ci kfree(hub->buffer); 176762306a36Sopenharmony_ci 176862306a36Sopenharmony_ci pm_suspend_ignore_children(&intf->dev, false); 176962306a36Sopenharmony_ci 177062306a36Sopenharmony_ci if (hub->quirk_disable_autosuspend) 177162306a36Sopenharmony_ci usb_autopm_put_interface(intf); 177262306a36Sopenharmony_ci 177362306a36Sopenharmony_ci onboard_hub_destroy_pdevs(&hub->onboard_hub_devs); 177462306a36Sopenharmony_ci 177562306a36Sopenharmony_ci kref_put(&hub->kref, hub_release); 177662306a36Sopenharmony_ci} 177762306a36Sopenharmony_ci 177862306a36Sopenharmony_cistatic bool hub_descriptor_is_sane(struct usb_host_interface *desc) 177962306a36Sopenharmony_ci{ 178062306a36Sopenharmony_ci /* Some hubs have a subclass of 1, which AFAICT according to the */ 178162306a36Sopenharmony_ci /* specs is not defined, but it works */ 178262306a36Sopenharmony_ci if (desc->desc.bInterfaceSubClass != 0 && 178362306a36Sopenharmony_ci desc->desc.bInterfaceSubClass != 1) 178462306a36Sopenharmony_ci return false; 178562306a36Sopenharmony_ci 178662306a36Sopenharmony_ci /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 178762306a36Sopenharmony_ci if (desc->desc.bNumEndpoints != 1) 178862306a36Sopenharmony_ci return false; 178962306a36Sopenharmony_ci 179062306a36Sopenharmony_ci /* If the first endpoint is not interrupt IN, we'd better punt! */ 179162306a36Sopenharmony_ci if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc)) 179262306a36Sopenharmony_ci return false; 179362306a36Sopenharmony_ci 179462306a36Sopenharmony_ci return true; 179562306a36Sopenharmony_ci} 179662306a36Sopenharmony_ci 179762306a36Sopenharmony_cistatic int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 179862306a36Sopenharmony_ci{ 179962306a36Sopenharmony_ci struct usb_host_interface *desc; 180062306a36Sopenharmony_ci struct usb_device *hdev; 180162306a36Sopenharmony_ci struct usb_hub *hub; 180262306a36Sopenharmony_ci 180362306a36Sopenharmony_ci desc = intf->cur_altsetting; 180462306a36Sopenharmony_ci hdev = interface_to_usbdev(intf); 180562306a36Sopenharmony_ci 180662306a36Sopenharmony_ci /* 180762306a36Sopenharmony_ci * Set default autosuspend delay as 0 to speedup bus suspend, 180862306a36Sopenharmony_ci * based on the below considerations: 180962306a36Sopenharmony_ci * 181062306a36Sopenharmony_ci * - Unlike other drivers, the hub driver does not rely on the 181162306a36Sopenharmony_ci * autosuspend delay to provide enough time to handle a wakeup 181262306a36Sopenharmony_ci * event, and the submitted status URB is just to check future 181362306a36Sopenharmony_ci * change on hub downstream ports, so it is safe to do it. 181462306a36Sopenharmony_ci * 181562306a36Sopenharmony_ci * - The patch might cause one or more auto supend/resume for 181662306a36Sopenharmony_ci * below very rare devices when they are plugged into hub 181762306a36Sopenharmony_ci * first time: 181862306a36Sopenharmony_ci * 181962306a36Sopenharmony_ci * devices having trouble initializing, and disconnect 182062306a36Sopenharmony_ci * themselves from the bus and then reconnect a second 182162306a36Sopenharmony_ci * or so later 182262306a36Sopenharmony_ci * 182362306a36Sopenharmony_ci * devices just for downloading firmware, and disconnects 182462306a36Sopenharmony_ci * themselves after completing it 182562306a36Sopenharmony_ci * 182662306a36Sopenharmony_ci * For these quite rare devices, their drivers may change the 182762306a36Sopenharmony_ci * autosuspend delay of their parent hub in the probe() to one 182862306a36Sopenharmony_ci * appropriate value to avoid the subtle problem if someone 182962306a36Sopenharmony_ci * does care it. 183062306a36Sopenharmony_ci * 183162306a36Sopenharmony_ci * - The patch may cause one or more auto suspend/resume on 183262306a36Sopenharmony_ci * hub during running 'lsusb', but it is probably too 183362306a36Sopenharmony_ci * infrequent to worry about. 183462306a36Sopenharmony_ci * 183562306a36Sopenharmony_ci * - Change autosuspend delay of hub can avoid unnecessary auto 183662306a36Sopenharmony_ci * suspend timer for hub, also may decrease power consumption 183762306a36Sopenharmony_ci * of USB bus. 183862306a36Sopenharmony_ci * 183962306a36Sopenharmony_ci * - If user has indicated to prevent autosuspend by passing 184062306a36Sopenharmony_ci * usbcore.autosuspend = -1 then keep autosuspend disabled. 184162306a36Sopenharmony_ci */ 184262306a36Sopenharmony_ci#ifdef CONFIG_PM 184362306a36Sopenharmony_ci if (hdev->dev.power.autosuspend_delay >= 0) 184462306a36Sopenharmony_ci pm_runtime_set_autosuspend_delay(&hdev->dev, 0); 184562306a36Sopenharmony_ci#endif 184662306a36Sopenharmony_ci 184762306a36Sopenharmony_ci /* 184862306a36Sopenharmony_ci * Hubs have proper suspend/resume support, except for root hubs 184962306a36Sopenharmony_ci * where the controller driver doesn't have bus_suspend and 185062306a36Sopenharmony_ci * bus_resume methods. 185162306a36Sopenharmony_ci */ 185262306a36Sopenharmony_ci if (hdev->parent) { /* normal device */ 185362306a36Sopenharmony_ci usb_enable_autosuspend(hdev); 185462306a36Sopenharmony_ci } else { /* root hub */ 185562306a36Sopenharmony_ci const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; 185662306a36Sopenharmony_ci 185762306a36Sopenharmony_ci if (drv->bus_suspend && drv->bus_resume) 185862306a36Sopenharmony_ci usb_enable_autosuspend(hdev); 185962306a36Sopenharmony_ci } 186062306a36Sopenharmony_ci 186162306a36Sopenharmony_ci if (hdev->level == MAX_TOPO_LEVEL) { 186262306a36Sopenharmony_ci dev_err(&intf->dev, 186362306a36Sopenharmony_ci "Unsupported bus topology: hub nested too deep\n"); 186462306a36Sopenharmony_ci return -E2BIG; 186562306a36Sopenharmony_ci } 186662306a36Sopenharmony_ci 186762306a36Sopenharmony_ci#ifdef CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB 186862306a36Sopenharmony_ci if (hdev->parent) { 186962306a36Sopenharmony_ci dev_warn(&intf->dev, "ignoring external hub\n"); 187062306a36Sopenharmony_ci return -ENODEV; 187162306a36Sopenharmony_ci } 187262306a36Sopenharmony_ci#endif 187362306a36Sopenharmony_ci 187462306a36Sopenharmony_ci if (!hub_descriptor_is_sane(desc)) { 187562306a36Sopenharmony_ci dev_err(&intf->dev, "bad descriptor, ignoring hub\n"); 187662306a36Sopenharmony_ci return -EIO; 187762306a36Sopenharmony_ci } 187862306a36Sopenharmony_ci 187962306a36Sopenharmony_ci /* We found a hub */ 188062306a36Sopenharmony_ci dev_info(&intf->dev, "USB hub found\n"); 188162306a36Sopenharmony_ci 188262306a36Sopenharmony_ci hub = kzalloc(sizeof(*hub), GFP_KERNEL); 188362306a36Sopenharmony_ci if (!hub) 188462306a36Sopenharmony_ci return -ENOMEM; 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci kref_init(&hub->kref); 188762306a36Sopenharmony_ci hub->intfdev = &intf->dev; 188862306a36Sopenharmony_ci hub->hdev = hdev; 188962306a36Sopenharmony_ci INIT_DELAYED_WORK(&hub->leds, led_work); 189062306a36Sopenharmony_ci INIT_DELAYED_WORK(&hub->init_work, NULL); 189162306a36Sopenharmony_ci INIT_WORK(&hub->events, hub_event); 189262306a36Sopenharmony_ci INIT_LIST_HEAD(&hub->onboard_hub_devs); 189362306a36Sopenharmony_ci spin_lock_init(&hub->irq_urb_lock); 189462306a36Sopenharmony_ci timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0); 189562306a36Sopenharmony_ci usb_get_intf(intf); 189662306a36Sopenharmony_ci usb_get_dev(hdev); 189762306a36Sopenharmony_ci 189862306a36Sopenharmony_ci usb_set_intfdata(intf, hub); 189962306a36Sopenharmony_ci intf->needs_remote_wakeup = 1; 190062306a36Sopenharmony_ci pm_suspend_ignore_children(&intf->dev, true); 190162306a36Sopenharmony_ci 190262306a36Sopenharmony_ci if (hdev->speed == USB_SPEED_HIGH) 190362306a36Sopenharmony_ci highspeed_hubs++; 190462306a36Sopenharmony_ci 190562306a36Sopenharmony_ci if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) 190662306a36Sopenharmony_ci hub->quirk_check_port_auto_suspend = 1; 190762306a36Sopenharmony_ci 190862306a36Sopenharmony_ci if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) { 190962306a36Sopenharmony_ci hub->quirk_disable_autosuspend = 1; 191062306a36Sopenharmony_ci usb_autopm_get_interface_no_resume(intf); 191162306a36Sopenharmony_ci } 191262306a36Sopenharmony_ci 191362306a36Sopenharmony_ci if ((id->driver_info & HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL) && 191462306a36Sopenharmony_ci desc->endpoint[0].desc.bInterval > USB_REDUCE_FRAME_INTR_BINTERVAL) { 191562306a36Sopenharmony_ci desc->endpoint[0].desc.bInterval = 191662306a36Sopenharmony_ci USB_REDUCE_FRAME_INTR_BINTERVAL; 191762306a36Sopenharmony_ci /* Tell the HCD about the interrupt ep's new bInterval */ 191862306a36Sopenharmony_ci usb_set_interface(hdev, 0, 0); 191962306a36Sopenharmony_ci } 192062306a36Sopenharmony_ci 192162306a36Sopenharmony_ci if (hub_configure(hub, &desc->endpoint[0].desc) >= 0) { 192262306a36Sopenharmony_ci onboard_hub_create_pdevs(hdev, &hub->onboard_hub_devs); 192362306a36Sopenharmony_ci 192462306a36Sopenharmony_ci return 0; 192562306a36Sopenharmony_ci } 192662306a36Sopenharmony_ci 192762306a36Sopenharmony_ci hub_disconnect(intf); 192862306a36Sopenharmony_ci return -ENODEV; 192962306a36Sopenharmony_ci} 193062306a36Sopenharmony_ci 193162306a36Sopenharmony_cistatic int 193262306a36Sopenharmony_cihub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 193362306a36Sopenharmony_ci{ 193462306a36Sopenharmony_ci struct usb_device *hdev = interface_to_usbdev(intf); 193562306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 193662306a36Sopenharmony_ci 193762306a36Sopenharmony_ci /* assert ifno == 0 (part of hub spec) */ 193862306a36Sopenharmony_ci switch (code) { 193962306a36Sopenharmony_ci case USBDEVFS_HUB_PORTINFO: { 194062306a36Sopenharmony_ci struct usbdevfs_hub_portinfo *info = user_data; 194162306a36Sopenharmony_ci int i; 194262306a36Sopenharmony_ci 194362306a36Sopenharmony_ci spin_lock_irq(&device_state_lock); 194462306a36Sopenharmony_ci if (hdev->devnum <= 0) 194562306a36Sopenharmony_ci info->nports = 0; 194662306a36Sopenharmony_ci else { 194762306a36Sopenharmony_ci info->nports = hdev->maxchild; 194862306a36Sopenharmony_ci for (i = 0; i < info->nports; i++) { 194962306a36Sopenharmony_ci if (hub->ports[i]->child == NULL) 195062306a36Sopenharmony_ci info->port[i] = 0; 195162306a36Sopenharmony_ci else 195262306a36Sopenharmony_ci info->port[i] = 195362306a36Sopenharmony_ci hub->ports[i]->child->devnum; 195462306a36Sopenharmony_ci } 195562306a36Sopenharmony_ci } 195662306a36Sopenharmony_ci spin_unlock_irq(&device_state_lock); 195762306a36Sopenharmony_ci 195862306a36Sopenharmony_ci return info->nports + 1; 195962306a36Sopenharmony_ci } 196062306a36Sopenharmony_ci 196162306a36Sopenharmony_ci default: 196262306a36Sopenharmony_ci return -ENOSYS; 196362306a36Sopenharmony_ci } 196462306a36Sopenharmony_ci} 196562306a36Sopenharmony_ci 196662306a36Sopenharmony_ci/* 196762306a36Sopenharmony_ci * Allow user programs to claim ports on a hub. When a device is attached 196862306a36Sopenharmony_ci * to one of these "claimed" ports, the program will "own" the device. 196962306a36Sopenharmony_ci */ 197062306a36Sopenharmony_cistatic int find_port_owner(struct usb_device *hdev, unsigned port1, 197162306a36Sopenharmony_ci struct usb_dev_state ***ppowner) 197262306a36Sopenharmony_ci{ 197362306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 197462306a36Sopenharmony_ci 197562306a36Sopenharmony_ci if (hdev->state == USB_STATE_NOTATTACHED) 197662306a36Sopenharmony_ci return -ENODEV; 197762306a36Sopenharmony_ci if (port1 == 0 || port1 > hdev->maxchild) 197862306a36Sopenharmony_ci return -EINVAL; 197962306a36Sopenharmony_ci 198062306a36Sopenharmony_ci /* Devices not managed by the hub driver 198162306a36Sopenharmony_ci * will always have maxchild equal to 0. 198262306a36Sopenharmony_ci */ 198362306a36Sopenharmony_ci *ppowner = &(hub->ports[port1 - 1]->port_owner); 198462306a36Sopenharmony_ci return 0; 198562306a36Sopenharmony_ci} 198662306a36Sopenharmony_ci 198762306a36Sopenharmony_ci/* In the following three functions, the caller must hold hdev's lock */ 198862306a36Sopenharmony_ciint usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 198962306a36Sopenharmony_ci struct usb_dev_state *owner) 199062306a36Sopenharmony_ci{ 199162306a36Sopenharmony_ci int rc; 199262306a36Sopenharmony_ci struct usb_dev_state **powner; 199362306a36Sopenharmony_ci 199462306a36Sopenharmony_ci rc = find_port_owner(hdev, port1, &powner); 199562306a36Sopenharmony_ci if (rc) 199662306a36Sopenharmony_ci return rc; 199762306a36Sopenharmony_ci if (*powner) 199862306a36Sopenharmony_ci return -EBUSY; 199962306a36Sopenharmony_ci *powner = owner; 200062306a36Sopenharmony_ci return rc; 200162306a36Sopenharmony_ci} 200262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hub_claim_port); 200362306a36Sopenharmony_ci 200462306a36Sopenharmony_ciint usb_hub_release_port(struct usb_device *hdev, unsigned port1, 200562306a36Sopenharmony_ci struct usb_dev_state *owner) 200662306a36Sopenharmony_ci{ 200762306a36Sopenharmony_ci int rc; 200862306a36Sopenharmony_ci struct usb_dev_state **powner; 200962306a36Sopenharmony_ci 201062306a36Sopenharmony_ci rc = find_port_owner(hdev, port1, &powner); 201162306a36Sopenharmony_ci if (rc) 201262306a36Sopenharmony_ci return rc; 201362306a36Sopenharmony_ci if (*powner != owner) 201462306a36Sopenharmony_ci return -ENOENT; 201562306a36Sopenharmony_ci *powner = NULL; 201662306a36Sopenharmony_ci return rc; 201762306a36Sopenharmony_ci} 201862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hub_release_port); 201962306a36Sopenharmony_ci 202062306a36Sopenharmony_civoid usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner) 202162306a36Sopenharmony_ci{ 202262306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 202362306a36Sopenharmony_ci int n; 202462306a36Sopenharmony_ci 202562306a36Sopenharmony_ci for (n = 0; n < hdev->maxchild; n++) { 202662306a36Sopenharmony_ci if (hub->ports[n]->port_owner == owner) 202762306a36Sopenharmony_ci hub->ports[n]->port_owner = NULL; 202862306a36Sopenharmony_ci } 202962306a36Sopenharmony_ci 203062306a36Sopenharmony_ci} 203162306a36Sopenharmony_ci 203262306a36Sopenharmony_ci/* The caller must hold udev's lock */ 203362306a36Sopenharmony_cibool usb_device_is_owned(struct usb_device *udev) 203462306a36Sopenharmony_ci{ 203562306a36Sopenharmony_ci struct usb_hub *hub; 203662306a36Sopenharmony_ci 203762306a36Sopenharmony_ci if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) 203862306a36Sopenharmony_ci return false; 203962306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 204062306a36Sopenharmony_ci return !!hub->ports[udev->portnum - 1]->port_owner; 204162306a36Sopenharmony_ci} 204262306a36Sopenharmony_ci 204362306a36Sopenharmony_cistatic void update_port_device_state(struct usb_device *udev) 204462306a36Sopenharmony_ci{ 204562306a36Sopenharmony_ci struct usb_hub *hub; 204662306a36Sopenharmony_ci struct usb_port *port_dev; 204762306a36Sopenharmony_ci 204862306a36Sopenharmony_ci if (udev->parent) { 204962306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 205062306a36Sopenharmony_ci 205162306a36Sopenharmony_ci /* 205262306a36Sopenharmony_ci * The Link Layer Validation System Driver (lvstest) 205362306a36Sopenharmony_ci * has a test step to unbind the hub before running the 205462306a36Sopenharmony_ci * rest of the procedure. This triggers hub_disconnect 205562306a36Sopenharmony_ci * which will set the hub's maxchild to 0, further 205662306a36Sopenharmony_ci * resulting in usb_hub_to_struct_hub returning NULL. 205762306a36Sopenharmony_ci */ 205862306a36Sopenharmony_ci if (hub) { 205962306a36Sopenharmony_ci port_dev = hub->ports[udev->portnum - 1]; 206062306a36Sopenharmony_ci WRITE_ONCE(port_dev->state, udev->state); 206162306a36Sopenharmony_ci sysfs_notify_dirent(port_dev->state_kn); 206262306a36Sopenharmony_ci } 206362306a36Sopenharmony_ci } 206462306a36Sopenharmony_ci} 206562306a36Sopenharmony_ci 206662306a36Sopenharmony_cistatic void recursively_mark_NOTATTACHED(struct usb_device *udev) 206762306a36Sopenharmony_ci{ 206862306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev); 206962306a36Sopenharmony_ci int i; 207062306a36Sopenharmony_ci 207162306a36Sopenharmony_ci for (i = 0; i < udev->maxchild; ++i) { 207262306a36Sopenharmony_ci if (hub->ports[i]->child) 207362306a36Sopenharmony_ci recursively_mark_NOTATTACHED(hub->ports[i]->child); 207462306a36Sopenharmony_ci } 207562306a36Sopenharmony_ci if (udev->state == USB_STATE_SUSPENDED) 207662306a36Sopenharmony_ci udev->active_duration -= jiffies; 207762306a36Sopenharmony_ci udev->state = USB_STATE_NOTATTACHED; 207862306a36Sopenharmony_ci update_port_device_state(udev); 207962306a36Sopenharmony_ci} 208062306a36Sopenharmony_ci 208162306a36Sopenharmony_ci/** 208262306a36Sopenharmony_ci * usb_set_device_state - change a device's current state (usbcore, hcds) 208362306a36Sopenharmony_ci * @udev: pointer to device whose state should be changed 208462306a36Sopenharmony_ci * @new_state: new state value to be stored 208562306a36Sopenharmony_ci * 208662306a36Sopenharmony_ci * udev->state is _not_ fully protected by the device lock. Although 208762306a36Sopenharmony_ci * most transitions are made only while holding the lock, the state can 208862306a36Sopenharmony_ci * can change to USB_STATE_NOTATTACHED at almost any time. This 208962306a36Sopenharmony_ci * is so that devices can be marked as disconnected as soon as possible, 209062306a36Sopenharmony_ci * without having to wait for any semaphores to be released. As a result, 209162306a36Sopenharmony_ci * all changes to any device's state must be protected by the 209262306a36Sopenharmony_ci * device_state_lock spinlock. 209362306a36Sopenharmony_ci * 209462306a36Sopenharmony_ci * Once a device has been added to the device tree, all changes to its state 209562306a36Sopenharmony_ci * should be made using this routine. The state should _not_ be set directly. 209662306a36Sopenharmony_ci * 209762306a36Sopenharmony_ci * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 209862306a36Sopenharmony_ci * Otherwise udev->state is set to new_state, and if new_state is 209962306a36Sopenharmony_ci * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 210062306a36Sopenharmony_ci * to USB_STATE_NOTATTACHED. 210162306a36Sopenharmony_ci */ 210262306a36Sopenharmony_civoid usb_set_device_state(struct usb_device *udev, 210362306a36Sopenharmony_ci enum usb_device_state new_state) 210462306a36Sopenharmony_ci{ 210562306a36Sopenharmony_ci unsigned long flags; 210662306a36Sopenharmony_ci int wakeup = -1; 210762306a36Sopenharmony_ci 210862306a36Sopenharmony_ci spin_lock_irqsave(&device_state_lock, flags); 210962306a36Sopenharmony_ci if (udev->state == USB_STATE_NOTATTACHED) 211062306a36Sopenharmony_ci ; /* do nothing */ 211162306a36Sopenharmony_ci else if (new_state != USB_STATE_NOTATTACHED) { 211262306a36Sopenharmony_ci 211362306a36Sopenharmony_ci /* root hub wakeup capabilities are managed out-of-band 211462306a36Sopenharmony_ci * and may involve silicon errata ... ignore them here. 211562306a36Sopenharmony_ci */ 211662306a36Sopenharmony_ci if (udev->parent) { 211762306a36Sopenharmony_ci if (udev->state == USB_STATE_SUSPENDED 211862306a36Sopenharmony_ci || new_state == USB_STATE_SUSPENDED) 211962306a36Sopenharmony_ci ; /* No change to wakeup settings */ 212062306a36Sopenharmony_ci else if (new_state == USB_STATE_CONFIGURED) 212162306a36Sopenharmony_ci wakeup = (udev->quirks & 212262306a36Sopenharmony_ci USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 : 212362306a36Sopenharmony_ci udev->actconfig->desc.bmAttributes & 212462306a36Sopenharmony_ci USB_CONFIG_ATT_WAKEUP; 212562306a36Sopenharmony_ci else 212662306a36Sopenharmony_ci wakeup = 0; 212762306a36Sopenharmony_ci } 212862306a36Sopenharmony_ci if (udev->state == USB_STATE_SUSPENDED && 212962306a36Sopenharmony_ci new_state != USB_STATE_SUSPENDED) 213062306a36Sopenharmony_ci udev->active_duration -= jiffies; 213162306a36Sopenharmony_ci else if (new_state == USB_STATE_SUSPENDED && 213262306a36Sopenharmony_ci udev->state != USB_STATE_SUSPENDED) 213362306a36Sopenharmony_ci udev->active_duration += jiffies; 213462306a36Sopenharmony_ci udev->state = new_state; 213562306a36Sopenharmony_ci update_port_device_state(udev); 213662306a36Sopenharmony_ci } else 213762306a36Sopenharmony_ci recursively_mark_NOTATTACHED(udev); 213862306a36Sopenharmony_ci spin_unlock_irqrestore(&device_state_lock, flags); 213962306a36Sopenharmony_ci if (wakeup >= 0) 214062306a36Sopenharmony_ci device_set_wakeup_capable(&udev->dev, wakeup); 214162306a36Sopenharmony_ci} 214262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_set_device_state); 214362306a36Sopenharmony_ci 214462306a36Sopenharmony_ci/* 214562306a36Sopenharmony_ci * Choose a device number. 214662306a36Sopenharmony_ci * 214762306a36Sopenharmony_ci * Device numbers are used as filenames in usbfs. On USB-1.1 and 214862306a36Sopenharmony_ci * USB-2.0 buses they are also used as device addresses, however on 214962306a36Sopenharmony_ci * USB-3.0 buses the address is assigned by the controller hardware 215062306a36Sopenharmony_ci * and it usually is not the same as the device number. 215162306a36Sopenharmony_ci * 215262306a36Sopenharmony_ci * Devices connected under xHCI are not as simple. The host controller 215362306a36Sopenharmony_ci * supports virtualization, so the hardware assigns device addresses and 215462306a36Sopenharmony_ci * the HCD must setup data structures before issuing a set address 215562306a36Sopenharmony_ci * command to the hardware. 215662306a36Sopenharmony_ci */ 215762306a36Sopenharmony_cistatic void choose_devnum(struct usb_device *udev) 215862306a36Sopenharmony_ci{ 215962306a36Sopenharmony_ci int devnum; 216062306a36Sopenharmony_ci struct usb_bus *bus = udev->bus; 216162306a36Sopenharmony_ci 216262306a36Sopenharmony_ci /* be safe when more hub events are proceed in parallel */ 216362306a36Sopenharmony_ci mutex_lock(&bus->devnum_next_mutex); 216462306a36Sopenharmony_ci 216562306a36Sopenharmony_ci /* Try to allocate the next devnum beginning at bus->devnum_next. */ 216662306a36Sopenharmony_ci devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 216762306a36Sopenharmony_ci bus->devnum_next); 216862306a36Sopenharmony_ci if (devnum >= 128) 216962306a36Sopenharmony_ci devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1); 217062306a36Sopenharmony_ci bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); 217162306a36Sopenharmony_ci if (devnum < 128) { 217262306a36Sopenharmony_ci set_bit(devnum, bus->devmap.devicemap); 217362306a36Sopenharmony_ci udev->devnum = devnum; 217462306a36Sopenharmony_ci } 217562306a36Sopenharmony_ci mutex_unlock(&bus->devnum_next_mutex); 217662306a36Sopenharmony_ci} 217762306a36Sopenharmony_ci 217862306a36Sopenharmony_cistatic void release_devnum(struct usb_device *udev) 217962306a36Sopenharmony_ci{ 218062306a36Sopenharmony_ci if (udev->devnum > 0) { 218162306a36Sopenharmony_ci clear_bit(udev->devnum, udev->bus->devmap.devicemap); 218262306a36Sopenharmony_ci udev->devnum = -1; 218362306a36Sopenharmony_ci } 218462306a36Sopenharmony_ci} 218562306a36Sopenharmony_ci 218662306a36Sopenharmony_cistatic void update_devnum(struct usb_device *udev, int devnum) 218762306a36Sopenharmony_ci{ 218862306a36Sopenharmony_ci udev->devnum = devnum; 218962306a36Sopenharmony_ci if (!udev->devaddr) 219062306a36Sopenharmony_ci udev->devaddr = (u8)devnum; 219162306a36Sopenharmony_ci} 219262306a36Sopenharmony_ci 219362306a36Sopenharmony_cistatic void hub_free_dev(struct usb_device *udev) 219462306a36Sopenharmony_ci{ 219562306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 219662306a36Sopenharmony_ci 219762306a36Sopenharmony_ci /* Root hubs aren't real devices, so don't free HCD resources */ 219862306a36Sopenharmony_ci if (hcd->driver->free_dev && udev->parent) 219962306a36Sopenharmony_ci hcd->driver->free_dev(hcd, udev); 220062306a36Sopenharmony_ci} 220162306a36Sopenharmony_ci 220262306a36Sopenharmony_cistatic void hub_disconnect_children(struct usb_device *udev) 220362306a36Sopenharmony_ci{ 220462306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev); 220562306a36Sopenharmony_ci int i; 220662306a36Sopenharmony_ci 220762306a36Sopenharmony_ci /* Free up all the children before we remove this device */ 220862306a36Sopenharmony_ci for (i = 0; i < udev->maxchild; i++) { 220962306a36Sopenharmony_ci if (hub->ports[i]->child) 221062306a36Sopenharmony_ci usb_disconnect(&hub->ports[i]->child); 221162306a36Sopenharmony_ci } 221262306a36Sopenharmony_ci} 221362306a36Sopenharmony_ci 221462306a36Sopenharmony_ci/** 221562306a36Sopenharmony_ci * usb_disconnect - disconnect a device (usbcore-internal) 221662306a36Sopenharmony_ci * @pdev: pointer to device being disconnected 221762306a36Sopenharmony_ci * 221862306a36Sopenharmony_ci * Context: task context, might sleep 221962306a36Sopenharmony_ci * 222062306a36Sopenharmony_ci * Something got disconnected. Get rid of it and all of its children. 222162306a36Sopenharmony_ci * 222262306a36Sopenharmony_ci * If *pdev is a normal device then the parent hub must already be locked. 222362306a36Sopenharmony_ci * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock, 222462306a36Sopenharmony_ci * which protects the set of root hubs as well as the list of buses. 222562306a36Sopenharmony_ci * 222662306a36Sopenharmony_ci * Only hub drivers (including virtual root hub drivers for host 222762306a36Sopenharmony_ci * controllers) should ever call this. 222862306a36Sopenharmony_ci * 222962306a36Sopenharmony_ci * This call is synchronous, and may not be used in an interrupt context. 223062306a36Sopenharmony_ci */ 223162306a36Sopenharmony_civoid usb_disconnect(struct usb_device **pdev) 223262306a36Sopenharmony_ci{ 223362306a36Sopenharmony_ci struct usb_port *port_dev = NULL; 223462306a36Sopenharmony_ci struct usb_device *udev = *pdev; 223562306a36Sopenharmony_ci struct usb_hub *hub = NULL; 223662306a36Sopenharmony_ci int port1 = 1; 223762306a36Sopenharmony_ci 223862306a36Sopenharmony_ci /* mark the device as inactive, so any further urb submissions for 223962306a36Sopenharmony_ci * this device (and any of its children) will fail immediately. 224062306a36Sopenharmony_ci * this quiesces everything except pending urbs. 224162306a36Sopenharmony_ci */ 224262306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_NOTATTACHED); 224362306a36Sopenharmony_ci dev_info(&udev->dev, "USB disconnect, device number %d\n", 224462306a36Sopenharmony_ci udev->devnum); 224562306a36Sopenharmony_ci 224662306a36Sopenharmony_ci /* 224762306a36Sopenharmony_ci * Ensure that the pm runtime code knows that the USB device 224862306a36Sopenharmony_ci * is in the process of being disconnected. 224962306a36Sopenharmony_ci */ 225062306a36Sopenharmony_ci pm_runtime_barrier(&udev->dev); 225162306a36Sopenharmony_ci 225262306a36Sopenharmony_ci usb_lock_device(udev); 225362306a36Sopenharmony_ci 225462306a36Sopenharmony_ci hub_disconnect_children(udev); 225562306a36Sopenharmony_ci 225662306a36Sopenharmony_ci /* deallocate hcd/hardware state ... nuking all pending urbs and 225762306a36Sopenharmony_ci * cleaning up all state associated with the current configuration 225862306a36Sopenharmony_ci * so that the hardware is now fully quiesced. 225962306a36Sopenharmony_ci */ 226062306a36Sopenharmony_ci dev_dbg(&udev->dev, "unregistering device\n"); 226162306a36Sopenharmony_ci usb_disable_device(udev, 0); 226262306a36Sopenharmony_ci usb_hcd_synchronize_unlinks(udev); 226362306a36Sopenharmony_ci 226462306a36Sopenharmony_ci if (udev->parent) { 226562306a36Sopenharmony_ci port1 = udev->portnum; 226662306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 226762306a36Sopenharmony_ci port_dev = hub->ports[port1 - 1]; 226862306a36Sopenharmony_ci 226962306a36Sopenharmony_ci sysfs_remove_link(&udev->dev.kobj, "port"); 227062306a36Sopenharmony_ci sysfs_remove_link(&port_dev->dev.kobj, "device"); 227162306a36Sopenharmony_ci 227262306a36Sopenharmony_ci /* 227362306a36Sopenharmony_ci * As usb_port_runtime_resume() de-references udev, make 227462306a36Sopenharmony_ci * sure no resumes occur during removal 227562306a36Sopenharmony_ci */ 227662306a36Sopenharmony_ci if (!test_and_set_bit(port1, hub->child_usage_bits)) 227762306a36Sopenharmony_ci pm_runtime_get_sync(&port_dev->dev); 227862306a36Sopenharmony_ci } 227962306a36Sopenharmony_ci 228062306a36Sopenharmony_ci usb_remove_ep_devs(&udev->ep0); 228162306a36Sopenharmony_ci usb_unlock_device(udev); 228262306a36Sopenharmony_ci 228362306a36Sopenharmony_ci /* Unregister the device. The device driver is responsible 228462306a36Sopenharmony_ci * for de-configuring the device and invoking the remove-device 228562306a36Sopenharmony_ci * notifier chain (used by usbfs and possibly others). 228662306a36Sopenharmony_ci */ 228762306a36Sopenharmony_ci device_del(&udev->dev); 228862306a36Sopenharmony_ci 228962306a36Sopenharmony_ci /* Free the device number and delete the parent's children[] 229062306a36Sopenharmony_ci * (or root_hub) pointer. 229162306a36Sopenharmony_ci */ 229262306a36Sopenharmony_ci release_devnum(udev); 229362306a36Sopenharmony_ci 229462306a36Sopenharmony_ci /* Avoid races with recursively_mark_NOTATTACHED() */ 229562306a36Sopenharmony_ci spin_lock_irq(&device_state_lock); 229662306a36Sopenharmony_ci *pdev = NULL; 229762306a36Sopenharmony_ci spin_unlock_irq(&device_state_lock); 229862306a36Sopenharmony_ci 229962306a36Sopenharmony_ci if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) 230062306a36Sopenharmony_ci pm_runtime_put(&port_dev->dev); 230162306a36Sopenharmony_ci 230262306a36Sopenharmony_ci hub_free_dev(udev); 230362306a36Sopenharmony_ci 230462306a36Sopenharmony_ci put_device(&udev->dev); 230562306a36Sopenharmony_ci} 230662306a36Sopenharmony_ci 230762306a36Sopenharmony_ci#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 230862306a36Sopenharmony_cistatic void show_string(struct usb_device *udev, char *id, char *string) 230962306a36Sopenharmony_ci{ 231062306a36Sopenharmony_ci if (!string) 231162306a36Sopenharmony_ci return; 231262306a36Sopenharmony_ci dev_info(&udev->dev, "%s: %s\n", id, string); 231362306a36Sopenharmony_ci} 231462306a36Sopenharmony_ci 231562306a36Sopenharmony_cistatic void announce_device(struct usb_device *udev) 231662306a36Sopenharmony_ci{ 231762306a36Sopenharmony_ci u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice); 231862306a36Sopenharmony_ci 231962306a36Sopenharmony_ci dev_info(&udev->dev, 232062306a36Sopenharmony_ci "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n", 232162306a36Sopenharmony_ci le16_to_cpu(udev->descriptor.idVendor), 232262306a36Sopenharmony_ci le16_to_cpu(udev->descriptor.idProduct), 232362306a36Sopenharmony_ci bcdDevice >> 8, bcdDevice & 0xff); 232462306a36Sopenharmony_ci dev_info(&udev->dev, 232562306a36Sopenharmony_ci "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 232662306a36Sopenharmony_ci udev->descriptor.iManufacturer, 232762306a36Sopenharmony_ci udev->descriptor.iProduct, 232862306a36Sopenharmony_ci udev->descriptor.iSerialNumber); 232962306a36Sopenharmony_ci show_string(udev, "Product", udev->product); 233062306a36Sopenharmony_ci show_string(udev, "Manufacturer", udev->manufacturer); 233162306a36Sopenharmony_ci show_string(udev, "SerialNumber", udev->serial); 233262306a36Sopenharmony_ci} 233362306a36Sopenharmony_ci#else 233462306a36Sopenharmony_cistatic inline void announce_device(struct usb_device *udev) { } 233562306a36Sopenharmony_ci#endif 233662306a36Sopenharmony_ci 233762306a36Sopenharmony_ci 233862306a36Sopenharmony_ci/** 233962306a36Sopenharmony_ci * usb_enumerate_device_otg - FIXME (usbcore-internal) 234062306a36Sopenharmony_ci * @udev: newly addressed device (in ADDRESS state) 234162306a36Sopenharmony_ci * 234262306a36Sopenharmony_ci * Finish enumeration for On-The-Go devices 234362306a36Sopenharmony_ci * 234462306a36Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 234562306a36Sopenharmony_ci */ 234662306a36Sopenharmony_cistatic int usb_enumerate_device_otg(struct usb_device *udev) 234762306a36Sopenharmony_ci{ 234862306a36Sopenharmony_ci int err = 0; 234962306a36Sopenharmony_ci 235062306a36Sopenharmony_ci#ifdef CONFIG_USB_OTG 235162306a36Sopenharmony_ci /* 235262306a36Sopenharmony_ci * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 235362306a36Sopenharmony_ci * to wake us after we've powered off VBUS; and HNP, switching roles 235462306a36Sopenharmony_ci * "host" to "peripheral". The OTG descriptor helps figure this out. 235562306a36Sopenharmony_ci */ 235662306a36Sopenharmony_ci if (!udev->bus->is_b_host 235762306a36Sopenharmony_ci && udev->config 235862306a36Sopenharmony_ci && udev->parent == udev->bus->root_hub) { 235962306a36Sopenharmony_ci struct usb_otg_descriptor *desc = NULL; 236062306a36Sopenharmony_ci struct usb_bus *bus = udev->bus; 236162306a36Sopenharmony_ci unsigned port1 = udev->portnum; 236262306a36Sopenharmony_ci 236362306a36Sopenharmony_ci /* descriptor may appear anywhere in config */ 236462306a36Sopenharmony_ci err = __usb_get_extra_descriptor(udev->rawdescriptors[0], 236562306a36Sopenharmony_ci le16_to_cpu(udev->config[0].desc.wTotalLength), 236662306a36Sopenharmony_ci USB_DT_OTG, (void **) &desc, sizeof(*desc)); 236762306a36Sopenharmony_ci if (err || !(desc->bmAttributes & USB_OTG_HNP)) 236862306a36Sopenharmony_ci return 0; 236962306a36Sopenharmony_ci 237062306a36Sopenharmony_ci dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", 237162306a36Sopenharmony_ci (port1 == bus->otg_port) ? "" : "non-"); 237262306a36Sopenharmony_ci 237362306a36Sopenharmony_ci /* enable HNP before suspend, it's simpler */ 237462306a36Sopenharmony_ci if (port1 == bus->otg_port) { 237562306a36Sopenharmony_ci bus->b_hnp_enable = 1; 237662306a36Sopenharmony_ci err = usb_control_msg(udev, 237762306a36Sopenharmony_ci usb_sndctrlpipe(udev, 0), 237862306a36Sopenharmony_ci USB_REQ_SET_FEATURE, 0, 237962306a36Sopenharmony_ci USB_DEVICE_B_HNP_ENABLE, 238062306a36Sopenharmony_ci 0, NULL, 0, 238162306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 238262306a36Sopenharmony_ci if (err < 0) { 238362306a36Sopenharmony_ci /* 238462306a36Sopenharmony_ci * OTG MESSAGE: report errors here, 238562306a36Sopenharmony_ci * customize to match your product. 238662306a36Sopenharmony_ci */ 238762306a36Sopenharmony_ci dev_err(&udev->dev, "can't set HNP mode: %d\n", 238862306a36Sopenharmony_ci err); 238962306a36Sopenharmony_ci bus->b_hnp_enable = 0; 239062306a36Sopenharmony_ci } 239162306a36Sopenharmony_ci } else if (desc->bLength == sizeof 239262306a36Sopenharmony_ci (struct usb_otg_descriptor)) { 239362306a36Sopenharmony_ci /* 239462306a36Sopenharmony_ci * We are operating on a legacy OTP device 239562306a36Sopenharmony_ci * These should be told that they are operating 239662306a36Sopenharmony_ci * on the wrong port if we have another port that does 239762306a36Sopenharmony_ci * support HNP 239862306a36Sopenharmony_ci */ 239962306a36Sopenharmony_ci if (bus->otg_port != 0) { 240062306a36Sopenharmony_ci /* Set a_alt_hnp_support for legacy otg device */ 240162306a36Sopenharmony_ci err = usb_control_msg(udev, 240262306a36Sopenharmony_ci usb_sndctrlpipe(udev, 0), 240362306a36Sopenharmony_ci USB_REQ_SET_FEATURE, 0, 240462306a36Sopenharmony_ci USB_DEVICE_A_ALT_HNP_SUPPORT, 240562306a36Sopenharmony_ci 0, NULL, 0, 240662306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 240762306a36Sopenharmony_ci if (err < 0) 240862306a36Sopenharmony_ci dev_err(&udev->dev, 240962306a36Sopenharmony_ci "set a_alt_hnp_support failed: %d\n", 241062306a36Sopenharmony_ci err); 241162306a36Sopenharmony_ci } 241262306a36Sopenharmony_ci } 241362306a36Sopenharmony_ci } 241462306a36Sopenharmony_ci#endif 241562306a36Sopenharmony_ci return err; 241662306a36Sopenharmony_ci} 241762306a36Sopenharmony_ci 241862306a36Sopenharmony_ci 241962306a36Sopenharmony_ci/** 242062306a36Sopenharmony_ci * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) 242162306a36Sopenharmony_ci * @udev: newly addressed device (in ADDRESS state) 242262306a36Sopenharmony_ci * 242362306a36Sopenharmony_ci * This is only called by usb_new_device() -- all comments that apply there 242462306a36Sopenharmony_ci * apply here wrt to environment. 242562306a36Sopenharmony_ci * 242662306a36Sopenharmony_ci * If the device is WUSB and not authorized, we don't attempt to read 242762306a36Sopenharmony_ci * the string descriptors, as they will be errored out by the device 242862306a36Sopenharmony_ci * until it has been authorized. 242962306a36Sopenharmony_ci * 243062306a36Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 243162306a36Sopenharmony_ci */ 243262306a36Sopenharmony_cistatic int usb_enumerate_device(struct usb_device *udev) 243362306a36Sopenharmony_ci{ 243462306a36Sopenharmony_ci int err; 243562306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 243662306a36Sopenharmony_ci 243762306a36Sopenharmony_ci if (udev->config == NULL) { 243862306a36Sopenharmony_ci err = usb_get_configuration(udev); 243962306a36Sopenharmony_ci if (err < 0) { 244062306a36Sopenharmony_ci if (err != -ENODEV) 244162306a36Sopenharmony_ci dev_err(&udev->dev, "can't read configurations, error %d\n", 244262306a36Sopenharmony_ci err); 244362306a36Sopenharmony_ci return err; 244462306a36Sopenharmony_ci } 244562306a36Sopenharmony_ci } 244662306a36Sopenharmony_ci 244762306a36Sopenharmony_ci /* read the standard strings and cache them if present */ 244862306a36Sopenharmony_ci udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 244962306a36Sopenharmony_ci udev->manufacturer = usb_cache_string(udev, 245062306a36Sopenharmony_ci udev->descriptor.iManufacturer); 245162306a36Sopenharmony_ci udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 245262306a36Sopenharmony_ci 245362306a36Sopenharmony_ci err = usb_enumerate_device_otg(udev); 245462306a36Sopenharmony_ci if (err < 0) 245562306a36Sopenharmony_ci return err; 245662306a36Sopenharmony_ci 245762306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support && 245862306a36Sopenharmony_ci !is_targeted(udev)) { 245962306a36Sopenharmony_ci /* Maybe it can talk to us, though we can't talk to it. 246062306a36Sopenharmony_ci * (Includes HNP test device.) 246162306a36Sopenharmony_ci */ 246262306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable 246362306a36Sopenharmony_ci || udev->bus->is_b_host)) { 246462306a36Sopenharmony_ci err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND); 246562306a36Sopenharmony_ci if (err < 0) 246662306a36Sopenharmony_ci dev_dbg(&udev->dev, "HNP fail, %d\n", err); 246762306a36Sopenharmony_ci } 246862306a36Sopenharmony_ci return -ENOTSUPP; 246962306a36Sopenharmony_ci } 247062306a36Sopenharmony_ci 247162306a36Sopenharmony_ci usb_detect_interface_quirks(udev); 247262306a36Sopenharmony_ci 247362306a36Sopenharmony_ci return 0; 247462306a36Sopenharmony_ci} 247562306a36Sopenharmony_ci 247662306a36Sopenharmony_cistatic void set_usb_port_removable(struct usb_device *udev) 247762306a36Sopenharmony_ci{ 247862306a36Sopenharmony_ci struct usb_device *hdev = udev->parent; 247962306a36Sopenharmony_ci struct usb_hub *hub; 248062306a36Sopenharmony_ci u8 port = udev->portnum; 248162306a36Sopenharmony_ci u16 wHubCharacteristics; 248262306a36Sopenharmony_ci bool removable = true; 248362306a36Sopenharmony_ci 248462306a36Sopenharmony_ci dev_set_removable(&udev->dev, DEVICE_REMOVABLE_UNKNOWN); 248562306a36Sopenharmony_ci 248662306a36Sopenharmony_ci if (!hdev) 248762306a36Sopenharmony_ci return; 248862306a36Sopenharmony_ci 248962306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 249062306a36Sopenharmony_ci 249162306a36Sopenharmony_ci /* 249262306a36Sopenharmony_ci * If the platform firmware has provided information about a port, 249362306a36Sopenharmony_ci * use that to determine whether it's removable. 249462306a36Sopenharmony_ci */ 249562306a36Sopenharmony_ci switch (hub->ports[udev->portnum - 1]->connect_type) { 249662306a36Sopenharmony_ci case USB_PORT_CONNECT_TYPE_HOT_PLUG: 249762306a36Sopenharmony_ci dev_set_removable(&udev->dev, DEVICE_REMOVABLE); 249862306a36Sopenharmony_ci return; 249962306a36Sopenharmony_ci case USB_PORT_CONNECT_TYPE_HARD_WIRED: 250062306a36Sopenharmony_ci case USB_PORT_NOT_USED: 250162306a36Sopenharmony_ci dev_set_removable(&udev->dev, DEVICE_FIXED); 250262306a36Sopenharmony_ci return; 250362306a36Sopenharmony_ci default: 250462306a36Sopenharmony_ci break; 250562306a36Sopenharmony_ci } 250662306a36Sopenharmony_ci 250762306a36Sopenharmony_ci /* 250862306a36Sopenharmony_ci * Otherwise, check whether the hub knows whether a port is removable 250962306a36Sopenharmony_ci * or not 251062306a36Sopenharmony_ci */ 251162306a36Sopenharmony_ci wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 251262306a36Sopenharmony_ci 251362306a36Sopenharmony_ci if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 251462306a36Sopenharmony_ci return; 251562306a36Sopenharmony_ci 251662306a36Sopenharmony_ci if (hub_is_superspeed(hdev)) { 251762306a36Sopenharmony_ci if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) 251862306a36Sopenharmony_ci & (1 << port)) 251962306a36Sopenharmony_ci removable = false; 252062306a36Sopenharmony_ci } else { 252162306a36Sopenharmony_ci if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 252262306a36Sopenharmony_ci removable = false; 252362306a36Sopenharmony_ci } 252462306a36Sopenharmony_ci 252562306a36Sopenharmony_ci if (removable) 252662306a36Sopenharmony_ci dev_set_removable(&udev->dev, DEVICE_REMOVABLE); 252762306a36Sopenharmony_ci else 252862306a36Sopenharmony_ci dev_set_removable(&udev->dev, DEVICE_FIXED); 252962306a36Sopenharmony_ci 253062306a36Sopenharmony_ci} 253162306a36Sopenharmony_ci 253262306a36Sopenharmony_ci/** 253362306a36Sopenharmony_ci * usb_new_device - perform initial device setup (usbcore-internal) 253462306a36Sopenharmony_ci * @udev: newly addressed device (in ADDRESS state) 253562306a36Sopenharmony_ci * 253662306a36Sopenharmony_ci * This is called with devices which have been detected but not fully 253762306a36Sopenharmony_ci * enumerated. The device descriptor is available, but not descriptors 253862306a36Sopenharmony_ci * for any device configuration. The caller must have locked either 253962306a36Sopenharmony_ci * the parent hub (if udev is a normal device) or else the 254062306a36Sopenharmony_ci * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to 254162306a36Sopenharmony_ci * udev has already been installed, but udev is not yet visible through 254262306a36Sopenharmony_ci * sysfs or other filesystem code. 254362306a36Sopenharmony_ci * 254462306a36Sopenharmony_ci * This call is synchronous, and may not be used in an interrupt context. 254562306a36Sopenharmony_ci * 254662306a36Sopenharmony_ci * Only the hub driver or root-hub registrar should ever call this. 254762306a36Sopenharmony_ci * 254862306a36Sopenharmony_ci * Return: Whether the device is configured properly or not. Zero if the 254962306a36Sopenharmony_ci * interface was registered with the driver core; else a negative errno 255062306a36Sopenharmony_ci * value. 255162306a36Sopenharmony_ci * 255262306a36Sopenharmony_ci */ 255362306a36Sopenharmony_ciint usb_new_device(struct usb_device *udev) 255462306a36Sopenharmony_ci{ 255562306a36Sopenharmony_ci int err; 255662306a36Sopenharmony_ci 255762306a36Sopenharmony_ci if (udev->parent) { 255862306a36Sopenharmony_ci /* Initialize non-root-hub device wakeup to disabled; 255962306a36Sopenharmony_ci * device (un)configuration controls wakeup capable 256062306a36Sopenharmony_ci * sysfs power/wakeup controls wakeup enabled/disabled 256162306a36Sopenharmony_ci */ 256262306a36Sopenharmony_ci device_init_wakeup(&udev->dev, 0); 256362306a36Sopenharmony_ci } 256462306a36Sopenharmony_ci 256562306a36Sopenharmony_ci /* Tell the runtime-PM framework the device is active */ 256662306a36Sopenharmony_ci pm_runtime_set_active(&udev->dev); 256762306a36Sopenharmony_ci pm_runtime_get_noresume(&udev->dev); 256862306a36Sopenharmony_ci pm_runtime_use_autosuspend(&udev->dev); 256962306a36Sopenharmony_ci pm_runtime_enable(&udev->dev); 257062306a36Sopenharmony_ci 257162306a36Sopenharmony_ci /* By default, forbid autosuspend for all devices. It will be 257262306a36Sopenharmony_ci * allowed for hubs during binding. 257362306a36Sopenharmony_ci */ 257462306a36Sopenharmony_ci usb_disable_autosuspend(udev); 257562306a36Sopenharmony_ci 257662306a36Sopenharmony_ci err = usb_enumerate_device(udev); /* Read descriptors */ 257762306a36Sopenharmony_ci if (err < 0) 257862306a36Sopenharmony_ci goto fail; 257962306a36Sopenharmony_ci dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 258062306a36Sopenharmony_ci udev->devnum, udev->bus->busnum, 258162306a36Sopenharmony_ci (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 258262306a36Sopenharmony_ci /* export the usbdev device-node for libusb */ 258362306a36Sopenharmony_ci udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 258462306a36Sopenharmony_ci (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 258562306a36Sopenharmony_ci 258662306a36Sopenharmony_ci /* Tell the world! */ 258762306a36Sopenharmony_ci announce_device(udev); 258862306a36Sopenharmony_ci 258962306a36Sopenharmony_ci if (udev->serial) 259062306a36Sopenharmony_ci add_device_randomness(udev->serial, strlen(udev->serial)); 259162306a36Sopenharmony_ci if (udev->product) 259262306a36Sopenharmony_ci add_device_randomness(udev->product, strlen(udev->product)); 259362306a36Sopenharmony_ci if (udev->manufacturer) 259462306a36Sopenharmony_ci add_device_randomness(udev->manufacturer, 259562306a36Sopenharmony_ci strlen(udev->manufacturer)); 259662306a36Sopenharmony_ci 259762306a36Sopenharmony_ci device_enable_async_suspend(&udev->dev); 259862306a36Sopenharmony_ci 259962306a36Sopenharmony_ci /* check whether the hub or firmware marks this port as non-removable */ 260062306a36Sopenharmony_ci set_usb_port_removable(udev); 260162306a36Sopenharmony_ci 260262306a36Sopenharmony_ci /* Register the device. The device driver is responsible 260362306a36Sopenharmony_ci * for configuring the device and invoking the add-device 260462306a36Sopenharmony_ci * notifier chain (used by usbfs and possibly others). 260562306a36Sopenharmony_ci */ 260662306a36Sopenharmony_ci err = device_add(&udev->dev); 260762306a36Sopenharmony_ci if (err) { 260862306a36Sopenharmony_ci dev_err(&udev->dev, "can't device_add, error %d\n", err); 260962306a36Sopenharmony_ci goto fail; 261062306a36Sopenharmony_ci } 261162306a36Sopenharmony_ci 261262306a36Sopenharmony_ci /* Create link files between child device and usb port device. */ 261362306a36Sopenharmony_ci if (udev->parent) { 261462306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 261562306a36Sopenharmony_ci int port1 = udev->portnum; 261662306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 261762306a36Sopenharmony_ci 261862306a36Sopenharmony_ci err = sysfs_create_link(&udev->dev.kobj, 261962306a36Sopenharmony_ci &port_dev->dev.kobj, "port"); 262062306a36Sopenharmony_ci if (err) 262162306a36Sopenharmony_ci goto fail; 262262306a36Sopenharmony_ci 262362306a36Sopenharmony_ci err = sysfs_create_link(&port_dev->dev.kobj, 262462306a36Sopenharmony_ci &udev->dev.kobj, "device"); 262562306a36Sopenharmony_ci if (err) { 262662306a36Sopenharmony_ci sysfs_remove_link(&udev->dev.kobj, "port"); 262762306a36Sopenharmony_ci goto fail; 262862306a36Sopenharmony_ci } 262962306a36Sopenharmony_ci 263062306a36Sopenharmony_ci if (!test_and_set_bit(port1, hub->child_usage_bits)) 263162306a36Sopenharmony_ci pm_runtime_get_sync(&port_dev->dev); 263262306a36Sopenharmony_ci } 263362306a36Sopenharmony_ci 263462306a36Sopenharmony_ci (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 263562306a36Sopenharmony_ci usb_mark_last_busy(udev); 263662306a36Sopenharmony_ci pm_runtime_put_sync_autosuspend(&udev->dev); 263762306a36Sopenharmony_ci return err; 263862306a36Sopenharmony_ci 263962306a36Sopenharmony_cifail: 264062306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_NOTATTACHED); 264162306a36Sopenharmony_ci pm_runtime_disable(&udev->dev); 264262306a36Sopenharmony_ci pm_runtime_set_suspended(&udev->dev); 264362306a36Sopenharmony_ci return err; 264462306a36Sopenharmony_ci} 264562306a36Sopenharmony_ci 264662306a36Sopenharmony_ci 264762306a36Sopenharmony_ci/** 264862306a36Sopenharmony_ci * usb_deauthorize_device - deauthorize a device (usbcore-internal) 264962306a36Sopenharmony_ci * @usb_dev: USB device 265062306a36Sopenharmony_ci * 265162306a36Sopenharmony_ci * Move the USB device to a very basic state where interfaces are disabled 265262306a36Sopenharmony_ci * and the device is in fact unconfigured and unusable. 265362306a36Sopenharmony_ci * 265462306a36Sopenharmony_ci * We share a lock (that we have) with device_del(), so we need to 265562306a36Sopenharmony_ci * defer its call. 265662306a36Sopenharmony_ci * 265762306a36Sopenharmony_ci * Return: 0. 265862306a36Sopenharmony_ci */ 265962306a36Sopenharmony_ciint usb_deauthorize_device(struct usb_device *usb_dev) 266062306a36Sopenharmony_ci{ 266162306a36Sopenharmony_ci usb_lock_device(usb_dev); 266262306a36Sopenharmony_ci if (usb_dev->authorized == 0) 266362306a36Sopenharmony_ci goto out_unauthorized; 266462306a36Sopenharmony_ci 266562306a36Sopenharmony_ci usb_dev->authorized = 0; 266662306a36Sopenharmony_ci usb_set_configuration(usb_dev, -1); 266762306a36Sopenharmony_ci 266862306a36Sopenharmony_ciout_unauthorized: 266962306a36Sopenharmony_ci usb_unlock_device(usb_dev); 267062306a36Sopenharmony_ci return 0; 267162306a36Sopenharmony_ci} 267262306a36Sopenharmony_ci 267362306a36Sopenharmony_ci 267462306a36Sopenharmony_ciint usb_authorize_device(struct usb_device *usb_dev) 267562306a36Sopenharmony_ci{ 267662306a36Sopenharmony_ci int result = 0, c; 267762306a36Sopenharmony_ci 267862306a36Sopenharmony_ci usb_lock_device(usb_dev); 267962306a36Sopenharmony_ci if (usb_dev->authorized == 1) 268062306a36Sopenharmony_ci goto out_authorized; 268162306a36Sopenharmony_ci 268262306a36Sopenharmony_ci result = usb_autoresume_device(usb_dev); 268362306a36Sopenharmony_ci if (result < 0) { 268462306a36Sopenharmony_ci dev_err(&usb_dev->dev, 268562306a36Sopenharmony_ci "can't autoresume for authorization: %d\n", result); 268662306a36Sopenharmony_ci goto error_autoresume; 268762306a36Sopenharmony_ci } 268862306a36Sopenharmony_ci 268962306a36Sopenharmony_ci usb_dev->authorized = 1; 269062306a36Sopenharmony_ci /* Choose and set the configuration. This registers the interfaces 269162306a36Sopenharmony_ci * with the driver core and lets interface drivers bind to them. 269262306a36Sopenharmony_ci */ 269362306a36Sopenharmony_ci c = usb_choose_configuration(usb_dev); 269462306a36Sopenharmony_ci if (c >= 0) { 269562306a36Sopenharmony_ci result = usb_set_configuration(usb_dev, c); 269662306a36Sopenharmony_ci if (result) { 269762306a36Sopenharmony_ci dev_err(&usb_dev->dev, 269862306a36Sopenharmony_ci "can't set config #%d, error %d\n", c, result); 269962306a36Sopenharmony_ci /* This need not be fatal. The user can try to 270062306a36Sopenharmony_ci * set other configurations. */ 270162306a36Sopenharmony_ci } 270262306a36Sopenharmony_ci } 270362306a36Sopenharmony_ci dev_info(&usb_dev->dev, "authorized to connect\n"); 270462306a36Sopenharmony_ci 270562306a36Sopenharmony_ci usb_autosuspend_device(usb_dev); 270662306a36Sopenharmony_cierror_autoresume: 270762306a36Sopenharmony_ciout_authorized: 270862306a36Sopenharmony_ci usb_unlock_device(usb_dev); /* complements locktree */ 270962306a36Sopenharmony_ci return result; 271062306a36Sopenharmony_ci} 271162306a36Sopenharmony_ci 271262306a36Sopenharmony_ci/** 271362306a36Sopenharmony_ci * get_port_ssp_rate - Match the extended port status to SSP rate 271462306a36Sopenharmony_ci * @hdev: The hub device 271562306a36Sopenharmony_ci * @ext_portstatus: extended port status 271662306a36Sopenharmony_ci * 271762306a36Sopenharmony_ci * Match the extended port status speed id to the SuperSpeed Plus sublink speed 271862306a36Sopenharmony_ci * capability attributes. Base on the number of connected lanes and speed, 271962306a36Sopenharmony_ci * return the corresponding enum usb_ssp_rate. 272062306a36Sopenharmony_ci */ 272162306a36Sopenharmony_cistatic enum usb_ssp_rate get_port_ssp_rate(struct usb_device *hdev, 272262306a36Sopenharmony_ci u32 ext_portstatus) 272362306a36Sopenharmony_ci{ 272462306a36Sopenharmony_ci struct usb_ssp_cap_descriptor *ssp_cap; 272562306a36Sopenharmony_ci u32 attr; 272662306a36Sopenharmony_ci u8 speed_id; 272762306a36Sopenharmony_ci u8 ssac; 272862306a36Sopenharmony_ci u8 lanes; 272962306a36Sopenharmony_ci int i; 273062306a36Sopenharmony_ci 273162306a36Sopenharmony_ci if (!hdev->bos) 273262306a36Sopenharmony_ci goto out; 273362306a36Sopenharmony_ci 273462306a36Sopenharmony_ci ssp_cap = hdev->bos->ssp_cap; 273562306a36Sopenharmony_ci if (!ssp_cap) 273662306a36Sopenharmony_ci goto out; 273762306a36Sopenharmony_ci 273862306a36Sopenharmony_ci speed_id = ext_portstatus & USB_EXT_PORT_STAT_RX_SPEED_ID; 273962306a36Sopenharmony_ci lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 274062306a36Sopenharmony_ci 274162306a36Sopenharmony_ci ssac = le32_to_cpu(ssp_cap->bmAttributes) & 274262306a36Sopenharmony_ci USB_SSP_SUBLINK_SPEED_ATTRIBS; 274362306a36Sopenharmony_ci 274462306a36Sopenharmony_ci for (i = 0; i <= ssac; i++) { 274562306a36Sopenharmony_ci u8 ssid; 274662306a36Sopenharmony_ci 274762306a36Sopenharmony_ci attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); 274862306a36Sopenharmony_ci ssid = FIELD_GET(USB_SSP_SUBLINK_SPEED_SSID, attr); 274962306a36Sopenharmony_ci if (speed_id == ssid) { 275062306a36Sopenharmony_ci u16 mantissa; 275162306a36Sopenharmony_ci u8 lse; 275262306a36Sopenharmony_ci u8 type; 275362306a36Sopenharmony_ci 275462306a36Sopenharmony_ci /* 275562306a36Sopenharmony_ci * Note: currently asymmetric lane types are only 275662306a36Sopenharmony_ci * applicable for SSIC operate in SuperSpeed protocol 275762306a36Sopenharmony_ci */ 275862306a36Sopenharmony_ci type = FIELD_GET(USB_SSP_SUBLINK_SPEED_ST, attr); 275962306a36Sopenharmony_ci if (type == USB_SSP_SUBLINK_SPEED_ST_ASYM_RX || 276062306a36Sopenharmony_ci type == USB_SSP_SUBLINK_SPEED_ST_ASYM_TX) 276162306a36Sopenharmony_ci goto out; 276262306a36Sopenharmony_ci 276362306a36Sopenharmony_ci if (FIELD_GET(USB_SSP_SUBLINK_SPEED_LP, attr) != 276462306a36Sopenharmony_ci USB_SSP_SUBLINK_SPEED_LP_SSP) 276562306a36Sopenharmony_ci goto out; 276662306a36Sopenharmony_ci 276762306a36Sopenharmony_ci lse = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSE, attr); 276862306a36Sopenharmony_ci mantissa = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSM, attr); 276962306a36Sopenharmony_ci 277062306a36Sopenharmony_ci /* Convert to Gbps */ 277162306a36Sopenharmony_ci for (; lse < USB_SSP_SUBLINK_SPEED_LSE_GBPS; lse++) 277262306a36Sopenharmony_ci mantissa /= 1000; 277362306a36Sopenharmony_ci 277462306a36Sopenharmony_ci if (mantissa >= 10 && lanes == 1) 277562306a36Sopenharmony_ci return USB_SSP_GEN_2x1; 277662306a36Sopenharmony_ci 277762306a36Sopenharmony_ci if (mantissa >= 10 && lanes == 2) 277862306a36Sopenharmony_ci return USB_SSP_GEN_2x2; 277962306a36Sopenharmony_ci 278062306a36Sopenharmony_ci if (mantissa >= 5 && lanes == 2) 278162306a36Sopenharmony_ci return USB_SSP_GEN_1x2; 278262306a36Sopenharmony_ci 278362306a36Sopenharmony_ci goto out; 278462306a36Sopenharmony_ci } 278562306a36Sopenharmony_ci } 278662306a36Sopenharmony_ci 278762306a36Sopenharmony_ciout: 278862306a36Sopenharmony_ci return USB_SSP_GEN_UNKNOWN; 278962306a36Sopenharmony_ci} 279062306a36Sopenharmony_ci 279162306a36Sopenharmony_ci#ifdef CONFIG_USB_FEW_INIT_RETRIES 279262306a36Sopenharmony_ci#define PORT_RESET_TRIES 2 279362306a36Sopenharmony_ci#define SET_ADDRESS_TRIES 1 279462306a36Sopenharmony_ci#define GET_DESCRIPTOR_TRIES 1 279562306a36Sopenharmony_ci#define GET_MAXPACKET0_TRIES 1 279662306a36Sopenharmony_ci#define PORT_INIT_TRIES 4 279762306a36Sopenharmony_ci 279862306a36Sopenharmony_ci#else 279962306a36Sopenharmony_ci#define PORT_RESET_TRIES 5 280062306a36Sopenharmony_ci#define SET_ADDRESS_TRIES 2 280162306a36Sopenharmony_ci#define GET_DESCRIPTOR_TRIES 2 280262306a36Sopenharmony_ci#define GET_MAXPACKET0_TRIES 3 280362306a36Sopenharmony_ci#define PORT_INIT_TRIES 4 280462306a36Sopenharmony_ci#endif /* CONFIG_USB_FEW_INIT_RETRIES */ 280562306a36Sopenharmony_ci 280662306a36Sopenharmony_ci#define DETECT_DISCONNECT_TRIES 5 280762306a36Sopenharmony_ci 280862306a36Sopenharmony_ci#define HUB_ROOT_RESET_TIME 60 /* times are in msec */ 280962306a36Sopenharmony_ci#define HUB_SHORT_RESET_TIME 10 281062306a36Sopenharmony_ci#define HUB_BH_RESET_TIME 50 281162306a36Sopenharmony_ci#define HUB_LONG_RESET_TIME 200 281262306a36Sopenharmony_ci#define HUB_RESET_TIMEOUT 800 281362306a36Sopenharmony_ci 281462306a36Sopenharmony_cistatic bool use_new_scheme(struct usb_device *udev, int retry, 281562306a36Sopenharmony_ci struct usb_port *port_dev) 281662306a36Sopenharmony_ci{ 281762306a36Sopenharmony_ci int old_scheme_first_port = 281862306a36Sopenharmony_ci (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || 281962306a36Sopenharmony_ci old_scheme_first; 282062306a36Sopenharmony_ci 282162306a36Sopenharmony_ci /* 282262306a36Sopenharmony_ci * "New scheme" enumeration causes an extra state transition to be 282362306a36Sopenharmony_ci * exposed to an xhci host and causes USB3 devices to receive control 282462306a36Sopenharmony_ci * commands in the default state. This has been seen to cause 282562306a36Sopenharmony_ci * enumeration failures, so disable this enumeration scheme for USB3 282662306a36Sopenharmony_ci * devices. 282762306a36Sopenharmony_ci */ 282862306a36Sopenharmony_ci if (udev->speed >= USB_SPEED_SUPER) 282962306a36Sopenharmony_ci return false; 283062306a36Sopenharmony_ci 283162306a36Sopenharmony_ci /* 283262306a36Sopenharmony_ci * If use_both_schemes is set, use the first scheme (whichever 283362306a36Sopenharmony_ci * it is) for the larger half of the retries, then use the other 283462306a36Sopenharmony_ci * scheme. Otherwise, use the first scheme for all the retries. 283562306a36Sopenharmony_ci */ 283662306a36Sopenharmony_ci if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2) 283762306a36Sopenharmony_ci return old_scheme_first_port; /* Second half */ 283862306a36Sopenharmony_ci return !old_scheme_first_port; /* First half or all */ 283962306a36Sopenharmony_ci} 284062306a36Sopenharmony_ci 284162306a36Sopenharmony_ci/* Is a USB 3.0 port in the Inactive or Compliance Mode state? 284262306a36Sopenharmony_ci * Port warm reset is required to recover 284362306a36Sopenharmony_ci */ 284462306a36Sopenharmony_cistatic bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 284562306a36Sopenharmony_ci u16 portstatus) 284662306a36Sopenharmony_ci{ 284762306a36Sopenharmony_ci u16 link_state; 284862306a36Sopenharmony_ci 284962306a36Sopenharmony_ci if (!hub_is_superspeed(hub->hdev)) 285062306a36Sopenharmony_ci return false; 285162306a36Sopenharmony_ci 285262306a36Sopenharmony_ci if (test_bit(port1, hub->warm_reset_bits)) 285362306a36Sopenharmony_ci return true; 285462306a36Sopenharmony_ci 285562306a36Sopenharmony_ci link_state = portstatus & USB_PORT_STAT_LINK_STATE; 285662306a36Sopenharmony_ci return link_state == USB_SS_PORT_LS_SS_INACTIVE 285762306a36Sopenharmony_ci || link_state == USB_SS_PORT_LS_COMP_MOD; 285862306a36Sopenharmony_ci} 285962306a36Sopenharmony_ci 286062306a36Sopenharmony_cistatic int hub_port_wait_reset(struct usb_hub *hub, int port1, 286162306a36Sopenharmony_ci struct usb_device *udev, unsigned int delay, bool warm) 286262306a36Sopenharmony_ci{ 286362306a36Sopenharmony_ci int delay_time, ret; 286462306a36Sopenharmony_ci u16 portstatus; 286562306a36Sopenharmony_ci u16 portchange; 286662306a36Sopenharmony_ci u32 ext_portstatus = 0; 286762306a36Sopenharmony_ci 286862306a36Sopenharmony_ci for (delay_time = 0; 286962306a36Sopenharmony_ci delay_time < HUB_RESET_TIMEOUT; 287062306a36Sopenharmony_ci delay_time += delay) { 287162306a36Sopenharmony_ci /* wait to give the device a chance to reset */ 287262306a36Sopenharmony_ci msleep(delay); 287362306a36Sopenharmony_ci 287462306a36Sopenharmony_ci /* read and decode port status */ 287562306a36Sopenharmony_ci if (hub_is_superspeedplus(hub->hdev)) 287662306a36Sopenharmony_ci ret = hub_ext_port_status(hub, port1, 287762306a36Sopenharmony_ci HUB_EXT_PORT_STATUS, 287862306a36Sopenharmony_ci &portstatus, &portchange, 287962306a36Sopenharmony_ci &ext_portstatus); 288062306a36Sopenharmony_ci else 288162306a36Sopenharmony_ci ret = usb_hub_port_status(hub, port1, &portstatus, 288262306a36Sopenharmony_ci &portchange); 288362306a36Sopenharmony_ci if (ret < 0) 288462306a36Sopenharmony_ci return ret; 288562306a36Sopenharmony_ci 288662306a36Sopenharmony_ci /* 288762306a36Sopenharmony_ci * The port state is unknown until the reset completes. 288862306a36Sopenharmony_ci * 288962306a36Sopenharmony_ci * On top of that, some chips may require additional time 289062306a36Sopenharmony_ci * to re-establish a connection after the reset is complete, 289162306a36Sopenharmony_ci * so also wait for the connection to be re-established. 289262306a36Sopenharmony_ci */ 289362306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_RESET) && 289462306a36Sopenharmony_ci (portstatus & USB_PORT_STAT_CONNECTION)) 289562306a36Sopenharmony_ci break; 289662306a36Sopenharmony_ci 289762306a36Sopenharmony_ci /* switch to the long delay after two short delay failures */ 289862306a36Sopenharmony_ci if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 289962306a36Sopenharmony_ci delay = HUB_LONG_RESET_TIME; 290062306a36Sopenharmony_ci 290162306a36Sopenharmony_ci dev_dbg(&hub->ports[port1 - 1]->dev, 290262306a36Sopenharmony_ci "not %sreset yet, waiting %dms\n", 290362306a36Sopenharmony_ci warm ? "warm " : "", delay); 290462306a36Sopenharmony_ci } 290562306a36Sopenharmony_ci 290662306a36Sopenharmony_ci if ((portstatus & USB_PORT_STAT_RESET)) 290762306a36Sopenharmony_ci return -EBUSY; 290862306a36Sopenharmony_ci 290962306a36Sopenharmony_ci if (hub_port_warm_reset_required(hub, port1, portstatus)) 291062306a36Sopenharmony_ci return -ENOTCONN; 291162306a36Sopenharmony_ci 291262306a36Sopenharmony_ci /* Device went away? */ 291362306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_CONNECTION)) 291462306a36Sopenharmony_ci return -ENOTCONN; 291562306a36Sopenharmony_ci 291662306a36Sopenharmony_ci /* Retry if connect change is set but status is still connected. 291762306a36Sopenharmony_ci * A USB 3.0 connection may bounce if multiple warm resets were issued, 291862306a36Sopenharmony_ci * but the device may have successfully re-connected. Ignore it. 291962306a36Sopenharmony_ci */ 292062306a36Sopenharmony_ci if (!hub_is_superspeed(hub->hdev) && 292162306a36Sopenharmony_ci (portchange & USB_PORT_STAT_C_CONNECTION)) { 292262306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 292362306a36Sopenharmony_ci USB_PORT_FEAT_C_CONNECTION); 292462306a36Sopenharmony_ci return -EAGAIN; 292562306a36Sopenharmony_ci } 292662306a36Sopenharmony_ci 292762306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_ENABLE)) 292862306a36Sopenharmony_ci return -EBUSY; 292962306a36Sopenharmony_ci 293062306a36Sopenharmony_ci if (!udev) 293162306a36Sopenharmony_ci return 0; 293262306a36Sopenharmony_ci 293362306a36Sopenharmony_ci if (hub_is_superspeedplus(hub->hdev)) { 293462306a36Sopenharmony_ci /* extended portstatus Rx and Tx lane count are zero based */ 293562306a36Sopenharmony_ci udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 293662306a36Sopenharmony_ci udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1; 293762306a36Sopenharmony_ci udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus); 293862306a36Sopenharmony_ci } else { 293962306a36Sopenharmony_ci udev->rx_lanes = 1; 294062306a36Sopenharmony_ci udev->tx_lanes = 1; 294162306a36Sopenharmony_ci udev->ssp_rate = USB_SSP_GEN_UNKNOWN; 294262306a36Sopenharmony_ci } 294362306a36Sopenharmony_ci if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN) 294462306a36Sopenharmony_ci udev->speed = USB_SPEED_SUPER_PLUS; 294562306a36Sopenharmony_ci else if (hub_is_superspeed(hub->hdev)) 294662306a36Sopenharmony_ci udev->speed = USB_SPEED_SUPER; 294762306a36Sopenharmony_ci else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 294862306a36Sopenharmony_ci udev->speed = USB_SPEED_HIGH; 294962306a36Sopenharmony_ci else if (portstatus & USB_PORT_STAT_LOW_SPEED) 295062306a36Sopenharmony_ci udev->speed = USB_SPEED_LOW; 295162306a36Sopenharmony_ci else 295262306a36Sopenharmony_ci udev->speed = USB_SPEED_FULL; 295362306a36Sopenharmony_ci return 0; 295462306a36Sopenharmony_ci} 295562306a36Sopenharmony_ci 295662306a36Sopenharmony_ci/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 295762306a36Sopenharmony_cistatic int hub_port_reset(struct usb_hub *hub, int port1, 295862306a36Sopenharmony_ci struct usb_device *udev, unsigned int delay, bool warm) 295962306a36Sopenharmony_ci{ 296062306a36Sopenharmony_ci int i, status; 296162306a36Sopenharmony_ci u16 portchange, portstatus; 296262306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 296362306a36Sopenharmony_ci int reset_recovery_time; 296462306a36Sopenharmony_ci 296562306a36Sopenharmony_ci if (!hub_is_superspeed(hub->hdev)) { 296662306a36Sopenharmony_ci if (warm) { 296762306a36Sopenharmony_ci dev_err(hub->intfdev, "only USB3 hub support " 296862306a36Sopenharmony_ci "warm reset\n"); 296962306a36Sopenharmony_ci return -EINVAL; 297062306a36Sopenharmony_ci } 297162306a36Sopenharmony_ci /* Block EHCI CF initialization during the port reset. 297262306a36Sopenharmony_ci * Some companion controllers don't like it when they mix. 297362306a36Sopenharmony_ci */ 297462306a36Sopenharmony_ci down_read(&ehci_cf_port_reset_rwsem); 297562306a36Sopenharmony_ci } else if (!warm) { 297662306a36Sopenharmony_ci /* 297762306a36Sopenharmony_ci * If the caller hasn't explicitly requested a warm reset, 297862306a36Sopenharmony_ci * double check and see if one is needed. 297962306a36Sopenharmony_ci */ 298062306a36Sopenharmony_ci if (usb_hub_port_status(hub, port1, &portstatus, 298162306a36Sopenharmony_ci &portchange) == 0) 298262306a36Sopenharmony_ci if (hub_port_warm_reset_required(hub, port1, 298362306a36Sopenharmony_ci portstatus)) 298462306a36Sopenharmony_ci warm = true; 298562306a36Sopenharmony_ci } 298662306a36Sopenharmony_ci clear_bit(port1, hub->warm_reset_bits); 298762306a36Sopenharmony_ci 298862306a36Sopenharmony_ci /* Reset the port */ 298962306a36Sopenharmony_ci for (i = 0; i < PORT_RESET_TRIES; i++) { 299062306a36Sopenharmony_ci status = set_port_feature(hub->hdev, port1, (warm ? 299162306a36Sopenharmony_ci USB_PORT_FEAT_BH_PORT_RESET : 299262306a36Sopenharmony_ci USB_PORT_FEAT_RESET)); 299362306a36Sopenharmony_ci if (status == -ENODEV) { 299462306a36Sopenharmony_ci ; /* The hub is gone */ 299562306a36Sopenharmony_ci } else if (status) { 299662306a36Sopenharmony_ci dev_err(&port_dev->dev, 299762306a36Sopenharmony_ci "cannot %sreset (err = %d)\n", 299862306a36Sopenharmony_ci warm ? "warm " : "", status); 299962306a36Sopenharmony_ci } else { 300062306a36Sopenharmony_ci status = hub_port_wait_reset(hub, port1, udev, delay, 300162306a36Sopenharmony_ci warm); 300262306a36Sopenharmony_ci if (status && status != -ENOTCONN && status != -ENODEV) 300362306a36Sopenharmony_ci dev_dbg(hub->intfdev, 300462306a36Sopenharmony_ci "port_wait_reset: err = %d\n", 300562306a36Sopenharmony_ci status); 300662306a36Sopenharmony_ci } 300762306a36Sopenharmony_ci 300862306a36Sopenharmony_ci /* 300962306a36Sopenharmony_ci * Check for disconnect or reset, and bail out after several 301062306a36Sopenharmony_ci * reset attempts to avoid warm reset loop. 301162306a36Sopenharmony_ci */ 301262306a36Sopenharmony_ci if (status == 0 || status == -ENOTCONN || status == -ENODEV || 301362306a36Sopenharmony_ci (status == -EBUSY && i == PORT_RESET_TRIES - 1)) { 301462306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 301562306a36Sopenharmony_ci USB_PORT_FEAT_C_RESET); 301662306a36Sopenharmony_ci 301762306a36Sopenharmony_ci if (!hub_is_superspeed(hub->hdev)) 301862306a36Sopenharmony_ci goto done; 301962306a36Sopenharmony_ci 302062306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 302162306a36Sopenharmony_ci USB_PORT_FEAT_C_BH_PORT_RESET); 302262306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 302362306a36Sopenharmony_ci USB_PORT_FEAT_C_PORT_LINK_STATE); 302462306a36Sopenharmony_ci 302562306a36Sopenharmony_ci if (udev) 302662306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 302762306a36Sopenharmony_ci USB_PORT_FEAT_C_CONNECTION); 302862306a36Sopenharmony_ci 302962306a36Sopenharmony_ci /* 303062306a36Sopenharmony_ci * If a USB 3.0 device migrates from reset to an error 303162306a36Sopenharmony_ci * state, re-issue the warm reset. 303262306a36Sopenharmony_ci */ 303362306a36Sopenharmony_ci if (usb_hub_port_status(hub, port1, 303462306a36Sopenharmony_ci &portstatus, &portchange) < 0) 303562306a36Sopenharmony_ci goto done; 303662306a36Sopenharmony_ci 303762306a36Sopenharmony_ci if (!hub_port_warm_reset_required(hub, port1, 303862306a36Sopenharmony_ci portstatus)) 303962306a36Sopenharmony_ci goto done; 304062306a36Sopenharmony_ci 304162306a36Sopenharmony_ci /* 304262306a36Sopenharmony_ci * If the port is in SS.Inactive or Compliance Mode, the 304362306a36Sopenharmony_ci * hot or warm reset failed. Try another warm reset. 304462306a36Sopenharmony_ci */ 304562306a36Sopenharmony_ci if (!warm) { 304662306a36Sopenharmony_ci dev_dbg(&port_dev->dev, 304762306a36Sopenharmony_ci "hot reset failed, warm reset\n"); 304862306a36Sopenharmony_ci warm = true; 304962306a36Sopenharmony_ci } 305062306a36Sopenharmony_ci } 305162306a36Sopenharmony_ci 305262306a36Sopenharmony_ci dev_dbg(&port_dev->dev, 305362306a36Sopenharmony_ci "not enabled, trying %sreset again...\n", 305462306a36Sopenharmony_ci warm ? "warm " : ""); 305562306a36Sopenharmony_ci delay = HUB_LONG_RESET_TIME; 305662306a36Sopenharmony_ci } 305762306a36Sopenharmony_ci 305862306a36Sopenharmony_ci dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); 305962306a36Sopenharmony_ci 306062306a36Sopenharmony_cidone: 306162306a36Sopenharmony_ci if (status == 0) { 306262306a36Sopenharmony_ci if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM) 306362306a36Sopenharmony_ci usleep_range(10000, 12000); 306462306a36Sopenharmony_ci else { 306562306a36Sopenharmony_ci /* TRSTRCY = 10 ms; plus some extra */ 306662306a36Sopenharmony_ci reset_recovery_time = 10 + 40; 306762306a36Sopenharmony_ci 306862306a36Sopenharmony_ci /* Hub needs extra delay after resetting its port. */ 306962306a36Sopenharmony_ci if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET) 307062306a36Sopenharmony_ci reset_recovery_time += 100; 307162306a36Sopenharmony_ci 307262306a36Sopenharmony_ci msleep(reset_recovery_time); 307362306a36Sopenharmony_ci } 307462306a36Sopenharmony_ci 307562306a36Sopenharmony_ci if (udev) { 307662306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 307762306a36Sopenharmony_ci 307862306a36Sopenharmony_ci update_devnum(udev, 0); 307962306a36Sopenharmony_ci /* The xHC may think the device is already reset, 308062306a36Sopenharmony_ci * so ignore the status. 308162306a36Sopenharmony_ci */ 308262306a36Sopenharmony_ci if (hcd->driver->reset_device) 308362306a36Sopenharmony_ci hcd->driver->reset_device(hcd, udev); 308462306a36Sopenharmony_ci 308562306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_DEFAULT); 308662306a36Sopenharmony_ci } 308762306a36Sopenharmony_ci } else { 308862306a36Sopenharmony_ci if (udev) 308962306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_NOTATTACHED); 309062306a36Sopenharmony_ci } 309162306a36Sopenharmony_ci 309262306a36Sopenharmony_ci if (!hub_is_superspeed(hub->hdev)) 309362306a36Sopenharmony_ci up_read(&ehci_cf_port_reset_rwsem); 309462306a36Sopenharmony_ci 309562306a36Sopenharmony_ci return status; 309662306a36Sopenharmony_ci} 309762306a36Sopenharmony_ci 309862306a36Sopenharmony_ci/* 309962306a36Sopenharmony_ci * hub_port_stop_enumerate - stop USB enumeration or ignore port events 310062306a36Sopenharmony_ci * @hub: target hub 310162306a36Sopenharmony_ci * @port1: port num of the port 310262306a36Sopenharmony_ci * @retries: port retries number of hub_port_init() 310362306a36Sopenharmony_ci * 310462306a36Sopenharmony_ci * Return: 310562306a36Sopenharmony_ci * true: ignore port actions/events or give up connection attempts. 310662306a36Sopenharmony_ci * false: keep original behavior. 310762306a36Sopenharmony_ci * 310862306a36Sopenharmony_ci * This function will be based on retries to check whether the port which is 310962306a36Sopenharmony_ci * marked with early_stop attribute would stop enumeration or ignore events. 311062306a36Sopenharmony_ci * 311162306a36Sopenharmony_ci * Note: 311262306a36Sopenharmony_ci * This function didn't change anything if early_stop is not set, and it will 311362306a36Sopenharmony_ci * prevent all connection attempts when early_stop is set and the attempts of 311462306a36Sopenharmony_ci * the port are more than 1. 311562306a36Sopenharmony_ci */ 311662306a36Sopenharmony_cistatic bool hub_port_stop_enumerate(struct usb_hub *hub, int port1, int retries) 311762306a36Sopenharmony_ci{ 311862306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 311962306a36Sopenharmony_ci 312062306a36Sopenharmony_ci if (port_dev->early_stop) { 312162306a36Sopenharmony_ci if (port_dev->ignore_event) 312262306a36Sopenharmony_ci return true; 312362306a36Sopenharmony_ci 312462306a36Sopenharmony_ci /* 312562306a36Sopenharmony_ci * We want unsuccessful attempts to fail quickly. 312662306a36Sopenharmony_ci * Since some devices may need one failure during 312762306a36Sopenharmony_ci * port initialization, we allow two tries but no 312862306a36Sopenharmony_ci * more. 312962306a36Sopenharmony_ci */ 313062306a36Sopenharmony_ci if (retries < 2) 313162306a36Sopenharmony_ci return false; 313262306a36Sopenharmony_ci 313362306a36Sopenharmony_ci port_dev->ignore_event = 1; 313462306a36Sopenharmony_ci } else 313562306a36Sopenharmony_ci port_dev->ignore_event = 0; 313662306a36Sopenharmony_ci 313762306a36Sopenharmony_ci return port_dev->ignore_event; 313862306a36Sopenharmony_ci} 313962306a36Sopenharmony_ci 314062306a36Sopenharmony_ci/* Check if a port is power on */ 314162306a36Sopenharmony_ciint usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus) 314262306a36Sopenharmony_ci{ 314362306a36Sopenharmony_ci int ret = 0; 314462306a36Sopenharmony_ci 314562306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) { 314662306a36Sopenharmony_ci if (portstatus & USB_SS_PORT_STAT_POWER) 314762306a36Sopenharmony_ci ret = 1; 314862306a36Sopenharmony_ci } else { 314962306a36Sopenharmony_ci if (portstatus & USB_PORT_STAT_POWER) 315062306a36Sopenharmony_ci ret = 1; 315162306a36Sopenharmony_ci } 315262306a36Sopenharmony_ci 315362306a36Sopenharmony_ci return ret; 315462306a36Sopenharmony_ci} 315562306a36Sopenharmony_ci 315662306a36Sopenharmony_cistatic void usb_lock_port(struct usb_port *port_dev) 315762306a36Sopenharmony_ci __acquires(&port_dev->status_lock) 315862306a36Sopenharmony_ci{ 315962306a36Sopenharmony_ci mutex_lock(&port_dev->status_lock); 316062306a36Sopenharmony_ci __acquire(&port_dev->status_lock); 316162306a36Sopenharmony_ci} 316262306a36Sopenharmony_ci 316362306a36Sopenharmony_cistatic void usb_unlock_port(struct usb_port *port_dev) 316462306a36Sopenharmony_ci __releases(&port_dev->status_lock) 316562306a36Sopenharmony_ci{ 316662306a36Sopenharmony_ci mutex_unlock(&port_dev->status_lock); 316762306a36Sopenharmony_ci __release(&port_dev->status_lock); 316862306a36Sopenharmony_ci} 316962306a36Sopenharmony_ci 317062306a36Sopenharmony_ci#ifdef CONFIG_PM 317162306a36Sopenharmony_ci 317262306a36Sopenharmony_ci/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 317362306a36Sopenharmony_cistatic int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 317462306a36Sopenharmony_ci{ 317562306a36Sopenharmony_ci int ret = 0; 317662306a36Sopenharmony_ci 317762306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) { 317862306a36Sopenharmony_ci if ((portstatus & USB_PORT_STAT_LINK_STATE) 317962306a36Sopenharmony_ci == USB_SS_PORT_LS_U3) 318062306a36Sopenharmony_ci ret = 1; 318162306a36Sopenharmony_ci } else { 318262306a36Sopenharmony_ci if (portstatus & USB_PORT_STAT_SUSPEND) 318362306a36Sopenharmony_ci ret = 1; 318462306a36Sopenharmony_ci } 318562306a36Sopenharmony_ci 318662306a36Sopenharmony_ci return ret; 318762306a36Sopenharmony_ci} 318862306a36Sopenharmony_ci 318962306a36Sopenharmony_ci/* Determine whether the device on a port is ready for a normal resume, 319062306a36Sopenharmony_ci * is ready for a reset-resume, or should be disconnected. 319162306a36Sopenharmony_ci */ 319262306a36Sopenharmony_cistatic int check_port_resume_type(struct usb_device *udev, 319362306a36Sopenharmony_ci struct usb_hub *hub, int port1, 319462306a36Sopenharmony_ci int status, u16 portchange, u16 portstatus) 319562306a36Sopenharmony_ci{ 319662306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 319762306a36Sopenharmony_ci int retries = 3; 319862306a36Sopenharmony_ci 319962306a36Sopenharmony_ci retry: 320062306a36Sopenharmony_ci /* Is a warm reset needed to recover the connection? */ 320162306a36Sopenharmony_ci if (status == 0 && udev->reset_resume 320262306a36Sopenharmony_ci && hub_port_warm_reset_required(hub, port1, portstatus)) { 320362306a36Sopenharmony_ci /* pass */; 320462306a36Sopenharmony_ci } 320562306a36Sopenharmony_ci /* Is the device still present? */ 320662306a36Sopenharmony_ci else if (status || port_is_suspended(hub, portstatus) || 320762306a36Sopenharmony_ci !usb_port_is_power_on(hub, portstatus)) { 320862306a36Sopenharmony_ci if (status >= 0) 320962306a36Sopenharmony_ci status = -ENODEV; 321062306a36Sopenharmony_ci } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 321162306a36Sopenharmony_ci if (retries--) { 321262306a36Sopenharmony_ci usleep_range(200, 300); 321362306a36Sopenharmony_ci status = usb_hub_port_status(hub, port1, &portstatus, 321462306a36Sopenharmony_ci &portchange); 321562306a36Sopenharmony_ci goto retry; 321662306a36Sopenharmony_ci } 321762306a36Sopenharmony_ci status = -ENODEV; 321862306a36Sopenharmony_ci } 321962306a36Sopenharmony_ci 322062306a36Sopenharmony_ci /* Can't do a normal resume if the port isn't enabled, 322162306a36Sopenharmony_ci * so try a reset-resume instead. 322262306a36Sopenharmony_ci */ 322362306a36Sopenharmony_ci else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 322462306a36Sopenharmony_ci if (udev->persist_enabled) 322562306a36Sopenharmony_ci udev->reset_resume = 1; 322662306a36Sopenharmony_ci else 322762306a36Sopenharmony_ci status = -ENODEV; 322862306a36Sopenharmony_ci } 322962306a36Sopenharmony_ci 323062306a36Sopenharmony_ci if (status) { 323162306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", 323262306a36Sopenharmony_ci portchange, portstatus, status); 323362306a36Sopenharmony_ci } else if (udev->reset_resume) { 323462306a36Sopenharmony_ci 323562306a36Sopenharmony_ci /* Late port handoff can set status-change bits */ 323662306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_CONNECTION) 323762306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 323862306a36Sopenharmony_ci USB_PORT_FEAT_C_CONNECTION); 323962306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_ENABLE) 324062306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 324162306a36Sopenharmony_ci USB_PORT_FEAT_C_ENABLE); 324262306a36Sopenharmony_ci 324362306a36Sopenharmony_ci /* 324462306a36Sopenharmony_ci * Whatever made this reset-resume necessary may have 324562306a36Sopenharmony_ci * turned on the port1 bit in hub->change_bits. But after 324662306a36Sopenharmony_ci * a successful reset-resume we want the bit to be clear; 324762306a36Sopenharmony_ci * if it was on it would indicate that something happened 324862306a36Sopenharmony_ci * following the reset-resume. 324962306a36Sopenharmony_ci */ 325062306a36Sopenharmony_ci clear_bit(port1, hub->change_bits); 325162306a36Sopenharmony_ci } 325262306a36Sopenharmony_ci 325362306a36Sopenharmony_ci return status; 325462306a36Sopenharmony_ci} 325562306a36Sopenharmony_ci 325662306a36Sopenharmony_ciint usb_disable_ltm(struct usb_device *udev) 325762306a36Sopenharmony_ci{ 325862306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 325962306a36Sopenharmony_ci 326062306a36Sopenharmony_ci /* Check if the roothub and device supports LTM. */ 326162306a36Sopenharmony_ci if (!usb_device_supports_ltm(hcd->self.root_hub) || 326262306a36Sopenharmony_ci !usb_device_supports_ltm(udev)) 326362306a36Sopenharmony_ci return 0; 326462306a36Sopenharmony_ci 326562306a36Sopenharmony_ci /* Clear Feature LTM Enable can only be sent if the device is 326662306a36Sopenharmony_ci * configured. 326762306a36Sopenharmony_ci */ 326862306a36Sopenharmony_ci if (!udev->actconfig) 326962306a36Sopenharmony_ci return 0; 327062306a36Sopenharmony_ci 327162306a36Sopenharmony_ci return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 327262306a36Sopenharmony_ci USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 327362306a36Sopenharmony_ci USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 327462306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 327562306a36Sopenharmony_ci} 327662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_disable_ltm); 327762306a36Sopenharmony_ci 327862306a36Sopenharmony_civoid usb_enable_ltm(struct usb_device *udev) 327962306a36Sopenharmony_ci{ 328062306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 328162306a36Sopenharmony_ci 328262306a36Sopenharmony_ci /* Check if the roothub and device supports LTM. */ 328362306a36Sopenharmony_ci if (!usb_device_supports_ltm(hcd->self.root_hub) || 328462306a36Sopenharmony_ci !usb_device_supports_ltm(udev)) 328562306a36Sopenharmony_ci return; 328662306a36Sopenharmony_ci 328762306a36Sopenharmony_ci /* Set Feature LTM Enable can only be sent if the device is 328862306a36Sopenharmony_ci * configured. 328962306a36Sopenharmony_ci */ 329062306a36Sopenharmony_ci if (!udev->actconfig) 329162306a36Sopenharmony_ci return; 329262306a36Sopenharmony_ci 329362306a36Sopenharmony_ci usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 329462306a36Sopenharmony_ci USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 329562306a36Sopenharmony_ci USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 329662306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 329762306a36Sopenharmony_ci} 329862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_enable_ltm); 329962306a36Sopenharmony_ci 330062306a36Sopenharmony_ci/* 330162306a36Sopenharmony_ci * usb_enable_remote_wakeup - enable remote wakeup for a device 330262306a36Sopenharmony_ci * @udev: target device 330362306a36Sopenharmony_ci * 330462306a36Sopenharmony_ci * For USB-2 devices: Set the device's remote wakeup feature. 330562306a36Sopenharmony_ci * 330662306a36Sopenharmony_ci * For USB-3 devices: Assume there's only one function on the device and 330762306a36Sopenharmony_ci * enable remote wake for the first interface. FIXME if the interface 330862306a36Sopenharmony_ci * association descriptor shows there's more than one function. 330962306a36Sopenharmony_ci */ 331062306a36Sopenharmony_cistatic int usb_enable_remote_wakeup(struct usb_device *udev) 331162306a36Sopenharmony_ci{ 331262306a36Sopenharmony_ci if (udev->speed < USB_SPEED_SUPER) 331362306a36Sopenharmony_ci return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 331462306a36Sopenharmony_ci USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 331562306a36Sopenharmony_ci USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 331662306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 331762306a36Sopenharmony_ci else 331862306a36Sopenharmony_ci return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 331962306a36Sopenharmony_ci USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 332062306a36Sopenharmony_ci USB_INTRF_FUNC_SUSPEND, 332162306a36Sopenharmony_ci USB_INTRF_FUNC_SUSPEND_RW | 332262306a36Sopenharmony_ci USB_INTRF_FUNC_SUSPEND_LP, 332362306a36Sopenharmony_ci NULL, 0, USB_CTRL_SET_TIMEOUT); 332462306a36Sopenharmony_ci} 332562306a36Sopenharmony_ci 332662306a36Sopenharmony_ci/* 332762306a36Sopenharmony_ci * usb_disable_remote_wakeup - disable remote wakeup for a device 332862306a36Sopenharmony_ci * @udev: target device 332962306a36Sopenharmony_ci * 333062306a36Sopenharmony_ci * For USB-2 devices: Clear the device's remote wakeup feature. 333162306a36Sopenharmony_ci * 333262306a36Sopenharmony_ci * For USB-3 devices: Assume there's only one function on the device and 333362306a36Sopenharmony_ci * disable remote wake for the first interface. FIXME if the interface 333462306a36Sopenharmony_ci * association descriptor shows there's more than one function. 333562306a36Sopenharmony_ci */ 333662306a36Sopenharmony_cistatic int usb_disable_remote_wakeup(struct usb_device *udev) 333762306a36Sopenharmony_ci{ 333862306a36Sopenharmony_ci if (udev->speed < USB_SPEED_SUPER) 333962306a36Sopenharmony_ci return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 334062306a36Sopenharmony_ci USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 334162306a36Sopenharmony_ci USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 334262306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 334362306a36Sopenharmony_ci else 334462306a36Sopenharmony_ci return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 334562306a36Sopenharmony_ci USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 334662306a36Sopenharmony_ci USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, 334762306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 334862306a36Sopenharmony_ci} 334962306a36Sopenharmony_ci 335062306a36Sopenharmony_ci/* Count of wakeup-enabled devices at or below udev */ 335162306a36Sopenharmony_ciunsigned usb_wakeup_enabled_descendants(struct usb_device *udev) 335262306a36Sopenharmony_ci{ 335362306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev); 335462306a36Sopenharmony_ci 335562306a36Sopenharmony_ci return udev->do_remote_wakeup + 335662306a36Sopenharmony_ci (hub ? hub->wakeup_enabled_descendants : 0); 335762306a36Sopenharmony_ci} 335862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants); 335962306a36Sopenharmony_ci 336062306a36Sopenharmony_ci/* 336162306a36Sopenharmony_ci * usb_port_suspend - suspend a usb device's upstream port 336262306a36Sopenharmony_ci * @udev: device that's no longer in active use, not a root hub 336362306a36Sopenharmony_ci * Context: must be able to sleep; device not locked; pm locks held 336462306a36Sopenharmony_ci * 336562306a36Sopenharmony_ci * Suspends a USB device that isn't in active use, conserving power. 336662306a36Sopenharmony_ci * Devices may wake out of a suspend, if anything important happens, 336762306a36Sopenharmony_ci * using the remote wakeup mechanism. They may also be taken out of 336862306a36Sopenharmony_ci * suspend by the host, using usb_port_resume(). It's also routine 336962306a36Sopenharmony_ci * to disconnect devices while they are suspended. 337062306a36Sopenharmony_ci * 337162306a36Sopenharmony_ci * This only affects the USB hardware for a device; its interfaces 337262306a36Sopenharmony_ci * (and, for hubs, child devices) must already have been suspended. 337362306a36Sopenharmony_ci * 337462306a36Sopenharmony_ci * Selective port suspend reduces power; most suspended devices draw 337562306a36Sopenharmony_ci * less than 500 uA. It's also used in OTG, along with remote wakeup. 337662306a36Sopenharmony_ci * All devices below the suspended port are also suspended. 337762306a36Sopenharmony_ci * 337862306a36Sopenharmony_ci * Devices leave suspend state when the host wakes them up. Some devices 337962306a36Sopenharmony_ci * also support "remote wakeup", where the device can activate the USB 338062306a36Sopenharmony_ci * tree above them to deliver data, such as a keypress or packet. In 338162306a36Sopenharmony_ci * some cases, this wakes the USB host. 338262306a36Sopenharmony_ci * 338362306a36Sopenharmony_ci * Suspending OTG devices may trigger HNP, if that's been enabled 338462306a36Sopenharmony_ci * between a pair of dual-role devices. That will change roles, such 338562306a36Sopenharmony_ci * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 338662306a36Sopenharmony_ci * 338762306a36Sopenharmony_ci * Devices on USB hub ports have only one "suspend" state, corresponding 338862306a36Sopenharmony_ci * to ACPI D2, "may cause the device to lose some context". 338962306a36Sopenharmony_ci * State transitions include: 339062306a36Sopenharmony_ci * 339162306a36Sopenharmony_ci * - suspend, resume ... when the VBUS power link stays live 339262306a36Sopenharmony_ci * - suspend, disconnect ... VBUS lost 339362306a36Sopenharmony_ci * 339462306a36Sopenharmony_ci * Once VBUS drop breaks the circuit, the port it's using has to go through 339562306a36Sopenharmony_ci * normal re-enumeration procedures, starting with enabling VBUS power. 339662306a36Sopenharmony_ci * Other than re-initializing the hub (plug/unplug, except for root hubs), 339762306a36Sopenharmony_ci * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq 339862306a36Sopenharmony_ci * timer, no SRP, no requests through sysfs. 339962306a36Sopenharmony_ci * 340062306a36Sopenharmony_ci * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get 340162306a36Sopenharmony_ci * suspended until their bus goes into global suspend (i.e., the root 340262306a36Sopenharmony_ci * hub is suspended). Nevertheless, we change @udev->state to 340362306a36Sopenharmony_ci * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual 340462306a36Sopenharmony_ci * upstream port setting is stored in @udev->port_is_suspended. 340562306a36Sopenharmony_ci * 340662306a36Sopenharmony_ci * Returns 0 on success, else negative errno. 340762306a36Sopenharmony_ci */ 340862306a36Sopenharmony_ciint usb_port_suspend(struct usb_device *udev, pm_message_t msg) 340962306a36Sopenharmony_ci{ 341062306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 341162306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 341262306a36Sopenharmony_ci int port1 = udev->portnum; 341362306a36Sopenharmony_ci int status; 341462306a36Sopenharmony_ci bool really_suspend = true; 341562306a36Sopenharmony_ci 341662306a36Sopenharmony_ci usb_lock_port(port_dev); 341762306a36Sopenharmony_ci 341862306a36Sopenharmony_ci /* enable remote wakeup when appropriate; this lets the device 341962306a36Sopenharmony_ci * wake up the upstream hub (including maybe the root hub). 342062306a36Sopenharmony_ci * 342162306a36Sopenharmony_ci * NOTE: OTG devices may issue remote wakeup (or SRP) even when 342262306a36Sopenharmony_ci * we don't explicitly enable it here. 342362306a36Sopenharmony_ci */ 342462306a36Sopenharmony_ci if (udev->do_remote_wakeup) { 342562306a36Sopenharmony_ci status = usb_enable_remote_wakeup(udev); 342662306a36Sopenharmony_ci if (status) { 342762306a36Sopenharmony_ci dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 342862306a36Sopenharmony_ci status); 342962306a36Sopenharmony_ci /* bail if autosuspend is requested */ 343062306a36Sopenharmony_ci if (PMSG_IS_AUTO(msg)) 343162306a36Sopenharmony_ci goto err_wakeup; 343262306a36Sopenharmony_ci } 343362306a36Sopenharmony_ci } 343462306a36Sopenharmony_ci 343562306a36Sopenharmony_ci /* disable USB2 hardware LPM */ 343662306a36Sopenharmony_ci usb_disable_usb2_hardware_lpm(udev); 343762306a36Sopenharmony_ci 343862306a36Sopenharmony_ci if (usb_disable_ltm(udev)) { 343962306a36Sopenharmony_ci dev_err(&udev->dev, "Failed to disable LTM before suspend\n"); 344062306a36Sopenharmony_ci status = -ENOMEM; 344162306a36Sopenharmony_ci if (PMSG_IS_AUTO(msg)) 344262306a36Sopenharmony_ci goto err_ltm; 344362306a36Sopenharmony_ci } 344462306a36Sopenharmony_ci 344562306a36Sopenharmony_ci /* see 7.1.7.6 */ 344662306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) 344762306a36Sopenharmony_ci status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); 344862306a36Sopenharmony_ci 344962306a36Sopenharmony_ci /* 345062306a36Sopenharmony_ci * For system suspend, we do not need to enable the suspend feature 345162306a36Sopenharmony_ci * on individual USB-2 ports. The devices will automatically go 345262306a36Sopenharmony_ci * into suspend a few ms after the root hub stops sending packets. 345362306a36Sopenharmony_ci * The USB 2.0 spec calls this "global suspend". 345462306a36Sopenharmony_ci * 345562306a36Sopenharmony_ci * However, many USB hubs have a bug: They don't relay wakeup requests 345662306a36Sopenharmony_ci * from a downstream port if the port's suspend feature isn't on. 345762306a36Sopenharmony_ci * Therefore we will turn on the suspend feature if udev or any of its 345862306a36Sopenharmony_ci * descendants is enabled for remote wakeup. 345962306a36Sopenharmony_ci */ 346062306a36Sopenharmony_ci else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0) 346162306a36Sopenharmony_ci status = set_port_feature(hub->hdev, port1, 346262306a36Sopenharmony_ci USB_PORT_FEAT_SUSPEND); 346362306a36Sopenharmony_ci else { 346462306a36Sopenharmony_ci really_suspend = false; 346562306a36Sopenharmony_ci status = 0; 346662306a36Sopenharmony_ci } 346762306a36Sopenharmony_ci if (status) { 346862306a36Sopenharmony_ci /* Check if the port has been suspended for the timeout case 346962306a36Sopenharmony_ci * to prevent the suspended port from incorrect handling. 347062306a36Sopenharmony_ci */ 347162306a36Sopenharmony_ci if (status == -ETIMEDOUT) { 347262306a36Sopenharmony_ci int ret; 347362306a36Sopenharmony_ci u16 portstatus, portchange; 347462306a36Sopenharmony_ci 347562306a36Sopenharmony_ci portstatus = portchange = 0; 347662306a36Sopenharmony_ci ret = usb_hub_port_status(hub, port1, &portstatus, 347762306a36Sopenharmony_ci &portchange); 347862306a36Sopenharmony_ci 347962306a36Sopenharmony_ci dev_dbg(&port_dev->dev, 348062306a36Sopenharmony_ci "suspend timeout, status %04x\n", portstatus); 348162306a36Sopenharmony_ci 348262306a36Sopenharmony_ci if (ret == 0 && port_is_suspended(hub, portstatus)) { 348362306a36Sopenharmony_ci status = 0; 348462306a36Sopenharmony_ci goto suspend_done; 348562306a36Sopenharmony_ci } 348662306a36Sopenharmony_ci } 348762306a36Sopenharmony_ci 348862306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); 348962306a36Sopenharmony_ci 349062306a36Sopenharmony_ci /* Try to enable USB3 LTM again */ 349162306a36Sopenharmony_ci usb_enable_ltm(udev); 349262306a36Sopenharmony_ci err_ltm: 349362306a36Sopenharmony_ci /* Try to enable USB2 hardware LPM again */ 349462306a36Sopenharmony_ci usb_enable_usb2_hardware_lpm(udev); 349562306a36Sopenharmony_ci 349662306a36Sopenharmony_ci if (udev->do_remote_wakeup) 349762306a36Sopenharmony_ci (void) usb_disable_remote_wakeup(udev); 349862306a36Sopenharmony_ci err_wakeup: 349962306a36Sopenharmony_ci 350062306a36Sopenharmony_ci /* System sleep transitions should never fail */ 350162306a36Sopenharmony_ci if (!PMSG_IS_AUTO(msg)) 350262306a36Sopenharmony_ci status = 0; 350362306a36Sopenharmony_ci } else { 350462306a36Sopenharmony_ci suspend_done: 350562306a36Sopenharmony_ci dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 350662306a36Sopenharmony_ci (PMSG_IS_AUTO(msg) ? "auto-" : ""), 350762306a36Sopenharmony_ci udev->do_remote_wakeup); 350862306a36Sopenharmony_ci if (really_suspend) { 350962306a36Sopenharmony_ci udev->port_is_suspended = 1; 351062306a36Sopenharmony_ci 351162306a36Sopenharmony_ci /* device has up to 10 msec to fully suspend */ 351262306a36Sopenharmony_ci msleep(10); 351362306a36Sopenharmony_ci } 351462306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_SUSPENDED); 351562306a36Sopenharmony_ci } 351662306a36Sopenharmony_ci 351762306a36Sopenharmony_ci if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled 351862306a36Sopenharmony_ci && test_and_clear_bit(port1, hub->child_usage_bits)) 351962306a36Sopenharmony_ci pm_runtime_put_sync(&port_dev->dev); 352062306a36Sopenharmony_ci 352162306a36Sopenharmony_ci usb_mark_last_busy(hub->hdev); 352262306a36Sopenharmony_ci 352362306a36Sopenharmony_ci usb_unlock_port(port_dev); 352462306a36Sopenharmony_ci return status; 352562306a36Sopenharmony_ci} 352662306a36Sopenharmony_ci 352762306a36Sopenharmony_ci/* 352862306a36Sopenharmony_ci * If the USB "suspend" state is in use (rather than "global suspend"), 352962306a36Sopenharmony_ci * many devices will be individually taken out of suspend state using 353062306a36Sopenharmony_ci * special "resume" signaling. This routine kicks in shortly after 353162306a36Sopenharmony_ci * hardware resume signaling is finished, either because of selective 353262306a36Sopenharmony_ci * resume (by host) or remote wakeup (by device) ... now see what changed 353362306a36Sopenharmony_ci * in the tree that's rooted at this device. 353462306a36Sopenharmony_ci * 353562306a36Sopenharmony_ci * If @udev->reset_resume is set then the device is reset before the 353662306a36Sopenharmony_ci * status check is done. 353762306a36Sopenharmony_ci */ 353862306a36Sopenharmony_cistatic int finish_port_resume(struct usb_device *udev) 353962306a36Sopenharmony_ci{ 354062306a36Sopenharmony_ci int status = 0; 354162306a36Sopenharmony_ci u16 devstatus = 0; 354262306a36Sopenharmony_ci 354362306a36Sopenharmony_ci /* caller owns the udev device lock */ 354462306a36Sopenharmony_ci dev_dbg(&udev->dev, "%s\n", 354562306a36Sopenharmony_ci udev->reset_resume ? "finish reset-resume" : "finish resume"); 354662306a36Sopenharmony_ci 354762306a36Sopenharmony_ci /* usb ch9 identifies four variants of SUSPENDED, based on what 354862306a36Sopenharmony_ci * state the device resumes to. Linux currently won't see the 354962306a36Sopenharmony_ci * first two on the host side; they'd be inside hub_port_init() 355062306a36Sopenharmony_ci * during many timeouts, but hub_wq can't suspend until later. 355162306a36Sopenharmony_ci */ 355262306a36Sopenharmony_ci usb_set_device_state(udev, udev->actconfig 355362306a36Sopenharmony_ci ? USB_STATE_CONFIGURED 355462306a36Sopenharmony_ci : USB_STATE_ADDRESS); 355562306a36Sopenharmony_ci 355662306a36Sopenharmony_ci /* 10.5.4.5 says not to reset a suspended port if the attached 355762306a36Sopenharmony_ci * device is enabled for remote wakeup. Hence the reset 355862306a36Sopenharmony_ci * operation is carried out here, after the port has been 355962306a36Sopenharmony_ci * resumed. 356062306a36Sopenharmony_ci */ 356162306a36Sopenharmony_ci if (udev->reset_resume) { 356262306a36Sopenharmony_ci /* 356362306a36Sopenharmony_ci * If the device morphs or switches modes when it is reset, 356462306a36Sopenharmony_ci * we don't want to perform a reset-resume. We'll fail the 356562306a36Sopenharmony_ci * resume, which will cause a logical disconnect, and then 356662306a36Sopenharmony_ci * the device will be rediscovered. 356762306a36Sopenharmony_ci */ 356862306a36Sopenharmony_ci retry_reset_resume: 356962306a36Sopenharmony_ci if (udev->quirks & USB_QUIRK_RESET) 357062306a36Sopenharmony_ci status = -ENODEV; 357162306a36Sopenharmony_ci else 357262306a36Sopenharmony_ci status = usb_reset_and_verify_device(udev); 357362306a36Sopenharmony_ci } 357462306a36Sopenharmony_ci 357562306a36Sopenharmony_ci /* 10.5.4.5 says be sure devices in the tree are still there. 357662306a36Sopenharmony_ci * For now let's assume the device didn't go crazy on resume, 357762306a36Sopenharmony_ci * and device drivers will know about any resume quirks. 357862306a36Sopenharmony_ci */ 357962306a36Sopenharmony_ci if (status == 0) { 358062306a36Sopenharmony_ci devstatus = 0; 358162306a36Sopenharmony_ci status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 358262306a36Sopenharmony_ci 358362306a36Sopenharmony_ci /* If a normal resume failed, try doing a reset-resume */ 358462306a36Sopenharmony_ci if (status && !udev->reset_resume && udev->persist_enabled) { 358562306a36Sopenharmony_ci dev_dbg(&udev->dev, "retry with reset-resume\n"); 358662306a36Sopenharmony_ci udev->reset_resume = 1; 358762306a36Sopenharmony_ci goto retry_reset_resume; 358862306a36Sopenharmony_ci } 358962306a36Sopenharmony_ci } 359062306a36Sopenharmony_ci 359162306a36Sopenharmony_ci if (status) { 359262306a36Sopenharmony_ci dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 359362306a36Sopenharmony_ci status); 359462306a36Sopenharmony_ci /* 359562306a36Sopenharmony_ci * There are a few quirky devices which violate the standard 359662306a36Sopenharmony_ci * by claiming to have remote wakeup enabled after a reset, 359762306a36Sopenharmony_ci * which crash if the feature is cleared, hence check for 359862306a36Sopenharmony_ci * udev->reset_resume 359962306a36Sopenharmony_ci */ 360062306a36Sopenharmony_ci } else if (udev->actconfig && !udev->reset_resume) { 360162306a36Sopenharmony_ci if (udev->speed < USB_SPEED_SUPER) { 360262306a36Sopenharmony_ci if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) 360362306a36Sopenharmony_ci status = usb_disable_remote_wakeup(udev); 360462306a36Sopenharmony_ci } else { 360562306a36Sopenharmony_ci status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0, 360662306a36Sopenharmony_ci &devstatus); 360762306a36Sopenharmony_ci if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP 360862306a36Sopenharmony_ci | USB_INTRF_STAT_FUNC_RW)) 360962306a36Sopenharmony_ci status = usb_disable_remote_wakeup(udev); 361062306a36Sopenharmony_ci } 361162306a36Sopenharmony_ci 361262306a36Sopenharmony_ci if (status) 361362306a36Sopenharmony_ci dev_dbg(&udev->dev, 361462306a36Sopenharmony_ci "disable remote wakeup, status %d\n", 361562306a36Sopenharmony_ci status); 361662306a36Sopenharmony_ci status = 0; 361762306a36Sopenharmony_ci } 361862306a36Sopenharmony_ci return status; 361962306a36Sopenharmony_ci} 362062306a36Sopenharmony_ci 362162306a36Sopenharmony_ci/* 362262306a36Sopenharmony_ci * There are some SS USB devices which take longer time for link training. 362362306a36Sopenharmony_ci * XHCI specs 4.19.4 says that when Link training is successful, port 362462306a36Sopenharmony_ci * sets CCS bit to 1. So if SW reads port status before successful link 362562306a36Sopenharmony_ci * training, then it will not find device to be present. 362662306a36Sopenharmony_ci * USB Analyzer log with such buggy devices show that in some cases 362762306a36Sopenharmony_ci * device switch on the RX termination after long delay of host enabling 362862306a36Sopenharmony_ci * the VBUS. In few other cases it has been seen that device fails to 362962306a36Sopenharmony_ci * negotiate link training in first attempt. It has been 363062306a36Sopenharmony_ci * reported till now that few devices take as long as 2000 ms to train 363162306a36Sopenharmony_ci * the link after host enabling its VBUS and termination. Following 363262306a36Sopenharmony_ci * routine implements a 2000 ms timeout for link training. If in a case 363362306a36Sopenharmony_ci * link trains before timeout, loop will exit earlier. 363462306a36Sopenharmony_ci * 363562306a36Sopenharmony_ci * There are also some 2.0 hard drive based devices and 3.0 thumb 363662306a36Sopenharmony_ci * drives that, when plugged into a 2.0 only port, take a long 363762306a36Sopenharmony_ci * time to set CCS after VBUS enable. 363862306a36Sopenharmony_ci * 363962306a36Sopenharmony_ci * FIXME: If a device was connected before suspend, but was removed 364062306a36Sopenharmony_ci * while system was asleep, then the loop in the following routine will 364162306a36Sopenharmony_ci * only exit at timeout. 364262306a36Sopenharmony_ci * 364362306a36Sopenharmony_ci * This routine should only be called when persist is enabled. 364462306a36Sopenharmony_ci */ 364562306a36Sopenharmony_cistatic int wait_for_connected(struct usb_device *udev, 364662306a36Sopenharmony_ci struct usb_hub *hub, int port1, 364762306a36Sopenharmony_ci u16 *portchange, u16 *portstatus) 364862306a36Sopenharmony_ci{ 364962306a36Sopenharmony_ci int status = 0, delay_ms = 0; 365062306a36Sopenharmony_ci 365162306a36Sopenharmony_ci while (delay_ms < 2000) { 365262306a36Sopenharmony_ci if (status || *portstatus & USB_PORT_STAT_CONNECTION) 365362306a36Sopenharmony_ci break; 365462306a36Sopenharmony_ci if (!usb_port_is_power_on(hub, *portstatus)) { 365562306a36Sopenharmony_ci status = -ENODEV; 365662306a36Sopenharmony_ci break; 365762306a36Sopenharmony_ci } 365862306a36Sopenharmony_ci msleep(20); 365962306a36Sopenharmony_ci delay_ms += 20; 366062306a36Sopenharmony_ci status = usb_hub_port_status(hub, port1, portstatus, portchange); 366162306a36Sopenharmony_ci } 366262306a36Sopenharmony_ci dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); 366362306a36Sopenharmony_ci return status; 366462306a36Sopenharmony_ci} 366562306a36Sopenharmony_ci 366662306a36Sopenharmony_ci/* 366762306a36Sopenharmony_ci * usb_port_resume - re-activate a suspended usb device's upstream port 366862306a36Sopenharmony_ci * @udev: device to re-activate, not a root hub 366962306a36Sopenharmony_ci * Context: must be able to sleep; device not locked; pm locks held 367062306a36Sopenharmony_ci * 367162306a36Sopenharmony_ci * This will re-activate the suspended device, increasing power usage 367262306a36Sopenharmony_ci * while letting drivers communicate again with its endpoints. 367362306a36Sopenharmony_ci * USB resume explicitly guarantees that the power session between 367462306a36Sopenharmony_ci * the host and the device is the same as it was when the device 367562306a36Sopenharmony_ci * suspended. 367662306a36Sopenharmony_ci * 367762306a36Sopenharmony_ci * If @udev->reset_resume is set then this routine won't check that the 367862306a36Sopenharmony_ci * port is still enabled. Furthermore, finish_port_resume() above will 367962306a36Sopenharmony_ci * reset @udev. The end result is that a broken power session can be 368062306a36Sopenharmony_ci * recovered and @udev will appear to persist across a loss of VBUS power. 368162306a36Sopenharmony_ci * 368262306a36Sopenharmony_ci * For example, if a host controller doesn't maintain VBUS suspend current 368362306a36Sopenharmony_ci * during a system sleep or is reset when the system wakes up, all the USB 368462306a36Sopenharmony_ci * power sessions below it will be broken. This is especially troublesome 368562306a36Sopenharmony_ci * for mass-storage devices containing mounted filesystems, since the 368662306a36Sopenharmony_ci * device will appear to have disconnected and all the memory mappings 368762306a36Sopenharmony_ci * to it will be lost. Using the USB_PERSIST facility, the device can be 368862306a36Sopenharmony_ci * made to appear as if it had not disconnected. 368962306a36Sopenharmony_ci * 369062306a36Sopenharmony_ci * This facility can be dangerous. Although usb_reset_and_verify_device() makes 369162306a36Sopenharmony_ci * every effort to insure that the same device is present after the 369262306a36Sopenharmony_ci * reset as before, it cannot provide a 100% guarantee. Furthermore it's 369362306a36Sopenharmony_ci * quite possible for a device to remain unaltered but its media to be 369462306a36Sopenharmony_ci * changed. If the user replaces a flash memory card while the system is 369562306a36Sopenharmony_ci * asleep, he will have only himself to blame when the filesystem on the 369662306a36Sopenharmony_ci * new card is corrupted and the system crashes. 369762306a36Sopenharmony_ci * 369862306a36Sopenharmony_ci * Returns 0 on success, else negative errno. 369962306a36Sopenharmony_ci */ 370062306a36Sopenharmony_ciint usb_port_resume(struct usb_device *udev, pm_message_t msg) 370162306a36Sopenharmony_ci{ 370262306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 370362306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 370462306a36Sopenharmony_ci int port1 = udev->portnum; 370562306a36Sopenharmony_ci int status; 370662306a36Sopenharmony_ci u16 portchange, portstatus; 370762306a36Sopenharmony_ci 370862306a36Sopenharmony_ci if (!test_and_set_bit(port1, hub->child_usage_bits)) { 370962306a36Sopenharmony_ci status = pm_runtime_resume_and_get(&port_dev->dev); 371062306a36Sopenharmony_ci if (status < 0) { 371162306a36Sopenharmony_ci dev_dbg(&udev->dev, "can't resume usb port, status %d\n", 371262306a36Sopenharmony_ci status); 371362306a36Sopenharmony_ci return status; 371462306a36Sopenharmony_ci } 371562306a36Sopenharmony_ci } 371662306a36Sopenharmony_ci 371762306a36Sopenharmony_ci usb_lock_port(port_dev); 371862306a36Sopenharmony_ci 371962306a36Sopenharmony_ci /* Skip the initial Clear-Suspend step for a remote wakeup */ 372062306a36Sopenharmony_ci status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 372162306a36Sopenharmony_ci if (status == 0 && !port_is_suspended(hub, portstatus)) { 372262306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_SUSPEND) 372362306a36Sopenharmony_ci pm_wakeup_event(&udev->dev, 0); 372462306a36Sopenharmony_ci goto SuspendCleared; 372562306a36Sopenharmony_ci } 372662306a36Sopenharmony_ci 372762306a36Sopenharmony_ci /* see 7.1.7.7; affects power usage, but not budgeting */ 372862306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) 372962306a36Sopenharmony_ci status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); 373062306a36Sopenharmony_ci else 373162306a36Sopenharmony_ci status = usb_clear_port_feature(hub->hdev, 373262306a36Sopenharmony_ci port1, USB_PORT_FEAT_SUSPEND); 373362306a36Sopenharmony_ci if (status) { 373462306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); 373562306a36Sopenharmony_ci } else { 373662306a36Sopenharmony_ci /* drive resume for USB_RESUME_TIMEOUT msec */ 373762306a36Sopenharmony_ci dev_dbg(&udev->dev, "usb %sresume\n", 373862306a36Sopenharmony_ci (PMSG_IS_AUTO(msg) ? "auto-" : "")); 373962306a36Sopenharmony_ci msleep(USB_RESUME_TIMEOUT); 374062306a36Sopenharmony_ci 374162306a36Sopenharmony_ci /* Virtual root hubs can trigger on GET_PORT_STATUS to 374262306a36Sopenharmony_ci * stop resume signaling. Then finish the resume 374362306a36Sopenharmony_ci * sequence. 374462306a36Sopenharmony_ci */ 374562306a36Sopenharmony_ci status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 374662306a36Sopenharmony_ci } 374762306a36Sopenharmony_ci 374862306a36Sopenharmony_ci SuspendCleared: 374962306a36Sopenharmony_ci if (status == 0) { 375062306a36Sopenharmony_ci udev->port_is_suspended = 0; 375162306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) { 375262306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_LINK_STATE) 375362306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 375462306a36Sopenharmony_ci USB_PORT_FEAT_C_PORT_LINK_STATE); 375562306a36Sopenharmony_ci } else { 375662306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_SUSPEND) 375762306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 375862306a36Sopenharmony_ci USB_PORT_FEAT_C_SUSPEND); 375962306a36Sopenharmony_ci } 376062306a36Sopenharmony_ci 376162306a36Sopenharmony_ci /* TRSMRCY = 10 msec */ 376262306a36Sopenharmony_ci msleep(10); 376362306a36Sopenharmony_ci } 376462306a36Sopenharmony_ci 376562306a36Sopenharmony_ci if (udev->persist_enabled) 376662306a36Sopenharmony_ci status = wait_for_connected(udev, hub, port1, &portchange, 376762306a36Sopenharmony_ci &portstatus); 376862306a36Sopenharmony_ci 376962306a36Sopenharmony_ci status = check_port_resume_type(udev, 377062306a36Sopenharmony_ci hub, port1, status, portchange, portstatus); 377162306a36Sopenharmony_ci if (status == 0) 377262306a36Sopenharmony_ci status = finish_port_resume(udev); 377362306a36Sopenharmony_ci if (status < 0) { 377462306a36Sopenharmony_ci dev_dbg(&udev->dev, "can't resume, status %d\n", status); 377562306a36Sopenharmony_ci hub_port_logical_disconnect(hub, port1); 377662306a36Sopenharmony_ci } else { 377762306a36Sopenharmony_ci /* Try to enable USB2 hardware LPM */ 377862306a36Sopenharmony_ci usb_enable_usb2_hardware_lpm(udev); 377962306a36Sopenharmony_ci 378062306a36Sopenharmony_ci /* Try to enable USB3 LTM */ 378162306a36Sopenharmony_ci usb_enable_ltm(udev); 378262306a36Sopenharmony_ci } 378362306a36Sopenharmony_ci 378462306a36Sopenharmony_ci usb_unlock_port(port_dev); 378562306a36Sopenharmony_ci 378662306a36Sopenharmony_ci return status; 378762306a36Sopenharmony_ci} 378862306a36Sopenharmony_ci 378962306a36Sopenharmony_ciint usb_remote_wakeup(struct usb_device *udev) 379062306a36Sopenharmony_ci{ 379162306a36Sopenharmony_ci int status = 0; 379262306a36Sopenharmony_ci 379362306a36Sopenharmony_ci usb_lock_device(udev); 379462306a36Sopenharmony_ci if (udev->state == USB_STATE_SUSPENDED) { 379562306a36Sopenharmony_ci dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 379662306a36Sopenharmony_ci status = usb_autoresume_device(udev); 379762306a36Sopenharmony_ci if (status == 0) { 379862306a36Sopenharmony_ci /* Let the drivers do their thing, then... */ 379962306a36Sopenharmony_ci usb_autosuspend_device(udev); 380062306a36Sopenharmony_ci } 380162306a36Sopenharmony_ci } 380262306a36Sopenharmony_ci usb_unlock_device(udev); 380362306a36Sopenharmony_ci return status; 380462306a36Sopenharmony_ci} 380562306a36Sopenharmony_ci 380662306a36Sopenharmony_ci/* Returns 1 if there was a remote wakeup and a connect status change. */ 380762306a36Sopenharmony_cistatic int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 380862306a36Sopenharmony_ci u16 portstatus, u16 portchange) 380962306a36Sopenharmony_ci __must_hold(&port_dev->status_lock) 381062306a36Sopenharmony_ci{ 381162306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port - 1]; 381262306a36Sopenharmony_ci struct usb_device *hdev; 381362306a36Sopenharmony_ci struct usb_device *udev; 381462306a36Sopenharmony_ci int connect_change = 0; 381562306a36Sopenharmony_ci u16 link_state; 381662306a36Sopenharmony_ci int ret; 381762306a36Sopenharmony_ci 381862306a36Sopenharmony_ci hdev = hub->hdev; 381962306a36Sopenharmony_ci udev = port_dev->child; 382062306a36Sopenharmony_ci if (!hub_is_superspeed(hdev)) { 382162306a36Sopenharmony_ci if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 382262306a36Sopenharmony_ci return 0; 382362306a36Sopenharmony_ci usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 382462306a36Sopenharmony_ci } else { 382562306a36Sopenharmony_ci link_state = portstatus & USB_PORT_STAT_LINK_STATE; 382662306a36Sopenharmony_ci if (!udev || udev->state != USB_STATE_SUSPENDED || 382762306a36Sopenharmony_ci (link_state != USB_SS_PORT_LS_U0 && 382862306a36Sopenharmony_ci link_state != USB_SS_PORT_LS_U1 && 382962306a36Sopenharmony_ci link_state != USB_SS_PORT_LS_U2)) 383062306a36Sopenharmony_ci return 0; 383162306a36Sopenharmony_ci } 383262306a36Sopenharmony_ci 383362306a36Sopenharmony_ci if (udev) { 383462306a36Sopenharmony_ci /* TRSMRCY = 10 msec */ 383562306a36Sopenharmony_ci msleep(10); 383662306a36Sopenharmony_ci 383762306a36Sopenharmony_ci usb_unlock_port(port_dev); 383862306a36Sopenharmony_ci ret = usb_remote_wakeup(udev); 383962306a36Sopenharmony_ci usb_lock_port(port_dev); 384062306a36Sopenharmony_ci if (ret < 0) 384162306a36Sopenharmony_ci connect_change = 1; 384262306a36Sopenharmony_ci } else { 384362306a36Sopenharmony_ci ret = -ENODEV; 384462306a36Sopenharmony_ci hub_port_disable(hub, port, 1); 384562306a36Sopenharmony_ci } 384662306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "resume, status %d\n", ret); 384762306a36Sopenharmony_ci return connect_change; 384862306a36Sopenharmony_ci} 384962306a36Sopenharmony_ci 385062306a36Sopenharmony_cistatic int check_ports_changed(struct usb_hub *hub) 385162306a36Sopenharmony_ci{ 385262306a36Sopenharmony_ci int port1; 385362306a36Sopenharmony_ci 385462306a36Sopenharmony_ci for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { 385562306a36Sopenharmony_ci u16 portstatus, portchange; 385662306a36Sopenharmony_ci int status; 385762306a36Sopenharmony_ci 385862306a36Sopenharmony_ci status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 385962306a36Sopenharmony_ci if (!status && portchange) 386062306a36Sopenharmony_ci return 1; 386162306a36Sopenharmony_ci } 386262306a36Sopenharmony_ci return 0; 386362306a36Sopenharmony_ci} 386462306a36Sopenharmony_ci 386562306a36Sopenharmony_cistatic int hub_suspend(struct usb_interface *intf, pm_message_t msg) 386662306a36Sopenharmony_ci{ 386762306a36Sopenharmony_ci struct usb_hub *hub = usb_get_intfdata(intf); 386862306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 386962306a36Sopenharmony_ci unsigned port1; 387062306a36Sopenharmony_ci 387162306a36Sopenharmony_ci /* 387262306a36Sopenharmony_ci * Warn if children aren't already suspended. 387362306a36Sopenharmony_ci * Also, add up the number of wakeup-enabled descendants. 387462306a36Sopenharmony_ci */ 387562306a36Sopenharmony_ci hub->wakeup_enabled_descendants = 0; 387662306a36Sopenharmony_ci for (port1 = 1; port1 <= hdev->maxchild; port1++) { 387762306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 387862306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 387962306a36Sopenharmony_ci 388062306a36Sopenharmony_ci if (udev && udev->can_submit) { 388162306a36Sopenharmony_ci dev_warn(&port_dev->dev, "device %s not suspended yet\n", 388262306a36Sopenharmony_ci dev_name(&udev->dev)); 388362306a36Sopenharmony_ci if (PMSG_IS_AUTO(msg)) 388462306a36Sopenharmony_ci return -EBUSY; 388562306a36Sopenharmony_ci } 388662306a36Sopenharmony_ci if (udev) 388762306a36Sopenharmony_ci hub->wakeup_enabled_descendants += 388862306a36Sopenharmony_ci usb_wakeup_enabled_descendants(udev); 388962306a36Sopenharmony_ci } 389062306a36Sopenharmony_ci 389162306a36Sopenharmony_ci if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { 389262306a36Sopenharmony_ci /* check if there are changes pending on hub ports */ 389362306a36Sopenharmony_ci if (check_ports_changed(hub)) { 389462306a36Sopenharmony_ci if (PMSG_IS_AUTO(msg)) 389562306a36Sopenharmony_ci return -EBUSY; 389662306a36Sopenharmony_ci pm_wakeup_event(&hdev->dev, 2000); 389762306a36Sopenharmony_ci } 389862306a36Sopenharmony_ci } 389962306a36Sopenharmony_ci 390062306a36Sopenharmony_ci if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 390162306a36Sopenharmony_ci /* Enable hub to send remote wakeup for all ports. */ 390262306a36Sopenharmony_ci for (port1 = 1; port1 <= hdev->maxchild; port1++) { 390362306a36Sopenharmony_ci set_port_feature(hdev, 390462306a36Sopenharmony_ci port1 | 390562306a36Sopenharmony_ci USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 390662306a36Sopenharmony_ci USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 390762306a36Sopenharmony_ci USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 390862306a36Sopenharmony_ci USB_PORT_FEAT_REMOTE_WAKE_MASK); 390962306a36Sopenharmony_ci } 391062306a36Sopenharmony_ci } 391162306a36Sopenharmony_ci 391262306a36Sopenharmony_ci dev_dbg(&intf->dev, "%s\n", __func__); 391362306a36Sopenharmony_ci 391462306a36Sopenharmony_ci /* stop hub_wq and related activity */ 391562306a36Sopenharmony_ci hub_quiesce(hub, HUB_SUSPEND); 391662306a36Sopenharmony_ci return 0; 391762306a36Sopenharmony_ci} 391862306a36Sopenharmony_ci 391962306a36Sopenharmony_ci/* Report wakeup requests from the ports of a resuming root hub */ 392062306a36Sopenharmony_cistatic void report_wakeup_requests(struct usb_hub *hub) 392162306a36Sopenharmony_ci{ 392262306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 392362306a36Sopenharmony_ci struct usb_device *udev; 392462306a36Sopenharmony_ci struct usb_hcd *hcd; 392562306a36Sopenharmony_ci unsigned long resuming_ports; 392662306a36Sopenharmony_ci int i; 392762306a36Sopenharmony_ci 392862306a36Sopenharmony_ci if (hdev->parent) 392962306a36Sopenharmony_ci return; /* Not a root hub */ 393062306a36Sopenharmony_ci 393162306a36Sopenharmony_ci hcd = bus_to_hcd(hdev->bus); 393262306a36Sopenharmony_ci if (hcd->driver->get_resuming_ports) { 393362306a36Sopenharmony_ci 393462306a36Sopenharmony_ci /* 393562306a36Sopenharmony_ci * The get_resuming_ports() method returns a bitmap (origin 0) 393662306a36Sopenharmony_ci * of ports which have started wakeup signaling but have not 393762306a36Sopenharmony_ci * yet finished resuming. During system resume we will 393862306a36Sopenharmony_ci * resume all the enabled ports, regardless of any wakeup 393962306a36Sopenharmony_ci * signals, which means the wakeup requests would be lost. 394062306a36Sopenharmony_ci * To prevent this, report them to the PM core here. 394162306a36Sopenharmony_ci */ 394262306a36Sopenharmony_ci resuming_ports = hcd->driver->get_resuming_ports(hcd); 394362306a36Sopenharmony_ci for (i = 0; i < hdev->maxchild; ++i) { 394462306a36Sopenharmony_ci if (test_bit(i, &resuming_ports)) { 394562306a36Sopenharmony_ci udev = hub->ports[i]->child; 394662306a36Sopenharmony_ci if (udev) 394762306a36Sopenharmony_ci pm_wakeup_event(&udev->dev, 0); 394862306a36Sopenharmony_ci } 394962306a36Sopenharmony_ci } 395062306a36Sopenharmony_ci } 395162306a36Sopenharmony_ci} 395262306a36Sopenharmony_ci 395362306a36Sopenharmony_cistatic int hub_resume(struct usb_interface *intf) 395462306a36Sopenharmony_ci{ 395562306a36Sopenharmony_ci struct usb_hub *hub = usb_get_intfdata(intf); 395662306a36Sopenharmony_ci 395762306a36Sopenharmony_ci dev_dbg(&intf->dev, "%s\n", __func__); 395862306a36Sopenharmony_ci hub_activate(hub, HUB_RESUME); 395962306a36Sopenharmony_ci 396062306a36Sopenharmony_ci /* 396162306a36Sopenharmony_ci * This should be called only for system resume, not runtime resume. 396262306a36Sopenharmony_ci * We can't tell the difference here, so some wakeup requests will be 396362306a36Sopenharmony_ci * reported at the wrong time or more than once. This shouldn't 396462306a36Sopenharmony_ci * matter much, so long as they do get reported. 396562306a36Sopenharmony_ci */ 396662306a36Sopenharmony_ci report_wakeup_requests(hub); 396762306a36Sopenharmony_ci return 0; 396862306a36Sopenharmony_ci} 396962306a36Sopenharmony_ci 397062306a36Sopenharmony_cistatic int hub_reset_resume(struct usb_interface *intf) 397162306a36Sopenharmony_ci{ 397262306a36Sopenharmony_ci struct usb_hub *hub = usb_get_intfdata(intf); 397362306a36Sopenharmony_ci 397462306a36Sopenharmony_ci dev_dbg(&intf->dev, "%s\n", __func__); 397562306a36Sopenharmony_ci hub_activate(hub, HUB_RESET_RESUME); 397662306a36Sopenharmony_ci return 0; 397762306a36Sopenharmony_ci} 397862306a36Sopenharmony_ci 397962306a36Sopenharmony_ci/** 398062306a36Sopenharmony_ci * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 398162306a36Sopenharmony_ci * @rhdev: struct usb_device for the root hub 398262306a36Sopenharmony_ci * 398362306a36Sopenharmony_ci * The USB host controller driver calls this function when its root hub 398462306a36Sopenharmony_ci * is resumed and Vbus power has been interrupted or the controller 398562306a36Sopenharmony_ci * has been reset. The routine marks @rhdev as having lost power. 398662306a36Sopenharmony_ci * When the hub driver is resumed it will take notice and carry out 398762306a36Sopenharmony_ci * power-session recovery for all the "USB-PERSIST"-enabled child devices; 398862306a36Sopenharmony_ci * the others will be disconnected. 398962306a36Sopenharmony_ci */ 399062306a36Sopenharmony_civoid usb_root_hub_lost_power(struct usb_device *rhdev) 399162306a36Sopenharmony_ci{ 399262306a36Sopenharmony_ci dev_notice(&rhdev->dev, "root hub lost power or was reset\n"); 399362306a36Sopenharmony_ci rhdev->reset_resume = 1; 399462306a36Sopenharmony_ci} 399562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 399662306a36Sopenharmony_ci 399762306a36Sopenharmony_cistatic const char * const usb3_lpm_names[] = { 399862306a36Sopenharmony_ci "U0", 399962306a36Sopenharmony_ci "U1", 400062306a36Sopenharmony_ci "U2", 400162306a36Sopenharmony_ci "U3", 400262306a36Sopenharmony_ci}; 400362306a36Sopenharmony_ci 400462306a36Sopenharmony_ci/* 400562306a36Sopenharmony_ci * Send a Set SEL control transfer to the device, prior to enabling 400662306a36Sopenharmony_ci * device-initiated U1 or U2. This lets the device know the exit latencies from 400762306a36Sopenharmony_ci * the time the device initiates a U1 or U2 exit, to the time it will receive a 400862306a36Sopenharmony_ci * packet from the host. 400962306a36Sopenharmony_ci * 401062306a36Sopenharmony_ci * This function will fail if the SEL or PEL values for udev are greater than 401162306a36Sopenharmony_ci * the maximum allowed values for the link state to be enabled. 401262306a36Sopenharmony_ci */ 401362306a36Sopenharmony_cistatic int usb_req_set_sel(struct usb_device *udev) 401462306a36Sopenharmony_ci{ 401562306a36Sopenharmony_ci struct usb_set_sel_req *sel_values; 401662306a36Sopenharmony_ci unsigned long long u1_sel; 401762306a36Sopenharmony_ci unsigned long long u1_pel; 401862306a36Sopenharmony_ci unsigned long long u2_sel; 401962306a36Sopenharmony_ci unsigned long long u2_pel; 402062306a36Sopenharmony_ci int ret; 402162306a36Sopenharmony_ci 402262306a36Sopenharmony_ci if (!udev->parent || udev->speed < USB_SPEED_SUPER || !udev->lpm_capable) 402362306a36Sopenharmony_ci return 0; 402462306a36Sopenharmony_ci 402562306a36Sopenharmony_ci /* Convert SEL and PEL stored in ns to us */ 402662306a36Sopenharmony_ci u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 402762306a36Sopenharmony_ci u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 402862306a36Sopenharmony_ci u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 402962306a36Sopenharmony_ci u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 403062306a36Sopenharmony_ci 403162306a36Sopenharmony_ci /* 403262306a36Sopenharmony_ci * Make sure that the calculated SEL and PEL values for the link 403362306a36Sopenharmony_ci * state we're enabling aren't bigger than the max SEL/PEL 403462306a36Sopenharmony_ci * value that will fit in the SET SEL control transfer. 403562306a36Sopenharmony_ci * Otherwise the device would get an incorrect idea of the exit 403662306a36Sopenharmony_ci * latency for the link state, and could start a device-initiated 403762306a36Sopenharmony_ci * U1/U2 when the exit latencies are too high. 403862306a36Sopenharmony_ci */ 403962306a36Sopenharmony_ci if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 404062306a36Sopenharmony_ci u1_pel > USB3_LPM_MAX_U1_SEL_PEL || 404162306a36Sopenharmony_ci u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 404262306a36Sopenharmony_ci u2_pel > USB3_LPM_MAX_U2_SEL_PEL) { 404362306a36Sopenharmony_ci dev_dbg(&udev->dev, "Device-initiated U1/U2 disabled due to long SEL or PEL\n"); 404462306a36Sopenharmony_ci return -EINVAL; 404562306a36Sopenharmony_ci } 404662306a36Sopenharmony_ci 404762306a36Sopenharmony_ci /* 404862306a36Sopenharmony_ci * usb_enable_lpm() can be called as part of a failed device reset, 404962306a36Sopenharmony_ci * which may be initiated by an error path of a mass storage driver. 405062306a36Sopenharmony_ci * Therefore, use GFP_NOIO. 405162306a36Sopenharmony_ci */ 405262306a36Sopenharmony_ci sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); 405362306a36Sopenharmony_ci if (!sel_values) 405462306a36Sopenharmony_ci return -ENOMEM; 405562306a36Sopenharmony_ci 405662306a36Sopenharmony_ci sel_values->u1_sel = u1_sel; 405762306a36Sopenharmony_ci sel_values->u1_pel = u1_pel; 405862306a36Sopenharmony_ci sel_values->u2_sel = cpu_to_le16(u2_sel); 405962306a36Sopenharmony_ci sel_values->u2_pel = cpu_to_le16(u2_pel); 406062306a36Sopenharmony_ci 406162306a36Sopenharmony_ci ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 406262306a36Sopenharmony_ci USB_REQ_SET_SEL, 406362306a36Sopenharmony_ci USB_RECIP_DEVICE, 406462306a36Sopenharmony_ci 0, 0, 406562306a36Sopenharmony_ci sel_values, sizeof *(sel_values), 406662306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 406762306a36Sopenharmony_ci kfree(sel_values); 406862306a36Sopenharmony_ci 406962306a36Sopenharmony_ci if (ret > 0) 407062306a36Sopenharmony_ci udev->lpm_devinit_allow = 1; 407162306a36Sopenharmony_ci 407262306a36Sopenharmony_ci return ret; 407362306a36Sopenharmony_ci} 407462306a36Sopenharmony_ci 407562306a36Sopenharmony_ci/* 407662306a36Sopenharmony_ci * Enable or disable device-initiated U1 or U2 transitions. 407762306a36Sopenharmony_ci */ 407862306a36Sopenharmony_cistatic int usb_set_device_initiated_lpm(struct usb_device *udev, 407962306a36Sopenharmony_ci enum usb3_link_state state, bool enable) 408062306a36Sopenharmony_ci{ 408162306a36Sopenharmony_ci int ret; 408262306a36Sopenharmony_ci int feature; 408362306a36Sopenharmony_ci 408462306a36Sopenharmony_ci switch (state) { 408562306a36Sopenharmony_ci case USB3_LPM_U1: 408662306a36Sopenharmony_ci feature = USB_DEVICE_U1_ENABLE; 408762306a36Sopenharmony_ci break; 408862306a36Sopenharmony_ci case USB3_LPM_U2: 408962306a36Sopenharmony_ci feature = USB_DEVICE_U2_ENABLE; 409062306a36Sopenharmony_ci break; 409162306a36Sopenharmony_ci default: 409262306a36Sopenharmony_ci dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 409362306a36Sopenharmony_ci __func__, enable ? "enable" : "disable"); 409462306a36Sopenharmony_ci return -EINVAL; 409562306a36Sopenharmony_ci } 409662306a36Sopenharmony_ci 409762306a36Sopenharmony_ci if (udev->state != USB_STATE_CONFIGURED) { 409862306a36Sopenharmony_ci dev_dbg(&udev->dev, "%s: Can't %s %s state " 409962306a36Sopenharmony_ci "for unconfigured device.\n", 410062306a36Sopenharmony_ci __func__, enable ? "enable" : "disable", 410162306a36Sopenharmony_ci usb3_lpm_names[state]); 410262306a36Sopenharmony_ci return 0; 410362306a36Sopenharmony_ci } 410462306a36Sopenharmony_ci 410562306a36Sopenharmony_ci if (enable) { 410662306a36Sopenharmony_ci /* 410762306a36Sopenharmony_ci * Now send the control transfer to enable device-initiated LPM 410862306a36Sopenharmony_ci * for either U1 or U2. 410962306a36Sopenharmony_ci */ 411062306a36Sopenharmony_ci ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 411162306a36Sopenharmony_ci USB_REQ_SET_FEATURE, 411262306a36Sopenharmony_ci USB_RECIP_DEVICE, 411362306a36Sopenharmony_ci feature, 411462306a36Sopenharmony_ci 0, NULL, 0, 411562306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 411662306a36Sopenharmony_ci } else { 411762306a36Sopenharmony_ci ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 411862306a36Sopenharmony_ci USB_REQ_CLEAR_FEATURE, 411962306a36Sopenharmony_ci USB_RECIP_DEVICE, 412062306a36Sopenharmony_ci feature, 412162306a36Sopenharmony_ci 0, NULL, 0, 412262306a36Sopenharmony_ci USB_CTRL_SET_TIMEOUT); 412362306a36Sopenharmony_ci } 412462306a36Sopenharmony_ci if (ret < 0) { 412562306a36Sopenharmony_ci dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 412662306a36Sopenharmony_ci enable ? "Enable" : "Disable", 412762306a36Sopenharmony_ci usb3_lpm_names[state]); 412862306a36Sopenharmony_ci return -EBUSY; 412962306a36Sopenharmony_ci } 413062306a36Sopenharmony_ci return 0; 413162306a36Sopenharmony_ci} 413262306a36Sopenharmony_ci 413362306a36Sopenharmony_cistatic int usb_set_lpm_timeout(struct usb_device *udev, 413462306a36Sopenharmony_ci enum usb3_link_state state, int timeout) 413562306a36Sopenharmony_ci{ 413662306a36Sopenharmony_ci int ret; 413762306a36Sopenharmony_ci int feature; 413862306a36Sopenharmony_ci 413962306a36Sopenharmony_ci switch (state) { 414062306a36Sopenharmony_ci case USB3_LPM_U1: 414162306a36Sopenharmony_ci feature = USB_PORT_FEAT_U1_TIMEOUT; 414262306a36Sopenharmony_ci break; 414362306a36Sopenharmony_ci case USB3_LPM_U2: 414462306a36Sopenharmony_ci feature = USB_PORT_FEAT_U2_TIMEOUT; 414562306a36Sopenharmony_ci break; 414662306a36Sopenharmony_ci default: 414762306a36Sopenharmony_ci dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 414862306a36Sopenharmony_ci __func__); 414962306a36Sopenharmony_ci return -EINVAL; 415062306a36Sopenharmony_ci } 415162306a36Sopenharmony_ci 415262306a36Sopenharmony_ci if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 415362306a36Sopenharmony_ci timeout != USB3_LPM_DEVICE_INITIATED) { 415462306a36Sopenharmony_ci dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 415562306a36Sopenharmony_ci "which is a reserved value.\n", 415662306a36Sopenharmony_ci usb3_lpm_names[state], timeout); 415762306a36Sopenharmony_ci return -EINVAL; 415862306a36Sopenharmony_ci } 415962306a36Sopenharmony_ci 416062306a36Sopenharmony_ci ret = set_port_feature(udev->parent, 416162306a36Sopenharmony_ci USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 416262306a36Sopenharmony_ci feature); 416362306a36Sopenharmony_ci if (ret < 0) { 416462306a36Sopenharmony_ci dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 416562306a36Sopenharmony_ci "error code %i\n", usb3_lpm_names[state], 416662306a36Sopenharmony_ci timeout, ret); 416762306a36Sopenharmony_ci return -EBUSY; 416862306a36Sopenharmony_ci } 416962306a36Sopenharmony_ci if (state == USB3_LPM_U1) 417062306a36Sopenharmony_ci udev->u1_params.timeout = timeout; 417162306a36Sopenharmony_ci else 417262306a36Sopenharmony_ci udev->u2_params.timeout = timeout; 417362306a36Sopenharmony_ci return 0; 417462306a36Sopenharmony_ci} 417562306a36Sopenharmony_ci 417662306a36Sopenharmony_ci/* 417762306a36Sopenharmony_ci * Don't allow device intiated U1/U2 if the system exit latency + one bus 417862306a36Sopenharmony_ci * interval is greater than the minimum service interval of any active 417962306a36Sopenharmony_ci * periodic endpoint. See USB 3.2 section 9.4.9 418062306a36Sopenharmony_ci */ 418162306a36Sopenharmony_cistatic bool usb_device_may_initiate_lpm(struct usb_device *udev, 418262306a36Sopenharmony_ci enum usb3_link_state state) 418362306a36Sopenharmony_ci{ 418462306a36Sopenharmony_ci unsigned int sel; /* us */ 418562306a36Sopenharmony_ci int i, j; 418662306a36Sopenharmony_ci 418762306a36Sopenharmony_ci if (!udev->lpm_devinit_allow) 418862306a36Sopenharmony_ci return false; 418962306a36Sopenharmony_ci 419062306a36Sopenharmony_ci if (state == USB3_LPM_U1) 419162306a36Sopenharmony_ci sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 419262306a36Sopenharmony_ci else if (state == USB3_LPM_U2) 419362306a36Sopenharmony_ci sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 419462306a36Sopenharmony_ci else 419562306a36Sopenharmony_ci return false; 419662306a36Sopenharmony_ci 419762306a36Sopenharmony_ci for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 419862306a36Sopenharmony_ci struct usb_interface *intf; 419962306a36Sopenharmony_ci struct usb_endpoint_descriptor *desc; 420062306a36Sopenharmony_ci unsigned int interval; 420162306a36Sopenharmony_ci 420262306a36Sopenharmony_ci intf = udev->actconfig->interface[i]; 420362306a36Sopenharmony_ci if (!intf) 420462306a36Sopenharmony_ci continue; 420562306a36Sopenharmony_ci 420662306a36Sopenharmony_ci for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) { 420762306a36Sopenharmony_ci desc = &intf->cur_altsetting->endpoint[j].desc; 420862306a36Sopenharmony_ci 420962306a36Sopenharmony_ci if (usb_endpoint_xfer_int(desc) || 421062306a36Sopenharmony_ci usb_endpoint_xfer_isoc(desc)) { 421162306a36Sopenharmony_ci interval = (1 << (desc->bInterval - 1)) * 125; 421262306a36Sopenharmony_ci if (sel + 125 > interval) 421362306a36Sopenharmony_ci return false; 421462306a36Sopenharmony_ci } 421562306a36Sopenharmony_ci } 421662306a36Sopenharmony_ci } 421762306a36Sopenharmony_ci return true; 421862306a36Sopenharmony_ci} 421962306a36Sopenharmony_ci 422062306a36Sopenharmony_ci/* 422162306a36Sopenharmony_ci * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 422262306a36Sopenharmony_ci * U1/U2 entry. 422362306a36Sopenharmony_ci * 422462306a36Sopenharmony_ci * We will attempt to enable U1 or U2, but there are no guarantees that the 422562306a36Sopenharmony_ci * control transfers to set the hub timeout or enable device-initiated U1/U2 422662306a36Sopenharmony_ci * will be successful. 422762306a36Sopenharmony_ci * 422862306a36Sopenharmony_ci * If the control transfer to enable device-initiated U1/U2 entry fails, then 422962306a36Sopenharmony_ci * hub-initiated U1/U2 will be disabled. 423062306a36Sopenharmony_ci * 423162306a36Sopenharmony_ci * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 423262306a36Sopenharmony_ci * driver know about it. If that call fails, it should be harmless, and just 423362306a36Sopenharmony_ci * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 423462306a36Sopenharmony_ci */ 423562306a36Sopenharmony_cistatic void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 423662306a36Sopenharmony_ci enum usb3_link_state state) 423762306a36Sopenharmony_ci{ 423862306a36Sopenharmony_ci int timeout; 423962306a36Sopenharmony_ci __u8 u1_mel; 424062306a36Sopenharmony_ci __le16 u2_mel; 424162306a36Sopenharmony_ci 424262306a36Sopenharmony_ci /* Skip if the device BOS descriptor couldn't be read */ 424362306a36Sopenharmony_ci if (!udev->bos) 424462306a36Sopenharmony_ci return; 424562306a36Sopenharmony_ci 424662306a36Sopenharmony_ci u1_mel = udev->bos->ss_cap->bU1devExitLat; 424762306a36Sopenharmony_ci u2_mel = udev->bos->ss_cap->bU2DevExitLat; 424862306a36Sopenharmony_ci 424962306a36Sopenharmony_ci /* If the device says it doesn't have *any* exit latency to come out of 425062306a36Sopenharmony_ci * U1 or U2, it's probably lying. Assume it doesn't implement that link 425162306a36Sopenharmony_ci * state. 425262306a36Sopenharmony_ci */ 425362306a36Sopenharmony_ci if ((state == USB3_LPM_U1 && u1_mel == 0) || 425462306a36Sopenharmony_ci (state == USB3_LPM_U2 && u2_mel == 0)) 425562306a36Sopenharmony_ci return; 425662306a36Sopenharmony_ci 425762306a36Sopenharmony_ci /* We allow the host controller to set the U1/U2 timeout internally 425862306a36Sopenharmony_ci * first, so that it can change its schedule to account for the 425962306a36Sopenharmony_ci * additional latency to send data to a device in a lower power 426062306a36Sopenharmony_ci * link state. 426162306a36Sopenharmony_ci */ 426262306a36Sopenharmony_ci timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 426362306a36Sopenharmony_ci 426462306a36Sopenharmony_ci /* xHCI host controller doesn't want to enable this LPM state. */ 426562306a36Sopenharmony_ci if (timeout == 0) 426662306a36Sopenharmony_ci return; 426762306a36Sopenharmony_ci 426862306a36Sopenharmony_ci if (timeout < 0) { 426962306a36Sopenharmony_ci dev_warn(&udev->dev, "Could not enable %s link state, " 427062306a36Sopenharmony_ci "xHCI error %i.\n", usb3_lpm_names[state], 427162306a36Sopenharmony_ci timeout); 427262306a36Sopenharmony_ci return; 427362306a36Sopenharmony_ci } 427462306a36Sopenharmony_ci 427562306a36Sopenharmony_ci if (usb_set_lpm_timeout(udev, state, timeout)) { 427662306a36Sopenharmony_ci /* If we can't set the parent hub U1/U2 timeout, 427762306a36Sopenharmony_ci * device-initiated LPM won't be allowed either, so let the xHCI 427862306a36Sopenharmony_ci * host know that this link state won't be enabled. 427962306a36Sopenharmony_ci */ 428062306a36Sopenharmony_ci hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 428162306a36Sopenharmony_ci return; 428262306a36Sopenharmony_ci } 428362306a36Sopenharmony_ci 428462306a36Sopenharmony_ci /* Only a configured device will accept the Set Feature 428562306a36Sopenharmony_ci * U1/U2_ENABLE 428662306a36Sopenharmony_ci */ 428762306a36Sopenharmony_ci if (udev->actconfig && 428862306a36Sopenharmony_ci usb_device_may_initiate_lpm(udev, state)) { 428962306a36Sopenharmony_ci if (usb_set_device_initiated_lpm(udev, state, true)) { 429062306a36Sopenharmony_ci /* 429162306a36Sopenharmony_ci * Request to enable device initiated U1/U2 failed, 429262306a36Sopenharmony_ci * better to turn off lpm in this case. 429362306a36Sopenharmony_ci */ 429462306a36Sopenharmony_ci usb_set_lpm_timeout(udev, state, 0); 429562306a36Sopenharmony_ci hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 429662306a36Sopenharmony_ci return; 429762306a36Sopenharmony_ci } 429862306a36Sopenharmony_ci } 429962306a36Sopenharmony_ci 430062306a36Sopenharmony_ci if (state == USB3_LPM_U1) 430162306a36Sopenharmony_ci udev->usb3_lpm_u1_enabled = 1; 430262306a36Sopenharmony_ci else if (state == USB3_LPM_U2) 430362306a36Sopenharmony_ci udev->usb3_lpm_u2_enabled = 1; 430462306a36Sopenharmony_ci} 430562306a36Sopenharmony_ci/* 430662306a36Sopenharmony_ci * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 430762306a36Sopenharmony_ci * U1/U2 entry. 430862306a36Sopenharmony_ci * 430962306a36Sopenharmony_ci * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 431062306a36Sopenharmony_ci * If zero is returned, the parent will not allow the link to go into U1/U2. 431162306a36Sopenharmony_ci * 431262306a36Sopenharmony_ci * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 431362306a36Sopenharmony_ci * it won't have an effect on the bus link state because the parent hub will 431462306a36Sopenharmony_ci * still disallow device-initiated U1/U2 entry. 431562306a36Sopenharmony_ci * 431662306a36Sopenharmony_ci * If zero is returned, the xHCI host controller may still think U1/U2 entry is 431762306a36Sopenharmony_ci * possible. The result will be slightly more bus bandwidth will be taken up 431862306a36Sopenharmony_ci * (to account for U1/U2 exit latency), but it should be harmless. 431962306a36Sopenharmony_ci */ 432062306a36Sopenharmony_cistatic int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 432162306a36Sopenharmony_ci enum usb3_link_state state) 432262306a36Sopenharmony_ci{ 432362306a36Sopenharmony_ci switch (state) { 432462306a36Sopenharmony_ci case USB3_LPM_U1: 432562306a36Sopenharmony_ci case USB3_LPM_U2: 432662306a36Sopenharmony_ci break; 432762306a36Sopenharmony_ci default: 432862306a36Sopenharmony_ci dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 432962306a36Sopenharmony_ci __func__); 433062306a36Sopenharmony_ci return -EINVAL; 433162306a36Sopenharmony_ci } 433262306a36Sopenharmony_ci 433362306a36Sopenharmony_ci if (usb_set_lpm_timeout(udev, state, 0)) 433462306a36Sopenharmony_ci return -EBUSY; 433562306a36Sopenharmony_ci 433662306a36Sopenharmony_ci usb_set_device_initiated_lpm(udev, state, false); 433762306a36Sopenharmony_ci 433862306a36Sopenharmony_ci if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 433962306a36Sopenharmony_ci dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 434062306a36Sopenharmony_ci "bus schedule bandwidth may be impacted.\n", 434162306a36Sopenharmony_ci usb3_lpm_names[state]); 434262306a36Sopenharmony_ci 434362306a36Sopenharmony_ci /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM 434462306a36Sopenharmony_ci * is disabled. Hub will disallows link to enter U1/U2 as well, 434562306a36Sopenharmony_ci * even device is initiating LPM. Hence LPM is disabled if hub LPM 434662306a36Sopenharmony_ci * timeout set to 0, no matter device-initiated LPM is disabled or 434762306a36Sopenharmony_ci * not. 434862306a36Sopenharmony_ci */ 434962306a36Sopenharmony_ci if (state == USB3_LPM_U1) 435062306a36Sopenharmony_ci udev->usb3_lpm_u1_enabled = 0; 435162306a36Sopenharmony_ci else if (state == USB3_LPM_U2) 435262306a36Sopenharmony_ci udev->usb3_lpm_u2_enabled = 0; 435362306a36Sopenharmony_ci 435462306a36Sopenharmony_ci return 0; 435562306a36Sopenharmony_ci} 435662306a36Sopenharmony_ci 435762306a36Sopenharmony_ci/* 435862306a36Sopenharmony_ci * Disable hub-initiated and device-initiated U1 and U2 entry. 435962306a36Sopenharmony_ci * Caller must own the bandwidth_mutex. 436062306a36Sopenharmony_ci * 436162306a36Sopenharmony_ci * This will call usb_enable_lpm() on failure, which will decrement 436262306a36Sopenharmony_ci * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 436362306a36Sopenharmony_ci */ 436462306a36Sopenharmony_ciint usb_disable_lpm(struct usb_device *udev) 436562306a36Sopenharmony_ci{ 436662306a36Sopenharmony_ci struct usb_hcd *hcd; 436762306a36Sopenharmony_ci 436862306a36Sopenharmony_ci if (!udev || !udev->parent || 436962306a36Sopenharmony_ci udev->speed < USB_SPEED_SUPER || 437062306a36Sopenharmony_ci !udev->lpm_capable || 437162306a36Sopenharmony_ci udev->state < USB_STATE_CONFIGURED) 437262306a36Sopenharmony_ci return 0; 437362306a36Sopenharmony_ci 437462306a36Sopenharmony_ci hcd = bus_to_hcd(udev->bus); 437562306a36Sopenharmony_ci if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 437662306a36Sopenharmony_ci return 0; 437762306a36Sopenharmony_ci 437862306a36Sopenharmony_ci udev->lpm_disable_count++; 437962306a36Sopenharmony_ci if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 438062306a36Sopenharmony_ci return 0; 438162306a36Sopenharmony_ci 438262306a36Sopenharmony_ci /* If LPM is enabled, attempt to disable it. */ 438362306a36Sopenharmony_ci if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 438462306a36Sopenharmony_ci goto enable_lpm; 438562306a36Sopenharmony_ci if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 438662306a36Sopenharmony_ci goto enable_lpm; 438762306a36Sopenharmony_ci 438862306a36Sopenharmony_ci return 0; 438962306a36Sopenharmony_ci 439062306a36Sopenharmony_cienable_lpm: 439162306a36Sopenharmony_ci usb_enable_lpm(udev); 439262306a36Sopenharmony_ci return -EBUSY; 439362306a36Sopenharmony_ci} 439462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_disable_lpm); 439562306a36Sopenharmony_ci 439662306a36Sopenharmony_ci/* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 439762306a36Sopenharmony_ciint usb_unlocked_disable_lpm(struct usb_device *udev) 439862306a36Sopenharmony_ci{ 439962306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 440062306a36Sopenharmony_ci int ret; 440162306a36Sopenharmony_ci 440262306a36Sopenharmony_ci if (!hcd) 440362306a36Sopenharmony_ci return -EINVAL; 440462306a36Sopenharmony_ci 440562306a36Sopenharmony_ci mutex_lock(hcd->bandwidth_mutex); 440662306a36Sopenharmony_ci ret = usb_disable_lpm(udev); 440762306a36Sopenharmony_ci mutex_unlock(hcd->bandwidth_mutex); 440862306a36Sopenharmony_ci 440962306a36Sopenharmony_ci return ret; 441062306a36Sopenharmony_ci} 441162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 441262306a36Sopenharmony_ci 441362306a36Sopenharmony_ci/* 441462306a36Sopenharmony_ci * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 441562306a36Sopenharmony_ci * xHCI host policy may prevent U1 or U2 from being enabled. 441662306a36Sopenharmony_ci * 441762306a36Sopenharmony_ci * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 441862306a36Sopenharmony_ci * until the lpm_disable_count drops to zero. Caller must own the 441962306a36Sopenharmony_ci * bandwidth_mutex. 442062306a36Sopenharmony_ci */ 442162306a36Sopenharmony_civoid usb_enable_lpm(struct usb_device *udev) 442262306a36Sopenharmony_ci{ 442362306a36Sopenharmony_ci struct usb_hcd *hcd; 442462306a36Sopenharmony_ci struct usb_hub *hub; 442562306a36Sopenharmony_ci struct usb_port *port_dev; 442662306a36Sopenharmony_ci 442762306a36Sopenharmony_ci if (!udev || !udev->parent || 442862306a36Sopenharmony_ci udev->speed < USB_SPEED_SUPER || 442962306a36Sopenharmony_ci !udev->lpm_capable || 443062306a36Sopenharmony_ci udev->state < USB_STATE_CONFIGURED) 443162306a36Sopenharmony_ci return; 443262306a36Sopenharmony_ci 443362306a36Sopenharmony_ci udev->lpm_disable_count--; 443462306a36Sopenharmony_ci hcd = bus_to_hcd(udev->bus); 443562306a36Sopenharmony_ci /* Double check that we can both enable and disable LPM. 443662306a36Sopenharmony_ci * Device must be configured to accept set feature U1/U2 timeout. 443762306a36Sopenharmony_ci */ 443862306a36Sopenharmony_ci if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 443962306a36Sopenharmony_ci !hcd->driver->disable_usb3_lpm_timeout) 444062306a36Sopenharmony_ci return; 444162306a36Sopenharmony_ci 444262306a36Sopenharmony_ci if (udev->lpm_disable_count > 0) 444362306a36Sopenharmony_ci return; 444462306a36Sopenharmony_ci 444562306a36Sopenharmony_ci hub = usb_hub_to_struct_hub(udev->parent); 444662306a36Sopenharmony_ci if (!hub) 444762306a36Sopenharmony_ci return; 444862306a36Sopenharmony_ci 444962306a36Sopenharmony_ci port_dev = hub->ports[udev->portnum - 1]; 445062306a36Sopenharmony_ci 445162306a36Sopenharmony_ci if (port_dev->usb3_lpm_u1_permit) 445262306a36Sopenharmony_ci usb_enable_link_state(hcd, udev, USB3_LPM_U1); 445362306a36Sopenharmony_ci 445462306a36Sopenharmony_ci if (port_dev->usb3_lpm_u2_permit) 445562306a36Sopenharmony_ci usb_enable_link_state(hcd, udev, USB3_LPM_U2); 445662306a36Sopenharmony_ci} 445762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_enable_lpm); 445862306a36Sopenharmony_ci 445962306a36Sopenharmony_ci/* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 446062306a36Sopenharmony_civoid usb_unlocked_enable_lpm(struct usb_device *udev) 446162306a36Sopenharmony_ci{ 446262306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 446362306a36Sopenharmony_ci 446462306a36Sopenharmony_ci if (!hcd) 446562306a36Sopenharmony_ci return; 446662306a36Sopenharmony_ci 446762306a36Sopenharmony_ci mutex_lock(hcd->bandwidth_mutex); 446862306a36Sopenharmony_ci usb_enable_lpm(udev); 446962306a36Sopenharmony_ci mutex_unlock(hcd->bandwidth_mutex); 447062306a36Sopenharmony_ci} 447162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 447262306a36Sopenharmony_ci 447362306a36Sopenharmony_ci/* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */ 447462306a36Sopenharmony_cistatic void hub_usb3_port_prepare_disable(struct usb_hub *hub, 447562306a36Sopenharmony_ci struct usb_port *port_dev) 447662306a36Sopenharmony_ci{ 447762306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 447862306a36Sopenharmony_ci int ret; 447962306a36Sopenharmony_ci 448062306a36Sopenharmony_ci if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { 448162306a36Sopenharmony_ci ret = hub_set_port_link_state(hub, port_dev->portnum, 448262306a36Sopenharmony_ci USB_SS_PORT_LS_U0); 448362306a36Sopenharmony_ci if (!ret) { 448462306a36Sopenharmony_ci msleep(USB_RESUME_TIMEOUT); 448562306a36Sopenharmony_ci ret = usb_disable_remote_wakeup(udev); 448662306a36Sopenharmony_ci } 448762306a36Sopenharmony_ci if (ret) 448862306a36Sopenharmony_ci dev_warn(&udev->dev, 448962306a36Sopenharmony_ci "Port disable: can't disable remote wake\n"); 449062306a36Sopenharmony_ci udev->do_remote_wakeup = 0; 449162306a36Sopenharmony_ci } 449262306a36Sopenharmony_ci} 449362306a36Sopenharmony_ci 449462306a36Sopenharmony_ci#else /* CONFIG_PM */ 449562306a36Sopenharmony_ci 449662306a36Sopenharmony_ci#define hub_suspend NULL 449762306a36Sopenharmony_ci#define hub_resume NULL 449862306a36Sopenharmony_ci#define hub_reset_resume NULL 449962306a36Sopenharmony_ci 450062306a36Sopenharmony_cistatic inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, 450162306a36Sopenharmony_ci struct usb_port *port_dev) { } 450262306a36Sopenharmony_ci 450362306a36Sopenharmony_ciint usb_disable_lpm(struct usb_device *udev) 450462306a36Sopenharmony_ci{ 450562306a36Sopenharmony_ci return 0; 450662306a36Sopenharmony_ci} 450762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_disable_lpm); 450862306a36Sopenharmony_ci 450962306a36Sopenharmony_civoid usb_enable_lpm(struct usb_device *udev) { } 451062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_enable_lpm); 451162306a36Sopenharmony_ci 451262306a36Sopenharmony_ciint usb_unlocked_disable_lpm(struct usb_device *udev) 451362306a36Sopenharmony_ci{ 451462306a36Sopenharmony_ci return 0; 451562306a36Sopenharmony_ci} 451662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 451762306a36Sopenharmony_ci 451862306a36Sopenharmony_civoid usb_unlocked_enable_lpm(struct usb_device *udev) { } 451962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 452062306a36Sopenharmony_ci 452162306a36Sopenharmony_ciint usb_disable_ltm(struct usb_device *udev) 452262306a36Sopenharmony_ci{ 452362306a36Sopenharmony_ci return 0; 452462306a36Sopenharmony_ci} 452562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_disable_ltm); 452662306a36Sopenharmony_ci 452762306a36Sopenharmony_civoid usb_enable_ltm(struct usb_device *udev) { } 452862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_enable_ltm); 452962306a36Sopenharmony_ci 453062306a36Sopenharmony_cistatic int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 453162306a36Sopenharmony_ci u16 portstatus, u16 portchange) 453262306a36Sopenharmony_ci{ 453362306a36Sopenharmony_ci return 0; 453462306a36Sopenharmony_ci} 453562306a36Sopenharmony_ci 453662306a36Sopenharmony_cistatic int usb_req_set_sel(struct usb_device *udev) 453762306a36Sopenharmony_ci{ 453862306a36Sopenharmony_ci return 0; 453962306a36Sopenharmony_ci} 454062306a36Sopenharmony_ci 454162306a36Sopenharmony_ci#endif /* CONFIG_PM */ 454262306a36Sopenharmony_ci 454362306a36Sopenharmony_ci/* 454462306a36Sopenharmony_ci * USB-3 does not have a similar link state as USB-2 that will avoid negotiating 454562306a36Sopenharmony_ci * a connection with a plugged-in cable but will signal the host when the cable 454662306a36Sopenharmony_ci * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices 454762306a36Sopenharmony_ci */ 454862306a36Sopenharmony_cistatic int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 454962306a36Sopenharmony_ci{ 455062306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 455162306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 455262306a36Sopenharmony_ci int ret = 0; 455362306a36Sopenharmony_ci 455462306a36Sopenharmony_ci if (!hub->error) { 455562306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) { 455662306a36Sopenharmony_ci hub_usb3_port_prepare_disable(hub, port_dev); 455762306a36Sopenharmony_ci ret = hub_set_port_link_state(hub, port_dev->portnum, 455862306a36Sopenharmony_ci USB_SS_PORT_LS_U3); 455962306a36Sopenharmony_ci } else { 456062306a36Sopenharmony_ci ret = usb_clear_port_feature(hdev, port1, 456162306a36Sopenharmony_ci USB_PORT_FEAT_ENABLE); 456262306a36Sopenharmony_ci } 456362306a36Sopenharmony_ci } 456462306a36Sopenharmony_ci if (port_dev->child && set_state) 456562306a36Sopenharmony_ci usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); 456662306a36Sopenharmony_ci if (ret && ret != -ENODEV) 456762306a36Sopenharmony_ci dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); 456862306a36Sopenharmony_ci return ret; 456962306a36Sopenharmony_ci} 457062306a36Sopenharmony_ci 457162306a36Sopenharmony_ci/* 457262306a36Sopenharmony_ci * usb_port_disable - disable a usb device's upstream port 457362306a36Sopenharmony_ci * @udev: device to disable 457462306a36Sopenharmony_ci * Context: @udev locked, must be able to sleep. 457562306a36Sopenharmony_ci * 457662306a36Sopenharmony_ci * Disables a USB device that isn't in active use. 457762306a36Sopenharmony_ci */ 457862306a36Sopenharmony_ciint usb_port_disable(struct usb_device *udev) 457962306a36Sopenharmony_ci{ 458062306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 458162306a36Sopenharmony_ci 458262306a36Sopenharmony_ci return hub_port_disable(hub, udev->portnum, 0); 458362306a36Sopenharmony_ci} 458462306a36Sopenharmony_ci 458562306a36Sopenharmony_ci/* USB 2.0 spec, 7.1.7.3 / fig 7-29: 458662306a36Sopenharmony_ci * 458762306a36Sopenharmony_ci * Between connect detection and reset signaling there must be a delay 458862306a36Sopenharmony_ci * of 100ms at least for debounce and power-settling. The corresponding 458962306a36Sopenharmony_ci * timer shall restart whenever the downstream port detects a disconnect. 459062306a36Sopenharmony_ci * 459162306a36Sopenharmony_ci * Apparently there are some bluetooth and irda-dongles and a number of 459262306a36Sopenharmony_ci * low-speed devices for which this debounce period may last over a second. 459362306a36Sopenharmony_ci * Not covered by the spec - but easy to deal with. 459462306a36Sopenharmony_ci * 459562306a36Sopenharmony_ci * This implementation uses a 1500ms total debounce timeout; if the 459662306a36Sopenharmony_ci * connection isn't stable by then it returns -ETIMEDOUT. It checks 459762306a36Sopenharmony_ci * every 25ms for transient disconnects. When the port status has been 459862306a36Sopenharmony_ci * unchanged for 100ms it returns the port status. 459962306a36Sopenharmony_ci */ 460062306a36Sopenharmony_ciint hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) 460162306a36Sopenharmony_ci{ 460262306a36Sopenharmony_ci int ret; 460362306a36Sopenharmony_ci u16 portchange, portstatus; 460462306a36Sopenharmony_ci unsigned connection = 0xffff; 460562306a36Sopenharmony_ci int total_time, stable_time = 0; 460662306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 460762306a36Sopenharmony_ci 460862306a36Sopenharmony_ci for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 460962306a36Sopenharmony_ci ret = usb_hub_port_status(hub, port1, &portstatus, &portchange); 461062306a36Sopenharmony_ci if (ret < 0) 461162306a36Sopenharmony_ci return ret; 461262306a36Sopenharmony_ci 461362306a36Sopenharmony_ci if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 461462306a36Sopenharmony_ci (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 461562306a36Sopenharmony_ci if (!must_be_connected || 461662306a36Sopenharmony_ci (connection == USB_PORT_STAT_CONNECTION)) 461762306a36Sopenharmony_ci stable_time += HUB_DEBOUNCE_STEP; 461862306a36Sopenharmony_ci if (stable_time >= HUB_DEBOUNCE_STABLE) 461962306a36Sopenharmony_ci break; 462062306a36Sopenharmony_ci } else { 462162306a36Sopenharmony_ci stable_time = 0; 462262306a36Sopenharmony_ci connection = portstatus & USB_PORT_STAT_CONNECTION; 462362306a36Sopenharmony_ci } 462462306a36Sopenharmony_ci 462562306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_CONNECTION) { 462662306a36Sopenharmony_ci usb_clear_port_feature(hub->hdev, port1, 462762306a36Sopenharmony_ci USB_PORT_FEAT_C_CONNECTION); 462862306a36Sopenharmony_ci } 462962306a36Sopenharmony_ci 463062306a36Sopenharmony_ci if (total_time >= HUB_DEBOUNCE_TIMEOUT) 463162306a36Sopenharmony_ci break; 463262306a36Sopenharmony_ci msleep(HUB_DEBOUNCE_STEP); 463362306a36Sopenharmony_ci } 463462306a36Sopenharmony_ci 463562306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", 463662306a36Sopenharmony_ci total_time, stable_time, portstatus); 463762306a36Sopenharmony_ci 463862306a36Sopenharmony_ci if (stable_time < HUB_DEBOUNCE_STABLE) 463962306a36Sopenharmony_ci return -ETIMEDOUT; 464062306a36Sopenharmony_ci return portstatus; 464162306a36Sopenharmony_ci} 464262306a36Sopenharmony_ci 464362306a36Sopenharmony_civoid usb_ep0_reinit(struct usb_device *udev) 464462306a36Sopenharmony_ci{ 464562306a36Sopenharmony_ci usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 464662306a36Sopenharmony_ci usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 464762306a36Sopenharmony_ci usb_enable_endpoint(udev, &udev->ep0, true); 464862306a36Sopenharmony_ci} 464962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_ep0_reinit); 465062306a36Sopenharmony_ci 465162306a36Sopenharmony_ci#define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 465262306a36Sopenharmony_ci#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 465362306a36Sopenharmony_ci 465462306a36Sopenharmony_cistatic int hub_set_address(struct usb_device *udev, int devnum) 465562306a36Sopenharmony_ci{ 465662306a36Sopenharmony_ci int retval; 465762306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 465862306a36Sopenharmony_ci 465962306a36Sopenharmony_ci /* 466062306a36Sopenharmony_ci * The host controller will choose the device address, 466162306a36Sopenharmony_ci * instead of the core having chosen it earlier 466262306a36Sopenharmony_ci */ 466362306a36Sopenharmony_ci if (!hcd->driver->address_device && devnum <= 1) 466462306a36Sopenharmony_ci return -EINVAL; 466562306a36Sopenharmony_ci if (udev->state == USB_STATE_ADDRESS) 466662306a36Sopenharmony_ci return 0; 466762306a36Sopenharmony_ci if (udev->state != USB_STATE_DEFAULT) 466862306a36Sopenharmony_ci return -EINVAL; 466962306a36Sopenharmony_ci if (hcd->driver->address_device) 467062306a36Sopenharmony_ci retval = hcd->driver->address_device(hcd, udev); 467162306a36Sopenharmony_ci else 467262306a36Sopenharmony_ci retval = usb_control_msg(udev, usb_sndaddr0pipe(), 467362306a36Sopenharmony_ci USB_REQ_SET_ADDRESS, 0, devnum, 0, 467462306a36Sopenharmony_ci NULL, 0, USB_CTRL_SET_TIMEOUT); 467562306a36Sopenharmony_ci if (retval == 0) { 467662306a36Sopenharmony_ci update_devnum(udev, devnum); 467762306a36Sopenharmony_ci /* Device now using proper address. */ 467862306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_ADDRESS); 467962306a36Sopenharmony_ci usb_ep0_reinit(udev); 468062306a36Sopenharmony_ci } 468162306a36Sopenharmony_ci return retval; 468262306a36Sopenharmony_ci} 468362306a36Sopenharmony_ci 468462306a36Sopenharmony_ci/* 468562306a36Sopenharmony_ci * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM 468662306a36Sopenharmony_ci * when they're plugged into a USB 2.0 port, but they don't work when LPM is 468762306a36Sopenharmony_ci * enabled. 468862306a36Sopenharmony_ci * 468962306a36Sopenharmony_ci * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the 469062306a36Sopenharmony_ci * device says it supports the new USB 2.0 Link PM errata by setting the BESL 469162306a36Sopenharmony_ci * support bit in the BOS descriptor. 469262306a36Sopenharmony_ci */ 469362306a36Sopenharmony_cistatic void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) 469462306a36Sopenharmony_ci{ 469562306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 469662306a36Sopenharmony_ci int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN; 469762306a36Sopenharmony_ci 469862306a36Sopenharmony_ci if (!udev->usb2_hw_lpm_capable || !udev->bos) 469962306a36Sopenharmony_ci return; 470062306a36Sopenharmony_ci 470162306a36Sopenharmony_ci if (hub) 470262306a36Sopenharmony_ci connect_type = hub->ports[udev->portnum - 1]->connect_type; 470362306a36Sopenharmony_ci 470462306a36Sopenharmony_ci if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || 470562306a36Sopenharmony_ci connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 470662306a36Sopenharmony_ci udev->usb2_hw_lpm_allowed = 1; 470762306a36Sopenharmony_ci usb_enable_usb2_hardware_lpm(udev); 470862306a36Sopenharmony_ci } 470962306a36Sopenharmony_ci} 471062306a36Sopenharmony_ci 471162306a36Sopenharmony_cistatic int hub_enable_device(struct usb_device *udev) 471262306a36Sopenharmony_ci{ 471362306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 471462306a36Sopenharmony_ci 471562306a36Sopenharmony_ci if (!hcd->driver->enable_device) 471662306a36Sopenharmony_ci return 0; 471762306a36Sopenharmony_ci if (udev->state == USB_STATE_ADDRESS) 471862306a36Sopenharmony_ci return 0; 471962306a36Sopenharmony_ci if (udev->state != USB_STATE_DEFAULT) 472062306a36Sopenharmony_ci return -EINVAL; 472162306a36Sopenharmony_ci 472262306a36Sopenharmony_ci return hcd->driver->enable_device(hcd, udev); 472362306a36Sopenharmony_ci} 472462306a36Sopenharmony_ci 472562306a36Sopenharmony_ci/* 472662306a36Sopenharmony_ci * Get the bMaxPacketSize0 value during initialization by reading the 472762306a36Sopenharmony_ci * device's device descriptor. Since we don't already know this value, 472862306a36Sopenharmony_ci * the transfer is unsafe and it ignores I/O errors, only testing for 472962306a36Sopenharmony_ci * reasonable received values. 473062306a36Sopenharmony_ci * 473162306a36Sopenharmony_ci * For "old scheme" initialization, size will be 8 so we read just the 473262306a36Sopenharmony_ci * start of the device descriptor, which should work okay regardless of 473362306a36Sopenharmony_ci * the actual bMaxPacketSize0 value. For "new scheme" initialization, 473462306a36Sopenharmony_ci * size will be 64 (and buf will point to a sufficiently large buffer), 473562306a36Sopenharmony_ci * which might not be kosher according to the USB spec but it's what 473662306a36Sopenharmony_ci * Windows does and what many devices expect. 473762306a36Sopenharmony_ci * 473862306a36Sopenharmony_ci * Returns: bMaxPacketSize0 or a negative error code. 473962306a36Sopenharmony_ci */ 474062306a36Sopenharmony_cistatic int get_bMaxPacketSize0(struct usb_device *udev, 474162306a36Sopenharmony_ci struct usb_device_descriptor *buf, int size, bool first_time) 474262306a36Sopenharmony_ci{ 474362306a36Sopenharmony_ci int i, rc; 474462306a36Sopenharmony_ci 474562306a36Sopenharmony_ci /* 474662306a36Sopenharmony_ci * Retry on all errors; some devices are flakey. 474762306a36Sopenharmony_ci * 255 is for WUSB devices, we actually need to use 474862306a36Sopenharmony_ci * 512 (WUSB1.0[4.8.1]). 474962306a36Sopenharmony_ci */ 475062306a36Sopenharmony_ci for (i = 0; i < GET_MAXPACKET0_TRIES; ++i) { 475162306a36Sopenharmony_ci /* Start with invalid values in case the transfer fails */ 475262306a36Sopenharmony_ci buf->bDescriptorType = buf->bMaxPacketSize0 = 0; 475362306a36Sopenharmony_ci rc = usb_control_msg(udev, usb_rcvaddr0pipe(), 475462306a36Sopenharmony_ci USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 475562306a36Sopenharmony_ci USB_DT_DEVICE << 8, 0, 475662306a36Sopenharmony_ci buf, size, 475762306a36Sopenharmony_ci initial_descriptor_timeout); 475862306a36Sopenharmony_ci switch (buf->bMaxPacketSize0) { 475962306a36Sopenharmony_ci case 8: case 16: case 32: case 64: case 9: 476062306a36Sopenharmony_ci if (buf->bDescriptorType == USB_DT_DEVICE) { 476162306a36Sopenharmony_ci rc = buf->bMaxPacketSize0; 476262306a36Sopenharmony_ci break; 476362306a36Sopenharmony_ci } 476462306a36Sopenharmony_ci fallthrough; 476562306a36Sopenharmony_ci default: 476662306a36Sopenharmony_ci if (rc >= 0) 476762306a36Sopenharmony_ci rc = -EPROTO; 476862306a36Sopenharmony_ci break; 476962306a36Sopenharmony_ci } 477062306a36Sopenharmony_ci 477162306a36Sopenharmony_ci /* 477262306a36Sopenharmony_ci * Some devices time out if they are powered on 477362306a36Sopenharmony_ci * when already connected. They need a second 477462306a36Sopenharmony_ci * reset, so return early. But only on the first 477562306a36Sopenharmony_ci * attempt, lest we get into a time-out/reset loop. 477662306a36Sopenharmony_ci */ 477762306a36Sopenharmony_ci if (rc > 0 || (rc == -ETIMEDOUT && first_time && 477862306a36Sopenharmony_ci udev->speed > USB_SPEED_FULL)) 477962306a36Sopenharmony_ci break; 478062306a36Sopenharmony_ci } 478162306a36Sopenharmony_ci return rc; 478262306a36Sopenharmony_ci} 478362306a36Sopenharmony_ci 478462306a36Sopenharmony_ci#define GET_DESCRIPTOR_BUFSIZE 64 478562306a36Sopenharmony_ci 478662306a36Sopenharmony_ci/* Reset device, (re)assign address, get device descriptor. 478762306a36Sopenharmony_ci * Device connection must be stable, no more debouncing needed. 478862306a36Sopenharmony_ci * Returns device in USB_STATE_ADDRESS, except on error. 478962306a36Sopenharmony_ci * 479062306a36Sopenharmony_ci * If this is called for an already-existing device (as part of 479162306a36Sopenharmony_ci * usb_reset_and_verify_device), the caller must own the device lock and 479262306a36Sopenharmony_ci * the port lock. For a newly detected device that is not accessible 479362306a36Sopenharmony_ci * through any global pointers, it's not necessary to lock the device, 479462306a36Sopenharmony_ci * but it is still necessary to lock the port. 479562306a36Sopenharmony_ci * 479662306a36Sopenharmony_ci * For a newly detected device, @dev_descr must be NULL. The device 479762306a36Sopenharmony_ci * descriptor retrieved from the device will then be stored in 479862306a36Sopenharmony_ci * @udev->descriptor. For an already existing device, @dev_descr 479962306a36Sopenharmony_ci * must be non-NULL. The device descriptor will be stored there, 480062306a36Sopenharmony_ci * not in @udev->descriptor, because descriptors for registered 480162306a36Sopenharmony_ci * devices are meant to be immutable. 480262306a36Sopenharmony_ci */ 480362306a36Sopenharmony_cistatic int 480462306a36Sopenharmony_cihub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, 480562306a36Sopenharmony_ci int retry_counter, struct usb_device_descriptor *dev_descr) 480662306a36Sopenharmony_ci{ 480762306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 480862306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 480962306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 481062306a36Sopenharmony_ci int retries, operations, retval, i; 481162306a36Sopenharmony_ci unsigned delay = HUB_SHORT_RESET_TIME; 481262306a36Sopenharmony_ci enum usb_device_speed oldspeed = udev->speed; 481362306a36Sopenharmony_ci const char *speed; 481462306a36Sopenharmony_ci int devnum = udev->devnum; 481562306a36Sopenharmony_ci const char *driver_name; 481662306a36Sopenharmony_ci bool do_new_scheme; 481762306a36Sopenharmony_ci const bool initial = !dev_descr; 481862306a36Sopenharmony_ci int maxp0; 481962306a36Sopenharmony_ci struct usb_device_descriptor *buf, *descr; 482062306a36Sopenharmony_ci 482162306a36Sopenharmony_ci buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 482262306a36Sopenharmony_ci if (!buf) 482362306a36Sopenharmony_ci return -ENOMEM; 482462306a36Sopenharmony_ci 482562306a36Sopenharmony_ci /* root hub ports have a slightly longer reset period 482662306a36Sopenharmony_ci * (from USB 2.0 spec, section 7.1.7.5) 482762306a36Sopenharmony_ci */ 482862306a36Sopenharmony_ci if (!hdev->parent) { 482962306a36Sopenharmony_ci delay = HUB_ROOT_RESET_TIME; 483062306a36Sopenharmony_ci if (port1 == hdev->bus->otg_port) 483162306a36Sopenharmony_ci hdev->bus->b_hnp_enable = 0; 483262306a36Sopenharmony_ci } 483362306a36Sopenharmony_ci 483462306a36Sopenharmony_ci /* Some low speed devices have problems with the quick delay, so */ 483562306a36Sopenharmony_ci /* be a bit pessimistic with those devices. RHbug #23670 */ 483662306a36Sopenharmony_ci if (oldspeed == USB_SPEED_LOW) 483762306a36Sopenharmony_ci delay = HUB_LONG_RESET_TIME; 483862306a36Sopenharmony_ci 483962306a36Sopenharmony_ci /* Reset the device; full speed may morph to high speed */ 484062306a36Sopenharmony_ci /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 484162306a36Sopenharmony_ci retval = hub_port_reset(hub, port1, udev, delay, false); 484262306a36Sopenharmony_ci if (retval < 0) /* error or disconnect */ 484362306a36Sopenharmony_ci goto fail; 484462306a36Sopenharmony_ci /* success, speed is known */ 484562306a36Sopenharmony_ci 484662306a36Sopenharmony_ci retval = -ENODEV; 484762306a36Sopenharmony_ci 484862306a36Sopenharmony_ci /* Don't allow speed changes at reset, except usb 3.0 to faster */ 484962306a36Sopenharmony_ci if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && 485062306a36Sopenharmony_ci !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { 485162306a36Sopenharmony_ci dev_dbg(&udev->dev, "device reset changed speed!\n"); 485262306a36Sopenharmony_ci goto fail; 485362306a36Sopenharmony_ci } 485462306a36Sopenharmony_ci oldspeed = udev->speed; 485562306a36Sopenharmony_ci 485662306a36Sopenharmony_ci if (initial) { 485762306a36Sopenharmony_ci /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 485862306a36Sopenharmony_ci * it's fixed size except for full speed devices. 485962306a36Sopenharmony_ci */ 486062306a36Sopenharmony_ci switch (udev->speed) { 486162306a36Sopenharmony_ci case USB_SPEED_SUPER_PLUS: 486262306a36Sopenharmony_ci case USB_SPEED_SUPER: 486362306a36Sopenharmony_ci udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 486462306a36Sopenharmony_ci break; 486562306a36Sopenharmony_ci case USB_SPEED_HIGH: /* fixed at 64 */ 486662306a36Sopenharmony_ci udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 486762306a36Sopenharmony_ci break; 486862306a36Sopenharmony_ci case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 486962306a36Sopenharmony_ci /* to determine the ep0 maxpacket size, try to read 487062306a36Sopenharmony_ci * the device descriptor to get bMaxPacketSize0 and 487162306a36Sopenharmony_ci * then correct our initial guess. 487262306a36Sopenharmony_ci */ 487362306a36Sopenharmony_ci udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 487462306a36Sopenharmony_ci break; 487562306a36Sopenharmony_ci case USB_SPEED_LOW: /* fixed at 8 */ 487662306a36Sopenharmony_ci udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 487762306a36Sopenharmony_ci break; 487862306a36Sopenharmony_ci default: 487962306a36Sopenharmony_ci goto fail; 488062306a36Sopenharmony_ci } 488162306a36Sopenharmony_ci } 488262306a36Sopenharmony_ci 488362306a36Sopenharmony_ci speed = usb_speed_string(udev->speed); 488462306a36Sopenharmony_ci 488562306a36Sopenharmony_ci /* 488662306a36Sopenharmony_ci * The controller driver may be NULL if the controller device 488762306a36Sopenharmony_ci * is the middle device between platform device and roothub. 488862306a36Sopenharmony_ci * This middle device may not need a device driver due to 488962306a36Sopenharmony_ci * all hardware control can be at platform device driver, this 489062306a36Sopenharmony_ci * platform device is usually a dual-role USB controller device. 489162306a36Sopenharmony_ci */ 489262306a36Sopenharmony_ci if (udev->bus->controller->driver) 489362306a36Sopenharmony_ci driver_name = udev->bus->controller->driver->name; 489462306a36Sopenharmony_ci else 489562306a36Sopenharmony_ci driver_name = udev->bus->sysdev->driver->name; 489662306a36Sopenharmony_ci 489762306a36Sopenharmony_ci if (udev->speed < USB_SPEED_SUPER) 489862306a36Sopenharmony_ci dev_info(&udev->dev, 489962306a36Sopenharmony_ci "%s %s USB device number %d using %s\n", 490062306a36Sopenharmony_ci (initial ? "new" : "reset"), speed, 490162306a36Sopenharmony_ci devnum, driver_name); 490262306a36Sopenharmony_ci 490362306a36Sopenharmony_ci if (initial) { 490462306a36Sopenharmony_ci /* Set up TT records, if needed */ 490562306a36Sopenharmony_ci if (hdev->tt) { 490662306a36Sopenharmony_ci udev->tt = hdev->tt; 490762306a36Sopenharmony_ci udev->ttport = hdev->ttport; 490862306a36Sopenharmony_ci } else if (udev->speed != USB_SPEED_HIGH 490962306a36Sopenharmony_ci && hdev->speed == USB_SPEED_HIGH) { 491062306a36Sopenharmony_ci if (!hub->tt.hub) { 491162306a36Sopenharmony_ci dev_err(&udev->dev, "parent hub has no TT\n"); 491262306a36Sopenharmony_ci retval = -EINVAL; 491362306a36Sopenharmony_ci goto fail; 491462306a36Sopenharmony_ci } 491562306a36Sopenharmony_ci udev->tt = &hub->tt; 491662306a36Sopenharmony_ci udev->ttport = port1; 491762306a36Sopenharmony_ci } 491862306a36Sopenharmony_ci } 491962306a36Sopenharmony_ci 492062306a36Sopenharmony_ci /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 492162306a36Sopenharmony_ci * Because device hardware and firmware is sometimes buggy in 492262306a36Sopenharmony_ci * this area, and this is how Linux has done it for ages. 492362306a36Sopenharmony_ci * Change it cautiously. 492462306a36Sopenharmony_ci * 492562306a36Sopenharmony_ci * NOTE: If use_new_scheme() is true we will start by issuing 492662306a36Sopenharmony_ci * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 492762306a36Sopenharmony_ci * so it may help with some non-standards-compliant devices. 492862306a36Sopenharmony_ci * Otherwise we start with SET_ADDRESS and then try to read the 492962306a36Sopenharmony_ci * first 8 bytes of the device descriptor to get the ep0 maxpacket 493062306a36Sopenharmony_ci * value. 493162306a36Sopenharmony_ci */ 493262306a36Sopenharmony_ci do_new_scheme = use_new_scheme(udev, retry_counter, port_dev); 493362306a36Sopenharmony_ci 493462306a36Sopenharmony_ci for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { 493562306a36Sopenharmony_ci if (hub_port_stop_enumerate(hub, port1, retries)) { 493662306a36Sopenharmony_ci retval = -ENODEV; 493762306a36Sopenharmony_ci break; 493862306a36Sopenharmony_ci } 493962306a36Sopenharmony_ci 494062306a36Sopenharmony_ci if (do_new_scheme) { 494162306a36Sopenharmony_ci retval = hub_enable_device(udev); 494262306a36Sopenharmony_ci if (retval < 0) { 494362306a36Sopenharmony_ci dev_err(&udev->dev, 494462306a36Sopenharmony_ci "hub failed to enable device, error %d\n", 494562306a36Sopenharmony_ci retval); 494662306a36Sopenharmony_ci goto fail; 494762306a36Sopenharmony_ci } 494862306a36Sopenharmony_ci 494962306a36Sopenharmony_ci maxp0 = get_bMaxPacketSize0(udev, buf, 495062306a36Sopenharmony_ci GET_DESCRIPTOR_BUFSIZE, retries == 0); 495162306a36Sopenharmony_ci if (maxp0 > 0 && !initial && 495262306a36Sopenharmony_ci maxp0 != udev->descriptor.bMaxPacketSize0) { 495362306a36Sopenharmony_ci dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 495462306a36Sopenharmony_ci retval = -ENODEV; 495562306a36Sopenharmony_ci goto fail; 495662306a36Sopenharmony_ci } 495762306a36Sopenharmony_ci 495862306a36Sopenharmony_ci retval = hub_port_reset(hub, port1, udev, delay, false); 495962306a36Sopenharmony_ci if (retval < 0) /* error or disconnect */ 496062306a36Sopenharmony_ci goto fail; 496162306a36Sopenharmony_ci if (oldspeed != udev->speed) { 496262306a36Sopenharmony_ci dev_dbg(&udev->dev, 496362306a36Sopenharmony_ci "device reset changed speed!\n"); 496462306a36Sopenharmony_ci retval = -ENODEV; 496562306a36Sopenharmony_ci goto fail; 496662306a36Sopenharmony_ci } 496762306a36Sopenharmony_ci if (maxp0 < 0) { 496862306a36Sopenharmony_ci if (maxp0 != -ENODEV) 496962306a36Sopenharmony_ci dev_err(&udev->dev, "device descriptor read/64, error %d\n", 497062306a36Sopenharmony_ci maxp0); 497162306a36Sopenharmony_ci retval = maxp0; 497262306a36Sopenharmony_ci continue; 497362306a36Sopenharmony_ci } 497462306a36Sopenharmony_ci } 497562306a36Sopenharmony_ci 497662306a36Sopenharmony_ci for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) { 497762306a36Sopenharmony_ci retval = hub_set_address(udev, devnum); 497862306a36Sopenharmony_ci if (retval >= 0) 497962306a36Sopenharmony_ci break; 498062306a36Sopenharmony_ci msleep(200); 498162306a36Sopenharmony_ci } 498262306a36Sopenharmony_ci if (retval < 0) { 498362306a36Sopenharmony_ci if (retval != -ENODEV) 498462306a36Sopenharmony_ci dev_err(&udev->dev, "device not accepting address %d, error %d\n", 498562306a36Sopenharmony_ci devnum, retval); 498662306a36Sopenharmony_ci goto fail; 498762306a36Sopenharmony_ci } 498862306a36Sopenharmony_ci if (udev->speed >= USB_SPEED_SUPER) { 498962306a36Sopenharmony_ci devnum = udev->devnum; 499062306a36Sopenharmony_ci dev_info(&udev->dev, 499162306a36Sopenharmony_ci "%s SuperSpeed%s%s USB device number %d using %s\n", 499262306a36Sopenharmony_ci (udev->config) ? "reset" : "new", 499362306a36Sopenharmony_ci (udev->speed == USB_SPEED_SUPER_PLUS) ? 499462306a36Sopenharmony_ci " Plus" : "", 499562306a36Sopenharmony_ci (udev->ssp_rate == USB_SSP_GEN_2x2) ? 499662306a36Sopenharmony_ci " Gen 2x2" : 499762306a36Sopenharmony_ci (udev->ssp_rate == USB_SSP_GEN_2x1) ? 499862306a36Sopenharmony_ci " Gen 2x1" : 499962306a36Sopenharmony_ci (udev->ssp_rate == USB_SSP_GEN_1x2) ? 500062306a36Sopenharmony_ci " Gen 1x2" : "", 500162306a36Sopenharmony_ci devnum, driver_name); 500262306a36Sopenharmony_ci } 500362306a36Sopenharmony_ci 500462306a36Sopenharmony_ci /* 500562306a36Sopenharmony_ci * cope with hardware quirkiness: 500662306a36Sopenharmony_ci * - let SET_ADDRESS settle, some device hardware wants it 500762306a36Sopenharmony_ci * - read ep0 maxpacket even for high and low speed, 500862306a36Sopenharmony_ci */ 500962306a36Sopenharmony_ci msleep(10); 501062306a36Sopenharmony_ci 501162306a36Sopenharmony_ci if (do_new_scheme) 501262306a36Sopenharmony_ci break; 501362306a36Sopenharmony_ci 501462306a36Sopenharmony_ci maxp0 = get_bMaxPacketSize0(udev, buf, 8, retries == 0); 501562306a36Sopenharmony_ci if (maxp0 < 0) { 501662306a36Sopenharmony_ci retval = maxp0; 501762306a36Sopenharmony_ci if (retval != -ENODEV) 501862306a36Sopenharmony_ci dev_err(&udev->dev, 501962306a36Sopenharmony_ci "device descriptor read/8, error %d\n", 502062306a36Sopenharmony_ci retval); 502162306a36Sopenharmony_ci } else { 502262306a36Sopenharmony_ci u32 delay; 502362306a36Sopenharmony_ci 502462306a36Sopenharmony_ci if (!initial && maxp0 != udev->descriptor.bMaxPacketSize0) { 502562306a36Sopenharmony_ci dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 502662306a36Sopenharmony_ci retval = -ENODEV; 502762306a36Sopenharmony_ci goto fail; 502862306a36Sopenharmony_ci } 502962306a36Sopenharmony_ci 503062306a36Sopenharmony_ci delay = udev->parent->hub_delay; 503162306a36Sopenharmony_ci udev->hub_delay = min_t(u32, delay, 503262306a36Sopenharmony_ci USB_TP_TRANSMISSION_DELAY_MAX); 503362306a36Sopenharmony_ci retval = usb_set_isoch_delay(udev); 503462306a36Sopenharmony_ci if (retval) { 503562306a36Sopenharmony_ci dev_dbg(&udev->dev, 503662306a36Sopenharmony_ci "Failed set isoch delay, error %d\n", 503762306a36Sopenharmony_ci retval); 503862306a36Sopenharmony_ci retval = 0; 503962306a36Sopenharmony_ci } 504062306a36Sopenharmony_ci break; 504162306a36Sopenharmony_ci } 504262306a36Sopenharmony_ci } 504362306a36Sopenharmony_ci if (retval) 504462306a36Sopenharmony_ci goto fail; 504562306a36Sopenharmony_ci 504662306a36Sopenharmony_ci /* 504762306a36Sopenharmony_ci * Check the ep0 maxpacket guess and correct it if necessary. 504862306a36Sopenharmony_ci * maxp0 is the value stored in the device descriptor; 504962306a36Sopenharmony_ci * i is the value it encodes (logarithmic for SuperSpeed or greater). 505062306a36Sopenharmony_ci */ 505162306a36Sopenharmony_ci i = maxp0; 505262306a36Sopenharmony_ci if (udev->speed >= USB_SPEED_SUPER) { 505362306a36Sopenharmony_ci if (maxp0 <= 16) 505462306a36Sopenharmony_ci i = 1 << maxp0; 505562306a36Sopenharmony_ci else 505662306a36Sopenharmony_ci i = 0; /* Invalid */ 505762306a36Sopenharmony_ci } 505862306a36Sopenharmony_ci if (usb_endpoint_maxp(&udev->ep0.desc) == i) { 505962306a36Sopenharmony_ci ; /* Initial ep0 maxpacket guess is right */ 506062306a36Sopenharmony_ci } else if ((udev->speed == USB_SPEED_FULL || 506162306a36Sopenharmony_ci udev->speed == USB_SPEED_HIGH) && 506262306a36Sopenharmony_ci (i == 8 || i == 16 || i == 32 || i == 64)) { 506362306a36Sopenharmony_ci /* Initial guess is wrong; use the descriptor's value */ 506462306a36Sopenharmony_ci if (udev->speed == USB_SPEED_FULL) 506562306a36Sopenharmony_ci dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 506662306a36Sopenharmony_ci else 506762306a36Sopenharmony_ci dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 506862306a36Sopenharmony_ci udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 506962306a36Sopenharmony_ci usb_ep0_reinit(udev); 507062306a36Sopenharmony_ci } else { 507162306a36Sopenharmony_ci /* Initial guess is wrong and descriptor's value is invalid */ 507262306a36Sopenharmony_ci dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", maxp0); 507362306a36Sopenharmony_ci retval = -EMSGSIZE; 507462306a36Sopenharmony_ci goto fail; 507562306a36Sopenharmony_ci } 507662306a36Sopenharmony_ci 507762306a36Sopenharmony_ci descr = usb_get_device_descriptor(udev); 507862306a36Sopenharmony_ci if (IS_ERR(descr)) { 507962306a36Sopenharmony_ci retval = PTR_ERR(descr); 508062306a36Sopenharmony_ci if (retval != -ENODEV) 508162306a36Sopenharmony_ci dev_err(&udev->dev, "device descriptor read/all, error %d\n", 508262306a36Sopenharmony_ci retval); 508362306a36Sopenharmony_ci goto fail; 508462306a36Sopenharmony_ci } 508562306a36Sopenharmony_ci if (initial) 508662306a36Sopenharmony_ci udev->descriptor = *descr; 508762306a36Sopenharmony_ci else 508862306a36Sopenharmony_ci *dev_descr = *descr; 508962306a36Sopenharmony_ci kfree(descr); 509062306a36Sopenharmony_ci 509162306a36Sopenharmony_ci /* 509262306a36Sopenharmony_ci * Some superspeed devices have finished the link training process 509362306a36Sopenharmony_ci * and attached to a superspeed hub port, but the device descriptor 509462306a36Sopenharmony_ci * got from those devices show they aren't superspeed devices. Warm 509562306a36Sopenharmony_ci * reset the port attached by the devices can fix them. 509662306a36Sopenharmony_ci */ 509762306a36Sopenharmony_ci if ((udev->speed >= USB_SPEED_SUPER) && 509862306a36Sopenharmony_ci (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 509962306a36Sopenharmony_ci dev_err(&udev->dev, "got a wrong device descriptor, warm reset device\n"); 510062306a36Sopenharmony_ci hub_port_reset(hub, port1, udev, HUB_BH_RESET_TIME, true); 510162306a36Sopenharmony_ci retval = -EINVAL; 510262306a36Sopenharmony_ci goto fail; 510362306a36Sopenharmony_ci } 510462306a36Sopenharmony_ci 510562306a36Sopenharmony_ci usb_detect_quirks(udev); 510662306a36Sopenharmony_ci 510762306a36Sopenharmony_ci if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 510862306a36Sopenharmony_ci retval = usb_get_bos_descriptor(udev); 510962306a36Sopenharmony_ci if (!retval) { 511062306a36Sopenharmony_ci udev->lpm_capable = usb_device_supports_lpm(udev); 511162306a36Sopenharmony_ci udev->lpm_disable_count = 1; 511262306a36Sopenharmony_ci usb_set_lpm_parameters(udev); 511362306a36Sopenharmony_ci usb_req_set_sel(udev); 511462306a36Sopenharmony_ci } 511562306a36Sopenharmony_ci } 511662306a36Sopenharmony_ci 511762306a36Sopenharmony_ci retval = 0; 511862306a36Sopenharmony_ci /* notify HCD that we have a device connected and addressed */ 511962306a36Sopenharmony_ci if (hcd->driver->update_device) 512062306a36Sopenharmony_ci hcd->driver->update_device(hcd, udev); 512162306a36Sopenharmony_ci hub_set_initial_usb2_lpm_policy(udev); 512262306a36Sopenharmony_cifail: 512362306a36Sopenharmony_ci if (retval) { 512462306a36Sopenharmony_ci hub_port_disable(hub, port1, 0); 512562306a36Sopenharmony_ci update_devnum(udev, devnum); /* for disconnect processing */ 512662306a36Sopenharmony_ci } 512762306a36Sopenharmony_ci kfree(buf); 512862306a36Sopenharmony_ci return retval; 512962306a36Sopenharmony_ci} 513062306a36Sopenharmony_ci 513162306a36Sopenharmony_cistatic void 513262306a36Sopenharmony_cicheck_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) 513362306a36Sopenharmony_ci{ 513462306a36Sopenharmony_ci struct usb_qualifier_descriptor *qual; 513562306a36Sopenharmony_ci int status; 513662306a36Sopenharmony_ci 513762306a36Sopenharmony_ci if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) 513862306a36Sopenharmony_ci return; 513962306a36Sopenharmony_ci 514062306a36Sopenharmony_ci qual = kmalloc(sizeof *qual, GFP_KERNEL); 514162306a36Sopenharmony_ci if (qual == NULL) 514262306a36Sopenharmony_ci return; 514362306a36Sopenharmony_ci 514462306a36Sopenharmony_ci status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0, 514562306a36Sopenharmony_ci qual, sizeof *qual); 514662306a36Sopenharmony_ci if (status == sizeof *qual) { 514762306a36Sopenharmony_ci dev_info(&udev->dev, "not running at top speed; " 514862306a36Sopenharmony_ci "connect to a high speed hub\n"); 514962306a36Sopenharmony_ci /* hub LEDs are probably harder to miss than syslog */ 515062306a36Sopenharmony_ci if (hub->has_indicators) { 515162306a36Sopenharmony_ci hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 515262306a36Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 515362306a36Sopenharmony_ci &hub->leds, 0); 515462306a36Sopenharmony_ci } 515562306a36Sopenharmony_ci } 515662306a36Sopenharmony_ci kfree(qual); 515762306a36Sopenharmony_ci} 515862306a36Sopenharmony_ci 515962306a36Sopenharmony_cistatic unsigned 516062306a36Sopenharmony_cihub_power_remaining(struct usb_hub *hub) 516162306a36Sopenharmony_ci{ 516262306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 516362306a36Sopenharmony_ci int remaining; 516462306a36Sopenharmony_ci int port1; 516562306a36Sopenharmony_ci 516662306a36Sopenharmony_ci if (!hub->limited_power) 516762306a36Sopenharmony_ci return 0; 516862306a36Sopenharmony_ci 516962306a36Sopenharmony_ci remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 517062306a36Sopenharmony_ci for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 517162306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 517262306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 517362306a36Sopenharmony_ci unsigned unit_load; 517462306a36Sopenharmony_ci int delta; 517562306a36Sopenharmony_ci 517662306a36Sopenharmony_ci if (!udev) 517762306a36Sopenharmony_ci continue; 517862306a36Sopenharmony_ci if (hub_is_superspeed(udev)) 517962306a36Sopenharmony_ci unit_load = 150; 518062306a36Sopenharmony_ci else 518162306a36Sopenharmony_ci unit_load = 100; 518262306a36Sopenharmony_ci 518362306a36Sopenharmony_ci /* 518462306a36Sopenharmony_ci * Unconfigured devices may not use more than one unit load, 518562306a36Sopenharmony_ci * or 8mA for OTG ports 518662306a36Sopenharmony_ci */ 518762306a36Sopenharmony_ci if (udev->actconfig) 518862306a36Sopenharmony_ci delta = usb_get_max_power(udev, udev->actconfig); 518962306a36Sopenharmony_ci else if (port1 != udev->bus->otg_port || hdev->parent) 519062306a36Sopenharmony_ci delta = unit_load; 519162306a36Sopenharmony_ci else 519262306a36Sopenharmony_ci delta = 8; 519362306a36Sopenharmony_ci if (delta > hub->mA_per_port) 519462306a36Sopenharmony_ci dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", 519562306a36Sopenharmony_ci delta, hub->mA_per_port); 519662306a36Sopenharmony_ci remaining -= delta; 519762306a36Sopenharmony_ci } 519862306a36Sopenharmony_ci if (remaining < 0) { 519962306a36Sopenharmony_ci dev_warn(hub->intfdev, "%dmA over power budget!\n", 520062306a36Sopenharmony_ci -remaining); 520162306a36Sopenharmony_ci remaining = 0; 520262306a36Sopenharmony_ci } 520362306a36Sopenharmony_ci return remaining; 520462306a36Sopenharmony_ci} 520562306a36Sopenharmony_ci 520662306a36Sopenharmony_ci 520762306a36Sopenharmony_cistatic int descriptors_changed(struct usb_device *udev, 520862306a36Sopenharmony_ci struct usb_device_descriptor *new_device_descriptor, 520962306a36Sopenharmony_ci struct usb_host_bos *old_bos) 521062306a36Sopenharmony_ci{ 521162306a36Sopenharmony_ci int changed = 0; 521262306a36Sopenharmony_ci unsigned index; 521362306a36Sopenharmony_ci unsigned serial_len = 0; 521462306a36Sopenharmony_ci unsigned len; 521562306a36Sopenharmony_ci unsigned old_length; 521662306a36Sopenharmony_ci int length; 521762306a36Sopenharmony_ci char *buf; 521862306a36Sopenharmony_ci 521962306a36Sopenharmony_ci if (memcmp(&udev->descriptor, new_device_descriptor, 522062306a36Sopenharmony_ci sizeof(*new_device_descriptor)) != 0) 522162306a36Sopenharmony_ci return 1; 522262306a36Sopenharmony_ci 522362306a36Sopenharmony_ci if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) 522462306a36Sopenharmony_ci return 1; 522562306a36Sopenharmony_ci if (udev->bos) { 522662306a36Sopenharmony_ci len = le16_to_cpu(udev->bos->desc->wTotalLength); 522762306a36Sopenharmony_ci if (len != le16_to_cpu(old_bos->desc->wTotalLength)) 522862306a36Sopenharmony_ci return 1; 522962306a36Sopenharmony_ci if (memcmp(udev->bos->desc, old_bos->desc, len)) 523062306a36Sopenharmony_ci return 1; 523162306a36Sopenharmony_ci } 523262306a36Sopenharmony_ci 523362306a36Sopenharmony_ci /* Since the idVendor, idProduct, and bcdDevice values in the 523462306a36Sopenharmony_ci * device descriptor haven't changed, we will assume the 523562306a36Sopenharmony_ci * Manufacturer and Product strings haven't changed either. 523662306a36Sopenharmony_ci * But the SerialNumber string could be different (e.g., a 523762306a36Sopenharmony_ci * different flash card of the same brand). 523862306a36Sopenharmony_ci */ 523962306a36Sopenharmony_ci if (udev->serial) 524062306a36Sopenharmony_ci serial_len = strlen(udev->serial) + 1; 524162306a36Sopenharmony_ci 524262306a36Sopenharmony_ci len = serial_len; 524362306a36Sopenharmony_ci for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 524462306a36Sopenharmony_ci old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 524562306a36Sopenharmony_ci len = max(len, old_length); 524662306a36Sopenharmony_ci } 524762306a36Sopenharmony_ci 524862306a36Sopenharmony_ci buf = kmalloc(len, GFP_NOIO); 524962306a36Sopenharmony_ci if (!buf) 525062306a36Sopenharmony_ci /* assume the worst */ 525162306a36Sopenharmony_ci return 1; 525262306a36Sopenharmony_ci 525362306a36Sopenharmony_ci for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 525462306a36Sopenharmony_ci old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 525562306a36Sopenharmony_ci length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 525662306a36Sopenharmony_ci old_length); 525762306a36Sopenharmony_ci if (length != old_length) { 525862306a36Sopenharmony_ci dev_dbg(&udev->dev, "config index %d, error %d\n", 525962306a36Sopenharmony_ci index, length); 526062306a36Sopenharmony_ci changed = 1; 526162306a36Sopenharmony_ci break; 526262306a36Sopenharmony_ci } 526362306a36Sopenharmony_ci if (memcmp(buf, udev->rawdescriptors[index], old_length) 526462306a36Sopenharmony_ci != 0) { 526562306a36Sopenharmony_ci dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 526662306a36Sopenharmony_ci index, 526762306a36Sopenharmony_ci ((struct usb_config_descriptor *) buf)-> 526862306a36Sopenharmony_ci bConfigurationValue); 526962306a36Sopenharmony_ci changed = 1; 527062306a36Sopenharmony_ci break; 527162306a36Sopenharmony_ci } 527262306a36Sopenharmony_ci } 527362306a36Sopenharmony_ci 527462306a36Sopenharmony_ci if (!changed && serial_len) { 527562306a36Sopenharmony_ci length = usb_string(udev, udev->descriptor.iSerialNumber, 527662306a36Sopenharmony_ci buf, serial_len); 527762306a36Sopenharmony_ci if (length + 1 != serial_len) { 527862306a36Sopenharmony_ci dev_dbg(&udev->dev, "serial string error %d\n", 527962306a36Sopenharmony_ci length); 528062306a36Sopenharmony_ci changed = 1; 528162306a36Sopenharmony_ci } else if (memcmp(buf, udev->serial, length) != 0) { 528262306a36Sopenharmony_ci dev_dbg(&udev->dev, "serial string changed\n"); 528362306a36Sopenharmony_ci changed = 1; 528462306a36Sopenharmony_ci } 528562306a36Sopenharmony_ci } 528662306a36Sopenharmony_ci 528762306a36Sopenharmony_ci kfree(buf); 528862306a36Sopenharmony_ci return changed; 528962306a36Sopenharmony_ci} 529062306a36Sopenharmony_ci 529162306a36Sopenharmony_cistatic void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, 529262306a36Sopenharmony_ci u16 portchange) 529362306a36Sopenharmony_ci{ 529462306a36Sopenharmony_ci int status = -ENODEV; 529562306a36Sopenharmony_ci int i; 529662306a36Sopenharmony_ci unsigned unit_load; 529762306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 529862306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 529962306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 530062306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 530162306a36Sopenharmony_ci static int unreliable_port = -1; 530262306a36Sopenharmony_ci bool retry_locked; 530362306a36Sopenharmony_ci 530462306a36Sopenharmony_ci /* Disconnect any existing devices under this port */ 530562306a36Sopenharmony_ci if (udev) { 530662306a36Sopenharmony_ci if (hcd->usb_phy && !hdev->parent) 530762306a36Sopenharmony_ci usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); 530862306a36Sopenharmony_ci usb_disconnect(&port_dev->child); 530962306a36Sopenharmony_ci } 531062306a36Sopenharmony_ci 531162306a36Sopenharmony_ci /* We can forget about a "removed" device when there's a physical 531262306a36Sopenharmony_ci * disconnect or the connect status changes. 531362306a36Sopenharmony_ci */ 531462306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_CONNECTION) || 531562306a36Sopenharmony_ci (portchange & USB_PORT_STAT_C_CONNECTION)) 531662306a36Sopenharmony_ci clear_bit(port1, hub->removed_bits); 531762306a36Sopenharmony_ci 531862306a36Sopenharmony_ci if (portchange & (USB_PORT_STAT_C_CONNECTION | 531962306a36Sopenharmony_ci USB_PORT_STAT_C_ENABLE)) { 532062306a36Sopenharmony_ci status = hub_port_debounce_be_stable(hub, port1); 532162306a36Sopenharmony_ci if (status < 0) { 532262306a36Sopenharmony_ci if (status != -ENODEV && 532362306a36Sopenharmony_ci port1 != unreliable_port && 532462306a36Sopenharmony_ci printk_ratelimit()) 532562306a36Sopenharmony_ci dev_err(&port_dev->dev, "connect-debounce failed\n"); 532662306a36Sopenharmony_ci portstatus &= ~USB_PORT_STAT_CONNECTION; 532762306a36Sopenharmony_ci unreliable_port = port1; 532862306a36Sopenharmony_ci } else { 532962306a36Sopenharmony_ci portstatus = status; 533062306a36Sopenharmony_ci } 533162306a36Sopenharmony_ci } 533262306a36Sopenharmony_ci 533362306a36Sopenharmony_ci /* Return now if debouncing failed or nothing is connected or 533462306a36Sopenharmony_ci * the device was "removed". 533562306a36Sopenharmony_ci */ 533662306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_CONNECTION) || 533762306a36Sopenharmony_ci test_bit(port1, hub->removed_bits)) { 533862306a36Sopenharmony_ci 533962306a36Sopenharmony_ci /* 534062306a36Sopenharmony_ci * maybe switch power back on (e.g. root hub was reset) 534162306a36Sopenharmony_ci * but only if the port isn't owned by someone else. 534262306a36Sopenharmony_ci */ 534362306a36Sopenharmony_ci if (hub_is_port_power_switchable(hub) 534462306a36Sopenharmony_ci && !usb_port_is_power_on(hub, portstatus) 534562306a36Sopenharmony_ci && !port_dev->port_owner) 534662306a36Sopenharmony_ci set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 534762306a36Sopenharmony_ci 534862306a36Sopenharmony_ci if (portstatus & USB_PORT_STAT_ENABLE) 534962306a36Sopenharmony_ci goto done; 535062306a36Sopenharmony_ci return; 535162306a36Sopenharmony_ci } 535262306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) 535362306a36Sopenharmony_ci unit_load = 150; 535462306a36Sopenharmony_ci else 535562306a36Sopenharmony_ci unit_load = 100; 535662306a36Sopenharmony_ci 535762306a36Sopenharmony_ci status = 0; 535862306a36Sopenharmony_ci 535962306a36Sopenharmony_ci for (i = 0; i < PORT_INIT_TRIES; i++) { 536062306a36Sopenharmony_ci if (hub_port_stop_enumerate(hub, port1, i)) { 536162306a36Sopenharmony_ci status = -ENODEV; 536262306a36Sopenharmony_ci break; 536362306a36Sopenharmony_ci } 536462306a36Sopenharmony_ci 536562306a36Sopenharmony_ci usb_lock_port(port_dev); 536662306a36Sopenharmony_ci mutex_lock(hcd->address0_mutex); 536762306a36Sopenharmony_ci retry_locked = true; 536862306a36Sopenharmony_ci /* reallocate for each attempt, since references 536962306a36Sopenharmony_ci * to the previous one can escape in various ways 537062306a36Sopenharmony_ci */ 537162306a36Sopenharmony_ci udev = usb_alloc_dev(hdev, hdev->bus, port1); 537262306a36Sopenharmony_ci if (!udev) { 537362306a36Sopenharmony_ci dev_err(&port_dev->dev, 537462306a36Sopenharmony_ci "couldn't allocate usb_device\n"); 537562306a36Sopenharmony_ci mutex_unlock(hcd->address0_mutex); 537662306a36Sopenharmony_ci usb_unlock_port(port_dev); 537762306a36Sopenharmony_ci goto done; 537862306a36Sopenharmony_ci } 537962306a36Sopenharmony_ci 538062306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_POWERED); 538162306a36Sopenharmony_ci udev->bus_mA = hub->mA_per_port; 538262306a36Sopenharmony_ci udev->level = hdev->level + 1; 538362306a36Sopenharmony_ci 538462306a36Sopenharmony_ci /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ 538562306a36Sopenharmony_ci if (hub_is_superspeed(hub->hdev)) 538662306a36Sopenharmony_ci udev->speed = USB_SPEED_SUPER; 538762306a36Sopenharmony_ci else 538862306a36Sopenharmony_ci udev->speed = USB_SPEED_UNKNOWN; 538962306a36Sopenharmony_ci 539062306a36Sopenharmony_ci choose_devnum(udev); 539162306a36Sopenharmony_ci if (udev->devnum <= 0) { 539262306a36Sopenharmony_ci status = -ENOTCONN; /* Don't retry */ 539362306a36Sopenharmony_ci goto loop; 539462306a36Sopenharmony_ci } 539562306a36Sopenharmony_ci 539662306a36Sopenharmony_ci /* reset (non-USB 3.0 devices) and get descriptor */ 539762306a36Sopenharmony_ci status = hub_port_init(hub, udev, port1, i, NULL); 539862306a36Sopenharmony_ci if (status < 0) 539962306a36Sopenharmony_ci goto loop; 540062306a36Sopenharmony_ci 540162306a36Sopenharmony_ci mutex_unlock(hcd->address0_mutex); 540262306a36Sopenharmony_ci usb_unlock_port(port_dev); 540362306a36Sopenharmony_ci retry_locked = false; 540462306a36Sopenharmony_ci 540562306a36Sopenharmony_ci if (udev->quirks & USB_QUIRK_DELAY_INIT) 540662306a36Sopenharmony_ci msleep(2000); 540762306a36Sopenharmony_ci 540862306a36Sopenharmony_ci /* consecutive bus-powered hubs aren't reliable; they can 540962306a36Sopenharmony_ci * violate the voltage drop budget. if the new child has 541062306a36Sopenharmony_ci * a "powered" LED, users should notice we didn't enable it 541162306a36Sopenharmony_ci * (without reading syslog), even without per-port LEDs 541262306a36Sopenharmony_ci * on the parent. 541362306a36Sopenharmony_ci */ 541462306a36Sopenharmony_ci if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 541562306a36Sopenharmony_ci && udev->bus_mA <= unit_load) { 541662306a36Sopenharmony_ci u16 devstat; 541762306a36Sopenharmony_ci 541862306a36Sopenharmony_ci status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, 541962306a36Sopenharmony_ci &devstat); 542062306a36Sopenharmony_ci if (status) { 542162306a36Sopenharmony_ci dev_dbg(&udev->dev, "get status %d ?\n", status); 542262306a36Sopenharmony_ci goto loop_disable; 542362306a36Sopenharmony_ci } 542462306a36Sopenharmony_ci if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 542562306a36Sopenharmony_ci dev_err(&udev->dev, 542662306a36Sopenharmony_ci "can't connect bus-powered hub " 542762306a36Sopenharmony_ci "to this port\n"); 542862306a36Sopenharmony_ci if (hub->has_indicators) { 542962306a36Sopenharmony_ci hub->indicator[port1-1] = 543062306a36Sopenharmony_ci INDICATOR_AMBER_BLINK; 543162306a36Sopenharmony_ci queue_delayed_work( 543262306a36Sopenharmony_ci system_power_efficient_wq, 543362306a36Sopenharmony_ci &hub->leds, 0); 543462306a36Sopenharmony_ci } 543562306a36Sopenharmony_ci status = -ENOTCONN; /* Don't retry */ 543662306a36Sopenharmony_ci goto loop_disable; 543762306a36Sopenharmony_ci } 543862306a36Sopenharmony_ci } 543962306a36Sopenharmony_ci 544062306a36Sopenharmony_ci /* check for devices running slower than they could */ 544162306a36Sopenharmony_ci if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 544262306a36Sopenharmony_ci && udev->speed == USB_SPEED_FULL 544362306a36Sopenharmony_ci && highspeed_hubs != 0) 544462306a36Sopenharmony_ci check_highspeed(hub, udev, port1); 544562306a36Sopenharmony_ci 544662306a36Sopenharmony_ci /* Store the parent's children[] pointer. At this point 544762306a36Sopenharmony_ci * udev becomes globally accessible, although presumably 544862306a36Sopenharmony_ci * no one will look at it until hdev is unlocked. 544962306a36Sopenharmony_ci */ 545062306a36Sopenharmony_ci status = 0; 545162306a36Sopenharmony_ci 545262306a36Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 545362306a36Sopenharmony_ci 545462306a36Sopenharmony_ci /* We mustn't add new devices if the parent hub has 545562306a36Sopenharmony_ci * been disconnected; we would race with the 545662306a36Sopenharmony_ci * recursively_mark_NOTATTACHED() routine. 545762306a36Sopenharmony_ci */ 545862306a36Sopenharmony_ci spin_lock_irq(&device_state_lock); 545962306a36Sopenharmony_ci if (hdev->state == USB_STATE_NOTATTACHED) 546062306a36Sopenharmony_ci status = -ENOTCONN; 546162306a36Sopenharmony_ci else 546262306a36Sopenharmony_ci port_dev->child = udev; 546362306a36Sopenharmony_ci spin_unlock_irq(&device_state_lock); 546462306a36Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 546562306a36Sopenharmony_ci 546662306a36Sopenharmony_ci /* Run it through the hoops (find a driver, etc) */ 546762306a36Sopenharmony_ci if (!status) { 546862306a36Sopenharmony_ci status = usb_new_device(udev); 546962306a36Sopenharmony_ci if (status) { 547062306a36Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 547162306a36Sopenharmony_ci spin_lock_irq(&device_state_lock); 547262306a36Sopenharmony_ci port_dev->child = NULL; 547362306a36Sopenharmony_ci spin_unlock_irq(&device_state_lock); 547462306a36Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 547562306a36Sopenharmony_ci } else { 547662306a36Sopenharmony_ci if (hcd->usb_phy && !hdev->parent) 547762306a36Sopenharmony_ci usb_phy_notify_connect(hcd->usb_phy, 547862306a36Sopenharmony_ci udev->speed); 547962306a36Sopenharmony_ci } 548062306a36Sopenharmony_ci } 548162306a36Sopenharmony_ci 548262306a36Sopenharmony_ci if (status) 548362306a36Sopenharmony_ci goto loop_disable; 548462306a36Sopenharmony_ci 548562306a36Sopenharmony_ci status = hub_power_remaining(hub); 548662306a36Sopenharmony_ci if (status) 548762306a36Sopenharmony_ci dev_dbg(hub->intfdev, "%dmA power budget left\n", status); 548862306a36Sopenharmony_ci 548962306a36Sopenharmony_ci return; 549062306a36Sopenharmony_ci 549162306a36Sopenharmony_ciloop_disable: 549262306a36Sopenharmony_ci hub_port_disable(hub, port1, 1); 549362306a36Sopenharmony_ciloop: 549462306a36Sopenharmony_ci usb_ep0_reinit(udev); 549562306a36Sopenharmony_ci release_devnum(udev); 549662306a36Sopenharmony_ci hub_free_dev(udev); 549762306a36Sopenharmony_ci if (retry_locked) { 549862306a36Sopenharmony_ci mutex_unlock(hcd->address0_mutex); 549962306a36Sopenharmony_ci usb_unlock_port(port_dev); 550062306a36Sopenharmony_ci } 550162306a36Sopenharmony_ci usb_put_dev(udev); 550262306a36Sopenharmony_ci if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 550362306a36Sopenharmony_ci break; 550462306a36Sopenharmony_ci 550562306a36Sopenharmony_ci /* When halfway through our retry count, power-cycle the port */ 550662306a36Sopenharmony_ci if (i == (PORT_INIT_TRIES - 1) / 2) { 550762306a36Sopenharmony_ci dev_info(&port_dev->dev, "attempt power cycle\n"); 550862306a36Sopenharmony_ci usb_hub_set_port_power(hdev, hub, port1, false); 550962306a36Sopenharmony_ci msleep(2 * hub_power_on_good_delay(hub)); 551062306a36Sopenharmony_ci usb_hub_set_port_power(hdev, hub, port1, true); 551162306a36Sopenharmony_ci msleep(hub_power_on_good_delay(hub)); 551262306a36Sopenharmony_ci } 551362306a36Sopenharmony_ci } 551462306a36Sopenharmony_ci if (hub->hdev->parent || 551562306a36Sopenharmony_ci !hcd->driver->port_handed_over || 551662306a36Sopenharmony_ci !(hcd->driver->port_handed_over)(hcd, port1)) { 551762306a36Sopenharmony_ci if (status != -ENOTCONN && status != -ENODEV) 551862306a36Sopenharmony_ci dev_err(&port_dev->dev, 551962306a36Sopenharmony_ci "unable to enumerate USB device\n"); 552062306a36Sopenharmony_ci } 552162306a36Sopenharmony_ci 552262306a36Sopenharmony_cidone: 552362306a36Sopenharmony_ci hub_port_disable(hub, port1, 1); 552462306a36Sopenharmony_ci if (hcd->driver->relinquish_port && !hub->hdev->parent) { 552562306a36Sopenharmony_ci if (status != -ENOTCONN && status != -ENODEV) 552662306a36Sopenharmony_ci hcd->driver->relinquish_port(hcd, port1); 552762306a36Sopenharmony_ci } 552862306a36Sopenharmony_ci} 552962306a36Sopenharmony_ci 553062306a36Sopenharmony_ci/* Handle physical or logical connection change events. 553162306a36Sopenharmony_ci * This routine is called when: 553262306a36Sopenharmony_ci * a port connection-change occurs; 553362306a36Sopenharmony_ci * a port enable-change occurs (often caused by EMI); 553462306a36Sopenharmony_ci * usb_reset_and_verify_device() encounters changed descriptors (as from 553562306a36Sopenharmony_ci * a firmware download) 553662306a36Sopenharmony_ci * caller already locked the hub 553762306a36Sopenharmony_ci */ 553862306a36Sopenharmony_cistatic void hub_port_connect_change(struct usb_hub *hub, int port1, 553962306a36Sopenharmony_ci u16 portstatus, u16 portchange) 554062306a36Sopenharmony_ci __must_hold(&port_dev->status_lock) 554162306a36Sopenharmony_ci{ 554262306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 554362306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 554462306a36Sopenharmony_ci struct usb_device_descriptor *descr; 554562306a36Sopenharmony_ci int status = -ENODEV; 554662306a36Sopenharmony_ci 554762306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, 554862306a36Sopenharmony_ci portchange, portspeed(hub, portstatus)); 554962306a36Sopenharmony_ci 555062306a36Sopenharmony_ci if (hub->has_indicators) { 555162306a36Sopenharmony_ci set_port_led(hub, port1, HUB_LED_AUTO); 555262306a36Sopenharmony_ci hub->indicator[port1-1] = INDICATOR_AUTO; 555362306a36Sopenharmony_ci } 555462306a36Sopenharmony_ci 555562306a36Sopenharmony_ci#ifdef CONFIG_USB_OTG 555662306a36Sopenharmony_ci /* during HNP, don't repeat the debounce */ 555762306a36Sopenharmony_ci if (hub->hdev->bus->is_b_host) 555862306a36Sopenharmony_ci portchange &= ~(USB_PORT_STAT_C_CONNECTION | 555962306a36Sopenharmony_ci USB_PORT_STAT_C_ENABLE); 556062306a36Sopenharmony_ci#endif 556162306a36Sopenharmony_ci 556262306a36Sopenharmony_ci /* Try to resuscitate an existing device */ 556362306a36Sopenharmony_ci if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 556462306a36Sopenharmony_ci udev->state != USB_STATE_NOTATTACHED) { 556562306a36Sopenharmony_ci if (portstatus & USB_PORT_STAT_ENABLE) { 556662306a36Sopenharmony_ci /* 556762306a36Sopenharmony_ci * USB-3 connections are initialized automatically by 556862306a36Sopenharmony_ci * the hostcontroller hardware. Therefore check for 556962306a36Sopenharmony_ci * changed device descriptors before resuscitating the 557062306a36Sopenharmony_ci * device. 557162306a36Sopenharmony_ci */ 557262306a36Sopenharmony_ci descr = usb_get_device_descriptor(udev); 557362306a36Sopenharmony_ci if (IS_ERR(descr)) { 557462306a36Sopenharmony_ci dev_dbg(&udev->dev, 557562306a36Sopenharmony_ci "can't read device descriptor %ld\n", 557662306a36Sopenharmony_ci PTR_ERR(descr)); 557762306a36Sopenharmony_ci } else { 557862306a36Sopenharmony_ci if (descriptors_changed(udev, descr, 557962306a36Sopenharmony_ci udev->bos)) { 558062306a36Sopenharmony_ci dev_dbg(&udev->dev, 558162306a36Sopenharmony_ci "device descriptor has changed\n"); 558262306a36Sopenharmony_ci } else { 558362306a36Sopenharmony_ci status = 0; /* Nothing to do */ 558462306a36Sopenharmony_ci } 558562306a36Sopenharmony_ci kfree(descr); 558662306a36Sopenharmony_ci } 558762306a36Sopenharmony_ci#ifdef CONFIG_PM 558862306a36Sopenharmony_ci } else if (udev->state == USB_STATE_SUSPENDED && 558962306a36Sopenharmony_ci udev->persist_enabled) { 559062306a36Sopenharmony_ci /* For a suspended device, treat this as a 559162306a36Sopenharmony_ci * remote wakeup event. 559262306a36Sopenharmony_ci */ 559362306a36Sopenharmony_ci usb_unlock_port(port_dev); 559462306a36Sopenharmony_ci status = usb_remote_wakeup(udev); 559562306a36Sopenharmony_ci usb_lock_port(port_dev); 559662306a36Sopenharmony_ci#endif 559762306a36Sopenharmony_ci } else { 559862306a36Sopenharmony_ci /* Don't resuscitate */; 559962306a36Sopenharmony_ci } 560062306a36Sopenharmony_ci } 560162306a36Sopenharmony_ci clear_bit(port1, hub->change_bits); 560262306a36Sopenharmony_ci 560362306a36Sopenharmony_ci /* successfully revalidated the connection */ 560462306a36Sopenharmony_ci if (status == 0) 560562306a36Sopenharmony_ci return; 560662306a36Sopenharmony_ci 560762306a36Sopenharmony_ci usb_unlock_port(port_dev); 560862306a36Sopenharmony_ci hub_port_connect(hub, port1, portstatus, portchange); 560962306a36Sopenharmony_ci usb_lock_port(port_dev); 561062306a36Sopenharmony_ci} 561162306a36Sopenharmony_ci 561262306a36Sopenharmony_ci/* Handle notifying userspace about hub over-current events */ 561362306a36Sopenharmony_cistatic void port_over_current_notify(struct usb_port *port_dev) 561462306a36Sopenharmony_ci{ 561562306a36Sopenharmony_ci char *envp[3] = { NULL, NULL, NULL }; 561662306a36Sopenharmony_ci struct device *hub_dev; 561762306a36Sopenharmony_ci char *port_dev_path; 561862306a36Sopenharmony_ci 561962306a36Sopenharmony_ci sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); 562062306a36Sopenharmony_ci 562162306a36Sopenharmony_ci hub_dev = port_dev->dev.parent; 562262306a36Sopenharmony_ci 562362306a36Sopenharmony_ci if (!hub_dev) 562462306a36Sopenharmony_ci return; 562562306a36Sopenharmony_ci 562662306a36Sopenharmony_ci port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); 562762306a36Sopenharmony_ci if (!port_dev_path) 562862306a36Sopenharmony_ci return; 562962306a36Sopenharmony_ci 563062306a36Sopenharmony_ci envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path); 563162306a36Sopenharmony_ci if (!envp[0]) 563262306a36Sopenharmony_ci goto exit; 563362306a36Sopenharmony_ci 563462306a36Sopenharmony_ci envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u", 563562306a36Sopenharmony_ci port_dev->over_current_count); 563662306a36Sopenharmony_ci if (!envp[1]) 563762306a36Sopenharmony_ci goto exit; 563862306a36Sopenharmony_ci 563962306a36Sopenharmony_ci kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); 564062306a36Sopenharmony_ci 564162306a36Sopenharmony_ciexit: 564262306a36Sopenharmony_ci kfree(envp[1]); 564362306a36Sopenharmony_ci kfree(envp[0]); 564462306a36Sopenharmony_ci kfree(port_dev_path); 564562306a36Sopenharmony_ci} 564662306a36Sopenharmony_ci 564762306a36Sopenharmony_cistatic void port_event(struct usb_hub *hub, int port1) 564862306a36Sopenharmony_ci __must_hold(&port_dev->status_lock) 564962306a36Sopenharmony_ci{ 565062306a36Sopenharmony_ci int connect_change; 565162306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[port1 - 1]; 565262306a36Sopenharmony_ci struct usb_device *udev = port_dev->child; 565362306a36Sopenharmony_ci struct usb_device *hdev = hub->hdev; 565462306a36Sopenharmony_ci u16 portstatus, portchange; 565562306a36Sopenharmony_ci int i = 0; 565662306a36Sopenharmony_ci 565762306a36Sopenharmony_ci connect_change = test_bit(port1, hub->change_bits); 565862306a36Sopenharmony_ci clear_bit(port1, hub->event_bits); 565962306a36Sopenharmony_ci clear_bit(port1, hub->wakeup_bits); 566062306a36Sopenharmony_ci 566162306a36Sopenharmony_ci if (usb_hub_port_status(hub, port1, &portstatus, &portchange) < 0) 566262306a36Sopenharmony_ci return; 566362306a36Sopenharmony_ci 566462306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_CONNECTION) { 566562306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 566662306a36Sopenharmony_ci connect_change = 1; 566762306a36Sopenharmony_ci } 566862306a36Sopenharmony_ci 566962306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_ENABLE) { 567062306a36Sopenharmony_ci if (!connect_change) 567162306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "enable change, status %08x\n", 567262306a36Sopenharmony_ci portstatus); 567362306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 567462306a36Sopenharmony_ci 567562306a36Sopenharmony_ci /* 567662306a36Sopenharmony_ci * EM interference sometimes causes badly shielded USB devices 567762306a36Sopenharmony_ci * to be shutdown by the hub, this hack enables them again. 567862306a36Sopenharmony_ci * Works at least with mouse driver. 567962306a36Sopenharmony_ci */ 568062306a36Sopenharmony_ci if (!(portstatus & USB_PORT_STAT_ENABLE) 568162306a36Sopenharmony_ci && !connect_change && udev) { 568262306a36Sopenharmony_ci dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); 568362306a36Sopenharmony_ci connect_change = 1; 568462306a36Sopenharmony_ci } 568562306a36Sopenharmony_ci } 568662306a36Sopenharmony_ci 568762306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 568862306a36Sopenharmony_ci u16 status = 0, unused; 568962306a36Sopenharmony_ci port_dev->over_current_count++; 569062306a36Sopenharmony_ci port_over_current_notify(port_dev); 569162306a36Sopenharmony_ci 569262306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "over-current change #%u\n", 569362306a36Sopenharmony_ci port_dev->over_current_count); 569462306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, 569562306a36Sopenharmony_ci USB_PORT_FEAT_C_OVER_CURRENT); 569662306a36Sopenharmony_ci msleep(100); /* Cool down */ 569762306a36Sopenharmony_ci hub_power_on(hub, true); 569862306a36Sopenharmony_ci usb_hub_port_status(hub, port1, &status, &unused); 569962306a36Sopenharmony_ci if (status & USB_PORT_STAT_OVERCURRENT) 570062306a36Sopenharmony_ci dev_err(&port_dev->dev, "over-current condition\n"); 570162306a36Sopenharmony_ci } 570262306a36Sopenharmony_ci 570362306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_RESET) { 570462306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "reset change\n"); 570562306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET); 570662306a36Sopenharmony_ci } 570762306a36Sopenharmony_ci if ((portchange & USB_PORT_STAT_C_BH_RESET) 570862306a36Sopenharmony_ci && hub_is_superspeed(hdev)) { 570962306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "warm reset change\n"); 571062306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, 571162306a36Sopenharmony_ci USB_PORT_FEAT_C_BH_PORT_RESET); 571262306a36Sopenharmony_ci } 571362306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_LINK_STATE) { 571462306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "link state change\n"); 571562306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, 571662306a36Sopenharmony_ci USB_PORT_FEAT_C_PORT_LINK_STATE); 571762306a36Sopenharmony_ci } 571862306a36Sopenharmony_ci if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 571962306a36Sopenharmony_ci dev_warn(&port_dev->dev, "config error\n"); 572062306a36Sopenharmony_ci usb_clear_port_feature(hdev, port1, 572162306a36Sopenharmony_ci USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 572262306a36Sopenharmony_ci } 572362306a36Sopenharmony_ci 572462306a36Sopenharmony_ci /* skip port actions that require the port to be powered on */ 572562306a36Sopenharmony_ci if (!pm_runtime_active(&port_dev->dev)) 572662306a36Sopenharmony_ci return; 572762306a36Sopenharmony_ci 572862306a36Sopenharmony_ci /* skip port actions if ignore_event and early_stop are true */ 572962306a36Sopenharmony_ci if (port_dev->ignore_event && port_dev->early_stop) 573062306a36Sopenharmony_ci return; 573162306a36Sopenharmony_ci 573262306a36Sopenharmony_ci if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) 573362306a36Sopenharmony_ci connect_change = 1; 573462306a36Sopenharmony_ci 573562306a36Sopenharmony_ci /* 573662306a36Sopenharmony_ci * Avoid trying to recover a USB3 SS.Inactive port with a warm reset if 573762306a36Sopenharmony_ci * the device was disconnected. A 12ms disconnect detect timer in 573862306a36Sopenharmony_ci * SS.Inactive state transitions the port to RxDetect automatically. 573962306a36Sopenharmony_ci * SS.Inactive link error state is common during device disconnect. 574062306a36Sopenharmony_ci */ 574162306a36Sopenharmony_ci while (hub_port_warm_reset_required(hub, port1, portstatus)) { 574262306a36Sopenharmony_ci if ((i++ < DETECT_DISCONNECT_TRIES) && udev) { 574362306a36Sopenharmony_ci u16 unused; 574462306a36Sopenharmony_ci 574562306a36Sopenharmony_ci msleep(20); 574662306a36Sopenharmony_ci usb_hub_port_status(hub, port1, &portstatus, &unused); 574762306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "Wait for inactive link disconnect detect\n"); 574862306a36Sopenharmony_ci continue; 574962306a36Sopenharmony_ci } else if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION) 575062306a36Sopenharmony_ci || udev->state == USB_STATE_NOTATTACHED) { 575162306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "do warm reset, port only\n"); 575262306a36Sopenharmony_ci if (hub_port_reset(hub, port1, NULL, 575362306a36Sopenharmony_ci HUB_BH_RESET_TIME, true) < 0) 575462306a36Sopenharmony_ci hub_port_disable(hub, port1, 1); 575562306a36Sopenharmony_ci } else { 575662306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "do warm reset, full device\n"); 575762306a36Sopenharmony_ci usb_unlock_port(port_dev); 575862306a36Sopenharmony_ci usb_lock_device(udev); 575962306a36Sopenharmony_ci usb_reset_device(udev); 576062306a36Sopenharmony_ci usb_unlock_device(udev); 576162306a36Sopenharmony_ci usb_lock_port(port_dev); 576262306a36Sopenharmony_ci connect_change = 0; 576362306a36Sopenharmony_ci } 576462306a36Sopenharmony_ci break; 576562306a36Sopenharmony_ci } 576662306a36Sopenharmony_ci 576762306a36Sopenharmony_ci if (connect_change) 576862306a36Sopenharmony_ci hub_port_connect_change(hub, port1, portstatus, portchange); 576962306a36Sopenharmony_ci} 577062306a36Sopenharmony_ci 577162306a36Sopenharmony_cistatic void hub_event(struct work_struct *work) 577262306a36Sopenharmony_ci{ 577362306a36Sopenharmony_ci struct usb_device *hdev; 577462306a36Sopenharmony_ci struct usb_interface *intf; 577562306a36Sopenharmony_ci struct usb_hub *hub; 577662306a36Sopenharmony_ci struct device *hub_dev; 577762306a36Sopenharmony_ci u16 hubstatus; 577862306a36Sopenharmony_ci u16 hubchange; 577962306a36Sopenharmony_ci int i, ret; 578062306a36Sopenharmony_ci 578162306a36Sopenharmony_ci hub = container_of(work, struct usb_hub, events); 578262306a36Sopenharmony_ci hdev = hub->hdev; 578362306a36Sopenharmony_ci hub_dev = hub->intfdev; 578462306a36Sopenharmony_ci intf = to_usb_interface(hub_dev); 578562306a36Sopenharmony_ci 578662306a36Sopenharmony_ci kcov_remote_start_usb((u64)hdev->bus->busnum); 578762306a36Sopenharmony_ci 578862306a36Sopenharmony_ci dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 578962306a36Sopenharmony_ci hdev->state, hdev->maxchild, 579062306a36Sopenharmony_ci /* NOTE: expects max 15 ports... */ 579162306a36Sopenharmony_ci (u16) hub->change_bits[0], 579262306a36Sopenharmony_ci (u16) hub->event_bits[0]); 579362306a36Sopenharmony_ci 579462306a36Sopenharmony_ci /* Lock the device, then check to see if we were 579562306a36Sopenharmony_ci * disconnected while waiting for the lock to succeed. */ 579662306a36Sopenharmony_ci usb_lock_device(hdev); 579762306a36Sopenharmony_ci if (unlikely(hub->disconnected)) 579862306a36Sopenharmony_ci goto out_hdev_lock; 579962306a36Sopenharmony_ci 580062306a36Sopenharmony_ci /* If the hub has died, clean up after it */ 580162306a36Sopenharmony_ci if (hdev->state == USB_STATE_NOTATTACHED) { 580262306a36Sopenharmony_ci hub->error = -ENODEV; 580362306a36Sopenharmony_ci hub_quiesce(hub, HUB_DISCONNECT); 580462306a36Sopenharmony_ci goto out_hdev_lock; 580562306a36Sopenharmony_ci } 580662306a36Sopenharmony_ci 580762306a36Sopenharmony_ci /* Autoresume */ 580862306a36Sopenharmony_ci ret = usb_autopm_get_interface(intf); 580962306a36Sopenharmony_ci if (ret) { 581062306a36Sopenharmony_ci dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 581162306a36Sopenharmony_ci goto out_hdev_lock; 581262306a36Sopenharmony_ci } 581362306a36Sopenharmony_ci 581462306a36Sopenharmony_ci /* If this is an inactive hub, do nothing */ 581562306a36Sopenharmony_ci if (hub->quiescing) 581662306a36Sopenharmony_ci goto out_autopm; 581762306a36Sopenharmony_ci 581862306a36Sopenharmony_ci if (hub->error) { 581962306a36Sopenharmony_ci dev_dbg(hub_dev, "resetting for error %d\n", hub->error); 582062306a36Sopenharmony_ci 582162306a36Sopenharmony_ci ret = usb_reset_device(hdev); 582262306a36Sopenharmony_ci if (ret) { 582362306a36Sopenharmony_ci dev_dbg(hub_dev, "error resetting hub: %d\n", ret); 582462306a36Sopenharmony_ci goto out_autopm; 582562306a36Sopenharmony_ci } 582662306a36Sopenharmony_ci 582762306a36Sopenharmony_ci hub->nerrors = 0; 582862306a36Sopenharmony_ci hub->error = 0; 582962306a36Sopenharmony_ci } 583062306a36Sopenharmony_ci 583162306a36Sopenharmony_ci /* deal with port status changes */ 583262306a36Sopenharmony_ci for (i = 1; i <= hdev->maxchild; i++) { 583362306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[i - 1]; 583462306a36Sopenharmony_ci 583562306a36Sopenharmony_ci if (test_bit(i, hub->event_bits) 583662306a36Sopenharmony_ci || test_bit(i, hub->change_bits) 583762306a36Sopenharmony_ci || test_bit(i, hub->wakeup_bits)) { 583862306a36Sopenharmony_ci /* 583962306a36Sopenharmony_ci * The get_noresume and barrier ensure that if 584062306a36Sopenharmony_ci * the port was in the process of resuming, we 584162306a36Sopenharmony_ci * flush that work and keep the port active for 584262306a36Sopenharmony_ci * the duration of the port_event(). However, 584362306a36Sopenharmony_ci * if the port is runtime pm suspended 584462306a36Sopenharmony_ci * (powered-off), we leave it in that state, run 584562306a36Sopenharmony_ci * an abbreviated port_event(), and move on. 584662306a36Sopenharmony_ci */ 584762306a36Sopenharmony_ci pm_runtime_get_noresume(&port_dev->dev); 584862306a36Sopenharmony_ci pm_runtime_barrier(&port_dev->dev); 584962306a36Sopenharmony_ci usb_lock_port(port_dev); 585062306a36Sopenharmony_ci port_event(hub, i); 585162306a36Sopenharmony_ci usb_unlock_port(port_dev); 585262306a36Sopenharmony_ci pm_runtime_put_sync(&port_dev->dev); 585362306a36Sopenharmony_ci } 585462306a36Sopenharmony_ci } 585562306a36Sopenharmony_ci 585662306a36Sopenharmony_ci /* deal with hub status changes */ 585762306a36Sopenharmony_ci if (test_and_clear_bit(0, hub->event_bits) == 0) 585862306a36Sopenharmony_ci ; /* do nothing */ 585962306a36Sopenharmony_ci else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 586062306a36Sopenharmony_ci dev_err(hub_dev, "get_hub_status failed\n"); 586162306a36Sopenharmony_ci else { 586262306a36Sopenharmony_ci if (hubchange & HUB_CHANGE_LOCAL_POWER) { 586362306a36Sopenharmony_ci dev_dbg(hub_dev, "power change\n"); 586462306a36Sopenharmony_ci clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 586562306a36Sopenharmony_ci if (hubstatus & HUB_STATUS_LOCAL_POWER) 586662306a36Sopenharmony_ci /* FIXME: Is this always true? */ 586762306a36Sopenharmony_ci hub->limited_power = 1; 586862306a36Sopenharmony_ci else 586962306a36Sopenharmony_ci hub->limited_power = 0; 587062306a36Sopenharmony_ci } 587162306a36Sopenharmony_ci if (hubchange & HUB_CHANGE_OVERCURRENT) { 587262306a36Sopenharmony_ci u16 status = 0; 587362306a36Sopenharmony_ci u16 unused; 587462306a36Sopenharmony_ci 587562306a36Sopenharmony_ci dev_dbg(hub_dev, "over-current change\n"); 587662306a36Sopenharmony_ci clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 587762306a36Sopenharmony_ci msleep(500); /* Cool down */ 587862306a36Sopenharmony_ci hub_power_on(hub, true); 587962306a36Sopenharmony_ci hub_hub_status(hub, &status, &unused); 588062306a36Sopenharmony_ci if (status & HUB_STATUS_OVERCURRENT) 588162306a36Sopenharmony_ci dev_err(hub_dev, "over-current condition\n"); 588262306a36Sopenharmony_ci } 588362306a36Sopenharmony_ci } 588462306a36Sopenharmony_ci 588562306a36Sopenharmony_ciout_autopm: 588662306a36Sopenharmony_ci /* Balance the usb_autopm_get_interface() above */ 588762306a36Sopenharmony_ci usb_autopm_put_interface_no_suspend(intf); 588862306a36Sopenharmony_ciout_hdev_lock: 588962306a36Sopenharmony_ci usb_unlock_device(hdev); 589062306a36Sopenharmony_ci 589162306a36Sopenharmony_ci /* Balance the stuff in kick_hub_wq() and allow autosuspend */ 589262306a36Sopenharmony_ci usb_autopm_put_interface(intf); 589362306a36Sopenharmony_ci kref_put(&hub->kref, hub_release); 589462306a36Sopenharmony_ci 589562306a36Sopenharmony_ci kcov_remote_stop(); 589662306a36Sopenharmony_ci} 589762306a36Sopenharmony_ci 589862306a36Sopenharmony_cistatic const struct usb_device_id hub_id_table[] = { 589962306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 590062306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT 590162306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_INT_CLASS, 590262306a36Sopenharmony_ci .idVendor = USB_VENDOR_SMSC, 590362306a36Sopenharmony_ci .idProduct = USB_PRODUCT_USB5534B, 590462306a36Sopenharmony_ci .bInterfaceClass = USB_CLASS_HUB, 590562306a36Sopenharmony_ci .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 590662306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 590762306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT, 590862306a36Sopenharmony_ci .idVendor = USB_VENDOR_CYPRESS, 590962306a36Sopenharmony_ci .idProduct = USB_PRODUCT_CY7C65632, 591062306a36Sopenharmony_ci .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 591162306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 591262306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_INT_CLASS, 591362306a36Sopenharmony_ci .idVendor = USB_VENDOR_GENESYS_LOGIC, 591462306a36Sopenharmony_ci .bInterfaceClass = USB_CLASS_HUB, 591562306a36Sopenharmony_ci .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, 591662306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 591762306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT, 591862306a36Sopenharmony_ci .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS, 591962306a36Sopenharmony_ci .idProduct = USB_PRODUCT_TUSB8041_USB2, 592062306a36Sopenharmony_ci .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 592162306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 592262306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT, 592362306a36Sopenharmony_ci .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS, 592462306a36Sopenharmony_ci .idProduct = USB_PRODUCT_TUSB8041_USB3, 592562306a36Sopenharmony_ci .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 592662306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 592762306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT, 592862306a36Sopenharmony_ci .idVendor = USB_VENDOR_MICROCHIP, 592962306a36Sopenharmony_ci .idProduct = USB_PRODUCT_USB4913, 593062306a36Sopenharmony_ci .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 593162306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 593262306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT, 593362306a36Sopenharmony_ci .idVendor = USB_VENDOR_MICROCHIP, 593462306a36Sopenharmony_ci .idProduct = USB_PRODUCT_USB4914, 593562306a36Sopenharmony_ci .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 593662306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 593762306a36Sopenharmony_ci | USB_DEVICE_ID_MATCH_PRODUCT, 593862306a36Sopenharmony_ci .idVendor = USB_VENDOR_MICROCHIP, 593962306a36Sopenharmony_ci .idProduct = USB_PRODUCT_USB4915, 594062306a36Sopenharmony_ci .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 594162306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 594262306a36Sopenharmony_ci .bDeviceClass = USB_CLASS_HUB}, 594362306a36Sopenharmony_ci { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 594462306a36Sopenharmony_ci .bInterfaceClass = USB_CLASS_HUB}, 594562306a36Sopenharmony_ci { } /* Terminating entry */ 594662306a36Sopenharmony_ci}; 594762306a36Sopenharmony_ci 594862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(usb, hub_id_table); 594962306a36Sopenharmony_ci 595062306a36Sopenharmony_cistatic struct usb_driver hub_driver = { 595162306a36Sopenharmony_ci .name = "hub", 595262306a36Sopenharmony_ci .probe = hub_probe, 595362306a36Sopenharmony_ci .disconnect = hub_disconnect, 595462306a36Sopenharmony_ci .suspend = hub_suspend, 595562306a36Sopenharmony_ci .resume = hub_resume, 595662306a36Sopenharmony_ci .reset_resume = hub_reset_resume, 595762306a36Sopenharmony_ci .pre_reset = hub_pre_reset, 595862306a36Sopenharmony_ci .post_reset = hub_post_reset, 595962306a36Sopenharmony_ci .unlocked_ioctl = hub_ioctl, 596062306a36Sopenharmony_ci .id_table = hub_id_table, 596162306a36Sopenharmony_ci .supports_autosuspend = 1, 596262306a36Sopenharmony_ci}; 596362306a36Sopenharmony_ci 596462306a36Sopenharmony_ciint usb_hub_init(void) 596562306a36Sopenharmony_ci{ 596662306a36Sopenharmony_ci if (usb_register(&hub_driver) < 0) { 596762306a36Sopenharmony_ci printk(KERN_ERR "%s: can't register hub driver\n", 596862306a36Sopenharmony_ci usbcore_name); 596962306a36Sopenharmony_ci return -1; 597062306a36Sopenharmony_ci } 597162306a36Sopenharmony_ci 597262306a36Sopenharmony_ci /* 597362306a36Sopenharmony_ci * The workqueue needs to be freezable to avoid interfering with 597462306a36Sopenharmony_ci * USB-PERSIST port handover. Otherwise it might see that a full-speed 597562306a36Sopenharmony_ci * device was gone before the EHCI controller had handed its port 597662306a36Sopenharmony_ci * over to the companion full-speed controller. 597762306a36Sopenharmony_ci */ 597862306a36Sopenharmony_ci hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0); 597962306a36Sopenharmony_ci if (hub_wq) 598062306a36Sopenharmony_ci return 0; 598162306a36Sopenharmony_ci 598262306a36Sopenharmony_ci /* Fall through if kernel_thread failed */ 598362306a36Sopenharmony_ci usb_deregister(&hub_driver); 598462306a36Sopenharmony_ci pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); 598562306a36Sopenharmony_ci 598662306a36Sopenharmony_ci return -1; 598762306a36Sopenharmony_ci} 598862306a36Sopenharmony_ci 598962306a36Sopenharmony_civoid usb_hub_cleanup(void) 599062306a36Sopenharmony_ci{ 599162306a36Sopenharmony_ci destroy_workqueue(hub_wq); 599262306a36Sopenharmony_ci 599362306a36Sopenharmony_ci /* 599462306a36Sopenharmony_ci * Hub resources are freed for us by usb_deregister. It calls 599562306a36Sopenharmony_ci * usb_driver_purge on every device which in turn calls that 599662306a36Sopenharmony_ci * devices disconnect function if it is using this driver. 599762306a36Sopenharmony_ci * The hub_disconnect function takes care of releasing the 599862306a36Sopenharmony_ci * individual hub resources. -greg 599962306a36Sopenharmony_ci */ 600062306a36Sopenharmony_ci usb_deregister(&hub_driver); 600162306a36Sopenharmony_ci} /* usb_hub_cleanup() */ 600262306a36Sopenharmony_ci 600362306a36Sopenharmony_ci/** 600462306a36Sopenharmony_ci * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 600562306a36Sopenharmony_ci * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 600662306a36Sopenharmony_ci * 600762306a36Sopenharmony_ci * WARNING - don't use this routine to reset a composite device 600862306a36Sopenharmony_ci * (one with multiple interfaces owned by separate drivers)! 600962306a36Sopenharmony_ci * Use usb_reset_device() instead. 601062306a36Sopenharmony_ci * 601162306a36Sopenharmony_ci * Do a port reset, reassign the device's address, and establish its 601262306a36Sopenharmony_ci * former operating configuration. If the reset fails, or the device's 601362306a36Sopenharmony_ci * descriptors change from their values before the reset, or the original 601462306a36Sopenharmony_ci * configuration and altsettings cannot be restored, a flag will be set 601562306a36Sopenharmony_ci * telling hub_wq to pretend the device has been disconnected and then 601662306a36Sopenharmony_ci * re-connected. All drivers will be unbound, and the device will be 601762306a36Sopenharmony_ci * re-enumerated and probed all over again. 601862306a36Sopenharmony_ci * 601962306a36Sopenharmony_ci * Return: 0 if the reset succeeded, -ENODEV if the device has been 602062306a36Sopenharmony_ci * flagged for logical disconnection, or some other negative error code 602162306a36Sopenharmony_ci * if the reset wasn't even attempted. 602262306a36Sopenharmony_ci * 602362306a36Sopenharmony_ci * Note: 602462306a36Sopenharmony_ci * The caller must own the device lock and the port lock, the latter is 602562306a36Sopenharmony_ci * taken by usb_reset_device(). For example, it's safe to use 602662306a36Sopenharmony_ci * usb_reset_device() from a driver probe() routine after downloading 602762306a36Sopenharmony_ci * new firmware. For calls that might not occur during probe(), drivers 602862306a36Sopenharmony_ci * should lock the device using usb_lock_device_for_reset(). 602962306a36Sopenharmony_ci * 603062306a36Sopenharmony_ci * Locking exception: This routine may also be called from within an 603162306a36Sopenharmony_ci * autoresume handler. Such usage won't conflict with other tasks 603262306a36Sopenharmony_ci * holding the device lock because these tasks should always call 603362306a36Sopenharmony_ci * usb_autopm_resume_device(), thereby preventing any unwanted 603462306a36Sopenharmony_ci * autoresume. The autoresume handler is expected to have already 603562306a36Sopenharmony_ci * acquired the port lock before calling this routine. 603662306a36Sopenharmony_ci */ 603762306a36Sopenharmony_cistatic int usb_reset_and_verify_device(struct usb_device *udev) 603862306a36Sopenharmony_ci{ 603962306a36Sopenharmony_ci struct usb_device *parent_hdev = udev->parent; 604062306a36Sopenharmony_ci struct usb_hub *parent_hub; 604162306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 604262306a36Sopenharmony_ci struct usb_device_descriptor descriptor; 604362306a36Sopenharmony_ci struct usb_host_bos *bos; 604462306a36Sopenharmony_ci int i, j, ret = 0; 604562306a36Sopenharmony_ci int port1 = udev->portnum; 604662306a36Sopenharmony_ci 604762306a36Sopenharmony_ci if (udev->state == USB_STATE_NOTATTACHED || 604862306a36Sopenharmony_ci udev->state == USB_STATE_SUSPENDED) { 604962306a36Sopenharmony_ci dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 605062306a36Sopenharmony_ci udev->state); 605162306a36Sopenharmony_ci return -EINVAL; 605262306a36Sopenharmony_ci } 605362306a36Sopenharmony_ci 605462306a36Sopenharmony_ci if (!parent_hdev) 605562306a36Sopenharmony_ci return -EISDIR; 605662306a36Sopenharmony_ci 605762306a36Sopenharmony_ci parent_hub = usb_hub_to_struct_hub(parent_hdev); 605862306a36Sopenharmony_ci 605962306a36Sopenharmony_ci /* Disable USB2 hardware LPM. 606062306a36Sopenharmony_ci * It will be re-enabled by the enumeration process. 606162306a36Sopenharmony_ci */ 606262306a36Sopenharmony_ci usb_disable_usb2_hardware_lpm(udev); 606362306a36Sopenharmony_ci 606462306a36Sopenharmony_ci bos = udev->bos; 606562306a36Sopenharmony_ci udev->bos = NULL; 606662306a36Sopenharmony_ci 606762306a36Sopenharmony_ci mutex_lock(hcd->address0_mutex); 606862306a36Sopenharmony_ci 606962306a36Sopenharmony_ci for (i = 0; i < PORT_INIT_TRIES; ++i) { 607062306a36Sopenharmony_ci if (hub_port_stop_enumerate(parent_hub, port1, i)) { 607162306a36Sopenharmony_ci ret = -ENODEV; 607262306a36Sopenharmony_ci break; 607362306a36Sopenharmony_ci } 607462306a36Sopenharmony_ci 607562306a36Sopenharmony_ci /* ep0 maxpacket size may change; let the HCD know about it. 607662306a36Sopenharmony_ci * Other endpoints will be handled by re-enumeration. */ 607762306a36Sopenharmony_ci usb_ep0_reinit(udev); 607862306a36Sopenharmony_ci ret = hub_port_init(parent_hub, udev, port1, i, &descriptor); 607962306a36Sopenharmony_ci if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 608062306a36Sopenharmony_ci break; 608162306a36Sopenharmony_ci } 608262306a36Sopenharmony_ci mutex_unlock(hcd->address0_mutex); 608362306a36Sopenharmony_ci 608462306a36Sopenharmony_ci if (ret < 0) 608562306a36Sopenharmony_ci goto re_enumerate; 608662306a36Sopenharmony_ci 608762306a36Sopenharmony_ci /* Device might have changed firmware (DFU or similar) */ 608862306a36Sopenharmony_ci if (descriptors_changed(udev, &descriptor, bos)) { 608962306a36Sopenharmony_ci dev_info(&udev->dev, "device firmware changed\n"); 609062306a36Sopenharmony_ci goto re_enumerate; 609162306a36Sopenharmony_ci } 609262306a36Sopenharmony_ci 609362306a36Sopenharmony_ci /* Restore the device's previous configuration */ 609462306a36Sopenharmony_ci if (!udev->actconfig) 609562306a36Sopenharmony_ci goto done; 609662306a36Sopenharmony_ci 609762306a36Sopenharmony_ci mutex_lock(hcd->bandwidth_mutex); 609862306a36Sopenharmony_ci ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 609962306a36Sopenharmony_ci if (ret < 0) { 610062306a36Sopenharmony_ci dev_warn(&udev->dev, 610162306a36Sopenharmony_ci "Busted HC? Not enough HCD resources for " 610262306a36Sopenharmony_ci "old configuration.\n"); 610362306a36Sopenharmony_ci mutex_unlock(hcd->bandwidth_mutex); 610462306a36Sopenharmony_ci goto re_enumerate; 610562306a36Sopenharmony_ci } 610662306a36Sopenharmony_ci ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 610762306a36Sopenharmony_ci USB_REQ_SET_CONFIGURATION, 0, 610862306a36Sopenharmony_ci udev->actconfig->desc.bConfigurationValue, 0, 610962306a36Sopenharmony_ci NULL, 0, USB_CTRL_SET_TIMEOUT); 611062306a36Sopenharmony_ci if (ret < 0) { 611162306a36Sopenharmony_ci dev_err(&udev->dev, 611262306a36Sopenharmony_ci "can't restore configuration #%d (error=%d)\n", 611362306a36Sopenharmony_ci udev->actconfig->desc.bConfigurationValue, ret); 611462306a36Sopenharmony_ci mutex_unlock(hcd->bandwidth_mutex); 611562306a36Sopenharmony_ci goto re_enumerate; 611662306a36Sopenharmony_ci } 611762306a36Sopenharmony_ci mutex_unlock(hcd->bandwidth_mutex); 611862306a36Sopenharmony_ci usb_set_device_state(udev, USB_STATE_CONFIGURED); 611962306a36Sopenharmony_ci 612062306a36Sopenharmony_ci /* Put interfaces back into the same altsettings as before. 612162306a36Sopenharmony_ci * Don't bother to send the Set-Interface request for interfaces 612262306a36Sopenharmony_ci * that were already in altsetting 0; besides being unnecessary, 612362306a36Sopenharmony_ci * many devices can't handle it. Instead just reset the host-side 612462306a36Sopenharmony_ci * endpoint state. 612562306a36Sopenharmony_ci */ 612662306a36Sopenharmony_ci for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 612762306a36Sopenharmony_ci struct usb_host_config *config = udev->actconfig; 612862306a36Sopenharmony_ci struct usb_interface *intf = config->interface[i]; 612962306a36Sopenharmony_ci struct usb_interface_descriptor *desc; 613062306a36Sopenharmony_ci 613162306a36Sopenharmony_ci desc = &intf->cur_altsetting->desc; 613262306a36Sopenharmony_ci if (desc->bAlternateSetting == 0) { 613362306a36Sopenharmony_ci usb_disable_interface(udev, intf, true); 613462306a36Sopenharmony_ci usb_enable_interface(udev, intf, true); 613562306a36Sopenharmony_ci ret = 0; 613662306a36Sopenharmony_ci } else { 613762306a36Sopenharmony_ci /* Let the bandwidth allocation function know that this 613862306a36Sopenharmony_ci * device has been reset, and it will have to use 613962306a36Sopenharmony_ci * alternate setting 0 as the current alternate setting. 614062306a36Sopenharmony_ci */ 614162306a36Sopenharmony_ci intf->resetting_device = 1; 614262306a36Sopenharmony_ci ret = usb_set_interface(udev, desc->bInterfaceNumber, 614362306a36Sopenharmony_ci desc->bAlternateSetting); 614462306a36Sopenharmony_ci intf->resetting_device = 0; 614562306a36Sopenharmony_ci } 614662306a36Sopenharmony_ci if (ret < 0) { 614762306a36Sopenharmony_ci dev_err(&udev->dev, "failed to restore interface %d " 614862306a36Sopenharmony_ci "altsetting %d (error=%d)\n", 614962306a36Sopenharmony_ci desc->bInterfaceNumber, 615062306a36Sopenharmony_ci desc->bAlternateSetting, 615162306a36Sopenharmony_ci ret); 615262306a36Sopenharmony_ci goto re_enumerate; 615362306a36Sopenharmony_ci } 615462306a36Sopenharmony_ci /* Resetting also frees any allocated streams */ 615562306a36Sopenharmony_ci for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) 615662306a36Sopenharmony_ci intf->cur_altsetting->endpoint[j].streams = 0; 615762306a36Sopenharmony_ci } 615862306a36Sopenharmony_ci 615962306a36Sopenharmony_cidone: 616062306a36Sopenharmony_ci /* Now that the alt settings are re-installed, enable LTM and LPM. */ 616162306a36Sopenharmony_ci usb_enable_usb2_hardware_lpm(udev); 616262306a36Sopenharmony_ci usb_unlocked_enable_lpm(udev); 616362306a36Sopenharmony_ci usb_enable_ltm(udev); 616462306a36Sopenharmony_ci usb_release_bos_descriptor(udev); 616562306a36Sopenharmony_ci udev->bos = bos; 616662306a36Sopenharmony_ci return 0; 616762306a36Sopenharmony_ci 616862306a36Sopenharmony_cire_enumerate: 616962306a36Sopenharmony_ci usb_release_bos_descriptor(udev); 617062306a36Sopenharmony_ci udev->bos = bos; 617162306a36Sopenharmony_ci hub_port_logical_disconnect(parent_hub, port1); 617262306a36Sopenharmony_ci return -ENODEV; 617362306a36Sopenharmony_ci} 617462306a36Sopenharmony_ci 617562306a36Sopenharmony_ci/** 617662306a36Sopenharmony_ci * usb_reset_device - warn interface drivers and perform a USB port reset 617762306a36Sopenharmony_ci * @udev: device to reset (not in NOTATTACHED state) 617862306a36Sopenharmony_ci * 617962306a36Sopenharmony_ci * Warns all drivers bound to registered interfaces (using their pre_reset 618062306a36Sopenharmony_ci * method), performs the port reset, and then lets the drivers know that 618162306a36Sopenharmony_ci * the reset is over (using their post_reset method). 618262306a36Sopenharmony_ci * 618362306a36Sopenharmony_ci * Return: The same as for usb_reset_and_verify_device(). 618462306a36Sopenharmony_ci * However, if a reset is already in progress (for instance, if a 618562306a36Sopenharmony_ci * driver doesn't have pre_reset() or post_reset() callbacks, and while 618662306a36Sopenharmony_ci * being unbound or re-bound during the ongoing reset its disconnect() 618762306a36Sopenharmony_ci * or probe() routine tries to perform a second, nested reset), the 618862306a36Sopenharmony_ci * routine returns -EINPROGRESS. 618962306a36Sopenharmony_ci * 619062306a36Sopenharmony_ci * Note: 619162306a36Sopenharmony_ci * The caller must own the device lock. For example, it's safe to use 619262306a36Sopenharmony_ci * this from a driver probe() routine after downloading new firmware. 619362306a36Sopenharmony_ci * For calls that might not occur during probe(), drivers should lock 619462306a36Sopenharmony_ci * the device using usb_lock_device_for_reset(). 619562306a36Sopenharmony_ci * 619662306a36Sopenharmony_ci * If an interface is currently being probed or disconnected, we assume 619762306a36Sopenharmony_ci * its driver knows how to handle resets. For all other interfaces, 619862306a36Sopenharmony_ci * if the driver doesn't have pre_reset and post_reset methods then 619962306a36Sopenharmony_ci * we attempt to unbind it and rebind afterward. 620062306a36Sopenharmony_ci */ 620162306a36Sopenharmony_ciint usb_reset_device(struct usb_device *udev) 620262306a36Sopenharmony_ci{ 620362306a36Sopenharmony_ci int ret; 620462306a36Sopenharmony_ci int i; 620562306a36Sopenharmony_ci unsigned int noio_flag; 620662306a36Sopenharmony_ci struct usb_port *port_dev; 620762306a36Sopenharmony_ci struct usb_host_config *config = udev->actconfig; 620862306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 620962306a36Sopenharmony_ci 621062306a36Sopenharmony_ci if (udev->state == USB_STATE_NOTATTACHED) { 621162306a36Sopenharmony_ci dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 621262306a36Sopenharmony_ci udev->state); 621362306a36Sopenharmony_ci return -EINVAL; 621462306a36Sopenharmony_ci } 621562306a36Sopenharmony_ci 621662306a36Sopenharmony_ci if (!udev->parent) { 621762306a36Sopenharmony_ci /* this requires hcd-specific logic; see ohci_restart() */ 621862306a36Sopenharmony_ci dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 621962306a36Sopenharmony_ci return -EISDIR; 622062306a36Sopenharmony_ci } 622162306a36Sopenharmony_ci 622262306a36Sopenharmony_ci if (udev->reset_in_progress) 622362306a36Sopenharmony_ci return -EINPROGRESS; 622462306a36Sopenharmony_ci udev->reset_in_progress = 1; 622562306a36Sopenharmony_ci 622662306a36Sopenharmony_ci port_dev = hub->ports[udev->portnum - 1]; 622762306a36Sopenharmony_ci 622862306a36Sopenharmony_ci /* 622962306a36Sopenharmony_ci * Don't allocate memory with GFP_KERNEL in current 623062306a36Sopenharmony_ci * context to avoid possible deadlock if usb mass 623162306a36Sopenharmony_ci * storage interface or usbnet interface(iSCSI case) 623262306a36Sopenharmony_ci * is included in current configuration. The easist 623362306a36Sopenharmony_ci * approach is to do it for every device reset, 623462306a36Sopenharmony_ci * because the device 'memalloc_noio' flag may have 623562306a36Sopenharmony_ci * not been set before reseting the usb device. 623662306a36Sopenharmony_ci */ 623762306a36Sopenharmony_ci noio_flag = memalloc_noio_save(); 623862306a36Sopenharmony_ci 623962306a36Sopenharmony_ci /* Prevent autosuspend during the reset */ 624062306a36Sopenharmony_ci usb_autoresume_device(udev); 624162306a36Sopenharmony_ci 624262306a36Sopenharmony_ci if (config) { 624362306a36Sopenharmony_ci for (i = 0; i < config->desc.bNumInterfaces; ++i) { 624462306a36Sopenharmony_ci struct usb_interface *cintf = config->interface[i]; 624562306a36Sopenharmony_ci struct usb_driver *drv; 624662306a36Sopenharmony_ci int unbind = 0; 624762306a36Sopenharmony_ci 624862306a36Sopenharmony_ci if (cintf->dev.driver) { 624962306a36Sopenharmony_ci drv = to_usb_driver(cintf->dev.driver); 625062306a36Sopenharmony_ci if (drv->pre_reset && drv->post_reset) 625162306a36Sopenharmony_ci unbind = (drv->pre_reset)(cintf); 625262306a36Sopenharmony_ci else if (cintf->condition == 625362306a36Sopenharmony_ci USB_INTERFACE_BOUND) 625462306a36Sopenharmony_ci unbind = 1; 625562306a36Sopenharmony_ci if (unbind) 625662306a36Sopenharmony_ci usb_forced_unbind_intf(cintf); 625762306a36Sopenharmony_ci } 625862306a36Sopenharmony_ci } 625962306a36Sopenharmony_ci } 626062306a36Sopenharmony_ci 626162306a36Sopenharmony_ci usb_lock_port(port_dev); 626262306a36Sopenharmony_ci ret = usb_reset_and_verify_device(udev); 626362306a36Sopenharmony_ci usb_unlock_port(port_dev); 626462306a36Sopenharmony_ci 626562306a36Sopenharmony_ci if (config) { 626662306a36Sopenharmony_ci for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 626762306a36Sopenharmony_ci struct usb_interface *cintf = config->interface[i]; 626862306a36Sopenharmony_ci struct usb_driver *drv; 626962306a36Sopenharmony_ci int rebind = cintf->needs_binding; 627062306a36Sopenharmony_ci 627162306a36Sopenharmony_ci if (!rebind && cintf->dev.driver) { 627262306a36Sopenharmony_ci drv = to_usb_driver(cintf->dev.driver); 627362306a36Sopenharmony_ci if (drv->post_reset) 627462306a36Sopenharmony_ci rebind = (drv->post_reset)(cintf); 627562306a36Sopenharmony_ci else if (cintf->condition == 627662306a36Sopenharmony_ci USB_INTERFACE_BOUND) 627762306a36Sopenharmony_ci rebind = 1; 627862306a36Sopenharmony_ci if (rebind) 627962306a36Sopenharmony_ci cintf->needs_binding = 1; 628062306a36Sopenharmony_ci } 628162306a36Sopenharmony_ci } 628262306a36Sopenharmony_ci 628362306a36Sopenharmony_ci /* If the reset failed, hub_wq will unbind drivers later */ 628462306a36Sopenharmony_ci if (ret == 0) 628562306a36Sopenharmony_ci usb_unbind_and_rebind_marked_interfaces(udev); 628662306a36Sopenharmony_ci } 628762306a36Sopenharmony_ci 628862306a36Sopenharmony_ci usb_autosuspend_device(udev); 628962306a36Sopenharmony_ci memalloc_noio_restore(noio_flag); 629062306a36Sopenharmony_ci udev->reset_in_progress = 0; 629162306a36Sopenharmony_ci return ret; 629262306a36Sopenharmony_ci} 629362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_reset_device); 629462306a36Sopenharmony_ci 629562306a36Sopenharmony_ci 629662306a36Sopenharmony_ci/** 629762306a36Sopenharmony_ci * usb_queue_reset_device - Reset a USB device from an atomic context 629862306a36Sopenharmony_ci * @iface: USB interface belonging to the device to reset 629962306a36Sopenharmony_ci * 630062306a36Sopenharmony_ci * This function can be used to reset a USB device from an atomic 630162306a36Sopenharmony_ci * context, where usb_reset_device() won't work (as it blocks). 630262306a36Sopenharmony_ci * 630362306a36Sopenharmony_ci * Doing a reset via this method is functionally equivalent to calling 630462306a36Sopenharmony_ci * usb_reset_device(), except for the fact that it is delayed to a 630562306a36Sopenharmony_ci * workqueue. This means that any drivers bound to other interfaces 630662306a36Sopenharmony_ci * might be unbound, as well as users from usbfs in user space. 630762306a36Sopenharmony_ci * 630862306a36Sopenharmony_ci * Corner cases: 630962306a36Sopenharmony_ci * 631062306a36Sopenharmony_ci * - Scheduling two resets at the same time from two different drivers 631162306a36Sopenharmony_ci * attached to two different interfaces of the same device is 631262306a36Sopenharmony_ci * possible; depending on how the driver attached to each interface 631362306a36Sopenharmony_ci * handles ->pre_reset(), the second reset might happen or not. 631462306a36Sopenharmony_ci * 631562306a36Sopenharmony_ci * - If the reset is delayed so long that the interface is unbound from 631662306a36Sopenharmony_ci * its driver, the reset will be skipped. 631762306a36Sopenharmony_ci * 631862306a36Sopenharmony_ci * - This function can be called during .probe(). It can also be called 631962306a36Sopenharmony_ci * during .disconnect(), but doing so is pointless because the reset 632062306a36Sopenharmony_ci * will not occur. If you really want to reset the device during 632162306a36Sopenharmony_ci * .disconnect(), call usb_reset_device() directly -- but watch out 632262306a36Sopenharmony_ci * for nested unbinding issues! 632362306a36Sopenharmony_ci */ 632462306a36Sopenharmony_civoid usb_queue_reset_device(struct usb_interface *iface) 632562306a36Sopenharmony_ci{ 632662306a36Sopenharmony_ci if (schedule_work(&iface->reset_ws)) 632762306a36Sopenharmony_ci usb_get_intf(iface); 632862306a36Sopenharmony_ci} 632962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_queue_reset_device); 633062306a36Sopenharmony_ci 633162306a36Sopenharmony_ci/** 633262306a36Sopenharmony_ci * usb_hub_find_child - Get the pointer of child device 633362306a36Sopenharmony_ci * attached to the port which is specified by @port1. 633462306a36Sopenharmony_ci * @hdev: USB device belonging to the usb hub 633562306a36Sopenharmony_ci * @port1: port num to indicate which port the child device 633662306a36Sopenharmony_ci * is attached to. 633762306a36Sopenharmony_ci * 633862306a36Sopenharmony_ci * USB drivers call this function to get hub's child device 633962306a36Sopenharmony_ci * pointer. 634062306a36Sopenharmony_ci * 634162306a36Sopenharmony_ci * Return: %NULL if input param is invalid and 634262306a36Sopenharmony_ci * child's usb_device pointer if non-NULL. 634362306a36Sopenharmony_ci */ 634462306a36Sopenharmony_cistruct usb_device *usb_hub_find_child(struct usb_device *hdev, 634562306a36Sopenharmony_ci int port1) 634662306a36Sopenharmony_ci{ 634762306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 634862306a36Sopenharmony_ci 634962306a36Sopenharmony_ci if (port1 < 1 || port1 > hdev->maxchild) 635062306a36Sopenharmony_ci return NULL; 635162306a36Sopenharmony_ci return hub->ports[port1 - 1]->child; 635262306a36Sopenharmony_ci} 635362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hub_find_child); 635462306a36Sopenharmony_ci 635562306a36Sopenharmony_civoid usb_hub_adjust_deviceremovable(struct usb_device *hdev, 635662306a36Sopenharmony_ci struct usb_hub_descriptor *desc) 635762306a36Sopenharmony_ci{ 635862306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 635962306a36Sopenharmony_ci enum usb_port_connect_type connect_type; 636062306a36Sopenharmony_ci int i; 636162306a36Sopenharmony_ci 636262306a36Sopenharmony_ci if (!hub) 636362306a36Sopenharmony_ci return; 636462306a36Sopenharmony_ci 636562306a36Sopenharmony_ci if (!hub_is_superspeed(hdev)) { 636662306a36Sopenharmony_ci for (i = 1; i <= hdev->maxchild; i++) { 636762306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[i - 1]; 636862306a36Sopenharmony_ci 636962306a36Sopenharmony_ci connect_type = port_dev->connect_type; 637062306a36Sopenharmony_ci if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 637162306a36Sopenharmony_ci u8 mask = 1 << (i%8); 637262306a36Sopenharmony_ci 637362306a36Sopenharmony_ci if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { 637462306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 637562306a36Sopenharmony_ci desc->u.hs.DeviceRemovable[i/8] |= mask; 637662306a36Sopenharmony_ci } 637762306a36Sopenharmony_ci } 637862306a36Sopenharmony_ci } 637962306a36Sopenharmony_ci } else { 638062306a36Sopenharmony_ci u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); 638162306a36Sopenharmony_ci 638262306a36Sopenharmony_ci for (i = 1; i <= hdev->maxchild; i++) { 638362306a36Sopenharmony_ci struct usb_port *port_dev = hub->ports[i - 1]; 638462306a36Sopenharmony_ci 638562306a36Sopenharmony_ci connect_type = port_dev->connect_type; 638662306a36Sopenharmony_ci if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 638762306a36Sopenharmony_ci u16 mask = 1 << i; 638862306a36Sopenharmony_ci 638962306a36Sopenharmony_ci if (!(port_removable & mask)) { 639062306a36Sopenharmony_ci dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 639162306a36Sopenharmony_ci port_removable |= mask; 639262306a36Sopenharmony_ci } 639362306a36Sopenharmony_ci } 639462306a36Sopenharmony_ci } 639562306a36Sopenharmony_ci 639662306a36Sopenharmony_ci desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); 639762306a36Sopenharmony_ci } 639862306a36Sopenharmony_ci} 639962306a36Sopenharmony_ci 640062306a36Sopenharmony_ci#ifdef CONFIG_ACPI 640162306a36Sopenharmony_ci/** 640262306a36Sopenharmony_ci * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle 640362306a36Sopenharmony_ci * @hdev: USB device belonging to the usb hub 640462306a36Sopenharmony_ci * @port1: port num of the port 640562306a36Sopenharmony_ci * 640662306a36Sopenharmony_ci * Return: Port's acpi handle if successful, %NULL if params are 640762306a36Sopenharmony_ci * invalid. 640862306a36Sopenharmony_ci */ 640962306a36Sopenharmony_ciacpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, 641062306a36Sopenharmony_ci int port1) 641162306a36Sopenharmony_ci{ 641262306a36Sopenharmony_ci struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 641362306a36Sopenharmony_ci 641462306a36Sopenharmony_ci if (!hub) 641562306a36Sopenharmony_ci return NULL; 641662306a36Sopenharmony_ci 641762306a36Sopenharmony_ci return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); 641862306a36Sopenharmony_ci} 641962306a36Sopenharmony_ci#endif 6420