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> 2393362a87SPhil Dibowitz #include <linux/usb/quirks.h> 249c8d6178Sakpm@osdl.org #include <linux/kthread.h> 254186ecf8SArjan van de Ven #include <linux/mutex.h> 267dfb7103SNigel Cunningham #include <linux/freezer.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <asm/uaccess.h> 291da177e4SLinus Torvalds #include <asm/byteorder.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include "usb.h" 321da177e4SLinus Torvalds 33f2a383e4SGreg Kroah-Hartman /* if we are in debug mode, always announce new devices */ 34f2a383e4SGreg Kroah-Hartman #ifdef DEBUG 35f2a383e4SGreg Kroah-Hartman #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES 36f2a383e4SGreg Kroah-Hartman #define CONFIG_USB_ANNOUNCE_NEW_DEVICES 37f2a383e4SGreg Kroah-Hartman #endif 38f2a383e4SGreg Kroah-Hartman #endif 39f2a383e4SGreg Kroah-Hartman 401bb5f66bSAlan Stern struct usb_hub { 411bb5f66bSAlan Stern struct device *intfdev; /* the "interface" device */ 421bb5f66bSAlan Stern struct usb_device *hdev; 43e8054854SAlan Stern struct kref kref; 441bb5f66bSAlan Stern struct urb *urb; /* for interrupt polling pipe */ 451bb5f66bSAlan Stern 461bb5f66bSAlan Stern /* buffer for urb ... with extra space in case of babble */ 471bb5f66bSAlan Stern char (*buffer)[8]; 481bb5f66bSAlan Stern union { 491bb5f66bSAlan Stern struct usb_hub_status hub; 501bb5f66bSAlan Stern struct usb_port_status port; 511bb5f66bSAlan Stern } *status; /* buffer for status reports */ 52db90e7a1SAlan Stern struct mutex status_mutex; /* for the status buffer */ 531bb5f66bSAlan Stern 541bb5f66bSAlan Stern int error; /* last reported error */ 551bb5f66bSAlan Stern int nerrors; /* track consecutive errors */ 561bb5f66bSAlan Stern 571bb5f66bSAlan Stern struct list_head event_list; /* hubs w/data or errs ready */ 581bb5f66bSAlan Stern unsigned long event_bits[1]; /* status change bitmask */ 591bb5f66bSAlan Stern unsigned long change_bits[1]; /* ports with logical connect 601bb5f66bSAlan Stern status change */ 611bb5f66bSAlan Stern unsigned long busy_bits[1]; /* ports being reset or 621bb5f66bSAlan Stern resumed */ 63253e0572SAlan Stern unsigned long removed_bits[1]; /* ports with a "removed" 64253e0572SAlan Stern device present */ 654ee823b8SSarah Sharp unsigned long wakeup_bits[1]; /* ports that have signaled 664ee823b8SSarah Sharp remote wakeup */ 671bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ 681bb5f66bSAlan Stern #error event_bits[] is too short! 691bb5f66bSAlan Stern #endif 701bb5f66bSAlan Stern 711bb5f66bSAlan Stern struct usb_hub_descriptor *descriptor; /* class descriptor */ 721bb5f66bSAlan Stern struct usb_tt tt; /* Transaction Translator */ 731bb5f66bSAlan Stern 741bb5f66bSAlan Stern unsigned mA_per_port; /* current for each child */ 751bb5f66bSAlan Stern 761bb5f66bSAlan Stern unsigned limited_power:1; 771bb5f66bSAlan Stern unsigned quiescing:1; 78e8054854SAlan Stern unsigned disconnected:1; 791bb5f66bSAlan Stern 801bb5f66bSAlan Stern unsigned has_indicators:1; 811bb5f66bSAlan Stern u8 indicator[USB_MAXCHILDREN]; 826d5aefb8SDavid Howells struct delayed_work leds; 838520f380SAlan Stern struct delayed_work init_work; 84336c5c31SLan Tianyu struct dev_state **port_owners; 851bb5f66bSAlan Stern }; 861bb5f66bSAlan Stern 87dbe79bbeSJohn Youn static inline int hub_is_superspeed(struct usb_device *hdev) 88dbe79bbeSJohn Youn { 897bf01185SAman Deep return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS); 90dbe79bbeSJohn Youn } 911bb5f66bSAlan Stern 92fa286188SGreg Kroah-Hartman /* Protect struct usb_device->state and ->children members 939ad3d6ccSAlan Stern * Note: Both are also protected by ->dev.sem, except that ->state can 941da177e4SLinus Torvalds * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 951da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock); 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* khubd's worklist and its lock */ 981da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock); 991da177e4SLinus Torvalds static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* Wakes up khubd */ 1021da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); 1031da177e4SLinus Torvalds 1049c8d6178Sakpm@osdl.org static struct task_struct *khubd_task; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */ 10790ab5ee9SRusty Russell static bool blinkenlights = 0; 1081da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO); 1091da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* 112fd7c519dSJaroslav Kysela * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 113fd7c519dSJaroslav Kysela * 10 seconds to send reply for the initial 64-byte descriptor request. 114fd7c519dSJaroslav Kysela */ 115fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */ 116fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 117fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 118b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout, 119b9cef6c3SWu Fengguang "initial 64-byte descriptor request timeout in milliseconds " 120b9cef6c3SWu Fengguang "(default 5000 - 5.0 seconds)"); 121fd7c519dSJaroslav Kysela 122fd7c519dSJaroslav Kysela /* 1231da177e4SLinus Torvalds * As of 2.6.10 we introduce a new USB device initialization scheme which 1241da177e4SLinus Torvalds * closely resembles the way Windows works. Hopefully it will be compatible 1251da177e4SLinus Torvalds * with a wider range of devices than the old scheme. However some previously 1261da177e4SLinus Torvalds * working devices may start giving rise to "device not accepting address" 1271da177e4SLinus Torvalds * errors; if that happens the user can try the old scheme by adjusting the 1281da177e4SLinus Torvalds * following module parameters. 1291da177e4SLinus Torvalds * 1301da177e4SLinus Torvalds * For maximum flexibility there are two boolean parameters to control the 1311da177e4SLinus Torvalds * hub driver's behavior. On the first initialization attempt, if the 1321da177e4SLinus Torvalds * "old_scheme_first" parameter is set then the old scheme will be used, 1331da177e4SLinus Torvalds * otherwise the new scheme is used. If that fails and "use_both_schemes" 1341da177e4SLinus Torvalds * is set, then the driver will make another attempt, using the other scheme. 1351da177e4SLinus Torvalds */ 13690ab5ee9SRusty Russell static bool old_scheme_first = 0; 1371da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 1381da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first, 1391da177e4SLinus Torvalds "start with the old device initialization scheme"); 1401da177e4SLinus Torvalds 14190ab5ee9SRusty Russell static bool use_both_schemes = 1; 1421da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 1431da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes, 1441da177e4SLinus Torvalds "try the other device initialization scheme if the " 1451da177e4SLinus Torvalds "first one fails"); 1461da177e4SLinus Torvalds 14732fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization. This interferes with 14832fe0198SAlan Stern * port reset on some companion controllers. 14932fe0198SAlan Stern */ 15032fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 15132fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 15232fe0198SAlan Stern 153948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT 1500 154948fea37SAlan Stern #define HUB_DEBOUNCE_STEP 25 155948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE 100 156948fea37SAlan Stern 1571da177e4SLinus Torvalds 158742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev); 159742120c6SMing Lei 160131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus) 1611da177e4SLinus Torvalds { 162131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev)) 163131dec34SSarah Sharp return "5.0 Gb/s"; 164288ead45SAlan Stern if (portstatus & USB_PORT_STAT_HIGH_SPEED) 1651da177e4SLinus Torvalds return "480 Mb/s"; 166288ead45SAlan Stern else if (portstatus & USB_PORT_STAT_LOW_SPEED) 1671da177e4SLinus Torvalds return "1.5 Mb/s"; 1681da177e4SLinus Torvalds else 1691da177e4SLinus Torvalds return "12 Mb/s"; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */ 17325118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev) 1741da177e4SLinus Torvalds { 175fa286188SGreg Kroah-Hartman if (!hdev || !hdev->actconfig) 17625118084SAlan Stern return NULL; 1771da177e4SLinus Torvalds return usb_get_intfdata(hdev->actconfig->interface[0]); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 180d9b2099cSSarah Sharp static int usb_device_supports_lpm(struct usb_device *udev) 181d9b2099cSSarah Sharp { 182d9b2099cSSarah Sharp /* USB 2.1 (and greater) devices indicate LPM support through 183d9b2099cSSarah Sharp * their USB 2.0 Extended Capabilities BOS descriptor. 184d9b2099cSSarah Sharp */ 185d9b2099cSSarah Sharp if (udev->speed == USB_SPEED_HIGH) { 186d9b2099cSSarah Sharp if (udev->bos->ext_cap && 187d9b2099cSSarah Sharp (USB_LPM_SUPPORT & 188d9b2099cSSarah Sharp le32_to_cpu(udev->bos->ext_cap->bmAttributes))) 189d9b2099cSSarah Sharp return 1; 190d9b2099cSSarah Sharp return 0; 191d9b2099cSSarah Sharp } 19251e0a012SSarah Sharp 19351e0a012SSarah Sharp /* All USB 3.0 must support LPM, but we need their max exit latency 19451e0a012SSarah Sharp * information from the SuperSpeed Extended Capabilities BOS descriptor. 19551e0a012SSarah Sharp */ 19651e0a012SSarah Sharp if (!udev->bos->ss_cap) { 19751e0a012SSarah Sharp dev_warn(&udev->dev, "No LPM exit latency info found. " 19851e0a012SSarah Sharp "Power management will be impacted.\n"); 199d9b2099cSSarah Sharp return 0; 200d9b2099cSSarah Sharp } 20151e0a012SSarah Sharp if (udev->parent->lpm_capable) 20251e0a012SSarah Sharp return 1; 20351e0a012SSarah Sharp 20451e0a012SSarah Sharp dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. " 20551e0a012SSarah Sharp "Power management will be impacted.\n"); 20651e0a012SSarah Sharp return 0; 20751e0a012SSarah Sharp } 20851e0a012SSarah Sharp 20951e0a012SSarah Sharp /* 21051e0a012SSarah Sharp * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from 21151e0a012SSarah Sharp * either U1 or U2. 21251e0a012SSarah Sharp */ 21351e0a012SSarah Sharp static void usb_set_lpm_mel(struct usb_device *udev, 21451e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params, 21551e0a012SSarah Sharp unsigned int udev_exit_latency, 21651e0a012SSarah Sharp struct usb_hub *hub, 21751e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params, 21851e0a012SSarah Sharp unsigned int hub_exit_latency) 21951e0a012SSarah Sharp { 22051e0a012SSarah Sharp unsigned int total_mel; 22151e0a012SSarah Sharp unsigned int device_mel; 22251e0a012SSarah Sharp unsigned int hub_mel; 22351e0a012SSarah Sharp 22451e0a012SSarah Sharp /* 22551e0a012SSarah Sharp * Calculate the time it takes to transition all links from the roothub 22651e0a012SSarah Sharp * to the parent hub into U0. The parent hub must then decode the 22751e0a012SSarah Sharp * packet (hub header decode latency) to figure out which port it was 22851e0a012SSarah Sharp * bound for. 22951e0a012SSarah Sharp * 23051e0a012SSarah Sharp * The Hub Header decode latency is expressed in 0.1us intervals (0x1 23151e0a012SSarah Sharp * means 0.1us). Multiply that by 100 to get nanoseconds. 23251e0a012SSarah Sharp */ 23351e0a012SSarah Sharp total_mel = hub_lpm_params->mel + 23451e0a012SSarah Sharp (hub->descriptor->u.ss.bHubHdrDecLat * 100); 23551e0a012SSarah Sharp 23651e0a012SSarah Sharp /* 23751e0a012SSarah Sharp * How long will it take to transition the downstream hub's port into 23851e0a012SSarah Sharp * U0? The greater of either the hub exit latency or the device exit 23951e0a012SSarah Sharp * latency. 24051e0a012SSarah Sharp * 24151e0a012SSarah Sharp * The BOS U1/U2 exit latencies are expressed in 1us intervals. 24251e0a012SSarah Sharp * Multiply that by 1000 to get nanoseconds. 24351e0a012SSarah Sharp */ 24451e0a012SSarah Sharp device_mel = udev_exit_latency * 1000; 24551e0a012SSarah Sharp hub_mel = hub_exit_latency * 1000; 24651e0a012SSarah Sharp if (device_mel > hub_mel) 24751e0a012SSarah Sharp total_mel += device_mel; 24851e0a012SSarah Sharp else 24951e0a012SSarah Sharp total_mel += hub_mel; 25051e0a012SSarah Sharp 25151e0a012SSarah Sharp udev_lpm_params->mel = total_mel; 25251e0a012SSarah Sharp } 25351e0a012SSarah Sharp 25451e0a012SSarah Sharp /* 25551e0a012SSarah Sharp * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate 25651e0a012SSarah Sharp * a transition from either U1 or U2. 25751e0a012SSarah Sharp */ 25851e0a012SSarah Sharp static void usb_set_lpm_pel(struct usb_device *udev, 25951e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params, 26051e0a012SSarah Sharp unsigned int udev_exit_latency, 26151e0a012SSarah Sharp struct usb_hub *hub, 26251e0a012SSarah Sharp struct usb3_lpm_parameters *hub_lpm_params, 26351e0a012SSarah Sharp unsigned int hub_exit_latency, 26451e0a012SSarah Sharp unsigned int port_to_port_exit_latency) 26551e0a012SSarah Sharp { 26651e0a012SSarah Sharp unsigned int first_link_pel; 26751e0a012SSarah Sharp unsigned int hub_pel; 26851e0a012SSarah Sharp 26951e0a012SSarah Sharp /* 27051e0a012SSarah Sharp * First, the device sends an LFPS to transition the link between the 27151e0a012SSarah Sharp * device and the parent hub into U0. The exit latency is the bigger of 27251e0a012SSarah Sharp * the device exit latency or the hub exit latency. 27351e0a012SSarah Sharp */ 27451e0a012SSarah Sharp if (udev_exit_latency > hub_exit_latency) 27551e0a012SSarah Sharp first_link_pel = udev_exit_latency * 1000; 27651e0a012SSarah Sharp else 27751e0a012SSarah Sharp first_link_pel = hub_exit_latency * 1000; 27851e0a012SSarah Sharp 27951e0a012SSarah Sharp /* 28051e0a012SSarah Sharp * When the hub starts to receive the LFPS, there is a slight delay for 28151e0a012SSarah Sharp * it to figure out that one of the ports is sending an LFPS. Then it 28251e0a012SSarah Sharp * will forward the LFPS to its upstream link. The exit latency is the 28351e0a012SSarah Sharp * delay, plus the PEL that we calculated for this hub. 28451e0a012SSarah Sharp */ 28551e0a012SSarah Sharp hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; 28651e0a012SSarah Sharp 28751e0a012SSarah Sharp /* 28851e0a012SSarah Sharp * According to figure C-7 in the USB 3.0 spec, the PEL for this device 28951e0a012SSarah Sharp * is the greater of the two exit latencies. 29051e0a012SSarah Sharp */ 29151e0a012SSarah Sharp if (first_link_pel > hub_pel) 29251e0a012SSarah Sharp udev_lpm_params->pel = first_link_pel; 29351e0a012SSarah Sharp else 29451e0a012SSarah Sharp udev_lpm_params->pel = hub_pel; 29551e0a012SSarah Sharp } 29651e0a012SSarah Sharp 29751e0a012SSarah Sharp /* 29851e0a012SSarah Sharp * Set the System Exit Latency (SEL) to indicate the total worst-case time from 29951e0a012SSarah Sharp * when a device initiates a transition to U0, until when it will receive the 30051e0a012SSarah Sharp * first packet from the host controller. 30151e0a012SSarah Sharp * 30251e0a012SSarah Sharp * Section C.1.5.1 describes the four components to this: 30351e0a012SSarah Sharp * - t1: device PEL 30451e0a012SSarah Sharp * - t2: time for the ERDY to make it from the device to the host. 30551e0a012SSarah Sharp * - t3: a host-specific delay to process the ERDY. 30651e0a012SSarah Sharp * - t4: time for the packet to make it from the host to the device. 30751e0a012SSarah Sharp * 30851e0a012SSarah Sharp * t3 is specific to both the xHCI host and the platform the host is integrated 30951e0a012SSarah Sharp * into. The Intel HW folks have said it's negligible, FIXME if a different 31051e0a012SSarah Sharp * vendor says otherwise. 31151e0a012SSarah Sharp */ 31251e0a012SSarah Sharp static void usb_set_lpm_sel(struct usb_device *udev, 31351e0a012SSarah Sharp struct usb3_lpm_parameters *udev_lpm_params) 31451e0a012SSarah Sharp { 31551e0a012SSarah Sharp struct usb_device *parent; 31651e0a012SSarah Sharp unsigned int num_hubs; 31751e0a012SSarah Sharp unsigned int total_sel; 31851e0a012SSarah Sharp 31951e0a012SSarah Sharp /* t1 = device PEL */ 32051e0a012SSarah Sharp total_sel = udev_lpm_params->pel; 32151e0a012SSarah Sharp /* How many external hubs are in between the device & the root port. */ 32251e0a012SSarah Sharp for (parent = udev->parent, num_hubs = 0; parent->parent; 32351e0a012SSarah Sharp parent = parent->parent) 32451e0a012SSarah Sharp num_hubs++; 32551e0a012SSarah Sharp /* t2 = 2.1us + 250ns * (num_hubs - 1) */ 32651e0a012SSarah Sharp if (num_hubs > 0) 32751e0a012SSarah Sharp total_sel += 2100 + 250 * (num_hubs - 1); 32851e0a012SSarah Sharp 32951e0a012SSarah Sharp /* t4 = 250ns * num_hubs */ 33051e0a012SSarah Sharp total_sel += 250 * num_hubs; 33151e0a012SSarah Sharp 33251e0a012SSarah Sharp udev_lpm_params->sel = total_sel; 33351e0a012SSarah Sharp } 33451e0a012SSarah Sharp 33551e0a012SSarah Sharp static void usb_set_lpm_parameters(struct usb_device *udev) 33651e0a012SSarah Sharp { 33751e0a012SSarah Sharp struct usb_hub *hub; 33851e0a012SSarah Sharp unsigned int port_to_port_delay; 33951e0a012SSarah Sharp unsigned int udev_u1_del; 34051e0a012SSarah Sharp unsigned int udev_u2_del; 34151e0a012SSarah Sharp unsigned int hub_u1_del; 34251e0a012SSarah Sharp unsigned int hub_u2_del; 34351e0a012SSarah Sharp 34451e0a012SSarah Sharp if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER) 34551e0a012SSarah Sharp return; 34651e0a012SSarah Sharp 34751e0a012SSarah Sharp hub = hdev_to_hub(udev->parent); 34851e0a012SSarah Sharp /* It doesn't take time to transition the roothub into U0, since it 34951e0a012SSarah Sharp * doesn't have an upstream link. 35051e0a012SSarah Sharp */ 35151e0a012SSarah Sharp if (!hub) 35251e0a012SSarah Sharp return; 35351e0a012SSarah Sharp 35451e0a012SSarah Sharp udev_u1_del = udev->bos->ss_cap->bU1devExitLat; 35551e0a012SSarah Sharp udev_u2_del = udev->bos->ss_cap->bU2DevExitLat; 35651e0a012SSarah Sharp hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; 35751e0a012SSarah Sharp hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat; 35851e0a012SSarah Sharp 35951e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, 36051e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del); 36151e0a012SSarah Sharp 36251e0a012SSarah Sharp usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, 36351e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del); 36451e0a012SSarah Sharp 36551e0a012SSarah Sharp /* 36651e0a012SSarah Sharp * Appendix C, section C.2.2.2, says that there is a slight delay from 36751e0a012SSarah Sharp * when the parent hub notices the downstream port is trying to 36851e0a012SSarah Sharp * transition to U0 to when the hub initiates a U0 transition on its 36951e0a012SSarah Sharp * upstream port. The section says the delays are tPort2PortU1EL and 37051e0a012SSarah Sharp * tPort2PortU2EL, but it doesn't define what they are. 37151e0a012SSarah Sharp * 37251e0a012SSarah Sharp * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking 37351e0a012SSarah Sharp * about the same delays. Use the maximum delay calculations from those 37451e0a012SSarah Sharp * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For 37551e0a012SSarah Sharp * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I 37651e0a012SSarah Sharp * assume the device exit latencies they are talking about are the hub 37751e0a012SSarah Sharp * exit latencies. 37851e0a012SSarah Sharp * 37951e0a012SSarah Sharp * What do we do if the U2 exit latency is less than the U1 exit 38051e0a012SSarah Sharp * latency? It's possible, although not likely... 38151e0a012SSarah Sharp */ 38251e0a012SSarah Sharp port_to_port_delay = 1; 38351e0a012SSarah Sharp 38451e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, 38551e0a012SSarah Sharp hub, &udev->parent->u1_params, hub_u1_del, 38651e0a012SSarah Sharp port_to_port_delay); 38751e0a012SSarah Sharp 38851e0a012SSarah Sharp if (hub_u2_del > hub_u1_del) 38951e0a012SSarah Sharp port_to_port_delay = 1 + hub_u2_del - hub_u1_del; 39051e0a012SSarah Sharp else 39151e0a012SSarah Sharp port_to_port_delay = 1 + hub_u1_del; 39251e0a012SSarah Sharp 39351e0a012SSarah Sharp usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, 39451e0a012SSarah Sharp hub, &udev->parent->u2_params, hub_u2_del, 39551e0a012SSarah Sharp port_to_port_delay); 39651e0a012SSarah Sharp 39751e0a012SSarah Sharp /* Now that we've got PEL, calculate SEL. */ 39851e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u1_params); 39951e0a012SSarah Sharp usb_set_lpm_sel(udev, &udev->u2_params); 40051e0a012SSarah Sharp } 401d9b2099cSSarah Sharp 4021da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */ 403dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data) 4041da177e4SLinus Torvalds { 405dbe79bbeSJohn Youn int i, ret, size; 406dbe79bbeSJohn Youn unsigned dtype; 407dbe79bbeSJohn Youn 408dbe79bbeSJohn Youn if (hub_is_superspeed(hdev)) { 409dbe79bbeSJohn Youn dtype = USB_DT_SS_HUB; 410dbe79bbeSJohn Youn size = USB_DT_SS_HUB_SIZE; 411dbe79bbeSJohn Youn } else { 412dbe79bbeSJohn Youn dtype = USB_DT_HUB; 413dbe79bbeSJohn Youn size = sizeof(struct usb_hub_descriptor); 414dbe79bbeSJohn Youn } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds for (i = 0; i < 3; i++) { 4171da177e4SLinus Torvalds ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 4181da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 419dbe79bbeSJohn Youn dtype << 8, 0, data, size, 4201da177e4SLinus Torvalds USB_CTRL_GET_TIMEOUT); 4211da177e4SLinus Torvalds if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) 4221da177e4SLinus Torvalds return ret; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds return -EINVAL; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* 4281da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.1 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4331da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.2 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4421da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 4431da177e4SLinus Torvalds NULL, 0, 1000); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* 4471da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.13 4481da177e4SLinus Torvalds */ 4491da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 4521da177e4SLinus Torvalds USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 4531da177e4SLinus Torvalds NULL, 0, 1000); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 4571da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 4581da177e4SLinus Torvalds * for info about using port indicators 4591da177e4SLinus Torvalds */ 4601da177e4SLinus Torvalds static void set_port_led( 4611da177e4SLinus Torvalds struct usb_hub *hub, 4621da177e4SLinus Torvalds int port1, 4631da177e4SLinus Torvalds int selector 4641da177e4SLinus Torvalds ) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds int status = set_port_feature(hub->hdev, (selector << 8) | port1, 4671da177e4SLinus Torvalds USB_PORT_FEAT_INDICATOR); 4681da177e4SLinus Torvalds if (status < 0) 4691da177e4SLinus Torvalds dev_dbg (hub->intfdev, 4701da177e4SLinus Torvalds "port %d indicator %s status %d\n", 4711da177e4SLinus Torvalds port1, 4721da177e4SLinus Torvalds ({ char *s; switch (selector) { 4731da177e4SLinus Torvalds case HUB_LED_AMBER: s = "amber"; break; 4741da177e4SLinus Torvalds case HUB_LED_GREEN: s = "green"; break; 4751da177e4SLinus Torvalds case HUB_LED_OFF: s = "off"; break; 4761da177e4SLinus Torvalds case HUB_LED_AUTO: s = "auto"; break; 4771da177e4SLinus Torvalds default: s = "??"; break; 4781da177e4SLinus Torvalds }; s; }), 4791da177e4SLinus Torvalds status); 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds #define LED_CYCLE_PERIOD ((2*HZ)/3) 4831da177e4SLinus Torvalds 484c4028958SDavid Howells static void led_work (struct work_struct *work) 4851da177e4SLinus Torvalds { 486c4028958SDavid Howells struct usb_hub *hub = 487c4028958SDavid Howells container_of(work, struct usb_hub, leds.work); 4881da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 4891da177e4SLinus Torvalds unsigned i; 4901da177e4SLinus Torvalds unsigned changed = 0; 4911da177e4SLinus Torvalds int cursor = -1; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 4941da177e4SLinus Torvalds return; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds for (i = 0; i < hub->descriptor->bNbrPorts; i++) { 4971da177e4SLinus Torvalds unsigned selector, mode; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* 30%-50% duty cycle */ 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds switch (hub->indicator[i]) { 5021da177e4SLinus Torvalds /* cycle marker */ 5031da177e4SLinus Torvalds case INDICATOR_CYCLE: 5041da177e4SLinus Torvalds cursor = i; 5051da177e4SLinus Torvalds selector = HUB_LED_AUTO; 5061da177e4SLinus Torvalds mode = INDICATOR_AUTO; 5071da177e4SLinus Torvalds break; 5081da177e4SLinus Torvalds /* blinking green = sw attention */ 5091da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK: 5101da177e4SLinus Torvalds selector = HUB_LED_GREEN; 5111da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK_OFF; 5121da177e4SLinus Torvalds break; 5131da177e4SLinus Torvalds case INDICATOR_GREEN_BLINK_OFF: 5141da177e4SLinus Torvalds selector = HUB_LED_OFF; 5151da177e4SLinus Torvalds mode = INDICATOR_GREEN_BLINK; 5161da177e4SLinus Torvalds break; 5171da177e4SLinus Torvalds /* blinking amber = hw attention */ 5181da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK: 5191da177e4SLinus Torvalds selector = HUB_LED_AMBER; 5201da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK_OFF; 5211da177e4SLinus Torvalds break; 5221da177e4SLinus Torvalds case INDICATOR_AMBER_BLINK_OFF: 5231da177e4SLinus Torvalds selector = HUB_LED_OFF; 5241da177e4SLinus Torvalds mode = INDICATOR_AMBER_BLINK; 5251da177e4SLinus Torvalds break; 5261da177e4SLinus Torvalds /* blink green/amber = reserved */ 5271da177e4SLinus Torvalds case INDICATOR_ALT_BLINK: 5281da177e4SLinus Torvalds selector = HUB_LED_GREEN; 5291da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK_OFF; 5301da177e4SLinus Torvalds break; 5311da177e4SLinus Torvalds case INDICATOR_ALT_BLINK_OFF: 5321da177e4SLinus Torvalds selector = HUB_LED_AMBER; 5331da177e4SLinus Torvalds mode = INDICATOR_ALT_BLINK; 5341da177e4SLinus Torvalds break; 5351da177e4SLinus Torvalds default: 5361da177e4SLinus Torvalds continue; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds if (selector != HUB_LED_AUTO) 5391da177e4SLinus Torvalds changed = 1; 5401da177e4SLinus Torvalds set_port_led(hub, i + 1, selector); 5411da177e4SLinus Torvalds hub->indicator[i] = mode; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds if (!changed && blinkenlights) { 5441da177e4SLinus Torvalds cursor++; 5451da177e4SLinus Torvalds cursor %= hub->descriptor->bNbrPorts; 5461da177e4SLinus Torvalds set_port_led(hub, cursor + 1, HUB_LED_GREEN); 5471da177e4SLinus Torvalds hub->indicator[cursor] = INDICATOR_CYCLE; 5481da177e4SLinus Torvalds changed++; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds if (changed) 5511da177e4SLinus Torvalds schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */ 5551da177e4SLinus Torvalds #define USB_STS_TIMEOUT 1000 5561da177e4SLinus Torvalds #define USB_STS_RETRIES 5 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* 5591da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.6 5601da177e4SLinus Torvalds */ 5611da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev, 5621da177e4SLinus Torvalds struct usb_hub_status *data) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 5651da177e4SLinus Torvalds 5663824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES && 5673824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) { 5681da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 5691da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 5701da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds return status; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 5761da177e4SLinus Torvalds * USB 2.0 spec Section 11.24.2.7 5771da177e4SLinus Torvalds */ 5781da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1, 5791da177e4SLinus Torvalds struct usb_port_status *data) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds int i, status = -ETIMEDOUT; 5821da177e4SLinus Torvalds 5833824c1ddSLibor Pechacek for (i = 0; i < USB_STS_RETRIES && 5843824c1ddSLibor Pechacek (status == -ETIMEDOUT || status == -EPIPE); i++) { 5851da177e4SLinus Torvalds status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 5861da177e4SLinus Torvalds USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, 5871da177e4SLinus Torvalds data, sizeof(*data), USB_STS_TIMEOUT); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds return status; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 5923eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1, 5933eb14915SAlan Stern u16 *status, u16 *change) 5943eb14915SAlan Stern { 5953eb14915SAlan Stern int ret; 5963eb14915SAlan Stern 5973eb14915SAlan Stern mutex_lock(&hub->status_mutex); 5983eb14915SAlan Stern ret = get_port_status(hub->hdev, port1, &hub->status->port); 5993eb14915SAlan Stern if (ret < 4) { 6003eb14915SAlan Stern dev_err(hub->intfdev, 6013eb14915SAlan Stern "%s failed (err = %d)\n", __func__, ret); 6023eb14915SAlan Stern if (ret >= 0) 6033eb14915SAlan Stern ret = -EIO; 6043eb14915SAlan Stern } else { 6053eb14915SAlan Stern *status = le16_to_cpu(hub->status->port.wPortStatus); 6063eb14915SAlan Stern *change = le16_to_cpu(hub->status->port.wPortChange); 607dbe79bbeSJohn Youn 6083eb14915SAlan Stern ret = 0; 6093eb14915SAlan Stern } 6103eb14915SAlan Stern mutex_unlock(&hub->status_mutex); 6113eb14915SAlan Stern return ret; 6123eb14915SAlan Stern } 6133eb14915SAlan Stern 6141da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds unsigned long flags; 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds spin_lock_irqsave(&hub_event_lock, flags); 6192e2c5eeaSRoel Kluin if (!hub->disconnected && list_empty(&hub->event_list)) { 6201da177e4SLinus Torvalds list_add_tail(&hub->event_list, &hub_event_list); 6218e4ceb38SAlan Stern 6228e4ceb38SAlan Stern /* Suppress autosuspend until khubd runs */ 6238e4ceb38SAlan Stern usb_autopm_get_interface_no_resume( 6248e4ceb38SAlan Stern to_usb_interface(hub->intfdev)); 6251da177e4SLinus Torvalds wake_up(&khubd_wait); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds spin_unlock_irqrestore(&hub_event_lock, flags); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev) 6311da177e4SLinus Torvalds { 63225118084SAlan Stern struct usb_hub *hub = hdev_to_hub(hdev); 63325118084SAlan Stern 63425118084SAlan Stern if (hub) 63525118084SAlan Stern kick_khubd(hub); 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6384ee823b8SSarah Sharp /* 6394ee823b8SSarah Sharp * Let the USB core know that a USB 3.0 device has sent a Function Wake Device 6404ee823b8SSarah Sharp * Notification, which indicates it had initiated remote wakeup. 6414ee823b8SSarah Sharp * 6424ee823b8SSarah Sharp * USB 3.0 hubs do not report the port link state change from U3 to U0 when the 6434ee823b8SSarah Sharp * device initiates resume, so the USB core will not receive notice of the 6444ee823b8SSarah Sharp * resume through the normal hub interrupt URB. 6454ee823b8SSarah Sharp */ 6464ee823b8SSarah Sharp void usb_wakeup_notification(struct usb_device *hdev, 6474ee823b8SSarah Sharp unsigned int portnum) 6484ee823b8SSarah Sharp { 6494ee823b8SSarah Sharp struct usb_hub *hub; 6504ee823b8SSarah Sharp 6514ee823b8SSarah Sharp if (!hdev) 6524ee823b8SSarah Sharp return; 6534ee823b8SSarah Sharp 6544ee823b8SSarah Sharp hub = hdev_to_hub(hdev); 6554ee823b8SSarah Sharp if (hub) { 6564ee823b8SSarah Sharp set_bit(portnum, hub->wakeup_bits); 6574ee823b8SSarah Sharp kick_khubd(hub); 6584ee823b8SSarah Sharp } 6594ee823b8SSarah Sharp } 6604ee823b8SSarah Sharp EXPORT_SYMBOL_GPL(usb_wakeup_notification); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */ 6637d12e780SDavid Howells static void hub_irq(struct urb *urb) 6641da177e4SLinus Torvalds { 665ec17cf1cSTobias Klauser struct usb_hub *hub = urb->context; 666e015268dSAlan Stern int status = urb->status; 66771d2718fSRoel Kluin unsigned i; 6681da177e4SLinus Torvalds unsigned long bits; 6691da177e4SLinus Torvalds 670e015268dSAlan Stern switch (status) { 6711da177e4SLinus Torvalds case -ENOENT: /* synchronous unlink */ 6721da177e4SLinus Torvalds case -ECONNRESET: /* async unlink */ 6731da177e4SLinus Torvalds case -ESHUTDOWN: /* hardware going away */ 6741da177e4SLinus Torvalds return; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds default: /* presumably an error */ 6771da177e4SLinus Torvalds /* Cause a hub reset after 10 consecutive errors */ 678e015268dSAlan Stern dev_dbg (hub->intfdev, "transfer --> %d\n", status); 6791da177e4SLinus Torvalds if ((++hub->nerrors < 10) || hub->error) 6801da177e4SLinus Torvalds goto resubmit; 681e015268dSAlan Stern hub->error = status; 6821da177e4SLinus Torvalds /* FALL THROUGH */ 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /* let khubd handle things */ 6851da177e4SLinus Torvalds case 0: /* we got data: port status changed */ 6861da177e4SLinus Torvalds bits = 0; 6871da177e4SLinus Torvalds for (i = 0; i < urb->actual_length; ++i) 6881da177e4SLinus Torvalds bits |= ((unsigned long) ((*hub->buffer)[i])) 6891da177e4SLinus Torvalds << (i*8); 6901da177e4SLinus Torvalds hub->event_bits[0] = bits; 6911da177e4SLinus Torvalds break; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds hub->nerrors = 0; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* Something happened, let khubd figure it out */ 6971da177e4SLinus Torvalds kick_khubd(hub); 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds resubmit: 7001da177e4SLinus Torvalds if (hub->quiescing) 7011da177e4SLinus Torvalds return; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 7041da177e4SLinus Torvalds && status != -ENODEV && status != -EPERM) 7051da177e4SLinus Torvalds dev_err (hub->intfdev, "resubmit --> %d\n", status); 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */ 7091da177e4SLinus Torvalds static inline int 7101da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) 7111da177e4SLinus Torvalds { 712c2f6595fSAlan Stern return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 7131da177e4SLinus Torvalds HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 7141da177e4SLinus Torvalds tt, NULL, 0, 1000); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* 7181da177e4SLinus Torvalds * enumeration blocks khubd for a long time. we use keventd instead, since 7191da177e4SLinus Torvalds * long blocking there is the exception, not the rule. accordingly, HCDs 7201da177e4SLinus Torvalds * talking to TTs must queue control transfers (not just bulk and iso), so 7211da177e4SLinus Torvalds * both can talk to the same hub concurrently. 7221da177e4SLinus Torvalds */ 723cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work) 7241da177e4SLinus Torvalds { 725c4028958SDavid Howells struct usb_hub *hub = 726cb88a1b8SAlan Stern container_of(work, struct usb_hub, tt.clear_work); 7271da177e4SLinus Torvalds unsigned long flags; 72855e5fdfaSMark Lord int limit = 100; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds spin_lock_irqsave (&hub->tt.lock, flags); 73155e5fdfaSMark Lord while (--limit && !list_empty (&hub->tt.clear_list)) { 732d0f830d3SH Hartley Sweeten struct list_head *next; 7331da177e4SLinus Torvalds struct usb_tt_clear *clear; 7341da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 735cb88a1b8SAlan Stern const struct hc_driver *drv; 7361da177e4SLinus Torvalds int status; 7371da177e4SLinus Torvalds 738d0f830d3SH Hartley Sweeten next = hub->tt.clear_list.next; 739d0f830d3SH Hartley Sweeten clear = list_entry (next, struct usb_tt_clear, clear_list); 7401da177e4SLinus Torvalds list_del (&clear->clear_list); 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /* drop lock so HCD can concurrently report other TT errors */ 7431da177e4SLinus Torvalds spin_unlock_irqrestore (&hub->tt.lock, flags); 7441da177e4SLinus Torvalds status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); 7451da177e4SLinus Torvalds if (status) 7461da177e4SLinus Torvalds dev_err (&hdev->dev, 7471da177e4SLinus Torvalds "clear tt %d (%04x) error %d\n", 7481da177e4SLinus Torvalds clear->tt, clear->devinfo, status); 749cb88a1b8SAlan Stern 750cb88a1b8SAlan Stern /* Tell the HCD, even if the operation failed */ 751cb88a1b8SAlan Stern drv = clear->hcd->driver; 752cb88a1b8SAlan Stern if (drv->clear_tt_buffer_complete) 753cb88a1b8SAlan Stern (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); 754cb88a1b8SAlan Stern 7551da177e4SLinus Torvalds kfree(clear); 756cb88a1b8SAlan Stern spin_lock_irqsave(&hub->tt.lock, flags); 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds spin_unlock_irqrestore (&hub->tt.lock, flags); 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /** 762cb88a1b8SAlan Stern * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub 763cb88a1b8SAlan Stern * @urb: an URB associated with the failed or incomplete split transaction 7641da177e4SLinus Torvalds * 7651da177e4SLinus Torvalds * High speed HCDs use this to tell the hub driver that some split control or 7661da177e4SLinus Torvalds * bulk transaction failed in a way that requires clearing internal state of 7671da177e4SLinus Torvalds * a transaction translator. This is normally detected (and reported) from 7681da177e4SLinus Torvalds * interrupt context. 7691da177e4SLinus Torvalds * 7701da177e4SLinus Torvalds * It may not be possible for that hub to handle additional full (or low) 7711da177e4SLinus Torvalds * speed transactions until that state is fully cleared out. 7721da177e4SLinus Torvalds */ 773cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb) 7741da177e4SLinus Torvalds { 775cb88a1b8SAlan Stern struct usb_device *udev = urb->dev; 776cb88a1b8SAlan Stern int pipe = urb->pipe; 7771da177e4SLinus Torvalds struct usb_tt *tt = udev->tt; 7781da177e4SLinus Torvalds unsigned long flags; 7791da177e4SLinus Torvalds struct usb_tt_clear *clear; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds /* we've got to cope with an arbitrary number of pending TT clears, 7821da177e4SLinus Torvalds * since each TT has "at least two" buffers that can need it (and 7831da177e4SLinus Torvalds * there can be many TTs per hub). even if they're uncommon. 7841da177e4SLinus Torvalds */ 78554e6ecb2SChristoph Lameter if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { 7861da177e4SLinus Torvalds dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 7871da177e4SLinus Torvalds /* FIXME recover somehow ... RESET_TT? */ 788cb88a1b8SAlan Stern return -ENOMEM; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* info that CLEAR_TT_BUFFER needs */ 7921da177e4SLinus Torvalds clear->tt = tt->multi ? udev->ttport : 1; 7931da177e4SLinus Torvalds clear->devinfo = usb_pipeendpoint (pipe); 7941da177e4SLinus Torvalds clear->devinfo |= udev->devnum << 4; 7951da177e4SLinus Torvalds clear->devinfo |= usb_pipecontrol (pipe) 7961da177e4SLinus Torvalds ? (USB_ENDPOINT_XFER_CONTROL << 11) 7971da177e4SLinus Torvalds : (USB_ENDPOINT_XFER_BULK << 11); 7981da177e4SLinus Torvalds if (usb_pipein (pipe)) 7991da177e4SLinus Torvalds clear->devinfo |= 1 << 15; 8001da177e4SLinus Torvalds 801cb88a1b8SAlan Stern /* info for completion callback */ 802cb88a1b8SAlan Stern clear->hcd = bus_to_hcd(udev->bus); 803cb88a1b8SAlan Stern clear->ep = urb->ep; 804cb88a1b8SAlan Stern 8051da177e4SLinus Torvalds /* tell keventd to clear state for this TT */ 8061da177e4SLinus Torvalds spin_lock_irqsave (&tt->lock, flags); 8071da177e4SLinus Torvalds list_add_tail (&clear->clear_list, &tt->clear_list); 808cb88a1b8SAlan Stern schedule_work(&tt->clear_work); 8091da177e4SLinus Torvalds spin_unlock_irqrestore (&tt->lock, flags); 810cb88a1b8SAlan Stern return 0; 8111da177e4SLinus Torvalds } 812cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); 8131da177e4SLinus Torvalds 8148520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller 8158520f380SAlan Stern * needs to delay. 8168520f380SAlan Stern */ 8178520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds int port1; 820b789696aSDavid Brownell unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; 8218520f380SAlan Stern unsigned delay; 8224489a571SAlan Stern u16 wHubCharacteristics = 8234489a571SAlan Stern le16_to_cpu(hub->descriptor->wHubCharacteristics); 8241da177e4SLinus Torvalds 8254489a571SAlan Stern /* Enable power on each port. Some hubs have reserved values 8264489a571SAlan Stern * of LPSM (> 2) in their descriptors, even though they are 8274489a571SAlan Stern * USB 2.0 hubs. Some hubs do not implement port-power switching 8284489a571SAlan Stern * but only emulate it. In all cases, the ports won't work 8294489a571SAlan Stern * unless we send these messages to the hub. 8304489a571SAlan Stern */ 8314489a571SAlan Stern if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) 8321da177e4SLinus Torvalds dev_dbg(hub->intfdev, "enabling power on all ports\n"); 8334489a571SAlan Stern else 8344489a571SAlan Stern dev_dbg(hub->intfdev, "trying to enable port power on " 8354489a571SAlan Stern "non-switchable hub\n"); 8361da177e4SLinus Torvalds for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) 8374489a571SAlan Stern set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 8381da177e4SLinus Torvalds 839b789696aSDavid Brownell /* Wait at least 100 msec for power to become stable */ 8408520f380SAlan Stern delay = max(pgood_delay, (unsigned) 100); 8418520f380SAlan Stern if (do_delay) 8428520f380SAlan Stern msleep(delay); 8438520f380SAlan Stern return delay; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub, 8471da177e4SLinus Torvalds u16 *status, u16 *change) 8481da177e4SLinus Torvalds { 8491da177e4SLinus Torvalds int ret; 8501da177e4SLinus Torvalds 851db90e7a1SAlan Stern mutex_lock(&hub->status_mutex); 8521da177e4SLinus Torvalds ret = get_hub_status(hub->hdev, &hub->status->hub); 8531da177e4SLinus Torvalds if (ret < 0) 8541da177e4SLinus Torvalds dev_err (hub->intfdev, 855441b62c1SHarvey Harrison "%s failed (err = %d)\n", __func__, ret); 8561da177e4SLinus Torvalds else { 8571da177e4SLinus Torvalds *status = le16_to_cpu(hub->status->hub.wHubStatus); 8581da177e4SLinus Torvalds *change = le16_to_cpu(hub->status->hub.wHubChange); 8591da177e4SLinus Torvalds ret = 0; 8601da177e4SLinus Torvalds } 861db90e7a1SAlan Stern mutex_unlock(&hub->status_mutex); 8621da177e4SLinus Torvalds return ret; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8658b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 8668b28c752SAlan Stern { 8678b28c752SAlan Stern struct usb_device *hdev = hub->hdev; 8680458d5b4SAlan Stern int ret = 0; 8698b28c752SAlan Stern 870fa286188SGreg Kroah-Hartman if (hdev->children[port1-1] && set_state) 871fa286188SGreg Kroah-Hartman usb_set_device_state(hdev->children[port1-1], 8728b28c752SAlan Stern USB_STATE_NOTATTACHED); 873dbe79bbeSJohn Youn if (!hub->error && !hub_is_superspeed(hub->hdev)) 8748b28c752SAlan Stern ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); 8758b28c752SAlan Stern if (ret) 8768b28c752SAlan Stern dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 8778b28c752SAlan Stern port1, ret); 8788b28c752SAlan Stern return ret; 8798b28c752SAlan Stern } 8808b28c752SAlan Stern 8810458d5b4SAlan Stern /* 8826d42fcdbSJustin P. Mattock * Disable a port and mark a logical connect-change event, so that some 8830458d5b4SAlan Stern * time later khubd will disconnect() any existing usb_device on the port 8840458d5b4SAlan Stern * and will re-enumerate if there actually is a device attached. 8850458d5b4SAlan Stern */ 8860458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 8877d069b7dSAlan Stern { 8880458d5b4SAlan Stern dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); 8890458d5b4SAlan Stern hub_port_disable(hub, port1, 1); 8900458d5b4SAlan Stern 8910458d5b4SAlan Stern /* FIXME let caller ask to power down the port: 8920458d5b4SAlan Stern * - some devices won't enumerate without a VBUS power cycle 8930458d5b4SAlan Stern * - SRP saves power that way 8940458d5b4SAlan Stern * - ... new call, TBD ... 8950458d5b4SAlan Stern * That's easy if this hub can switch power per-port, and 8960458d5b4SAlan Stern * khubd reactivates the port later (timer, SRP, etc). 8970458d5b4SAlan Stern * Powerdown must be optional, because of reset/DFU. 8980458d5b4SAlan Stern */ 8990458d5b4SAlan Stern 9000458d5b4SAlan Stern set_bit(port1, hub->change_bits); 9010458d5b4SAlan Stern kick_khubd(hub); 9020458d5b4SAlan Stern } 9030458d5b4SAlan Stern 904253e0572SAlan Stern /** 905253e0572SAlan Stern * usb_remove_device - disable a device's port on its parent hub 906253e0572SAlan Stern * @udev: device to be disabled and removed 907253e0572SAlan Stern * Context: @udev locked, must be able to sleep. 908253e0572SAlan Stern * 909253e0572SAlan Stern * After @udev's port has been disabled, khubd is notified and it will 910253e0572SAlan Stern * see that the device has been disconnected. When the device is 911253e0572SAlan Stern * physically unplugged and something is plugged in, the events will 912253e0572SAlan Stern * be received and processed normally. 913253e0572SAlan Stern */ 914253e0572SAlan Stern int usb_remove_device(struct usb_device *udev) 915253e0572SAlan Stern { 916253e0572SAlan Stern struct usb_hub *hub; 917253e0572SAlan Stern struct usb_interface *intf; 918253e0572SAlan Stern 919253e0572SAlan Stern if (!udev->parent) /* Can't remove a root hub */ 920253e0572SAlan Stern return -EINVAL; 921253e0572SAlan Stern hub = hdev_to_hub(udev->parent); 922253e0572SAlan Stern intf = to_usb_interface(hub->intfdev); 923253e0572SAlan Stern 924253e0572SAlan Stern usb_autopm_get_interface(intf); 925253e0572SAlan Stern set_bit(udev->portnum, hub->removed_bits); 926253e0572SAlan Stern hub_port_logical_disconnect(hub, udev->portnum); 927253e0572SAlan Stern usb_autopm_put_interface(intf); 928253e0572SAlan Stern return 0; 929253e0572SAlan Stern } 930253e0572SAlan Stern 9316ee0b270SAlan Stern enum hub_activation_type { 9328e4ceb38SAlan Stern HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ 9338520f380SAlan Stern HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 9346ee0b270SAlan Stern }; 9355e6effaeSAlan Stern 9368520f380SAlan Stern static void hub_init_func2(struct work_struct *ws); 9378520f380SAlan Stern static void hub_init_func3(struct work_struct *ws); 9388520f380SAlan Stern 939f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) 9405e6effaeSAlan Stern { 9415e6effaeSAlan Stern struct usb_device *hdev = hub->hdev; 942653a39d1SSarah Sharp struct usb_hcd *hcd; 943653a39d1SSarah Sharp int ret; 9445e6effaeSAlan Stern int port1; 945f2835219SAlan Stern int status; 946948fea37SAlan Stern bool need_debounce_delay = false; 9478520f380SAlan Stern unsigned delay; 9488520f380SAlan Stern 9498520f380SAlan Stern /* Continue a partial initialization */ 9508520f380SAlan Stern if (type == HUB_INIT2) 9518520f380SAlan Stern goto init2; 9528520f380SAlan Stern if (type == HUB_INIT3) 9538520f380SAlan Stern goto init3; 9545e6effaeSAlan Stern 955a45aa3b3SElric Fu /* The superspeed hub except for root hub has to use Hub Depth 956a45aa3b3SElric Fu * value as an offset into the route string to locate the bits 957a45aa3b3SElric Fu * it uses to determine the downstream port number. So hub driver 958a45aa3b3SElric Fu * should send a set hub depth request to superspeed hub after 959a45aa3b3SElric Fu * the superspeed hub is set configuration in initialization or 960a45aa3b3SElric Fu * reset procedure. 961a45aa3b3SElric Fu * 962a45aa3b3SElric Fu * After a resume, port power should still be on. 963f2835219SAlan Stern * For any other type of activation, turn it on. 964f2835219SAlan Stern */ 9658520f380SAlan Stern if (type != HUB_RESUME) { 966a45aa3b3SElric Fu if (hdev->parent && hub_is_superspeed(hdev)) { 967a45aa3b3SElric Fu ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 968a45aa3b3SElric Fu HUB_SET_DEPTH, USB_RT_HUB, 969a45aa3b3SElric Fu hdev->level - 1, 0, NULL, 0, 970a45aa3b3SElric Fu USB_CTRL_SET_TIMEOUT); 971a45aa3b3SElric Fu if (ret < 0) 972a45aa3b3SElric Fu dev_err(hub->intfdev, 973a45aa3b3SElric Fu "set hub depth failed\n"); 974a45aa3b3SElric Fu } 9758520f380SAlan Stern 9768520f380SAlan Stern /* Speed up system boot by using a delayed_work for the 9778520f380SAlan Stern * hub's initial power-up delays. This is pretty awkward 9788520f380SAlan Stern * and the implementation looks like a home-brewed sort of 9798520f380SAlan Stern * setjmp/longjmp, but it saves at least 100 ms for each 9808520f380SAlan Stern * root hub (assuming usbcore is compiled into the kernel 9818520f380SAlan Stern * rather than as a module). It adds up. 9828520f380SAlan Stern * 9838520f380SAlan Stern * This can't be done for HUB_RESUME or HUB_RESET_RESUME 9848520f380SAlan Stern * because for those activation types the ports have to be 9858520f380SAlan Stern * operational when we return. In theory this could be done 9868520f380SAlan Stern * for HUB_POST_RESET, but it's easier not to. 9878520f380SAlan Stern */ 9888520f380SAlan Stern if (type == HUB_INIT) { 9898520f380SAlan Stern delay = hub_power_on(hub, false); 9908520f380SAlan Stern PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); 9918520f380SAlan Stern schedule_delayed_work(&hub->init_work, 9928520f380SAlan Stern msecs_to_jiffies(delay)); 99361fbeba1SAlan Stern 99461fbeba1SAlan Stern /* Suppress autosuspend until init is done */ 9958e4ceb38SAlan Stern usb_autopm_get_interface_no_resume( 9968e4ceb38SAlan Stern to_usb_interface(hub->intfdev)); 9978520f380SAlan Stern return; /* Continues at init2: below */ 998653a39d1SSarah Sharp } else if (type == HUB_RESET_RESUME) { 999653a39d1SSarah Sharp /* The internal host controller state for the hub device 1000653a39d1SSarah Sharp * may be gone after a host power loss on system resume. 1001653a39d1SSarah Sharp * Update the device's info so the HW knows it's a hub. 1002653a39d1SSarah Sharp */ 1003653a39d1SSarah Sharp hcd = bus_to_hcd(hdev->bus); 1004653a39d1SSarah Sharp if (hcd->driver->update_hub_device) { 1005653a39d1SSarah Sharp ret = hcd->driver->update_hub_device(hcd, hdev, 1006653a39d1SSarah Sharp &hub->tt, GFP_NOIO); 1007653a39d1SSarah Sharp if (ret < 0) { 1008653a39d1SSarah Sharp dev_err(hub->intfdev, "Host not " 1009653a39d1SSarah Sharp "accepting hub info " 1010653a39d1SSarah Sharp "update.\n"); 1011653a39d1SSarah Sharp dev_err(hub->intfdev, "LS/FS devices " 1012653a39d1SSarah Sharp "and hubs may not work " 1013653a39d1SSarah Sharp "under this hub\n."); 1014653a39d1SSarah Sharp } 1015653a39d1SSarah Sharp } 1016653a39d1SSarah Sharp hub_power_on(hub, true); 10178520f380SAlan Stern } else { 10188520f380SAlan Stern hub_power_on(hub, true); 10198520f380SAlan Stern } 10208520f380SAlan Stern } 10218520f380SAlan Stern init2: 1022f2835219SAlan Stern 10236ee0b270SAlan Stern /* Check each port and set hub->change_bits to let khubd know 10246ee0b270SAlan Stern * which ports need attention. 10255e6effaeSAlan Stern */ 10265e6effaeSAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 1027fa286188SGreg Kroah-Hartman struct usb_device *udev = hdev->children[port1-1]; 10285e6effaeSAlan Stern u16 portstatus, portchange; 10295e6effaeSAlan Stern 10306ee0b270SAlan Stern portstatus = portchange = 0; 10316ee0b270SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 10326ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 10336ee0b270SAlan Stern dev_dbg(hub->intfdev, 10346ee0b270SAlan Stern "port %d: status %04x change %04x\n", 10356ee0b270SAlan Stern port1, portstatus, portchange); 10365e6effaeSAlan Stern 10376ee0b270SAlan Stern /* After anything other than HUB_RESUME (i.e., initialization 10386ee0b270SAlan Stern * or any sort of reset), every port should be disabled. 10396ee0b270SAlan Stern * Unconnected ports should likewise be disabled (paranoia), 10406ee0b270SAlan Stern * and so should ports for which we have no usb_device. 10415e6effaeSAlan Stern */ 10426ee0b270SAlan Stern if ((portstatus & USB_PORT_STAT_ENABLE) && ( 10436ee0b270SAlan Stern type != HUB_RESUME || 10446ee0b270SAlan Stern !(portstatus & USB_PORT_STAT_CONNECTION) || 10456ee0b270SAlan Stern !udev || 10466ee0b270SAlan Stern udev->state == USB_STATE_NOTATTACHED)) { 10479f0a6cd3SAndiry Xu /* 10489f0a6cd3SAndiry Xu * USB3 protocol ports will automatically transition 10499f0a6cd3SAndiry Xu * to Enabled state when detect an USB3.0 device attach. 10509f0a6cd3SAndiry Xu * Do not disable USB3 protocol ports. 10519f0a6cd3SAndiry Xu */ 1052131dec34SSarah Sharp if (!hub_is_superspeed(hdev)) { 10539f0a6cd3SAndiry Xu clear_port_feature(hdev, port1, 10549f0a6cd3SAndiry Xu USB_PORT_FEAT_ENABLE); 10556ee0b270SAlan Stern portstatus &= ~USB_PORT_STAT_ENABLE; 105685f0ff46SSarah Sharp } else { 105785f0ff46SSarah Sharp /* Pretend that power was lost for USB3 devs */ 105885f0ff46SSarah Sharp portstatus &= ~USB_PORT_STAT_ENABLE; 10596ee0b270SAlan Stern } 10609f0a6cd3SAndiry Xu } 10616ee0b270SAlan Stern 1062948fea37SAlan Stern /* Clear status-change flags; we'll debounce later */ 1063948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) { 1064948fea37SAlan Stern need_debounce_delay = true; 1065948fea37SAlan Stern clear_port_feature(hub->hdev, port1, 1066948fea37SAlan Stern USB_PORT_FEAT_C_CONNECTION); 1067948fea37SAlan Stern } 1068948fea37SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) { 1069948fea37SAlan Stern need_debounce_delay = true; 1070948fea37SAlan Stern clear_port_feature(hub->hdev, port1, 1071948fea37SAlan Stern USB_PORT_FEAT_C_ENABLE); 1072948fea37SAlan Stern } 107379c3dd81SDon Zickus if ((portchange & USB_PORT_STAT_C_BH_RESET) && 107479c3dd81SDon Zickus hub_is_superspeed(hub->hdev)) { 107579c3dd81SDon Zickus need_debounce_delay = true; 107679c3dd81SDon Zickus clear_port_feature(hub->hdev, port1, 107779c3dd81SDon Zickus USB_PORT_FEAT_C_BH_PORT_RESET); 107879c3dd81SDon Zickus } 1079253e0572SAlan Stern /* We can forget about a "removed" device when there's a 1080253e0572SAlan Stern * physical disconnect or the connect status changes. 1081253e0572SAlan Stern */ 1082253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 1083253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION)) 1084253e0572SAlan Stern clear_bit(port1, hub->removed_bits); 1085253e0572SAlan Stern 10866ee0b270SAlan Stern if (!udev || udev->state == USB_STATE_NOTATTACHED) { 10876ee0b270SAlan Stern /* Tell khubd to disconnect the device or 10886ee0b270SAlan Stern * check for a new connection 10896ee0b270SAlan Stern */ 10906ee0b270SAlan Stern if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 10916ee0b270SAlan Stern set_bit(port1, hub->change_bits); 10926ee0b270SAlan Stern 10936ee0b270SAlan Stern } else if (portstatus & USB_PORT_STAT_ENABLE) { 109472937e1eSSarah Sharp bool port_resumed = (portstatus & 109572937e1eSSarah Sharp USB_PORT_STAT_LINK_STATE) == 109672937e1eSSarah Sharp USB_SS_PORT_LS_U0; 10976ee0b270SAlan Stern /* The power session apparently survived the resume. 10986ee0b270SAlan Stern * If there was an overcurrent or suspend change 10996ee0b270SAlan Stern * (i.e., remote wakeup request), have khubd 110072937e1eSSarah Sharp * take care of it. Look at the port link state 110172937e1eSSarah Sharp * for USB 3.0 hubs, since they don't have a suspend 110272937e1eSSarah Sharp * change bit, and they don't set the port link change 110372937e1eSSarah Sharp * bit on device-initiated resume. 11046ee0b270SAlan Stern */ 110572937e1eSSarah Sharp if (portchange || (hub_is_superspeed(hub->hdev) && 110672937e1eSSarah Sharp port_resumed)) 11076ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11086ee0b270SAlan Stern 11096ee0b270SAlan Stern } else if (udev->persist_enabled) { 11106ee0b270SAlan Stern #ifdef CONFIG_PM 11115e6effaeSAlan Stern udev->reset_resume = 1; 11126ee0b270SAlan Stern #endif 11138808f00cSAlan Stern set_bit(port1, hub->change_bits); 11148808f00cSAlan Stern 11156ee0b270SAlan Stern } else { 11166ee0b270SAlan Stern /* The power session is gone; tell khubd */ 11176ee0b270SAlan Stern usb_set_device_state(udev, USB_STATE_NOTATTACHED); 11186ee0b270SAlan Stern set_bit(port1, hub->change_bits); 11195e6effaeSAlan Stern } 11205e6effaeSAlan Stern } 11215e6effaeSAlan Stern 1122948fea37SAlan Stern /* If no port-status-change flags were set, we don't need any 1123948fea37SAlan Stern * debouncing. If flags were set we can try to debounce the 1124948fea37SAlan Stern * ports all at once right now, instead of letting khubd do them 1125948fea37SAlan Stern * one at a time later on. 1126948fea37SAlan Stern * 1127948fea37SAlan Stern * If any port-status changes do occur during this delay, khubd 1128948fea37SAlan Stern * will see them later and handle them normally. 1129948fea37SAlan Stern */ 11308520f380SAlan Stern if (need_debounce_delay) { 11318520f380SAlan Stern delay = HUB_DEBOUNCE_STABLE; 1132f2835219SAlan Stern 11338520f380SAlan Stern /* Don't do a long sleep inside a workqueue routine */ 11348520f380SAlan Stern if (type == HUB_INIT2) { 11358520f380SAlan Stern PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); 11368520f380SAlan Stern schedule_delayed_work(&hub->init_work, 11378520f380SAlan Stern msecs_to_jiffies(delay)); 11388520f380SAlan Stern return; /* Continues at init3: below */ 11398520f380SAlan Stern } else { 11408520f380SAlan Stern msleep(delay); 11418520f380SAlan Stern } 11428520f380SAlan Stern } 11438520f380SAlan Stern init3: 1144f2835219SAlan Stern hub->quiescing = 0; 1145f2835219SAlan Stern 1146f2835219SAlan Stern status = usb_submit_urb(hub->urb, GFP_NOIO); 1147f2835219SAlan Stern if (status < 0) 1148f2835219SAlan Stern dev_err(hub->intfdev, "activate --> %d\n", status); 1149f2835219SAlan Stern if (hub->has_indicators && blinkenlights) 1150f2835219SAlan Stern schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 1151f2835219SAlan Stern 1152f2835219SAlan Stern /* Scan all ports that need attention */ 1153f2835219SAlan Stern kick_khubd(hub); 11548e4ceb38SAlan Stern 11558e4ceb38SAlan Stern /* Allow autosuspend if it was suppressed */ 11568e4ceb38SAlan Stern if (type <= HUB_INIT3) 11578e4ceb38SAlan Stern usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); 11585e6effaeSAlan Stern } 11595e6effaeSAlan Stern 11608520f380SAlan Stern /* Implement the continuations for the delays above */ 11618520f380SAlan Stern static void hub_init_func2(struct work_struct *ws) 11628520f380SAlan Stern { 11638520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 11648520f380SAlan Stern 11658520f380SAlan Stern hub_activate(hub, HUB_INIT2); 11668520f380SAlan Stern } 11678520f380SAlan Stern 11688520f380SAlan Stern static void hub_init_func3(struct work_struct *ws) 11698520f380SAlan Stern { 11708520f380SAlan Stern struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 11718520f380SAlan Stern 11728520f380SAlan Stern hub_activate(hub, HUB_INIT3); 11738520f380SAlan Stern } 11748520f380SAlan Stern 11754330354fSAlan Stern enum hub_quiescing_type { 11764330354fSAlan Stern HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND 11774330354fSAlan Stern }; 11784330354fSAlan Stern 11794330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) 11804330354fSAlan Stern { 11814330354fSAlan Stern struct usb_device *hdev = hub->hdev; 11824330354fSAlan Stern int i; 11834330354fSAlan Stern 11848520f380SAlan Stern cancel_delayed_work_sync(&hub->init_work); 11858520f380SAlan Stern 11864330354fSAlan Stern /* khubd and related activity won't re-trigger */ 11874330354fSAlan Stern hub->quiescing = 1; 11884330354fSAlan Stern 11894330354fSAlan Stern if (type != HUB_SUSPEND) { 11904330354fSAlan Stern /* Disconnect all the children */ 11914330354fSAlan Stern for (i = 0; i < hdev->maxchild; ++i) { 1192fa286188SGreg Kroah-Hartman if (hdev->children[i]) 1193fa286188SGreg Kroah-Hartman usb_disconnect(&hdev->children[i]); 11944330354fSAlan Stern } 11954330354fSAlan Stern } 11964330354fSAlan Stern 11974330354fSAlan Stern /* Stop khubd and related activity */ 11984330354fSAlan Stern usb_kill_urb(hub->urb); 11994330354fSAlan Stern if (hub->has_indicators) 12004330354fSAlan Stern cancel_delayed_work_sync(&hub->leds); 12014330354fSAlan Stern if (hub->tt.hub) 1202cb88a1b8SAlan Stern cancel_work_sync(&hub->tt.clear_work); 12034330354fSAlan Stern } 12044330354fSAlan Stern 12053eb14915SAlan Stern /* caller has locked the hub device */ 12063eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf) 12073eb14915SAlan Stern { 12083eb14915SAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 12093eb14915SAlan Stern 12104330354fSAlan Stern hub_quiesce(hub, HUB_PRE_RESET); 1211f07600cfSAlan Stern return 0; 12127d069b7dSAlan Stern } 12137d069b7dSAlan Stern 12147d069b7dSAlan Stern /* caller has locked the hub device */ 1215f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf) 12167d069b7dSAlan Stern { 12177de18d8bSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 12187de18d8bSAlan Stern 1219f2835219SAlan Stern hub_activate(hub, HUB_POST_RESET); 1220f07600cfSAlan Stern return 0; 12217d069b7dSAlan Stern } 12227d069b7dSAlan Stern 12231da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub, 12241da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint) 12251da177e4SLinus Torvalds { 1226b356b7c7SSarah Sharp struct usb_hcd *hcd; 12271da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 12281da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 12291da177e4SLinus Torvalds u16 hubstatus, hubchange; 123074ad9bd2SGreg Kroah-Hartman u16 wHubCharacteristics; 12311da177e4SLinus Torvalds unsigned int pipe; 12321da177e4SLinus Torvalds int maxp, ret; 12337cbe5dcaSAlan Stern char *message = "out of memory"; 12341da177e4SLinus Torvalds 1235d697cddaSAlan Stern hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); 12361da177e4SLinus Torvalds if (!hub->buffer) { 12371da177e4SLinus Torvalds ret = -ENOMEM; 12381da177e4SLinus Torvalds goto fail; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 12421da177e4SLinus Torvalds if (!hub->status) { 12431da177e4SLinus Torvalds ret = -ENOMEM; 12441da177e4SLinus Torvalds goto fail; 12451da177e4SLinus Torvalds } 1246db90e7a1SAlan Stern mutex_init(&hub->status_mutex); 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); 12491da177e4SLinus Torvalds if (!hub->descriptor) { 12501da177e4SLinus Torvalds ret = -ENOMEM; 12511da177e4SLinus Torvalds goto fail; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds /* Request the entire hub descriptor. 12551da177e4SLinus Torvalds * hub->descriptor can handle USB_MAXCHILDREN ports, 12561da177e4SLinus Torvalds * but the hub can/will return fewer bytes here. 12571da177e4SLinus Torvalds */ 1258dbe79bbeSJohn Youn ret = get_hub_descriptor(hdev, hub->descriptor); 12591da177e4SLinus Torvalds if (ret < 0) { 12601da177e4SLinus Torvalds message = "can't read hub descriptor"; 12611da177e4SLinus Torvalds goto fail; 12621da177e4SLinus Torvalds } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { 12631da177e4SLinus Torvalds message = "hub has too many ports!"; 12641da177e4SLinus Torvalds ret = -ENODEV; 12651da177e4SLinus Torvalds goto fail; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds hdev->maxchild = hub->descriptor->bNbrPorts; 12691da177e4SLinus Torvalds dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 12701da177e4SLinus Torvalds (hdev->maxchild == 1) ? "" : "s"); 12711da177e4SLinus Torvalds 1272fa286188SGreg Kroah-Hartman hdev->children = kzalloc(hdev->maxchild * 1273fa286188SGreg Kroah-Hartman sizeof(struct usb_device *), GFP_KERNEL); 1274336c5c31SLan Tianyu hub->port_owners = kzalloc(hdev->maxchild * sizeof(struct dev_state *), 1275336c5c31SLan Tianyu GFP_KERNEL); 1276304f0b24SGreg Kroah-Hartman if (!hdev->children || !hub->port_owners) { 12777cbe5dcaSAlan Stern ret = -ENOMEM; 12787cbe5dcaSAlan Stern goto fail; 12797cbe5dcaSAlan Stern } 12807cbe5dcaSAlan Stern 128174ad9bd2SGreg Kroah-Hartman wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 12821da177e4SLinus Torvalds 1283dbe79bbeSJohn Youn /* FIXME for USB 3.0, skip for now */ 1284dbe79bbeSJohn Youn if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && 1285dbe79bbeSJohn Youn !(hub_is_superspeed(hdev))) { 12861da177e4SLinus Torvalds int i; 12871da177e4SLinus Torvalds char portstr [USB_MAXCHILDREN + 1]; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds for (i = 0; i < hdev->maxchild; i++) 1290dbe79bbeSJohn Youn portstr[i] = hub->descriptor->u.hs.DeviceRemovable 12911da177e4SLinus Torvalds [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 12921da177e4SLinus Torvalds ? 'F' : 'R'; 12931da177e4SLinus Torvalds portstr[hdev->maxchild] = 0; 12941da177e4SLinus Torvalds dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 12951da177e4SLinus Torvalds } else 12961da177e4SLinus Torvalds dev_dbg(hub_dev, "standalone hub\n"); 12971da177e4SLinus Torvalds 129874ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_LPSM) { 12997bf01185SAman Deep case HUB_CHAR_COMMON_LPSM: 13001da177e4SLinus Torvalds dev_dbg(hub_dev, "ganged power switching\n"); 13011da177e4SLinus Torvalds break; 13027bf01185SAman Deep case HUB_CHAR_INDV_PORT_LPSM: 13031da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port power switching\n"); 13041da177e4SLinus Torvalds break; 13057bf01185SAman Deep case HUB_CHAR_NO_LPSM: 13067bf01185SAman Deep case HUB_CHAR_LPSM: 13071da177e4SLinus Torvalds dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 13081da177e4SLinus Torvalds break; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 131174ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_OCPM) { 13127bf01185SAman Deep case HUB_CHAR_COMMON_OCPM: 13131da177e4SLinus Torvalds dev_dbg(hub_dev, "global over-current protection\n"); 13141da177e4SLinus Torvalds break; 13157bf01185SAman Deep case HUB_CHAR_INDV_PORT_OCPM: 13161da177e4SLinus Torvalds dev_dbg(hub_dev, "individual port over-current protection\n"); 13171da177e4SLinus Torvalds break; 13187bf01185SAman Deep case HUB_CHAR_NO_OCPM: 13197bf01185SAman Deep case HUB_CHAR_OCPM: 13201da177e4SLinus Torvalds dev_dbg(hub_dev, "no over-current protection\n"); 13211da177e4SLinus Torvalds break; 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds spin_lock_init (&hub->tt.lock); 13251da177e4SLinus Torvalds INIT_LIST_HEAD (&hub->tt.clear_list); 1326cb88a1b8SAlan Stern INIT_WORK(&hub->tt.clear_work, hub_tt_work); 13271da177e4SLinus Torvalds switch (hdev->descriptor.bDeviceProtocol) { 13287bf01185SAman Deep case USB_HUB_PR_FS: 13291da177e4SLinus Torvalds break; 13307bf01185SAman Deep case USB_HUB_PR_HS_SINGLE_TT: 13311da177e4SLinus Torvalds dev_dbg(hub_dev, "Single TT\n"); 13321da177e4SLinus Torvalds hub->tt.hub = hdev; 13331da177e4SLinus Torvalds break; 13347bf01185SAman Deep case USB_HUB_PR_HS_MULTI_TT: 13351da177e4SLinus Torvalds ret = usb_set_interface(hdev, 0, 1); 13361da177e4SLinus Torvalds if (ret == 0) { 13371da177e4SLinus Torvalds dev_dbg(hub_dev, "TT per port\n"); 13381da177e4SLinus Torvalds hub->tt.multi = 1; 13391da177e4SLinus Torvalds } else 13401da177e4SLinus Torvalds dev_err(hub_dev, "Using single TT (err %d)\n", 13411da177e4SLinus Torvalds ret); 13421da177e4SLinus Torvalds hub->tt.hub = hdev; 13431da177e4SLinus Torvalds break; 13447bf01185SAman Deep case USB_HUB_PR_SS: 1345d2e9b4d6SSarah Sharp /* USB 3.0 hubs don't have a TT */ 1346d2e9b4d6SSarah Sharp break; 13471da177e4SLinus Torvalds default: 13481da177e4SLinus Torvalds dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 13491da177e4SLinus Torvalds hdev->descriptor.bDeviceProtocol); 13501da177e4SLinus Torvalds break; 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 1353e09711aeSdavid-b@pacbell.net /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 135474ad9bd2SGreg Kroah-Hartman switch (wHubCharacteristics & HUB_CHAR_TTTT) { 1355e09711aeSdavid-b@pacbell.net case HUB_TTTT_8_BITS: 1356e09711aeSdavid-b@pacbell.net if (hdev->descriptor.bDeviceProtocol != 0) { 1357e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666; 1358e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1359e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1360e09711aeSdavid-b@pacbell.net 8, hub->tt.think_time); 1361e09711aeSdavid-b@pacbell.net } 13621da177e4SLinus Torvalds break; 1363e09711aeSdavid-b@pacbell.net case HUB_TTTT_16_BITS: 1364e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 2; 1365e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1366e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1367e09711aeSdavid-b@pacbell.net 16, hub->tt.think_time); 13681da177e4SLinus Torvalds break; 1369e09711aeSdavid-b@pacbell.net case HUB_TTTT_24_BITS: 1370e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 3; 1371e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1372e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1373e09711aeSdavid-b@pacbell.net 24, hub->tt.think_time); 13741da177e4SLinus Torvalds break; 1375e09711aeSdavid-b@pacbell.net case HUB_TTTT_32_BITS: 1376e09711aeSdavid-b@pacbell.net hub->tt.think_time = 666 * 4; 1377e09711aeSdavid-b@pacbell.net dev_dbg(hub_dev, "TT requires at most %d " 1378e09711aeSdavid-b@pacbell.net "FS bit times (%d ns)\n", 1379e09711aeSdavid-b@pacbell.net 32, hub->tt.think_time); 13801da177e4SLinus Torvalds break; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* probe() zeroes hub->indicator[] */ 138474ad9bd2SGreg Kroah-Hartman if (wHubCharacteristics & HUB_CHAR_PORTIND) { 13851da177e4SLinus Torvalds hub->has_indicators = 1; 13861da177e4SLinus Torvalds dev_dbg(hub_dev, "Port indicators are supported\n"); 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds dev_dbg(hub_dev, "power on to power good time: %dms\n", 13901da177e4SLinus Torvalds hub->descriptor->bPwrOn2PwrGood * 2); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds /* power budgeting mostly matters with bus-powered hubs, 13931da177e4SLinus Torvalds * and battery-powered root hubs (may provide just 8 mA). 13941da177e4SLinus Torvalds */ 13951da177e4SLinus Torvalds ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 139655c52718SAlan Stern if (ret < 2) { 13971da177e4SLinus Torvalds message = "can't get hub status"; 13981da177e4SLinus Torvalds goto fail; 13991da177e4SLinus Torvalds } 14007d35b929SAlan Stern le16_to_cpus(&hubstatus); 14017d35b929SAlan Stern if (hdev == hdev->bus->root_hub) { 140255c52718SAlan Stern if (hdev->bus_mA == 0 || hdev->bus_mA >= 500) 140355c52718SAlan Stern hub->mA_per_port = 500; 140455c52718SAlan Stern else { 140555c52718SAlan Stern hub->mA_per_port = hdev->bus_mA; 140655c52718SAlan Stern hub->limited_power = 1; 140755c52718SAlan Stern } 14087d35b929SAlan Stern } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 14091da177e4SLinus Torvalds dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 14101da177e4SLinus Torvalds hub->descriptor->bHubContrCurrent); 141155c52718SAlan Stern hub->limited_power = 1; 141255c52718SAlan Stern if (hdev->maxchild > 0) { 141355c52718SAlan Stern int remaining = hdev->bus_mA - 141455c52718SAlan Stern hub->descriptor->bHubContrCurrent; 14151da177e4SLinus Torvalds 141655c52718SAlan Stern if (remaining < hdev->maxchild * 100) 141755c52718SAlan Stern dev_warn(hub_dev, 141855c52718SAlan Stern "insufficient power available " 141955c52718SAlan Stern "to use all downstream ports\n"); 142055c52718SAlan Stern hub->mA_per_port = 100; /* 7.2.1.1 */ 142155c52718SAlan Stern } 142255c52718SAlan Stern } else { /* Self-powered external hub */ 142355c52718SAlan Stern /* FIXME: What about battery-powered external hubs that 142455c52718SAlan Stern * provide less current per port? */ 142555c52718SAlan Stern hub->mA_per_port = 500; 142655c52718SAlan Stern } 142755c52718SAlan Stern if (hub->mA_per_port < 500) 142855c52718SAlan Stern dev_dbg(hub_dev, "%umA bus power budget for each child\n", 142955c52718SAlan Stern hub->mA_per_port); 14301da177e4SLinus Torvalds 1431b356b7c7SSarah Sharp /* Update the HCD's internal representation of this hub before khubd 1432b356b7c7SSarah Sharp * starts getting port status changes for devices under the hub. 1433b356b7c7SSarah Sharp */ 1434b356b7c7SSarah Sharp hcd = bus_to_hcd(hdev->bus); 1435b356b7c7SSarah Sharp if (hcd->driver->update_hub_device) { 1436b356b7c7SSarah Sharp ret = hcd->driver->update_hub_device(hcd, hdev, 1437b356b7c7SSarah Sharp &hub->tt, GFP_KERNEL); 1438b356b7c7SSarah Sharp if (ret < 0) { 1439b356b7c7SSarah Sharp message = "can't update HCD hub info"; 1440b356b7c7SSarah Sharp goto fail; 1441b356b7c7SSarah Sharp } 1442b356b7c7SSarah Sharp } 1443b356b7c7SSarah Sharp 14441da177e4SLinus Torvalds ret = hub_hub_status(hub, &hubstatus, &hubchange); 14451da177e4SLinus Torvalds if (ret < 0) { 14461da177e4SLinus Torvalds message = "can't get hub status"; 14471da177e4SLinus Torvalds goto fail; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds /* local power status reports aren't always correct */ 14511da177e4SLinus Torvalds if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 14521da177e4SLinus Torvalds dev_dbg(hub_dev, "local power source is %s\n", 14531da177e4SLinus Torvalds (hubstatus & HUB_STATUS_LOCAL_POWER) 14541da177e4SLinus Torvalds ? "lost (inactive)" : "good"); 14551da177e4SLinus Torvalds 145674ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 14571da177e4SLinus Torvalds dev_dbg(hub_dev, "%sover-current condition exists\n", 14581da177e4SLinus Torvalds (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 14591da177e4SLinus Torvalds 146088fafff9Sinaky@linux.intel.com /* set up the interrupt endpoint 146188fafff9Sinaky@linux.intel.com * We use the EP's maxpacket size instead of (PORTS+1+7)/8 146288fafff9Sinaky@linux.intel.com * bytes as USB2.0[11.12.3] says because some hubs are known 146388fafff9Sinaky@linux.intel.com * to send more data (and thus cause overflow). For root hubs, 146488fafff9Sinaky@linux.intel.com * maxpktsize is defined in hcd.c's fake endpoint descriptors 146588fafff9Sinaky@linux.intel.com * to be big enough for at least USB_MAXCHILDREN ports. */ 14661da177e4SLinus Torvalds pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 14671da177e4SLinus Torvalds maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds if (maxp > sizeof(*hub->buffer)) 14701da177e4SLinus Torvalds maxp = sizeof(*hub->buffer); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds hub->urb = usb_alloc_urb(0, GFP_KERNEL); 14731da177e4SLinus Torvalds if (!hub->urb) { 14741da177e4SLinus Torvalds ret = -ENOMEM; 14751da177e4SLinus Torvalds goto fail; 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 14791da177e4SLinus Torvalds hub, endpoint->bInterval); 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds /* maybe cycle the hub leds */ 14821da177e4SLinus Torvalds if (hub->has_indicators && blinkenlights) 14831da177e4SLinus Torvalds hub->indicator [0] = INDICATOR_CYCLE; 14841da177e4SLinus Torvalds 1485f2835219SAlan Stern hub_activate(hub, HUB_INIT); 14861da177e4SLinus Torvalds return 0; 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds fail: 14891da177e4SLinus Torvalds dev_err (hub_dev, "config failed, %s (err %d)\n", 14901da177e4SLinus Torvalds message, ret); 14911da177e4SLinus Torvalds /* hub_disconnect() frees urb and descriptor */ 14921da177e4SLinus Torvalds return ret; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 1495e8054854SAlan Stern static void hub_release(struct kref *kref) 1496e8054854SAlan Stern { 1497e8054854SAlan Stern struct usb_hub *hub = container_of(kref, struct usb_hub, kref); 1498e8054854SAlan Stern 1499e8054854SAlan Stern usb_put_intf(to_usb_interface(hub->intfdev)); 1500e8054854SAlan Stern kfree(hub); 1501e8054854SAlan Stern } 1502e8054854SAlan Stern 15031da177e4SLinus Torvalds static unsigned highspeed_hubs; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf) 15061da177e4SLinus Torvalds { 15071da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 1508fa286188SGreg Kroah-Hartman struct usb_device *hdev = interface_to_usbdev(intf); 1509e8054854SAlan Stern 1510e8054854SAlan Stern /* Take the hub off the event list and don't let it be added again */ 1511e8054854SAlan Stern spin_lock_irq(&hub_event_lock); 15128e4ceb38SAlan Stern if (!list_empty(&hub->event_list)) { 1513e8054854SAlan Stern list_del_init(&hub->event_list); 15148e4ceb38SAlan Stern usb_autopm_put_interface_no_suspend(intf); 15158e4ceb38SAlan Stern } 1516e8054854SAlan Stern hub->disconnected = 1; 1517e8054854SAlan Stern spin_unlock_irq(&hub_event_lock); 15181da177e4SLinus Torvalds 15197de18d8bSAlan Stern /* Disconnect all children and quiesce the hub */ 15207de18d8bSAlan Stern hub->error = 0; 15214330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 15227de18d8bSAlan Stern 15238b28c752SAlan Stern usb_set_intfdata (intf, NULL); 15247cbe5dcaSAlan Stern hub->hdev->maxchild = 0; 15251da177e4SLinus Torvalds 1526e8054854SAlan Stern if (hub->hdev->speed == USB_SPEED_HIGH) 15271da177e4SLinus Torvalds highspeed_hubs--; 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds usb_free_urb(hub->urb); 1530fa286188SGreg Kroah-Hartman kfree(hdev->children); 1531304f0b24SGreg Kroah-Hartman kfree(hub->port_owners); 15321da177e4SLinus Torvalds kfree(hub->descriptor); 15331da177e4SLinus Torvalds kfree(hub->status); 1534d697cddaSAlan Stern kfree(hub->buffer); 15351da177e4SLinus Torvalds 1536e8054854SAlan Stern kref_put(&hub->kref, hub_release); 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 15401da177e4SLinus Torvalds { 15411da177e4SLinus Torvalds struct usb_host_interface *desc; 15421da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint; 15431da177e4SLinus Torvalds struct usb_device *hdev; 15441da177e4SLinus Torvalds struct usb_hub *hub; 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds desc = intf->cur_altsetting; 15471da177e4SLinus Torvalds hdev = interface_to_usbdev(intf); 15481da177e4SLinus Torvalds 15492839f5bcSSarah Sharp /* Hubs have proper suspend/resume support. */ 1550088f7fecSAlan Stern usb_enable_autosuspend(hdev); 1551088f7fecSAlan Stern 155238f3ad5eSFelipe Balbi if (hdev->level == MAX_TOPO_LEVEL) { 1553b9cef6c3SWu Fengguang dev_err(&intf->dev, 1554b9cef6c3SWu Fengguang "Unsupported bus topology: hub nested too deep\n"); 155538f3ad5eSFelipe Balbi return -E2BIG; 155638f3ad5eSFelipe Balbi } 155738f3ad5eSFelipe Balbi 155889ccbdc9SDavid Brownell #ifdef CONFIG_USB_OTG_BLACKLIST_HUB 155989ccbdc9SDavid Brownell if (hdev->parent) { 156089ccbdc9SDavid Brownell dev_warn(&intf->dev, "ignoring external hub\n"); 156189ccbdc9SDavid Brownell return -ENODEV; 156289ccbdc9SDavid Brownell } 156389ccbdc9SDavid Brownell #endif 156489ccbdc9SDavid Brownell 15651da177e4SLinus Torvalds /* Some hubs have a subclass of 1, which AFAICT according to the */ 15661da177e4SLinus Torvalds /* specs is not defined, but it works */ 15671da177e4SLinus Torvalds if ((desc->desc.bInterfaceSubClass != 0) && 15681da177e4SLinus Torvalds (desc->desc.bInterfaceSubClass != 1)) { 15691da177e4SLinus Torvalds descriptor_error: 15701da177e4SLinus Torvalds dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); 15711da177e4SLinus Torvalds return -EIO; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 15751da177e4SLinus Torvalds if (desc->desc.bNumEndpoints != 1) 15761da177e4SLinus Torvalds goto descriptor_error; 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds endpoint = &desc->endpoint[0].desc; 15791da177e4SLinus Torvalds 1580fbf81c29SLuiz Fernando N. Capitulino /* If it's not an interrupt in endpoint, we'd better punt! */ 1581fbf81c29SLuiz Fernando N. Capitulino if (!usb_endpoint_is_int_in(endpoint)) 15821da177e4SLinus Torvalds goto descriptor_error; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* We found a hub */ 15851da177e4SLinus Torvalds dev_info (&intf->dev, "USB hub found\n"); 15861da177e4SLinus Torvalds 15870a1ef3b5SAlan Stern hub = kzalloc(sizeof(*hub), GFP_KERNEL); 15881da177e4SLinus Torvalds if (!hub) { 15891da177e4SLinus Torvalds dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); 15901da177e4SLinus Torvalds return -ENOMEM; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds 1593e8054854SAlan Stern kref_init(&hub->kref); 15941da177e4SLinus Torvalds INIT_LIST_HEAD(&hub->event_list); 15951da177e4SLinus Torvalds hub->intfdev = &intf->dev; 15961da177e4SLinus Torvalds hub->hdev = hdev; 1597c4028958SDavid Howells INIT_DELAYED_WORK(&hub->leds, led_work); 15988520f380SAlan Stern INIT_DELAYED_WORK(&hub->init_work, NULL); 1599e8054854SAlan Stern usb_get_intf(intf); 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds usb_set_intfdata (intf, hub); 160240f122f3SAlan Stern intf->needs_remote_wakeup = 1; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds if (hdev->speed == USB_SPEED_HIGH) 16051da177e4SLinus Torvalds highspeed_hubs++; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds if (hub_configure(hub, endpoint) >= 0) 16081da177e4SLinus Torvalds return 0; 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds hub_disconnect (intf); 16111da177e4SLinus Torvalds return -ENODEV; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds static int 16151da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 16161da177e4SLinus Torvalds { 16171da177e4SLinus Torvalds struct usb_device *hdev = interface_to_usbdev (intf); 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds /* assert ifno == 0 (part of hub spec) */ 16201da177e4SLinus Torvalds switch (code) { 16211da177e4SLinus Torvalds case USBDEVFS_HUB_PORTINFO: { 16221da177e4SLinus Torvalds struct usbdevfs_hub_portinfo *info = user_data; 16231da177e4SLinus Torvalds int i; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 16261da177e4SLinus Torvalds if (hdev->devnum <= 0) 16271da177e4SLinus Torvalds info->nports = 0; 16281da177e4SLinus Torvalds else { 16291da177e4SLinus Torvalds info->nports = hdev->maxchild; 16301da177e4SLinus Torvalds for (i = 0; i < info->nports; i++) { 1631fa286188SGreg Kroah-Hartman if (hdev->children[i] == NULL) 16321da177e4SLinus Torvalds info->port[i] = 0; 16331da177e4SLinus Torvalds else 16341da177e4SLinus Torvalds info->port[i] = 1635fa286188SGreg Kroah-Hartman hdev->children[i]->devnum; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds return info->nports + 1; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds default: 16441da177e4SLinus Torvalds return -ENOSYS; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16487cbe5dcaSAlan Stern /* 16497cbe5dcaSAlan Stern * Allow user programs to claim ports on a hub. When a device is attached 16507cbe5dcaSAlan Stern * to one of these "claimed" ports, the program will "own" the device. 16517cbe5dcaSAlan Stern */ 16527cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1, 1653336c5c31SLan Tianyu struct dev_state ***ppowner) 16547cbe5dcaSAlan Stern { 16557cbe5dcaSAlan Stern if (hdev->state == USB_STATE_NOTATTACHED) 16567cbe5dcaSAlan Stern return -ENODEV; 16577cbe5dcaSAlan Stern if (port1 == 0 || port1 > hdev->maxchild) 16587cbe5dcaSAlan Stern return -EINVAL; 16597cbe5dcaSAlan Stern 16607cbe5dcaSAlan Stern /* This assumes that devices not managed by the hub driver 16617cbe5dcaSAlan Stern * will always have maxchild equal to 0. 16627cbe5dcaSAlan Stern */ 1663304f0b24SGreg Kroah-Hartman *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]); 16647cbe5dcaSAlan Stern return 0; 16657cbe5dcaSAlan Stern } 16667cbe5dcaSAlan Stern 16677cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */ 1668336c5c31SLan Tianyu int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 1669336c5c31SLan Tianyu struct dev_state *owner) 16707cbe5dcaSAlan Stern { 16717cbe5dcaSAlan Stern int rc; 1672336c5c31SLan Tianyu struct dev_state **powner; 16737cbe5dcaSAlan Stern 16747cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner); 16757cbe5dcaSAlan Stern if (rc) 16767cbe5dcaSAlan Stern return rc; 16777cbe5dcaSAlan Stern if (*powner) 16787cbe5dcaSAlan Stern return -EBUSY; 16797cbe5dcaSAlan Stern *powner = owner; 16807cbe5dcaSAlan Stern return rc; 16817cbe5dcaSAlan Stern } 16827cbe5dcaSAlan Stern 1683336c5c31SLan Tianyu int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 1684336c5c31SLan Tianyu struct dev_state *owner) 16857cbe5dcaSAlan Stern { 16867cbe5dcaSAlan Stern int rc; 1687336c5c31SLan Tianyu struct dev_state **powner; 16887cbe5dcaSAlan Stern 16897cbe5dcaSAlan Stern rc = find_port_owner(hdev, port1, &powner); 16907cbe5dcaSAlan Stern if (rc) 16917cbe5dcaSAlan Stern return rc; 16927cbe5dcaSAlan Stern if (*powner != owner) 16937cbe5dcaSAlan Stern return -ENOENT; 16947cbe5dcaSAlan Stern *powner = NULL; 16957cbe5dcaSAlan Stern return rc; 16967cbe5dcaSAlan Stern } 16977cbe5dcaSAlan Stern 1698336c5c31SLan Tianyu void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner) 16997cbe5dcaSAlan Stern { 17007cbe5dcaSAlan Stern int n; 1701336c5c31SLan Tianyu struct dev_state **powner; 17027cbe5dcaSAlan Stern 1703304f0b24SGreg Kroah-Hartman n = find_port_owner(hdev, 1, &powner); 1704304f0b24SGreg Kroah-Hartman if (n == 0) { 1705304f0b24SGreg Kroah-Hartman for (; n < hdev->maxchild; (++n, ++powner)) { 1706304f0b24SGreg Kroah-Hartman if (*powner == owner) 1707304f0b24SGreg Kroah-Hartman *powner = NULL; 17087cbe5dcaSAlan Stern } 1709304f0b24SGreg Kroah-Hartman } 17107cbe5dcaSAlan Stern } 17117cbe5dcaSAlan Stern 17127cbe5dcaSAlan Stern /* The caller must hold udev's lock */ 17137cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev) 17147cbe5dcaSAlan Stern { 17157cbe5dcaSAlan Stern struct usb_hub *hub; 17167cbe5dcaSAlan Stern 17177cbe5dcaSAlan Stern if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) 17187cbe5dcaSAlan Stern return false; 17197cbe5dcaSAlan Stern hub = hdev_to_hub(udev->parent); 1720304f0b24SGreg Kroah-Hartman return !!hub->port_owners[udev->portnum - 1]; 17217cbe5dcaSAlan Stern } 17227cbe5dcaSAlan Stern 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev) 17251da177e4SLinus Torvalds { 17261da177e4SLinus Torvalds int i; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds for (i = 0; i < udev->maxchild; ++i) { 1729fa286188SGreg Kroah-Hartman if (udev->children[i]) 1730fa286188SGreg Kroah-Hartman recursively_mark_NOTATTACHED(udev->children[i]); 17311da177e4SLinus Torvalds } 17329bbdf1e0SAlan Stern if (udev->state == USB_STATE_SUSPENDED) 173315123006SSarah Sharp udev->active_duration -= jiffies; 17341da177e4SLinus Torvalds udev->state = USB_STATE_NOTATTACHED; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds /** 17381da177e4SLinus Torvalds * usb_set_device_state - change a device's current state (usbcore, hcds) 17391da177e4SLinus Torvalds * @udev: pointer to device whose state should be changed 17401da177e4SLinus Torvalds * @new_state: new state value to be stored 17411da177e4SLinus Torvalds * 17421da177e4SLinus Torvalds * udev->state is _not_ fully protected by the device lock. Although 17431da177e4SLinus Torvalds * most transitions are made only while holding the lock, the state can 17441da177e4SLinus Torvalds * can change to USB_STATE_NOTATTACHED at almost any time. This 17451da177e4SLinus Torvalds * is so that devices can be marked as disconnected as soon as possible, 17461da177e4SLinus Torvalds * without having to wait for any semaphores to be released. As a result, 17471da177e4SLinus Torvalds * all changes to any device's state must be protected by the 17481da177e4SLinus Torvalds * device_state_lock spinlock. 17491da177e4SLinus Torvalds * 17501da177e4SLinus Torvalds * Once a device has been added to the device tree, all changes to its state 17511da177e4SLinus Torvalds * should be made using this routine. The state should _not_ be set directly. 17521da177e4SLinus Torvalds * 17531da177e4SLinus Torvalds * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 17541da177e4SLinus Torvalds * Otherwise udev->state is set to new_state, and if new_state is 17551da177e4SLinus Torvalds * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 17561da177e4SLinus Torvalds * to USB_STATE_NOTATTACHED. 17571da177e4SLinus Torvalds */ 17581da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev, 17591da177e4SLinus Torvalds enum usb_device_state new_state) 17601da177e4SLinus Torvalds { 17611da177e4SLinus Torvalds unsigned long flags; 17624681b171SRafael J. Wysocki int wakeup = -1; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds spin_lock_irqsave(&device_state_lock, flags); 17651da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED) 17661da177e4SLinus Torvalds ; /* do nothing */ 1767b94dc6b5SDavid Brownell else if (new_state != USB_STATE_NOTATTACHED) { 1768fb669cc0SDavid Brownell 1769fb669cc0SDavid Brownell /* root hub wakeup capabilities are managed out-of-band 1770fb669cc0SDavid Brownell * and may involve silicon errata ... ignore them here. 1771fb669cc0SDavid Brownell */ 1772fb669cc0SDavid Brownell if (udev->parent) { 1773645daaabSAlan Stern if (udev->state == USB_STATE_SUSPENDED 1774645daaabSAlan Stern || new_state == USB_STATE_SUSPENDED) 1775645daaabSAlan Stern ; /* No change to wakeup settings */ 1776645daaabSAlan Stern else if (new_state == USB_STATE_CONFIGURED) 17774681b171SRafael J. Wysocki wakeup = udev->actconfig->desc.bmAttributes 17784681b171SRafael J. Wysocki & USB_CONFIG_ATT_WAKEUP; 1779645daaabSAlan Stern else 17804681b171SRafael J. Wysocki wakeup = 0; 1781fb669cc0SDavid Brownell } 178215123006SSarah Sharp if (udev->state == USB_STATE_SUSPENDED && 178315123006SSarah Sharp new_state != USB_STATE_SUSPENDED) 178415123006SSarah Sharp udev->active_duration -= jiffies; 178515123006SSarah Sharp else if (new_state == USB_STATE_SUSPENDED && 178615123006SSarah Sharp udev->state != USB_STATE_SUSPENDED) 178715123006SSarah Sharp udev->active_duration += jiffies; 1788645daaabSAlan Stern udev->state = new_state; 1789b94dc6b5SDavid Brownell } else 17901da177e4SLinus Torvalds recursively_mark_NOTATTACHED(udev); 17911da177e4SLinus Torvalds spin_unlock_irqrestore(&device_state_lock, flags); 17924681b171SRafael J. Wysocki if (wakeup >= 0) 17934681b171SRafael J. Wysocki device_set_wakeup_capable(&udev->dev, wakeup); 17941da177e4SLinus Torvalds } 17956da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state); 17961da177e4SLinus Torvalds 17978af548dcSInaky Perez-Gonzalez /* 17983b29b68bSAlan Stern * Choose a device number. 17993b29b68bSAlan Stern * 18003b29b68bSAlan Stern * Device numbers are used as filenames in usbfs. On USB-1.1 and 18013b29b68bSAlan Stern * USB-2.0 buses they are also used as device addresses, however on 18023b29b68bSAlan Stern * USB-3.0 buses the address is assigned by the controller hardware 18033b29b68bSAlan Stern * and it usually is not the same as the device number. 18043b29b68bSAlan Stern * 18058af548dcSInaky Perez-Gonzalez * WUSB devices are simple: they have no hubs behind, so the mapping 18068af548dcSInaky Perez-Gonzalez * device <-> virtual port number becomes 1:1. Why? to simplify the 18078af548dcSInaky Perez-Gonzalez * life of the device connection logic in 18088af548dcSInaky Perez-Gonzalez * drivers/usb/wusbcore/devconnect.c. When we do the initial secret 18098af548dcSInaky Perez-Gonzalez * handshake we need to assign a temporary address in the unauthorized 18108af548dcSInaky Perez-Gonzalez * space. For simplicity we use the first virtual port number found to 18118af548dcSInaky Perez-Gonzalez * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] 18128af548dcSInaky Perez-Gonzalez * and that becomes it's address [X < 128] or its unauthorized address 18138af548dcSInaky Perez-Gonzalez * [X | 0x80]. 18148af548dcSInaky Perez-Gonzalez * 18158af548dcSInaky Perez-Gonzalez * We add 1 as an offset to the one-based USB-stack port number 18168af548dcSInaky Perez-Gonzalez * (zero-based wusb virtual port index) for two reasons: (a) dev addr 18178af548dcSInaky Perez-Gonzalez * 0 is reserved by USB for default address; (b) Linux's USB stack 18188af548dcSInaky Perez-Gonzalez * uses always #1 for the root hub of the controller. So USB stack's 18198af548dcSInaky Perez-Gonzalez * port #1, which is wusb virtual-port #0 has address #2. 1820c6515272SSarah Sharp * 1821c6515272SSarah Sharp * Devices connected under xHCI are not as simple. The host controller 1822c6515272SSarah Sharp * supports virtualization, so the hardware assigns device addresses and 1823c6515272SSarah Sharp * the HCD must setup data structures before issuing a set address 1824c6515272SSarah Sharp * command to the hardware. 18258af548dcSInaky Perez-Gonzalez */ 18263b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev) 18271da177e4SLinus Torvalds { 18281da177e4SLinus Torvalds int devnum; 18291da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /* If khubd ever becomes multithreaded, this will need a lock */ 18328af548dcSInaky Perez-Gonzalez if (udev->wusb) { 18338af548dcSInaky Perez-Gonzalez devnum = udev->portnum + 1; 18348af548dcSInaky Perez-Gonzalez BUG_ON(test_bit(devnum, bus->devmap.devicemap)); 18358af548dcSInaky Perez-Gonzalez } else { 18368af548dcSInaky Perez-Gonzalez /* Try to allocate the next devnum beginning at 18378af548dcSInaky Perez-Gonzalez * bus->devnum_next. */ 18381da177e4SLinus Torvalds devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 18391da177e4SLinus Torvalds bus->devnum_next); 18401da177e4SLinus Torvalds if (devnum >= 128) 18418af548dcSInaky Perez-Gonzalez devnum = find_next_zero_bit(bus->devmap.devicemap, 18428af548dcSInaky Perez-Gonzalez 128, 1); 18431da177e4SLinus Torvalds bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); 18448af548dcSInaky Perez-Gonzalez } 18451da177e4SLinus Torvalds if (devnum < 128) { 18461da177e4SLinus Torvalds set_bit(devnum, bus->devmap.devicemap); 18471da177e4SLinus Torvalds udev->devnum = devnum; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 18513b29b68bSAlan Stern static void release_devnum(struct usb_device *udev) 18521da177e4SLinus Torvalds { 18531da177e4SLinus Torvalds if (udev->devnum > 0) { 18541da177e4SLinus Torvalds clear_bit(udev->devnum, udev->bus->devmap.devicemap); 18551da177e4SLinus Torvalds udev->devnum = -1; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 18593b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum) 18604953d141SDavid Vrabel { 18614953d141SDavid Vrabel /* The address for a WUSB device is managed by wusbcore. */ 18624953d141SDavid Vrabel if (!udev->wusb) 18634953d141SDavid Vrabel udev->devnum = devnum; 18644953d141SDavid Vrabel } 18654953d141SDavid Vrabel 1866f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev) 1867f7410cedSHerbert Xu { 1868f7410cedSHerbert Xu struct usb_hcd *hcd = bus_to_hcd(udev->bus); 1869f7410cedSHerbert Xu 1870f7410cedSHerbert Xu /* Root hubs aren't real devices, so don't free HCD resources */ 1871f7410cedSHerbert Xu if (hcd->driver->free_dev && udev->parent) 1872f7410cedSHerbert Xu hcd->driver->free_dev(hcd, udev); 1873f7410cedSHerbert Xu } 1874f7410cedSHerbert Xu 18751da177e4SLinus Torvalds /** 18761da177e4SLinus Torvalds * usb_disconnect - disconnect a device (usbcore-internal) 18771da177e4SLinus Torvalds * @pdev: pointer to device being disconnected 18781da177e4SLinus Torvalds * Context: !in_interrupt () 18791da177e4SLinus Torvalds * 18801da177e4SLinus Torvalds * Something got disconnected. Get rid of it and all of its children. 18811da177e4SLinus Torvalds * 18821da177e4SLinus Torvalds * If *pdev is a normal device then the parent hub must already be locked. 18831da177e4SLinus Torvalds * If *pdev is a root hub then this routine will acquire the 18841da177e4SLinus Torvalds * usb_bus_list_lock on behalf of the caller. 18851da177e4SLinus Torvalds * 18861da177e4SLinus Torvalds * Only hub drivers (including virtual root hub drivers for host 18871da177e4SLinus Torvalds * controllers) should ever call this. 18881da177e4SLinus Torvalds * 18891da177e4SLinus Torvalds * This call is synchronous, and may not be used in an interrupt context. 18901da177e4SLinus Torvalds */ 18911da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev) 18921da177e4SLinus Torvalds { 18931da177e4SLinus Torvalds struct usb_device *udev = *pdev; 18941da177e4SLinus Torvalds int i; 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds /* mark the device as inactive, so any further urb submissions for 18971da177e4SLinus Torvalds * this device (and any of its children) will fail immediately. 189825985edcSLucas De Marchi * this quiesces everything except pending urbs. 18991da177e4SLinus Torvalds */ 19001da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 19013b29b68bSAlan Stern dev_info(&udev->dev, "USB disconnect, device number %d\n", 19023b29b68bSAlan Stern udev->devnum); 19031da177e4SLinus Torvalds 19049ad3d6ccSAlan Stern usb_lock_device(udev); 19059ad3d6ccSAlan Stern 19061da177e4SLinus Torvalds /* Free up all the children before we remove this device */ 19078816230eSHuajun Li for (i = 0; i < udev->maxchild; i++) { 1908fa286188SGreg Kroah-Hartman if (udev->children[i]) 1909fa286188SGreg Kroah-Hartman usb_disconnect(&udev->children[i]); 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds /* deallocate hcd/hardware state ... nuking all pending urbs and 19131da177e4SLinus Torvalds * cleaning up all state associated with the current configuration 19141da177e4SLinus Torvalds * so that the hardware is now fully quiesced. 19151da177e4SLinus Torvalds */ 1916782da727SAlan Stern dev_dbg (&udev->dev, "unregistering device\n"); 19171da177e4SLinus Torvalds usb_disable_device(udev, 0); 1918cde217a5SAlan Stern usb_hcd_synchronize_unlinks(udev); 19191da177e4SLinus Torvalds 19203b23dd6fSAlan Stern usb_remove_ep_devs(&udev->ep0); 1921782da727SAlan Stern usb_unlock_device(udev); 19223099e75aSGreg Kroah-Hartman 1923782da727SAlan Stern /* Unregister the device. The device driver is responsible 19243b23dd6fSAlan Stern * for de-configuring the device and invoking the remove-device 19253b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 1926782da727SAlan Stern */ 1927782da727SAlan Stern device_del(&udev->dev); 1928782da727SAlan Stern 1929782da727SAlan Stern /* Free the device number and delete the parent's children[] 19301da177e4SLinus Torvalds * (or root_hub) pointer. 19311da177e4SLinus Torvalds */ 19323b29b68bSAlan Stern release_devnum(udev); 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds /* Avoid races with recursively_mark_NOTATTACHED() */ 19351da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 19361da177e4SLinus Torvalds *pdev = NULL; 19371da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 19381da177e4SLinus Torvalds 1939f7410cedSHerbert Xu hub_free_dev(udev); 1940f7410cedSHerbert Xu 1941782da727SAlan Stern put_device(&udev->dev); 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds 1944f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 19451da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string) 19461da177e4SLinus Torvalds { 19471da177e4SLinus Torvalds if (!string) 19481da177e4SLinus Torvalds return; 19491da177e4SLinus Torvalds dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 1952f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev) 1953f2a383e4SGreg Kroah-Hartman { 1954f2a383e4SGreg Kroah-Hartman dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", 1955f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idVendor), 1956f2a383e4SGreg Kroah-Hartman le16_to_cpu(udev->descriptor.idProduct)); 1957b9cef6c3SWu Fengguang dev_info(&udev->dev, 1958b9cef6c3SWu Fengguang "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 1959f2a383e4SGreg Kroah-Hartman udev->descriptor.iManufacturer, 1960f2a383e4SGreg Kroah-Hartman udev->descriptor.iProduct, 1961f2a383e4SGreg Kroah-Hartman udev->descriptor.iSerialNumber); 1962f2a383e4SGreg Kroah-Hartman show_string(udev, "Product", udev->product); 1963f2a383e4SGreg Kroah-Hartman show_string(udev, "Manufacturer", udev->manufacturer); 1964f2a383e4SGreg Kroah-Hartman show_string(udev, "SerialNumber", udev->serial); 1965f2a383e4SGreg Kroah-Hartman } 19661da177e4SLinus Torvalds #else 1967f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { } 19681da177e4SLinus Torvalds #endif 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 19711da177e4SLinus Torvalds #include "otg_whitelist.h" 19721da177e4SLinus Torvalds #endif 19731da177e4SLinus Torvalds 19743ede760fSOliver Neukum /** 19758d8558d1SAlan Stern * usb_enumerate_device_otg - FIXME (usbcore-internal) 19763ede760fSOliver Neukum * @udev: newly addressed device (in ADDRESS state) 19773ede760fSOliver Neukum * 19788d8558d1SAlan Stern * Finish enumeration for On-The-Go devices 19793ede760fSOliver Neukum */ 19808d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev) 19811da177e4SLinus Torvalds { 1982d9d16e8aSInaky Perez-Gonzalez int err = 0; 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 19851da177e4SLinus Torvalds /* 19861da177e4SLinus Torvalds * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 19871da177e4SLinus Torvalds * to wake us after we've powered off VBUS; and HNP, switching roles 19881da177e4SLinus Torvalds * "host" to "peripheral". The OTG descriptor helps figure this out. 19891da177e4SLinus Torvalds */ 19901da177e4SLinus Torvalds if (!udev->bus->is_b_host 19911da177e4SLinus Torvalds && udev->config 19921da177e4SLinus Torvalds && udev->parent == udev->bus->root_hub) { 19932eb5052eSFelipe Balbi struct usb_otg_descriptor *desc = NULL; 19941da177e4SLinus Torvalds struct usb_bus *bus = udev->bus; 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds /* descriptor may appear anywhere in config */ 19971da177e4SLinus Torvalds if (__usb_get_extra_descriptor (udev->rawdescriptors[0], 19981da177e4SLinus Torvalds le16_to_cpu(udev->config[0].desc.wTotalLength), 19991da177e4SLinus Torvalds USB_DT_OTG, (void **) &desc) == 0) { 20001da177e4SLinus Torvalds if (desc->bmAttributes & USB_OTG_HNP) { 200112c3da34SAlan Stern unsigned port1 = udev->portnum; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds dev_info(&udev->dev, 20041da177e4SLinus Torvalds "Dual-Role OTG device on %sHNP port\n", 20051da177e4SLinus Torvalds (port1 == bus->otg_port) 20061da177e4SLinus Torvalds ? "" : "non-"); 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /* enable HNP before suspend, it's simpler */ 20091da177e4SLinus Torvalds if (port1 == bus->otg_port) 20101da177e4SLinus Torvalds bus->b_hnp_enable = 1; 20111da177e4SLinus Torvalds err = usb_control_msg(udev, 20121da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 20131da177e4SLinus Torvalds USB_REQ_SET_FEATURE, 0, 20141da177e4SLinus Torvalds bus->b_hnp_enable 20151da177e4SLinus Torvalds ? USB_DEVICE_B_HNP_ENABLE 20161da177e4SLinus Torvalds : USB_DEVICE_A_ALT_HNP_SUPPORT, 20171da177e4SLinus Torvalds 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 20181da177e4SLinus Torvalds if (err < 0) { 20191da177e4SLinus Torvalds /* OTG MESSAGE: report errors here, 20201da177e4SLinus Torvalds * customize to match your product. 20211da177e4SLinus Torvalds */ 20221da177e4SLinus Torvalds dev_info(&udev->dev, 2023b9cef6c3SWu Fengguang "can't set HNP mode: %d\n", 20241da177e4SLinus Torvalds err); 20251da177e4SLinus Torvalds bus->b_hnp_enable = 0; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds if (!is_targeted(udev)) { 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds /* Maybe it can talk to us, though we can't talk to it. 20341da177e4SLinus Torvalds * (Includes HNP test device.) 20351da177e4SLinus Torvalds */ 20361da177e4SLinus Torvalds if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { 2037634a84f8SDavid Brownell err = usb_port_suspend(udev, PMSG_SUSPEND); 20381da177e4SLinus Torvalds if (err < 0) 20391da177e4SLinus Torvalds dev_dbg(&udev->dev, "HNP fail, %d\n", err); 20401da177e4SLinus Torvalds } 2041ffcdc18dSVikram Pandita err = -ENOTSUPP; 20421da177e4SLinus Torvalds goto fail; 20431da177e4SLinus Torvalds } 2044d9d16e8aSInaky Perez-Gonzalez fail: 20451da177e4SLinus Torvalds #endif 2046d9d16e8aSInaky Perez-Gonzalez return err; 2047d9d16e8aSInaky Perez-Gonzalez } 20481da177e4SLinus Torvalds 2049d9d16e8aSInaky Perez-Gonzalez 2050d9d16e8aSInaky Perez-Gonzalez /** 20518d8558d1SAlan Stern * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) 2052d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 2053d9d16e8aSInaky Perez-Gonzalez * 2054d9d16e8aSInaky Perez-Gonzalez * This is only called by usb_new_device() and usb_authorize_device() 2055d9d16e8aSInaky Perez-Gonzalez * and FIXME -- all comments that apply to them apply here wrt to 2056d9d16e8aSInaky Perez-Gonzalez * environment. 2057d9d16e8aSInaky Perez-Gonzalez * 2058d9d16e8aSInaky Perez-Gonzalez * If the device is WUSB and not authorized, we don't attempt to read 2059d9d16e8aSInaky Perez-Gonzalez * the string descriptors, as they will be errored out by the device 2060d9d16e8aSInaky Perez-Gonzalez * until it has been authorized. 2061d9d16e8aSInaky Perez-Gonzalez */ 20628d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev) 2063d9d16e8aSInaky Perez-Gonzalez { 2064d9d16e8aSInaky Perez-Gonzalez int err; 2065d9d16e8aSInaky Perez-Gonzalez 2066d9d16e8aSInaky Perez-Gonzalez if (udev->config == NULL) { 2067d9d16e8aSInaky Perez-Gonzalez err = usb_get_configuration(udev); 2068d9d16e8aSInaky Perez-Gonzalez if (err < 0) { 2069d9d16e8aSInaky Perez-Gonzalez dev_err(&udev->dev, "can't read configurations, error %d\n", 2070d9d16e8aSInaky Perez-Gonzalez err); 2071d9d16e8aSInaky Perez-Gonzalez goto fail; 2072d9d16e8aSInaky Perez-Gonzalez } 2073d9d16e8aSInaky Perez-Gonzalez } 2074d9d16e8aSInaky Perez-Gonzalez if (udev->wusb == 1 && udev->authorized == 0) { 2075d9d16e8aSInaky Perez-Gonzalez udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2076d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2077d9d16e8aSInaky Perez-Gonzalez udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2078d9d16e8aSInaky Perez-Gonzalez } 2079d9d16e8aSInaky Perez-Gonzalez else { 2080d9d16e8aSInaky Perez-Gonzalez /* read the standard strings and cache them if present */ 2081d9d16e8aSInaky Perez-Gonzalez udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 2082d9d16e8aSInaky Perez-Gonzalez udev->manufacturer = usb_cache_string(udev, 2083d9d16e8aSInaky Perez-Gonzalez udev->descriptor.iManufacturer); 2084d9d16e8aSInaky Perez-Gonzalez udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 2085d9d16e8aSInaky Perez-Gonzalez } 20868d8558d1SAlan Stern err = usb_enumerate_device_otg(udev); 2087d9d16e8aSInaky Perez-Gonzalez fail: 2088d9d16e8aSInaky Perez-Gonzalez return err; 2089d9d16e8aSInaky Perez-Gonzalez } 2090d9d16e8aSInaky Perez-Gonzalez 2091d35e70d5SMatthew Garrett static void set_usb_port_removable(struct usb_device *udev) 2092d35e70d5SMatthew Garrett { 2093d35e70d5SMatthew Garrett struct usb_device *hdev = udev->parent; 2094d35e70d5SMatthew Garrett struct usb_hub *hub; 2095d35e70d5SMatthew Garrett u8 port = udev->portnum; 2096d35e70d5SMatthew Garrett u16 wHubCharacteristics; 2097d35e70d5SMatthew Garrett bool removable = true; 2098d35e70d5SMatthew Garrett 2099d35e70d5SMatthew Garrett if (!hdev) 2100d35e70d5SMatthew Garrett return; 2101d35e70d5SMatthew Garrett 2102d35e70d5SMatthew Garrett hub = hdev_to_hub(udev->parent); 2103d35e70d5SMatthew Garrett 2104d35e70d5SMatthew Garrett wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2105d35e70d5SMatthew Garrett 2106d35e70d5SMatthew Garrett if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 2107d35e70d5SMatthew Garrett return; 2108d35e70d5SMatthew Garrett 2109d35e70d5SMatthew Garrett if (hub_is_superspeed(hdev)) { 2110d35e70d5SMatthew Garrett if (hub->descriptor->u.ss.DeviceRemovable & (1 << port)) 2111d35e70d5SMatthew Garrett removable = false; 2112d35e70d5SMatthew Garrett } else { 2113d35e70d5SMatthew Garrett if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 2114d35e70d5SMatthew Garrett removable = false; 2115d35e70d5SMatthew Garrett } 2116d35e70d5SMatthew Garrett 2117d35e70d5SMatthew Garrett if (removable) 2118d35e70d5SMatthew Garrett udev->removable = USB_DEVICE_REMOVABLE; 2119d35e70d5SMatthew Garrett else 2120d35e70d5SMatthew Garrett udev->removable = USB_DEVICE_FIXED; 2121d35e70d5SMatthew Garrett } 2122d9d16e8aSInaky Perez-Gonzalez 2123d9d16e8aSInaky Perez-Gonzalez /** 2124d9d16e8aSInaky Perez-Gonzalez * usb_new_device - perform initial device setup (usbcore-internal) 2125d9d16e8aSInaky Perez-Gonzalez * @udev: newly addressed device (in ADDRESS state) 2126d9d16e8aSInaky Perez-Gonzalez * 21278d8558d1SAlan Stern * This is called with devices which have been detected but not fully 21288d8558d1SAlan Stern * enumerated. The device descriptor is available, but not descriptors 2129d9d16e8aSInaky Perez-Gonzalez * for any device configuration. The caller must have locked either 2130d9d16e8aSInaky Perez-Gonzalez * the parent hub (if udev is a normal device) or else the 2131d9d16e8aSInaky Perez-Gonzalez * usb_bus_list_lock (if udev is a root hub). The parent's pointer to 2132d9d16e8aSInaky Perez-Gonzalez * udev has already been installed, but udev is not yet visible through 2133d9d16e8aSInaky Perez-Gonzalez * sysfs or other filesystem code. 2134d9d16e8aSInaky Perez-Gonzalez * 2135d9d16e8aSInaky Perez-Gonzalez * It will return if the device is configured properly or not. Zero if 2136d9d16e8aSInaky Perez-Gonzalez * the interface was registered with the driver core; else a negative 2137d9d16e8aSInaky Perez-Gonzalez * errno value. 2138d9d16e8aSInaky Perez-Gonzalez * 2139d9d16e8aSInaky Perez-Gonzalez * This call is synchronous, and may not be used in an interrupt context. 2140d9d16e8aSInaky Perez-Gonzalez * 2141d9d16e8aSInaky Perez-Gonzalez * Only the hub driver or root-hub registrar should ever call this. 2142d9d16e8aSInaky Perez-Gonzalez */ 2143d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev) 2144d9d16e8aSInaky Perez-Gonzalez { 2145d9d16e8aSInaky Perez-Gonzalez int err; 2146d9d16e8aSInaky Perez-Gonzalez 214716985408SDan Streetman if (udev->parent) { 214816985408SDan Streetman /* Initialize non-root-hub device wakeup to disabled; 214916985408SDan Streetman * device (un)configuration controls wakeup capable 215016985408SDan Streetman * sysfs power/wakeup controls wakeup enabled/disabled 215116985408SDan Streetman */ 215216985408SDan Streetman device_init_wakeup(&udev->dev, 0); 215316985408SDan Streetman } 215416985408SDan Streetman 21559bbdf1e0SAlan Stern /* Tell the runtime-PM framework the device is active */ 21569bbdf1e0SAlan Stern pm_runtime_set_active(&udev->dev); 2157c08512c7SAlan Stern pm_runtime_get_noresume(&udev->dev); 2158fcc4a01eSAlan Stern pm_runtime_use_autosuspend(&udev->dev); 21599bbdf1e0SAlan Stern pm_runtime_enable(&udev->dev); 21609bbdf1e0SAlan Stern 2161c08512c7SAlan Stern /* By default, forbid autosuspend for all devices. It will be 2162c08512c7SAlan Stern * allowed for hubs during binding. 2163c08512c7SAlan Stern */ 2164c08512c7SAlan Stern usb_disable_autosuspend(udev); 2165c08512c7SAlan Stern 21668d8558d1SAlan Stern err = usb_enumerate_device(udev); /* Read descriptors */ 2167d9d16e8aSInaky Perez-Gonzalez if (err < 0) 2168d9d16e8aSInaky Perez-Gonzalez goto fail; 2169c6515272SSarah Sharp dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 2170c6515272SSarah Sharp udev->devnum, udev->bus->busnum, 2171c6515272SSarah Sharp (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 21729f8b17e6SKay Sievers /* export the usbdev device-node for libusb */ 21739f8b17e6SKay Sievers udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 21749f8b17e6SKay Sievers (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 21759f8b17e6SKay Sievers 21766cd13201SAlan Stern /* Tell the world! */ 21776cd13201SAlan Stern announce_device(udev); 2178195af2ccSAlan Stern 2179927bc916SRafael J. Wysocki device_enable_async_suspend(&udev->dev); 2180d35e70d5SMatthew Garrett 2181d35e70d5SMatthew Garrett /* 2182d35e70d5SMatthew Garrett * check whether the hub marks this port as non-removable. Do it 2183d35e70d5SMatthew Garrett * now so that platform-specific data can override it in 2184d35e70d5SMatthew Garrett * device_add() 2185d35e70d5SMatthew Garrett */ 2186d35e70d5SMatthew Garrett if (udev->parent) 2187d35e70d5SMatthew Garrett set_usb_port_removable(udev); 2188d35e70d5SMatthew Garrett 2189782da727SAlan Stern /* Register the device. The device driver is responsible 21903b23dd6fSAlan Stern * for configuring the device and invoking the add-device 21913b23dd6fSAlan Stern * notifier chain (used by usbfs and possibly others). 2192782da727SAlan Stern */ 21931da177e4SLinus Torvalds err = device_add(&udev->dev); 21941da177e4SLinus Torvalds if (err) { 21951da177e4SLinus Torvalds dev_err(&udev->dev, "can't device_add, error %d\n", err); 21961da177e4SLinus Torvalds goto fail; 21971da177e4SLinus Torvalds } 21989ad3d6ccSAlan Stern 21993b23dd6fSAlan Stern (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2200c08512c7SAlan Stern usb_mark_last_busy(udev); 2201c08512c7SAlan Stern pm_runtime_put_sync_autosuspend(&udev->dev); 2202c066475eSGreg Kroah-Hartman return err; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds fail: 22051da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_NOTATTACHED); 22069bbdf1e0SAlan Stern pm_runtime_disable(&udev->dev); 22079bbdf1e0SAlan Stern pm_runtime_set_suspended(&udev->dev); 2208d9d16e8aSInaky Perez-Gonzalez return err; 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 221193993a0aSInaky Perez-Gonzalez 221293993a0aSInaky Perez-Gonzalez /** 2213fd39c86bSRandy Dunlap * usb_deauthorize_device - deauthorize a device (usbcore-internal) 2214fd39c86bSRandy Dunlap * @usb_dev: USB device 2215fd39c86bSRandy Dunlap * 2216fd39c86bSRandy Dunlap * Move the USB device to a very basic state where interfaces are disabled 2217fd39c86bSRandy Dunlap * and the device is in fact unconfigured and unusable. 221893993a0aSInaky Perez-Gonzalez * 221993993a0aSInaky Perez-Gonzalez * We share a lock (that we have) with device_del(), so we need to 222093993a0aSInaky Perez-Gonzalez * defer its call. 222193993a0aSInaky Perez-Gonzalez */ 222293993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev) 222393993a0aSInaky Perez-Gonzalez { 222493993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 222593993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 0) 222693993a0aSInaky Perez-Gonzalez goto out_unauthorized; 2227da307123SAlan Stern 222893993a0aSInaky Perez-Gonzalez usb_dev->authorized = 0; 222993993a0aSInaky Perez-Gonzalez usb_set_configuration(usb_dev, -1); 2230da307123SAlan Stern 2231da307123SAlan Stern kfree(usb_dev->product); 223293993a0aSInaky Perez-Gonzalez usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2233da307123SAlan Stern kfree(usb_dev->manufacturer); 223493993a0aSInaky Perez-Gonzalez usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2235da307123SAlan Stern kfree(usb_dev->serial); 223693993a0aSInaky Perez-Gonzalez usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); 2237da307123SAlan Stern 2238da307123SAlan Stern usb_destroy_configuration(usb_dev); 223993993a0aSInaky Perez-Gonzalez usb_dev->descriptor.bNumConfigurations = 0; 2240da307123SAlan Stern 224193993a0aSInaky Perez-Gonzalez out_unauthorized: 224293993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); 224393993a0aSInaky Perez-Gonzalez return 0; 224493993a0aSInaky Perez-Gonzalez } 224593993a0aSInaky Perez-Gonzalez 224693993a0aSInaky Perez-Gonzalez 224793993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev) 224893993a0aSInaky Perez-Gonzalez { 224993993a0aSInaky Perez-Gonzalez int result = 0, c; 2250da307123SAlan Stern 225193993a0aSInaky Perez-Gonzalez usb_lock_device(usb_dev); 225293993a0aSInaky Perez-Gonzalez if (usb_dev->authorized == 1) 225393993a0aSInaky Perez-Gonzalez goto out_authorized; 2254da307123SAlan Stern 225593993a0aSInaky Perez-Gonzalez result = usb_autoresume_device(usb_dev); 225693993a0aSInaky Perez-Gonzalez if (result < 0) { 225793993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 225893993a0aSInaky Perez-Gonzalez "can't autoresume for authorization: %d\n", result); 225993993a0aSInaky Perez-Gonzalez goto error_autoresume; 226093993a0aSInaky Perez-Gonzalez } 226193993a0aSInaky Perez-Gonzalez result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); 226293993a0aSInaky Perez-Gonzalez if (result < 0) { 226393993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, "can't re-read device descriptor for " 226493993a0aSInaky Perez-Gonzalez "authorization: %d\n", result); 226593993a0aSInaky Perez-Gonzalez goto error_device_descriptor; 226693993a0aSInaky Perez-Gonzalez } 2267da307123SAlan Stern 2268da307123SAlan Stern kfree(usb_dev->product); 2269da307123SAlan Stern usb_dev->product = NULL; 2270da307123SAlan Stern kfree(usb_dev->manufacturer); 2271da307123SAlan Stern usb_dev->manufacturer = NULL; 2272da307123SAlan Stern kfree(usb_dev->serial); 2273da307123SAlan Stern usb_dev->serial = NULL; 2274da307123SAlan Stern 227593993a0aSInaky Perez-Gonzalez usb_dev->authorized = 1; 22768d8558d1SAlan Stern result = usb_enumerate_device(usb_dev); 227793993a0aSInaky Perez-Gonzalez if (result < 0) 22788d8558d1SAlan Stern goto error_enumerate; 227993993a0aSInaky Perez-Gonzalez /* Choose and set the configuration. This registers the interfaces 228093993a0aSInaky Perez-Gonzalez * with the driver core and lets interface drivers bind to them. 228193993a0aSInaky Perez-Gonzalez */ 2282b5ea060fSGreg Kroah-Hartman c = usb_choose_configuration(usb_dev); 228393993a0aSInaky Perez-Gonzalez if (c >= 0) { 228493993a0aSInaky Perez-Gonzalez result = usb_set_configuration(usb_dev, c); 228593993a0aSInaky Perez-Gonzalez if (result) { 228693993a0aSInaky Perez-Gonzalez dev_err(&usb_dev->dev, 228793993a0aSInaky Perez-Gonzalez "can't set config #%d, error %d\n", c, result); 228893993a0aSInaky Perez-Gonzalez /* This need not be fatal. The user can try to 228993993a0aSInaky Perez-Gonzalez * set other configurations. */ 229093993a0aSInaky Perez-Gonzalez } 229193993a0aSInaky Perez-Gonzalez } 229293993a0aSInaky Perez-Gonzalez dev_info(&usb_dev->dev, "authorized to connect\n"); 2293da307123SAlan Stern 22948d8558d1SAlan Stern error_enumerate: 229593993a0aSInaky Perez-Gonzalez error_device_descriptor: 2296da307123SAlan Stern usb_autosuspend_device(usb_dev); 229793993a0aSInaky Perez-Gonzalez error_autoresume: 229893993a0aSInaky Perez-Gonzalez out_authorized: 229993993a0aSInaky Perez-Gonzalez usb_unlock_device(usb_dev); // complements locktree 230093993a0aSInaky Perez-Gonzalez return result; 230193993a0aSInaky Perez-Gonzalez } 230293993a0aSInaky Perez-Gonzalez 230393993a0aSInaky Perez-Gonzalez 23040165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ 23050165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub) 23060165de09SInaky Perez-Gonzalez { 23070165de09SInaky Perez-Gonzalez struct usb_hcd *hcd; 23080165de09SInaky Perez-Gonzalez if (hub->hdev->parent != NULL) /* not a root hub? */ 23090165de09SInaky Perez-Gonzalez return 0; 23100165de09SInaky Perez-Gonzalez hcd = container_of(hub->hdev->bus, struct usb_hcd, self); 23110165de09SInaky Perez-Gonzalez return hcd->wireless; 23120165de09SInaky Perez-Gonzalez } 23130165de09SInaky Perez-Gonzalez 23140165de09SInaky Perez-Gonzalez 23151da177e4SLinus Torvalds #define PORT_RESET_TRIES 5 23161da177e4SLinus Torvalds #define SET_ADDRESS_TRIES 2 23171da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES 2 23181da177e4SLinus Torvalds #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) 231990ab5ee9SRusty Russell #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 23221da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME 10 232375d7cf72SAndiry Xu #define HUB_BH_RESET_TIME 50 23241da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME 200 23251da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT 500 23261da177e4SLinus Torvalds 232710d674a8SSarah Sharp static int hub_port_reset(struct usb_hub *hub, int port1, 232810d674a8SSarah Sharp struct usb_device *udev, unsigned int delay, bool warm); 232910d674a8SSarah Sharp 233010d674a8SSarah Sharp /* Is a USB 3.0 port in the Inactive state? */ 233110d674a8SSarah Sharp static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus) 233210d674a8SSarah Sharp { 233310d674a8SSarah Sharp return hub_is_superspeed(hub->hdev) && 233410d674a8SSarah Sharp (portstatus & USB_PORT_STAT_LINK_STATE) == 233510d674a8SSarah Sharp USB_SS_PORT_LS_SS_INACTIVE; 233610d674a8SSarah Sharp } 233710d674a8SSarah Sharp 23381da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1, 233975d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm) 23401da177e4SLinus Torvalds { 23411da177e4SLinus Torvalds int delay_time, ret; 23421da177e4SLinus Torvalds u16 portstatus; 23431da177e4SLinus Torvalds u16 portchange; 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds for (delay_time = 0; 23461da177e4SLinus Torvalds delay_time < HUB_RESET_TIMEOUT; 23471da177e4SLinus Torvalds delay_time += delay) { 23481da177e4SLinus Torvalds /* wait to give the device a chance to reset */ 23491da177e4SLinus Torvalds msleep(delay); 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds /* read and decode port status */ 23521da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 23531da177e4SLinus Torvalds if (ret < 0) 23541da177e4SLinus Torvalds return ret; 23551da177e4SLinus Torvalds 235675d7cf72SAndiry Xu /* 235775d7cf72SAndiry Xu * Some buggy devices require a warm reset to be issued even 235875d7cf72SAndiry Xu * when the port appears not to be connected. 235975d7cf72SAndiry Xu */ 236075d7cf72SAndiry Xu if (!warm) { 236110d674a8SSarah Sharp /* 236210d674a8SSarah Sharp * Some buggy devices can cause an NEC host controller 236310d674a8SSarah Sharp * to transition to the "Error" state after a hot port 236410d674a8SSarah Sharp * reset. This will show up as the port state in 236510d674a8SSarah Sharp * "Inactive", and the port may also report a 236610d674a8SSarah Sharp * disconnect. Forcing a warm port reset seems to make 236710d674a8SSarah Sharp * the device work. 236810d674a8SSarah Sharp * 236910d674a8SSarah Sharp * See https://bugzilla.kernel.org/show_bug.cgi?id=41752 237010d674a8SSarah Sharp */ 237110d674a8SSarah Sharp if (hub_port_inactive(hub, portstatus)) { 237210d674a8SSarah Sharp int ret; 237310d674a8SSarah Sharp 237410d674a8SSarah Sharp if ((portchange & USB_PORT_STAT_C_CONNECTION)) 237510d674a8SSarah Sharp clear_port_feature(hub->hdev, port1, 237610d674a8SSarah Sharp USB_PORT_FEAT_C_CONNECTION); 237710d674a8SSarah Sharp if (portchange & USB_PORT_STAT_C_LINK_STATE) 237810d674a8SSarah Sharp clear_port_feature(hub->hdev, port1, 237910d674a8SSarah Sharp USB_PORT_FEAT_C_PORT_LINK_STATE); 238010d674a8SSarah Sharp if (portchange & USB_PORT_STAT_C_RESET) 238110d674a8SSarah Sharp clear_port_feature(hub->hdev, port1, 238210d674a8SSarah Sharp USB_PORT_FEAT_C_RESET); 238310d674a8SSarah Sharp dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n", 238410d674a8SSarah Sharp port1); 238510d674a8SSarah Sharp ret = hub_port_reset(hub, port1, 238610d674a8SSarah Sharp udev, HUB_BH_RESET_TIME, 238710d674a8SSarah Sharp true); 238810d674a8SSarah Sharp if ((portchange & USB_PORT_STAT_C_CONNECTION)) 238910d674a8SSarah Sharp clear_port_feature(hub->hdev, port1, 239010d674a8SSarah Sharp USB_PORT_FEAT_C_CONNECTION); 239110d674a8SSarah Sharp return ret; 239210d674a8SSarah Sharp } 23931da177e4SLinus Torvalds /* Device went away? */ 23941da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_CONNECTION)) 23951da177e4SLinus Torvalds return -ENOTCONN; 23961da177e4SLinus Torvalds 2397dd4dd19eSAlan Stern /* bomb out completely if the connection bounced */ 23981da177e4SLinus Torvalds if ((portchange & USB_PORT_STAT_C_CONNECTION)) 2399dd4dd19eSAlan Stern return -ENOTCONN; 24001da177e4SLinus Torvalds 240175d7cf72SAndiry Xu /* if we`ve finished resetting, then break out of 240275d7cf72SAndiry Xu * the loop 240375d7cf72SAndiry Xu */ 24041da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_RESET) && 24051da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_ENABLE)) { 24060165de09SInaky Perez-Gonzalez if (hub_is_wusb(hub)) 2407551cdbbeSGreg Kroah-Hartman udev->speed = USB_SPEED_WIRELESS; 2408131dec34SSarah Sharp else if (hub_is_superspeed(hub->hdev)) 2409809cd1cbSSarah Sharp udev->speed = USB_SPEED_SUPER; 24100165de09SInaky Perez-Gonzalez else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 24111da177e4SLinus Torvalds udev->speed = USB_SPEED_HIGH; 24121da177e4SLinus Torvalds else if (portstatus & USB_PORT_STAT_LOW_SPEED) 24131da177e4SLinus Torvalds udev->speed = USB_SPEED_LOW; 24141da177e4SLinus Torvalds else 24151da177e4SLinus Torvalds udev->speed = USB_SPEED_FULL; 24161da177e4SLinus Torvalds return 0; 24171da177e4SLinus Torvalds } 241875d7cf72SAndiry Xu } else { 241975d7cf72SAndiry Xu if (portchange & USB_PORT_STAT_C_BH_RESET) 242075d7cf72SAndiry Xu return 0; 242175d7cf72SAndiry Xu } 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds /* switch to the long delay after two short delay failures */ 24241da177e4SLinus Torvalds if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 24251da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds dev_dbg (hub->intfdev, 242875d7cf72SAndiry Xu "port %d not %sreset yet, waiting %dms\n", 242975d7cf72SAndiry Xu port1, warm ? "warm " : "", delay); 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds return -EBUSY; 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 243575d7cf72SAndiry Xu static void hub_port_finish_reset(struct usb_hub *hub, int port1, 243675d7cf72SAndiry Xu struct usb_device *udev, int *status, bool warm) 24371da177e4SLinus Torvalds { 243875d7cf72SAndiry Xu switch (*status) { 24391da177e4SLinus Torvalds case 0: 244075d7cf72SAndiry Xu if (!warm) { 244175d7cf72SAndiry Xu struct usb_hcd *hcd; 2442b789696aSDavid Brownell /* TRSTRCY = 10 ms; plus some extra */ 2443b789696aSDavid Brownell msleep(10 + 40); 24443b29b68bSAlan Stern update_devnum(udev, 0); 244575d7cf72SAndiry Xu hcd = bus_to_hcd(udev->bus); 2446a5f0efabSSarah Sharp if (hcd->driver->reset_device) { 244775d7cf72SAndiry Xu *status = hcd->driver->reset_device(hcd, udev); 244875d7cf72SAndiry Xu if (*status < 0) { 2449a5f0efabSSarah Sharp dev_err(&udev->dev, "Cannot reset " 2450a5f0efabSSarah Sharp "HCD device state\n"); 2451a5f0efabSSarah Sharp break; 2452a5f0efabSSarah Sharp } 2453a5f0efabSSarah Sharp } 245475d7cf72SAndiry Xu } 24551da177e4SLinus Torvalds /* FALL THROUGH */ 24561da177e4SLinus Torvalds case -ENOTCONN: 24571da177e4SLinus Torvalds case -ENODEV: 24581da177e4SLinus Torvalds clear_port_feature(hub->hdev, 24591da177e4SLinus Torvalds port1, USB_PORT_FEAT_C_RESET); 24601da177e4SLinus Torvalds /* FIXME need disconnect() for NOTATTACHED device */ 246175d7cf72SAndiry Xu if (warm) { 246275d7cf72SAndiry Xu clear_port_feature(hub->hdev, port1, 246375d7cf72SAndiry Xu USB_PORT_FEAT_C_BH_PORT_RESET); 246475d7cf72SAndiry Xu clear_port_feature(hub->hdev, port1, 246575d7cf72SAndiry Xu USB_PORT_FEAT_C_PORT_LINK_STATE); 246675d7cf72SAndiry Xu } else { 246775d7cf72SAndiry Xu usb_set_device_state(udev, *status 24681da177e4SLinus Torvalds ? USB_STATE_NOTATTACHED 24691da177e4SLinus Torvalds : USB_STATE_DEFAULT); 247075d7cf72SAndiry Xu } 247175d7cf72SAndiry Xu break; 247275d7cf72SAndiry Xu } 247375d7cf72SAndiry Xu } 247475d7cf72SAndiry Xu 247575d7cf72SAndiry Xu /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 247675d7cf72SAndiry Xu static int hub_port_reset(struct usb_hub *hub, int port1, 247775d7cf72SAndiry Xu struct usb_device *udev, unsigned int delay, bool warm) 247875d7cf72SAndiry Xu { 247975d7cf72SAndiry Xu int i, status; 248075d7cf72SAndiry Xu 248175d7cf72SAndiry Xu if (!warm) { 248275d7cf72SAndiry Xu /* Block EHCI CF initialization during the port reset. 248375d7cf72SAndiry Xu * Some companion controllers don't like it when they mix. 248475d7cf72SAndiry Xu */ 248575d7cf72SAndiry Xu down_read(&ehci_cf_port_reset_rwsem); 248675d7cf72SAndiry Xu } else { 248775d7cf72SAndiry Xu if (!hub_is_superspeed(hub->hdev)) { 248875d7cf72SAndiry Xu dev_err(hub->intfdev, "only USB3 hub support " 248975d7cf72SAndiry Xu "warm reset\n"); 249075d7cf72SAndiry Xu return -EINVAL; 249175d7cf72SAndiry Xu } 249275d7cf72SAndiry Xu } 249375d7cf72SAndiry Xu 249475d7cf72SAndiry Xu /* Reset the port */ 249575d7cf72SAndiry Xu for (i = 0; i < PORT_RESET_TRIES; i++) { 249675d7cf72SAndiry Xu status = set_port_feature(hub->hdev, port1, (warm ? 249775d7cf72SAndiry Xu USB_PORT_FEAT_BH_PORT_RESET : 249875d7cf72SAndiry Xu USB_PORT_FEAT_RESET)); 249975d7cf72SAndiry Xu if (status) { 250075d7cf72SAndiry Xu dev_err(hub->intfdev, 250175d7cf72SAndiry Xu "cannot %sreset port %d (err = %d)\n", 250275d7cf72SAndiry Xu warm ? "warm " : "", port1, status); 250375d7cf72SAndiry Xu } else { 250475d7cf72SAndiry Xu status = hub_port_wait_reset(hub, port1, udev, delay, 250575d7cf72SAndiry Xu warm); 250675d7cf72SAndiry Xu if (status && status != -ENOTCONN) 250775d7cf72SAndiry Xu dev_dbg(hub->intfdev, 250875d7cf72SAndiry Xu "port_wait_reset: err = %d\n", 250975d7cf72SAndiry Xu status); 251075d7cf72SAndiry Xu } 251175d7cf72SAndiry Xu 251275d7cf72SAndiry Xu /* return on disconnect or reset */ 251375d7cf72SAndiry Xu if (status == 0 || status == -ENOTCONN || status == -ENODEV) { 251475d7cf72SAndiry Xu hub_port_finish_reset(hub, port1, udev, &status, warm); 251532fe0198SAlan Stern goto done; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds dev_dbg (hub->intfdev, 251975d7cf72SAndiry Xu "port %d not enabled, trying %sreset again...\n", 252075d7cf72SAndiry Xu port1, warm ? "warm " : ""); 25211da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds dev_err (hub->intfdev, 25251da177e4SLinus Torvalds "Cannot enable port %i. Maybe the USB cable is bad?\n", 25261da177e4SLinus Torvalds port1); 25271da177e4SLinus Torvalds 252832fe0198SAlan Stern done: 252975d7cf72SAndiry Xu if (!warm) 253032fe0198SAlan Stern up_read(&ehci_cf_port_reset_rwsem); 253175d7cf72SAndiry Xu 25321da177e4SLinus Torvalds return status; 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 25350ed9a57eSAndiry Xu /* Check if a port is power on */ 25360ed9a57eSAndiry Xu static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) 25370ed9a57eSAndiry Xu { 25380ed9a57eSAndiry Xu int ret = 0; 25390ed9a57eSAndiry Xu 25400ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) { 25410ed9a57eSAndiry Xu if (portstatus & USB_SS_PORT_STAT_POWER) 25420ed9a57eSAndiry Xu ret = 1; 25430ed9a57eSAndiry Xu } else { 25440ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_POWER) 25450ed9a57eSAndiry Xu ret = 1; 25460ed9a57eSAndiry Xu } 25470ed9a57eSAndiry Xu 25480ed9a57eSAndiry Xu return ret; 25490ed9a57eSAndiry Xu } 25500ed9a57eSAndiry Xu 2551d388dab7SAlan Stern #ifdef CONFIG_PM 25521da177e4SLinus Torvalds 25530ed9a57eSAndiry Xu /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 25540ed9a57eSAndiry Xu static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 25550ed9a57eSAndiry Xu { 25560ed9a57eSAndiry Xu int ret = 0; 25570ed9a57eSAndiry Xu 25580ed9a57eSAndiry Xu if (hub_is_superspeed(hub->hdev)) { 25590ed9a57eSAndiry Xu if ((portstatus & USB_PORT_STAT_LINK_STATE) 25600ed9a57eSAndiry Xu == USB_SS_PORT_LS_U3) 25610ed9a57eSAndiry Xu ret = 1; 25620ed9a57eSAndiry Xu } else { 25630ed9a57eSAndiry Xu if (portstatus & USB_PORT_STAT_SUSPEND) 25640ed9a57eSAndiry Xu ret = 1; 25650ed9a57eSAndiry Xu } 25660ed9a57eSAndiry Xu 25670ed9a57eSAndiry Xu return ret; 25680ed9a57eSAndiry Xu } 2569b01b03f3SAlan Stern 2570b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume, 2571b01b03f3SAlan Stern * is ready for a reset-resume, or should be disconnected. 2572b01b03f3SAlan Stern */ 2573b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev, 2574b01b03f3SAlan Stern struct usb_hub *hub, int port1, 2575b01b03f3SAlan Stern int status, unsigned portchange, unsigned portstatus) 2576b01b03f3SAlan Stern { 2577b01b03f3SAlan Stern /* Is the device still present? */ 25780ed9a57eSAndiry Xu if (status || port_is_suspended(hub, portstatus) || 25790ed9a57eSAndiry Xu !port_is_power_on(hub, portstatus) || 25800ed9a57eSAndiry Xu !(portstatus & USB_PORT_STAT_CONNECTION)) { 2581b01b03f3SAlan Stern if (status >= 0) 2582b01b03f3SAlan Stern status = -ENODEV; 2583b01b03f3SAlan Stern } 2584b01b03f3SAlan Stern 258586c57edfSAlan Stern /* Can't do a normal resume if the port isn't enabled, 258686c57edfSAlan Stern * so try a reset-resume instead. 258786c57edfSAlan Stern */ 258886c57edfSAlan Stern else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 258986c57edfSAlan Stern if (udev->persist_enabled) 259086c57edfSAlan Stern udev->reset_resume = 1; 259186c57edfSAlan Stern else 2592b01b03f3SAlan Stern status = -ENODEV; 259386c57edfSAlan Stern } 2594b01b03f3SAlan Stern 2595b01b03f3SAlan Stern if (status) { 2596b01b03f3SAlan Stern dev_dbg(hub->intfdev, 2597b01b03f3SAlan Stern "port %d status %04x.%04x after resume, %d\n", 2598b01b03f3SAlan Stern port1, portchange, portstatus, status); 2599b01b03f3SAlan Stern } else if (udev->reset_resume) { 2600b01b03f3SAlan Stern 2601b01b03f3SAlan Stern /* Late port handoff can set status-change bits */ 2602b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_CONNECTION) 2603b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 2604b01b03f3SAlan Stern USB_PORT_FEAT_C_CONNECTION); 2605b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_ENABLE) 2606b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 2607b01b03f3SAlan Stern USB_PORT_FEAT_C_ENABLE); 2608b01b03f3SAlan Stern } 2609b01b03f3SAlan Stern 2610b01b03f3SAlan Stern return status; 2611b01b03f3SAlan Stern } 2612b01b03f3SAlan Stern 26131da177e4SLinus Torvalds #ifdef CONFIG_USB_SUSPEND 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds /* 2616140d8f68SAlan Stern * usb_port_suspend - suspend a usb device's upstream port 2617624d6c07SAlan Stern * @udev: device that's no longer in active use, not a root hub 26185edbfb7cSDavid Brownell * Context: must be able to sleep; device not locked; pm locks held 26191da177e4SLinus Torvalds * 26201da177e4SLinus Torvalds * Suspends a USB device that isn't in active use, conserving power. 26211da177e4SLinus Torvalds * Devices may wake out of a suspend, if anything important happens, 26221da177e4SLinus Torvalds * using the remote wakeup mechanism. They may also be taken out of 2623140d8f68SAlan Stern * suspend by the host, using usb_port_resume(). It's also routine 26241da177e4SLinus Torvalds * to disconnect devices while they are suspended. 26251da177e4SLinus Torvalds * 2626390a8c34SDavid Brownell * This only affects the USB hardware for a device; its interfaces 2627390a8c34SDavid Brownell * (and, for hubs, child devices) must already have been suspended. 2628390a8c34SDavid Brownell * 2629624d6c07SAlan Stern * Selective port suspend reduces power; most suspended devices draw 2630624d6c07SAlan Stern * less than 500 uA. It's also used in OTG, along with remote wakeup. 2631624d6c07SAlan Stern * All devices below the suspended port are also suspended. 2632624d6c07SAlan Stern * 2633624d6c07SAlan Stern * Devices leave suspend state when the host wakes them up. Some devices 2634624d6c07SAlan Stern * also support "remote wakeup", where the device can activate the USB 2635624d6c07SAlan Stern * tree above them to deliver data, such as a keypress or packet. In 2636624d6c07SAlan Stern * some cases, this wakes the USB host. 2637624d6c07SAlan Stern * 26381da177e4SLinus Torvalds * Suspending OTG devices may trigger HNP, if that's been enabled 26391da177e4SLinus Torvalds * between a pair of dual-role devices. That will change roles, such 26401da177e4SLinus Torvalds * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 26411da177e4SLinus Torvalds * 26424956eccdSAlan Stern * Devices on USB hub ports have only one "suspend" state, corresponding 26434956eccdSAlan Stern * to ACPI D2, "may cause the device to lose some context". 26444956eccdSAlan Stern * State transitions include: 26454956eccdSAlan Stern * 26464956eccdSAlan Stern * - suspend, resume ... when the VBUS power link stays live 26474956eccdSAlan Stern * - suspend, disconnect ... VBUS lost 26484956eccdSAlan Stern * 26494956eccdSAlan Stern * Once VBUS drop breaks the circuit, the port it's using has to go through 26504956eccdSAlan Stern * normal re-enumeration procedures, starting with enabling VBUS power. 26514956eccdSAlan Stern * Other than re-initializing the hub (plug/unplug, except for root hubs), 26524956eccdSAlan Stern * Linux (2.6) currently has NO mechanisms to initiate that: no khubd 26534956eccdSAlan Stern * timer, no SRP, no requests through sysfs. 26544956eccdSAlan Stern * 26554956eccdSAlan Stern * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when 26564956eccdSAlan Stern * the root hub for their bus goes into global suspend ... so we don't 26574956eccdSAlan Stern * (falsely) update the device power state to say it suspended. 26584956eccdSAlan Stern * 26591da177e4SLinus Torvalds * Returns 0 on success, else negative errno. 26601da177e4SLinus Torvalds */ 266165bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 26621da177e4SLinus Torvalds { 2663624d6c07SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 2664624d6c07SAlan Stern int port1 = udev->portnum; 2665624d6c07SAlan Stern int status; 26664956eccdSAlan Stern 2667624d6c07SAlan Stern /* enable remote wakeup when appropriate; this lets the device 2668624d6c07SAlan Stern * wake up the upstream hub (including maybe the root hub). 2669624d6c07SAlan Stern * 2670624d6c07SAlan Stern * NOTE: OTG devices may issue remote wakeup (or SRP) even when 2671624d6c07SAlan Stern * we don't explicitly enable it here. 2672624d6c07SAlan Stern */ 2673624d6c07SAlan Stern if (udev->do_remote_wakeup) { 2674623bef9eSSarah Sharp if (!hub_is_superspeed(hub->hdev)) { 2675624d6c07SAlan Stern status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2676624d6c07SAlan Stern USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 2677624d6c07SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, 2678624d6c07SAlan Stern NULL, 0, 2679624d6c07SAlan Stern USB_CTRL_SET_TIMEOUT); 2680623bef9eSSarah Sharp } else { 2681623bef9eSSarah Sharp /* Assume there's only one function on the USB 3.0 2682623bef9eSSarah Sharp * device and enable remote wake for the first 2683623bef9eSSarah Sharp * interface. FIXME if the interface association 2684623bef9eSSarah Sharp * descriptor shows there's more than one function. 2685623bef9eSSarah Sharp */ 2686623bef9eSSarah Sharp status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2687623bef9eSSarah Sharp USB_REQ_SET_FEATURE, 2688623bef9eSSarah Sharp USB_RECIP_INTERFACE, 2689623bef9eSSarah Sharp USB_INTRF_FUNC_SUSPEND, 26903b9b6acdSSarah Sharp USB_INTRF_FUNC_SUSPEND_RW | 26913b9b6acdSSarah Sharp USB_INTRF_FUNC_SUSPEND_LP, 2692623bef9eSSarah Sharp NULL, 0, 2693623bef9eSSarah Sharp USB_CTRL_SET_TIMEOUT); 2694623bef9eSSarah Sharp } 26950c487206SOliver Neukum if (status) { 2696624d6c07SAlan Stern dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 2697624d6c07SAlan Stern status); 26980c487206SOliver Neukum /* bail if autosuspend is requested */ 26995b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg)) 27000c487206SOliver Neukum return status; 27010c487206SOliver Neukum } 2702624d6c07SAlan Stern } 2703624d6c07SAlan Stern 270465580b43SAndiry Xu /* disable USB2 hardware LPM */ 270565580b43SAndiry Xu if (udev->usb2_hw_lpm_enabled == 1) 270665580b43SAndiry Xu usb_set_usb2_hardware_lpm(udev, 0); 270765580b43SAndiry Xu 27088306095fSSarah Sharp if (usb_unlocked_disable_lpm(udev)) { 27098306095fSSarah Sharp dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.", 27108306095fSSarah Sharp __func__); 27118306095fSSarah Sharp return -ENOMEM; 27128306095fSSarah Sharp } 27138306095fSSarah Sharp 2714624d6c07SAlan Stern /* see 7.1.7.6 */ 2715a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) 2716a7114230SAndiry Xu status = set_port_feature(hub->hdev, 2717a7114230SAndiry Xu port1 | (USB_SS_PORT_LS_U3 << 3), 2718a7114230SAndiry Xu USB_PORT_FEAT_LINK_STATE); 2719a8f08d86SAndiry Xu else 2720a8f08d86SAndiry Xu status = set_port_feature(hub->hdev, port1, 2721a8f08d86SAndiry Xu USB_PORT_FEAT_SUSPEND); 2722624d6c07SAlan Stern if (status) { 2723624d6c07SAlan Stern dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", 2724624d6c07SAlan Stern port1, status); 2725624d6c07SAlan Stern /* paranoia: "should not happen" */ 27260c487206SOliver Neukum if (udev->do_remote_wakeup) 2727624d6c07SAlan Stern (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2728624d6c07SAlan Stern USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 2729624d6c07SAlan Stern USB_DEVICE_REMOTE_WAKEUP, 0, 2730624d6c07SAlan Stern NULL, 0, 2731624d6c07SAlan Stern USB_CTRL_SET_TIMEOUT); 2732cbb33004SAlan Stern 2733c3e751e4SAndiry Xu /* Try to enable USB2 hardware LPM again */ 2734c3e751e4SAndiry Xu if (udev->usb2_hw_lpm_capable == 1) 2735c3e751e4SAndiry Xu usb_set_usb2_hardware_lpm(udev, 1); 2736c3e751e4SAndiry Xu 27378306095fSSarah Sharp /* Try to enable USB3 LPM again */ 27388306095fSSarah Sharp usb_unlocked_enable_lpm(udev); 27398306095fSSarah Sharp 2740cbb33004SAlan Stern /* System sleep transitions should never fail */ 27415b1b0b81SAlan Stern if (!PMSG_IS_AUTO(msg)) 2742cbb33004SAlan Stern status = 0; 2743624d6c07SAlan Stern } else { 2744624d6c07SAlan Stern /* device has up to 10 msec to fully suspend */ 274530b1a7a3SAlan Stern dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 274630b1a7a3SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : ""), 274730b1a7a3SAlan Stern udev->do_remote_wakeup); 2748624d6c07SAlan Stern usb_set_device_state(udev, USB_STATE_SUSPENDED); 2749624d6c07SAlan Stern msleep(10); 2750624d6c07SAlan Stern } 2751c08512c7SAlan Stern usb_mark_last_busy(hub->hdev); 27524956eccdSAlan Stern return status; 27531da177e4SLinus Torvalds } 2754f3f3253dSDavid Brownell 27551da177e4SLinus Torvalds /* 2756390a8c34SDavid Brownell * If the USB "suspend" state is in use (rather than "global suspend"), 2757390a8c34SDavid Brownell * many devices will be individually taken out of suspend state using 275854515fe5SAlan Stern * special "resume" signaling. This routine kicks in shortly after 27591da177e4SLinus Torvalds * hardware resume signaling is finished, either because of selective 27601da177e4SLinus Torvalds * resume (by host) or remote wakeup (by device) ... now see what changed 27611da177e4SLinus Torvalds * in the tree that's rooted at this device. 276254515fe5SAlan Stern * 276354515fe5SAlan Stern * If @udev->reset_resume is set then the device is reset before the 276454515fe5SAlan Stern * status check is done. 27651da177e4SLinus Torvalds */ 2766140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev) 27671da177e4SLinus Torvalds { 276854515fe5SAlan Stern int status = 0; 27691da177e4SLinus Torvalds u16 devstatus; 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds /* caller owns the udev device lock */ 2772b9cef6c3SWu Fengguang dev_dbg(&udev->dev, "%s\n", 2773b9cef6c3SWu Fengguang udev->reset_resume ? "finish reset-resume" : "finish resume"); 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds /* usb ch9 identifies four variants of SUSPENDED, based on what 27761da177e4SLinus Torvalds * state the device resumes to. Linux currently won't see the 27771da177e4SLinus Torvalds * first two on the host side; they'd be inside hub_port_init() 27781da177e4SLinus Torvalds * during many timeouts, but khubd can't suspend until later. 27791da177e4SLinus Torvalds */ 27801da177e4SLinus Torvalds usb_set_device_state(udev, udev->actconfig 27811da177e4SLinus Torvalds ? USB_STATE_CONFIGURED 27821da177e4SLinus Torvalds : USB_STATE_ADDRESS); 27831da177e4SLinus Torvalds 278454515fe5SAlan Stern /* 10.5.4.5 says not to reset a suspended port if the attached 278554515fe5SAlan Stern * device is enabled for remote wakeup. Hence the reset 278654515fe5SAlan Stern * operation is carried out here, after the port has been 278754515fe5SAlan Stern * resumed. 278854515fe5SAlan Stern */ 278954515fe5SAlan Stern if (udev->reset_resume) 279086c57edfSAlan Stern retry_reset_resume: 2791742120c6SMing Lei status = usb_reset_and_verify_device(udev); 279254515fe5SAlan Stern 27931da177e4SLinus Torvalds /* 10.5.4.5 says be sure devices in the tree are still there. 27941da177e4SLinus Torvalds * For now let's assume the device didn't go crazy on resume, 27951da177e4SLinus Torvalds * and device drivers will know about any resume quirks. 27961da177e4SLinus Torvalds */ 279754515fe5SAlan Stern if (status == 0) { 279846dede46SAlan Stern devstatus = 0; 27991da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 2800b40b7a90SAlan Stern if (status >= 0) 280146dede46SAlan Stern status = (status > 0 ? 0 : -ENODEV); 280286c57edfSAlan Stern 280386c57edfSAlan Stern /* If a normal resume failed, try doing a reset-resume */ 280486c57edfSAlan Stern if (status && !udev->reset_resume && udev->persist_enabled) { 280586c57edfSAlan Stern dev_dbg(&udev->dev, "retry with reset-resume\n"); 280686c57edfSAlan Stern udev->reset_resume = 1; 280786c57edfSAlan Stern goto retry_reset_resume; 280886c57edfSAlan Stern } 280954515fe5SAlan Stern } 2810b40b7a90SAlan Stern 2811624d6c07SAlan Stern if (status) { 2812624d6c07SAlan Stern dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 28131da177e4SLinus Torvalds status); 2814624d6c07SAlan Stern } else if (udev->actconfig) { 28151da177e4SLinus Torvalds le16_to_cpus(&devstatus); 2816686314cfSAlan Stern if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { 28171da177e4SLinus Torvalds status = usb_control_msg(udev, 28181da177e4SLinus Torvalds usb_sndctrlpipe(udev, 0), 28191da177e4SLinus Torvalds USB_REQ_CLEAR_FEATURE, 28201da177e4SLinus Torvalds USB_RECIP_DEVICE, 28211da177e4SLinus Torvalds USB_DEVICE_REMOTE_WAKEUP, 0, 28221da177e4SLinus Torvalds NULL, 0, 28231da177e4SLinus Torvalds USB_CTRL_SET_TIMEOUT); 2824a8e7c565SAlan Stern if (status) 2825b9cef6c3SWu Fengguang dev_dbg(&udev->dev, 2826b9cef6c3SWu Fengguang "disable remote wakeup, status %d\n", 2827b9cef6c3SWu Fengguang status); 28284bf0ba86SAlan Stern } 28291da177e4SLinus Torvalds status = 0; 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds return status; 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 2834624d6c07SAlan Stern /* 2835624d6c07SAlan Stern * usb_port_resume - re-activate a suspended usb device's upstream port 2836624d6c07SAlan Stern * @udev: device to re-activate, not a root hub 2837624d6c07SAlan Stern * Context: must be able to sleep; device not locked; pm locks held 2838624d6c07SAlan Stern * 2839624d6c07SAlan Stern * This will re-activate the suspended device, increasing power usage 2840624d6c07SAlan Stern * while letting drivers communicate again with its endpoints. 2841624d6c07SAlan Stern * USB resume explicitly guarantees that the power session between 2842624d6c07SAlan Stern * the host and the device is the same as it was when the device 2843624d6c07SAlan Stern * suspended. 2844624d6c07SAlan Stern * 2845feccc30dSAlan Stern * If @udev->reset_resume is set then this routine won't check that the 2846feccc30dSAlan Stern * port is still enabled. Furthermore, finish_port_resume() above will 284754515fe5SAlan Stern * reset @udev. The end result is that a broken power session can be 284854515fe5SAlan Stern * recovered and @udev will appear to persist across a loss of VBUS power. 284954515fe5SAlan Stern * 285054515fe5SAlan Stern * For example, if a host controller doesn't maintain VBUS suspend current 285154515fe5SAlan Stern * during a system sleep or is reset when the system wakes up, all the USB 285254515fe5SAlan Stern * power sessions below it will be broken. This is especially troublesome 285354515fe5SAlan Stern * for mass-storage devices containing mounted filesystems, since the 285454515fe5SAlan Stern * device will appear to have disconnected and all the memory mappings 285554515fe5SAlan Stern * to it will be lost. Using the USB_PERSIST facility, the device can be 285654515fe5SAlan Stern * made to appear as if it had not disconnected. 285754515fe5SAlan Stern * 2858742120c6SMing Lei * This facility can be dangerous. Although usb_reset_and_verify_device() makes 2859feccc30dSAlan Stern * every effort to insure that the same device is present after the 286054515fe5SAlan Stern * reset as before, it cannot provide a 100% guarantee. Furthermore it's 286154515fe5SAlan Stern * quite possible for a device to remain unaltered but its media to be 286254515fe5SAlan Stern * changed. If the user replaces a flash memory card while the system is 286354515fe5SAlan Stern * asleep, he will have only himself to blame when the filesystem on the 286454515fe5SAlan Stern * new card is corrupted and the system crashes. 286554515fe5SAlan Stern * 2866624d6c07SAlan Stern * Returns 0 on success, else negative errno. 2867624d6c07SAlan Stern */ 286865bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 28691da177e4SLinus Torvalds { 2870624d6c07SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 2871624d6c07SAlan Stern int port1 = udev->portnum; 28721da177e4SLinus Torvalds int status; 2873d25450c6SAlan Stern u16 portchange, portstatus; 2874d25450c6SAlan Stern 2875d25450c6SAlan Stern /* Skip the initial Clear-Suspend step for a remote wakeup */ 2876d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 28770ed9a57eSAndiry Xu if (status == 0 && !port_is_suspended(hub, portstatus)) 2878d25450c6SAlan Stern goto SuspendCleared; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds // dev_dbg(hub->intfdev, "resume port %d\n", port1); 28811da177e4SLinus Torvalds 2882d5cbad4bSAlan Stern set_bit(port1, hub->busy_bits); 2883d5cbad4bSAlan Stern 28841da177e4SLinus Torvalds /* see 7.1.7.7; affects power usage, but not budgeting */ 2885a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) 2886a7114230SAndiry Xu status = set_port_feature(hub->hdev, 2887a7114230SAndiry Xu port1 | (USB_SS_PORT_LS_U0 << 3), 2888a7114230SAndiry Xu USB_PORT_FEAT_LINK_STATE); 2889a7114230SAndiry Xu else 28901da177e4SLinus Torvalds status = clear_port_feature(hub->hdev, 28911da177e4SLinus Torvalds port1, USB_PORT_FEAT_SUSPEND); 28921da177e4SLinus Torvalds if (status) { 2893624d6c07SAlan Stern dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", 28941da177e4SLinus Torvalds port1, status); 28951da177e4SLinus Torvalds } else { 28961da177e4SLinus Torvalds /* drive resume for at least 20 msec */ 2897645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", 28985b1b0b81SAlan Stern (PMSG_IS_AUTO(msg) ? "auto-" : "")); 28991da177e4SLinus Torvalds msleep(25); 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds /* Virtual root hubs can trigger on GET_PORT_STATUS to 29021da177e4SLinus Torvalds * stop resume signaling. Then finish the resume 29031da177e4SLinus Torvalds * sequence. 29041da177e4SLinus Torvalds */ 2905d25450c6SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 290654515fe5SAlan Stern 29071da177e4SLinus Torvalds /* TRSMRCY = 10 msec */ 29081da177e4SLinus Torvalds msleep(10); 29091da177e4SLinus Torvalds } 2910b01b03f3SAlan Stern 2911b01b03f3SAlan Stern SuspendCleared: 2912b01b03f3SAlan Stern if (status == 0) { 2913a7114230SAndiry Xu if (hub_is_superspeed(hub->hdev)) { 2914a7114230SAndiry Xu if (portchange & USB_PORT_STAT_C_LINK_STATE) 2915a7114230SAndiry Xu clear_port_feature(hub->hdev, port1, 2916a7114230SAndiry Xu USB_PORT_FEAT_C_PORT_LINK_STATE); 2917a7114230SAndiry Xu } else { 2918b01b03f3SAlan Stern if (portchange & USB_PORT_STAT_C_SUSPEND) 2919b01b03f3SAlan Stern clear_port_feature(hub->hdev, port1, 2920b01b03f3SAlan Stern USB_PORT_FEAT_C_SUSPEND); 29211da177e4SLinus Torvalds } 2922a7114230SAndiry Xu } 29231da177e4SLinus Torvalds 2924d5cbad4bSAlan Stern clear_bit(port1, hub->busy_bits); 2925d5cbad4bSAlan Stern 2926b01b03f3SAlan Stern status = check_port_resume_type(udev, 2927b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 292854515fe5SAlan Stern if (status == 0) 292954515fe5SAlan Stern status = finish_port_resume(udev); 293054515fe5SAlan Stern if (status < 0) { 293154515fe5SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 293254515fe5SAlan Stern hub_port_logical_disconnect(hub, port1); 293365580b43SAndiry Xu } else { 293465580b43SAndiry Xu /* Try to enable USB2 hardware LPM */ 293565580b43SAndiry Xu if (udev->usb2_hw_lpm_capable == 1) 293665580b43SAndiry Xu usb_set_usb2_hardware_lpm(udev, 1); 29378306095fSSarah Sharp 29388306095fSSarah Sharp /* Try to enable USB3 LPM */ 29398306095fSSarah Sharp usb_unlocked_enable_lpm(udev); 294054515fe5SAlan Stern } 294165580b43SAndiry Xu 29421da177e4SLinus Torvalds return status; 29431da177e4SLinus Torvalds } 29441da177e4SLinus Torvalds 29458808f00cSAlan Stern /* caller has locked udev */ 29460534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev) 29471da177e4SLinus Torvalds { 29481da177e4SLinus Torvalds int status = 0; 29491da177e4SLinus Torvalds 29501da177e4SLinus Torvalds if (udev->state == USB_STATE_SUSPENDED) { 2951645daaabSAlan Stern dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 29529bbdf1e0SAlan Stern status = usb_autoresume_device(udev); 29539bbdf1e0SAlan Stern if (status == 0) { 29549bbdf1e0SAlan Stern /* Let the drivers do their thing, then... */ 29559bbdf1e0SAlan Stern usb_autosuspend_device(udev); 29569bbdf1e0SAlan Stern } 2957d25450c6SAlan Stern } 29581da177e4SLinus Torvalds return status; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 2961d388dab7SAlan Stern #else /* CONFIG_USB_SUSPEND */ 2962d388dab7SAlan Stern 2963d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */ 2964d388dab7SAlan Stern 296565bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 2966d388dab7SAlan Stern { 2967d388dab7SAlan Stern return 0; 2968d388dab7SAlan Stern } 2969d388dab7SAlan Stern 2970b01b03f3SAlan Stern /* However we may need to do a reset-resume */ 2971b01b03f3SAlan Stern 297265bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg) 2973d388dab7SAlan Stern { 2974b01b03f3SAlan Stern struct usb_hub *hub = hdev_to_hub(udev->parent); 2975b01b03f3SAlan Stern int port1 = udev->portnum; 2976b01b03f3SAlan Stern int status; 2977b01b03f3SAlan Stern u16 portchange, portstatus; 297854515fe5SAlan Stern 2979b01b03f3SAlan Stern status = hub_port_status(hub, port1, &portstatus, &portchange); 2980b01b03f3SAlan Stern status = check_port_resume_type(udev, 2981b01b03f3SAlan Stern hub, port1, status, portchange, portstatus); 2982b01b03f3SAlan Stern 2983b01b03f3SAlan Stern if (status) { 2984b01b03f3SAlan Stern dev_dbg(&udev->dev, "can't resume, status %d\n", status); 2985b01b03f3SAlan Stern hub_port_logical_disconnect(hub, port1); 2986b01b03f3SAlan Stern } else if (udev->reset_resume) { 298754515fe5SAlan Stern dev_dbg(&udev->dev, "reset-resume\n"); 2988742120c6SMing Lei status = usb_reset_and_verify_device(udev); 298954515fe5SAlan Stern } 299054515fe5SAlan Stern return status; 2991d388dab7SAlan Stern } 2992d388dab7SAlan Stern 2993d388dab7SAlan Stern #endif 2994d388dab7SAlan Stern 2995db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 29961da177e4SLinus Torvalds { 29971da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata (intf); 29981da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 29991da177e4SLinus Torvalds unsigned port1; 30004296c70aSSarah Sharp int status; 30011da177e4SLinus Torvalds 3002cbb33004SAlan Stern /* Warn if children aren't already suspended */ 30031da177e4SLinus Torvalds for (port1 = 1; port1 <= hdev->maxchild; port1++) { 30041da177e4SLinus Torvalds struct usb_device *udev; 30051da177e4SLinus Torvalds 3006fa286188SGreg Kroah-Hartman udev = hdev->children [port1-1]; 30076840d255SAlan Stern if (udev && udev->can_submit) { 3008cbb33004SAlan Stern dev_warn(&intf->dev, "port %d nyet suspended\n", port1); 30095b1b0b81SAlan Stern if (PMSG_IS_AUTO(msg)) 3010c9f89fa4SDavid Brownell return -EBUSY; 3011c9f89fa4SDavid Brownell } 30121da177e4SLinus Torvalds } 30134296c70aSSarah Sharp if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 30144296c70aSSarah Sharp /* Enable hub to send remote wakeup for all ports. */ 30154296c70aSSarah Sharp for (port1 = 1; port1 <= hdev->maxchild; port1++) { 30164296c70aSSarah Sharp status = set_port_feature(hdev, 30174296c70aSSarah Sharp port1 | 30184296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 30194296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 30204296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 30214296c70aSSarah Sharp USB_PORT_FEAT_REMOTE_WAKE_MASK); 30224296c70aSSarah Sharp } 30234296c70aSSarah Sharp } 30241da177e4SLinus Torvalds 3025441b62c1SHarvey Harrison dev_dbg(&intf->dev, "%s\n", __func__); 302640f122f3SAlan Stern 3027c9f89fa4SDavid Brownell /* stop khubd and related activity */ 30284330354fSAlan Stern hub_quiesce(hub, HUB_SUSPEND); 3029b6f6436dSAlan Stern return 0; 30301da177e4SLinus Torvalds } 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf) 30331da177e4SLinus Torvalds { 30341da177e4SLinus Torvalds struct usb_hub *hub = usb_get_intfdata(intf); 30351da177e4SLinus Torvalds 30365e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 3037f2835219SAlan Stern hub_activate(hub, HUB_RESUME); 30381da177e4SLinus Torvalds return 0; 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds 3041b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf) 3042f07600cfSAlan Stern { 3043b41a60ecSAlan Stern struct usb_hub *hub = usb_get_intfdata(intf); 3044f07600cfSAlan Stern 30455e6effaeSAlan Stern dev_dbg(&intf->dev, "%s\n", __func__); 3046f2835219SAlan Stern hub_activate(hub, HUB_RESET_RESUME); 3047f07600cfSAlan Stern return 0; 3048f07600cfSAlan Stern } 3049f07600cfSAlan Stern 305054515fe5SAlan Stern /** 305154515fe5SAlan Stern * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 305254515fe5SAlan Stern * @rhdev: struct usb_device for the root hub 305354515fe5SAlan Stern * 305454515fe5SAlan Stern * The USB host controller driver calls this function when its root hub 305554515fe5SAlan Stern * is resumed and Vbus power has been interrupted or the controller 3056feccc30dSAlan Stern * has been reset. The routine marks @rhdev as having lost power. 3057feccc30dSAlan Stern * When the hub driver is resumed it will take notice and carry out 3058feccc30dSAlan Stern * power-session recovery for all the "USB-PERSIST"-enabled child devices; 3059feccc30dSAlan Stern * the others will be disconnected. 306054515fe5SAlan Stern */ 306154515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev) 306254515fe5SAlan Stern { 306354515fe5SAlan Stern dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); 306454515fe5SAlan Stern rhdev->reset_resume = 1; 306554515fe5SAlan Stern } 306654515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 306754515fe5SAlan Stern 30681ea7e0e8SSarah Sharp static const char * const usb3_lpm_names[] = { 30691ea7e0e8SSarah Sharp "U0", 30701ea7e0e8SSarah Sharp "U1", 30711ea7e0e8SSarah Sharp "U2", 30721ea7e0e8SSarah Sharp "U3", 30731ea7e0e8SSarah Sharp }; 30741ea7e0e8SSarah Sharp 30751ea7e0e8SSarah Sharp /* 30761ea7e0e8SSarah Sharp * Send a Set SEL control transfer to the device, prior to enabling 30771ea7e0e8SSarah Sharp * device-initiated U1 or U2. This lets the device know the exit latencies from 30781ea7e0e8SSarah Sharp * the time the device initiates a U1 or U2 exit, to the time it will receive a 30791ea7e0e8SSarah Sharp * packet from the host. 30801ea7e0e8SSarah Sharp * 30811ea7e0e8SSarah Sharp * This function will fail if the SEL or PEL values for udev are greater than 30821ea7e0e8SSarah Sharp * the maximum allowed values for the link state to be enabled. 30831ea7e0e8SSarah Sharp */ 30841ea7e0e8SSarah Sharp static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) 30851ea7e0e8SSarah Sharp { 30861ea7e0e8SSarah Sharp struct usb_set_sel_req *sel_values; 30871ea7e0e8SSarah Sharp unsigned long long u1_sel; 30881ea7e0e8SSarah Sharp unsigned long long u1_pel; 30891ea7e0e8SSarah Sharp unsigned long long u2_sel; 30901ea7e0e8SSarah Sharp unsigned long long u2_pel; 30911ea7e0e8SSarah Sharp int ret; 30921ea7e0e8SSarah Sharp 30931ea7e0e8SSarah Sharp /* Convert SEL and PEL stored in ns to us */ 30941ea7e0e8SSarah Sharp u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 30951ea7e0e8SSarah Sharp u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 30961ea7e0e8SSarah Sharp u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 30971ea7e0e8SSarah Sharp u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 30981ea7e0e8SSarah Sharp 30991ea7e0e8SSarah Sharp /* 31001ea7e0e8SSarah Sharp * Make sure that the calculated SEL and PEL values for the link 31011ea7e0e8SSarah Sharp * state we're enabling aren't bigger than the max SEL/PEL 31021ea7e0e8SSarah Sharp * value that will fit in the SET SEL control transfer. 31031ea7e0e8SSarah Sharp * Otherwise the device would get an incorrect idea of the exit 31041ea7e0e8SSarah Sharp * latency for the link state, and could start a device-initiated 31051ea7e0e8SSarah Sharp * U1/U2 when the exit latencies are too high. 31061ea7e0e8SSarah Sharp */ 31071ea7e0e8SSarah Sharp if ((state == USB3_LPM_U1 && 31081ea7e0e8SSarah Sharp (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 31091ea7e0e8SSarah Sharp u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || 31101ea7e0e8SSarah Sharp (state == USB3_LPM_U2 && 31111ea7e0e8SSarah Sharp (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 31121ea7e0e8SSarah Sharp u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { 31131ea7e0e8SSarah Sharp dev_dbg(&udev->dev, "Device-initiated %s disabled due " 31141ea7e0e8SSarah Sharp "to long SEL %llu ms or PEL %llu ms\n", 31151ea7e0e8SSarah Sharp usb3_lpm_names[state], u1_sel, u1_pel); 31161ea7e0e8SSarah Sharp return -EINVAL; 31171ea7e0e8SSarah Sharp } 31181ea7e0e8SSarah Sharp 31191ea7e0e8SSarah Sharp /* 31201ea7e0e8SSarah Sharp * If we're enabling device-initiated LPM for one link state, 31211ea7e0e8SSarah Sharp * but the other link state has a too high SEL or PEL value, 31221ea7e0e8SSarah Sharp * just set those values to the max in the Set SEL request. 31231ea7e0e8SSarah Sharp */ 31241ea7e0e8SSarah Sharp if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) 31251ea7e0e8SSarah Sharp u1_sel = USB3_LPM_MAX_U1_SEL_PEL; 31261ea7e0e8SSarah Sharp 31271ea7e0e8SSarah Sharp if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) 31281ea7e0e8SSarah Sharp u1_pel = USB3_LPM_MAX_U1_SEL_PEL; 31291ea7e0e8SSarah Sharp 31301ea7e0e8SSarah Sharp if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) 31311ea7e0e8SSarah Sharp u2_sel = USB3_LPM_MAX_U2_SEL_PEL; 31321ea7e0e8SSarah Sharp 31331ea7e0e8SSarah Sharp if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) 31341ea7e0e8SSarah Sharp u2_pel = USB3_LPM_MAX_U2_SEL_PEL; 31351ea7e0e8SSarah Sharp 31361ea7e0e8SSarah Sharp /* 31371ea7e0e8SSarah Sharp * usb_enable_lpm() can be called as part of a failed device reset, 31381ea7e0e8SSarah Sharp * which may be initiated by an error path of a mass storage driver. 31391ea7e0e8SSarah Sharp * Therefore, use GFP_NOIO. 31401ea7e0e8SSarah Sharp */ 31411ea7e0e8SSarah Sharp sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); 31421ea7e0e8SSarah Sharp if (!sel_values) 31431ea7e0e8SSarah Sharp return -ENOMEM; 31441ea7e0e8SSarah Sharp 31451ea7e0e8SSarah Sharp sel_values->u1_sel = u1_sel; 31461ea7e0e8SSarah Sharp sel_values->u1_pel = u1_pel; 31471ea7e0e8SSarah Sharp sel_values->u2_sel = cpu_to_le16(u2_sel); 31481ea7e0e8SSarah Sharp sel_values->u2_pel = cpu_to_le16(u2_pel); 31491ea7e0e8SSarah Sharp 31501ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 31511ea7e0e8SSarah Sharp USB_REQ_SET_SEL, 31521ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 31531ea7e0e8SSarah Sharp 0, 0, 31541ea7e0e8SSarah Sharp sel_values, sizeof *(sel_values), 31551ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 31561ea7e0e8SSarah Sharp kfree(sel_values); 31571ea7e0e8SSarah Sharp return ret; 31581ea7e0e8SSarah Sharp } 31591ea7e0e8SSarah Sharp 31601ea7e0e8SSarah Sharp /* 31611ea7e0e8SSarah Sharp * Enable or disable device-initiated U1 or U2 transitions. 31621ea7e0e8SSarah Sharp */ 31631ea7e0e8SSarah Sharp static int usb_set_device_initiated_lpm(struct usb_device *udev, 31641ea7e0e8SSarah Sharp enum usb3_link_state state, bool enable) 31651ea7e0e8SSarah Sharp { 31661ea7e0e8SSarah Sharp int ret; 31671ea7e0e8SSarah Sharp int feature; 31681ea7e0e8SSarah Sharp 31691ea7e0e8SSarah Sharp switch (state) { 31701ea7e0e8SSarah Sharp case USB3_LPM_U1: 31711ea7e0e8SSarah Sharp feature = USB_DEVICE_U1_ENABLE; 31721ea7e0e8SSarah Sharp break; 31731ea7e0e8SSarah Sharp case USB3_LPM_U2: 31741ea7e0e8SSarah Sharp feature = USB_DEVICE_U2_ENABLE; 31751ea7e0e8SSarah Sharp break; 31761ea7e0e8SSarah Sharp default: 31771ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 31781ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable"); 31791ea7e0e8SSarah Sharp return -EINVAL; 31801ea7e0e8SSarah Sharp } 31811ea7e0e8SSarah Sharp 31821ea7e0e8SSarah Sharp if (udev->state != USB_STATE_CONFIGURED) { 31831ea7e0e8SSarah Sharp dev_dbg(&udev->dev, "%s: Can't %s %s state " 31841ea7e0e8SSarah Sharp "for unconfigured device.\n", 31851ea7e0e8SSarah Sharp __func__, enable ? "enable" : "disable", 31861ea7e0e8SSarah Sharp usb3_lpm_names[state]); 31871ea7e0e8SSarah Sharp return 0; 31881ea7e0e8SSarah Sharp } 31891ea7e0e8SSarah Sharp 31901ea7e0e8SSarah Sharp if (enable) { 31911ea7e0e8SSarah Sharp /* 31921ea7e0e8SSarah Sharp * First, let the device know about the exit latencies 31931ea7e0e8SSarah Sharp * associated with the link state we're about to enable. 31941ea7e0e8SSarah Sharp */ 31951ea7e0e8SSarah Sharp ret = usb_req_set_sel(udev, state); 31961ea7e0e8SSarah Sharp if (ret < 0) { 31971ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Set SEL for device-initiated " 31981ea7e0e8SSarah Sharp "%s failed.\n", usb3_lpm_names[state]); 31991ea7e0e8SSarah Sharp return -EBUSY; 32001ea7e0e8SSarah Sharp } 32011ea7e0e8SSarah Sharp /* 32021ea7e0e8SSarah Sharp * Now send the control transfer to enable device-initiated LPM 32031ea7e0e8SSarah Sharp * for either U1 or U2. 32041ea7e0e8SSarah Sharp */ 32051ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 32061ea7e0e8SSarah Sharp USB_REQ_SET_FEATURE, 32071ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 32081ea7e0e8SSarah Sharp feature, 32091ea7e0e8SSarah Sharp 0, NULL, 0, 32101ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 32111ea7e0e8SSarah Sharp } else { 32121ea7e0e8SSarah Sharp ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 32131ea7e0e8SSarah Sharp USB_REQ_CLEAR_FEATURE, 32141ea7e0e8SSarah Sharp USB_RECIP_DEVICE, 32151ea7e0e8SSarah Sharp feature, 32161ea7e0e8SSarah Sharp 0, NULL, 0, 32171ea7e0e8SSarah Sharp USB_CTRL_SET_TIMEOUT); 32181ea7e0e8SSarah Sharp } 32191ea7e0e8SSarah Sharp if (ret < 0) { 32201ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 32211ea7e0e8SSarah Sharp enable ? "Enable" : "Disable", 32221ea7e0e8SSarah Sharp usb3_lpm_names[state]); 32231ea7e0e8SSarah Sharp return -EBUSY; 32241ea7e0e8SSarah Sharp } 32251ea7e0e8SSarah Sharp return 0; 32261ea7e0e8SSarah Sharp } 32271ea7e0e8SSarah Sharp 32281ea7e0e8SSarah Sharp static int usb_set_lpm_timeout(struct usb_device *udev, 32291ea7e0e8SSarah Sharp enum usb3_link_state state, int timeout) 32301ea7e0e8SSarah Sharp { 32311ea7e0e8SSarah Sharp int ret; 32321ea7e0e8SSarah Sharp int feature; 32331ea7e0e8SSarah Sharp 32341ea7e0e8SSarah Sharp switch (state) { 32351ea7e0e8SSarah Sharp case USB3_LPM_U1: 32361ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U1_TIMEOUT; 32371ea7e0e8SSarah Sharp break; 32381ea7e0e8SSarah Sharp case USB3_LPM_U2: 32391ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U2_TIMEOUT; 32401ea7e0e8SSarah Sharp break; 32411ea7e0e8SSarah Sharp default: 32421ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 32431ea7e0e8SSarah Sharp __func__); 32441ea7e0e8SSarah Sharp return -EINVAL; 32451ea7e0e8SSarah Sharp } 32461ea7e0e8SSarah Sharp 32471ea7e0e8SSarah Sharp if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 32481ea7e0e8SSarah Sharp timeout != USB3_LPM_DEVICE_INITIATED) { 32491ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 32501ea7e0e8SSarah Sharp "which is a reserved value.\n", 32511ea7e0e8SSarah Sharp usb3_lpm_names[state], timeout); 32521ea7e0e8SSarah Sharp return -EINVAL; 32531ea7e0e8SSarah Sharp } 32541ea7e0e8SSarah Sharp 32551ea7e0e8SSarah Sharp ret = set_port_feature(udev->parent, 32561ea7e0e8SSarah Sharp USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 32571ea7e0e8SSarah Sharp feature); 32581ea7e0e8SSarah Sharp if (ret < 0) { 32591ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 32601ea7e0e8SSarah Sharp "error code %i\n", usb3_lpm_names[state], 32611ea7e0e8SSarah Sharp timeout, ret); 32621ea7e0e8SSarah Sharp return -EBUSY; 32631ea7e0e8SSarah Sharp } 32641ea7e0e8SSarah Sharp if (state == USB3_LPM_U1) 32651ea7e0e8SSarah Sharp udev->u1_params.timeout = timeout; 32661ea7e0e8SSarah Sharp else 32671ea7e0e8SSarah Sharp udev->u2_params.timeout = timeout; 32681ea7e0e8SSarah Sharp return 0; 32691ea7e0e8SSarah Sharp } 32701ea7e0e8SSarah Sharp 32711ea7e0e8SSarah Sharp /* 32721ea7e0e8SSarah Sharp * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 32731ea7e0e8SSarah Sharp * U1/U2 entry. 32741ea7e0e8SSarah Sharp * 32751ea7e0e8SSarah Sharp * We will attempt to enable U1 or U2, but there are no guarantees that the 32761ea7e0e8SSarah Sharp * control transfers to set the hub timeout or enable device-initiated U1/U2 32771ea7e0e8SSarah Sharp * will be successful. 32781ea7e0e8SSarah Sharp * 32791ea7e0e8SSarah Sharp * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 32801ea7e0e8SSarah Sharp * driver know about it. If that call fails, it should be harmless, and just 32811ea7e0e8SSarah Sharp * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 32821ea7e0e8SSarah Sharp */ 32831ea7e0e8SSarah Sharp static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 32841ea7e0e8SSarah Sharp enum usb3_link_state state) 32851ea7e0e8SSarah Sharp { 32861ea7e0e8SSarah Sharp int timeout; 32871ea7e0e8SSarah Sharp 32881ea7e0e8SSarah Sharp /* We allow the host controller to set the U1/U2 timeout internally 32891ea7e0e8SSarah Sharp * first, so that it can change its schedule to account for the 32901ea7e0e8SSarah Sharp * additional latency to send data to a device in a lower power 32911ea7e0e8SSarah Sharp * link state. 32921ea7e0e8SSarah Sharp */ 32931ea7e0e8SSarah Sharp timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 32941ea7e0e8SSarah Sharp 32951ea7e0e8SSarah Sharp /* xHCI host controller doesn't want to enable this LPM state. */ 32961ea7e0e8SSarah Sharp if (timeout == 0) 32971ea7e0e8SSarah Sharp return; 32981ea7e0e8SSarah Sharp 32991ea7e0e8SSarah Sharp if (timeout < 0) { 33001ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not enable %s link state, " 33011ea7e0e8SSarah Sharp "xHCI error %i.\n", usb3_lpm_names[state], 33021ea7e0e8SSarah Sharp timeout); 33031ea7e0e8SSarah Sharp return; 33041ea7e0e8SSarah Sharp } 33051ea7e0e8SSarah Sharp 33061ea7e0e8SSarah Sharp if (usb_set_lpm_timeout(udev, state, timeout)) 33071ea7e0e8SSarah Sharp /* If we can't set the parent hub U1/U2 timeout, 33081ea7e0e8SSarah Sharp * device-initiated LPM won't be allowed either, so let the xHCI 33091ea7e0e8SSarah Sharp * host know that this link state won't be enabled. 33101ea7e0e8SSarah Sharp */ 33111ea7e0e8SSarah Sharp hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 33121ea7e0e8SSarah Sharp 33131ea7e0e8SSarah Sharp /* Only a configured device will accept the Set Feature U1/U2_ENABLE */ 33141ea7e0e8SSarah Sharp else if (udev->actconfig) 33151ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, true); 33161ea7e0e8SSarah Sharp 33171ea7e0e8SSarah Sharp } 33181ea7e0e8SSarah Sharp 33191ea7e0e8SSarah Sharp /* 33201ea7e0e8SSarah Sharp * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 33211ea7e0e8SSarah Sharp * U1/U2 entry. 33221ea7e0e8SSarah Sharp * 33231ea7e0e8SSarah Sharp * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 33241ea7e0e8SSarah Sharp * If zero is returned, the parent will not allow the link to go into U1/U2. 33251ea7e0e8SSarah Sharp * 33261ea7e0e8SSarah Sharp * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 33271ea7e0e8SSarah Sharp * it won't have an effect on the bus link state because the parent hub will 33281ea7e0e8SSarah Sharp * still disallow device-initiated U1/U2 entry. 33291ea7e0e8SSarah Sharp * 33301ea7e0e8SSarah Sharp * If zero is returned, the xHCI host controller may still think U1/U2 entry is 33311ea7e0e8SSarah Sharp * possible. The result will be slightly more bus bandwidth will be taken up 33321ea7e0e8SSarah Sharp * (to account for U1/U2 exit latency), but it should be harmless. 33331ea7e0e8SSarah Sharp */ 33341ea7e0e8SSarah Sharp static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 33351ea7e0e8SSarah Sharp enum usb3_link_state state) 33361ea7e0e8SSarah Sharp { 33371ea7e0e8SSarah Sharp int feature; 33381ea7e0e8SSarah Sharp 33391ea7e0e8SSarah Sharp switch (state) { 33401ea7e0e8SSarah Sharp case USB3_LPM_U1: 33411ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U1_TIMEOUT; 33421ea7e0e8SSarah Sharp break; 33431ea7e0e8SSarah Sharp case USB3_LPM_U2: 33441ea7e0e8SSarah Sharp feature = USB_PORT_FEAT_U2_TIMEOUT; 33451ea7e0e8SSarah Sharp break; 33461ea7e0e8SSarah Sharp default: 33471ea7e0e8SSarah Sharp dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 33481ea7e0e8SSarah Sharp __func__); 33491ea7e0e8SSarah Sharp return -EINVAL; 33501ea7e0e8SSarah Sharp } 33511ea7e0e8SSarah Sharp 33521ea7e0e8SSarah Sharp if (usb_set_lpm_timeout(udev, state, 0)) 33531ea7e0e8SSarah Sharp return -EBUSY; 33541ea7e0e8SSarah Sharp 33551ea7e0e8SSarah Sharp usb_set_device_initiated_lpm(udev, state, false); 33561ea7e0e8SSarah Sharp 33571ea7e0e8SSarah Sharp if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 33581ea7e0e8SSarah Sharp dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 33591ea7e0e8SSarah Sharp "bus schedule bandwidth may be impacted.\n", 33601ea7e0e8SSarah Sharp usb3_lpm_names[state]); 33611ea7e0e8SSarah Sharp return 0; 33621ea7e0e8SSarah Sharp } 33631ea7e0e8SSarah Sharp 33641ea7e0e8SSarah Sharp /* 33651ea7e0e8SSarah Sharp * Disable hub-initiated and device-initiated U1 and U2 entry. 33661ea7e0e8SSarah Sharp * Caller must own the bandwidth_mutex. 33671ea7e0e8SSarah Sharp * 33681ea7e0e8SSarah Sharp * This will call usb_enable_lpm() on failure, which will decrement 33691ea7e0e8SSarah Sharp * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 33701ea7e0e8SSarah Sharp */ 33711ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev) 33721ea7e0e8SSarah Sharp { 33731ea7e0e8SSarah Sharp struct usb_hcd *hcd; 33741ea7e0e8SSarah Sharp 33751ea7e0e8SSarah Sharp if (!udev || !udev->parent || 33761ea7e0e8SSarah Sharp udev->speed != USB_SPEED_SUPER || 33771ea7e0e8SSarah Sharp !udev->lpm_capable) 33781ea7e0e8SSarah Sharp return 0; 33791ea7e0e8SSarah Sharp 33801ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus); 33811ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 33821ea7e0e8SSarah Sharp return 0; 33831ea7e0e8SSarah Sharp 33841ea7e0e8SSarah Sharp udev->lpm_disable_count++; 338555558c33SDan Carpenter if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 33861ea7e0e8SSarah Sharp return 0; 33871ea7e0e8SSarah Sharp 33881ea7e0e8SSarah Sharp /* If LPM is enabled, attempt to disable it. */ 33891ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 33901ea7e0e8SSarah Sharp goto enable_lpm; 33911ea7e0e8SSarah Sharp if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 33921ea7e0e8SSarah Sharp goto enable_lpm; 33931ea7e0e8SSarah Sharp 33941ea7e0e8SSarah Sharp return 0; 33951ea7e0e8SSarah Sharp 33961ea7e0e8SSarah Sharp enable_lpm: 33971ea7e0e8SSarah Sharp usb_enable_lpm(udev); 33981ea7e0e8SSarah Sharp return -EBUSY; 33991ea7e0e8SSarah Sharp } 34001ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm); 34011ea7e0e8SSarah Sharp 34021ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 34031ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev) 34041ea7e0e8SSarah Sharp { 34051ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 34061ea7e0e8SSarah Sharp int ret; 34071ea7e0e8SSarah Sharp 34081ea7e0e8SSarah Sharp if (!hcd) 34091ea7e0e8SSarah Sharp return -EINVAL; 34101ea7e0e8SSarah Sharp 34111ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex); 34121ea7e0e8SSarah Sharp ret = usb_disable_lpm(udev); 34131ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 34141ea7e0e8SSarah Sharp 34151ea7e0e8SSarah Sharp return ret; 34161ea7e0e8SSarah Sharp } 34171ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 34181ea7e0e8SSarah Sharp 34191ea7e0e8SSarah Sharp /* 34201ea7e0e8SSarah Sharp * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 34211ea7e0e8SSarah Sharp * xHCI host policy may prevent U1 or U2 from being enabled. 34221ea7e0e8SSarah Sharp * 34231ea7e0e8SSarah Sharp * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 34241ea7e0e8SSarah Sharp * until the lpm_disable_count drops to zero. Caller must own the 34251ea7e0e8SSarah Sharp * bandwidth_mutex. 34261ea7e0e8SSarah Sharp */ 34271ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) 34281ea7e0e8SSarah Sharp { 34291ea7e0e8SSarah Sharp struct usb_hcd *hcd; 34301ea7e0e8SSarah Sharp 34311ea7e0e8SSarah Sharp if (!udev || !udev->parent || 34321ea7e0e8SSarah Sharp udev->speed != USB_SPEED_SUPER || 34331ea7e0e8SSarah Sharp !udev->lpm_capable) 34341ea7e0e8SSarah Sharp return; 34351ea7e0e8SSarah Sharp 34361ea7e0e8SSarah Sharp udev->lpm_disable_count--; 34371ea7e0e8SSarah Sharp hcd = bus_to_hcd(udev->bus); 34381ea7e0e8SSarah Sharp /* Double check that we can both enable and disable LPM. 34391ea7e0e8SSarah Sharp * Device must be configured to accept set feature U1/U2 timeout. 34401ea7e0e8SSarah Sharp */ 34411ea7e0e8SSarah Sharp if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 34421ea7e0e8SSarah Sharp !hcd->driver->disable_usb3_lpm_timeout) 34431ea7e0e8SSarah Sharp return; 34441ea7e0e8SSarah Sharp 34451ea7e0e8SSarah Sharp if (udev->lpm_disable_count > 0) 34461ea7e0e8SSarah Sharp return; 34471ea7e0e8SSarah Sharp 34481ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U1); 34491ea7e0e8SSarah Sharp usb_enable_link_state(hcd, udev, USB3_LPM_U2); 34501ea7e0e8SSarah Sharp } 34511ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm); 34521ea7e0e8SSarah Sharp 34531ea7e0e8SSarah Sharp /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 34541ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) 34551ea7e0e8SSarah Sharp { 34561ea7e0e8SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 34571ea7e0e8SSarah Sharp 34581ea7e0e8SSarah Sharp if (!hcd) 34591ea7e0e8SSarah Sharp return; 34601ea7e0e8SSarah Sharp 34611ea7e0e8SSarah Sharp mutex_lock(hcd->bandwidth_mutex); 34621ea7e0e8SSarah Sharp usb_enable_lpm(udev); 34631ea7e0e8SSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 34641ea7e0e8SSarah Sharp } 34651ea7e0e8SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 34661ea7e0e8SSarah Sharp 34671ea7e0e8SSarah Sharp 3468d388dab7SAlan Stern #else /* CONFIG_PM */ 3469d388dab7SAlan Stern 3470511366daSAndrew Morton #define hub_suspend NULL 3471511366daSAndrew Morton #define hub_resume NULL 3472f07600cfSAlan Stern #define hub_reset_resume NULL 34731ea7e0e8SSarah Sharp 34741ea7e0e8SSarah Sharp int usb_disable_lpm(struct usb_device *udev) 34751ea7e0e8SSarah Sharp { 34761ea7e0e8SSarah Sharp return 0; 34771ea7e0e8SSarah Sharp } 3478e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_disable_lpm); 34791ea7e0e8SSarah Sharp 34801ea7e0e8SSarah Sharp void usb_enable_lpm(struct usb_device *udev) { } 3481e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_enable_lpm); 34821ea7e0e8SSarah Sharp 34831ea7e0e8SSarah Sharp int usb_unlocked_disable_lpm(struct usb_device *udev) 34841ea7e0e8SSarah Sharp { 34851ea7e0e8SSarah Sharp return 0; 34861ea7e0e8SSarah Sharp } 3487e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 34881ea7e0e8SSarah Sharp 34891ea7e0e8SSarah Sharp void usb_unlocked_enable_lpm(struct usb_device *udev) { } 3490e9261fb6SSarah Sharp EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 3491d388dab7SAlan Stern #endif 3492d388dab7SAlan Stern 34931da177e4SLinus Torvalds 34941da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 34951da177e4SLinus Torvalds * 34961da177e4SLinus Torvalds * Between connect detection and reset signaling there must be a delay 34971da177e4SLinus Torvalds * of 100ms at least for debounce and power-settling. The corresponding 34981da177e4SLinus Torvalds * timer shall restart whenever the downstream port detects a disconnect. 34991da177e4SLinus Torvalds * 35001da177e4SLinus Torvalds * Apparently there are some bluetooth and irda-dongles and a number of 35011da177e4SLinus Torvalds * low-speed devices for which this debounce period may last over a second. 35021da177e4SLinus Torvalds * Not covered by the spec - but easy to deal with. 35031da177e4SLinus Torvalds * 35041da177e4SLinus Torvalds * This implementation uses a 1500ms total debounce timeout; if the 35051da177e4SLinus Torvalds * connection isn't stable by then it returns -ETIMEDOUT. It checks 35061da177e4SLinus Torvalds * every 25ms for transient disconnects. When the port status has been 35071da177e4SLinus Torvalds * unchanged for 100ms it returns the port status. 35081da177e4SLinus Torvalds */ 35091da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1) 35101da177e4SLinus Torvalds { 35111da177e4SLinus Torvalds int ret; 35121da177e4SLinus Torvalds int total_time, stable_time = 0; 35131da177e4SLinus Torvalds u16 portchange, portstatus; 35141da177e4SLinus Torvalds unsigned connection = 0xffff; 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 35171da177e4SLinus Torvalds ret = hub_port_status(hub, port1, &portstatus, &portchange); 35181da177e4SLinus Torvalds if (ret < 0) 35191da177e4SLinus Torvalds return ret; 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 35221da177e4SLinus Torvalds (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 35231da177e4SLinus Torvalds stable_time += HUB_DEBOUNCE_STEP; 35241da177e4SLinus Torvalds if (stable_time >= HUB_DEBOUNCE_STABLE) 35251da177e4SLinus Torvalds break; 35261da177e4SLinus Torvalds } else { 35271da177e4SLinus Torvalds stable_time = 0; 35281da177e4SLinus Torvalds connection = portstatus & USB_PORT_STAT_CONNECTION; 35291da177e4SLinus Torvalds } 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 35321da177e4SLinus Torvalds clear_port_feature(hub->hdev, port1, 35331da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 35341da177e4SLinus Torvalds } 35351da177e4SLinus Torvalds 35361da177e4SLinus Torvalds if (total_time >= HUB_DEBOUNCE_TIMEOUT) 35371da177e4SLinus Torvalds break; 35381da177e4SLinus Torvalds msleep(HUB_DEBOUNCE_STEP); 35391da177e4SLinus Torvalds } 35401da177e4SLinus Torvalds 35411da177e4SLinus Torvalds dev_dbg (hub->intfdev, 35421da177e4SLinus Torvalds "debounce: port %d: total %dms stable %dms status 0x%x\n", 35431da177e4SLinus Torvalds port1, total_time, stable_time, portstatus); 35441da177e4SLinus Torvalds 35451da177e4SLinus Torvalds if (stable_time < HUB_DEBOUNCE_STABLE) 35461da177e4SLinus Torvalds return -ETIMEDOUT; 35471da177e4SLinus Torvalds return portstatus; 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds 3550fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev) 35511da177e4SLinus Torvalds { 3552ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 3553ddeac4e7SAlan Stern usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 35542caf7fcdSAlan Stern usb_enable_endpoint(udev, &udev->ep0, true); 35551da177e4SLinus Torvalds } 3556fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit); 35571da177e4SLinus Torvalds 35581da177e4SLinus Torvalds #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 35591da177e4SLinus Torvalds #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 35601da177e4SLinus Torvalds 35614326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum) 35621da177e4SLinus Torvalds { 35631da177e4SLinus Torvalds int retval; 3564c6515272SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 35651da177e4SLinus Torvalds 3566c6515272SSarah Sharp /* 3567c6515272SSarah Sharp * The host controller will choose the device address, 3568c6515272SSarah Sharp * instead of the core having chosen it earlier 3569c6515272SSarah Sharp */ 3570c6515272SSarah Sharp if (!hcd->driver->address_device && devnum <= 1) 35711da177e4SLinus Torvalds return -EINVAL; 35721da177e4SLinus Torvalds if (udev->state == USB_STATE_ADDRESS) 35731da177e4SLinus Torvalds return 0; 35741da177e4SLinus Torvalds if (udev->state != USB_STATE_DEFAULT) 35751da177e4SLinus Torvalds return -EINVAL; 3576c8d4af8eSAndiry Xu if (hcd->driver->address_device) 3577c6515272SSarah Sharp retval = hcd->driver->address_device(hcd, udev); 3578c8d4af8eSAndiry Xu else 35791da177e4SLinus Torvalds retval = usb_control_msg(udev, usb_sndaddr0pipe(), 35804326ed0bSAlan Stern USB_REQ_SET_ADDRESS, 0, devnum, 0, 35811da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 35821da177e4SLinus Torvalds if (retval == 0) { 35833b29b68bSAlan Stern update_devnum(udev, devnum); 35844953d141SDavid Vrabel /* Device now using proper address. */ 35851da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_ADDRESS); 3586fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 35871da177e4SLinus Torvalds } 35881da177e4SLinus Torvalds return retval; 35891da177e4SLinus Torvalds } 35901da177e4SLinus Torvalds 35911da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor. 35921da177e4SLinus Torvalds * Device connection must be stable, no more debouncing needed. 35931da177e4SLinus Torvalds * Returns device in USB_STATE_ADDRESS, except on error. 35941da177e4SLinus Torvalds * 35951da177e4SLinus Torvalds * If this is called for an already-existing device (as part of 3596742120c6SMing Lei * usb_reset_and_verify_device), the caller must own the device lock. For a 35971da177e4SLinus Torvalds * newly detected device that is not accessible through any global 35981da177e4SLinus Torvalds * pointers, it's not necessary to lock the device. 35991da177e4SLinus Torvalds */ 36001da177e4SLinus Torvalds static int 36011da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 36021da177e4SLinus Torvalds int retry_counter) 36031da177e4SLinus Torvalds { 36044186ecf8SArjan van de Ven static DEFINE_MUTEX(usb_address0_mutex); 36051da177e4SLinus Torvalds 36061da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 3607e7b77172SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 36081da177e4SLinus Torvalds int i, j, retval; 36091da177e4SLinus Torvalds unsigned delay = HUB_SHORT_RESET_TIME; 36101da177e4SLinus Torvalds enum usb_device_speed oldspeed = udev->speed; 3611e538dfdaSMichal Nazarewicz const char *speed; 36124326ed0bSAlan Stern int devnum = udev->devnum; 36131da177e4SLinus Torvalds 36141da177e4SLinus Torvalds /* root hub ports have a slightly longer reset period 36151da177e4SLinus Torvalds * (from USB 2.0 spec, section 7.1.7.5) 36161da177e4SLinus Torvalds */ 36171da177e4SLinus Torvalds if (!hdev->parent) { 36181da177e4SLinus Torvalds delay = HUB_ROOT_RESET_TIME; 36191da177e4SLinus Torvalds if (port1 == hdev->bus->otg_port) 36201da177e4SLinus Torvalds hdev->bus->b_hnp_enable = 0; 36211da177e4SLinus Torvalds } 36221da177e4SLinus Torvalds 36231da177e4SLinus Torvalds /* Some low speed devices have problems with the quick delay, so */ 36241da177e4SLinus Torvalds /* be a bit pessimistic with those devices. RHbug #23670 */ 36251da177e4SLinus Torvalds if (oldspeed == USB_SPEED_LOW) 36261da177e4SLinus Torvalds delay = HUB_LONG_RESET_TIME; 36271da177e4SLinus Torvalds 36284186ecf8SArjan van de Ven mutex_lock(&usb_address0_mutex); 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds /* Reset the device; full speed may morph to high speed */ 3631e7b77172SSarah Sharp /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 363275d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false); 36331da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 36341da177e4SLinus Torvalds goto fail; 36351da177e4SLinus Torvalds /* success, speed is known */ 363607194ab7SLuben Tuikov 36371da177e4SLinus Torvalds retval = -ENODEV; 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { 36401da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset changed speed!\n"); 36411da177e4SLinus Torvalds goto fail; 36421da177e4SLinus Torvalds } 36431da177e4SLinus Torvalds oldspeed = udev->speed; 36441da177e4SLinus Torvalds 36451da177e4SLinus Torvalds /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 36461da177e4SLinus Torvalds * it's fixed size except for full speed devices. 36475bb6e0aeSInaky Perez-Gonzalez * For Wireless USB devices, ep0 max packet is always 512 (tho 36485bb6e0aeSInaky Perez-Gonzalez * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 36491da177e4SLinus Torvalds */ 36501da177e4SLinus Torvalds switch (udev->speed) { 36516b403b02SSarah Sharp case USB_SPEED_SUPER: 3652551cdbbeSGreg Kroah-Hartman case USB_SPEED_WIRELESS: /* fixed at 512 */ 3653551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 36545bb6e0aeSInaky Perez-Gonzalez break; 36551da177e4SLinus Torvalds case USB_SPEED_HIGH: /* fixed at 64 */ 3656551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 36571da177e4SLinus Torvalds break; 36581da177e4SLinus Torvalds case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 36591da177e4SLinus Torvalds /* to determine the ep0 maxpacket size, try to read 36601da177e4SLinus Torvalds * the device descriptor to get bMaxPacketSize0 and 36611da177e4SLinus Torvalds * then correct our initial guess. 36621da177e4SLinus Torvalds */ 3663551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 36641da177e4SLinus Torvalds break; 36651da177e4SLinus Torvalds case USB_SPEED_LOW: /* fixed at 8 */ 3666551509d2SHarvey Harrison udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 36671da177e4SLinus Torvalds break; 36681da177e4SLinus Torvalds default: 36691da177e4SLinus Torvalds goto fail; 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds 3672e538dfdaSMichal Nazarewicz if (udev->speed == USB_SPEED_WIRELESS) 3673e538dfdaSMichal Nazarewicz speed = "variable speed Wireless"; 3674e538dfdaSMichal Nazarewicz else 3675e538dfdaSMichal Nazarewicz speed = usb_speed_string(udev->speed); 3676e538dfdaSMichal Nazarewicz 3677c6515272SSarah Sharp if (udev->speed != USB_SPEED_SUPER) 367883a07196SInaky Perez-Gonzalez dev_info(&udev->dev, 3679e538dfdaSMichal Nazarewicz "%s %s USB device number %d using %s\n", 3680e538dfdaSMichal Nazarewicz (udev->config) ? "reset" : "new", speed, 36813b29b68bSAlan Stern devnum, udev->bus->controller->driver->name); 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds /* Set up TT records, if needed */ 36841da177e4SLinus Torvalds if (hdev->tt) { 36851da177e4SLinus Torvalds udev->tt = hdev->tt; 36861da177e4SLinus Torvalds udev->ttport = hdev->ttport; 36871da177e4SLinus Torvalds } else if (udev->speed != USB_SPEED_HIGH 36881da177e4SLinus Torvalds && hdev->speed == USB_SPEED_HIGH) { 3689d199c96dSAlan Stern if (!hub->tt.hub) { 3690d199c96dSAlan Stern dev_err(&udev->dev, "parent hub has no TT\n"); 3691d199c96dSAlan Stern retval = -EINVAL; 3692d199c96dSAlan Stern goto fail; 3693d199c96dSAlan Stern } 36941da177e4SLinus Torvalds udev->tt = &hub->tt; 36951da177e4SLinus Torvalds udev->ttport = port1; 36961da177e4SLinus Torvalds } 36971da177e4SLinus Torvalds 36981da177e4SLinus Torvalds /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 36991da177e4SLinus Torvalds * Because device hardware and firmware is sometimes buggy in 37001da177e4SLinus Torvalds * this area, and this is how Linux has done it for ages. 37011da177e4SLinus Torvalds * Change it cautiously. 37021da177e4SLinus Torvalds * 37031da177e4SLinus Torvalds * NOTE: If USE_NEW_SCHEME() is true we will start by issuing 37041da177e4SLinus Torvalds * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 37051da177e4SLinus Torvalds * so it may help with some non-standards-compliant devices. 37061da177e4SLinus Torvalds * Otherwise we start with SET_ADDRESS and then try to read the 37071da177e4SLinus Torvalds * first 8 bytes of the device descriptor to get the ep0 maxpacket 37081da177e4SLinus Torvalds * value. 37091da177e4SLinus Torvalds */ 37101da177e4SLinus Torvalds for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 3711c6515272SSarah Sharp if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) { 37121da177e4SLinus Torvalds struct usb_device_descriptor *buf; 37131da177e4SLinus Torvalds int r = 0; 37141da177e4SLinus Torvalds 37151da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE 64 37161da177e4SLinus Torvalds buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 37171da177e4SLinus Torvalds if (!buf) { 37181da177e4SLinus Torvalds retval = -ENOMEM; 37191da177e4SLinus Torvalds continue; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 3722b89ee19aSAlan Stern /* Retry on all errors; some devices are flakey. 3723b89ee19aSAlan Stern * 255 is for WUSB devices, we actually need to use 3724b89ee19aSAlan Stern * 512 (WUSB1.0[4.8.1]). 37251da177e4SLinus Torvalds */ 37261da177e4SLinus Torvalds for (j = 0; j < 3; ++j) { 37271da177e4SLinus Torvalds buf->bMaxPacketSize0 = 0; 37281da177e4SLinus Torvalds r = usb_control_msg(udev, usb_rcvaddr0pipe(), 37291da177e4SLinus Torvalds USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 37301da177e4SLinus Torvalds USB_DT_DEVICE << 8, 0, 37311da177e4SLinus Torvalds buf, GET_DESCRIPTOR_BUFSIZE, 3732fd7c519dSJaroslav Kysela initial_descriptor_timeout); 37331da177e4SLinus Torvalds switch (buf->bMaxPacketSize0) { 37345bb6e0aeSInaky Perez-Gonzalez case 8: case 16: case 32: case 64: case 255: 37351da177e4SLinus Torvalds if (buf->bDescriptorType == 37361da177e4SLinus Torvalds USB_DT_DEVICE) { 37371da177e4SLinus Torvalds r = 0; 37381da177e4SLinus Torvalds break; 37391da177e4SLinus Torvalds } 37401da177e4SLinus Torvalds /* FALL THROUGH */ 37411da177e4SLinus Torvalds default: 37421da177e4SLinus Torvalds if (r == 0) 37431da177e4SLinus Torvalds r = -EPROTO; 37441da177e4SLinus Torvalds break; 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds if (r == 0) 37471da177e4SLinus Torvalds break; 37481da177e4SLinus Torvalds } 37491da177e4SLinus Torvalds udev->descriptor.bMaxPacketSize0 = 37501da177e4SLinus Torvalds buf->bMaxPacketSize0; 37511da177e4SLinus Torvalds kfree(buf); 37521da177e4SLinus Torvalds 375375d7cf72SAndiry Xu retval = hub_port_reset(hub, port1, udev, delay, false); 37541da177e4SLinus Torvalds if (retval < 0) /* error or disconnect */ 37551da177e4SLinus Torvalds goto fail; 37561da177e4SLinus Torvalds if (oldspeed != udev->speed) { 37571da177e4SLinus Torvalds dev_dbg(&udev->dev, 37581da177e4SLinus Torvalds "device reset changed speed!\n"); 37591da177e4SLinus Torvalds retval = -ENODEV; 37601da177e4SLinus Torvalds goto fail; 37611da177e4SLinus Torvalds } 37621da177e4SLinus Torvalds if (r) { 3763b9cef6c3SWu Fengguang dev_err(&udev->dev, 3764b9cef6c3SWu Fengguang "device descriptor read/64, error %d\n", 3765b9cef6c3SWu Fengguang r); 37661da177e4SLinus Torvalds retval = -EMSGSIZE; 37671da177e4SLinus Torvalds continue; 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE 37701da177e4SLinus Torvalds } 37711da177e4SLinus Torvalds 37726c529cdcSInaky Perez-Gonzalez /* 37736c529cdcSInaky Perez-Gonzalez * If device is WUSB, we already assigned an 37746c529cdcSInaky Perez-Gonzalez * unauthorized address in the Connect Ack sequence; 37756c529cdcSInaky Perez-Gonzalez * authorization will assign the final address. 37766c529cdcSInaky Perez-Gonzalez */ 37776c529cdcSInaky Perez-Gonzalez if (udev->wusb == 0) { 37781da177e4SLinus Torvalds for (j = 0; j < SET_ADDRESS_TRIES; ++j) { 37794326ed0bSAlan Stern retval = hub_set_address(udev, devnum); 37801da177e4SLinus Torvalds if (retval >= 0) 37811da177e4SLinus Torvalds break; 37821da177e4SLinus Torvalds msleep(200); 37831da177e4SLinus Torvalds } 37841da177e4SLinus Torvalds if (retval < 0) { 37851da177e4SLinus Torvalds dev_err(&udev->dev, 37861da177e4SLinus Torvalds "device not accepting address %d, error %d\n", 37874326ed0bSAlan Stern devnum, retval); 37881da177e4SLinus Torvalds goto fail; 37891da177e4SLinus Torvalds } 3790c6515272SSarah Sharp if (udev->speed == USB_SPEED_SUPER) { 3791c6515272SSarah Sharp devnum = udev->devnum; 3792c6515272SSarah Sharp dev_info(&udev->dev, 37933b29b68bSAlan Stern "%s SuperSpeed USB device number %d using %s\n", 3794c6515272SSarah Sharp (udev->config) ? "reset" : "new", 37953b29b68bSAlan Stern devnum, udev->bus->controller->driver->name); 3796c6515272SSarah Sharp } 37971da177e4SLinus Torvalds 37981da177e4SLinus Torvalds /* cope with hardware quirkiness: 37991da177e4SLinus Torvalds * - let SET_ADDRESS settle, some device hardware wants it 38001da177e4SLinus Torvalds * - read ep0 maxpacket even for high and low speed, 38011da177e4SLinus Torvalds */ 38021da177e4SLinus Torvalds msleep(10); 3803c6515272SSarah Sharp if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) 38041da177e4SLinus Torvalds break; 38056c529cdcSInaky Perez-Gonzalez } 38061da177e4SLinus Torvalds 38071da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, 8); 38081da177e4SLinus Torvalds if (retval < 8) { 3809b9cef6c3SWu Fengguang dev_err(&udev->dev, 3810b9cef6c3SWu Fengguang "device descriptor read/8, error %d\n", 3811b9cef6c3SWu Fengguang retval); 38121da177e4SLinus Torvalds if (retval >= 0) 38131da177e4SLinus Torvalds retval = -EMSGSIZE; 38141da177e4SLinus Torvalds } else { 38151da177e4SLinus Torvalds retval = 0; 38161da177e4SLinus Torvalds break; 38171da177e4SLinus Torvalds } 38181da177e4SLinus Torvalds } 38191da177e4SLinus Torvalds if (retval) 38201da177e4SLinus Torvalds goto fail; 38211da177e4SLinus Torvalds 3822d8aec3dbSElric Fu /* 3823d8aec3dbSElric Fu * Some superspeed devices have finished the link training process 3824d8aec3dbSElric Fu * and attached to a superspeed hub port, but the device descriptor 3825d8aec3dbSElric Fu * got from those devices show they aren't superspeed devices. Warm 3826d8aec3dbSElric Fu * reset the port attached by the devices can fix them. 3827d8aec3dbSElric Fu */ 3828d8aec3dbSElric Fu if ((udev->speed == USB_SPEED_SUPER) && 3829d8aec3dbSElric Fu (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 3830d8aec3dbSElric Fu dev_err(&udev->dev, "got a wrong device descriptor, " 3831d8aec3dbSElric Fu "warm reset device\n"); 3832d8aec3dbSElric Fu hub_port_reset(hub, port1, udev, 3833d8aec3dbSElric Fu HUB_BH_RESET_TIME, true); 3834d8aec3dbSElric Fu retval = -EINVAL; 3835d8aec3dbSElric Fu goto fail; 3836d8aec3dbSElric Fu } 3837d8aec3dbSElric Fu 38386b403b02SSarah Sharp if (udev->descriptor.bMaxPacketSize0 == 0xff || 38396b403b02SSarah Sharp udev->speed == USB_SPEED_SUPER) 38406b403b02SSarah Sharp i = 512; 38416b403b02SSarah Sharp else 38426b403b02SSarah Sharp i = udev->descriptor.bMaxPacketSize0; 384329cc8897SKuninori Morimoto if (usb_endpoint_maxp(&udev->ep0.desc) != i) { 384456626a72SAlan Stern if (udev->speed == USB_SPEED_LOW || 38451da177e4SLinus Torvalds !(i == 8 || i == 16 || i == 32 || i == 64)) { 384656626a72SAlan Stern dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); 38471da177e4SLinus Torvalds retval = -EMSGSIZE; 38481da177e4SLinus Torvalds goto fail; 38491da177e4SLinus Torvalds } 385056626a72SAlan Stern if (udev->speed == USB_SPEED_FULL) 38511da177e4SLinus Torvalds dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 385256626a72SAlan Stern else 385356626a72SAlan Stern dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 38541da177e4SLinus Torvalds udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 3855fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 38561da177e4SLinus Torvalds } 38571da177e4SLinus Torvalds 38581da177e4SLinus Torvalds retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 38591da177e4SLinus Torvalds if (retval < (signed)sizeof(udev->descriptor)) { 3860b9cef6c3SWu Fengguang dev_err(&udev->dev, "device descriptor read/all, error %d\n", 3861b9cef6c3SWu Fengguang retval); 38621da177e4SLinus Torvalds if (retval >= 0) 38631da177e4SLinus Torvalds retval = -ENOMSG; 38641da177e4SLinus Torvalds goto fail; 38651da177e4SLinus Torvalds } 38661da177e4SLinus Torvalds 38671ff4df56SAndiry Xu if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 38681ff4df56SAndiry Xu retval = usb_get_bos_descriptor(udev); 386951e0a012SSarah Sharp if (!retval) { 3870d9b2099cSSarah Sharp udev->lpm_capable = usb_device_supports_lpm(udev); 387151e0a012SSarah Sharp usb_set_lpm_parameters(udev); 387251e0a012SSarah Sharp } 38731ff4df56SAndiry Xu } 38743148bf04SAndiry Xu 38751da177e4SLinus Torvalds retval = 0; 387648f24970SAlek Du /* notify HCD that we have a device connected and addressed */ 387748f24970SAlek Du if (hcd->driver->update_device) 387848f24970SAlek Du hcd->driver->update_device(hcd, udev); 38791da177e4SLinus Torvalds fail: 38804326ed0bSAlan Stern if (retval) { 38811da177e4SLinus Torvalds hub_port_disable(hub, port1, 0); 38823b29b68bSAlan Stern update_devnum(udev, devnum); /* for disconnect processing */ 38834326ed0bSAlan Stern } 38844186ecf8SArjan van de Ven mutex_unlock(&usb_address0_mutex); 38851da177e4SLinus Torvalds return retval; 38861da177e4SLinus Torvalds } 38871da177e4SLinus Torvalds 38881da177e4SLinus Torvalds static void 38891da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) 38901da177e4SLinus Torvalds { 38911da177e4SLinus Torvalds struct usb_qualifier_descriptor *qual; 38921da177e4SLinus Torvalds int status; 38931da177e4SLinus Torvalds 3894e94b1766SChristoph Lameter qual = kmalloc (sizeof *qual, GFP_KERNEL); 38951da177e4SLinus Torvalds if (qual == NULL) 38961da177e4SLinus Torvalds return; 38971da177e4SLinus Torvalds 38981da177e4SLinus Torvalds status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, 38991da177e4SLinus Torvalds qual, sizeof *qual); 39001da177e4SLinus Torvalds if (status == sizeof *qual) { 39011da177e4SLinus Torvalds dev_info(&udev->dev, "not running at top speed; " 39021da177e4SLinus Torvalds "connect to a high speed hub\n"); 39031da177e4SLinus Torvalds /* hub LEDs are probably harder to miss than syslog */ 39041da177e4SLinus Torvalds if (hub->has_indicators) { 39051da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 3906c4028958SDavid Howells schedule_delayed_work (&hub->leds, 0); 39071da177e4SLinus Torvalds } 39081da177e4SLinus Torvalds } 39091da177e4SLinus Torvalds kfree(qual); 39101da177e4SLinus Torvalds } 39111da177e4SLinus Torvalds 39121da177e4SLinus Torvalds static unsigned 39131da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub) 39141da177e4SLinus Torvalds { 39151da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 39161da177e4SLinus Torvalds int remaining; 391755c52718SAlan Stern int port1; 39181da177e4SLinus Torvalds 391955c52718SAlan Stern if (!hub->limited_power) 39201da177e4SLinus Torvalds return 0; 39211da177e4SLinus Torvalds 392255c52718SAlan Stern remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 392355c52718SAlan Stern for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 3924fa286188SGreg Kroah-Hartman struct usb_device *udev = hdev->children[port1 - 1]; 392555c52718SAlan Stern int delta; 39261da177e4SLinus Torvalds 39271da177e4SLinus Torvalds if (!udev) 39281da177e4SLinus Torvalds continue; 39291da177e4SLinus Torvalds 393055c52718SAlan Stern /* Unconfigured devices may not use more than 100mA, 393155c52718SAlan Stern * or 8mA for OTG ports */ 39321da177e4SLinus Torvalds if (udev->actconfig) 393355c52718SAlan Stern delta = udev->actconfig->desc.bMaxPower * 2; 393455c52718SAlan Stern else if (port1 != udev->bus->otg_port || hdev->parent) 393555c52718SAlan Stern delta = 100; 39361da177e4SLinus Torvalds else 393755c52718SAlan Stern delta = 8; 393855c52718SAlan Stern if (delta > hub->mA_per_port) 3939b9cef6c3SWu Fengguang dev_warn(&udev->dev, 3940b9cef6c3SWu Fengguang "%dmA is over %umA budget for port %d!\n", 394155c52718SAlan Stern delta, hub->mA_per_port, port1); 39421da177e4SLinus Torvalds remaining -= delta; 39431da177e4SLinus Torvalds } 39441da177e4SLinus Torvalds if (remaining < 0) { 394555c52718SAlan Stern dev_warn(hub->intfdev, "%dmA over power budget!\n", 394655c52718SAlan Stern - remaining); 39471da177e4SLinus Torvalds remaining = 0; 39481da177e4SLinus Torvalds } 39491da177e4SLinus Torvalds return remaining; 39501da177e4SLinus Torvalds } 39511da177e4SLinus Torvalds 39521da177e4SLinus Torvalds /* Handle physical or logical connection change events. 39531da177e4SLinus Torvalds * This routine is called when: 39541da177e4SLinus Torvalds * a port connection-change occurs; 39551da177e4SLinus Torvalds * a port enable-change occurs (often caused by EMI); 3956742120c6SMing Lei * usb_reset_and_verify_device() encounters changed descriptors (as from 39571da177e4SLinus Torvalds * a firmware download) 39581da177e4SLinus Torvalds * caller already locked the hub 39591da177e4SLinus Torvalds */ 39601da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1, 39611da177e4SLinus Torvalds u16 portstatus, u16 portchange) 39621da177e4SLinus Torvalds { 39631da177e4SLinus Torvalds struct usb_device *hdev = hub->hdev; 39641da177e4SLinus Torvalds struct device *hub_dev = hub->intfdev; 396590da096eSBalaji Rao struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 396624618b0cSAlan Stern unsigned wHubCharacteristics = 396724618b0cSAlan Stern le16_to_cpu(hub->descriptor->wHubCharacteristics); 39688808f00cSAlan Stern struct usb_device *udev; 39691da177e4SLinus Torvalds int status, i; 39701da177e4SLinus Torvalds 39711da177e4SLinus Torvalds dev_dbg (hub_dev, 39721da177e4SLinus Torvalds "port %d, status %04x, change %04x, %s\n", 3973131dec34SSarah Sharp port1, portstatus, portchange, portspeed(hub, portstatus)); 39741da177e4SLinus Torvalds 39751da177e4SLinus Torvalds if (hub->has_indicators) { 39761da177e4SLinus Torvalds set_port_led(hub, port1, HUB_LED_AUTO); 39771da177e4SLinus Torvalds hub->indicator[port1-1] = INDICATOR_AUTO; 39781da177e4SLinus Torvalds } 39791da177e4SLinus Torvalds 39801da177e4SLinus Torvalds #ifdef CONFIG_USB_OTG 39811da177e4SLinus Torvalds /* during HNP, don't repeat the debounce */ 39821da177e4SLinus Torvalds if (hdev->bus->is_b_host) 398324618b0cSAlan Stern portchange &= ~(USB_PORT_STAT_C_CONNECTION | 398424618b0cSAlan Stern USB_PORT_STAT_C_ENABLE); 39851da177e4SLinus Torvalds #endif 39861da177e4SLinus Torvalds 39878808f00cSAlan Stern /* Try to resuscitate an existing device */ 3988fa286188SGreg Kroah-Hartman udev = hdev->children[port1-1]; 39898808f00cSAlan Stern if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 39908808f00cSAlan Stern udev->state != USB_STATE_NOTATTACHED) { 39918808f00cSAlan Stern usb_lock_device(udev); 39928808f00cSAlan Stern if (portstatus & USB_PORT_STAT_ENABLE) { 39938808f00cSAlan Stern status = 0; /* Nothing to do */ 39948808f00cSAlan Stern 39958808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND 39965257d97aSAlan Stern } else if (udev->state == USB_STATE_SUSPENDED && 39975257d97aSAlan Stern udev->persist_enabled) { 39988808f00cSAlan Stern /* For a suspended device, treat this as a 39998808f00cSAlan Stern * remote wakeup event. 40008808f00cSAlan Stern */ 40010534d468SAlan Stern status = usb_remote_wakeup(udev); 40028808f00cSAlan Stern #endif 40038808f00cSAlan Stern 40048808f00cSAlan Stern } else { 40055257d97aSAlan Stern status = -ENODEV; /* Don't resuscitate */ 40068808f00cSAlan Stern } 40078808f00cSAlan Stern usb_unlock_device(udev); 40088808f00cSAlan Stern 40098808f00cSAlan Stern if (status == 0) { 40108808f00cSAlan Stern clear_bit(port1, hub->change_bits); 40118808f00cSAlan Stern return; 40128808f00cSAlan Stern } 40138808f00cSAlan Stern } 40148808f00cSAlan Stern 401524618b0cSAlan Stern /* Disconnect any existing devices under this port */ 40168808f00cSAlan Stern if (udev) 4017fa286188SGreg Kroah-Hartman usb_disconnect(&hdev->children[port1-1]); 401824618b0cSAlan Stern clear_bit(port1, hub->change_bits); 401924618b0cSAlan Stern 4020253e0572SAlan Stern /* We can forget about a "removed" device when there's a physical 4021253e0572SAlan Stern * disconnect or the connect status changes. 4022253e0572SAlan Stern */ 4023253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 4024253e0572SAlan Stern (portchange & USB_PORT_STAT_C_CONNECTION)) 4025253e0572SAlan Stern clear_bit(port1, hub->removed_bits); 4026253e0572SAlan Stern 40275257d97aSAlan Stern if (portchange & (USB_PORT_STAT_C_CONNECTION | 40285257d97aSAlan Stern USB_PORT_STAT_C_ENABLE)) { 40295257d97aSAlan Stern status = hub_port_debounce(hub, port1); 40305257d97aSAlan Stern if (status < 0) { 40315257d97aSAlan Stern if (printk_ratelimit()) 40325257d97aSAlan Stern dev_err(hub_dev, "connect-debounce failed, " 40335257d97aSAlan Stern "port %d disabled\n", port1); 40345257d97aSAlan Stern portstatus &= ~USB_PORT_STAT_CONNECTION; 40355257d97aSAlan Stern } else { 40365257d97aSAlan Stern portstatus = status; 40375257d97aSAlan Stern } 40385257d97aSAlan Stern } 40395257d97aSAlan Stern 4040253e0572SAlan Stern /* Return now if debouncing failed or nothing is connected or 4041253e0572SAlan Stern * the device was "removed". 4042253e0572SAlan Stern */ 4043253e0572SAlan Stern if (!(portstatus & USB_PORT_STAT_CONNECTION) || 4044253e0572SAlan Stern test_bit(port1, hub->removed_bits)) { 40451da177e4SLinus Torvalds 40461da177e4SLinus Torvalds /* maybe switch power back on (e.g. root hub was reset) */ 404774ad9bd2SGreg Kroah-Hartman if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 40480ed9a57eSAndiry Xu && !port_is_power_on(hub, portstatus)) 40491da177e4SLinus Torvalds set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 40501da177e4SLinus Torvalds 40511da177e4SLinus Torvalds if (portstatus & USB_PORT_STAT_ENABLE) 40521da177e4SLinus Torvalds goto done; 40531da177e4SLinus Torvalds return; 40541da177e4SLinus Torvalds } 40551da177e4SLinus Torvalds 40561da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; i++) { 40571da177e4SLinus Torvalds 40581da177e4SLinus Torvalds /* reallocate for each attempt, since references 40591da177e4SLinus Torvalds * to the previous one can escape in various ways 40601da177e4SLinus Torvalds */ 40611da177e4SLinus Torvalds udev = usb_alloc_dev(hdev, hdev->bus, port1); 40621da177e4SLinus Torvalds if (!udev) { 40631da177e4SLinus Torvalds dev_err (hub_dev, 40641da177e4SLinus Torvalds "couldn't allocate port %d usb_device\n", 40651da177e4SLinus Torvalds port1); 40661da177e4SLinus Torvalds goto done; 40671da177e4SLinus Torvalds } 40681da177e4SLinus Torvalds 40691da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_POWERED); 407055c52718SAlan Stern udev->bus_mA = hub->mA_per_port; 4071b6956ffaSAlan Stern udev->level = hdev->level + 1; 40728af548dcSInaky Perez-Gonzalez udev->wusb = hub_is_wusb(hub); 40731da177e4SLinus Torvalds 4074131dec34SSarah Sharp /* Only USB 3.0 devices are connected to SuperSpeed hubs. */ 4075131dec34SSarah Sharp if (hub_is_superspeed(hub->hdev)) 4076e7b77172SSarah Sharp udev->speed = USB_SPEED_SUPER; 4077e7b77172SSarah Sharp else 4078e7b77172SSarah Sharp udev->speed = USB_SPEED_UNKNOWN; 4079e7b77172SSarah Sharp 40803b29b68bSAlan Stern choose_devnum(udev); 4081c6515272SSarah Sharp if (udev->devnum <= 0) { 4082c6515272SSarah Sharp status = -ENOTCONN; /* Don't retry */ 4083c6515272SSarah Sharp goto loop; 4084c6515272SSarah Sharp } 4085c6515272SSarah Sharp 4086e7b77172SSarah Sharp /* reset (non-USB 3.0 devices) and get descriptor */ 40871da177e4SLinus Torvalds status = hub_port_init(hub, udev, port1, i); 40881da177e4SLinus Torvalds if (status < 0) 40891da177e4SLinus Torvalds goto loop; 40901da177e4SLinus Torvalds 409193362a87SPhil Dibowitz usb_detect_quirks(udev); 409293362a87SPhil Dibowitz if (udev->quirks & USB_QUIRK_DELAY_INIT) 409393362a87SPhil Dibowitz msleep(1000); 409493362a87SPhil Dibowitz 40951da177e4SLinus Torvalds /* consecutive bus-powered hubs aren't reliable; they can 40961da177e4SLinus Torvalds * violate the voltage drop budget. if the new child has 40971da177e4SLinus Torvalds * a "powered" LED, users should notice we didn't enable it 40981da177e4SLinus Torvalds * (without reading syslog), even without per-port LEDs 40991da177e4SLinus Torvalds * on the parent. 41001da177e4SLinus Torvalds */ 41011da177e4SLinus Torvalds if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 410255c52718SAlan Stern && udev->bus_mA <= 100) { 41031da177e4SLinus Torvalds u16 devstat; 41041da177e4SLinus Torvalds 41051da177e4SLinus Torvalds status = usb_get_status(udev, USB_RECIP_DEVICE, 0, 41061da177e4SLinus Torvalds &devstat); 410755c52718SAlan Stern if (status < 2) { 41081da177e4SLinus Torvalds dev_dbg(&udev->dev, "get status %d ?\n", status); 41091da177e4SLinus Torvalds goto loop_disable; 41101da177e4SLinus Torvalds } 411155c52718SAlan Stern le16_to_cpus(&devstat); 41121da177e4SLinus Torvalds if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 41131da177e4SLinus Torvalds dev_err(&udev->dev, 41141da177e4SLinus Torvalds "can't connect bus-powered hub " 41151da177e4SLinus Torvalds "to this port\n"); 41161da177e4SLinus Torvalds if (hub->has_indicators) { 41171da177e4SLinus Torvalds hub->indicator[port1-1] = 41181da177e4SLinus Torvalds INDICATOR_AMBER_BLINK; 4119c4028958SDavid Howells schedule_delayed_work (&hub->leds, 0); 41201da177e4SLinus Torvalds } 41211da177e4SLinus Torvalds status = -ENOTCONN; /* Don't retry */ 41221da177e4SLinus Torvalds goto loop_disable; 41231da177e4SLinus Torvalds } 41241da177e4SLinus Torvalds } 41251da177e4SLinus Torvalds 41261da177e4SLinus Torvalds /* check for devices running slower than they could */ 41271da177e4SLinus Torvalds if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 41281da177e4SLinus Torvalds && udev->speed == USB_SPEED_FULL 41291da177e4SLinus Torvalds && highspeed_hubs != 0) 41301da177e4SLinus Torvalds check_highspeed (hub, udev, port1); 41311da177e4SLinus Torvalds 4132fa286188SGreg Kroah-Hartman /* Store the parent's children[] pointer. At this point 41331da177e4SLinus Torvalds * udev becomes globally accessible, although presumably 41341da177e4SLinus Torvalds * no one will look at it until hdev is unlocked. 41351da177e4SLinus Torvalds */ 41361da177e4SLinus Torvalds status = 0; 41371da177e4SLinus Torvalds 41381da177e4SLinus Torvalds /* We mustn't add new devices if the parent hub has 41391da177e4SLinus Torvalds * been disconnected; we would race with the 41401da177e4SLinus Torvalds * recursively_mark_NOTATTACHED() routine. 41411da177e4SLinus Torvalds */ 41421da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 41431da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) 41441da177e4SLinus Torvalds status = -ENOTCONN; 41451da177e4SLinus Torvalds else 4146fa286188SGreg Kroah-Hartman hdev->children[port1-1] = udev; 41471da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 41481da177e4SLinus Torvalds 41491da177e4SLinus Torvalds /* Run it through the hoops (find a driver, etc) */ 41501da177e4SLinus Torvalds if (!status) { 41511da177e4SLinus Torvalds status = usb_new_device(udev); 41521da177e4SLinus Torvalds if (status) { 41531da177e4SLinus Torvalds spin_lock_irq(&device_state_lock); 4154fa286188SGreg Kroah-Hartman hdev->children[port1-1] = NULL; 41551da177e4SLinus Torvalds spin_unlock_irq(&device_state_lock); 41561da177e4SLinus Torvalds } 41571da177e4SLinus Torvalds } 41581da177e4SLinus Torvalds 41591da177e4SLinus Torvalds if (status) 41601da177e4SLinus Torvalds goto loop_disable; 41611da177e4SLinus Torvalds 41621da177e4SLinus Torvalds status = hub_power_remaining(hub); 41631da177e4SLinus Torvalds if (status) 416455c52718SAlan Stern dev_dbg(hub_dev, "%dmA power budget left\n", status); 41651da177e4SLinus Torvalds 41661da177e4SLinus Torvalds return; 41671da177e4SLinus Torvalds 41681da177e4SLinus Torvalds loop_disable: 41691da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 41701da177e4SLinus Torvalds loop: 4171fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 41723b29b68bSAlan Stern release_devnum(udev); 4173f7410cedSHerbert Xu hub_free_dev(udev); 41741da177e4SLinus Torvalds usb_put_dev(udev); 4175ffcdc18dSVikram Pandita if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 41761da177e4SLinus Torvalds break; 41771da177e4SLinus Torvalds } 41783a31155cSAlan Stern if (hub->hdev->parent || 41793a31155cSAlan Stern !hcd->driver->port_handed_over || 41803a31155cSAlan Stern !(hcd->driver->port_handed_over)(hcd, port1)) 41813a31155cSAlan Stern dev_err(hub_dev, "unable to enumerate USB device on port %d\n", 41823a31155cSAlan Stern port1); 41831da177e4SLinus Torvalds 41841da177e4SLinus Torvalds done: 41851da177e4SLinus Torvalds hub_port_disable(hub, port1, 1); 418690da096eSBalaji Rao if (hcd->driver->relinquish_port && !hub->hdev->parent) 418790da096eSBalaji Rao hcd->driver->relinquish_port(hcd, port1); 41881da177e4SLinus Torvalds } 41891da177e4SLinus Torvalds 4190714b07beSSarah Sharp /* Returns 1 if there was a remote wakeup and a connect status change. */ 4191714b07beSSarah Sharp static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 419272937e1eSSarah Sharp u16 portstatus, u16 portchange) 4193714b07beSSarah Sharp { 4194714b07beSSarah Sharp struct usb_device *hdev; 4195714b07beSSarah Sharp struct usb_device *udev; 4196714b07beSSarah Sharp int connect_change = 0; 4197714b07beSSarah Sharp int ret; 4198714b07beSSarah Sharp 4199714b07beSSarah Sharp hdev = hub->hdev; 4200fa286188SGreg Kroah-Hartman udev = hdev->children[port-1]; 42014ee823b8SSarah Sharp if (!hub_is_superspeed(hdev)) { 4202714b07beSSarah Sharp if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 4203714b07beSSarah Sharp return 0; 4204714b07beSSarah Sharp clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 42054ee823b8SSarah Sharp } else { 42064ee823b8SSarah Sharp if (!udev || udev->state != USB_STATE_SUSPENDED || 420772937e1eSSarah Sharp (portstatus & USB_PORT_STAT_LINK_STATE) != 420872937e1eSSarah Sharp USB_SS_PORT_LS_U0) 42094ee823b8SSarah Sharp return 0; 42104ee823b8SSarah Sharp } 4211714b07beSSarah Sharp 4212714b07beSSarah Sharp if (udev) { 4213714b07beSSarah Sharp /* TRSMRCY = 10 msec */ 4214714b07beSSarah Sharp msleep(10); 4215714b07beSSarah Sharp 4216714b07beSSarah Sharp usb_lock_device(udev); 4217714b07beSSarah Sharp ret = usb_remote_wakeup(udev); 4218714b07beSSarah Sharp usb_unlock_device(udev); 4219714b07beSSarah Sharp if (ret < 0) 4220714b07beSSarah Sharp connect_change = 1; 4221714b07beSSarah Sharp } else { 4222714b07beSSarah Sharp ret = -ENODEV; 4223714b07beSSarah Sharp hub_port_disable(hub, port, 1); 4224714b07beSSarah Sharp } 4225714b07beSSarah Sharp dev_dbg(hub->intfdev, "resume on port %d, status %d\n", 4226714b07beSSarah Sharp port, ret); 4227714b07beSSarah Sharp return connect_change; 4228714b07beSSarah Sharp } 4229714b07beSSarah Sharp 42301da177e4SLinus Torvalds static void hub_events(void) 42311da177e4SLinus Torvalds { 42321da177e4SLinus Torvalds struct list_head *tmp; 42331da177e4SLinus Torvalds struct usb_device *hdev; 42341da177e4SLinus Torvalds struct usb_interface *intf; 42351da177e4SLinus Torvalds struct usb_hub *hub; 42361da177e4SLinus Torvalds struct device *hub_dev; 42371da177e4SLinus Torvalds u16 hubstatus; 42381da177e4SLinus Torvalds u16 hubchange; 42391da177e4SLinus Torvalds u16 portstatus; 42401da177e4SLinus Torvalds u16 portchange; 42411da177e4SLinus Torvalds int i, ret; 42424ee823b8SSarah Sharp int connect_change, wakeup_change; 42431da177e4SLinus Torvalds 42441da177e4SLinus Torvalds /* 42451da177e4SLinus Torvalds * We restart the list every time to avoid a deadlock with 42461da177e4SLinus Torvalds * deleting hubs downstream from this one. This should be 42471da177e4SLinus Torvalds * safe since we delete the hub from the event list. 42481da177e4SLinus Torvalds * Not the most efficient, but avoids deadlocks. 42491da177e4SLinus Torvalds */ 42501da177e4SLinus Torvalds while (1) { 42511da177e4SLinus Torvalds 42521da177e4SLinus Torvalds /* Grab the first entry at the beginning of the list */ 42531da177e4SLinus Torvalds spin_lock_irq(&hub_event_lock); 42541da177e4SLinus Torvalds if (list_empty(&hub_event_list)) { 42551da177e4SLinus Torvalds spin_unlock_irq(&hub_event_lock); 42561da177e4SLinus Torvalds break; 42571da177e4SLinus Torvalds } 42581da177e4SLinus Torvalds 42591da177e4SLinus Torvalds tmp = hub_event_list.next; 42601da177e4SLinus Torvalds list_del_init(tmp); 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds hub = list_entry(tmp, struct usb_hub, event_list); 4263e8054854SAlan Stern kref_get(&hub->kref); 4264e8054854SAlan Stern spin_unlock_irq(&hub_event_lock); 42651da177e4SLinus Torvalds 4266e8054854SAlan Stern hdev = hub->hdev; 4267e8054854SAlan Stern hub_dev = hub->intfdev; 4268e8054854SAlan Stern intf = to_usb_interface(hub_dev); 426940f122f3SAlan Stern dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 42701da177e4SLinus Torvalds hdev->state, hub->descriptor 42711da177e4SLinus Torvalds ? hub->descriptor->bNbrPorts 42721da177e4SLinus Torvalds : 0, 42731da177e4SLinus Torvalds /* NOTE: expects max 15 ports... */ 42741da177e4SLinus Torvalds (u16) hub->change_bits[0], 427540f122f3SAlan Stern (u16) hub->event_bits[0]); 42761da177e4SLinus Torvalds 42771da177e4SLinus Torvalds /* Lock the device, then check to see if we were 42781da177e4SLinus Torvalds * disconnected while waiting for the lock to succeed. */ 427906b84e8aSAlan Stern usb_lock_device(hdev); 4280e8054854SAlan Stern if (unlikely(hub->disconnected)) 42819bbdf1e0SAlan Stern goto loop_disconnected; 42821da177e4SLinus Torvalds 42831da177e4SLinus Torvalds /* If the hub has died, clean up after it */ 42841da177e4SLinus Torvalds if (hdev->state == USB_STATE_NOTATTACHED) { 42857de18d8bSAlan Stern hub->error = -ENODEV; 42864330354fSAlan Stern hub_quiesce(hub, HUB_DISCONNECT); 42871da177e4SLinus Torvalds goto loop; 42881da177e4SLinus Torvalds } 42891da177e4SLinus Torvalds 429040f122f3SAlan Stern /* Autoresume */ 429140f122f3SAlan Stern ret = usb_autopm_get_interface(intf); 429240f122f3SAlan Stern if (ret) { 429340f122f3SAlan Stern dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 42941da177e4SLinus Torvalds goto loop; 429540f122f3SAlan Stern } 429640f122f3SAlan Stern 429740f122f3SAlan Stern /* If this is an inactive hub, do nothing */ 429840f122f3SAlan Stern if (hub->quiescing) 429940f122f3SAlan Stern goto loop_autopm; 43001da177e4SLinus Torvalds 43011da177e4SLinus Torvalds if (hub->error) { 43021da177e4SLinus Torvalds dev_dbg (hub_dev, "resetting for error %d\n", 43031da177e4SLinus Torvalds hub->error); 43041da177e4SLinus Torvalds 4305742120c6SMing Lei ret = usb_reset_device(hdev); 43061da177e4SLinus Torvalds if (ret) { 43071da177e4SLinus Torvalds dev_dbg (hub_dev, 43081da177e4SLinus Torvalds "error resetting hub: %d\n", ret); 430940f122f3SAlan Stern goto loop_autopm; 43101da177e4SLinus Torvalds } 43111da177e4SLinus Torvalds 43121da177e4SLinus Torvalds hub->nerrors = 0; 43131da177e4SLinus Torvalds hub->error = 0; 43141da177e4SLinus Torvalds } 43151da177e4SLinus Torvalds 43161da177e4SLinus Torvalds /* deal with port status changes */ 43171da177e4SLinus Torvalds for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { 43181da177e4SLinus Torvalds if (test_bit(i, hub->busy_bits)) 43191da177e4SLinus Torvalds continue; 43201da177e4SLinus Torvalds connect_change = test_bit(i, hub->change_bits); 432172937e1eSSarah Sharp wakeup_change = test_and_clear_bit(i, hub->wakeup_bits); 43221da177e4SLinus Torvalds if (!test_and_clear_bit(i, hub->event_bits) && 43234ee823b8SSarah Sharp !connect_change && !wakeup_change) 43241da177e4SLinus Torvalds continue; 43251da177e4SLinus Torvalds 43261da177e4SLinus Torvalds ret = hub_port_status(hub, i, 43271da177e4SLinus Torvalds &portstatus, &portchange); 43281da177e4SLinus Torvalds if (ret < 0) 43291da177e4SLinus Torvalds continue; 43301da177e4SLinus Torvalds 43311da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_CONNECTION) { 43321da177e4SLinus Torvalds clear_port_feature(hdev, i, 43331da177e4SLinus Torvalds USB_PORT_FEAT_C_CONNECTION); 43341da177e4SLinus Torvalds connect_change = 1; 43351da177e4SLinus Torvalds } 43361da177e4SLinus Torvalds 43371da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_ENABLE) { 43381da177e4SLinus Torvalds if (!connect_change) 43391da177e4SLinus Torvalds dev_dbg (hub_dev, 43401da177e4SLinus Torvalds "port %d enable change, " 43411da177e4SLinus Torvalds "status %08x\n", 43421da177e4SLinus Torvalds i, portstatus); 43431da177e4SLinus Torvalds clear_port_feature(hdev, i, 43441da177e4SLinus Torvalds USB_PORT_FEAT_C_ENABLE); 43451da177e4SLinus Torvalds 43461da177e4SLinus Torvalds /* 43471da177e4SLinus Torvalds * EM interference sometimes causes badly 43481da177e4SLinus Torvalds * shielded USB devices to be shutdown by 43491da177e4SLinus Torvalds * the hub, this hack enables them again. 43501da177e4SLinus Torvalds * Works at least with mouse driver. 43511da177e4SLinus Torvalds */ 43521da177e4SLinus Torvalds if (!(portstatus & USB_PORT_STAT_ENABLE) 43531da177e4SLinus Torvalds && !connect_change 4354fa286188SGreg Kroah-Hartman && hdev->children[i-1]) { 43551da177e4SLinus Torvalds dev_err (hub_dev, 43561da177e4SLinus Torvalds "port %i " 43571da177e4SLinus Torvalds "disabled by hub (EMI?), " 43581da177e4SLinus Torvalds "re-enabling...\n", 43591da177e4SLinus Torvalds i); 43601da177e4SLinus Torvalds connect_change = 1; 43611da177e4SLinus Torvalds } 43621da177e4SLinus Torvalds } 43631da177e4SLinus Torvalds 436472937e1eSSarah Sharp if (hub_handle_remote_wakeup(hub, i, 436572937e1eSSarah Sharp portstatus, portchange)) 43661da177e4SLinus Torvalds connect_change = 1; 43671da177e4SLinus Torvalds 43681da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 4369752d57a8SPaul Bolle u16 status = 0; 4370752d57a8SPaul Bolle u16 unused; 4371752d57a8SPaul Bolle 4372752d57a8SPaul Bolle dev_dbg(hub_dev, "over-current change on port " 4373752d57a8SPaul Bolle "%d\n", i); 43741da177e4SLinus Torvalds clear_port_feature(hdev, i, 43751da177e4SLinus Torvalds USB_PORT_FEAT_C_OVER_CURRENT); 4376752d57a8SPaul Bolle msleep(100); /* Cool down */ 43778520f380SAlan Stern hub_power_on(hub, true); 4378752d57a8SPaul Bolle hub_port_status(hub, i, &status, &unused); 4379752d57a8SPaul Bolle if (status & USB_PORT_STAT_OVERCURRENT) 4380752d57a8SPaul Bolle dev_err(hub_dev, "over-current " 4381752d57a8SPaul Bolle "condition on port %d\n", i); 43821da177e4SLinus Torvalds } 43831da177e4SLinus Torvalds 43841da177e4SLinus Torvalds if (portchange & USB_PORT_STAT_C_RESET) { 43851da177e4SLinus Torvalds dev_dbg (hub_dev, 43861da177e4SLinus Torvalds "reset change on port %d\n", 43871da177e4SLinus Torvalds i); 43881da177e4SLinus Torvalds clear_port_feature(hdev, i, 43891da177e4SLinus Torvalds USB_PORT_FEAT_C_RESET); 43901da177e4SLinus Torvalds } 4391c7061574SSarah Sharp if ((portchange & USB_PORT_STAT_C_BH_RESET) && 4392c7061574SSarah Sharp hub_is_superspeed(hub->hdev)) { 4393c7061574SSarah Sharp dev_dbg(hub_dev, 4394c7061574SSarah Sharp "warm reset change on port %d\n", 4395c7061574SSarah Sharp i); 4396c7061574SSarah Sharp clear_port_feature(hdev, i, 4397c7061574SSarah Sharp USB_PORT_FEAT_C_BH_PORT_RESET); 4398c7061574SSarah Sharp } 4399dbe79bbeSJohn Youn if (portchange & USB_PORT_STAT_C_LINK_STATE) { 4400dbe79bbeSJohn Youn clear_port_feature(hub->hdev, i, 4401dbe79bbeSJohn Youn USB_PORT_FEAT_C_PORT_LINK_STATE); 4402dbe79bbeSJohn Youn } 4403dbe79bbeSJohn Youn if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 4404dbe79bbeSJohn Youn dev_warn(hub_dev, 4405dbe79bbeSJohn Youn "config error on port %d\n", 4406dbe79bbeSJohn Youn i); 4407dbe79bbeSJohn Youn clear_port_feature(hub->hdev, i, 4408dbe79bbeSJohn Youn USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 4409dbe79bbeSJohn Youn } 44101da177e4SLinus Torvalds 44115e467f6eSAndiry Xu /* Warm reset a USB3 protocol port if it's in 44125e467f6eSAndiry Xu * SS.Inactive state. 44135e467f6eSAndiry Xu */ 44145e467f6eSAndiry Xu if (hub_is_superspeed(hub->hdev) && 44155e467f6eSAndiry Xu (portstatus & USB_PORT_STAT_LINK_STATE) 44165e467f6eSAndiry Xu == USB_SS_PORT_LS_SS_INACTIVE) { 44175e467f6eSAndiry Xu dev_dbg(hub_dev, "warm reset port %d\n", i); 441875d7cf72SAndiry Xu hub_port_reset(hub, i, NULL, 441975d7cf72SAndiry Xu HUB_BH_RESET_TIME, true); 44205e467f6eSAndiry Xu } 44215e467f6eSAndiry Xu 44221da177e4SLinus Torvalds if (connect_change) 44231da177e4SLinus Torvalds hub_port_connect_change(hub, i, 44241da177e4SLinus Torvalds portstatus, portchange); 44251da177e4SLinus Torvalds } /* end for i */ 44261da177e4SLinus Torvalds 44271da177e4SLinus Torvalds /* deal with hub status changes */ 44281da177e4SLinus Torvalds if (test_and_clear_bit(0, hub->event_bits) == 0) 44291da177e4SLinus Torvalds ; /* do nothing */ 44301da177e4SLinus Torvalds else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 44311da177e4SLinus Torvalds dev_err (hub_dev, "get_hub_status failed\n"); 44321da177e4SLinus Torvalds else { 44331da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_LOCAL_POWER) { 44341da177e4SLinus Torvalds dev_dbg (hub_dev, "power change\n"); 44351da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 443655c52718SAlan Stern if (hubstatus & HUB_STATUS_LOCAL_POWER) 443755c52718SAlan Stern /* FIXME: Is this always true? */ 443855c52718SAlan Stern hub->limited_power = 1; 4439403fae78Sjidong xiao else 4440403fae78Sjidong xiao hub->limited_power = 0; 44411da177e4SLinus Torvalds } 44421da177e4SLinus Torvalds if (hubchange & HUB_CHANGE_OVERCURRENT) { 4443752d57a8SPaul Bolle u16 status = 0; 4444752d57a8SPaul Bolle u16 unused; 4445752d57a8SPaul Bolle 4446752d57a8SPaul Bolle dev_dbg(hub_dev, "over-current change\n"); 44471da177e4SLinus Torvalds clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 4448752d57a8SPaul Bolle msleep(500); /* Cool down */ 44498520f380SAlan Stern hub_power_on(hub, true); 4450752d57a8SPaul Bolle hub_hub_status(hub, &status, &unused); 4451752d57a8SPaul Bolle if (status & HUB_STATUS_OVERCURRENT) 4452752d57a8SPaul Bolle dev_err(hub_dev, "over-current " 4453752d57a8SPaul Bolle "condition\n"); 44541da177e4SLinus Torvalds } 44551da177e4SLinus Torvalds } 44561da177e4SLinus Torvalds 445740f122f3SAlan Stern loop_autopm: 44588e4ceb38SAlan Stern /* Balance the usb_autopm_get_interface() above */ 44598e4ceb38SAlan Stern usb_autopm_put_interface_no_suspend(intf); 44601da177e4SLinus Torvalds loop: 44618e4ceb38SAlan Stern /* Balance the usb_autopm_get_interface_no_resume() in 44628e4ceb38SAlan Stern * kick_khubd() and allow autosuspend. 44638e4ceb38SAlan Stern */ 44648e4ceb38SAlan Stern usb_autopm_put_interface(intf); 44659bbdf1e0SAlan Stern loop_disconnected: 44661da177e4SLinus Torvalds usb_unlock_device(hdev); 4467e8054854SAlan Stern kref_put(&hub->kref, hub_release); 44681da177e4SLinus Torvalds 44691da177e4SLinus Torvalds } /* end while (1) */ 44701da177e4SLinus Torvalds } 44711da177e4SLinus Torvalds 44721da177e4SLinus Torvalds static int hub_thread(void *__unused) 44731da177e4SLinus Torvalds { 44743bb1af52SAlan Stern /* khubd needs to be freezable to avoid intefering with USB-PERSIST 44753bb1af52SAlan Stern * port handover. Otherwise it might see that a full-speed device 44763bb1af52SAlan Stern * was gone before the EHCI controller had handed its port over to 44773bb1af52SAlan Stern * the companion full-speed controller. 44783bb1af52SAlan Stern */ 447983144186SRafael J. Wysocki set_freezable(); 44803bb1af52SAlan Stern 44811da177e4SLinus Torvalds do { 44821da177e4SLinus Torvalds hub_events(); 4483e42837bcSRafael J. Wysocki wait_event_freezable(khubd_wait, 44849c8d6178Sakpm@osdl.org !list_empty(&hub_event_list) || 44859c8d6178Sakpm@osdl.org kthread_should_stop()); 44869c8d6178Sakpm@osdl.org } while (!kthread_should_stop() || !list_empty(&hub_event_list)); 44871da177e4SLinus Torvalds 44881da177e4SLinus Torvalds pr_debug("%s: khubd exiting\n", usbcore_name); 44899c8d6178Sakpm@osdl.org return 0; 44901da177e4SLinus Torvalds } 44911da177e4SLinus Torvalds 44921e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = { 44931da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 44941da177e4SLinus Torvalds .bDeviceClass = USB_CLASS_HUB}, 44951da177e4SLinus Torvalds { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 44961da177e4SLinus Torvalds .bInterfaceClass = USB_CLASS_HUB}, 44971da177e4SLinus Torvalds { } /* Terminating entry */ 44981da177e4SLinus Torvalds }; 44991da177e4SLinus Torvalds 45001da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table); 45011da177e4SLinus Torvalds 45021da177e4SLinus Torvalds static struct usb_driver hub_driver = { 45031da177e4SLinus Torvalds .name = "hub", 45041da177e4SLinus Torvalds .probe = hub_probe, 45051da177e4SLinus Torvalds .disconnect = hub_disconnect, 45061da177e4SLinus Torvalds .suspend = hub_suspend, 45071da177e4SLinus Torvalds .resume = hub_resume, 4508f07600cfSAlan Stern .reset_resume = hub_reset_resume, 45097de18d8bSAlan Stern .pre_reset = hub_pre_reset, 45107de18d8bSAlan Stern .post_reset = hub_post_reset, 4511c532b29aSAndi Kleen .unlocked_ioctl = hub_ioctl, 45121da177e4SLinus Torvalds .id_table = hub_id_table, 451340f122f3SAlan Stern .supports_autosuspend = 1, 45141da177e4SLinus Torvalds }; 45151da177e4SLinus Torvalds 45161da177e4SLinus Torvalds int usb_hub_init(void) 45171da177e4SLinus Torvalds { 45181da177e4SLinus Torvalds if (usb_register(&hub_driver) < 0) { 45191da177e4SLinus Torvalds printk(KERN_ERR "%s: can't register hub driver\n", 45201da177e4SLinus Torvalds usbcore_name); 45211da177e4SLinus Torvalds return -1; 45221da177e4SLinus Torvalds } 45231da177e4SLinus Torvalds 45249c8d6178Sakpm@osdl.org khubd_task = kthread_run(hub_thread, NULL, "khubd"); 45259c8d6178Sakpm@osdl.org if (!IS_ERR(khubd_task)) 45261da177e4SLinus Torvalds return 0; 45271da177e4SLinus Torvalds 45281da177e4SLinus Torvalds /* Fall through if kernel_thread failed */ 45291da177e4SLinus Torvalds usb_deregister(&hub_driver); 45301da177e4SLinus Torvalds printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); 45311da177e4SLinus Torvalds 45321da177e4SLinus Torvalds return -1; 45331da177e4SLinus Torvalds } 45341da177e4SLinus Torvalds 45351da177e4SLinus Torvalds void usb_hub_cleanup(void) 45361da177e4SLinus Torvalds { 45379c8d6178Sakpm@osdl.org kthread_stop(khubd_task); 45381da177e4SLinus Torvalds 45391da177e4SLinus Torvalds /* 45401da177e4SLinus Torvalds * Hub resources are freed for us by usb_deregister. It calls 45411da177e4SLinus Torvalds * usb_driver_purge on every device which in turn calls that 45421da177e4SLinus Torvalds * devices disconnect function if it is using this driver. 45431da177e4SLinus Torvalds * The hub_disconnect function takes care of releasing the 45441da177e4SLinus Torvalds * individual hub resources. -greg 45451da177e4SLinus Torvalds */ 45461da177e4SLinus Torvalds usb_deregister(&hub_driver); 45471da177e4SLinus Torvalds } /* usb_hub_cleanup() */ 45481da177e4SLinus Torvalds 4549eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev, 4550eb764c4bSAlan Stern struct usb_device_descriptor *old_device_descriptor) 45511da177e4SLinus Torvalds { 4552eb764c4bSAlan Stern int changed = 0; 45531da177e4SLinus Torvalds unsigned index; 4554eb764c4bSAlan Stern unsigned serial_len = 0; 4555eb764c4bSAlan Stern unsigned len; 4556eb764c4bSAlan Stern unsigned old_length; 4557eb764c4bSAlan Stern int length; 4558eb764c4bSAlan Stern char *buf; 45591da177e4SLinus Torvalds 4560eb764c4bSAlan Stern if (memcmp(&udev->descriptor, old_device_descriptor, 4561eb764c4bSAlan Stern sizeof(*old_device_descriptor)) != 0) 4562eb764c4bSAlan Stern return 1; 4563eb764c4bSAlan Stern 4564eb764c4bSAlan Stern /* Since the idVendor, idProduct, and bcdDevice values in the 4565eb764c4bSAlan Stern * device descriptor haven't changed, we will assume the 4566eb764c4bSAlan Stern * Manufacturer and Product strings haven't changed either. 4567eb764c4bSAlan Stern * But the SerialNumber string could be different (e.g., a 4568eb764c4bSAlan Stern * different flash card of the same brand). 4569eb764c4bSAlan Stern */ 4570eb764c4bSAlan Stern if (udev->serial) 4571eb764c4bSAlan Stern serial_len = strlen(udev->serial) + 1; 4572eb764c4bSAlan Stern 4573eb764c4bSAlan Stern len = serial_len; 45741da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 4575eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 4576eb764c4bSAlan Stern len = max(len, old_length); 45771da177e4SLinus Torvalds } 4578eb764c4bSAlan Stern 45790cc1a51fSOliver Neukum buf = kmalloc(len, GFP_NOIO); 45801da177e4SLinus Torvalds if (buf == NULL) { 45811da177e4SLinus Torvalds dev_err(&udev->dev, "no mem to re-read configs after reset\n"); 45821da177e4SLinus Torvalds /* assume the worst */ 45831da177e4SLinus Torvalds return 1; 45841da177e4SLinus Torvalds } 45851da177e4SLinus Torvalds for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 4586eb764c4bSAlan Stern old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 45871da177e4SLinus Torvalds length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 45881da177e4SLinus Torvalds old_length); 4589eb764c4bSAlan Stern if (length != old_length) { 45901da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d, error %d\n", 45911da177e4SLinus Torvalds index, length); 4592eb764c4bSAlan Stern changed = 1; 45931da177e4SLinus Torvalds break; 45941da177e4SLinus Torvalds } 45951da177e4SLinus Torvalds if (memcmp (buf, udev->rawdescriptors[index], old_length) 45961da177e4SLinus Torvalds != 0) { 45971da177e4SLinus Torvalds dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 4598eb764c4bSAlan Stern index, 4599eb764c4bSAlan Stern ((struct usb_config_descriptor *) buf)-> 4600eb764c4bSAlan Stern bConfigurationValue); 4601eb764c4bSAlan Stern changed = 1; 46021da177e4SLinus Torvalds break; 46031da177e4SLinus Torvalds } 46041da177e4SLinus Torvalds } 4605eb764c4bSAlan Stern 4606eb764c4bSAlan Stern if (!changed && serial_len) { 4607eb764c4bSAlan Stern length = usb_string(udev, udev->descriptor.iSerialNumber, 4608eb764c4bSAlan Stern buf, serial_len); 4609eb764c4bSAlan Stern if (length + 1 != serial_len) { 4610eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string error %d\n", 4611eb764c4bSAlan Stern length); 4612eb764c4bSAlan Stern changed = 1; 4613eb764c4bSAlan Stern } else if (memcmp(buf, udev->serial, length) != 0) { 4614eb764c4bSAlan Stern dev_dbg(&udev->dev, "serial string changed\n"); 4615eb764c4bSAlan Stern changed = 1; 4616eb764c4bSAlan Stern } 4617eb764c4bSAlan Stern } 4618eb764c4bSAlan Stern 46191da177e4SLinus Torvalds kfree(buf); 4620eb764c4bSAlan Stern return changed; 46211da177e4SLinus Torvalds } 46221da177e4SLinus Torvalds 46231da177e4SLinus Torvalds /** 4624742120c6SMing Lei * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 46251da177e4SLinus Torvalds * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 46261da177e4SLinus Torvalds * 462779efa097SAlan Stern * WARNING - don't use this routine to reset a composite device 462879efa097SAlan Stern * (one with multiple interfaces owned by separate drivers)! 4629742120c6SMing Lei * Use usb_reset_device() instead. 46301da177e4SLinus Torvalds * 46311da177e4SLinus Torvalds * Do a port reset, reassign the device's address, and establish its 46321da177e4SLinus Torvalds * former operating configuration. If the reset fails, or the device's 46331da177e4SLinus Torvalds * descriptors change from their values before the reset, or the original 46341da177e4SLinus Torvalds * configuration and altsettings cannot be restored, a flag will be set 46351da177e4SLinus Torvalds * telling khubd to pretend the device has been disconnected and then 46361da177e4SLinus Torvalds * re-connected. All drivers will be unbound, and the device will be 46371da177e4SLinus Torvalds * re-enumerated and probed all over again. 46381da177e4SLinus Torvalds * 46391da177e4SLinus Torvalds * Returns 0 if the reset succeeded, -ENODEV if the device has been 46401da177e4SLinus Torvalds * flagged for logical disconnection, or some other negative error code 46411da177e4SLinus Torvalds * if the reset wasn't even attempted. 46421da177e4SLinus Torvalds * 46431da177e4SLinus Torvalds * The caller must own the device lock. For example, it's safe to use 46441da177e4SLinus Torvalds * this from a driver probe() routine after downloading new firmware. 46451da177e4SLinus Torvalds * For calls that might not occur during probe(), drivers should lock 46461da177e4SLinus Torvalds * the device using usb_lock_device_for_reset(). 46476bc6cff5SAlan Stern * 46486bc6cff5SAlan Stern * Locking exception: This routine may also be called from within an 46496bc6cff5SAlan Stern * autoresume handler. Such usage won't conflict with other tasks 46506bc6cff5SAlan Stern * holding the device lock because these tasks should always call 46516bc6cff5SAlan Stern * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. 46521da177e4SLinus Torvalds */ 4653742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev) 46541da177e4SLinus Torvalds { 46551da177e4SLinus Torvalds struct usb_device *parent_hdev = udev->parent; 46561da177e4SLinus Torvalds struct usb_hub *parent_hub; 46573f0479e0SSarah Sharp struct usb_hcd *hcd = bus_to_hcd(udev->bus); 46581da177e4SLinus Torvalds struct usb_device_descriptor descriptor = udev->descriptor; 465912c3da34SAlan Stern int i, ret = 0; 466012c3da34SAlan Stern int port1 = udev->portnum; 46611da177e4SLinus Torvalds 46621da177e4SLinus Torvalds if (udev->state == USB_STATE_NOTATTACHED || 46631da177e4SLinus Torvalds udev->state == USB_STATE_SUSPENDED) { 46641da177e4SLinus Torvalds dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 46651da177e4SLinus Torvalds udev->state); 46661da177e4SLinus Torvalds return -EINVAL; 46671da177e4SLinus Torvalds } 46681da177e4SLinus Torvalds 46691da177e4SLinus Torvalds if (!parent_hdev) { 46704bec9917SWolfram Sang /* this requires hcd-specific logic; see ohci_restart() */ 4671441b62c1SHarvey Harrison dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 46721da177e4SLinus Torvalds return -EISDIR; 46731da177e4SLinus Torvalds } 46741da177e4SLinus Torvalds parent_hub = hdev_to_hub(parent_hdev); 46751da177e4SLinus Torvalds 46766d1d0513SSarah Sharp /* Disable LPM while we reset the device and reinstall the alt settings. 46776d1d0513SSarah Sharp * Device-initiated LPM settings, and system exit latency settings are 46786d1d0513SSarah Sharp * cleared when the device is reset, so we have to set them up again. 46796d1d0513SSarah Sharp */ 46806d1d0513SSarah Sharp ret = usb_unlocked_disable_lpm(udev); 46816d1d0513SSarah Sharp if (ret) { 46826d1d0513SSarah Sharp dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__); 46836d1d0513SSarah Sharp goto re_enumerate; 46846d1d0513SSarah Sharp } 46856d1d0513SSarah Sharp 46861da177e4SLinus Torvalds set_bit(port1, parent_hub->busy_bits); 46871da177e4SLinus Torvalds for (i = 0; i < SET_CONFIG_TRIES; ++i) { 46881da177e4SLinus Torvalds 46891da177e4SLinus Torvalds /* ep0 maxpacket size may change; let the HCD know about it. 46901da177e4SLinus Torvalds * Other endpoints will be handled by re-enumeration. */ 4691fc721f51SInaky Perez-Gonzalez usb_ep0_reinit(udev); 46921da177e4SLinus Torvalds ret = hub_port_init(parent_hub, udev, port1, i); 4693dd4dd19eSAlan Stern if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 46941da177e4SLinus Torvalds break; 46951da177e4SLinus Torvalds } 46961da177e4SLinus Torvalds clear_bit(port1, parent_hub->busy_bits); 4697d5cbad4bSAlan Stern 46981da177e4SLinus Torvalds if (ret < 0) 46991da177e4SLinus Torvalds goto re_enumerate; 47001da177e4SLinus Torvalds 47011da177e4SLinus Torvalds /* Device might have changed firmware (DFU or similar) */ 4702eb764c4bSAlan Stern if (descriptors_changed(udev, &descriptor)) { 47031da177e4SLinus Torvalds dev_info(&udev->dev, "device firmware changed\n"); 47041da177e4SLinus Torvalds udev->descriptor = descriptor; /* for disconnect() calls */ 47051da177e4SLinus Torvalds goto re_enumerate; 47061da177e4SLinus Torvalds } 47071da177e4SLinus Torvalds 47084fe0387aSAlan Stern /* Restore the device's previous configuration */ 47091da177e4SLinus Torvalds if (!udev->actconfig) 47101da177e4SLinus Torvalds goto done; 47113f0479e0SSarah Sharp 4712d673bfcbSSarah Sharp mutex_lock(hcd->bandwidth_mutex); 47133f0479e0SSarah Sharp ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 47143f0479e0SSarah Sharp if (ret < 0) { 47153f0479e0SSarah Sharp dev_warn(&udev->dev, 47163f0479e0SSarah Sharp "Busted HC? Not enough HCD resources for " 47173f0479e0SSarah Sharp "old configuration.\n"); 4718d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 47193f0479e0SSarah Sharp goto re_enumerate; 47203f0479e0SSarah Sharp } 47211da177e4SLinus Torvalds ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 47221da177e4SLinus Torvalds USB_REQ_SET_CONFIGURATION, 0, 47231da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, 0, 47241da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 47251da177e4SLinus Torvalds if (ret < 0) { 47261da177e4SLinus Torvalds dev_err(&udev->dev, 47271da177e4SLinus Torvalds "can't restore configuration #%d (error=%d)\n", 47281da177e4SLinus Torvalds udev->actconfig->desc.bConfigurationValue, ret); 4729d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 47301da177e4SLinus Torvalds goto re_enumerate; 47311da177e4SLinus Torvalds } 4732d673bfcbSSarah Sharp mutex_unlock(hcd->bandwidth_mutex); 47331da177e4SLinus Torvalds usb_set_device_state(udev, USB_STATE_CONFIGURED); 47341da177e4SLinus Torvalds 47354fe0387aSAlan Stern /* Put interfaces back into the same altsettings as before. 47364fe0387aSAlan Stern * Don't bother to send the Set-Interface request for interfaces 47374fe0387aSAlan Stern * that were already in altsetting 0; besides being unnecessary, 47384fe0387aSAlan Stern * many devices can't handle it. Instead just reset the host-side 47394fe0387aSAlan Stern * endpoint state. 47404fe0387aSAlan Stern */ 47411da177e4SLinus Torvalds for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 47423f0479e0SSarah Sharp struct usb_host_config *config = udev->actconfig; 47433f0479e0SSarah Sharp struct usb_interface *intf = config->interface[i]; 47441da177e4SLinus Torvalds struct usb_interface_descriptor *desc; 47451da177e4SLinus Torvalds 47461da177e4SLinus Torvalds desc = &intf->cur_altsetting->desc; 47474fe0387aSAlan Stern if (desc->bAlternateSetting == 0) { 47484fe0387aSAlan Stern usb_disable_interface(udev, intf, true); 47494fe0387aSAlan Stern usb_enable_interface(udev, intf, true); 47504fe0387aSAlan Stern ret = 0; 47514fe0387aSAlan Stern } else { 475204a723eaSSarah Sharp /* Let the bandwidth allocation function know that this 475304a723eaSSarah Sharp * device has been reset, and it will have to use 475404a723eaSSarah Sharp * alternate setting 0 as the current alternate setting. 47553f0479e0SSarah Sharp */ 475604a723eaSSarah Sharp intf->resetting_device = 1; 47571da177e4SLinus Torvalds ret = usb_set_interface(udev, desc->bInterfaceNumber, 47581da177e4SLinus Torvalds desc->bAlternateSetting); 475904a723eaSSarah Sharp intf->resetting_device = 0; 47604fe0387aSAlan Stern } 47611da177e4SLinus Torvalds if (ret < 0) { 47621da177e4SLinus Torvalds dev_err(&udev->dev, "failed to restore interface %d " 47631da177e4SLinus Torvalds "altsetting %d (error=%d)\n", 47641da177e4SLinus Torvalds desc->bInterfaceNumber, 47651da177e4SLinus Torvalds desc->bAlternateSetting, 47661da177e4SLinus Torvalds ret); 47671da177e4SLinus Torvalds goto re_enumerate; 47681da177e4SLinus Torvalds } 47691da177e4SLinus Torvalds } 47701da177e4SLinus Torvalds 47716d1d0513SSarah Sharp done: 47728306095fSSarah Sharp /* Now that the alt settings are re-installed, enable LPM. */ 47738306095fSSarah Sharp usb_unlocked_enable_lpm(udev); 47741da177e4SLinus Torvalds return 0; 47751da177e4SLinus Torvalds 47761da177e4SLinus Torvalds re_enumerate: 47776d1d0513SSarah Sharp /* LPM state doesn't matter when we're about to destroy the device. */ 47781da177e4SLinus Torvalds hub_port_logical_disconnect(parent_hub, port1); 47791da177e4SLinus Torvalds return -ENODEV; 47801da177e4SLinus Torvalds } 478179efa097SAlan Stern 478279efa097SAlan Stern /** 4783742120c6SMing Lei * usb_reset_device - warn interface drivers and perform a USB port reset 478479efa097SAlan Stern * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 478579efa097SAlan Stern * 478679efa097SAlan Stern * Warns all drivers bound to registered interfaces (using their pre_reset 478779efa097SAlan Stern * method), performs the port reset, and then lets the drivers know that 478879efa097SAlan Stern * the reset is over (using their post_reset method). 478979efa097SAlan Stern * 4790742120c6SMing Lei * Return value is the same as for usb_reset_and_verify_device(). 479179efa097SAlan Stern * 479279efa097SAlan Stern * The caller must own the device lock. For example, it's safe to use 479379efa097SAlan Stern * this from a driver probe() routine after downloading new firmware. 479479efa097SAlan Stern * For calls that might not occur during probe(), drivers should lock 479579efa097SAlan Stern * the device using usb_lock_device_for_reset(). 479678d9a487SAlan Stern * 479778d9a487SAlan Stern * If an interface is currently being probed or disconnected, we assume 479878d9a487SAlan Stern * its driver knows how to handle resets. For all other interfaces, 479978d9a487SAlan Stern * if the driver doesn't have pre_reset and post_reset methods then 480078d9a487SAlan Stern * we attempt to unbind it and rebind afterward. 480179efa097SAlan Stern */ 4802742120c6SMing Lei int usb_reset_device(struct usb_device *udev) 480379efa097SAlan Stern { 480479efa097SAlan Stern int ret; 4805852c4b43SAlan Stern int i; 480679efa097SAlan Stern struct usb_host_config *config = udev->actconfig; 480779efa097SAlan Stern 480879efa097SAlan Stern if (udev->state == USB_STATE_NOTATTACHED || 480979efa097SAlan Stern udev->state == USB_STATE_SUSPENDED) { 481079efa097SAlan Stern dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 481179efa097SAlan Stern udev->state); 481279efa097SAlan Stern return -EINVAL; 481379efa097SAlan Stern } 481479efa097SAlan Stern 4815645daaabSAlan Stern /* Prevent autosuspend during the reset */ 481694fcda1fSAlan Stern usb_autoresume_device(udev); 4817645daaabSAlan Stern 481879efa097SAlan Stern if (config) { 4819852c4b43SAlan Stern for (i = 0; i < config->desc.bNumInterfaces; ++i) { 4820852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 482179efa097SAlan Stern struct usb_driver *drv; 482278d9a487SAlan Stern int unbind = 0; 482379efa097SAlan Stern 4824852c4b43SAlan Stern if (cintf->dev.driver) { 482579efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 482678d9a487SAlan Stern if (drv->pre_reset && drv->post_reset) 482778d9a487SAlan Stern unbind = (drv->pre_reset)(cintf); 482878d9a487SAlan Stern else if (cintf->condition == 482978d9a487SAlan Stern USB_INTERFACE_BOUND) 483078d9a487SAlan Stern unbind = 1; 483178d9a487SAlan Stern if (unbind) 483278d9a487SAlan Stern usb_forced_unbind_intf(cintf); 483379efa097SAlan Stern } 483479efa097SAlan Stern } 483579efa097SAlan Stern } 483679efa097SAlan Stern 4837742120c6SMing Lei ret = usb_reset_and_verify_device(udev); 483879efa097SAlan Stern 483979efa097SAlan Stern if (config) { 4840852c4b43SAlan Stern for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 4841852c4b43SAlan Stern struct usb_interface *cintf = config->interface[i]; 484279efa097SAlan Stern struct usb_driver *drv; 484378d9a487SAlan Stern int rebind = cintf->needs_binding; 484479efa097SAlan Stern 484578d9a487SAlan Stern if (!rebind && cintf->dev.driver) { 484679efa097SAlan Stern drv = to_usb_driver(cintf->dev.driver); 484779efa097SAlan Stern if (drv->post_reset) 484878d9a487SAlan Stern rebind = (drv->post_reset)(cintf); 484978d9a487SAlan Stern else if (cintf->condition == 485078d9a487SAlan Stern USB_INTERFACE_BOUND) 485178d9a487SAlan Stern rebind = 1; 485279efa097SAlan Stern } 48536c640945SAlan Stern if (ret == 0 && rebind) 485478d9a487SAlan Stern usb_rebind_intf(cintf); 485579efa097SAlan Stern } 485679efa097SAlan Stern } 485779efa097SAlan Stern 485894fcda1fSAlan Stern usb_autosuspend_device(udev); 485979efa097SAlan Stern return ret; 486079efa097SAlan Stern } 4861742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device); 4862dc023dceSInaky Perez-Gonzalez 4863dc023dceSInaky Perez-Gonzalez 4864dc023dceSInaky Perez-Gonzalez /** 4865dc023dceSInaky Perez-Gonzalez * usb_queue_reset_device - Reset a USB device from an atomic context 4866dc023dceSInaky Perez-Gonzalez * @iface: USB interface belonging to the device to reset 4867dc023dceSInaky Perez-Gonzalez * 4868dc023dceSInaky Perez-Gonzalez * This function can be used to reset a USB device from an atomic 4869dc023dceSInaky Perez-Gonzalez * context, where usb_reset_device() won't work (as it blocks). 4870dc023dceSInaky Perez-Gonzalez * 4871dc023dceSInaky Perez-Gonzalez * Doing a reset via this method is functionally equivalent to calling 4872dc023dceSInaky Perez-Gonzalez * usb_reset_device(), except for the fact that it is delayed to a 4873dc023dceSInaky Perez-Gonzalez * workqueue. This means that any drivers bound to other interfaces 4874dc023dceSInaky Perez-Gonzalez * might be unbound, as well as users from usbfs in user space. 4875dc023dceSInaky Perez-Gonzalez * 4876dc023dceSInaky Perez-Gonzalez * Corner cases: 4877dc023dceSInaky Perez-Gonzalez * 4878dc023dceSInaky Perez-Gonzalez * - Scheduling two resets at the same time from two different drivers 4879dc023dceSInaky Perez-Gonzalez * attached to two different interfaces of the same device is 4880dc023dceSInaky Perez-Gonzalez * possible; depending on how the driver attached to each interface 4881dc023dceSInaky Perez-Gonzalez * handles ->pre_reset(), the second reset might happen or not. 4882dc023dceSInaky Perez-Gonzalez * 4883dc023dceSInaky Perez-Gonzalez * - If a driver is unbound and it had a pending reset, the reset will 4884dc023dceSInaky Perez-Gonzalez * be cancelled. 4885dc023dceSInaky Perez-Gonzalez * 4886dc023dceSInaky Perez-Gonzalez * - This function can be called during .probe() or .disconnect() 4887dc023dceSInaky Perez-Gonzalez * times. On return from .disconnect(), any pending resets will be 4888dc023dceSInaky Perez-Gonzalez * cancelled. 4889dc023dceSInaky Perez-Gonzalez * 4890dc023dceSInaky Perez-Gonzalez * There is no no need to lock/unlock the @reset_ws as schedule_work() 4891dc023dceSInaky Perez-Gonzalez * does its own. 4892dc023dceSInaky Perez-Gonzalez * 4893dc023dceSInaky Perez-Gonzalez * NOTE: We don't do any reference count tracking because it is not 4894dc023dceSInaky Perez-Gonzalez * needed. The lifecycle of the work_struct is tied to the 4895dc023dceSInaky Perez-Gonzalez * usb_interface. Before destroying the interface we cancel the 4896dc023dceSInaky Perez-Gonzalez * work_struct, so the fact that work_struct is queued and or 4897dc023dceSInaky Perez-Gonzalez * running means the interface (and thus, the device) exist and 4898dc023dceSInaky Perez-Gonzalez * are referenced. 4899dc023dceSInaky Perez-Gonzalez */ 4900dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface) 4901dc023dceSInaky Perez-Gonzalez { 4902dc023dceSInaky Perez-Gonzalez schedule_work(&iface->reset_ws); 4903dc023dceSInaky Perez-Gonzalez } 4904dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device); 4905