11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * USB hub driver. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 51da177e4SLinus Torvalds * (C) Copyright 1999 Johannes Erdfelt 61da177e4SLinus Torvalds * (C) Copyright 1999 Gregory P. Smith 71da177e4SLinus Torvalds * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) 81da177e4SLinus Torvalds * 9b65fba3dSGreg Kroah-Hartman * Released under the GPLv2 only. 10b65fba3dSGreg Kroah-Hartman * SPDX-License-Identifier: GPL-2.0 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/kernel.h> 141da177e4SLinus Torvalds #include <linux/errno.h> 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/moduleparam.h> 171da177e4SLinus Torvalds #include <linux/completion.h> 185b3cc15aSIngo Molnar #include <linux/sched/mm.h> 191da177e4SLinus Torvalds #include <linux/list.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/ioctl.h> 221da177e4SLinus Torvalds #include <linux/usb.h> 231da177e4SLinus Torvalds #include <linux/usbdevice_fs.h> 2427729aadSEric Lescouet #include <linux/usb/hcd.h> 25925aa46bSRichard Zhao #include <linux/usb/otg.h> 2693362a87SPhil Dibowitz #include <linux/usb/quirks.h> 2732a69589SPetr Mladek #include <linux/workqueue.h> 284186ecf8SArjan van de Ven #include <linux/mutex.h> 29b04b3156STheodore Ts'o #include <linux/random.h> 30ad493e5eSLan Tianyu #include <linux/pm_qos.h> 311da177e4SLinus Torvalds 327c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 331da177e4SLinus Torvalds #include <asm/byteorder.h> 341da177e4SLinus Torvalds 356e30d7cbSLan Tianyu #include "hub.h" 36026f3fcbSPeter Chen #include "otg_whitelist.h" 371da177e4SLinus Torvalds 38e6f30deaSMing Lei #define USB_VENDOR_GENESYS_LOGIC 0x05e3 39e6f30deaSMing Lei #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01 40e6f30deaSMing Lei 41fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members 429ad3d6ccSAlan Stern * Note: Both are also protected by ->dev.sem, except that ->state can 431da177e4SLinus Torvalds * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 441da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock); 451da177e4SLinus Torvalds 4632a69589SPetr Mladek /* workqueue to process hub events */ 4732a69589SPetr Mladek static struct workqueue_struct *hub_wq; 4832a69589SPetr Mladek static void hub_event(struct work_struct *work); 491da177e4SLinus Torvalds 50d8521afeSDan Williams /* synchronize hub-port add/remove and peering operations */ 51d8521afeSDan Williams DEFINE_MUTEX(usb_port_peer_mutex); 52d8521afeSDan Williams 531da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */ 54a44007a4SSaurabh Sengar static bool blinkenlights; 551da177e4SLinus Torvalds module_param(blinkenlights, bool, S_IRUGO); 561da177e4SLinus Torvalds MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs"); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* 59fd7c519dSJaroslav Kysela * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 60fd7c519dSJaroslav Kysela * 10 seconds to send reply for the initial 64-byte descriptor request. 61fd7c519dSJaroslav Kysela */ 62fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */ 63fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 64fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 65b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout, 66b9cef6c3SWu Fengguang "initial 64-byte descriptor request timeout in milliseconds " 67b9cef6c3SWu Fengguang "(default 5000 - 5.0 seconds)"); 68fd7c519dSJaroslav Kysela 69fd7c519dSJaroslav Kysela /* 701da177e4SLinus Torvalds * As of 2.6.10 we introduce a new USB device initialization scheme which 711da177e4SLinus Torvalds * closely resembles the way Windows works. Hopefully it will be compatible 721da177e4SLinus Torvalds * with a wider range of devices than the old scheme. However some previously 731da177e4SLinus Torvalds * working devices may start giving rise to "device not accepting address" 741da177e4SLinus Torvalds * errors; if that happens the user can try the old scheme by adjusting the 751da177e4SLinus Torvalds * following module parameters. 761da177e4SLinus Torvalds * 771da177e4SLinus Torvalds * For maximum flexibility there are two boolean parameters to control the 781da177e4SLinus Torvalds * hub driver's behavior. On the first initialization attempt, if the 791da177e4SLinus Torvalds * "old_scheme_first" parameter is set then the old scheme will be used, 801da177e4SLinus Torvalds * otherwise the new scheme is used. If that fails and "use_both_schemes" 811da177e4SLinus Torvalds * is set, then the driver will make another attempt, using the other scheme. 821da177e4SLinus Torvalds */ 83a44007a4SSaurabh Sengar static bool old_scheme_first; 841da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 851da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first, 861da177e4SLinus Torvalds "start with the old device initialization scheme"); 871da177e4SLinus Torvalds 8890ab5ee9SRusty Russell static bool use_both_schemes = 1; 891da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 901da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes, 911da177e4SLinus Torvalds "try the other device initialization scheme if the " 921da177e4SLinus Torvalds "first one fails"); 931da177e4SLinus Torvalds 9432fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization. This interferes with 9532fe0198SAlan Stern * port reset on some companion controllers. 9632fe0198SAlan Stern */ 9732fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 9832fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 9932fe0198SAlan Stern 100ad493e5eSLan Tianyu #define HUB_DEBOUNCE_TIMEOUT 2000 101948fea37SAlan Stern #define HUB_DEBOUNCE_STEP 25 102948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE 100 103948fea37SAlan Stern 10432a69589SPetr Mladek static void hub_release(struct kref *kref); 105742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev); 1063bc02bceSGeert Uytterhoeven static int hub_port_disable(struct usb_hub *hub, int port1, int set_state); 107742120c6SMing Lei 108131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus) 1091da177e4SLinus Torvalds { 110d64aab0cSOliver Neukum if (hub_is_superspeedplus(hub->hdev)) 111d64aab0cSOliver Neukum return "10.0 Gb/s"; 112131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev)) 113131dec34SSarah Sharp return "5.0 Gb/s"; 114288ead45SAlan Stern if (portstatus & USB_PORT_STAT_HIGH_SPEED) 1151da177e4SLinus Torvalds return "480 Mb/s"; 116288ead45SAlan Stern else if (portstatus & USB_PORT_STAT_LOW_SPEED) 1171da177e4SLinus Torvalds return "1.5 Mb/s"; 1181da177e4SLinus Torvalds else 1191da177e4SLinus Torvalds return "12 Mb/s"; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */ 123ad493e5eSLan Tianyu struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev) 1241da177e4SLinus Torvalds { 125ff823c79SLan Tianyu if (!hdev || !hdev->actconfig || !hdev->maxchild) 12625118084SAlan Stern return NULL; 1271da177e4SLinus Torvalds return usb_get_intfdata(hdev->actconfig->interface[0]); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1302d2a3167SLu Baolu int usb_device_supports_lpm(struct usb_device *udev) 131d9b2099cSSarah Sharp { 132ad87e032SAlan Stern /* Some devices have trouble with LPM */ 133ad87e032SAlan Stern if (udev->quirks & USB_QUIRK_NO_LPM) 134ad87e032SAlan Stern return 0; 135ad87e032SAlan Stern 136d9b2099cSSarah Sharp /* USB 2.1 (and greater) devices indicate LPM support through 137d9b2099cSSarah Sharp * their USB 2.0 Extended Capabilities BOS descriptor. 138d9b2099cSSarah Sharp */ 139a8425292SRupesh Tatiya if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) { 140d9b2099cSSarah Sharp if (udev->bos->ext_cap && 141d9b2099cSSarah Sharp (USB_LPM_SUPPORT & 142d9b2099cSSarah Sharp le32_to_cpu(udev->bos->ext_cap->bmAttributes))) 143d9b2099cSSarah Sharp return 1; 144d9b2099cSSarah Sharp return 0; 145d9b2099cSSarah Sharp } 14651e0a012SSarah Sharp 14725cd2882SSarah Sharp /* 14825cd2882SSarah Sharp * According to the USB 3.0 spec, all USB 3.0 devices must support LPM. 14925cd2882SSarah Sharp * However, there are some that don't, and they set the U1/U2 exit 15025cd2882SSarah Sharp * latencies to zero. 15151e0a012SSarah Sharp */ 15251e0a012SSarah Sharp if (!udev->bos->ss_cap) { 15325cd2882SSarah Sharp dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n"); 154d9b2099cSSarah Sharp return 0; 155d9b2099cSSarah Sharp } 15651e0a012SSarah Sharp 15725cd2882SSarah Sharp if (udev->bos->ss_cap->bU1devExitLat == 0 && 15825cd2882SSarah Sharp udev->bos->ss_cap->bU2DevExitLat == 0) { 15925cd2882SSarah Sharp if (udev->parent) 16025cd2882SSarah Sharp dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n"); 16125cd2882SSarah Sharp else 16225cd2882SSarah Sharp dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n"); 16325cd2882SSarah Sharp return 0; 16425cd2882SSarah Sharp } 16525cd2882SSarah Sharp 16625cd2882SSarah Sharp if (!udev->parent || udev->parent->lpm_capable) 16725cd2882SSarah Sharp return 1; 16851e0a012SSarah Sharp return 0; 16951e0a012SSarah Sharp } 17051e0a012SSarah Sharp 17151e0a012SSarah Sharp /* 17251e0a012SSarah Sharp * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from 17351e0a012SSarah Sharp * either U1 or U2. 17451e0a012SSarah Sharp */ 17551e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev, 17651e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params, 17751e0a012SSarah Sharp unsigned int udev_exit_latency, 17851e0a012SSarah Sharp struct usb_hub *hub, 17951e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params, 18051e0a012SSarah Sharp unsigned int hub_exit_latency) 18151e0a012SSarah Sharp { 18251e0a012SSarah Sharp unsigned int total_mel; 18351e0a012SSarah Sharp unsigned int device_mel; 18451e0a012SSarah Sharp unsigned int hub_mel; 18551e0a012SSarah Sharp 18651e0a012SSarah Sharp /* 18751e0a012SSarah Sharp * Calculate the time it takes to transition all links from the roothub 18851e0a012SSarah Sharp * to the parent hub into U0. The parent hub must then decode the 18951e0a012SSarah Sharp * packet (hub header decode latency) to figure out which port it was 19051e0a012SSarah Sharp * bound for. 19151e0a012SSarah Sharp * 19251e0a012SSarah Sharp * The Hub Header decode latency is expressed in 0.1us intervals (0x1 19351e0a012SSarah Sharp * means 0.1us). Multiply that by 100 to get nanoseconds. 19451e0a012SSarah Sharp */ 19551e0a012SSarah Sharp total_mel = hub_lpm_params->mel + 19651e0a012SSarah Sharp (hub->descriptor->u.ss.bHubHdrDecLat * 100); 19751e0a012SSarah Sharp 19851e0a012SSarah Sharp /* 19951e0a012SSarah Sharp * How long will it take to transition the downstream hub's port into 20051e0a012SSarah Sharp * U0? The greater of either the hub exit latency or the device exit 20151e0a012SSarah Sharp * latency. 20251e0a012SSarah Sharp * 20351e0a012SSarah Sharp * The BOS U1/U2 exit latencies are expressed in 1us intervals. 20451e0a012SSarah Sharp * Multiply that by 1000 to get nanoseconds. 20551e0a012SSarah Sharp */ 20651e0a012SSarah Sharp device_mel = udev_exit_latency * 1000; 20751e0a012SSarah Sharp hub_mel = hub_exit_latency * 1000; 20851e0a012SSarah Sharp if (device_mel > hub_mel) 20951e0a012SSarah Sharp total_mel += device_mel; 21051e0a012SSarah Sharp else 21151e0a012SSarah Sharp total_mel += hub_mel; 21251e0a012SSarah Sharp 21351e0a012SSarah Sharp udev_lpm_params->mel = total_mel; 21451e0a012SSarah Sharp } 21551e0a012SSarah Sharp 21651e0a012SSarah Sharp /* 21751e0a012SSarah Sharp * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate 21851e0a012SSarah Sharp * a transition from either U1 or U2. 21951e0a012SSarah Sharp */ 22051e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev, 22151e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params, 22251e0a012SSarah Sharp unsigned int udev_exit_latency, 22351e0a012SSarah Sharp struct usb_hub *hub, 22451e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params, 22551e0a012SSarah Sharp unsigned int hub_exit_latency, 22651e0a012SSarah Sharp unsigned int port_to_port_exit_latency) 22751e0a012SSarah Sharp { 22851e0a012SSarah Sharp unsigned int first_link_pel; 22951e0a012SSarah Sharp unsigned int hub_pel; 23051e0a012SSarah Sharp 23151e0a012SSarah Sharp /* 23251e0a012SSarah Sharp * First, the device sends an LFPS to transition the link between the 23351e0a012SSarah Sharp * device and the parent hub into U0. The exit latency is the bigger of 23451e0a012SSarah Sharp * the device exit latency or the hub exit latency. 23551e0a012SSarah Sharp */ 23651e0a012SSarah Sharp if (udev_exit_latency > hub_exit_latency) 23751e0a012SSarah Sharp first_link_pel = udev_exit_latency * 1000; 23851e0a012SSarah Sharp else 23951e0a012SSarah Sharp first_link_pel = hub_exit_latency * 1000; 24051e0a012SSarah Sharp 24151e0a012SSarah Sharp /* 24251e0a012SSarah Sharp * When the hub starts to receive the LFPS, there is a slight delay for 24351e0a012SSarah Sharp * it to figure out that one of the ports is sending an LFPS. Then it 24451e0a012SSarah Sharp * will forward the LFPS to its upstream link. The exit latency is the 24551e0a012SSarah Sharp * delay, plus the PEL that we calculated for this hub. 24651e0a012SSarah Sharp */ 24751e0a012SSarah Sharp hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; 24851e0a012SSarah Sharp 24951e0a012SSarah Sharp /* 25051e0a012SSarah Sharp * According to figure C-7 in the USB 3.0 spec, the PEL for this device 25151e0a012SSarah Sharp * is the greater of the two exit latencies. 25251e0a012SSarah Sharp */ 25351e0a012SSarah Sharp if (first_link_pel > hub_pel) 25451e0a012SSarah Sharp udev_lpm_params->pel = first_link_pel; 25551e0a012SSarah Sharp else 25651e0a012SSarah Sharp udev_lpm_params->pel = hub_pel; 25751e0a012SSarah Sharp } 25851e0a012SSarah Sharp 25951e0a012SSarah Sharp /* 26051e0a012SSarah Sharp * Set the System Exit Latency (SEL) to indicate the total worst-case time from 26151e0a012SSarah Sharp * when a device initiates a transition to U0, until when it will receive the 26251e0a012SSarah Sharp * first packet from the host controller. 26351e0a012SSarah Sharp * 26451e0a012SSarah Sharp * Section C.1.5.1 describes the four components to this: 26551e0a012SSarah Sharp * - t1: device PEL 26651e0a012SSarah Sharp * - t2: time for the ERDY to make it from the device to the host. 26751e0a012SSarah Sharp * - t3: a host-specific delay to process the ERDY. 26851e0a012SSarah Sharp * - t4: time for the packet to make it from the host to the device. 26951e0a012SSarah Sharp * 27051e0a012SSarah Sharp * t3 is specific to both the xHCI host and the platform the host is integrated 27151e0a012SSarah Sharp * into. The Intel HW folks have said it's negligible, FIXME if a different 27251e0a012SSarah Sharp * vendor says otherwise. 27351e0a012SSarah Sharp */ 27451e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev, 27551e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params) 27651e0a012SSarah Sharp { 27751e0a012SSarah Sharp struct usb_device *parent; 27851e0a012SSarah Sharp unsigned int num_hubs; 27951e0a012SSarah Sharp unsigned int total_sel; 28051e0a012SSarah Sharp 28151e0a012SSarah Sharp /* t1 = device PEL */ 28251e0a012SSarah Sharp total_sel = udev_lpm_params->pel; 28351e0a012SSarah Sharp /* How many external hubs are in between the device & the root port. */ 28451e0a012SSarah Sharp for (parent = udev->parent, num_hubs = 0; parent->parent; 28551e0a012SSarah Sharp parent = parent->parent) 28651e0a012SSarah Sharp num_hubs++; 28751e0a012SSarah Sharp /* t2 = 2.1us + 250ns * (num_hubs - 1) */ 28851e0a012SSarah Sharp if (num_hubs > 0) 28951e0a012SSarah Sharp total_sel += 2100 + 250 * (num_hubs - 1); 29051e0a012SSarah Sharp 29151e0a012SSarah Sharp /* t4 = 250ns * num_hubs */ 29251e0a012SSarah Sharp total_sel += 250 * num_hubs; 29351e0a012SSarah Sharp 29451e0a012SSarah Sharp udev_lpm_params->sel = total_sel; 29551e0a012SSarah Sharp } 29651e0a012SSarah Sharp 29751e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev) 29851e0a012SSarah Sharp { 29951e0a012SSarah Sharp struct usb_hub *hub; 30051e0a012SSarah Sharp unsigned int port_to_port_delay; 30151e0a012SSarah Sharp unsigned int udev_u1_del; 30251e0a012SSarah Sharp unsigned int udev_u2_del; 30351e0a012SSarah Sharp unsigned int hub_u1_del; 30451e0a012SSarah Sharp unsigned int hub_u2_del; 30551e0a012SSarah Sharp 3068a1b2725SMathias Nyman if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER) 30751e0a012SSarah Sharp return; 30851e0a012SSarah Sharp 309ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent); 31051e0a012SSarah Sharp /* It doesn't take time to transition the roothub into U0, since it 31151e0a012SSarah Sharp * doesn't have an upstream link. 31251e0a012SSarah Sharp */ 31351e0a012SSarah Sharp if (!hub) 31451e0a012SSarah Sharp return; 31551e0a012SSarah Sharp 31651e0a012SSarah Sharp udev_u1_del = udev->bos->ss_cap->bU1devExitLat; 3174d967995SXenia Ragiadakou udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat); 31851e0a012SSarah Sharp hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; 3194d967995SXenia Ragiadakou hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat); 32051e0a012SSarah Sharp 32151e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, 32251e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del); 32351e0a012SSarah Sharp 32451e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, 32551e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del); 32651e0a012SSarah Sharp 32751e0a012SSarah Sharp /* 32851e0a012SSarah Sharp * Appendix C, section C.2.2.2, says that there is a slight delay from 32951e0a012SSarah Sharp * when the parent hub notices the downstream port is trying to 33051e0a012SSarah Sharp * transition to U0 to when the hub initiates a U0 transition on its 33151e0a012SSarah Sharp * upstream port. The section says the delays are tPort2PortU1EL and 33251e0a012SSarah Sharp * tPort2PortU2EL, but it doesn't define what they are. 33351e0a012SSarah Sharp * 33451e0a012SSarah Sharp * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking 33551e0a012SSarah Sharp * about the same delays. Use the maximum delay calculations from those 33651e0a012SSarah Sharp * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For 33751e0a012SSarah Sharp * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I 33851e0a012SSarah Sharp * assume the device exit latencies they are talking about are the hub 33951e0a012SSarah Sharp * exit latencies. 34051e0a012SSarah Sharp * 34151e0a012SSarah Sharp * What do we do if the U2 exit latency is less than the U1 exit 34251e0a012SSarah Sharp * latency? It's possible, although not likely... 34351e0a012SSarah Sharp */ 34451e0a012SSarah Sharp port_to_port_delay = 1; 34551e0a012SSarah Sharp 34651e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, 34751e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del, 34851e0a012SSarah Sharp port_to_port_delay); 34951e0a012SSarah Sharp 35051e0a012SSarah Sharp if (hub_u2_del > hub_u1_del) 35151e0a012SSarah Sharp port_to_port_delay = 1 + hub_u2_del - hub_u1_del; 35251e0a012SSarah Sharp else 35351e0a012SSarah Sharp port_to_port_delay = 1 + hub_u1_del; 35451e0a012SSarah Sharp 35551e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, 35651e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del, 35751e0a012SSarah Sharp port_to_port_delay); 35851e0a012SSarah Sharp 35951e0a012SSarah Sharp /* Now that we've got PEL, calculate SEL. */ 36051e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u1_params); 36151e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u2_params); 36251e0a012SSarah Sharp } 363d9b2099cSSarah Sharp 3641da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */ 365dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data) 3661da177e4SLinus Torvalds { 367dbe79bbeSJohn Youn int i, ret, size; 368dbe79bbeSJohn Youn unsigned dtype; 369dbe79bbeSJohn Youn 370dbe79bbeSJohn Youn if (hub_is_superspeed(hdev)) { 371dbe79bbeSJohn Youn dtype = USB_DT_SS_HUB; 372dbe79bbeSJohn Youn size = USB_DT_SS_HUB_SIZE; 373dbe79bbeSJohn Youn } else { 374dbe79bbeSJohn Youn dtype = USB_DT_HUB; 375dbe79bbeSJohn Youn size = sizeof(struct usb_hub_descriptor); 376dbe79bbeSJohn Youn } 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds for (i = 0; i < 3; i++) { 3791da177e4SLinus Torvalds ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 3801da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 381dbe79bbeSJohn Youn dtype << 8, 0, data, size, 3821da177e4SLinus Torvalds USB_CTRL_GET_TIMEOUT); 3831da177e4SLinus Torvalds if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) 3841da177e4SLinus Torvalds return ret; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds return -EINVAL; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* 3901da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.1 3911da177e4SLinus Torvalds */ 3921da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 3951da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* 3991da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.2 4001da177e4SLinus Torvalds */ 401ad493e5eSLan Tianyu int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4041da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 4051da177e4SLinus Torvalds NULL, 0, 1000); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* 4091da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.13 4101da177e4SLinus Torvalds */ 4111da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4141da177e4SLinus Torvalds USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 4151da177e4SLinus Torvalds NULL, 0, 1000); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 418d99f6b41SDan Williams static char *to_led_name(int selector) 419d99f6b41SDan Williams { 420d99f6b41SDan Williams switch (selector) { 421d99f6b41SDan Williams case HUB_LED_AMBER: 422d99f6b41SDan Williams return "amber"; 423d99f6b41SDan Williams case HUB_LED_GREEN: 424d99f6b41SDan Williams return "green"; 425d99f6b41SDan Williams case HUB_LED_OFF: 426d99f6b41SDan Williams return "off"; 427d99f6b41SDan Williams case HUB_LED_AUTO: 428d99f6b41SDan Williams return "auto"; 429d99f6b41SDan Williams default: 430d99f6b41SDan Williams return "??"; 431d99f6b41SDan Williams } 432d99f6b41SDan Williams } 433d99f6b41SDan Williams 4341da177e4SLinus Torvalds /* 4351da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 4361da177e4SLinus Torvalds * for info about using port indicators 4371da177e4SLinus Torvalds */ 438d99f6b41SDan Williams static void set_port_led(struct usb_hub *hub, int port1, int selector) 4391da177e4SLinus Torvalds { 440d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 441d99f6b41SDan Williams int status; 442d99f6b41SDan Williams 443d99f6b41SDan Williams status = set_port_feature(hub->hdev, (selector << 8) | port1, 4441da177e4SLinus Torvalds USB_PORT_FEAT_INDICATOR); 445d99f6b41SDan Williams dev_dbg(&port_dev->dev, "indicator %s status %d\n", 446d99f6b41SDan Williams to_led_name(selector), status); 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds #define LED_CYCLE_PERIOD ((2*HZ)/3) 4501da177e4SLinus Torvalds 451c4028958SDavid Howells static void led_work(struct work_struct *work) 4521da177e4SLinus Torvalds { 453c4028958SDavid Howells struct usb_hub *hub = 454c4028958SDavid Howells container_of(work, struct usb_hub, leds.work); 4551da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 4561da177e4SLinus Torvalds unsigned i; 4571da177e4SLinus Torvalds unsigned changed = 0; 4581da177e4SLinus Torvalds int cursor = -1; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 4611da177e4SLinus Torvalds return; 4621da177e4SLinus Torvalds 4633bbc47d8SKrzysztof Mazur for (i = 0; i < hdev->maxchild; i++) { 4641da177e4SLinus Torvalds unsigned selector, mode; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /* 30%-50% duty cycle */ 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds switch (hub->indicator[i]) { 4691da177e4SLinus Torvalds /* cycle marker */ 4701da177e4SLinus Torvalds case INDICATOR_CYCLE: 4711da177e4SLinus Torvalds cursor = i; 4721da177e4SLinus Torvalds selector = HUB_LED_AUTO; 4731da177e4SLinus Torvalds mode = INDICATOR_AUTO; 4741da177e4SLinus Torvalds break; 4751da177e4SLinus Torvalds /* blinking green = sw attention */ 4761da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK: 4771da177e4SLinus Torvalds selector = HUB_LED_GREEN; 4781da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK_OFF; 4791da177e4SLinus Torvalds break; 4801da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK_OFF: 4811da177e4SLinus Torvalds selector = HUB_LED_OFF; 4821da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK; 4831da177e4SLinus Torvalds break; 4841da177e4SLinus Torvalds /* blinking amber = hw attention */ 4851da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK: 4861da177e4SLinus Torvalds selector = HUB_LED_AMBER; 4871da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK_OFF; 4881da177e4SLinus Torvalds break; 4891da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK_OFF: 4901da177e4SLinus Torvalds selector = HUB_LED_OFF; 4911da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK; 4921da177e4SLinus Torvalds break; 4931da177e4SLinus Torvalds /* blink green/amber = reserved */ 4941da177e4SLinus Torvalds case INDICATOR_ALT_BLINK: 4951da177e4SLinus Torvalds selector = HUB_LED_GREEN; 4961da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK_OFF; 4971da177e4SLinus Torvalds break; 4981da177e4SLinus Torvalds case INDICATOR_ALT_BLINK_OFF: 4991da177e4SLinus Torvalds selector = HUB_LED_AMBER; 5001da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK; 5011da177e4SLinus Torvalds break; 5021da177e4SLinus Torvalds default: 5031da177e4SLinus Torvalds continue; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds if (selector != HUB_LED_AUTO) 5061da177e4SLinus Torvalds changed = 1; 5071da177e4SLinus Torvalds set_port_led(hub, i + 1, selector); 5081da177e4SLinus Torvalds hub->indicator[i] = mode; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds if (!changed && blinkenlights) { 5111da177e4SLinus Torvalds cursor++; 5123bbc47d8SKrzysztof Mazur cursor %= hdev->maxchild; 5131da177e4SLinus Torvalds set_port_led(hub, cursor + 1, HUB_LED_GREEN); 5141da177e4SLinus Torvalds hub->indicator[cursor] = INDICATOR_CYCLE; 5151da177e4SLinus Torvalds changed++; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds if (changed) 51822f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq, 51922f6a0f0SShaibal Dutta &hub->leds, LED_CYCLE_PERIOD); 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */ 5231da177e4SLinus Torvalds #define USB_STS_TIMEOUT 1000 5241da177e4SLinus Torvalds #define USB_STS_RETRIES 5 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds /* 5271da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.6 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev, 5301da177e4SLinus Torvalds struct usb_hub_status *data) 5311da177e4SLinus Torvalds { 5321da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 5331da177e4SLinus Torvalds 5343824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES && 5353824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) { 5361da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 5371da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 5381da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds return status; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /* 5441da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7 5450cdd49a1SMathias Nyman * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6 5461da177e4SLinus Torvalds */ 5471da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1, 5480cdd49a1SMathias Nyman void *data, u16 value, u16 length) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 5511da177e4SLinus Torvalds 5523824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES && 5533824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) { 5541da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 5550cdd49a1SMathias Nyman USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value, 5560cdd49a1SMathias Nyman port1, data, length, USB_STS_TIMEOUT); 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds return status; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5610cdd49a1SMathias Nyman static int hub_ext_port_status(struct usb_hub *hub, int port1, int type, 5620cdd49a1SMathias Nyman u16 *status, u16 *change, u32 *ext_status) 5633eb14915SAlan Stern { 5643eb14915SAlan Stern int ret; 5650cdd49a1SMathias Nyman int len = 4; 5660cdd49a1SMathias Nyman 5670cdd49a1SMathias Nyman if (type != HUB_PORT_STATUS) 5680cdd49a1SMathias Nyman len = 8; 5693eb14915SAlan Stern 5703eb14915SAlan Stern mutex_lock(&hub->status_mutex); 5710cdd49a1SMathias Nyman ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len); 5720cdd49a1SMathias Nyman if (ret < len) { 573e9e88fb7SAlan Stern if (ret != -ENODEV) 5743eb14915SAlan Stern dev_err(hub->intfdev, 5753eb14915SAlan Stern "%s failed (err = %d)\n", __func__, ret); 5763eb14915SAlan Stern if (ret >= 0) 5773eb14915SAlan Stern ret = -EIO; 5783eb14915SAlan Stern } else { 5793eb14915SAlan Stern *status = le16_to_cpu(hub->status->port.wPortStatus); 5803eb14915SAlan Stern *change = le16_to_cpu(hub->status->port.wPortChange); 5810cdd49a1SMathias Nyman if (type != HUB_PORT_STATUS && ext_status) 5820cdd49a1SMathias Nyman *ext_status = le32_to_cpu( 5830cdd49a1SMathias Nyman hub->status->port.dwExtPortStatus); 5843eb14915SAlan Stern ret = 0; 5853eb14915SAlan Stern } 5863eb14915SAlan Stern mutex_unlock(&hub->status_mutex); 5873eb14915SAlan Stern return ret; 5883eb14915SAlan Stern } 5893eb14915SAlan Stern 5900cdd49a1SMathias Nyman static int hub_port_status(struct usb_hub *hub, int port1, 5910cdd49a1SMathias Nyman u16 *status, u16 *change) 5920cdd49a1SMathias Nyman { 5930cdd49a1SMathias Nyman return hub_ext_port_status(hub, port1, HUB_PORT_STATUS, 5940cdd49a1SMathias Nyman status, change, NULL); 5950cdd49a1SMathias Nyman } 5960cdd49a1SMathias Nyman 59732a69589SPetr Mladek static void kick_hub_wq(struct usb_hub *hub) 5981da177e4SLinus Torvalds { 59932a69589SPetr Mladek struct usb_interface *intf; 6001da177e4SLinus Torvalds 60132a69589SPetr Mladek if (hub->disconnected || work_pending(&hub->events)) 60232a69589SPetr Mladek return; 6038e4ceb38SAlan Stern 60432a69589SPetr Mladek /* 60532a69589SPetr Mladek * Suppress autosuspend until the event is proceed. 60632a69589SPetr Mladek * 60732a69589SPetr Mladek * Be careful and make sure that the symmetric operation is 60832a69589SPetr Mladek * always called. We are here only when there is no pending 60932a69589SPetr Mladek * work for this hub. Therefore put the interface either when 61032a69589SPetr Mladek * the new work is called or when it is canceled. 61132a69589SPetr Mladek */ 61232a69589SPetr Mladek intf = to_usb_interface(hub->intfdev); 61332a69589SPetr Mladek usb_autopm_get_interface_no_resume(intf); 61432a69589SPetr Mladek kref_get(&hub->kref); 61532a69589SPetr Mladek 61632a69589SPetr Mladek if (queue_work(hub_wq, &hub->events)) 61732a69589SPetr Mladek return; 61832a69589SPetr Mladek 61932a69589SPetr Mladek /* the work has already been scheduled */ 62032a69589SPetr Mladek usb_autopm_put_interface_async(intf); 62132a69589SPetr Mladek kref_put(&hub->kref, hub_release); 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 62459d48b3fSPetr Mladek void usb_kick_hub_wq(struct usb_device *hdev) 6251da177e4SLinus Torvalds { 626ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 62725118084SAlan Stern 62825118084SAlan Stern if (hub) 62932a69589SPetr Mladek kick_hub_wq(hub); 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6324ee823b8SSarah Sharp /* 6334ee823b8SSarah Sharp * Let the USB core know that a USB 3.0 device has sent a Function Wake Device 6344ee823b8SSarah Sharp * Notification, which indicates it had initiated remote wakeup. 6354ee823b8SSarah Sharp * 6364ee823b8SSarah Sharp * USB 3.0 hubs do not report the port link state change from U3 to U0 when the 6374ee823b8SSarah Sharp * device initiates resume, so the USB core will not receive notice of the 6384ee823b8SSarah Sharp * resume through the normal hub interrupt URB. 6394ee823b8SSarah Sharp */ 6404ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev, 6414ee823b8SSarah Sharp unsigned int portnum) 6424ee823b8SSarah Sharp { 6434ee823b8SSarah Sharp struct usb_hub *hub; 6444ee823b8SSarah Sharp 6454ee823b8SSarah Sharp if (!hdev) 6464ee823b8SSarah Sharp return; 6474ee823b8SSarah Sharp 648ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(hdev); 6494ee823b8SSarah Sharp if (hub) { 6504ee823b8SSarah Sharp set_bit(portnum, hub->wakeup_bits); 65132a69589SPetr Mladek kick_hub_wq(hub); 6524ee823b8SSarah Sharp } 6534ee823b8SSarah Sharp } 6544ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */ 6577d12e780SDavid Howells static void hub_irq(struct urb *urb) 6581da177e4SLinus Torvalds { 659ec17cf1cSTobias Klauser struct usb_hub *hub = urb->context; 660e015268dSAlan Stern int status = urb->status; 66171d2718fSRoel Kluin unsigned i; 6621da177e4SLinus Torvalds unsigned long bits; 6631da177e4SLinus Torvalds 664e015268dSAlan Stern switch (status) { 6651da177e4SLinus Torvalds case -ENOENT: /* synchronous unlink */ 6661da177e4SLinus Torvalds case -ECONNRESET: /* async unlink */ 6671da177e4SLinus Torvalds case -ESHUTDOWN: /* hardware going away */ 6681da177e4SLinus Torvalds return; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds default: /* presumably an error */ 6711da177e4SLinus Torvalds /* Cause a hub reset after 10 consecutive errors */ 672e015268dSAlan Stern dev_dbg(hub->intfdev, "transfer --> %d\n", status); 6731da177e4SLinus Torvalds if ((++hub->nerrors < 10) || hub->error) 6741da177e4SLinus Torvalds goto resubmit; 675e015268dSAlan Stern hub->error = status; 6761da177e4SLinus Torvalds /* FALL THROUGH */ 6771da177e4SLinus Torvalds 67837ebb549SPetr Mladek /* let hub_wq handle things */ 6791da177e4SLinus Torvalds case 0: /* we got data: port status changed */ 6801da177e4SLinus Torvalds bits = 0; 6811da177e4SLinus Torvalds for (i = 0; i < urb->actual_length; ++i) 6821da177e4SLinus Torvalds bits |= ((unsigned long) ((*hub->buffer)[i])) 6831da177e4SLinus Torvalds << (i*8); 6841da177e4SLinus Torvalds hub->event_bits[0] = bits; 6851da177e4SLinus Torvalds break; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds hub->nerrors = 0; 6891da177e4SLinus Torvalds 69037ebb549SPetr Mladek /* Something happened, let hub_wq figure it out */ 69132a69589SPetr Mladek kick_hub_wq(hub); 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds resubmit: 6941da177e4SLinus Torvalds if (hub->quiescing) 6951da177e4SLinus Torvalds return; 6961da177e4SLinus Torvalds 697088a3daeSKris Borer status = usb_submit_urb(hub->urb, GFP_ATOMIC); 698088a3daeSKris Borer if (status != 0 && status != -ENODEV && status != -EPERM) 6991da177e4SLinus Torvalds dev_err(hub->intfdev, "resubmit --> %d\n", status); 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */ 7031da177e4SLinus Torvalds static inline int 7041da177e4SLinus Torvalds hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt) 7051da177e4SLinus Torvalds { 7062c7b871bSWilliam Gulland /* Need to clear both directions for control ep */ 7072c7b871bSWilliam Gulland if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) == 7082c7b871bSWilliam Gulland USB_ENDPOINT_XFER_CONTROL) { 7092c7b871bSWilliam Gulland int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 7102c7b871bSWilliam Gulland HUB_CLEAR_TT_BUFFER, USB_RT_PORT, 7112c7b871bSWilliam Gulland devinfo ^ 0x8000, tt, NULL, 0, 1000); 7122c7b871bSWilliam Gulland if (status) 7132c7b871bSWilliam Gulland return status; 7142c7b871bSWilliam Gulland } 715c2f6595fSAlan Stern return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 7161da177e4SLinus Torvalds HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 7171da177e4SLinus Torvalds tt, NULL, 0, 1000); 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* 72137ebb549SPetr Mladek * enumeration blocks hub_wq for a long time. we use keventd instead, since 7221da177e4SLinus Torvalds * long blocking there is the exception, not the rule. accordingly, HCDs 7231da177e4SLinus Torvalds * talking to TTs must queue control transfers (not just bulk and iso), so 7241da177e4SLinus Torvalds * both can talk to the same hub concurrently. 7251da177e4SLinus Torvalds */ 726cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work) 7271da177e4SLinus Torvalds { 728c4028958SDavid Howells struct usb_hub *hub = 729cb88a1b8SAlan Stern container_of(work, struct usb_hub, tt.clear_work); 7301da177e4SLinus Torvalds unsigned long flags; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds spin_lock_irqsave(&hub->tt.lock, flags); 7333b6054daSOctavian Purdila while (!list_empty(&hub->tt.clear_list)) { 734d0f830d3SH Hartley Sweeten struct list_head *next; 7351da177e4SLinus Torvalds struct usb_tt_clear *clear; 7361da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 737cb88a1b8SAlan Stern const struct hc_driver *drv; 7381da177e4SLinus Torvalds int status; 7391da177e4SLinus Torvalds 740d0f830d3SH Hartley Sweeten next = hub->tt.clear_list.next; 741d0f830d3SH Hartley Sweeten clear = list_entry(next, struct usb_tt_clear, clear_list); 7421da177e4SLinus Torvalds list_del(&clear->clear_list); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* drop lock so HCD can concurrently report other TT errors */ 7451da177e4SLinus Torvalds spin_unlock_irqrestore(&hub->tt.lock, flags); 7461da177e4SLinus Torvalds status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt); 747e9e88fb7SAlan Stern if (status && status != -ENODEV) 7481da177e4SLinus Torvalds dev_err(&hdev->dev, 7491da177e4SLinus Torvalds "clear tt %d (%04x) error %d\n", 7501da177e4SLinus Torvalds clear->tt, clear->devinfo, status); 751cb88a1b8SAlan Stern 752cb88a1b8SAlan Stern /* Tell the HCD, even if the operation failed */ 753cb88a1b8SAlan Stern drv = clear->hcd->driver; 754cb88a1b8SAlan Stern if (drv->clear_tt_buffer_complete) 755cb88a1b8SAlan Stern (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); 756cb88a1b8SAlan Stern 7571da177e4SLinus Torvalds kfree(clear); 758cb88a1b8SAlan Stern spin_lock_irqsave(&hub->tt.lock, flags); 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds spin_unlock_irqrestore(&hub->tt.lock, flags); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /** 764971fcd49SLan Tianyu * usb_hub_set_port_power - control hub port's power state 76541341261SMathias Nyman * @hdev: USB device belonging to the usb hub 76641341261SMathias Nyman * @hub: target hub 767971fcd49SLan Tianyu * @port1: port index 768971fcd49SLan Tianyu * @set: expected status 769971fcd49SLan Tianyu * 770971fcd49SLan Tianyu * call this function to control port's power via setting or 771971fcd49SLan Tianyu * clearing the port's PORT_POWER feature. 772626f090cSYacine Belkadi * 773626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise. 774971fcd49SLan Tianyu */ 77541341261SMathias Nyman int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, 77641341261SMathias Nyman int port1, bool set) 777971fcd49SLan Tianyu { 778971fcd49SLan Tianyu int ret; 779971fcd49SLan Tianyu 780971fcd49SLan Tianyu if (set) 781971fcd49SLan Tianyu ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 782971fcd49SLan Tianyu else 783ad493e5eSLan Tianyu ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 784ad493e5eSLan Tianyu 785d5c3834eSDan Williams if (ret) 786971fcd49SLan Tianyu return ret; 787d5c3834eSDan Williams 788d5c3834eSDan Williams if (set) 789d5c3834eSDan Williams set_bit(port1, hub->power_bits); 790d5c3834eSDan Williams else 791d5c3834eSDan Williams clear_bit(port1, hub->power_bits); 792d5c3834eSDan Williams return 0; 793971fcd49SLan Tianyu } 794971fcd49SLan Tianyu 795971fcd49SLan Tianyu /** 796cb88a1b8SAlan Stern * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub 797cb88a1b8SAlan Stern * @urb: an URB associated with the failed or incomplete split transaction 7981da177e4SLinus Torvalds * 7991da177e4SLinus Torvalds * High speed HCDs use this to tell the hub driver that some split control or 8001da177e4SLinus Torvalds * bulk transaction failed in a way that requires clearing internal state of 8011da177e4SLinus Torvalds * a transaction translator. This is normally detected (and reported) from 8021da177e4SLinus Torvalds * interrupt context. 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * It may not be possible for that hub to handle additional full (or low) 8051da177e4SLinus Torvalds * speed transactions until that state is fully cleared out. 806626f090cSYacine Belkadi * 807626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise. 8081da177e4SLinus Torvalds */ 809cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb) 8101da177e4SLinus Torvalds { 811cb88a1b8SAlan Stern struct usb_device *udev = urb->dev; 812cb88a1b8SAlan Stern int pipe = urb->pipe; 8131da177e4SLinus Torvalds struct usb_tt *tt = udev->tt; 8141da177e4SLinus Torvalds unsigned long flags; 8151da177e4SLinus Torvalds struct usb_tt_clear *clear; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* we've got to cope with an arbitrary number of pending TT clears, 8181da177e4SLinus Torvalds * since each TT has "at least two" buffers that can need it (and 8191da177e4SLinus Torvalds * there can be many TTs per hub). even if they're uncommon. 8201da177e4SLinus Torvalds */ 821d544d273SGreg Kroah-Hartman clear = kmalloc(sizeof *clear, GFP_ATOMIC); 822d544d273SGreg Kroah-Hartman if (clear == NULL) { 8231da177e4SLinus Torvalds dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 8241da177e4SLinus Torvalds /* FIXME recover somehow ... RESET_TT? */ 825cb88a1b8SAlan Stern return -ENOMEM; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* info that CLEAR_TT_BUFFER needs */ 8291da177e4SLinus Torvalds clear->tt = tt->multi ? udev->ttport : 1; 8301da177e4SLinus Torvalds clear->devinfo = usb_pipeendpoint (pipe); 8311da177e4SLinus Torvalds clear->devinfo |= udev->devnum << 4; 8321da177e4SLinus Torvalds clear->devinfo |= usb_pipecontrol(pipe) 8331da177e4SLinus Torvalds ? (USB_ENDPOINT_XFER_CONTROL << 11) 8341da177e4SLinus Torvalds : (USB_ENDPOINT_XFER_BULK << 11); 8351da177e4SLinus Torvalds if (usb_pipein(pipe)) 8361da177e4SLinus Torvalds clear->devinfo |= 1 << 15; 8371da177e4SLinus Torvalds 838cb88a1b8SAlan Stern /* info for completion callback */ 839cb88a1b8SAlan Stern clear->hcd = bus_to_hcd(udev->bus); 840cb88a1b8SAlan Stern clear->ep = urb->ep; 841cb88a1b8SAlan Stern 8421da177e4SLinus Torvalds /* tell keventd to clear state for this TT */ 8431da177e4SLinus Torvalds spin_lock_irqsave(&tt->lock, flags); 8441da177e4SLinus Torvalds list_add_tail(&clear->clear_list, &tt->clear_list); 845cb88a1b8SAlan Stern schedule_work(&tt->clear_work); 8461da177e4SLinus Torvalds spin_unlock_irqrestore(&tt->lock, flags); 847cb88a1b8SAlan Stern return 0; 8481da177e4SLinus Torvalds } 849cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); 8501da177e4SLinus Torvalds 8517ad3c470SDan Williams static void hub_power_on(struct usb_hub *hub, bool do_delay) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds int port1; 8541da177e4SLinus Torvalds 8554489a571SAlan Stern /* Enable power on each port. Some hubs have reserved values 8564489a571SAlan Stern * of LPSM (> 2) in their descriptors, even though they are 8574489a571SAlan Stern * USB 2.0 hubs. Some hubs do not implement port-power switching 8584489a571SAlan Stern * but only emulate it. In all cases, the ports won't work 8594489a571SAlan Stern * unless we send these messages to the hub. 8604489a571SAlan Stern */ 8619262c19dSDan Williams if (hub_is_port_power_switchable(hub)) 8621da177e4SLinus Torvalds dev_dbg(hub->intfdev, "enabling power on all ports\n"); 8634489a571SAlan Stern else 8644489a571SAlan Stern dev_dbg(hub->intfdev, "trying to enable port power on " 8654489a571SAlan Stern "non-switchable hub\n"); 8663bbc47d8SKrzysztof Mazur for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) 867d5c3834eSDan Williams if (test_bit(port1, hub->power_bits)) 8684489a571SAlan Stern set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 869ad493e5eSLan Tianyu else 870ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 871ad493e5eSLan Tianyu USB_PORT_FEAT_POWER); 8728520f380SAlan Stern if (do_delay) 8737ad3c470SDan Williams msleep(hub_power_on_good_delay(hub)); 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub, 8771da177e4SLinus Torvalds u16 *status, u16 *change) 8781da177e4SLinus Torvalds { 8791da177e4SLinus Torvalds int ret; 8801da177e4SLinus Torvalds 881db90e7a1SAlan Stern mutex_lock(&hub->status_mutex); 8821da177e4SLinus Torvalds ret = get_hub_status(hub->hdev, &hub->status->hub); 883e9e88fb7SAlan Stern if (ret < 0) { 884e9e88fb7SAlan Stern if (ret != -ENODEV) 8851da177e4SLinus Torvalds dev_err(hub->intfdev, 886441b62c1SHarvey Harrison "%s failed (err = %d)\n", __func__, ret); 887e9e88fb7SAlan Stern } else { 8881da177e4SLinus Torvalds *status = le16_to_cpu(hub->status->hub.wHubStatus); 8891da177e4SLinus Torvalds *change = le16_to_cpu(hub->status->hub.wHubChange); 8901da177e4SLinus Torvalds ret = 0; 8911da177e4SLinus Torvalds } 892db90e7a1SAlan Stern mutex_unlock(&hub->status_mutex); 8931da177e4SLinus Torvalds return ret; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 89641e7e056SSarah Sharp static int hub_set_port_link_state(struct usb_hub *hub, int port1, 89741e7e056SSarah Sharp unsigned int link_status) 89841e7e056SSarah Sharp { 89941e7e056SSarah Sharp return set_port_feature(hub->hdev, 90041e7e056SSarah Sharp port1 | (link_status << 3), 90141e7e056SSarah Sharp USB_PORT_FEAT_LINK_STATE); 90241e7e056SSarah Sharp } 90341e7e056SSarah Sharp 90441e7e056SSarah Sharp /* 9056d42fcdbSJustin P. Mattock * Disable a port and mark a logical connect-change event, so that some 90637ebb549SPetr Mladek * time later hub_wq will disconnect() any existing usb_device on the port 9070458d5b4SAlan Stern * and will re-enumerate if there actually is a device attached. 9080458d5b4SAlan Stern */ 9090458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 9107d069b7dSAlan Stern { 911d99f6b41SDan Williams dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n"); 9120458d5b4SAlan Stern hub_port_disable(hub, port1, 1); 9130458d5b4SAlan Stern 9140458d5b4SAlan Stern /* FIXME let caller ask to power down the port: 9150458d5b4SAlan Stern * - some devices won't enumerate without a VBUS power cycle 9160458d5b4SAlan Stern * - SRP saves power that way 9170458d5b4SAlan Stern * - ... new call, TBD ... 9180458d5b4SAlan Stern * That's easy if this hub can switch power per-port, and 91937ebb549SPetr Mladek * hub_wq reactivates the port later (timer, SRP, etc). 9200458d5b4SAlan Stern * Powerdown must be optional, because of reset/DFU. 9210458d5b4SAlan Stern */ 9220458d5b4SAlan Stern 9230458d5b4SAlan Stern set_bit(port1, hub->change_bits); 92432a69589SPetr Mladek kick_hub_wq(hub); 9250458d5b4SAlan Stern } 9260458d5b4SAlan Stern 927253e0572SAlan Stern /** 928253e0572SAlan Stern * usb_remove_device - disable a device's port on its parent hub 929253e0572SAlan Stern * @udev: device to be disabled and removed 930253e0572SAlan Stern * Context: @udev locked, must be able to sleep. 931253e0572SAlan Stern * 93237ebb549SPetr Mladek * After @udev's port has been disabled, hub_wq is notified and it will 933253e0572SAlan Stern * see that the device has been disconnected. When the device is 934253e0572SAlan Stern * physically unplugged and something is plugged in, the events will 935253e0572SAlan Stern * be received and processed normally. 936626f090cSYacine Belkadi * 937626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise. 938253e0572SAlan Stern */ 939253e0572SAlan Stern int usb_remove_device(struct usb_device *udev) 940253e0572SAlan Stern { 941253e0572SAlan Stern struct usb_hub *hub; 942253e0572SAlan Stern struct usb_interface *intf; 943253e0572SAlan Stern 944253e0572SAlan Stern if (!udev->parent) /* Can't remove a root hub */ 945253e0572SAlan Stern return -EINVAL; 946ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent); 947253e0572SAlan Stern intf = to_usb_interface(hub->intfdev); 948253e0572SAlan Stern 949253e0572SAlan Stern usb_autopm_get_interface(intf); 950253e0572SAlan Stern set_bit(udev->portnum, hub->removed_bits); 951253e0572SAlan Stern hub_port_logical_disconnect(hub, udev->portnum); 952253e0572SAlan Stern usb_autopm_put_interface(intf); 953253e0572SAlan Stern return 0; 954253e0572SAlan Stern } 955253e0572SAlan Stern 9566ee0b270SAlan Stern enum hub_activation_type { 9578e4ceb38SAlan Stern HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ 9588520f380SAlan Stern HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 9596ee0b270SAlan Stern }; 9605e6effaeSAlan Stern 9618520f380SAlan Stern static void hub_init_func2(struct work_struct *ws); 9628520f380SAlan Stern static void hub_init_func3(struct work_struct *ws); 9638520f380SAlan Stern 964f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) 9655e6effaeSAlan Stern { 9665e6effaeSAlan Stern struct usb_device *hdev = hub->hdev; 967653a39d1SSarah Sharp struct usb_hcd *hcd; 968653a39d1SSarah Sharp int ret; 9695e6effaeSAlan Stern int port1; 970f2835219SAlan Stern int status; 971948fea37SAlan Stern bool need_debounce_delay = false; 9728520f380SAlan Stern unsigned delay; 9738520f380SAlan Stern 9748520f380SAlan Stern /* Continue a partial initialization */ 975e50293efSAlan Stern if (type == HUB_INIT2 || type == HUB_INIT3) { 97607d316a2SAlan Stern device_lock(&hdev->dev); 977e50293efSAlan Stern 978e50293efSAlan Stern /* Was the hub disconnected while we were waiting? */ 979ca5cbc8bSAlan Stern if (hub->disconnected) 980ca5cbc8bSAlan Stern goto disconnected; 9818520f380SAlan Stern if (type == HUB_INIT2) 9828520f380SAlan Stern goto init2; 9838520f380SAlan Stern goto init3; 984e50293efSAlan Stern } 985e50293efSAlan Stern kref_get(&hub->kref); 9865e6effaeSAlan Stern 987a45aa3b3SElric Fu /* The superspeed hub except for root hub has to use Hub Depth 988a45aa3b3SElric Fu * value as an offset into the route string to locate the bits 989a45aa3b3SElric Fu * it uses to determine the downstream port number. So hub driver 990a45aa3b3SElric Fu * should send a set hub depth request to superspeed hub after 991a45aa3b3SElric Fu * the superspeed hub is set configuration in initialization or 992a45aa3b3SElric Fu * reset procedure. 993a45aa3b3SElric Fu * 994a45aa3b3SElric Fu * After a resume, port power should still be on. 995f2835219SAlan Stern * For any other type of activation, turn it on. 996f2835219SAlan Stern */ 9978520f380SAlan Stern if (type != HUB_RESUME) { 998a45aa3b3SElric Fu if (hdev->parent && hub_is_superspeed(hdev)) { 999a45aa3b3SElric Fu ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 1000a45aa3b3SElric Fu HUB_SET_DEPTH, USB_RT_HUB, 1001a45aa3b3SElric Fu hdev->level - 1, 0, NULL, 0, 1002a45aa3b3SElric Fu USB_CTRL_SET_TIMEOUT); 1003a45aa3b3SElric Fu if (ret < 0) 1004a45aa3b3SElric Fu dev_err(hub->intfdev, 1005a45aa3b3SElric Fu "set hub depth failed\n"); 1006a45aa3b3SElric Fu } 10078520f380SAlan Stern 10088520f380SAlan Stern /* Speed up system boot by using a delayed_work for the 10098520f380SAlan Stern * hub's initial power-up delays. This is pretty awkward 10108520f380SAlan Stern * and the implementation looks like a home-brewed sort of 10118520f380SAlan Stern * setjmp/longjmp, but it saves at least 100 ms for each 10128520f380SAlan Stern * root hub (assuming usbcore is compiled into the kernel 10138520f380SAlan Stern * rather than as a module). It adds up. 10148520f380SAlan Stern * 10158520f380SAlan Stern * This can't be done for HUB_RESUME or HUB_RESET_RESUME 10168520f380SAlan Stern * because for those activation types the ports have to be 10178520f380SAlan Stern * operational when we return. In theory this could be done 10188520f380SAlan Stern * for HUB_POST_RESET, but it's easier not to. 10198520f380SAlan Stern */ 10208520f380SAlan Stern if (type == HUB_INIT) { 102117a364e2SKris Borer delay = hub_power_on_good_delay(hub); 10227ad3c470SDan Williams 10237ad3c470SDan Williams hub_power_on(hub, false); 102477fa83cfSTejun Heo INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); 102522f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq, 102622f6a0f0SShaibal Dutta &hub->init_work, 10278520f380SAlan Stern msecs_to_jiffies(delay)); 102861fbeba1SAlan Stern 102961fbeba1SAlan Stern /* Suppress autosuspend until init is done */ 10308e4ceb38SAlan Stern usb_autopm_get_interface_no_resume( 10318e4ceb38SAlan Stern to_usb_interface(hub->intfdev)); 10328520f380SAlan Stern return; /* Continues at init2: below */ 1033653a39d1SSarah Sharp } else if (type == HUB_RESET_RESUME) { 1034653a39d1SSarah Sharp /* The internal host controller state for the hub device 1035653a39d1SSarah Sharp * may be gone after a host power loss on system resume. 1036653a39d1SSarah Sharp * Update the device's info so the HW knows it's a hub. 1037653a39d1SSarah Sharp */ 1038653a39d1SSarah Sharp hcd = bus_to_hcd(hdev->bus); 1039653a39d1SSarah Sharp if (hcd->driver->update_hub_device) { 1040653a39d1SSarah Sharp ret = hcd->driver->update_hub_device(hcd, hdev, 1041653a39d1SSarah Sharp &hub->tt, GFP_NOIO); 1042653a39d1SSarah Sharp if (ret < 0) { 1043653a39d1SSarah Sharp dev_err(hub->intfdev, "Host not " 1044653a39d1SSarah Sharp "accepting hub info " 1045653a39d1SSarah Sharp "update.\n"); 1046653a39d1SSarah Sharp dev_err(hub->intfdev, "LS/FS devices " 1047653a39d1SSarah Sharp "and hubs may not work " 1048653a39d1SSarah Sharp "under this hub\n."); 1049653a39d1SSarah Sharp } 1050653a39d1SSarah Sharp } 1051653a39d1SSarah Sharp hub_power_on(hub, true); 10528520f380SAlan Stern } else { 10538520f380SAlan Stern hub_power_on(hub, true); 10548520f380SAlan Stern } 10558520f380SAlan Stern } 10568520f380SAlan Stern init2: 1057f2835219SAlan Stern 1058d99f6b41SDan Williams /* 105937ebb549SPetr Mladek * Check each port and set hub->change_bits to let hub_wq know 10606ee0b270SAlan Stern * which ports need attention. 10615e6effaeSAlan Stern */ 10625e6effaeSAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 1063d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 1064d99f6b41SDan Williams struct usb_device *udev = port_dev->child; 10655e6effaeSAlan Stern u16 portstatus, portchange; 10665e6effaeSAlan Stern 10676ee0b270SAlan Stern portstatus = portchange = 0; 10686ee0b270SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 10696ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 1070d99f6b41SDan Williams dev_dbg(&port_dev->dev, "status %04x change %04x\n", 1071d99f6b41SDan Williams portstatus, portchange); 10725e6effaeSAlan Stern 1073d99f6b41SDan Williams /* 1074d99f6b41SDan Williams * After anything other than HUB_RESUME (i.e., initialization 10756ee0b270SAlan Stern * or any sort of reset), every port should be disabled. 10766ee0b270SAlan Stern * Unconnected ports should likewise be disabled (paranoia), 10776ee0b270SAlan Stern * and so should ports for which we have no usb_device. 10785e6effaeSAlan Stern */ 10796ee0b270SAlan Stern if ((portstatus & USB_PORT_STAT_ENABLE) && ( 10806ee0b270SAlan Stern type != HUB_RESUME || 10816ee0b270SAlan Stern !(portstatus & USB_PORT_STAT_CONNECTION) || 10826ee0b270SAlan Stern !udev || 10836ee0b270SAlan Stern udev->state == USB_STATE_NOTATTACHED)) { 10849f0a6cd3SAndiry Xu /* 10859f0a6cd3SAndiry Xu * USB3 protocol ports will automatically transition 10869f0a6cd3SAndiry Xu * to Enabled state when detect an USB3.0 device attach. 1087fd1ac4cfSDan Williams * Do not disable USB3 protocol ports, just pretend 1088fd1ac4cfSDan Williams * power was lost 10899f0a6cd3SAndiry Xu */ 1090fd1ac4cfSDan Williams portstatus &= ~USB_PORT_STAT_ENABLE; 1091fd1ac4cfSDan Williams if (!hub_is_superspeed(hdev)) 1092ad493e5eSLan Tianyu usb_clear_port_feature(hdev, port1, 10939f0a6cd3SAndiry Xu USB_PORT_FEAT_ENABLE); 10949f0a6cd3SAndiry Xu } 10956ee0b270SAlan Stern 1096948fea37SAlan Stern /* Clear status-change flags; we'll debounce later */ 1097948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) { 1098948fea37SAlan Stern need_debounce_delay = true; 1099ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 1100948fea37SAlan Stern USB_PORT_FEAT_C_CONNECTION); 1101948fea37SAlan Stern } 1102948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) { 1103948fea37SAlan Stern need_debounce_delay = true; 1104ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 1105948fea37SAlan Stern USB_PORT_FEAT_C_ENABLE); 1106948fea37SAlan Stern } 1107e92aee33SJulius Werner if (portchange & USB_PORT_STAT_C_RESET) { 1108e92aee33SJulius Werner need_debounce_delay = true; 1109e92aee33SJulius Werner usb_clear_port_feature(hub->hdev, port1, 1110e92aee33SJulius Werner USB_PORT_FEAT_C_RESET); 1111e92aee33SJulius Werner } 111279c3dd81SDon Zickus if ((portchange & USB_PORT_STAT_C_BH_RESET) && 111379c3dd81SDon Zickus hub_is_superspeed(hub->hdev)) { 111479c3dd81SDon Zickus need_debounce_delay = true; 1115ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 111679c3dd81SDon Zickus USB_PORT_FEAT_C_BH_PORT_RESET); 111779c3dd81SDon Zickus } 1118253e0572SAlan Stern /* We can forget about a "removed" device when there's a 1119253e0572SAlan Stern * physical disconnect or the connect status changes. 1120253e0572SAlan Stern */ 1121253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 1122253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION)) 1123253e0572SAlan Stern clear_bit(port1, hub->removed_bits); 1124253e0572SAlan Stern 11256ee0b270SAlan Stern if (!udev || udev->state == USB_STATE_NOTATTACHED) { 112637ebb549SPetr Mladek /* Tell hub_wq to disconnect the device or 11276ee0b270SAlan Stern * check for a new connection 11286ee0b270SAlan Stern */ 112908d1dec6SShen Guang if (udev || (portstatus & USB_PORT_STAT_CONNECTION) || 113008d1dec6SShen Guang (portstatus & USB_PORT_STAT_OVERCURRENT)) 11316ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11326ee0b270SAlan Stern 11336ee0b270SAlan Stern } else if (portstatus & USB_PORT_STAT_ENABLE) { 113472937e1eSSarah Sharp bool port_resumed = (portstatus & 113572937e1eSSarah Sharp USB_PORT_STAT_LINK_STATE) == 113672937e1eSSarah Sharp USB_SS_PORT_LS_U0; 11376ee0b270SAlan Stern /* The power session apparently survived the resume. 11386ee0b270SAlan Stern * If there was an overcurrent or suspend change 113937ebb549SPetr Mladek * (i.e., remote wakeup request), have hub_wq 114072937e1eSSarah Sharp * take care of it. Look at the port link state 114172937e1eSSarah Sharp * for USB 3.0 hubs, since they don't have a suspend 114272937e1eSSarah Sharp * change bit, and they don't set the port link change 114372937e1eSSarah Sharp * bit on device-initiated resume. 11446ee0b270SAlan Stern */ 114572937e1eSSarah Sharp if (portchange || (hub_is_superspeed(hub->hdev) && 114672937e1eSSarah Sharp port_resumed)) 11476ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11486ee0b270SAlan Stern 11496ee0b270SAlan Stern } else if (udev->persist_enabled) { 11506ee0b270SAlan Stern #ifdef CONFIG_PM 11515e6effaeSAlan Stern udev->reset_resume = 1; 11526ee0b270SAlan Stern #endif 1153ad493e5eSLan Tianyu /* Don't set the change_bits when the device 1154ad493e5eSLan Tianyu * was powered off. 1155ad493e5eSLan Tianyu */ 1156d5c3834eSDan Williams if (test_bit(port1, hub->power_bits)) 11578808f00cSAlan Stern set_bit(port1, hub->change_bits); 11588808f00cSAlan Stern 11596ee0b270SAlan Stern } else { 116037ebb549SPetr Mladek /* The power session is gone; tell hub_wq */ 11616ee0b270SAlan Stern usb_set_device_state(udev, USB_STATE_NOTATTACHED); 11626ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11635e6effaeSAlan Stern } 11645e6effaeSAlan Stern } 11655e6effaeSAlan Stern 1166948fea37SAlan Stern /* If no port-status-change flags were set, we don't need any 1167948fea37SAlan Stern * debouncing. If flags were set we can try to debounce the 116837ebb549SPetr Mladek * ports all at once right now, instead of letting hub_wq do them 1169948fea37SAlan Stern * one at a time later on. 1170948fea37SAlan Stern * 117137ebb549SPetr Mladek * If any port-status changes do occur during this delay, hub_wq 1172948fea37SAlan Stern * will see them later and handle them normally. 1173948fea37SAlan Stern */ 11748520f380SAlan Stern if (need_debounce_delay) { 11758520f380SAlan Stern delay = HUB_DEBOUNCE_STABLE; 1176f2835219SAlan Stern 11778520f380SAlan Stern /* Don't do a long sleep inside a workqueue routine */ 11788520f380SAlan Stern if (type == HUB_INIT2) { 117977fa83cfSTejun Heo INIT_DELAYED_WORK(&hub->init_work, hub_init_func3); 118022f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq, 118122f6a0f0SShaibal Dutta &hub->init_work, 11828520f380SAlan Stern msecs_to_jiffies(delay)); 118307d316a2SAlan Stern device_unlock(&hdev->dev); 11848520f380SAlan Stern return; /* Continues at init3: below */ 11858520f380SAlan Stern } else { 11868520f380SAlan Stern msleep(delay); 11878520f380SAlan Stern } 11888520f380SAlan Stern } 11898520f380SAlan Stern init3: 1190f2835219SAlan Stern hub->quiescing = 0; 1191f2835219SAlan Stern 1192f2835219SAlan Stern status = usb_submit_urb(hub->urb, GFP_NOIO); 1193f2835219SAlan Stern if (status < 0) 1194f2835219SAlan Stern dev_err(hub->intfdev, "activate --> %d\n", status); 1195f2835219SAlan Stern if (hub->has_indicators && blinkenlights) 119622f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq, 119722f6a0f0SShaibal Dutta &hub->leds, LED_CYCLE_PERIOD); 1198f2835219SAlan Stern 1199f2835219SAlan Stern /* Scan all ports that need attention */ 120032a69589SPetr Mladek kick_hub_wq(hub); 12018e4ceb38SAlan Stern 1202ca5cbc8bSAlan Stern if (type == HUB_INIT2 || type == HUB_INIT3) { 12038e4ceb38SAlan Stern /* Allow autosuspend if it was suppressed */ 1204ca5cbc8bSAlan Stern disconnected: 12058e4ceb38SAlan Stern usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); 120607d316a2SAlan Stern device_unlock(&hdev->dev); 1207ca5cbc8bSAlan Stern } 1208e50293efSAlan Stern 1209e50293efSAlan Stern kref_put(&hub->kref, hub_release); 12105e6effaeSAlan Stern } 12115e6effaeSAlan Stern 12128520f380SAlan Stern /* Implement the continuations for the delays above */ 12138520f380SAlan Stern static void hub_init_func2(struct work_struct *ws) 12148520f380SAlan Stern { 12158520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 12168520f380SAlan Stern 12178520f380SAlan Stern hub_activate(hub, HUB_INIT2); 12188520f380SAlan Stern } 12198520f380SAlan Stern 12208520f380SAlan Stern static void hub_init_func3(struct work_struct *ws) 12218520f380SAlan Stern { 12228520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 12238520f380SAlan Stern 12248520f380SAlan Stern hub_activate(hub, HUB_INIT3); 12258520f380SAlan Stern } 12268520f380SAlan Stern 12274330354fSAlan Stern enum hub_quiescing_type { 12284330354fSAlan Stern HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND 12294330354fSAlan Stern }; 12304330354fSAlan Stern 12314330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) 12324330354fSAlan Stern { 12334330354fSAlan Stern struct usb_device *hdev = hub->hdev; 12344330354fSAlan Stern int i; 12354330354fSAlan Stern 123637ebb549SPetr Mladek /* hub_wq and related activity won't re-trigger */ 12374330354fSAlan Stern hub->quiescing = 1; 12384330354fSAlan Stern 12394330354fSAlan Stern if (type != HUB_SUSPEND) { 12404330354fSAlan Stern /* Disconnect all the children */ 12414330354fSAlan Stern for (i = 0; i < hdev->maxchild; ++i) { 1242ff823c79SLan Tianyu if (hub->ports[i]->child) 1243ff823c79SLan Tianyu usb_disconnect(&hub->ports[i]->child); 12444330354fSAlan Stern } 12454330354fSAlan Stern } 12464330354fSAlan Stern 124737ebb549SPetr Mladek /* Stop hub_wq and related activity */ 12484330354fSAlan Stern usb_kill_urb(hub->urb); 12494330354fSAlan Stern if (hub->has_indicators) 12504330354fSAlan Stern cancel_delayed_work_sync(&hub->leds); 12514330354fSAlan Stern if (hub->tt.hub) 1252036546bfSOctavian Purdila flush_work(&hub->tt.clear_work); 12534330354fSAlan Stern } 12544330354fSAlan Stern 1255600856c2SAlan Stern static void hub_pm_barrier_for_all_ports(struct usb_hub *hub) 1256600856c2SAlan Stern { 1257600856c2SAlan Stern int i; 1258600856c2SAlan Stern 1259600856c2SAlan Stern for (i = 0; i < hub->hdev->maxchild; ++i) 1260600856c2SAlan Stern pm_runtime_barrier(&hub->ports[i]->dev); 1261600856c2SAlan Stern } 1262600856c2SAlan Stern 12633eb14915SAlan Stern /* caller has locked the hub device */ 12643eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf) 12653eb14915SAlan Stern { 12663eb14915SAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 12673eb14915SAlan Stern 12684330354fSAlan Stern hub_quiesce(hub, HUB_PRE_RESET); 1269600856c2SAlan Stern hub->in_reset = 1; 1270600856c2SAlan Stern hub_pm_barrier_for_all_ports(hub); 1271f07600cfSAlan Stern return 0; 12727d069b7dSAlan Stern } 12737d069b7dSAlan Stern 12747d069b7dSAlan Stern /* caller has locked the hub device */ 1275f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf) 12767d069b7dSAlan Stern { 12777de18d8bSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 12787de18d8bSAlan Stern 1279600856c2SAlan Stern hub->in_reset = 0; 1280600856c2SAlan Stern hub_pm_barrier_for_all_ports(hub); 1281f2835219SAlan Stern hub_activate(hub, HUB_POST_RESET); 1282f07600cfSAlan Stern return 0; 12837d069b7dSAlan Stern } 12847d069b7dSAlan Stern 12851da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub, 12861da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint) 12871da177e4SLinus Torvalds { 1288b356b7c7SSarah Sharp struct usb_hcd *hcd; 12891da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 12901da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 12911da177e4SLinus Torvalds u16 hubstatus, hubchange; 129274ad9bd2SGreg Kroah-Hartman u16 wHubCharacteristics; 12931da177e4SLinus Torvalds unsigned int pipe; 1294fa2a9566SLan Tianyu int maxp, ret, i; 12957cbe5dcaSAlan Stern char *message = "out of memory"; 1296430ee58eSSebastian Andrzej Siewior unsigned unit_load; 1297430ee58eSSebastian Andrzej Siewior unsigned full_load; 1298d8521afeSDan Williams unsigned maxchild; 12991da177e4SLinus Torvalds 1300d697cddaSAlan Stern hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); 13011da177e4SLinus Torvalds if (!hub->buffer) { 13021da177e4SLinus Torvalds ret = -ENOMEM; 13031da177e4SLinus Torvalds goto fail; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 13071da177e4SLinus Torvalds if (!hub->status) { 13081da177e4SLinus Torvalds ret = -ENOMEM; 13091da177e4SLinus Torvalds goto fail; 13101da177e4SLinus Torvalds } 1311db90e7a1SAlan Stern mutex_init(&hub->status_mutex); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); 13141da177e4SLinus Torvalds if (!hub->descriptor) { 13151da177e4SLinus Torvalds ret = -ENOMEM; 13161da177e4SLinus Torvalds goto fail; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds /* Request the entire hub descriptor. 13201da177e4SLinus Torvalds * hub->descriptor can handle USB_MAXCHILDREN ports, 13211da177e4SLinus Torvalds * but the hub can/will return fewer bytes here. 13221da177e4SLinus Torvalds */ 1323dbe79bbeSJohn Youn ret = get_hub_descriptor(hdev, hub->descriptor); 13241da177e4SLinus Torvalds if (ret < 0) { 13251da177e4SLinus Torvalds message = "can't read hub descriptor"; 13261da177e4SLinus Torvalds goto fail; 13271da177e4SLinus Torvalds } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { 13281da177e4SLinus Torvalds message = "hub has too many ports!"; 13291da177e4SLinus Torvalds ret = -ENODEV; 13301da177e4SLinus Torvalds goto fail; 1331769d7368SDavid Linares } else if (hub->descriptor->bNbrPorts == 0) { 1332769d7368SDavid Linares message = "hub doesn't have any ports!"; 1333769d7368SDavid Linares ret = -ENODEV; 1334769d7368SDavid Linares goto fail; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 1337d8521afeSDan Williams maxchild = hub->descriptor->bNbrPorts; 1338d8521afeSDan Williams dev_info(hub_dev, "%d port%s detected\n", maxchild, 1339d8521afeSDan Williams (maxchild == 1) ? "" : "s"); 13401da177e4SLinus Torvalds 1341d8521afeSDan Williams hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL); 1342ff823c79SLan Tianyu if (!hub->ports) { 13437cbe5dcaSAlan Stern ret = -ENOMEM; 13447cbe5dcaSAlan Stern goto fail; 13457cbe5dcaSAlan Stern } 13467cbe5dcaSAlan Stern 134774ad9bd2SGreg Kroah-Hartman wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 1348430ee58eSSebastian Andrzej Siewior if (hub_is_superspeed(hdev)) { 1349430ee58eSSebastian Andrzej Siewior unit_load = 150; 1350430ee58eSSebastian Andrzej Siewior full_load = 900; 1351430ee58eSSebastian Andrzej Siewior } else { 1352430ee58eSSebastian Andrzej Siewior unit_load = 100; 1353430ee58eSSebastian Andrzej Siewior full_load = 500; 1354430ee58eSSebastian Andrzej Siewior } 13551da177e4SLinus Torvalds 1356dbe79bbeSJohn Youn /* FIXME for USB 3.0, skip for now */ 1357dbe79bbeSJohn Youn if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && 1358dbe79bbeSJohn Youn !(hub_is_superspeed(hdev))) { 13591da177e4SLinus Torvalds char portstr[USB_MAXCHILDREN + 1]; 13601da177e4SLinus Torvalds 1361d8521afeSDan Williams for (i = 0; i < maxchild; i++) 1362dbe79bbeSJohn Youn portstr[i] = hub->descriptor->u.hs.DeviceRemovable 13631da177e4SLinus Torvalds [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 13641da177e4SLinus Torvalds ? 'F' : 'R'; 1365d8521afeSDan Williams portstr[maxchild] = 0; 13661da177e4SLinus Torvalds dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 13671da177e4SLinus Torvalds } else 13681da177e4SLinus Torvalds dev_dbg(hub_dev, "standalone hub\n"); 13691da177e4SLinus Torvalds 137074ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_LPSM) { 13717bf01185SAman Deep case HUB_CHAR_COMMON_LPSM: 13721da177e4SLinus Torvalds dev_dbg(hub_dev, "ganged power switching\n"); 13731da177e4SLinus Torvalds break; 13747bf01185SAman Deep case HUB_CHAR_INDV_PORT_LPSM: 13751da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port power switching\n"); 13761da177e4SLinus Torvalds break; 13777bf01185SAman Deep case HUB_CHAR_NO_LPSM: 13787bf01185SAman Deep case HUB_CHAR_LPSM: 13791da177e4SLinus Torvalds dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 13801da177e4SLinus Torvalds break; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 138374ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_OCPM) { 13847bf01185SAman Deep case HUB_CHAR_COMMON_OCPM: 13851da177e4SLinus Torvalds dev_dbg(hub_dev, "global over-current protection\n"); 13861da177e4SLinus Torvalds break; 13877bf01185SAman Deep case HUB_CHAR_INDV_PORT_OCPM: 13881da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port over-current protection\n"); 13891da177e4SLinus Torvalds break; 13907bf01185SAman Deep case HUB_CHAR_NO_OCPM: 13917bf01185SAman Deep case HUB_CHAR_OCPM: 13921da177e4SLinus Torvalds dev_dbg(hub_dev, "no over-current protection\n"); 13931da177e4SLinus Torvalds break; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds spin_lock_init(&hub->tt.lock); 13971da177e4SLinus Torvalds INIT_LIST_HEAD(&hub->tt.clear_list); 1398cb88a1b8SAlan Stern INIT_WORK(&hub->tt.clear_work, hub_tt_work); 13991da177e4SLinus Torvalds switch (hdev->descriptor.bDeviceProtocol) { 14007bf01185SAman Deep case USB_HUB_PR_FS: 14011da177e4SLinus Torvalds break; 14027bf01185SAman Deep case USB_HUB_PR_HS_SINGLE_TT: 14031da177e4SLinus Torvalds dev_dbg(hub_dev, "Single TT\n"); 14041da177e4SLinus Torvalds hub->tt.hub = hdev; 14051da177e4SLinus Torvalds break; 14067bf01185SAman Deep case USB_HUB_PR_HS_MULTI_TT: 14071da177e4SLinus Torvalds ret = usb_set_interface(hdev, 0, 1); 14081da177e4SLinus Torvalds if (ret == 0) { 14091da177e4SLinus Torvalds dev_dbg(hub_dev, "TT per port\n"); 14101da177e4SLinus Torvalds hub->tt.multi = 1; 14111da177e4SLinus Torvalds } else 14121da177e4SLinus Torvalds dev_err(hub_dev, "Using single TT (err %d)\n", 14131da177e4SLinus Torvalds ret); 14141da177e4SLinus Torvalds hub->tt.hub = hdev; 14151da177e4SLinus Torvalds break; 14167bf01185SAman Deep case USB_HUB_PR_SS: 1417d2e9b4d6SSarah Sharp /* USB 3.0 hubs don't have a TT */ 1418d2e9b4d6SSarah Sharp break; 14191da177e4SLinus Torvalds default: 14201da177e4SLinus Torvalds dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 14211da177e4SLinus Torvalds hdev->descriptor.bDeviceProtocol); 14221da177e4SLinus Torvalds break; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 1425e09711aeSdavid-b@pacbell.net /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 142674ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_TTTT) { 1427e09711aeSdavid-b@pacbell.net case HUB_TTTT_8_BITS: 1428e09711aeSdavid-b@pacbell.net if (hdev->descriptor.bDeviceProtocol != 0) { 1429e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666; 1430e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1431e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1432e09711aeSdavid-b@pacbell.net 8, hub->tt.think_time); 1433e09711aeSdavid-b@pacbell.net } 14341da177e4SLinus Torvalds break; 1435e09711aeSdavid-b@pacbell.net case HUB_TTTT_16_BITS: 1436e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 2; 1437e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1438e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1439e09711aeSdavid-b@pacbell.net 16, hub->tt.think_time); 14401da177e4SLinus Torvalds break; 1441e09711aeSdavid-b@pacbell.net case HUB_TTTT_24_BITS: 1442e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 3; 1443e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1444e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1445e09711aeSdavid-b@pacbell.net 24, hub->tt.think_time); 14461da177e4SLinus Torvalds break; 1447e09711aeSdavid-b@pacbell.net case HUB_TTTT_32_BITS: 1448e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 4; 1449e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1450e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1451e09711aeSdavid-b@pacbell.net 32, hub->tt.think_time); 14521da177e4SLinus Torvalds break; 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds /* probe() zeroes hub->indicator[] */ 145674ad9bd2SGreg Kroah-Hartman if (wHubCharacteristics & HUB_CHAR_PORTIND) { 14571da177e4SLinus Torvalds hub->has_indicators = 1; 14581da177e4SLinus Torvalds dev_dbg(hub_dev, "Port indicators are supported\n"); 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds dev_dbg(hub_dev, "power on to power good time: %dms\n", 14621da177e4SLinus Torvalds hub->descriptor->bPwrOn2PwrGood * 2); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds /* power budgeting mostly matters with bus-powered hubs, 14651da177e4SLinus Torvalds * and battery-powered root hubs (may provide just 8 mA). 14661da177e4SLinus Torvalds */ 14671da177e4SLinus Torvalds ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 146815b7336eSAlan Stern if (ret) { 14691da177e4SLinus Torvalds message = "can't get hub status"; 14701da177e4SLinus Torvalds goto fail; 14711da177e4SLinus Torvalds } 1472430ee58eSSebastian Andrzej Siewior hcd = bus_to_hcd(hdev->bus); 14737d35b929SAlan Stern if (hdev == hdev->bus->root_hub) { 1474430ee58eSSebastian Andrzej Siewior if (hcd->power_budget > 0) 1475430ee58eSSebastian Andrzej Siewior hdev->bus_mA = hcd->power_budget; 1476430ee58eSSebastian Andrzej Siewior else 1477d8521afeSDan Williams hdev->bus_mA = full_load * maxchild; 1478430ee58eSSebastian Andrzej Siewior if (hdev->bus_mA >= full_load) 1479430ee58eSSebastian Andrzej Siewior hub->mA_per_port = full_load; 148055c52718SAlan Stern else { 148155c52718SAlan Stern hub->mA_per_port = hdev->bus_mA; 148255c52718SAlan Stern hub->limited_power = 1; 148355c52718SAlan Stern } 14847d35b929SAlan Stern } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 148555c52718SAlan Stern int remaining = hdev->bus_mA - 148655c52718SAlan Stern hub->descriptor->bHubContrCurrent; 14871da177e4SLinus Torvalds 1488430ee58eSSebastian Andrzej Siewior dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 1489430ee58eSSebastian Andrzej Siewior hub->descriptor->bHubContrCurrent); 1490430ee58eSSebastian Andrzej Siewior hub->limited_power = 1; 1491430ee58eSSebastian Andrzej Siewior 1492d8521afeSDan Williams if (remaining < maxchild * unit_load) 149355c52718SAlan Stern dev_warn(hub_dev, 149455c52718SAlan Stern "insufficient power available " 149555c52718SAlan Stern "to use all downstream ports\n"); 1496430ee58eSSebastian Andrzej Siewior hub->mA_per_port = unit_load; /* 7.2.1 */ 1497430ee58eSSebastian Andrzej Siewior 149855c52718SAlan Stern } else { /* Self-powered external hub */ 149955c52718SAlan Stern /* FIXME: What about battery-powered external hubs that 150055c52718SAlan Stern * provide less current per port? */ 1501430ee58eSSebastian Andrzej Siewior hub->mA_per_port = full_load; 150255c52718SAlan Stern } 1503430ee58eSSebastian Andrzej Siewior if (hub->mA_per_port < full_load) 150455c52718SAlan Stern dev_dbg(hub_dev, "%umA bus power budget for each child\n", 150555c52718SAlan Stern hub->mA_per_port); 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds ret = hub_hub_status(hub, &hubstatus, &hubchange); 15081da177e4SLinus Torvalds if (ret < 0) { 15091da177e4SLinus Torvalds message = "can't get hub status"; 15101da177e4SLinus Torvalds goto fail; 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds /* local power status reports aren't always correct */ 15141da177e4SLinus Torvalds if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 15151da177e4SLinus Torvalds dev_dbg(hub_dev, "local power source is %s\n", 15161da177e4SLinus Torvalds (hubstatus & HUB_STATUS_LOCAL_POWER) 15171da177e4SLinus Torvalds ? "lost (inactive)" : "good"); 15181da177e4SLinus Torvalds 151974ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 15201da177e4SLinus Torvalds dev_dbg(hub_dev, "%sover-current condition exists\n", 15211da177e4SLinus Torvalds (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 15221da177e4SLinus Torvalds 152388fafff9Sinaky@linux.intel.com /* set up the interrupt endpoint 152488fafff9Sinaky@linux.intel.com * We use the EP's maxpacket size instead of (PORTS+1+7)/8 152588fafff9Sinaky@linux.intel.com * bytes as USB2.0[11.12.3] says because some hubs are known 152688fafff9Sinaky@linux.intel.com * to send more data (and thus cause overflow). For root hubs, 152788fafff9Sinaky@linux.intel.com * maxpktsize is defined in hcd.c's fake endpoint descriptors 152888fafff9Sinaky@linux.intel.com * to be big enough for at least USB_MAXCHILDREN ports. */ 15291da177e4SLinus Torvalds pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 15301da177e4SLinus Torvalds maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds if (maxp > sizeof(*hub->buffer)) 15331da177e4SLinus Torvalds maxp = sizeof(*hub->buffer); 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds hub->urb = usb_alloc_urb(0, GFP_KERNEL); 15361da177e4SLinus Torvalds if (!hub->urb) { 15371da177e4SLinus Torvalds ret = -ENOMEM; 15381da177e4SLinus Torvalds goto fail; 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 15421da177e4SLinus Torvalds hub, endpoint->bInterval); 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds /* maybe cycle the hub leds */ 15451da177e4SLinus Torvalds if (hub->has_indicators && blinkenlights) 15461da177e4SLinus Torvalds hub->indicator[0] = INDICATOR_CYCLE; 15471da177e4SLinus Torvalds 1548d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex); 1549d8521afeSDan Williams for (i = 0; i < maxchild; i++) { 1550e58547ebSKrzysztof Mazur ret = usb_hub_create_port_device(hub, i + 1); 1551e58547ebSKrzysztof Mazur if (ret < 0) { 1552fa2a9566SLan Tianyu dev_err(hub->intfdev, 1553fa2a9566SLan Tianyu "couldn't create port%d device.\n", i + 1); 1554d8521afeSDan Williams break; 1555d8521afeSDan Williams } 1556d8521afeSDan Williams } 1557e58547ebSKrzysztof Mazur hdev->maxchild = i; 1558e3d10505SDan Williams for (i = 0; i < hdev->maxchild; i++) { 1559e3d10505SDan Williams struct usb_port *port_dev = hub->ports[i]; 1560e3d10505SDan Williams 1561e3d10505SDan Williams pm_runtime_put(&port_dev->dev); 1562e3d10505SDan Williams } 1563e3d10505SDan Williams 1564d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex); 1565d8521afeSDan Williams if (ret < 0) 1566d8521afeSDan Williams goto fail; 1567fa2a9566SLan Tianyu 156837ebb549SPetr Mladek /* Update the HCD's internal representation of this hub before hub_wq 1569e3d95580SDan Williams * starts getting port status changes for devices under the hub. 1570e3d95580SDan Williams */ 1571e3d95580SDan Williams if (hcd->driver->update_hub_device) { 1572e3d95580SDan Williams ret = hcd->driver->update_hub_device(hcd, hdev, 1573e3d95580SDan Williams &hub->tt, GFP_KERNEL); 1574e3d95580SDan Williams if (ret < 0) { 1575e3d95580SDan Williams message = "can't update HCD hub info"; 1576e3d95580SDan Williams goto fail; 1577e3d95580SDan Williams } 1578e3d95580SDan Williams } 1579e3d95580SDan Williams 1580d2123fd9SLan Tianyu usb_hub_adjust_deviceremovable(hdev, hub->descriptor); 1581d2123fd9SLan Tianyu 1582f2835219SAlan Stern hub_activate(hub, HUB_INIT); 15831da177e4SLinus Torvalds return 0; 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds fail: 15861da177e4SLinus Torvalds dev_err(hub_dev, "config failed, %s (err %d)\n", 15871da177e4SLinus Torvalds message, ret); 15881da177e4SLinus Torvalds /* hub_disconnect() frees urb and descriptor */ 15891da177e4SLinus Torvalds return ret; 15901da177e4SLinus Torvalds } 15911da177e4SLinus Torvalds 1592e8054854SAlan Stern static void hub_release(struct kref *kref) 1593e8054854SAlan Stern { 1594e8054854SAlan Stern struct usb_hub *hub = container_of(kref, struct usb_hub, kref); 1595e8054854SAlan Stern 15965d14f323SPetr Mladek usb_put_dev(hub->hdev); 1597e8054854SAlan Stern usb_put_intf(to_usb_interface(hub->intfdev)); 1598e8054854SAlan Stern kfree(hub); 1599e8054854SAlan Stern } 1600e8054854SAlan Stern 16011da177e4SLinus Torvalds static unsigned highspeed_hubs; 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 1606fa286188SGreg Kroah-Hartman struct usb_device *hdev = interface_to_usbdev(intf); 1607543d7784SAlan Stern int port1; 1608fa2a9566SLan Tianyu 160932a69589SPetr Mladek /* 161032a69589SPetr Mladek * Stop adding new hub events. We do not want to block here and thus 161132a69589SPetr Mladek * will not try to remove any pending work item. 161232a69589SPetr Mladek */ 1613e8054854SAlan Stern hub->disconnected = 1; 16141da177e4SLinus Torvalds 16157de18d8bSAlan Stern /* Disconnect all children and quiesce the hub */ 16167de18d8bSAlan Stern hub->error = 0; 16174330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 16187de18d8bSAlan Stern 1619d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex); 1620d8521afeSDan Williams 1621543d7784SAlan Stern /* Avoid races with recursively_mark_NOTATTACHED() */ 1622543d7784SAlan Stern spin_lock_irq(&device_state_lock); 1623543d7784SAlan Stern port1 = hdev->maxchild; 1624543d7784SAlan Stern hdev->maxchild = 0; 16258b28c752SAlan Stern usb_set_intfdata(intf, NULL); 1626543d7784SAlan Stern spin_unlock_irq(&device_state_lock); 16271f2235b8SAlexander Shishkin 1628543d7784SAlan Stern for (; port1 > 0; --port1) 1629543d7784SAlan Stern usb_hub_remove_port_device(hub, port1); 16301da177e4SLinus Torvalds 1631d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex); 1632d8521afeSDan Williams 1633e8054854SAlan Stern if (hub->hdev->speed == USB_SPEED_HIGH) 16341da177e4SLinus Torvalds highspeed_hubs--; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds usb_free_urb(hub->urb); 1637fa2a9566SLan Tianyu kfree(hub->ports); 16381da177e4SLinus Torvalds kfree(hub->descriptor); 16391da177e4SLinus Torvalds kfree(hub->status); 1640d697cddaSAlan Stern kfree(hub->buffer); 16411da177e4SLinus Torvalds 1642971fcd49SLan Tianyu pm_suspend_ignore_children(&intf->dev, false); 1643e8054854SAlan Stern kref_put(&hub->kref, hub_release); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 16471da177e4SLinus Torvalds { 16481da177e4SLinus Torvalds struct usb_host_interface *desc; 16491da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint; 16501da177e4SLinus Torvalds struct usb_device *hdev; 16511da177e4SLinus Torvalds struct usb_hub *hub; 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds desc = intf->cur_altsetting; 16541da177e4SLinus Torvalds hdev = interface_to_usbdev(intf); 16551da177e4SLinus Torvalds 1656596d789aSMing Lei /* 1657596d789aSMing Lei * Set default autosuspend delay as 0 to speedup bus suspend, 1658596d789aSMing Lei * based on the below considerations: 1659596d789aSMing Lei * 1660596d789aSMing Lei * - Unlike other drivers, the hub driver does not rely on the 1661596d789aSMing Lei * autosuspend delay to provide enough time to handle a wakeup 1662596d789aSMing Lei * event, and the submitted status URB is just to check future 1663596d789aSMing Lei * change on hub downstream ports, so it is safe to do it. 1664596d789aSMing Lei * 1665596d789aSMing Lei * - The patch might cause one or more auto supend/resume for 1666596d789aSMing Lei * below very rare devices when they are plugged into hub 1667596d789aSMing Lei * first time: 1668596d789aSMing Lei * 1669596d789aSMing Lei * devices having trouble initializing, and disconnect 1670596d789aSMing Lei * themselves from the bus and then reconnect a second 1671596d789aSMing Lei * or so later 1672596d789aSMing Lei * 1673596d789aSMing Lei * devices just for downloading firmware, and disconnects 1674596d789aSMing Lei * themselves after completing it 1675596d789aSMing Lei * 1676596d789aSMing Lei * For these quite rare devices, their drivers may change the 1677596d789aSMing Lei * autosuspend delay of their parent hub in the probe() to one 1678596d789aSMing Lei * appropriate value to avoid the subtle problem if someone 1679596d789aSMing Lei * does care it. 1680596d789aSMing Lei * 1681596d789aSMing Lei * - The patch may cause one or more auto suspend/resume on 1682596d789aSMing Lei * hub during running 'lsusb', but it is probably too 1683596d789aSMing Lei * infrequent to worry about. 1684596d789aSMing Lei * 1685596d789aSMing Lei * - Change autosuspend delay of hub can avoid unnecessary auto 1686596d789aSMing Lei * suspend timer for hub, also may decrease power consumption 1687596d789aSMing Lei * of USB bus. 1688bdd405d2SRoger Quadros * 1689bdd405d2SRoger Quadros * - If user has indicated to prevent autosuspend by passing 1690bdd405d2SRoger Quadros * usbcore.autosuspend = -1 then keep autosuspend disabled. 1691596d789aSMing Lei */ 1692ceb6c9c8SRafael J. Wysocki #ifdef CONFIG_PM 1693bdd405d2SRoger Quadros if (hdev->dev.power.autosuspend_delay >= 0) 1694596d789aSMing Lei pm_runtime_set_autosuspend_delay(&hdev->dev, 0); 1695a9ef803dSGreg Kroah-Hartman #endif 1696596d789aSMing Lei 16978ef42dddSAlan Stern /* 16988ef42dddSAlan Stern * Hubs have proper suspend/resume support, except for root hubs 16998ef42dddSAlan Stern * where the controller driver doesn't have bus_suspend and 17008ef42dddSAlan Stern * bus_resume methods. 17018ef42dddSAlan Stern */ 17028ef42dddSAlan Stern if (hdev->parent) { /* normal device */ 1703088f7fecSAlan Stern usb_enable_autosuspend(hdev); 17048ef42dddSAlan Stern } else { /* root hub */ 17058ef42dddSAlan Stern const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; 17068ef42dddSAlan Stern 17078ef42dddSAlan Stern if (drv->bus_suspend && drv->bus_resume) 17088ef42dddSAlan Stern usb_enable_autosuspend(hdev); 17098ef42dddSAlan Stern } 1710088f7fecSAlan Stern 171138f3ad5eSFelipe Balbi if (hdev->level == MAX_TOPO_LEVEL) { 1712b9cef6c3SWu Fengguang dev_err(&intf->dev, 1713b9cef6c3SWu Fengguang "Unsupported bus topology: hub nested too deep\n"); 171438f3ad5eSFelipe Balbi return -E2BIG; 171538f3ad5eSFelipe Balbi } 171638f3ad5eSFelipe Balbi 171789ccbdc9SDavid Brownell #ifdef CONFIG_USB_OTG_BLACKLIST_HUB 171889ccbdc9SDavid Brownell if (hdev->parent) { 171989ccbdc9SDavid Brownell dev_warn(&intf->dev, "ignoring external hub\n"); 172089ccbdc9SDavid Brownell return -ENODEV; 172189ccbdc9SDavid Brownell } 172289ccbdc9SDavid Brownell #endif 172389ccbdc9SDavid Brownell 17241da177e4SLinus Torvalds /* Some hubs have a subclass of 1, which AFAICT according to the */ 17251da177e4SLinus Torvalds /* specs is not defined, but it works */ 17261da177e4SLinus Torvalds if ((desc->desc.bInterfaceSubClass != 0) && 17271da177e4SLinus Torvalds (desc->desc.bInterfaceSubClass != 1)) { 17281da177e4SLinus Torvalds descriptor_error: 17291da177e4SLinus Torvalds dev_err(&intf->dev, "bad descriptor, ignoring hub\n"); 17301da177e4SLinus Torvalds return -EIO; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 17341da177e4SLinus Torvalds if (desc->desc.bNumEndpoints != 1) 17351da177e4SLinus Torvalds goto descriptor_error; 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds endpoint = &desc->endpoint[0].desc; 17381da177e4SLinus Torvalds 1739fbf81c29SLuiz Fernando N. Capitulino /* If it's not an interrupt in endpoint, we'd better punt! */ 1740fbf81c29SLuiz Fernando N. Capitulino if (!usb_endpoint_is_int_in(endpoint)) 17411da177e4SLinus Torvalds goto descriptor_error; 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* We found a hub */ 17441da177e4SLinus Torvalds dev_info(&intf->dev, "USB hub found\n"); 17451da177e4SLinus Torvalds 17460a1ef3b5SAlan Stern hub = kzalloc(sizeof(*hub), GFP_KERNEL); 1747b74e7062SWolfram Sang if (!hub) 17481da177e4SLinus Torvalds return -ENOMEM; 17491da177e4SLinus Torvalds 1750e8054854SAlan Stern kref_init(&hub->kref); 17511da177e4SLinus Torvalds hub->intfdev = &intf->dev; 17521da177e4SLinus Torvalds hub->hdev = hdev; 1753c4028958SDavid Howells INIT_DELAYED_WORK(&hub->leds, led_work); 17548520f380SAlan Stern INIT_DELAYED_WORK(&hub->init_work, NULL); 175532a69589SPetr Mladek INIT_WORK(&hub->events, hub_event); 1756e8054854SAlan Stern usb_get_intf(intf); 17575d14f323SPetr Mladek usb_get_dev(hdev); 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds usb_set_intfdata(intf, hub); 176040f122f3SAlan Stern intf->needs_remote_wakeup = 1; 1761971fcd49SLan Tianyu pm_suspend_ignore_children(&intf->dev, true); 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds if (hdev->speed == USB_SPEED_HIGH) 17641da177e4SLinus Torvalds highspeed_hubs++; 17651da177e4SLinus Torvalds 1766e6f30deaSMing Lei if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) 1767e6f30deaSMing Lei hub->quirk_check_port_auto_suspend = 1; 1768e6f30deaSMing Lei 17691da177e4SLinus Torvalds if (hub_configure(hub, endpoint) >= 0) 17701da177e4SLinus Torvalds return 0; 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds hub_disconnect(intf); 17731da177e4SLinus Torvalds return -ENODEV; 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds static int 17771da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 17781da177e4SLinus Torvalds { 17791da177e4SLinus Torvalds struct usb_device *hdev = interface_to_usbdev(intf); 1780ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds /* assert ifno == 0 (part of hub spec) */ 17831da177e4SLinus Torvalds switch (code) { 17841da177e4SLinus Torvalds case USBDEVFS_HUB_PORTINFO: { 17851da177e4SLinus Torvalds struct usbdevfs_hub_portinfo *info = user_data; 17861da177e4SLinus Torvalds int i; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 17891da177e4SLinus Torvalds if (hdev->devnum <= 0) 17901da177e4SLinus Torvalds info->nports = 0; 17911da177e4SLinus Torvalds else { 17921da177e4SLinus Torvalds info->nports = hdev->maxchild; 17931da177e4SLinus Torvalds for (i = 0; i < info->nports; i++) { 1794ff823c79SLan Tianyu if (hub->ports[i]->child == NULL) 17951da177e4SLinus Torvalds info->port[i] = 0; 17961da177e4SLinus Torvalds else 17971da177e4SLinus Torvalds info->port[i] = 1798ff823c79SLan Tianyu hub->ports[i]->child->devnum; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds return info->nports + 1; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds default: 18071da177e4SLinus Torvalds return -ENOSYS; 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds 18117cbe5dcaSAlan Stern /* 18127cbe5dcaSAlan Stern * Allow user programs to claim ports on a hub. When a device is attached 18137cbe5dcaSAlan Stern * to one of these "claimed" ports, the program will "own" the device. 18147cbe5dcaSAlan Stern */ 18157cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1, 18169b6f0c4bSValentina Manea struct usb_dev_state ***ppowner) 18177cbe5dcaSAlan Stern { 181841341261SMathias Nyman struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 181941341261SMathias Nyman 18207cbe5dcaSAlan Stern if (hdev->state == USB_STATE_NOTATTACHED) 18217cbe5dcaSAlan Stern return -ENODEV; 18227cbe5dcaSAlan Stern if (port1 == 0 || port1 > hdev->maxchild) 18237cbe5dcaSAlan Stern return -EINVAL; 18247cbe5dcaSAlan Stern 182541341261SMathias Nyman /* Devices not managed by the hub driver 18267cbe5dcaSAlan Stern * will always have maxchild equal to 0. 18277cbe5dcaSAlan Stern */ 182841341261SMathias Nyman *ppowner = &(hub->ports[port1 - 1]->port_owner); 18297cbe5dcaSAlan Stern return 0; 18307cbe5dcaSAlan Stern } 18317cbe5dcaSAlan Stern 18327cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */ 1833336c5c31SLan Tianyu int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 18349b6f0c4bSValentina Manea struct usb_dev_state *owner) 18357cbe5dcaSAlan Stern { 18367cbe5dcaSAlan Stern int rc; 18379b6f0c4bSValentina Manea struct usb_dev_state **powner; 18387cbe5dcaSAlan Stern 18397cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner); 18407cbe5dcaSAlan Stern if (rc) 18417cbe5dcaSAlan Stern return rc; 18427cbe5dcaSAlan Stern if (*powner) 18437cbe5dcaSAlan Stern return -EBUSY; 18447cbe5dcaSAlan Stern *powner = owner; 18457cbe5dcaSAlan Stern return rc; 18467cbe5dcaSAlan Stern } 18476080cd0eSValentina Manea EXPORT_SYMBOL_GPL(usb_hub_claim_port); 18487cbe5dcaSAlan Stern 1849336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 18509b6f0c4bSValentina Manea struct usb_dev_state *owner) 18517cbe5dcaSAlan Stern { 18527cbe5dcaSAlan Stern int rc; 18539b6f0c4bSValentina Manea struct usb_dev_state **powner; 18547cbe5dcaSAlan Stern 18557cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner); 18567cbe5dcaSAlan Stern if (rc) 18577cbe5dcaSAlan Stern return rc; 18587cbe5dcaSAlan Stern if (*powner != owner) 18597cbe5dcaSAlan Stern return -ENOENT; 18607cbe5dcaSAlan Stern *powner = NULL; 18617cbe5dcaSAlan Stern return rc; 18627cbe5dcaSAlan Stern } 18636080cd0eSValentina Manea EXPORT_SYMBOL_GPL(usb_hub_release_port); 18647cbe5dcaSAlan Stern 18659b6f0c4bSValentina Manea void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner) 18667cbe5dcaSAlan Stern { 1867ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 18687cbe5dcaSAlan Stern int n; 18697cbe5dcaSAlan Stern 1870fa2a9566SLan Tianyu for (n = 0; n < hdev->maxchild; n++) { 1871fa2a9566SLan Tianyu if (hub->ports[n]->port_owner == owner) 1872fa2a9566SLan Tianyu hub->ports[n]->port_owner = NULL; 18737cbe5dcaSAlan Stern } 1874fa2a9566SLan Tianyu 18757cbe5dcaSAlan Stern } 18767cbe5dcaSAlan Stern 18777cbe5dcaSAlan Stern /* The caller must hold udev's lock */ 18787cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev) 18797cbe5dcaSAlan Stern { 18807cbe5dcaSAlan Stern struct usb_hub *hub; 18817cbe5dcaSAlan Stern 18827cbe5dcaSAlan Stern if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) 18837cbe5dcaSAlan Stern return false; 1884ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent); 1885fa2a9566SLan Tianyu return !!hub->ports[udev->portnum - 1]->port_owner; 18867cbe5dcaSAlan Stern } 18877cbe5dcaSAlan Stern 18881da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev) 18891da177e4SLinus Torvalds { 1890ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev); 18911da177e4SLinus Torvalds int i; 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds for (i = 0; i < udev->maxchild; ++i) { 1894ff823c79SLan Tianyu if (hub->ports[i]->child) 1895ff823c79SLan Tianyu recursively_mark_NOTATTACHED(hub->ports[i]->child); 18961da177e4SLinus Torvalds } 18979bbdf1e0SAlan Stern if (udev->state == USB_STATE_SUSPENDED) 189815123006SSarah Sharp udev->active_duration -= jiffies; 18991da177e4SLinus Torvalds udev->state = USB_STATE_NOTATTACHED; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /** 19031da177e4SLinus Torvalds * usb_set_device_state - change a device's current state (usbcore, hcds) 19041da177e4SLinus Torvalds * @udev: pointer to device whose state should be changed 19051da177e4SLinus Torvalds * @new_state: new state value to be stored 19061da177e4SLinus Torvalds * 19071da177e4SLinus Torvalds * udev->state is _not_ fully protected by the device lock. Although 19081da177e4SLinus Torvalds * most transitions are made only while holding the lock, the state can 19091da177e4SLinus Torvalds * can change to USB_STATE_NOTATTACHED at almost any time. This 19101da177e4SLinus Torvalds * is so that devices can be marked as disconnected as soon as possible, 19111da177e4SLinus Torvalds * without having to wait for any semaphores to be released. As a result, 19121da177e4SLinus Torvalds * all changes to any device's state must be protected by the 19131da177e4SLinus Torvalds * device_state_lock spinlock. 19141da177e4SLinus Torvalds * 19151da177e4SLinus Torvalds * Once a device has been added to the device tree, all changes to its state 19161da177e4SLinus Torvalds * should be made using this routine. The state should _not_ be set directly. 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 19191da177e4SLinus Torvalds * Otherwise udev->state is set to new_state, and if new_state is 19201da177e4SLinus Torvalds * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 19211da177e4SLinus Torvalds * to USB_STATE_NOTATTACHED. 19221da177e4SLinus Torvalds */ 19231da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev, 19241da177e4SLinus Torvalds enum usb_device_state new_state) 19251da177e4SLinus Torvalds { 19261da177e4SLinus Torvalds unsigned long flags; 19274681b171SRafael J. Wysocki int wakeup = -1; 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds spin_lock_irqsave(&device_state_lock, flags); 19301da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED) 19311da177e4SLinus Torvalds ; /* do nothing */ 1932b94dc6b5SDavid Brownell else if (new_state != USB_STATE_NOTATTACHED) { 1933fb669cc0SDavid Brownell 1934fb669cc0SDavid Brownell /* root hub wakeup capabilities are managed out-of-band 1935fb669cc0SDavid Brownell * and may involve silicon errata ... ignore them here. 1936fb669cc0SDavid Brownell */ 1937fb669cc0SDavid Brownell if (udev->parent) { 1938645daaabSAlan Stern if (udev->state == USB_STATE_SUSPENDED 1939645daaabSAlan Stern || new_state == USB_STATE_SUSPENDED) 1940645daaabSAlan Stern ; /* No change to wakeup settings */ 1941645daaabSAlan Stern else if (new_state == USB_STATE_CONFIGURED) 1942ddbe1fcaSLu Baolu wakeup = (udev->quirks & 1943ddbe1fcaSLu Baolu USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 : 1944ddbe1fcaSLu Baolu udev->actconfig->desc.bmAttributes & 1945ddbe1fcaSLu Baolu USB_CONFIG_ATT_WAKEUP; 1946645daaabSAlan Stern else 19474681b171SRafael J. Wysocki wakeup = 0; 1948fb669cc0SDavid Brownell } 194915123006SSarah Sharp if (udev->state == USB_STATE_SUSPENDED && 195015123006SSarah Sharp new_state != USB_STATE_SUSPENDED) 195115123006SSarah Sharp udev->active_duration -= jiffies; 195215123006SSarah Sharp else if (new_state == USB_STATE_SUSPENDED && 195315123006SSarah Sharp udev->state != USB_STATE_SUSPENDED) 195415123006SSarah Sharp udev->active_duration += jiffies; 1955645daaabSAlan Stern udev->state = new_state; 1956b94dc6b5SDavid Brownell } else 19571da177e4SLinus Torvalds recursively_mark_NOTATTACHED(udev); 19581da177e4SLinus Torvalds spin_unlock_irqrestore(&device_state_lock, flags); 19594681b171SRafael J. Wysocki if (wakeup >= 0) 19604681b171SRafael J. Wysocki device_set_wakeup_capable(&udev->dev, wakeup); 19611da177e4SLinus Torvalds } 19626da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state); 19631da177e4SLinus Torvalds 19648af548dcSInaky Perez-Gonzalez /* 19653b29b68bSAlan Stern * Choose a device number. 19663b29b68bSAlan Stern * 19673b29b68bSAlan Stern * Device numbers are used as filenames in usbfs. On USB-1.1 and 19683b29b68bSAlan Stern * USB-2.0 buses they are also used as device addresses, however on 19693b29b68bSAlan Stern * USB-3.0 buses the address is assigned by the controller hardware 19703b29b68bSAlan Stern * and it usually is not the same as the device number. 19713b29b68bSAlan Stern * 19728af548dcSInaky Perez-Gonzalez * WUSB devices are simple: they have no hubs behind, so the mapping 19738af548dcSInaky Perez-Gonzalez * device <-> virtual port number becomes 1:1. Why? to simplify the 19748af548dcSInaky Perez-Gonzalez * life of the device connection logic in 19758af548dcSInaky Perez-Gonzalez * drivers/usb/wusbcore/devconnect.c. When we do the initial secret 19768af548dcSInaky Perez-Gonzalez * handshake we need to assign a temporary address in the unauthorized 19778af548dcSInaky Perez-Gonzalez * space. For simplicity we use the first virtual port number found to 19788af548dcSInaky Perez-Gonzalez * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] 19798af548dcSInaky Perez-Gonzalez * and that becomes it's address [X < 128] or its unauthorized address 19808af548dcSInaky Perez-Gonzalez * [X | 0x80]. 19818af548dcSInaky Perez-Gonzalez * 19828af548dcSInaky Perez-Gonzalez * We add 1 as an offset to the one-based USB-stack port number 19838af548dcSInaky Perez-Gonzalez * (zero-based wusb virtual port index) for two reasons: (a) dev addr 19848af548dcSInaky Perez-Gonzalez * 0 is reserved by USB for default address; (b) Linux's USB stack 19858af548dcSInaky Perez-Gonzalez * uses always #1 for the root hub of the controller. So USB stack's 19868af548dcSInaky Perez-Gonzalez * port #1, which is wusb virtual-port #0 has address #2. 1987c6515272SSarah Sharp * 1988c6515272SSarah Sharp * Devices connected under xHCI are not as simple. The host controller 1989c6515272SSarah Sharp * supports virtualization, so the hardware assigns device addresses and 1990c6515272SSarah Sharp * the HCD must setup data structures before issuing a set address 1991c6515272SSarah Sharp * command to the hardware. 19928af548dcSInaky Perez-Gonzalez */ 19933b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev) 19941da177e4SLinus Torvalds { 19951da177e4SLinus Torvalds int devnum; 19961da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 19971da177e4SLinus Torvalds 1998638139ebSPetr Mladek /* be safe when more hub events are proceed in parallel */ 1999feb26ac3SChris Bainbridge mutex_lock(&bus->devnum_next_mutex); 20008af548dcSInaky Perez-Gonzalez if (udev->wusb) { 20018af548dcSInaky Perez-Gonzalez devnum = udev->portnum + 1; 20028af548dcSInaky Perez-Gonzalez BUG_ON(test_bit(devnum, bus->devmap.devicemap)); 20038af548dcSInaky Perez-Gonzalez } else { 20048af548dcSInaky Perez-Gonzalez /* Try to allocate the next devnum beginning at 20058af548dcSInaky Perez-Gonzalez * bus->devnum_next. */ 20061da177e4SLinus Torvalds devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 20071da177e4SLinus Torvalds bus->devnum_next); 20081da177e4SLinus Torvalds if (devnum >= 128) 20098af548dcSInaky Perez-Gonzalez devnum = find_next_zero_bit(bus->devmap.devicemap, 20108af548dcSInaky Perez-Gonzalez 128, 1); 20111da177e4SLinus Torvalds bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); 20128af548dcSInaky Perez-Gonzalez } 20131da177e4SLinus Torvalds if (devnum < 128) { 20141da177e4SLinus Torvalds set_bit(devnum, bus->devmap.devicemap); 20151da177e4SLinus Torvalds udev->devnum = devnum; 20161da177e4SLinus Torvalds } 2017feb26ac3SChris Bainbridge mutex_unlock(&bus->devnum_next_mutex); 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 20203b29b68bSAlan Stern static void release_devnum(struct usb_device *udev) 20211da177e4SLinus Torvalds { 20221da177e4SLinus Torvalds if (udev->devnum > 0) { 20231da177e4SLinus Torvalds clear_bit(udev->devnum, udev->bus->devmap.devicemap); 20241da177e4SLinus Torvalds udev->devnum = -1; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds 20283b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum) 20294953d141SDavid Vrabel { 20304953d141SDavid Vrabel /* The address for a WUSB device is managed by wusbcore. */ 20314953d141SDavid Vrabel if (!udev->wusb) 20324953d141SDavid Vrabel udev->devnum = devnum; 20334953d141SDavid Vrabel } 20344953d141SDavid Vrabel 2035f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev) 2036f7410cedSHerbert Xu { 2037f7410cedSHerbert Xu struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2038f7410cedSHerbert Xu 2039f7410cedSHerbert Xu /* Root hubs aren't real devices, so don't free HCD resources */ 2040f7410cedSHerbert Xu if (hcd->driver->free_dev && udev->parent) 2041f7410cedSHerbert Xu hcd->driver->free_dev(hcd, udev); 2042f7410cedSHerbert Xu } 2043f7410cedSHerbert Xu 20447027df36SDan Williams static void hub_disconnect_children(struct usb_device *udev) 20457027df36SDan Williams { 20467027df36SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(udev); 20477027df36SDan Williams int i; 20487027df36SDan Williams 20497027df36SDan Williams /* Free up all the children before we remove this device */ 20507027df36SDan Williams for (i = 0; i < udev->maxchild; i++) { 20517027df36SDan Williams if (hub->ports[i]->child) 20527027df36SDan Williams usb_disconnect(&hub->ports[i]->child); 20537027df36SDan Williams } 20547027df36SDan Williams } 20557027df36SDan Williams 20561da177e4SLinus Torvalds /** 20571da177e4SLinus Torvalds * usb_disconnect - disconnect a device (usbcore-internal) 20581da177e4SLinus Torvalds * @pdev: pointer to device being disconnected 20591da177e4SLinus Torvalds * Context: !in_interrupt () 20601da177e4SLinus Torvalds * 20611da177e4SLinus Torvalds * Something got disconnected. Get rid of it and all of its children. 20621da177e4SLinus Torvalds * 20631da177e4SLinus Torvalds * If *pdev is a normal device then the parent hub must already be locked. 2064a4b5d606SHeiner Kallweit * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock, 2065db8f2aa3SBjorn Helgaas * which protects the set of root hubs as well as the list of buses. 20661da177e4SLinus Torvalds * 20671da177e4SLinus Torvalds * Only hub drivers (including virtual root hub drivers for host 20681da177e4SLinus Torvalds * controllers) should ever call this. 20691da177e4SLinus Torvalds * 20701da177e4SLinus Torvalds * This call is synchronous, and may not be used in an interrupt context. 20711da177e4SLinus Torvalds */ 20721da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev) 20731da177e4SLinus Torvalds { 20747027df36SDan Williams struct usb_port *port_dev = NULL; 20751da177e4SLinus Torvalds struct usb_device *udev = *pdev; 20765b1dc209SPeter Chen struct usb_hub *hub = NULL; 20775b1dc209SPeter Chen int port1 = 1; 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds /* mark the device as inactive, so any further urb submissions for 20801da177e4SLinus Torvalds * this device (and any of its children) will fail immediately. 208125985edcSLucas De Marchi * this quiesces everything except pending urbs. 20821da177e4SLinus Torvalds */ 20831da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 20843b29b68bSAlan Stern dev_info(&udev->dev, "USB disconnect, device number %d\n", 20853b29b68bSAlan Stern udev->devnum); 20861da177e4SLinus Torvalds 20879ad3d6ccSAlan Stern usb_lock_device(udev); 20889ad3d6ccSAlan Stern 20897027df36SDan Williams hub_disconnect_children(udev); 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds /* deallocate hcd/hardware state ... nuking all pending urbs and 20921da177e4SLinus Torvalds * cleaning up all state associated with the current configuration 20931da177e4SLinus Torvalds * so that the hardware is now fully quiesced. 20941da177e4SLinus Torvalds */ 2095782da727SAlan Stern dev_dbg(&udev->dev, "unregistering device\n"); 20961da177e4SLinus Torvalds usb_disable_device(udev, 0); 2097cde217a5SAlan Stern usb_hcd_synchronize_unlinks(udev); 20981da177e4SLinus Torvalds 2099fde26380SLan Tianyu if (udev->parent) { 21007027df36SDan Williams port1 = udev->portnum; 21017027df36SDan Williams hub = usb_hub_to_struct_hub(udev->parent); 21027027df36SDan Williams port_dev = hub->ports[port1 - 1]; 2103fde26380SLan Tianyu 2104fde26380SLan Tianyu sysfs_remove_link(&udev->dev.kobj, "port"); 2105fde26380SLan Tianyu sysfs_remove_link(&port_dev->dev.kobj, "device"); 2106971fcd49SLan Tianyu 21077027df36SDan Williams /* 21087027df36SDan Williams * As usb_port_runtime_resume() de-references udev, make 21097027df36SDan Williams * sure no resumes occur during removal 21107027df36SDan Williams */ 21117027df36SDan Williams if (!test_and_set_bit(port1, hub->child_usage_bits)) 21127027df36SDan Williams pm_runtime_get_sync(&port_dev->dev); 2113fde26380SLan Tianyu } 2114fde26380SLan Tianyu 21153b23dd6fSAlan Stern usb_remove_ep_devs(&udev->ep0); 2116782da727SAlan Stern usb_unlock_device(udev); 21173099e75aSGreg Kroah-Hartman 2118782da727SAlan Stern /* Unregister the device. The device driver is responsible 21193b23dd6fSAlan Stern * for de-configuring the device and invoking the remove-device 21203b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 2121782da727SAlan Stern */ 2122782da727SAlan Stern device_del(&udev->dev); 2123782da727SAlan Stern 2124782da727SAlan Stern /* Free the device number and delete the parent's children[] 21251da177e4SLinus Torvalds * (or root_hub) pointer. 21261da177e4SLinus Torvalds */ 21273b29b68bSAlan Stern release_devnum(udev); 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds /* Avoid races with recursively_mark_NOTATTACHED() */ 21301da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 21311da177e4SLinus Torvalds *pdev = NULL; 21321da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 21331da177e4SLinus Torvalds 21347027df36SDan Williams if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) 21357027df36SDan Williams pm_runtime_put(&port_dev->dev); 21367027df36SDan Williams 2137f7410cedSHerbert Xu hub_free_dev(udev); 2138f7410cedSHerbert Xu 2139782da727SAlan Stern put_device(&udev->dev); 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 2142f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 21431da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds if (!string) 21461da177e4SLinus Torvalds return; 2147f2ec522eSJoe Perches dev_info(&udev->dev, "%s: %s\n", id, string); 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 2150f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev) 2151f2a383e4SGreg Kroah-Hartman { 2152f2a383e4SGreg Kroah-Hartman dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", 2153f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idVendor), 2154f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idProduct)); 2155b9cef6c3SWu Fengguang dev_info(&udev->dev, 2156b9cef6c3SWu Fengguang "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 2157f2a383e4SGreg Kroah-Hartman udev->descriptor.iManufacturer, 2158f2a383e4SGreg Kroah-Hartman udev->descriptor.iProduct, 2159f2a383e4SGreg Kroah-Hartman udev->descriptor.iSerialNumber); 2160f2a383e4SGreg Kroah-Hartman show_string(udev, "Product", udev->product); 2161f2a383e4SGreg Kroah-Hartman show_string(udev, "Manufacturer", udev->manufacturer); 2162f2a383e4SGreg Kroah-Hartman show_string(udev, "SerialNumber", udev->serial); 2163f2a383e4SGreg Kroah-Hartman } 21641da177e4SLinus Torvalds #else 2165f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { } 21661da177e4SLinus Torvalds #endif 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds 21693ede760fSOliver Neukum /** 21708d8558d1SAlan Stern * usb_enumerate_device_otg - FIXME (usbcore-internal) 21713ede760fSOliver Neukum * @udev: newly addressed device (in ADDRESS state) 21723ede760fSOliver Neukum * 21738d8558d1SAlan Stern * Finish enumeration for On-The-Go devices 2174626f090cSYacine Belkadi * 2175626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise. 21763ede760fSOliver Neukum */ 21778d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev) 21781da177e4SLinus Torvalds { 2179d9d16e8aSInaky Perez-Gonzalez int err = 0; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 21821da177e4SLinus Torvalds /* 21831da177e4SLinus Torvalds * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 21841da177e4SLinus Torvalds * to wake us after we've powered off VBUS; and HNP, switching roles 21851da177e4SLinus Torvalds * "host" to "peripheral". The OTG descriptor helps figure this out. 21861da177e4SLinus Torvalds */ 21871da177e4SLinus Torvalds if (!udev->bus->is_b_host 21881da177e4SLinus Torvalds && udev->config 21891da177e4SLinus Torvalds && udev->parent == udev->bus->root_hub) { 21902eb5052eSFelipe Balbi struct usb_otg_descriptor *desc = NULL; 21911da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 219212c3da34SAlan Stern unsigned port1 = udev->portnum; 21931da177e4SLinus Torvalds 21947d2d641cSLi Jun /* descriptor may appear anywhere in config */ 21957d2d641cSLi Jun err = __usb_get_extra_descriptor(udev->rawdescriptors[0], 21967d2d641cSLi Jun le16_to_cpu(udev->config[0].desc.wTotalLength), 21977d2d641cSLi Jun USB_DT_OTG, (void **) &desc); 21987d2d641cSLi Jun if (err || !(desc->bmAttributes & USB_OTG_HNP)) 21997d2d641cSLi Jun return 0; 22007d2d641cSLi Jun 22017d2d641cSLi Jun dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", 22027d2d641cSLi Jun (port1 == bus->otg_port) ? "" : "non-"); 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds /* enable HNP before suspend, it's simpler */ 22057d2d641cSLi Jun if (port1 == bus->otg_port) { 22061da177e4SLinus Torvalds bus->b_hnp_enable = 1; 22071da177e4SLinus Torvalds err = usb_control_msg(udev, 22081da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 22091da177e4SLinus Torvalds USB_REQ_SET_FEATURE, 0, 22107d2d641cSLi Jun USB_DEVICE_B_HNP_ENABLE, 22117d2d641cSLi Jun 0, NULL, 0, 22127d2d641cSLi Jun USB_CTRL_SET_TIMEOUT); 22131da177e4SLinus Torvalds if (err < 0) { 22147d2d641cSLi Jun /* 22157d2d641cSLi Jun * OTG MESSAGE: report errors here, 22161da177e4SLinus Torvalds * customize to match your product. 22171da177e4SLinus Torvalds */ 22187d2d641cSLi Jun dev_err(&udev->dev, "can't set HNP mode: %d\n", 22191da177e4SLinus Torvalds err); 22201da177e4SLinus Torvalds bus->b_hnp_enable = 0; 22211da177e4SLinus Torvalds } 22227d2d641cSLi Jun } else if (desc->bLength == sizeof 22237d2d641cSLi Jun (struct usb_otg_descriptor)) { 22247d2d641cSLi Jun /* Set a_alt_hnp_support for legacy otg device */ 22257d2d641cSLi Jun err = usb_control_msg(udev, 22267d2d641cSLi Jun usb_sndctrlpipe(udev, 0), 22277d2d641cSLi Jun USB_REQ_SET_FEATURE, 0, 22287d2d641cSLi Jun USB_DEVICE_A_ALT_HNP_SUPPORT, 22297d2d641cSLi Jun 0, NULL, 0, 22307d2d641cSLi Jun USB_CTRL_SET_TIMEOUT); 22317d2d641cSLi Jun if (err < 0) 22327d2d641cSLi Jun dev_err(&udev->dev, 22337d2d641cSLi Jun "set a_alt_hnp_support failed: %d\n", 22347d2d641cSLi Jun err); 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds #endif 2238d9d16e8aSInaky Perez-Gonzalez return err; 2239d9d16e8aSInaky Perez-Gonzalez } 22401da177e4SLinus Torvalds 2241d9d16e8aSInaky Perez-Gonzalez 2242d9d16e8aSInaky Perez-Gonzalez /** 22438d8558d1SAlan Stern * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) 2244d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 2245d9d16e8aSInaky Perez-Gonzalez * 2246d9d16e8aSInaky Perez-Gonzalez * This is only called by usb_new_device() and usb_authorize_device() 2247d9d16e8aSInaky Perez-Gonzalez * and FIXME -- all comments that apply to them apply here wrt to 2248d9d16e8aSInaky Perez-Gonzalez * environment. 2249d9d16e8aSInaky Perez-Gonzalez * 2250d9d16e8aSInaky Perez-Gonzalez * If the device is WUSB and not authorized, we don't attempt to read 2251d9d16e8aSInaky Perez-Gonzalez * the string descriptors, as they will be errored out by the device 2252d9d16e8aSInaky Perez-Gonzalez * until it has been authorized. 2253626f090cSYacine Belkadi * 2254626f090cSYacine Belkadi * Return: 0 if successful. A negative error code otherwise. 2255d9d16e8aSInaky Perez-Gonzalez */ 22568d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev) 2257d9d16e8aSInaky Perez-Gonzalez { 2258d9d16e8aSInaky Perez-Gonzalez int err; 2259026f3fcbSPeter Chen struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2260d9d16e8aSInaky Perez-Gonzalez 2261d9d16e8aSInaky Perez-Gonzalez if (udev->config == NULL) { 2262d9d16e8aSInaky Perez-Gonzalez err = usb_get_configuration(udev); 2263d9d16e8aSInaky Perez-Gonzalez if (err < 0) { 2264e9e88fb7SAlan Stern if (err != -ENODEV) 2265d9d16e8aSInaky Perez-Gonzalez dev_err(&udev->dev, "can't read configurations, error %d\n", 2266d9d16e8aSInaky Perez-Gonzalez err); 226780da2e0dSLaurent Pinchart return err; 2268d9d16e8aSInaky Perez-Gonzalez } 2269d9d16e8aSInaky Perez-Gonzalez } 227083e83ecbSThomas Pugliese 2271d9d16e8aSInaky Perez-Gonzalez /* read the standard strings and cache them if present */ 2272d9d16e8aSInaky Perez-Gonzalez udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 2273d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = usb_cache_string(udev, 2274d9d16e8aSInaky Perez-Gonzalez udev->descriptor.iManufacturer); 2275d9d16e8aSInaky Perez-Gonzalez udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 227683e83ecbSThomas Pugliese 22778d8558d1SAlan Stern err = usb_enumerate_device_otg(udev); 227880da2e0dSLaurent Pinchart if (err < 0) 2279d9d16e8aSInaky Perez-Gonzalez return err; 228080da2e0dSLaurent Pinchart 2281026f3fcbSPeter Chen if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support && 2282e5a9d621SPeter Chen !is_targeted(udev)) { 2283026f3fcbSPeter Chen /* Maybe it can talk to us, though we can't talk to it. 2284026f3fcbSPeter Chen * (Includes HNP test device.) 2285026f3fcbSPeter Chen */ 2286e5a9d621SPeter Chen if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable 2287e5a9d621SPeter Chen || udev->bus->is_b_host)) { 2288026f3fcbSPeter Chen err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND); 2289026f3fcbSPeter Chen if (err < 0) 2290026f3fcbSPeter Chen dev_dbg(&udev->dev, "HNP fail, %d\n", err); 2291026f3fcbSPeter Chen } 2292026f3fcbSPeter Chen return -ENOTSUPP; 2293026f3fcbSPeter Chen } 2294026f3fcbSPeter Chen 229580da2e0dSLaurent Pinchart usb_detect_interface_quirks(udev); 229680da2e0dSLaurent Pinchart 229780da2e0dSLaurent Pinchart return 0; 2298d9d16e8aSInaky Perez-Gonzalez } 2299d9d16e8aSInaky Perez-Gonzalez 2300d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev) 2301d35e70d5SMatthew Garrett { 2302d35e70d5SMatthew Garrett struct usb_device *hdev = udev->parent; 2303d35e70d5SMatthew Garrett struct usb_hub *hub; 2304d35e70d5SMatthew Garrett u8 port = udev->portnum; 2305d35e70d5SMatthew Garrett u16 wHubCharacteristics; 2306d35e70d5SMatthew Garrett bool removable = true; 2307d35e70d5SMatthew Garrett 2308d35e70d5SMatthew Garrett if (!hdev) 2309d35e70d5SMatthew Garrett return; 2310d35e70d5SMatthew Garrett 2311ad493e5eSLan Tianyu hub = usb_hub_to_struct_hub(udev->parent); 2312d35e70d5SMatthew Garrett 231392bfbf71SMatthew Garrett /* 231492bfbf71SMatthew Garrett * If the platform firmware has provided information about a port, 231592bfbf71SMatthew Garrett * use that to determine whether it's removable. 231692bfbf71SMatthew Garrett */ 231792bfbf71SMatthew Garrett switch (hub->ports[udev->portnum - 1]->connect_type) { 231892bfbf71SMatthew Garrett case USB_PORT_CONNECT_TYPE_HOT_PLUG: 231992bfbf71SMatthew Garrett udev->removable = USB_DEVICE_REMOVABLE; 232092bfbf71SMatthew Garrett return; 232192bfbf71SMatthew Garrett case USB_PORT_CONNECT_TYPE_HARD_WIRED: 232258339c2eSMatthew Garrett case USB_PORT_NOT_USED: 232392bfbf71SMatthew Garrett udev->removable = USB_DEVICE_FIXED; 232492bfbf71SMatthew Garrett return; 232558339c2eSMatthew Garrett default: 232658339c2eSMatthew Garrett break; 232792bfbf71SMatthew Garrett } 232892bfbf71SMatthew Garrett 232992bfbf71SMatthew Garrett /* 233092bfbf71SMatthew Garrett * Otherwise, check whether the hub knows whether a port is removable 233192bfbf71SMatthew Garrett * or not 233292bfbf71SMatthew Garrett */ 2333d35e70d5SMatthew Garrett wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2334d35e70d5SMatthew Garrett 2335d35e70d5SMatthew Garrett if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 2336d35e70d5SMatthew Garrett return; 2337d35e70d5SMatthew Garrett 2338d35e70d5SMatthew Garrett if (hub_is_superspeed(hdev)) { 2339ca3c1539SLan Tianyu if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) 2340ca3c1539SLan Tianyu & (1 << port)) 2341d35e70d5SMatthew Garrett removable = false; 2342d35e70d5SMatthew Garrett } else { 2343d35e70d5SMatthew Garrett if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 2344d35e70d5SMatthew Garrett removable = false; 2345d35e70d5SMatthew Garrett } 2346d35e70d5SMatthew Garrett 2347d35e70d5SMatthew Garrett if (removable) 2348d35e70d5SMatthew Garrett udev->removable = USB_DEVICE_REMOVABLE; 2349d35e70d5SMatthew Garrett else 2350d35e70d5SMatthew Garrett udev->removable = USB_DEVICE_FIXED; 2351a4204ff0SDan Williams 2352d35e70d5SMatthew Garrett } 2353d9d16e8aSInaky Perez-Gonzalez 2354d9d16e8aSInaky Perez-Gonzalez /** 2355d9d16e8aSInaky Perez-Gonzalez * usb_new_device - perform initial device setup (usbcore-internal) 2356d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 2357d9d16e8aSInaky Perez-Gonzalez * 23588d8558d1SAlan Stern * This is called with devices which have been detected but not fully 23598d8558d1SAlan Stern * enumerated. The device descriptor is available, but not descriptors 2360d9d16e8aSInaky Perez-Gonzalez * for any device configuration. The caller must have locked either 2361d9d16e8aSInaky Perez-Gonzalez * the parent hub (if udev is a normal device) or else the 2362a4b5d606SHeiner Kallweit * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to 2363d9d16e8aSInaky Perez-Gonzalez * udev has already been installed, but udev is not yet visible through 2364d9d16e8aSInaky Perez-Gonzalez * sysfs or other filesystem code. 2365d9d16e8aSInaky Perez-Gonzalez * 2366d9d16e8aSInaky Perez-Gonzalez * This call is synchronous, and may not be used in an interrupt context. 2367d9d16e8aSInaky Perez-Gonzalez * 2368d9d16e8aSInaky Perez-Gonzalez * Only the hub driver or root-hub registrar should ever call this. 2369626f090cSYacine Belkadi * 2370626f090cSYacine Belkadi * Return: Whether the device is configured properly or not. Zero if the 2371626f090cSYacine Belkadi * interface was registered with the driver core; else a negative errno 2372626f090cSYacine Belkadi * value. 2373626f090cSYacine Belkadi * 2374d9d16e8aSInaky Perez-Gonzalez */ 2375d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev) 2376d9d16e8aSInaky Perez-Gonzalez { 2377d9d16e8aSInaky Perez-Gonzalez int err; 2378d9d16e8aSInaky Perez-Gonzalez 237916985408SDan Streetman if (udev->parent) { 238016985408SDan Streetman /* Initialize non-root-hub device wakeup to disabled; 238116985408SDan Streetman * device (un)configuration controls wakeup capable 238216985408SDan Streetman * sysfs power/wakeup controls wakeup enabled/disabled 238316985408SDan Streetman */ 238416985408SDan Streetman device_init_wakeup(&udev->dev, 0); 238516985408SDan Streetman } 238616985408SDan Streetman 23879bbdf1e0SAlan Stern /* Tell the runtime-PM framework the device is active */ 23889bbdf1e0SAlan Stern pm_runtime_set_active(&udev->dev); 2389c08512c7SAlan Stern pm_runtime_get_noresume(&udev->dev); 2390fcc4a01eSAlan Stern pm_runtime_use_autosuspend(&udev->dev); 23919bbdf1e0SAlan Stern pm_runtime_enable(&udev->dev); 23929bbdf1e0SAlan Stern 2393c08512c7SAlan Stern /* By default, forbid autosuspend for all devices. It will be 2394c08512c7SAlan Stern * allowed for hubs during binding. 2395c08512c7SAlan Stern */ 2396c08512c7SAlan Stern usb_disable_autosuspend(udev); 2397c08512c7SAlan Stern 23988d8558d1SAlan Stern err = usb_enumerate_device(udev); /* Read descriptors */ 2399d9d16e8aSInaky Perez-Gonzalez if (err < 0) 2400d9d16e8aSInaky Perez-Gonzalez goto fail; 2401c6515272SSarah Sharp dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 2402c6515272SSarah Sharp udev->devnum, udev->bus->busnum, 2403c6515272SSarah Sharp (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 24049f8b17e6SKay Sievers /* export the usbdev device-node for libusb */ 24059f8b17e6SKay Sievers udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 24069f8b17e6SKay Sievers (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 24079f8b17e6SKay Sievers 24086cd13201SAlan Stern /* Tell the world! */ 24096cd13201SAlan Stern announce_device(udev); 2410195af2ccSAlan Stern 2411b04b3156STheodore Ts'o if (udev->serial) 2412b04b3156STheodore Ts'o add_device_randomness(udev->serial, strlen(udev->serial)); 2413b04b3156STheodore Ts'o if (udev->product) 2414b04b3156STheodore Ts'o add_device_randomness(udev->product, strlen(udev->product)); 2415b04b3156STheodore Ts'o if (udev->manufacturer) 2416b04b3156STheodore Ts'o add_device_randomness(udev->manufacturer, 2417b04b3156STheodore Ts'o strlen(udev->manufacturer)); 2418b04b3156STheodore Ts'o 2419927bc916SRafael J. Wysocki device_enable_async_suspend(&udev->dev); 2420d35e70d5SMatthew Garrett 2421a4204ff0SDan Williams /* check whether the hub or firmware marks this port as non-removable */ 2422d35e70d5SMatthew Garrett if (udev->parent) 2423d35e70d5SMatthew Garrett set_usb_port_removable(udev); 2424d35e70d5SMatthew Garrett 2425782da727SAlan Stern /* Register the device. The device driver is responsible 24263b23dd6fSAlan Stern * for configuring the device and invoking the add-device 24273b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 2428782da727SAlan Stern */ 24291da177e4SLinus Torvalds err = device_add(&udev->dev); 24301da177e4SLinus Torvalds if (err) { 24311da177e4SLinus Torvalds dev_err(&udev->dev, "can't device_add, error %d\n", err); 24321da177e4SLinus Torvalds goto fail; 24331da177e4SLinus Torvalds } 24349ad3d6ccSAlan Stern 2435fde26380SLan Tianyu /* Create link files between child device and usb port device. */ 2436fde26380SLan Tianyu if (udev->parent) { 2437ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 2438d5c3834eSDan Williams int port1 = udev->portnum; 2439d5c3834eSDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 2440fde26380SLan Tianyu 2441fde26380SLan Tianyu err = sysfs_create_link(&udev->dev.kobj, 2442fde26380SLan Tianyu &port_dev->dev.kobj, "port"); 2443fde26380SLan Tianyu if (err) 2444fde26380SLan Tianyu goto fail; 2445fde26380SLan Tianyu 2446fde26380SLan Tianyu err = sysfs_create_link(&port_dev->dev.kobj, 2447fde26380SLan Tianyu &udev->dev.kobj, "device"); 2448fde26380SLan Tianyu if (err) { 2449fde26380SLan Tianyu sysfs_remove_link(&udev->dev.kobj, "port"); 2450fde26380SLan Tianyu goto fail; 2451fde26380SLan Tianyu } 2452971fcd49SLan Tianyu 2453d5c3834eSDan Williams if (!test_and_set_bit(port1, hub->child_usage_bits)) 2454971fcd49SLan Tianyu pm_runtime_get_sync(&port_dev->dev); 2455fde26380SLan Tianyu } 2456fde26380SLan Tianyu 24573b23dd6fSAlan Stern (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2458c08512c7SAlan Stern usb_mark_last_busy(udev); 2459c08512c7SAlan Stern pm_runtime_put_sync_autosuspend(&udev->dev); 2460c066475eSGreg Kroah-Hartman return err; 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds fail: 24631da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 24649bbdf1e0SAlan Stern pm_runtime_disable(&udev->dev); 24659bbdf1e0SAlan Stern pm_runtime_set_suspended(&udev->dev); 2466d9d16e8aSInaky Perez-Gonzalez return err; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 246993993a0aSInaky Perez-Gonzalez 247093993a0aSInaky Perez-Gonzalez /** 2471fd39c86bSRandy Dunlap * usb_deauthorize_device - deauthorize a device (usbcore-internal) 2472fd39c86bSRandy Dunlap * @usb_dev: USB device 2473fd39c86bSRandy Dunlap * 2474fd39c86bSRandy Dunlap * Move the USB device to a very basic state where interfaces are disabled 2475fd39c86bSRandy Dunlap * and the device is in fact unconfigured and unusable. 247693993a0aSInaky Perez-Gonzalez * 247793993a0aSInaky Perez-Gonzalez * We share a lock (that we have) with device_del(), so we need to 247893993a0aSInaky Perez-Gonzalez * defer its call. 2479626f090cSYacine Belkadi * 2480626f090cSYacine Belkadi * Return: 0. 248193993a0aSInaky Perez-Gonzalez */ 248293993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev) 248393993a0aSInaky Perez-Gonzalez { 248493993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 248593993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 0) 248693993a0aSInaky Perez-Gonzalez goto out_unauthorized; 2487da307123SAlan Stern 248893993a0aSInaky Perez-Gonzalez usb_dev->authorized = 0; 248993993a0aSInaky Perez-Gonzalez usb_set_configuration(usb_dev, -1); 2490da307123SAlan Stern 249193993a0aSInaky Perez-Gonzalez out_unauthorized: 249293993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); 249393993a0aSInaky Perez-Gonzalez return 0; 249493993a0aSInaky Perez-Gonzalez } 249593993a0aSInaky Perez-Gonzalez 249693993a0aSInaky Perez-Gonzalez 249793993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev) 249893993a0aSInaky Perez-Gonzalez { 249993993a0aSInaky Perez-Gonzalez int result = 0, c; 2500da307123SAlan Stern 250193993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 250293993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 1) 250393993a0aSInaky Perez-Gonzalez goto out_authorized; 2504da307123SAlan Stern 250593993a0aSInaky Perez-Gonzalez result = usb_autoresume_device(usb_dev); 250693993a0aSInaky Perez-Gonzalez if (result < 0) { 250793993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 250893993a0aSInaky Perez-Gonzalez "can't autoresume for authorization: %d\n", result); 250993993a0aSInaky Perez-Gonzalez goto error_autoresume; 251093993a0aSInaky Perez-Gonzalez } 2511e50a322eSJosef Gajdusek 2512e50a322eSJosef Gajdusek if (usb_dev->wusb) { 251393993a0aSInaky Perez-Gonzalez result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); 251493993a0aSInaky Perez-Gonzalez if (result < 0) { 251593993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, "can't re-read device descriptor for " 251693993a0aSInaky Perez-Gonzalez "authorization: %d\n", result); 251793993a0aSInaky Perez-Gonzalez goto error_device_descriptor; 251893993a0aSInaky Perez-Gonzalez } 2519e50a322eSJosef Gajdusek } 2520da307123SAlan Stern 252193993a0aSInaky Perez-Gonzalez usb_dev->authorized = 1; 252293993a0aSInaky Perez-Gonzalez /* Choose and set the configuration. This registers the interfaces 252393993a0aSInaky Perez-Gonzalez * with the driver core and lets interface drivers bind to them. 252493993a0aSInaky Perez-Gonzalez */ 2525b5ea060fSGreg Kroah-Hartman c = usb_choose_configuration(usb_dev); 252693993a0aSInaky Perez-Gonzalez if (c >= 0) { 252793993a0aSInaky Perez-Gonzalez result = usb_set_configuration(usb_dev, c); 252893993a0aSInaky Perez-Gonzalez if (result) { 252993993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 253093993a0aSInaky Perez-Gonzalez "can't set config #%d, error %d\n", c, result); 253193993a0aSInaky Perez-Gonzalez /* This need not be fatal. The user can try to 253293993a0aSInaky Perez-Gonzalez * set other configurations. */ 253393993a0aSInaky Perez-Gonzalez } 253493993a0aSInaky Perez-Gonzalez } 253593993a0aSInaky Perez-Gonzalez dev_info(&usb_dev->dev, "authorized to connect\n"); 2536da307123SAlan Stern 253793993a0aSInaky Perez-Gonzalez error_device_descriptor: 2538da307123SAlan Stern usb_autosuspend_device(usb_dev); 253993993a0aSInaky Perez-Gonzalez error_autoresume: 254093993a0aSInaky Perez-Gonzalez out_authorized: 2541781b2137SMatthias Beyer usb_unlock_device(usb_dev); /* complements locktree */ 254293993a0aSInaky Perez-Gonzalez return result; 254393993a0aSInaky Perez-Gonzalez } 254493993a0aSInaky Perez-Gonzalez 25450cdd49a1SMathias Nyman /* 25460cdd49a1SMathias Nyman * Return 1 if port speed is SuperSpeedPlus, 0 otherwise 25470cdd49a1SMathias Nyman * check it from the link protocol field of the current speed ID attribute. 25480cdd49a1SMathias Nyman * current speed ID is got from ext port status request. Sublink speed attribute 25490cdd49a1SMathias Nyman * table is returned with the hub BOS SSP device capability descriptor 25500cdd49a1SMathias Nyman */ 25510cdd49a1SMathias Nyman static int port_speed_is_ssp(struct usb_device *hdev, int speed_id) 25520cdd49a1SMathias Nyman { 25530cdd49a1SMathias Nyman int ssa_count; 25540cdd49a1SMathias Nyman u32 ss_attr; 25550cdd49a1SMathias Nyman int i; 25560cdd49a1SMathias Nyman struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap; 25570cdd49a1SMathias Nyman 25580cdd49a1SMathias Nyman if (!ssp_cap) 25590cdd49a1SMathias Nyman return 0; 25600cdd49a1SMathias Nyman 25610cdd49a1SMathias Nyman ssa_count = le32_to_cpu(ssp_cap->bmAttributes) & 25620cdd49a1SMathias Nyman USB_SSP_SUBLINK_SPEED_ATTRIBS; 25630cdd49a1SMathias Nyman 25640cdd49a1SMathias Nyman for (i = 0; i <= ssa_count; i++) { 25650cdd49a1SMathias Nyman ss_attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); 25660cdd49a1SMathias Nyman if (speed_id == (ss_attr & USB_SSP_SUBLINK_SPEED_SSID)) 25670cdd49a1SMathias Nyman return !!(ss_attr & USB_SSP_SUBLINK_SPEED_LP); 25680cdd49a1SMathias Nyman } 25690cdd49a1SMathias Nyman return 0; 25700cdd49a1SMathias Nyman } 257193993a0aSInaky Perez-Gonzalez 25720165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ 25730165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub) 25740165de09SInaky Perez-Gonzalez { 25750165de09SInaky Perez-Gonzalez struct usb_hcd *hcd; 25760165de09SInaky Perez-Gonzalez if (hub->hdev->parent != NULL) /* not a root hub? */ 25770165de09SInaky Perez-Gonzalez return 0; 25786ae706aeSGeliang Tang hcd = bus_to_hcd(hub->hdev->bus); 25790165de09SInaky Perez-Gonzalez return hcd->wireless; 25800165de09SInaky Perez-Gonzalez } 25810165de09SInaky Perez-Gonzalez 25820165de09SInaky Perez-Gonzalez 25831da177e4SLinus Torvalds #define PORT_RESET_TRIES 5 25841da177e4SLinus Torvalds #define SET_ADDRESS_TRIES 2 25851da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES 2 25861da177e4SLinus Torvalds #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) 258790ab5ee9SRusty Russell #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 25901da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME 10 259175d7cf72SAndiry Xu #define HUB_BH_RESET_TIME 50 25921da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME 200 259377c7f072SSarah Sharp #define HUB_RESET_TIMEOUT 800 25941da177e4SLinus Torvalds 259548fc7dbdSDan Williams /* 259648fc7dbdSDan Williams * "New scheme" enumeration causes an extra state transition to be 259748fc7dbdSDan Williams * exposed to an xhci host and causes USB3 devices to receive control 259848fc7dbdSDan Williams * commands in the default state. This has been seen to cause 259948fc7dbdSDan Williams * enumeration failures, so disable this enumeration scheme for USB3 260048fc7dbdSDan Williams * devices. 260148fc7dbdSDan Williams */ 260248fc7dbdSDan Williams static bool use_new_scheme(struct usb_device *udev, int retry) 260348fc7dbdSDan Williams { 26048a1b2725SMathias Nyman if (udev->speed >= USB_SPEED_SUPER) 260548fc7dbdSDan Williams return false; 260648fc7dbdSDan Williams 260748fc7dbdSDan Williams return USE_NEW_SCHEME(retry); 260848fc7dbdSDan Williams } 260948fc7dbdSDan Williams 2610025d4430SRahul Bedarkar /* Is a USB 3.0 port in the Inactive or Compliance Mode state? 26118bea2bd3SStanislaw Ledwon * Port worm reset is required to recover 26128bea2bd3SStanislaw Ledwon */ 26133cd12f91SDan Williams static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 26143cd12f91SDan Williams u16 portstatus) 261510d674a8SSarah Sharp { 26163cd12f91SDan Williams u16 link_state; 26173cd12f91SDan Williams 26183cd12f91SDan Williams if (!hub_is_superspeed(hub->hdev)) 26193cd12f91SDan Williams return false; 26203cd12f91SDan Williams 26213cd12f91SDan Williams if (test_bit(port1, hub->warm_reset_bits)) 26223cd12f91SDan Williams return true; 26233cd12f91SDan Williams 26243cd12f91SDan Williams link_state = portstatus & USB_PORT_STAT_LINK_STATE; 26253cd12f91SDan Williams return link_state == USB_SS_PORT_LS_SS_INACTIVE 26263cd12f91SDan Williams || link_state == USB_SS_PORT_LS_COMP_MOD; 262710d674a8SSarah Sharp } 262810d674a8SSarah Sharp 26291da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1, 263075d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm) 26311da177e4SLinus Torvalds { 26321da177e4SLinus Torvalds int delay_time, ret; 26331da177e4SLinus Torvalds u16 portstatus; 26341da177e4SLinus Torvalds u16 portchange; 26350cdd49a1SMathias Nyman u32 ext_portstatus = 0; 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds for (delay_time = 0; 26381da177e4SLinus Torvalds delay_time < HUB_RESET_TIMEOUT; 26391da177e4SLinus Torvalds delay_time += delay) { 26401da177e4SLinus Torvalds /* wait to give the device a chance to reset */ 26411da177e4SLinus Torvalds msleep(delay); 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds /* read and decode port status */ 26440cdd49a1SMathias Nyman if (hub_is_superspeedplus(hub->hdev)) 26450cdd49a1SMathias Nyman ret = hub_ext_port_status(hub, port1, 26460cdd49a1SMathias Nyman HUB_EXT_PORT_STATUS, 26470cdd49a1SMathias Nyman &portstatus, &portchange, 26480cdd49a1SMathias Nyman &ext_portstatus); 26490cdd49a1SMathias Nyman else 26500cdd49a1SMathias Nyman ret = hub_port_status(hub, port1, &portstatus, 26510cdd49a1SMathias Nyman &portchange); 26521da177e4SLinus Torvalds if (ret < 0) 26531da177e4SLinus Torvalds return ret; 26541da177e4SLinus Torvalds 265522547c4cSGuenter Roeck /* 265622547c4cSGuenter Roeck * The port state is unknown until the reset completes. 265722547c4cSGuenter Roeck * 265822547c4cSGuenter Roeck * On top of that, some chips may require additional time 265922547c4cSGuenter Roeck * to re-establish a connection after the reset is complete, 266022547c4cSGuenter Roeck * so also wait for the connection to be re-established. 266122547c4cSGuenter Roeck */ 266222547c4cSGuenter Roeck if (!(portstatus & USB_PORT_STAT_RESET) && 266322547c4cSGuenter Roeck (portstatus & USB_PORT_STAT_CONNECTION)) 2664470f0be8SSarah Sharp break; 2665470f0be8SSarah Sharp 2666470f0be8SSarah Sharp /* switch to the long delay after two short delay failures */ 2667470f0be8SSarah Sharp if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 2668470f0be8SSarah Sharp delay = HUB_LONG_RESET_TIME; 2669470f0be8SSarah Sharp 2670d99f6b41SDan Williams dev_dbg(&hub->ports[port1 - 1]->dev, 2671d99f6b41SDan Williams "not %sreset yet, waiting %dms\n", 2672d99f6b41SDan Williams warm ? "warm " : "", delay); 2673470f0be8SSarah Sharp } 2674470f0be8SSarah Sharp 26754f43447eSSarah Sharp if ((portstatus & USB_PORT_STAT_RESET)) 2676470f0be8SSarah Sharp return -EBUSY; 26774f43447eSSarah Sharp 26783cd12f91SDan Williams if (hub_port_warm_reset_required(hub, port1, portstatus)) 2679a24a6078SSarah Sharp return -ENOTCONN; 268010d674a8SSarah Sharp 26811da177e4SLinus Torvalds /* Device went away? */ 26821da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_CONNECTION)) 26831da177e4SLinus Torvalds return -ENOTCONN; 26841da177e4SLinus Torvalds 2685a24a6078SSarah Sharp /* bomb out completely if the connection bounced. A USB 3.0 2686a24a6078SSarah Sharp * connection may bounce if multiple warm resets were issued, 2687a24a6078SSarah Sharp * but the device may have successfully re-connected. Ignore it. 2688a24a6078SSarah Sharp */ 2689a24a6078SSarah Sharp if (!hub_is_superspeed(hub->hdev) && 2690a24a6078SSarah Sharp (portchange & USB_PORT_STAT_C_CONNECTION)) 2691dd4dd19eSAlan Stern return -ENOTCONN; 26921da177e4SLinus Torvalds 2693470f0be8SSarah Sharp if (!(portstatus & USB_PORT_STAT_ENABLE)) 2694470f0be8SSarah Sharp return -EBUSY; 2695470f0be8SSarah Sharp 26962d4fa940SSarah Sharp if (!udev) 26972d4fa940SSarah Sharp return 0; 26982d4fa940SSarah Sharp 26990165de09SInaky Perez-Gonzalez if (hub_is_wusb(hub)) 2700551cdbbeSGreg Kroah-Hartman udev->speed = USB_SPEED_WIRELESS; 27010cdd49a1SMathias Nyman else if (hub_is_superspeedplus(hub->hdev) && 27020cdd49a1SMathias Nyman port_speed_is_ssp(hub->hdev, ext_portstatus & 27030cdd49a1SMathias Nyman USB_EXT_PORT_STAT_RX_SPEED_ID)) 27040cdd49a1SMathias Nyman udev->speed = USB_SPEED_SUPER_PLUS; 2705131dec34SSarah Sharp else if (hub_is_superspeed(hub->hdev)) 2706809cd1cbSSarah Sharp udev->speed = USB_SPEED_SUPER; 27070165de09SInaky Perez-Gonzalez else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 27081da177e4SLinus Torvalds udev->speed = USB_SPEED_HIGH; 27091da177e4SLinus Torvalds else if (portstatus & USB_PORT_STAT_LOW_SPEED) 27101da177e4SLinus Torvalds udev->speed = USB_SPEED_LOW; 27111da177e4SLinus Torvalds else 27121da177e4SLinus Torvalds udev->speed = USB_SPEED_FULL; 27131da177e4SLinus Torvalds return 0; 27141da177e4SLinus Torvalds } 27151da177e4SLinus Torvalds 271675d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 271775d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1, 271875d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm) 271975d7cf72SAndiry Xu { 272075d7cf72SAndiry Xu int i, status; 2721a24a6078SSarah Sharp u16 portchange, portstatus; 2722d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 272375d7cf72SAndiry Xu 272475d7cf72SAndiry Xu if (!hub_is_superspeed(hub->hdev)) { 27250fe51aa5SSarah Sharp if (warm) { 272675d7cf72SAndiry Xu dev_err(hub->intfdev, "only USB3 hub support " 272775d7cf72SAndiry Xu "warm reset\n"); 272875d7cf72SAndiry Xu return -EINVAL; 272975d7cf72SAndiry Xu } 27300fe51aa5SSarah Sharp /* Block EHCI CF initialization during the port reset. 27310fe51aa5SSarah Sharp * Some companion controllers don't like it when they mix. 27320fe51aa5SSarah Sharp */ 27330fe51aa5SSarah Sharp down_read(&ehci_cf_port_reset_rwsem); 2734d3b9d7a9SSarah Sharp } else if (!warm) { 2735d3b9d7a9SSarah Sharp /* 2736d3b9d7a9SSarah Sharp * If the caller hasn't explicitly requested a warm reset, 2737d3b9d7a9SSarah Sharp * double check and see if one is needed. 2738d3b9d7a9SSarah Sharp */ 2739fb6d1f7dSRobert Schlabbach if (hub_port_status(hub, port1, &portstatus, &portchange) == 0) 2740fb6d1f7dSRobert Schlabbach if (hub_port_warm_reset_required(hub, port1, 2741fb6d1f7dSRobert Schlabbach portstatus)) 2742d3b9d7a9SSarah Sharp warm = true; 274375d7cf72SAndiry Xu } 27443cd12f91SDan Williams clear_bit(port1, hub->warm_reset_bits); 274575d7cf72SAndiry Xu 274675d7cf72SAndiry Xu /* Reset the port */ 274775d7cf72SAndiry Xu for (i = 0; i < PORT_RESET_TRIES; i++) { 274875d7cf72SAndiry Xu status = set_port_feature(hub->hdev, port1, (warm ? 274975d7cf72SAndiry Xu USB_PORT_FEAT_BH_PORT_RESET : 275075d7cf72SAndiry Xu USB_PORT_FEAT_RESET)); 2751e9e88fb7SAlan Stern if (status == -ENODEV) { 2752e9e88fb7SAlan Stern ; /* The hub is gone */ 2753e9e88fb7SAlan Stern } else if (status) { 2754d99f6b41SDan Williams dev_err(&port_dev->dev, 2755d99f6b41SDan Williams "cannot %sreset (err = %d)\n", 2756d99f6b41SDan Williams warm ? "warm " : "", status); 275775d7cf72SAndiry Xu } else { 275875d7cf72SAndiry Xu status = hub_port_wait_reset(hub, port1, udev, delay, 275975d7cf72SAndiry Xu warm); 2760e9e88fb7SAlan Stern if (status && status != -ENOTCONN && status != -ENODEV) 276175d7cf72SAndiry Xu dev_dbg(hub->intfdev, 276275d7cf72SAndiry Xu "port_wait_reset: err = %d\n", 276375d7cf72SAndiry Xu status); 276475d7cf72SAndiry Xu } 276575d7cf72SAndiry Xu 2766a24a6078SSarah Sharp /* Check for disconnect or reset */ 276775d7cf72SAndiry Xu if (status == 0 || status == -ENOTCONN || status == -ENODEV) { 2768fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1, 2769fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_RESET); 2770a24a6078SSarah Sharp 2771a24a6078SSarah Sharp if (!hub_is_superspeed(hub->hdev)) 277232fe0198SAlan Stern goto done; 2773a24a6078SSarah Sharp 2774fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1, 2775fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_BH_PORT_RESET); 2776fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1, 2777fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_PORT_LINK_STATE); 2778fb6d1f7dSRobert Schlabbach usb_clear_port_feature(hub->hdev, port1, 2779fb6d1f7dSRobert Schlabbach USB_PORT_FEAT_C_CONNECTION); 2780fb6d1f7dSRobert Schlabbach 2781a24a6078SSarah Sharp /* 2782a24a6078SSarah Sharp * If a USB 3.0 device migrates from reset to an error 2783a24a6078SSarah Sharp * state, re-issue the warm reset. 2784a24a6078SSarah Sharp */ 2785a24a6078SSarah Sharp if (hub_port_status(hub, port1, 2786a24a6078SSarah Sharp &portstatus, &portchange) < 0) 2787a24a6078SSarah Sharp goto done; 2788a24a6078SSarah Sharp 27893cd12f91SDan Williams if (!hub_port_warm_reset_required(hub, port1, 27903cd12f91SDan Williams portstatus)) 2791a24a6078SSarah Sharp goto done; 2792a24a6078SSarah Sharp 2793a24a6078SSarah Sharp /* 2794a24a6078SSarah Sharp * If the port is in SS.Inactive or Compliance Mode, the 2795a24a6078SSarah Sharp * hot or warm reset failed. Try another warm reset. 2796a24a6078SSarah Sharp */ 2797a24a6078SSarah Sharp if (!warm) { 2798d99f6b41SDan Williams dev_dbg(&port_dev->dev, 2799d99f6b41SDan Williams "hot reset failed, warm reset\n"); 2800a24a6078SSarah Sharp warm = true; 2801a24a6078SSarah Sharp } 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 2804d99f6b41SDan Williams dev_dbg(&port_dev->dev, 2805d99f6b41SDan Williams "not enabled, trying %sreset again...\n", 2806d99f6b41SDan Williams warm ? "warm " : ""); 28071da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 2810d99f6b41SDan Williams dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); 28111da177e4SLinus Torvalds 281232fe0198SAlan Stern done: 2813fb6d1f7dSRobert Schlabbach if (status == 0) { 2814fb6d1f7dSRobert Schlabbach /* TRSTRCY = 10 ms; plus some extra */ 2815fb6d1f7dSRobert Schlabbach msleep(10 + 40); 2816fb6d1f7dSRobert Schlabbach if (udev) { 2817fb6d1f7dSRobert Schlabbach struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2818fb6d1f7dSRobert Schlabbach 2819fb6d1f7dSRobert Schlabbach update_devnum(udev, 0); 2820fb6d1f7dSRobert Schlabbach /* The xHC may think the device is already reset, 2821fb6d1f7dSRobert Schlabbach * so ignore the status. 2822fb6d1f7dSRobert Schlabbach */ 2823fb6d1f7dSRobert Schlabbach if (hcd->driver->reset_device) 2824fb6d1f7dSRobert Schlabbach hcd->driver->reset_device(hcd, udev); 2825fb6d1f7dSRobert Schlabbach 2826fb6d1f7dSRobert Schlabbach usb_set_device_state(udev, USB_STATE_DEFAULT); 2827fb6d1f7dSRobert Schlabbach } 2828fb6d1f7dSRobert Schlabbach } else { 2829fb6d1f7dSRobert Schlabbach if (udev) 2830fb6d1f7dSRobert Schlabbach usb_set_device_state(udev, USB_STATE_NOTATTACHED); 2831fb6d1f7dSRobert Schlabbach } 2832fb6d1f7dSRobert Schlabbach 28330fe51aa5SSarah Sharp if (!hub_is_superspeed(hub->hdev)) 283432fe0198SAlan Stern up_read(&ehci_cf_port_reset_rwsem); 283575d7cf72SAndiry Xu 28361da177e4SLinus Torvalds return status; 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 28390ed9a57eSAndiry Xu /* Check if a port is power on */ 28400ed9a57eSAndiry Xu static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) 28410ed9a57eSAndiry Xu { 28420ed9a57eSAndiry Xu int ret = 0; 28430ed9a57eSAndiry Xu 28440ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) { 28450ed9a57eSAndiry Xu if (portstatus & USB_SS_PORT_STAT_POWER) 28460ed9a57eSAndiry Xu ret = 1; 28470ed9a57eSAndiry Xu } else { 28480ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_POWER) 28490ed9a57eSAndiry Xu ret = 1; 28500ed9a57eSAndiry Xu } 28510ed9a57eSAndiry Xu 28520ed9a57eSAndiry Xu return ret; 28530ed9a57eSAndiry Xu } 28540ed9a57eSAndiry Xu 28555c79a1e3SDan Williams static void usb_lock_port(struct usb_port *port_dev) 28565c79a1e3SDan Williams __acquires(&port_dev->status_lock) 28575c79a1e3SDan Williams { 28585c79a1e3SDan Williams mutex_lock(&port_dev->status_lock); 28595c79a1e3SDan Williams __acquire(&port_dev->status_lock); 28605c79a1e3SDan Williams } 28615c79a1e3SDan Williams 28625c79a1e3SDan Williams static void usb_unlock_port(struct usb_port *port_dev) 28635c79a1e3SDan Williams __releases(&port_dev->status_lock) 28645c79a1e3SDan Williams { 28655c79a1e3SDan Williams mutex_unlock(&port_dev->status_lock); 28665c79a1e3SDan Williams __release(&port_dev->status_lock); 28675c79a1e3SDan Williams } 28685c79a1e3SDan Williams 2869d388dab7SAlan Stern #ifdef CONFIG_PM 28701da177e4SLinus Torvalds 28710ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 28720ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 28730ed9a57eSAndiry Xu { 28740ed9a57eSAndiry Xu int ret = 0; 28750ed9a57eSAndiry Xu 28760ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) { 28770ed9a57eSAndiry Xu if ((portstatus & USB_PORT_STAT_LINK_STATE) 28780ed9a57eSAndiry Xu == USB_SS_PORT_LS_U3) 28790ed9a57eSAndiry Xu ret = 1; 28800ed9a57eSAndiry Xu } else { 28810ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_SUSPEND) 28820ed9a57eSAndiry Xu ret = 1; 28830ed9a57eSAndiry Xu } 28840ed9a57eSAndiry Xu 28850ed9a57eSAndiry Xu return ret; 28860ed9a57eSAndiry Xu } 2887b01b03f3SAlan Stern 2888b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume, 2889b01b03f3SAlan Stern * is ready for a reset-resume, or should be disconnected. 2890b01b03f3SAlan Stern */ 2891b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev, 2892b01b03f3SAlan Stern struct usb_hub *hub, int port1, 28937fa40910SJulius Werner int status, u16 portchange, u16 portstatus) 2894b01b03f3SAlan Stern { 2895d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 28967fa40910SJulius Werner int retries = 3; 2897d99f6b41SDan Williams 28987fa40910SJulius Werner retry: 28993cd12f91SDan Williams /* Is a warm reset needed to recover the connection? */ 29003cd12f91SDan Williams if (status == 0 && udev->reset_resume 29013cd12f91SDan Williams && hub_port_warm_reset_required(hub, port1, portstatus)) { 29023cd12f91SDan Williams /* pass */; 29033cd12f91SDan Williams } 2904b01b03f3SAlan Stern /* Is the device still present? */ 29053cd12f91SDan Williams else if (status || port_is_suspended(hub, portstatus) || 29067fa40910SJulius Werner !port_is_power_on(hub, portstatus)) { 2907b01b03f3SAlan Stern if (status >= 0) 2908b01b03f3SAlan Stern status = -ENODEV; 29097fa40910SJulius Werner } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 29107fa40910SJulius Werner if (retries--) { 29117fa40910SJulius Werner usleep_range(200, 300); 29127fa40910SJulius Werner status = hub_port_status(hub, port1, &portstatus, 29137fa40910SJulius Werner &portchange); 29147fa40910SJulius Werner goto retry; 29157fa40910SJulius Werner } 29167fa40910SJulius Werner status = -ENODEV; 2917b01b03f3SAlan Stern } 2918b01b03f3SAlan Stern 291986c57edfSAlan Stern /* Can't do a normal resume if the port isn't enabled, 292086c57edfSAlan Stern * so try a reset-resume instead. 292186c57edfSAlan Stern */ 292286c57edfSAlan Stern else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 292386c57edfSAlan Stern if (udev->persist_enabled) 292486c57edfSAlan Stern udev->reset_resume = 1; 292586c57edfSAlan Stern else 2926b01b03f3SAlan Stern status = -ENODEV; 292786c57edfSAlan Stern } 2928b01b03f3SAlan Stern 2929b01b03f3SAlan Stern if (status) { 2930d99f6b41SDan Williams dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", 2931d99f6b41SDan Williams portchange, portstatus, status); 2932b01b03f3SAlan Stern } else if (udev->reset_resume) { 2933b01b03f3SAlan Stern 2934b01b03f3SAlan Stern /* Late port handoff can set status-change bits */ 2935b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) 2936ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 2937b01b03f3SAlan Stern USB_PORT_FEAT_C_CONNECTION); 2938b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) 2939ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 2940b01b03f3SAlan Stern USB_PORT_FEAT_C_ENABLE); 2941b01b03f3SAlan Stern } 2942b01b03f3SAlan Stern 2943b01b03f3SAlan Stern return status; 2944b01b03f3SAlan Stern } 2945b01b03f3SAlan Stern 2946f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev) 2947f74631e3SSarah Sharp { 2948f74631e3SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2949f74631e3SSarah Sharp 2950f74631e3SSarah Sharp /* Check if the roothub and device supports LTM. */ 2951f74631e3SSarah Sharp if (!usb_device_supports_ltm(hcd->self.root_hub) || 2952f74631e3SSarah Sharp !usb_device_supports_ltm(udev)) 2953f74631e3SSarah Sharp return 0; 2954f74631e3SSarah Sharp 2955f74631e3SSarah Sharp /* Clear Feature LTM Enable can only be sent if the device is 2956f74631e3SSarah Sharp * configured. 2957f74631e3SSarah Sharp */ 2958f74631e3SSarah Sharp if (!udev->actconfig) 2959f74631e3SSarah Sharp return 0; 2960f74631e3SSarah Sharp 2961f74631e3SSarah Sharp return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2962f74631e3SSarah Sharp USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 2963f74631e3SSarah Sharp USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 2964f74631e3SSarah Sharp USB_CTRL_SET_TIMEOUT); 2965f74631e3SSarah Sharp } 2966f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm); 2967f74631e3SSarah Sharp 2968f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) 2969f74631e3SSarah Sharp { 2970f74631e3SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2971f74631e3SSarah Sharp 2972f74631e3SSarah Sharp /* Check if the roothub and device supports LTM. */ 2973f74631e3SSarah Sharp if (!usb_device_supports_ltm(hcd->self.root_hub) || 2974f74631e3SSarah Sharp !usb_device_supports_ltm(udev)) 2975f74631e3SSarah Sharp return; 2976f74631e3SSarah Sharp 2977f74631e3SSarah Sharp /* Set Feature LTM Enable can only be sent if the device is 2978f74631e3SSarah Sharp * configured. 2979f74631e3SSarah Sharp */ 2980f74631e3SSarah Sharp if (!udev->actconfig) 2981f74631e3SSarah Sharp return; 2982f74631e3SSarah Sharp 2983f74631e3SSarah Sharp usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2984f74631e3SSarah Sharp USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 2985f74631e3SSarah Sharp USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 2986f74631e3SSarah Sharp USB_CTRL_SET_TIMEOUT); 2987f74631e3SSarah Sharp } 2988f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm); 2989f74631e3SSarah Sharp 299054a3ac0cSLan Tianyu /* 299128e86165SAlan Stern * usb_enable_remote_wakeup - enable remote wakeup for a device 299254a3ac0cSLan Tianyu * @udev: target device 299354a3ac0cSLan Tianyu * 299428e86165SAlan Stern * For USB-2 devices: Set the device's remote wakeup feature. 299528e86165SAlan Stern * 299628e86165SAlan Stern * For USB-3 devices: Assume there's only one function on the device and 299728e86165SAlan Stern * enable remote wake for the first interface. FIXME if the interface 299828e86165SAlan Stern * association descriptor shows there's more than one function. 299954a3ac0cSLan Tianyu */ 300028e86165SAlan Stern static int usb_enable_remote_wakeup(struct usb_device *udev) 300154a3ac0cSLan Tianyu { 300228e86165SAlan Stern if (udev->speed < USB_SPEED_SUPER) 300328e86165SAlan Stern return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 300428e86165SAlan Stern USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 300528e86165SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 300628e86165SAlan Stern USB_CTRL_SET_TIMEOUT); 300728e86165SAlan Stern else 300828e86165SAlan Stern return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 300928e86165SAlan Stern USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 301028e86165SAlan Stern USB_INTRF_FUNC_SUSPEND, 301128e86165SAlan Stern USB_INTRF_FUNC_SUSPEND_RW | 301228e86165SAlan Stern USB_INTRF_FUNC_SUSPEND_LP, 301328e86165SAlan Stern NULL, 0, USB_CTRL_SET_TIMEOUT); 301428e86165SAlan Stern } 301528e86165SAlan Stern 301628e86165SAlan Stern /* 301728e86165SAlan Stern * usb_disable_remote_wakeup - disable remote wakeup for a device 301828e86165SAlan Stern * @udev: target device 301928e86165SAlan Stern * 302028e86165SAlan Stern * For USB-2 devices: Clear the device's remote wakeup feature. 302128e86165SAlan Stern * 302228e86165SAlan Stern * For USB-3 devices: Assume there's only one function on the device and 302328e86165SAlan Stern * disable remote wake for the first interface. FIXME if the interface 302428e86165SAlan Stern * association descriptor shows there's more than one function. 302528e86165SAlan Stern */ 302628e86165SAlan Stern static int usb_disable_remote_wakeup(struct usb_device *udev) 302728e86165SAlan Stern { 302828e86165SAlan Stern if (udev->speed < USB_SPEED_SUPER) 302928e86165SAlan Stern return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 303028e86165SAlan Stern USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 303128e86165SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 303228e86165SAlan Stern USB_CTRL_SET_TIMEOUT); 303328e86165SAlan Stern else 303454a3ac0cSLan Tianyu return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 30354e248000SYonglong Wu USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 303654a3ac0cSLan Tianyu USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, 303754a3ac0cSLan Tianyu USB_CTRL_SET_TIMEOUT); 303854a3ac0cSLan Tianyu } 30391da177e4SLinus Torvalds 3040e583d9dbSAlan Stern /* Count of wakeup-enabled devices at or below udev */ 3041e583d9dbSAlan Stern static unsigned wakeup_enabled_descendants(struct usb_device *udev) 3042e583d9dbSAlan Stern { 3043e583d9dbSAlan Stern struct usb_hub *hub = usb_hub_to_struct_hub(udev); 3044e583d9dbSAlan Stern 3045e583d9dbSAlan Stern return udev->do_remote_wakeup + 3046e583d9dbSAlan Stern (hub ? hub->wakeup_enabled_descendants : 0); 3047e583d9dbSAlan Stern } 3048e583d9dbSAlan Stern 30491da177e4SLinus Torvalds /* 3050140d8f68SAlan Stern * usb_port_suspend - suspend a usb device's upstream port 3051624d6c07SAlan Stern * @udev: device that's no longer in active use, not a root hub 30525edbfb7cSDavid Brownell * Context: must be able to sleep; device not locked; pm locks held 30531da177e4SLinus Torvalds * 30541da177e4SLinus Torvalds * Suspends a USB device that isn't in active use, conserving power. 30551da177e4SLinus Torvalds * Devices may wake out of a suspend, if anything important happens, 30561da177e4SLinus Torvalds * using the remote wakeup mechanism. They may also be taken out of 3057140d8f68SAlan Stern * suspend by the host, using usb_port_resume(). It's also routine 30581da177e4SLinus Torvalds * to disconnect devices while they are suspended. 30591da177e4SLinus Torvalds * 3060390a8c34SDavid Brownell * This only affects the USB hardware for a device; its interfaces 3061390a8c34SDavid Brownell * (and, for hubs, child devices) must already have been suspended. 3062390a8c34SDavid Brownell * 3063624d6c07SAlan Stern * Selective port suspend reduces power; most suspended devices draw 3064624d6c07SAlan Stern * less than 500 uA. It's also used in OTG, along with remote wakeup. 3065624d6c07SAlan Stern * All devices below the suspended port are also suspended. 3066624d6c07SAlan Stern * 3067624d6c07SAlan Stern * Devices leave suspend state when the host wakes them up. Some devices 3068624d6c07SAlan Stern * also support "remote wakeup", where the device can activate the USB 3069624d6c07SAlan Stern * tree above them to deliver data, such as a keypress or packet. In 3070624d6c07SAlan Stern * some cases, this wakes the USB host. 3071624d6c07SAlan Stern * 30721da177e4SLinus Torvalds * Suspending OTG devices may trigger HNP, if that's been enabled 30731da177e4SLinus Torvalds * between a pair of dual-role devices. That will change roles, such 30741da177e4SLinus Torvalds * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 30751da177e4SLinus Torvalds * 30764956eccdSAlan Stern * Devices on USB hub ports have only one "suspend" state, corresponding 30774956eccdSAlan Stern * to ACPI D2, "may cause the device to lose some context". 30784956eccdSAlan Stern * State transitions include: 30794956eccdSAlan Stern * 30804956eccdSAlan Stern * - suspend, resume ... when the VBUS power link stays live 30814956eccdSAlan Stern * - suspend, disconnect ... VBUS lost 30824956eccdSAlan Stern * 30834956eccdSAlan Stern * Once VBUS drop breaks the circuit, the port it's using has to go through 30844956eccdSAlan Stern * normal re-enumeration procedures, starting with enabling VBUS power. 30854956eccdSAlan Stern * Other than re-initializing the hub (plug/unplug, except for root hubs), 308637ebb549SPetr Mladek * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq 30874956eccdSAlan Stern * timer, no SRP, no requests through sysfs. 30884956eccdSAlan Stern * 3089e583d9dbSAlan Stern * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get 3090e583d9dbSAlan Stern * suspended until their bus goes into global suspend (i.e., the root 30910aa2832dSAlan Stern * hub is suspended). Nevertheless, we change @udev->state to 30920aa2832dSAlan Stern * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual 30930aa2832dSAlan Stern * upstream port setting is stored in @udev->port_is_suspended. 30944956eccdSAlan Stern * 30951da177e4SLinus Torvalds * Returns 0 on success, else negative errno. 30961da177e4SLinus Torvalds */ 309765bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 30981da177e4SLinus Torvalds { 3099ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3100ad493e5eSLan Tianyu struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3101624d6c07SAlan Stern int port1 = udev->portnum; 3102624d6c07SAlan Stern int status; 31030aa2832dSAlan Stern bool really_suspend = true; 31044956eccdSAlan Stern 31055c79a1e3SDan Williams usb_lock_port(port_dev); 31065c79a1e3SDan Williams 3107624d6c07SAlan Stern /* enable remote wakeup when appropriate; this lets the device 3108624d6c07SAlan Stern * wake up the upstream hub (including maybe the root hub). 3109624d6c07SAlan Stern * 3110624d6c07SAlan Stern * NOTE: OTG devices may issue remote wakeup (or SRP) even when 3111624d6c07SAlan Stern * we don't explicitly enable it here. 3112624d6c07SAlan Stern */ 3113624d6c07SAlan Stern if (udev->do_remote_wakeup) { 311428e86165SAlan Stern status = usb_enable_remote_wakeup(udev); 31150c487206SOliver Neukum if (status) { 3116624d6c07SAlan Stern dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 3117624d6c07SAlan Stern status); 31180c487206SOliver Neukum /* bail if autosuspend is requested */ 31195b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg)) 31204fae6f0fSAlan Stern goto err_wakeup; 31210c487206SOliver Neukum } 3122624d6c07SAlan Stern } 3123624d6c07SAlan Stern 312465580b43SAndiry Xu /* disable USB2 hardware LPM */ 312565580b43SAndiry Xu if (udev->usb2_hw_lpm_enabled == 1) 312665580b43SAndiry Xu usb_set_usb2_hardware_lpm(udev, 0); 312765580b43SAndiry Xu 3128f74631e3SSarah Sharp if (usb_disable_ltm(udev)) { 31294fae6f0fSAlan Stern dev_err(&udev->dev, "Failed to disable LTM before suspend\n."); 31304fae6f0fSAlan Stern status = -ENOMEM; 31314fae6f0fSAlan Stern if (PMSG_IS_AUTO(msg)) 31324fae6f0fSAlan Stern goto err_ltm; 3133f74631e3SSarah Sharp } 31348306095fSSarah Sharp if (usb_unlocked_disable_lpm(udev)) { 31354fae6f0fSAlan Stern dev_err(&udev->dev, "Failed to disable LPM before suspend\n."); 31364fae6f0fSAlan Stern status = -ENOMEM; 31374fae6f0fSAlan Stern if (PMSG_IS_AUTO(msg)) 31384fae6f0fSAlan Stern goto err_lpm3; 31398306095fSSarah Sharp } 31408306095fSSarah Sharp 3141624d6c07SAlan Stern /* see 7.1.7.6 */ 3142a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) 3143c2f60db7SSarah Sharp status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); 3144e583d9dbSAlan Stern 31450aa2832dSAlan Stern /* 31460aa2832dSAlan Stern * For system suspend, we do not need to enable the suspend feature 31470aa2832dSAlan Stern * on individual USB-2 ports. The devices will automatically go 31480aa2832dSAlan Stern * into suspend a few ms after the root hub stops sending packets. 31490aa2832dSAlan Stern * The USB 2.0 spec calls this "global suspend". 3150e583d9dbSAlan Stern * 3151e583d9dbSAlan Stern * However, many USB hubs have a bug: They don't relay wakeup requests 3152e583d9dbSAlan Stern * from a downstream port if the port's suspend feature isn't on. 3153e583d9dbSAlan Stern * Therefore we will turn on the suspend feature if udev or any of its 3154e583d9dbSAlan Stern * descendants is enabled for remote wakeup. 31550aa2832dSAlan Stern */ 3156e583d9dbSAlan Stern else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0) 3157e583d9dbSAlan Stern status = set_port_feature(hub->hdev, port1, 3158e583d9dbSAlan Stern USB_PORT_FEAT_SUSPEND); 31590aa2832dSAlan Stern else { 31600aa2832dSAlan Stern really_suspend = false; 31610aa2832dSAlan Stern status = 0; 31620aa2832dSAlan Stern } 3163624d6c07SAlan Stern if (status) { 3164d99f6b41SDan Williams dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); 3165cbb33004SAlan Stern 31664fae6f0fSAlan Stern /* Try to enable USB3 LPM and LTM again */ 31674fae6f0fSAlan Stern usb_unlocked_enable_lpm(udev); 31684fae6f0fSAlan Stern err_lpm3: 31694fae6f0fSAlan Stern usb_enable_ltm(udev); 31704fae6f0fSAlan Stern err_ltm: 3171c3e751e4SAndiry Xu /* Try to enable USB2 hardware LPM again */ 3172c3e751e4SAndiry Xu if (udev->usb2_hw_lpm_capable == 1) 3173c3e751e4SAndiry Xu usb_set_usb2_hardware_lpm(udev, 1); 3174c3e751e4SAndiry Xu 31754fae6f0fSAlan Stern if (udev->do_remote_wakeup) 31764fae6f0fSAlan Stern (void) usb_disable_remote_wakeup(udev); 31774fae6f0fSAlan Stern err_wakeup: 31788306095fSSarah Sharp 3179cbb33004SAlan Stern /* System sleep transitions should never fail */ 31805b1b0b81SAlan Stern if (!PMSG_IS_AUTO(msg)) 3181cbb33004SAlan Stern status = 0; 3182624d6c07SAlan Stern } else { 318330b1a7a3SAlan Stern dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 318430b1a7a3SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : ""), 318530b1a7a3SAlan Stern udev->do_remote_wakeup); 31860aa2832dSAlan Stern if (really_suspend) { 3187bfd1e910SAlan Stern udev->port_is_suspended = 1; 3188e583d9dbSAlan Stern 3189e583d9dbSAlan Stern /* device has up to 10 msec to fully suspend */ 3190624d6c07SAlan Stern msleep(10); 3191624d6c07SAlan Stern } 3192e583d9dbSAlan Stern usb_set_device_state(udev, USB_STATE_SUSPENDED); 31930aa2832dSAlan Stern } 3194ad493e5eSLan Tianyu 3195d5c3834eSDan Williams if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled 3196d5c3834eSDan Williams && test_and_clear_bit(port1, hub->child_usage_bits)) 3197ad493e5eSLan Tianyu pm_runtime_put_sync(&port_dev->dev); 3198ad493e5eSLan Tianyu 3199c08512c7SAlan Stern usb_mark_last_busy(hub->hdev); 32005c79a1e3SDan Williams 32015c79a1e3SDan Williams usb_unlock_port(port_dev); 32024956eccdSAlan Stern return status; 32031da177e4SLinus Torvalds } 3204f3f3253dSDavid Brownell 32051da177e4SLinus Torvalds /* 3206390a8c34SDavid Brownell * If the USB "suspend" state is in use (rather than "global suspend"), 3207390a8c34SDavid Brownell * many devices will be individually taken out of suspend state using 320854515fe5SAlan Stern * special "resume" signaling. This routine kicks in shortly after 32091da177e4SLinus Torvalds * hardware resume signaling is finished, either because of selective 32101da177e4SLinus Torvalds * resume (by host) or remote wakeup (by device) ... now see what changed 32111da177e4SLinus Torvalds * in the tree that's rooted at this device. 321254515fe5SAlan Stern * 321354515fe5SAlan Stern * If @udev->reset_resume is set then the device is reset before the 321454515fe5SAlan Stern * status check is done. 32151da177e4SLinus Torvalds */ 3216140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev) 32171da177e4SLinus Torvalds { 321854515fe5SAlan Stern int status = 0; 321907e72b95SOliver Neukum u16 devstatus = 0; 32201da177e4SLinus Torvalds 32211da177e4SLinus Torvalds /* caller owns the udev device lock */ 3222b9cef6c3SWu Fengguang dev_dbg(&udev->dev, "%s\n", 3223b9cef6c3SWu Fengguang udev->reset_resume ? "finish reset-resume" : "finish resume"); 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds /* usb ch9 identifies four variants of SUSPENDED, based on what 32261da177e4SLinus Torvalds * state the device resumes to. Linux currently won't see the 32271da177e4SLinus Torvalds * first two on the host side; they'd be inside hub_port_init() 322837ebb549SPetr Mladek * during many timeouts, but hub_wq can't suspend until later. 32291da177e4SLinus Torvalds */ 32301da177e4SLinus Torvalds usb_set_device_state(udev, udev->actconfig 32311da177e4SLinus Torvalds ? USB_STATE_CONFIGURED 32321da177e4SLinus Torvalds : USB_STATE_ADDRESS); 32331da177e4SLinus Torvalds 323454515fe5SAlan Stern /* 10.5.4.5 says not to reset a suspended port if the attached 323554515fe5SAlan Stern * device is enabled for remote wakeup. Hence the reset 323654515fe5SAlan Stern * operation is carried out here, after the port has been 323754515fe5SAlan Stern * resumed. 323854515fe5SAlan Stern */ 32391d10255cSAlan Stern if (udev->reset_resume) { 32401d10255cSAlan Stern /* 32411d10255cSAlan Stern * If the device morphs or switches modes when it is reset, 32421d10255cSAlan Stern * we don't want to perform a reset-resume. We'll fail the 32431d10255cSAlan Stern * resume, which will cause a logical disconnect, and then 32441d10255cSAlan Stern * the device will be rediscovered. 32451d10255cSAlan Stern */ 324686c57edfSAlan Stern retry_reset_resume: 32471d10255cSAlan Stern if (udev->quirks & USB_QUIRK_RESET) 32481d10255cSAlan Stern status = -ENODEV; 32491d10255cSAlan Stern else 3250742120c6SMing Lei status = usb_reset_and_verify_device(udev); 32511d10255cSAlan Stern } 325254515fe5SAlan Stern 32531da177e4SLinus Torvalds /* 10.5.4.5 says be sure devices in the tree are still there. 32541da177e4SLinus Torvalds * For now let's assume the device didn't go crazy on resume, 32551da177e4SLinus Torvalds * and device drivers will know about any resume quirks. 32561da177e4SLinus Torvalds */ 325754515fe5SAlan Stern if (status == 0) { 325846dede46SAlan Stern devstatus = 0; 32591da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 326086c57edfSAlan Stern 326186c57edfSAlan Stern /* If a normal resume failed, try doing a reset-resume */ 326286c57edfSAlan Stern if (status && !udev->reset_resume && udev->persist_enabled) { 326386c57edfSAlan Stern dev_dbg(&udev->dev, "retry with reset-resume\n"); 326486c57edfSAlan Stern udev->reset_resume = 1; 326586c57edfSAlan Stern goto retry_reset_resume; 326686c57edfSAlan Stern } 326754515fe5SAlan Stern } 3268b40b7a90SAlan Stern 3269624d6c07SAlan Stern if (status) { 3270624d6c07SAlan Stern dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 32711da177e4SLinus Torvalds status); 327207e72b95SOliver Neukum /* 327307e72b95SOliver Neukum * There are a few quirky devices which violate the standard 327407e72b95SOliver Neukum * by claiming to have remote wakeup enabled after a reset, 327507e72b95SOliver Neukum * which crash if the feature is cleared, hence check for 327607e72b95SOliver Neukum * udev->reset_resume 327707e72b95SOliver Neukum */ 327807e72b95SOliver Neukum } else if (udev->actconfig && !udev->reset_resume) { 327928e86165SAlan Stern if (udev->speed < USB_SPEED_SUPER) { 328054a3ac0cSLan Tianyu if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) 328128e86165SAlan Stern status = usb_disable_remote_wakeup(udev); 328254a3ac0cSLan Tianyu } else { 328354a3ac0cSLan Tianyu status = usb_get_status(udev, USB_RECIP_INTERFACE, 0, 328454a3ac0cSLan Tianyu &devstatus); 328554a3ac0cSLan Tianyu if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP 328654a3ac0cSLan Tianyu | USB_INTRF_STAT_FUNC_RW)) 328728e86165SAlan Stern status = usb_disable_remote_wakeup(udev); 328854a3ac0cSLan Tianyu } 328954a3ac0cSLan Tianyu 3290a8e7c565SAlan Stern if (status) 3291b9cef6c3SWu Fengguang dev_dbg(&udev->dev, 3292b9cef6c3SWu Fengguang "disable remote wakeup, status %d\n", 3293b9cef6c3SWu Fengguang status); 32941da177e4SLinus Torvalds status = 0; 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds return status; 32971da177e4SLinus Torvalds } 32981da177e4SLinus Torvalds 3299624d6c07SAlan Stern /* 3300a40178b2SPratyush Anand * There are some SS USB devices which take longer time for link training. 3301a40178b2SPratyush Anand * XHCI specs 4.19.4 says that when Link training is successful, port 33026b82b122SAl Cooper * sets CCS bit to 1. So if SW reads port status before successful link 3303a40178b2SPratyush Anand * training, then it will not find device to be present. 3304a40178b2SPratyush Anand * USB Analyzer log with such buggy devices show that in some cases 3305a40178b2SPratyush Anand * device switch on the RX termination after long delay of host enabling 3306a40178b2SPratyush Anand * the VBUS. In few other cases it has been seen that device fails to 3307a40178b2SPratyush Anand * negotiate link training in first attempt. It has been 3308a40178b2SPratyush Anand * reported till now that few devices take as long as 2000 ms to train 3309a40178b2SPratyush Anand * the link after host enabling its VBUS and termination. Following 3310a40178b2SPratyush Anand * routine implements a 2000 ms timeout for link training. If in a case 3311a40178b2SPratyush Anand * link trains before timeout, loop will exit earlier. 3312a40178b2SPratyush Anand * 33136b82b122SAl Cooper * There are also some 2.0 hard drive based devices and 3.0 thumb 33146b82b122SAl Cooper * drives that, when plugged into a 2.0 only port, take a long 33156b82b122SAl Cooper * time to set CCS after VBUS enable. 33166b82b122SAl Cooper * 3317a40178b2SPratyush Anand * FIXME: If a device was connected before suspend, but was removed 3318a40178b2SPratyush Anand * while system was asleep, then the loop in the following routine will 3319a40178b2SPratyush Anand * only exit at timeout. 3320a40178b2SPratyush Anand * 33216b82b122SAl Cooper * This routine should only be called when persist is enabled. 3322a40178b2SPratyush Anand */ 33236b82b122SAl Cooper static int wait_for_connected(struct usb_device *udev, 3324a40178b2SPratyush Anand struct usb_hub *hub, int *port1, 3325a40178b2SPratyush Anand u16 *portchange, u16 *portstatus) 3326a40178b2SPratyush Anand { 3327a40178b2SPratyush Anand int status = 0, delay_ms = 0; 3328a40178b2SPratyush Anand 3329a40178b2SPratyush Anand while (delay_ms < 2000) { 3330a40178b2SPratyush Anand if (status || *portstatus & USB_PORT_STAT_CONNECTION) 3331a40178b2SPratyush Anand break; 3332a40178b2SPratyush Anand msleep(20); 3333a40178b2SPratyush Anand delay_ms += 20; 3334a40178b2SPratyush Anand status = hub_port_status(hub, *port1, portstatus, portchange); 3335a40178b2SPratyush Anand } 33366b82b122SAl Cooper dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); 3337a40178b2SPratyush Anand return status; 3338a40178b2SPratyush Anand } 3339a40178b2SPratyush Anand 3340a40178b2SPratyush Anand /* 3341624d6c07SAlan Stern * usb_port_resume - re-activate a suspended usb device's upstream port 3342624d6c07SAlan Stern * @udev: device to re-activate, not a root hub 3343624d6c07SAlan Stern * Context: must be able to sleep; device not locked; pm locks held 3344624d6c07SAlan Stern * 3345624d6c07SAlan Stern * This will re-activate the suspended device, increasing power usage 3346624d6c07SAlan Stern * while letting drivers communicate again with its endpoints. 3347624d6c07SAlan Stern * USB resume explicitly guarantees that the power session between 3348624d6c07SAlan Stern * the host and the device is the same as it was when the device 3349624d6c07SAlan Stern * suspended. 3350624d6c07SAlan Stern * 3351feccc30dSAlan Stern * If @udev->reset_resume is set then this routine won't check that the 3352feccc30dSAlan Stern * port is still enabled. Furthermore, finish_port_resume() above will 335354515fe5SAlan Stern * reset @udev. The end result is that a broken power session can be 335454515fe5SAlan Stern * recovered and @udev will appear to persist across a loss of VBUS power. 335554515fe5SAlan Stern * 335654515fe5SAlan Stern * For example, if a host controller doesn't maintain VBUS suspend current 335754515fe5SAlan Stern * during a system sleep or is reset when the system wakes up, all the USB 335854515fe5SAlan Stern * power sessions below it will be broken. This is especially troublesome 335954515fe5SAlan Stern * for mass-storage devices containing mounted filesystems, since the 336054515fe5SAlan Stern * device will appear to have disconnected and all the memory mappings 336154515fe5SAlan Stern * to it will be lost. Using the USB_PERSIST facility, the device can be 336254515fe5SAlan Stern * made to appear as if it had not disconnected. 336354515fe5SAlan Stern * 3364742120c6SMing Lei * This facility can be dangerous. Although usb_reset_and_verify_device() makes 3365feccc30dSAlan Stern * every effort to insure that the same device is present after the 336654515fe5SAlan Stern * reset as before, it cannot provide a 100% guarantee. Furthermore it's 336754515fe5SAlan Stern * quite possible for a device to remain unaltered but its media to be 336854515fe5SAlan Stern * changed. If the user replaces a flash memory card while the system is 336954515fe5SAlan Stern * asleep, he will have only himself to blame when the filesystem on the 337054515fe5SAlan Stern * new card is corrupted and the system crashes. 337154515fe5SAlan Stern * 3372624d6c07SAlan Stern * Returns 0 on success, else negative errno. 3373624d6c07SAlan Stern */ 337465bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 33751da177e4SLinus Torvalds { 3376ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3377ad493e5eSLan Tianyu struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3378624d6c07SAlan Stern int port1 = udev->portnum; 33791da177e4SLinus Torvalds int status; 3380d25450c6SAlan Stern u16 portchange, portstatus; 3381d25450c6SAlan Stern 3382d5c3834eSDan Williams if (!test_and_set_bit(port1, hub->child_usage_bits)) { 3383ad493e5eSLan Tianyu status = pm_runtime_get_sync(&port_dev->dev); 3384ad493e5eSLan Tianyu if (status < 0) { 3385ad493e5eSLan Tianyu dev_dbg(&udev->dev, "can't resume usb port, status %d\n", 3386ad493e5eSLan Tianyu status); 3387ad493e5eSLan Tianyu return status; 3388ad493e5eSLan Tianyu } 3389ad493e5eSLan Tianyu } 3390ad493e5eSLan Tianyu 33915c79a1e3SDan Williams usb_lock_port(port_dev); 33925c79a1e3SDan Williams 3393d25450c6SAlan Stern /* Skip the initial Clear-Suspend step for a remote wakeup */ 3394d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 33950ed9a57eSAndiry Xu if (status == 0 && !port_is_suspended(hub, portstatus)) 3396d25450c6SAlan Stern goto SuspendCleared; 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds /* see 7.1.7.7; affects power usage, but not budgeting */ 3399a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) 3400c2f60db7SSarah Sharp status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); 3401a7114230SAndiry Xu else 3402ad493e5eSLan Tianyu status = usb_clear_port_feature(hub->hdev, 34031da177e4SLinus Torvalds port1, USB_PORT_FEAT_SUSPEND); 34041da177e4SLinus Torvalds if (status) { 3405d99f6b41SDan Williams dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); 34061da177e4SLinus Torvalds } else { 3407bbc78c07SFelipe Balbi /* drive resume for USB_RESUME_TIMEOUT msec */ 3408645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", 34095b1b0b81SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : "")); 3410bbc78c07SFelipe Balbi msleep(USB_RESUME_TIMEOUT); 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds /* Virtual root hubs can trigger on GET_PORT_STATUS to 34131da177e4SLinus Torvalds * stop resume signaling. Then finish the resume 34141da177e4SLinus Torvalds * sequence. 34151da177e4SLinus Torvalds */ 3416d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 341754515fe5SAlan Stern 34181da177e4SLinus Torvalds /* TRSMRCY = 10 msec */ 34191da177e4SLinus Torvalds msleep(10); 34201da177e4SLinus Torvalds } 3421b01b03f3SAlan Stern 3422b01b03f3SAlan Stern SuspendCleared: 3423b01b03f3SAlan Stern if (status == 0) { 3424bfd1e910SAlan Stern udev->port_is_suspended = 0; 3425a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) { 3426a7114230SAndiry Xu if (portchange & USB_PORT_STAT_C_LINK_STATE) 3427ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 3428a7114230SAndiry Xu USB_PORT_FEAT_C_PORT_LINK_STATE); 3429a7114230SAndiry Xu } else { 3430b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_SUSPEND) 3431ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 3432b01b03f3SAlan Stern USB_PORT_FEAT_C_SUSPEND); 34331da177e4SLinus Torvalds } 3434a7114230SAndiry Xu } 34351da177e4SLinus Torvalds 34366b82b122SAl Cooper if (udev->persist_enabled) 34376b82b122SAl Cooper status = wait_for_connected(udev, hub, &port1, &portchange, 3438a40178b2SPratyush Anand &portstatus); 3439a40178b2SPratyush Anand 3440b01b03f3SAlan Stern status = check_port_resume_type(udev, 3441b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 344254515fe5SAlan Stern if (status == 0) 344354515fe5SAlan Stern status = finish_port_resume(udev); 344454515fe5SAlan Stern if (status < 0) { 344554515fe5SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 344654515fe5SAlan Stern hub_port_logical_disconnect(hub, port1); 344765580b43SAndiry Xu } else { 344865580b43SAndiry Xu /* Try to enable USB2 hardware LPM */ 344965580b43SAndiry Xu if (udev->usb2_hw_lpm_capable == 1) 345065580b43SAndiry Xu usb_set_usb2_hardware_lpm(udev, 1); 34518306095fSSarah Sharp 3452f74631e3SSarah Sharp /* Try to enable USB3 LTM and LPM */ 3453f74631e3SSarah Sharp usb_enable_ltm(udev); 34548306095fSSarah Sharp usb_unlocked_enable_lpm(udev); 345554515fe5SAlan Stern } 345665580b43SAndiry Xu 34575c79a1e3SDan Williams usb_unlock_port(port_dev); 34585c79a1e3SDan Williams 34591da177e4SLinus Torvalds return status; 34601da177e4SLinus Torvalds } 34611da177e4SLinus Torvalds 34620534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev) 34631da177e4SLinus Torvalds { 34641da177e4SLinus Torvalds int status = 0; 34651da177e4SLinus Torvalds 34665c79a1e3SDan Williams usb_lock_device(udev); 34671da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED) { 3468645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 34699bbdf1e0SAlan Stern status = usb_autoresume_device(udev); 34709bbdf1e0SAlan Stern if (status == 0) { 34719bbdf1e0SAlan Stern /* Let the drivers do their thing, then... */ 34729bbdf1e0SAlan Stern usb_autosuspend_device(udev); 34739bbdf1e0SAlan Stern } 3474d25450c6SAlan Stern } 34755c79a1e3SDan Williams usb_unlock_device(udev); 34761da177e4SLinus Torvalds return status; 34771da177e4SLinus Torvalds } 34781da177e4SLinus Torvalds 34797e73be22SDan Williams /* Returns 1 if there was a remote wakeup and a connect status change. */ 34807e73be22SDan Williams static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 34817e73be22SDan Williams u16 portstatus, u16 portchange) 34827e73be22SDan Williams __must_hold(&port_dev->status_lock) 34837e73be22SDan Williams { 34847e73be22SDan Williams struct usb_port *port_dev = hub->ports[port - 1]; 34857e73be22SDan Williams struct usb_device *hdev; 34867e73be22SDan Williams struct usb_device *udev; 34877e73be22SDan Williams int connect_change = 0; 34887e73be22SDan Williams int ret; 34897e73be22SDan Williams 34907e73be22SDan Williams hdev = hub->hdev; 34917e73be22SDan Williams udev = port_dev->child; 34927e73be22SDan Williams if (!hub_is_superspeed(hdev)) { 34937e73be22SDan Williams if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 34947e73be22SDan Williams return 0; 34957e73be22SDan Williams usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 34967e73be22SDan Williams } else { 34977e73be22SDan Williams if (!udev || udev->state != USB_STATE_SUSPENDED || 34987e73be22SDan Williams (portstatus & USB_PORT_STAT_LINK_STATE) != 34997e73be22SDan Williams USB_SS_PORT_LS_U0) 35007e73be22SDan Williams return 0; 35017e73be22SDan Williams } 35027e73be22SDan Williams 35037e73be22SDan Williams if (udev) { 35047e73be22SDan Williams /* TRSMRCY = 10 msec */ 35057e73be22SDan Williams msleep(10); 35067e73be22SDan Williams 35077e73be22SDan Williams usb_unlock_port(port_dev); 35087e73be22SDan Williams ret = usb_remote_wakeup(udev); 35097e73be22SDan Williams usb_lock_port(port_dev); 35107e73be22SDan Williams if (ret < 0) 35117e73be22SDan Williams connect_change = 1; 35127e73be22SDan Williams } else { 35137e73be22SDan Williams ret = -ENODEV; 35147e73be22SDan Williams hub_port_disable(hub, port, 1); 35157e73be22SDan Williams } 35167e73be22SDan Williams dev_dbg(&port_dev->dev, "resume, status %d\n", ret); 35177e73be22SDan Williams return connect_change; 35187e73be22SDan Williams } 35197e73be22SDan Williams 3520e6f30deaSMing Lei static int check_ports_changed(struct usb_hub *hub) 3521e6f30deaSMing Lei { 3522e6f30deaSMing Lei int port1; 3523e6f30deaSMing Lei 3524e6f30deaSMing Lei for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { 3525e6f30deaSMing Lei u16 portstatus, portchange; 3526e6f30deaSMing Lei int status; 3527e6f30deaSMing Lei 3528e6f30deaSMing Lei status = hub_port_status(hub, port1, &portstatus, &portchange); 3529e6f30deaSMing Lei if (!status && portchange) 3530e6f30deaSMing Lei return 1; 3531e6f30deaSMing Lei } 3532e6f30deaSMing Lei return 0; 3533e6f30deaSMing Lei } 3534e6f30deaSMing Lei 3535db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 35361da177e4SLinus Torvalds { 35371da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 35381da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 35391da177e4SLinus Torvalds unsigned port1; 35404296c70aSSarah Sharp int status; 35411da177e4SLinus Torvalds 3542e583d9dbSAlan Stern /* 3543e583d9dbSAlan Stern * Warn if children aren't already suspended. 3544e583d9dbSAlan Stern * Also, add up the number of wakeup-enabled descendants. 3545e583d9dbSAlan Stern */ 3546e583d9dbSAlan Stern hub->wakeup_enabled_descendants = 0; 35471da177e4SLinus Torvalds for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3548d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 3549d99f6b41SDan Williams struct usb_device *udev = port_dev->child; 35501da177e4SLinus Torvalds 35516840d255SAlan Stern if (udev && udev->can_submit) { 3552b658b8f5SDan Williams dev_warn(&port_dev->dev, "device %s not suspended yet\n", 3553b658b8f5SDan Williams dev_name(&udev->dev)); 35545b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg)) 3555c9f89fa4SDavid Brownell return -EBUSY; 3556c9f89fa4SDavid Brownell } 3557e583d9dbSAlan Stern if (udev) 3558e583d9dbSAlan Stern hub->wakeup_enabled_descendants += 3559e583d9dbSAlan Stern wakeup_enabled_descendants(udev); 35601da177e4SLinus Torvalds } 3561e6f30deaSMing Lei 3562e6f30deaSMing Lei if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { 3563e6f30deaSMing Lei /* check if there are changes pending on hub ports */ 3564e6f30deaSMing Lei if (check_ports_changed(hub)) { 3565e6f30deaSMing Lei if (PMSG_IS_AUTO(msg)) 3566e6f30deaSMing Lei return -EBUSY; 3567e6f30deaSMing Lei pm_wakeup_event(&hdev->dev, 2000); 3568e6f30deaSMing Lei } 3569e6f30deaSMing Lei } 3570e6f30deaSMing Lei 35714296c70aSSarah Sharp if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 35724296c70aSSarah Sharp /* Enable hub to send remote wakeup for all ports. */ 35734296c70aSSarah Sharp for (port1 = 1; port1 <= hdev->maxchild; port1++) { 35744296c70aSSarah Sharp status = set_port_feature(hdev, 35754296c70aSSarah Sharp port1 | 35764296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 35774296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 35784296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 35794296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_MASK); 35804296c70aSSarah Sharp } 35814296c70aSSarah Sharp } 35821da177e4SLinus Torvalds 3583441b62c1SHarvey Harrison dev_dbg(&intf->dev, "%s\n", __func__); 358440f122f3SAlan Stern 358537ebb549SPetr Mladek /* stop hub_wq and related activity */ 35864330354fSAlan Stern hub_quiesce(hub, HUB_SUSPEND); 3587b6f6436dSAlan Stern return 0; 35881da177e4SLinus Torvalds } 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf) 35911da177e4SLinus Torvalds { 35921da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 35931da177e4SLinus Torvalds 35945e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 3595f2835219SAlan Stern hub_activate(hub, HUB_RESUME); 35961da177e4SLinus Torvalds return 0; 35971da177e4SLinus Torvalds } 35981da177e4SLinus Torvalds 3599b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf) 3600f07600cfSAlan Stern { 3601b41a60ecSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 3602f07600cfSAlan Stern 36035e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 3604f2835219SAlan Stern hub_activate(hub, HUB_RESET_RESUME); 3605f07600cfSAlan Stern return 0; 3606f07600cfSAlan Stern } 3607f07600cfSAlan Stern 360854515fe5SAlan Stern /** 360954515fe5SAlan Stern * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 361054515fe5SAlan Stern * @rhdev: struct usb_device for the root hub 361154515fe5SAlan Stern * 361254515fe5SAlan Stern * The USB host controller driver calls this function when its root hub 361354515fe5SAlan Stern * is resumed and Vbus power has been interrupted or the controller 3614feccc30dSAlan Stern * has been reset. The routine marks @rhdev as having lost power. 3615feccc30dSAlan Stern * When the hub driver is resumed it will take notice and carry out 3616feccc30dSAlan Stern * power-session recovery for all the "USB-PERSIST"-enabled child devices; 3617feccc30dSAlan Stern * the others will be disconnected. 361854515fe5SAlan Stern */ 361954515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev) 362054515fe5SAlan Stern { 362154515fe5SAlan Stern dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); 362254515fe5SAlan Stern rhdev->reset_resume = 1; 362354515fe5SAlan Stern } 362454515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 362554515fe5SAlan Stern 36261ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[] = { 36271ea7e0e8SSarah Sharp "U0", 36281ea7e0e8SSarah Sharp "U1", 36291ea7e0e8SSarah Sharp "U2", 36301ea7e0e8SSarah Sharp "U3", 36311ea7e0e8SSarah Sharp }; 36321ea7e0e8SSarah Sharp 36331ea7e0e8SSarah Sharp /* 36341ea7e0e8SSarah Sharp * Send a Set SEL control transfer to the device, prior to enabling 36351ea7e0e8SSarah Sharp * device-initiated U1 or U2. This lets the device know the exit latencies from 36361ea7e0e8SSarah Sharp * the time the device initiates a U1 or U2 exit, to the time it will receive a 36371ea7e0e8SSarah Sharp * packet from the host. 36381ea7e0e8SSarah Sharp * 36391ea7e0e8SSarah Sharp * This function will fail if the SEL or PEL values for udev are greater than 36401ea7e0e8SSarah Sharp * the maximum allowed values for the link state to be enabled. 36411ea7e0e8SSarah Sharp */ 36421ea7e0e8SSarah Sharp static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) 36431ea7e0e8SSarah Sharp { 36441ea7e0e8SSarah Sharp struct usb_set_sel_req *sel_values; 36451ea7e0e8SSarah Sharp unsigned long long u1_sel; 36461ea7e0e8SSarah Sharp unsigned long long u1_pel; 36471ea7e0e8SSarah Sharp unsigned long long u2_sel; 36481ea7e0e8SSarah Sharp unsigned long long u2_pel; 36491ea7e0e8SSarah Sharp int ret; 36501ea7e0e8SSarah Sharp 365138d7f688SXenia Ragiadakou if (udev->state != USB_STATE_CONFIGURED) 365238d7f688SXenia Ragiadakou return 0; 365338d7f688SXenia Ragiadakou 36541ea7e0e8SSarah Sharp /* Convert SEL and PEL stored in ns to us */ 36551ea7e0e8SSarah Sharp u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 36561ea7e0e8SSarah Sharp u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 36571ea7e0e8SSarah Sharp u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 36581ea7e0e8SSarah Sharp u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 36591ea7e0e8SSarah Sharp 36601ea7e0e8SSarah Sharp /* 36611ea7e0e8SSarah Sharp * Make sure that the calculated SEL and PEL values for the link 36621ea7e0e8SSarah Sharp * state we're enabling aren't bigger than the max SEL/PEL 36631ea7e0e8SSarah Sharp * value that will fit in the SET SEL control transfer. 36641ea7e0e8SSarah Sharp * Otherwise the device would get an incorrect idea of the exit 36651ea7e0e8SSarah Sharp * latency for the link state, and could start a device-initiated 36661ea7e0e8SSarah Sharp * U1/U2 when the exit latencies are too high. 36671ea7e0e8SSarah Sharp */ 36681ea7e0e8SSarah Sharp if ((state == USB3_LPM_U1 && 36691ea7e0e8SSarah Sharp (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 36701ea7e0e8SSarah Sharp u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || 36711ea7e0e8SSarah Sharp (state == USB3_LPM_U2 && 36721ea7e0e8SSarah Sharp (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 36731ea7e0e8SSarah Sharp u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { 36741510a1a2SSarah Sharp dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n", 36751ea7e0e8SSarah Sharp usb3_lpm_names[state], u1_sel, u1_pel); 36761ea7e0e8SSarah Sharp return -EINVAL; 36771ea7e0e8SSarah Sharp } 36781ea7e0e8SSarah Sharp 36791ea7e0e8SSarah Sharp /* 36801ea7e0e8SSarah Sharp * If we're enabling device-initiated LPM for one link state, 36811ea7e0e8SSarah Sharp * but the other link state has a too high SEL or PEL value, 36821ea7e0e8SSarah Sharp * just set those values to the max in the Set SEL request. 36831ea7e0e8SSarah Sharp */ 36841ea7e0e8SSarah Sharp if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) 36851ea7e0e8SSarah Sharp u1_sel = USB3_LPM_MAX_U1_SEL_PEL; 36861ea7e0e8SSarah Sharp 36871ea7e0e8SSarah Sharp if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) 36881ea7e0e8SSarah Sharp u1_pel = USB3_LPM_MAX_U1_SEL_PEL; 36891ea7e0e8SSarah Sharp 36901ea7e0e8SSarah Sharp if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) 36911ea7e0e8SSarah Sharp u2_sel = USB3_LPM_MAX_U2_SEL_PEL; 36921ea7e0e8SSarah Sharp 36931ea7e0e8SSarah Sharp if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) 36941ea7e0e8SSarah Sharp u2_pel = USB3_LPM_MAX_U2_SEL_PEL; 36951ea7e0e8SSarah Sharp 36961ea7e0e8SSarah Sharp /* 36971ea7e0e8SSarah Sharp * usb_enable_lpm() can be called as part of a failed device reset, 36981ea7e0e8SSarah Sharp * which may be initiated by an error path of a mass storage driver. 36991ea7e0e8SSarah Sharp * Therefore, use GFP_NOIO. 37001ea7e0e8SSarah Sharp */ 37011ea7e0e8SSarah Sharp sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); 37021ea7e0e8SSarah Sharp if (!sel_values) 37031ea7e0e8SSarah Sharp return -ENOMEM; 37041ea7e0e8SSarah Sharp 37051ea7e0e8SSarah Sharp sel_values->u1_sel = u1_sel; 37061ea7e0e8SSarah Sharp sel_values->u1_pel = u1_pel; 37071ea7e0e8SSarah Sharp sel_values->u2_sel = cpu_to_le16(u2_sel); 37081ea7e0e8SSarah Sharp sel_values->u2_pel = cpu_to_le16(u2_pel); 37091ea7e0e8SSarah Sharp 37101ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 37111ea7e0e8SSarah Sharp USB_REQ_SET_SEL, 37121ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 37131ea7e0e8SSarah Sharp 0, 0, 37141ea7e0e8SSarah Sharp sel_values, sizeof *(sel_values), 37151ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 37161ea7e0e8SSarah Sharp kfree(sel_values); 37171ea7e0e8SSarah Sharp return ret; 37181ea7e0e8SSarah Sharp } 37191ea7e0e8SSarah Sharp 37201ea7e0e8SSarah Sharp /* 37211ea7e0e8SSarah Sharp * Enable or disable device-initiated U1 or U2 transitions. 37221ea7e0e8SSarah Sharp */ 37231ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev, 37241ea7e0e8SSarah Sharp enum usb3_link_state state, bool enable) 37251ea7e0e8SSarah Sharp { 37261ea7e0e8SSarah Sharp int ret; 37271ea7e0e8SSarah Sharp int feature; 37281ea7e0e8SSarah Sharp 37291ea7e0e8SSarah Sharp switch (state) { 37301ea7e0e8SSarah Sharp case USB3_LPM_U1: 37311ea7e0e8SSarah Sharp feature = USB_DEVICE_U1_ENABLE; 37321ea7e0e8SSarah Sharp break; 37331ea7e0e8SSarah Sharp case USB3_LPM_U2: 37341ea7e0e8SSarah Sharp feature = USB_DEVICE_U2_ENABLE; 37351ea7e0e8SSarah Sharp break; 37361ea7e0e8SSarah Sharp default: 37371ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 37381ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable"); 37391ea7e0e8SSarah Sharp return -EINVAL; 37401ea7e0e8SSarah Sharp } 37411ea7e0e8SSarah Sharp 37421ea7e0e8SSarah Sharp if (udev->state != USB_STATE_CONFIGURED) { 37431ea7e0e8SSarah Sharp dev_dbg(&udev->dev, "%s: Can't %s %s state " 37441ea7e0e8SSarah Sharp "for unconfigured device.\n", 37451ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable", 37461ea7e0e8SSarah Sharp usb3_lpm_names[state]); 37471ea7e0e8SSarah Sharp return 0; 37481ea7e0e8SSarah Sharp } 37491ea7e0e8SSarah Sharp 37501ea7e0e8SSarah Sharp if (enable) { 37511ea7e0e8SSarah Sharp /* 37521ea7e0e8SSarah Sharp * Now send the control transfer to enable device-initiated LPM 37531ea7e0e8SSarah Sharp * for either U1 or U2. 37541ea7e0e8SSarah Sharp */ 37551ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 37561ea7e0e8SSarah Sharp USB_REQ_SET_FEATURE, 37571ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 37581ea7e0e8SSarah Sharp feature, 37591ea7e0e8SSarah Sharp 0, NULL, 0, 37601ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 37611ea7e0e8SSarah Sharp } else { 37621ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 37631ea7e0e8SSarah Sharp USB_REQ_CLEAR_FEATURE, 37641ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 37651ea7e0e8SSarah Sharp feature, 37661ea7e0e8SSarah Sharp 0, NULL, 0, 37671ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 37681ea7e0e8SSarah Sharp } 37691ea7e0e8SSarah Sharp if (ret < 0) { 37701ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 37711ea7e0e8SSarah Sharp enable ? "Enable" : "Disable", 37721ea7e0e8SSarah Sharp usb3_lpm_names[state]); 37731ea7e0e8SSarah Sharp return -EBUSY; 37741ea7e0e8SSarah Sharp } 37751ea7e0e8SSarah Sharp return 0; 37761ea7e0e8SSarah Sharp } 37771ea7e0e8SSarah Sharp 37781ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev, 37791ea7e0e8SSarah Sharp enum usb3_link_state state, int timeout) 37801ea7e0e8SSarah Sharp { 37811ea7e0e8SSarah Sharp int ret; 37821ea7e0e8SSarah Sharp int feature; 37831ea7e0e8SSarah Sharp 37841ea7e0e8SSarah Sharp switch (state) { 37851ea7e0e8SSarah Sharp case USB3_LPM_U1: 37861ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U1_TIMEOUT; 37871ea7e0e8SSarah Sharp break; 37881ea7e0e8SSarah Sharp case USB3_LPM_U2: 37891ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U2_TIMEOUT; 37901ea7e0e8SSarah Sharp break; 37911ea7e0e8SSarah Sharp default: 37921ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 37931ea7e0e8SSarah Sharp __func__); 37941ea7e0e8SSarah Sharp return -EINVAL; 37951ea7e0e8SSarah Sharp } 37961ea7e0e8SSarah Sharp 37971ea7e0e8SSarah Sharp if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 37981ea7e0e8SSarah Sharp timeout != USB3_LPM_DEVICE_INITIATED) { 37991ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 38001ea7e0e8SSarah Sharp "which is a reserved value.\n", 38011ea7e0e8SSarah Sharp usb3_lpm_names[state], timeout); 38021ea7e0e8SSarah Sharp return -EINVAL; 38031ea7e0e8SSarah Sharp } 38041ea7e0e8SSarah Sharp 38051ea7e0e8SSarah Sharp ret = set_port_feature(udev->parent, 38061ea7e0e8SSarah Sharp USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 38071ea7e0e8SSarah Sharp feature); 38081ea7e0e8SSarah Sharp if (ret < 0) { 38091ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 38101ea7e0e8SSarah Sharp "error code %i\n", usb3_lpm_names[state], 38111ea7e0e8SSarah Sharp timeout, ret); 38121ea7e0e8SSarah Sharp return -EBUSY; 38131ea7e0e8SSarah Sharp } 38141ea7e0e8SSarah Sharp if (state == USB3_LPM_U1) 38151ea7e0e8SSarah Sharp udev->u1_params.timeout = timeout; 38161ea7e0e8SSarah Sharp else 38171ea7e0e8SSarah Sharp udev->u2_params.timeout = timeout; 38181ea7e0e8SSarah Sharp return 0; 38191ea7e0e8SSarah Sharp } 38201ea7e0e8SSarah Sharp 38211ea7e0e8SSarah Sharp /* 38221ea7e0e8SSarah Sharp * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 38231ea7e0e8SSarah Sharp * U1/U2 entry. 38241ea7e0e8SSarah Sharp * 38251ea7e0e8SSarah Sharp * We will attempt to enable U1 or U2, but there are no guarantees that the 38261ea7e0e8SSarah Sharp * control transfers to set the hub timeout or enable device-initiated U1/U2 38271ea7e0e8SSarah Sharp * will be successful. 38281ea7e0e8SSarah Sharp * 38291ea7e0e8SSarah Sharp * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 38301ea7e0e8SSarah Sharp * driver know about it. If that call fails, it should be harmless, and just 38311ea7e0e8SSarah Sharp * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 38321ea7e0e8SSarah Sharp */ 38331ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 38341ea7e0e8SSarah Sharp enum usb3_link_state state) 38351ea7e0e8SSarah Sharp { 383665a95b75SSarah Sharp int timeout, ret; 3837ae8963adSSarah Sharp __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat; 3838ae8963adSSarah Sharp __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat; 3839ae8963adSSarah Sharp 3840ae8963adSSarah Sharp /* If the device says it doesn't have *any* exit latency to come out of 3841ae8963adSSarah Sharp * U1 or U2, it's probably lying. Assume it doesn't implement that link 3842ae8963adSSarah Sharp * state. 3843ae8963adSSarah Sharp */ 3844ae8963adSSarah Sharp if ((state == USB3_LPM_U1 && u1_mel == 0) || 3845ae8963adSSarah Sharp (state == USB3_LPM_U2 && u2_mel == 0)) 3846ae8963adSSarah Sharp return; 38471ea7e0e8SSarah Sharp 384865a95b75SSarah Sharp /* 384965a95b75SSarah Sharp * First, let the device know about the exit latencies 385065a95b75SSarah Sharp * associated with the link state we're about to enable. 385165a95b75SSarah Sharp */ 385265a95b75SSarah Sharp ret = usb_req_set_sel(udev, state); 385365a95b75SSarah Sharp if (ret < 0) { 385465a95b75SSarah Sharp dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n", 385565a95b75SSarah Sharp usb3_lpm_names[state]); 385665a95b75SSarah Sharp return; 385765a95b75SSarah Sharp } 385865a95b75SSarah Sharp 38591ea7e0e8SSarah Sharp /* We allow the host controller to set the U1/U2 timeout internally 38601ea7e0e8SSarah Sharp * first, so that it can change its schedule to account for the 38611ea7e0e8SSarah Sharp * additional latency to send data to a device in a lower power 38621ea7e0e8SSarah Sharp * link state. 38631ea7e0e8SSarah Sharp */ 38641ea7e0e8SSarah Sharp timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 38651ea7e0e8SSarah Sharp 38661ea7e0e8SSarah Sharp /* xHCI host controller doesn't want to enable this LPM state. */ 38671ea7e0e8SSarah Sharp if (timeout == 0) 38681ea7e0e8SSarah Sharp return; 38691ea7e0e8SSarah Sharp 38701ea7e0e8SSarah Sharp if (timeout < 0) { 38711ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not enable %s link state, " 38721ea7e0e8SSarah Sharp "xHCI error %i.\n", usb3_lpm_names[state], 38731ea7e0e8SSarah Sharp timeout); 38741ea7e0e8SSarah Sharp return; 38751ea7e0e8SSarah Sharp } 38761ea7e0e8SSarah Sharp 3877bf5ce5bfSLu Baolu if (usb_set_lpm_timeout(udev, state, timeout)) { 38781ea7e0e8SSarah Sharp /* If we can't set the parent hub U1/U2 timeout, 38791ea7e0e8SSarah Sharp * device-initiated LPM won't be allowed either, so let the xHCI 38801ea7e0e8SSarah Sharp * host know that this link state won't be enabled. 38811ea7e0e8SSarah Sharp */ 38821ea7e0e8SSarah Sharp hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 3883bf5ce5bfSLu Baolu } else { 3884bf5ce5bfSLu Baolu /* Only a configured device will accept the Set Feature 3885bf5ce5bfSLu Baolu * U1/U2_ENABLE 3886bf5ce5bfSLu Baolu */ 3887bf5ce5bfSLu Baolu if (udev->actconfig) 38881ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, true); 38891ea7e0e8SSarah Sharp 3890bf5ce5bfSLu Baolu /* As soon as usb_set_lpm_timeout(timeout) returns 0, the 3891bf5ce5bfSLu Baolu * hub-initiated LPM is enabled. Thus, LPM is enabled no 3892bf5ce5bfSLu Baolu * matter the result of usb_set_device_initiated_lpm(). 3893bf5ce5bfSLu Baolu * The only difference is whether device is able to initiate 3894bf5ce5bfSLu Baolu * LPM. 3895bf5ce5bfSLu Baolu */ 3896bf5ce5bfSLu Baolu if (state == USB3_LPM_U1) 3897bf5ce5bfSLu Baolu udev->usb3_lpm_u1_enabled = 1; 3898bf5ce5bfSLu Baolu else if (state == USB3_LPM_U2) 3899bf5ce5bfSLu Baolu udev->usb3_lpm_u2_enabled = 1; 3900bf5ce5bfSLu Baolu } 39011ea7e0e8SSarah Sharp } 39021ea7e0e8SSarah Sharp 39031ea7e0e8SSarah Sharp /* 39041ea7e0e8SSarah Sharp * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 39051ea7e0e8SSarah Sharp * U1/U2 entry. 39061ea7e0e8SSarah Sharp * 39071ea7e0e8SSarah Sharp * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 39081ea7e0e8SSarah Sharp * If zero is returned, the parent will not allow the link to go into U1/U2. 39091ea7e0e8SSarah Sharp * 39101ea7e0e8SSarah Sharp * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 39111ea7e0e8SSarah Sharp * it won't have an effect on the bus link state because the parent hub will 39121ea7e0e8SSarah Sharp * still disallow device-initiated U1/U2 entry. 39131ea7e0e8SSarah Sharp * 39141ea7e0e8SSarah Sharp * If zero is returned, the xHCI host controller may still think U1/U2 entry is 39151ea7e0e8SSarah Sharp * possible. The result will be slightly more bus bandwidth will be taken up 39161ea7e0e8SSarah Sharp * (to account for U1/U2 exit latency), but it should be harmless. 39171ea7e0e8SSarah Sharp */ 39181ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 39191ea7e0e8SSarah Sharp enum usb3_link_state state) 39201ea7e0e8SSarah Sharp { 39211ea7e0e8SSarah Sharp switch (state) { 39221ea7e0e8SSarah Sharp case USB3_LPM_U1: 39231ea7e0e8SSarah Sharp case USB3_LPM_U2: 39241ea7e0e8SSarah Sharp break; 39251ea7e0e8SSarah Sharp default: 39261ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 39271ea7e0e8SSarah Sharp __func__); 39281ea7e0e8SSarah Sharp return -EINVAL; 39291ea7e0e8SSarah Sharp } 39301ea7e0e8SSarah Sharp 39311ea7e0e8SSarah Sharp if (usb_set_lpm_timeout(udev, state, 0)) 39321ea7e0e8SSarah Sharp return -EBUSY; 39331ea7e0e8SSarah Sharp 39341ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, false); 39351ea7e0e8SSarah Sharp 39361ea7e0e8SSarah Sharp if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 39371ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 39381ea7e0e8SSarah Sharp "bus schedule bandwidth may be impacted.\n", 39391ea7e0e8SSarah Sharp usb3_lpm_names[state]); 3940bf5ce5bfSLu Baolu 3941bf5ce5bfSLu Baolu /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM 3942bf5ce5bfSLu Baolu * is disabled. Hub will disallows link to enter U1/U2 as well, 3943bf5ce5bfSLu Baolu * even device is initiating LPM. Hence LPM is disabled if hub LPM 3944bf5ce5bfSLu Baolu * timeout set to 0, no matter device-initiated LPM is disabled or 3945bf5ce5bfSLu Baolu * not. 3946bf5ce5bfSLu Baolu */ 3947bf5ce5bfSLu Baolu if (state == USB3_LPM_U1) 3948bf5ce5bfSLu Baolu udev->usb3_lpm_u1_enabled = 0; 3949bf5ce5bfSLu Baolu else if (state == USB3_LPM_U2) 3950bf5ce5bfSLu Baolu udev->usb3_lpm_u2_enabled = 0; 3951bf5ce5bfSLu Baolu 39521ea7e0e8SSarah Sharp return 0; 39531ea7e0e8SSarah Sharp } 39541ea7e0e8SSarah Sharp 39551ea7e0e8SSarah Sharp /* 39561ea7e0e8SSarah Sharp * Disable hub-initiated and device-initiated U1 and U2 entry. 39571ea7e0e8SSarah Sharp * Caller must own the bandwidth_mutex. 39581ea7e0e8SSarah Sharp * 39591ea7e0e8SSarah Sharp * This will call usb_enable_lpm() on failure, which will decrement 39601ea7e0e8SSarah Sharp * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 39611ea7e0e8SSarah Sharp */ 39621ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev) 39631ea7e0e8SSarah Sharp { 39641ea7e0e8SSarah Sharp struct usb_hcd *hcd; 39651ea7e0e8SSarah Sharp 39661ea7e0e8SSarah Sharp if (!udev || !udev->parent || 39678a1b2725SMathias Nyman udev->speed < USB_SPEED_SUPER || 396851df62ffSPratyush Anand !udev->lpm_capable || 396951df62ffSPratyush Anand udev->state < USB_STATE_DEFAULT) 39701ea7e0e8SSarah Sharp return 0; 39711ea7e0e8SSarah Sharp 39721ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus); 39731ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 39741ea7e0e8SSarah Sharp return 0; 39751ea7e0e8SSarah Sharp 39761ea7e0e8SSarah Sharp udev->lpm_disable_count++; 397755558c33SDan Carpenter if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 39781ea7e0e8SSarah Sharp return 0; 39791ea7e0e8SSarah Sharp 39801ea7e0e8SSarah Sharp /* If LPM is enabled, attempt to disable it. */ 39811ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 39821ea7e0e8SSarah Sharp goto enable_lpm; 39831ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 39841ea7e0e8SSarah Sharp goto enable_lpm; 39851ea7e0e8SSarah Sharp 39861ea7e0e8SSarah Sharp return 0; 39871ea7e0e8SSarah Sharp 39881ea7e0e8SSarah Sharp enable_lpm: 39891ea7e0e8SSarah Sharp usb_enable_lpm(udev); 39901ea7e0e8SSarah Sharp return -EBUSY; 39911ea7e0e8SSarah Sharp } 39921ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm); 39931ea7e0e8SSarah Sharp 39941ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 39951ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev) 39961ea7e0e8SSarah Sharp { 39971ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 39981ea7e0e8SSarah Sharp int ret; 39991ea7e0e8SSarah Sharp 40001ea7e0e8SSarah Sharp if (!hcd) 40011ea7e0e8SSarah Sharp return -EINVAL; 40021ea7e0e8SSarah Sharp 40031ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex); 40041ea7e0e8SSarah Sharp ret = usb_disable_lpm(udev); 40051ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 40061ea7e0e8SSarah Sharp 40071ea7e0e8SSarah Sharp return ret; 40081ea7e0e8SSarah Sharp } 40091ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 40101ea7e0e8SSarah Sharp 40111ea7e0e8SSarah Sharp /* 40121ea7e0e8SSarah Sharp * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 40131ea7e0e8SSarah Sharp * xHCI host policy may prevent U1 or U2 from being enabled. 40141ea7e0e8SSarah Sharp * 40151ea7e0e8SSarah Sharp * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 40161ea7e0e8SSarah Sharp * until the lpm_disable_count drops to zero. Caller must own the 40171ea7e0e8SSarah Sharp * bandwidth_mutex. 40181ea7e0e8SSarah Sharp */ 40191ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) 40201ea7e0e8SSarah Sharp { 40211ea7e0e8SSarah Sharp struct usb_hcd *hcd; 4022513072d9SLu Baolu struct usb_hub *hub; 4023513072d9SLu Baolu struct usb_port *port_dev; 40241ea7e0e8SSarah Sharp 40251ea7e0e8SSarah Sharp if (!udev || !udev->parent || 40268a1b2725SMathias Nyman udev->speed < USB_SPEED_SUPER || 402751df62ffSPratyush Anand !udev->lpm_capable || 402851df62ffSPratyush Anand udev->state < USB_STATE_DEFAULT) 40291ea7e0e8SSarah Sharp return; 40301ea7e0e8SSarah Sharp 40311ea7e0e8SSarah Sharp udev->lpm_disable_count--; 40321ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus); 40331ea7e0e8SSarah Sharp /* Double check that we can both enable and disable LPM. 40341ea7e0e8SSarah Sharp * Device must be configured to accept set feature U1/U2 timeout. 40351ea7e0e8SSarah Sharp */ 40361ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 40371ea7e0e8SSarah Sharp !hcd->driver->disable_usb3_lpm_timeout) 40381ea7e0e8SSarah Sharp return; 40391ea7e0e8SSarah Sharp 40401ea7e0e8SSarah Sharp if (udev->lpm_disable_count > 0) 40411ea7e0e8SSarah Sharp return; 40421ea7e0e8SSarah Sharp 4043513072d9SLu Baolu hub = usb_hub_to_struct_hub(udev->parent); 4044513072d9SLu Baolu if (!hub) 4045513072d9SLu Baolu return; 4046513072d9SLu Baolu 4047513072d9SLu Baolu port_dev = hub->ports[udev->portnum - 1]; 4048513072d9SLu Baolu 4049513072d9SLu Baolu if (port_dev->usb3_lpm_u1_permit) 40501ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U1); 4051513072d9SLu Baolu 4052513072d9SLu Baolu if (port_dev->usb3_lpm_u2_permit) 40531ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U2); 40541ea7e0e8SSarah Sharp } 40551ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm); 40561ea7e0e8SSarah Sharp 40571ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 40581ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) 40591ea7e0e8SSarah Sharp { 40601ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 40611ea7e0e8SSarah Sharp 40621ea7e0e8SSarah Sharp if (!hcd) 40631ea7e0e8SSarah Sharp return; 40641ea7e0e8SSarah Sharp 40651ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex); 40661ea7e0e8SSarah Sharp usb_enable_lpm(udev); 40671ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 40681ea7e0e8SSarah Sharp } 40691ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 40701ea7e0e8SSarah Sharp 407137be6676SMathias Nyman /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */ 407237be6676SMathias Nyman static void hub_usb3_port_prepare_disable(struct usb_hub *hub, 407337be6676SMathias Nyman struct usb_port *port_dev) 407437be6676SMathias Nyman { 407537be6676SMathias Nyman struct usb_device *udev = port_dev->child; 407637be6676SMathias Nyman int ret; 407737be6676SMathias Nyman 407837be6676SMathias Nyman if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { 407937be6676SMathias Nyman ret = hub_set_port_link_state(hub, port_dev->portnum, 408037be6676SMathias Nyman USB_SS_PORT_LS_U0); 408137be6676SMathias Nyman if (!ret) { 408237be6676SMathias Nyman msleep(USB_RESUME_TIMEOUT); 408337be6676SMathias Nyman ret = usb_disable_remote_wakeup(udev); 408437be6676SMathias Nyman } 408537be6676SMathias Nyman if (ret) 408637be6676SMathias Nyman dev_warn(&udev->dev, 408737be6676SMathias Nyman "Port disable: can't disable remote wake\n"); 408837be6676SMathias Nyman udev->do_remote_wakeup = 0; 408937be6676SMathias Nyman } 409037be6676SMathias Nyman } 40911ea7e0e8SSarah Sharp 4092d388dab7SAlan Stern #else /* CONFIG_PM */ 4093d388dab7SAlan Stern 4094511366daSAndrew Morton #define hub_suspend NULL 4095511366daSAndrew Morton #define hub_resume NULL 4096f07600cfSAlan Stern #define hub_reset_resume NULL 40971ea7e0e8SSarah Sharp 409837be6676SMathias Nyman static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, 409937be6676SMathias Nyman struct usb_port *port_dev) { } 410037be6676SMathias Nyman 41011ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev) 41021ea7e0e8SSarah Sharp { 41031ea7e0e8SSarah Sharp return 0; 41041ea7e0e8SSarah Sharp } 4105e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm); 41061ea7e0e8SSarah Sharp 41071ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { } 4108e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm); 41091ea7e0e8SSarah Sharp 41101ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev) 41111ea7e0e8SSarah Sharp { 41121ea7e0e8SSarah Sharp return 0; 41131ea7e0e8SSarah Sharp } 4114e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 41151ea7e0e8SSarah Sharp 41161ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { } 4117e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4118f74631e3SSarah Sharp 4119f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev) 4120f74631e3SSarah Sharp { 4121f74631e3SSarah Sharp return 0; 4122f74631e3SSarah Sharp } 4123f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm); 4124f74631e3SSarah Sharp 4125f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) { } 4126f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm); 4127c4b51a43SAlan Stern 41284a95b1fcSStephen Rothwell static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 41294a95b1fcSStephen Rothwell u16 portstatus, u16 portchange) 41304a95b1fcSStephen Rothwell { 41314a95b1fcSStephen Rothwell return 0; 41324a95b1fcSStephen Rothwell } 41334a95b1fcSStephen Rothwell 4134c4b51a43SAlan Stern #endif /* CONFIG_PM */ 4135d388dab7SAlan Stern 41363bc02bceSGeert Uytterhoeven /* 41373bc02bceSGeert Uytterhoeven * USB-3 does not have a similar link state as USB-2 that will avoid negotiating 41383bc02bceSGeert Uytterhoeven * a connection with a plugged-in cable but will signal the host when the cable 41393bc02bceSGeert Uytterhoeven * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices 41403bc02bceSGeert Uytterhoeven */ 41413bc02bceSGeert Uytterhoeven static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 41423bc02bceSGeert Uytterhoeven { 41433bc02bceSGeert Uytterhoeven struct usb_port *port_dev = hub->ports[port1 - 1]; 41443bc02bceSGeert Uytterhoeven struct usb_device *hdev = hub->hdev; 41453bc02bceSGeert Uytterhoeven int ret = 0; 41463bc02bceSGeert Uytterhoeven 41473bc02bceSGeert Uytterhoeven if (!hub->error) { 41483bc02bceSGeert Uytterhoeven if (hub_is_superspeed(hub->hdev)) { 41493bc02bceSGeert Uytterhoeven hub_usb3_port_prepare_disable(hub, port_dev); 41503bc02bceSGeert Uytterhoeven ret = hub_set_port_link_state(hub, port_dev->portnum, 41513bc02bceSGeert Uytterhoeven USB_SS_PORT_LS_U3); 41523bc02bceSGeert Uytterhoeven } else { 41533bc02bceSGeert Uytterhoeven ret = usb_clear_port_feature(hdev, port1, 41543bc02bceSGeert Uytterhoeven USB_PORT_FEAT_ENABLE); 41553bc02bceSGeert Uytterhoeven } 41563bc02bceSGeert Uytterhoeven } 41573bc02bceSGeert Uytterhoeven if (port_dev->child && set_state) 41583bc02bceSGeert Uytterhoeven usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); 41593bc02bceSGeert Uytterhoeven if (ret && ret != -ENODEV) 41603bc02bceSGeert Uytterhoeven dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); 41613bc02bceSGeert Uytterhoeven return ret; 41623bc02bceSGeert Uytterhoeven } 41633bc02bceSGeert Uytterhoeven 41641da177e4SLinus Torvalds 41651da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 41661da177e4SLinus Torvalds * 41671da177e4SLinus Torvalds * Between connect detection and reset signaling there must be a delay 41681da177e4SLinus Torvalds * of 100ms at least for debounce and power-settling. The corresponding 41691da177e4SLinus Torvalds * timer shall restart whenever the downstream port detects a disconnect. 41701da177e4SLinus Torvalds * 41711da177e4SLinus Torvalds * Apparently there are some bluetooth and irda-dongles and a number of 41721da177e4SLinus Torvalds * low-speed devices for which this debounce period may last over a second. 41731da177e4SLinus Torvalds * Not covered by the spec - but easy to deal with. 41741da177e4SLinus Torvalds * 41751da177e4SLinus Torvalds * This implementation uses a 1500ms total debounce timeout; if the 41761da177e4SLinus Torvalds * connection isn't stable by then it returns -ETIMEDOUT. It checks 41771da177e4SLinus Torvalds * every 25ms for transient disconnects. When the port status has been 41781da177e4SLinus Torvalds * unchanged for 100ms it returns the port status. 41791da177e4SLinus Torvalds */ 4180ad493e5eSLan Tianyu int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) 41811da177e4SLinus Torvalds { 41821da177e4SLinus Torvalds int ret; 41831da177e4SLinus Torvalds u16 portchange, portstatus; 41841da177e4SLinus Torvalds unsigned connection = 0xffff; 4185d99f6b41SDan Williams int total_time, stable_time = 0; 4186d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 41871da177e4SLinus Torvalds 41881da177e4SLinus Torvalds for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 41891da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 41901da177e4SLinus Torvalds if (ret < 0) 41911da177e4SLinus Torvalds return ret; 41921da177e4SLinus Torvalds 41931da177e4SLinus Torvalds if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 41941da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 4195ad493e5eSLan Tianyu if (!must_be_connected || 4196ad493e5eSLan Tianyu (connection == USB_PORT_STAT_CONNECTION)) 41971da177e4SLinus Torvalds stable_time += HUB_DEBOUNCE_STEP; 41981da177e4SLinus Torvalds if (stable_time >= HUB_DEBOUNCE_STABLE) 41991da177e4SLinus Torvalds break; 42001da177e4SLinus Torvalds } else { 42011da177e4SLinus Torvalds stable_time = 0; 42021da177e4SLinus Torvalds connection = portstatus & USB_PORT_STAT_CONNECTION; 42031da177e4SLinus Torvalds } 42041da177e4SLinus Torvalds 42051da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 4206ad493e5eSLan Tianyu usb_clear_port_feature(hub->hdev, port1, 42071da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 42081da177e4SLinus Torvalds } 42091da177e4SLinus Torvalds 42101da177e4SLinus Torvalds if (total_time >= HUB_DEBOUNCE_TIMEOUT) 42111da177e4SLinus Torvalds break; 42121da177e4SLinus Torvalds msleep(HUB_DEBOUNCE_STEP); 42131da177e4SLinus Torvalds } 42141da177e4SLinus Torvalds 4215d99f6b41SDan Williams dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", 4216d99f6b41SDan Williams total_time, stable_time, portstatus); 42171da177e4SLinus Torvalds 42181da177e4SLinus Torvalds if (stable_time < HUB_DEBOUNCE_STABLE) 42191da177e4SLinus Torvalds return -ETIMEDOUT; 42201da177e4SLinus Torvalds return portstatus; 42211da177e4SLinus Torvalds } 42221da177e4SLinus Torvalds 4223fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev) 42241da177e4SLinus Torvalds { 4225ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 4226ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 42272caf7fcdSAlan Stern usb_enable_endpoint(udev, &udev->ep0, true); 42281da177e4SLinus Torvalds } 4229fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit); 42301da177e4SLinus Torvalds 42311da177e4SLinus Torvalds #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 42321da177e4SLinus Torvalds #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 42331da177e4SLinus Torvalds 42344326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum) 42351da177e4SLinus Torvalds { 42361da177e4SLinus Torvalds int retval; 4237c6515272SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 42381da177e4SLinus Torvalds 4239c6515272SSarah Sharp /* 4240c6515272SSarah Sharp * The host controller will choose the device address, 4241c6515272SSarah Sharp * instead of the core having chosen it earlier 4242c6515272SSarah Sharp */ 4243c6515272SSarah Sharp if (!hcd->driver->address_device && devnum <= 1) 42441da177e4SLinus Torvalds return -EINVAL; 42451da177e4SLinus Torvalds if (udev->state == USB_STATE_ADDRESS) 42461da177e4SLinus Torvalds return 0; 42471da177e4SLinus Torvalds if (udev->state != USB_STATE_DEFAULT) 42481da177e4SLinus Torvalds return -EINVAL; 4249c8d4af8eSAndiry Xu if (hcd->driver->address_device) 4250c6515272SSarah Sharp retval = hcd->driver->address_device(hcd, udev); 4251c8d4af8eSAndiry Xu else 42521da177e4SLinus Torvalds retval = usb_control_msg(udev, usb_sndaddr0pipe(), 42534326ed0bSAlan Stern USB_REQ_SET_ADDRESS, 0, devnum, 0, 42541da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 42551da177e4SLinus Torvalds if (retval == 0) { 42563b29b68bSAlan Stern update_devnum(udev, devnum); 42574953d141SDavid Vrabel /* Device now using proper address. */ 42581da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_ADDRESS); 4259fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 42601da177e4SLinus Torvalds } 42611da177e4SLinus Torvalds return retval; 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds 4264890dae88SMathias Nyman /* 4265890dae88SMathias Nyman * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM 4266890dae88SMathias Nyman * when they're plugged into a USB 2.0 port, but they don't work when LPM is 4267890dae88SMathias Nyman * enabled. 4268890dae88SMathias Nyman * 4269890dae88SMathias Nyman * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the 4270890dae88SMathias Nyman * device says it supports the new USB 2.0 Link PM errata by setting the BESL 4271890dae88SMathias Nyman * support bit in the BOS descriptor. 4272890dae88SMathias Nyman */ 4273890dae88SMathias Nyman static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) 4274890dae88SMathias Nyman { 4275d99f6b41SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4276d99f6b41SDan Williams int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN; 4277890dae88SMathias Nyman 4278890dae88SMathias Nyman if (!udev->usb2_hw_lpm_capable) 4279890dae88SMathias Nyman return; 4280890dae88SMathias Nyman 4281d99f6b41SDan Williams if (hub) 4282d99f6b41SDan Williams connect_type = hub->ports[udev->portnum - 1]->connect_type; 4283890dae88SMathias Nyman 428423c05820SBjørn Mork if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || 4285890dae88SMathias Nyman connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 4286890dae88SMathias Nyman udev->usb2_hw_lpm_allowed = 1; 4287890dae88SMathias Nyman usb_set_usb2_hardware_lpm(udev, 1); 4288890dae88SMathias Nyman } 4289890dae88SMathias Nyman } 4290890dae88SMathias Nyman 429148fc7dbdSDan Williams static int hub_enable_device(struct usb_device *udev) 429248fc7dbdSDan Williams { 429348fc7dbdSDan Williams struct usb_hcd *hcd = bus_to_hcd(udev->bus); 429448fc7dbdSDan Williams 429548fc7dbdSDan Williams if (!hcd->driver->enable_device) 429648fc7dbdSDan Williams return 0; 429748fc7dbdSDan Williams if (udev->state == USB_STATE_ADDRESS) 429848fc7dbdSDan Williams return 0; 429948fc7dbdSDan Williams if (udev->state != USB_STATE_DEFAULT) 430048fc7dbdSDan Williams return -EINVAL; 430148fc7dbdSDan Williams 430248fc7dbdSDan Williams return hcd->driver->enable_device(hcd, udev); 430348fc7dbdSDan Williams } 430448fc7dbdSDan Williams 43051da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor. 43061da177e4SLinus Torvalds * Device connection must be stable, no more debouncing needed. 43071da177e4SLinus Torvalds * Returns device in USB_STATE_ADDRESS, except on error. 43081da177e4SLinus Torvalds * 43091da177e4SLinus Torvalds * If this is called for an already-existing device (as part of 43105c79a1e3SDan Williams * usb_reset_and_verify_device), the caller must own the device lock and 43115c79a1e3SDan Williams * the port lock. For a newly detected device that is not accessible 43125c79a1e3SDan Williams * through any global pointers, it's not necessary to lock the device, 43135c79a1e3SDan Williams * but it is still necessary to lock the port. 43141da177e4SLinus Torvalds */ 43151da177e4SLinus Torvalds static int 43161da177e4SLinus Torvalds hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, 43171da177e4SLinus Torvalds int retry_counter) 43181da177e4SLinus Torvalds { 43191da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 4320e7b77172SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 43210d5ce778SOliver Neukum int retries, operations, retval, i; 43221da177e4SLinus Torvalds unsigned delay = HUB_SHORT_RESET_TIME; 43231da177e4SLinus Torvalds enum usb_device_speed oldspeed = udev->speed; 4324e538dfdaSMichal Nazarewicz const char *speed; 43254326ed0bSAlan Stern int devnum = udev->devnum; 43261da177e4SLinus Torvalds 43271da177e4SLinus Torvalds /* root hub ports have a slightly longer reset period 43281da177e4SLinus Torvalds * (from USB 2.0 spec, section 7.1.7.5) 43291da177e4SLinus Torvalds */ 43301da177e4SLinus Torvalds if (!hdev->parent) { 43311da177e4SLinus Torvalds delay = HUB_ROOT_RESET_TIME; 43321da177e4SLinus Torvalds if (port1 == hdev->bus->otg_port) 43331da177e4SLinus Torvalds hdev->bus->b_hnp_enable = 0; 43341da177e4SLinus Torvalds } 43351da177e4SLinus Torvalds 43361da177e4SLinus Torvalds /* Some low speed devices have problems with the quick delay, so */ 43371da177e4SLinus Torvalds /* be a bit pessimistic with those devices. RHbug #23670 */ 43381da177e4SLinus Torvalds if (oldspeed == USB_SPEED_LOW) 43391da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 43401da177e4SLinus Torvalds 4341feb26ac3SChris Bainbridge mutex_lock(hcd->address0_mutex); 43421da177e4SLinus Torvalds 43431da177e4SLinus Torvalds /* Reset the device; full speed may morph to high speed */ 4344e7b77172SSarah Sharp /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 434575d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false); 43461da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 43471da177e4SLinus Torvalds goto fail; 43481da177e4SLinus Torvalds /* success, speed is known */ 434907194ab7SLuben Tuikov 43501da177e4SLinus Torvalds retval = -ENODEV; 43511da177e4SLinus Torvalds 43528a1b2725SMathias Nyman /* Don't allow speed changes at reset, except usb 3.0 to faster */ 43538a1b2725SMathias Nyman if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && 43548a1b2725SMathias Nyman !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { 43551da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset changed speed!\n"); 43561da177e4SLinus Torvalds goto fail; 43571da177e4SLinus Torvalds } 43581da177e4SLinus Torvalds oldspeed = udev->speed; 43591da177e4SLinus Torvalds 43601da177e4SLinus Torvalds /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 43611da177e4SLinus Torvalds * it's fixed size except for full speed devices. 43625bb6e0aeSInaky Perez-Gonzalez * For Wireless USB devices, ep0 max packet is always 512 (tho 43635bb6e0aeSInaky Perez-Gonzalez * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 43641da177e4SLinus Torvalds */ 43651da177e4SLinus Torvalds switch (udev->speed) { 43668a1b2725SMathias Nyman case USB_SPEED_SUPER_PLUS: 43676b403b02SSarah Sharp case USB_SPEED_SUPER: 4368551cdbbeSGreg Kroah-Hartman case USB_SPEED_WIRELESS: /* fixed at 512 */ 4369551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 43705bb6e0aeSInaky Perez-Gonzalez break; 43711da177e4SLinus Torvalds case USB_SPEED_HIGH: /* fixed at 64 */ 4372551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 43731da177e4SLinus Torvalds break; 43741da177e4SLinus Torvalds case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 43751da177e4SLinus Torvalds /* to determine the ep0 maxpacket size, try to read 43761da177e4SLinus Torvalds * the device descriptor to get bMaxPacketSize0 and 43771da177e4SLinus Torvalds * then correct our initial guess. 43781da177e4SLinus Torvalds */ 4379551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 43801da177e4SLinus Torvalds break; 43811da177e4SLinus Torvalds case USB_SPEED_LOW: /* fixed at 8 */ 4382551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 43831da177e4SLinus Torvalds break; 43841da177e4SLinus Torvalds default: 43851da177e4SLinus Torvalds goto fail; 43861da177e4SLinus Torvalds } 43871da177e4SLinus Torvalds 4388e538dfdaSMichal Nazarewicz if (udev->speed == USB_SPEED_WIRELESS) 4389e538dfdaSMichal Nazarewicz speed = "variable speed Wireless"; 4390e538dfdaSMichal Nazarewicz else 4391e538dfdaSMichal Nazarewicz speed = usb_speed_string(udev->speed); 4392e538dfdaSMichal Nazarewicz 43938a1b2725SMathias Nyman if (udev->speed < USB_SPEED_SUPER) 439483a07196SInaky Perez-Gonzalez dev_info(&udev->dev, 4395e538dfdaSMichal Nazarewicz "%s %s USB device number %d using %s\n", 4396e538dfdaSMichal Nazarewicz (udev->config) ? "reset" : "new", speed, 43973b29b68bSAlan Stern devnum, udev->bus->controller->driver->name); 43981da177e4SLinus Torvalds 43991da177e4SLinus Torvalds /* Set up TT records, if needed */ 44001da177e4SLinus Torvalds if (hdev->tt) { 44011da177e4SLinus Torvalds udev->tt = hdev->tt; 44021da177e4SLinus Torvalds udev->ttport = hdev->ttport; 44031da177e4SLinus Torvalds } else if (udev->speed != USB_SPEED_HIGH 44041da177e4SLinus Torvalds && hdev->speed == USB_SPEED_HIGH) { 4405d199c96dSAlan Stern if (!hub->tt.hub) { 4406d199c96dSAlan Stern dev_err(&udev->dev, "parent hub has no TT\n"); 4407d199c96dSAlan Stern retval = -EINVAL; 4408d199c96dSAlan Stern goto fail; 4409d199c96dSAlan Stern } 44101da177e4SLinus Torvalds udev->tt = &hub->tt; 44111da177e4SLinus Torvalds udev->ttport = port1; 44121da177e4SLinus Torvalds } 44131da177e4SLinus Torvalds 44141da177e4SLinus Torvalds /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 44151da177e4SLinus Torvalds * Because device hardware and firmware is sometimes buggy in 44161da177e4SLinus Torvalds * this area, and this is how Linux has done it for ages. 44171da177e4SLinus Torvalds * Change it cautiously. 44181da177e4SLinus Torvalds * 441948fc7dbdSDan Williams * NOTE: If use_new_scheme() is true we will start by issuing 44201da177e4SLinus Torvalds * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 44211da177e4SLinus Torvalds * so it may help with some non-standards-compliant devices. 44221da177e4SLinus Torvalds * Otherwise we start with SET_ADDRESS and then try to read the 44231da177e4SLinus Torvalds * first 8 bytes of the device descriptor to get the ep0 maxpacket 44241da177e4SLinus Torvalds * value. 44251da177e4SLinus Torvalds */ 44260d5ce778SOliver Neukum for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { 442748fc7dbdSDan Williams bool did_new_scheme = false; 442848fc7dbdSDan Williams 442948fc7dbdSDan Williams if (use_new_scheme(udev, retry_counter)) { 44301da177e4SLinus Torvalds struct usb_device_descriptor *buf; 44311da177e4SLinus Torvalds int r = 0; 44321da177e4SLinus Torvalds 443348fc7dbdSDan Williams did_new_scheme = true; 443448fc7dbdSDan Williams retval = hub_enable_device(udev); 4435938569ebSOliver Neukum if (retval < 0) { 4436938569ebSOliver Neukum dev_err(&udev->dev, 4437938569ebSOliver Neukum "hub failed to enable device, error %d\n", 4438938569ebSOliver Neukum retval); 443948fc7dbdSDan Williams goto fail; 4440938569ebSOliver Neukum } 444148fc7dbdSDan Williams 44421da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE 64 44431da177e4SLinus Torvalds buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 44441da177e4SLinus Torvalds if (!buf) { 44451da177e4SLinus Torvalds retval = -ENOMEM; 44461da177e4SLinus Torvalds continue; 44471da177e4SLinus Torvalds } 44481da177e4SLinus Torvalds 4449b89ee19aSAlan Stern /* Retry on all errors; some devices are flakey. 4450b89ee19aSAlan Stern * 255 is for WUSB devices, we actually need to use 4451b89ee19aSAlan Stern * 512 (WUSB1.0[4.8.1]). 44521da177e4SLinus Torvalds */ 44530d5ce778SOliver Neukum for (operations = 0; operations < 3; ++operations) { 44541da177e4SLinus Torvalds buf->bMaxPacketSize0 = 0; 44551da177e4SLinus Torvalds r = usb_control_msg(udev, usb_rcvaddr0pipe(), 44561da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 44571da177e4SLinus Torvalds USB_DT_DEVICE << 8, 0, 44581da177e4SLinus Torvalds buf, GET_DESCRIPTOR_BUFSIZE, 4459fd7c519dSJaroslav Kysela initial_descriptor_timeout); 44601da177e4SLinus Torvalds switch (buf->bMaxPacketSize0) { 44615bb6e0aeSInaky Perez-Gonzalez case 8: case 16: case 32: case 64: case 255: 44621da177e4SLinus Torvalds if (buf->bDescriptorType == 44631da177e4SLinus Torvalds USB_DT_DEVICE) { 44641da177e4SLinus Torvalds r = 0; 44651da177e4SLinus Torvalds break; 44661da177e4SLinus Torvalds } 44671da177e4SLinus Torvalds /* FALL THROUGH */ 44681da177e4SLinus Torvalds default: 44691da177e4SLinus Torvalds if (r == 0) 44701da177e4SLinus Torvalds r = -EPROTO; 44711da177e4SLinus Torvalds break; 44721da177e4SLinus Torvalds } 4473264904ccSOliver Neukum /* 4474264904ccSOliver Neukum * Some devices time out if they are powered on 4475264904ccSOliver Neukum * when already connected. They need a second 4476264904ccSOliver Neukum * reset. But only on the first attempt, 4477264904ccSOliver Neukum * lest we get into a time out/reset loop 4478264904ccSOliver Neukum */ 44790d5ce778SOliver Neukum if (r == 0 || (r == -ETIMEDOUT && retries == 0)) 44801da177e4SLinus Torvalds break; 44811da177e4SLinus Torvalds } 44821da177e4SLinus Torvalds udev->descriptor.bMaxPacketSize0 = 44831da177e4SLinus Torvalds buf->bMaxPacketSize0; 44841da177e4SLinus Torvalds kfree(buf); 44851da177e4SLinus Torvalds 448675d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false); 44871da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 44881da177e4SLinus Torvalds goto fail; 44891da177e4SLinus Torvalds if (oldspeed != udev->speed) { 44901da177e4SLinus Torvalds dev_dbg(&udev->dev, 44911da177e4SLinus Torvalds "device reset changed speed!\n"); 44921da177e4SLinus Torvalds retval = -ENODEV; 44931da177e4SLinus Torvalds goto fail; 44941da177e4SLinus Torvalds } 44951da177e4SLinus Torvalds if (r) { 4496e9e88fb7SAlan Stern if (r != -ENODEV) 4497e9e88fb7SAlan Stern dev_err(&udev->dev, "device descriptor read/64, error %d\n", 4498b9cef6c3SWu Fengguang r); 44991da177e4SLinus Torvalds retval = -EMSGSIZE; 45001da177e4SLinus Torvalds continue; 45011da177e4SLinus Torvalds } 45021da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE 45031da177e4SLinus Torvalds } 45041da177e4SLinus Torvalds 45056c529cdcSInaky Perez-Gonzalez /* 45066c529cdcSInaky Perez-Gonzalez * If device is WUSB, we already assigned an 45076c529cdcSInaky Perez-Gonzalez * unauthorized address in the Connect Ack sequence; 45086c529cdcSInaky Perez-Gonzalez * authorization will assign the final address. 45096c529cdcSInaky Perez-Gonzalez */ 45106c529cdcSInaky Perez-Gonzalez if (udev->wusb == 0) { 45110d5ce778SOliver Neukum for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) { 45124326ed0bSAlan Stern retval = hub_set_address(udev, devnum); 45131da177e4SLinus Torvalds if (retval >= 0) 45141da177e4SLinus Torvalds break; 45151da177e4SLinus Torvalds msleep(200); 45161da177e4SLinus Torvalds } 45171da177e4SLinus Torvalds if (retval < 0) { 4518e9e88fb7SAlan Stern if (retval != -ENODEV) 4519e9e88fb7SAlan Stern dev_err(&udev->dev, "device not accepting address %d, error %d\n", 45204326ed0bSAlan Stern devnum, retval); 45211da177e4SLinus Torvalds goto fail; 45221da177e4SLinus Torvalds } 45238a1b2725SMathias Nyman if (udev->speed >= USB_SPEED_SUPER) { 4524c6515272SSarah Sharp devnum = udev->devnum; 4525c6515272SSarah Sharp dev_info(&udev->dev, 45268a1b2725SMathias Nyman "%s SuperSpeed%s USB device number %d using %s\n", 4527c6515272SSarah Sharp (udev->config) ? "reset" : "new", 45288a1b2725SMathias Nyman (udev->speed == USB_SPEED_SUPER_PLUS) ? "Plus" : "", 45293b29b68bSAlan Stern devnum, udev->bus->controller->driver->name); 4530c6515272SSarah Sharp } 45311da177e4SLinus Torvalds 45321da177e4SLinus Torvalds /* cope with hardware quirkiness: 45331da177e4SLinus Torvalds * - let SET_ADDRESS settle, some device hardware wants it 45341da177e4SLinus Torvalds * - read ep0 maxpacket even for high and low speed, 45351da177e4SLinus Torvalds */ 45361da177e4SLinus Torvalds msleep(10); 453748fc7dbdSDan Williams /* use_new_scheme() checks the speed which may have 453848fc7dbdSDan Williams * changed since the initial look so we cache the result 453948fc7dbdSDan Williams * in did_new_scheme 454048fc7dbdSDan Williams */ 454148fc7dbdSDan Williams if (did_new_scheme) 45421da177e4SLinus Torvalds break; 45436c529cdcSInaky Perez-Gonzalez } 45441da177e4SLinus Torvalds 45451da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, 8); 45461da177e4SLinus Torvalds if (retval < 8) { 4547e9e88fb7SAlan Stern if (retval != -ENODEV) 4548b9cef6c3SWu Fengguang dev_err(&udev->dev, 4549b9cef6c3SWu Fengguang "device descriptor read/8, error %d\n", 4550b9cef6c3SWu Fengguang retval); 45511da177e4SLinus Torvalds if (retval >= 0) 45521da177e4SLinus Torvalds retval = -EMSGSIZE; 45531da177e4SLinus Torvalds } else { 45541da177e4SLinus Torvalds retval = 0; 45551da177e4SLinus Torvalds break; 45561da177e4SLinus Torvalds } 45571da177e4SLinus Torvalds } 45581da177e4SLinus Torvalds if (retval) 45591da177e4SLinus Torvalds goto fail; 45601da177e4SLinus Torvalds 4561d8aec3dbSElric Fu /* 4562d8aec3dbSElric Fu * Some superspeed devices have finished the link training process 4563d8aec3dbSElric Fu * and attached to a superspeed hub port, but the device descriptor 4564d8aec3dbSElric Fu * got from those devices show they aren't superspeed devices. Warm 4565d8aec3dbSElric Fu * reset the port attached by the devices can fix them. 4566d8aec3dbSElric Fu */ 45678a1b2725SMathias Nyman if ((udev->speed >= USB_SPEED_SUPER) && 4568d8aec3dbSElric Fu (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 4569d8aec3dbSElric Fu dev_err(&udev->dev, "got a wrong device descriptor, " 4570d8aec3dbSElric Fu "warm reset device\n"); 4571d8aec3dbSElric Fu hub_port_reset(hub, port1, udev, 4572d8aec3dbSElric Fu HUB_BH_RESET_TIME, true); 4573d8aec3dbSElric Fu retval = -EINVAL; 4574d8aec3dbSElric Fu goto fail; 4575d8aec3dbSElric Fu } 4576d8aec3dbSElric Fu 45776b403b02SSarah Sharp if (udev->descriptor.bMaxPacketSize0 == 0xff || 45788a1b2725SMathias Nyman udev->speed >= USB_SPEED_SUPER) 45796b403b02SSarah Sharp i = 512; 45806b403b02SSarah Sharp else 45816b403b02SSarah Sharp i = udev->descriptor.bMaxPacketSize0; 458229cc8897SKuninori Morimoto if (usb_endpoint_maxp(&udev->ep0.desc) != i) { 458356626a72SAlan Stern if (udev->speed == USB_SPEED_LOW || 45841da177e4SLinus Torvalds !(i == 8 || i == 16 || i == 32 || i == 64)) { 458556626a72SAlan Stern dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); 45861da177e4SLinus Torvalds retval = -EMSGSIZE; 45871da177e4SLinus Torvalds goto fail; 45881da177e4SLinus Torvalds } 458956626a72SAlan Stern if (udev->speed == USB_SPEED_FULL) 45901da177e4SLinus Torvalds dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 459156626a72SAlan Stern else 459256626a72SAlan Stern dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 45931da177e4SLinus Torvalds udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 4594fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 45951da177e4SLinus Torvalds } 45961da177e4SLinus Torvalds 45971da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 45981da177e4SLinus Torvalds if (retval < (signed)sizeof(udev->descriptor)) { 4599e9e88fb7SAlan Stern if (retval != -ENODEV) 4600b9cef6c3SWu Fengguang dev_err(&udev->dev, "device descriptor read/all, error %d\n", 4601b9cef6c3SWu Fengguang retval); 46021da177e4SLinus Torvalds if (retval >= 0) 46031da177e4SLinus Torvalds retval = -ENOMSG; 46041da177e4SLinus Torvalds goto fail; 46051da177e4SLinus Torvalds } 46061da177e4SLinus Torvalds 4607ad87e032SAlan Stern usb_detect_quirks(udev); 4608ad87e032SAlan Stern 46091ff4df56SAndiry Xu if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 46101ff4df56SAndiry Xu retval = usb_get_bos_descriptor(udev); 461151e0a012SSarah Sharp if (!retval) { 4612d9b2099cSSarah Sharp udev->lpm_capable = usb_device_supports_lpm(udev); 461351e0a012SSarah Sharp usb_set_lpm_parameters(udev); 461451e0a012SSarah Sharp } 46151ff4df56SAndiry Xu } 46163148bf04SAndiry Xu 46171da177e4SLinus Torvalds retval = 0; 461848f24970SAlek Du /* notify HCD that we have a device connected and addressed */ 461948f24970SAlek Du if (hcd->driver->update_device) 462048f24970SAlek Du hcd->driver->update_device(hcd, udev); 4621890dae88SMathias Nyman hub_set_initial_usb2_lpm_policy(udev); 46221da177e4SLinus Torvalds fail: 46234326ed0bSAlan Stern if (retval) { 46241da177e4SLinus Torvalds hub_port_disable(hub, port1, 0); 46253b29b68bSAlan Stern update_devnum(udev, devnum); /* for disconnect processing */ 46264326ed0bSAlan Stern } 4627feb26ac3SChris Bainbridge mutex_unlock(hcd->address0_mutex); 46281da177e4SLinus Torvalds return retval; 46291da177e4SLinus Torvalds } 46301da177e4SLinus Torvalds 46311da177e4SLinus Torvalds static void 46321da177e4SLinus Torvalds check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) 46331da177e4SLinus Torvalds { 46341da177e4SLinus Torvalds struct usb_qualifier_descriptor *qual; 46351da177e4SLinus Torvalds int status; 46361da177e4SLinus Torvalds 46372a159389SJohan Hovold if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) 46382a159389SJohan Hovold return; 46392a159389SJohan Hovold 4640e94b1766SChristoph Lameter qual = kmalloc(sizeof *qual, GFP_KERNEL); 46411da177e4SLinus Torvalds if (qual == NULL) 46421da177e4SLinus Torvalds return; 46431da177e4SLinus Torvalds 46441da177e4SLinus Torvalds status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0, 46451da177e4SLinus Torvalds qual, sizeof *qual); 46461da177e4SLinus Torvalds if (status == sizeof *qual) { 46471da177e4SLinus Torvalds dev_info(&udev->dev, "not running at top speed; " 46481da177e4SLinus Torvalds "connect to a high speed hub\n"); 46491da177e4SLinus Torvalds /* hub LEDs are probably harder to miss than syslog */ 46501da177e4SLinus Torvalds if (hub->has_indicators) { 46511da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 465222f6a0f0SShaibal Dutta queue_delayed_work(system_power_efficient_wq, 465322f6a0f0SShaibal Dutta &hub->leds, 0); 46541da177e4SLinus Torvalds } 46551da177e4SLinus Torvalds } 46561da177e4SLinus Torvalds kfree(qual); 46571da177e4SLinus Torvalds } 46581da177e4SLinus Torvalds 46591da177e4SLinus Torvalds static unsigned 46601da177e4SLinus Torvalds hub_power_remaining(struct usb_hub *hub) 46611da177e4SLinus Torvalds { 46621da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 46631da177e4SLinus Torvalds int remaining; 466455c52718SAlan Stern int port1; 46651da177e4SLinus Torvalds 466655c52718SAlan Stern if (!hub->limited_power) 46671da177e4SLinus Torvalds return 0; 46681da177e4SLinus Torvalds 466955c52718SAlan Stern remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 467055c52718SAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 4671d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 4672d99f6b41SDan Williams struct usb_device *udev = port_dev->child; 4673430ee58eSSebastian Andrzej Siewior unsigned unit_load; 4674d99f6b41SDan Williams int delta; 46751da177e4SLinus Torvalds 46761da177e4SLinus Torvalds if (!udev) 46771da177e4SLinus Torvalds continue; 4678430ee58eSSebastian Andrzej Siewior if (hub_is_superspeed(udev)) 4679430ee58eSSebastian Andrzej Siewior unit_load = 150; 4680430ee58eSSebastian Andrzej Siewior else 4681430ee58eSSebastian Andrzej Siewior unit_load = 100; 46821da177e4SLinus Torvalds 4683430ee58eSSebastian Andrzej Siewior /* 4684430ee58eSSebastian Andrzej Siewior * Unconfigured devices may not use more than one unit load, 4685430ee58eSSebastian Andrzej Siewior * or 8mA for OTG ports 4686430ee58eSSebastian Andrzej Siewior */ 46871da177e4SLinus Torvalds if (udev->actconfig) 46888d8479dbSSebastian Andrzej Siewior delta = usb_get_max_power(udev, udev->actconfig); 468955c52718SAlan Stern else if (port1 != udev->bus->otg_port || hdev->parent) 4690430ee58eSSebastian Andrzej Siewior delta = unit_load; 46911da177e4SLinus Torvalds else 469255c52718SAlan Stern delta = 8; 469355c52718SAlan Stern if (delta > hub->mA_per_port) 4694d99f6b41SDan Williams dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", 4695d99f6b41SDan Williams delta, hub->mA_per_port); 46961da177e4SLinus Torvalds remaining -= delta; 46971da177e4SLinus Torvalds } 46981da177e4SLinus Torvalds if (remaining < 0) { 469955c52718SAlan Stern dev_warn(hub->intfdev, "%dmA over power budget!\n", 470055c52718SAlan Stern -remaining); 47011da177e4SLinus Torvalds remaining = 0; 47021da177e4SLinus Torvalds } 47031da177e4SLinus Torvalds return remaining; 47041da177e4SLinus Torvalds } 47051da177e4SLinus Torvalds 4706af376a46SDan Williams static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, 4707af376a46SDan Williams u16 portchange) 47081da177e4SLinus Torvalds { 47091da177e4SLinus Torvalds int status, i; 4710430ee58eSSebastian Andrzej Siewior unsigned unit_load; 47111da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 47121da177e4SLinus Torvalds struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 4713d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 4714af376a46SDan Williams struct usb_device *udev = port_dev->child; 47155ee0f803SOliver Neukum static int unreliable_port = -1; 47168808f00cSAlan Stern 471724618b0cSAlan Stern /* Disconnect any existing devices under this port */ 4718b76baa81SPeter Chen if (udev) { 4719b2108f1eSPeter Chen if (hcd->usb_phy && !hdev->parent) 47203d46e73dSAntoine Tenart usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); 4721d99f6b41SDan Williams usb_disconnect(&port_dev->child); 4722b76baa81SPeter Chen } 472324618b0cSAlan Stern 4724253e0572SAlan Stern /* We can forget about a "removed" device when there's a physical 4725253e0572SAlan Stern * disconnect or the connect status changes. 4726253e0572SAlan Stern */ 4727253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 4728253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION)) 4729253e0572SAlan Stern clear_bit(port1, hub->removed_bits); 4730253e0572SAlan Stern 47315257d97aSAlan Stern if (portchange & (USB_PORT_STAT_C_CONNECTION | 47325257d97aSAlan Stern USB_PORT_STAT_C_ENABLE)) { 4733ad493e5eSLan Tianyu status = hub_port_debounce_be_stable(hub, port1); 47345257d97aSAlan Stern if (status < 0) { 47355ee0f803SOliver Neukum if (status != -ENODEV && 47365ee0f803SOliver Neukum port1 != unreliable_port && 47375ee0f803SOliver Neukum printk_ratelimit()) 4738dd5f5006STakashi Iwai dev_err(&port_dev->dev, "connect-debounce failed\n"); 47395257d97aSAlan Stern portstatus &= ~USB_PORT_STAT_CONNECTION; 47405ee0f803SOliver Neukum unreliable_port = port1; 47415257d97aSAlan Stern } else { 47425257d97aSAlan Stern portstatus = status; 47435257d97aSAlan Stern } 47445257d97aSAlan Stern } 47455257d97aSAlan Stern 4746253e0572SAlan Stern /* Return now if debouncing failed or nothing is connected or 4747253e0572SAlan Stern * the device was "removed". 4748253e0572SAlan Stern */ 4749253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 4750253e0572SAlan Stern test_bit(port1, hub->removed_bits)) { 47511da177e4SLinus Torvalds 4752fbaecff0SDeepak Das /* 4753fbaecff0SDeepak Das * maybe switch power back on (e.g. root hub was reset) 4754fbaecff0SDeepak Das * but only if the port isn't owned by someone else. 4755fbaecff0SDeepak Das */ 47569262c19dSDan Williams if (hub_is_port_power_switchable(hub) 4757fbaecff0SDeepak Das && !port_is_power_on(hub, portstatus) 4758fbaecff0SDeepak Das && !port_dev->port_owner) 47591da177e4SLinus Torvalds set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 47601da177e4SLinus Torvalds 47611da177e4SLinus Torvalds if (portstatus & USB_PORT_STAT_ENABLE) 47621da177e4SLinus Torvalds goto done; 47631da177e4SLinus Torvalds return; 47641da177e4SLinus Torvalds } 4765430ee58eSSebastian Andrzej Siewior if (hub_is_superspeed(hub->hdev)) 4766430ee58eSSebastian Andrzej Siewior unit_load = 150; 4767430ee58eSSebastian Andrzej Siewior else 4768430ee58eSSebastian Andrzej Siewior unit_load = 100; 47691da177e4SLinus Torvalds 4770e9e88fb7SAlan Stern status = 0; 47711da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; i++) { 47721da177e4SLinus Torvalds 47731da177e4SLinus Torvalds /* reallocate for each attempt, since references 47741da177e4SLinus Torvalds * to the previous one can escape in various ways 47751da177e4SLinus Torvalds */ 47761da177e4SLinus Torvalds udev = usb_alloc_dev(hdev, hdev->bus, port1); 47771da177e4SLinus Torvalds if (!udev) { 4778d99f6b41SDan Williams dev_err(&port_dev->dev, 4779d99f6b41SDan Williams "couldn't allocate usb_device\n"); 47801da177e4SLinus Torvalds goto done; 47811da177e4SLinus Torvalds } 47821da177e4SLinus Torvalds 47831da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_POWERED); 478455c52718SAlan Stern udev->bus_mA = hub->mA_per_port; 4785b6956ffaSAlan Stern udev->level = hdev->level + 1; 47868af548dcSInaky Perez-Gonzalez udev->wusb = hub_is_wusb(hub); 47871da177e4SLinus Torvalds 47888a1b2725SMathias Nyman /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ 4789131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev)) 4790e7b77172SSarah Sharp udev->speed = USB_SPEED_SUPER; 4791e7b77172SSarah Sharp else 4792e7b77172SSarah Sharp udev->speed = USB_SPEED_UNKNOWN; 4793e7b77172SSarah Sharp 47943b29b68bSAlan Stern choose_devnum(udev); 4795c6515272SSarah Sharp if (udev->devnum <= 0) { 4796c6515272SSarah Sharp status = -ENOTCONN; /* Don't retry */ 4797c6515272SSarah Sharp goto loop; 4798c6515272SSarah Sharp } 4799c6515272SSarah Sharp 4800e7b77172SSarah Sharp /* reset (non-USB 3.0 devices) and get descriptor */ 48015c79a1e3SDan Williams usb_lock_port(port_dev); 48021da177e4SLinus Torvalds status = hub_port_init(hub, udev, port1, i); 48035c79a1e3SDan Williams usb_unlock_port(port_dev); 48041da177e4SLinus Torvalds if (status < 0) 48051da177e4SLinus Torvalds goto loop; 48061da177e4SLinus Torvalds 480793362a87SPhil Dibowitz if (udev->quirks & USB_QUIRK_DELAY_INIT) 480893362a87SPhil Dibowitz msleep(1000); 480993362a87SPhil Dibowitz 48101da177e4SLinus Torvalds /* consecutive bus-powered hubs aren't reliable; they can 48111da177e4SLinus Torvalds * violate the voltage drop budget. if the new child has 48121da177e4SLinus Torvalds * a "powered" LED, users should notice we didn't enable it 48131da177e4SLinus Torvalds * (without reading syslog), even without per-port LEDs 48141da177e4SLinus Torvalds * on the parent. 48151da177e4SLinus Torvalds */ 48161da177e4SLinus Torvalds if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 4817430ee58eSSebastian Andrzej Siewior && udev->bus_mA <= unit_load) { 48181da177e4SLinus Torvalds u16 devstat; 48191da177e4SLinus Torvalds 48201da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, 48211da177e4SLinus Torvalds &devstat); 482215b7336eSAlan Stern if (status) { 48231da177e4SLinus Torvalds dev_dbg(&udev->dev, "get status %d ?\n", status); 48241da177e4SLinus Torvalds goto loop_disable; 48251da177e4SLinus Torvalds } 48261da177e4SLinus Torvalds if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 48271da177e4SLinus Torvalds dev_err(&udev->dev, 48281da177e4SLinus Torvalds "can't connect bus-powered hub " 48291da177e4SLinus Torvalds "to this port\n"); 48301da177e4SLinus Torvalds if (hub->has_indicators) { 48311da177e4SLinus Torvalds hub->indicator[port1-1] = 48321da177e4SLinus Torvalds INDICATOR_AMBER_BLINK; 483322f6a0f0SShaibal Dutta queue_delayed_work( 483422f6a0f0SShaibal Dutta system_power_efficient_wq, 483522f6a0f0SShaibal Dutta &hub->leds, 0); 48361da177e4SLinus Torvalds } 48371da177e4SLinus Torvalds status = -ENOTCONN; /* Don't retry */ 48381da177e4SLinus Torvalds goto loop_disable; 48391da177e4SLinus Torvalds } 48401da177e4SLinus Torvalds } 48411da177e4SLinus Torvalds 48421da177e4SLinus Torvalds /* check for devices running slower than they could */ 48431da177e4SLinus Torvalds if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 48441da177e4SLinus Torvalds && udev->speed == USB_SPEED_FULL 48451da177e4SLinus Torvalds && highspeed_hubs != 0) 48461da177e4SLinus Torvalds check_highspeed(hub, udev, port1); 48471da177e4SLinus Torvalds 4848fa286188SGreg Kroah-Hartman /* Store the parent's children[] pointer. At this point 48491da177e4SLinus Torvalds * udev becomes globally accessible, although presumably 48501da177e4SLinus Torvalds * no one will look at it until hdev is unlocked. 48511da177e4SLinus Torvalds */ 48521da177e4SLinus Torvalds status = 0; 48531da177e4SLinus Torvalds 4854d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex); 4855d8521afeSDan Williams 48561da177e4SLinus Torvalds /* We mustn't add new devices if the parent hub has 48571da177e4SLinus Torvalds * been disconnected; we would race with the 48581da177e4SLinus Torvalds * recursively_mark_NOTATTACHED() routine. 48591da177e4SLinus Torvalds */ 48601da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 48611da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) 48621da177e4SLinus Torvalds status = -ENOTCONN; 48631da177e4SLinus Torvalds else 4864d99f6b41SDan Williams port_dev->child = udev; 48651da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 4866d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex); 48671da177e4SLinus Torvalds 48681da177e4SLinus Torvalds /* Run it through the hoops (find a driver, etc) */ 48691da177e4SLinus Torvalds if (!status) { 48701da177e4SLinus Torvalds status = usb_new_device(udev); 48711da177e4SLinus Torvalds if (status) { 4872d8521afeSDan Williams mutex_lock(&usb_port_peer_mutex); 48731da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 4874d99f6b41SDan Williams port_dev->child = NULL; 48751da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 4876d8521afeSDan Williams mutex_unlock(&usb_port_peer_mutex); 487701ed67dcSTony Zheng } else { 487801ed67dcSTony Zheng if (hcd->usb_phy && !hdev->parent) 487901ed67dcSTony Zheng usb_phy_notify_connect(hcd->usb_phy, 488001ed67dcSTony Zheng udev->speed); 48811da177e4SLinus Torvalds } 48821da177e4SLinus Torvalds } 48831da177e4SLinus Torvalds 48841da177e4SLinus Torvalds if (status) 48851da177e4SLinus Torvalds goto loop_disable; 48861da177e4SLinus Torvalds 48871da177e4SLinus Torvalds status = hub_power_remaining(hub); 48881da177e4SLinus Torvalds if (status) 4889d99f6b41SDan Williams dev_dbg(hub->intfdev, "%dmA power budget left\n", status); 48901da177e4SLinus Torvalds 48911da177e4SLinus Torvalds return; 48921da177e4SLinus Torvalds 48931da177e4SLinus Torvalds loop_disable: 48941da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 48951da177e4SLinus Torvalds loop: 4896fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 48973b29b68bSAlan Stern release_devnum(udev); 4898f7410cedSHerbert Xu hub_free_dev(udev); 48991da177e4SLinus Torvalds usb_put_dev(udev); 4900ffcdc18dSVikram Pandita if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 49011da177e4SLinus Torvalds break; 49021da177e4SLinus Torvalds } 49033a31155cSAlan Stern if (hub->hdev->parent || 49043a31155cSAlan Stern !hcd->driver->port_handed_over || 4905e9e88fb7SAlan Stern !(hcd->driver->port_handed_over)(hcd, port1)) { 4906e9e88fb7SAlan Stern if (status != -ENOTCONN && status != -ENODEV) 4907d99f6b41SDan Williams dev_err(&port_dev->dev, 4908d99f6b41SDan Williams "unable to enumerate USB device\n"); 4909e9e88fb7SAlan Stern } 49101da177e4SLinus Torvalds 49111da177e4SLinus Torvalds done: 49121da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 491390da096eSBalaji Rao if (hcd->driver->relinquish_port && !hub->hdev->parent) 491490da096eSBalaji Rao hcd->driver->relinquish_port(hcd, port1); 4915af376a46SDan Williams 49161da177e4SLinus Torvalds } 49171da177e4SLinus Torvalds 4918af376a46SDan Williams /* Handle physical or logical connection change events. 4919af376a46SDan Williams * This routine is called when: 4920af376a46SDan Williams * a port connection-change occurs; 4921af376a46SDan Williams * a port enable-change occurs (often caused by EMI); 4922af376a46SDan Williams * usb_reset_and_verify_device() encounters changed descriptors (as from 4923af376a46SDan Williams * a firmware download) 4924af376a46SDan Williams * caller already locked the hub 4925af376a46SDan Williams */ 4926af376a46SDan Williams static void hub_port_connect_change(struct usb_hub *hub, int port1, 492772937e1eSSarah Sharp u16 portstatus, u16 portchange) 49285c79a1e3SDan Williams __must_hold(&port_dev->status_lock) 4929714b07beSSarah Sharp { 4930af376a46SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 4931af376a46SDan Williams struct usb_device *udev = port_dev->child; 4932af376a46SDan Williams int status = -ENODEV; 4933714b07beSSarah Sharp 4934af376a46SDan Williams dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, 4935af376a46SDan Williams portchange, portspeed(hub, portstatus)); 4936af376a46SDan Williams 4937af376a46SDan Williams if (hub->has_indicators) { 4938af376a46SDan Williams set_port_led(hub, port1, HUB_LED_AUTO); 4939af376a46SDan Williams hub->indicator[port1-1] = INDICATOR_AUTO; 49404ee823b8SSarah Sharp } 4941714b07beSSarah Sharp 4942af376a46SDan Williams #ifdef CONFIG_USB_OTG 4943af376a46SDan Williams /* during HNP, don't repeat the debounce */ 4944af376a46SDan Williams if (hub->hdev->bus->is_b_host) 4945af376a46SDan Williams portchange &= ~(USB_PORT_STAT_C_CONNECTION | 4946af376a46SDan Williams USB_PORT_STAT_C_ENABLE); 4947af376a46SDan Williams #endif 4948714b07beSSarah Sharp 4949af376a46SDan Williams /* Try to resuscitate an existing device */ 4950af376a46SDan Williams if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 4951af376a46SDan Williams udev->state != USB_STATE_NOTATTACHED) { 4952af376a46SDan Williams if (portstatus & USB_PORT_STAT_ENABLE) { 4953af376a46SDan Williams status = 0; /* Nothing to do */ 4954ceb6c9c8SRafael J. Wysocki #ifdef CONFIG_PM 4955af376a46SDan Williams } else if (udev->state == USB_STATE_SUSPENDED && 4956af376a46SDan Williams udev->persist_enabled) { 4957af376a46SDan Williams /* For a suspended device, treat this as a 4958af376a46SDan Williams * remote wakeup event. 4959af376a46SDan Williams */ 49605c79a1e3SDan Williams usb_unlock_port(port_dev); 4961af376a46SDan Williams status = usb_remote_wakeup(udev); 49625c79a1e3SDan Williams usb_lock_port(port_dev); 4963af376a46SDan Williams #endif 4964af376a46SDan Williams } else { 4965af376a46SDan Williams /* Don't resuscitate */; 4966af376a46SDan Williams } 4967af376a46SDan Williams } 4968af376a46SDan Williams clear_bit(port1, hub->change_bits); 4969af376a46SDan Williams 49705c79a1e3SDan Williams /* successfully revalidated the connection */ 4971af376a46SDan Williams if (status == 0) 4972af376a46SDan Williams return; 4973af376a46SDan Williams 49745c79a1e3SDan Williams usb_unlock_port(port_dev); 4975af376a46SDan Williams hub_port_connect(hub, port1, portstatus, portchange); 49765c79a1e3SDan Williams usb_lock_port(port_dev); 49771da177e4SLinus Torvalds } 49781da177e4SLinus Torvalds 4979af376a46SDan Williams static void port_event(struct usb_hub *hub, int port1) 49805c79a1e3SDan Williams __must_hold(&port_dev->status_lock) 4981af376a46SDan Williams { 49827671bd1eSZhuang Jin Can int connect_change; 4983af376a46SDan Williams struct usb_port *port_dev = hub->ports[port1 - 1]; 4984af376a46SDan Williams struct usb_device *udev = port_dev->child; 4985af376a46SDan Williams struct usb_device *hdev = hub->hdev; 4986af376a46SDan Williams u16 portstatus, portchange; 4987af376a46SDan Williams 4988af376a46SDan Williams connect_change = test_bit(port1, hub->change_bits); 4989af376a46SDan Williams clear_bit(port1, hub->event_bits); 4990af376a46SDan Williams clear_bit(port1, hub->wakeup_bits); 4991af376a46SDan Williams 4992af376a46SDan Williams if (hub_port_status(hub, port1, &portstatus, &portchange) < 0) 4993af376a46SDan Williams return; 4994af376a46SDan Williams 4995af376a46SDan Williams if (portchange & USB_PORT_STAT_C_CONNECTION) { 4996af376a46SDan Williams usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 4997714b07beSSarah Sharp connect_change = 1; 4998714b07beSSarah Sharp } 4999af376a46SDan Williams 5000af376a46SDan Williams if (portchange & USB_PORT_STAT_C_ENABLE) { 5001af376a46SDan Williams if (!connect_change) 5002af376a46SDan Williams dev_dbg(&port_dev->dev, "enable change, status %08x\n", 5003af376a46SDan Williams portstatus); 5004af376a46SDan Williams usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 5005af376a46SDan Williams 5006af376a46SDan Williams /* 5007af376a46SDan Williams * EM interference sometimes causes badly shielded USB devices 5008af376a46SDan Williams * to be shutdown by the hub, this hack enables them again. 5009af376a46SDan Williams * Works at least with mouse driver. 5010af376a46SDan Williams */ 5011af376a46SDan Williams if (!(portstatus & USB_PORT_STAT_ENABLE) 5012af376a46SDan Williams && !connect_change && udev) { 5013af376a46SDan Williams dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); 5014af376a46SDan Williams connect_change = 1; 5015714b07beSSarah Sharp } 5016af376a46SDan Williams } 5017af376a46SDan Williams 5018af376a46SDan Williams if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 5019af376a46SDan Williams u16 status = 0, unused; 5020af376a46SDan Williams 5021af376a46SDan Williams dev_dbg(&port_dev->dev, "over-current change\n"); 5022af376a46SDan Williams usb_clear_port_feature(hdev, port1, 5023af376a46SDan Williams USB_PORT_FEAT_C_OVER_CURRENT); 5024af376a46SDan Williams msleep(100); /* Cool down */ 5025af376a46SDan Williams hub_power_on(hub, true); 5026af376a46SDan Williams hub_port_status(hub, port1, &status, &unused); 5027af376a46SDan Williams if (status & USB_PORT_STAT_OVERCURRENT) 5028af376a46SDan Williams dev_err(&port_dev->dev, "over-current condition\n"); 5029af376a46SDan Williams } 5030af376a46SDan Williams 5031af376a46SDan Williams if (portchange & USB_PORT_STAT_C_RESET) { 5032af376a46SDan Williams dev_dbg(&port_dev->dev, "reset change\n"); 5033af376a46SDan Williams usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET); 5034af376a46SDan Williams } 5035af376a46SDan Williams if ((portchange & USB_PORT_STAT_C_BH_RESET) 5036af376a46SDan Williams && hub_is_superspeed(hdev)) { 5037af376a46SDan Williams dev_dbg(&port_dev->dev, "warm reset change\n"); 5038af376a46SDan Williams usb_clear_port_feature(hdev, port1, 5039af376a46SDan Williams USB_PORT_FEAT_C_BH_PORT_RESET); 5040af376a46SDan Williams } 5041af376a46SDan Williams if (portchange & USB_PORT_STAT_C_LINK_STATE) { 5042af376a46SDan Williams dev_dbg(&port_dev->dev, "link state change\n"); 5043af376a46SDan Williams usb_clear_port_feature(hdev, port1, 5044af376a46SDan Williams USB_PORT_FEAT_C_PORT_LINK_STATE); 5045af376a46SDan Williams } 5046af376a46SDan Williams if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 5047af376a46SDan Williams dev_warn(&port_dev->dev, "config error\n"); 5048af376a46SDan Williams usb_clear_port_feature(hdev, port1, 5049af376a46SDan Williams USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 5050af376a46SDan Williams } 5051af376a46SDan Williams 5052097a155fSDan Williams /* skip port actions that require the port to be powered on */ 5053097a155fSDan Williams if (!pm_runtime_active(&port_dev->dev)) 5054097a155fSDan Williams return; 5055097a155fSDan Williams 5056af376a46SDan Williams if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) 5057af376a46SDan Williams connect_change = 1; 5058af376a46SDan Williams 5059af376a46SDan Williams /* 5060af376a46SDan Williams * Warm reset a USB3 protocol port if it's in 5061af376a46SDan Williams * SS.Inactive state. 5062af376a46SDan Williams */ 50633cd12f91SDan Williams if (hub_port_warm_reset_required(hub, port1, portstatus)) { 5064af376a46SDan Williams dev_dbg(&port_dev->dev, "do warm reset\n"); 5065af376a46SDan Williams if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION) 5066af376a46SDan Williams || udev->state == USB_STATE_NOTATTACHED) { 5067af376a46SDan Williams if (hub_port_reset(hub, port1, NULL, 5068af376a46SDan Williams HUB_BH_RESET_TIME, true) < 0) 5069af376a46SDan Williams hub_port_disable(hub, port1, 1); 50707671bd1eSZhuang Jin Can } else { 50715c79a1e3SDan Williams usb_unlock_port(port_dev); 5072af376a46SDan Williams usb_lock_device(udev); 5073af376a46SDan Williams usb_reset_device(udev); 5074af376a46SDan Williams usb_unlock_device(udev); 50755c79a1e3SDan Williams usb_lock_port(port_dev); 5076af376a46SDan Williams connect_change = 0; 5077af376a46SDan Williams } 50787671bd1eSZhuang Jin Can } 5079af376a46SDan Williams 5080af376a46SDan Williams if (connect_change) 5081af376a46SDan Williams hub_port_connect_change(hub, port1, portstatus, portchange); 5082af376a46SDan Williams } 5083af376a46SDan Williams 508432a69589SPetr Mladek static void hub_event(struct work_struct *work) 50851da177e4SLinus Torvalds { 50861da177e4SLinus Torvalds struct usb_device *hdev; 50871da177e4SLinus Torvalds struct usb_interface *intf; 50881da177e4SLinus Torvalds struct usb_hub *hub; 50891da177e4SLinus Torvalds struct device *hub_dev; 50901da177e4SLinus Torvalds u16 hubstatus; 50911da177e4SLinus Torvalds u16 hubchange; 50921da177e4SLinus Torvalds int i, ret; 50931da177e4SLinus Torvalds 509432a69589SPetr Mladek hub = container_of(work, struct usb_hub, events); 50955d14f323SPetr Mladek hdev = hub->hdev; 5096e8054854SAlan Stern hub_dev = hub->intfdev; 5097e8054854SAlan Stern intf = to_usb_interface(hub_dev); 509832a69589SPetr Mladek 509940f122f3SAlan Stern dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 51003bbc47d8SKrzysztof Mazur hdev->state, hdev->maxchild, 51011da177e4SLinus Torvalds /* NOTE: expects max 15 ports... */ 51021da177e4SLinus Torvalds (u16) hub->change_bits[0], 510340f122f3SAlan Stern (u16) hub->event_bits[0]); 51041da177e4SLinus Torvalds 51051da177e4SLinus Torvalds /* Lock the device, then check to see if we were 51061da177e4SLinus Torvalds * disconnected while waiting for the lock to succeed. */ 510706b84e8aSAlan Stern usb_lock_device(hdev); 5108e8054854SAlan Stern if (unlikely(hub->disconnected)) 510932a69589SPetr Mladek goto out_hdev_lock; 51101da177e4SLinus Torvalds 51111da177e4SLinus Torvalds /* If the hub has died, clean up after it */ 51121da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) { 51137de18d8bSAlan Stern hub->error = -ENODEV; 51144330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 511532a69589SPetr Mladek goto out_hdev_lock; 51161da177e4SLinus Torvalds } 51171da177e4SLinus Torvalds 511840f122f3SAlan Stern /* Autoresume */ 511940f122f3SAlan Stern ret = usb_autopm_get_interface(intf); 512040f122f3SAlan Stern if (ret) { 512140f122f3SAlan Stern dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 512232a69589SPetr Mladek goto out_hdev_lock; 512340f122f3SAlan Stern } 512440f122f3SAlan Stern 512540f122f3SAlan Stern /* If this is an inactive hub, do nothing */ 512640f122f3SAlan Stern if (hub->quiescing) 5127eb6e2924SPetr Mladek goto out_autopm; 51281da177e4SLinus Torvalds 51291da177e4SLinus Torvalds if (hub->error) { 5130eb6e2924SPetr Mladek dev_dbg(hub_dev, "resetting for error %d\n", hub->error); 51311da177e4SLinus Torvalds 5132742120c6SMing Lei ret = usb_reset_device(hdev); 51331da177e4SLinus Torvalds if (ret) { 5134eb6e2924SPetr Mladek dev_dbg(hub_dev, "error resetting hub: %d\n", ret); 5135eb6e2924SPetr Mladek goto out_autopm; 51361da177e4SLinus Torvalds } 51371da177e4SLinus Torvalds 51381da177e4SLinus Torvalds hub->nerrors = 0; 51391da177e4SLinus Torvalds hub->error = 0; 51401da177e4SLinus Torvalds } 51411da177e4SLinus Torvalds 51421da177e4SLinus Torvalds /* deal with port status changes */ 51433bbc47d8SKrzysztof Mazur for (i = 1; i <= hdev->maxchild; i++) { 5144097a155fSDan Williams struct usb_port *port_dev = hub->ports[i - 1]; 5145a82b76f7SHans de Goede 51465c79a1e3SDan Williams if (test_bit(i, hub->event_bits) 5147af376a46SDan Williams || test_bit(i, hub->change_bits) 51485c79a1e3SDan Williams || test_bit(i, hub->wakeup_bits)) { 51491da177e4SLinus Torvalds /* 5150097a155fSDan Williams * The get_noresume and barrier ensure that if 5151097a155fSDan Williams * the port was in the process of resuming, we 5152097a155fSDan Williams * flush that work and keep the port active for 5153097a155fSDan Williams * the duration of the port_event(). However, 5154097a155fSDan Williams * if the port is runtime pm suspended 5155097a155fSDan Williams * (powered-off), we leave it in that state, run 5156097a155fSDan Williams * an abbreviated port_event(), and move on. 51571da177e4SLinus Torvalds */ 5158097a155fSDan Williams pm_runtime_get_noresume(&port_dev->dev); 5159097a155fSDan Williams pm_runtime_barrier(&port_dev->dev); 51605c79a1e3SDan Williams usb_lock_port(port_dev); 5161af376a46SDan Williams port_event(hub, i); 51625c79a1e3SDan Williams usb_unlock_port(port_dev); 5163097a155fSDan Williams pm_runtime_put_sync(&port_dev->dev); 51641da177e4SLinus Torvalds } 51651da177e4SLinus Torvalds } 51661da177e4SLinus Torvalds 51671da177e4SLinus Torvalds /* deal with hub status changes */ 51681da177e4SLinus Torvalds if (test_and_clear_bit(0, hub->event_bits) == 0) 51691da177e4SLinus Torvalds ; /* do nothing */ 51701da177e4SLinus Torvalds else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 51711da177e4SLinus Torvalds dev_err(hub_dev, "get_hub_status failed\n"); 51721da177e4SLinus Torvalds else { 51731da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_LOCAL_POWER) { 51741da177e4SLinus Torvalds dev_dbg(hub_dev, "power change\n"); 51751da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 517655c52718SAlan Stern if (hubstatus & HUB_STATUS_LOCAL_POWER) 517755c52718SAlan Stern /* FIXME: Is this always true? */ 517855c52718SAlan Stern hub->limited_power = 1; 5179403fae78Sjidong xiao else 5180403fae78Sjidong xiao hub->limited_power = 0; 51811da177e4SLinus Torvalds } 51821da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_OVERCURRENT) { 5183752d57a8SPaul Bolle u16 status = 0; 5184752d57a8SPaul Bolle u16 unused; 5185752d57a8SPaul Bolle 5186752d57a8SPaul Bolle dev_dbg(hub_dev, "over-current change\n"); 51871da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 5188752d57a8SPaul Bolle msleep(500); /* Cool down */ 51898520f380SAlan Stern hub_power_on(hub, true); 5190752d57a8SPaul Bolle hub_hub_status(hub, &status, &unused); 5191752d57a8SPaul Bolle if (status & HUB_STATUS_OVERCURRENT) 5192eb6e2924SPetr Mladek dev_err(hub_dev, "over-current condition\n"); 51931da177e4SLinus Torvalds } 51941da177e4SLinus Torvalds } 51951da177e4SLinus Torvalds 5196eb6e2924SPetr Mladek out_autopm: 51978e4ceb38SAlan Stern /* Balance the usb_autopm_get_interface() above */ 51988e4ceb38SAlan Stern usb_autopm_put_interface_no_suspend(intf); 519932a69589SPetr Mladek out_hdev_lock: 52001da177e4SLinus Torvalds usb_unlock_device(hdev); 520132a69589SPetr Mladek 520232a69589SPetr Mladek /* Balance the stuff in kick_hub_wq() and allow autosuspend */ 520332a69589SPetr Mladek usb_autopm_put_interface(intf); 5204e8054854SAlan Stern kref_put(&hub->kref, hub_release); 52051da177e4SLinus Torvalds } 52061da177e4SLinus Torvalds 52071e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = { 5208e6f30deaSMing Lei { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5209e6f30deaSMing Lei | USB_DEVICE_ID_MATCH_INT_CLASS, 5210e6f30deaSMing Lei .idVendor = USB_VENDOR_GENESYS_LOGIC, 5211e6f30deaSMing Lei .bInterfaceClass = USB_CLASS_HUB, 5212e6f30deaSMing Lei .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, 52131da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 52141da177e4SLinus Torvalds .bDeviceClass = USB_CLASS_HUB}, 52151da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 52161da177e4SLinus Torvalds .bInterfaceClass = USB_CLASS_HUB}, 52171da177e4SLinus Torvalds { } /* Terminating entry */ 52181da177e4SLinus Torvalds }; 52191da177e4SLinus Torvalds 52201da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, hub_id_table); 52211da177e4SLinus Torvalds 52221da177e4SLinus Torvalds static struct usb_driver hub_driver = { 52231da177e4SLinus Torvalds .name = "hub", 52241da177e4SLinus Torvalds .probe = hub_probe, 52251da177e4SLinus Torvalds .disconnect = hub_disconnect, 52261da177e4SLinus Torvalds .suspend = hub_suspend, 52271da177e4SLinus Torvalds .resume = hub_resume, 5228f07600cfSAlan Stern .reset_resume = hub_reset_resume, 52297de18d8bSAlan Stern .pre_reset = hub_pre_reset, 52307de18d8bSAlan Stern .post_reset = hub_post_reset, 5231c532b29aSAndi Kleen .unlocked_ioctl = hub_ioctl, 52321da177e4SLinus Torvalds .id_table = hub_id_table, 523340f122f3SAlan Stern .supports_autosuspend = 1, 52341da177e4SLinus Torvalds }; 52351da177e4SLinus Torvalds 52361da177e4SLinus Torvalds int usb_hub_init(void) 52371da177e4SLinus Torvalds { 52381da177e4SLinus Torvalds if (usb_register(&hub_driver) < 0) { 52391da177e4SLinus Torvalds printk(KERN_ERR "%s: can't register hub driver\n", 52401da177e4SLinus Torvalds usbcore_name); 52411da177e4SLinus Torvalds return -1; 52421da177e4SLinus Torvalds } 52431da177e4SLinus Torvalds 524432a69589SPetr Mladek /* 524532a69589SPetr Mladek * The workqueue needs to be freezable to avoid interfering with 524632a69589SPetr Mladek * USB-PERSIST port handover. Otherwise it might see that a full-speed 524732a69589SPetr Mladek * device was gone before the EHCI controller had handed its port 524832a69589SPetr Mladek * over to the companion full-speed controller. 524932a69589SPetr Mladek */ 5250638139ebSPetr Mladek hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0); 525132a69589SPetr Mladek if (hub_wq) 52521da177e4SLinus Torvalds return 0; 52531da177e4SLinus Torvalds 52541da177e4SLinus Torvalds /* Fall through if kernel_thread failed */ 52551da177e4SLinus Torvalds usb_deregister(&hub_driver); 525632a69589SPetr Mladek pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); 52571da177e4SLinus Torvalds 52581da177e4SLinus Torvalds return -1; 52591da177e4SLinus Torvalds } 52601da177e4SLinus Torvalds 52611da177e4SLinus Torvalds void usb_hub_cleanup(void) 52621da177e4SLinus Torvalds { 526332a69589SPetr Mladek destroy_workqueue(hub_wq); 52641da177e4SLinus Torvalds 52651da177e4SLinus Torvalds /* 52661da177e4SLinus Torvalds * Hub resources are freed for us by usb_deregister. It calls 52671da177e4SLinus Torvalds * usb_driver_purge on every device which in turn calls that 52681da177e4SLinus Torvalds * devices disconnect function if it is using this driver. 52691da177e4SLinus Torvalds * The hub_disconnect function takes care of releasing the 52701da177e4SLinus Torvalds * individual hub resources. -greg 52711da177e4SLinus Torvalds */ 52721da177e4SLinus Torvalds usb_deregister(&hub_driver); 52731da177e4SLinus Torvalds } /* usb_hub_cleanup() */ 52741da177e4SLinus Torvalds 5275eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev, 5276e3376d6cSXenia Ragiadakou struct usb_device_descriptor *old_device_descriptor, 5277e3376d6cSXenia Ragiadakou struct usb_host_bos *old_bos) 52781da177e4SLinus Torvalds { 5279eb764c4bSAlan Stern int changed = 0; 52801da177e4SLinus Torvalds unsigned index; 5281eb764c4bSAlan Stern unsigned serial_len = 0; 5282eb764c4bSAlan Stern unsigned len; 5283eb764c4bSAlan Stern unsigned old_length; 5284eb764c4bSAlan Stern int length; 5285eb764c4bSAlan Stern char *buf; 52861da177e4SLinus Torvalds 5287eb764c4bSAlan Stern if (memcmp(&udev->descriptor, old_device_descriptor, 5288eb764c4bSAlan Stern sizeof(*old_device_descriptor)) != 0) 5289eb764c4bSAlan Stern return 1; 5290eb764c4bSAlan Stern 5291e3376d6cSXenia Ragiadakou if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) 5292e3376d6cSXenia Ragiadakou return 1; 5293e3376d6cSXenia Ragiadakou if (udev->bos) { 5294b9a10481SXenia Ragiadakou len = le16_to_cpu(udev->bos->desc->wTotalLength); 5295b9a10481SXenia Ragiadakou if (len != le16_to_cpu(old_bos->desc->wTotalLength)) 5296e3376d6cSXenia Ragiadakou return 1; 5297b9a10481SXenia Ragiadakou if (memcmp(udev->bos->desc, old_bos->desc, len)) 5298e3376d6cSXenia Ragiadakou return 1; 5299e3376d6cSXenia Ragiadakou } 5300e3376d6cSXenia Ragiadakou 5301eb764c4bSAlan Stern /* Since the idVendor, idProduct, and bcdDevice values in the 5302eb764c4bSAlan Stern * device descriptor haven't changed, we will assume the 5303eb764c4bSAlan Stern * Manufacturer and Product strings haven't changed either. 5304eb764c4bSAlan Stern * But the SerialNumber string could be different (e.g., a 5305eb764c4bSAlan Stern * different flash card of the same brand). 5306eb764c4bSAlan Stern */ 5307eb764c4bSAlan Stern if (udev->serial) 5308eb764c4bSAlan Stern serial_len = strlen(udev->serial) + 1; 5309eb764c4bSAlan Stern 5310eb764c4bSAlan Stern len = serial_len; 53111da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5312eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5313eb764c4bSAlan Stern len = max(len, old_length); 53141da177e4SLinus Torvalds } 5315eb764c4bSAlan Stern 53160cc1a51fSOliver Neukum buf = kmalloc(len, GFP_NOIO); 5317b74e7062SWolfram Sang if (!buf) 53181da177e4SLinus Torvalds /* assume the worst */ 53191da177e4SLinus Torvalds return 1; 5320b74e7062SWolfram Sang 53211da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5322eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 53231da177e4SLinus Torvalds length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 53241da177e4SLinus Torvalds old_length); 5325eb764c4bSAlan Stern if (length != old_length) { 53261da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d, error %d\n", 53271da177e4SLinus Torvalds index, length); 5328eb764c4bSAlan Stern changed = 1; 53291da177e4SLinus Torvalds break; 53301da177e4SLinus Torvalds } 53311da177e4SLinus Torvalds if (memcmp(buf, udev->rawdescriptors[index], old_length) 53321da177e4SLinus Torvalds != 0) { 53331da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 5334eb764c4bSAlan Stern index, 5335eb764c4bSAlan Stern ((struct usb_config_descriptor *) buf)-> 5336eb764c4bSAlan Stern bConfigurationValue); 5337eb764c4bSAlan Stern changed = 1; 53381da177e4SLinus Torvalds break; 53391da177e4SLinus Torvalds } 53401da177e4SLinus Torvalds } 5341eb764c4bSAlan Stern 5342eb764c4bSAlan Stern if (!changed && serial_len) { 5343eb764c4bSAlan Stern length = usb_string(udev, udev->descriptor.iSerialNumber, 5344eb764c4bSAlan Stern buf, serial_len); 5345eb764c4bSAlan Stern if (length + 1 != serial_len) { 5346eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string error %d\n", 5347eb764c4bSAlan Stern length); 5348eb764c4bSAlan Stern changed = 1; 5349eb764c4bSAlan Stern } else if (memcmp(buf, udev->serial, length) != 0) { 5350eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string changed\n"); 5351eb764c4bSAlan Stern changed = 1; 5352eb764c4bSAlan Stern } 5353eb764c4bSAlan Stern } 5354eb764c4bSAlan Stern 53551da177e4SLinus Torvalds kfree(buf); 5356eb764c4bSAlan Stern return changed; 53571da177e4SLinus Torvalds } 53581da177e4SLinus Torvalds 53591da177e4SLinus Torvalds /** 5360742120c6SMing Lei * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 53611da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 53621da177e4SLinus Torvalds * 536379efa097SAlan Stern * WARNING - don't use this routine to reset a composite device 536479efa097SAlan Stern * (one with multiple interfaces owned by separate drivers)! 5365742120c6SMing Lei * Use usb_reset_device() instead. 53661da177e4SLinus Torvalds * 53671da177e4SLinus Torvalds * Do a port reset, reassign the device's address, and establish its 53681da177e4SLinus Torvalds * former operating configuration. If the reset fails, or the device's 53691da177e4SLinus Torvalds * descriptors change from their values before the reset, or the original 53701da177e4SLinus Torvalds * configuration and altsettings cannot be restored, a flag will be set 537137ebb549SPetr Mladek * telling hub_wq to pretend the device has been disconnected and then 53721da177e4SLinus Torvalds * re-connected. All drivers will be unbound, and the device will be 53731da177e4SLinus Torvalds * re-enumerated and probed all over again. 53741da177e4SLinus Torvalds * 5375626f090cSYacine Belkadi * Return: 0 if the reset succeeded, -ENODEV if the device has been 53761da177e4SLinus Torvalds * flagged for logical disconnection, or some other negative error code 53771da177e4SLinus Torvalds * if the reset wasn't even attempted. 53781da177e4SLinus Torvalds * 5379626f090cSYacine Belkadi * Note: 53805c79a1e3SDan Williams * The caller must own the device lock and the port lock, the latter is 53815c79a1e3SDan Williams * taken by usb_reset_device(). For example, it's safe to use 53825c79a1e3SDan Williams * usb_reset_device() from a driver probe() routine after downloading 53835c79a1e3SDan Williams * new firmware. For calls that might not occur during probe(), drivers 53845c79a1e3SDan Williams * should lock the device using usb_lock_device_for_reset(). 53856bc6cff5SAlan Stern * 53866bc6cff5SAlan Stern * Locking exception: This routine may also be called from within an 53876bc6cff5SAlan Stern * autoresume handler. Such usage won't conflict with other tasks 53886bc6cff5SAlan Stern * holding the device lock because these tasks should always call 53895c79a1e3SDan Williams * usb_autopm_resume_device(), thereby preventing any unwanted 53905c79a1e3SDan Williams * autoresume. The autoresume handler is expected to have already 53915c79a1e3SDan Williams * acquired the port lock before calling this routine. 53921da177e4SLinus Torvalds */ 5393742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev) 53941da177e4SLinus Torvalds { 53951da177e4SLinus Torvalds struct usb_device *parent_hdev = udev->parent; 53961da177e4SLinus Torvalds struct usb_hub *parent_hub; 53973f0479e0SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 53981da177e4SLinus Torvalds struct usb_device_descriptor descriptor = udev->descriptor; 5399e3376d6cSXenia Ragiadakou struct usb_host_bos *bos; 54007a7b562dSHans de Goede int i, j, ret = 0; 540112c3da34SAlan Stern int port1 = udev->portnum; 54021da177e4SLinus Torvalds 54031da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED || 54041da177e4SLinus Torvalds udev->state == USB_STATE_SUSPENDED) { 54051da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 54061da177e4SLinus Torvalds udev->state); 54071da177e4SLinus Torvalds return -EINVAL; 54081da177e4SLinus Torvalds } 54091da177e4SLinus Torvalds 54105c79a1e3SDan Williams if (!parent_hdev) 54111da177e4SLinus Torvalds return -EISDIR; 54125c79a1e3SDan Williams 5413ad493e5eSLan Tianyu parent_hub = usb_hub_to_struct_hub(parent_hdev); 54141da177e4SLinus Torvalds 5415dcc01c08SSarah Sharp /* Disable USB2 hardware LPM. 5416dcc01c08SSarah Sharp * It will be re-enabled by the enumeration process. 5417dcc01c08SSarah Sharp */ 5418dcc01c08SSarah Sharp if (udev->usb2_hw_lpm_enabled == 1) 5419dcc01c08SSarah Sharp usb_set_usb2_hardware_lpm(udev, 0); 5420dcc01c08SSarah Sharp 5421f74631e3SSarah Sharp /* Disable LPM and LTM while we reset the device and reinstall the alt 5422f74631e3SSarah Sharp * settings. Device-initiated LPM settings, and system exit latency 5423f74631e3SSarah Sharp * settings are cleared when the device is reset, so we have to set 5424f74631e3SSarah Sharp * them up again. 54256d1d0513SSarah Sharp */ 54266d1d0513SSarah Sharp ret = usb_unlocked_disable_lpm(udev); 54276d1d0513SSarah Sharp if (ret) { 54286d1d0513SSarah Sharp dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__); 5429464ad8c4SHans Yang goto re_enumerate_no_bos; 54306d1d0513SSarah Sharp } 5431f74631e3SSarah Sharp ret = usb_disable_ltm(udev); 5432f74631e3SSarah Sharp if (ret) { 5433f74631e3SSarah Sharp dev_err(&udev->dev, "%s Failed to disable LTM\n.", 5434f74631e3SSarah Sharp __func__); 5435464ad8c4SHans Yang goto re_enumerate_no_bos; 5436f74631e3SSarah Sharp } 54376d1d0513SSarah Sharp 5438464ad8c4SHans Yang bos = udev->bos; 5439e5bdfd50SGreg Kroah-Hartman udev->bos = NULL; 5440464ad8c4SHans Yang 54411da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; ++i) { 54421da177e4SLinus Torvalds 54431da177e4SLinus Torvalds /* ep0 maxpacket size may change; let the HCD know about it. 54441da177e4SLinus Torvalds * Other endpoints will be handled by re-enumeration. */ 5445fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 54461da177e4SLinus Torvalds ret = hub_port_init(parent_hub, udev, port1, i); 5447dd4dd19eSAlan Stern if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 54481da177e4SLinus Torvalds break; 54491da177e4SLinus Torvalds } 5450d5cbad4bSAlan Stern 54511da177e4SLinus Torvalds if (ret < 0) 54521da177e4SLinus Torvalds goto re_enumerate; 54531da177e4SLinus Torvalds 54541da177e4SLinus Torvalds /* Device might have changed firmware (DFU or similar) */ 5455e3376d6cSXenia Ragiadakou if (descriptors_changed(udev, &descriptor, bos)) { 54561da177e4SLinus Torvalds dev_info(&udev->dev, "device firmware changed\n"); 54571da177e4SLinus Torvalds udev->descriptor = descriptor; /* for disconnect() calls */ 54581da177e4SLinus Torvalds goto re_enumerate; 54591da177e4SLinus Torvalds } 54601da177e4SLinus Torvalds 54614fe0387aSAlan Stern /* Restore the device's previous configuration */ 54621da177e4SLinus Torvalds if (!udev->actconfig) 54631da177e4SLinus Torvalds goto done; 54643f0479e0SSarah Sharp 5465d673bfcbSSarah Sharp mutex_lock(hcd->bandwidth_mutex); 54663f0479e0SSarah Sharp ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 54673f0479e0SSarah Sharp if (ret < 0) { 54683f0479e0SSarah Sharp dev_warn(&udev->dev, 54693f0479e0SSarah Sharp "Busted HC? Not enough HCD resources for " 54703f0479e0SSarah Sharp "old configuration.\n"); 5471d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 54723f0479e0SSarah Sharp goto re_enumerate; 54733f0479e0SSarah Sharp } 54741da177e4SLinus Torvalds ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 54751da177e4SLinus Torvalds USB_REQ_SET_CONFIGURATION, 0, 54761da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, 0, 54771da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 54781da177e4SLinus Torvalds if (ret < 0) { 54791da177e4SLinus Torvalds dev_err(&udev->dev, 54801da177e4SLinus Torvalds "can't restore configuration #%d (error=%d)\n", 54811da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, ret); 5482d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 54831da177e4SLinus Torvalds goto re_enumerate; 54841da177e4SLinus Torvalds } 5485d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 54861da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_CONFIGURED); 54871da177e4SLinus Torvalds 54884fe0387aSAlan Stern /* Put interfaces back into the same altsettings as before. 54894fe0387aSAlan Stern * Don't bother to send the Set-Interface request for interfaces 54904fe0387aSAlan Stern * that were already in altsetting 0; besides being unnecessary, 54914fe0387aSAlan Stern * many devices can't handle it. Instead just reset the host-side 54924fe0387aSAlan Stern * endpoint state. 54934fe0387aSAlan Stern */ 54941da177e4SLinus Torvalds for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 54953f0479e0SSarah Sharp struct usb_host_config *config = udev->actconfig; 54963f0479e0SSarah Sharp struct usb_interface *intf = config->interface[i]; 54971da177e4SLinus Torvalds struct usb_interface_descriptor *desc; 54981da177e4SLinus Torvalds 54991da177e4SLinus Torvalds desc = &intf->cur_altsetting->desc; 55004fe0387aSAlan Stern if (desc->bAlternateSetting == 0) { 55014fe0387aSAlan Stern usb_disable_interface(udev, intf, true); 55024fe0387aSAlan Stern usb_enable_interface(udev, intf, true); 55034fe0387aSAlan Stern ret = 0; 55044fe0387aSAlan Stern } else { 550504a723eaSSarah Sharp /* Let the bandwidth allocation function know that this 550604a723eaSSarah Sharp * device has been reset, and it will have to use 550704a723eaSSarah Sharp * alternate setting 0 as the current alternate setting. 55083f0479e0SSarah Sharp */ 550904a723eaSSarah Sharp intf->resetting_device = 1; 55101da177e4SLinus Torvalds ret = usb_set_interface(udev, desc->bInterfaceNumber, 55111da177e4SLinus Torvalds desc->bAlternateSetting); 551204a723eaSSarah Sharp intf->resetting_device = 0; 55134fe0387aSAlan Stern } 55141da177e4SLinus Torvalds if (ret < 0) { 55151da177e4SLinus Torvalds dev_err(&udev->dev, "failed to restore interface %d " 55161da177e4SLinus Torvalds "altsetting %d (error=%d)\n", 55171da177e4SLinus Torvalds desc->bInterfaceNumber, 55181da177e4SLinus Torvalds desc->bAlternateSetting, 55191da177e4SLinus Torvalds ret); 55201da177e4SLinus Torvalds goto re_enumerate; 55211da177e4SLinus Torvalds } 55227a7b562dSHans de Goede /* Resetting also frees any allocated streams */ 55237a7b562dSHans de Goede for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) 55247a7b562dSHans de Goede intf->cur_altsetting->endpoint[j].streams = 0; 55251da177e4SLinus Torvalds } 55261da177e4SLinus Torvalds 55271da177e4SLinus Torvalds done: 5528f74631e3SSarah Sharp /* Now that the alt settings are re-installed, enable LTM and LPM. */ 5529de68bab4SSarah Sharp usb_set_usb2_hardware_lpm(udev, 1); 553078d9a487SAlan Stern usb_unlocked_enable_lpm(udev); 5531f74631e3SSarah Sharp usb_enable_ltm(udev); 5532e3376d6cSXenia Ragiadakou usb_release_bos_descriptor(udev); 5533e3376d6cSXenia Ragiadakou udev->bos = bos; 55341da177e4SLinus Torvalds return 0; 55351da177e4SLinus Torvalds 55361da177e4SLinus Torvalds re_enumerate: 5537e3376d6cSXenia Ragiadakou usb_release_bos_descriptor(udev); 5538e3376d6cSXenia Ragiadakou udev->bos = bos; 5539464ad8c4SHans Yang re_enumerate_no_bos: 5540464ad8c4SHans Yang /* LPM state doesn't matter when we're about to destroy the device. */ 5541464ad8c4SHans Yang hub_port_logical_disconnect(parent_hub, port1); 55421da177e4SLinus Torvalds return -ENODEV; 55431da177e4SLinus Torvalds } 55441da177e4SLinus Torvalds 55451da177e4SLinus Torvalds /** 55461da177e4SLinus Torvalds * usb_reset_device - warn interface drivers and perform a USB port reset 55471da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 55481da177e4SLinus Torvalds * 554979efa097SAlan Stern * Warns all drivers bound to registered interfaces (using their pre_reset 555079efa097SAlan Stern * method), performs the port reset, and then lets the drivers know that 5551742120c6SMing Lei * the reset is over (using their post_reset method). 555279efa097SAlan Stern * 5553626f090cSYacine Belkadi * Return: The same as for usb_reset_and_verify_device(). 555479efa097SAlan Stern * 5555626f090cSYacine Belkadi * Note: 555679efa097SAlan Stern * The caller must own the device lock. For example, it's safe to use 555779efa097SAlan Stern * this from a driver probe() routine after downloading new firmware. 555879efa097SAlan Stern * For calls that might not occur during probe(), drivers should lock 5559742120c6SMing Lei * the device using usb_lock_device_for_reset(). 556079efa097SAlan Stern * 556179efa097SAlan Stern * If an interface is currently being probed or disconnected, we assume 556279efa097SAlan Stern * its driver knows how to handle resets. For all other interfaces, 556379efa097SAlan Stern * if the driver doesn't have pre_reset and post_reset methods then 556479efa097SAlan Stern * we attempt to unbind it and rebind afterward. 556579efa097SAlan Stern */ 5566742120c6SMing Lei int usb_reset_device(struct usb_device *udev) 556779efa097SAlan Stern { 556879efa097SAlan Stern int ret; 5569852c4b43SAlan Stern int i; 55704d769defSMing Lei unsigned int noio_flag; 55715c79a1e3SDan Williams struct usb_port *port_dev; 557279efa097SAlan Stern struct usb_host_config *config = udev->actconfig; 55735c79a1e3SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 557479efa097SAlan Stern 557579efa097SAlan Stern if (udev->state == USB_STATE_NOTATTACHED || 557679efa097SAlan Stern udev->state == USB_STATE_SUSPENDED) { 557779efa097SAlan Stern dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 557879efa097SAlan Stern udev->state); 557979efa097SAlan Stern return -EINVAL; 558079efa097SAlan Stern } 558179efa097SAlan Stern 55825c79a1e3SDan Williams if (!udev->parent) { 55835c79a1e3SDan Williams /* this requires hcd-specific logic; see ohci_restart() */ 55845c79a1e3SDan Williams dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 55855c79a1e3SDan Williams return -EISDIR; 55865c79a1e3SDan Williams } 55875c79a1e3SDan Williams 55885c79a1e3SDan Williams port_dev = hub->ports[udev->portnum - 1]; 55895c79a1e3SDan Williams 55904d769defSMing Lei /* 55914d769defSMing Lei * Don't allocate memory with GFP_KERNEL in current 55924d769defSMing Lei * context to avoid possible deadlock if usb mass 55934d769defSMing Lei * storage interface or usbnet interface(iSCSI case) 55944d769defSMing Lei * is included in current configuration. The easist 55954d769defSMing Lei * approach is to do it for every device reset, 55964d769defSMing Lei * because the device 'memalloc_noio' flag may have 55974d769defSMing Lei * not been set before reseting the usb device. 55984d769defSMing Lei */ 55994d769defSMing Lei noio_flag = memalloc_noio_save(); 56004d769defSMing Lei 5601645daaabSAlan Stern /* Prevent autosuspend during the reset */ 560294fcda1fSAlan Stern usb_autoresume_device(udev); 5603645daaabSAlan Stern 560479efa097SAlan Stern if (config) { 5605852c4b43SAlan Stern for (i = 0; i < config->desc.bNumInterfaces; ++i) { 5606852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 560779efa097SAlan Stern struct usb_driver *drv; 560878d9a487SAlan Stern int unbind = 0; 560979efa097SAlan Stern 5610852c4b43SAlan Stern if (cintf->dev.driver) { 561179efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 561278d9a487SAlan Stern if (drv->pre_reset && drv->post_reset) 561378d9a487SAlan Stern unbind = (drv->pre_reset)(cintf); 561478d9a487SAlan Stern else if (cintf->condition == 561578d9a487SAlan Stern USB_INTERFACE_BOUND) 561678d9a487SAlan Stern unbind = 1; 561778d9a487SAlan Stern if (unbind) 561878d9a487SAlan Stern usb_forced_unbind_intf(cintf); 561979efa097SAlan Stern } 562079efa097SAlan Stern } 562179efa097SAlan Stern } 562279efa097SAlan Stern 56235c79a1e3SDan Williams usb_lock_port(port_dev); 5624742120c6SMing Lei ret = usb_reset_and_verify_device(udev); 56255c79a1e3SDan Williams usb_unlock_port(port_dev); 562679efa097SAlan Stern 562779efa097SAlan Stern if (config) { 5628852c4b43SAlan Stern for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 5629852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 563079efa097SAlan Stern struct usb_driver *drv; 563178d9a487SAlan Stern int rebind = cintf->needs_binding; 563279efa097SAlan Stern 563378d9a487SAlan Stern if (!rebind && cintf->dev.driver) { 563479efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 563579efa097SAlan Stern if (drv->post_reset) 563678d9a487SAlan Stern rebind = (drv->post_reset)(cintf); 563778d9a487SAlan Stern else if (cintf->condition == 563878d9a487SAlan Stern USB_INTERFACE_BOUND) 563978d9a487SAlan Stern rebind = 1; 56406aec044cSAlan Stern if (rebind) 56416aec044cSAlan Stern cintf->needs_binding = 1; 564279efa097SAlan Stern } 564379efa097SAlan Stern } 56446aec044cSAlan Stern usb_unbind_and_rebind_marked_interfaces(udev); 564579efa097SAlan Stern } 564679efa097SAlan Stern 564794fcda1fSAlan Stern usb_autosuspend_device(udev); 56484d769defSMing Lei memalloc_noio_restore(noio_flag); 564979efa097SAlan Stern return ret; 565079efa097SAlan Stern } 5651742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device); 5652dc023dceSInaky Perez-Gonzalez 5653dc023dceSInaky Perez-Gonzalez 5654dc023dceSInaky Perez-Gonzalez /** 5655dc023dceSInaky Perez-Gonzalez * usb_queue_reset_device - Reset a USB device from an atomic context 5656dc023dceSInaky Perez-Gonzalez * @iface: USB interface belonging to the device to reset 5657dc023dceSInaky Perez-Gonzalez * 5658dc023dceSInaky Perez-Gonzalez * This function can be used to reset a USB device from an atomic 5659dc023dceSInaky Perez-Gonzalez * context, where usb_reset_device() won't work (as it blocks). 5660dc023dceSInaky Perez-Gonzalez * 5661dc023dceSInaky Perez-Gonzalez * Doing a reset via this method is functionally equivalent to calling 5662dc023dceSInaky Perez-Gonzalez * usb_reset_device(), except for the fact that it is delayed to a 5663dc023dceSInaky Perez-Gonzalez * workqueue. This means that any drivers bound to other interfaces 5664dc023dceSInaky Perez-Gonzalez * might be unbound, as well as users from usbfs in user space. 5665dc023dceSInaky Perez-Gonzalez * 5666dc023dceSInaky Perez-Gonzalez * Corner cases: 5667dc023dceSInaky Perez-Gonzalez * 5668dc023dceSInaky Perez-Gonzalez * - Scheduling two resets at the same time from two different drivers 5669dc023dceSInaky Perez-Gonzalez * attached to two different interfaces of the same device is 5670dc023dceSInaky Perez-Gonzalez * possible; depending on how the driver attached to each interface 5671dc023dceSInaky Perez-Gonzalez * handles ->pre_reset(), the second reset might happen or not. 5672dc023dceSInaky Perez-Gonzalez * 5673524134d4SAlan Stern * - If the reset is delayed so long that the interface is unbound from 5674524134d4SAlan Stern * its driver, the reset will be skipped. 5675dc023dceSInaky Perez-Gonzalez * 5676524134d4SAlan Stern * - This function can be called during .probe(). It can also be called 5677524134d4SAlan Stern * during .disconnect(), but doing so is pointless because the reset 5678524134d4SAlan Stern * will not occur. If you really want to reset the device during 5679524134d4SAlan Stern * .disconnect(), call usb_reset_device() directly -- but watch out 5680524134d4SAlan Stern * for nested unbinding issues! 5681dc023dceSInaky Perez-Gonzalez */ 5682dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface) 5683dc023dceSInaky Perez-Gonzalez { 5684524134d4SAlan Stern if (schedule_work(&iface->reset_ws)) 5685524134d4SAlan Stern usb_get_intf(iface); 5686dc023dceSInaky Perez-Gonzalez } 5687dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device); 5688ff823c79SLan Tianyu 5689ff823c79SLan Tianyu /** 5690ff823c79SLan Tianyu * usb_hub_find_child - Get the pointer of child device 5691ff823c79SLan Tianyu * attached to the port which is specified by @port1. 5692ff823c79SLan Tianyu * @hdev: USB device belonging to the usb hub 5693ff823c79SLan Tianyu * @port1: port num to indicate which port the child device 5694ff823c79SLan Tianyu * is attached to. 5695ff823c79SLan Tianyu * 5696ff823c79SLan Tianyu * USB drivers call this function to get hub's child device 5697ff823c79SLan Tianyu * pointer. 5698ff823c79SLan Tianyu * 5699626f090cSYacine Belkadi * Return: %NULL if input param is invalid and 5700ff823c79SLan Tianyu * child's usb_device pointer if non-NULL. 5701ff823c79SLan Tianyu */ 5702ff823c79SLan Tianyu struct usb_device *usb_hub_find_child(struct usb_device *hdev, 5703ff823c79SLan Tianyu int port1) 5704ff823c79SLan Tianyu { 5705ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 5706ff823c79SLan Tianyu 5707ff823c79SLan Tianyu if (port1 < 1 || port1 > hdev->maxchild) 5708ff823c79SLan Tianyu return NULL; 5709ff823c79SLan Tianyu return hub->ports[port1 - 1]->child; 5710ff823c79SLan Tianyu } 5711ff823c79SLan Tianyu EXPORT_SYMBOL_GPL(usb_hub_find_child); 5712d5575424SLan Tianyu 5713d2123fd9SLan Tianyu void usb_hub_adjust_deviceremovable(struct usb_device *hdev, 5714d2123fd9SLan Tianyu struct usb_hub_descriptor *desc) 5715d2123fd9SLan Tianyu { 5716d99f6b41SDan Williams struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 5717d2123fd9SLan Tianyu enum usb_port_connect_type connect_type; 5718d2123fd9SLan Tianyu int i; 5719d2123fd9SLan Tianyu 5720d99f6b41SDan Williams if (!hub) 5721d99f6b41SDan Williams return; 5722d99f6b41SDan Williams 5723d2123fd9SLan Tianyu if (!hub_is_superspeed(hdev)) { 5724d2123fd9SLan Tianyu for (i = 1; i <= hdev->maxchild; i++) { 5725d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[i - 1]; 5726d2123fd9SLan Tianyu 5727d99f6b41SDan Williams connect_type = port_dev->connect_type; 5728d2123fd9SLan Tianyu if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 5729d2123fd9SLan Tianyu u8 mask = 1 << (i%8); 5730d2123fd9SLan Tianyu 5731d2123fd9SLan Tianyu if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { 5732d99f6b41SDan Williams dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 5733d2123fd9SLan Tianyu desc->u.hs.DeviceRemovable[i/8] |= mask; 5734d2123fd9SLan Tianyu } 5735d2123fd9SLan Tianyu } 5736d2123fd9SLan Tianyu } 5737d2123fd9SLan Tianyu } else { 5738d2123fd9SLan Tianyu u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); 5739d2123fd9SLan Tianyu 5740d2123fd9SLan Tianyu for (i = 1; i <= hdev->maxchild; i++) { 5741d99f6b41SDan Williams struct usb_port *port_dev = hub->ports[i - 1]; 5742d2123fd9SLan Tianyu 5743d99f6b41SDan Williams connect_type = port_dev->connect_type; 5744d2123fd9SLan Tianyu if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 5745d2123fd9SLan Tianyu u16 mask = 1 << i; 5746d2123fd9SLan Tianyu 5747d2123fd9SLan Tianyu if (!(port_removable & mask)) { 5748d99f6b41SDan Williams dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 5749d2123fd9SLan Tianyu port_removable |= mask; 5750d2123fd9SLan Tianyu } 5751d2123fd9SLan Tianyu } 5752d2123fd9SLan Tianyu } 5753d2123fd9SLan Tianyu 5754d2123fd9SLan Tianyu desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); 5755d2123fd9SLan Tianyu } 5756d2123fd9SLan Tianyu } 5757d2123fd9SLan Tianyu 5758d5575424SLan Tianyu #ifdef CONFIG_ACPI 5759d5575424SLan Tianyu /** 5760d5575424SLan Tianyu * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle 5761d5575424SLan Tianyu * @hdev: USB device belonging to the usb hub 5762d5575424SLan Tianyu * @port1: port num of the port 5763d5575424SLan Tianyu * 5764626f090cSYacine Belkadi * Return: Port's acpi handle if successful, %NULL if params are 5765626f090cSYacine Belkadi * invalid. 5766d5575424SLan Tianyu */ 5767d5575424SLan Tianyu acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, 5768d5575424SLan Tianyu int port1) 5769d5575424SLan Tianyu { 5770ad493e5eSLan Tianyu struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 5771d5575424SLan Tianyu 577241341261SMathias Nyman if (!hub) 577341341261SMathias Nyman return NULL; 577441341261SMathias Nyman 57753a83f992SRafael J. Wysocki return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); 5776d5575424SLan Tianyu } 5777d5575424SLan Tianyu #endif 5778