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 * 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/moduleparam.h> 151da177e4SLinus Torvalds #include <linux/completion.h> 161da177e4SLinus Torvalds #include <linux/sched.h> 171da177e4SLinus Torvalds #include <linux/list.h> 181da177e4SLinus Torvalds #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/ioctl.h> 201da177e4SLinus Torvalds #include <linux/usb.h> 211da177e4SLinus Torvalds #include <linux/usbdevice_fs.h> 2227729aadSEric Lescouet #include <linux/usb/hcd.h> 23925aa46bSRichard Zhao #include <linux/usb/otg.h> 2493362a87SPhil Dibowitz #include <linux/usb/quirks.h> 259c8d6178Sakpm@osdl.org #include <linux/kthread.h> 264186ecf8SArjan van de Ven #include <linux/mutex.h> 277dfb7103SNigel Cunningham #include <linux/freezer.h> 28b04b3156STheodore Ts'o #include <linux/random.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <asm/uaccess.h> 311da177e4SLinus Torvalds #include <asm/byteorder.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include "usb.h" 341da177e4SLinus Torvalds 35f2a383e4SGreg Kroah-Hartman /* if we are in debug mode, always announce new devices */ 36f2a383e4SGreg Kroah-Hartman #ifdef DEBUG 37f2a383e4SGreg Kroah-Hartman #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES 38f2a383e4SGreg Kroah-Hartman #define CONFIG_USB_ANNOUNCE_NEW_DEVICES 39f2a383e4SGreg Kroah-Hartman #endif 40f2a383e4SGreg Kroah-Hartman #endif 41f2a383e4SGreg Kroah-Hartman 42e6f30deaSMing Lei #define USB_VENDOR_GENESYS_LOGIC 0x05e3 43e6f30deaSMing Lei #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01 44e6f30deaSMing Lei 45fa2a9566SLan Tianyu struct usb_port { 46ff823c79SLan Tianyu struct usb_device *child; 47fa2a9566SLan Tianyu struct device dev; 48fa2a9566SLan Tianyu struct dev_state *port_owner; 4905f91689SLan Tianyu enum usb_port_connect_type connect_type; 50fa2a9566SLan Tianyu }; 51fa2a9566SLan Tianyu 521bb5f66bSAlan Stern struct usb_hub { 531bb5f66bSAlan Stern struct device *intfdev; /* the "interface" device */ 541bb5f66bSAlan Stern struct usb_device *hdev; 55e8054854SAlan Stern struct kref kref; 561bb5f66bSAlan Stern struct urb *urb; /* for interrupt polling pipe */ 571bb5f66bSAlan Stern 581bb5f66bSAlan Stern /* buffer for urb ... with extra space in case of babble */ 591bb5f66bSAlan Stern char (*buffer)[8]; 601bb5f66bSAlan Stern union { 611bb5f66bSAlan Stern struct usb_hub_status hub; 621bb5f66bSAlan Stern struct usb_port_status port; 631bb5f66bSAlan Stern } *status; /* buffer for status reports */ 64db90e7a1SAlan Stern struct mutex status_mutex; /* for the status buffer */ 651bb5f66bSAlan Stern 661bb5f66bSAlan Stern int error; /* last reported error */ 671bb5f66bSAlan Stern int nerrors; /* track consecutive errors */ 681bb5f66bSAlan Stern 691bb5f66bSAlan Stern struct list_head event_list; /* hubs w/data or errs ready */ 701bb5f66bSAlan Stern unsigned long event_bits[1]; /* status change bitmask */ 711bb5f66bSAlan Stern unsigned long change_bits[1]; /* ports with logical connect 721bb5f66bSAlan Stern status change */ 731bb5f66bSAlan Stern unsigned long busy_bits[1]; /* ports being reset or 741bb5f66bSAlan Stern resumed */ 75253e0572SAlan Stern unsigned long removed_bits[1]; /* ports with a "removed" 76253e0572SAlan Stern device present */ 774ee823b8SSarah Sharp unsigned long wakeup_bits[1]; /* ports that have signaled 784ee823b8SSarah Sharp remote wakeup */ 791bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ 801bb5f66bSAlan Stern #error event_bits[] is too short! 811bb5f66bSAlan Stern #endif 821bb5f66bSAlan Stern 831bb5f66bSAlan Stern struct usb_hub_descriptor *descriptor; /* class descriptor */ 841bb5f66bSAlan Stern struct usb_tt tt; /* Transaction Translator */ 851bb5f66bSAlan Stern 861bb5f66bSAlan Stern unsigned mA_per_port; /* current for each child */ 871bb5f66bSAlan Stern 881bb5f66bSAlan Stern unsigned limited_power:1; 891bb5f66bSAlan Stern unsigned quiescing:1; 90e8054854SAlan Stern unsigned disconnected:1; 911bb5f66bSAlan Stern 92e6f30deaSMing Lei unsigned quirk_check_port_auto_suspend:1; 93e6f30deaSMing Lei 941bb5f66bSAlan Stern unsigned has_indicators:1; 951bb5f66bSAlan Stern u8 indicator[USB_MAXCHILDREN]; 966d5aefb8SDavid Howells struct delayed_work leds; 978520f380SAlan Stern struct delayed_work init_work; 98fa2a9566SLan Tianyu struct usb_port **ports; 991bb5f66bSAlan Stern }; 1001bb5f66bSAlan Stern 101dbe79bbeSJohn Youn static inline int hub_is_superspeed(struct usb_device *hdev) 102dbe79bbeSJohn Youn { 1037bf01185SAman Deep return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS); 104dbe79bbeSJohn Youn } 1051bb5f66bSAlan Stern 106fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members 1079ad3d6ccSAlan Stern * Note: Both are also protected by ->dev.sem, except that ->state can 1081da177e4SLinus Torvalds * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 1091da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* khubd's worklist and its lock */ 1121da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock); 1131da177e4SLinus Torvalds static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds /* Wakes up khubd */ 1161da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); 1171da177e4SLinus Torvalds 1189c8d6178Sakpm@osdl.org static struct task_struct *khubd_task; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */ 12190ab5ee9SRusty Russell static bool blinkenlights = 0; 1221da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO); 1231da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 126fd7c519dSJaroslav Kysela * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 127fd7c519dSJaroslav Kysela * 10 seconds to send reply for the initial 64-byte descriptor request. 128fd7c519dSJaroslav Kysela */ 129fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */ 130fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 131fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 132b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout, 133b9cef6c3SWu Fengguang "initial 64-byte descriptor request timeout in milliseconds " 134b9cef6c3SWu Fengguang "(default 5000 - 5.0 seconds)"); 135fd7c519dSJaroslav Kysela 136fd7c519dSJaroslav Kysela /* 1371da177e4SLinus Torvalds * As of 2.6.10 we introduce a new USB device initialization scheme which 1381da177e4SLinus Torvalds * closely resembles the way Windows works. Hopefully it will be compatible 1391da177e4SLinus Torvalds * with a wider range of devices than the old scheme. However some previously 1401da177e4SLinus Torvalds * working devices may start giving rise to "device not accepting address" 1411da177e4SLinus Torvalds * errors; if that happens the user can try the old scheme by adjusting the 1421da177e4SLinus Torvalds * following module parameters. 1431da177e4SLinus Torvalds * 1441da177e4SLinus Torvalds * For maximum flexibility there are two boolean parameters to control the 1451da177e4SLinus Torvalds * hub driver's behavior. On the first initialization attempt, if the 1461da177e4SLinus Torvalds * "old_scheme_first" parameter is set then the old scheme will be used, 1471da177e4SLinus Torvalds * otherwise the new scheme is used. If that fails and "use_both_schemes" 1481da177e4SLinus Torvalds * is set, then the driver will make another attempt, using the other scheme. 1491da177e4SLinus Torvalds */ 15090ab5ee9SRusty Russell static bool old_scheme_first = 0; 1511da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 1521da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first, 1531da177e4SLinus Torvalds "start with the old device initialization scheme"); 1541da177e4SLinus Torvalds 15590ab5ee9SRusty Russell static bool use_both_schemes = 1; 1561da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 1571da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes, 1581da177e4SLinus Torvalds "try the other device initialization scheme if the " 1591da177e4SLinus Torvalds "first one fails"); 1601da177e4SLinus Torvalds 16132fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization. This interferes with 16232fe0198SAlan Stern * port reset on some companion controllers. 16332fe0198SAlan Stern */ 16432fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 16532fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 16632fe0198SAlan Stern 167948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT 1500 168948fea37SAlan Stern #define HUB_DEBOUNCE_STEP 25 169948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE 100 170948fea37SAlan Stern 171fa2a9566SLan Tianyu #define to_usb_port(_dev) \ 172fa2a9566SLan Tianyu container_of(_dev, struct usb_port, dev) 1731da177e4SLinus Torvalds 174742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev); 175742120c6SMing Lei 176131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus) 1771da177e4SLinus Torvalds { 178131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev)) 179131dec34SSarah Sharp return "5.0 Gb/s"; 180288ead45SAlan Stern if (portstatus & USB_PORT_STAT_HIGH_SPEED) 1811da177e4SLinus Torvalds return "480 Mb/s"; 182288ead45SAlan Stern else if (portstatus & USB_PORT_STAT_LOW_SPEED) 1831da177e4SLinus Torvalds return "1.5 Mb/s"; 1841da177e4SLinus Torvalds else 1851da177e4SLinus Torvalds return "12 Mb/s"; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */ 18925118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev) 1901da177e4SLinus Torvalds { 191ff823c79SLan Tianyu if (!hdev || !hdev->actconfig || !hdev->maxchild) 19225118084SAlan Stern return NULL; 1931da177e4SLinus Torvalds return usb_get_intfdata(hdev->actconfig->interface[0]); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 196d9b2099cSSarah Sharp static int usb_device_supports_lpm(struct usb_device *udev) 197d9b2099cSSarah Sharp { 198d9b2099cSSarah Sharp /* USB 2.1 (and greater) devices indicate LPM support through 199d9b2099cSSarah Sharp * their USB 2.0 Extended Capabilities BOS descriptor. 200d9b2099cSSarah Sharp */ 201d9b2099cSSarah Sharp if (udev->speed == USB_SPEED_HIGH) { 202d9b2099cSSarah Sharp if (udev->bos->ext_cap && 203d9b2099cSSarah Sharp (USB_LPM_SUPPORT & 204d9b2099cSSarah Sharp le32_to_cpu(udev->bos->ext_cap->bmAttributes))) 205d9b2099cSSarah Sharp return 1; 206d9b2099cSSarah Sharp return 0; 207d9b2099cSSarah Sharp } 20851e0a012SSarah Sharp 20951e0a012SSarah Sharp /* All USB 3.0 must support LPM, but we need their max exit latency 21051e0a012SSarah Sharp * information from the SuperSpeed Extended Capabilities BOS descriptor. 21151e0a012SSarah Sharp */ 21251e0a012SSarah Sharp if (!udev->bos->ss_cap) { 21351e0a012SSarah Sharp dev_warn(&udev->dev, "No LPM exit latency info found. " 21451e0a012SSarah Sharp "Power management will be impacted.\n"); 215d9b2099cSSarah Sharp return 0; 216d9b2099cSSarah Sharp } 21751e0a012SSarah Sharp if (udev->parent->lpm_capable) 21851e0a012SSarah Sharp return 1; 21951e0a012SSarah Sharp 22051e0a012SSarah Sharp dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. " 22151e0a012SSarah Sharp "Power management will be impacted.\n"); 22251e0a012SSarah Sharp return 0; 22351e0a012SSarah Sharp } 22451e0a012SSarah Sharp 22551e0a012SSarah Sharp /* 22651e0a012SSarah Sharp * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from 22751e0a012SSarah Sharp * either U1 or U2. 22851e0a012SSarah Sharp */ 22951e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev, 23051e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params, 23151e0a012SSarah Sharp unsigned int udev_exit_latency, 23251e0a012SSarah Sharp struct usb_hub *hub, 23351e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params, 23451e0a012SSarah Sharp unsigned int hub_exit_latency) 23551e0a012SSarah Sharp { 23651e0a012SSarah Sharp unsigned int total_mel; 23751e0a012SSarah Sharp unsigned int device_mel; 23851e0a012SSarah Sharp unsigned int hub_mel; 23951e0a012SSarah Sharp 24051e0a012SSarah Sharp /* 24151e0a012SSarah Sharp * Calculate the time it takes to transition all links from the roothub 24251e0a012SSarah Sharp * to the parent hub into U0. The parent hub must then decode the 24351e0a012SSarah Sharp * packet (hub header decode latency) to figure out which port it was 24451e0a012SSarah Sharp * bound for. 24551e0a012SSarah Sharp * 24651e0a012SSarah Sharp * The Hub Header decode latency is expressed in 0.1us intervals (0x1 24751e0a012SSarah Sharp * means 0.1us). Multiply that by 100 to get nanoseconds. 24851e0a012SSarah Sharp */ 24951e0a012SSarah Sharp total_mel = hub_lpm_params->mel + 25051e0a012SSarah Sharp (hub->descriptor->u.ss.bHubHdrDecLat * 100); 25151e0a012SSarah Sharp 25251e0a012SSarah Sharp /* 25351e0a012SSarah Sharp * How long will it take to transition the downstream hub's port into 25451e0a012SSarah Sharp * U0? The greater of either the hub exit latency or the device exit 25551e0a012SSarah Sharp * latency. 25651e0a012SSarah Sharp * 25751e0a012SSarah Sharp * The BOS U1/U2 exit latencies are expressed in 1us intervals. 25851e0a012SSarah Sharp * Multiply that by 1000 to get nanoseconds. 25951e0a012SSarah Sharp */ 26051e0a012SSarah Sharp device_mel = udev_exit_latency * 1000; 26151e0a012SSarah Sharp hub_mel = hub_exit_latency * 1000; 26251e0a012SSarah Sharp if (device_mel > hub_mel) 26351e0a012SSarah Sharp total_mel += device_mel; 26451e0a012SSarah Sharp else 26551e0a012SSarah Sharp total_mel += hub_mel; 26651e0a012SSarah Sharp 26751e0a012SSarah Sharp udev_lpm_params->mel = total_mel; 26851e0a012SSarah Sharp } 26951e0a012SSarah Sharp 27051e0a012SSarah Sharp /* 27151e0a012SSarah Sharp * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate 27251e0a012SSarah Sharp * a transition from either U1 or U2. 27351e0a012SSarah Sharp */ 27451e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev, 27551e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params, 27651e0a012SSarah Sharp unsigned int udev_exit_latency, 27751e0a012SSarah Sharp struct usb_hub *hub, 27851e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params, 27951e0a012SSarah Sharp unsigned int hub_exit_latency, 28051e0a012SSarah Sharp unsigned int port_to_port_exit_latency) 28151e0a012SSarah Sharp { 28251e0a012SSarah Sharp unsigned int first_link_pel; 28351e0a012SSarah Sharp unsigned int hub_pel; 28451e0a012SSarah Sharp 28551e0a012SSarah Sharp /* 28651e0a012SSarah Sharp * First, the device sends an LFPS to transition the link between the 28751e0a012SSarah Sharp * device and the parent hub into U0. The exit latency is the bigger of 28851e0a012SSarah Sharp * the device exit latency or the hub exit latency. 28951e0a012SSarah Sharp */ 29051e0a012SSarah Sharp if (udev_exit_latency > hub_exit_latency) 29151e0a012SSarah Sharp first_link_pel = udev_exit_latency * 1000; 29251e0a012SSarah Sharp else 29351e0a012SSarah Sharp first_link_pel = hub_exit_latency * 1000; 29451e0a012SSarah Sharp 29551e0a012SSarah Sharp /* 29651e0a012SSarah Sharp * When the hub starts to receive the LFPS, there is a slight delay for 29751e0a012SSarah Sharp * it to figure out that one of the ports is sending an LFPS. Then it 29851e0a012SSarah Sharp * will forward the LFPS to its upstream link. The exit latency is the 29951e0a012SSarah Sharp * delay, plus the PEL that we calculated for this hub. 30051e0a012SSarah Sharp */ 30151e0a012SSarah Sharp hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; 30251e0a012SSarah Sharp 30351e0a012SSarah Sharp /* 30451e0a012SSarah Sharp * According to figure C-7 in the USB 3.0 spec, the PEL for this device 30551e0a012SSarah Sharp * is the greater of the two exit latencies. 30651e0a012SSarah Sharp */ 30751e0a012SSarah Sharp if (first_link_pel > hub_pel) 30851e0a012SSarah Sharp udev_lpm_params->pel = first_link_pel; 30951e0a012SSarah Sharp else 31051e0a012SSarah Sharp udev_lpm_params->pel = hub_pel; 31151e0a012SSarah Sharp } 31251e0a012SSarah Sharp 31351e0a012SSarah Sharp /* 31451e0a012SSarah Sharp * Set the System Exit Latency (SEL) to indicate the total worst-case time from 31551e0a012SSarah Sharp * when a device initiates a transition to U0, until when it will receive the 31651e0a012SSarah Sharp * first packet from the host controller. 31751e0a012SSarah Sharp * 31851e0a012SSarah Sharp * Section C.1.5.1 describes the four components to this: 31951e0a012SSarah Sharp * - t1: device PEL 32051e0a012SSarah Sharp * - t2: time for the ERDY to make it from the device to the host. 32151e0a012SSarah Sharp * - t3: a host-specific delay to process the ERDY. 32251e0a012SSarah Sharp * - t4: time for the packet to make it from the host to the device. 32351e0a012SSarah Sharp * 32451e0a012SSarah Sharp * t3 is specific to both the xHCI host and the platform the host is integrated 32551e0a012SSarah Sharp * into. The Intel HW folks have said it's negligible, FIXME if a different 32651e0a012SSarah Sharp * vendor says otherwise. 32751e0a012SSarah Sharp */ 32851e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev, 32951e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params) 33051e0a012SSarah Sharp { 33151e0a012SSarah Sharp struct usb_device *parent; 33251e0a012SSarah Sharp unsigned int num_hubs; 33351e0a012SSarah Sharp unsigned int total_sel; 33451e0a012SSarah Sharp 33551e0a012SSarah Sharp /* t1 = device PEL */ 33651e0a012SSarah Sharp total_sel = udev_lpm_params->pel; 33751e0a012SSarah Sharp /* How many external hubs are in between the device & the root port. */ 33851e0a012SSarah Sharp for (parent = udev->parent, num_hubs = 0; parent->parent; 33951e0a012SSarah Sharp parent = parent->parent) 34051e0a012SSarah Sharp num_hubs++; 34151e0a012SSarah Sharp /* t2 = 2.1us + 250ns * (num_hubs - 1) */ 34251e0a012SSarah Sharp if (num_hubs > 0) 34351e0a012SSarah Sharp total_sel += 2100 + 250 * (num_hubs - 1); 34451e0a012SSarah Sharp 34551e0a012SSarah Sharp /* t4 = 250ns * num_hubs */ 34651e0a012SSarah Sharp total_sel += 250 * num_hubs; 34751e0a012SSarah Sharp 34851e0a012SSarah Sharp udev_lpm_params->sel = total_sel; 34951e0a012SSarah Sharp } 35051e0a012SSarah Sharp 35151e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev) 35251e0a012SSarah Sharp { 35351e0a012SSarah Sharp struct usb_hub *hub; 35451e0a012SSarah Sharp unsigned int port_to_port_delay; 35551e0a012SSarah Sharp unsigned int udev_u1_del; 35651e0a012SSarah Sharp unsigned int udev_u2_del; 35751e0a012SSarah Sharp unsigned int hub_u1_del; 35851e0a012SSarah Sharp unsigned int hub_u2_del; 35951e0a012SSarah Sharp 36051e0a012SSarah Sharp if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER) 36151e0a012SSarah Sharp return; 36251e0a012SSarah Sharp 36351e0a012SSarah Sharp hub = hdev_to_hub(udev->parent); 36451e0a012SSarah Sharp /* It doesn't take time to transition the roothub into U0, since it 36551e0a012SSarah Sharp * doesn't have an upstream link. 36651e0a012SSarah Sharp */ 36751e0a012SSarah Sharp if (!hub) 36851e0a012SSarah Sharp return; 36951e0a012SSarah Sharp 37051e0a012SSarah Sharp udev_u1_del = udev->bos->ss_cap->bU1devExitLat; 37151e0a012SSarah Sharp udev_u2_del = udev->bos->ss_cap->bU2DevExitLat; 37251e0a012SSarah Sharp hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; 37351e0a012SSarah Sharp hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat; 37451e0a012SSarah Sharp 37551e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, 37651e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del); 37751e0a012SSarah Sharp 37851e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, 37951e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del); 38051e0a012SSarah Sharp 38151e0a012SSarah Sharp /* 38251e0a012SSarah Sharp * Appendix C, section C.2.2.2, says that there is a slight delay from 38351e0a012SSarah Sharp * when the parent hub notices the downstream port is trying to 38451e0a012SSarah Sharp * transition to U0 to when the hub initiates a U0 transition on its 38551e0a012SSarah Sharp * upstream port. The section says the delays are tPort2PortU1EL and 38651e0a012SSarah Sharp * tPort2PortU2EL, but it doesn't define what they are. 38751e0a012SSarah Sharp * 38851e0a012SSarah Sharp * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking 38951e0a012SSarah Sharp * about the same delays. Use the maximum delay calculations from those 39051e0a012SSarah Sharp * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For 39151e0a012SSarah Sharp * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I 39251e0a012SSarah Sharp * assume the device exit latencies they are talking about are the hub 39351e0a012SSarah Sharp * exit latencies. 39451e0a012SSarah Sharp * 39551e0a012SSarah Sharp * What do we do if the U2 exit latency is less than the U1 exit 39651e0a012SSarah Sharp * latency? It's possible, although not likely... 39751e0a012SSarah Sharp */ 39851e0a012SSarah Sharp port_to_port_delay = 1; 39951e0a012SSarah Sharp 40051e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, 40151e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del, 40251e0a012SSarah Sharp port_to_port_delay); 40351e0a012SSarah Sharp 40451e0a012SSarah Sharp if (hub_u2_del > hub_u1_del) 40551e0a012SSarah Sharp port_to_port_delay = 1 + hub_u2_del - hub_u1_del; 40651e0a012SSarah Sharp else 40751e0a012SSarah Sharp port_to_port_delay = 1 + hub_u1_del; 40851e0a012SSarah Sharp 40951e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, 41051e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del, 41151e0a012SSarah Sharp port_to_port_delay); 41251e0a012SSarah Sharp 41351e0a012SSarah Sharp /* Now that we've got PEL, calculate SEL. */ 41451e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u1_params); 41551e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u2_params); 41651e0a012SSarah Sharp } 417d9b2099cSSarah Sharp 4181da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */ 419dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data) 4201da177e4SLinus Torvalds { 421dbe79bbeSJohn Youn int i, ret, size; 422dbe79bbeSJohn Youn unsigned dtype; 423dbe79bbeSJohn Youn 424dbe79bbeSJohn Youn if (hub_is_superspeed(hdev)) { 425dbe79bbeSJohn Youn dtype = USB_DT_SS_HUB; 426dbe79bbeSJohn Youn size = USB_DT_SS_HUB_SIZE; 427dbe79bbeSJohn Youn } else { 428dbe79bbeSJohn Youn dtype = USB_DT_HUB; 429dbe79bbeSJohn Youn size = sizeof(struct usb_hub_descriptor); 430dbe79bbeSJohn Youn } 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds for (i = 0; i < 3; i++) { 4331da177e4SLinus Torvalds ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 4341da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 435dbe79bbeSJohn Youn dtype << 8, 0, data, size, 4361da177e4SLinus Torvalds USB_CTRL_GET_TIMEOUT); 4371da177e4SLinus Torvalds if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) 4381da177e4SLinus Torvalds return ret; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds return -EINVAL; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* 4441da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.1 4451da177e4SLinus Torvalds */ 4461da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature) 4471da177e4SLinus Torvalds { 4481da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4491da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds /* 4531da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.2 4541da177e4SLinus Torvalds */ 4551da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4581da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 4591da177e4SLinus Torvalds NULL, 0, 1000); 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* 4631da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.13 4641da177e4SLinus Torvalds */ 4651da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4681da177e4SLinus Torvalds USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 4691da177e4SLinus Torvalds NULL, 0, 1000); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /* 4731da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 4741da177e4SLinus Torvalds * for info about using port indicators 4751da177e4SLinus Torvalds */ 4761da177e4SLinus Torvalds static void set_port_led( 4771da177e4SLinus Torvalds struct usb_hub *hub, 4781da177e4SLinus Torvalds int port1, 4791da177e4SLinus Torvalds int selector 4801da177e4SLinus Torvalds ) 4811da177e4SLinus Torvalds { 4821da177e4SLinus Torvalds int status = set_port_feature(hub->hdev, (selector << 8) | port1, 4831da177e4SLinus Torvalds USB_PORT_FEAT_INDICATOR); 4841da177e4SLinus Torvalds if (status < 0) 4851da177e4SLinus Torvalds dev_dbg (hub->intfdev, 4861da177e4SLinus Torvalds "port %d indicator %s status %d\n", 4871da177e4SLinus Torvalds port1, 4881da177e4SLinus Torvalds ({ char *s; switch (selector) { 4891da177e4SLinus Torvalds case HUB_LED_AMBER: s = "amber"; break; 4901da177e4SLinus Torvalds case HUB_LED_GREEN: s = "green"; break; 4911da177e4SLinus Torvalds case HUB_LED_OFF: s = "off"; break; 4921da177e4SLinus Torvalds case HUB_LED_AUTO: s = "auto"; break; 4931da177e4SLinus Torvalds default: s = "??"; break; 4941da177e4SLinus Torvalds }; s; }), 4951da177e4SLinus Torvalds status); 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds #define LED_CYCLE_PERIOD ((2*HZ)/3) 4991da177e4SLinus Torvalds 500c4028958SDavid Howells static void led_work (struct work_struct *work) 5011da177e4SLinus Torvalds { 502c4028958SDavid Howells struct usb_hub *hub = 503c4028958SDavid Howells container_of(work, struct usb_hub, leds.work); 5041da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 5051da177e4SLinus Torvalds unsigned i; 5061da177e4SLinus Torvalds unsigned changed = 0; 5071da177e4SLinus Torvalds int cursor = -1; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 5101da177e4SLinus Torvalds return; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds for (i = 0; i < hub->descriptor->bNbrPorts; i++) { 5131da177e4SLinus Torvalds unsigned selector, mode; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* 30%-50% duty cycle */ 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds switch (hub->indicator[i]) { 5181da177e4SLinus Torvalds /* cycle marker */ 5191da177e4SLinus Torvalds case INDICATOR_CYCLE: 5201da177e4SLinus Torvalds cursor = i; 5211da177e4SLinus Torvalds selector = HUB_LED_AUTO; 5221da177e4SLinus Torvalds mode = INDICATOR_AUTO; 5231da177e4SLinus Torvalds break; 5241da177e4SLinus Torvalds /* blinking green = sw attention */ 5251da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK: 5261da177e4SLinus Torvalds selector = HUB_LED_GREEN; 5271da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK_OFF; 5281da177e4SLinus Torvalds break; 5291da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK_OFF: 5301da177e4SLinus Torvalds selector = HUB_LED_OFF; 5311da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK; 5321da177e4SLinus Torvalds break; 5331da177e4SLinus Torvalds /* blinking amber = hw attention */ 5341da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK: 5351da177e4SLinus Torvalds selector = HUB_LED_AMBER; 5361da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK_OFF; 5371da177e4SLinus Torvalds break; 5381da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK_OFF: 5391da177e4SLinus Torvalds selector = HUB_LED_OFF; 5401da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK; 5411da177e4SLinus Torvalds break; 5421da177e4SLinus Torvalds /* blink green/amber = reserved */ 5431da177e4SLinus Torvalds case INDICATOR_ALT_BLINK: 5441da177e4SLinus Torvalds selector = HUB_LED_GREEN; 5451da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK_OFF; 5461da177e4SLinus Torvalds break; 5471da177e4SLinus Torvalds case INDICATOR_ALT_BLINK_OFF: 5481da177e4SLinus Torvalds selector = HUB_LED_AMBER; 5491da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK; 5501da177e4SLinus Torvalds break; 5511da177e4SLinus Torvalds default: 5521da177e4SLinus Torvalds continue; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds if (selector != HUB_LED_AUTO) 5551da177e4SLinus Torvalds changed = 1; 5561da177e4SLinus Torvalds set_port_led(hub, i + 1, selector); 5571da177e4SLinus Torvalds hub->indicator[i] = mode; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds if (!changed && blinkenlights) { 5601da177e4SLinus Torvalds cursor++; 5611da177e4SLinus Torvalds cursor %= hub->descriptor->bNbrPorts; 5621da177e4SLinus Torvalds set_port_led(hub, cursor + 1, HUB_LED_GREEN); 5631da177e4SLinus Torvalds hub->indicator[cursor] = INDICATOR_CYCLE; 5641da177e4SLinus Torvalds changed++; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds if (changed) 5671da177e4SLinus Torvalds schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */ 5711da177e4SLinus Torvalds #define USB_STS_TIMEOUT 1000 5721da177e4SLinus Torvalds #define USB_STS_RETRIES 5 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds /* 5751da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.6 5761da177e4SLinus Torvalds */ 5771da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev, 5781da177e4SLinus Torvalds struct usb_hub_status *data) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 5811da177e4SLinus Torvalds 5823824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES && 5833824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) { 5841da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 5851da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 5861da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds return status; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* 5921da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7 5931da177e4SLinus Torvalds */ 5941da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1, 5951da177e4SLinus Torvalds struct usb_port_status *data) 5961da177e4SLinus Torvalds { 5971da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 5981da177e4SLinus Torvalds 5993824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES && 6003824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) { 6011da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 6021da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, 6031da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds return status; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6083eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1, 6093eb14915SAlan Stern u16 *status, u16 *change) 6103eb14915SAlan Stern { 6113eb14915SAlan Stern int ret; 6123eb14915SAlan Stern 6133eb14915SAlan Stern mutex_lock(&hub->status_mutex); 6143eb14915SAlan Stern ret = get_port_status(hub->hdev, port1, &hub->status->port); 6153eb14915SAlan Stern if (ret < 4) { 6163eb14915SAlan Stern dev_err(hub->intfdev, 6173eb14915SAlan Stern "%s failed (err = %d)\n", __func__, ret); 6183eb14915SAlan Stern if (ret >= 0) 6193eb14915SAlan Stern ret = -EIO; 6203eb14915SAlan Stern } else { 6213eb14915SAlan Stern *status = le16_to_cpu(hub->status->port.wPortStatus); 6223eb14915SAlan Stern *change = le16_to_cpu(hub->status->port.wPortChange); 623dbe79bbeSJohn Youn 6243eb14915SAlan Stern ret = 0; 6253eb14915SAlan Stern } 6263eb14915SAlan Stern mutex_unlock(&hub->status_mutex); 6273eb14915SAlan Stern return ret; 6283eb14915SAlan Stern } 6293eb14915SAlan Stern 6301da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds unsigned long flags; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds spin_lock_irqsave(&hub_event_lock, flags); 6352e2c5eeaSRoel Kluin if (!hub->disconnected && list_empty(&hub->event_list)) { 6361da177e4SLinus Torvalds list_add_tail(&hub->event_list, &hub_event_list); 6378e4ceb38SAlan Stern 6388e4ceb38SAlan Stern /* Suppress autosuspend until khubd runs */ 6398e4ceb38SAlan Stern usb_autopm_get_interface_no_resume( 6408e4ceb38SAlan Stern to_usb_interface(hub->intfdev)); 6411da177e4SLinus Torvalds wake_up(&khubd_wait); 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds spin_unlock_irqrestore(&hub_event_lock, flags); 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev) 6471da177e4SLinus Torvalds { 64825118084SAlan Stern struct usb_hub *hub = hdev_to_hub(hdev); 64925118084SAlan Stern 65025118084SAlan Stern if (hub) 65125118084SAlan Stern kick_khubd(hub); 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6544ee823b8SSarah Sharp /* 6554ee823b8SSarah Sharp * Let the USB core know that a USB 3.0 device has sent a Function Wake Device 6564ee823b8SSarah Sharp * Notification, which indicates it had initiated remote wakeup. 6574ee823b8SSarah Sharp * 6584ee823b8SSarah Sharp * USB 3.0 hubs do not report the port link state change from U3 to U0 when the 6594ee823b8SSarah Sharp * device initiates resume, so the USB core will not receive notice of the 6604ee823b8SSarah Sharp * resume through the normal hub interrupt URB. 6614ee823b8SSarah Sharp */ 6624ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev, 6634ee823b8SSarah Sharp unsigned int portnum) 6644ee823b8SSarah Sharp { 6654ee823b8SSarah Sharp struct usb_hub *hub; 6664ee823b8SSarah Sharp 6674ee823b8SSarah Sharp if (!hdev) 6684ee823b8SSarah Sharp return; 6694ee823b8SSarah Sharp 6704ee823b8SSarah Sharp hub = hdev_to_hub(hdev); 6714ee823b8SSarah Sharp if (hub) { 6724ee823b8SSarah Sharp set_bit(portnum, hub->wakeup_bits); 6734ee823b8SSarah Sharp kick_khubd(hub); 6744ee823b8SSarah Sharp } 6754ee823b8SSarah Sharp } 6764ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */ 6797d12e780SDavid Howells static void hub_irq(struct urb *urb) 6801da177e4SLinus Torvalds { 681ec17cf1cSTobias Klauser struct usb_hub *hub = urb->context; 682e015268dSAlan Stern int status = urb->status; 68371d2718fSRoel Kluin unsigned i; 6841da177e4SLinus Torvalds unsigned long bits; 6851da177e4SLinus Torvalds 686e015268dSAlan Stern switch (status) { 6871da177e4SLinus Torvalds case -ENOENT: /* synchronous unlink */ 6881da177e4SLinus Torvalds case -ECONNRESET: /* async unlink */ 6891da177e4SLinus Torvalds case -ESHUTDOWN: /* hardware going away */ 6901da177e4SLinus Torvalds return; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds default: /* presumably an error */ 6931da177e4SLinus Torvalds /* Cause a hub reset after 10 consecutive errors */ 694e015268dSAlan Stern dev_dbg (hub->intfdev, "transfer --> %d\n", status); 6951da177e4SLinus Torvalds if ((++hub->nerrors < 10) || hub->error) 6961da177e4SLinus Torvalds goto resubmit; 697e015268dSAlan Stern hub->error = status; 6981da177e4SLinus Torvalds /* FALL THROUGH */ 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* let khubd handle things */ 7011da177e4SLinus Torvalds case 0: /* we got data: port status changed */ 7021da177e4SLinus Torvalds bits = 0; 7031da177e4SLinus Torvalds for (i = 0; i < urb->actual_length; ++i) 7041da177e4SLinus Torvalds bits |= ((unsigned long) ((*hub->buffer)[i])) 7051da177e4SLinus Torvalds << (i*8); 7061da177e4SLinus Torvalds hub->event_bits[0] = bits; 7071da177e4SLinus Torvalds break; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds hub->nerrors = 0; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* Something happened, let khubd figure it out */ 7131da177e4SLinus Torvalds kick_khubd(hub); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds resubmit: 7161da177e4SLinus Torvalds if (hub->quiescing) 7171da177e4SLinus Torvalds return; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 7201da177e4SLinus Torvalds && status != -ENODEV && status != -EPERM) 7211da177e4SLinus Torvalds dev_err (hub->intfdev, "resubmit --> %d\n", status); 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */ 7251da177e4SLinus Torvalds static inline int 7261da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) 7271da177e4SLinus Torvalds { 728c2f6595fSAlan Stern return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 7291da177e4SLinus Torvalds HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 7301da177e4SLinus Torvalds tt, NULL, 0, 1000); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* 7341da177e4SLinus Torvalds * enumeration blocks khubd for a long time. we use keventd instead, since 7351da177e4SLinus Torvalds * long blocking there is the exception, not the rule. accordingly, HCDs 7361da177e4SLinus Torvalds * talking to TTs must queue control transfers (not just bulk and iso), so 7371da177e4SLinus Torvalds * both can talk to the same hub concurrently. 7381da177e4SLinus Torvalds */ 739cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work) 7401da177e4SLinus Torvalds { 741c4028958SDavid Howells struct usb_hub *hub = 742cb88a1b8SAlan Stern container_of(work, struct usb_hub, tt.clear_work); 7431da177e4SLinus Torvalds unsigned long flags; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds spin_lock_irqsave (&hub->tt.lock, flags); 7463b6054daSOctavian Purdila while (!list_empty(&hub->tt.clear_list)) { 747d0f830d3SH Hartley Sweeten struct list_head *next; 7481da177e4SLinus Torvalds struct usb_tt_clear *clear; 7491da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 750cb88a1b8SAlan Stern const struct hc_driver *drv; 7511da177e4SLinus Torvalds int status; 7521da177e4SLinus Torvalds 753d0f830d3SH Hartley Sweeten next = hub->tt.clear_list.next; 754d0f830d3SH Hartley Sweeten clear = list_entry (next, struct usb_tt_clear, clear_list); 7551da177e4SLinus Torvalds list_del (&clear->clear_list); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* drop lock so HCD can concurrently report other TT errors */ 7581da177e4SLinus Torvalds spin_unlock_irqrestore (&hub->tt.lock, flags); 7591da177e4SLinus Torvalds status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); 7601da177e4SLinus Torvalds if (status) 7611da177e4SLinus Torvalds dev_err (&hdev->dev, 7621da177e4SLinus Torvalds "clear tt %d (%04x) error %d\n", 7631da177e4SLinus Torvalds clear->tt, clear->devinfo, status); 764cb88a1b8SAlan Stern 765cb88a1b8SAlan Stern /* Tell the HCD, even if the operation failed */ 766cb88a1b8SAlan Stern drv = clear->hcd->driver; 767cb88a1b8SAlan Stern if (drv->clear_tt_buffer_complete) 768cb88a1b8SAlan Stern (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); 769cb88a1b8SAlan Stern 7701da177e4SLinus Torvalds kfree(clear); 771cb88a1b8SAlan Stern spin_lock_irqsave(&hub->tt.lock, flags); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds spin_unlock_irqrestore (&hub->tt.lock, flags); 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds /** 777cb88a1b8SAlan Stern * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub 778cb88a1b8SAlan Stern * @urb: an URB associated with the failed or incomplete split transaction 7791da177e4SLinus Torvalds * 7801da177e4SLinus Torvalds * High speed HCDs use this to tell the hub driver that some split control or 7811da177e4SLinus Torvalds * bulk transaction failed in a way that requires clearing internal state of 7821da177e4SLinus Torvalds * a transaction translator. This is normally detected (and reported) from 7831da177e4SLinus Torvalds * interrupt context. 7841da177e4SLinus Torvalds * 7851da177e4SLinus Torvalds * It may not be possible for that hub to handle additional full (or low) 7861da177e4SLinus Torvalds * speed transactions until that state is fully cleared out. 7871da177e4SLinus Torvalds */ 788cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb) 7891da177e4SLinus Torvalds { 790cb88a1b8SAlan Stern struct usb_device *udev = urb->dev; 791cb88a1b8SAlan Stern int pipe = urb->pipe; 7921da177e4SLinus Torvalds struct usb_tt *tt = udev->tt; 7931da177e4SLinus Torvalds unsigned long flags; 7941da177e4SLinus Torvalds struct usb_tt_clear *clear; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* we've got to cope with an arbitrary number of pending TT clears, 7971da177e4SLinus Torvalds * since each TT has "at least two" buffers that can need it (and 7981da177e4SLinus Torvalds * there can be many TTs per hub). even if they're uncommon. 7991da177e4SLinus Torvalds */ 80054e6ecb2SChristoph Lameter if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { 8011da177e4SLinus Torvalds dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 8021da177e4SLinus Torvalds /* FIXME recover somehow ... RESET_TT? */ 803cb88a1b8SAlan Stern return -ENOMEM; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* info that CLEAR_TT_BUFFER needs */ 8071da177e4SLinus Torvalds clear->tt = tt->multi ? udev->ttport : 1; 8081da177e4SLinus Torvalds clear->devinfo = usb_pipeendpoint (pipe); 8091da177e4SLinus Torvalds clear->devinfo |= udev->devnum << 4; 8101da177e4SLinus Torvalds clear->devinfo |= usb_pipecontrol (pipe) 8111da177e4SLinus Torvalds ? (USB_ENDPOINT_XFER_CONTROL << 11) 8121da177e4SLinus Torvalds : (USB_ENDPOINT_XFER_BULK << 11); 8131da177e4SLinus Torvalds if (usb_pipein (pipe)) 8141da177e4SLinus Torvalds clear->devinfo |= 1 << 15; 8151da177e4SLinus Torvalds 816cb88a1b8SAlan Stern /* info for completion callback */ 817cb88a1b8SAlan Stern clear->hcd = bus_to_hcd(udev->bus); 818cb88a1b8SAlan Stern clear->ep = urb->ep; 819cb88a1b8SAlan Stern 8201da177e4SLinus Torvalds /* tell keventd to clear state for this TT */ 8211da177e4SLinus Torvalds spin_lock_irqsave (&tt->lock, flags); 8221da177e4SLinus Torvalds list_add_tail (&clear->clear_list, &tt->clear_list); 823cb88a1b8SAlan Stern schedule_work(&tt->clear_work); 8241da177e4SLinus Torvalds spin_unlock_irqrestore (&tt->lock, flags); 825cb88a1b8SAlan Stern return 0; 8261da177e4SLinus Torvalds } 827cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); 8281da177e4SLinus Torvalds 8298520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller 8308520f380SAlan Stern * needs to delay. 8318520f380SAlan Stern */ 8328520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds int port1; 835b789696aSDavid Brownell unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; 8368520f380SAlan Stern unsigned delay; 8374489a571SAlan Stern u16 wHubCharacteristics = 8384489a571SAlan Stern le16_to_cpu(hub->descriptor->wHubCharacteristics); 8391da177e4SLinus Torvalds 8404489a571SAlan Stern /* Enable power on each port. Some hubs have reserved values 8414489a571SAlan Stern * of LPSM (> 2) in their descriptors, even though they are 8424489a571SAlan Stern * USB 2.0 hubs. Some hubs do not implement port-power switching 8434489a571SAlan Stern * but only emulate it. In all cases, the ports won't work 8444489a571SAlan Stern * unless we send these messages to the hub. 8454489a571SAlan Stern */ 8464489a571SAlan Stern if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) 8471da177e4SLinus Torvalds dev_dbg(hub->intfdev, "enabling power on all ports\n"); 8484489a571SAlan Stern else 8494489a571SAlan Stern dev_dbg(hub->intfdev, "trying to enable port power on " 8504489a571SAlan Stern "non-switchable hub\n"); 8511da177e4SLinus Torvalds for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) 8524489a571SAlan Stern set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 8531da177e4SLinus Torvalds 854b789696aSDavid Brownell /* Wait at least 100 msec for power to become stable */ 8558520f380SAlan Stern delay = max(pgood_delay, (unsigned) 100); 8568520f380SAlan Stern if (do_delay) 8578520f380SAlan Stern msleep(delay); 8588520f380SAlan Stern return delay; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub, 8621da177e4SLinus Torvalds u16 *status, u16 *change) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds int ret; 8651da177e4SLinus Torvalds 866db90e7a1SAlan Stern mutex_lock(&hub->status_mutex); 8671da177e4SLinus Torvalds ret = get_hub_status(hub->hdev, &hub->status->hub); 8681da177e4SLinus Torvalds if (ret < 0) 8691da177e4SLinus Torvalds dev_err (hub->intfdev, 870441b62c1SHarvey Harrison "%s failed (err = %d)\n", __func__, ret); 8711da177e4SLinus Torvalds else { 8721da177e4SLinus Torvalds *status = le16_to_cpu(hub->status->hub.wHubStatus); 8731da177e4SLinus Torvalds *change = le16_to_cpu(hub->status->hub.wHubChange); 8741da177e4SLinus Torvalds ret = 0; 8751da177e4SLinus Torvalds } 876db90e7a1SAlan Stern mutex_unlock(&hub->status_mutex); 8771da177e4SLinus Torvalds return ret; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 88041e7e056SSarah Sharp static int hub_set_port_link_state(struct usb_hub *hub, int port1, 88141e7e056SSarah Sharp unsigned int link_status) 88241e7e056SSarah Sharp { 88341e7e056SSarah Sharp return set_port_feature(hub->hdev, 88441e7e056SSarah Sharp port1 | (link_status << 3), 88541e7e056SSarah Sharp USB_PORT_FEAT_LINK_STATE); 88641e7e056SSarah Sharp } 88741e7e056SSarah Sharp 88841e7e056SSarah Sharp /* 88941e7e056SSarah Sharp * If USB 3.0 ports are placed into the Disabled state, they will no longer 89041e7e056SSarah Sharp * detect any device connects or disconnects. This is generally not what the 89141e7e056SSarah Sharp * USB core wants, since it expects a disabled port to produce a port status 89241e7e056SSarah Sharp * change event when a new device connects. 89341e7e056SSarah Sharp * 89441e7e056SSarah Sharp * Instead, set the link state to Disabled, wait for the link to settle into 89541e7e056SSarah Sharp * that state, clear any change bits, and then put the port into the RxDetect 89641e7e056SSarah Sharp * state. 89741e7e056SSarah Sharp */ 89841e7e056SSarah Sharp static int hub_usb3_port_disable(struct usb_hub *hub, int port1) 89941e7e056SSarah Sharp { 90041e7e056SSarah Sharp int ret; 90141e7e056SSarah Sharp int total_time; 90241e7e056SSarah Sharp u16 portchange, portstatus; 90341e7e056SSarah Sharp 90441e7e056SSarah Sharp if (!hub_is_superspeed(hub->hdev)) 90541e7e056SSarah Sharp return -EINVAL; 90641e7e056SSarah Sharp 90741e7e056SSarah Sharp ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED); 90841e7e056SSarah Sharp if (ret) { 90941e7e056SSarah Sharp dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 91041e7e056SSarah Sharp port1, ret); 91141e7e056SSarah Sharp return ret; 91241e7e056SSarah Sharp } 91341e7e056SSarah Sharp 91441e7e056SSarah Sharp /* Wait for the link to enter the disabled state. */ 91541e7e056SSarah Sharp for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 91641e7e056SSarah Sharp ret = hub_port_status(hub, port1, &portstatus, &portchange); 91741e7e056SSarah Sharp if (ret < 0) 91841e7e056SSarah Sharp return ret; 91941e7e056SSarah Sharp 92041e7e056SSarah Sharp if ((portstatus & USB_PORT_STAT_LINK_STATE) == 92141e7e056SSarah Sharp USB_SS_PORT_LS_SS_DISABLED) 92241e7e056SSarah Sharp break; 92341e7e056SSarah Sharp if (total_time >= HUB_DEBOUNCE_TIMEOUT) 92441e7e056SSarah Sharp break; 92541e7e056SSarah Sharp msleep(HUB_DEBOUNCE_STEP); 92641e7e056SSarah Sharp } 92741e7e056SSarah Sharp if (total_time >= HUB_DEBOUNCE_TIMEOUT) 92841e7e056SSarah Sharp dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n", 92941e7e056SSarah Sharp port1, total_time); 93041e7e056SSarah Sharp 93141e7e056SSarah Sharp return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT); 93241e7e056SSarah Sharp } 93341e7e056SSarah Sharp 9348b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 9358b28c752SAlan Stern { 9368b28c752SAlan Stern struct usb_device *hdev = hub->hdev; 9370458d5b4SAlan Stern int ret = 0; 9388b28c752SAlan Stern 939ff823c79SLan Tianyu if (hub->ports[port1 - 1]->child && set_state) 940ff823c79SLan Tianyu usb_set_device_state(hub->ports[port1 - 1]->child, 9418b28c752SAlan Stern USB_STATE_NOTATTACHED); 94241e7e056SSarah Sharp if (!hub->error) { 94341e7e056SSarah Sharp if (hub_is_superspeed(hub->hdev)) 94441e7e056SSarah Sharp ret = hub_usb3_port_disable(hub, port1); 94541e7e056SSarah Sharp else 94641e7e056SSarah Sharp ret = clear_port_feature(hdev, port1, 94741e7e056SSarah Sharp USB_PORT_FEAT_ENABLE); 94841e7e056SSarah Sharp } 9498b28c752SAlan Stern if (ret) 9508b28c752SAlan Stern dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 9518b28c752SAlan Stern port1, ret); 9528b28c752SAlan Stern return ret; 9538b28c752SAlan Stern } 9548b28c752SAlan Stern 9550458d5b4SAlan Stern /* 9566d42fcdbSJustin P. Mattock * Disable a port and mark a logical connect-change event, so that some 9570458d5b4SAlan Stern * time later khubd will disconnect() any existing usb_device on the port 9580458d5b4SAlan Stern * and will re-enumerate if there actually is a device attached. 9590458d5b4SAlan Stern */ 9600458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 9617d069b7dSAlan Stern { 9620458d5b4SAlan Stern dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); 9630458d5b4SAlan Stern hub_port_disable(hub, port1, 1); 9640458d5b4SAlan Stern 9650458d5b4SAlan Stern /* FIXME let caller ask to power down the port: 9660458d5b4SAlan Stern * - some devices won't enumerate without a VBUS power cycle 9670458d5b4SAlan Stern * - SRP saves power that way 9680458d5b4SAlan Stern * - ... new call, TBD ... 9690458d5b4SAlan Stern * That's easy if this hub can switch power per-port, and 9700458d5b4SAlan Stern * khubd reactivates the port later (timer, SRP, etc). 9710458d5b4SAlan Stern * Powerdown must be optional, because of reset/DFU. 9720458d5b4SAlan Stern */ 9730458d5b4SAlan Stern 9740458d5b4SAlan Stern set_bit(port1, hub->change_bits); 9750458d5b4SAlan Stern kick_khubd(hub); 9760458d5b4SAlan Stern } 9770458d5b4SAlan Stern 978253e0572SAlan Stern /** 979253e0572SAlan Stern * usb_remove_device - disable a device's port on its parent hub 980253e0572SAlan Stern * @udev: device to be disabled and removed 981253e0572SAlan Stern * Context: @udev locked, must be able to sleep. 982253e0572SAlan Stern * 983253e0572SAlan Stern * After @udev's port has been disabled, khubd is notified and it will 984253e0572SAlan Stern * see that the device has been disconnected. When the device is 985253e0572SAlan Stern * physically unplugged and something is plugged in, the events will 986253e0572SAlan Stern * be received and processed normally. 987253e0572SAlan Stern */ 988253e0572SAlan Stern int usb_remove_device(struct usb_device *udev) 989253e0572SAlan Stern { 990253e0572SAlan Stern struct usb_hub *hub; 991253e0572SAlan Stern struct usb_interface *intf; 992253e0572SAlan Stern 993253e0572SAlan Stern if (!udev->parent) /* Can't remove a root hub */ 994253e0572SAlan Stern return -EINVAL; 995253e0572SAlan Stern hub = hdev_to_hub(udev->parent); 996253e0572SAlan Stern intf = to_usb_interface(hub->intfdev); 997253e0572SAlan Stern 998253e0572SAlan Stern usb_autopm_get_interface(intf); 999253e0572SAlan Stern set_bit(udev->portnum, hub->removed_bits); 1000253e0572SAlan Stern hub_port_logical_disconnect(hub, udev->portnum); 1001253e0572SAlan Stern usb_autopm_put_interface(intf); 1002253e0572SAlan Stern return 0; 1003253e0572SAlan Stern } 1004253e0572SAlan Stern 10056ee0b270SAlan Stern enum hub_activation_type { 10068e4ceb38SAlan Stern HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ 10078520f380SAlan Stern HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 10086ee0b270SAlan Stern }; 10095e6effaeSAlan Stern 10108520f380SAlan Stern static void hub_init_func2(struct work_struct *ws); 10118520f380SAlan Stern static void hub_init_func3(struct work_struct *ws); 10128520f380SAlan Stern 1013f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) 10145e6effaeSAlan Stern { 10155e6effaeSAlan Stern struct usb_device *hdev = hub->hdev; 1016653a39d1SSarah Sharp struct usb_hcd *hcd; 1017653a39d1SSarah Sharp int ret; 10185e6effaeSAlan Stern int port1; 1019f2835219SAlan Stern int status; 1020948fea37SAlan Stern bool need_debounce_delay = false; 10218520f380SAlan Stern unsigned delay; 10228520f380SAlan Stern 10238520f380SAlan Stern /* Continue a partial initialization */ 10248520f380SAlan Stern if (type == HUB_INIT2) 10258520f380SAlan Stern goto init2; 10268520f380SAlan Stern if (type == HUB_INIT3) 10278520f380SAlan Stern goto init3; 10285e6effaeSAlan Stern 1029a45aa3b3SElric Fu /* The superspeed hub except for root hub has to use Hub Depth 1030a45aa3b3SElric Fu * value as an offset into the route string to locate the bits 1031a45aa3b3SElric Fu * it uses to determine the downstream port number. So hub driver 1032a45aa3b3SElric Fu * should send a set hub depth request to superspeed hub after 1033a45aa3b3SElric Fu * the superspeed hub is set configuration in initialization or 1034a45aa3b3SElric Fu * reset procedure. 1035a45aa3b3SElric Fu * 1036a45aa3b3SElric Fu * After a resume, port power should still be on. 1037f2835219SAlan Stern * For any other type of activation, turn it on. 1038f2835219SAlan Stern */ 10398520f380SAlan Stern if (type != HUB_RESUME) { 1040a45aa3b3SElric Fu if (hdev->parent && hub_is_superspeed(hdev)) { 1041a45aa3b3SElric Fu ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 1042a45aa3b3SElric Fu HUB_SET_DEPTH, USB_RT_HUB, 1043a45aa3b3SElric Fu hdev->level - 1, 0, NULL, 0, 1044a45aa3b3SElric Fu USB_CTRL_SET_TIMEOUT); 1045a45aa3b3SElric Fu if (ret < 0) 1046a45aa3b3SElric Fu dev_err(hub->intfdev, 1047a45aa3b3SElric Fu "set hub depth failed\n"); 1048a45aa3b3SElric Fu } 10498520f380SAlan Stern 10508520f380SAlan Stern /* Speed up system boot by using a delayed_work for the 10518520f380SAlan Stern * hub's initial power-up delays. This is pretty awkward 10528520f380SAlan Stern * and the implementation looks like a home-brewed sort of 10538520f380SAlan Stern * setjmp/longjmp, but it saves at least 100 ms for each 10548520f380SAlan Stern * root hub (assuming usbcore is compiled into the kernel 10558520f380SAlan Stern * rather than as a module). It adds up. 10568520f380SAlan Stern * 10578520f380SAlan Stern * This can't be done for HUB_RESUME or HUB_RESET_RESUME 10588520f380SAlan Stern * because for those activation types the ports have to be 10598520f380SAlan Stern * operational when we return. In theory this could be done 10608520f380SAlan Stern * for HUB_POST_RESET, but it's easier not to. 10618520f380SAlan Stern */ 10628520f380SAlan Stern if (type == HUB_INIT) { 10638520f380SAlan Stern delay = hub_power_on(hub, false); 10648520f380SAlan Stern PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); 10658520f380SAlan Stern schedule_delayed_work(&hub->init_work, 10668520f380SAlan Stern msecs_to_jiffies(delay)); 106761fbeba1SAlan Stern 106861fbeba1SAlan Stern /* Suppress autosuspend until init is done */ 10698e4ceb38SAlan Stern usb_autopm_get_interface_no_resume( 10708e4ceb38SAlan Stern to_usb_interface(hub->intfdev)); 10718520f380SAlan Stern return; /* Continues at init2: below */ 1072653a39d1SSarah Sharp } else if (type == HUB_RESET_RESUME) { 1073653a39d1SSarah Sharp /* The internal host controller state for the hub device 1074653a39d1SSarah Sharp * may be gone after a host power loss on system resume. 1075653a39d1SSarah Sharp * Update the device's info so the HW knows it's a hub. 1076653a39d1SSarah Sharp */ 1077653a39d1SSarah Sharp hcd = bus_to_hcd(hdev->bus); 1078653a39d1SSarah Sharp if (hcd->driver->update_hub_device) { 1079653a39d1SSarah Sharp ret = hcd->driver->update_hub_device(hcd, hdev, 1080653a39d1SSarah Sharp &hub->tt, GFP_NOIO); 1081653a39d1SSarah Sharp if (ret < 0) { 1082653a39d1SSarah Sharp dev_err(hub->intfdev, "Host not " 1083653a39d1SSarah Sharp "accepting hub info " 1084653a39d1SSarah Sharp "update.\n"); 1085653a39d1SSarah Sharp dev_err(hub->intfdev, "LS/FS devices " 1086653a39d1SSarah Sharp "and hubs may not work " 1087653a39d1SSarah Sharp "under this hub\n."); 1088653a39d1SSarah Sharp } 1089653a39d1SSarah Sharp } 1090653a39d1SSarah Sharp hub_power_on(hub, true); 10918520f380SAlan Stern } else { 10928520f380SAlan Stern hub_power_on(hub, true); 10938520f380SAlan Stern } 10948520f380SAlan Stern } 10958520f380SAlan Stern init2: 1096f2835219SAlan Stern 10976ee0b270SAlan Stern /* Check each port and set hub->change_bits to let khubd know 10986ee0b270SAlan Stern * which ports need attention. 10995e6effaeSAlan Stern */ 11005e6effaeSAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 1101ff823c79SLan Tianyu struct usb_device *udev = hub->ports[port1 - 1]->child; 11025e6effaeSAlan Stern u16 portstatus, portchange; 11035e6effaeSAlan Stern 11046ee0b270SAlan Stern portstatus = portchange = 0; 11056ee0b270SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 11066ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 11076ee0b270SAlan Stern dev_dbg(hub->intfdev, 11086ee0b270SAlan Stern "port %d: status %04x change %04x\n", 11096ee0b270SAlan Stern port1, portstatus, portchange); 11105e6effaeSAlan Stern 11116ee0b270SAlan Stern /* After anything other than HUB_RESUME (i.e., initialization 11126ee0b270SAlan Stern * or any sort of reset), every port should be disabled. 11136ee0b270SAlan Stern * Unconnected ports should likewise be disabled (paranoia), 11146ee0b270SAlan Stern * and so should ports for which we have no usb_device. 11155e6effaeSAlan Stern */ 11166ee0b270SAlan Stern if ((portstatus & USB_PORT_STAT_ENABLE) && ( 11176ee0b270SAlan Stern type != HUB_RESUME || 11186ee0b270SAlan Stern !(portstatus & USB_PORT_STAT_CONNECTION) || 11196ee0b270SAlan Stern !udev || 11206ee0b270SAlan Stern udev->state == USB_STATE_NOTATTACHED)) { 11219f0a6cd3SAndiry Xu /* 11229f0a6cd3SAndiry Xu * USB3 protocol ports will automatically transition 11239f0a6cd3SAndiry Xu * to Enabled state when detect an USB3.0 device attach. 11249f0a6cd3SAndiry Xu * Do not disable USB3 protocol ports. 11259f0a6cd3SAndiry Xu */ 1126131dec34SSarah Sharp if (!hub_is_superspeed(hdev)) { 11279f0a6cd3SAndiry Xu clear_port_feature(hdev, port1, 11289f0a6cd3SAndiry Xu USB_PORT_FEAT_ENABLE); 11296ee0b270SAlan Stern portstatus &= ~USB_PORT_STAT_ENABLE; 113085f0ff46SSarah Sharp } else { 113185f0ff46SSarah Sharp /* Pretend that power was lost for USB3 devs */ 113285f0ff46SSarah Sharp portstatus &= ~USB_PORT_STAT_ENABLE; 11336ee0b270SAlan Stern } 11349f0a6cd3SAndiry Xu } 11356ee0b270SAlan Stern 1136948fea37SAlan Stern /* Clear status-change flags; we'll debounce later */ 1137948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) { 1138948fea37SAlan Stern need_debounce_delay = true; 1139948fea37SAlan Stern clear_port_feature(hub->hdev, port1, 1140948fea37SAlan Stern USB_PORT_FEAT_C_CONNECTION); 1141948fea37SAlan Stern } 1142948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) { 1143948fea37SAlan Stern need_debounce_delay = true; 1144948fea37SAlan Stern clear_port_feature(hub->hdev, port1, 1145948fea37SAlan Stern USB_PORT_FEAT_C_ENABLE); 1146948fea37SAlan Stern } 114779c3dd81SDon Zickus if ((portchange & USB_PORT_STAT_C_BH_RESET) && 114879c3dd81SDon Zickus hub_is_superspeed(hub->hdev)) { 114979c3dd81SDon Zickus need_debounce_delay = true; 115079c3dd81SDon Zickus clear_port_feature(hub->hdev, port1, 115179c3dd81SDon Zickus USB_PORT_FEAT_C_BH_PORT_RESET); 115279c3dd81SDon Zickus } 1153253e0572SAlan Stern /* We can forget about a "removed" device when there's a 1154253e0572SAlan Stern * physical disconnect or the connect status changes. 1155253e0572SAlan Stern */ 1156253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 1157253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION)) 1158253e0572SAlan Stern clear_bit(port1, hub->removed_bits); 1159253e0572SAlan Stern 11606ee0b270SAlan Stern if (!udev || udev->state == USB_STATE_NOTATTACHED) { 11616ee0b270SAlan Stern /* Tell khubd to disconnect the device or 11626ee0b270SAlan Stern * check for a new connection 11636ee0b270SAlan Stern */ 11646ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 11656ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11666ee0b270SAlan Stern 11676ee0b270SAlan Stern } else if (portstatus & USB_PORT_STAT_ENABLE) { 116872937e1eSSarah Sharp bool port_resumed = (portstatus & 116972937e1eSSarah Sharp USB_PORT_STAT_LINK_STATE) == 117072937e1eSSarah Sharp USB_SS_PORT_LS_U0; 11716ee0b270SAlan Stern /* The power session apparently survived the resume. 11726ee0b270SAlan Stern * If there was an overcurrent or suspend change 11736ee0b270SAlan Stern * (i.e., remote wakeup request), have khubd 117472937e1eSSarah Sharp * take care of it. Look at the port link state 117572937e1eSSarah Sharp * for USB 3.0 hubs, since they don't have a suspend 117672937e1eSSarah Sharp * change bit, and they don't set the port link change 117772937e1eSSarah Sharp * bit on device-initiated resume. 11786ee0b270SAlan Stern */ 117972937e1eSSarah Sharp if (portchange || (hub_is_superspeed(hub->hdev) && 118072937e1eSSarah Sharp port_resumed)) 11816ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11826ee0b270SAlan Stern 11836ee0b270SAlan Stern } else if (udev->persist_enabled) { 11846ee0b270SAlan Stern #ifdef CONFIG_PM 11855e6effaeSAlan Stern udev->reset_resume = 1; 11866ee0b270SAlan Stern #endif 11878808f00cSAlan Stern set_bit(port1, hub->change_bits); 11888808f00cSAlan Stern 11896ee0b270SAlan Stern } else { 11906ee0b270SAlan Stern /* The power session is gone; tell khubd */ 11916ee0b270SAlan Stern usb_set_device_state(udev, USB_STATE_NOTATTACHED); 11926ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11935e6effaeSAlan Stern } 11945e6effaeSAlan Stern } 11955e6effaeSAlan Stern 1196948fea37SAlan Stern /* If no port-status-change flags were set, we don't need any 1197948fea37SAlan Stern * debouncing. If flags were set we can try to debounce the 1198948fea37SAlan Stern * ports all at once right now, instead of letting khubd do them 1199948fea37SAlan Stern * one at a time later on. 1200948fea37SAlan Stern * 1201948fea37SAlan Stern * If any port-status changes do occur during this delay, khubd 1202948fea37SAlan Stern * will see them later and handle them normally. 1203948fea37SAlan Stern */ 12048520f380SAlan Stern if (need_debounce_delay) { 12058520f380SAlan Stern delay = HUB_DEBOUNCE_STABLE; 1206f2835219SAlan Stern 12078520f380SAlan Stern /* Don't do a long sleep inside a workqueue routine */ 12088520f380SAlan Stern if (type == HUB_INIT2) { 12098520f380SAlan Stern PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); 12108520f380SAlan Stern schedule_delayed_work(&hub->init_work, 12118520f380SAlan Stern msecs_to_jiffies(delay)); 12128520f380SAlan Stern return; /* Continues at init3: below */ 12138520f380SAlan Stern } else { 12148520f380SAlan Stern msleep(delay); 12158520f380SAlan Stern } 12168520f380SAlan Stern } 12178520f380SAlan Stern init3: 1218f2835219SAlan Stern hub->quiescing = 0; 1219f2835219SAlan Stern 1220f2835219SAlan Stern status = usb_submit_urb(hub->urb, GFP_NOIO); 1221f2835219SAlan Stern if (status < 0) 1222f2835219SAlan Stern dev_err(hub->intfdev, "activate --> %d\n", status); 1223f2835219SAlan Stern if (hub->has_indicators && blinkenlights) 1224f2835219SAlan Stern schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 1225f2835219SAlan Stern 1226f2835219SAlan Stern /* Scan all ports that need attention */ 1227f2835219SAlan Stern kick_khubd(hub); 12288e4ceb38SAlan Stern 12298e4ceb38SAlan Stern /* Allow autosuspend if it was suppressed */ 12308e4ceb38SAlan Stern if (type <= HUB_INIT3) 12318e4ceb38SAlan Stern usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); 12325e6effaeSAlan Stern } 12335e6effaeSAlan Stern 12348520f380SAlan Stern /* Implement the continuations for the delays above */ 12358520f380SAlan Stern static void hub_init_func2(struct work_struct *ws) 12368520f380SAlan Stern { 12378520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 12388520f380SAlan Stern 12398520f380SAlan Stern hub_activate(hub, HUB_INIT2); 12408520f380SAlan Stern } 12418520f380SAlan Stern 12428520f380SAlan Stern static void hub_init_func3(struct work_struct *ws) 12438520f380SAlan Stern { 12448520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 12458520f380SAlan Stern 12468520f380SAlan Stern hub_activate(hub, HUB_INIT3); 12478520f380SAlan Stern } 12488520f380SAlan Stern 12494330354fSAlan Stern enum hub_quiescing_type { 12504330354fSAlan Stern HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND 12514330354fSAlan Stern }; 12524330354fSAlan Stern 12534330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) 12544330354fSAlan Stern { 12554330354fSAlan Stern struct usb_device *hdev = hub->hdev; 12564330354fSAlan Stern int i; 12574330354fSAlan Stern 12588520f380SAlan Stern cancel_delayed_work_sync(&hub->init_work); 12598520f380SAlan Stern 12604330354fSAlan Stern /* khubd and related activity won't re-trigger */ 12614330354fSAlan Stern hub->quiescing = 1; 12624330354fSAlan Stern 12634330354fSAlan Stern if (type != HUB_SUSPEND) { 12644330354fSAlan Stern /* Disconnect all the children */ 12654330354fSAlan Stern for (i = 0; i < hdev->maxchild; ++i) { 1266ff823c79SLan Tianyu if (hub->ports[i]->child) 1267ff823c79SLan Tianyu usb_disconnect(&hub->ports[i]->child); 12684330354fSAlan Stern } 12694330354fSAlan Stern } 12704330354fSAlan Stern 12714330354fSAlan Stern /* Stop khubd and related activity */ 12724330354fSAlan Stern usb_kill_urb(hub->urb); 12734330354fSAlan Stern if (hub->has_indicators) 12744330354fSAlan Stern cancel_delayed_work_sync(&hub->leds); 12754330354fSAlan Stern if (hub->tt.hub) 1276036546bfSOctavian Purdila flush_work(&hub->tt.clear_work); 12774330354fSAlan Stern } 12784330354fSAlan Stern 12793eb14915SAlan Stern /* caller has locked the hub device */ 12803eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf) 12813eb14915SAlan Stern { 12823eb14915SAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 12833eb14915SAlan Stern 12844330354fSAlan Stern hub_quiesce(hub, HUB_PRE_RESET); 1285f07600cfSAlan Stern return 0; 12867d069b7dSAlan Stern } 12877d069b7dSAlan Stern 12887d069b7dSAlan Stern /* caller has locked the hub device */ 1289f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf) 12907d069b7dSAlan Stern { 12917de18d8bSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 12927de18d8bSAlan Stern 1293f2835219SAlan Stern hub_activate(hub, HUB_POST_RESET); 1294f07600cfSAlan Stern return 0; 12957d069b7dSAlan Stern } 12967d069b7dSAlan Stern 1297fa2a9566SLan Tianyu static void usb_port_device_release(struct device *dev) 1298fa2a9566SLan Tianyu { 1299fa2a9566SLan Tianyu struct usb_port *port_dev = to_usb_port(dev); 1300fa2a9566SLan Tianyu 1301fa2a9566SLan Tianyu kfree(port_dev); 1302fa2a9566SLan Tianyu } 1303fa2a9566SLan Tianyu 1304fa2a9566SLan Tianyu static void usb_hub_remove_port_device(struct usb_hub *hub, 1305fa2a9566SLan Tianyu int port1) 1306fa2a9566SLan Tianyu { 1307fa2a9566SLan Tianyu device_unregister(&hub->ports[port1 - 1]->dev); 1308fa2a9566SLan Tianyu } 1309fa2a9566SLan Tianyu 1310fa2a9566SLan Tianyu struct device_type usb_port_device_type = { 1311fa2a9566SLan Tianyu .name = "usb_port", 1312fa2a9566SLan Tianyu .release = usb_port_device_release, 1313fa2a9566SLan Tianyu }; 1314fa2a9566SLan Tianyu 1315fa2a9566SLan Tianyu static int usb_hub_create_port_device(struct usb_hub *hub, 1316fa2a9566SLan Tianyu int port1) 1317fa2a9566SLan Tianyu { 1318fa2a9566SLan Tianyu struct usb_port *port_dev = NULL; 1319fa2a9566SLan Tianyu int retval; 1320fa2a9566SLan Tianyu 1321fa2a9566SLan Tianyu port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 1322fa2a9566SLan Tianyu if (!port_dev) { 1323fa2a9566SLan Tianyu retval = -ENOMEM; 1324fa2a9566SLan Tianyu goto exit; 1325fa2a9566SLan Tianyu } 1326fa2a9566SLan Tianyu 1327fa2a9566SLan Tianyu hub->ports[port1 - 1] = port_dev; 1328fa2a9566SLan Tianyu port_dev->dev.parent = hub->intfdev; 1329fa2a9566SLan Tianyu port_dev->dev.type = &usb_port_device_type; 1330fa2a9566SLan Tianyu dev_set_name(&port_dev->dev, "port%d", port1); 1331fa2a9566SLan Tianyu 1332fa2a9566SLan Tianyu retval = device_register(&port_dev->dev); 1333fa2a9566SLan Tianyu if (retval) 1334fa2a9566SLan Tianyu goto error_register; 1335fa2a9566SLan Tianyu return 0; 1336fa2a9566SLan Tianyu 1337fa2a9566SLan Tianyu error_register: 1338fa2a9566SLan Tianyu put_device(&port_dev->dev); 1339fa2a9566SLan Tianyu exit: 1340fa2a9566SLan Tianyu return retval; 1341fa2a9566SLan Tianyu } 1342fa2a9566SLan Tianyu 13431da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub, 13441da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint) 13451da177e4SLinus Torvalds { 1346b356b7c7SSarah Sharp struct usb_hcd *hcd; 13471da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 13481da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 13491da177e4SLinus Torvalds u16 hubstatus, hubchange; 135074ad9bd2SGreg Kroah-Hartman u16 wHubCharacteristics; 13511da177e4SLinus Torvalds unsigned int pipe; 1352fa2a9566SLan Tianyu int maxp, ret, i; 13537cbe5dcaSAlan Stern char *message = "out of memory"; 13541da177e4SLinus Torvalds 1355d697cddaSAlan Stern hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); 13561da177e4SLinus Torvalds if (!hub->buffer) { 13571da177e4SLinus Torvalds ret = -ENOMEM; 13581da177e4SLinus Torvalds goto fail; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 13621da177e4SLinus Torvalds if (!hub->status) { 13631da177e4SLinus Torvalds ret = -ENOMEM; 13641da177e4SLinus Torvalds goto fail; 13651da177e4SLinus Torvalds } 1366db90e7a1SAlan Stern mutex_init(&hub->status_mutex); 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); 13691da177e4SLinus Torvalds if (!hub->descriptor) { 13701da177e4SLinus Torvalds ret = -ENOMEM; 13711da177e4SLinus Torvalds goto fail; 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /* Request the entire hub descriptor. 13751da177e4SLinus Torvalds * hub->descriptor can handle USB_MAXCHILDREN ports, 13761da177e4SLinus Torvalds * but the hub can/will return fewer bytes here. 13771da177e4SLinus Torvalds */ 1378dbe79bbeSJohn Youn ret = get_hub_descriptor(hdev, hub->descriptor); 13791da177e4SLinus Torvalds if (ret < 0) { 13801da177e4SLinus Torvalds message = "can't read hub descriptor"; 13811da177e4SLinus Torvalds goto fail; 13821da177e4SLinus Torvalds } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { 13831da177e4SLinus Torvalds message = "hub has too many ports!"; 13841da177e4SLinus Torvalds ret = -ENODEV; 13851da177e4SLinus Torvalds goto fail; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds hdev->maxchild = hub->descriptor->bNbrPorts; 13891da177e4SLinus Torvalds dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 13901da177e4SLinus Torvalds (hdev->maxchild == 1) ? "" : "s"); 13911da177e4SLinus Torvalds 1392fa2a9566SLan Tianyu hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *), 1393336c5c31SLan Tianyu GFP_KERNEL); 1394ff823c79SLan Tianyu if (!hub->ports) { 13957cbe5dcaSAlan Stern ret = -ENOMEM; 13967cbe5dcaSAlan Stern goto fail; 13977cbe5dcaSAlan Stern } 13987cbe5dcaSAlan Stern 139974ad9bd2SGreg Kroah-Hartman wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 14001da177e4SLinus Torvalds 1401dbe79bbeSJohn Youn /* FIXME for USB 3.0, skip for now */ 1402dbe79bbeSJohn Youn if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && 1403dbe79bbeSJohn Youn !(hub_is_superspeed(hdev))) { 14041da177e4SLinus Torvalds int i; 14051da177e4SLinus Torvalds char portstr [USB_MAXCHILDREN + 1]; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds for (i = 0; i < hdev->maxchild; i++) 1408dbe79bbeSJohn Youn portstr[i] = hub->descriptor->u.hs.DeviceRemovable 14091da177e4SLinus Torvalds [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 14101da177e4SLinus Torvalds ? 'F' : 'R'; 14111da177e4SLinus Torvalds portstr[hdev->maxchild] = 0; 14121da177e4SLinus Torvalds dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 14131da177e4SLinus Torvalds } else 14141da177e4SLinus Torvalds dev_dbg(hub_dev, "standalone hub\n"); 14151da177e4SLinus Torvalds 141674ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_LPSM) { 14177bf01185SAman Deep case HUB_CHAR_COMMON_LPSM: 14181da177e4SLinus Torvalds dev_dbg(hub_dev, "ganged power switching\n"); 14191da177e4SLinus Torvalds break; 14207bf01185SAman Deep case HUB_CHAR_INDV_PORT_LPSM: 14211da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port power switching\n"); 14221da177e4SLinus Torvalds break; 14237bf01185SAman Deep case HUB_CHAR_NO_LPSM: 14247bf01185SAman Deep case HUB_CHAR_LPSM: 14251da177e4SLinus Torvalds dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 14261da177e4SLinus Torvalds break; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 142974ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_OCPM) { 14307bf01185SAman Deep case HUB_CHAR_COMMON_OCPM: 14311da177e4SLinus Torvalds dev_dbg(hub_dev, "global over-current protection\n"); 14321da177e4SLinus Torvalds break; 14337bf01185SAman Deep case HUB_CHAR_INDV_PORT_OCPM: 14341da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port over-current protection\n"); 14351da177e4SLinus Torvalds break; 14367bf01185SAman Deep case HUB_CHAR_NO_OCPM: 14377bf01185SAman Deep case HUB_CHAR_OCPM: 14381da177e4SLinus Torvalds dev_dbg(hub_dev, "no over-current protection\n"); 14391da177e4SLinus Torvalds break; 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds spin_lock_init (&hub->tt.lock); 14431da177e4SLinus Torvalds INIT_LIST_HEAD (&hub->tt.clear_list); 1444cb88a1b8SAlan Stern INIT_WORK(&hub->tt.clear_work, hub_tt_work); 14451da177e4SLinus Torvalds switch (hdev->descriptor.bDeviceProtocol) { 14467bf01185SAman Deep case USB_HUB_PR_FS: 14471da177e4SLinus Torvalds break; 14487bf01185SAman Deep case USB_HUB_PR_HS_SINGLE_TT: 14491da177e4SLinus Torvalds dev_dbg(hub_dev, "Single TT\n"); 14501da177e4SLinus Torvalds hub->tt.hub = hdev; 14511da177e4SLinus Torvalds break; 14527bf01185SAman Deep case USB_HUB_PR_HS_MULTI_TT: 14531da177e4SLinus Torvalds ret = usb_set_interface(hdev, 0, 1); 14541da177e4SLinus Torvalds if (ret == 0) { 14551da177e4SLinus Torvalds dev_dbg(hub_dev, "TT per port\n"); 14561da177e4SLinus Torvalds hub->tt.multi = 1; 14571da177e4SLinus Torvalds } else 14581da177e4SLinus Torvalds dev_err(hub_dev, "Using single TT (err %d)\n", 14591da177e4SLinus Torvalds ret); 14601da177e4SLinus Torvalds hub->tt.hub = hdev; 14611da177e4SLinus Torvalds break; 14627bf01185SAman Deep case USB_HUB_PR_SS: 1463d2e9b4d6SSarah Sharp /* USB 3.0 hubs don't have a TT */ 1464d2e9b4d6SSarah Sharp break; 14651da177e4SLinus Torvalds default: 14661da177e4SLinus Torvalds dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 14671da177e4SLinus Torvalds hdev->descriptor.bDeviceProtocol); 14681da177e4SLinus Torvalds break; 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 1471e09711aeSdavid-b@pacbell.net /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 147274ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_TTTT) { 1473e09711aeSdavid-b@pacbell.net case HUB_TTTT_8_BITS: 1474e09711aeSdavid-b@pacbell.net if (hdev->descriptor.bDeviceProtocol != 0) { 1475e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666; 1476e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1477e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1478e09711aeSdavid-b@pacbell.net 8, hub->tt.think_time); 1479e09711aeSdavid-b@pacbell.net } 14801da177e4SLinus Torvalds break; 1481e09711aeSdavid-b@pacbell.net case HUB_TTTT_16_BITS: 1482e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 2; 1483e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1484e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1485e09711aeSdavid-b@pacbell.net 16, hub->tt.think_time); 14861da177e4SLinus Torvalds break; 1487e09711aeSdavid-b@pacbell.net case HUB_TTTT_24_BITS: 1488e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 3; 1489e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1490e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1491e09711aeSdavid-b@pacbell.net 24, hub->tt.think_time); 14921da177e4SLinus Torvalds break; 1493e09711aeSdavid-b@pacbell.net case HUB_TTTT_32_BITS: 1494e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 4; 1495e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1496e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1497e09711aeSdavid-b@pacbell.net 32, hub->tt.think_time); 14981da177e4SLinus Torvalds break; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds /* probe() zeroes hub->indicator[] */ 150274ad9bd2SGreg Kroah-Hartman if (wHubCharacteristics & HUB_CHAR_PORTIND) { 15031da177e4SLinus Torvalds hub->has_indicators = 1; 15041da177e4SLinus Torvalds dev_dbg(hub_dev, "Port indicators are supported\n"); 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds dev_dbg(hub_dev, "power on to power good time: %dms\n", 15081da177e4SLinus Torvalds hub->descriptor->bPwrOn2PwrGood * 2); 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds /* power budgeting mostly matters with bus-powered hubs, 15111da177e4SLinus Torvalds * and battery-powered root hubs (may provide just 8 mA). 15121da177e4SLinus Torvalds */ 15131da177e4SLinus Torvalds ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 151455c52718SAlan Stern if (ret < 2) { 15151da177e4SLinus Torvalds message = "can't get hub status"; 15161da177e4SLinus Torvalds goto fail; 15171da177e4SLinus Torvalds } 15187d35b929SAlan Stern le16_to_cpus(&hubstatus); 15197d35b929SAlan Stern if (hdev == hdev->bus->root_hub) { 152055c52718SAlan Stern if (hdev->bus_mA == 0 || hdev->bus_mA >= 500) 152155c52718SAlan Stern hub->mA_per_port = 500; 152255c52718SAlan Stern else { 152355c52718SAlan Stern hub->mA_per_port = hdev->bus_mA; 152455c52718SAlan Stern hub->limited_power = 1; 152555c52718SAlan Stern } 15267d35b929SAlan Stern } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 15271da177e4SLinus Torvalds dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 15281da177e4SLinus Torvalds hub->descriptor->bHubContrCurrent); 152955c52718SAlan Stern hub->limited_power = 1; 153055c52718SAlan Stern if (hdev->maxchild > 0) { 153155c52718SAlan Stern int remaining = hdev->bus_mA - 153255c52718SAlan Stern hub->descriptor->bHubContrCurrent; 15331da177e4SLinus Torvalds 153455c52718SAlan Stern if (remaining < hdev->maxchild * 100) 153555c52718SAlan Stern dev_warn(hub_dev, 153655c52718SAlan Stern "insufficient power available " 153755c52718SAlan Stern "to use all downstream ports\n"); 153855c52718SAlan Stern hub->mA_per_port = 100; /* 7.2.1.1 */ 153955c52718SAlan Stern } 154055c52718SAlan Stern } else { /* Self-powered external hub */ 154155c52718SAlan Stern /* FIXME: What about battery-powered external hubs that 154255c52718SAlan Stern * provide less current per port? */ 154355c52718SAlan Stern hub->mA_per_port = 500; 154455c52718SAlan Stern } 154555c52718SAlan Stern if (hub->mA_per_port < 500) 154655c52718SAlan Stern dev_dbg(hub_dev, "%umA bus power budget for each child\n", 154755c52718SAlan Stern hub->mA_per_port); 15481da177e4SLinus Torvalds 1549b356b7c7SSarah Sharp /* Update the HCD's internal representation of this hub before khubd 1550b356b7c7SSarah Sharp * starts getting port status changes for devices under the hub. 1551b356b7c7SSarah Sharp */ 1552b356b7c7SSarah Sharp hcd = bus_to_hcd(hdev->bus); 1553b356b7c7SSarah Sharp if (hcd->driver->update_hub_device) { 1554b356b7c7SSarah Sharp ret = hcd->driver->update_hub_device(hcd, hdev, 1555b356b7c7SSarah Sharp &hub->tt, GFP_KERNEL); 1556b356b7c7SSarah Sharp if (ret < 0) { 1557b356b7c7SSarah Sharp message = "can't update HCD hub info"; 1558b356b7c7SSarah Sharp goto fail; 1559b356b7c7SSarah Sharp } 1560b356b7c7SSarah Sharp } 1561b356b7c7SSarah Sharp 15621da177e4SLinus Torvalds ret = hub_hub_status(hub, &hubstatus, &hubchange); 15631da177e4SLinus Torvalds if (ret < 0) { 15641da177e4SLinus Torvalds message = "can't get hub status"; 15651da177e4SLinus Torvalds goto fail; 15661da177e4SLinus Torvalds } 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds /* local power status reports aren't always correct */ 15691da177e4SLinus Torvalds if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 15701da177e4SLinus Torvalds dev_dbg(hub_dev, "local power source is %s\n", 15711da177e4SLinus Torvalds (hubstatus & HUB_STATUS_LOCAL_POWER) 15721da177e4SLinus Torvalds ? "lost (inactive)" : "good"); 15731da177e4SLinus Torvalds 157474ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 15751da177e4SLinus Torvalds dev_dbg(hub_dev, "%sover-current condition exists\n", 15761da177e4SLinus Torvalds (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 15771da177e4SLinus Torvalds 157888fafff9Sinaky@linux.intel.com /* set up the interrupt endpoint 157988fafff9Sinaky@linux.intel.com * We use the EP's maxpacket size instead of (PORTS+1+7)/8 158088fafff9Sinaky@linux.intel.com * bytes as USB2.0[11.12.3] says because some hubs are known 158188fafff9Sinaky@linux.intel.com * to send more data (and thus cause overflow). For root hubs, 158288fafff9Sinaky@linux.intel.com * maxpktsize is defined in hcd.c's fake endpoint descriptors 158388fafff9Sinaky@linux.intel.com * to be big enough for at least USB_MAXCHILDREN ports. */ 15841da177e4SLinus Torvalds pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 15851da177e4SLinus Torvalds maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds if (maxp > sizeof(*hub->buffer)) 15881da177e4SLinus Torvalds maxp = sizeof(*hub->buffer); 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds hub->urb = usb_alloc_urb(0, GFP_KERNEL); 15911da177e4SLinus Torvalds if (!hub->urb) { 15921da177e4SLinus Torvalds ret = -ENOMEM; 15931da177e4SLinus Torvalds goto fail; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 15971da177e4SLinus Torvalds hub, endpoint->bInterval); 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds /* maybe cycle the hub leds */ 16001da177e4SLinus Torvalds if (hub->has_indicators && blinkenlights) 16011da177e4SLinus Torvalds hub->indicator [0] = INDICATOR_CYCLE; 16021da177e4SLinus Torvalds 1603fa2a9566SLan Tianyu for (i = 0; i < hdev->maxchild; i++) 1604fa2a9566SLan Tianyu if (usb_hub_create_port_device(hub, i + 1) < 0) 1605fa2a9566SLan Tianyu dev_err(hub->intfdev, 1606fa2a9566SLan Tianyu "couldn't create port%d device.\n", i + 1); 1607fa2a9566SLan Tianyu 1608f2835219SAlan Stern hub_activate(hub, HUB_INIT); 16091da177e4SLinus Torvalds return 0; 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds fail: 16121da177e4SLinus Torvalds dev_err (hub_dev, "config failed, %s (err %d)\n", 16131da177e4SLinus Torvalds message, ret); 16141da177e4SLinus Torvalds /* hub_disconnect() frees urb and descriptor */ 16151da177e4SLinus Torvalds return ret; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 1618e8054854SAlan Stern static void hub_release(struct kref *kref) 1619e8054854SAlan Stern { 1620e8054854SAlan Stern struct usb_hub *hub = container_of(kref, struct usb_hub, kref); 1621e8054854SAlan Stern 1622e8054854SAlan Stern usb_put_intf(to_usb_interface(hub->intfdev)); 1623e8054854SAlan Stern kfree(hub); 1624e8054854SAlan Stern } 1625e8054854SAlan Stern 16261da177e4SLinus Torvalds static unsigned highspeed_hubs; 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf) 16291da177e4SLinus Torvalds { 16301da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 1631fa286188SGreg Kroah-Hartman struct usb_device *hdev = interface_to_usbdev(intf); 1632fa2a9566SLan Tianyu int i; 1633fa2a9566SLan Tianyu 1634e8054854SAlan Stern /* Take the hub off the event list and don't let it be added again */ 1635e8054854SAlan Stern spin_lock_irq(&hub_event_lock); 16368e4ceb38SAlan Stern if (!list_empty(&hub->event_list)) { 1637e8054854SAlan Stern list_del_init(&hub->event_list); 16388e4ceb38SAlan Stern usb_autopm_put_interface_no_suspend(intf); 16398e4ceb38SAlan Stern } 1640e8054854SAlan Stern hub->disconnected = 1; 1641e8054854SAlan Stern spin_unlock_irq(&hub_event_lock); 16421da177e4SLinus Torvalds 16437de18d8bSAlan Stern /* Disconnect all children and quiesce the hub */ 16447de18d8bSAlan Stern hub->error = 0; 16454330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 16467de18d8bSAlan Stern 16478b28c752SAlan Stern usb_set_intfdata (intf, NULL); 16481f2235b8SAlexander Shishkin 16491f2235b8SAlexander Shishkin for (i = 0; i < hdev->maxchild; i++) 16501f2235b8SAlexander Shishkin usb_hub_remove_port_device(hub, i + 1); 16517cbe5dcaSAlan Stern hub->hdev->maxchild = 0; 16521da177e4SLinus Torvalds 1653e8054854SAlan Stern if (hub->hdev->speed == USB_SPEED_HIGH) 16541da177e4SLinus Torvalds highspeed_hubs--; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds usb_free_urb(hub->urb); 1657fa2a9566SLan Tianyu kfree(hub->ports); 16581da177e4SLinus Torvalds kfree(hub->descriptor); 16591da177e4SLinus Torvalds kfree(hub->status); 1660d697cddaSAlan Stern kfree(hub->buffer); 16611da177e4SLinus Torvalds 1662e8054854SAlan Stern kref_put(&hub->kref, hub_release); 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 16661da177e4SLinus Torvalds { 16671da177e4SLinus Torvalds struct usb_host_interface *desc; 16681da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint; 16691da177e4SLinus Torvalds struct usb_device *hdev; 16701da177e4SLinus Torvalds struct usb_hub *hub; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds desc = intf->cur_altsetting; 16731da177e4SLinus Torvalds hdev = interface_to_usbdev(intf); 16741da177e4SLinus Torvalds 1675596d789aSMing Lei /* 1676596d789aSMing Lei * Set default autosuspend delay as 0 to speedup bus suspend, 1677596d789aSMing Lei * based on the below considerations: 1678596d789aSMing Lei * 1679596d789aSMing Lei * - Unlike other drivers, the hub driver does not rely on the 1680596d789aSMing Lei * autosuspend delay to provide enough time to handle a wakeup 1681596d789aSMing Lei * event, and the submitted status URB is just to check future 1682596d789aSMing Lei * change on hub downstream ports, so it is safe to do it. 1683596d789aSMing Lei * 1684596d789aSMing Lei * - The patch might cause one or more auto supend/resume for 1685596d789aSMing Lei * below very rare devices when they are plugged into hub 1686596d789aSMing Lei * first time: 1687596d789aSMing Lei * 1688596d789aSMing Lei * devices having trouble initializing, and disconnect 1689596d789aSMing Lei * themselves from the bus and then reconnect a second 1690596d789aSMing Lei * or so later 1691596d789aSMing Lei * 1692596d789aSMing Lei * devices just for downloading firmware, and disconnects 1693596d789aSMing Lei * themselves after completing it 1694596d789aSMing Lei * 1695596d789aSMing Lei * For these quite rare devices, their drivers may change the 1696596d789aSMing Lei * autosuspend delay of their parent hub in the probe() to one 1697596d789aSMing Lei * appropriate value to avoid the subtle problem if someone 1698596d789aSMing Lei * does care it. 1699596d789aSMing Lei * 1700596d789aSMing Lei * - The patch may cause one or more auto suspend/resume on 1701596d789aSMing Lei * hub during running 'lsusb', but it is probably too 1702596d789aSMing Lei * infrequent to worry about. 1703596d789aSMing Lei * 1704596d789aSMing Lei * - Change autosuspend delay of hub can avoid unnecessary auto 1705596d789aSMing Lei * suspend timer for hub, also may decrease power consumption 1706596d789aSMing Lei * of USB bus. 1707596d789aSMing Lei */ 1708596d789aSMing Lei pm_runtime_set_autosuspend_delay(&hdev->dev, 0); 1709596d789aSMing Lei 17102839f5bcSSarah Sharp /* Hubs have proper suspend/resume support. */ 1711088f7fecSAlan Stern usb_enable_autosuspend(hdev); 1712088f7fecSAlan Stern 171338f3ad5eSFelipe Balbi if (hdev->level == MAX_TOPO_LEVEL) { 1714b9cef6c3SWu Fengguang dev_err(&intf->dev, 1715b9cef6c3SWu Fengguang "Unsupported bus topology: hub nested too deep\n"); 171638f3ad5eSFelipe Balbi return -E2BIG; 171738f3ad5eSFelipe Balbi } 171838f3ad5eSFelipe Balbi 171989ccbdc9SDavid Brownell #ifdef CONFIG_USB_OTG_BLACKLIST_HUB 172089ccbdc9SDavid Brownell if (hdev->parent) { 172189ccbdc9SDavid Brownell dev_warn(&intf->dev, "ignoring external hub\n"); 172289ccbdc9SDavid Brownell return -ENODEV; 172389ccbdc9SDavid Brownell } 172489ccbdc9SDavid Brownell #endif 172589ccbdc9SDavid Brownell 17261da177e4SLinus Torvalds /* Some hubs have a subclass of 1, which AFAICT according to the */ 17271da177e4SLinus Torvalds /* specs is not defined, but it works */ 17281da177e4SLinus Torvalds if ((desc->desc.bInterfaceSubClass != 0) && 17291da177e4SLinus Torvalds (desc->desc.bInterfaceSubClass != 1)) { 17301da177e4SLinus Torvalds descriptor_error: 17311da177e4SLinus Torvalds dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); 17321da177e4SLinus Torvalds return -EIO; 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 17361da177e4SLinus Torvalds if (desc->desc.bNumEndpoints != 1) 17371da177e4SLinus Torvalds goto descriptor_error; 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds endpoint = &desc->endpoint[0].desc; 17401da177e4SLinus Torvalds 1741fbf81c29SLuiz Fernando N. Capitulino /* If it's not an interrupt in endpoint, we'd better punt! */ 1742fbf81c29SLuiz Fernando N. Capitulino if (!usb_endpoint_is_int_in(endpoint)) 17431da177e4SLinus Torvalds goto descriptor_error; 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /* We found a hub */ 17461da177e4SLinus Torvalds dev_info (&intf->dev, "USB hub found\n"); 17471da177e4SLinus Torvalds 17480a1ef3b5SAlan Stern hub = kzalloc(sizeof(*hub), GFP_KERNEL); 17491da177e4SLinus Torvalds if (!hub) { 17501da177e4SLinus Torvalds dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); 17511da177e4SLinus Torvalds return -ENOMEM; 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 1754e8054854SAlan Stern kref_init(&hub->kref); 17551da177e4SLinus Torvalds INIT_LIST_HEAD(&hub->event_list); 17561da177e4SLinus Torvalds hub->intfdev = &intf->dev; 17571da177e4SLinus Torvalds hub->hdev = hdev; 1758c4028958SDavid Howells INIT_DELAYED_WORK(&hub->leds, led_work); 17598520f380SAlan Stern INIT_DELAYED_WORK(&hub->init_work, NULL); 1760e8054854SAlan Stern usb_get_intf(intf); 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds usb_set_intfdata (intf, hub); 176340f122f3SAlan Stern intf->needs_remote_wakeup = 1; 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds if (hdev->speed == USB_SPEED_HIGH) 17661da177e4SLinus Torvalds highspeed_hubs++; 17671da177e4SLinus Torvalds 1768e6f30deaSMing Lei if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) 1769e6f30deaSMing Lei hub->quirk_check_port_auto_suspend = 1; 1770e6f30deaSMing Lei 17711da177e4SLinus Torvalds if (hub_configure(hub, endpoint) >= 0) 17721da177e4SLinus Torvalds return 0; 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds hub_disconnect (intf); 17751da177e4SLinus Torvalds return -ENODEV; 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds static int 17791da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 17801da177e4SLinus Torvalds { 17811da177e4SLinus Torvalds struct usb_device *hdev = interface_to_usbdev (intf); 1782ff823c79SLan Tianyu struct usb_hub *hub = hdev_to_hub(hdev); 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds /* assert ifno == 0 (part of hub spec) */ 17851da177e4SLinus Torvalds switch (code) { 17861da177e4SLinus Torvalds case USBDEVFS_HUB_PORTINFO: { 17871da177e4SLinus Torvalds struct usbdevfs_hub_portinfo *info = user_data; 17881da177e4SLinus Torvalds int i; 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 17911da177e4SLinus Torvalds if (hdev->devnum <= 0) 17921da177e4SLinus Torvalds info->nports = 0; 17931da177e4SLinus Torvalds else { 17941da177e4SLinus Torvalds info->nports = hdev->maxchild; 17951da177e4SLinus Torvalds for (i = 0; i < info->nports; i++) { 1796ff823c79SLan Tianyu if (hub->ports[i]->child == NULL) 17971da177e4SLinus Torvalds info->port[i] = 0; 17981da177e4SLinus Torvalds else 17991da177e4SLinus Torvalds info->port[i] = 1800ff823c79SLan Tianyu hub->ports[i]->child->devnum; 18011da177e4SLinus Torvalds } 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds return info->nports + 1; 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds default: 18091da177e4SLinus Torvalds return -ENOSYS; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 18137cbe5dcaSAlan Stern /* 18147cbe5dcaSAlan Stern * Allow user programs to claim ports on a hub. When a device is attached 18157cbe5dcaSAlan Stern * to one of these "claimed" ports, the program will "own" the device. 18167cbe5dcaSAlan Stern */ 18177cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1, 1818336c5c31SLan Tianyu struct dev_state ***ppowner) 18197cbe5dcaSAlan Stern { 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 18257cbe5dcaSAlan Stern /* This assumes that devices not managed by the hub driver 18267cbe5dcaSAlan Stern * will always have maxchild equal to 0. 18277cbe5dcaSAlan Stern */ 1828fa2a9566SLan Tianyu *ppowner = &(hdev_to_hub(hdev)->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, 1834336c5c31SLan Tianyu struct dev_state *owner) 18357cbe5dcaSAlan Stern { 18367cbe5dcaSAlan Stern int rc; 1837336c5c31SLan Tianyu struct 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 } 18477cbe5dcaSAlan Stern 1848336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 1849336c5c31SLan Tianyu struct dev_state *owner) 18507cbe5dcaSAlan Stern { 18517cbe5dcaSAlan Stern int rc; 1852336c5c31SLan Tianyu struct dev_state **powner; 18537cbe5dcaSAlan Stern 18547cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner); 18557cbe5dcaSAlan Stern if (rc) 18567cbe5dcaSAlan Stern return rc; 18577cbe5dcaSAlan Stern if (*powner != owner) 18587cbe5dcaSAlan Stern return -ENOENT; 18597cbe5dcaSAlan Stern *powner = NULL; 18607cbe5dcaSAlan Stern return rc; 18617cbe5dcaSAlan Stern } 18627cbe5dcaSAlan Stern 1863336c5c31SLan Tianyu void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner) 18647cbe5dcaSAlan Stern { 1865fa2a9566SLan Tianyu struct usb_hub *hub = hdev_to_hub(hdev); 18667cbe5dcaSAlan Stern int n; 18677cbe5dcaSAlan Stern 1868fa2a9566SLan Tianyu for (n = 0; n < hdev->maxchild; n++) { 1869fa2a9566SLan Tianyu if (hub->ports[n]->port_owner == owner) 1870fa2a9566SLan Tianyu hub->ports[n]->port_owner = NULL; 18717cbe5dcaSAlan Stern } 1872fa2a9566SLan Tianyu 18737cbe5dcaSAlan Stern } 18747cbe5dcaSAlan Stern 18757cbe5dcaSAlan Stern /* The caller must hold udev's lock */ 18767cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev) 18777cbe5dcaSAlan Stern { 18787cbe5dcaSAlan Stern struct usb_hub *hub; 18797cbe5dcaSAlan Stern 18807cbe5dcaSAlan Stern if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) 18817cbe5dcaSAlan Stern return false; 18827cbe5dcaSAlan Stern hub = hdev_to_hub(udev->parent); 1883fa2a9566SLan Tianyu return !!hub->ports[udev->portnum - 1]->port_owner; 18847cbe5dcaSAlan Stern } 18857cbe5dcaSAlan Stern 18861da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev) 18871da177e4SLinus Torvalds { 1888ff823c79SLan Tianyu struct usb_hub *hub = hdev_to_hub(udev); 18891da177e4SLinus Torvalds int i; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds for (i = 0; i < udev->maxchild; ++i) { 1892ff823c79SLan Tianyu if (hub->ports[i]->child) 1893ff823c79SLan Tianyu recursively_mark_NOTATTACHED(hub->ports[i]->child); 18941da177e4SLinus Torvalds } 18959bbdf1e0SAlan Stern if (udev->state == USB_STATE_SUSPENDED) 189615123006SSarah Sharp udev->active_duration -= jiffies; 18971da177e4SLinus Torvalds udev->state = USB_STATE_NOTATTACHED; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds /** 19011da177e4SLinus Torvalds * usb_set_device_state - change a device's current state (usbcore, hcds) 19021da177e4SLinus Torvalds * @udev: pointer to device whose state should be changed 19031da177e4SLinus Torvalds * @new_state: new state value to be stored 19041da177e4SLinus Torvalds * 19051da177e4SLinus Torvalds * udev->state is _not_ fully protected by the device lock. Although 19061da177e4SLinus Torvalds * most transitions are made only while holding the lock, the state can 19071da177e4SLinus Torvalds * can change to USB_STATE_NOTATTACHED at almost any time. This 19081da177e4SLinus Torvalds * is so that devices can be marked as disconnected as soon as possible, 19091da177e4SLinus Torvalds * without having to wait for any semaphores to be released. As a result, 19101da177e4SLinus Torvalds * all changes to any device's state must be protected by the 19111da177e4SLinus Torvalds * device_state_lock spinlock. 19121da177e4SLinus Torvalds * 19131da177e4SLinus Torvalds * Once a device has been added to the device tree, all changes to its state 19141da177e4SLinus Torvalds * should be made using this routine. The state should _not_ be set directly. 19151da177e4SLinus Torvalds * 19161da177e4SLinus Torvalds * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 19171da177e4SLinus Torvalds * Otherwise udev->state is set to new_state, and if new_state is 19181da177e4SLinus Torvalds * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 19191da177e4SLinus Torvalds * to USB_STATE_NOTATTACHED. 19201da177e4SLinus Torvalds */ 19211da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev, 19221da177e4SLinus Torvalds enum usb_device_state new_state) 19231da177e4SLinus Torvalds { 19241da177e4SLinus Torvalds unsigned long flags; 19254681b171SRafael J. Wysocki int wakeup = -1; 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds spin_lock_irqsave(&device_state_lock, flags); 19281da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED) 19291da177e4SLinus Torvalds ; /* do nothing */ 1930b94dc6b5SDavid Brownell else if (new_state != USB_STATE_NOTATTACHED) { 1931fb669cc0SDavid Brownell 1932fb669cc0SDavid Brownell /* root hub wakeup capabilities are managed out-of-band 1933fb669cc0SDavid Brownell * and may involve silicon errata ... ignore them here. 1934fb669cc0SDavid Brownell */ 1935fb669cc0SDavid Brownell if (udev->parent) { 1936645daaabSAlan Stern if (udev->state == USB_STATE_SUSPENDED 1937645daaabSAlan Stern || new_state == USB_STATE_SUSPENDED) 1938645daaabSAlan Stern ; /* No change to wakeup settings */ 1939645daaabSAlan Stern else if (new_state == USB_STATE_CONFIGURED) 19404681b171SRafael J. Wysocki wakeup = udev->actconfig->desc.bmAttributes 19414681b171SRafael J. Wysocki & USB_CONFIG_ATT_WAKEUP; 1942645daaabSAlan Stern else 19434681b171SRafael J. Wysocki wakeup = 0; 1944fb669cc0SDavid Brownell } 194515123006SSarah Sharp if (udev->state == USB_STATE_SUSPENDED && 194615123006SSarah Sharp new_state != USB_STATE_SUSPENDED) 194715123006SSarah Sharp udev->active_duration -= jiffies; 194815123006SSarah Sharp else if (new_state == USB_STATE_SUSPENDED && 194915123006SSarah Sharp udev->state != USB_STATE_SUSPENDED) 195015123006SSarah Sharp udev->active_duration += jiffies; 1951645daaabSAlan Stern udev->state = new_state; 1952b94dc6b5SDavid Brownell } else 19531da177e4SLinus Torvalds recursively_mark_NOTATTACHED(udev); 19541da177e4SLinus Torvalds spin_unlock_irqrestore(&device_state_lock, flags); 19554681b171SRafael J. Wysocki if (wakeup >= 0) 19564681b171SRafael J. Wysocki device_set_wakeup_capable(&udev->dev, wakeup); 19571da177e4SLinus Torvalds } 19586da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state); 19591da177e4SLinus Torvalds 19608af548dcSInaky Perez-Gonzalez /* 19613b29b68bSAlan Stern * Choose a device number. 19623b29b68bSAlan Stern * 19633b29b68bSAlan Stern * Device numbers are used as filenames in usbfs. On USB-1.1 and 19643b29b68bSAlan Stern * USB-2.0 buses they are also used as device addresses, however on 19653b29b68bSAlan Stern * USB-3.0 buses the address is assigned by the controller hardware 19663b29b68bSAlan Stern * and it usually is not the same as the device number. 19673b29b68bSAlan Stern * 19688af548dcSInaky Perez-Gonzalez * WUSB devices are simple: they have no hubs behind, so the mapping 19698af548dcSInaky Perez-Gonzalez * device <-> virtual port number becomes 1:1. Why? to simplify the 19708af548dcSInaky Perez-Gonzalez * life of the device connection logic in 19718af548dcSInaky Perez-Gonzalez * drivers/usb/wusbcore/devconnect.c. When we do the initial secret 19728af548dcSInaky Perez-Gonzalez * handshake we need to assign a temporary address in the unauthorized 19738af548dcSInaky Perez-Gonzalez * space. For simplicity we use the first virtual port number found to 19748af548dcSInaky Perez-Gonzalez * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] 19758af548dcSInaky Perez-Gonzalez * and that becomes it's address [X < 128] or its unauthorized address 19768af548dcSInaky Perez-Gonzalez * [X | 0x80]. 19778af548dcSInaky Perez-Gonzalez * 19788af548dcSInaky Perez-Gonzalez * We add 1 as an offset to the one-based USB-stack port number 19798af548dcSInaky Perez-Gonzalez * (zero-based wusb virtual port index) for two reasons: (a) dev addr 19808af548dcSInaky Perez-Gonzalez * 0 is reserved by USB for default address; (b) Linux's USB stack 19818af548dcSInaky Perez-Gonzalez * uses always #1 for the root hub of the controller. So USB stack's 19828af548dcSInaky Perez-Gonzalez * port #1, which is wusb virtual-port #0 has address #2. 1983c6515272SSarah Sharp * 1984c6515272SSarah Sharp * Devices connected under xHCI are not as simple. The host controller 1985c6515272SSarah Sharp * supports virtualization, so the hardware assigns device addresses and 1986c6515272SSarah Sharp * the HCD must setup data structures before issuing a set address 1987c6515272SSarah Sharp * command to the hardware. 19888af548dcSInaky Perez-Gonzalez */ 19893b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev) 19901da177e4SLinus Torvalds { 19911da177e4SLinus Torvalds int devnum; 19921da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds /* If khubd ever becomes multithreaded, this will need a lock */ 19958af548dcSInaky Perez-Gonzalez if (udev->wusb) { 19968af548dcSInaky Perez-Gonzalez devnum = udev->portnum + 1; 19978af548dcSInaky Perez-Gonzalez BUG_ON(test_bit(devnum, bus->devmap.devicemap)); 19988af548dcSInaky Perez-Gonzalez } else { 19998af548dcSInaky Perez-Gonzalez /* Try to allocate the next devnum beginning at 20008af548dcSInaky Perez-Gonzalez * bus->devnum_next. */ 20011da177e4SLinus Torvalds devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 20021da177e4SLinus Torvalds bus->devnum_next); 20031da177e4SLinus Torvalds if (devnum >= 128) 20048af548dcSInaky Perez-Gonzalez devnum = find_next_zero_bit(bus->devmap.devicemap, 20058af548dcSInaky Perez-Gonzalez 128, 1); 20061da177e4SLinus Torvalds bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); 20078af548dcSInaky Perez-Gonzalez } 20081da177e4SLinus Torvalds if (devnum < 128) { 20091da177e4SLinus Torvalds set_bit(devnum, bus->devmap.devicemap); 20101da177e4SLinus Torvalds udev->devnum = devnum; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20143b29b68bSAlan Stern static void release_devnum(struct usb_device *udev) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds if (udev->devnum > 0) { 20171da177e4SLinus Torvalds clear_bit(udev->devnum, udev->bus->devmap.devicemap); 20181da177e4SLinus Torvalds udev->devnum = -1; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds 20223b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum) 20234953d141SDavid Vrabel { 20244953d141SDavid Vrabel /* The address for a WUSB device is managed by wusbcore. */ 20254953d141SDavid Vrabel if (!udev->wusb) 20264953d141SDavid Vrabel udev->devnum = devnum; 20274953d141SDavid Vrabel } 20284953d141SDavid Vrabel 2029f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev) 2030f7410cedSHerbert Xu { 2031f7410cedSHerbert Xu struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2032f7410cedSHerbert Xu 2033f7410cedSHerbert Xu /* Root hubs aren't real devices, so don't free HCD resources */ 2034f7410cedSHerbert Xu if (hcd->driver->free_dev && udev->parent) 2035f7410cedSHerbert Xu hcd->driver->free_dev(hcd, udev); 2036f7410cedSHerbert Xu } 2037f7410cedSHerbert Xu 20381da177e4SLinus Torvalds /** 20391da177e4SLinus Torvalds * usb_disconnect - disconnect a device (usbcore-internal) 20401da177e4SLinus Torvalds * @pdev: pointer to device being disconnected 20411da177e4SLinus Torvalds * Context: !in_interrupt () 20421da177e4SLinus Torvalds * 20431da177e4SLinus Torvalds * Something got disconnected. Get rid of it and all of its children. 20441da177e4SLinus Torvalds * 20451da177e4SLinus Torvalds * If *pdev is a normal device then the parent hub must already be locked. 20461da177e4SLinus Torvalds * If *pdev is a root hub then this routine will acquire the 20471da177e4SLinus Torvalds * usb_bus_list_lock on behalf of the caller. 20481da177e4SLinus Torvalds * 20491da177e4SLinus Torvalds * Only hub drivers (including virtual root hub drivers for host 20501da177e4SLinus Torvalds * controllers) should ever call this. 20511da177e4SLinus Torvalds * 20521da177e4SLinus Torvalds * This call is synchronous, and may not be used in an interrupt context. 20531da177e4SLinus Torvalds */ 20541da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev) 20551da177e4SLinus Torvalds { 20561da177e4SLinus Torvalds struct usb_device *udev = *pdev; 2057ff823c79SLan Tianyu struct usb_hub *hub = hdev_to_hub(udev); 20581da177e4SLinus Torvalds int i; 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds /* mark the device as inactive, so any further urb submissions for 20611da177e4SLinus Torvalds * this device (and any of its children) will fail immediately. 206225985edcSLucas De Marchi * this quiesces everything except pending urbs. 20631da177e4SLinus Torvalds */ 20641da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 20653b29b68bSAlan Stern dev_info(&udev->dev, "USB disconnect, device number %d\n", 20663b29b68bSAlan Stern udev->devnum); 20671da177e4SLinus Torvalds 20689ad3d6ccSAlan Stern usb_lock_device(udev); 20699ad3d6ccSAlan Stern 20701da177e4SLinus Torvalds /* Free up all the children before we remove this device */ 20718816230eSHuajun Li for (i = 0; i < udev->maxchild; i++) { 2072ff823c79SLan Tianyu if (hub->ports[i]->child) 2073ff823c79SLan Tianyu usb_disconnect(&hub->ports[i]->child); 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds /* deallocate hcd/hardware state ... nuking all pending urbs and 20771da177e4SLinus Torvalds * cleaning up all state associated with the current configuration 20781da177e4SLinus Torvalds * so that the hardware is now fully quiesced. 20791da177e4SLinus Torvalds */ 2080782da727SAlan Stern dev_dbg (&udev->dev, "unregistering device\n"); 20811da177e4SLinus Torvalds usb_disable_device(udev, 0); 2082cde217a5SAlan Stern usb_hcd_synchronize_unlinks(udev); 20831da177e4SLinus Torvalds 20843b23dd6fSAlan Stern usb_remove_ep_devs(&udev->ep0); 2085782da727SAlan Stern usb_unlock_device(udev); 20863099e75aSGreg Kroah-Hartman 2087782da727SAlan Stern /* Unregister the device. The device driver is responsible 20883b23dd6fSAlan Stern * for de-configuring the device and invoking the remove-device 20893b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 2090782da727SAlan Stern */ 2091782da727SAlan Stern device_del(&udev->dev); 2092782da727SAlan Stern 2093782da727SAlan Stern /* Free the device number and delete the parent's children[] 20941da177e4SLinus Torvalds * (or root_hub) pointer. 20951da177e4SLinus Torvalds */ 20963b29b68bSAlan Stern release_devnum(udev); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /* Avoid races with recursively_mark_NOTATTACHED() */ 20991da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 21001da177e4SLinus Torvalds *pdev = NULL; 21011da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 21021da177e4SLinus Torvalds 2103f7410cedSHerbert Xu hub_free_dev(udev); 2104f7410cedSHerbert Xu 2105782da727SAlan Stern put_device(&udev->dev); 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds 2108f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 21091da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string) 21101da177e4SLinus Torvalds { 21111da177e4SLinus Torvalds if (!string) 21121da177e4SLinus Torvalds return; 2113f2ec522eSJoe Perches dev_info(&udev->dev, "%s: %s\n", id, string); 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds 2116f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev) 2117f2a383e4SGreg Kroah-Hartman { 2118f2a383e4SGreg Kroah-Hartman dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", 2119f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idVendor), 2120f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idProduct)); 2121b9cef6c3SWu Fengguang dev_info(&udev->dev, 2122b9cef6c3SWu Fengguang "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 2123f2a383e4SGreg Kroah-Hartman udev->descriptor.iManufacturer, 2124f2a383e4SGreg Kroah-Hartman udev->descriptor.iProduct, 2125f2a383e4SGreg Kroah-Hartman udev->descriptor.iSerialNumber); 2126f2a383e4SGreg Kroah-Hartman show_string(udev, "Product", udev->product); 2127f2a383e4SGreg Kroah-Hartman show_string(udev, "Manufacturer", udev->manufacturer); 2128f2a383e4SGreg Kroah-Hartman show_string(udev, "SerialNumber", udev->serial); 2129f2a383e4SGreg Kroah-Hartman } 21301da177e4SLinus Torvalds #else 2131f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { } 21321da177e4SLinus Torvalds #endif 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 21351da177e4SLinus Torvalds #include "otg_whitelist.h" 21361da177e4SLinus Torvalds #endif 21371da177e4SLinus Torvalds 21383ede760fSOliver Neukum /** 21398d8558d1SAlan Stern * usb_enumerate_device_otg - FIXME (usbcore-internal) 21403ede760fSOliver Neukum * @udev: newly addressed device (in ADDRESS state) 21413ede760fSOliver Neukum * 21428d8558d1SAlan Stern * Finish enumeration for On-The-Go devices 21433ede760fSOliver Neukum */ 21448d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev) 21451da177e4SLinus Torvalds { 2146d9d16e8aSInaky Perez-Gonzalez int err = 0; 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 21491da177e4SLinus Torvalds /* 21501da177e4SLinus Torvalds * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 21511da177e4SLinus Torvalds * to wake us after we've powered off VBUS; and HNP, switching roles 21521da177e4SLinus Torvalds * "host" to "peripheral". The OTG descriptor helps figure this out. 21531da177e4SLinus Torvalds */ 21541da177e4SLinus Torvalds if (!udev->bus->is_b_host 21551da177e4SLinus Torvalds && udev->config 21561da177e4SLinus Torvalds && udev->parent == udev->bus->root_hub) { 21572eb5052eSFelipe Balbi struct usb_otg_descriptor *desc = NULL; 21581da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds /* descriptor may appear anywhere in config */ 21611da177e4SLinus Torvalds if (__usb_get_extra_descriptor (udev->rawdescriptors[0], 21621da177e4SLinus Torvalds le16_to_cpu(udev->config[0].desc.wTotalLength), 21631da177e4SLinus Torvalds USB_DT_OTG, (void **) &desc) == 0) { 21641da177e4SLinus Torvalds if (desc->bmAttributes & USB_OTG_HNP) { 216512c3da34SAlan Stern unsigned port1 = udev->portnum; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds dev_info(&udev->dev, 21681da177e4SLinus Torvalds "Dual-Role OTG device on %sHNP port\n", 21691da177e4SLinus Torvalds (port1 == bus->otg_port) 21701da177e4SLinus Torvalds ? "" : "non-"); 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds /* enable HNP before suspend, it's simpler */ 21731da177e4SLinus Torvalds if (port1 == bus->otg_port) 21741da177e4SLinus Torvalds bus->b_hnp_enable = 1; 21751da177e4SLinus Torvalds err = usb_control_msg(udev, 21761da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 21771da177e4SLinus Torvalds USB_REQ_SET_FEATURE, 0, 21781da177e4SLinus Torvalds bus->b_hnp_enable 21791da177e4SLinus Torvalds ? USB_DEVICE_B_HNP_ENABLE 21801da177e4SLinus Torvalds : USB_DEVICE_A_ALT_HNP_SUPPORT, 21811da177e4SLinus Torvalds 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 21821da177e4SLinus Torvalds if (err < 0) { 21831da177e4SLinus Torvalds /* OTG MESSAGE: report errors here, 21841da177e4SLinus Torvalds * customize to match your product. 21851da177e4SLinus Torvalds */ 21861da177e4SLinus Torvalds dev_info(&udev->dev, 2187b9cef6c3SWu Fengguang "can't set HNP mode: %d\n", 21881da177e4SLinus Torvalds err); 21891da177e4SLinus Torvalds bus->b_hnp_enable = 0; 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds if (!is_targeted(udev)) { 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds /* Maybe it can talk to us, though we can't talk to it. 21981da177e4SLinus Torvalds * (Includes HNP test device.) 21991da177e4SLinus Torvalds */ 22001da177e4SLinus Torvalds if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { 2201634a84f8SDavid Brownell err = usb_port_suspend(udev, PMSG_SUSPEND); 22021da177e4SLinus Torvalds if (err < 0) 22031da177e4SLinus Torvalds dev_dbg(&udev->dev, "HNP fail, %d\n", err); 22041da177e4SLinus Torvalds } 2205ffcdc18dSVikram Pandita err = -ENOTSUPP; 22061da177e4SLinus Torvalds goto fail; 22071da177e4SLinus Torvalds } 2208d9d16e8aSInaky Perez-Gonzalez fail: 22091da177e4SLinus Torvalds #endif 2210d9d16e8aSInaky Perez-Gonzalez return err; 2211d9d16e8aSInaky Perez-Gonzalez } 22121da177e4SLinus Torvalds 2213d9d16e8aSInaky Perez-Gonzalez 2214d9d16e8aSInaky Perez-Gonzalez /** 22158d8558d1SAlan Stern * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) 2216d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 2217d9d16e8aSInaky Perez-Gonzalez * 2218d9d16e8aSInaky Perez-Gonzalez * This is only called by usb_new_device() and usb_authorize_device() 2219d9d16e8aSInaky Perez-Gonzalez * and FIXME -- all comments that apply to them apply here wrt to 2220d9d16e8aSInaky Perez-Gonzalez * environment. 2221d9d16e8aSInaky Perez-Gonzalez * 2222d9d16e8aSInaky Perez-Gonzalez * If the device is WUSB and not authorized, we don't attempt to read 2223d9d16e8aSInaky Perez-Gonzalez * the string descriptors, as they will be errored out by the device 2224d9d16e8aSInaky Perez-Gonzalez * until it has been authorized. 2225d9d16e8aSInaky Perez-Gonzalez */ 22268d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev) 2227d9d16e8aSInaky Perez-Gonzalez { 2228d9d16e8aSInaky Perez-Gonzalez int err; 2229d9d16e8aSInaky Perez-Gonzalez 2230d9d16e8aSInaky Perez-Gonzalez if (udev->config == NULL) { 2231d9d16e8aSInaky Perez-Gonzalez err = usb_get_configuration(udev); 2232d9d16e8aSInaky Perez-Gonzalez if (err < 0) { 2233d9d16e8aSInaky Perez-Gonzalez dev_err(&udev->dev, "can't read configurations, error %d\n", 2234d9d16e8aSInaky Perez-Gonzalez err); 223580da2e0dSLaurent Pinchart return err; 2236d9d16e8aSInaky Perez-Gonzalez } 2237d9d16e8aSInaky Perez-Gonzalez } 2238d9d16e8aSInaky Perez-Gonzalez if (udev->wusb == 1 && udev->authorized == 0) { 2239d9d16e8aSInaky Perez-Gonzalez udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2240d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2241d9d16e8aSInaky Perez-Gonzalez udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2242d9d16e8aSInaky Perez-Gonzalez } 2243d9d16e8aSInaky Perez-Gonzalez else { 2244d9d16e8aSInaky Perez-Gonzalez /* read the standard strings and cache them if present */ 2245d9d16e8aSInaky Perez-Gonzalez udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 2246d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = usb_cache_string(udev, 2247d9d16e8aSInaky Perez-Gonzalez udev->descriptor.iManufacturer); 2248d9d16e8aSInaky Perez-Gonzalez udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 2249d9d16e8aSInaky Perez-Gonzalez } 22508d8558d1SAlan Stern err = usb_enumerate_device_otg(udev); 225180da2e0dSLaurent Pinchart if (err < 0) 2252d9d16e8aSInaky Perez-Gonzalez return err; 225380da2e0dSLaurent Pinchart 225480da2e0dSLaurent Pinchart usb_detect_interface_quirks(udev); 225580da2e0dSLaurent Pinchart 225680da2e0dSLaurent Pinchart return 0; 2257d9d16e8aSInaky Perez-Gonzalez } 2258d9d16e8aSInaky Perez-Gonzalez 2259d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev) 2260d35e70d5SMatthew Garrett { 2261d35e70d5SMatthew Garrett struct usb_device *hdev = udev->parent; 2262d35e70d5SMatthew Garrett struct usb_hub *hub; 2263d35e70d5SMatthew Garrett u8 port = udev->portnum; 2264d35e70d5SMatthew Garrett u16 wHubCharacteristics; 2265d35e70d5SMatthew Garrett bool removable = true; 2266d35e70d5SMatthew Garrett 2267d35e70d5SMatthew Garrett if (!hdev) 2268d35e70d5SMatthew Garrett return; 2269d35e70d5SMatthew Garrett 2270d35e70d5SMatthew Garrett hub = hdev_to_hub(udev->parent); 2271d35e70d5SMatthew Garrett 2272d35e70d5SMatthew Garrett wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2273d35e70d5SMatthew Garrett 2274d35e70d5SMatthew Garrett if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 2275d35e70d5SMatthew Garrett return; 2276d35e70d5SMatthew Garrett 2277d35e70d5SMatthew Garrett if (hub_is_superspeed(hdev)) { 2278ca3c1539SLan Tianyu if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) 2279ca3c1539SLan Tianyu & (1 << port)) 2280d35e70d5SMatthew Garrett removable = false; 2281d35e70d5SMatthew Garrett } else { 2282d35e70d5SMatthew Garrett if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 2283d35e70d5SMatthew Garrett removable = false; 2284d35e70d5SMatthew Garrett } 2285d35e70d5SMatthew Garrett 2286d35e70d5SMatthew Garrett if (removable) 2287d35e70d5SMatthew Garrett udev->removable = USB_DEVICE_REMOVABLE; 2288d35e70d5SMatthew Garrett else 2289d35e70d5SMatthew Garrett udev->removable = USB_DEVICE_FIXED; 2290d35e70d5SMatthew Garrett } 2291d9d16e8aSInaky Perez-Gonzalez 2292d9d16e8aSInaky Perez-Gonzalez /** 2293d9d16e8aSInaky Perez-Gonzalez * usb_new_device - perform initial device setup (usbcore-internal) 2294d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 2295d9d16e8aSInaky Perez-Gonzalez * 22968d8558d1SAlan Stern * This is called with devices which have been detected but not fully 22978d8558d1SAlan Stern * enumerated. The device descriptor is available, but not descriptors 2298d9d16e8aSInaky Perez-Gonzalez * for any device configuration. The caller must have locked either 2299d9d16e8aSInaky Perez-Gonzalez * the parent hub (if udev is a normal device) or else the 2300d9d16e8aSInaky Perez-Gonzalez * usb_bus_list_lock (if udev is a root hub). The parent's pointer to 2301d9d16e8aSInaky Perez-Gonzalez * udev has already been installed, but udev is not yet visible through 2302d9d16e8aSInaky Perez-Gonzalez * sysfs or other filesystem code. 2303d9d16e8aSInaky Perez-Gonzalez * 2304d9d16e8aSInaky Perez-Gonzalez * It will return if the device is configured properly or not. Zero if 2305d9d16e8aSInaky Perez-Gonzalez * the interface was registered with the driver core; else a negative 2306d9d16e8aSInaky Perez-Gonzalez * errno value. 2307d9d16e8aSInaky Perez-Gonzalez * 2308d9d16e8aSInaky Perez-Gonzalez * This call is synchronous, and may not be used in an interrupt context. 2309d9d16e8aSInaky Perez-Gonzalez * 2310d9d16e8aSInaky Perez-Gonzalez * Only the hub driver or root-hub registrar should ever call this. 2311d9d16e8aSInaky Perez-Gonzalez */ 2312d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev) 2313d9d16e8aSInaky Perez-Gonzalez { 2314d9d16e8aSInaky Perez-Gonzalez int err; 2315d9d16e8aSInaky Perez-Gonzalez 231616985408SDan Streetman if (udev->parent) { 231716985408SDan Streetman /* Initialize non-root-hub device wakeup to disabled; 231816985408SDan Streetman * device (un)configuration controls wakeup capable 231916985408SDan Streetman * sysfs power/wakeup controls wakeup enabled/disabled 232016985408SDan Streetman */ 232116985408SDan Streetman device_init_wakeup(&udev->dev, 0); 232216985408SDan Streetman } 232316985408SDan Streetman 23249bbdf1e0SAlan Stern /* Tell the runtime-PM framework the device is active */ 23259bbdf1e0SAlan Stern pm_runtime_set_active(&udev->dev); 2326c08512c7SAlan Stern pm_runtime_get_noresume(&udev->dev); 2327fcc4a01eSAlan Stern pm_runtime_use_autosuspend(&udev->dev); 23289bbdf1e0SAlan Stern pm_runtime_enable(&udev->dev); 23299bbdf1e0SAlan Stern 2330c08512c7SAlan Stern /* By default, forbid autosuspend for all devices. It will be 2331c08512c7SAlan Stern * allowed for hubs during binding. 2332c08512c7SAlan Stern */ 2333c08512c7SAlan Stern usb_disable_autosuspend(udev); 2334c08512c7SAlan Stern 23358d8558d1SAlan Stern err = usb_enumerate_device(udev); /* Read descriptors */ 2336d9d16e8aSInaky Perez-Gonzalez if (err < 0) 2337d9d16e8aSInaky Perez-Gonzalez goto fail; 2338c6515272SSarah Sharp dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 2339c6515272SSarah Sharp udev->devnum, udev->bus->busnum, 2340c6515272SSarah Sharp (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 23419f8b17e6SKay Sievers /* export the usbdev device-node for libusb */ 23429f8b17e6SKay Sievers udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 23439f8b17e6SKay Sievers (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 23449f8b17e6SKay Sievers 23456cd13201SAlan Stern /* Tell the world! */ 23466cd13201SAlan Stern announce_device(udev); 2347195af2ccSAlan Stern 2348b04b3156STheodore Ts'o if (udev->serial) 2349b04b3156STheodore Ts'o add_device_randomness(udev->serial, strlen(udev->serial)); 2350b04b3156STheodore Ts'o if (udev->product) 2351b04b3156STheodore Ts'o add_device_randomness(udev->product, strlen(udev->product)); 2352b04b3156STheodore Ts'o if (udev->manufacturer) 2353b04b3156STheodore Ts'o add_device_randomness(udev->manufacturer, 2354b04b3156STheodore Ts'o strlen(udev->manufacturer)); 2355b04b3156STheodore Ts'o 2356927bc916SRafael J. Wysocki device_enable_async_suspend(&udev->dev); 2357d35e70d5SMatthew Garrett 2358d35e70d5SMatthew Garrett /* 2359d35e70d5SMatthew Garrett * check whether the hub marks this port as non-removable. Do it 2360d35e70d5SMatthew Garrett * now so that platform-specific data can override it in 2361d35e70d5SMatthew Garrett * device_add() 2362d35e70d5SMatthew Garrett */ 2363d35e70d5SMatthew Garrett if (udev->parent) 2364d35e70d5SMatthew Garrett set_usb_port_removable(udev); 2365d35e70d5SMatthew Garrett 2366782da727SAlan Stern /* Register the device. The device driver is responsible 23673b23dd6fSAlan Stern * for configuring the device and invoking the add-device 23683b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 2369782da727SAlan Stern */ 23701da177e4SLinus Torvalds err = device_add(&udev->dev); 23711da177e4SLinus Torvalds if (err) { 23721da177e4SLinus Torvalds dev_err(&udev->dev, "can't device_add, error %d\n", err); 23731da177e4SLinus Torvalds goto fail; 23741da177e4SLinus Torvalds } 23759ad3d6ccSAlan Stern 23763b23dd6fSAlan Stern (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2377c08512c7SAlan Stern usb_mark_last_busy(udev); 2378c08512c7SAlan Stern pm_runtime_put_sync_autosuspend(&udev->dev); 2379c066475eSGreg Kroah-Hartman return err; 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds fail: 23821da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 23839bbdf1e0SAlan Stern pm_runtime_disable(&udev->dev); 23849bbdf1e0SAlan Stern pm_runtime_set_suspended(&udev->dev); 2385d9d16e8aSInaky Perez-Gonzalez return err; 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds 238893993a0aSInaky Perez-Gonzalez 238993993a0aSInaky Perez-Gonzalez /** 2390fd39c86bSRandy Dunlap * usb_deauthorize_device - deauthorize a device (usbcore-internal) 2391fd39c86bSRandy Dunlap * @usb_dev: USB device 2392fd39c86bSRandy Dunlap * 2393fd39c86bSRandy Dunlap * Move the USB device to a very basic state where interfaces are disabled 2394fd39c86bSRandy Dunlap * and the device is in fact unconfigured and unusable. 239593993a0aSInaky Perez-Gonzalez * 239693993a0aSInaky Perez-Gonzalez * We share a lock (that we have) with device_del(), so we need to 239793993a0aSInaky Perez-Gonzalez * defer its call. 239893993a0aSInaky Perez-Gonzalez */ 239993993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev) 240093993a0aSInaky Perez-Gonzalez { 240193993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 240293993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 0) 240393993a0aSInaky Perez-Gonzalez goto out_unauthorized; 2404da307123SAlan Stern 240593993a0aSInaky Perez-Gonzalez usb_dev->authorized = 0; 240693993a0aSInaky Perez-Gonzalez usb_set_configuration(usb_dev, -1); 2407da307123SAlan Stern 2408da307123SAlan Stern kfree(usb_dev->product); 240993993a0aSInaky Perez-Gonzalez usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2410da307123SAlan Stern kfree(usb_dev->manufacturer); 241193993a0aSInaky Perez-Gonzalez usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2412da307123SAlan Stern kfree(usb_dev->serial); 241393993a0aSInaky Perez-Gonzalez usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2414da307123SAlan Stern 2415da307123SAlan Stern usb_destroy_configuration(usb_dev); 241693993a0aSInaky Perez-Gonzalez usb_dev->descriptor.bNumConfigurations = 0; 2417da307123SAlan Stern 241893993a0aSInaky Perez-Gonzalez out_unauthorized: 241993993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); 242093993a0aSInaky Perez-Gonzalez return 0; 242193993a0aSInaky Perez-Gonzalez } 242293993a0aSInaky Perez-Gonzalez 242393993a0aSInaky Perez-Gonzalez 242493993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev) 242593993a0aSInaky Perez-Gonzalez { 242693993a0aSInaky Perez-Gonzalez int result = 0, c; 2427da307123SAlan Stern 242893993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 242993993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 1) 243093993a0aSInaky Perez-Gonzalez goto out_authorized; 2431da307123SAlan Stern 243293993a0aSInaky Perez-Gonzalez result = usb_autoresume_device(usb_dev); 243393993a0aSInaky Perez-Gonzalez if (result < 0) { 243493993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 243593993a0aSInaky Perez-Gonzalez "can't autoresume for authorization: %d\n", result); 243693993a0aSInaky Perez-Gonzalez goto error_autoresume; 243793993a0aSInaky Perez-Gonzalez } 243893993a0aSInaky Perez-Gonzalez result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); 243993993a0aSInaky Perez-Gonzalez if (result < 0) { 244093993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, "can't re-read device descriptor for " 244193993a0aSInaky Perez-Gonzalez "authorization: %d\n", result); 244293993a0aSInaky Perez-Gonzalez goto error_device_descriptor; 244393993a0aSInaky Perez-Gonzalez } 2444da307123SAlan Stern 2445da307123SAlan Stern kfree(usb_dev->product); 2446da307123SAlan Stern usb_dev->product = NULL; 2447da307123SAlan Stern kfree(usb_dev->manufacturer); 2448da307123SAlan Stern usb_dev->manufacturer = NULL; 2449da307123SAlan Stern kfree(usb_dev->serial); 2450da307123SAlan Stern usb_dev->serial = NULL; 2451da307123SAlan Stern 245293993a0aSInaky Perez-Gonzalez usb_dev->authorized = 1; 24538d8558d1SAlan Stern result = usb_enumerate_device(usb_dev); 245493993a0aSInaky Perez-Gonzalez if (result < 0) 24558d8558d1SAlan Stern goto error_enumerate; 245693993a0aSInaky Perez-Gonzalez /* Choose and set the configuration. This registers the interfaces 245793993a0aSInaky Perez-Gonzalez * with the driver core and lets interface drivers bind to them. 245893993a0aSInaky Perez-Gonzalez */ 2459b5ea060fSGreg Kroah-Hartman c = usb_choose_configuration(usb_dev); 246093993a0aSInaky Perez-Gonzalez if (c >= 0) { 246193993a0aSInaky Perez-Gonzalez result = usb_set_configuration(usb_dev, c); 246293993a0aSInaky Perez-Gonzalez if (result) { 246393993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 246493993a0aSInaky Perez-Gonzalez "can't set config #%d, error %d\n", c, result); 246593993a0aSInaky Perez-Gonzalez /* This need not be fatal. The user can try to 246693993a0aSInaky Perez-Gonzalez * set other configurations. */ 246793993a0aSInaky Perez-Gonzalez } 246893993a0aSInaky Perez-Gonzalez } 246993993a0aSInaky Perez-Gonzalez dev_info(&usb_dev->dev, "authorized to connect\n"); 2470da307123SAlan Stern 24718d8558d1SAlan Stern error_enumerate: 247293993a0aSInaky Perez-Gonzalez error_device_descriptor: 2473da307123SAlan Stern usb_autosuspend_device(usb_dev); 247493993a0aSInaky Perez-Gonzalez error_autoresume: 247593993a0aSInaky Perez-Gonzalez out_authorized: 247693993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); // complements locktree 247793993a0aSInaky Perez-Gonzalez return result; 247893993a0aSInaky Perez-Gonzalez } 247993993a0aSInaky Perez-Gonzalez 248093993a0aSInaky Perez-Gonzalez 24810165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ 24820165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub) 24830165de09SInaky Perez-Gonzalez { 24840165de09SInaky Perez-Gonzalez struct usb_hcd *hcd; 24850165de09SInaky Perez-Gonzalez if (hub->hdev->parent != NULL) /* not a root hub? */ 24860165de09SInaky Perez-Gonzalez return 0; 24870165de09SInaky Perez-Gonzalez hcd = container_of(hub->hdev->bus, struct usb_hcd, self); 24880165de09SInaky Perez-Gonzalez return hcd->wireless; 24890165de09SInaky Perez-Gonzalez } 24900165de09SInaky Perez-Gonzalez 24910165de09SInaky Perez-Gonzalez 24921da177e4SLinus Torvalds #define PORT_RESET_TRIES 5 24931da177e4SLinus Torvalds #define SET_ADDRESS_TRIES 2 24941da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES 2 24951da177e4SLinus Torvalds #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) 249690ab5ee9SRusty Russell #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 24991da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME 10 250075d7cf72SAndiry Xu #define HUB_BH_RESET_TIME 50 25011da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME 200 250277c7f072SSarah Sharp #define HUB_RESET_TIMEOUT 800 25031da177e4SLinus Torvalds 250410d674a8SSarah Sharp static int hub_port_reset(struct usb_hub *hub, int port1, 250510d674a8SSarah Sharp struct usb_device *udev, unsigned int delay, bool warm); 250610d674a8SSarah Sharp 25078bea2bd3SStanislaw Ledwon /* Is a USB 3.0 port in the Inactive or Complinance Mode state? 25088bea2bd3SStanislaw Ledwon * Port worm reset is required to recover 25098bea2bd3SStanislaw Ledwon */ 25108bea2bd3SStanislaw Ledwon static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus) 251110d674a8SSarah Sharp { 251210d674a8SSarah Sharp return hub_is_superspeed(hub->hdev) && 25138bea2bd3SStanislaw Ledwon (((portstatus & USB_PORT_STAT_LINK_STATE) == 25148bea2bd3SStanislaw Ledwon USB_SS_PORT_LS_SS_INACTIVE) || 25158bea2bd3SStanislaw Ledwon ((portstatus & USB_PORT_STAT_LINK_STATE) == 25168bea2bd3SStanislaw Ledwon USB_SS_PORT_LS_COMP_MOD)) ; 251710d674a8SSarah Sharp } 251810d674a8SSarah Sharp 25191da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1, 252075d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm) 25211da177e4SLinus Torvalds { 25221da177e4SLinus Torvalds int delay_time, ret; 25231da177e4SLinus Torvalds u16 portstatus; 25241da177e4SLinus Torvalds u16 portchange; 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds for (delay_time = 0; 25271da177e4SLinus Torvalds delay_time < HUB_RESET_TIMEOUT; 25281da177e4SLinus Torvalds delay_time += delay) { 25291da177e4SLinus Torvalds /* wait to give the device a chance to reset */ 25301da177e4SLinus Torvalds msleep(delay); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds /* read and decode port status */ 25331da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 25341da177e4SLinus Torvalds if (ret < 0) 25351da177e4SLinus Torvalds return ret; 25361da177e4SLinus Torvalds 25374f43447eSSarah Sharp /* The port state is unknown until the reset completes. */ 25384f43447eSSarah Sharp if ((portstatus & USB_PORT_STAT_RESET)) 25394f43447eSSarah Sharp goto delay; 25404f43447eSSarah Sharp 2541a24a6078SSarah Sharp if (hub_port_warm_reset_required(hub, portstatus)) 2542a24a6078SSarah Sharp return -ENOTCONN; 254310d674a8SSarah Sharp 25441da177e4SLinus Torvalds /* Device went away? */ 25451da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_CONNECTION)) 25461da177e4SLinus Torvalds return -ENOTCONN; 25471da177e4SLinus Torvalds 2548a24a6078SSarah Sharp /* bomb out completely if the connection bounced. A USB 3.0 2549a24a6078SSarah Sharp * connection may bounce if multiple warm resets were issued, 2550a24a6078SSarah Sharp * but the device may have successfully re-connected. Ignore it. 2551a24a6078SSarah Sharp */ 2552a24a6078SSarah Sharp if (!hub_is_superspeed(hub->hdev) && 2553a24a6078SSarah Sharp (portchange & USB_PORT_STAT_C_CONNECTION)) 2554dd4dd19eSAlan Stern return -ENOTCONN; 25551da177e4SLinus Torvalds 25564f43447eSSarah Sharp if ((portstatus & USB_PORT_STAT_ENABLE)) { 25572d4fa940SSarah Sharp if (!udev) 25582d4fa940SSarah Sharp return 0; 25592d4fa940SSarah Sharp 25600165de09SInaky Perez-Gonzalez if (hub_is_wusb(hub)) 2561551cdbbeSGreg Kroah-Hartman udev->speed = USB_SPEED_WIRELESS; 2562131dec34SSarah Sharp else if (hub_is_superspeed(hub->hdev)) 2563809cd1cbSSarah Sharp udev->speed = USB_SPEED_SUPER; 25640165de09SInaky Perez-Gonzalez else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 25651da177e4SLinus Torvalds udev->speed = USB_SPEED_HIGH; 25661da177e4SLinus Torvalds else if (portstatus & USB_PORT_STAT_LOW_SPEED) 25671da177e4SLinus Torvalds udev->speed = USB_SPEED_LOW; 25681da177e4SLinus Torvalds else 25691da177e4SLinus Torvalds udev->speed = USB_SPEED_FULL; 25701da177e4SLinus Torvalds return 0; 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds 25734f43447eSSarah Sharp delay: 25741da177e4SLinus Torvalds /* switch to the long delay after two short delay failures */ 25751da177e4SLinus Torvalds if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 25761da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds dev_dbg (hub->intfdev, 257975d7cf72SAndiry Xu "port %d not %sreset yet, waiting %dms\n", 258075d7cf72SAndiry Xu port1, warm ? "warm " : "", delay); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds return -EBUSY; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 258675d7cf72SAndiry Xu static void hub_port_finish_reset(struct usb_hub *hub, int port1, 2587a24a6078SSarah Sharp struct usb_device *udev, int *status) 25881da177e4SLinus Torvalds { 258975d7cf72SAndiry Xu switch (*status) { 25901da177e4SLinus Torvalds case 0: 2591b789696aSDavid Brownell /* TRSTRCY = 10 ms; plus some extra */ 2592b789696aSDavid Brownell msleep(10 + 40); 25932d4fa940SSarah Sharp if (udev) { 2594a24a6078SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2595a24a6078SSarah Sharp 25963b29b68bSAlan Stern update_devnum(udev, 0); 2597a24a6078SSarah Sharp /* The xHC may think the device is already reset, 2598a24a6078SSarah Sharp * so ignore the status. 25998b8132bcSSarah Sharp */ 26008b8132bcSSarah Sharp if (hcd->driver->reset_device) 26018b8132bcSSarah Sharp hcd->driver->reset_device(hcd, udev); 260275d7cf72SAndiry Xu } 26031da177e4SLinus Torvalds /* FALL THROUGH */ 26041da177e4SLinus Torvalds case -ENOTCONN: 26051da177e4SLinus Torvalds case -ENODEV: 26061da177e4SLinus Torvalds clear_port_feature(hub->hdev, 26071da177e4SLinus Torvalds port1, USB_PORT_FEAT_C_RESET); 26081da177e4SLinus Torvalds /* FIXME need disconnect() for NOTATTACHED device */ 26091c7439c6SSarah Sharp if (hub_is_superspeed(hub->hdev)) { 261075d7cf72SAndiry Xu clear_port_feature(hub->hdev, port1, 261175d7cf72SAndiry Xu USB_PORT_FEAT_C_BH_PORT_RESET); 261275d7cf72SAndiry Xu clear_port_feature(hub->hdev, port1, 261375d7cf72SAndiry Xu USB_PORT_FEAT_C_PORT_LINK_STATE); 2614a24a6078SSarah Sharp clear_port_feature(hub->hdev, port1, 2615a24a6078SSarah Sharp USB_PORT_FEAT_C_CONNECTION); 26161c7439c6SSarah Sharp } 2617a24a6078SSarah Sharp if (udev) 261875d7cf72SAndiry Xu usb_set_device_state(udev, *status 26191da177e4SLinus Torvalds ? USB_STATE_NOTATTACHED 26201da177e4SLinus Torvalds : USB_STATE_DEFAULT); 262175d7cf72SAndiry Xu break; 262275d7cf72SAndiry Xu } 262375d7cf72SAndiry Xu } 262475d7cf72SAndiry Xu 262575d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 262675d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1, 262775d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm) 262875d7cf72SAndiry Xu { 262975d7cf72SAndiry Xu int i, status; 2630a24a6078SSarah Sharp u16 portchange, portstatus; 263175d7cf72SAndiry Xu 263275d7cf72SAndiry Xu if (!hub_is_superspeed(hub->hdev)) { 26330fe51aa5SSarah Sharp if (warm) { 263475d7cf72SAndiry Xu dev_err(hub->intfdev, "only USB3 hub support " 263575d7cf72SAndiry Xu "warm reset\n"); 263675d7cf72SAndiry Xu return -EINVAL; 263775d7cf72SAndiry Xu } 26380fe51aa5SSarah Sharp /* Block EHCI CF initialization during the port reset. 26390fe51aa5SSarah Sharp * Some companion controllers don't like it when they mix. 26400fe51aa5SSarah Sharp */ 26410fe51aa5SSarah Sharp down_read(&ehci_cf_port_reset_rwsem); 264275d7cf72SAndiry Xu } 264375d7cf72SAndiry Xu 264475d7cf72SAndiry Xu /* Reset the port */ 264575d7cf72SAndiry Xu for (i = 0; i < PORT_RESET_TRIES; i++) { 264675d7cf72SAndiry Xu status = set_port_feature(hub->hdev, port1, (warm ? 264775d7cf72SAndiry Xu USB_PORT_FEAT_BH_PORT_RESET : 264875d7cf72SAndiry Xu USB_PORT_FEAT_RESET)); 264975d7cf72SAndiry Xu if (status) { 265075d7cf72SAndiry Xu dev_err(hub->intfdev, 265175d7cf72SAndiry Xu "cannot %sreset port %d (err = %d)\n", 265275d7cf72SAndiry Xu warm ? "warm " : "", port1, status); 265375d7cf72SAndiry Xu } else { 265475d7cf72SAndiry Xu status = hub_port_wait_reset(hub, port1, udev, delay, 265575d7cf72SAndiry Xu warm); 265675d7cf72SAndiry Xu if (status && status != -ENOTCONN) 265775d7cf72SAndiry Xu dev_dbg(hub->intfdev, 265875d7cf72SAndiry Xu "port_wait_reset: err = %d\n", 265975d7cf72SAndiry Xu status); 266075d7cf72SAndiry Xu } 266175d7cf72SAndiry Xu 2662a24a6078SSarah Sharp /* Check for disconnect or reset */ 266375d7cf72SAndiry Xu if (status == 0 || status == -ENOTCONN || status == -ENODEV) { 2664a24a6078SSarah Sharp hub_port_finish_reset(hub, port1, udev, &status); 2665a24a6078SSarah Sharp 2666a24a6078SSarah Sharp if (!hub_is_superspeed(hub->hdev)) 266732fe0198SAlan Stern goto done; 2668a24a6078SSarah Sharp 2669a24a6078SSarah Sharp /* 2670a24a6078SSarah Sharp * If a USB 3.0 device migrates from reset to an error 2671a24a6078SSarah Sharp * state, re-issue the warm reset. 2672a24a6078SSarah Sharp */ 2673a24a6078SSarah Sharp if (hub_port_status(hub, port1, 2674a24a6078SSarah Sharp &portstatus, &portchange) < 0) 2675a24a6078SSarah Sharp goto done; 2676a24a6078SSarah Sharp 2677a24a6078SSarah Sharp if (!hub_port_warm_reset_required(hub, portstatus)) 2678a24a6078SSarah Sharp goto done; 2679a24a6078SSarah Sharp 2680a24a6078SSarah Sharp /* 2681a24a6078SSarah Sharp * If the port is in SS.Inactive or Compliance Mode, the 2682a24a6078SSarah Sharp * hot or warm reset failed. Try another warm reset. 2683a24a6078SSarah Sharp */ 2684a24a6078SSarah Sharp if (!warm) { 2685a24a6078SSarah Sharp dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n", 2686a24a6078SSarah Sharp port1); 2687a24a6078SSarah Sharp warm = true; 2688a24a6078SSarah Sharp } 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds dev_dbg (hub->intfdev, 269275d7cf72SAndiry Xu "port %d not enabled, trying %sreset again...\n", 269375d7cf72SAndiry Xu port1, warm ? "warm " : ""); 26941da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 26951da177e4SLinus Torvalds } 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds dev_err (hub->intfdev, 26981da177e4SLinus Torvalds "Cannot enable port %i. Maybe the USB cable is bad?\n", 26991da177e4SLinus Torvalds port1); 27001da177e4SLinus Torvalds 270132fe0198SAlan Stern done: 27020fe51aa5SSarah Sharp if (!hub_is_superspeed(hub->hdev)) 270332fe0198SAlan Stern up_read(&ehci_cf_port_reset_rwsem); 270475d7cf72SAndiry Xu 27051da177e4SLinus Torvalds return status; 27061da177e4SLinus Torvalds } 27071da177e4SLinus Torvalds 27080ed9a57eSAndiry Xu /* Check if a port is power on */ 27090ed9a57eSAndiry Xu static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) 27100ed9a57eSAndiry Xu { 27110ed9a57eSAndiry Xu int ret = 0; 27120ed9a57eSAndiry Xu 27130ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) { 27140ed9a57eSAndiry Xu if (portstatus & USB_SS_PORT_STAT_POWER) 27150ed9a57eSAndiry Xu ret = 1; 27160ed9a57eSAndiry Xu } else { 27170ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_POWER) 27180ed9a57eSAndiry Xu ret = 1; 27190ed9a57eSAndiry Xu } 27200ed9a57eSAndiry Xu 27210ed9a57eSAndiry Xu return ret; 27220ed9a57eSAndiry Xu } 27230ed9a57eSAndiry Xu 2724d388dab7SAlan Stern #ifdef CONFIG_PM 27251da177e4SLinus Torvalds 27260ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 27270ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 27280ed9a57eSAndiry Xu { 27290ed9a57eSAndiry Xu int ret = 0; 27300ed9a57eSAndiry Xu 27310ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) { 27320ed9a57eSAndiry Xu if ((portstatus & USB_PORT_STAT_LINK_STATE) 27330ed9a57eSAndiry Xu == USB_SS_PORT_LS_U3) 27340ed9a57eSAndiry Xu ret = 1; 27350ed9a57eSAndiry Xu } else { 27360ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_SUSPEND) 27370ed9a57eSAndiry Xu ret = 1; 27380ed9a57eSAndiry Xu } 27390ed9a57eSAndiry Xu 27400ed9a57eSAndiry Xu return ret; 27410ed9a57eSAndiry Xu } 2742b01b03f3SAlan Stern 2743b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume, 2744b01b03f3SAlan Stern * is ready for a reset-resume, or should be disconnected. 2745b01b03f3SAlan Stern */ 2746b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev, 2747b01b03f3SAlan Stern struct usb_hub *hub, int port1, 2748b01b03f3SAlan Stern int status, unsigned portchange, unsigned portstatus) 2749b01b03f3SAlan Stern { 2750b01b03f3SAlan Stern /* Is the device still present? */ 27510ed9a57eSAndiry Xu if (status || port_is_suspended(hub, portstatus) || 27520ed9a57eSAndiry Xu !port_is_power_on(hub, portstatus) || 27530ed9a57eSAndiry Xu !(portstatus & USB_PORT_STAT_CONNECTION)) { 2754b01b03f3SAlan Stern if (status >= 0) 2755b01b03f3SAlan Stern status = -ENODEV; 2756b01b03f3SAlan Stern } 2757b01b03f3SAlan Stern 275886c57edfSAlan Stern /* Can't do a normal resume if the port isn't enabled, 275986c57edfSAlan Stern * so try a reset-resume instead. 276086c57edfSAlan Stern */ 276186c57edfSAlan Stern else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 276286c57edfSAlan Stern if (udev->persist_enabled) 276386c57edfSAlan Stern udev->reset_resume = 1; 276486c57edfSAlan Stern else 2765b01b03f3SAlan Stern status = -ENODEV; 276686c57edfSAlan Stern } 2767b01b03f3SAlan Stern 2768b01b03f3SAlan Stern if (status) { 2769b01b03f3SAlan Stern dev_dbg(hub->intfdev, 2770b01b03f3SAlan Stern "port %d status %04x.%04x after resume, %d\n", 2771b01b03f3SAlan Stern port1, portchange, portstatus, status); 2772b01b03f3SAlan Stern } else if (udev->reset_resume) { 2773b01b03f3SAlan Stern 2774b01b03f3SAlan Stern /* Late port handoff can set status-change bits */ 2775b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) 2776b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 2777b01b03f3SAlan Stern USB_PORT_FEAT_C_CONNECTION); 2778b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) 2779b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 2780b01b03f3SAlan Stern USB_PORT_FEAT_C_ENABLE); 2781b01b03f3SAlan Stern } 2782b01b03f3SAlan Stern 2783b01b03f3SAlan Stern return status; 2784b01b03f3SAlan Stern } 2785b01b03f3SAlan Stern 2786f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev) 2787f74631e3SSarah Sharp { 2788f74631e3SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2789f74631e3SSarah Sharp 2790f74631e3SSarah Sharp /* Check if the roothub and device supports LTM. */ 2791f74631e3SSarah Sharp if (!usb_device_supports_ltm(hcd->self.root_hub) || 2792f74631e3SSarah Sharp !usb_device_supports_ltm(udev)) 2793f74631e3SSarah Sharp return 0; 2794f74631e3SSarah Sharp 2795f74631e3SSarah Sharp /* Clear Feature LTM Enable can only be sent if the device is 2796f74631e3SSarah Sharp * configured. 2797f74631e3SSarah Sharp */ 2798f74631e3SSarah Sharp if (!udev->actconfig) 2799f74631e3SSarah Sharp return 0; 2800f74631e3SSarah Sharp 2801f74631e3SSarah Sharp return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2802f74631e3SSarah Sharp USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 2803f74631e3SSarah Sharp USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 2804f74631e3SSarah Sharp USB_CTRL_SET_TIMEOUT); 2805f74631e3SSarah Sharp } 2806f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm); 2807f74631e3SSarah Sharp 2808f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) 2809f74631e3SSarah Sharp { 2810f74631e3SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2811f74631e3SSarah Sharp 2812f74631e3SSarah Sharp /* Check if the roothub and device supports LTM. */ 2813f74631e3SSarah Sharp if (!usb_device_supports_ltm(hcd->self.root_hub) || 2814f74631e3SSarah Sharp !usb_device_supports_ltm(udev)) 2815f74631e3SSarah Sharp return; 2816f74631e3SSarah Sharp 2817f74631e3SSarah Sharp /* Set Feature LTM Enable can only be sent if the device is 2818f74631e3SSarah Sharp * configured. 2819f74631e3SSarah Sharp */ 2820f74631e3SSarah Sharp if (!udev->actconfig) 2821f74631e3SSarah Sharp return; 2822f74631e3SSarah Sharp 2823f74631e3SSarah Sharp usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2824f74631e3SSarah Sharp USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 2825f74631e3SSarah Sharp USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 2826f74631e3SSarah Sharp USB_CTRL_SET_TIMEOUT); 2827f74631e3SSarah Sharp } 2828f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm); 2829f74631e3SSarah Sharp 28301da177e4SLinus Torvalds #ifdef CONFIG_USB_SUSPEND 28311da177e4SLinus Torvalds 28321da177e4SLinus Torvalds /* 2833140d8f68SAlan Stern * usb_port_suspend - suspend a usb device's upstream port 2834624d6c07SAlan Stern * @udev: device that's no longer in active use, not a root hub 28355edbfb7cSDavid Brownell * Context: must be able to sleep; device not locked; pm locks held 28361da177e4SLinus Torvalds * 28371da177e4SLinus Torvalds * Suspends a USB device that isn't in active use, conserving power. 28381da177e4SLinus Torvalds * Devices may wake out of a suspend, if anything important happens, 28391da177e4SLinus Torvalds * using the remote wakeup mechanism. They may also be taken out of 2840140d8f68SAlan Stern * suspend by the host, using usb_port_resume(). It's also routine 28411da177e4SLinus Torvalds * to disconnect devices while they are suspended. 28421da177e4SLinus Torvalds * 2843390a8c34SDavid Brownell * This only affects the USB hardware for a device; its interfaces 2844390a8c34SDavid Brownell * (and, for hubs, child devices) must already have been suspended. 2845390a8c34SDavid Brownell * 2846624d6c07SAlan Stern * Selective port suspend reduces power; most suspended devices draw 2847624d6c07SAlan Stern * less than 500 uA. It's also used in OTG, along with remote wakeup. 2848624d6c07SAlan Stern * All devices below the suspended port are also suspended. 2849624d6c07SAlan Stern * 2850624d6c07SAlan Stern * Devices leave suspend state when the host wakes them up. Some devices 2851624d6c07SAlan Stern * also support "remote wakeup", where the device can activate the USB 2852624d6c07SAlan Stern * tree above them to deliver data, such as a keypress or packet. In 2853624d6c07SAlan Stern * some cases, this wakes the USB host. 2854624d6c07SAlan Stern * 28551da177e4SLinus Torvalds * Suspending OTG devices may trigger HNP, if that's been enabled 28561da177e4SLinus Torvalds * between a pair of dual-role devices. That will change roles, such 28571da177e4SLinus Torvalds * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 28581da177e4SLinus Torvalds * 28594956eccdSAlan Stern * Devices on USB hub ports have only one "suspend" state, corresponding 28604956eccdSAlan Stern * to ACPI D2, "may cause the device to lose some context". 28614956eccdSAlan Stern * State transitions include: 28624956eccdSAlan Stern * 28634956eccdSAlan Stern * - suspend, resume ... when the VBUS power link stays live 28644956eccdSAlan Stern * - suspend, disconnect ... VBUS lost 28654956eccdSAlan Stern * 28664956eccdSAlan Stern * Once VBUS drop breaks the circuit, the port it's using has to go through 28674956eccdSAlan Stern * normal re-enumeration procedures, starting with enabling VBUS power. 28684956eccdSAlan Stern * Other than re-initializing the hub (plug/unplug, except for root hubs), 28694956eccdSAlan Stern * Linux (2.6) currently has NO mechanisms to initiate that: no khubd 28704956eccdSAlan Stern * timer, no SRP, no requests through sysfs. 28714956eccdSAlan Stern * 28724956eccdSAlan Stern * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when 28734956eccdSAlan Stern * the root hub for their bus goes into global suspend ... so we don't 28744956eccdSAlan Stern * (falsely) update the device power state to say it suspended. 28754956eccdSAlan Stern * 28761da177e4SLinus Torvalds * Returns 0 on success, else negative errno. 28771da177e4SLinus Torvalds */ 287865bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 28791da177e4SLinus Torvalds { 2880624d6c07SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 2881624d6c07SAlan Stern int port1 = udev->portnum; 2882624d6c07SAlan Stern int status; 28834956eccdSAlan Stern 2884624d6c07SAlan Stern /* enable remote wakeup when appropriate; this lets the device 2885624d6c07SAlan Stern * wake up the upstream hub (including maybe the root hub). 2886624d6c07SAlan Stern * 2887624d6c07SAlan Stern * NOTE: OTG devices may issue remote wakeup (or SRP) even when 2888624d6c07SAlan Stern * we don't explicitly enable it here. 2889624d6c07SAlan Stern */ 2890624d6c07SAlan Stern if (udev->do_remote_wakeup) { 2891623bef9eSSarah Sharp if (!hub_is_superspeed(hub->hdev)) { 2892624d6c07SAlan Stern status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2893624d6c07SAlan Stern USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 2894624d6c07SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, 2895624d6c07SAlan Stern NULL, 0, 2896624d6c07SAlan Stern USB_CTRL_SET_TIMEOUT); 2897623bef9eSSarah Sharp } else { 2898623bef9eSSarah Sharp /* Assume there's only one function on the USB 3.0 2899623bef9eSSarah Sharp * device and enable remote wake for the first 2900623bef9eSSarah Sharp * interface. FIXME if the interface association 2901623bef9eSSarah Sharp * descriptor shows there's more than one function. 2902623bef9eSSarah Sharp */ 2903623bef9eSSarah Sharp status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2904623bef9eSSarah Sharp USB_REQ_SET_FEATURE, 2905623bef9eSSarah Sharp USB_RECIP_INTERFACE, 2906623bef9eSSarah Sharp USB_INTRF_FUNC_SUSPEND, 29073b9b6acdSSarah Sharp USB_INTRF_FUNC_SUSPEND_RW | 29083b9b6acdSSarah Sharp USB_INTRF_FUNC_SUSPEND_LP, 2909623bef9eSSarah Sharp NULL, 0, 2910623bef9eSSarah Sharp USB_CTRL_SET_TIMEOUT); 2911623bef9eSSarah Sharp } 29120c487206SOliver Neukum if (status) { 2913624d6c07SAlan Stern dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 2914624d6c07SAlan Stern status); 29150c487206SOliver Neukum /* bail if autosuspend is requested */ 29165b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg)) 29170c487206SOliver Neukum return status; 29180c487206SOliver Neukum } 2919624d6c07SAlan Stern } 2920624d6c07SAlan Stern 292165580b43SAndiry Xu /* disable USB2 hardware LPM */ 292265580b43SAndiry Xu if (udev->usb2_hw_lpm_enabled == 1) 292365580b43SAndiry Xu usb_set_usb2_hardware_lpm(udev, 0); 292465580b43SAndiry Xu 2925f74631e3SSarah Sharp if (usb_disable_ltm(udev)) { 2926f74631e3SSarah Sharp dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.", 2927f74631e3SSarah Sharp __func__); 2928f74631e3SSarah Sharp return -ENOMEM; 2929f74631e3SSarah Sharp } 29308306095fSSarah Sharp if (usb_unlocked_disable_lpm(udev)) { 29318306095fSSarah Sharp dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.", 29328306095fSSarah Sharp __func__); 29338306095fSSarah Sharp return -ENOMEM; 29348306095fSSarah Sharp } 29358306095fSSarah Sharp 2936624d6c07SAlan Stern /* see 7.1.7.6 */ 2937a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) 2938a7114230SAndiry Xu status = set_port_feature(hub->hdev, 2939a7114230SAndiry Xu port1 | (USB_SS_PORT_LS_U3 << 3), 2940a7114230SAndiry Xu USB_PORT_FEAT_LINK_STATE); 2941a8f08d86SAndiry Xu else 2942a8f08d86SAndiry Xu status = set_port_feature(hub->hdev, port1, 2943a8f08d86SAndiry Xu USB_PORT_FEAT_SUSPEND); 2944624d6c07SAlan Stern if (status) { 2945624d6c07SAlan Stern dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", 2946624d6c07SAlan Stern port1, status); 2947624d6c07SAlan Stern /* paranoia: "should not happen" */ 29480c487206SOliver Neukum if (udev->do_remote_wakeup) 2949624d6c07SAlan Stern (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2950624d6c07SAlan Stern USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 2951624d6c07SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, 2952624d6c07SAlan Stern NULL, 0, 2953624d6c07SAlan Stern USB_CTRL_SET_TIMEOUT); 2954cbb33004SAlan Stern 2955c3e751e4SAndiry Xu /* Try to enable USB2 hardware LPM again */ 2956c3e751e4SAndiry Xu if (udev->usb2_hw_lpm_capable == 1) 2957c3e751e4SAndiry Xu usb_set_usb2_hardware_lpm(udev, 1); 2958c3e751e4SAndiry Xu 2959f74631e3SSarah Sharp /* Try to enable USB3 LTM and LPM again */ 2960f74631e3SSarah Sharp usb_enable_ltm(udev); 29618306095fSSarah Sharp usb_unlocked_enable_lpm(udev); 29628306095fSSarah Sharp 2963cbb33004SAlan Stern /* System sleep transitions should never fail */ 29645b1b0b81SAlan Stern if (!PMSG_IS_AUTO(msg)) 2965cbb33004SAlan Stern status = 0; 2966624d6c07SAlan Stern } else { 2967624d6c07SAlan Stern /* device has up to 10 msec to fully suspend */ 296830b1a7a3SAlan Stern dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 296930b1a7a3SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : ""), 297030b1a7a3SAlan Stern udev->do_remote_wakeup); 2971624d6c07SAlan Stern usb_set_device_state(udev, USB_STATE_SUSPENDED); 2972bfd1e910SAlan Stern udev->port_is_suspended = 1; 2973624d6c07SAlan Stern msleep(10); 2974624d6c07SAlan Stern } 2975c08512c7SAlan Stern usb_mark_last_busy(hub->hdev); 29764956eccdSAlan Stern return status; 29771da177e4SLinus Torvalds } 2978f3f3253dSDavid Brownell 29791da177e4SLinus Torvalds /* 2980390a8c34SDavid Brownell * If the USB "suspend" state is in use (rather than "global suspend"), 2981390a8c34SDavid Brownell * many devices will be individually taken out of suspend state using 298254515fe5SAlan Stern * special "resume" signaling. This routine kicks in shortly after 29831da177e4SLinus Torvalds * hardware resume signaling is finished, either because of selective 29841da177e4SLinus Torvalds * resume (by host) or remote wakeup (by device) ... now see what changed 29851da177e4SLinus Torvalds * in the tree that's rooted at this device. 298654515fe5SAlan Stern * 298754515fe5SAlan Stern * If @udev->reset_resume is set then the device is reset before the 298854515fe5SAlan Stern * status check is done. 29891da177e4SLinus Torvalds */ 2990140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev) 29911da177e4SLinus Torvalds { 299254515fe5SAlan Stern int status = 0; 29931da177e4SLinus Torvalds u16 devstatus; 29941da177e4SLinus Torvalds 29951da177e4SLinus Torvalds /* caller owns the udev device lock */ 2996b9cef6c3SWu Fengguang dev_dbg(&udev->dev, "%s\n", 2997b9cef6c3SWu Fengguang udev->reset_resume ? "finish reset-resume" : "finish resume"); 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds /* usb ch9 identifies four variants of SUSPENDED, based on what 30001da177e4SLinus Torvalds * state the device resumes to. Linux currently won't see the 30011da177e4SLinus Torvalds * first two on the host side; they'd be inside hub_port_init() 30021da177e4SLinus Torvalds * during many timeouts, but khubd can't suspend until later. 30031da177e4SLinus Torvalds */ 30041da177e4SLinus Torvalds usb_set_device_state(udev, udev->actconfig 30051da177e4SLinus Torvalds ? USB_STATE_CONFIGURED 30061da177e4SLinus Torvalds : USB_STATE_ADDRESS); 30071da177e4SLinus Torvalds 300854515fe5SAlan Stern /* 10.5.4.5 says not to reset a suspended port if the attached 300954515fe5SAlan Stern * device is enabled for remote wakeup. Hence the reset 301054515fe5SAlan Stern * operation is carried out here, after the port has been 301154515fe5SAlan Stern * resumed. 301254515fe5SAlan Stern */ 301354515fe5SAlan Stern if (udev->reset_resume) 301486c57edfSAlan Stern retry_reset_resume: 3015742120c6SMing Lei status = usb_reset_and_verify_device(udev); 301654515fe5SAlan Stern 30171da177e4SLinus Torvalds /* 10.5.4.5 says be sure devices in the tree are still there. 30181da177e4SLinus Torvalds * For now let's assume the device didn't go crazy on resume, 30191da177e4SLinus Torvalds * and device drivers will know about any resume quirks. 30201da177e4SLinus Torvalds */ 302154515fe5SAlan Stern if (status == 0) { 302246dede46SAlan Stern devstatus = 0; 30231da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 3024b40b7a90SAlan Stern if (status >= 0) 302546dede46SAlan Stern status = (status > 0 ? 0 : -ENODEV); 302686c57edfSAlan Stern 302786c57edfSAlan Stern /* If a normal resume failed, try doing a reset-resume */ 302886c57edfSAlan Stern if (status && !udev->reset_resume && udev->persist_enabled) { 302986c57edfSAlan Stern dev_dbg(&udev->dev, "retry with reset-resume\n"); 303086c57edfSAlan Stern udev->reset_resume = 1; 303186c57edfSAlan Stern goto retry_reset_resume; 303286c57edfSAlan Stern } 303354515fe5SAlan Stern } 3034b40b7a90SAlan Stern 3035624d6c07SAlan Stern if (status) { 3036624d6c07SAlan Stern dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 30371da177e4SLinus Torvalds status); 3038624d6c07SAlan Stern } else if (udev->actconfig) { 30391da177e4SLinus Torvalds le16_to_cpus(&devstatus); 3040686314cfSAlan Stern if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { 30411da177e4SLinus Torvalds status = usb_control_msg(udev, 30421da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 30431da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, 30441da177e4SLinus Torvalds USB_RECIP_DEVICE, 30451da177e4SLinus Torvalds USB_DEVICE_REMOTE_WAKEUP, 0, 30461da177e4SLinus Torvalds NULL, 0, 30471da177e4SLinus Torvalds USB_CTRL_SET_TIMEOUT); 3048a8e7c565SAlan Stern if (status) 3049b9cef6c3SWu Fengguang dev_dbg(&udev->dev, 3050b9cef6c3SWu Fengguang "disable remote wakeup, status %d\n", 3051b9cef6c3SWu Fengguang status); 30524bf0ba86SAlan Stern } 30531da177e4SLinus Torvalds status = 0; 30541da177e4SLinus Torvalds } 30551da177e4SLinus Torvalds return status; 30561da177e4SLinus Torvalds } 30571da177e4SLinus Torvalds 3058624d6c07SAlan Stern /* 3059624d6c07SAlan Stern * usb_port_resume - re-activate a suspended usb device's upstream port 3060624d6c07SAlan Stern * @udev: device to re-activate, not a root hub 3061624d6c07SAlan Stern * Context: must be able to sleep; device not locked; pm locks held 3062624d6c07SAlan Stern * 3063624d6c07SAlan Stern * This will re-activate the suspended device, increasing power usage 3064624d6c07SAlan Stern * while letting drivers communicate again with its endpoints. 3065624d6c07SAlan Stern * USB resume explicitly guarantees that the power session between 3066624d6c07SAlan Stern * the host and the device is the same as it was when the device 3067624d6c07SAlan Stern * suspended. 3068624d6c07SAlan Stern * 3069feccc30dSAlan Stern * If @udev->reset_resume is set then this routine won't check that the 3070feccc30dSAlan Stern * port is still enabled. Furthermore, finish_port_resume() above will 307154515fe5SAlan Stern * reset @udev. The end result is that a broken power session can be 307254515fe5SAlan Stern * recovered and @udev will appear to persist across a loss of VBUS power. 307354515fe5SAlan Stern * 307454515fe5SAlan Stern * For example, if a host controller doesn't maintain VBUS suspend current 307554515fe5SAlan Stern * during a system sleep or is reset when the system wakes up, all the USB 307654515fe5SAlan Stern * power sessions below it will be broken. This is especially troublesome 307754515fe5SAlan Stern * for mass-storage devices containing mounted filesystems, since the 307854515fe5SAlan Stern * device will appear to have disconnected and all the memory mappings 307954515fe5SAlan Stern * to it will be lost. Using the USB_PERSIST facility, the device can be 308054515fe5SAlan Stern * made to appear as if it had not disconnected. 308154515fe5SAlan Stern * 3082742120c6SMing Lei * This facility can be dangerous. Although usb_reset_and_verify_device() makes 3083feccc30dSAlan Stern * every effort to insure that the same device is present after the 308454515fe5SAlan Stern * reset as before, it cannot provide a 100% guarantee. Furthermore it's 308554515fe5SAlan Stern * quite possible for a device to remain unaltered but its media to be 308654515fe5SAlan Stern * changed. If the user replaces a flash memory card while the system is 308754515fe5SAlan Stern * asleep, he will have only himself to blame when the filesystem on the 308854515fe5SAlan Stern * new card is corrupted and the system crashes. 308954515fe5SAlan Stern * 3090624d6c07SAlan Stern * Returns 0 on success, else negative errno. 3091624d6c07SAlan Stern */ 309265bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 30931da177e4SLinus Torvalds { 3094624d6c07SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 3095624d6c07SAlan Stern int port1 = udev->portnum; 30961da177e4SLinus Torvalds int status; 3097d25450c6SAlan Stern u16 portchange, portstatus; 3098d25450c6SAlan Stern 3099d25450c6SAlan Stern /* Skip the initial Clear-Suspend step for a remote wakeup */ 3100d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 31010ed9a57eSAndiry Xu if (status == 0 && !port_is_suspended(hub, portstatus)) 3102d25450c6SAlan Stern goto SuspendCleared; 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds // dev_dbg(hub->intfdev, "resume port %d\n", port1); 31051da177e4SLinus Torvalds 3106d5cbad4bSAlan Stern set_bit(port1, hub->busy_bits); 3107d5cbad4bSAlan Stern 31081da177e4SLinus Torvalds /* see 7.1.7.7; affects power usage, but not budgeting */ 3109a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) 3110a7114230SAndiry Xu status = set_port_feature(hub->hdev, 3111a7114230SAndiry Xu port1 | (USB_SS_PORT_LS_U0 << 3), 3112a7114230SAndiry Xu USB_PORT_FEAT_LINK_STATE); 3113a7114230SAndiry Xu else 31141da177e4SLinus Torvalds status = clear_port_feature(hub->hdev, 31151da177e4SLinus Torvalds port1, USB_PORT_FEAT_SUSPEND); 31161da177e4SLinus Torvalds if (status) { 3117624d6c07SAlan Stern dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", 31181da177e4SLinus Torvalds port1, status); 31191da177e4SLinus Torvalds } else { 31201da177e4SLinus Torvalds /* drive resume for at least 20 msec */ 3121645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", 31225b1b0b81SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : "")); 31231da177e4SLinus Torvalds msleep(25); 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds /* Virtual root hubs can trigger on GET_PORT_STATUS to 31261da177e4SLinus Torvalds * stop resume signaling. Then finish the resume 31271da177e4SLinus Torvalds * sequence. 31281da177e4SLinus Torvalds */ 3129d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 313054515fe5SAlan Stern 31311da177e4SLinus Torvalds /* TRSMRCY = 10 msec */ 31321da177e4SLinus Torvalds msleep(10); 31331da177e4SLinus Torvalds } 3134b01b03f3SAlan Stern 3135b01b03f3SAlan Stern SuspendCleared: 3136b01b03f3SAlan Stern if (status == 0) { 3137bfd1e910SAlan Stern udev->port_is_suspended = 0; 3138a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) { 3139a7114230SAndiry Xu if (portchange & USB_PORT_STAT_C_LINK_STATE) 3140a7114230SAndiry Xu clear_port_feature(hub->hdev, port1, 3141a7114230SAndiry Xu USB_PORT_FEAT_C_PORT_LINK_STATE); 3142a7114230SAndiry Xu } else { 3143b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_SUSPEND) 3144b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 3145b01b03f3SAlan Stern USB_PORT_FEAT_C_SUSPEND); 31461da177e4SLinus Torvalds } 3147a7114230SAndiry Xu } 31481da177e4SLinus Torvalds 3149d5cbad4bSAlan Stern clear_bit(port1, hub->busy_bits); 3150d5cbad4bSAlan Stern 3151b01b03f3SAlan Stern status = check_port_resume_type(udev, 3152b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 315354515fe5SAlan Stern if (status == 0) 315454515fe5SAlan Stern status = finish_port_resume(udev); 315554515fe5SAlan Stern if (status < 0) { 315654515fe5SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 315754515fe5SAlan Stern hub_port_logical_disconnect(hub, port1); 315865580b43SAndiry Xu } else { 315965580b43SAndiry Xu /* Try to enable USB2 hardware LPM */ 316065580b43SAndiry Xu if (udev->usb2_hw_lpm_capable == 1) 316165580b43SAndiry Xu usb_set_usb2_hardware_lpm(udev, 1); 31628306095fSSarah Sharp 3163f74631e3SSarah Sharp /* Try to enable USB3 LTM and LPM */ 3164f74631e3SSarah Sharp usb_enable_ltm(udev); 31658306095fSSarah Sharp usb_unlocked_enable_lpm(udev); 316654515fe5SAlan Stern } 316765580b43SAndiry Xu 31681da177e4SLinus Torvalds return status; 31691da177e4SLinus Torvalds } 31701da177e4SLinus Torvalds 31718808f00cSAlan Stern /* caller has locked udev */ 31720534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev) 31731da177e4SLinus Torvalds { 31741da177e4SLinus Torvalds int status = 0; 31751da177e4SLinus Torvalds 31761da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED) { 3177645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 31789bbdf1e0SAlan Stern status = usb_autoresume_device(udev); 31799bbdf1e0SAlan Stern if (status == 0) { 31809bbdf1e0SAlan Stern /* Let the drivers do their thing, then... */ 31819bbdf1e0SAlan Stern usb_autosuspend_device(udev); 31829bbdf1e0SAlan Stern } 3183d25450c6SAlan Stern } 31841da177e4SLinus Torvalds return status; 31851da177e4SLinus Torvalds } 31861da177e4SLinus Torvalds 3187d388dab7SAlan Stern #else /* CONFIG_USB_SUSPEND */ 3188d388dab7SAlan Stern 3189d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */ 3190d388dab7SAlan Stern 319165bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 3192d388dab7SAlan Stern { 3193d388dab7SAlan Stern return 0; 3194d388dab7SAlan Stern } 3195d388dab7SAlan Stern 3196b01b03f3SAlan Stern /* However we may need to do a reset-resume */ 3197b01b03f3SAlan Stern 319865bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 3199d388dab7SAlan Stern { 3200b01b03f3SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 3201b01b03f3SAlan Stern int port1 = udev->portnum; 3202b01b03f3SAlan Stern int status; 3203b01b03f3SAlan Stern u16 portchange, portstatus; 320454515fe5SAlan Stern 3205b01b03f3SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 3206b01b03f3SAlan Stern status = check_port_resume_type(udev, 3207b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 3208b01b03f3SAlan Stern 3209b01b03f3SAlan Stern if (status) { 3210b01b03f3SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 3211b01b03f3SAlan Stern hub_port_logical_disconnect(hub, port1); 3212b01b03f3SAlan Stern } else if (udev->reset_resume) { 321354515fe5SAlan Stern dev_dbg(&udev->dev, "reset-resume\n"); 3214742120c6SMing Lei status = usb_reset_and_verify_device(udev); 321554515fe5SAlan Stern } 321654515fe5SAlan Stern return status; 3217d388dab7SAlan Stern } 3218d388dab7SAlan Stern 3219d388dab7SAlan Stern #endif 3220d388dab7SAlan Stern 3221e6f30deaSMing Lei static int check_ports_changed(struct usb_hub *hub) 3222e6f30deaSMing Lei { 3223e6f30deaSMing Lei int port1; 3224e6f30deaSMing Lei 3225e6f30deaSMing Lei for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { 3226e6f30deaSMing Lei u16 portstatus, portchange; 3227e6f30deaSMing Lei int status; 3228e6f30deaSMing Lei 3229e6f30deaSMing Lei status = hub_port_status(hub, port1, &portstatus, &portchange); 3230e6f30deaSMing Lei if (!status && portchange) 3231e6f30deaSMing Lei return 1; 3232e6f30deaSMing Lei } 3233e6f30deaSMing Lei return 0; 3234e6f30deaSMing Lei } 3235e6f30deaSMing Lei 3236db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 32371da177e4SLinus Torvalds { 32381da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata (intf); 32391da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 32401da177e4SLinus Torvalds unsigned port1; 32414296c70aSSarah Sharp int status; 32421da177e4SLinus Torvalds 3243cbb33004SAlan Stern /* Warn if children aren't already suspended */ 32441da177e4SLinus Torvalds for (port1 = 1; port1 <= hdev->maxchild; port1++) { 32451da177e4SLinus Torvalds struct usb_device *udev; 32461da177e4SLinus Torvalds 3247ff823c79SLan Tianyu udev = hub->ports[port1 - 1]->child; 32486840d255SAlan Stern if (udev && udev->can_submit) { 3249cbb33004SAlan Stern dev_warn(&intf->dev, "port %d nyet suspended\n", port1); 32505b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg)) 3251c9f89fa4SDavid Brownell return -EBUSY; 3252c9f89fa4SDavid Brownell } 32531da177e4SLinus Torvalds } 3254e6f30deaSMing Lei 3255e6f30deaSMing Lei if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { 3256e6f30deaSMing Lei /* check if there are changes pending on hub ports */ 3257e6f30deaSMing Lei if (check_ports_changed(hub)) { 3258e6f30deaSMing Lei if (PMSG_IS_AUTO(msg)) 3259e6f30deaSMing Lei return -EBUSY; 3260e6f30deaSMing Lei pm_wakeup_event(&hdev->dev, 2000); 3261e6f30deaSMing Lei } 3262e6f30deaSMing Lei } 3263e6f30deaSMing Lei 32644296c70aSSarah Sharp if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 32654296c70aSSarah Sharp /* Enable hub to send remote wakeup for all ports. */ 32664296c70aSSarah Sharp for (port1 = 1; port1 <= hdev->maxchild; port1++) { 32674296c70aSSarah Sharp status = set_port_feature(hdev, 32684296c70aSSarah Sharp port1 | 32694296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 32704296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 32714296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 32724296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_MASK); 32734296c70aSSarah Sharp } 32744296c70aSSarah Sharp } 32751da177e4SLinus Torvalds 3276441b62c1SHarvey Harrison dev_dbg(&intf->dev, "%s\n", __func__); 327740f122f3SAlan Stern 3278c9f89fa4SDavid Brownell /* stop khubd and related activity */ 32794330354fSAlan Stern hub_quiesce(hub, HUB_SUSPEND); 3280b6f6436dSAlan Stern return 0; 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds 32831da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf) 32841da177e4SLinus Torvalds { 32851da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 32861da177e4SLinus Torvalds 32875e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 3288f2835219SAlan Stern hub_activate(hub, HUB_RESUME); 32891da177e4SLinus Torvalds return 0; 32901da177e4SLinus Torvalds } 32911da177e4SLinus Torvalds 3292b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf) 3293f07600cfSAlan Stern { 3294b41a60ecSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 3295f07600cfSAlan Stern 32965e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 3297f2835219SAlan Stern hub_activate(hub, HUB_RESET_RESUME); 3298f07600cfSAlan Stern return 0; 3299f07600cfSAlan Stern } 3300f07600cfSAlan Stern 330154515fe5SAlan Stern /** 330254515fe5SAlan Stern * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 330354515fe5SAlan Stern * @rhdev: struct usb_device for the root hub 330454515fe5SAlan Stern * 330554515fe5SAlan Stern * The USB host controller driver calls this function when its root hub 330654515fe5SAlan Stern * is resumed and Vbus power has been interrupted or the controller 3307feccc30dSAlan Stern * has been reset. The routine marks @rhdev as having lost power. 3308feccc30dSAlan Stern * When the hub driver is resumed it will take notice and carry out 3309feccc30dSAlan Stern * power-session recovery for all the "USB-PERSIST"-enabled child devices; 3310feccc30dSAlan Stern * the others will be disconnected. 331154515fe5SAlan Stern */ 331254515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev) 331354515fe5SAlan Stern { 331454515fe5SAlan Stern dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); 331554515fe5SAlan Stern rhdev->reset_resume = 1; 331654515fe5SAlan Stern } 331754515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 331854515fe5SAlan Stern 33191ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[] = { 33201ea7e0e8SSarah Sharp "U0", 33211ea7e0e8SSarah Sharp "U1", 33221ea7e0e8SSarah Sharp "U2", 33231ea7e0e8SSarah Sharp "U3", 33241ea7e0e8SSarah Sharp }; 33251ea7e0e8SSarah Sharp 33261ea7e0e8SSarah Sharp /* 33271ea7e0e8SSarah Sharp * Send a Set SEL control transfer to the device, prior to enabling 33281ea7e0e8SSarah Sharp * device-initiated U1 or U2. This lets the device know the exit latencies from 33291ea7e0e8SSarah Sharp * the time the device initiates a U1 or U2 exit, to the time it will receive a 33301ea7e0e8SSarah Sharp * packet from the host. 33311ea7e0e8SSarah Sharp * 33321ea7e0e8SSarah Sharp * This function will fail if the SEL or PEL values for udev are greater than 33331ea7e0e8SSarah Sharp * the maximum allowed values for the link state to be enabled. 33341ea7e0e8SSarah Sharp */ 33351ea7e0e8SSarah Sharp static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) 33361ea7e0e8SSarah Sharp { 33371ea7e0e8SSarah Sharp struct usb_set_sel_req *sel_values; 33381ea7e0e8SSarah Sharp unsigned long long u1_sel; 33391ea7e0e8SSarah Sharp unsigned long long u1_pel; 33401ea7e0e8SSarah Sharp unsigned long long u2_sel; 33411ea7e0e8SSarah Sharp unsigned long long u2_pel; 33421ea7e0e8SSarah Sharp int ret; 33431ea7e0e8SSarah Sharp 33441ea7e0e8SSarah Sharp /* Convert SEL and PEL stored in ns to us */ 33451ea7e0e8SSarah Sharp u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 33461ea7e0e8SSarah Sharp u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 33471ea7e0e8SSarah Sharp u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 33481ea7e0e8SSarah Sharp u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 33491ea7e0e8SSarah Sharp 33501ea7e0e8SSarah Sharp /* 33511ea7e0e8SSarah Sharp * Make sure that the calculated SEL and PEL values for the link 33521ea7e0e8SSarah Sharp * state we're enabling aren't bigger than the max SEL/PEL 33531ea7e0e8SSarah Sharp * value that will fit in the SET SEL control transfer. 33541ea7e0e8SSarah Sharp * Otherwise the device would get an incorrect idea of the exit 33551ea7e0e8SSarah Sharp * latency for the link state, and could start a device-initiated 33561ea7e0e8SSarah Sharp * U1/U2 when the exit latencies are too high. 33571ea7e0e8SSarah Sharp */ 33581ea7e0e8SSarah Sharp if ((state == USB3_LPM_U1 && 33591ea7e0e8SSarah Sharp (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 33601ea7e0e8SSarah Sharp u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || 33611ea7e0e8SSarah Sharp (state == USB3_LPM_U2 && 33621ea7e0e8SSarah Sharp (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 33631ea7e0e8SSarah Sharp u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { 33641510a1a2SSarah Sharp dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n", 33651ea7e0e8SSarah Sharp usb3_lpm_names[state], u1_sel, u1_pel); 33661ea7e0e8SSarah Sharp return -EINVAL; 33671ea7e0e8SSarah Sharp } 33681ea7e0e8SSarah Sharp 33691ea7e0e8SSarah Sharp /* 33701ea7e0e8SSarah Sharp * If we're enabling device-initiated LPM for one link state, 33711ea7e0e8SSarah Sharp * but the other link state has a too high SEL or PEL value, 33721ea7e0e8SSarah Sharp * just set those values to the max in the Set SEL request. 33731ea7e0e8SSarah Sharp */ 33741ea7e0e8SSarah Sharp if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) 33751ea7e0e8SSarah Sharp u1_sel = USB3_LPM_MAX_U1_SEL_PEL; 33761ea7e0e8SSarah Sharp 33771ea7e0e8SSarah Sharp if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) 33781ea7e0e8SSarah Sharp u1_pel = USB3_LPM_MAX_U1_SEL_PEL; 33791ea7e0e8SSarah Sharp 33801ea7e0e8SSarah Sharp if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) 33811ea7e0e8SSarah Sharp u2_sel = USB3_LPM_MAX_U2_SEL_PEL; 33821ea7e0e8SSarah Sharp 33831ea7e0e8SSarah Sharp if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) 33841ea7e0e8SSarah Sharp u2_pel = USB3_LPM_MAX_U2_SEL_PEL; 33851ea7e0e8SSarah Sharp 33861ea7e0e8SSarah Sharp /* 33871ea7e0e8SSarah Sharp * usb_enable_lpm() can be called as part of a failed device reset, 33881ea7e0e8SSarah Sharp * which may be initiated by an error path of a mass storage driver. 33891ea7e0e8SSarah Sharp * Therefore, use GFP_NOIO. 33901ea7e0e8SSarah Sharp */ 33911ea7e0e8SSarah Sharp sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); 33921ea7e0e8SSarah Sharp if (!sel_values) 33931ea7e0e8SSarah Sharp return -ENOMEM; 33941ea7e0e8SSarah Sharp 33951ea7e0e8SSarah Sharp sel_values->u1_sel = u1_sel; 33961ea7e0e8SSarah Sharp sel_values->u1_pel = u1_pel; 33971ea7e0e8SSarah Sharp sel_values->u2_sel = cpu_to_le16(u2_sel); 33981ea7e0e8SSarah Sharp sel_values->u2_pel = cpu_to_le16(u2_pel); 33991ea7e0e8SSarah Sharp 34001ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 34011ea7e0e8SSarah Sharp USB_REQ_SET_SEL, 34021ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 34031ea7e0e8SSarah Sharp 0, 0, 34041ea7e0e8SSarah Sharp sel_values, sizeof *(sel_values), 34051ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 34061ea7e0e8SSarah Sharp kfree(sel_values); 34071ea7e0e8SSarah Sharp return ret; 34081ea7e0e8SSarah Sharp } 34091ea7e0e8SSarah Sharp 34101ea7e0e8SSarah Sharp /* 34111ea7e0e8SSarah Sharp * Enable or disable device-initiated U1 or U2 transitions. 34121ea7e0e8SSarah Sharp */ 34131ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev, 34141ea7e0e8SSarah Sharp enum usb3_link_state state, bool enable) 34151ea7e0e8SSarah Sharp { 34161ea7e0e8SSarah Sharp int ret; 34171ea7e0e8SSarah Sharp int feature; 34181ea7e0e8SSarah Sharp 34191ea7e0e8SSarah Sharp switch (state) { 34201ea7e0e8SSarah Sharp case USB3_LPM_U1: 34211ea7e0e8SSarah Sharp feature = USB_DEVICE_U1_ENABLE; 34221ea7e0e8SSarah Sharp break; 34231ea7e0e8SSarah Sharp case USB3_LPM_U2: 34241ea7e0e8SSarah Sharp feature = USB_DEVICE_U2_ENABLE; 34251ea7e0e8SSarah Sharp break; 34261ea7e0e8SSarah Sharp default: 34271ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 34281ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable"); 34291ea7e0e8SSarah Sharp return -EINVAL; 34301ea7e0e8SSarah Sharp } 34311ea7e0e8SSarah Sharp 34321ea7e0e8SSarah Sharp if (udev->state != USB_STATE_CONFIGURED) { 34331ea7e0e8SSarah Sharp dev_dbg(&udev->dev, "%s: Can't %s %s state " 34341ea7e0e8SSarah Sharp "for unconfigured device.\n", 34351ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable", 34361ea7e0e8SSarah Sharp usb3_lpm_names[state]); 34371ea7e0e8SSarah Sharp return 0; 34381ea7e0e8SSarah Sharp } 34391ea7e0e8SSarah Sharp 34401ea7e0e8SSarah Sharp if (enable) { 34411ea7e0e8SSarah Sharp /* 34421ea7e0e8SSarah Sharp * Now send the control transfer to enable device-initiated LPM 34431ea7e0e8SSarah Sharp * for either U1 or U2. 34441ea7e0e8SSarah Sharp */ 34451ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 34461ea7e0e8SSarah Sharp USB_REQ_SET_FEATURE, 34471ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 34481ea7e0e8SSarah Sharp feature, 34491ea7e0e8SSarah Sharp 0, NULL, 0, 34501ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 34511ea7e0e8SSarah Sharp } else { 34521ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 34531ea7e0e8SSarah Sharp USB_REQ_CLEAR_FEATURE, 34541ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 34551ea7e0e8SSarah Sharp feature, 34561ea7e0e8SSarah Sharp 0, NULL, 0, 34571ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 34581ea7e0e8SSarah Sharp } 34591ea7e0e8SSarah Sharp if (ret < 0) { 34601ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 34611ea7e0e8SSarah Sharp enable ? "Enable" : "Disable", 34621ea7e0e8SSarah Sharp usb3_lpm_names[state]); 34631ea7e0e8SSarah Sharp return -EBUSY; 34641ea7e0e8SSarah Sharp } 34651ea7e0e8SSarah Sharp return 0; 34661ea7e0e8SSarah Sharp } 34671ea7e0e8SSarah Sharp 34681ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev, 34691ea7e0e8SSarah Sharp enum usb3_link_state state, int timeout) 34701ea7e0e8SSarah Sharp { 34711ea7e0e8SSarah Sharp int ret; 34721ea7e0e8SSarah Sharp int feature; 34731ea7e0e8SSarah Sharp 34741ea7e0e8SSarah Sharp switch (state) { 34751ea7e0e8SSarah Sharp case USB3_LPM_U1: 34761ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U1_TIMEOUT; 34771ea7e0e8SSarah Sharp break; 34781ea7e0e8SSarah Sharp case USB3_LPM_U2: 34791ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U2_TIMEOUT; 34801ea7e0e8SSarah Sharp break; 34811ea7e0e8SSarah Sharp default: 34821ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 34831ea7e0e8SSarah Sharp __func__); 34841ea7e0e8SSarah Sharp return -EINVAL; 34851ea7e0e8SSarah Sharp } 34861ea7e0e8SSarah Sharp 34871ea7e0e8SSarah Sharp if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 34881ea7e0e8SSarah Sharp timeout != USB3_LPM_DEVICE_INITIATED) { 34891ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 34901ea7e0e8SSarah Sharp "which is a reserved value.\n", 34911ea7e0e8SSarah Sharp usb3_lpm_names[state], timeout); 34921ea7e0e8SSarah Sharp return -EINVAL; 34931ea7e0e8SSarah Sharp } 34941ea7e0e8SSarah Sharp 34951ea7e0e8SSarah Sharp ret = set_port_feature(udev->parent, 34961ea7e0e8SSarah Sharp USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 34971ea7e0e8SSarah Sharp feature); 34981ea7e0e8SSarah Sharp if (ret < 0) { 34991ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 35001ea7e0e8SSarah Sharp "error code %i\n", usb3_lpm_names[state], 35011ea7e0e8SSarah Sharp timeout, ret); 35021ea7e0e8SSarah Sharp return -EBUSY; 35031ea7e0e8SSarah Sharp } 35041ea7e0e8SSarah Sharp if (state == USB3_LPM_U1) 35051ea7e0e8SSarah Sharp udev->u1_params.timeout = timeout; 35061ea7e0e8SSarah Sharp else 35071ea7e0e8SSarah Sharp udev->u2_params.timeout = timeout; 35081ea7e0e8SSarah Sharp return 0; 35091ea7e0e8SSarah Sharp } 35101ea7e0e8SSarah Sharp 35111ea7e0e8SSarah Sharp /* 35121ea7e0e8SSarah Sharp * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 35131ea7e0e8SSarah Sharp * U1/U2 entry. 35141ea7e0e8SSarah Sharp * 35151ea7e0e8SSarah Sharp * We will attempt to enable U1 or U2, but there are no guarantees that the 35161ea7e0e8SSarah Sharp * control transfers to set the hub timeout or enable device-initiated U1/U2 35171ea7e0e8SSarah Sharp * will be successful. 35181ea7e0e8SSarah Sharp * 35191ea7e0e8SSarah Sharp * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 35201ea7e0e8SSarah Sharp * driver know about it. If that call fails, it should be harmless, and just 35211ea7e0e8SSarah Sharp * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 35221ea7e0e8SSarah Sharp */ 35231ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 35241ea7e0e8SSarah Sharp enum usb3_link_state state) 35251ea7e0e8SSarah Sharp { 352665a95b75SSarah Sharp int timeout, ret; 3527ae8963adSSarah Sharp __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat; 3528ae8963adSSarah Sharp __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat; 3529ae8963adSSarah Sharp 3530ae8963adSSarah Sharp /* If the device says it doesn't have *any* exit latency to come out of 3531ae8963adSSarah Sharp * U1 or U2, it's probably lying. Assume it doesn't implement that link 3532ae8963adSSarah Sharp * state. 3533ae8963adSSarah Sharp */ 3534ae8963adSSarah Sharp if ((state == USB3_LPM_U1 && u1_mel == 0) || 3535ae8963adSSarah Sharp (state == USB3_LPM_U2 && u2_mel == 0)) 3536ae8963adSSarah Sharp return; 35371ea7e0e8SSarah Sharp 353865a95b75SSarah Sharp /* 353965a95b75SSarah Sharp * First, let the device know about the exit latencies 354065a95b75SSarah Sharp * associated with the link state we're about to enable. 354165a95b75SSarah Sharp */ 354265a95b75SSarah Sharp ret = usb_req_set_sel(udev, state); 354365a95b75SSarah Sharp if (ret < 0) { 354465a95b75SSarah Sharp dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n", 354565a95b75SSarah Sharp usb3_lpm_names[state]); 354665a95b75SSarah Sharp return; 354765a95b75SSarah Sharp } 354865a95b75SSarah Sharp 35491ea7e0e8SSarah Sharp /* We allow the host controller to set the U1/U2 timeout internally 35501ea7e0e8SSarah Sharp * first, so that it can change its schedule to account for the 35511ea7e0e8SSarah Sharp * additional latency to send data to a device in a lower power 35521ea7e0e8SSarah Sharp * link state. 35531ea7e0e8SSarah Sharp */ 35541ea7e0e8SSarah Sharp timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 35551ea7e0e8SSarah Sharp 35561ea7e0e8SSarah Sharp /* xHCI host controller doesn't want to enable this LPM state. */ 35571ea7e0e8SSarah Sharp if (timeout == 0) 35581ea7e0e8SSarah Sharp return; 35591ea7e0e8SSarah Sharp 35601ea7e0e8SSarah Sharp if (timeout < 0) { 35611ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not enable %s link state, " 35621ea7e0e8SSarah Sharp "xHCI error %i.\n", usb3_lpm_names[state], 35631ea7e0e8SSarah Sharp timeout); 35641ea7e0e8SSarah Sharp return; 35651ea7e0e8SSarah Sharp } 35661ea7e0e8SSarah Sharp 35671ea7e0e8SSarah Sharp if (usb_set_lpm_timeout(udev, state, timeout)) 35681ea7e0e8SSarah Sharp /* If we can't set the parent hub U1/U2 timeout, 35691ea7e0e8SSarah Sharp * device-initiated LPM won't be allowed either, so let the xHCI 35701ea7e0e8SSarah Sharp * host know that this link state won't be enabled. 35711ea7e0e8SSarah Sharp */ 35721ea7e0e8SSarah Sharp hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 35731ea7e0e8SSarah Sharp 35741ea7e0e8SSarah Sharp /* Only a configured device will accept the Set Feature U1/U2_ENABLE */ 35751ea7e0e8SSarah Sharp else if (udev->actconfig) 35761ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, true); 35771ea7e0e8SSarah Sharp 35781ea7e0e8SSarah Sharp } 35791ea7e0e8SSarah Sharp 35801ea7e0e8SSarah Sharp /* 35811ea7e0e8SSarah Sharp * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 35821ea7e0e8SSarah Sharp * U1/U2 entry. 35831ea7e0e8SSarah Sharp * 35841ea7e0e8SSarah Sharp * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 35851ea7e0e8SSarah Sharp * If zero is returned, the parent will not allow the link to go into U1/U2. 35861ea7e0e8SSarah Sharp * 35871ea7e0e8SSarah Sharp * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 35881ea7e0e8SSarah Sharp * it won't have an effect on the bus link state because the parent hub will 35891ea7e0e8SSarah Sharp * still disallow device-initiated U1/U2 entry. 35901ea7e0e8SSarah Sharp * 35911ea7e0e8SSarah Sharp * If zero is returned, the xHCI host controller may still think U1/U2 entry is 35921ea7e0e8SSarah Sharp * possible. The result will be slightly more bus bandwidth will be taken up 35931ea7e0e8SSarah Sharp * (to account for U1/U2 exit latency), but it should be harmless. 35941ea7e0e8SSarah Sharp */ 35951ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 35961ea7e0e8SSarah Sharp enum usb3_link_state state) 35971ea7e0e8SSarah Sharp { 35981ea7e0e8SSarah Sharp int feature; 35991ea7e0e8SSarah Sharp 36001ea7e0e8SSarah Sharp switch (state) { 36011ea7e0e8SSarah Sharp case USB3_LPM_U1: 36021ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U1_TIMEOUT; 36031ea7e0e8SSarah Sharp break; 36041ea7e0e8SSarah Sharp case USB3_LPM_U2: 36051ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U2_TIMEOUT; 36061ea7e0e8SSarah Sharp break; 36071ea7e0e8SSarah Sharp default: 36081ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 36091ea7e0e8SSarah Sharp __func__); 36101ea7e0e8SSarah Sharp return -EINVAL; 36111ea7e0e8SSarah Sharp } 36121ea7e0e8SSarah Sharp 36131ea7e0e8SSarah Sharp if (usb_set_lpm_timeout(udev, state, 0)) 36141ea7e0e8SSarah Sharp return -EBUSY; 36151ea7e0e8SSarah Sharp 36161ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, false); 36171ea7e0e8SSarah Sharp 36181ea7e0e8SSarah Sharp if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 36191ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 36201ea7e0e8SSarah Sharp "bus schedule bandwidth may be impacted.\n", 36211ea7e0e8SSarah Sharp usb3_lpm_names[state]); 36221ea7e0e8SSarah Sharp return 0; 36231ea7e0e8SSarah Sharp } 36241ea7e0e8SSarah Sharp 36251ea7e0e8SSarah Sharp /* 36261ea7e0e8SSarah Sharp * Disable hub-initiated and device-initiated U1 and U2 entry. 36271ea7e0e8SSarah Sharp * Caller must own the bandwidth_mutex. 36281ea7e0e8SSarah Sharp * 36291ea7e0e8SSarah Sharp * This will call usb_enable_lpm() on failure, which will decrement 36301ea7e0e8SSarah Sharp * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 36311ea7e0e8SSarah Sharp */ 36321ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev) 36331ea7e0e8SSarah Sharp { 36341ea7e0e8SSarah Sharp struct usb_hcd *hcd; 36351ea7e0e8SSarah Sharp 36361ea7e0e8SSarah Sharp if (!udev || !udev->parent || 36371ea7e0e8SSarah Sharp udev->speed != USB_SPEED_SUPER || 36381ea7e0e8SSarah Sharp !udev->lpm_capable) 36391ea7e0e8SSarah Sharp return 0; 36401ea7e0e8SSarah Sharp 36411ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus); 36421ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 36431ea7e0e8SSarah Sharp return 0; 36441ea7e0e8SSarah Sharp 36451ea7e0e8SSarah Sharp udev->lpm_disable_count++; 364655558c33SDan Carpenter if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 36471ea7e0e8SSarah Sharp return 0; 36481ea7e0e8SSarah Sharp 36491ea7e0e8SSarah Sharp /* If LPM is enabled, attempt to disable it. */ 36501ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 36511ea7e0e8SSarah Sharp goto enable_lpm; 36521ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 36531ea7e0e8SSarah Sharp goto enable_lpm; 36541ea7e0e8SSarah Sharp 36551ea7e0e8SSarah Sharp return 0; 36561ea7e0e8SSarah Sharp 36571ea7e0e8SSarah Sharp enable_lpm: 36581ea7e0e8SSarah Sharp usb_enable_lpm(udev); 36591ea7e0e8SSarah Sharp return -EBUSY; 36601ea7e0e8SSarah Sharp } 36611ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm); 36621ea7e0e8SSarah Sharp 36631ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 36641ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev) 36651ea7e0e8SSarah Sharp { 36661ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 36671ea7e0e8SSarah Sharp int ret; 36681ea7e0e8SSarah Sharp 36691ea7e0e8SSarah Sharp if (!hcd) 36701ea7e0e8SSarah Sharp return -EINVAL; 36711ea7e0e8SSarah Sharp 36721ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex); 36731ea7e0e8SSarah Sharp ret = usb_disable_lpm(udev); 36741ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 36751ea7e0e8SSarah Sharp 36761ea7e0e8SSarah Sharp return ret; 36771ea7e0e8SSarah Sharp } 36781ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 36791ea7e0e8SSarah Sharp 36801ea7e0e8SSarah Sharp /* 36811ea7e0e8SSarah Sharp * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 36821ea7e0e8SSarah Sharp * xHCI host policy may prevent U1 or U2 from being enabled. 36831ea7e0e8SSarah Sharp * 36841ea7e0e8SSarah Sharp * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 36851ea7e0e8SSarah Sharp * until the lpm_disable_count drops to zero. Caller must own the 36861ea7e0e8SSarah Sharp * bandwidth_mutex. 36871ea7e0e8SSarah Sharp */ 36881ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) 36891ea7e0e8SSarah Sharp { 36901ea7e0e8SSarah Sharp struct usb_hcd *hcd; 36911ea7e0e8SSarah Sharp 36921ea7e0e8SSarah Sharp if (!udev || !udev->parent || 36931ea7e0e8SSarah Sharp udev->speed != USB_SPEED_SUPER || 36941ea7e0e8SSarah Sharp !udev->lpm_capable) 36951ea7e0e8SSarah Sharp return; 36961ea7e0e8SSarah Sharp 36971ea7e0e8SSarah Sharp udev->lpm_disable_count--; 36981ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus); 36991ea7e0e8SSarah Sharp /* Double check that we can both enable and disable LPM. 37001ea7e0e8SSarah Sharp * Device must be configured to accept set feature U1/U2 timeout. 37011ea7e0e8SSarah Sharp */ 37021ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 37031ea7e0e8SSarah Sharp !hcd->driver->disable_usb3_lpm_timeout) 37041ea7e0e8SSarah Sharp return; 37051ea7e0e8SSarah Sharp 37061ea7e0e8SSarah Sharp if (udev->lpm_disable_count > 0) 37071ea7e0e8SSarah Sharp return; 37081ea7e0e8SSarah Sharp 37091ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U1); 37101ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U2); 37111ea7e0e8SSarah Sharp } 37121ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm); 37131ea7e0e8SSarah Sharp 37141ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 37151ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) 37161ea7e0e8SSarah Sharp { 37171ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 37181ea7e0e8SSarah Sharp 37191ea7e0e8SSarah Sharp if (!hcd) 37201ea7e0e8SSarah Sharp return; 37211ea7e0e8SSarah Sharp 37221ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex); 37231ea7e0e8SSarah Sharp usb_enable_lpm(udev); 37241ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 37251ea7e0e8SSarah Sharp } 37261ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 37271ea7e0e8SSarah Sharp 37281ea7e0e8SSarah Sharp 3729d388dab7SAlan Stern #else /* CONFIG_PM */ 3730d388dab7SAlan Stern 3731511366daSAndrew Morton #define hub_suspend NULL 3732511366daSAndrew Morton #define hub_resume NULL 3733f07600cfSAlan Stern #define hub_reset_resume NULL 37341ea7e0e8SSarah Sharp 37351ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev) 37361ea7e0e8SSarah Sharp { 37371ea7e0e8SSarah Sharp return 0; 37381ea7e0e8SSarah Sharp } 3739e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm); 37401ea7e0e8SSarah Sharp 37411ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { } 3742e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm); 37431ea7e0e8SSarah Sharp 37441ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev) 37451ea7e0e8SSarah Sharp { 37461ea7e0e8SSarah Sharp return 0; 37471ea7e0e8SSarah Sharp } 3748e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 37491ea7e0e8SSarah Sharp 37501ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { } 3751e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 3752f74631e3SSarah Sharp 3753f74631e3SSarah Sharp int usb_disable_ltm(struct usb_device *udev) 3754f74631e3SSarah Sharp { 3755f74631e3SSarah Sharp return 0; 3756f74631e3SSarah Sharp } 3757f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_ltm); 3758f74631e3SSarah Sharp 3759f74631e3SSarah Sharp void usb_enable_ltm(struct usb_device *udev) { } 3760f74631e3SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_ltm); 3761d388dab7SAlan Stern #endif 3762d388dab7SAlan Stern 37631da177e4SLinus Torvalds 37641da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 37651da177e4SLinus Torvalds * 37661da177e4SLinus Torvalds * Between connect detection and reset signaling there must be a delay 37671da177e4SLinus Torvalds * of 100ms at least for debounce and power-settling. The corresponding 37681da177e4SLinus Torvalds * timer shall restart whenever the downstream port detects a disconnect. 37691da177e4SLinus Torvalds * 37701da177e4SLinus Torvalds * Apparently there are some bluetooth and irda-dongles and a number of 37711da177e4SLinus Torvalds * low-speed devices for which this debounce period may last over a second. 37721da177e4SLinus Torvalds * Not covered by the spec - but easy to deal with. 37731da177e4SLinus Torvalds * 37741da177e4SLinus Torvalds * This implementation uses a 1500ms total debounce timeout; if the 37751da177e4SLinus Torvalds * connection isn't stable by then it returns -ETIMEDOUT. It checks 37761da177e4SLinus Torvalds * every 25ms for transient disconnects. When the port status has been 37771da177e4SLinus Torvalds * unchanged for 100ms it returns the port status. 37781da177e4SLinus Torvalds */ 37791da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1) 37801da177e4SLinus Torvalds { 37811da177e4SLinus Torvalds int ret; 37821da177e4SLinus Torvalds int total_time, stable_time = 0; 37831da177e4SLinus Torvalds u16 portchange, portstatus; 37841da177e4SLinus Torvalds unsigned connection = 0xffff; 37851da177e4SLinus Torvalds 37861da177e4SLinus Torvalds for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 37871da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 37881da177e4SLinus Torvalds if (ret < 0) 37891da177e4SLinus Torvalds return ret; 37901da177e4SLinus Torvalds 37911da177e4SLinus Torvalds if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 37921da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 37931da177e4SLinus Torvalds stable_time += HUB_DEBOUNCE_STEP; 37941da177e4SLinus Torvalds if (stable_time >= HUB_DEBOUNCE_STABLE) 37951da177e4SLinus Torvalds break; 37961da177e4SLinus Torvalds } else { 37971da177e4SLinus Torvalds stable_time = 0; 37981da177e4SLinus Torvalds connection = portstatus & USB_PORT_STAT_CONNECTION; 37991da177e4SLinus Torvalds } 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 38021da177e4SLinus Torvalds clear_port_feature(hub->hdev, port1, 38031da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 38041da177e4SLinus Torvalds } 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds if (total_time >= HUB_DEBOUNCE_TIMEOUT) 38071da177e4SLinus Torvalds break; 38081da177e4SLinus Torvalds msleep(HUB_DEBOUNCE_STEP); 38091da177e4SLinus Torvalds } 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds dev_dbg (hub->intfdev, 38121da177e4SLinus Torvalds "debounce: port %d: total %dms stable %dms status 0x%x\n", 38131da177e4SLinus Torvalds port1, total_time, stable_time, portstatus); 38141da177e4SLinus Torvalds 38151da177e4SLinus Torvalds if (stable_time < HUB_DEBOUNCE_STABLE) 38161da177e4SLinus Torvalds return -ETIMEDOUT; 38171da177e4SLinus Torvalds return portstatus; 38181da177e4SLinus Torvalds } 38191da177e4SLinus Torvalds 3820fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev) 38211da177e4SLinus Torvalds { 3822ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 3823ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 38242caf7fcdSAlan Stern usb_enable_endpoint(udev, &udev->ep0, true); 38251da177e4SLinus Torvalds } 3826fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit); 38271da177e4SLinus Torvalds 38281da177e4SLinus Torvalds #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 38291da177e4SLinus Torvalds #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 38301da177e4SLinus Torvalds 38314326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum) 38321da177e4SLinus Torvalds { 38331da177e4SLinus Torvalds int retval; 3834c6515272SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 38351da177e4SLinus Torvalds 3836c6515272SSarah Sharp /* 3837c6515272SSarah Sharp * The host controller will choose the device address, 3838c6515272SSarah Sharp * instead of the core having chosen it earlier 3839c6515272SSarah Sharp */ 3840c6515272SSarah Sharp if (!hcd->driver->address_device && devnum <= 1) 38411da177e4SLinus Torvalds return -EINVAL; 38421da177e4SLinus Torvalds if (udev->state == USB_STATE_ADDRESS) 38431da177e4SLinus Torvalds return 0; 38441da177e4SLinus Torvalds if (udev->state != USB_STATE_DEFAULT) 38451da177e4SLinus Torvalds return -EINVAL; 3846c8d4af8eSAndiry Xu if (hcd->driver->address_device) 3847c6515272SSarah Sharp retval = hcd->driver->address_device(hcd, udev); 3848c8d4af8eSAndiry Xu else 38491da177e4SLinus Torvalds retval = usb_control_msg(udev, usb_sndaddr0pipe(), 38504326ed0bSAlan Stern USB_REQ_SET_ADDRESS, 0, devnum, 0, 38511da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 38521da177e4SLinus Torvalds if (retval == 0) { 38533b29b68bSAlan Stern update_devnum(udev, devnum); 38544953d141SDavid Vrabel /* Device now using proper address. */ 38551da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_ADDRESS); 3856fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 38571da177e4SLinus Torvalds } 38581da177e4SLinus Torvalds return retval; 38591da177e4SLinus Torvalds } 38601da177e4SLinus Torvalds 38611da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor. 38621da177e4SLinus Torvalds * Device connection must be stable, no more debouncing needed. 38631da177e4SLinus Torvalds * Returns device in USB_STATE_ADDRESS, except on error. 38641da177e4SLinus Torvalds * 38651da177e4SLinus Torvalds * If this is called for an already-existing device (as part of 3866742120c6SMing Lei * usb_reset_and_verify_device), the caller must own the device lock. For a 38671da177e4SLinus Torvalds * newly detected device that is not accessible through any global 38681da177e4SLinus Torvalds * pointers, it's not necessary to lock the device. 38691da177e4SLinus Torvalds */ 38701da177e4SLinus Torvalds static int 38711da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 38721da177e4SLinus Torvalds int retry_counter) 38731da177e4SLinus Torvalds { 38744186ecf8SArjan van de Ven static DEFINE_MUTEX(usb_address0_mutex); 38751da177e4SLinus Torvalds 38761da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 3877e7b77172SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 38781da177e4SLinus Torvalds int i, j, retval; 38791da177e4SLinus Torvalds unsigned delay = HUB_SHORT_RESET_TIME; 38801da177e4SLinus Torvalds enum usb_device_speed oldspeed = udev->speed; 3881e538dfdaSMichal Nazarewicz const char *speed; 38824326ed0bSAlan Stern int devnum = udev->devnum; 38831da177e4SLinus Torvalds 38841da177e4SLinus Torvalds /* root hub ports have a slightly longer reset period 38851da177e4SLinus Torvalds * (from USB 2.0 spec, section 7.1.7.5) 38861da177e4SLinus Torvalds */ 38871da177e4SLinus Torvalds if (!hdev->parent) { 38881da177e4SLinus Torvalds delay = HUB_ROOT_RESET_TIME; 38891da177e4SLinus Torvalds if (port1 == hdev->bus->otg_port) 38901da177e4SLinus Torvalds hdev->bus->b_hnp_enable = 0; 38911da177e4SLinus Torvalds } 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds /* Some low speed devices have problems with the quick delay, so */ 38941da177e4SLinus Torvalds /* be a bit pessimistic with those devices. RHbug #23670 */ 38951da177e4SLinus Torvalds if (oldspeed == USB_SPEED_LOW) 38961da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 38971da177e4SLinus Torvalds 38984186ecf8SArjan van de Ven mutex_lock(&usb_address0_mutex); 38991da177e4SLinus Torvalds 39001da177e4SLinus Torvalds /* Reset the device; full speed may morph to high speed */ 3901e7b77172SSarah Sharp /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 390275d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false); 39031da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 39041da177e4SLinus Torvalds goto fail; 39051da177e4SLinus Torvalds /* success, speed is known */ 390607194ab7SLuben Tuikov 39071da177e4SLinus Torvalds retval = -ENODEV; 39081da177e4SLinus Torvalds 39091da177e4SLinus Torvalds if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { 39101da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset changed speed!\n"); 39111da177e4SLinus Torvalds goto fail; 39121da177e4SLinus Torvalds } 39131da177e4SLinus Torvalds oldspeed = udev->speed; 39141da177e4SLinus Torvalds 39151da177e4SLinus Torvalds /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 39161da177e4SLinus Torvalds * it's fixed size except for full speed devices. 39175bb6e0aeSInaky Perez-Gonzalez * For Wireless USB devices, ep0 max packet is always 512 (tho 39185bb6e0aeSInaky Perez-Gonzalez * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 39191da177e4SLinus Torvalds */ 39201da177e4SLinus Torvalds switch (udev->speed) { 39216b403b02SSarah Sharp case USB_SPEED_SUPER: 3922551cdbbeSGreg Kroah-Hartman case USB_SPEED_WIRELESS: /* fixed at 512 */ 3923551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 39245bb6e0aeSInaky Perez-Gonzalez break; 39251da177e4SLinus Torvalds case USB_SPEED_HIGH: /* fixed at 64 */ 3926551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 39271da177e4SLinus Torvalds break; 39281da177e4SLinus Torvalds case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 39291da177e4SLinus Torvalds /* to determine the ep0 maxpacket size, try to read 39301da177e4SLinus Torvalds * the device descriptor to get bMaxPacketSize0 and 39311da177e4SLinus Torvalds * then correct our initial guess. 39321da177e4SLinus Torvalds */ 3933551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 39341da177e4SLinus Torvalds break; 39351da177e4SLinus Torvalds case USB_SPEED_LOW: /* fixed at 8 */ 3936551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 39371da177e4SLinus Torvalds break; 39381da177e4SLinus Torvalds default: 39391da177e4SLinus Torvalds goto fail; 39401da177e4SLinus Torvalds } 39411da177e4SLinus Torvalds 3942e538dfdaSMichal Nazarewicz if (udev->speed == USB_SPEED_WIRELESS) 3943e538dfdaSMichal Nazarewicz speed = "variable speed Wireless"; 3944e538dfdaSMichal Nazarewicz else 3945e538dfdaSMichal Nazarewicz speed = usb_speed_string(udev->speed); 3946e538dfdaSMichal Nazarewicz 3947c6515272SSarah Sharp if (udev->speed != USB_SPEED_SUPER) 394883a07196SInaky Perez-Gonzalez dev_info(&udev->dev, 3949e538dfdaSMichal Nazarewicz "%s %s USB device number %d using %s\n", 3950e538dfdaSMichal Nazarewicz (udev->config) ? "reset" : "new", speed, 39513b29b68bSAlan Stern devnum, udev->bus->controller->driver->name); 39521da177e4SLinus Torvalds 39531da177e4SLinus Torvalds /* Set up TT records, if needed */ 39541da177e4SLinus Torvalds if (hdev->tt) { 39551da177e4SLinus Torvalds udev->tt = hdev->tt; 39561da177e4SLinus Torvalds udev->ttport = hdev->ttport; 39571da177e4SLinus Torvalds } else if (udev->speed != USB_SPEED_HIGH 39581da177e4SLinus Torvalds && hdev->speed == USB_SPEED_HIGH) { 3959d199c96dSAlan Stern if (!hub->tt.hub) { 3960d199c96dSAlan Stern dev_err(&udev->dev, "parent hub has no TT\n"); 3961d199c96dSAlan Stern retval = -EINVAL; 3962d199c96dSAlan Stern goto fail; 3963d199c96dSAlan Stern } 39641da177e4SLinus Torvalds udev->tt = &hub->tt; 39651da177e4SLinus Torvalds udev->ttport = port1; 39661da177e4SLinus Torvalds } 39671da177e4SLinus Torvalds 39681da177e4SLinus Torvalds /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 39691da177e4SLinus Torvalds * Because device hardware and firmware is sometimes buggy in 39701da177e4SLinus Torvalds * this area, and this is how Linux has done it for ages. 39711da177e4SLinus Torvalds * Change it cautiously. 39721da177e4SLinus Torvalds * 39731da177e4SLinus Torvalds * NOTE: If USE_NEW_SCHEME() is true we will start by issuing 39741da177e4SLinus Torvalds * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 39751da177e4SLinus Torvalds * so it may help with some non-standards-compliant devices. 39761da177e4SLinus Torvalds * Otherwise we start with SET_ADDRESS and then try to read the 39771da177e4SLinus Torvalds * first 8 bytes of the device descriptor to get the ep0 maxpacket 39781da177e4SLinus Torvalds * value. 39791da177e4SLinus Torvalds */ 39801da177e4SLinus Torvalds for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 3981c6515272SSarah Sharp if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) { 39821da177e4SLinus Torvalds struct usb_device_descriptor *buf; 39831da177e4SLinus Torvalds int r = 0; 39841da177e4SLinus Torvalds 39851da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE 64 39861da177e4SLinus Torvalds buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 39871da177e4SLinus Torvalds if (!buf) { 39881da177e4SLinus Torvalds retval = -ENOMEM; 39891da177e4SLinus Torvalds continue; 39901da177e4SLinus Torvalds } 39911da177e4SLinus Torvalds 3992b89ee19aSAlan Stern /* Retry on all errors; some devices are flakey. 3993b89ee19aSAlan Stern * 255 is for WUSB devices, we actually need to use 3994b89ee19aSAlan Stern * 512 (WUSB1.0[4.8.1]). 39951da177e4SLinus Torvalds */ 39961da177e4SLinus Torvalds for (j = 0; j < 3; ++j) { 39971da177e4SLinus Torvalds buf->bMaxPacketSize0 = 0; 39981da177e4SLinus Torvalds r = usb_control_msg(udev, usb_rcvaddr0pipe(), 39991da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 40001da177e4SLinus Torvalds USB_DT_DEVICE << 8, 0, 40011da177e4SLinus Torvalds buf, GET_DESCRIPTOR_BUFSIZE, 4002fd7c519dSJaroslav Kysela initial_descriptor_timeout); 40031da177e4SLinus Torvalds switch (buf->bMaxPacketSize0) { 40045bb6e0aeSInaky Perez-Gonzalez case 8: case 16: case 32: case 64: case 255: 40051da177e4SLinus Torvalds if (buf->bDescriptorType == 40061da177e4SLinus Torvalds USB_DT_DEVICE) { 40071da177e4SLinus Torvalds r = 0; 40081da177e4SLinus Torvalds break; 40091da177e4SLinus Torvalds } 40101da177e4SLinus Torvalds /* FALL THROUGH */ 40111da177e4SLinus Torvalds default: 40121da177e4SLinus Torvalds if (r == 0) 40131da177e4SLinus Torvalds r = -EPROTO; 40141da177e4SLinus Torvalds break; 40151da177e4SLinus Torvalds } 40161da177e4SLinus Torvalds if (r == 0) 40171da177e4SLinus Torvalds break; 40181da177e4SLinus Torvalds } 40191da177e4SLinus Torvalds udev->descriptor.bMaxPacketSize0 = 40201da177e4SLinus Torvalds buf->bMaxPacketSize0; 40211da177e4SLinus Torvalds kfree(buf); 40221da177e4SLinus Torvalds 402375d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false); 40241da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 40251da177e4SLinus Torvalds goto fail; 40261da177e4SLinus Torvalds if (oldspeed != udev->speed) { 40271da177e4SLinus Torvalds dev_dbg(&udev->dev, 40281da177e4SLinus Torvalds "device reset changed speed!\n"); 40291da177e4SLinus Torvalds retval = -ENODEV; 40301da177e4SLinus Torvalds goto fail; 40311da177e4SLinus Torvalds } 40321da177e4SLinus Torvalds if (r) { 4033b9cef6c3SWu Fengguang dev_err(&udev->dev, 4034b9cef6c3SWu Fengguang "device descriptor read/64, error %d\n", 4035b9cef6c3SWu Fengguang r); 40361da177e4SLinus Torvalds retval = -EMSGSIZE; 40371da177e4SLinus Torvalds continue; 40381da177e4SLinus Torvalds } 40391da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE 40401da177e4SLinus Torvalds } 40411da177e4SLinus Torvalds 40426c529cdcSInaky Perez-Gonzalez /* 40436c529cdcSInaky Perez-Gonzalez * If device is WUSB, we already assigned an 40446c529cdcSInaky Perez-Gonzalez * unauthorized address in the Connect Ack sequence; 40456c529cdcSInaky Perez-Gonzalez * authorization will assign the final address. 40466c529cdcSInaky Perez-Gonzalez */ 40476c529cdcSInaky Perez-Gonzalez if (udev->wusb == 0) { 40481da177e4SLinus Torvalds for (j = 0; j < SET_ADDRESS_TRIES; ++j) { 40494326ed0bSAlan Stern retval = hub_set_address(udev, devnum); 40501da177e4SLinus Torvalds if (retval >= 0) 40511da177e4SLinus Torvalds break; 40521da177e4SLinus Torvalds msleep(200); 40531da177e4SLinus Torvalds } 40541da177e4SLinus Torvalds if (retval < 0) { 40551da177e4SLinus Torvalds dev_err(&udev->dev, 40561da177e4SLinus Torvalds "device not accepting address %d, error %d\n", 40574326ed0bSAlan Stern devnum, retval); 40581da177e4SLinus Torvalds goto fail; 40591da177e4SLinus Torvalds } 4060c6515272SSarah Sharp if (udev->speed == USB_SPEED_SUPER) { 4061c6515272SSarah Sharp devnum = udev->devnum; 4062c6515272SSarah Sharp dev_info(&udev->dev, 40633b29b68bSAlan Stern "%s SuperSpeed USB device number %d using %s\n", 4064c6515272SSarah Sharp (udev->config) ? "reset" : "new", 40653b29b68bSAlan Stern devnum, udev->bus->controller->driver->name); 4066c6515272SSarah Sharp } 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds /* cope with hardware quirkiness: 40691da177e4SLinus Torvalds * - let SET_ADDRESS settle, some device hardware wants it 40701da177e4SLinus Torvalds * - read ep0 maxpacket even for high and low speed, 40711da177e4SLinus Torvalds */ 40721da177e4SLinus Torvalds msleep(10); 4073c6515272SSarah Sharp if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) 40741da177e4SLinus Torvalds break; 40756c529cdcSInaky Perez-Gonzalez } 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, 8); 40781da177e4SLinus Torvalds if (retval < 8) { 4079b9cef6c3SWu Fengguang dev_err(&udev->dev, 4080b9cef6c3SWu Fengguang "device descriptor read/8, error %d\n", 4081b9cef6c3SWu Fengguang retval); 40821da177e4SLinus Torvalds if (retval >= 0) 40831da177e4SLinus Torvalds retval = -EMSGSIZE; 40841da177e4SLinus Torvalds } else { 40851da177e4SLinus Torvalds retval = 0; 40861da177e4SLinus Torvalds break; 40871da177e4SLinus Torvalds } 40881da177e4SLinus Torvalds } 40891da177e4SLinus Torvalds if (retval) 40901da177e4SLinus Torvalds goto fail; 40911da177e4SLinus Torvalds 4092b76baa81SPeter Chen if (hcd->phy && !hdev->parent) 4093ac96511bSPeter Chen usb_phy_notify_connect(hcd->phy, udev->speed); 4094b76baa81SPeter Chen 4095d8aec3dbSElric Fu /* 4096d8aec3dbSElric Fu * Some superspeed devices have finished the link training process 4097d8aec3dbSElric Fu * and attached to a superspeed hub port, but the device descriptor 4098d8aec3dbSElric Fu * got from those devices show they aren't superspeed devices. Warm 4099d8aec3dbSElric Fu * reset the port attached by the devices can fix them. 4100d8aec3dbSElric Fu */ 4101d8aec3dbSElric Fu if ((udev->speed == USB_SPEED_SUPER) && 4102d8aec3dbSElric Fu (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 4103d8aec3dbSElric Fu dev_err(&udev->dev, "got a wrong device descriptor, " 4104d8aec3dbSElric Fu "warm reset device\n"); 4105d8aec3dbSElric Fu hub_port_reset(hub, port1, udev, 4106d8aec3dbSElric Fu HUB_BH_RESET_TIME, true); 4107d8aec3dbSElric Fu retval = -EINVAL; 4108d8aec3dbSElric Fu goto fail; 4109d8aec3dbSElric Fu } 4110d8aec3dbSElric Fu 41116b403b02SSarah Sharp if (udev->descriptor.bMaxPacketSize0 == 0xff || 41126b403b02SSarah Sharp udev->speed == USB_SPEED_SUPER) 41136b403b02SSarah Sharp i = 512; 41146b403b02SSarah Sharp else 41156b403b02SSarah Sharp i = udev->descriptor.bMaxPacketSize0; 411629cc8897SKuninori Morimoto if (usb_endpoint_maxp(&udev->ep0.desc) != i) { 411756626a72SAlan Stern if (udev->speed == USB_SPEED_LOW || 41181da177e4SLinus Torvalds !(i == 8 || i == 16 || i == 32 || i == 64)) { 411956626a72SAlan Stern dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); 41201da177e4SLinus Torvalds retval = -EMSGSIZE; 41211da177e4SLinus Torvalds goto fail; 41221da177e4SLinus Torvalds } 412356626a72SAlan Stern if (udev->speed == USB_SPEED_FULL) 41241da177e4SLinus Torvalds dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 412556626a72SAlan Stern else 412656626a72SAlan Stern dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 41271da177e4SLinus Torvalds udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 4128fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 41291da177e4SLinus Torvalds } 41301da177e4SLinus Torvalds 41311da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 41321da177e4SLinus Torvalds if (retval < (signed)sizeof(udev->descriptor)) { 4133b9cef6c3SWu Fengguang dev_err(&udev->dev, "device descriptor read/all, error %d\n", 4134b9cef6c3SWu Fengguang retval); 41351da177e4SLinus Torvalds if (retval >= 0) 41361da177e4SLinus Torvalds retval = -ENOMSG; 41371da177e4SLinus Torvalds goto fail; 41381da177e4SLinus Torvalds } 41391da177e4SLinus Torvalds 41401ff4df56SAndiry Xu if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 41411ff4df56SAndiry Xu retval = usb_get_bos_descriptor(udev); 414251e0a012SSarah Sharp if (!retval) { 4143d9b2099cSSarah Sharp udev->lpm_capable = usb_device_supports_lpm(udev); 414451e0a012SSarah Sharp usb_set_lpm_parameters(udev); 414551e0a012SSarah Sharp } 41461ff4df56SAndiry Xu } 41473148bf04SAndiry Xu 41481da177e4SLinus Torvalds retval = 0; 414948f24970SAlek Du /* notify HCD that we have a device connected and addressed */ 415048f24970SAlek Du if (hcd->driver->update_device) 415148f24970SAlek Du hcd->driver->update_device(hcd, udev); 41521da177e4SLinus Torvalds fail: 41534326ed0bSAlan Stern if (retval) { 41541da177e4SLinus Torvalds hub_port_disable(hub, port1, 0); 41553b29b68bSAlan Stern update_devnum(udev, devnum); /* for disconnect processing */ 41564326ed0bSAlan Stern } 41574186ecf8SArjan van de Ven mutex_unlock(&usb_address0_mutex); 41581da177e4SLinus Torvalds return retval; 41591da177e4SLinus Torvalds } 41601da177e4SLinus Torvalds 41611da177e4SLinus Torvalds static void 41621da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) 41631da177e4SLinus Torvalds { 41641da177e4SLinus Torvalds struct usb_qualifier_descriptor *qual; 41651da177e4SLinus Torvalds int status; 41661da177e4SLinus Torvalds 4167e94b1766SChristoph Lameter qual = kmalloc (sizeof *qual, GFP_KERNEL); 41681da177e4SLinus Torvalds if (qual == NULL) 41691da177e4SLinus Torvalds return; 41701da177e4SLinus Torvalds 41711da177e4SLinus Torvalds status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, 41721da177e4SLinus Torvalds qual, sizeof *qual); 41731da177e4SLinus Torvalds if (status == sizeof *qual) { 41741da177e4SLinus Torvalds dev_info(&udev->dev, "not running at top speed; " 41751da177e4SLinus Torvalds "connect to a high speed hub\n"); 41761da177e4SLinus Torvalds /* hub LEDs are probably harder to miss than syslog */ 41771da177e4SLinus Torvalds if (hub->has_indicators) { 41781da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 4179c4028958SDavid Howells schedule_delayed_work (&hub->leds, 0); 41801da177e4SLinus Torvalds } 41811da177e4SLinus Torvalds } 41821da177e4SLinus Torvalds kfree(qual); 41831da177e4SLinus Torvalds } 41841da177e4SLinus Torvalds 41851da177e4SLinus Torvalds static unsigned 41861da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub) 41871da177e4SLinus Torvalds { 41881da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 41891da177e4SLinus Torvalds int remaining; 419055c52718SAlan Stern int port1; 41911da177e4SLinus Torvalds 419255c52718SAlan Stern if (!hub->limited_power) 41931da177e4SLinus Torvalds return 0; 41941da177e4SLinus Torvalds 419555c52718SAlan Stern remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 419655c52718SAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 4197ff823c79SLan Tianyu struct usb_device *udev = hub->ports[port1 - 1]->child; 419855c52718SAlan Stern int delta; 41991da177e4SLinus Torvalds 42001da177e4SLinus Torvalds if (!udev) 42011da177e4SLinus Torvalds continue; 42021da177e4SLinus Torvalds 420355c52718SAlan Stern /* Unconfigured devices may not use more than 100mA, 420455c52718SAlan Stern * or 8mA for OTG ports */ 42051da177e4SLinus Torvalds if (udev->actconfig) 420655c52718SAlan Stern delta = udev->actconfig->desc.bMaxPower * 2; 420755c52718SAlan Stern else if (port1 != udev->bus->otg_port || hdev->parent) 420855c52718SAlan Stern delta = 100; 42091da177e4SLinus Torvalds else 421055c52718SAlan Stern delta = 8; 421155c52718SAlan Stern if (delta > hub->mA_per_port) 4212b9cef6c3SWu Fengguang dev_warn(&udev->dev, 4213b9cef6c3SWu Fengguang "%dmA is over %umA budget for port %d!\n", 421455c52718SAlan Stern delta, hub->mA_per_port, port1); 42151da177e4SLinus Torvalds remaining -= delta; 42161da177e4SLinus Torvalds } 42171da177e4SLinus Torvalds if (remaining < 0) { 421855c52718SAlan Stern dev_warn(hub->intfdev, "%dmA over power budget!\n", 421955c52718SAlan Stern - remaining); 42201da177e4SLinus Torvalds remaining = 0; 42211da177e4SLinus Torvalds } 42221da177e4SLinus Torvalds return remaining; 42231da177e4SLinus Torvalds } 42241da177e4SLinus Torvalds 42251da177e4SLinus Torvalds /* Handle physical or logical connection change events. 42261da177e4SLinus Torvalds * This routine is called when: 42271da177e4SLinus Torvalds * a port connection-change occurs; 42281da177e4SLinus Torvalds * a port enable-change occurs (often caused by EMI); 4229742120c6SMing Lei * usb_reset_and_verify_device() encounters changed descriptors (as from 42301da177e4SLinus Torvalds * a firmware download) 42311da177e4SLinus Torvalds * caller already locked the hub 42321da177e4SLinus Torvalds */ 42331da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1, 42341da177e4SLinus Torvalds u16 portstatus, u16 portchange) 42351da177e4SLinus Torvalds { 42361da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 42371da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 423890da096eSBalaji Rao struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 423924618b0cSAlan Stern unsigned wHubCharacteristics = 424024618b0cSAlan Stern le16_to_cpu(hub->descriptor->wHubCharacteristics); 42418808f00cSAlan Stern struct usb_device *udev; 42421da177e4SLinus Torvalds int status, i; 42431da177e4SLinus Torvalds 42441da177e4SLinus Torvalds dev_dbg (hub_dev, 42451da177e4SLinus Torvalds "port %d, status %04x, change %04x, %s\n", 4246131dec34SSarah Sharp port1, portstatus, portchange, portspeed(hub, portstatus)); 42471da177e4SLinus Torvalds 42481da177e4SLinus Torvalds if (hub->has_indicators) { 42491da177e4SLinus Torvalds set_port_led(hub, port1, HUB_LED_AUTO); 42501da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_AUTO; 42511da177e4SLinus Torvalds } 42521da177e4SLinus Torvalds 42531da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 42541da177e4SLinus Torvalds /* during HNP, don't repeat the debounce */ 42551da177e4SLinus Torvalds if (hdev->bus->is_b_host) 425624618b0cSAlan Stern portchange &= ~(USB_PORT_STAT_C_CONNECTION | 425724618b0cSAlan Stern USB_PORT_STAT_C_ENABLE); 42581da177e4SLinus Torvalds #endif 42591da177e4SLinus Torvalds 42608808f00cSAlan Stern /* Try to resuscitate an existing device */ 4261ff823c79SLan Tianyu udev = hub->ports[port1 - 1]->child; 42628808f00cSAlan Stern if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 42638808f00cSAlan Stern udev->state != USB_STATE_NOTATTACHED) { 42648808f00cSAlan Stern usb_lock_device(udev); 42658808f00cSAlan Stern if (portstatus & USB_PORT_STAT_ENABLE) { 42668808f00cSAlan Stern status = 0; /* Nothing to do */ 42678808f00cSAlan Stern 42688808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND 42695257d97aSAlan Stern } else if (udev->state == USB_STATE_SUSPENDED && 42705257d97aSAlan Stern udev->persist_enabled) { 42718808f00cSAlan Stern /* For a suspended device, treat this as a 42728808f00cSAlan Stern * remote wakeup event. 42738808f00cSAlan Stern */ 42740534d468SAlan Stern status = usb_remote_wakeup(udev); 42758808f00cSAlan Stern #endif 42768808f00cSAlan Stern 42778808f00cSAlan Stern } else { 42785257d97aSAlan Stern status = -ENODEV; /* Don't resuscitate */ 42798808f00cSAlan Stern } 42808808f00cSAlan Stern usb_unlock_device(udev); 42818808f00cSAlan Stern 42828808f00cSAlan Stern if (status == 0) { 42838808f00cSAlan Stern clear_bit(port1, hub->change_bits); 42848808f00cSAlan Stern return; 42858808f00cSAlan Stern } 42868808f00cSAlan Stern } 42878808f00cSAlan Stern 428824618b0cSAlan Stern /* Disconnect any existing devices under this port */ 4289b76baa81SPeter Chen if (udev) { 4290b76baa81SPeter Chen if (hcd->phy && !hdev->parent && 4291b76baa81SPeter Chen !(portstatus & USB_PORT_STAT_CONNECTION)) 4292ac96511bSPeter Chen usb_phy_notify_disconnect(hcd->phy, udev->speed); 4293ff823c79SLan Tianyu usb_disconnect(&hub->ports[port1 - 1]->child); 4294b76baa81SPeter Chen } 429524618b0cSAlan Stern clear_bit(port1, hub->change_bits); 429624618b0cSAlan Stern 4297253e0572SAlan Stern /* We can forget about a "removed" device when there's a physical 4298253e0572SAlan Stern * disconnect or the connect status changes. 4299253e0572SAlan Stern */ 4300253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 4301253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION)) 4302253e0572SAlan Stern clear_bit(port1, hub->removed_bits); 4303253e0572SAlan Stern 43045257d97aSAlan Stern if (portchange & (USB_PORT_STAT_C_CONNECTION | 43055257d97aSAlan Stern USB_PORT_STAT_C_ENABLE)) { 43065257d97aSAlan Stern status = hub_port_debounce(hub, port1); 43075257d97aSAlan Stern if (status < 0) { 43085257d97aSAlan Stern if (printk_ratelimit()) 43095257d97aSAlan Stern dev_err(hub_dev, "connect-debounce failed, " 43105257d97aSAlan Stern "port %d disabled\n", port1); 43115257d97aSAlan Stern portstatus &= ~USB_PORT_STAT_CONNECTION; 43125257d97aSAlan Stern } else { 43135257d97aSAlan Stern portstatus = status; 43145257d97aSAlan Stern } 43155257d97aSAlan Stern } 43165257d97aSAlan Stern 4317253e0572SAlan Stern /* Return now if debouncing failed or nothing is connected or 4318253e0572SAlan Stern * the device was "removed". 4319253e0572SAlan Stern */ 4320253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 4321253e0572SAlan Stern test_bit(port1, hub->removed_bits)) { 43221da177e4SLinus Torvalds 43231da177e4SLinus Torvalds /* maybe switch power back on (e.g. root hub was reset) */ 432474ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 43250ed9a57eSAndiry Xu && !port_is_power_on(hub, portstatus)) 43261da177e4SLinus Torvalds set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 43271da177e4SLinus Torvalds 43281da177e4SLinus Torvalds if (portstatus & USB_PORT_STAT_ENABLE) 43291da177e4SLinus Torvalds goto done; 43301da177e4SLinus Torvalds return; 43311da177e4SLinus Torvalds } 43321da177e4SLinus Torvalds 43331da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; i++) { 43341da177e4SLinus Torvalds 43351da177e4SLinus Torvalds /* reallocate for each attempt, since references 43361da177e4SLinus Torvalds * to the previous one can escape in various ways 43371da177e4SLinus Torvalds */ 43381da177e4SLinus Torvalds udev = usb_alloc_dev(hdev, hdev->bus, port1); 43391da177e4SLinus Torvalds if (!udev) { 43401da177e4SLinus Torvalds dev_err (hub_dev, 43411da177e4SLinus Torvalds "couldn't allocate port %d usb_device\n", 43421da177e4SLinus Torvalds port1); 43431da177e4SLinus Torvalds goto done; 43441da177e4SLinus Torvalds } 43451da177e4SLinus Torvalds 43461da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_POWERED); 434755c52718SAlan Stern udev->bus_mA = hub->mA_per_port; 4348b6956ffaSAlan Stern udev->level = hdev->level + 1; 43498af548dcSInaky Perez-Gonzalez udev->wusb = hub_is_wusb(hub); 43501da177e4SLinus Torvalds 4351131dec34SSarah Sharp /* Only USB 3.0 devices are connected to SuperSpeed hubs. */ 4352131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev)) 4353e7b77172SSarah Sharp udev->speed = USB_SPEED_SUPER; 4354e7b77172SSarah Sharp else 4355e7b77172SSarah Sharp udev->speed = USB_SPEED_UNKNOWN; 4356e7b77172SSarah Sharp 43573b29b68bSAlan Stern choose_devnum(udev); 4358c6515272SSarah Sharp if (udev->devnum <= 0) { 4359c6515272SSarah Sharp status = -ENOTCONN; /* Don't retry */ 4360c6515272SSarah Sharp goto loop; 4361c6515272SSarah Sharp } 4362c6515272SSarah Sharp 4363e7b77172SSarah Sharp /* reset (non-USB 3.0 devices) and get descriptor */ 43641da177e4SLinus Torvalds status = hub_port_init(hub, udev, port1, i); 43651da177e4SLinus Torvalds if (status < 0) 43661da177e4SLinus Torvalds goto loop; 43671da177e4SLinus Torvalds 436893362a87SPhil Dibowitz usb_detect_quirks(udev); 436993362a87SPhil Dibowitz if (udev->quirks & USB_QUIRK_DELAY_INIT) 437093362a87SPhil Dibowitz msleep(1000); 437193362a87SPhil Dibowitz 43721da177e4SLinus Torvalds /* consecutive bus-powered hubs aren't reliable; they can 43731da177e4SLinus Torvalds * violate the voltage drop budget. if the new child has 43741da177e4SLinus Torvalds * a "powered" LED, users should notice we didn't enable it 43751da177e4SLinus Torvalds * (without reading syslog), even without per-port LEDs 43761da177e4SLinus Torvalds * on the parent. 43771da177e4SLinus Torvalds */ 43781da177e4SLinus Torvalds if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 437955c52718SAlan Stern && udev->bus_mA <= 100) { 43801da177e4SLinus Torvalds u16 devstat; 43811da177e4SLinus Torvalds 43821da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, 43831da177e4SLinus Torvalds &devstat); 438455c52718SAlan Stern if (status < 2) { 43851da177e4SLinus Torvalds dev_dbg(&udev->dev, "get status %d ?\n", status); 43861da177e4SLinus Torvalds goto loop_disable; 43871da177e4SLinus Torvalds } 438855c52718SAlan Stern le16_to_cpus(&devstat); 43891da177e4SLinus Torvalds if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 43901da177e4SLinus Torvalds dev_err(&udev->dev, 43911da177e4SLinus Torvalds "can't connect bus-powered hub " 43921da177e4SLinus Torvalds "to this port\n"); 43931da177e4SLinus Torvalds if (hub->has_indicators) { 43941da177e4SLinus Torvalds hub->indicator[port1-1] = 43951da177e4SLinus Torvalds INDICATOR_AMBER_BLINK; 4396c4028958SDavid Howells schedule_delayed_work (&hub->leds, 0); 43971da177e4SLinus Torvalds } 43981da177e4SLinus Torvalds status = -ENOTCONN; /* Don't retry */ 43991da177e4SLinus Torvalds goto loop_disable; 44001da177e4SLinus Torvalds } 44011da177e4SLinus Torvalds } 44021da177e4SLinus Torvalds 44031da177e4SLinus Torvalds /* check for devices running slower than they could */ 44041da177e4SLinus Torvalds if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 44051da177e4SLinus Torvalds && udev->speed == USB_SPEED_FULL 44061da177e4SLinus Torvalds && highspeed_hubs != 0) 44071da177e4SLinus Torvalds check_highspeed (hub, udev, port1); 44081da177e4SLinus Torvalds 4409fa286188SGreg Kroah-Hartman /* Store the parent's children[] pointer. At this point 44101da177e4SLinus Torvalds * udev becomes globally accessible, although presumably 44111da177e4SLinus Torvalds * no one will look at it until hdev is unlocked. 44121da177e4SLinus Torvalds */ 44131da177e4SLinus Torvalds status = 0; 44141da177e4SLinus Torvalds 44151da177e4SLinus Torvalds /* We mustn't add new devices if the parent hub has 44161da177e4SLinus Torvalds * been disconnected; we would race with the 44171da177e4SLinus Torvalds * recursively_mark_NOTATTACHED() routine. 44181da177e4SLinus Torvalds */ 44191da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 44201da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) 44211da177e4SLinus Torvalds status = -ENOTCONN; 44221da177e4SLinus Torvalds else 4423ff823c79SLan Tianyu hub->ports[port1 - 1]->child = udev; 44241da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 44251da177e4SLinus Torvalds 44261da177e4SLinus Torvalds /* Run it through the hoops (find a driver, etc) */ 44271da177e4SLinus Torvalds if (!status) { 44281da177e4SLinus Torvalds status = usb_new_device(udev); 44291da177e4SLinus Torvalds if (status) { 44301da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 4431ff823c79SLan Tianyu hub->ports[port1 - 1]->child = NULL; 44321da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 44331da177e4SLinus Torvalds } 44341da177e4SLinus Torvalds } 44351da177e4SLinus Torvalds 44361da177e4SLinus Torvalds if (status) 44371da177e4SLinus Torvalds goto loop_disable; 44381da177e4SLinus Torvalds 44391da177e4SLinus Torvalds status = hub_power_remaining(hub); 44401da177e4SLinus Torvalds if (status) 444155c52718SAlan Stern dev_dbg(hub_dev, "%dmA power budget left\n", status); 44421da177e4SLinus Torvalds 44431da177e4SLinus Torvalds return; 44441da177e4SLinus Torvalds 44451da177e4SLinus Torvalds loop_disable: 44461da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 44471da177e4SLinus Torvalds loop: 4448fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 44493b29b68bSAlan Stern release_devnum(udev); 4450f7410cedSHerbert Xu hub_free_dev(udev); 44511da177e4SLinus Torvalds usb_put_dev(udev); 4452ffcdc18dSVikram Pandita if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 44531da177e4SLinus Torvalds break; 44541da177e4SLinus Torvalds } 44553a31155cSAlan Stern if (hub->hdev->parent || 44563a31155cSAlan Stern !hcd->driver->port_handed_over || 44573a31155cSAlan Stern !(hcd->driver->port_handed_over)(hcd, port1)) 44583a31155cSAlan Stern dev_err(hub_dev, "unable to enumerate USB device on port %d\n", 44593a31155cSAlan Stern port1); 44601da177e4SLinus Torvalds 44611da177e4SLinus Torvalds done: 44621da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 446390da096eSBalaji Rao if (hcd->driver->relinquish_port && !hub->hdev->parent) 446490da096eSBalaji Rao hcd->driver->relinquish_port(hcd, port1); 44651da177e4SLinus Torvalds } 44661da177e4SLinus Torvalds 4467714b07beSSarah Sharp /* Returns 1 if there was a remote wakeup and a connect status change. */ 4468714b07beSSarah Sharp static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 446972937e1eSSarah Sharp u16 portstatus, u16 portchange) 4470714b07beSSarah Sharp { 4471714b07beSSarah Sharp struct usb_device *hdev; 4472714b07beSSarah Sharp struct usb_device *udev; 4473714b07beSSarah Sharp int connect_change = 0; 4474714b07beSSarah Sharp int ret; 4475714b07beSSarah Sharp 4476714b07beSSarah Sharp hdev = hub->hdev; 4477ff823c79SLan Tianyu udev = hub->ports[port - 1]->child; 44784ee823b8SSarah Sharp if (!hub_is_superspeed(hdev)) { 4479714b07beSSarah Sharp if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 4480714b07beSSarah Sharp return 0; 4481714b07beSSarah Sharp clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 44824ee823b8SSarah Sharp } else { 44834ee823b8SSarah Sharp if (!udev || udev->state != USB_STATE_SUSPENDED || 448472937e1eSSarah Sharp (portstatus & USB_PORT_STAT_LINK_STATE) != 448572937e1eSSarah Sharp USB_SS_PORT_LS_U0) 44864ee823b8SSarah Sharp return 0; 44874ee823b8SSarah Sharp } 4488714b07beSSarah Sharp 4489714b07beSSarah Sharp if (udev) { 4490714b07beSSarah Sharp /* TRSMRCY = 10 msec */ 4491714b07beSSarah Sharp msleep(10); 4492714b07beSSarah Sharp 4493714b07beSSarah Sharp usb_lock_device(udev); 4494714b07beSSarah Sharp ret = usb_remote_wakeup(udev); 4495714b07beSSarah Sharp usb_unlock_device(udev); 4496714b07beSSarah Sharp if (ret < 0) 4497714b07beSSarah Sharp connect_change = 1; 4498714b07beSSarah Sharp } else { 4499714b07beSSarah Sharp ret = -ENODEV; 4500714b07beSSarah Sharp hub_port_disable(hub, port, 1); 4501714b07beSSarah Sharp } 4502714b07beSSarah Sharp dev_dbg(hub->intfdev, "resume on port %d, status %d\n", 4503714b07beSSarah Sharp port, ret); 4504714b07beSSarah Sharp return connect_change; 4505714b07beSSarah Sharp } 4506714b07beSSarah Sharp 45071da177e4SLinus Torvalds static void hub_events(void) 45081da177e4SLinus Torvalds { 45091da177e4SLinus Torvalds struct list_head *tmp; 45101da177e4SLinus Torvalds struct usb_device *hdev; 45111da177e4SLinus Torvalds struct usb_interface *intf; 45121da177e4SLinus Torvalds struct usb_hub *hub; 45131da177e4SLinus Torvalds struct device *hub_dev; 45141da177e4SLinus Torvalds u16 hubstatus; 45151da177e4SLinus Torvalds u16 hubchange; 45161da177e4SLinus Torvalds u16 portstatus; 45171da177e4SLinus Torvalds u16 portchange; 45181da177e4SLinus Torvalds int i, ret; 45194ee823b8SSarah Sharp int connect_change, wakeup_change; 45201da177e4SLinus Torvalds 45211da177e4SLinus Torvalds /* 45221da177e4SLinus Torvalds * We restart the list every time to avoid a deadlock with 45231da177e4SLinus Torvalds * deleting hubs downstream from this one. This should be 45241da177e4SLinus Torvalds * safe since we delete the hub from the event list. 45251da177e4SLinus Torvalds * Not the most efficient, but avoids deadlocks. 45261da177e4SLinus Torvalds */ 45271da177e4SLinus Torvalds while (1) { 45281da177e4SLinus Torvalds 45291da177e4SLinus Torvalds /* Grab the first entry at the beginning of the list */ 45301da177e4SLinus Torvalds spin_lock_irq(&hub_event_lock); 45311da177e4SLinus Torvalds if (list_empty(&hub_event_list)) { 45321da177e4SLinus Torvalds spin_unlock_irq(&hub_event_lock); 45331da177e4SLinus Torvalds break; 45341da177e4SLinus Torvalds } 45351da177e4SLinus Torvalds 45361da177e4SLinus Torvalds tmp = hub_event_list.next; 45371da177e4SLinus Torvalds list_del_init(tmp); 45381da177e4SLinus Torvalds 45391da177e4SLinus Torvalds hub = list_entry(tmp, struct usb_hub, event_list); 4540e8054854SAlan Stern kref_get(&hub->kref); 4541e8054854SAlan Stern spin_unlock_irq(&hub_event_lock); 45421da177e4SLinus Torvalds 4543e8054854SAlan Stern hdev = hub->hdev; 4544e8054854SAlan Stern hub_dev = hub->intfdev; 4545e8054854SAlan Stern intf = to_usb_interface(hub_dev); 454640f122f3SAlan Stern dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 45471da177e4SLinus Torvalds hdev->state, hub->descriptor 45481da177e4SLinus Torvalds ? hub->descriptor->bNbrPorts 45491da177e4SLinus Torvalds : 0, 45501da177e4SLinus Torvalds /* NOTE: expects max 15 ports... */ 45511da177e4SLinus Torvalds (u16) hub->change_bits[0], 455240f122f3SAlan Stern (u16) hub->event_bits[0]); 45531da177e4SLinus Torvalds 45541da177e4SLinus Torvalds /* Lock the device, then check to see if we were 45551da177e4SLinus Torvalds * disconnected while waiting for the lock to succeed. */ 455606b84e8aSAlan Stern usb_lock_device(hdev); 4557e8054854SAlan Stern if (unlikely(hub->disconnected)) 45589bbdf1e0SAlan Stern goto loop_disconnected; 45591da177e4SLinus Torvalds 45601da177e4SLinus Torvalds /* If the hub has died, clean up after it */ 45611da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) { 45627de18d8bSAlan Stern hub->error = -ENODEV; 45634330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 45641da177e4SLinus Torvalds goto loop; 45651da177e4SLinus Torvalds } 45661da177e4SLinus Torvalds 456740f122f3SAlan Stern /* Autoresume */ 456840f122f3SAlan Stern ret = usb_autopm_get_interface(intf); 456940f122f3SAlan Stern if (ret) { 457040f122f3SAlan Stern dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 45711da177e4SLinus Torvalds goto loop; 457240f122f3SAlan Stern } 457340f122f3SAlan Stern 457440f122f3SAlan Stern /* If this is an inactive hub, do nothing */ 457540f122f3SAlan Stern if (hub->quiescing) 457640f122f3SAlan Stern goto loop_autopm; 45771da177e4SLinus Torvalds 45781da177e4SLinus Torvalds if (hub->error) { 45791da177e4SLinus Torvalds dev_dbg (hub_dev, "resetting for error %d\n", 45801da177e4SLinus Torvalds hub->error); 45811da177e4SLinus Torvalds 4582742120c6SMing Lei ret = usb_reset_device(hdev); 45831da177e4SLinus Torvalds if (ret) { 45841da177e4SLinus Torvalds dev_dbg (hub_dev, 45851da177e4SLinus Torvalds "error resetting hub: %d\n", ret); 458640f122f3SAlan Stern goto loop_autopm; 45871da177e4SLinus Torvalds } 45881da177e4SLinus Torvalds 45891da177e4SLinus Torvalds hub->nerrors = 0; 45901da177e4SLinus Torvalds hub->error = 0; 45911da177e4SLinus Torvalds } 45921da177e4SLinus Torvalds 45931da177e4SLinus Torvalds /* deal with port status changes */ 45941da177e4SLinus Torvalds for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { 45951da177e4SLinus Torvalds if (test_bit(i, hub->busy_bits)) 45961da177e4SLinus Torvalds continue; 45971da177e4SLinus Torvalds connect_change = test_bit(i, hub->change_bits); 459872937e1eSSarah Sharp wakeup_change = test_and_clear_bit(i, hub->wakeup_bits); 45991da177e4SLinus Torvalds if (!test_and_clear_bit(i, hub->event_bits) && 46004ee823b8SSarah Sharp !connect_change && !wakeup_change) 46011da177e4SLinus Torvalds continue; 46021da177e4SLinus Torvalds 46031da177e4SLinus Torvalds ret = hub_port_status(hub, i, 46041da177e4SLinus Torvalds &portstatus, &portchange); 46051da177e4SLinus Torvalds if (ret < 0) 46061da177e4SLinus Torvalds continue; 46071da177e4SLinus Torvalds 46081da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 46091da177e4SLinus Torvalds clear_port_feature(hdev, i, 46101da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 46111da177e4SLinus Torvalds connect_change = 1; 46121da177e4SLinus Torvalds } 46131da177e4SLinus Torvalds 46141da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_ENABLE) { 46151da177e4SLinus Torvalds if (!connect_change) 46161da177e4SLinus Torvalds dev_dbg (hub_dev, 46171da177e4SLinus Torvalds "port %d enable change, " 46181da177e4SLinus Torvalds "status %08x\n", 46191da177e4SLinus Torvalds i, portstatus); 46201da177e4SLinus Torvalds clear_port_feature(hdev, i, 46211da177e4SLinus Torvalds USB_PORT_FEAT_C_ENABLE); 46221da177e4SLinus Torvalds 46231da177e4SLinus Torvalds /* 46241da177e4SLinus Torvalds * EM interference sometimes causes badly 46251da177e4SLinus Torvalds * shielded USB devices to be shutdown by 46261da177e4SLinus Torvalds * the hub, this hack enables them again. 46271da177e4SLinus Torvalds * Works at least with mouse driver. 46281da177e4SLinus Torvalds */ 46291da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_ENABLE) 46301da177e4SLinus Torvalds && !connect_change 4631ff823c79SLan Tianyu && hub->ports[i - 1]->child) { 46321da177e4SLinus Torvalds dev_err (hub_dev, 46331da177e4SLinus Torvalds "port %i " 46341da177e4SLinus Torvalds "disabled by hub (EMI?), " 46351da177e4SLinus Torvalds "re-enabling...\n", 46361da177e4SLinus Torvalds i); 46371da177e4SLinus Torvalds connect_change = 1; 46381da177e4SLinus Torvalds } 46391da177e4SLinus Torvalds } 46401da177e4SLinus Torvalds 464172937e1eSSarah Sharp if (hub_handle_remote_wakeup(hub, i, 464272937e1eSSarah Sharp portstatus, portchange)) 46431da177e4SLinus Torvalds connect_change = 1; 46441da177e4SLinus Torvalds 46451da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 4646752d57a8SPaul Bolle u16 status = 0; 4647752d57a8SPaul Bolle u16 unused; 4648752d57a8SPaul Bolle 4649752d57a8SPaul Bolle dev_dbg(hub_dev, "over-current change on port " 4650752d57a8SPaul Bolle "%d\n", i); 46511da177e4SLinus Torvalds clear_port_feature(hdev, i, 46521da177e4SLinus Torvalds USB_PORT_FEAT_C_OVER_CURRENT); 4653752d57a8SPaul Bolle msleep(100); /* Cool down */ 46548520f380SAlan Stern hub_power_on(hub, true); 4655752d57a8SPaul Bolle hub_port_status(hub, i, &status, &unused); 4656752d57a8SPaul Bolle if (status & USB_PORT_STAT_OVERCURRENT) 4657752d57a8SPaul Bolle dev_err(hub_dev, "over-current " 4658752d57a8SPaul Bolle "condition on port %d\n", i); 46591da177e4SLinus Torvalds } 46601da177e4SLinus Torvalds 46611da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_RESET) { 46621da177e4SLinus Torvalds dev_dbg (hub_dev, 46631da177e4SLinus Torvalds "reset change on port %d\n", 46641da177e4SLinus Torvalds i); 46651da177e4SLinus Torvalds clear_port_feature(hdev, i, 46661da177e4SLinus Torvalds USB_PORT_FEAT_C_RESET); 46671da177e4SLinus Torvalds } 4668c7061574SSarah Sharp if ((portchange & USB_PORT_STAT_C_BH_RESET) && 4669c7061574SSarah Sharp hub_is_superspeed(hub->hdev)) { 4670c7061574SSarah Sharp dev_dbg(hub_dev, 4671c7061574SSarah Sharp "warm reset change on port %d\n", 4672c7061574SSarah Sharp i); 4673c7061574SSarah Sharp clear_port_feature(hdev, i, 4674c7061574SSarah Sharp USB_PORT_FEAT_C_BH_PORT_RESET); 4675c7061574SSarah Sharp } 4676dbe79bbeSJohn Youn if (portchange & USB_PORT_STAT_C_LINK_STATE) { 4677dbe79bbeSJohn Youn clear_port_feature(hub->hdev, i, 4678dbe79bbeSJohn Youn USB_PORT_FEAT_C_PORT_LINK_STATE); 4679dbe79bbeSJohn Youn } 4680dbe79bbeSJohn Youn if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 4681dbe79bbeSJohn Youn dev_warn(hub_dev, 4682dbe79bbeSJohn Youn "config error on port %d\n", 4683dbe79bbeSJohn Youn i); 4684dbe79bbeSJohn Youn clear_port_feature(hub->hdev, i, 4685dbe79bbeSJohn Youn USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 4686dbe79bbeSJohn Youn } 46871da177e4SLinus Torvalds 46885e467f6eSAndiry Xu /* Warm reset a USB3 protocol port if it's in 46895e467f6eSAndiry Xu * SS.Inactive state. 46905e467f6eSAndiry Xu */ 46918bea2bd3SStanislaw Ledwon if (hub_port_warm_reset_required(hub, portstatus)) { 469265bdac5eSSarah Sharp int status; 469365bdac5eSSarah Sharp 46945e467f6eSAndiry Xu dev_dbg(hub_dev, "warm reset port %d\n", i); 469565bdac5eSSarah Sharp status = hub_port_reset(hub, i, NULL, 469675d7cf72SAndiry Xu HUB_BH_RESET_TIME, true); 469765bdac5eSSarah Sharp if (status < 0) 469865bdac5eSSarah Sharp hub_port_disable(hub, i, 1); 469965bdac5eSSarah Sharp connect_change = 0; 47005e467f6eSAndiry Xu } 47015e467f6eSAndiry Xu 47021da177e4SLinus Torvalds if (connect_change) 47031da177e4SLinus Torvalds hub_port_connect_change(hub, i, 47041da177e4SLinus Torvalds portstatus, portchange); 47051da177e4SLinus Torvalds } /* end for i */ 47061da177e4SLinus Torvalds 47071da177e4SLinus Torvalds /* deal with hub status changes */ 47081da177e4SLinus Torvalds if (test_and_clear_bit(0, hub->event_bits) == 0) 47091da177e4SLinus Torvalds ; /* do nothing */ 47101da177e4SLinus Torvalds else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 47111da177e4SLinus Torvalds dev_err (hub_dev, "get_hub_status failed\n"); 47121da177e4SLinus Torvalds else { 47131da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_LOCAL_POWER) { 47141da177e4SLinus Torvalds dev_dbg (hub_dev, "power change\n"); 47151da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 471655c52718SAlan Stern if (hubstatus & HUB_STATUS_LOCAL_POWER) 471755c52718SAlan Stern /* FIXME: Is this always true? */ 471855c52718SAlan Stern hub->limited_power = 1; 4719403fae78Sjidong xiao else 4720403fae78Sjidong xiao hub->limited_power = 0; 47211da177e4SLinus Torvalds } 47221da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_OVERCURRENT) { 4723752d57a8SPaul Bolle u16 status = 0; 4724752d57a8SPaul Bolle u16 unused; 4725752d57a8SPaul Bolle 4726752d57a8SPaul Bolle dev_dbg(hub_dev, "over-current change\n"); 47271da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 4728752d57a8SPaul Bolle msleep(500); /* Cool down */ 47298520f380SAlan Stern hub_power_on(hub, true); 4730752d57a8SPaul Bolle hub_hub_status(hub, &status, &unused); 4731752d57a8SPaul Bolle if (status & HUB_STATUS_OVERCURRENT) 4732752d57a8SPaul Bolle dev_err(hub_dev, "over-current " 4733752d57a8SPaul Bolle "condition\n"); 47341da177e4SLinus Torvalds } 47351da177e4SLinus Torvalds } 47361da177e4SLinus Torvalds 473740f122f3SAlan Stern loop_autopm: 47388e4ceb38SAlan Stern /* Balance the usb_autopm_get_interface() above */ 47398e4ceb38SAlan Stern usb_autopm_put_interface_no_suspend(intf); 47401da177e4SLinus Torvalds loop: 47418e4ceb38SAlan Stern /* Balance the usb_autopm_get_interface_no_resume() in 47428e4ceb38SAlan Stern * kick_khubd() and allow autosuspend. 47438e4ceb38SAlan Stern */ 47448e4ceb38SAlan Stern usb_autopm_put_interface(intf); 47459bbdf1e0SAlan Stern loop_disconnected: 47461da177e4SLinus Torvalds usb_unlock_device(hdev); 4747e8054854SAlan Stern kref_put(&hub->kref, hub_release); 47481da177e4SLinus Torvalds 47491da177e4SLinus Torvalds } /* end while (1) */ 47501da177e4SLinus Torvalds } 47511da177e4SLinus Torvalds 47521da177e4SLinus Torvalds static int hub_thread(void *__unused) 47531da177e4SLinus Torvalds { 47543bb1af52SAlan Stern /* khubd needs to be freezable to avoid intefering with USB-PERSIST 47553bb1af52SAlan Stern * port handover. Otherwise it might see that a full-speed device 47563bb1af52SAlan Stern * was gone before the EHCI controller had handed its port over to 47573bb1af52SAlan Stern * the companion full-speed controller. 47583bb1af52SAlan Stern */ 475983144186SRafael J. Wysocki set_freezable(); 47603bb1af52SAlan Stern 47611da177e4SLinus Torvalds do { 47621da177e4SLinus Torvalds hub_events(); 4763e42837bcSRafael J. Wysocki wait_event_freezable(khubd_wait, 47649c8d6178Sakpm@osdl.org !list_empty(&hub_event_list) || 47659c8d6178Sakpm@osdl.org kthread_should_stop()); 47669c8d6178Sakpm@osdl.org } while (!kthread_should_stop() || !list_empty(&hub_event_list)); 47671da177e4SLinus Torvalds 47681da177e4SLinus Torvalds pr_debug("%s: khubd exiting\n", usbcore_name); 47699c8d6178Sakpm@osdl.org return 0; 47701da177e4SLinus Torvalds } 47711da177e4SLinus Torvalds 47721e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = { 4773e6f30deaSMing Lei { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 4774e6f30deaSMing Lei | USB_DEVICE_ID_MATCH_INT_CLASS, 4775e6f30deaSMing Lei .idVendor = USB_VENDOR_GENESYS_LOGIC, 4776e6f30deaSMing Lei .bInterfaceClass = USB_CLASS_HUB, 4777e6f30deaSMing Lei .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, 47781da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 47791da177e4SLinus Torvalds .bDeviceClass = USB_CLASS_HUB}, 47801da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 47811da177e4SLinus Torvalds .bInterfaceClass = USB_CLASS_HUB}, 47821da177e4SLinus Torvalds { } /* Terminating entry */ 47831da177e4SLinus Torvalds }; 47841da177e4SLinus Torvalds 47851da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table); 47861da177e4SLinus Torvalds 47871da177e4SLinus Torvalds static struct usb_driver hub_driver = { 47881da177e4SLinus Torvalds .name = "hub", 47891da177e4SLinus Torvalds .probe = hub_probe, 47901da177e4SLinus Torvalds .disconnect = hub_disconnect, 47911da177e4SLinus Torvalds .suspend = hub_suspend, 47921da177e4SLinus Torvalds .resume = hub_resume, 4793f07600cfSAlan Stern .reset_resume = hub_reset_resume, 47947de18d8bSAlan Stern .pre_reset = hub_pre_reset, 47957de18d8bSAlan Stern .post_reset = hub_post_reset, 4796c532b29aSAndi Kleen .unlocked_ioctl = hub_ioctl, 47971da177e4SLinus Torvalds .id_table = hub_id_table, 479840f122f3SAlan Stern .supports_autosuspend = 1, 47991da177e4SLinus Torvalds }; 48001da177e4SLinus Torvalds 48011da177e4SLinus Torvalds int usb_hub_init(void) 48021da177e4SLinus Torvalds { 48031da177e4SLinus Torvalds if (usb_register(&hub_driver) < 0) { 48041da177e4SLinus Torvalds printk(KERN_ERR "%s: can't register hub driver\n", 48051da177e4SLinus Torvalds usbcore_name); 48061da177e4SLinus Torvalds return -1; 48071da177e4SLinus Torvalds } 48081da177e4SLinus Torvalds 48099c8d6178Sakpm@osdl.org khubd_task = kthread_run(hub_thread, NULL, "khubd"); 48109c8d6178Sakpm@osdl.org if (!IS_ERR(khubd_task)) 48111da177e4SLinus Torvalds return 0; 48121da177e4SLinus Torvalds 48131da177e4SLinus Torvalds /* Fall through if kernel_thread failed */ 48141da177e4SLinus Torvalds usb_deregister(&hub_driver); 48151da177e4SLinus Torvalds printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); 48161da177e4SLinus Torvalds 48171da177e4SLinus Torvalds return -1; 48181da177e4SLinus Torvalds } 48191da177e4SLinus Torvalds 48201da177e4SLinus Torvalds void usb_hub_cleanup(void) 48211da177e4SLinus Torvalds { 48229c8d6178Sakpm@osdl.org kthread_stop(khubd_task); 48231da177e4SLinus Torvalds 48241da177e4SLinus Torvalds /* 48251da177e4SLinus Torvalds * Hub resources are freed for us by usb_deregister. It calls 48261da177e4SLinus Torvalds * usb_driver_purge on every device which in turn calls that 48271da177e4SLinus Torvalds * devices disconnect function if it is using this driver. 48281da177e4SLinus Torvalds * The hub_disconnect function takes care of releasing the 48291da177e4SLinus Torvalds * individual hub resources. -greg 48301da177e4SLinus Torvalds */ 48311da177e4SLinus Torvalds usb_deregister(&hub_driver); 48321da177e4SLinus Torvalds } /* usb_hub_cleanup() */ 48331da177e4SLinus Torvalds 4834eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev, 4835eb764c4bSAlan Stern struct usb_device_descriptor *old_device_descriptor) 48361da177e4SLinus Torvalds { 4837eb764c4bSAlan Stern int changed = 0; 48381da177e4SLinus Torvalds unsigned index; 4839eb764c4bSAlan Stern unsigned serial_len = 0; 4840eb764c4bSAlan Stern unsigned len; 4841eb764c4bSAlan Stern unsigned old_length; 4842eb764c4bSAlan Stern int length; 4843eb764c4bSAlan Stern char *buf; 48441da177e4SLinus Torvalds 4845eb764c4bSAlan Stern if (memcmp(&udev->descriptor, old_device_descriptor, 4846eb764c4bSAlan Stern sizeof(*old_device_descriptor)) != 0) 4847eb764c4bSAlan Stern return 1; 4848eb764c4bSAlan Stern 4849eb764c4bSAlan Stern /* Since the idVendor, idProduct, and bcdDevice values in the 4850eb764c4bSAlan Stern * device descriptor haven't changed, we will assume the 4851eb764c4bSAlan Stern * Manufacturer and Product strings haven't changed either. 4852eb764c4bSAlan Stern * But the SerialNumber string could be different (e.g., a 4853eb764c4bSAlan Stern * different flash card of the same brand). 4854eb764c4bSAlan Stern */ 4855eb764c4bSAlan Stern if (udev->serial) 4856eb764c4bSAlan Stern serial_len = strlen(udev->serial) + 1; 4857eb764c4bSAlan Stern 4858eb764c4bSAlan Stern len = serial_len; 48591da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 4860eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 4861eb764c4bSAlan Stern len = max(len, old_length); 48621da177e4SLinus Torvalds } 4863eb764c4bSAlan Stern 48640cc1a51fSOliver Neukum buf = kmalloc(len, GFP_NOIO); 48651da177e4SLinus Torvalds if (buf == NULL) { 48661da177e4SLinus Torvalds dev_err(&udev->dev, "no mem to re-read configs after reset\n"); 48671da177e4SLinus Torvalds /* assume the worst */ 48681da177e4SLinus Torvalds return 1; 48691da177e4SLinus Torvalds } 48701da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 4871eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 48721da177e4SLinus Torvalds length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 48731da177e4SLinus Torvalds old_length); 4874eb764c4bSAlan Stern if (length != old_length) { 48751da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d, error %d\n", 48761da177e4SLinus Torvalds index, length); 4877eb764c4bSAlan Stern changed = 1; 48781da177e4SLinus Torvalds break; 48791da177e4SLinus Torvalds } 48801da177e4SLinus Torvalds if (memcmp (buf, udev->rawdescriptors[index], old_length) 48811da177e4SLinus Torvalds != 0) { 48821da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 4883eb764c4bSAlan Stern index, 4884eb764c4bSAlan Stern ((struct usb_config_descriptor *) buf)-> 4885eb764c4bSAlan Stern bConfigurationValue); 4886eb764c4bSAlan Stern changed = 1; 48871da177e4SLinus Torvalds break; 48881da177e4SLinus Torvalds } 48891da177e4SLinus Torvalds } 4890eb764c4bSAlan Stern 4891eb764c4bSAlan Stern if (!changed && serial_len) { 4892eb764c4bSAlan Stern length = usb_string(udev, udev->descriptor.iSerialNumber, 4893eb764c4bSAlan Stern buf, serial_len); 4894eb764c4bSAlan Stern if (length + 1 != serial_len) { 4895eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string error %d\n", 4896eb764c4bSAlan Stern length); 4897eb764c4bSAlan Stern changed = 1; 4898eb764c4bSAlan Stern } else if (memcmp(buf, udev->serial, length) != 0) { 4899eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string changed\n"); 4900eb764c4bSAlan Stern changed = 1; 4901eb764c4bSAlan Stern } 4902eb764c4bSAlan Stern } 4903eb764c4bSAlan Stern 49041da177e4SLinus Torvalds kfree(buf); 4905eb764c4bSAlan Stern return changed; 49061da177e4SLinus Torvalds } 49071da177e4SLinus Torvalds 49081da177e4SLinus Torvalds /** 4909742120c6SMing Lei * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 49101da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 49111da177e4SLinus Torvalds * 491279efa097SAlan Stern * WARNING - don't use this routine to reset a composite device 491379efa097SAlan Stern * (one with multiple interfaces owned by separate drivers)! 4914742120c6SMing Lei * Use usb_reset_device() instead. 49151da177e4SLinus Torvalds * 49161da177e4SLinus Torvalds * Do a port reset, reassign the device's address, and establish its 49171da177e4SLinus Torvalds * former operating configuration. If the reset fails, or the device's 49181da177e4SLinus Torvalds * descriptors change from their values before the reset, or the original 49191da177e4SLinus Torvalds * configuration and altsettings cannot be restored, a flag will be set 49201da177e4SLinus Torvalds * telling khubd to pretend the device has been disconnected and then 49211da177e4SLinus Torvalds * re-connected. All drivers will be unbound, and the device will be 49221da177e4SLinus Torvalds * re-enumerated and probed all over again. 49231da177e4SLinus Torvalds * 49241da177e4SLinus Torvalds * Returns 0 if the reset succeeded, -ENODEV if the device has been 49251da177e4SLinus Torvalds * flagged for logical disconnection, or some other negative error code 49261da177e4SLinus Torvalds * if the reset wasn't even attempted. 49271da177e4SLinus Torvalds * 49281da177e4SLinus Torvalds * The caller must own the device lock. For example, it's safe to use 49291da177e4SLinus Torvalds * this from a driver probe() routine after downloading new firmware. 49301da177e4SLinus Torvalds * For calls that might not occur during probe(), drivers should lock 49311da177e4SLinus Torvalds * the device using usb_lock_device_for_reset(). 49326bc6cff5SAlan Stern * 49336bc6cff5SAlan Stern * Locking exception: This routine may also be called from within an 49346bc6cff5SAlan Stern * autoresume handler. Such usage won't conflict with other tasks 49356bc6cff5SAlan Stern * holding the device lock because these tasks should always call 49366bc6cff5SAlan Stern * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. 49371da177e4SLinus Torvalds */ 4938742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev) 49391da177e4SLinus Torvalds { 49401da177e4SLinus Torvalds struct usb_device *parent_hdev = udev->parent; 49411da177e4SLinus Torvalds struct usb_hub *parent_hub; 49423f0479e0SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 49431da177e4SLinus Torvalds struct usb_device_descriptor descriptor = udev->descriptor; 494412c3da34SAlan Stern int i, ret = 0; 494512c3da34SAlan Stern int port1 = udev->portnum; 49461da177e4SLinus Torvalds 49471da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED || 49481da177e4SLinus Torvalds udev->state == USB_STATE_SUSPENDED) { 49491da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 49501da177e4SLinus Torvalds udev->state); 49511da177e4SLinus Torvalds return -EINVAL; 49521da177e4SLinus Torvalds } 49531da177e4SLinus Torvalds 49541da177e4SLinus Torvalds if (!parent_hdev) { 49554bec9917SWolfram Sang /* this requires hcd-specific logic; see ohci_restart() */ 4956441b62c1SHarvey Harrison dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 49571da177e4SLinus Torvalds return -EISDIR; 49581da177e4SLinus Torvalds } 49591da177e4SLinus Torvalds parent_hub = hdev_to_hub(parent_hdev); 49601da177e4SLinus Torvalds 4961f74631e3SSarah Sharp /* Disable LPM and LTM while we reset the device and reinstall the alt 4962f74631e3SSarah Sharp * settings. Device-initiated LPM settings, and system exit latency 4963f74631e3SSarah Sharp * settings are cleared when the device is reset, so we have to set 4964f74631e3SSarah Sharp * them up again. 49656d1d0513SSarah Sharp */ 49666d1d0513SSarah Sharp ret = usb_unlocked_disable_lpm(udev); 49676d1d0513SSarah Sharp if (ret) { 49686d1d0513SSarah Sharp dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__); 49696d1d0513SSarah Sharp goto re_enumerate; 49706d1d0513SSarah Sharp } 4971f74631e3SSarah Sharp ret = usb_disable_ltm(udev); 4972f74631e3SSarah Sharp if (ret) { 4973f74631e3SSarah Sharp dev_err(&udev->dev, "%s Failed to disable LTM\n.", 4974f74631e3SSarah Sharp __func__); 4975f74631e3SSarah Sharp goto re_enumerate; 4976f74631e3SSarah Sharp } 49776d1d0513SSarah Sharp 49781da177e4SLinus Torvalds set_bit(port1, parent_hub->busy_bits); 49791da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; ++i) { 49801da177e4SLinus Torvalds 49811da177e4SLinus Torvalds /* ep0 maxpacket size may change; let the HCD know about it. 49821da177e4SLinus Torvalds * Other endpoints will be handled by re-enumeration. */ 4983fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 49841da177e4SLinus Torvalds ret = hub_port_init(parent_hub, udev, port1, i); 4985dd4dd19eSAlan Stern if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 49861da177e4SLinus Torvalds break; 49871da177e4SLinus Torvalds } 49881da177e4SLinus Torvalds clear_bit(port1, parent_hub->busy_bits); 4989d5cbad4bSAlan Stern 49901da177e4SLinus Torvalds if (ret < 0) 49911da177e4SLinus Torvalds goto re_enumerate; 49921da177e4SLinus Torvalds 49931da177e4SLinus Torvalds /* Device might have changed firmware (DFU or similar) */ 4994eb764c4bSAlan Stern if (descriptors_changed(udev, &descriptor)) { 49951da177e4SLinus Torvalds dev_info(&udev->dev, "device firmware changed\n"); 49961da177e4SLinus Torvalds udev->descriptor = descriptor; /* for disconnect() calls */ 49971da177e4SLinus Torvalds goto re_enumerate; 49981da177e4SLinus Torvalds } 49991da177e4SLinus Torvalds 50004fe0387aSAlan Stern /* Restore the device's previous configuration */ 50011da177e4SLinus Torvalds if (!udev->actconfig) 50021da177e4SLinus Torvalds goto done; 50033f0479e0SSarah Sharp 5004d673bfcbSSarah Sharp mutex_lock(hcd->bandwidth_mutex); 50053f0479e0SSarah Sharp ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 50063f0479e0SSarah Sharp if (ret < 0) { 50073f0479e0SSarah Sharp dev_warn(&udev->dev, 50083f0479e0SSarah Sharp "Busted HC? Not enough HCD resources for " 50093f0479e0SSarah Sharp "old configuration.\n"); 5010d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 50113f0479e0SSarah Sharp goto re_enumerate; 50123f0479e0SSarah Sharp } 50131da177e4SLinus Torvalds ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 50141da177e4SLinus Torvalds USB_REQ_SET_CONFIGURATION, 0, 50151da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, 0, 50161da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 50171da177e4SLinus Torvalds if (ret < 0) { 50181da177e4SLinus Torvalds dev_err(&udev->dev, 50191da177e4SLinus Torvalds "can't restore configuration #%d (error=%d)\n", 50201da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, ret); 5021d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 50221da177e4SLinus Torvalds goto re_enumerate; 50231da177e4SLinus Torvalds } 5024d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 50251da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_CONFIGURED); 50261da177e4SLinus Torvalds 50274fe0387aSAlan Stern /* Put interfaces back into the same altsettings as before. 50284fe0387aSAlan Stern * Don't bother to send the Set-Interface request for interfaces 50294fe0387aSAlan Stern * that were already in altsetting 0; besides being unnecessary, 50304fe0387aSAlan Stern * many devices can't handle it. Instead just reset the host-side 50314fe0387aSAlan Stern * endpoint state. 50324fe0387aSAlan Stern */ 50331da177e4SLinus Torvalds for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 50343f0479e0SSarah Sharp struct usb_host_config *config = udev->actconfig; 50353f0479e0SSarah Sharp struct usb_interface *intf = config->interface[i]; 50361da177e4SLinus Torvalds struct usb_interface_descriptor *desc; 50371da177e4SLinus Torvalds 50381da177e4SLinus Torvalds desc = &intf->cur_altsetting->desc; 50394fe0387aSAlan Stern if (desc->bAlternateSetting == 0) { 50404fe0387aSAlan Stern usb_disable_interface(udev, intf, true); 50414fe0387aSAlan Stern usb_enable_interface(udev, intf, true); 50424fe0387aSAlan Stern ret = 0; 50434fe0387aSAlan Stern } else { 504404a723eaSSarah Sharp /* Let the bandwidth allocation function know that this 504504a723eaSSarah Sharp * device has been reset, and it will have to use 504604a723eaSSarah Sharp * alternate setting 0 as the current alternate setting. 50473f0479e0SSarah Sharp */ 504804a723eaSSarah Sharp intf->resetting_device = 1; 50491da177e4SLinus Torvalds ret = usb_set_interface(udev, desc->bInterfaceNumber, 50501da177e4SLinus Torvalds desc->bAlternateSetting); 505104a723eaSSarah Sharp intf->resetting_device = 0; 50524fe0387aSAlan Stern } 50531da177e4SLinus Torvalds if (ret < 0) { 50541da177e4SLinus Torvalds dev_err(&udev->dev, "failed to restore interface %d " 50551da177e4SLinus Torvalds "altsetting %d (error=%d)\n", 50561da177e4SLinus Torvalds desc->bInterfaceNumber, 50571da177e4SLinus Torvalds desc->bAlternateSetting, 50581da177e4SLinus Torvalds ret); 50591da177e4SLinus Torvalds goto re_enumerate; 50601da177e4SLinus Torvalds } 50611da177e4SLinus Torvalds } 50621da177e4SLinus Torvalds 50631da177e4SLinus Torvalds done: 5064f74631e3SSarah Sharp /* Now that the alt settings are re-installed, enable LTM and LPM. */ 506578d9a487SAlan Stern usb_unlocked_enable_lpm(udev); 5066f74631e3SSarah Sharp usb_enable_ltm(udev); 50671da177e4SLinus Torvalds return 0; 50681da177e4SLinus Torvalds 50691da177e4SLinus Torvalds re_enumerate: 50706d1d0513SSarah Sharp /* LPM state doesn't matter when we're about to destroy the device. */ 50711da177e4SLinus Torvalds hub_port_logical_disconnect(parent_hub, port1); 50721da177e4SLinus Torvalds return -ENODEV; 50731da177e4SLinus Torvalds } 50741da177e4SLinus Torvalds 50751da177e4SLinus Torvalds /** 50761da177e4SLinus Torvalds * usb_reset_device - warn interface drivers and perform a USB port reset 50771da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 50781da177e4SLinus Torvalds * 507979efa097SAlan Stern * Warns all drivers bound to registered interfaces (using their pre_reset 508079efa097SAlan Stern * method), performs the port reset, and then lets the drivers know that 5081742120c6SMing Lei * the reset is over (using their post_reset method). 508279efa097SAlan Stern * 508379efa097SAlan Stern * Return value is the same as for usb_reset_and_verify_device(). 508479efa097SAlan Stern * 508579efa097SAlan Stern * The caller must own the device lock. For example, it's safe to use 508679efa097SAlan Stern * this from a driver probe() routine after downloading new firmware. 508779efa097SAlan Stern * For calls that might not occur during probe(), drivers should lock 5088742120c6SMing Lei * the device using usb_lock_device_for_reset(). 508979efa097SAlan Stern * 509079efa097SAlan Stern * If an interface is currently being probed or disconnected, we assume 509179efa097SAlan Stern * its driver knows how to handle resets. For all other interfaces, 509279efa097SAlan Stern * if the driver doesn't have pre_reset and post_reset methods then 509379efa097SAlan Stern * we attempt to unbind it and rebind afterward. 509479efa097SAlan Stern */ 5095742120c6SMing Lei int usb_reset_device(struct usb_device *udev) 509679efa097SAlan Stern { 509779efa097SAlan Stern int ret; 5098852c4b43SAlan Stern int i; 509979efa097SAlan Stern struct usb_host_config *config = udev->actconfig; 510079efa097SAlan Stern 510179efa097SAlan Stern if (udev->state == USB_STATE_NOTATTACHED || 510279efa097SAlan Stern udev->state == USB_STATE_SUSPENDED) { 510379efa097SAlan Stern dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 510479efa097SAlan Stern udev->state); 510579efa097SAlan Stern return -EINVAL; 510679efa097SAlan Stern } 510779efa097SAlan Stern 5108645daaabSAlan Stern /* Prevent autosuspend during the reset */ 510994fcda1fSAlan Stern usb_autoresume_device(udev); 5110645daaabSAlan Stern 511179efa097SAlan Stern if (config) { 5112852c4b43SAlan Stern for (i = 0; i < config->desc.bNumInterfaces; ++i) { 5113852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 511479efa097SAlan Stern struct usb_driver *drv; 511578d9a487SAlan Stern int unbind = 0; 511679efa097SAlan Stern 5117852c4b43SAlan Stern if (cintf->dev.driver) { 511879efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 511978d9a487SAlan Stern if (drv->pre_reset && drv->post_reset) 512078d9a487SAlan Stern unbind = (drv->pre_reset)(cintf); 512178d9a487SAlan Stern else if (cintf->condition == 512278d9a487SAlan Stern USB_INTERFACE_BOUND) 512378d9a487SAlan Stern unbind = 1; 512478d9a487SAlan Stern if (unbind) 512578d9a487SAlan Stern usb_forced_unbind_intf(cintf); 512679efa097SAlan Stern } 512779efa097SAlan Stern } 512879efa097SAlan Stern } 512979efa097SAlan Stern 5130742120c6SMing Lei ret = usb_reset_and_verify_device(udev); 513179efa097SAlan Stern 513279efa097SAlan Stern if (config) { 5133852c4b43SAlan Stern for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 5134852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 513579efa097SAlan Stern struct usb_driver *drv; 513678d9a487SAlan Stern int rebind = cintf->needs_binding; 513779efa097SAlan Stern 513878d9a487SAlan Stern if (!rebind && cintf->dev.driver) { 513979efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 514079efa097SAlan Stern if (drv->post_reset) 514178d9a487SAlan Stern rebind = (drv->post_reset)(cintf); 514278d9a487SAlan Stern else if (cintf->condition == 514378d9a487SAlan Stern USB_INTERFACE_BOUND) 514478d9a487SAlan Stern rebind = 1; 514579efa097SAlan Stern } 51466c640945SAlan Stern if (ret == 0 && rebind) 514778d9a487SAlan Stern usb_rebind_intf(cintf); 514879efa097SAlan Stern } 514979efa097SAlan Stern } 515079efa097SAlan Stern 515194fcda1fSAlan Stern usb_autosuspend_device(udev); 515279efa097SAlan Stern return ret; 515379efa097SAlan Stern } 5154742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device); 5155dc023dceSInaky Perez-Gonzalez 5156dc023dceSInaky Perez-Gonzalez 5157dc023dceSInaky Perez-Gonzalez /** 5158dc023dceSInaky Perez-Gonzalez * usb_queue_reset_device - Reset a USB device from an atomic context 5159dc023dceSInaky Perez-Gonzalez * @iface: USB interface belonging to the device to reset 5160dc023dceSInaky Perez-Gonzalez * 5161dc023dceSInaky Perez-Gonzalez * This function can be used to reset a USB device from an atomic 5162dc023dceSInaky Perez-Gonzalez * context, where usb_reset_device() won't work (as it blocks). 5163dc023dceSInaky Perez-Gonzalez * 5164dc023dceSInaky Perez-Gonzalez * Doing a reset via this method is functionally equivalent to calling 5165dc023dceSInaky Perez-Gonzalez * usb_reset_device(), except for the fact that it is delayed to a 5166dc023dceSInaky Perez-Gonzalez * workqueue. This means that any drivers bound to other interfaces 5167dc023dceSInaky Perez-Gonzalez * might be unbound, as well as users from usbfs in user space. 5168dc023dceSInaky Perez-Gonzalez * 5169dc023dceSInaky Perez-Gonzalez * Corner cases: 5170dc023dceSInaky Perez-Gonzalez * 5171dc023dceSInaky Perez-Gonzalez * - Scheduling two resets at the same time from two different drivers 5172dc023dceSInaky Perez-Gonzalez * attached to two different interfaces of the same device is 5173dc023dceSInaky Perez-Gonzalez * possible; depending on how the driver attached to each interface 5174dc023dceSInaky Perez-Gonzalez * handles ->pre_reset(), the second reset might happen or not. 5175dc023dceSInaky Perez-Gonzalez * 5176dc023dceSInaky Perez-Gonzalez * - If a driver is unbound and it had a pending reset, the reset will 5177dc023dceSInaky Perez-Gonzalez * be cancelled. 5178dc023dceSInaky Perez-Gonzalez * 5179dc023dceSInaky Perez-Gonzalez * - This function can be called during .probe() or .disconnect() 5180dc023dceSInaky Perez-Gonzalez * times. On return from .disconnect(), any pending resets will be 5181dc023dceSInaky Perez-Gonzalez * cancelled. 5182dc023dceSInaky Perez-Gonzalez * 5183dc023dceSInaky Perez-Gonzalez * There is no no need to lock/unlock the @reset_ws as schedule_work() 5184dc023dceSInaky Perez-Gonzalez * does its own. 5185dc023dceSInaky Perez-Gonzalez * 5186dc023dceSInaky Perez-Gonzalez * NOTE: We don't do any reference count tracking because it is not 5187dc023dceSInaky Perez-Gonzalez * needed. The lifecycle of the work_struct is tied to the 5188dc023dceSInaky Perez-Gonzalez * usb_interface. Before destroying the interface we cancel the 5189dc023dceSInaky Perez-Gonzalez * work_struct, so the fact that work_struct is queued and or 5190dc023dceSInaky Perez-Gonzalez * running means the interface (and thus, the device) exist and 5191dc023dceSInaky Perez-Gonzalez * are referenced. 5192dc023dceSInaky Perez-Gonzalez */ 5193dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface) 5194dc023dceSInaky Perez-Gonzalez { 5195dc023dceSInaky Perez-Gonzalez schedule_work(&iface->reset_ws); 5196dc023dceSInaky Perez-Gonzalez } 5197dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device); 5198ff823c79SLan Tianyu 5199ff823c79SLan Tianyu /** 5200ff823c79SLan Tianyu * usb_hub_find_child - Get the pointer of child device 5201ff823c79SLan Tianyu * attached to the port which is specified by @port1. 5202ff823c79SLan Tianyu * @hdev: USB device belonging to the usb hub 5203ff823c79SLan Tianyu * @port1: port num to indicate which port the child device 5204ff823c79SLan Tianyu * is attached to. 5205ff823c79SLan Tianyu * 5206ff823c79SLan Tianyu * USB drivers call this function to get hub's child device 5207ff823c79SLan Tianyu * pointer. 5208ff823c79SLan Tianyu * 5209ff823c79SLan Tianyu * Return NULL if input param is invalid and 5210ff823c79SLan Tianyu * child's usb_device pointer if non-NULL. 5211ff823c79SLan Tianyu */ 5212ff823c79SLan Tianyu struct usb_device *usb_hub_find_child(struct usb_device *hdev, 5213ff823c79SLan Tianyu int port1) 5214ff823c79SLan Tianyu { 5215ff823c79SLan Tianyu struct usb_hub *hub = hdev_to_hub(hdev); 5216ff823c79SLan Tianyu 5217ff823c79SLan Tianyu if (port1 < 1 || port1 > hdev->maxchild) 5218ff823c79SLan Tianyu return NULL; 5219ff823c79SLan Tianyu return hub->ports[port1 - 1]->child; 5220ff823c79SLan Tianyu } 5221ff823c79SLan Tianyu EXPORT_SYMBOL_GPL(usb_hub_find_child); 5222d5575424SLan Tianyu 522305f91689SLan Tianyu /** 522405f91689SLan Tianyu * usb_set_hub_port_connect_type - set hub port connect type. 522505f91689SLan Tianyu * @hdev: USB device belonging to the usb hub 522605f91689SLan Tianyu * @port1: port num of the port 522705f91689SLan Tianyu * @type: connect type of the port 522805f91689SLan Tianyu */ 522905f91689SLan Tianyu void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1, 523005f91689SLan Tianyu enum usb_port_connect_type type) 523105f91689SLan Tianyu { 523205f91689SLan Tianyu struct usb_hub *hub = hdev_to_hub(hdev); 523305f91689SLan Tianyu 523405f91689SLan Tianyu hub->ports[port1 - 1]->connect_type = type; 523505f91689SLan Tianyu } 523605f91689SLan Tianyu 523705f91689SLan Tianyu /** 523805f91689SLan Tianyu * usb_get_hub_port_connect_type - Get the port's connect type 523905f91689SLan Tianyu * @hdev: USB device belonging to the usb hub 524005f91689SLan Tianyu * @port1: port num of the port 524105f91689SLan Tianyu * 524205f91689SLan Tianyu * Return connect type of the port and if input params are 524305f91689SLan Tianyu * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN. 524405f91689SLan Tianyu */ 524505f91689SLan Tianyu enum usb_port_connect_type 524605f91689SLan Tianyu usb_get_hub_port_connect_type(struct usb_device *hdev, int port1) 524705f91689SLan Tianyu { 524805f91689SLan Tianyu struct usb_hub *hub = hdev_to_hub(hdev); 524905f91689SLan Tianyu 525005f91689SLan Tianyu return hub->ports[port1 - 1]->connect_type; 525105f91689SLan Tianyu } 525205f91689SLan Tianyu 5253d5575424SLan Tianyu #ifdef CONFIG_ACPI 5254d5575424SLan Tianyu /** 5255d5575424SLan Tianyu * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle 5256d5575424SLan Tianyu * @hdev: USB device belonging to the usb hub 5257d5575424SLan Tianyu * @port1: port num of the port 5258d5575424SLan Tianyu * 5259d5575424SLan Tianyu * Return port's acpi handle if successful, NULL if params are 5260d5575424SLan Tianyu * invaild. 5261d5575424SLan Tianyu */ 5262d5575424SLan Tianyu acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, 5263d5575424SLan Tianyu int port1) 5264d5575424SLan Tianyu { 5265d5575424SLan Tianyu struct usb_hub *hub = hdev_to_hub(hdev); 5266d5575424SLan Tianyu 5267d5575424SLan Tianyu return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev); 5268d5575424SLan Tianyu } 5269d5575424SLan Tianyu #endif 5270